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

Skip to content

Commit ff8b494

Browse files
committed
changes for keyword args to built-in functions and classes
1 parent ce0a6de commit ff8b494

1 file changed

Lines changed: 43 additions & 19 deletions

File tree

Python/ceval.c

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,21 +1565,42 @@ eval_code2(co, globals, locals,
15651565
else {
15661566
object *args = newtupleobject(na);
15671567
object *kwdict = NULL;
1568-
if (args == NULL)
1569-
x = NULL;
1570-
else if (nk > 0) {
1571-
err_setstr(SystemError,
1572-
"calling built-in with keywords not yet implemented");
1568+
if (args == NULL) {
15731569
x = NULL;
1570+
break;
15741571
}
1575-
else {
1576-
while (--na >= 0) {
1577-
w = POP();
1578-
SETTUPLEITEM(args, na, w);
1572+
if (nk > 0) {
1573+
kwdict = newdictobject();
1574+
if (kwdict == NULL) {
1575+
x = NULL;
1576+
break;
15791577
}
1580-
x = call_object(func, args);
1581-
DECREF(args);
1578+
err = 0;
1579+
while (--nk >= 0) {
1580+
object *value = POP();
1581+
object *key = POP();
1582+
err = mappinginsert(
1583+
kwdict, key, value);
1584+
if (err) {
1585+
DECREF(key);
1586+
DECREF(value);
1587+
break;
1588+
}
1589+
}
1590+
if (err) {
1591+
DECREF(args);
1592+
DECREF(kwdict);
1593+
break;
1594+
}
1595+
}
1596+
while (--na >= 0) {
1597+
w = POP();
1598+
SETTUPLEITEM(args, na, w);
15821599
}
1600+
x = PyEval_CallObjectWithKeywords(
1601+
func, args, kwdict);
1602+
DECREF(args);
1603+
XDECREF(kwdict);
15831604
}
15841605
DECREF(func);
15851606
while (stack_pointer > pfunc) {
@@ -2281,25 +2302,28 @@ call_builtin(func, arg, kw)
22812302
object *arg;
22822303
object *kw;
22832304
{
2284-
if (kw != NULL) {
2285-
err_setstr(SystemError,
2286-
"calling built-in with keywords not yet implemented");
2287-
return NULL;
2288-
}
22892305
if (is_methodobject(func)) {
22902306
method meth = getmethod(func);
22912307
object *self = getself(func);
2292-
if (!getvarargs(func)) {
2308+
int flags = getflags(func);
2309+
if (!(flags & METH_VARARGS)) {
22932310
int size = gettuplesize(arg);
22942311
if (size == 1)
22952312
arg = GETTUPLEITEM(arg, 0);
22962313
else if (size == 0)
22972314
arg = NULL;
22982315
}
2316+
if (flags & METH_KEYWORDS)
2317+
return (*(PyCFunctionWithKeywords)meth)(self, arg, kw);
2318+
if (kw != NULL) {
2319+
err_setstr(TypeError,
2320+
"this function takes no keyword arguments");
2321+
return NULL;
2322+
}
22992323
return (*meth)(self, arg);
23002324
}
23012325
if (is_classobject(func)) {
2302-
return newinstanceobject(func, arg);
2326+
return newinstanceobject(func, arg, kw);
23032327
}
23042328
if (is_instanceobject(func)) {
23052329
object *res, *call = getattr(func,"__call__");
@@ -2309,7 +2333,7 @@ call_builtin(func, arg, kw)
23092333
"no __call__ method defined");
23102334
return NULL;
23112335
}
2312-
res = call_object(call, arg);
2336+
res = PyEval_CallObjectWithKeywords(call, arg, kw);
23132337
DECREF(call);
23142338
return res;
23152339
}

0 commit comments

Comments
 (0)