Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(65923)

Delta Between Two Patch Sets: Objects/abstract.c

Issue 27809: _PyObject_FastCall(): add support for keyword arguments
Left Patch Set: Created 3 years, 2 months ago
Right Patch Set: Created 3 years, 1 month ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Modules/_sre.c ('k') | Objects/fileobject.c » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* Abstract Object Interface (many thanks to Jim Fulton) */ 1 /* Abstract Object Interface (many thanks to Jim Fulton) */
2 2
3 #include "Python.h" 3 #include "Python.h"
4 #include <ctype.h> 4 #include <ctype.h>
5 #include "structmember.h" /* we need the offsetof() macro from there */ 5 #include "structmember.h" /* we need the offsetof() macro from there */
6 #include "longintrepr.h" 6 #include "longintrepr.h"
7 7
8 8
9 9
10 /* Shorthands to return certain errors */ 10 /* Shorthands to return certain errors */
(...skipping 1263 matching lines...) Expand 10 before | Expand all | Expand 10 after
1274 1274
1275 finish: 1275 finish:
1276 Py_DECREF(value); 1276 Py_DECREF(value);
1277 return result; 1277 return result;
1278 } 1278 }
1279 1279
1280 1280
1281 PyObject * 1281 PyObject *
1282 PyNumber_Long(PyObject *o) 1282 PyNumber_Long(PyObject *o)
1283 { 1283 {
1284 PyObject *result;
1284 PyNumberMethods *m; 1285 PyNumberMethods *m;
1285 PyObject *trunc_func; 1286 PyObject *trunc_func;
1286 Py_buffer view; 1287 Py_buffer view;
1287 _Py_IDENTIFIER(__trunc__); 1288 _Py_IDENTIFIER(__trunc__);
1288 1289
1289 if (o == NULL) { 1290 if (o == NULL) {
1290 return null_error(); 1291 return null_error();
1291 } 1292 }
1292 1293
1293 if (PyLong_CheckExact(o)) { 1294 if (PyLong_CheckExact(o)) {
1294 Py_INCREF(o); 1295 Py_INCREF(o);
1295 return o; 1296 return o;
1296 } 1297 }
1297 m = o->ob_type->tp_as_number; 1298 m = o->ob_type->tp_as_number;
1298 if (m && m->nb_int) { /* This should include subclasses of int */ 1299 if (m && m->nb_int) { /* This should include subclasses of int */
1299 return (PyObject *)_PyLong_FromNbInt(o); 1300 result = (PyObject *)_PyLong_FromNbInt(o);
1301 if (result != NULL && !PyLong_CheckExact(result)) {
1302 Py_SETREF(result, _PyLong_Copy((PyLongObject *)result));
1303 }
1304 return result;
1300 } 1305 }
1301 trunc_func = _PyObject_LookupSpecial(o, &PyId___trunc__); 1306 trunc_func = _PyObject_LookupSpecial(o, &PyId___trunc__);
1302 if (trunc_func) { 1307 if (trunc_func) {
1303 PyObject *truncated = PyEval_CallObject(trunc_func, NULL); 1308 result = PyEval_CallObject(trunc_func, NULL);
1304 PyObject *int_instance;
1305 Py_DECREF(trunc_func); 1309 Py_DECREF(trunc_func);
1306 if (truncated == NULL || PyLong_Check(truncated)) 1310 if (result == NULL || PyLong_CheckExact(result)) {
1307 return truncated; 1311 return result;
1312 }
1313 if (PyLong_Check(result)) {
1314 Py_SETREF(result, _PyLong_Copy((PyLongObject *)result));
1315 return result;
1316 }
1308 /* __trunc__ is specified to return an Integral type, 1317 /* __trunc__ is specified to return an Integral type,
1309 but int() needs to return an int. */ 1318 but int() needs to return an int. */
1310 m = truncated->ob_type->tp_as_number; 1319 m = result->ob_type->tp_as_number;
1311 if (m == NULL || m->nb_int == NULL) { 1320 if (m == NULL || m->nb_int == NULL) {
1312 PyErr_Format( 1321 PyErr_Format(
1313 PyExc_TypeError, 1322 PyExc_TypeError,
1314 "__trunc__ returned non-Integral (type %.200s)", 1323 "__trunc__ returned non-Integral (type %.200s)",
1315 truncated->ob_type->tp_name); 1324 result->ob_type->tp_name);
1316 Py_DECREF(truncated); 1325 Py_DECREF(result);
1317 return NULL; 1326 return NULL;
1318 } 1327 }
1319 int_instance = (PyObject *)_PyLong_FromNbInt(truncated); 1328 Py_SETREF(result, (PyObject *)_PyLong_FromNbInt(result));
1320 Py_DECREF(truncated); 1329 if (result != NULL && !PyLong_CheckExact(result)) {
1321 return int_instance; 1330 Py_SETREF(result, _PyLong_Copy((PyLongObject *)result));
1331 }
1332 return result;
1322 } 1333 }
1323 if (PyErr_Occurred()) 1334 if (PyErr_Occurred())
1324 return NULL; 1335 return NULL;
1325 1336
1326 if (PyUnicode_Check(o)) 1337 if (PyUnicode_Check(o))
1327 /* The below check is done in PyLong_FromUnicode(). */ 1338 /* The below check is done in PyLong_FromUnicode(). */
1328 return PyLong_FromUnicodeObject(o, 10); 1339 return PyLong_FromUnicodeObject(o, 10);
1329 1340
1330 if (PyBytes_Check(o)) 1341 if (PyBytes_Check(o))
1331 /* need to do extra error checking that PyLong_FromString() 1342 /* need to do extra error checking that PyLong_FromString()
1332 * doesn't do. In particular int('9\x005') must raise an 1343 * doesn't do. In particular int('9\x005') must raise an
1333 * exception, not truncate at the null. 1344 * exception, not truncate at the null.
1334 */ 1345 */
1335 return _PyLong_FromBytes(PyBytes_AS_STRING(o), 1346 return _PyLong_FromBytes(PyBytes_AS_STRING(o),
1336 PyBytes_GET_SIZE(o), 10); 1347 PyBytes_GET_SIZE(o), 10);
1337 1348
1338 if (PyByteArray_Check(o)) 1349 if (PyByteArray_Check(o))
1339 return _PyLong_FromBytes(PyByteArray_AS_STRING(o), 1350 return _PyLong_FromBytes(PyByteArray_AS_STRING(o),
1340 PyByteArray_GET_SIZE(o), 10); 1351 PyByteArray_GET_SIZE(o), 10);
1341 1352
1342 if (PyObject_GetBuffer(o, &view, PyBUF_SIMPLE) == 0) { 1353 if (PyObject_GetBuffer(o, &view, PyBUF_SIMPLE) == 0) {
1343 PyObject *result, *bytes; 1354 PyObject *bytes;
1344 1355
1345 /* Copy to NUL-terminated buffer. */ 1356 /* Copy to NUL-terminated buffer. */
1346 bytes = PyBytes_FromStringAndSize((const char *)view.buf, view.len); 1357 bytes = PyBytes_FromStringAndSize((const char *)view.buf, view.len);
1347 if (bytes == NULL) { 1358 if (bytes == NULL) {
1348 PyBuffer_Release(&view); 1359 PyBuffer_Release(&view);
1349 return NULL; 1360 return NULL;
1350 } 1361 }
1351 result = _PyLong_FromBytes(PyBytes_AS_STRING(bytes), 1362 result = _PyLong_FromBytes(PyBytes_AS_STRING(bytes),
1352 PyBytes_GET_SIZE(bytes), 10); 1363 PyBytes_GET_SIZE(bytes), 10);
1353 Py_DECREF(bytes); 1364 Py_DECREF(bytes);
(...skipping 883 matching lines...) Expand 10 before | Expand all | Expand 10 after
2237 for (i=0; i < nargs; i++) { 2248 for (i=0; i < nargs; i++) {
2238 PyObject *item = stack[i]; 2249 PyObject *item = stack[i];
2239 Py_INCREF(item); 2250 Py_INCREF(item);
2240 PyTuple_SET_ITEM(args, i, item); 2251 PyTuple_SET_ITEM(args, i, item);
2241 } 2252 }
2242 2253
2243 return args; 2254 return args;
2244 } 2255 }
2245 2256
2246 PyObject * 2257 PyObject *
2247 _PyObject_FastCall(PyObject *func, PyObject **args, int nargs, PyObject *kwargs) 2258 _PyObject_FastCallDict(PyObject *func, PyObject **args, int nargs, PyObject *kwa rgs)
2248 { 2259 {
2249 ternaryfunc call; 2260 ternaryfunc call;
2250 PyObject *result = NULL; 2261 PyObject *result = NULL;
2251 2262
2252 /* _PyObject_FastCall() must not be called with an exception set, 2263 /* _PyObject_FastCallDict() must not be called with an exception set,
2253 because it may clear it (directly or indirectly) and so the 2264 because it may clear it (directly or indirectly) and so the
2254 caller loses its exception */ 2265 caller loses its exception */
2255 assert(!PyErr_Occurred()); 2266 assert(!PyErr_Occurred());
2256 2267
2257 assert(func != NULL); 2268 assert(func != NULL);
2258 assert(nargs >= 0); 2269 assert(nargs >= 0);
2259 assert(nargs == 0 || args != NULL); 2270 assert(nargs == 0 || args != NULL);
2260 assert(kwargs == NULL || PyDict_Check(kwargs)); 2271 /* issue #27128: support for keywords will come later:
2272 _PyFunction_FastCall() doesn't support keyword arguments yet */
2273 assert(kwargs == NULL);
2261 2274
2262 if (Py_EnterRecursiveCall(" while calling a Python object")) { 2275 if (Py_EnterRecursiveCall(" while calling a Python object")) {
2263 return NULL; 2276 return NULL;
2264 } 2277 }
2265 2278
2266 if (PyFunction_Check(func)) { 2279 if (PyFunction_Check(func)) {
2267 result = _PyFunction_FastCall(func, args, nargs, kwargs); 2280 result = _PyFunction_FastCall(func, args, nargs, kwargs);
2268 } 2281 }
2269 else if (PyCFunction_Check(func)) { 2282 else if (PyCFunction_Check(func)) {
2270 result = _PyCFunction_FastCall(func, args, nargs, kwargs); 2283 result = _PyCFunction_FastCall(func, args, nargs, kwargs);
(...skipping 27 matching lines...) Expand all
2298 } 2311 }
2299 2312
2300 static PyObject* 2313 static PyObject*
2301 call_function_tail(PyObject *callable, PyObject *args) 2314 call_function_tail(PyObject *callable, PyObject *args)
2302 { 2315 {
2303 PyObject *result; 2316 PyObject *result;
2304 2317
2305 assert(args != NULL); 2318 assert(args != NULL);
2306 2319
2307 if (!PyTuple_Check(args)) { 2320 if (!PyTuple_Check(args)) {
2308 result = _PyObject_FastCall(callable, &args, 1, NULL); 2321 result = _PyObject_CallArg1(callable, args);
2309 } 2322 }
2310 else { 2323 else {
2311 result = PyObject_Call(callable, args, NULL); 2324 result = PyObject_Call(callable, args, NULL);
2312 } 2325 }
2313 2326
2314 return result; 2327 return result;
2315 } 2328 }
2316 2329
2317 PyObject * 2330 PyObject *
2318 PyObject_CallFunction(PyObject *callable, const char *format, ...) 2331 PyObject_CallFunction(PyObject *callable, const char *format, ...)
2319 { 2332 {
2320 va_list va; 2333 va_list va;
2321 PyObject *args, *result; 2334 PyObject *args, *result;
2322 2335
2323 if (callable == NULL) { 2336 if (callable == NULL) {
2324 return null_error(); 2337 return null_error();
2325 } 2338 }
2326 2339
2327 if (!format || !*format) { 2340 if (!format || !*format) {
2328 return _PyObject_FastCall(callable, NULL, 0, NULL); 2341 return _PyObject_CallNoArg(callable);
2329 } 2342 }
2330 2343
2331 va_start(va, format); 2344 va_start(va, format);
2332 args = Py_VaBuildValue(format, va); 2345 args = Py_VaBuildValue(format, va);
2333 va_end(va); 2346 va_end(va);
2334 if (args == NULL) { 2347 if (args == NULL) {
2335 return NULL; 2348 return NULL;
2336 } 2349 }
2337 2350
2338 result = call_function_tail(callable, args); 2351 result = call_function_tail(callable, args);
2339 Py_DECREF(args); 2352 Py_DECREF(args);
2340 return result; 2353 return result;
2341 } 2354 }
2342 2355
2343 PyObject * 2356 PyObject *
2344 _PyObject_CallFunction_SizeT(PyObject *callable, const char *format, ...) 2357 _PyObject_CallFunction_SizeT(PyObject *callable, const char *format, ...)
2345 { 2358 {
2346 va_list va; 2359 va_list va;
2347 PyObject *args, *result; 2360 PyObject *args, *result;
2348 2361
2349 if (callable == NULL) { 2362 if (callable == NULL) {
2350 return null_error(); 2363 return null_error();
2351 } 2364 }
2352 2365
2353 if (!format || !*format) { 2366 if (!format || !*format) {
2354 return _PyObject_FastCall(callable, NULL, 0, NULL); 2367 return _PyObject_CallNoArg(callable);
2355 } 2368 }
2356 2369
2357 va_start(va, format); 2370 va_start(va, format);
2358 args = _Py_VaBuildValue_SizeT(format, va); 2371 args = _Py_VaBuildValue_SizeT(format, va);
2359 va_end(va); 2372 va_end(va);
2360 if (args == NULL) { 2373 if (args == NULL) {
2361 return NULL; 2374 return NULL;
2362 } 2375 }
2363 2376
2364 result = call_function_tail(callable, args); 2377 result = call_function_tail(callable, args);
2365 Py_DECREF(args); 2378 Py_DECREF(args);
2366 return result; 2379 return result;
2367 } 2380 }
2368 2381
2369 static PyObject* 2382 static PyObject*
2370 callmethod(PyObject* func, const char *format, va_list va, int is_size_t) 2383 callmethod(PyObject* func, const char *format, va_list va, int is_size_t)
2371 { 2384 {
2372 PyObject *args, *result; 2385 PyObject *args, *result;
2373 2386
2374 assert(func != NULL); 2387 assert(func != NULL);
2375 2388
2376 if (!PyCallable_Check(func)) { 2389 if (!PyCallable_Check(func)) {
2377 type_error("attribute of type '%.200s' is not callable", func); 2390 type_error("attribute of type '%.200s' is not callable", func);
2378 return NULL; 2391 return NULL;
2379 } 2392 }
2380 2393
2381 if (!format || !*format) { 2394 if (!format || !*format) {
2382 return _PyObject_FastCall(func, NULL, 0, NULL); 2395 return _PyObject_CallNoArg(func);
2383 } 2396 }
2384 2397
2385 if (is_size_t) { 2398 if (is_size_t) {
2386 args = _Py_VaBuildValue_SizeT(format, va); 2399 args = _Py_VaBuildValue_SizeT(format, va);
2387 } 2400 }
2388 else { 2401 else {
2389 args = Py_VaBuildValue(format, va); 2402 args = Py_VaBuildValue(format, va);
2390 } 2403 }
2391 if (args == NULL) { 2404 if (args == NULL) {
2392 return NULL; 2405 return NULL;
(...skipping 602 matching lines...) Expand 10 before | Expand all | Expand 10 after
2995 /* Free's a NULL terminated char** array of C strings. */ 3008 /* Free's a NULL terminated char** array of C strings. */
2996 void 3009 void
2997 _Py_FreeCharPArray(char *const array[]) 3010 _Py_FreeCharPArray(char *const array[])
2998 { 3011 {
2999 Py_ssize_t i; 3012 Py_ssize_t i;
3000 for (i = 0; array[i] != NULL; ++i) { 3013 for (i = 0; array[i] != NULL; ++i) {
3001 PyMem_Free(array[i]); 3014 PyMem_Free(array[i]);
3002 } 3015 }
3003 PyMem_Free((void*)array); 3016 PyMem_Free((void*)array);
3004 } 3017 }
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+