Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 49632d9

Browse files
authored
Merge branch 'main' into main
2 parents b326048 + 447d061 commit 49632d9

20 files changed

Lines changed: 305 additions & 98 deletions

Doc/library/fractions.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ another rational number, or from a string.
117117

118118
.. versionadded:: 3.8
119119

120+
.. method:: is_integer()
121+
122+
Return ``True`` if the Fraction is an integer.
123+
124+
.. versionadded:: 3.12
125+
120126
.. classmethod:: from_float(flt)
121127

122128
Alternative constructor which only accepts instances of

Doc/library/functions.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ are always available. They are listed here in alphabetical order.
513513

514514
.. _func-eval:
515515

516-
.. function:: eval(expression, /, globals=None, locals=None)
516+
.. function:: eval(expression, globals=None, locals=None)
517517

518518
The arguments are a string and optional globals and locals. If provided,
519519
*globals* must be a dictionary. If provided, *locals* can be any mapping

Doc/library/importlib.resources.rst

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ This module leverages Python's import system to provide access to *resources*
1414
within *packages*.
1515

1616
"Resources" are file-like resources associated with a module or package in
17-
Python. The resources may be contained directly in a package or within a
18-
subdirectory contained in that package. Resources may be text or binary. As a
19-
result, Python module sources (.py) of a package and compilation artifacts
20-
(pycache) are technically de-facto resources of that package. In practice,
21-
however, resources are primarily those non-Python artifacts exposed
22-
specifically by the package author.
17+
Python. The resources may be contained directly in a package, within a
18+
subdirectory contained in that package, or adjacent to modules outside a
19+
package. Resources may be text or binary. As a result, Python module sources
20+
(.py) of a package and compilation artifacts (pycache) are technically
21+
de-facto resources of that package. In practice, however, resources are
22+
primarily those non-Python artifacts exposed specifically by the package
23+
author.
2324

2425
Resources can be opened or read in either binary or text mode.
2526

@@ -49,27 +50,35 @@ for example, a package and its resources can be imported from a zip file using
4950
``get_resource_reader(fullname)`` method as specified by
5051
:class:`importlib.resources.abc.ResourceReader`.
5152

52-
.. data:: Package
53+
.. data:: Anchor
5354

54-
Whenever a function accepts a ``Package`` argument, you can pass in
55-
either a :class:`module object <types.ModuleType>` or a module name
56-
as a string. You can only pass module objects whose
57-
``__spec__.submodule_search_locations`` is not ``None``.
55+
Represents an anchor for resources, either a :class:`module object
56+
<types.ModuleType>` or a module name as a string. Defined as
57+
``Union[str, ModuleType]``.
5858

59-
The ``Package`` type is defined as ``Union[str, ModuleType]``.
60-
61-
.. function:: files(package)
59+
.. function:: files(anchor: Optional[Anchor] = None)
6260

6361
Returns a :class:`~importlib.resources.abc.Traversable` object
64-
representing the resource container for the package (think directory)
65-
and its resources (think files). A Traversable may contain other
66-
containers (think subdirectories).
62+
representing the resource container (think directory) and its resources
63+
(think files). A Traversable may contain other containers (think
64+
subdirectories).
6765

68-
*package* is either a name or a module object which conforms to the
69-
:data:`Package` requirements.
66+
*anchor* is an optional :data:`Anchor`. If the anchor is a
67+
package, resources are resolved from that package. If a module,
68+
resources are resolved adjacent to that module (in the same package
69+
or the package root). If the anchor is omitted, the caller's module
70+
is used.
7071

7172
.. versionadded:: 3.9
7273

74+
.. versionchanged:: 3.12
75+
"package" parameter was renamed to "anchor". "anchor" can now
76+
be a non-package module and if omitted will default to the caller's
77+
module. "package" is still accepted for compatibility but will raise
78+
a DeprecationWarning. Consider passing the anchor positionally or
79+
using ``importlib_resources >= 5.10`` for a compatible interface
80+
on older Pythons.
81+
7382
.. function:: as_file(traversable)
7483

