Skip to content

Commit

Permalink
Merge branch 'main' into urls2
Browse files Browse the repository at this point in the history
  • Loading branch information
serhiy-storchaka committed Dec 29, 2023
2 parents fb03e5f + cf34b77 commit 26951a7
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 66 deletions.
3 changes: 3 additions & 0 deletions Doc/library/os.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4986,6 +4986,9 @@ written in Python, such as a mail server's external command delivery program.

.. availability:: Unix, not Emscripten, not WASI.

.. note::
This function is not available on macOS.

.. versionadded:: 3.3


Expand Down
59 changes: 44 additions & 15 deletions Doc/library/random.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ instance of the :class:`random.Random` class. You can instantiate your own
instances of :class:`Random` to get generators that don't share state.

Class :class:`Random` can also be subclassed if you want to use a different
basic generator of your own devising: in that case, override the :meth:`~Random.random`,
:meth:`~Random.seed`, :meth:`~Random.getstate`, and :meth:`~Random.setstate` methods.
Optionally, a new generator can supply a :meth:`~Random.getrandbits` method --- this
allows :meth:`randrange` to produce selections over an arbitrarily large range.
basic generator of your own devising: see the documentation on that class for
more details.

The :mod:`random` module also provides the :class:`SystemRandom` class which
uses the system function :func:`os.urandom` to generate random numbers
Expand Down Expand Up @@ -88,7 +86,7 @@ Bookkeeping functions

.. versionchanged:: 3.11
The *seed* must be one of the following types:
*NoneType*, :class:`int`, :class:`float`, :class:`str`,
``None``, :class:`int`, :class:`float`, :class:`str`,
:class:`bytes`, or :class:`bytearray`.

.. function:: getstate()
Expand Down Expand Up @@ -412,6 +410,37 @@ Alternative Generator
``None``, :class:`int`, :class:`float`, :class:`str`,
:class:`bytes`, or :class:`bytearray`.

Subclasses of :class:`!Random` should override the following methods if they
wish to make use of a different basic generator:

.. method:: Random.seed(a=None, version=2)

Override this method in subclasses to customise the :meth:`~random.seed`
behaviour of :class:`!Random` instances.

.. method:: Random.getstate()

Override this method in subclasses to customise the :meth:`~random.getstate`
behaviour of :class:`!Random` instances.

.. method:: Random.setstate(state)

Override this method in subclasses to customise the :meth:`~random.setstate`
behaviour of :class:`!Random` instances.

.. method:: Random.random()

Override this method in subclasses to customise the :meth:`~random.random`
behaviour of :class:`!Random` instances.

Optionally, a custom generator subclass can also supply the following method:

.. method:: Random.getrandbits(k)

Override this method in subclasses to customise the
:meth:`~random.getrandbits` behaviour of :class:`!Random` instances.


.. class:: SystemRandom([seed])

Class that uses the :func:`os.urandom` function for generating random numbers
Expand Down Expand Up @@ -445,30 +474,30 @@ Examples

Basic examples::

>>> random() # Random float: 0.0 <= x < 1.0
>>> random() # Random float: 0.0 <= x < 1.0
0.37444887175646646

>>> uniform(2.5, 10.0) # Random float: 2.5 <= x <= 10.0
>>> uniform(2.5, 10.0) # Random float: 2.5 <= x <= 10.0
3.1800146073117523

>>> expovariate(1 / 5) # Interval between arrivals averaging 5 seconds
>>> expovariate(1 / 5) # Interval between arrivals averaging 5 seconds
5.148957571865031

>>> randrange(10) # Integer from 0 to 9 inclusive
>>> randrange(10) # Integer from 0 to 9 inclusive
7

>>> randrange(0, 101, 2) # Even integer from 0 to 100 inclusive
>>> randrange(0, 101, 2) # Even integer from 0 to 100 inclusive
26

>>> choice(['win', 'lose', 'draw']) # Single random element from a sequence
>>> choice(['win', 'lose', 'draw']) # Single random element from a sequence
'draw'

>>> deck = 'ace two three four'.split()
>>> shuffle(deck) # Shuffle a list
>>> shuffle(deck) # Shuffle a list
>>> deck
['four', 'two', 'ace', 'three']

