Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Doc/c-api/object.rst
Original file line numberDiff line numberDiff line change
Expand Up@@ -196,6 +196,7 @@ Object Protocol
This function now includes a debug assertion to help ensure that it
does not silently discard an active exception.


.. c:function:: PyObject* PyObject_Bytes(PyObject *o)

.. index:: builtin: bytes
Expand Down
1 change: 1 addition & 0 deletions Include/cpython/object.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -348,6 +348,7 @@ static inline void _Py_Dealloc_inline(PyObject *op)
}
#define _Py_Dealloc(op) _Py_Dealloc_inline(op)

PyAPI_FUNC(PyObject *) _PyObject_FunctionStr(PyObject *);

/* Safely decref `op` and set `op` to `op2`.
*
Expand Down
10 changes: 5 additions & 5 deletions Lib/test/test_call.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -74,7 +74,7 @@ def test_varargs3_kw(self):
self.assertRaisesRegex(TypeError, msg, bool, x=2)

def test_varargs4_kw(self):
msg = r"^index\(\) takes no keyword arguments$"
msg = r"^list[.]index\(\) takes no keyword arguments$"
self.assertRaisesRegex(TypeError, msg, [].index, x=2)

def test_varargs5_kw(self):
Expand All@@ -90,19 +90,19 @@ def test_varargs7_kw(self):
self.assertRaisesRegex(TypeError, msg, next, x=2)

def test_varargs8_kw(self):
msg = r"^pack\(\) takes no keyword arguments$"
msg = r"^_struct[.]pack\(\) takes no keyword arguments$"
self.assertRaisesRegex(TypeError, msg, struct.pack, x=2)

def test_varargs9_kw(self):
msg = r"^pack_into\(\) takes no keyword arguments$"
msg = r"^_struct[.]pack_into\(\) takes no keyword arguments$"
self.assertRaisesRegex(TypeError, msg, struct.pack_into, x=2)

def test_varargs10_kw(self):
msg = r"^index\(\) takes no keyword arguments$"
msg = r"^deque[.]index\(\) takes no keyword arguments$"
self.assertRaisesRegex(TypeError, msg, collections.deque().index, x=2)

def test_varargs11_kw(self):
msg = r"^pack\(\) takes no keyword arguments$"
msg = r"^Struct[.]pack\(\) takes no keyword arguments$"
self.assertRaisesRegex(TypeError, msg, struct.Struct.pack, struct.Struct(""), x=2)

def test_varargs12_kw(self):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_descr.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -1967,7 +1967,7 @@ def test_methods_in_c(self):
# different error messages.
set_add = set.add

expected_errmsg = "descriptor 'add' of 'set' object needs an argument"
expected_errmsg = "unbound method set.add() needs an argument"

with self.assertRaises(TypeError) as cm:
set_add()
Expand Down
38 changes: 19 additions & 19 deletions Lib/test/test_extcall.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -52,15 +52,15 @@
>>> f(1, 2, **{'a': -1, 'b': 5}, **{'a': 4, 'c': 6})
Traceback (most recent call last):
...
TypeError: f() got multiple values for keyword argument 'a'
TypeError: test.test_extcall.f() got multiple values for keyword argument 'a'
>>> f(1, 2, **{'a': -1, 'b': 5}, a=4, c=6)
Traceback (most recent call last):
...
TypeError: f() got multiple values for keyword argument 'a'
TypeError: test.test_extcall.f() got multiple values for keyword argument 'a'
>>> f(1, 2, a=3, **{'a': 4}, **{'a': 5})
Traceback (most recent call last):
...
TypeError: f() got multiple values for keyword argument 'a'
TypeError: test.test_extcall.f() got multiple values for keyword argument 'a'
>>> f(1, 2, 3, *[4, 5], **{'a':6, 'b':7})
(1, 2, 3, 4, 5) {'a': 6, 'b': 7}
>>> f(1, 2, 3, x=4, y=5, *(6, 7), **{'a':8, 'b': 9})
Expand DownExpand Up@@ -118,7 +118,7 @@
>>> g(*Nothing())
Traceback (most recent call last):
...
TypeError: g() argument after * must be an iterable, not Nothing
TypeError: test.test_extcall.g() argument after * must be an iterable, not Nothing

>>> class Nothing:
... def __len__(self): return 5
Expand All@@ -127,7 +127,7 @@
>>> g(*Nothing())
Traceback (most recent call last):
...
TypeError: g() argument after * must be an iterable, not Nothing
TypeError: test.test_extcall.g() argument after * must be an iterable, not Nothing

>>> class Nothing():
... def __len__(self): return 5
Expand DownExpand Up@@ -247,17 +247,17 @@
>>> h(*h)
Traceback (most recent call last):
...
TypeError: h() argument after * must be an iterable, not function
TypeError: test.test_extcall.h() argument after * must be an iterable, not function

>>> h(1, *h)
Traceback (most recent call last):
...
TypeError: h() argument after * must be an iterable, not function
TypeError: test.test_extcall.h() argument after * must be an iterable, not function

>>> h(*[1], *h)
Traceback (most recent call last):
...
TypeError: h() argument after * must be an iterable, not function
TypeError: test.test_extcall.h() argument after * must be an iterable, not function

>>> dir(*h)
Traceback (most recent call last):
Expand All@@ -268,38 +268,38 @@
>>> nothing(*h)
Traceback (most recent call last):
...
TypeError: NoneType object argument after * must be an iterable, \
TypeError: None argument after * must be an iterable, \
not function

>>> h(**h)
Traceback (most recent call last):
...
TypeError: h() argument after ** must be a mapping, not function
TypeError: test.test_extcall.h() argument after ** must be a mapping, not function

>>> h(**[])
Traceback (most recent call last):
...
TypeError: h() argument after ** must be a mapping, not list
TypeError: test.test_extcall.h() argument after ** must be a mapping, not list

>>> h(a=1, **h)
Traceback (most recent call last):
...
TypeError: h() argument after ** must be a mapping, not function
TypeError: test.test_extcall.h() argument after ** must be a mapping, not function

>>> h(a=1, **[])
Traceback (most recent call last):
...
TypeError: h() argument after ** must be a mapping, not list
TypeError: test.test_extcall.h() argument after ** must be a mapping, not list

>>> h(**{'a': 1}, **h)
Traceback (most recent call last):
...
TypeError: h() argument after ** must be a mapping, not function
TypeError: test.test_extcall.h() argument after ** must be a mapping, not function

>>> h(**{'a': 1}, **[])
Traceback (most recent call last):
...
TypeError: h() argument after ** must be a mapping, not list
TypeError: test.test_extcall.h() argument after ** must be a mapping, not list

>>> dir(**h)
Traceback (most recent call last):
Expand All@@ -309,7 +309,7 @@
>>> nothing(**h)
Traceback (most recent call last):
...
TypeError: NoneType object argument after ** must be a mapping, \
TypeError: None argument after ** must be a mapping, \
not function

>>> dir(b=1, **{'b': 1})
Expand DownExpand Up@@ -351,17 +351,17 @@
>>> g(**MultiDict([('x', 1), ('x', 2)]))
Traceback (most recent call last):
...
TypeError: g() got multiple values for keyword argument 'x'
TypeError: test.test_extcall.g() got multiple values for keyword argument 'x'

>>> g(a=3, **MultiDict([('x', 1), ('x', 2)]))
Traceback (most recent call last):
...
TypeError: g() got multiple values for keyword argument 'x'
TypeError: test.test_extcall.g() got multiple values for keyword argument 'x'

>>> g(**MultiDict([('a', 3)]), **MultiDict([('x', 1), ('x', 2)]))
Traceback (most recent call last):
...
TypeError: g() got multiple values for keyword argument 'x'
TypeError: test.test_extcall.g() got multiple values for keyword argument 'x'

Another helper function

Expand Down
10 changes: 5 additions & 5 deletions Lib/test/test_unpack_ex.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -236,27 +236,27 @@
>>> f(x=5, **{'x': 3}, y=2)
Traceback (most recent call last):
...
TypeError: f() got multiple values for keyword argument 'x'
TypeError: test.test_unpack_ex.f() got multiple values for keyword argument 'x'

>>> f(**{'x': 3}, x=5, y=2)
Traceback (most recent call last):
...
TypeError: f() got multiple values for keyword argument 'x'
TypeError: test.test_unpack_ex.f() got multiple values for keyword argument 'x'

>>> f(**{'x': 3}, **{'x': 5}, y=2)
Traceback (most recent call last):
...
TypeError: f() got multiple values for keyword argument 'x'
TypeError: test.test_unpack_ex.f() got multiple values for keyword argument 'x'

>>> f(x=5, **{'x': 3}, **{'x': 2})
Traceback (most recent call last):
...
TypeError: f() got multiple values for keyword argument 'x'
TypeError: test.test_unpack_ex.f() got multiple values for keyword argument 'x'

>>> f(**{1: 3}, **{1: 5})
Traceback (most recent call last):
...
TypeError: f() got multiple values for keyword argument '1'
TypeError: test.test_unpack_ex.f() got multiple values for keyword argument '1'

Unpacking non-sequence

Expand Down
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
Add :c:func:`_PyObject_FunctionStr` to get a user-friendly string representation
of a function-like object. Patch by Jeroen Demeyer.
57 changes: 29 additions & 28 deletions Objects/descrobject.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -231,45 +231,38 @@ getset_set(PyGetSetDescrObject *descr, PyObject *obj, PyObject *value)
*
* First, common helpers
*/
static const char *
get_name(PyObject *func) {
assert(PyObject_TypeCheck(func, &PyMethodDescr_Type));
return ((PyMethodDescrObject *)func)->d_method->ml_name;
}

