Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit 2612eed

Browse files
author
Anselm Kruis
committed
Merge branch main into main-slp
2 parents 5f6645b + 218e47b commit 2612eed

35 files changed

+694
-450
lines changed

‎Doc/c-api/code.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ bound into a function.
4545
The first parameter (*argcount*) now represents the total number of positional arguments,
4646
including positional-only.
4747
48-
.. audit-event:: code.__new__ "code filename name argcount kwonlyargcount nlocals stacksize flags"
48+
.. audit-event:: code.__new__ "code filename name argcount posonlyargcount kwonlyargcount nlocals stacksize flags"
4949
5050
.. c:function:: PyCodeObject* PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
5151

‎Doc/c-api/complex.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,10 @@ Complex Numbers as Python Objects
129129
130130
If *op* is not a Python complex number object but has a :meth:`__complex__`
131131
method, this method will first be called to convert *op* to a Python complex
132-
number object. Upon failure, this method returns ``-1.0`` as a real value.
132+
number object. If ``__complex__()`` is not defined then it falls back to
133+
:meth:`__float__`. If ``__float__()`` is not defined then it falls back
134+
to :meth:`__index__`. Upon failure, this method returns ``-1.0`` as a real
135+
value.
136+
137+
.. versionchanged:: 3.8
138+
Use :meth:`__index__` if available.

‎Doc/c-api/float.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,13 @@ Floating Point Objects
4747
Return a C :c:type:`double` representation of the contents of *pyfloat*. If
4848
*pyfloat* is not a Python floating point object but has a :meth:`__float__`
4949
method, this method will first be called to convert *pyfloat* into a float.
50+
If ``__float__()`` is not defined then it falls back to :meth:`__index__`.
5051
This method returns ``-1.0`` upon failure, so one should call
5152
:c:func:`PyErr_Occurred` to check for errors.
5253
54+
.. versionchanged:: 3.8
55+
Use :meth:`__index__` if available.
56+
5357
5458
.. c:function:: double PyFloat_AS_DOUBLE(PyObject *pyfloat)
5559

‎Doc/library/functions.rst

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,11 @@ are always available. They are listed here in alphabetical order.
318318
:class:`int` and :class:`float`. If both arguments are omitted, returns
319319
``0j``.
320320

321+
For a general Python object ``x``, ``complex(x)`` delegates to
322+
``x.__complex__()``. If ``__complex__()`` is not defined then it falls back
323+
to :meth:`__float__`. If ``__float__()`` is not defined then it falls back
324+
to :meth:`__index__`.
325+
321326
.. note::
322327

323328
When converting from a string, the string must not contain whitespace
@@ -330,6 +335,10 @@ are always available. They are listed here in alphabetical order.
330335
.. versionchanged:: 3.6
331336
Grouping digits with underscores as in code literals is allowed.
332337

338+
.. versionchanged:: 3.8
339+
Falls back to :meth:`__index__` if :meth:`__complex__` and
340+
:meth:`__float__` are not defined.
341+
333342

334343
.. function:: delattr(object, name)
335344

@@ -584,7 +593,8 @@ are always available. They are listed here in alphabetical order.
584593
float, an :exc:`OverflowError` will be raised.
585594

586595
For a general Python object ``x``, ``float(x)`` delegates to
587-
``x.__float__()``.
596+
``x.__float__()``. If ``__float__()`` is not defined then it falls back
597+
to :meth:`__index__`.
588598

589599
If no argument is given, ``0.0`` is returned.
590600

@@ -609,6 +619,9 @@ are always available. They are listed here in alphabetical order.
609619
.. versionchanged:: 3.7
610620
*x* is now a positional-only parameter.
611621

622+
.. versionchanged:: 3.8
623+
Falls back to :meth:`__index__` if :meth:`__float__` is not defined.
624+
612625

613626
.. index::
614627
single: __format__
@@ -780,7 +793,8 @@ are always available. They are listed here in alphabetical order.
780793

781794
Return an integer object constructed from a number or string *x*, or return
782795
``0`` if no arguments are given. If *x* defines :meth:`__int__`,
783-
``int(x)`` returns ``x.__int__()``. If *x* defines :meth:`__trunc__`,
796+
``int(x)`` returns ``x.__int__()``. If *x* defines :meth:`__index__`,
797+
it returns ``x.__index__()``. If *x* defines :meth:`__trunc__`,
784798
it returns ``x.__trunc__()``.
785799
For floating point numbers, this truncates towards zero.
786800

@@ -812,6 +826,9 @@ are always available. They are listed here in alphabetical order.
812826
.. versionchanged:: 3.7
813827
*x* is now a positional-only parameter.
814828

829+
.. versionchanged:: 3.8
830+
Falls back to :meth:`__index__` if :meth:`__int__` is not defined.
831+
815832

816833
.. function:: isinstance(object, classinfo)
817834