>>> sample([10, 20, 30, 40, 50], k=4) # Four samples without replacement
>>> sample([10, 20, 30, 40, 50], k=4) # Four samples without replacement
[40, 10, 50, 30]

Simulations::
Expand Down Expand Up @@ -572,14 +601,14 @@ Simulation of arrival times and service deliveries for a multiserver queue::
including simulation, sampling, shuffling, and cross-validation.

`Economics Simulation
<https://nbviewer.jupyter.org/url/norvig.com/ipython/Economics.ipynb>`_
<https://nbviewer.org/url/norvig.com/ipython/Economics.ipynb>`_
a simulation of a marketplace by
`Peter Norvig <https://norvig.com/bio.html>`_ that shows effective
use of many of the tools and distributions provided by this module
(gauss, uniform, sample, betavariate, choice, triangular, and randrange).

`A Concrete Introduction to Probability (using Python)
<https://nbviewer.jupyter.org/url/norvig.com/ipython/Probability.ipynb>`_
<https://nbviewer.org/url/norvig.com/ipython/Probability.ipynb>`_
a tutorial by `Peter Norvig <https://norvig.com/bio.html>`_ covering
the basics of probability theory, how to write simulations, and
how to perform data analysis using Python.
Expand Down
1 change: 0 additions & 1 deletion Doc/tools/.nitignore
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ Doc/library/profile.rst
Doc/library/pyclbr.rst
Doc/library/pydoc.rst
Doc/library/pyexpat.rst
Doc/library/random.rst
Doc/library/readline.rst
Doc/library/resource.rst
Doc/library/select.rst
Expand Down
4 changes: 4 additions & 0 deletions Lib/pathlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,10 @@ def iterdir(self):
def _scandir(self):
return os.scandir(self)

def _make_child_entry(self, entry):
# Transform an entry yielded from _scandir() into a path object.
return self._make_child_relpath(entry.name)

