diff --git a/Doc/library/mmap.rst b/Doc/library/mmap.rst index d9d401a2789c0e..9c0b828fbd4d28 100644 --- a/Doc/library/mmap.rst +++ b/Doc/library/mmap.rst @@ -324,10 +324,10 @@ To map anonymous memory, -1 should be passed as the fileno along with the length Return the length of the file, which can be larger than the size of the memory-mapped area. - For anonymous mapping, return its size. + For an anonymous mapping, return its size. .. versionchanged:: next - Supports anonymous mapping on Unix. + Anonymous mappings are now supported on Unix. .. method:: tell() diff --git a/Doc/library/os.rst b/Doc/library/os.rst index 45ec6c7a51b7b0..0333fe9f9967f8 100644 --- a/Doc/library/os.rst +++ b/Doc/library/os.rst @@ -3757,9 +3757,9 @@ features: import os for root, dirs, files, rootfd in os.fwalk('python/Lib/xml'): - print(root, "consumes", end="") + print(root, "consumes", end=" ") print(sum([os.stat(name, dir_fd=rootfd).st_size for name in files]), - end="") + end=" ") print("bytes in", len(files), "non-directory files") if '__pycache__' in dirs: dirs.remove('__pycache__') # don't visit __pycache__ directories diff --git a/Doc/library/random.rst b/Doc/library/random.rst index b1120b3a4d8eb4..e9cebf46d57b01 100644 --- a/Doc/library/random.rst +++ b/Doc/library/random.rst @@ -630,7 +630,8 @@ Recipes ------- These recipes show how to efficiently make random selections -from the combinatoric iterators in the :mod:`itertools` module: +from the combinatoric iterators in the :mod:`itertools` module +or the :pypi:`more-itertools` project: .. testcode:: import random @@ -661,6 +662,17 @@ from the combinatoric iterators in the :mod:`itertools` module: indices = sorted(random.choices(range(n), k=r)) return tuple(pool[i] for i in indices) + def random_derangement(iterable): + "Choose a permutation where no element is in its original position." + seq = tuple(iterable) + if len(seq) < 2: + raise ValueError('derangments require at least two values') + perm = list(seq) + while True: + random.shuffle(perm) + if all(p != q for p, q in zip(seq, perm)): + return tuple(perm) + The default :func:`.random` returns multiples of 2⁻⁵³ in the range *0.0 ≤ x < 1.0*. All such numbers are evenly spaced and are exactly representable as Python floats. However, many other representable diff --git a/Lib/test/test_interpreters/test_api.py b/Lib/test/test_interpreters/test_api.py index a34b20beaca7a3..289e607ad3fad3 100644 --- a/Lib/test/test_interpreters/test_api.py +++ b/Lib/test/test_interpreters/test_api.py @@ -2204,6 +2204,16 @@ def test_whence(self): whence = eval(text) self.assertEqual(whence, _interpreters.WHENCE_LEGACY_CAPI) + def test_contextvars_missing(self): + script = f""" + import contextvars + print(getattr(contextvars.Token, "MISSING", "'doesn't exist'")) + """ + + orig = _interpreters.create() + text = self.run_and_capture(orig, script) + self.assertEqual(text.strip(), "") + def test_is_running(self): def check(interpid, expected): with self.assertRaisesRegex(InterpreterError, 'unrecognized'): diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-07-13-21-21-17.gh-issue-136599.sLhm2O.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-07-13-21-21-17.gh-issue-136599.sLhm2O.rst new file mode 100644 index 00000000000000..9bcb13f9e20caf --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-07-13-21-21-17.gh-issue-136599.sLhm2O.rst @@ -0,0 +1 @@ +Improve performance of :class:`int` hash calculations. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-09-05-01-19-04.gh-issue-138192.erluq5.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-09-05-01-19-04.gh-issue-138192.erluq5.rst new file mode 100644 index 00000000000000..05fa8edacd680a --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-09-05-01-19-04.gh-issue-138192.erluq5.rst @@ -0,0 +1,2 @@ +Fix :mod:`contextvars` initialization so that all subinterpreters are assigned the +:attr:`~contextvars.Token.MISSING` value. diff --git a/Misc/NEWS.d/next/Library/2021-03-07-16-31-36.bpo-43429.Koa0mf.rst b/Misc/NEWS.d/next/Library/2021-03-07-16-31-36.bpo-43429.Koa0mf.rst index a6221ba9c88585..0919e2c6ae1d8a 100644 --- a/Misc/NEWS.d/next/Library/2021-03-07-16-31-36.bpo-43429.Koa0mf.rst +++ b/Misc/NEWS.d/next/Library/2021-03-07-16-31-36.bpo-43429.Koa0mf.rst @@ -1,5 +1,5 @@ The :meth:`~mmap.mmap.size` method of the :class:`mmap.mmap` class now returns the size of an anonymous mapping on both Unix and Windows. Previously, the size would be returned on Windows and an :exc:`OSError` -would be raised on Unix. -Raise :exc:`ValueError` instead of :exc:`OSError` with ``trackfd=False``. +would be raised on Unix. :exc:`ValueError` is now raised instead of +:exc:`OSError` when ``trackfd=False``. diff --git a/Objects/longobject.c b/Objects/longobject.c index b612eabaab480d..287458ba2da62a 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -3676,7 +3676,23 @@ long_hash(PyObject *obj) } i = _PyLong_DigitCount(v); sign = _PyLong_NonCompactSign(v); - x = 0; + + // unroll first digit + Py_BUILD_ASSERT(PyHASH_BITS > PyLong_SHIFT); + assert(i >= 1); + --i; + x = v->long_value.ob_digit[i]; + assert(x < PyHASH_MODULUS); + +#if PyHASH_BITS >= 2 * PyLong_SHIFT + // unroll second digit + assert(i >= 1); + --i; + x <<= PyLong_SHIFT; + x += v->long_value.ob_digit[i]; + assert(x < PyHASH_MODULUS); +#endif + while (--i >= 0) { /* Here x is a quantity in the range [0, _PyHASH_MODULUS); we want to compute x * 2**PyLong_SHIFT + v->long_value.ob_digit[i] modulo diff --git a/Python/context.c b/Python/context.c index b764f5813fac79..2f978b1c0abc43 100644 --- a/Python/context.c +++ b/Python/context.c @@ -1360,11 +1360,8 @@ get_token_missing(void) PyStatus _PyContext_Init(PyInterpreterState *interp) { - if (!_Py_IsMainInterpreter(interp)) { - return _PyStatus_OK(); - } - PyObject *missing = get_token_missing(); + assert(PyUnstable_IsImmortal(missing)); if (PyDict_SetItemString( _PyType_GetDict(&PyContextToken_Type), "MISSING", missing)) {