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

Skip to content

Commit 142aee3

Browse files
Merge heads
2 parents be9a4e5 + d8fdffe commit 142aee3

33 files changed

Lines changed: 762 additions & 496 deletions

Doc/library/decimal.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ Decimal objects
345345
*value* can be an integer, string, tuple, :class:`float`, or another :class:`Decimal`
346346
object. If no *value* is given, returns ``Decimal('0')``. If *value* is a
347347
string, it should conform to the decimal numeric string syntax after leading
348-
and trailing whitespace characters are removed::
348+
and trailing whitespace characters, as well as underscores throughout, are removed::
349349

350350
sign ::= '+' | '-'
351351
digit ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
@@ -394,6 +394,10 @@ Decimal objects
394394
:class:`float` arguments raise an exception if the :exc:`FloatOperation`
395395
trap is set. By default the trap is off.
396396

397+
.. versionchanged:: 3.6
398+
Underscores are allowed for grouping, as with integral and floating-point
399+
literals in code.
400+
397401
Decimal floating point objects share many properties with the other built-in
398402
numeric types such as :class:`float` and :class:`int`. All of the usual math
399403
operations and special methods apply. Likewise, decimal objects can be
@@ -1075,8 +1079,8 @@ In addition to the three supplied contexts, new contexts can be created with the
10751079
Decimal('4.44')
10761080

10771081
This method implements the to-number operation of the IBM specification.
1078-
If the argument is a string, no leading or trailing whitespace is
1079-
permitted.
1082+
If the argument is a string, no leading or trailing whitespace or
1083+
underscores are permitted.
10801084

10811085
.. method:: create_decimal_from_float(f)
10821086

Doc/library/functions.rst

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,9 @@ are always available. They are listed here in alphabetical order.
271271

272272
The complex type is described in :ref:`typesnumeric`.
273273

274+
.. versionchanged:: 3.6
275+
Grouping digits with underscores as in code literals is allowed.
276+
274277

275278
.. function:: delattr(object, name)
276279

@@ -531,11 +534,14 @@ are always available. They are listed here in alphabetical order.
531534

532535
The float type is described in :ref:`typesnumeric`.
533536

534-
.. index::
535-
single: __format__
536-
single: string; format() (built-in function)
537+
.. versionchanged:: 3.6
538+
Grouping digits with underscores as in code literals is allowed.
537539

538540

541+
.. index::
542+
single: __format__
543+
single: string; format() (built-in function)
544+
539545
.. function:: format(value[, format_spec])
540546

541547
Convert a *value* to a "formatted" representation, as controlled by
@@ -702,6 +708,10 @@ are always available. They are listed here in alphabetical order.
702708
:meth:`base.__int__ <object.__int__>` instead of :meth:`base.__index__
703709
<object.__index__>`.
704710

711+
.. versionchanged:: 3.6
712+
Grouping digits with underscores as in code literals is allowed.
713+
714+
705715
.. function:: isinstance(object, classinfo)
706716

707717
Return true if the *object* argument is an instance of the *classinfo*

Doc/library/sys.rst

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,18 +1104,6 @@ always available.
11041104
thus may not be available in all Python implementations.
11051105

11061106

1107-
.. function:: settscdump(on_flag)
1108-
1109-
Activate dumping of VM measurements using the Pentium timestamp counter, if
1110-
*on_flag* is true. Deactivate these dumps if *on_flag* is off. The function is
1111-
available only if Python was compiled with ``--with-tsc``. To understand
1112-
the output of this dump, read :file:`Python/ceval.c` in the Python sources.
1113-
1114-
.. impl-detail::
1115-
This function is intimately bound to CPython implementation details and
1116-
thus not likely to be implemented elsewhere.
1117-
1118-
11191107
.. function:: set_coroutine_wrapper(wrapper)
11201108