‎Doc/library/math.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,11 @@ Number-theoretic and representation functions
238238
and without order.
239239

240240
Also called the binomial coefficient. It is mathematically equal to the expression
241-
``n! / (k! (n - k)!)``. It is equivalent to the coefficient of k-th term in
241+
``n! / (k! (n - k)!)``. It is equivalent to the coefficient of the *k*-th term in the
242242
polynomial expansion of the expression ``(1 + x) ** n``.
243243

244244
Raises :exc:`TypeError` if the arguments not integers.
245-
Raises :exc:`ValueError` if the arguments are negative or if k > n.
245+
Raises :exc:`ValueError` if the arguments are negative or if *k* > *n*.
246246

247247
.. versionadded:: 3.8
248248

‎Doc/library/shlex.rst

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ variables which either control lexical analysis or can be used for debugging:
225225
appear in filename specifications and command line parameters, will also be
226226
included in this attribute, and any characters which appear in
227227
``punctuation_chars`` will be removed from ``wordchars`` if they are present
228-
there.
228+
there. If :attr:`whitespace_split` is set to ``True``, this will have no
229+
effect.
229230

230231

231232
.. attribute:: shlex.whitespace
@@ -258,11 +259,13 @@ variables which either control lexical analysis or can be used for debugging:
258259

259260
If ``True``, tokens will only be split in whitespaces. This is useful, for
260261
example, for parsing command lines with :class:`~shlex.shlex`, getting
261-
tokens in a similar way to shell arguments. If this attribute is ``True``,
262-
:attr:`punctuation_chars` will have no effect, and splitting will happen
263-
only on whitespaces. When using :attr:`punctuation_chars`, which is
264-
intended to provide parsing closer to that implemented by shells, it is
265-
advisable to leave ``whitespace_split`` as ``False`` (the default value).
262+
tokens in a similar way to shell arguments. When used in combination with
263+
:attr:`punctuation_chars`, tokens will be split on whitespace in addition to
264+
those characters.
265+
266+
.. versionchanged:: 3.8
267+
The :attr:`punctuation_chars` attribute was made compatible with the
268+
:attr:`whitespace_split` attribute.
266269

267270

268271
.. attribute:: shlex.infile
@@ -398,12 +401,15 @@ otherwise. To illustrate, you can see the difference in the following snippet:
398401

399402
>>> import shlex
400403
>>> text = "a && b; c && d || e; f >'abc'; (def \"ghi\")"
401-
>>> list(shlex.shlex(text))
402-
['a', '&', '&', 'b', ';', 'c', '&', '&', 'd', '|', '|', 'e', ';', 'f', '>',
403-
"'abc'", ';', '(', 'def', '"ghi"', ')']
404-
>>> list(shlex.shlex(text, punctuation_chars=True))
405-
['a', '&&', 'b', ';', 'c', '&&', 'd', '||', 'e', ';', 'f', '>', "'abc'",
406-
';', '(', 'def', '"ghi"', ')']
404+
>>> s = shlex.shlex(text, posix=True)
405+
>>> s.whitespace_split = True
406+
>>> list(s)
407+
['a', '&&', 'b;', 'c', '&&', 'd', '||', 'e;', 'f', '>abc;', '(def', 'ghi)']
408+
>>> s = shlex.shlex(text, posix=True, punctuation_chars=True)
409+
>>> s.whitespace_split = True
410+
>>> list(s)
411+
['a', '&&', 'b', ';', 'c', '&&', 'd', '||', 'e', ';', 'f', '>', 'abc', ';',
412+
'(', 'def', 'ghi', ')']
407413

408414
Of course, tokens will be returned which are not valid for shells, and you'll
409415
need to implement your own error checks on the returned tokens.
@@ -428,6 +434,11 @@ which characters constitute punctuation. For example::
428434
>>> list(s)
429435
['~/a', '&&', 'b-c', '--color=auto', '||', 'd', '*.py?']
430436

437+
However, to match the shell as closely as possible, it is recommended to
438+
always use ``posix`` and :attr:`~shlex.whitespace_split` when using
439+
:attr:`~shlex.punctuation_chars`, which will negate
440+
:attr:`~shlex.wordchars` entirely.
441+
431442
For best effect, ``punctuation_chars`` should be set in conjunction with
432443
``posix=True``. (Note that ``posix=False`` is the default for
433444
:class:`~shlex.shlex`.)

‎Doc/reference/datamodel.rst

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2394,11 +2394,9 @@ left undefined.
23942394
functions). Presence of this method indicates that the numeric object is
23952395
an integer type. Must return an integer.
23962396

2397-
.. note::
2398-
2399-
In order to have a coherent integer type class, when :meth:`__index__` is
2400-
defined :meth:`__int__` should also be defined, and both should return
2401-
the same value.
2397+
If :meth:`__int__`, :meth:`__float__` and :meth:`__complex__` are not
2398+
defined then corresponding built-in functions :func:`int`, :func:`float`
2399+
and :func:`complex` fall back to :meth:`__index__`.
24022400