def absolute(self):
"""Return an absolute version of this path
No normalization or symlink resolution is performed.
Expand Down
13 changes: 8 additions & 5 deletions Lib/pathlib/_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,8 @@ def _select_children(parent_paths, dir_only, follow_symlinks, match):
continue
except OSError:
continue
name = entry.name
if match(name):
yield parent_path._make_child_relpath(name)
if match(entry.name):
yield parent_path._make_child_entry(entry)


def _select_recursive(parent_paths, dir_only, follow_symlinks):
Expand All @@ -112,12 +111,12 @@ def _select_recursive(parent_paths, dir_only, follow_symlinks):
for entry in entries:
try:
if entry.is_dir(follow_symlinks=follow_symlinks):
paths.append(path._make_child_relpath(entry.name))
paths.append(path._make_child_entry(entry))
continue
except OSError:
pass
if not dir_only:
yield path._make_child_relpath(entry.name)
yield path._make_child_entry(entry)


def _select_unique(paths):
Expand Down Expand Up @@ -788,6 +787,10 @@ def _scandir(self):
from contextlib import nullcontext
return nullcontext(self.iterdir())

def _make_child_entry(self, entry):
# Transform an entry yielded from _scandir() into a path object.
return entry

def _make_child_relpath(self, name):
path_str = str(self)
tail = self._tail
Expand Down
43 changes: 43 additions & 0 deletions Lib/test/test_pathlib/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from test.support import import_helper
from test.support import is_emscripten, is_wasi
from test.support import set_recursion_limit
from test.support import os_helper
from test.support.os_helper import TESTFN, FakePath
from test.test_pathlib import test_pathlib_abc
Expand Down Expand Up @@ -1660,6 +1661,48 @@ def test_walk_many_open_files(self):
self.assertEqual(next(it), expected)
path = path / 'd'

def test_walk_above_recursion_limit(self):
recursion_limit = 40
# directory_depth > recursion_limit
directory_depth = recursion_limit + 10
base = self.cls(self.base, 'deep')
path = base.joinpath(*(['d'] * directory_depth))
path.mkdir(parents=True)

with set_recursion_limit(recursion_limit):
list(base.walk())
list(base.walk(top_down=False))

def test_glob_many_open_files(self):
depth = 30
P = self.cls
p = base = P(self.base) / 'deep'
p.mkdir()
for _ in range(depth):
p /= 'd'
p.mkdir()
pattern = '/'.join(['*'] * depth)
iters = [base.glob(pattern) for j in range(100)]
for it in iters:
self.assertEqual(next(it), p)
iters = [base.rglob('d') for j in range(100)]
p = base
for i in range(depth):
p = p / 'd'
for it in iters:
self.assertEqual(next(it), p)

def test_glob_above_recursion_limit(self):
recursion_limit = 50
# directory_depth > recursion_limit
directory_depth = recursion_limit + 10
base = self.cls(self.base, 'deep')
path = base.joinpath(*(['d'] * directory_depth))
path.mkdir(parents=True)

with set_recursion_limit(recursion_limit):
list(base.glob('**/'))


@only_posix
class PosixPathTest(PathTest, PurePosixPathTest):
Expand Down
42 changes: 0 additions & 42 deletions Lib/test/test_pathlib/test_pathlib_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from pathlib._abc import UnsupportedOperation, PurePathBase, PathBase
import posixpath

from test.support import set_recursion_limit
from test.support.os_helper import TESTFN


Expand Down Expand Up @@ -1224,25 +1223,6 @@ def test_rglob_symlink_loop(self):
}
self.assertEqual(given, {p / x for x in expect})

def test_glob_many_open_files(self):
depth = 30
P = self.cls
p = base = P(self.base) / 'deep'
p.mkdir()
for _ in range(depth):
p /= 'd'
p.mkdir()
pattern = '/'.join(['*'] * depth)
iters = [base.glob(pattern) for j in range(100)]
for it in iters:
self.assertEqual(next(it), p)
iters = [base.rglob('d') for j in range(100)]
p = base
for i in range(depth):
p = p / 'd'
for it in iters:
self.assertEqual(next(it), p)

def test_glob_dotdot(self):
# ".." is not special in globs.
P = self.cls
Expand Down Expand Up @@ -1286,17 +1266,6 @@ def test_glob_long_symlink(self):
bad_link.symlink_to("bad" * 200)
self.assertEqual(sorted(base.glob('**/*')), [bad_link])

def test_glob_above_recursion_limit(self):
recursion_limit = 50
# directory_depth > recursion_limit
directory_depth = recursion_limit + 10
base = self.cls(self.base, 'deep')
path = base.joinpath(*(['d'] * directory_depth))
path.mkdir(parents=True)

with set_recursion_limit(recursion_limit):
list(base.glob('**/'))

def test_glob_recursive_no_trailing_slash(self):
P = self.cls
p = P(self.base)
Expand Down Expand Up @@ -1825,17 +1794,6 @@ def test_walk_symlink_location(self):
else:
self.fail("symlink not found")

def test_walk_above_recursion_limit(self):
recursion_limit = 40
# directory_depth > recursion_limit
directory_depth = recursion_limit + 10
base = self.cls(self.base, 'deep')
path = base.joinpath(*(['d'] * directory_depth))
path.mkdir(parents=True)

with set_recursion_limit(recursion_limit):
list(base.walk())
list(base.walk(top_down=False))

class DummyPathWithSymlinks(DummyPath):
def readlink(self):
Expand Down
1 change: 1 addition & 0 deletions Lib/webbrowser.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ def __init__(self, name='default'):
super().__init__(name)

def open(self, url, new=0, autoraise=True):
sys.audit("webbrowser.open", url)
if self.name == 'default':
script = 'open location "%s"' % url.replace('"', '%22') # opens in default browser
else:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Make sure that ``webbrowser.MacOSXOSAScript`` sends ``webbrowser.open``
audit event.
4 changes: 1 addition & 3 deletions Modules/pyexpat.c
Original file line number Diff line number Diff line change
Expand Up @@ -2062,9 +2062,7 @@ pyexpat_free(void *module)

static PyModuleDef_Slot pyexpat_slots[] = {
{Py_mod_exec, pyexpat_exec},
// XXX gh-103092: fix isolation.
{Py_mod_multiple_interpreters, Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED},
//{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
{0, NULL}
};

Expand Down

0 comments on commit 26951a7

Please sign in to comment.