7584
Given a :class:`~importlib.resources.abc.Traversable` object representing
@@ -86,6 +95,7 @@ for example, a package and its resources can be imported from a zip file using
8695

8796
.. versionadded:: 3.9
8897

98+
8999
Deprecated functions
90100
--------------------
91101

@@ -94,6 +104,18 @@ scheduled for removal in a future version of Python.
94104
The main drawback of these functions is that they do not support
95105
directories: they assume all resources are located directly within a *package*.
96106

107+
.. data:: Package
108+
109+
Whenever a function accepts a ``Package`` argument, you can pass in
110+
either a :class:`module object <types.ModuleType>` or a module name
111+
as a string. You can only pass module objects whose
112+
``__spec__.submodule_search_locations`` is not ``None``.
113+
114+
The ``Package`` type is defined as ``Union[str, ModuleType]``.
115+
116+
.. deprecated:: 3.12
117+
118+
97119
.. data:: Resource
98120

99121
For *resource* arguments of the functions below, you can pass in
@@ -102,6 +124,7 @@ directories: they assume all resources are located directly within a *package*.
102124

103125
The ``Resource`` type is defined as ``Union[str, os.PathLike]``.
104126

127+
105128
.. function:: open_binary(package, resource)
106129

107130
Open for binary reading the *resource* within *package*.

Doc/library/socket.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -803,8 +803,8 @@ The following functions all create :ref:`socket objects <socket-objects>`.
803803
``(host, port)``) and returns the socket object.
804804

805805
*family* should be either :data:`AF_INET` or :data:`AF_INET6`.
806-
*backlog* is the queue size passed to :meth:`socket.listen`; when ``0``
807-
a default reasonable value is chosen.
806+
*backlog* is the queue size passed to :meth:`socket.listen`; if not specified
807+
, a default reasonable value is chosen.
808808
*reuse_port* dictates whether to set the :data:`SO_REUSEPORT` socket option.
809809

810810
If *dualstack_ipv6* is true and the platform supports it the socket will

Doc/reference/expressions.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,9 @@ generator. That generator then controls the execution of the generator
454454
function. The execution starts when one of the generator's methods is called.
455455
At that time, the execution proceeds to the first yield expression, where it is
456456
suspended again, returning the value of :token:`~python-grammar:expression_list`
457-
to the generator's caller. By suspended, we mean that all local state is
457+
to the generator's caller,
458+
or ``None`` if :token:`~python-grammer:expression_list` is omitted.
459+
By suspended, we mean that all local state is
458460
retained, including the current bindings of local variables, the instruction
459461
pointer, the internal evaluation stack, and the state of any exception handling.
460462
When the execution is resumed by calling one of the generator's methods, the

Lib/fractions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,10 @@ def from_decimal(cls, dec):
225225
(cls.__name__, dec, type(dec).__name__))
226226
return cls(*dec.as_integer_ratio())
227227

