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

Skip to content

Commit 6b4ec51

Browse files
committed
Fix for SF bug #117241
When a method is called with no regular arguments and * args, defer the first arg is subclass check until after the * args have been expanded. N.B. The CALL_FUNCTION implementation is getting really hairy; should review it to see if it can be simplified.
1 parent 5942b43 commit 6b4ec51

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

Lib/test/test_extcall.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,21 @@ def f2(*a, **b):
144144
d[key] = i
145145
a, b = f2(1, *(2, 3), **d)
146146
print len(a), len(b), b == d
147+
148+
class Foo:
149+
def method(self, arg1, arg2):
150+
return arg1 + arg2
151+
152+
x = Foo()
153+
print Foo.method(*(x, 1, 2))
154+
print Foo.method(x, *(1, 2))
155+
try:
156+
print Foo.method(*(1, 2, 3))
157+
except TypeError, err:
158+
print err
159+
try:
160+
print Foo.method(1, *(2, 3))
161+
except TypeError, err:
162+
print err
163+
164+

Python/ceval.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1822,7 +1822,7 @@ eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals,
18221822
na++;
18231823
n++;
18241824
}
1825-
else {
1825+
else if (!((flags & 1) && na == 0)) {
18261826
/* Unbound methods must be called with an
18271827
instance of the class (or a derived
18281828
class) as first argument */
@@ -1895,6 +1895,20 @@ eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals,
18951895
if (nstar < 0) {
18961896
goto extcall_fail;
18971897
}
1898+
if (class && self == NULL && na == 0) {
1899+
/* * arg is first argument of method,
1900+
so check it is isinstance of class */
1901+
self = PyTuple_GET_ITEM(stararg, 0);
1902+
if (!(PyInstance_Check(self) &&
1903+
PyClass_IsSubclass((PyObject *)
1904+
(((PyInstanceObject *)self)->in_class),
1905+
class))) {
1906+
PyErr_SetString(PyExc_TypeError,
1907+
"unbound method must be called with instance as first argument");
1908+
x = NULL;
1909+
break;
1910+
}
1911+
}
18981912
}
18991913
if (nk > 0) {
19001914
if (kwdict == NULL) {

0 commit comments

Comments
 (0)