typedef void (*funcptr)(void);

static inline int
method_check_args(PyObject *func, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{
assert(!PyErr_Occurred());
assert(PyObject_TypeCheck(func, &PyMethodDescr_Type));
if (nargs < 1) {
PyErr_Format(PyExc_TypeError,
"descriptor '%.200s' of '%.100s' "
"object needs an argument",
get_name(func), PyDescr_TYPE(func)->tp_name);
PyObject *funcstr = _PyObject_FunctionStr(func);
if (funcstr != NULL) {
PyErr_Format(PyExc_TypeError,
"unbound method %U needs an argument", funcstr);
Py_DECREF(funcstr);
}
return -1;
}
PyObject *self = args[0];
if (!_PyObject_RealIsSubclass((PyObject *)Py_TYPE(self),
(PyObject *)PyDescr_TYPE(func)))
{
PyErr_Format(PyExc_TypeError,
"descriptor '%.200s' for '%.100s' objects "
"doesn't apply to a '%.100s' object",
get_name(func), PyDescr_TYPE(func)->tp_name,
Py_TYPE(self)->tp_name);
PyObject *dummy;
if (descr_check((PyDescrObject *)func, self, &dummy)) {
return -1;
}
if (kwnames && PyTuple_GET_SIZE(kwnames)) {
PyErr_Format(PyExc_TypeError,
"%.200s() takes no keyword arguments", get_name(func));
PyObject *funcstr = _PyObject_FunctionStr(func);
if (funcstr != NULL) {
PyErr_Format(PyExc_TypeError,
"%U takes no keyword arguments", funcstr);
Py_DECREF(funcstr);
}
return -1;
}
return 0;
}

typedef void (*funcptr)(void);

static inline funcptr
method_enter_call(PyThreadState *tstate, PyObject *func)
{
Expand DownExpand Up@@ -387,8 +380,12 @@ method_vectorcall_NOARGS(
return NULL;
}
if (nargs != 1) {
PyErr_Format(PyExc_TypeError,
"%.200s() takes no arguments (%zd given)", get_name(func), nargs-1);
PyObject *funcstr = _PyObject_FunctionStr(func);
if (funcstr != NULL) {
PyErr_Format(PyExc_TypeError,
"%U takes no arguments (%zd given)", funcstr, nargs-1);
Py_DECREF(funcstr);
}
return NULL;
}
PyCFunction meth = (PyCFunction)method_enter_call(tstate, func);
Expand All@@ -410,9 +407,13 @@ method_vectorcall_O(
return NULL;
}
if (nargs != 2) {
PyErr_Format(PyExc_TypeError,
"%.200s() takes exactly one argument (%zd given)",
get_name(func), nargs-1);
PyObject *funcstr = _PyObject_FunctionStr(func);
if (funcstr != NULL) {
PyErr_Format(PyExc_TypeError,
"%U takes exactly one argument (%zd given)",
funcstr, nargs-1);
Py_DECREF(funcstr);
}
return NULL;
}
PyCFunction meth = (PyCFunction)method_enter_call(tstate, func);
Expand Down
37 changes: 20 additions & 17 deletions Objects/methodobject.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -334,29 +334,26 @@ _PyCFunction_Fini(void)
*
* First, common helpers
*/
static const char *
get_name(PyObject *func)
{
assert(PyCFunction_Check(func));
PyMethodDef *method = ((PyCFunctionObject *)func)->m_ml;
return method->ml_name;
}

typedef void (*funcptr)(void);

static inline int
cfunction_check_kwargs(PyThreadState *tstate, PyObject *func, PyObject *kwnames)
{
assert(!_PyErr_Occurred(tstate));
assert(PyCFunction_Check(func));
if (kwnames && PyTuple_GET_SIZE(kwnames)) {
_PyErr_Format(tstate, PyExc_TypeError,
"%.200s() takes no keyword arguments", get_name(func));
PyObject *funcstr = _PyObject_FunctionStr(func);
if (funcstr != NULL) {
_PyErr_Format(tstate, PyExc_TypeError,
"%U takes no keyword arguments", funcstr);
Py_DECREF(funcstr);
}
return -1;
}
return 0;
}

typedef void (*funcptr)(void);

static inline funcptr
cfunction_enter_call(PyThreadState *tstate, PyObject *func)
{
Expand DownExpand Up@@ -412,9 +409,12 @@ cfunction_vectorcall_NOARGS(
}
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
if (nargs != 0) {
_PyErr_Format(tstate, PyExc_TypeError,
"%.200s() takes no arguments (%zd given)",
get_name(func), nargs);
PyObject *funcstr = _PyObject_FunctionStr(func);
if (funcstr != NULL) {
_PyErr_Format(tstate, PyExc_TypeError,
"%U takes no arguments (%zd given)", funcstr, nargs);
Py_DECREF(funcstr);
}
return NULL;
}
PyCFunction meth = (PyCFunction)cfunction_enter_call(tstate, func);
Expand All@@ -436,9 +436,12 @@ cfunction_vectorcall_O(
}
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
if (nargs != 1) {
_PyErr_Format(tstate, PyExc_TypeError,
"%.200s() takes exactly one argument (%zd given)",
get_name(func), nargs);
PyObject *funcstr = _PyObject_FunctionStr(func);
if (funcstr != NULL) {
_PyErr_Format(tstate, PyExc_TypeError,
"%U takes exactly one argument (%zd given)", funcstr, nargs);
Py_DECREF(funcstr);
}
return NULL;
}
PyCFunction meth = (PyCFunction)cfunction_enter_call(tstate, func);
Expand Down
Loading