228+
def is_integer(self):
229+
"""Return True if the Fraction is an integer."""
230+
return self._denominator == 1
231+
228232
def as_integer_ratio(self):
229233
"""Return the integer ratio as a tuple.
230234

Lib/idlelib/idle_test/test_browser.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,7 @@ def test_ondoubleclick(self, fopen):
170170

171171
with mock.patch('os.path.exists', return_value=True):
172172
mbt.OnDoubleClick()
173-
fopen.assert_called()
174-
fopen.called_with(fname)
173+
fopen.assert_called_once_with(fname)
175174

176175

177176
class ChildBrowserTreeItemTest(unittest.TestCase):

Lib/importlib/resources/_common.py

Lines changed: 67 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,58 @@
55
import contextlib
66
import types
77
import importlib
8+
import inspect
9+
import warnings
10+
import itertools
811

9-
from typing import Union, Optional
12+
from typing import Union, Optional, cast
1013
from .abc import ResourceReader, Traversable
1114

1215
from ._adapters import wrap_spec
1316

1417
Package = Union[types.ModuleType, str]
18+
Anchor = Package
1519

1620

17-
def files(package):
18-
# type: (Package) -> Traversable
21+
def package_to_anchor(func):
1922
"""
20-
Get a Traversable resource from a package
23+
Replace 'package' parameter as 'anchor' and warn about the change.
24+
25+
Other errors should fall through.
26+
27+
>>> files('a', 'b')
28+
Traceback (most recent call last):
29+
TypeError: files() takes from 0 to 1 positional arguments but 2 were given
30+
"""
31+
undefined = object()
32+
33+
@functools.wraps(func)
34+
def wrapper(anchor=undefined, package=undefined):
35+
if package is not undefined:
36+
if anchor is not undefined:
37+
return func(anchor, package)
38+
warnings.warn(
39+
"First parameter to files is renamed to 'anchor'",
40+
DeprecationWarning,
41+
stacklevel=2,
42+
)
43+
return func(package)
44+
elif anchor is undefined:
45+
return func()
46+
return func(anchor)
47+
48+
return wrapper
49+
50+
51+
@package_to_anchor
52+
def files(anchor: Optional[Anchor] = None) -> Traversable:
53+
"""
54+
Get a Traversable resource for an anchor.
2155
"""
22-
return from_package(get_package(package))
56+
return from_package(resolve(anchor))
2357

2458

25-
def get_resource_reader(package):
26-
# type: (types.ModuleType) -> Optional[ResourceReader]
59+
def get_resource_reader(package: types.ModuleType) -> Optional[ResourceReader]:
2760
"""
2861
Return the package's loader if it's a ResourceReader.
2962
"""
@@ -39,24 +72,39 @@ def get_resource_reader(package):
3972
return reader(spec.name) # type: ignore
4073

4174

42-
def resolve(cand):
43-
# type: (Package) -> types.ModuleType
44-
return cand if isinstance(cand, types.ModuleType) else importlib.import_module(cand)
75+
@functools.singledispatch
76+
def resolve(cand: Optional[Anchor]) -> types.ModuleType:
77+
return cast(types.ModuleType, cand)
78+
79+
80+
@resolve.register
81+
def _(cand: str) -> types.ModuleType:
82+
return importlib.import_module(cand)
83+
4584

85+
@resolve.register
86+
def _(cand: None) -> types.ModuleType:
87+
return resolve(_infer_caller().f_globals['__name__'])
4688

47-
def get_package(package):
48-
# type: (Package) -> types.ModuleType
49-
"""Take a package name or module object and return the module.
5089

51-
Raise an exception if the resolved module is not a package.
90+
def _infer_caller():
5291
"""
53-
resolved = resolve(package)
54-
if wrap_spec(resolved).submodule_search_locations is None:
55-
raise TypeError(f'{package!r} is not a package')
56-
return resolved
92+
Walk the stack and find the frame of the first caller not in this module.
93+
"""
94+
95+
def is_this_file(frame_info):
96+
return frame_info.filename == __file__
97+
98+
def is_wrapper(frame_info):
99+
return frame_info.function == 'wrapper'
100+
101+
not_this_file = itertools.filterfalse(is_this_file, inspect.stack())
102+
# also exclude 'wrapper' due to singledispatch in the call stack
103+
callers = itertools.filterfalse(is_wrapper, not_this_file)
104+
return next(callers).frame
57105

58106

59-
def from_package(package):
107+
def from_package(package: types.ModuleType):
60108
"""
61109
Return a Traversable object for the given package.
62110

Lib/importlib/resources/_legacy.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ def wrapper(*args, **kwargs):
2727
return wrapper
2828

2929

30-
def normalize_path(path):
31-
# type: (Any) -> str
30+
def normalize_path(path: Any) -> str:
3231
"""Normalize a path by ensuring it is a string.
3332
3433
If the resulting string contains path separators, an exception is raised.

Lib/importlib/resources/abc.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ def open(self, mode='r', *args, **kwargs):
142142
accepted by io.TextIOWrapper.
143143
"""
144144

145-
@abc.abstractproperty
145+
@property
146+
@abc.abstractmethod
146147
def name(self) -> str:
147148
"""
148149
The base name of this object without any parent references.

0 commit comments

Comments
 (0)