11211109
Allows intercepting creation of :term:`coroutine` objects (only ones that

Doc/reference/lexical_analysis.rst

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -721,20 +721,24 @@ Integer literals
721721
Integer literals are described by the following lexical definitions:
722722

723723
.. productionlist::
724-
integer: `decimalinteger` | `octinteger` | `hexinteger` | `bininteger`
725-
decimalinteger: `nonzerodigit` `digit`* | "0"+
724+
integer: `decinteger` | `bininteger` | `octinteger` | `hexinteger`
725+
decinteger: `nonzerodigit` (["_"] `digit`)* | "0"+ (["_"] "0")*
726+
bininteger: "0" ("b" | "B") (["_"] `bindigit`)+
727+
octinteger: "0" ("o" | "O") (["_"] `octdigit`)+
728+
hexinteger: "0" ("x" | "X") (["_"] `hexdigit`)+
726729
nonzerodigit: "1"..."9"
727730
digit: "0"..."9"
728-
octinteger: "0" ("o" | "O") `octdigit`+
729-
hexinteger: "0" ("x" | "X") `hexdigit`+
730-
bininteger: "0" ("b" | "B") `bindigit`+
731+
bindigit: "0" | "1"
731732
octdigit: "0"..."7"
732733
hexdigit: `digit` | "a"..."f" | "A"..."F"
733-
bindigit: "0" | "1"
734734

735735
There is no limit for the length of integer literals apart from what can be
736736
stored in available memory.
737737

738+
Underscores are ignored for determining the numeric value of the literal. They
739+
can be used to group digits for enhanced readability. One underscore can occur
740+
between digits, and after base specifiers like ``0x``.
741+
738742
Note that leading zeros in a non-zero decimal number are not allowed. This is
739743
for disambiguation with C-style octal literals, which Python used before version
740744
3.0.
@@ -743,6 +747,10 @@ Some examples of integer literals::
743747

744748
7 2147483647 0o177 0b100110111
745749
3 79228162514264337593543950336 0o377 0xdeadbeef
750+
100_000_000_000 0b_1110_0101
751+
752+
.. versionchanged:: 3.6
753+
Underscores are now allowed for grouping purposes in literals.
746754

747755

748756
.. _floating:
@@ -754,23 +762,28 @@ Floating point literals are described by the following lexical definitions:
754762

755763
.. productionlist::
756764
floatnumber: `pointfloat` | `exponentfloat`
757-
pointfloat: [`intpart`] `fraction` | `intpart` "."
758-
exponentfloat: (`intpart` | `pointfloat`) `exponent`
759-
intpart: `digit`+
760-
fraction: "." `digit`+
761-
exponent: ("e" | "E") ["+" | "-"] `digit`+
765+
pointfloat: [`digitpart`] `fraction` | `digitpart` "."
766+
exponentfloat: (`digitpart` | `pointfloat`) `exponent`
767+
digitpart: `digit` (["_"] `digit`)*
768+
fraction: "." `digitpart`
769+
exponent: ("e" | "E") ["+" | "-"] `digitpart`
762770

763771
Note that the integer and exponent parts are always interpreted using radix 10.
764772
For example, ``077e010`` is legal, and denotes the same number as ``77e10``. The
765-
allowed range of floating point literals is implementation-dependent. Some
766-
examples of floating point literals::
773+
allowed range of floating point literals is implementation-dependent. As in
774+
integer literals, underscores are supported for digit grouping.
775+
776+
Some examples of floating point literals::
767777

768-
3.14 10. .001 1e100 3.14e-10 0e0
778+
3.14 10. .001 1e100 3.14e-10 0e0 3.14_15_93
769779

770780
Note that numeric literals do not include a sign; a phrase like ``-1`` is
771781
actually an expression composed of the unary operator ``-`` and the literal
772782
``1``.
773783

784+
.. versionchanged:: 3.6
785+
Underscores are now allowed for grouping purposes in literals.
786+
774787

775788
.. _imaginary:
776789

@@ -780,15 +793,15 @@ Imaginary literals
780793
Imaginary literals are described by the following lexical definitions:
781794

782795
.. productionlist::
783-
imagnumber: (`floatnumber` | `intpart`) ("j" | "J")
796+
imagnumber: (`floatnumber` | `digitpart`) ("j" | "J")
784797

785798
An imaginary literal yields a complex number with a real part of 0.0. Complex
786799
numbers are represented as a pair of floating point numbers and have the same
787800
restrictions on their range. To create a complex number with a nonzero real
788801
part, add a floating point number to it, e.g., ``(3+4j)``. Some examples of
789802
imaginary literals::
790803

791-
3.14j 10.j 10j .001j 1e100j 3.14e-10j
804+
3.14j 10.j 10j .001j 1e100j 3.14e-10j 3.14_15_93j
792805

793806

794807
.. _operators:

Doc/using/windows.rst

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -747,10 +747,11 @@ populated on Windows:
747747

748748
* If the environment variable :envvar:`PYTHONHOME` is set, it is assumed as
749749
"Python Home". Otherwise, the path of the main Python executable is used to
750-
locate a "landmark file" (``Lib\os.py``) to deduce the "Python Home". If a
751-
Python home is found, the relevant sub-directories added to :data:`sys.path`
752-
(``Lib``, ``plat-win``, etc) are based on that folder. Otherwise, the core
753-
Python path is constructed from the PythonPath stored in the registry.
750+
locate a "landmark file" (either ``Lib\os.py`` or ``pythonXY.zip``) to deduce
751+
the "Python Home". If a Python home is found, the relevant sub-directories
752+
added to :data:`sys.path` (``Lib``, ``plat-win``, etc) are based on that
753+
folder. Otherwise, the core Python path is constructed from the PythonPath
754+
stored in the registry.
754755

755756
* If the Python Home cannot be located, no :envvar:`PYTHONPATH` is specified in
756757
the environment, and no registry entries can be found, a default path with
@@ -795,18 +796,22 @@ following advice will prevent conflicts with other installations:
795796
* If you cannot use the previous suggestions (for example, you are a
796797
distribution that allows people to run :file:`python.exe` directly), ensure
797798
that the landmark file (:file:`Lib\\os.py`) exists in your install directory.
798-
(Note that it will not be detected inside a ZIP file.)
799+
(Note that it will not be detected inside a ZIP file, but a correctly named
800+
ZIP file will be detected instead.)
799801

800802
These will ensure that the files in a system-wide installation will not take
801803
precedence over the copy of the standard library bundled with your application.
802804
Otherwise, your users may experience problems using your application. Note that
803805
the first suggestion is the best, as the other may still be susceptible to
804806
non-standard paths in the registry and user site-packages.
805807

806-
.. versionchanged:: 3.6
808+
.. versionchanged::
809+
3.6
807810

808-
Adds ``sys.path`` file support and removes ``applocal`` option from
809-
``pyvenv.cfg``.
811+
* Adds ``sys.path`` file support and removes ``applocal`` option from
812+
``pyvenv.cfg``.
813+
* Adds ``pythonXX.zip`` as a potential landmark when directly adjacent
814+
to the executable.
810815

811816
Additional modules
812817
==================

Doc/whatsnew/3.6.rst

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ Windows improvements:
101101
which means that when the 260 character path limit may no longer apply.
102102
See :ref:`removing the MAX_PATH limitation <max-path>` for details.
103103

104+
* A ``sys.path`` file can be added to force isolated mode and fully specify
105+
all search paths to avoid registry and environment lookup.
106+
107+
* A ``python36.zip`` file now works as a landmark to infer :envvar:`PYTHONHOME`
108+
104109
.. PEP-sized items next.
105110
106111
.. _pep-4XX:
@@ -124,6 +129,29 @@ Windows improvements:
124129
New Features
125130
============
126131

132+
.. _pep-515:
133+
134+
PEP 515: Underscores in Numeric Literals
135+
========================================
136+
137+
Prior to PEP 515, there was no support for writing long numeric
138+
literals with some form of separator to improve readability. For
139+
instance, how big is ``1000000000000000```? With :pep:`515`, though,
140+
you can use underscores to separate digits as desired to make numeric
141+
literals easier to read: ``1_000_000_000_000_000``. Underscores can be
142+
used with other numeric literals beyond integers, e.g.
143+
``0x_FF_FF_FF_FF``.
144+
145+
Single underscores are allowed between digits and after any base
146+
specifier. More than a single underscore in a row, leading, or
147+
trailing underscores are not allowed.
148+
149+
.. seealso::
150+
151+
:pep:`523` - Underscores in Numeric Literals
152+
PEP written by Georg Brandl & Serhiy Storchaka.
153+
154+
127155
.. _pep-523:
128156

129157
PEP 523: Adding a frame evaluation API to CPython

Include/pystate.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ typedef struct _is {
4343
#ifdef HAVE_DLOPEN
4444
int dlopenflags;
4545
#endif
46-
#ifdef WITH_TSC
47-
int tscdump;
48-
#endif
4946

5047
PyObject *builtins_copy;
5148
PyObject *import_func;

Include/pystrtod.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ PyAPI_FUNC(char *) PyOS_double_to_string(double val,
1919
int *type);
2020

2121
#ifndef Py_LIMITED_API
22+
PyAPI_FUNC(PyObject *) _Py_string_to_number_with_underscores(
23+
const char *str, Py_ssize_t len, const char *what, PyObject *obj, void *arg,
24+
PyObject *(*innerfunc)(const char *, Py_ssize_t, void *));
25+
2226
PyAPI_FUNC(double) _Py_parse_inf_or_nan(const char *p, char **endptr);
2327
#endif
2428

Lib/_pydecimal.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ def __new__(cls, value="0", context=None):
589589
# From a string
590590
# REs insist on real strings, so we can too.
591591
if isinstance(value, str):
592-
m = _parser(value.strip())
592+
m = _parser(value.strip().replace("_", ""))
593593
if m is None:
594594
if context is None:
595595
context = getcontext()
@@ -4125,7 +4125,7 @@ def _set_rounding(self, type):
41254125
This will make it round up for that operation.
41264126
"""
41274127
rounding = self.rounding
4128-
self.rounding= type
4128+
self.rounding = type
41294129
return rounding
41304130

41314131
def create_decimal(self, num='0'):
@@ -4134,10 +4134,10 @@ def create_decimal(self, num='0'):
41344134
This method implements the to-number operation of the
41354135
IBM Decimal specification."""
41364136

4137-
if isinstance(num, str) and num != num.strip():
4137+
if isinstance(num, str) and (num != num.strip() or '_' in num):
41384138
return self._raise_error(ConversionSyntax,
4139-
"no trailing or leading whitespace is "
4140-
"permitted.")
4139+
"trailing or leading whitespace and "
4140+
"underscores are not permitted.")
41414141

41424142
d = Decimal(num, context=self)
41434143
if d._isnan() and len(d._int) > self.prec - self.clamp:

Lib/test/test_complex.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import unittest
22
from test import support
3+
from test.test_grammar import (VALID_UNDERSCORE_LITERALS,
4+
INVALID_UNDERSCORE_LITERALS)
35

46
from random import random
57
from math import atan2, isnan, copysign
@@ -377,6 +379,18 @@ def __complex__(self):
377379
self.assertAlmostEqual(complex(complex1(1j)), 2j)
378380
self.assertRaises(TypeError, complex, complex2(1j))
379381

382+
def test_underscores(self):
383+
# check underscores
384+
for lit in VALID_UNDERSCORE_LITERALS:
385+
if not any(ch in lit for ch in 'xXoObB'):
386+
self.assertEqual(complex(lit), eval(lit))
387+
self.assertEqual(complex(lit), complex(lit.replace('_', '')))
388+
for lit in INVALID_UNDERSCORE_LITERALS:
389+
if lit in ('0_7', '09_99'): # octals are not recognized here
390+
continue
391+
if not any(ch in lit for ch in 'xXoObB'):
392+
self.assertRaises(ValueError, complex, lit)
393+
380394
def test_hash(self):
381395
for x in range(-30, 30):
382396
self.assertEqual(hash(x), hash(complex(x, 0)))

0 commit comments

Comments
 (0)