Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Doc/library/mmap.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions Doc/library/os.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion Doc/library/random.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_interpreters/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(), "<Token.MISSING>")

def test_is_running(self):
def check(interpid, expected):
with self.assertRaisesRegex(InterpreterError, 'unrecognized'):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve performance of :class:`int` hash calculations.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :mod:`contextvars` initialization so that all subinterpreters are assigned the
:attr:`~contextvars.Token.MISSING` value.
Original file line number Diff line number Diff line change
@@ -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``.
18 changes: 17 additions & 1 deletion Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions Python/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down
Loading