24032401

24042402
.. method:: object.__round__(self, [,ndigits])

‎Doc/tutorial/controlflow.rst

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -139,18 +139,24 @@ but in fact it isn't. It is an object which returns the successive items of
139139
the desired sequence when you iterate over it, but it doesn't really make
140140
the list, thus saving space.
141141

142-
We say such an object is *iterable*, that is, suitable as a target for
142+
We say such an object is :term:`iterable`, that is, suitable as a target for
143143
functions and constructs that expect something from which they can
144-
obtain successive items until the supply is exhausted. We have seen that
145-
the :keyword:`for` statement is such an *iterator*. The function:func:`list`
146-
is another; it creates lists from iterables::
144+
obtain successive items until the supply is exhausted. We have seen that
145+
the :keyword:`for` statement is such a construct, while an example of function
146+
that takes an iterable is :func:`sum`::
147147

148+
>>> sum(range(4)) # 0 + 1 + 2 + 3
149+
6
148150

149-
>>> list(range(5))
150-
[0, 1, 2, 3, 4]
151+
Later we will see more functions that return iterables and take iterables as
152+
arguments. Lastly, maybe you are curious about how to get a list from a range.
153+
Here is the solution::
151154

152-
Later we will see more functions that return iterables and take iterables as argument.
155+
>>> list(range(4))
156+
[0, 1, 2, 3]
153157

158+
In chapter :ref:`tut-structures`, we will discuss in more detail about
159+
:func:`list`.
154160

155161
.. _tut-break:
156162

@@ -161,7 +167,7 @@ The :keyword:`break` statement, like in C, breaks out of the innermost enclosing
161167
:keyword:`for` or :keyword:`while` loop.
162168

163169
Loop statements may have an :keyword:`!else` clause; it is executed when the loop
164-
terminates through exhaustion of the list (with :keyword:`for`) or when the
170+
terminates through exhaustion of the iterable (with :keyword:`for`) or when the
165171
condition becomes false (with :keyword:`while`), but not when the loop is
166172
terminated by a :keyword:`break` statement. This is exemplified by the
167173
following loop, which searches for prime numbers::
@@ -188,8 +194,8 @@ following loop, which searches for prime numbers::
188194
the :keyword:`for` loop, **not** the :keyword:`if` statement.)
189195

190196
When used with a loop, the ``else`` clause has more in common with the
191-
``else`` clause of a :keyword:`try` statement than it does that of
192-
:keyword:`if` statements: a :keyword:`!try` statement's ``else`` clause runs
197+
``else`` clause of a :keyword:`try` statement than it does with that of
198+
:keyword:`if` statements: a :keyword:`try` statement's ``else`` clause runs
193199
when no exception occurs, and a loop's ``else`` clause runs when no ``break``
194200
occurs. For more on the :keyword:`!try` statement and exceptions, see
195201
:ref:`tut-handling`.

‎Doc/whatsnew/3.8.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,12 @@ Other Language Changes
250250
compatible with the existing :meth:`float.as_integer_ratio` method.
251251
(Contributed by Lisa Roach in :issue:`33073`.)
252252

253+
* Constructors of :class:`int`, :class:`float` and :class:`complex` will now
254+
use the :meth:`~object.__index__` special method, if available and the
255+
corresponding method :meth:`~object.__int__`, :meth:`~object.__float__`
256+
or :meth:`~object.__complex__` is not available.
257+
(Contributed by Serhiy Storchaka in :issue:`20092`.)
258+
253259
* Added support of ``\N{name}`` escapes in :mod:`regular expressions <re>`.
254260
(Contributed by Jonathan Eunice and Serhiy Storchaka in :issue:`30688`.)
255261

@@ -868,7 +874,10 @@ Build and C API Changes
868874
``__index__()`` method (like :class:`~decimal.Decimal` and
869875
:class:`~fractions.Fraction`). :c:func:`PyNumber_Check` will now return
870876
``1`` for objects implementing ``__index__()``.
871-
(Contributed by Serhiy Storchaka in :issue:`36048`.)
877+
:c:func:`PyNumber_Long`, :c:func:`PyNumber_Float` and
878+
:c:func:`PyFloat_AsDouble` also now use the ``__index__()`` method if
879+
available.
880+
(Contributed by Serhiy Storchaka in :issue:`36048` and :issue:`20092`.)
872881

873882
* Heap-allocated type objects will now increase their reference count
874883
in :c:func:`PyObject_Init` (and its parallel macro ``PyObject_INIT``)

‎Lib/idlelib/NEWS.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ Released on 2019年10月20日?
33
======================================
44

55

6+
bpo-32411: Stop sorting dict created with desired line order.
7+
68
bpo-37038: Make idlelib.run runnable; add test clause.
79

810
bpo-36958: Print any argument other than None or int passed to

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /