Python-checkins
Threads by month
- ----- 2025 -----
- March
- February
- January
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2010 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2009 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2008 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2007 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2006 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2005 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2004 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2003 -----
- December
- November
- October
- September
- August
July 2018
- 3 participants
- 471 discussions

Revert "closes bpo-27494: Fix 2to3 handling of trailing comma after a generator expression (GH-3771)" (GH-8241)
by Miss Islington (bot) July 31, 2018
by Miss Islington (bot) July 31, 2018
July 31, 2018
https://github.com/python/cpython/commit/9ecbe3321f7bb3726017a053e583ca507d…
commit: 9ecbe3321f7bb3726017a053e583ca507d4453fc
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington(a)users.noreply.github.com>
committer: GitHub <noreply(a)github.com>
date: 2018-07-30T23:52:49-07:00
summary:
Revert "closes bpo-27494: Fix 2to3 handling of trailing comma after a generator expression (GH-3771)" (GH-8241)
This reverts commit af810b35b494ef1d255d4bf340b92a9dad446995.
This is …
[View More]not valid syntax (see bpo-32012).
(cherry picked from commit 4b8a7f51da224d1a0ad8159935f78ba4e6e16037)
Co-authored-by: Serhiy Storchaka <storchaka(a)gmail.com>
files:
A Misc/NEWS.d/next/Library/2018-07-11-10-03-21.bpo-27494.04OWkW.rst
M Lib/lib2to3/Grammar.txt
M Lib/lib2to3/fixer_util.py
M Lib/lib2to3/fixes/fix_dict.py
M Lib/lib2to3/fixes/fix_paren.py
M Lib/lib2to3/fixes/fix_xrange.py
M Lib/lib2to3/tests/test_parser.py
diff --git a/Lib/lib2to3/Grammar.txt b/Lib/lib2to3/Grammar.txt
index 4905c91e635e..a7ddad3cf322 100644
--- a/Lib/lib2to3/Grammar.txt
+++ b/Lib/lib2to3/Grammar.txt
@@ -111,8 +111,8 @@ atom: ('(' [yield_expr|testlist_gexp] ')' |
'{' [dictsetmaker] '}' |
'`' testlist1 '`' |
NAME | NUMBER | STRING+ | '.' '.' '.')
-listmaker: (test|star_expr) ( old_comp_for | (',' (test|star_expr))* [','] )
-testlist_gexp: (test|star_expr) ( old_comp_for | (',' (test|star_expr))* [','] )
+listmaker: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] )
+testlist_gexp: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] )
lambdef: 'lambda' [varargslist] ':' test
trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
subscriptlist: subscript (',' subscript)* [',']
@@ -142,28 +142,9 @@ argument: ( test [comp_for] |
star_expr )
comp_iter: comp_for | comp_if
-comp_for: [ASYNC] 'for' exprlist 'in' or_test [comp_iter]
+comp_for: [ASYNC] 'for' exprlist 'in' testlist_safe [comp_iter]
comp_if: 'if' old_test [comp_iter]
-# As noted above, testlist_safe extends the syntax allowed in list
-# comprehensions and generators. We can't use it indiscriminately in all
-# derivations using a comp_for-like pattern because the testlist_safe derivation
-# contains comma which clashes with trailing comma in arglist.
-#
-# This was an issue because the parser would not follow the correct derivation
-# when parsing syntactically valid Python code. Since testlist_safe was created
-# specifically to handle list comprehensions and generator expressions enclosed
-# with parentheses, it's safe to only use it in those. That avoids the issue; we
-# can parse code like set(x for x in [],).
-#
-# The syntax supported by this set of rules is not a valid Python 3 syntax,
-# hence the prefix "old".
-#
-# See https://bugs.python.org/issue27494
-old_comp_iter: old_comp_for | old_comp_if
-old_comp_for: [ASYNC] 'for' exprlist 'in' testlist_safe [old_comp_iter]
-old_comp_if: 'if' old_test [old_comp_iter]
-
testlist1: test (',' test)*
# not used in grammar, but may appear in "node" passed from Parser to Compiler
diff --git a/Lib/lib2to3/fixer_util.py b/Lib/lib2to3/fixer_util.py
index 2f9b1c2e766e..babe6cb3f662 100644
--- a/Lib/lib2to3/fixer_util.py
+++ b/Lib/lib2to3/fixer_util.py
@@ -101,8 +101,8 @@ def ListComp(xp, fp, it, test=None):
test.prefix = " "
if_leaf = Leaf(token.NAME, "if")
if_leaf.prefix = " "
- inner_args.append(Node(syms.old_comp_if, [if_leaf, test]))
- inner = Node(syms.listmaker, [xp, Node(syms.old_comp_for, inner_args)])
+ inner_args.append(Node(syms.comp_if, [if_leaf, test]))
+ inner = Node(syms.listmaker, [xp, Node(syms.comp_for, inner_args)])
return Node(syms.atom,
[Leaf(token.LBRACE, "["),
inner,
@@ -208,7 +208,7 @@ def attr_chain(obj, attr):
next = getattr(next, attr)
p0 = """for_stmt< 'for' any 'in' node=any ':' any* >
- | old_comp_for< 'for' any 'in' node=any any* >
+ | comp_for< 'for' any 'in' node=any any* >
"""
p1 = """
power<
diff --git a/Lib/lib2to3/fixes/fix_dict.py b/Lib/lib2to3/fixes/fix_dict.py
index 55be553baac8..d3655c9f1b2d 100644
--- a/Lib/lib2to3/fixes/fix_dict.py
+++ b/Lib/lib2to3/fixes/fix_dict.py
@@ -83,7 +83,7 @@ def transform(self, node, results):
p1 = patcomp.compile_pattern(P1)
P2 = """for_stmt< 'for' any 'in' node=any ':' any* >
- | old_comp_for< 'for' any 'in' node=any any* >
+ | comp_for< 'for' any 'in' node=any any* >
"""
p2 = patcomp.compile_pattern(P2)
diff --git a/Lib/lib2to3/fixes/fix_paren.py b/Lib/lib2to3/fixes/fix_paren.py
index de49eef157d5..b205aa7e1e93 100644
--- a/Lib/lib2to3/fixes/fix_paren.py
+++ b/Lib/lib2to3/fixes/fix_paren.py
@@ -15,7 +15,7 @@ class FixParen(fixer_base.BaseFix):
PATTERN = """
atom< ('[' | '(')
(listmaker< any
- old_comp_for<
+ comp_for<
'for' NAME 'in'
target=testlist_safe< any (',' any)+ [',']
>
@@ -24,7 +24,7 @@ class FixParen(fixer_base.BaseFix):
>
|
testlist_gexp< any
- old_comp_for<
+ comp_for<
'for' NAME 'in'
target=testlist_safe< any (',' any)+ [',']
>
diff --git a/Lib/lib2to3/fixes/fix_xrange.py b/Lib/lib2to3/fixes/fix_xrange.py
index f5f06f354350..1e491e166a3f 100644
--- a/Lib/lib2to3/fixes/fix_xrange.py
+++ b/Lib/lib2to3/fixes/fix_xrange.py
@@ -55,7 +55,7 @@ def transform_range(self, node, results):
p1 = patcomp.compile_pattern(P1)
P2 = """for_stmt< 'for' any 'in' node=any ':' any* >
- | old_comp_for< 'for' any 'in' node=any any* >
+ | comp_for< 'for' any 'in' node=any any* >
| comparison< any 'in' node=any any*>
"""
p2 = patcomp.compile_pattern(P2)
diff --git a/Lib/lib2to3/tests/test_parser.py b/Lib/lib2to3/tests/test_parser.py
index 74653ea80fef..7eb9afac46e0 100644
--- a/Lib/lib2to3/tests/test_parser.py
+++ b/Lib/lib2to3/tests/test_parser.py
@@ -612,13 +612,6 @@ def test_multiline_str_literals(self):
self.validate(s)
-class TestGeneratorExpressions(GrammarTest):
-
- def test_trailing_comma_after_generator_expression_argument_works(self):
- # BPO issue 27494
- self.validate("set(x for x in [],)")
-
-
def diff_texts(a, b, filename):
a = a.splitlines()
b = b.splitlines()
diff --git a/Misc/NEWS.d/next/Library/2018-07-11-10-03-21.bpo-27494.04OWkW.rst b/Misc/NEWS.d/next/Library/2018-07-11-10-03-21.bpo-27494.04OWkW.rst
new file mode 100644
index 000000000000..9ad67c46170f
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-07-11-10-03-21.bpo-27494.04OWkW.rst
@@ -0,0 +1,2 @@
+Reverted :issue:`27494`. 2to3 rejects now a trailing comma in generator
+expressions.
[View Less]
1
0

bpo-33729: Fix issues with arguments parsing in hashlib. (GH-8346)
by Serhiy Storchaka July 31, 2018
by Serhiy Storchaka July 31, 2018
July 31, 2018
https://github.com/python/cpython/commit/f1d36d8efaecd5c84cb35e35119b283f37…
commit: f1d36d8efaecd5c84cb35e35119b283f37d83c40
branch: master
author: Serhiy Storchaka <storchaka(a)gmail.com>
committer: GitHub <noreply(a)github.com>
date: 2018-07-31T09:50:16+03:00
summary:
bpo-33729: Fix issues with arguments parsing in hashlib. (GH-8346)
* help(hashlib) didn't work because of incorrect module name in blake2b and
blake2s classes.
* Constructors blake2*(), sha3_*(), shake_*() and …
[View More]keccak_*() incorrectly
accepted keyword argument "string" for binary data, but documented as
accepting the "data" keyword argument. Now this parameter is positional-only.
* Keyword-only parameters in blake2b() and blake2s() were not documented as
keyword-only.
* Default value for some parameters of blake2b() and blake2s() was None,
which is not acceptable value.
* The length argument for shake_*.digest() was wrapped out to 32 bits.
* The argument for shake_128.digest() and shake_128.hexdigest() was not
positional-only as intended.
* TypeError messages for incorrect arguments in all constructors sha3_*(),
shake_*() and keccak_*() incorrectly referred to sha3_224.
Also made the following enhancements:
* More accurately specified input and result types for strings, bytes and
bytes-like objects.
* Unified positional parameter names for update() and constructors.
* Improved formatting.
files:
A Misc/NEWS.d/next/Library/2018-07-20-09-11-05.bpo-33729.sO6iTb.rst
M Doc/library/hashlib.rst
M Lib/hashlib.py
M Lib/test/test_hashlib.py
M Modules/_blake2/blake2b_impl.c
M Modules/_blake2/blake2s_impl.c
M Modules/_blake2/clinic/blake2b_impl.c.h
M Modules/_blake2/clinic/blake2s_impl.c.h
M Modules/_hashopenssl.c
M Modules/_sha3/clinic/sha3module.c.h
M Modules/_sha3/sha3module.c
M Modules/clinic/_hashopenssl.c.h
diff --git a/Doc/library/hashlib.rst b/Doc/library/hashlib.rst
index da941e5bb42b..baf6b0af242c 100644
--- a/Doc/library/hashlib.rst
+++ b/Doc/library/hashlib.rst
@@ -101,7 +101,7 @@ More condensed:
.. function:: new(name[, data])
- Is a generic constructor that takes the string name of the desired
+ Is a generic constructor that takes the string *name* of the desired
algorithm as its first parameter. It also exists to allow access to the
above listed hashes as well as any other algorithms that your OpenSSL
library may offer. The named constructors are much faster than :func:`new`
@@ -162,10 +162,10 @@ A hash object has the following attributes:
A hash object has the following methods:
-.. method:: hash.update(arg)
+.. method:: hash.update(data)
- Update the hash object with the object *arg*, which must be interpretable as
- a buffer of bytes. Repeated calls are equivalent to a single call with the
+ Update the hash object with the :term:`bytes-like object`.
+ Repeated calls are equivalent to a single call with the
concatenation of all the arguments: ``m.update(a); m.update(b)`` is
equivalent to ``m.update(a+b)``.
@@ -206,7 +206,7 @@ by the SHAKE algorithm.
.. method:: shake.digest(length)
Return the digest of the data passed to the :meth:`update` method so far.
- This is a bytes object of size ``length`` which may contain bytes in
+ This is a bytes object of size *length* which may contain bytes in
the whole range from 0 to 255.
@@ -262,9 +262,10 @@ include a `salt <https://en.wikipedia.org/wiki/Salt_%28cryptography%29>`_.
The function provides scrypt password-based key derivation function as
defined in :rfc:`7914`.
- *password* and *salt* must be bytes-like objects. Applications and
- libraries should limit *password* to a sensible length (e.g. 1024). *salt*
- should be about 16 or more bytes from a proper source, e.g. :func:`os.urandom`.
+ *password* and *salt* must be :term:`bytes-like objects
+ <bytes-like object>`. Applications and libraries should limit *password*
+ to a sensible length (e.g. 1024). *salt* should be about 16 or more
+ bytes from a proper source, e.g. :func:`os.urandom`.
*n* is the CPU/Memory cost factor, *r* the block size, *p* parallelization
factor and *maxmem* limits memory (OpenSSL 1.1.0 defaults to 32 MiB).
@@ -305,11 +306,11 @@ Creating hash objects
New hash objects are created by calling constructor functions:
-.. function:: blake2b(data=b'', digest_size=64, key=b'', salt=b'', \
+.. function:: blake2b(data=b'', *, digest_size=64, key=b'', salt=b'', \
person=b'', fanout=1, depth=1, leaf_size=0, node_offset=0, \
node_depth=0, inner_size=0, last_node=False)
-.. function:: blake2s(data=b'', digest_size=32, key=b'', salt=b'', \
+.. function:: blake2s(data=b'', *, digest_size=32, key=b'', salt=b'', \
person=b'', fanout=1, depth=1, leaf_size=0, node_offset=0, \
node_depth=0, inner_size=0, last_node=False)
@@ -317,8 +318,8 @@ New hash objects are created by calling constructor functions:
These functions return the corresponding hash objects for calculating
BLAKE2b or BLAKE2s. They optionally take these general parameters:
-* *data*: initial chunk of data to hash, which must be interpretable as buffer
- of bytes.
+* *data*: initial chunk of data to hash, which must be
+ :term:`bytes-like object`. It can be passed only as positional argument.
* *digest_size*: size of output digest in bytes.
@@ -427,7 +428,7 @@ object, and, finally, get the digest out of the object by calling
As a shortcut, you can pass the first chunk of data to update directly to the
-constructor as the first argument (or as *data* keyword argument):
+constructor as the positional argument:
>>> from hashlib import blake2b
>>> blake2b(b'Hello world').hexdigest()
diff --git a/Lib/hashlib.py b/Lib/hashlib.py
index e30c6100d7db..4e783a86a34e 100644
--- a/Lib/hashlib.py
+++ b/Lib/hashlib.py
@@ -25,18 +25,18 @@
sha384 and sha512 will be slow on 32 bit platforms.
Hash objects have these methods:
- - update(arg): Update the hash object with the bytes in arg. Repeated calls
- are equivalent to a single call with the concatenation of all
- the arguments.
- - digest(): Return the digest of the bytes passed to the update() method
- so far.
- - hexdigest(): Like digest() except the digest is returned as a unicode
- object of double length, containing only hexadecimal digits.
- - copy(): Return a copy (clone) of the hash object. This can be used to
- efficiently compute the digests of strings that share a common
- initial substring.
-
-For example, to obtain the digest of the string 'Nobody inspects the
+ - update(data): Update the hash object with the bytes in data. Repeated calls
+ are equivalent to a single call with the concatenation of all
+ the arguments.
+ - digest(): Return the digest of the bytes passed to the update() method
+ so far as a bytes object.
+ - hexdigest(): Like digest() except the digest is returned as a string
+ of double length, containing only hexadecimal digits.
+ - copy(): Return a copy (clone) of the hash object. This can be used to
+ efficiently compute the digests of datas that share a common
+ initial substring.
+
+For example, to obtain the digest of the byte string 'Nobody inspects the
spammish repetition':
>>> import hashlib
@@ -130,14 +130,15 @@ def __get_openssl_constructor(name):
def __py_new(name, data=b'', **kwargs):
"""new(name, data=b'', **kwargs) - Return a new hashing object using the
- named algorithm; optionally initialized with data (which must be bytes).
+ named algorithm; optionally initialized with data (which must be
+ a bytes-like object).
"""
return __get_builtin_constructor(name)(data, **kwargs)
def __hash_new(name, data=b'', **kwargs):
"""new(name, data=b'') - Return a new hashing object using the named algorithm;
- optionally initialized with data (which must be bytes).
+ optionally initialized with data (which must be a bytes-like object).
"""
if name in {'blake2b', 'blake2s'}:
# Prefer our blake2 implementation.
diff --git a/Lib/test/test_hashlib.py b/Lib/test/test_hashlib.py
index 0f76b24cbb0f..c8a873f1e01f 100644
--- a/Lib/test/test_hashlib.py
+++ b/Lib/test/test_hashlib.py
@@ -568,8 +568,12 @@ def check_blake2(self, constructor, salt_size, person_size, key_size,
self.assertRaises(ValueError, constructor, node_offset=-1)
self.assertRaises(OverflowError, constructor, node_offset=max_offset+1)
+ self.assertRaises(TypeError, constructor, data=b'')
+ self.assertRaises(TypeError, constructor, string=b'')
+ self.assertRaises(TypeError, constructor, '')
+
constructor(
- string=b'',
+ b'',
key=b'',
salt=b'',
person=b'',
diff --git a/Misc/NEWS.d/next/Library/2018-07-20-09-11-05.bpo-33729.sO6iTb.rst b/Misc/NEWS.d/next/Library/2018-07-20-09-11-05.bpo-33729.sO6iTb.rst
new file mode 100644
index 000000000000..c4718722ad7c
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-07-20-09-11-05.bpo-33729.sO6iTb.rst
@@ -0,0 +1 @@
+Fixed issues with arguments parsing in :mod:`hashlib`.
diff --git a/Modules/_blake2/blake2b_impl.c b/Modules/_blake2/blake2b_impl.c
index 92d7c5c3a2da..7ad4b650b325 100644
--- a/Modules/_blake2/blake2b_impl.c
+++ b/Modules/_blake2/blake2b_impl.c
@@ -47,10 +47,10 @@ typedef struct {
#include "clinic/blake2b_impl.c.h"
/*[clinic input]
-module _blake2b
-class _blake2b.blake2b "BLAKE2bObject *" "&PyBlake2_BLAKE2bType"
+module _blake2
+class _blake2.blake2b "BLAKE2bObject *" "&PyBlake2_BLAKE2bType"
[clinic start generated code]*/
-/*[clinic end generated code: output=da39a3ee5e6b4b0d input=6893358c6622aecf]*/
+/*[clinic end generated code: output=da39a3ee5e6b4b0d input=d47b0527b39c673f]*/
static BLAKE2bObject *
@@ -66,13 +66,14 @@ new_BLAKE2bObject(PyTypeObject *type)
/*[clinic input]
@classmethod
-_blake2b.blake2b.__new__ as py_blake2b_new
- string as data: object = NULL
+_blake2.blake2b.__new__ as py_blake2b_new
+ data: object(c_default="NULL") = b''
+ /
*
- digest_size: int(c_default="BLAKE2B_OUTBYTES") = _blake2b.blake2b.MAX_DIGEST_SIZE
- key: Py_buffer = None
- salt: Py_buffer = None
- person: Py_buffer = None
+ digest_size: int(c_default="BLAKE2B_OUTBYTES") = _blake2.blake2b.MAX_DIGEST_SIZE
+ key: Py_buffer(c_default="NULL", py_default="b''") = None
+ salt: Py_buffer(c_default="NULL", py_default="b''") = None
+ person: Py_buffer(c_default="NULL", py_default="b''") = None
fanout: int = 1
depth: int = 1
leaf_size: unsigned_long = 0
@@ -90,7 +91,7 @@ py_blake2b_new_impl(PyTypeObject *type, PyObject *data, int digest_size,
int fanout, int depth, unsigned long leaf_size,
unsigned long long node_offset, int node_depth,
int inner_size, int last_node)
-/*[clinic end generated code: output=65e732c66c2297a0 input=75ab5196b695adee]*/
+/*[clinic end generated code: output=65e732c66c2297a0 input=82be35a4e6a9daa2]*/
{
BLAKE2bObject *self = NULL;
Py_buffer buf;
@@ -237,14 +238,14 @@ py_blake2b_new_impl(PyTypeObject *type, PyObject *data, int digest_size,
}
/*[clinic input]
-_blake2b.blake2b.copy
+_blake2.blake2b.copy
Return a copy of the hash object.
[clinic start generated code]*/
static PyObject *
-_blake2b_blake2b_copy_impl(BLAKE2bObject *self)
-/*[clinic end generated code: output=c89cd33550ab1543 input=4c9c319f18f10747]*/
+_blake2_blake2b_copy_impl(BLAKE2bObject *self)
+/*[clinic end generated code: output=ff6acee5f93656ae input=e383c2d199fd8a2e]*/
{
BLAKE2bObject *cpy;
@@ -259,21 +260,21 @@ _blake2b_blake2b_copy_impl(BLAKE2bObject *self)
}
/*[clinic input]
-_blake2b.blake2b.update
+_blake2.blake2b.update
- obj: object
+ data: object
/
-Update this hash object's state with the provided string.
+Update this hash object's state with the provided bytes-like object.
[clinic start generated code]*/
static PyObject *
-_blake2b_blake2b_update(BLAKE2bObject *self, PyObject *obj)
-/*[clinic end generated code: output=a888f07c4cddbe94 input=3ecb8c13ee4260f2]*/
+_blake2_blake2b_update(BLAKE2bObject *self, PyObject *data)
+/*[clinic end generated code: output=010dfcbe22654359 input=ffc4aa6a6a225d31]*/
{
Py_buffer buf;
- GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
+ GET_BUFFER_VIEW_OR_ERROUT(data, &buf);
if (self->lock == NULL && buf.len >= HASHLIB_GIL_MINSIZE)
self->lock = PyThread_allocate_lock();
@@ -293,14 +294,14 @@ _blake2b_blake2b_update(BLAKE2bObject *self, PyObject *obj)
}
/*[clinic input]
-_blake2b.blake2b.digest
+_blake2.blake2b.digest
-Return the digest value as a string of binary data.
+Return the digest value as a bytes object.
[clinic start generated code]*/
static PyObject *
-_blake2b_blake2b_digest_impl(BLAKE2bObject *self)
-/*[clinic end generated code: output=b13a79360d984740 input=ac2fa462ebb1b9c7]*/
+_blake2_blake2b_digest_impl(BLAKE2bObject *self)
+/*[clinic end generated code: output=a5864660f4bfc61a input=7d21659e9c5fff02]*/
{
uint8_t digest[BLAKE2B_OUTBYTES];
blake2b_state state_cpy;
@@ -314,14 +315,14 @@ _blake2b_blake2b_digest_impl(BLAKE2bObject *self)
}
/*[clinic input]
-_blake2b.blake2b.hexdigest
+_blake2.blake2b.hexdigest
Return the digest value as a string of hexadecimal digits.
[clinic start generated code]*/
static PyObject *
-_blake2b_blake2b_hexdigest_impl(BLAKE2bObject *self)
-/*[clinic end generated code: output=6a503611715b24bd input=d58f0b2f37812e33]*/
+_blake2_blake2b_hexdigest_impl(BLAKE2bObject *self)
+/*[clinic end generated code: output=b5598a87d8794a60 input=76930f6946351f56]*/
{
uint8_t digest[BLAKE2B_OUTBYTES];
blake2b_state state_cpy;
@@ -335,10 +336,10 @@ _blake2b_blake2b_hexdigest_impl(BLAKE2bObject *self)
static PyMethodDef py_blake2b_methods[] = {
- _BLAKE2B_BLAKE2B_COPY_METHODDEF
- _BLAKE2B_BLAKE2B_DIGEST_METHODDEF
- _BLAKE2B_BLAKE2B_HEXDIGEST_METHODDEF
- _BLAKE2B_BLAKE2B_UPDATE_METHODDEF
+ _BLAKE2_BLAKE2B_COPY_METHODDEF
+ _BLAKE2_BLAKE2B_DIGEST_METHODDEF
+ _BLAKE2_BLAKE2B_HEXDIGEST_METHODDEF
+ _BLAKE2_BLAKE2B_UPDATE_METHODDEF
{NULL, NULL}
};
diff --git a/Modules/_blake2/blake2s_impl.c b/Modules/_blake2/blake2s_impl.c
index 8c49082377f4..8864fd829787 100644
--- a/Modules/_blake2/blake2s_impl.c
+++ b/Modules/_blake2/blake2s_impl.c
@@ -47,10 +47,10 @@ typedef struct {
#include "clinic/blake2s_impl.c.h"
/*[clinic input]
-module _blake2s
-class _blake2s.blake2s "BLAKE2sObject *" "&PyBlake2_BLAKE2sType"
+module _blake2
+class _blake2.blake2s "BLAKE2sObject *" "&PyBlake2_BLAKE2sType"
[clinic start generated code]*/
-/*[clinic end generated code: output=da39a3ee5e6b4b0d input=edbfcf7557a685a7]*/
+/*[clinic end generated code: output=da39a3ee5e6b4b0d input=4b79d7ffe07286ce]*/
static BLAKE2sObject *
@@ -66,13 +66,14 @@ new_BLAKE2sObject(PyTypeObject *type)
/*[clinic input]
@classmethod
-_blake2s.blake2s.__new__ as py_blake2s_new
- string as data: object = NULL
+_blake2.blake2s.__new__ as py_blake2s_new
+ data: object(c_default="NULL") = b''
+ /
*
- digest_size: int(c_default="BLAKE2S_OUTBYTES") = _blake2s.blake2s.MAX_DIGEST_SIZE
- key: Py_buffer = None
- salt: Py_buffer = None
- person: Py_buffer = None
+ digest_size: int(c_default="BLAKE2S_OUTBYTES") = _blake2.blake2s.MAX_DIGEST_SIZE
+ key: Py_buffer(c_default="NULL", py_default="b''") = None
+ salt: Py_buffer(c_default="NULL", py_default="b''") = None
+ person: Py_buffer(c_default="NULL", py_default="b''") = None
fanout: int = 1
depth: int = 1
leaf_size: unsigned_long = 0
@@ -90,7 +91,7 @@ py_blake2s_new_impl(PyTypeObject *type, PyObject *data, int digest_size,
int fanout, int depth, unsigned long leaf_size,
unsigned long long node_offset, int node_depth,
int inner_size, int last_node)
-/*[clinic end generated code: output=b95806be0514dcf7 input=f18d6efd9b9a1271]*/
+/*[clinic end generated code: output=b95806be0514dcf7 input=641c0509debf714d]*/
{
BLAKE2sObject *self = NULL;
Py_buffer buf;
@@ -237,14 +238,14 @@ py_blake2s_new_impl(PyTypeObject *type, PyObject *data, int digest_size,
}
/*[clinic input]
-_blake2s.blake2s.copy
+_blake2.blake2s.copy
Return a copy of the hash object.
[clinic start generated code]*/
static PyObject *
-_blake2s_blake2s_copy_impl(BLAKE2sObject *self)
-/*[clinic end generated code: output=6c5bada404b7aed7 input=c8858e887ae4a07a]*/
+_blake2_blake2s_copy_impl(BLAKE2sObject *self)
+/*[clinic end generated code: output=5b90131c4eae275e input=0b9d44942f0fe4b2]*/
{
BLAKE2sObject *cpy;
@@ -259,21 +260,21 @@ _blake2s_blake2s_copy_impl(BLAKE2sObject *self)
}
/*[clinic input]
-_blake2s.blake2s.update
+_blake2.blake2s.update
- obj: object
+ data: object
/
-Update this hash object's state with the provided string.
+Update this hash object's state with the provided bytes-like object.
[clinic start generated code]*/
static PyObject *
-_blake2s_blake2s_update(BLAKE2sObject *self, PyObject *obj)
-/*[clinic end generated code: output=fe8438a1d3cede87 input=47a408b9a3cc05c5]*/
+_blake2_blake2s_update(BLAKE2sObject *self, PyObject *data)
+/*[clinic end generated code: output=757dc087fec37815 input=97500db2f9de4aaa]*/
{
Py_buffer buf;
- GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
+ GET_BUFFER_VIEW_OR_ERROUT(data, &buf);
if (self->lock == NULL && buf.len >= HASHLIB_GIL_MINSIZE)
self->lock = PyThread_allocate_lock();
@@ -293,14 +294,14 @@ _blake2s_blake2s_update(BLAKE2sObject *self, PyObject *obj)
}
/*[clinic input]
-_blake2s.blake2s.digest
+_blake2.blake2s.digest
-Return the digest value as a string of binary data.
+Return the digest value as a bytes object.
[clinic start generated code]*/
static PyObject *
-_blake2s_blake2s_digest_impl(BLAKE2sObject *self)
-/*[clinic end generated code: output=80e81a48c6f79cf9 input=feb9a220135bdeba]*/
+_blake2_blake2s_digest_impl(BLAKE2sObject *self)
+/*[clinic end generated code: output=40c566ca4bc6bc51 input=f41e0b8d6d937454]*/
{
uint8_t digest[BLAKE2S_OUTBYTES];
blake2s_state state_cpy;
@@ -314,14 +315,14 @@ _blake2s_blake2s_digest_impl(BLAKE2sObject *self)
}
/*[clinic input]
-_blake2s.blake2s.hexdigest
+_blake2.blake2s.hexdigest
Return the digest value as a string of hexadecimal digits.
[clinic start generated code]*/
static PyObject *
-_blake2s_blake2s_hexdigest_impl(BLAKE2sObject *self)
-/*[clinic end generated code: output=db6c5028c0a3c2e5 input=4e4877b8bd7aea91]*/
+_blake2_blake2s_hexdigest_impl(BLAKE2sObject *self)
+/*[clinic end generated code: output=15153eb5e59c52eb input=c77a1321567e8952]*/
{
uint8_t digest[BLAKE2S_OUTBYTES];
blake2s_state state_cpy;
@@ -335,10 +336,10 @@ _blake2s_blake2s_hexdigest_impl(BLAKE2sObject *self)
static PyMethodDef py_blake2s_methods[] = {
- _BLAKE2S_BLAKE2S_COPY_METHODDEF
- _BLAKE2S_BLAKE2S_DIGEST_METHODDEF
- _BLAKE2S_BLAKE2S_HEXDIGEST_METHODDEF
- _BLAKE2S_BLAKE2S_UPDATE_METHODDEF
+ _BLAKE2_BLAKE2S_COPY_METHODDEF
+ _BLAKE2_BLAKE2S_DIGEST_METHODDEF
+ _BLAKE2_BLAKE2S_HEXDIGEST_METHODDEF
+ _BLAKE2_BLAKE2S_UPDATE_METHODDEF
{NULL, NULL}
};
diff --git a/Modules/_blake2/clinic/blake2b_impl.c.h b/Modules/_blake2/clinic/blake2b_impl.c.h
index 3cb70fb18f5f..3315bf9490cf 100644
--- a/Modules/_blake2/clinic/blake2b_impl.c.h
+++ b/Modules/_blake2/clinic/blake2b_impl.c.h
@@ -3,10 +3,9 @@ preserve
[clinic start generated code]*/
PyDoc_STRVAR(py_blake2b_new__doc__,
-"blake2b(string=None, *, digest_size=_blake2b.blake2b.MAX_DIGEST_SIZE,\n"
-" key=None, salt=None, person=None, fanout=1, depth=1,\n"
-" leaf_size=0, node_offset=0, node_depth=0, inner_size=0,\n"
-" last_node=False)\n"
+"blake2b(data=b\'\', /, *, digest_size=_blake2.blake2b.MAX_DIGEST_SIZE,\n"
+" key=b\'\', salt=b\'\', person=b\'\', fanout=1, depth=1, leaf_size=0,\n"
+" node_offset=0, node_depth=0, inner_size=0, last_node=False)\n"
"--\n"
"\n"
"Return a new BLAKE2b hash object.");
@@ -22,7 +21,7 @@ static PyObject *
py_blake2b_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
- static const char * const _keywords[] = {"string", "digest_size", "key", "salt", "person", "fanout", "depth", "leaf_size", "node_offset", "node_depth", "inner_size", "last_node", NULL};
+ static const char * const _keywords[] = {"", "digest_size", "key", "salt", "person", "fanout", "depth", "leaf_size", "node_offset", "node_depth", "inner_size", "last_node", NULL};
static _PyArg_Parser _parser = {"|O$iy*y*y*iiO&O&iip:blake2b", _keywords, 0};
PyObject *data = NULL;
int digest_size = BLAKE2B_OUTBYTES;
@@ -60,66 +59,66 @@ py_blake2b_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
return return_value;
}
-PyDoc_STRVAR(_blake2b_blake2b_copy__doc__,
+PyDoc_STRVAR(_blake2_blake2b_copy__doc__,
"copy($self, /)\n"
"--\n"
"\n"
"Return a copy of the hash object.");
-#define _BLAKE2B_BLAKE2B_COPY_METHODDEF \
- {"copy", (PyCFunction)_blake2b_blake2b_copy, METH_NOARGS, _blake2b_blake2b_copy__doc__},
+#define _BLAKE2_BLAKE2B_COPY_METHODDEF \
+ {"copy", (PyCFunction)_blake2_blake2b_copy, METH_NOARGS, _blake2_blake2b_copy__doc__},
static PyObject *
-_blake2b_blake2b_copy_impl(BLAKE2bObject *self);
+_blake2_blake2b_copy_impl(BLAKE2bObject *self);
static PyObject *
-_blake2b_blake2b_copy(BLAKE2bObject *self, PyObject *Py_UNUSED(ignored))
+_blake2_blake2b_copy(BLAKE2bObject *self, PyObject *Py_UNUSED(ignored))
{
- return _blake2b_blake2b_copy_impl(self);
+ return _blake2_blake2b_copy_impl(self);
}
-PyDoc_STRVAR(_blake2b_blake2b_update__doc__,
-"update($self, obj, /)\n"
+PyDoc_STRVAR(_blake2_blake2b_update__doc__,
+"update($self, data, /)\n"
"--\n"
"\n"
-"Update this hash object\'s state with the provided string.");
+"Update this hash object\'s state with the provided bytes-like object.");
-#define _BLAKE2B_BLAKE2B_UPDATE_METHODDEF \
- {"update", (PyCFunction)_blake2b_blake2b_update, METH_O, _blake2b_blake2b_update__doc__},
+#define _BLAKE2_BLAKE2B_UPDATE_METHODDEF \
+ {"update", (PyCFunction)_blake2_blake2b_update, METH_O, _blake2_blake2b_update__doc__},
-PyDoc_STRVAR(_blake2b_blake2b_digest__doc__,
+PyDoc_STRVAR(_blake2_blake2b_digest__doc__,
"digest($self, /)\n"
"--\n"
"\n"
-"Return the digest value as a string of binary data.");
+"Return the digest value as a bytes object.");
-#define _BLAKE2B_BLAKE2B_DIGEST_METHODDEF \
- {"digest", (PyCFunction)_blake2b_blake2b_digest, METH_NOARGS, _blake2b_blake2b_digest__doc__},
+#define _BLAKE2_BLAKE2B_DIGEST_METHODDEF \
+ {"digest", (PyCFunction)_blake2_blake2b_digest, METH_NOARGS, _blake2_blake2b_digest__doc__},
static PyObject *
-_blake2b_blake2b_digest_impl(BLAKE2bObject *self);
+_blake2_blake2b_digest_impl(BLAKE2bObject *self);
static PyObject *
-_blake2b_blake2b_digest(BLAKE2bObject *self, PyObject *Py_UNUSED(ignored))
+_blake2_blake2b_digest(BLAKE2bObject *self, PyObject *Py_UNUSED(ignored))
{
- return _blake2b_blake2b_digest_impl(self);
+ return _blake2_blake2b_digest_impl(self);
}
-PyDoc_STRVAR(_blake2b_blake2b_hexdigest__doc__,
+PyDoc_STRVAR(_blake2_blake2b_hexdigest__doc__,
"hexdigest($self, /)\n"
"--\n"
"\n"
"Return the digest value as a string of hexadecimal digits.");
-#define _BLAKE2B_BLAKE2B_HEXDIGEST_METHODDEF \
- {"hexdigest", (PyCFunction)_blake2b_blake2b_hexdigest, METH_NOARGS, _blake2b_blake2b_hexdigest__doc__},
+#define _BLAKE2_BLAKE2B_HEXDIGEST_METHODDEF \
+ {"hexdigest", (PyCFunction)_blake2_blake2b_hexdigest, METH_NOARGS, _blake2_blake2b_hexdigest__doc__},
static PyObject *
-_blake2b_blake2b_hexdigest_impl(BLAKE2bObject *self);
+_blake2_blake2b_hexdigest_impl(BLAKE2bObject *self);
static PyObject *
-_blake2b_blake2b_hexdigest(BLAKE2bObject *self, PyObject *Py_UNUSED(ignored))
+_blake2_blake2b_hexdigest(BLAKE2bObject *self, PyObject *Py_UNUSED(ignored))
{
- return _blake2b_blake2b_hexdigest_impl(self);
+ return _blake2_blake2b_hexdigest_impl(self);
}
-/*[clinic end generated code: output=afc5c45dff0a24f9 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=39c77de2142faa12 input=a9049054013a1b77]*/
diff --git a/Modules/_blake2/clinic/blake2s_impl.c.h b/Modules/_blake2/clinic/blake2s_impl.c.h
index b444f7bb0d92..23d19ecc9ce8 100644
--- a/Modules/_blake2/clinic/blake2s_impl.c.h
+++ b/Modules/_blake2/clinic/blake2s_impl.c.h
@@ -3,10 +3,9 @@ preserve
[clinic start generated code]*/
PyDoc_STRVAR(py_blake2s_new__doc__,
-"blake2s(string=None, *, digest_size=_blake2s.blake2s.MAX_DIGEST_SIZE,\n"
-" key=None, salt=None, person=None, fanout=1, depth=1,\n"
-" leaf_size=0, node_offset=0, node_depth=0, inner_size=0,\n"
-" last_node=False)\n"
+"blake2s(data=b\'\', /, *, digest_size=_blake2.blake2s.MAX_DIGEST_SIZE,\n"
+" key=b\'\', salt=b\'\', person=b\'\', fanout=1, depth=1, leaf_size=0,\n"
+" node_offset=0, node_depth=0, inner_size=0, last_node=False)\n"
"--\n"
"\n"
"Return a new BLAKE2s hash object.");
@@ -22,7 +21,7 @@ static PyObject *
py_blake2s_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
- static const char * const _keywords[] = {"string", "digest_size", "key", "salt", "person", "fanout", "depth", "leaf_size", "node_offset", "node_depth", "inner_size", "last_node", NULL};
+ static const char * const _keywords[] = {"", "digest_size", "key", "salt", "person", "fanout", "depth", "leaf_size", "node_offset", "node_depth", "inner_size", "last_node", NULL};
static _PyArg_Parser _parser = {"|O$iy*y*y*iiO&O&iip:blake2s", _keywords, 0};
PyObject *data = NULL;
int digest_size = BLAKE2S_OUTBYTES;
@@ -60,66 +59,66 @@ py_blake2s_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
return return_value;
}
-PyDoc_STRVAR(_blake2s_blake2s_copy__doc__,
+PyDoc_STRVAR(_blake2_blake2s_copy__doc__,
"copy($self, /)\n"
"--\n"
"\n"
"Return a copy of the hash object.");
-#define _BLAKE2S_BLAKE2S_COPY_METHODDEF \
- {"copy", (PyCFunction)_blake2s_blake2s_copy, METH_NOARGS, _blake2s_blake2s_copy__doc__},
+#define _BLAKE2_BLAKE2S_COPY_METHODDEF \
+ {"copy", (PyCFunction)_blake2_blake2s_copy, METH_NOARGS, _blake2_blake2s_copy__doc__},
static PyObject *
-_blake2s_blake2s_copy_impl(BLAKE2sObject *self);
+_blake2_blake2s_copy_impl(BLAKE2sObject *self);
static PyObject *
-_blake2s_blake2s_copy(BLAKE2sObject *self, PyObject *Py_UNUSED(ignored))
+_blake2_blake2s_copy(BLAKE2sObject *self, PyObject *Py_UNUSED(ignored))
{
- return _blake2s_blake2s_copy_impl(self);
+ return _blake2_blake2s_copy_impl(self);
}
-PyDoc_STRVAR(_blake2s_blake2s_update__doc__,
-"update($self, obj, /)\n"
+PyDoc_STRVAR(_blake2_blake2s_update__doc__,
+"update($self, data, /)\n"
"--\n"
"\n"
-"Update this hash object\'s state with the provided string.");
+"Update this hash object\'s state with the provided bytes-like object.");
-#define _BLAKE2S_BLAKE2S_UPDATE_METHODDEF \
- {"update", (PyCFunction)_blake2s_blake2s_update, METH_O, _blake2s_blake2s_update__doc__},
+#define _BLAKE2_BLAKE2S_UPDATE_METHODDEF \
+ {"update", (PyCFunction)_blake2_blake2s_update, METH_O, _blake2_blake2s_update__doc__},
-PyDoc_STRVAR(_blake2s_blake2s_digest__doc__,
+PyDoc_STRVAR(_blake2_blake2s_digest__doc__,
"digest($self, /)\n"
"--\n"
"\n"
-"Return the digest value as a string of binary data.");
+"Return the digest value as a bytes object.");
-#define _BLAKE2S_BLAKE2S_DIGEST_METHODDEF \
- {"digest", (PyCFunction)_blake2s_blake2s_digest, METH_NOARGS, _blake2s_blake2s_digest__doc__},
+#define _BLAKE2_BLAKE2S_DIGEST_METHODDEF \
+ {"digest", (PyCFunction)_blake2_blake2s_digest, METH_NOARGS, _blake2_blake2s_digest__doc__},
static PyObject *
-_blake2s_blake2s_digest_impl(BLAKE2sObject *self);
+_blake2_blake2s_digest_impl(BLAKE2sObject *self);
static PyObject *
-_blake2s_blake2s_digest(BLAKE2sObject *self, PyObject *Py_UNUSED(ignored))
+_blake2_blake2s_digest(BLAKE2sObject *self, PyObject *Py_UNUSED(ignored))
{
- return _blake2s_blake2s_digest_impl(self);
+ return _blake2_blake2s_digest_impl(self);
}
-PyDoc_STRVAR(_blake2s_blake2s_hexdigest__doc__,
+PyDoc_STRVAR(_blake2_blake2s_hexdigest__doc__,
"hexdigest($self, /)\n"
"--\n"
"\n"
"Return the digest value as a string of hexadecimal digits.");
-#define _BLAKE2S_BLAKE2S_HEXDIGEST_METHODDEF \
- {"hexdigest", (PyCFunction)_blake2s_blake2s_hexdigest, METH_NOARGS, _blake2s_blake2s_hexdigest__doc__},
+#define _BLAKE2_BLAKE2S_HEXDIGEST_METHODDEF \
+ {"hexdigest", (PyCFunction)_blake2_blake2s_hexdigest, METH_NOARGS, _blake2_blake2s_hexdigest__doc__},
static PyObject *
-_blake2s_blake2s_hexdigest_impl(BLAKE2sObject *self);
+_blake2_blake2s_hexdigest_impl(BLAKE2sObject *self);
static PyObject *
-_blake2s_blake2s_hexdigest(BLAKE2sObject *self, PyObject *Py_UNUSED(ignored))
+_blake2_blake2s_hexdigest(BLAKE2sObject *self, PyObject *Py_UNUSED(ignored))
{
- return _blake2s_blake2s_hexdigest_impl(self);
+ return _blake2_blake2s_hexdigest_impl(self);
}
-/*[clinic end generated code: output=b705723d16f21f57 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=a31a1d56f0e0781f input=a9049054013a1b77]*/
diff --git a/Modules/_hashopenssl.c b/Modules/_hashopenssl.c
index b6dcc0699233..40cd6327312d 100644
--- a/Modules/_hashopenssl.c
+++ b/Modules/_hashopenssl.c
@@ -729,6 +729,10 @@ pbkdf2_hmac(PyObject *self, PyObject *args, PyObject *kwdict)
#if OPENSSL_VERSION_NUMBER > 0x10100000L && !defined(OPENSSL_NO_SCRYPT) && !defined(LIBRESSL_VERSION_NUMBER)
#define PY_SCRYPT 1
+/* XXX: Parameters salt, n, r and p should be required keyword-only parameters.
+ They are optional in the Argument Clinic declaration only due to a
+ limitation of PyArg_ParseTupleAndKeywords. */
+
/*[clinic input]
_hashlib.scrypt
@@ -858,13 +862,13 @@ _hashlib.hmac_digest
msg: Py_buffer
digest: str
-Single-shot HMAC
+Single-shot HMAC.
[clinic start generated code]*/
static PyObject *
_hashlib_hmac_digest_impl(PyObject *module, Py_buffer *key, Py_buffer *msg,
const char *digest)
-/*[clinic end generated code: output=75630e684cdd8762 input=10e964917921e2f2]*/
+/*[clinic end generated code: output=75630e684cdd8762 input=562d2f4249511bd3]*/
{
unsigned char md[EVP_MAX_MD_SIZE] = {0};
unsigned int md_len = 0;
diff --git a/Modules/_sha3/clinic/sha3module.c.h b/Modules/_sha3/clinic/sha3module.c.h
index a1e80b061e58..b2a896ec9161 100644
--- a/Modules/_sha3/clinic/sha3module.c.h
+++ b/Modules/_sha3/clinic/sha3module.c.h
@@ -2,33 +2,6 @@
preserve
[clinic start generated code]*/
-PyDoc_STRVAR(py_sha3_new__doc__,
-"sha3_224(string=None)\n"
-"--\n"
-"\n"
-"Return a new SHA3 hash object with a hashbit length of 28 bytes.");
-
-static PyObject *
-py_sha3_new_impl(PyTypeObject *type, PyObject *data);
-
-static PyObject *
-py_sha3_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
-{
- PyObject *return_value = NULL;
- static const char * const _keywords[] = {"string", NULL};
- static _PyArg_Parser _parser = {"|O:sha3_224", _keywords, 0};
- PyObject *data = NULL;
-
- if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
- &data)) {
- goto exit;
- }
- return_value = py_sha3_new_impl(type, data);
-
-exit:
- return return_value;
-}
-
PyDoc_STRVAR(_sha3_sha3_224_copy__doc__,
"copy($self, /)\n"
"--\n"
@@ -51,7 +24,7 @@ PyDoc_STRVAR(_sha3_sha3_224_digest__doc__,
"digest($self, /)\n"
"--\n"
"\n"
-"Return the digest value as a string of binary data.");
+"Return the digest value as a bytes object.");
#define _SHA3_SHA3_224_DIGEST_METHODDEF \
{"digest", (PyCFunction)_sha3_sha3_224_digest, METH_NOARGS, _sha3_sha3_224_digest__doc__},
@@ -84,36 +57,33 @@ _sha3_sha3_224_hexdigest(SHA3object *self, PyObject *Py_UNUSED(ignored))
}
PyDoc_STRVAR(_sha3_sha3_224_update__doc__,
-"update($self, obj, /)\n"
+"update($self, data, /)\n"
"--\n"
"\n"
-"Update this hash object\'s state with the provided string.");
+"Update this hash object\'s state with the provided bytes-like object.");
#define _SHA3_SHA3_224_UPDATE_METHODDEF \
{"update", (PyCFunction)_sha3_sha3_224_update, METH_O, _sha3_sha3_224_update__doc__},
PyDoc_STRVAR(_sha3_shake_128_digest__doc__,
-"digest($self, /, length)\n"
+"digest($self, length, /)\n"
"--\n"
"\n"
-"Return the digest value as a string of binary data.");
+"Return the digest value as a bytes object.");
#define _SHA3_SHAKE_128_DIGEST_METHODDEF \
- {"digest", (PyCFunction)_sha3_shake_128_digest, METH_FASTCALL|METH_KEYWORDS, _sha3_shake_128_digest__doc__},
+ {"digest", (PyCFunction)_sha3_shake_128_digest, METH_O, _sha3_shake_128_digest__doc__},
static PyObject *
_sha3_shake_128_digest_impl(SHA3object *self, unsigned long length);
static PyObject *
-_sha3_shake_128_digest(SHA3object *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+_sha3_shake_128_digest(SHA3object *self, PyObject *arg)
{
PyObject *return_value = NULL;
- static const char * const _keywords[] = {"length", NULL};
- static _PyArg_Parser _parser = {"k:digest", _keywords, 0};
unsigned long length;
- if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
- &length)) {
+ if (!PyArg_Parse(arg, "O&:digest", _PyLong_UnsignedLong_Converter, &length)) {
goto exit;
}
return_value = _sha3_shake_128_digest_impl(self, length);
@@ -123,27 +93,24 @@ _sha3_shake_128_digest(SHA3object *self, PyObject *const *args, Py_ssize_t nargs
}
PyDoc_STRVAR(_sha3_shake_128_hexdigest__doc__,
-"hexdigest($self, /, length)\n"
+"hexdigest($self, length, /)\n"
"--\n"
"\n"
"Return the digest value as a string of hexadecimal digits.");
#define _SHA3_SHAKE_128_HEXDIGEST_METHODDEF \
- {"hexdigest", (PyCFunction)_sha3_shake_128_hexdigest, METH_FASTCALL|METH_KEYWORDS, _sha3_shake_128_hexdigest__doc__},
+ {"hexdigest", (PyCFunction)_sha3_shake_128_hexdigest, METH_O, _sha3_shake_128_hexdigest__doc__},
static PyObject *
_sha3_shake_128_hexdigest_impl(SHA3object *self, unsigned long length);
static PyObject *
-_sha3_shake_128_hexdigest(SHA3object *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+_sha3_shake_128_hexdigest(SHA3object *self, PyObject *arg)
{
PyObject *return_value = NULL;
- static const char * const _keywords[] = {"length", NULL};
- static _PyArg_Parser _parser = {"k:hexdigest", _keywords, 0};
unsigned long length;
- if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
- &length)) {
+ if (!PyArg_Parse(arg, "O&:hexdigest", _PyLong_UnsignedLong_Converter, &length)) {
goto exit;
}
return_value = _sha3_shake_128_hexdigest_impl(self, length);
@@ -151,4 +118,4 @@ _sha3_shake_128_hexdigest(SHA3object *self, PyObject *const *args, Py_ssize_t na
exit:
return return_value;
}
-/*[clinic end generated code: output=a3aeb6c3b2fbd905 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=bf823532a7bffe68 input=a9049054013a1b77]*/
diff --git a/Modules/_sha3/sha3module.c b/Modules/_sha3/sha3module.c
index 970ce1616e57..f5032fcb72dd 100644
--- a/Modules/_sha3/sha3module.c
+++ b/Modules/_sha3/sha3module.c
@@ -170,21 +170,20 @@ newSHA3object(PyTypeObject *type)
}
-/*[clinic input]
-@classmethod
-_sha3.sha3_224.__new__ as py_sha3_new
- string as data: object = NULL
-
-Return a new SHA3 hash object with a hashbit length of 28 bytes.
-[clinic start generated code]*/
-
static PyObject *
-py_sha3_new_impl(PyTypeObject *type, PyObject *data)
-/*[clinic end generated code: output=8d5c34279e69bf09 input=d7c582b950a858b6]*/
+py_sha3_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
SHA3object *self = NULL;
Py_buffer buf = {NULL, NULL};
HashReturn res;
+ PyObject *data = NULL;
+
+ if (!_PyArg_NoKeywords(_PyType_Name(type), kwargs)) {
+ return NULL;
+ }
+ if (!PyArg_UnpackTuple(args, _PyType_Name(type), 0, 1, &data)) {
+ return NULL;
+ }
self = newSHA3object(type);
if (self == NULL) {
@@ -292,12 +291,12 @@ _sha3_sha3_224_copy_impl(SHA3object *self)
/*[clinic input]
_sha3.sha3_224.digest
-Return the digest value as a string of binary data.
+Return the digest value as a bytes object.
[clinic start generated code]*/
static PyObject *
_sha3_sha3_224_digest_impl(SHA3object *self)
-/*[clinic end generated code: output=fd531842e20b2d5b input=a5807917d219b30e]*/
+/*[clinic end generated code: output=fd531842e20b2d5b input=5b2a659536bbd248]*/
{
unsigned char digest[SHA3_MAX_DIGESTSIZE + SHA3_LANESIZE];
SHA3_state temp;
@@ -347,20 +346,20 @@ _sha3_sha3_224_hexdigest_impl(SHA3object *self)
/*[clinic input]
_sha3.sha3_224.update
- obj: object
+ data: object
/
-Update this hash object's state with the provided string.
+Update this hash object's state with the provided bytes-like object.
[clinic start generated code]*/
static PyObject *
-_sha3_sha3_224_update(SHA3object *self, PyObject *obj)
-/*[clinic end generated code: output=06721d55b483e0af input=be44bf0d1c279791]*/
+_sha3_sha3_224_update(SHA3object *self, PyObject *data)
+/*[clinic end generated code: output=d3223352286ed357 input=a887f54dcc4ae227]*/
{
Py_buffer buf;
HashReturn res;
- GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
+ GET_BUFFER_VIEW_OR_ERROUT(data, &buf);
/* add new data, the function takes the length in bits not bytes */
if (self->lock == NULL && buf.len >= HASHLIB_GIL_MINSIZE) {
@@ -529,44 +528,49 @@ static PyGetSetDef SHA3_getseters[] = {
py_sha3_new, /* tp_new */ \
}
+PyDoc_STRVAR(sha3_224__doc__,
+"sha3_224([data]) -> SHA3 object\n\
+\n\
+Return a new SHA3 hash object with a hashbit length of 28 bytes.");
+
PyDoc_STRVAR(sha3_256__doc__,
-"sha3_256([string]) -> SHA3 object\n\
+"sha3_256([data]) -> SHA3 object\n\
\n\
Return a new SHA3 hash object with a hashbit length of 32 bytes.");
PyDoc_STRVAR(sha3_384__doc__,
-"sha3_384([string]) -> SHA3 object\n\
+"sha3_384([data]) -> SHA3 object\n\
\n\
Return a new SHA3 hash object with a hashbit length of 48 bytes.");
PyDoc_STRVAR(sha3_512__doc__,
-"sha3_512([string]) -> SHA3 object\n\
+"sha3_512([data]) -> SHA3 object\n\
\n\
Return a new SHA3 hash object with a hashbit length of 64 bytes.");
-SHA3_TYPE(SHA3_224type, "_sha3.sha3_224", py_sha3_new__doc__, SHA3_methods);
+SHA3_TYPE(SHA3_224type, "_sha3.sha3_224", sha3_224__doc__, SHA3_methods);
SHA3_TYPE(SHA3_256type, "_sha3.sha3_256", sha3_256__doc__, SHA3_methods);
SHA3_TYPE(SHA3_384type, "_sha3.sha3_384", sha3_384__doc__, SHA3_methods);
SHA3_TYPE(SHA3_512type, "_sha3.sha3_512", sha3_512__doc__, SHA3_methods);
#ifdef PY_WITH_KECCAK
PyDoc_STRVAR(keccak_224__doc__,
-"keccak_224([string]) -> Keccak object\n\
+"keccak_224([data]) -> Keccak object\n\
\n\
Return a new Keccak hash object with a hashbit length of 28 bytes.");
PyDoc_STRVAR(keccak_256__doc__,
-"keccak_256([string]) -> Keccak object\n\
+"keccak_256([data]) -> Keccak object\n\
\n\
Return a new Keccak hash object with a hashbit length of 32 bytes.");
PyDoc_STRVAR(keccak_384__doc__,
-"keccak_384([string]) -> Keccak object\n\
+"keccak_384([data]) -> Keccak object\n\
\n\
Return a new Keccak hash object with a hashbit length of 48 bytes.");
PyDoc_STRVAR(keccak_512__doc__,
-"keccak_512([string]) -> Keccak object\n\
+"keccak_512([data]) -> Keccak object\n\
\n\
Return a new Keccak hash object with a hashbit length of 64 bytes.");
@@ -624,15 +628,15 @@ _SHAKE_digest(SHA3object *self, unsigned long digestlen, int hex)
/*[clinic input]
_sha3.shake_128.digest
- length: unsigned_long(bitwise=True)
- \
+ length: unsigned_long
+ /
-Return the digest value as a string of binary data.
+Return the digest value as a bytes object.
[clinic start generated code]*/
static PyObject *
_sha3_shake_128_digest_impl(SHA3object *self, unsigned long length)
-/*[clinic end generated code: output=2313605e2f87bb8f input=608c8ca80ae9d115]*/
+/*[clinic end generated code: output=2313605e2f87bb8f input=418ef6a36d2e6082]*/
{
return _SHAKE_digest(self, length, 0);
}
@@ -641,15 +645,15 @@ _sha3_shake_128_digest_impl(SHA3object *self, unsigned long length)
/*[clinic input]
_sha3.shake_128.hexdigest
- length: unsigned_long(bitwise=True)
- \
+ length: unsigned_long
+ /
Return the digest value as a string of hexadecimal digits.
[clinic start generated code]*/
static PyObject *
_sha3_shake_128_hexdigest_impl(SHA3object *self, unsigned long length)
-/*[clinic end generated code: output=bf8e2f1e490944a8 input=64e56b4760db4573]*/
+/*[clinic end generated code: output=bf8e2f1e490944a8 input=69fb29b0926ae321]*/
{
return _SHAKE_digest(self, length, 1);
}
@@ -664,12 +668,12 @@ static PyMethodDef SHAKE_methods[] = {
};
PyDoc_STRVAR(shake_128__doc__,
-"shake_128([string]) -> SHAKE object\n\
+"shake_128([data]) -> SHAKE object\n\
\n\
Return a new SHAKE hash object.");
PyDoc_STRVAR(shake_256__doc__,
-"shake_256([string]) -> SHAKE object\n\
+"shake_256([data]) -> SHAKE object\n\
\n\
Return a new SHAKE hash object.");
diff --git a/Modules/clinic/_hashopenssl.c.h b/Modules/clinic/_hashopenssl.c.h
index cbc8638c9468..070f8290f26f 100644
--- a/Modules/clinic/_hashopenssl.c.h
+++ b/Modules/clinic/_hashopenssl.c.h
@@ -58,7 +58,7 @@ PyDoc_STRVAR(_hashlib_hmac_digest__doc__,
"hmac_digest($module, /, key, msg, digest)\n"
"--\n"
"\n"
-"Single-shot HMAC");
+"Single-shot HMAC.");
#define _HASHLIB_HMAC_DIGEST_METHODDEF \
{"hmac_digest", (PyCFunction)_hashlib_hmac_digest, METH_FASTCALL|METH_KEYWORDS, _hashlib_hmac_digest__doc__},
@@ -99,4 +99,4 @@ _hashlib_hmac_digest(PyObject *module, PyObject *const *args, Py_ssize_t nargs,
#ifndef _HASHLIB_SCRYPT_METHODDEF
#define _HASHLIB_SCRYPT_METHODDEF
#endif /* !defined(_HASHLIB_SCRYPT_METHODDEF) */
-/*[clinic end generated code: output=b5b90821caf05391 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=b129f1a6ec7b8503 input=a9049054013a1b77]*/
[View Less]
1
0

Revert "closes bpo-27494: Fix 2to3 handling of trailing comma after a generator expression (GH-3771)" (#8241)
by Serhiy Storchaka July 31, 2018
by Serhiy Storchaka July 31, 2018
July 31, 2018
https://github.com/python/cpython/commit/4b8a7f51da224d1a0ad8159935f78ba4e6…
commit: 4b8a7f51da224d1a0ad8159935f78ba4e6e16037
branch: master
author: Serhiy Storchaka <storchaka(a)gmail.com>
committer: GitHub <noreply(a)github.com>
date: 2018-07-31T09:34:30+03:00
summary:
Revert "closes bpo-27494: Fix 2to3 handling of trailing comma after a generator expression (GH-3771)" (#8241)
This reverts commit af810b35b494ef1d255d4bf340b92a9dad446995.
This is not valid syntax (see bpo-32012)…
[View More].
files:
A Misc/NEWS.d/next/Library/2018-07-11-10-03-21.bpo-27494.04OWkW.rst
M Lib/lib2to3/Grammar.txt
M Lib/lib2to3/fixer_util.py
M Lib/lib2to3/fixes/fix_dict.py
M Lib/lib2to3/fixes/fix_paren.py
M Lib/lib2to3/fixes/fix_xrange.py
M Lib/lib2to3/tests/test_parser.py
diff --git a/Lib/lib2to3/Grammar.txt b/Lib/lib2to3/Grammar.txt
index 4905c91e635e..a7ddad3cf322 100644
--- a/Lib/lib2to3/Grammar.txt
+++ b/Lib/lib2to3/Grammar.txt
@@ -111,8 +111,8 @@ atom: ('(' [yield_expr|testlist_gexp] ')' |
'{' [dictsetmaker] '}' |
'`' testlist1 '`' |
NAME | NUMBER | STRING+ | '.' '.' '.')
-listmaker: (test|star_expr) ( old_comp_for | (',' (test|star_expr))* [','] )
-testlist_gexp: (test|star_expr) ( old_comp_for | (',' (test|star_expr))* [','] )
+listmaker: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] )
+testlist_gexp: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] )
lambdef: 'lambda' [varargslist] ':' test
trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
subscriptlist: subscript (',' subscript)* [',']
@@ -142,28 +142,9 @@ argument: ( test [comp_for] |
star_expr )
comp_iter: comp_for | comp_if
-comp_for: [ASYNC] 'for' exprlist 'in' or_test [comp_iter]
+comp_for: [ASYNC] 'for' exprlist 'in' testlist_safe [comp_iter]
comp_if: 'if' old_test [comp_iter]
-# As noted above, testlist_safe extends the syntax allowed in list
-# comprehensions and generators. We can't use it indiscriminately in all
-# derivations using a comp_for-like pattern because the testlist_safe derivation
-# contains comma which clashes with trailing comma in arglist.
-#
-# This was an issue because the parser would not follow the correct derivation
-# when parsing syntactically valid Python code. Since testlist_safe was created
-# specifically to handle list comprehensions and generator expressions enclosed
-# with parentheses, it's safe to only use it in those. That avoids the issue; we
-# can parse code like set(x for x in [],).
-#
-# The syntax supported by this set of rules is not a valid Python 3 syntax,
-# hence the prefix "old".
-#
-# See https://bugs.python.org/issue27494
-old_comp_iter: old_comp_for | old_comp_if
-old_comp_for: [ASYNC] 'for' exprlist 'in' testlist_safe [old_comp_iter]
-old_comp_if: 'if' old_test [old_comp_iter]
-
testlist1: test (',' test)*
# not used in grammar, but may appear in "node" passed from Parser to Compiler
diff --git a/Lib/lib2to3/fixer_util.py b/Lib/lib2to3/fixer_util.py
index 2f9b1c2e766e..babe6cb3f662 100644
--- a/Lib/lib2to3/fixer_util.py
+++ b/Lib/lib2to3/fixer_util.py
@@ -101,8 +101,8 @@ def ListComp(xp, fp, it, test=None):
test.prefix = " "
if_leaf = Leaf(token.NAME, "if")
if_leaf.prefix = " "
- inner_args.append(Node(syms.old_comp_if, [if_leaf, test]))
- inner = Node(syms.listmaker, [xp, Node(syms.old_comp_for, inner_args)])
+ inner_args.append(Node(syms.comp_if, [if_leaf, test]))
+ inner = Node(syms.listmaker, [xp, Node(syms.comp_for, inner_args)])
return Node(syms.atom,
[Leaf(token.LBRACE, "["),
inner,
@@ -208,7 +208,7 @@ def attr_chain(obj, attr):
next = getattr(next, attr)
p0 = """for_stmt< 'for' any 'in' node=any ':' any* >
- | old_comp_for< 'for' any 'in' node=any any* >
+ | comp_for< 'for' any 'in' node=any any* >
"""
p1 = """
power<
diff --git a/Lib/lib2to3/fixes/fix_dict.py b/Lib/lib2to3/fixes/fix_dict.py
index 55be553baac8..d3655c9f1b2d 100644
--- a/Lib/lib2to3/fixes/fix_dict.py
+++ b/Lib/lib2to3/fixes/fix_dict.py
@@ -83,7 +83,7 @@ def transform(self, node, results):
p1 = patcomp.compile_pattern(P1)
P2 = """for_stmt< 'for' any 'in' node=any ':' any* >
- | old_comp_for< 'for' any 'in' node=any any* >
+ | comp_for< 'for' any 'in' node=any any* >
"""
p2 = patcomp.compile_pattern(P2)
diff --git a/Lib/lib2to3/fixes/fix_paren.py b/Lib/lib2to3/fixes/fix_paren.py
index de49eef157d5..b205aa7e1e93 100644
--- a/Lib/lib2to3/fixes/fix_paren.py
+++ b/Lib/lib2to3/fixes/fix_paren.py
@@ -15,7 +15,7 @@ class FixParen(fixer_base.BaseFix):
PATTERN = """
atom< ('[' | '(')
(listmaker< any
- old_comp_for<
+ comp_for<
'for' NAME 'in'
target=testlist_safe< any (',' any)+ [',']
>
@@ -24,7 +24,7 @@ class FixParen(fixer_base.BaseFix):
>
|
testlist_gexp< any
- old_comp_for<
+ comp_for<
'for' NAME 'in'
target=testlist_safe< any (',' any)+ [',']
>
diff --git a/Lib/lib2to3/fixes/fix_xrange.py b/Lib/lib2to3/fixes/fix_xrange.py
index f5f06f354350..1e491e166a3f 100644
--- a/Lib/lib2to3/fixes/fix_xrange.py
+++ b/Lib/lib2to3/fixes/fix_xrange.py
@@ -55,7 +55,7 @@ def transform_range(self, node, results):
p1 = patcomp.compile_pattern(P1)
P2 = """for_stmt< 'for' any 'in' node=any ':' any* >
- | old_comp_for< 'for' any 'in' node=any any* >
+ | comp_for< 'for' any 'in' node=any any* >
| comparison< any 'in' node=any any*>
"""
p2 = patcomp.compile_pattern(P2)
diff --git a/Lib/lib2to3/tests/test_parser.py b/Lib/lib2to3/tests/test_parser.py
index 74653ea80fef..7eb9afac46e0 100644
--- a/Lib/lib2to3/tests/test_parser.py
+++ b/Lib/lib2to3/tests/test_parser.py
@@ -612,13 +612,6 @@ def test_multiline_str_literals(self):
self.validate(s)
-class TestGeneratorExpressions(GrammarTest):
-
- def test_trailing_comma_after_generator_expression_argument_works(self):
- # BPO issue 27494
- self.validate("set(x for x in [],)")
-
-
def diff_texts(a, b, filename):
a = a.splitlines()
b = b.splitlines()
diff --git a/Misc/NEWS.d/next/Library/2018-07-11-10-03-21.bpo-27494.04OWkW.rst b/Misc/NEWS.d/next/Library/2018-07-11-10-03-21.bpo-27494.04OWkW.rst
new file mode 100644
index 000000000000..9ad67c46170f
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-07-11-10-03-21.bpo-27494.04OWkW.rst
@@ -0,0 +1,2 @@
+Reverted :issue:`27494`. 2to3 rejects now a trailing comma in generator
+expressions.
[View Less]
1
0

bpo-1617161: Make the hash and equality of methods not depending on the value of self. (GH-7848)
by Serhiy Storchaka July 31, 2018
by Serhiy Storchaka July 31, 2018
July 31, 2018
https://github.com/python/cpython/commit/ac20e0f98d6727ba97a9575bfa2a11b2f6…
commit: ac20e0f98d6727ba97a9575bfa2a11b2f6247c35
branch: master
author: Serhiy Storchaka <storchaka(a)gmail.com>
committer: GitHub <noreply(a)github.com>
date: 2018-07-31T09:18:24+03:00
summary:
bpo-1617161: Make the hash and equality of methods not depending on the value of self. (GH-7848)
* The hash of BuiltinMethodType instances no longer depends on the hash
of __self__. It depends now on the hash of …
[View More]id(__self__).
* The hash and equality of ModuleType and MethodWrapperType instances no
longer depend on the hash and equality of __self__. They depend now on
the hash and equality of id(__self__).
* MethodWrapperType instances no longer support ordering.
files:
A Misc/NEWS.d/next/Core and Builtins/2018-06-21-21-42-15.bpo-1617161.tSo2yM.rst
M Lib/test/test_class.py
M Lib/test/test_descr.py
M Objects/classobject.c
M Objects/descrobject.c
M Objects/methodobject.c
diff --git a/Lib/test/test_class.py b/Lib/test/test_class.py
index 841cac9171cc..998452a10eee 100644
--- a/Lib/test/test_class.py
+++ b/Lib/test/test_class.py
@@ -534,6 +534,16 @@ class I:
else:
self.fail("attribute error for I.__init__ got masked")
+ def assertNotOrderable(self, a, b):
+ with self.assertRaises(TypeError):
+ a < b
+ with self.assertRaises(TypeError):
+ a > b
+ with self.assertRaises(TypeError):
+ a <= b
+ with self.assertRaises(TypeError):
+ a >= b
+
def testHashComparisonOfMethods(self):
# Test comparison and hash of methods
class A:
@@ -544,24 +554,30 @@ def f(self):
def g(self):
pass
def __eq__(self, other):
- return self.x == other.x
+ return True
def __hash__(self):
- return self.x
+ raise TypeError
class B(A):
pass
a1 = A(1)
- a2 = A(2)
- self.assertEqual(a1.f, a1.f)
- self.assertNotEqual(a1.f, a2.f)
- self.assertNotEqual(a1.f, a1.g)
- self.assertEqual(a1.f, A(1).f)
+ a2 = A(1)
+ self.assertTrue(a1.f == a1.f)
+ self.assertFalse(a1.f != a1.f)
+ self.assertFalse(a1.f == a2.f)
+ self.assertTrue(a1.f != a2.f)
+ self.assertFalse(a1.f == a1.g)
+ self.assertTrue(a1.f != a1.g)
+ self.assertNotOrderable(a1.f, a1.f)
self.assertEqual(hash(a1.f), hash(a1.f))
- self.assertEqual(hash(a1.f), hash(A(1).f))
- self.assertNotEqual(A.f, a1.f)
- self.assertNotEqual(A.f, A.g)
- self.assertEqual(B.f, A.f)
+ self.assertFalse(A.f == a1.f)
+ self.assertTrue(A.f != a1.f)
+ self.assertFalse(A.f == A.g)
+ self.assertTrue(A.f != A.g)
+ self.assertTrue(B.f == A.f)
+ self.assertFalse(B.f != A.f)
+ self.assertNotOrderable(A.f, A.f)
self.assertEqual(hash(B.f), hash(A.f))
# the following triggers a SystemError in 2.4
diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py
index 0e7728ebf2d7..b96d35cc7299 100644
--- a/Lib/test/test_descr.py
+++ b/Lib/test/test_descr.py
@@ -4350,38 +4350,71 @@ def __init__(self):
else:
self.fail("did not test __init__() for None return")
+ def assertNotOrderable(self, a, b):
+ with self.assertRaises(TypeError):
+ a < b
+ with self.assertRaises(TypeError):
+ a > b
+ with self.assertRaises(TypeError):
+ a <= b
+ with self.assertRaises(TypeError):
+ a >= b
+
def test_method_wrapper(self):
# Testing method-wrapper objects...
# <type 'method-wrapper'> did not support any reflection before 2.5
-
- # XXX should methods really support __eq__?
-
l = []
- self.assertEqual(l.__add__, l.__add__)
- self.assertEqual(l.__add__, [].__add__)
- self.assertNotEqual(l.__add__, [5].__add__)
- self.assertNotEqual(l.__add__, l.__mul__)
+ self.assertTrue(l.__add__ == l.__add__)
+ self.assertFalse(l.__add__ != l.__add__)
+ self.assertFalse(l.__add__ == [].__add__)
+ self.assertTrue(l.__add__ != [].__add__)
+ self.assertFalse(l.__add__ == l.__mul__)
+ self.assertTrue(l.__add__ != l.__mul__)
+ self.assertNotOrderable(l.__add__, l.__add__)
self.assertEqual(l.__add__.__name__, '__add__')
- if hasattr(l.__add__, '__self__'):
- # CPython
- self.assertIs(l.__add__.__self__, l)
- self.assertIs(l.__add__.__objclass__, list)
- else:
- # Python implementations where [].__add__ is a normal bound method
- self.assertIs(l.__add__.im_self, l)
- self.assertIs(l.__add__.im_class, list)
+ self.assertIs(l.__add__.__self__, l)
+ self.assertIs(l.__add__.__objclass__, list)
self.assertEqual(l.__add__.__doc__, list.__add__.__doc__)
- try:
- hash(l.__add__)
- except TypeError:
- pass
- else:
- self.fail("no TypeError from hash([].__add__)")
+ # hash([].__add__) should not be based on hash([])
+ hash(l.__add__)
- t = ()
- t += (7,)
- self.assertEqual(t.__add__, (7,).__add__)
- self.assertEqual(hash(t.__add__), hash((7,).__add__))
+ def test_builtin_function_or_method(self):
+ # Not really belonging to test_descr, but introspection and
+ # comparison on <type 'builtin_function_or_method'> seems not
+ # to be tested elsewhere
+ l = []
+ self.assertTrue(l.append == l.append)
+ self.assertFalse(l.append != l.append)
+ self.assertFalse(l.append == [].append)
+ self.assertTrue(l.append != [].append)
+ self.assertFalse(l.append == l.pop)
+ self.assertTrue(l.append != l.pop)
+ self.assertNotOrderable(l.append, l.append)
+ self.assertEqual(l.append.__name__, 'append')
+ self.assertIs(l.append.__self__, l)
+ # self.assertIs(l.append.__objclass__, list) --- could be added?
+ self.assertEqual(l.append.__doc__, list.append.__doc__)
+ # hash([].append) should not be based on hash([])
+ hash(l.append)
+
+ def test_special_unbound_method_types(self):
+ # Testing objects of <type 'wrapper_descriptor'>...
+ self.assertTrue(list.__add__ == list.__add__)
+ self.assertFalse(list.__add__ != list.__add__)
+ self.assertFalse(list.__add__ == list.__mul__)
+ self.assertTrue(list.__add__ != list.__mul__)
+ self.assertNotOrderable(list.__add__, list.__add__)
+ self.assertEqual(list.__add__.__name__, '__add__')
+ self.assertIs(list.__add__.__objclass__, list)
+
+ # Testing objects of <type 'method_descriptor'>...
+ self.assertTrue(list.append == list.append)
+ self.assertFalse(list.append != list.append)
+ self.assertFalse(list.append == list.pop)
+ self.assertTrue(list.append != list.pop)
+ self.assertNotOrderable(list.append, list.append)
+ self.assertEqual(list.append.__name__, 'append')
+ self.assertIs(list.append.__objclass__, list)
def test_not_implemented(self):
# Testing NotImplemented...
diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-06-21-21-42-15.bpo-1617161.tSo2yM.rst b/Misc/NEWS.d/next/Core and Builtins/2018-06-21-21-42-15.bpo-1617161.tSo2yM.rst
new file mode 100644
index 000000000000..bd19944075c1
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2018-06-21-21-42-15.bpo-1617161.tSo2yM.rst
@@ -0,0 +1,7 @@
+The hash of :class:`BuiltinMethodType` instances (methods of built-in classes)
+now depends on the hash of the identity of *__self__* instead of its value.
+The hash and equality of :class:`ModuleType` and :class:`MethodWrapperType`
+instances (methods of user-defined classes and some methods of built-in classes
+like ``str.__add__``) now depend on the hash and equality of the identity
+of *__self__* instead of its value. :class:`MethodWrapperType` instances no
+longer support ordering.
diff --git a/Objects/classobject.c b/Objects/classobject.c
index 095b0cad2b5c..a193ada6d44c 100644
--- a/Objects/classobject.c
+++ b/Objects/classobject.c
@@ -225,13 +225,9 @@ method_richcompare(PyObject *self, PyObject *other, int op)
b = (PyMethodObject *)other;
eq = PyObject_RichCompareBool(a->im_func, b->im_func, Py_EQ);
if (eq == 1) {
- if (a->im_self == NULL || b->im_self == NULL)
- eq = a->im_self == b->im_self;
- else
- eq = PyObject_RichCompareBool(a->im_self, b->im_self,
- Py_EQ);
+ eq = (a->im_self == b->im_self);
}
- if (eq < 0)
+ else if (eq < 0)
return NULL;
if (op == Py_EQ)
res = eq ? Py_True : Py_False;
@@ -274,11 +270,9 @@ method_hash(PyMethodObject *a)
{
Py_hash_t x, y;
if (a->im_self == NULL)
- x = PyObject_Hash(Py_None);
+ x = _Py_HashPointer(Py_None);
else
- x = PyObject_Hash(a->im_self);
- if (x == -1)
- return -1;
+ x = _Py_HashPointer(a->im_self);
y = PyObject_Hash(a->im_func);
if (y == -1)
return -1;
diff --git a/Objects/descrobject.c b/Objects/descrobject.c
index dfad1ecda3ca..b1bee904ec86 100644
--- a/Objects/descrobject.c
+++ b/Objects/descrobject.c
@@ -1038,38 +1038,35 @@ wrapper_dealloc(wrapperobject *wp)
static PyObject *
wrapper_richcompare(PyObject *a, PyObject *b, int op)
{
- PyWrapperDescrObject *a_descr, *b_descr;
+ wrapperobject *wa, *wb;
+ int eq;
assert(a != NULL && b != NULL);
/* both arguments should be wrapperobjects */
- if (!Wrapper_Check(a) || !Wrapper_Check(b)) {
+ if ((op != Py_EQ && op != Py_NE)
+ || !Wrapper_Check(a) || !Wrapper_Check(b))
+ {
Py_RETURN_NOTIMPLEMENTED;
}
- /* compare by descriptor address; if the descriptors are the same,
- compare by the objects they're bound to */
- a_descr = ((wrapperobject *)a)->descr;
- b_descr = ((wrapperobject *)b)->descr;
- if (a_descr == b_descr) {
- a = ((wrapperobject *)a)->self;
- b = ((wrapperobject *)b)->self;
- return PyObject_RichCompare(a, b, op);
+ wa = (wrapperobject *)a;
+ wb = (wrapperobject *)b;
+ eq = (wa->descr == wb->descr && wa->self == wb->self);
+ if (eq == (op == Py_EQ)) {
+ Py_RETURN_TRUE;
+ }
+ else {
+ Py_RETURN_FALSE;
}
-
- Py_RETURN_RICHCOMPARE(a_descr, b_descr, op);
}
static Py_hash_t
wrapper_hash(wrapperobject *wp)
{
Py_hash_t x, y;
- x = _Py_HashPointer(wp->descr);
- if (x == -1)
- return -1;
- y = PyObject_Hash(wp->self);
- if (y == -1)
- return -1;
+ x = _Py_HashPointer(wp->self);
+ y = _Py_HashPointer(wp->descr);
x = x ^ y;
if (x == -1)
x = -2;
diff --git a/Objects/methodobject.c b/Objects/methodobject.c
index 9606768a48c5..a7042ca39e38 100644
--- a/Objects/methodobject.c
+++ b/Objects/methodobject.c
@@ -251,16 +251,8 @@ static Py_hash_t
meth_hash(PyCFunctionObject *a)
{
Py_hash_t x, y;
- if (a->m_self == NULL)
- x = 0;
- else {
- x = PyObject_Hash(a->m_self);
- if (x == -1)
- return -1;
- }
+ x = _Py_HashPointer(a->m_self);
y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
- if (y == -1)
- return -1;
x ^= y;
if (x == -1)
x = -2;
[View Less]
1
0

[2.7] bpo-34234: Use _PyAnyInt_Check() and _PyAnyInt_CheckExact(). (GH-8479)
by Serhiy Storchaka July 31, 2018
by Serhiy Storchaka July 31, 2018
July 31, 2018
https://github.com/python/cpython/commit/48c8bf21f97aeb124dbd48bf2bdec1ab4e…
commit: 48c8bf21f97aeb124dbd48bf2bdec1ab4ebc5202
branch: 2.7
author: Serhiy Storchaka <storchaka(a)gmail.com>
committer: GitHub <noreply(a)github.com>
date: 2018-07-31T09:09:36+03:00
summary:
[2.7] bpo-34234: Use _PyAnyInt_Check() and _PyAnyInt_CheckExact(). (GH-8479)
files:
M Include/intobject.h
M Modules/_csv.c
M Modules/_ctypes/_ctypes.c
M Modules/_ctypes/cfield.c
M Modules/_cursesmodule.c
M Modules/…
[View More]_elementtree.c
M Modules/_json.c
M Modules/_randommodule.c
M Modules/_sqlite/statement.c
M Modules/_sre.c
M Modules/_struct.c
M Modules/_tkinter.c
M Modules/cjkcodecs/multibytecodec.c
M Modules/datetimemodule.c
M Modules/dlmodule.c
M Modules/mathmodule.c
M Modules/posixmodule.c
M Modules/socketmodule.c
M Modules/svmodule.c
M Modules/termios.c
M Objects/abstract.c
M Objects/bytearrayobject.c
M Objects/classobject.c
M Objects/codeobject.c
M Objects/complexobject.c
M Objects/enumobject.c
M Objects/floatobject.c
M Objects/sliceobject.c
M Objects/stringobject.c
M Objects/unicodeobject.c
M PC/_winreg.c
M Parser/asdl_c.py
M Python/Python-ast.c
M Python/bltinmodule.c
M Python/ceval.c
M Python/pythonrun.c
diff --git a/Include/intobject.h b/Include/intobject.h
index 59d061629e25..d1985748b3e3 100644
--- a/Include/intobject.h
+++ b/Include/intobject.h
@@ -31,6 +31,9 @@ PyAPI_DATA(PyTypeObject) PyInt_Type;
PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_INT_SUBCLASS)
#define PyInt_CheckExact(op) (Py_TYPE(op) == &PyInt_Type)
+#define _PyAnyInt_Check(op) (PyInt_Check(op) || PyLong_Check(op))
+#define _PyAnyInt_CheckExact(op) (PyInt_CheckExact(op) || PyLong_CheckExact(op))
+
PyAPI_FUNC(PyObject *) PyInt_FromString(char*, char**, int);
#ifdef Py_USING_UNICODE
PyAPI_FUNC(PyObject *) PyInt_FromUnicode(Py_UNICODE*, Py_ssize_t, int);
diff --git a/Modules/_csv.c b/Modules/_csv.c
index c39c0f10c3cc..ea7d08931c45 100644
--- a/Modules/_csv.c
+++ b/Modules/_csv.c
@@ -224,7 +224,7 @@ _set_int(const char *name, int *target, PyObject *src, int dflt)
if (src == NULL)
*target = dflt;
else {
- if (!PyInt_Check(src) && !PyLong_Check(src)) {
+ if (!_PyAnyInt_Check(src)) {
PyErr_Format(PyExc_TypeError,
"\"%s\" must be an integer", name);
return -1;
@@ -1452,7 +1452,7 @@ csv_field_size_limit(PyObject *module, PyObject *args)
if (!PyArg_UnpackTuple(args, "field_size_limit", 0, 1, &new_limit))
return NULL;
if (new_limit != NULL) {
- if (!PyInt_Check(new_limit) && !PyLong_Check(new_limit)) {
+ if (!_PyAnyInt_Check(new_limit)) {
PyErr_Format(PyExc_TypeError,
"limit must be an integer");
return NULL;
diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
index 25740ed77121..fabbdf13e940 100644
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -547,7 +547,7 @@ static PyObject *
CDataType_from_address(PyObject *type, PyObject *value)
{
void *buf;
- if (!PyInt_Check(value) && !PyLong_Check(value)) {
+ if (!_PyAnyInt_Check(value)) {
PyErr_SetString(PyExc_TypeError,
"integer expected");
return NULL;
@@ -691,7 +691,7 @@ CDataType_in_dll(PyObject *type, PyObject *args)
obj = PyObject_GetAttrString(dll, "_handle");
if (!obj)
return NULL;
- if (!PyInt_Check(obj) && !PyLong_Check(obj)) {
+ if (!_PyAnyInt_Check(obj)) {
PyErr_SetString(PyExc_TypeError,
"the _handle attribute of the second argument must be an integer");
Py_DECREF(obj);
@@ -1779,7 +1779,7 @@ c_void_p_from_param(PyObject *type, PyObject *value)
}
/* Should probably allow buffer interface as well */
/* int, long */
- if (PyInt_Check(value) || PyLong_Check(value)) {
+ if (_PyAnyInt_Check(value)) {
PyCArgObject *parg;
struct fielddesc *fd = _ctypes_get_fielddesc("P");
@@ -3419,7 +3419,7 @@ static int
_get_name(PyObject *obj, char **pname)
{
#ifdef MS_WIN32
- if (PyInt_Check(obj) || PyLong_Check(obj)) {
+ if (_PyAnyInt_Check(obj)) {
/* We have to use MAKEINTRESOURCEA for Windows CE.
Works on Windows as well, of course.
*/
@@ -3469,7 +3469,7 @@ PyCFuncPtr_FromDll(PyTypeObject *type, PyObject *args, PyObject *kwds)
Py_DECREF(ftuple);
return NULL;
}
- if (!PyInt_Check(obj) && !PyLong_Check(obj)) {
+ if (!_PyAnyInt_Check(obj)) {
PyErr_SetString(PyExc_TypeError,
"the _handle attribute of the second argument must be an integer");
Py_DECREF(ftuple);
@@ -3600,8 +3600,7 @@ PyCFuncPtr_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
#endif
if (1 == PyTuple_GET_SIZE(args)
- && (PyInt_Check(PyTuple_GET_ITEM(args, 0))
- || PyLong_Check(PyTuple_GET_ITEM(args, 0)))) {
+ && _PyAnyInt_Check(PyTuple_GET_ITEM(args, 0))) {
CDataObject *ob;
void *ptr = PyLong_AsVoidPtr(PyTuple_GET_ITEM(args, 0));
if (ptr == NULL && PyErr_Occurred())
diff --git a/Modules/_ctypes/cfield.c b/Modules/_ctypes/cfield.c
index 6f632d0a0728..46f041b00e39 100644
--- a/Modules/_ctypes/cfield.c
+++ b/Modules/_ctypes/cfield.c
@@ -1361,7 +1361,7 @@ z_set(void *ptr, PyObject *value, Py_ssize_t size)
return NULL;
*(char **)ptr = PyString_AS_STRING(str);
return str;
- } else if (PyInt_Check(value) || PyLong_Check(value)) {
+ } else if (_PyAnyInt_Check(value)) {
#if SIZEOF_VOID_P == SIZEOF_LONG_LONG
*(char **)ptr = (char *)PyInt_AsUnsignedLongLongMask(value);
#else
@@ -1410,7 +1410,7 @@ Z_set(void *ptr, PyObject *value, Py_ssize_t size)
_ctypes_conversion_errors);
if (!value)
return NULL;
- } else if (PyInt_Check(value) || PyLong_Check(value)) {
+ } else if (_PyAnyInt_Check(value)) {
#if SIZEOF_VOID_P == SIZEOF_LONG_LONG
*(wchar_t **)ptr = (wchar_t *)PyInt_AsUnsignedLongLongMask(value);
#else
@@ -1565,7 +1565,7 @@ P_set(void *ptr, PyObject *value, Py_ssize_t size)
_RET(value);
}
- if (!PyInt_Check(value) && !PyLong_Check(value)) {
+ if (!_PyAnyInt_Check(value)) {
PyErr_SetString(PyExc_TypeError,
"cannot be converted to pointer");
return NULL;
diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c
index 935712a0bed7..0ec4ee1244f6 100644
--- a/Modules/_cursesmodule.c
+++ b/Modules/_cursesmodule.c
@@ -194,7 +194,7 @@ PyCursesCheckERR(int code, char *fname)
static int
PyCurses_ConvertToChtype(PyObject *obj, chtype *ch)
{
- if (PyInt_Check(obj) || PyLong_Check(obj)) {
+ if (_PyAnyInt_Check(obj)) {
*ch = (chtype) PyInt_AsLong(obj);
if (*ch == (chtype) -1 && PyErr_Occurred())
return 0;
@@ -2603,7 +2603,7 @@ PyCurses_UnCtrl(PyObject *self, PyObject *args)
if (!PyArg_ParseTuple(args,"O;ch or int",&temp)) return NULL;
- if (PyInt_Check(temp) || PyLong_Check(temp)) {
+ if (_PyAnyInt_Check(temp)) {
ch = (chtype) PyInt_AsLong(temp);
if (ch == (chtype) -1 && PyErr_Occurred())
return NULL;
@@ -2628,7 +2628,7 @@ PyCurses_UngetCh(PyObject *self, PyObject *args)
if (!PyArg_ParseTuple(args,"O;ch or int",&temp)) return NULL;
- if (PyInt_Check(temp) || PyLong_Check(temp)) {
+ if (_PyAnyInt_Check(temp)) {
ch = (int) PyInt_AsLong(temp);
if (ch == -1 && PyErr_Occurred())
return NULL;
diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c
index 1d316a1c91d2..f7f992dd3a95 100644
--- a/Modules/_elementtree.c
+++ b/Modules/_elementtree.c
@@ -1347,7 +1347,7 @@ element_subscr(PyObject* self_, PyObject* item)
ElementObject* self = (ElementObject*) self_;
#if (PY_VERSION_HEX < 0x02050000)
- if (PyInt_Check(item) || PyLong_Check(item)) {
+ if (_PyAnyInt_Check(item)) {
long i = PyInt_AsLong(item);
#else
if (PyIndex_Check(item)) {
@@ -1404,7 +1404,7 @@ element_ass_subscr(PyObject* self_, PyObject* item, PyObject* value)
ElementObject* self = (ElementObject*) self_;
#if (PY_VERSION_HEX < 0x02050000)
- if (PyInt_Check(item) || PyLong_Check(item)) {
+ if (_PyAnyInt_Check(item)) {
long i = PyInt_AsLong(item);
#else
if (PyIndex_Check(item)) {
diff --git a/Modules/_json.c b/Modules/_json.c
index 39ec467b093e..28c0b3f4570e 100644
--- a/Modules/_json.c
+++ b/Modules/_json.c
@@ -1981,7 +1981,7 @@ encoder_listencode_obj(PyEncoderObject *s, PyObject *rval, PyObject *obj, Py_ssi
return -1;
return _steal_list_append(rval, encoded);
}
- else if (PyInt_Check(obj) || PyLong_Check(obj)) {
+ else if (_PyAnyInt_Check(obj)) {
PyObject *encoded = PyObject_Str(obj);
if (encoded == NULL)
return -1;
@@ -2131,7 +2131,7 @@ encoder_listencode_dict(PyEncoderObject *s, PyObject *rval, PyObject *dct, Py_ss
if (kstr == NULL)
goto bail;
}
- else if (PyInt_Check(key) || PyLong_Check(key)) {
+ else if (_PyAnyInt_Check(key)) {
kstr = PyObject_Str(key);
if (kstr == NULL)
goto bail;
diff --git a/Modules/_randommodule.c b/Modules/_randommodule.c
index 2e49db6ef737..2c0daef71f79 100644
--- a/Modules/_randommodule.c
+++ b/Modules/_randommodule.c
@@ -415,7 +415,7 @@ random_jumpahead(RandomObject *self, PyObject *n)
PyObject *remobj;
unsigned long *mt, tmp, nonzero;
- if (!PyInt_Check(n) && !PyLong_Check(n)) {
+ if (!_PyAnyInt_Check(n)) {
PyErr_Format(PyExc_TypeError, "jumpahead requires an "
"integer, not '%s'",
Py_TYPE(n)->tp_name);
diff --git a/Modules/_sqlite/statement.c b/Modules/_sqlite/statement.c
index 5d6263c8e66c..ec60095ab8c4 100644
--- a/Modules/_sqlite/statement.c
+++ b/Modules/_sqlite/statement.c
@@ -205,7 +205,7 @@ static int _need_adapt(PyObject* obj)
return 1;
}
- if (PyInt_CheckExact(obj) || PyLong_CheckExact(obj)
+ if (_PyAnyInt_CheckExact(obj)
|| PyFloat_CheckExact(obj) || PyString_CheckExact(obj)
|| PyUnicode_CheckExact(obj) || PyBuffer_Check(obj)) {
return 0;
diff --git a/Modules/_sre.c b/Modules/_sre.c
index efef8f508f3d..081a1aa0053f 100644
--- a/Modules/_sre.c
+++ b/Modules/_sre.c
@@ -3327,7 +3327,7 @@ match_getindex(MatchObject* self, PyObject* index)
{
Py_ssize_t i;
- if (PyInt_Check(index) || PyLong_Check(index))
+ if (_PyAnyInt_Check(index))
return PyInt_AsSsize_t(index);
i = -1;
@@ -3335,7 +3335,7 @@ match_getindex(MatchObject* self, PyObject* index)
if (self->pattern->groupindex) {
index = PyObject_GetItem(self->pattern->groupindex, index);
if (index) {
- if (PyInt_Check(index) || PyLong_Check(index))
+ if (_PyAnyInt_Check(index))
i = PyInt_AsSsize_t(index);
Py_DECREF(index);
} else
diff --git a/Modules/_struct.c b/Modules/_struct.c
index d83d57f2e8fa..8b97672070ac 100644
--- a/Modules/_struct.c
+++ b/Modules/_struct.c
@@ -110,7 +110,7 @@ get_pylong(PyObject *v)
PyObject *r, *w;
int converted = 0;
assert(v != NULL);
- if (!PyInt_Check(v) && !PyLong_Check(v)) {
+ if (!_PyAnyInt_Check(v)) {
PyNumberMethods *m;
/* Not an integer; first try to use __index__ to
convert to an integer. If the __index__ method
@@ -150,7 +150,7 @@ get_pylong(PyObject *v)
v = m->nb_int(v);
if (v == NULL)
return NULL;
- if (!PyInt_Check(v) && !PyLong_Check(v)) {
+ if (!_PyAnyInt_Check(v)) {
PyErr_SetString(PyExc_TypeError,
"__int__ method returned "
"non-integer");
@@ -169,7 +169,7 @@ get_pylong(PyObject *v)
/* Ensure we own a reference to v. */
Py_INCREF(v);
- assert(PyInt_Check(v) || PyLong_Check(v));
+ assert(_PyAnyInt_Check(v));
if (PyInt_Check(v)) {
r = PyLong_FromLong(PyInt_AS_LONG(v));
Py_DECREF(v);
diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c
index 444c268c0b47..c71ffd016ad8 100644
--- a/Modules/_tkinter.c
+++ b/Modules/_tkinter.c
@@ -2107,7 +2107,7 @@ Tkapp_GetInt(PyObject *self, PyObject *args)
if (PyTuple_Size(args) == 1) {
PyObject* o = PyTuple_GetItem(args, 0);
- if (PyInt_Check(o) || PyLong_Check(o)) {
+ if (_PyAnyInt_Check(o)) {
Py_INCREF(o);
return o;
}
diff --git a/Modules/cjkcodecs/multibytecodec.c b/Modules/cjkcodecs/multibytecodec.c
index 8901b42e41e8..4b482bf9e251 100644
--- a/Modules/cjkcodecs/multibytecodec.c
+++ b/Modules/cjkcodecs/multibytecodec.c
@@ -310,8 +310,7 @@ multibytecodec_encerror(MultibyteCodec *codec,
if (!PyTuple_Check(retobj) || PyTuple_GET_SIZE(retobj) != 2 ||
!PyUnicode_Check((tobj = PyTuple_GET_ITEM(retobj, 0))) ||
- !(PyInt_Check(PyTuple_GET_ITEM(retobj, 1)) ||
- PyLong_Check(PyTuple_GET_ITEM(retobj, 1)))) {
+ !_PyAnyInt_Check(PyTuple_GET_ITEM(retobj, 1))) {
PyErr_SetString(PyExc_TypeError,
"encoding error handler must return "
"(unicode, int) tuple");
@@ -430,8 +429,7 @@ multibytecodec_decerror(MultibyteCodec *codec,
if (!PyTuple_Check(retobj) || PyTuple_GET_SIZE(retobj) != 2 ||
!PyUnicode_Check((retuni = PyTuple_GET_ITEM(retobj, 0))) ||
- !(PyInt_Check(PyTuple_GET_ITEM(retobj, 1)) ||
- PyLong_Check(PyTuple_GET_ITEM(retobj, 1)))) {
+ !_PyAnyInt_Check(PyTuple_GET_ITEM(retobj, 1))) {
PyErr_SetString(PyExc_TypeError,
"decoding error handler must return "
"(unicode, int) tuple");
diff --git a/Modules/datetimemodule.c b/Modules/datetimemodule.c
index e818c5cfdc4d..c0b7102de234 100644
--- a/Modules/datetimemodule.c
+++ b/Modules/datetimemodule.c
@@ -1537,7 +1537,7 @@ delta_to_microseconds(PyDateTime_Delta *self)
if (x2 == NULL)
goto Done;
result = PyNumber_Add(x1, x2);
- assert(result == NULL || PyInt_CheckExact(result) || PyLong_CheckExact(result));
+ assert(result == NULL || _PyAnyInt_CheckExact(result));
Done:
Py_XDECREF(x1);
@@ -1560,7 +1560,7 @@ microseconds_to_delta_ex(PyObject *pyus, PyTypeObject *type)
PyObject *num = NULL;
PyObject *result = NULL;
- assert(PyInt_CheckExact(pyus) || PyLong_CheckExact(pyus));
+ assert(_PyAnyInt_CheckExact(pyus));
tuple = PyNumber_Divmod(pyus, us_per_second);
if (tuple == NULL)
goto Done;
@@ -1803,11 +1803,11 @@ delta_multiply(PyObject *left, PyObject *right)
if (PyDelta_Check(left)) {
/* delta * ??? */
- if (PyInt_Check(right) || PyLong_Check(right))
+ if (_PyAnyInt_Check(right))
result = multiply_int_timedelta(right,
(PyDateTime_Delta *) left);
}
- else if (PyInt_Check(left) || PyLong_Check(left))
+ else if (_PyAnyInt_Check(left))
result = multiply_int_timedelta(left,
(PyDateTime_Delta *) right);
@@ -1823,7 +1823,7 @@ delta_divide(PyObject *left, PyObject *right)
if (PyDelta_Check(left)) {
/* delta * ??? */
- if (PyInt_Check(right) || PyLong_Check(right))
+ if (_PyAnyInt_Check(right))
result = divide_timedelta_int(
(PyDateTime_Delta *)left,
right);
@@ -1852,14 +1852,14 @@ accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor,
assert(num != NULL);
- if (PyInt_Check(num) || PyLong_Check(num)) {
+ if (_PyAnyInt_Check(num)) {
prod = PyNumber_Multiply(factor, num);
if (prod == NULL)
return NULL;
- assert(PyInt_CheckExact(prod) || PyLong_CheckExact(prod));
+ assert(_PyAnyInt_CheckExact(prod));
sum = PyNumber_Add(sofar, prod);
Py_DECREF(prod);
- assert(sum == NULL || PyInt_CheckExact(sum) || PyLong_CheckExact(sum));
+ assert(sum == NULL || _PyAnyInt_CheckExact(sum));
return sum;
}
@@ -1902,7 +1902,7 @@ accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor,
* fractional part requires float arithmetic, and may
* lose a little info.
*/
- assert(PyInt_CheckExact(factor) || PyLong_CheckExact(factor));
+ assert(_PyAnyInt_CheckExact(factor));
if (PyInt_Check(factor))
dnum = (double)PyInt_AsLong(factor);
else
@@ -1920,7 +1920,7 @@ accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor,
Py_DECREF(sum);
Py_DECREF(x);
*leftover += fracpart;
- assert(y == NULL || PyInt_CheckExact(y) || PyLong_CheckExact(y));
+ assert(y == NULL || _PyAnyInt_CheckExact(y));
return y;
}
diff --git a/Modules/dlmodule.c b/Modules/dlmodule.c
index 7a6686e3a6df..3f150487007c 100644
--- a/Modules/dlmodule.c
+++ b/Modules/dlmodule.c
@@ -107,7 +107,7 @@ dl_call(dlobject *xp, PyObject *args)
}
for (i = 1; i < n; i++) {
PyObject *v = PyTuple_GetItem(args, i);
- if (PyInt_Check(v) || PyLong_Check(v)) {
+ if (_PyAnyInt_Check(v)) {
alist[i-1] = PyInt_AsLong(v);
if (alist[i-1] == -1 && PyErr_Occurred())
return NULL;
diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c
index 01ed36b2137d..67354a759498 100644
--- a/Modules/mathmodule.c
+++ b/Modules/mathmodule.c
@@ -1188,7 +1188,7 @@ math_ldexp(PyObject *self, PyObject *args)
if (! PyArg_ParseTuple(args, "dO:ldexp", &x, &oexp))
return NULL;
- if (PyLong_Check(oexp) || PyInt_Check(oexp)) {
+ if (_PyAnyInt_Check(oexp)) {
/* on overflow, replace exponent with either LONG_MAX
or LONG_MIN, depending on the sign. */
exp = PyLong_AsLongAndOverflow(oexp, &overflow);
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 2baf9202c303..7a1a6945c102 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -6121,7 +6121,7 @@ posix_setgroups(PyObject *self, PyObject *groups)
elem = PySequence_GetItem(groups, i);
if (!elem)
return NULL;
- if (!PyInt_Check(elem) && !PyLong_Check(elem)) {
+ if (!_PyAnyInt_Check(elem)) {
PyErr_SetString(PyExc_TypeError,
"groups must be integers");
Py_DECREF(elem);
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index 8d3670529e85..634c1b90778b 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -4194,7 +4194,7 @@ socket_getaddrinfo(PyObject *self, PyObject *args)
"getaddrinfo() argument 1 must be string or None");
return NULL;
}
- if (PyInt_Check(pobj) || PyLong_Check(pobj)) {
+ if (_PyAnyInt_Check(pobj)) {
long value = PyLong_AsLong(pobj);
if (value == -1 && PyErr_Occurred())
return NULL;
diff --git a/Modules/svmodule.c b/Modules/svmodule.c
index 42c49c853a00..14f236cb1620 100644
--- a/Modules/svmodule.c
+++ b/Modules/svmodule.c
@@ -686,7 +686,7 @@ sv_LoadMap(svobject *self, PyObject *args)
if (!cell)
goto finally;
- if (!PyInt_Check(cell) && !PyLong_Check(cell)) {
+ if (!_PyAnyInt_Check(cell)) {
PyErr_BadArgument();
goto finally;
}
@@ -757,7 +757,7 @@ doParams(svobject *self, PyObject *args,
if (!v)
goto finally;
- if (!PyInt_Check(v) && !PyLong_Check(v)) {
+ if (!_PyAnyInt_Check(v)) {
PyErr_BadArgument();
goto finally;
}
diff --git a/Modules/termios.c b/Modules/termios.c
index 9d4d780dca27..e26e714e0ec1 100644
--- a/Modules/termios.c
+++ b/Modules/termios.c
@@ -185,7 +185,7 @@ termios_tcsetattr(PyObject *self, PyObject *args)
if (PyString_Check(v) && PyString_Size(v) == 1)
mode.c_cc[i] = (cc_t) * PyString_AsString(v);
- else if (PyInt_Check(v) || PyLong_Check(v)) {
+ else if (_PyAnyInt_Check(v)) {
mode.c_cc[i] = (cc_t) PyInt_AsLong(v);
if (mode.c_cc[i] == (cc_t) -1 && PyErr_Occurred())
return NULL;
diff --git a/Objects/abstract.c b/Objects/abstract.c
index aa92ea9156d3..75c1a1047357 100644
--- a/Objects/abstract.c
+++ b/Objects/abstract.c
@@ -1492,14 +1492,13 @@ PyNumber_Index(PyObject *item)
PyObject *result = NULL;
if (item == NULL)
return null_error();
- if (PyInt_Check(item) || PyLong_Check(item)) {
+ if (_PyAnyInt_Check(item)) {
Py_INCREF(item);
return item;
}
if (PyIndex_Check(item)) {
result = item->ob_type->tp_as_number->nb_index(item);
- if (result &&
- !PyInt_Check(result) && !PyLong_Check(result)) {
+ if (result && !_PyAnyInt_Check(result)) {
PyErr_Format(PyExc_TypeError,
"__index__ returned non-(int,long) " \
"(type %.200s)",
@@ -1574,8 +1573,7 @@ _PyNumber_ConvertIntegralToInt(PyObject *integral, const char* error_format)
return NULL;
}
- if (integral && (!PyInt_Check(integral) &&
- !PyLong_Check(integral))) {
+ if (integral && !_PyAnyInt_Check(integral)) {
/* Don't go through tp_as_number->nb_int to avoid
hitting the classic class fallback to __trunc__. */
PyObject *int_func = PyObject_GetAttr(integral, int_name);
@@ -1586,8 +1584,7 @@ _PyNumber_ConvertIntegralToInt(PyObject *integral, const char* error_format)
Py_DECREF(integral);
integral = PyEval_CallObject(int_func, NULL);
Py_DECREF(int_func);
- if (integral && (!PyInt_Check(integral) &&
- !PyLong_Check(integral))) {
+ if (integral && !_PyAnyInt_Check(integral)) {
goto non_integral_error;
}
}
@@ -1632,7 +1629,7 @@ PyNumber_Int(PyObject *o)
if (m && m->nb_int) { /* This should include subclasses of int */
/* Classic classes always take this branch. */
PyObject *res = m->nb_int(o);
- if (res && (!PyInt_Check(res) && !PyLong_Check(res))) {
+ if (res && !_PyAnyInt_Check(res)) {
PyErr_Format(PyExc_TypeError,
"__int__ returned non-int (type %.200s)",
res->ob_type->tp_name);
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
index 04c25061ef52..c178d9ef65d4 100644
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -35,7 +35,7 @@ _getbytevalue(PyObject* arg, int *value)
*value = Py_CHARMASK(((PyBytesObject*)arg)->ob_sval[0]);
return 1;
}
- else if (PyInt_Check(arg) || PyLong_Check(arg)) {
+ else if (_PyAnyInt_Check(arg)) {
face_value = PyLong_AsLong(arg);
}
else {
diff --git a/Objects/classobject.c b/Objects/classobject.c
index 5b645787cbf6..02d7cfd019b9 100644
--- a/Objects/classobject.c
+++ b/Objects/classobject.c
@@ -1009,7 +1009,7 @@ instance_hash(PyInstanceObject *inst)
Py_DECREF(func);
if (res == NULL)
return -1;
- if (PyInt_Check(res) || PyLong_Check(res))
+ if (_PyAnyInt_Check(res))
/* This already converts a -1 result to -2. */
outcome = Py_TYPE(res)->tp_hash(res);
else {
diff --git a/Objects/codeobject.c b/Objects/codeobject.c
index a66aa6905a92..d50e4c61f088 100644
--- a/Objects/codeobject.c
+++ b/Objects/codeobject.c
@@ -423,10 +423,9 @@ _PyCode_ConstantKey(PyObject *op)
/* Py_None is a singleton */
if (op == Py_None
- || PyInt_CheckExact(op)
- || PyLong_CheckExact(op)
- || PyBool_Check(op)
- || PyBytes_CheckExact(op)
+ || _PyAnyInt_CheckExact(op)
+ || PyBool_Check(op)
+ || PyBytes_CheckExact(op)
#ifdef Py_USING_UNICODE
|| PyUnicode_CheckExact(op)
#endif
diff --git a/Objects/complexobject.c b/Objects/complexobject.c
index aaefa2dcda8a..871eea319f4c 100644
--- a/Objects/complexobject.c
+++ b/Objects/complexobject.c
@@ -796,7 +796,7 @@ complex_richcompare(PyObject *v, PyObject *w, int op)
* NotImplemented. Only comparisons with core numeric types raise
* TypeError.
*/
- if (PyInt_Check(w) || PyLong_Check(w) ||
+ if (_PyAnyInt_Check(w) ||
PyFloat_Check(w) || PyComplex_Check(w)) {
PyErr_SetString(PyExc_TypeError,
"no ordering relation is defined "
@@ -809,7 +809,7 @@ complex_richcompare(PyObject *v, PyObject *w, int op)
assert(PyComplex_Check(v));
TO_COMPLEX(v, i);
- if (PyInt_Check(w) || PyLong_Check(w)) {
+ if (_PyAnyInt_Check(w)) {
/* Check for 0.0 imaginary part first to avoid the rich
* comparison when possible.
*/
diff --git a/Objects/enumobject.c b/Objects/enumobject.c
index 8f86a5b8de39..73b656b8e08b 100644
--- a/Objects/enumobject.c
+++ b/Objects/enumobject.c
@@ -31,7 +31,7 @@ enum_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Py_DECREF(en);
return NULL;
}
- assert(PyInt_Check(start) || PyLong_Check(start));
+ assert(_PyAnyInt_Check(start));
en->en_index = PyInt_AsSsize_t(start);
if (en->en_index == -1 && PyErr_Occurred()) {
PyErr_Clear();
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
index 37dd67e66938..5954d39cdb33 100644
--- a/Objects/floatobject.c
+++ b/Objects/floatobject.c
@@ -428,7 +428,7 @@ float_richcompare(PyObject *v, PyObject *w, int op)
j = PyFloat_AS_DOUBLE(w);
else if (!Py_IS_FINITE(i)) {
- if (PyInt_Check(w) || PyLong_Check(w))
+ if (_PyAnyInt_Check(w))
/* If i is an infinity, its magnitude exceeds any
* finite integer, so it doesn't matter which int we
* compare i with. If i is a NaN, similarly.
diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c
index 9daeafcc4a18..dc1821157375 100644
--- a/Objects/sliceobject.c
+++ b/Objects/sliceobject.c
@@ -107,20 +107,20 @@ PySlice_GetIndices(PySliceObject *r, Py_ssize_t length,
if (r->step == Py_None) {
*step = 1;
} else {
- if (!PyInt_Check(r->step) && !PyLong_Check(r->step)) return -1;
+ if (!_PyAnyInt_Check(r->step)) return -1;
*step = PyInt_AsSsize_t(r->step);
}
if (r->start == Py_None) {
*start = *step < 0 ? length-1 : 0;
} else {
- if (!PyInt_Check(r->start) && !PyLong_Check(r->start)) return -1;
+ if (!_PyAnyInt_Check(r->start)) return -1;
*start = PyInt_AsSsize_t(r->start);
if (*start < 0) *start += length;
}
if (r->stop == Py_None) {
*stop = *step < 0 ? -1 : length;
} else {
- if (!PyInt_Check(r->stop) && !PyLong_Check(r->stop)) return -1;
+ if (!_PyAnyInt_Check(r->stop)) return -1;
*stop = PyInt_AsSsize_t(r->stop);
if (*stop < 0) *stop += length;
}
diff --git a/Objects/stringobject.c b/Objects/stringobject.c
index 59d22e76946b..b21afb4424d1 100644
--- a/Objects/stringobject.c
+++ b/Objects/stringobject.c
@@ -4504,7 +4504,7 @@ PyString_Format(PyObject *format, PyObject *args)
if (PyNumber_Check(v)) {
PyObject *iobj=NULL;
- if (PyInt_Check(v) || (PyLong_Check(v))) {
+ if (_PyAnyInt_Check(v)) {
iobj = v;
Py_INCREF(iobj);
}
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index d011f7d50c2b..8bf04df2c86c 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -8628,7 +8628,7 @@ PyObject *PyUnicode_Format(PyObject *format,
if (PyNumber_Check(v)) {
PyObject *iobj=NULL;
- if (PyInt_Check(v) || (PyLong_Check(v))) {
+ if (_PyAnyInt_Check(v)) {
iobj = v;
Py_INCREF(iobj);
}
diff --git a/PC/_winreg.c b/PC/_winreg.c
index e139cce85f82..f0f8df33107b 100644
--- a/PC/_winreg.c
+++ b/PC/_winreg.c
@@ -641,7 +641,7 @@ PyHKEY_AsHKEY(PyObject *ob, HKEY *pHANDLE, BOOL bNoneOK)
PyHKEYObject *pH = (PyHKEYObject *)ob;
*pHANDLE = pH->hkey;
}
- else if (PyInt_Check(ob) || PyLong_Check(ob)) {
+ else if (_PyAnyInt_Check(ob)) {
/* We also support integers */
PyErr_Clear();
*pHANDLE = (HKEY)PyLong_AsVoidPtr(ob);
@@ -753,8 +753,7 @@ Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize)
Py_ssize_t i,j;
switch (typ) {
case REG_DWORD:
- if (value != Py_None &&
- !(PyInt_Check(value) || PyLong_Check(value)))
+ if (value != Py_None && !_PyAnyInt_Check(value))
return FALSE;
*retDataBuf = (BYTE *)PyMem_NEW(DWORD, 1);
if (*retDataBuf==NULL){
diff --git a/Parser/asdl_c.py b/Parser/asdl_c.py
index 08353a9e7f5d..ac61c78afc4a 100755
--- a/Parser/asdl_c.py
+++ b/Parser/asdl_c.py
@@ -830,7 +830,7 @@ def visitModule(self, mod):
static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
{
int i;
- if (!PyInt_Check(obj) && !PyLong_Check(obj)) {
+ if (!_PyAnyInt_Check(obj)) {
PyObject *s = PyObject_Repr(obj);
if (s == NULL) return 1;
PyErr_Format(PyExc_ValueError, "invalid integer value: %.400s",
diff --git a/Python/Python-ast.c b/Python/Python-ast.c
index 4ac5cf578fb7..2e7a1afbbc27 100644
--- a/Python/Python-ast.c
+++ b/Python/Python-ast.c
@@ -617,7 +617,7 @@ static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
{
int i;
- if (!PyInt_Check(obj) && !PyLong_Check(obj)) {
+ if (!_PyAnyInt_Check(obj)) {
PyObject *s = PyObject_Repr(obj);
if (s == NULL) return 1;
PyErr_Format(PyExc_ValueError, "invalid integer value: %.400s",
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index 9ce3b275eab2..f19115d2cb3f 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -1780,7 +1780,7 @@ get_range_long_argument(PyObject *arg, const char *name)
{
PyObject *v;
PyNumberMethods *nb;
- if (PyInt_Check(arg) || PyLong_Check(arg)) {
+ if (_PyAnyInt_Check(arg)) {
Py_INCREF(arg);
return arg;
}
@@ -1795,7 +1795,7 @@ get_range_long_argument(PyObject *arg, const char *name)
v = nb->nb_int(arg);
if (v == NULL)
return NULL;
- if (PyInt_Check(v) || PyLong_Check(v))
+ if (_PyAnyInt_Check(v))
return v;
Py_DECREF(v);
PyErr_SetString(PyExc_TypeError,
diff --git a/Python/ceval.c b/Python/ceval.c
index b55b4d66880c..2088a271d86e 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -4750,8 +4750,7 @@ _PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
#undef ISINDEX
-#define ISINDEX(x) ((x) == NULL || \
- PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x))
+#define ISINDEX(x) ((x) == NULL || _PyAnyInt_Check(x) || PyIndex_Check(x))
static PyObject *
apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index c33b6c00efbb..5707c9f52452 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -1136,7 +1136,7 @@ handle_system_exit(void)
/* If we failed to dig out the 'code' attribute,
just let the else clause below print the error. */
}
- if (PyInt_Check(value) || PyLong_Check(value))
+ if (_PyAnyInt_Check(value))
exitcode = (int)PyInt_AsLong(value);
else {
PyObject *sys_stderr = PySys_GetObject("stderr");
[View Less]
1
0

July 31, 2018
https://github.com/python/cpython/commit/dc9039da239ee572eaaf56e4a026be1fc4…
commit: dc9039da239ee572eaaf56e4a026be1fc4d74e24
branch: 2.7
author: Miss Islington (bot) <31488909+miss-islington(a)users.noreply.github.com>
committer: GitHub <noreply(a)github.com>
date: 2018-07-30T22:58:12-07:00
summary:
bpo-27671: Update FAQ about why len is function (GH-8432)
(cherry picked from commit c48e26dcadbff8620bb5881d3bd148fc8894d0ef)
Co-authored-by: INADA Naoki <methane(a)users.noreply.…
[View More]github.com>
files:
M Doc/faq/design.rst
diff --git a/Doc/faq/design.rst b/Doc/faq/design.rst
index 285c7f12b9c5..8588daaa2f58 100644
--- a/Doc/faq/design.rst
+++ b/Doc/faq/design.rst
@@ -226,24 +226,25 @@ Python file objects support the iterator protocol, so you can now write simply::
Why does Python use methods for some functionality (e.g. list.index()) but functions for other (e.g. len(list))?
----------------------------------------------------------------------------------------------------------------
-The major reason is history. Functions were used for those operations that were
-generic for a group of types and which were intended to work even for objects
-that didn't have methods at all (e.g. tuples). It is also convenient to have a
-function that can readily be applied to an amorphous collection of objects when
-you use the functional features of Python (``map()``, ``zip()`` et al).
-
-In fact, implementing ``len()``, ``max()``, ``min()`` as a built-in function is
-actually less code than implementing them as methods for each type. One can
-quibble about individual cases but it's a part of Python, and it's too late to
-make such fundamental changes now. The functions have to remain to avoid massive
-code breakage.
-
-.. XXX talk about protocols?
-
-.. note::
-
- For string operations, Python has moved from external functions (the
- ``string`` module) to methods. However, ``len()`` is still a function.
+As Guido said:
+
+ (a) For some operations, prefix notation just reads better than
+ postfix -- prefix (and infix!) operations have a long tradition in
+ mathematics which likes notations where the visuals help the
+ mathematician thinking about a problem. Compare the easy with which we
+ rewrite a formula like x*(a+b) into x*a + x*b to the clumsiness of
+ doing the same thing using a raw OO notation.
+
+ (b) When I read code that says len(x) I *know* that it is asking for
+ the length of something. This tells me two things: the result is an
+ integer, and the argument is some kind of container. To the contrary,
+ when I read x.len(), I have to already know that x is some kind of
+ container implementing an interface or inheriting from a class that
+ has a standard len(). Witness the confusion we occasionally have when
+ a class that is not implementing a mapping has a get() or keys()
+ method, or something that isn't a file has a write() method.
+
+ -- https://mail.python.org/pipermail/python-3000/2006-November/004643.html
Why is join() a string method instead of a list or tuple method?
[View Less]
1
0

July 31, 2018
https://github.com/python/cpython/commit/0b376eb0d63e0c51ed55c620b40edae6de…
commit: 0b376eb0d63e0c51ed55c620b40edae6ded4ea48
branch: 3.6
author: Miss Islington (bot) <31488909+miss-islington(a)users.noreply.github.com>
committer: GitHub <noreply(a)github.com>
date: 2018-07-30T22:56:27-07:00
summary:
bpo-27671: Update FAQ about why len is function (GH-8432)
(cherry picked from commit c48e26dcadbff8620bb5881d3bd148fc8894d0ef)
Co-authored-by: INADA Naoki <methane(a)users.noreply.…
[View More]github.com>
files:
M Doc/faq/design.rst
diff --git a/Doc/faq/design.rst b/Doc/faq/design.rst
index bb20f04c5554..234dc9c12b5e 100644
--- a/Doc/faq/design.rst
+++ b/Doc/faq/design.rst
@@ -215,24 +215,25 @@ objects using the ``for`` statement. For example, :term:`file objects
Why does Python use methods for some functionality (e.g. list.index()) but functions for other (e.g. len(list))?
----------------------------------------------------------------------------------------------------------------
-The major reason is history. Functions were used for those operations that were
-generic for a group of types and which were intended to work even for objects
-that didn't have methods at all (e.g. tuples). It is also convenient to have a
-function that can readily be applied to an amorphous collection of objects when
-you use the functional features of Python (``map()``, ``zip()`` et al).
-
-In fact, implementing ``len()``, ``max()``, ``min()`` as a built-in function is
-actually less code than implementing them as methods for each type. One can
-quibble about individual cases but it's a part of Python, and it's too late to
-make such fundamental changes now. The functions have to remain to avoid massive
-code breakage.
-
-.. XXX talk about protocols?
-
-.. note::
-
- For string operations, Python has moved from external functions (the
- ``string`` module) to methods. However, ``len()`` is still a function.
+As Guido said:
+
+ (a) For some operations, prefix notation just reads better than
+ postfix -- prefix (and infix!) operations have a long tradition in
+ mathematics which likes notations where the visuals help the
+ mathematician thinking about a problem. Compare the easy with which we
+ rewrite a formula like x*(a+b) into x*a + x*b to the clumsiness of
+ doing the same thing using a raw OO notation.
+
+ (b) When I read code that says len(x) I *know* that it is asking for
+ the length of something. This tells me two things: the result is an
+ integer, and the argument is some kind of container. To the contrary,
+ when I read x.len(), I have to already know that x is some kind of
+ container implementing an interface or inheriting from a class that
+ has a standard len(). Witness the confusion we occasionally have when
+ a class that is not implementing a mapping has a get() or keys()
+ method, or something that isn't a file has a write() method.
+
+ -- https://mail.python.org/pipermail/python-3000/2006-November/004643.html
Why is join() a string method instead of a list or tuple method?
[View Less]
1
0

July 31, 2018
https://github.com/python/cpython/commit/a621f406402e05febb302cf31962e9d2d7…
commit: a621f406402e05febb302cf31962e9d2d747d8f6
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington(a)users.noreply.github.com>
committer: GitHub <noreply(a)github.com>
date: 2018-07-30T22:54:25-07:00
summary:
bpo-27671: Update FAQ about why len is function (GH-8432)
(cherry picked from commit c48e26dcadbff8620bb5881d3bd148fc8894d0ef)
Co-authored-by: INADA Naoki <methane(a)users.noreply.…
[View More]github.com>
files:
M Doc/faq/design.rst
diff --git a/Doc/faq/design.rst b/Doc/faq/design.rst
index 10fa490931c5..5e54df61bda9 100644
--- a/Doc/faq/design.rst
+++ b/Doc/faq/design.rst
@@ -215,24 +215,25 @@ objects using the ``for`` statement. For example, :term:`file objects
Why does Python use methods for some functionality (e.g. list.index()) but functions for other (e.g. len(list))?
----------------------------------------------------------------------------------------------------------------
-The major reason is history. Functions were used for those operations that were
-generic for a group of types and which were intended to work even for objects
-that didn't have methods at all (e.g. tuples). It is also convenient to have a
-function that can readily be applied to an amorphous collection of objects when
-you use the functional features of Python (``map()``, ``zip()`` et al).
-
-In fact, implementing ``len()``, ``max()``, ``min()`` as a built-in function is
-actually less code than implementing them as methods for each type. One can
-quibble about individual cases but it's a part of Python, and it's too late to
-make such fundamental changes now. The functions have to remain to avoid massive
-code breakage.
-
-.. XXX talk about protocols?
-
-.. note::
-
- For string operations, Python has moved from external functions (the
- ``string`` module) to methods. However, ``len()`` is still a function.
+As Guido said:
+
+ (a) For some operations, prefix notation just reads better than
+ postfix -- prefix (and infix!) operations have a long tradition in
+ mathematics which likes notations where the visuals help the
+ mathematician thinking about a problem. Compare the easy with which we
+ rewrite a formula like x*(a+b) into x*a + x*b to the clumsiness of
+ doing the same thing using a raw OO notation.
+
+ (b) When I read code that says len(x) I *know* that it is asking for
+ the length of something. This tells me two things: the result is an
+ integer, and the argument is some kind of container. To the contrary,
+ when I read x.len(), I have to already know that x is some kind of
+ container implementing an interface or inheriting from a class that
+ has a standard len(). Witness the confusion we occasionally have when
+ a class that is not implementing a mapping has a get() or keys()
+ method, or something that isn't a file has a write() method.
+
+ -- https://mail.python.org/pipermail/python-3000/2006-November/004643.html
Why is join() a string method instead of a list or tuple method?
[View Less]
1
0

July 31, 2018
https://github.com/python/cpython/commit/c48e26dcadbff8620bb5881d3bd148fc88…
commit: c48e26dcadbff8620bb5881d3bd148fc8894d0ef
branch: master
author: INADA Naoki <methane(a)users.noreply.github.com>
committer: GitHub <noreply(a)github.com>
date: 2018-07-31T14:49:22+09:00
summary:
bpo-27671: Update FAQ about why len is function (GH-8432)
files:
M Doc/faq/design.rst
diff --git a/Doc/faq/design.rst b/Doc/faq/design.rst
index 10fa490931c5..5e54df61bda9 100644
--- a/Doc/faq/design.rst
+…
[View More]++ b/Doc/faq/design.rst
@@ -215,24 +215,25 @@ objects using the ``for`` statement. For example, :term:`file objects
Why does Python use methods for some functionality (e.g. list.index()) but functions for other (e.g. len(list))?
----------------------------------------------------------------------------------------------------------------
-The major reason is history. Functions were used for those operations that were
-generic for a group of types and which were intended to work even for objects
-that didn't have methods at all (e.g. tuples). It is also convenient to have a
-function that can readily be applied to an amorphous collection of objects when
-you use the functional features of Python (``map()``, ``zip()`` et al).
-
-In fact, implementing ``len()``, ``max()``, ``min()`` as a built-in function is
-actually less code than implementing them as methods for each type. One can
-quibble about individual cases but it's a part of Python, and it's too late to
-make such fundamental changes now. The functions have to remain to avoid massive
-code breakage.
-
-.. XXX talk about protocols?
-
-.. note::
-
- For string operations, Python has moved from external functions (the
- ``string`` module) to methods. However, ``len()`` is still a function.
+As Guido said:
+
+ (a) For some operations, prefix notation just reads better than
+ postfix -- prefix (and infix!) operations have a long tradition in
+ mathematics which likes notations where the visuals help the
+ mathematician thinking about a problem. Compare the easy with which we
+ rewrite a formula like x*(a+b) into x*a + x*b to the clumsiness of
+ doing the same thing using a raw OO notation.
+
+ (b) When I read code that says len(x) I *know* that it is asking for
+ the length of something. This tells me two things: the result is an
+ integer, and the argument is some kind of container. To the contrary,
+ when I read x.len(), I have to already know that x is some kind of
+ container implementing an interface or inheriting from a class that
+ has a standard len(). Witness the confusion we occasionally have when
+ a class that is not implementing a mapping has a get() or keys()
+ method, or something that isn't a file has a write() method.
+
+ -- https://mail.python.org/pipermail/python-3000/2006-November/004643.html
Why is join() a string method instead of a list or tuple method?
[View Less]
1
0

Remove creation of a list for row_cast_map in pysqlite_cursor_init() (GH-8494)
by Berker Peksag July 30, 2018
by Berker Peksag July 30, 2018
July 30, 2018
https://github.com/python/cpython/commit/b229b072a9e972905883da5e9eed1f22ab…
commit: b229b072a9e972905883da5e9eed1f22ab81c7b7
branch: master
author: Sergey Fedoseev <fedoseev.sergey(a)gmail.com>
committer: Berker Peksag <berker.peksag(a)gmail.com>
date: 2018-07-31T00:11:50+03:00
summary:
Remove creation of a list for row_cast_map in pysqlite_cursor_init() (GH-8494)
This list is never used: if detect_types is on, this list will be
replaced with another one before row_cast_map is …
[View More]used, if
detect_types is off, row_cast_map is not used at all.
files:
M Modules/_sqlite/cursor.c
diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c
index 70b304f4e3fb..c62ad5d64eec 100644
--- a/Modules/_sqlite/cursor.c
+++ b/Modules/_sqlite/cursor.c
@@ -42,11 +42,7 @@ static int pysqlite_cursor_init(pysqlite_Cursor* self, PyObject* args, PyObject*
Py_XSETREF(self->connection, connection);
Py_CLEAR(self->statement);
Py_CLEAR(self->next_row);
-
- Py_XSETREF(self->row_cast_map, PyList_New(0));
- if (!self->row_cast_map) {
- return -1;
- }
+ Py_CLEAR(self->row_cast_map);
Py_INCREF(Py_None);
Py_XSETREF(self->description, Py_None);
@@ -253,6 +249,7 @@ PyObject* _pysqlite_fetch_one_row(pysqlite_Cursor* self)
for (i = 0; i < numcols; i++) {
if (self->connection->detect_types) {
+ assert(self->row_cast_map != NULL);
converter = PyList_GetItem(self->row_cast_map, i);
if (!converter) {
converter = Py_None;
[View Less]
1
0