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

Skip to content

Commit d8b690f

Browse files
committed
python#2895: don't crash with bytes as keyword argument names.
1 parent bf82e37 commit d8b690f

3 files changed

Lines changed: 11 additions & 1 deletion

File tree

Lib/test/test_grammar.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,14 @@ def d22v(a, b, c=1, d=2, *rest): pass
265265
d22v(*(1, 2, 3, 4))
266266
d22v(1, 2, *(3, 4, 5))
267267
d22v(1, *(2, 3), **{'d': 4})
268+
269+
# keyword argument type tests
270+
try:
271+
str('x', **{b'foo':1 })
272+
except TypeError:
273+
pass
274+
else:
275+
self.fail('Bytes should not work as keyword argument names')
268276
# keyword only argument tests
269277
def pos0key1(*, key): return key
270278
pos0key1(key=100)

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ What's new in Python 3.0b1?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue 2895: Don't crash when given bytes objects as keyword names.
16+
1517
- Issue 2798: When parsing arguments with PyArg_ParseTuple, the "s" code now
1618
allows any unicode string and returns a utf-8 encoded buffer, just like the
1719
"s#" code already does. The "z" code was corrected as well.

Python/getargs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1532,7 +1532,7 @@ vgetargskeywords(PyObject *args, PyObject *keywords, const char *format,
15321532
while (PyDict_Next(keywords, &pos, &key, &value)) {
15331533
int match = 0;
15341534
char *ks;
1535-
if (!PyString_Check(key) && !PyUnicode_Check(key)) {
1535+
if (!PyUnicode_Check(key)) {
15361536
PyErr_SetString(PyExc_TypeError,
15371537
"keywords must be strings");
15381538
return cleanreturn(0, freelist);

0 commit comments

Comments
 (0)