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

Side by Side Diff: Python/ceval.c

Issue 27095: Simplify MAKE_FUNCTION
Patch Set: Created 3 years, 4 months 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:
View unified diff | Download patch
OLDNEW
1 1
2 /* Execute compiled code */ 2 /* Execute compiled code */
3 3
4 /* XXX TO DO: 4 /* XXX TO DO:
5 XXX speed up searching for keywords by using a dictionary 5 XXX speed up searching for keywords by using a dictionary
6 XXX document it! 6 XXX document it!
7 */ 7 */
8 8
9 /* enable more aggressive intra-module optimizations, where available */ 9 /* enable more aggressive intra-module optimizations, where available */
10 #define PY_LOCAL_AGGRESSIVE 10 #define PY_LOCAL_AGGRESSIVE
(...skipping 3307 matching lines...) Expand 10 before | Expand all | Expand 10 after
3318 while (stack_pointer > pfunc) { 3318 while (stack_pointer > pfunc) {
3319 PyObject *o = POP(); 3319 PyObject *o = POP();
3320 Py_DECREF(o); 3320 Py_DECREF(o);
3321 } 3321 }
3322 PUSH(res); 3322 PUSH(res);
3323 if (res == NULL) 3323 if (res == NULL)
3324 goto error; 3324 goto error;
3325 DISPATCH(); 3325 DISPATCH();
3326 } 3326 }
3327 3327
3328 TARGET(MAKE_CLOSURE)
3329 TARGET(MAKE_FUNCTION) { 3328 TARGET(MAKE_FUNCTION) {
3330 int posdefaults = oparg & 0xff; 3329 PyObject *qualname = POP();
3331 int kwdefaults = (oparg>>8) & 0xff; 3330 PyObject *codeobj = POP();
3332 int num_annotations = (oparg >> 16) & 0x7fff; 3331 PyFunctionObject *func = (PyFunctionObject *)
3332 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
3333 3333
3334 PyObject *qualname = POP(); /* qualname */ 3334 Py_DECREF(codeobj);
3335 PyObject *code = POP(); /* code object */
3336 PyObject *func = PyFunction_NewWithQualName(code, f->f_globals, qual name);
3337 Py_DECREF(code);
3338 Py_DECREF(qualname); 3335 Py_DECREF(qualname);
3339 3336 if (func == NULL) {
3340 if (func == NULL)
3341 goto error; 3337 goto error;
3342
3343 if (opcode == MAKE_CLOSURE) {
3344 PyObject *closure = POP();
3345 if (PyFunction_SetClosure(func, closure) != 0) {
3346 /* Can't happen unless bytecode is corrupt. */
3347 Py_DECREF(func);
3348 Py_DECREF(closure);
3349 goto error;
3350 }
3351 Py_DECREF(closure);
3352 } 3338 }
3353 3339
3354 if (num_annotations > 0) { 3340 if (oparg & 0x08) {
3355 Py_ssize_t name_ix; 3341 assert(PyTuple_CheckExact(TOP()));
3356 PyObject *names = POP(); /* names of args with annotations */ 3342 func ->func_closure = POP();
3357 PyObject *anns = PyDict_New(); 3343 }
3358 if (anns == NULL) { 3344 if (oparg & 0x04) {
3359 Py_DECREF(func); 3345 assert(PyDict_CheckExact(TOP()));
3360 Py_DECREF(names); 3346 func->func_annotations = POP();
3361 goto error; 3347 }
3362 } 3348 if (oparg & 0x02) {
3363 name_ix = PyTuple_Size(names); 3349 assert(PyDict_CheckExact(TOP()));
3364 assert(num_annotations == name_ix+1); 3350 func->func_kwdefaults = POP();
3365 while (name_ix > 0) { 3351 }
3366 PyObject *name, *value; 3352 if (oparg & 0x01) {
3367 int err; 3353 assert(PyTuple_CheckExact(TOP()));
3368 --name_ix; 3354 func->func_defaults = POP();
3369 name = PyTuple_GET_ITEM(names, name_ix);
3370 value = POP();
3371 err = PyDict_SetItem(anns, name, value);
3372 Py_DECREF(value);
3373 if (err != 0) {
3374 Py_DECREF(anns);
3375 Py_DECREF(func);
3376 Py_DECREF(names);
3377 goto error;
3378 }
3379 }
3380 Py_DECREF(names);
3381
3382 if (PyFunction_SetAnnotations(func, anns) != 0) {
3383 /* Can't happen unless
3384 PyFunction_SetAnnotations changes. */
3385 Py_DECREF(anns);
3386 Py_DECREF(func);
3387 goto error;
3388 }
3389 Py_DECREF(anns);
3390 } 3355 }
3391 3356
3392 /* XXX Maybe this should be a separate opcode? */ 3357 PUSH((PyObject *)func);
3393 if (kwdefaults > 0) {
3394 PyObject *defs = PyDict_New();
3395 if (defs == NULL) {
3396 Py_DECREF(func);
3397 goto error;
3398 }
3399 while (--kwdefaults >= 0) {
3400 PyObject *v = POP(); /* default value */
3401 PyObject *key = POP(); /* kw only arg name */
3402 int err = PyDict_SetItem(defs, key, v);
3403 Py_DECREF(v);
3404 Py_DECREF(key);
3405 if (err != 0) {
3406 Py_DECREF(defs);
3407 Py_DECREF(func);
3408 goto error;
3409 }
3410 }
3411 if (PyFunction_SetKwDefaults(func, defs) != 0) {
3412 /* Can't happen unless
3413 PyFunction_SetKwDefaults changes. */
3414 Py_DECREF(func);
3415 Py_DECREF(defs);
3416 goto error;
3417 }
3418 Py_DECREF(defs);
3419 }
3420 if (posdefaults > 0) {
3421 PyObject *defs = PyTuple_New(posdefaults);
3422 if (defs == NULL) {
3423 Py_DECREF(func);
3424 goto error;
3425 }
3426 while (--posdefaults >= 0)
3427 PyTuple_SET_ITEM(defs, posdefaults, POP());
3428 if (PyFunction_SetDefaults(func, defs) != 0) {
3429 /* Can't happen unless
3430 PyFunction_SetDefaults changes. */
3431 Py_DECREF(defs);
3432 Py_DECREF(func);
3433 goto error;
3434 }
3435 Py_DECREF(defs);
3436 }
3437 PUSH(func);
3438 DISPATCH(); 3358 DISPATCH();
3439 } 3359 }
3440 3360
3441 TARGET(BUILD_SLICE) { 3361 TARGET(BUILD_SLICE) {
3442 PyObject *start, *stop, *step, *slice; 3362 PyObject *start, *stop, *step, *slice;
3443 if (oparg == 3) 3363 if (oparg == 3)
3444 step = POP(); 3364 step = POP();
3445 else 3365 else
3446 step = NULL; 3366 step = NULL;
3447 stop = POP(); 3367 stop = POP();
(...skipping 2009 matching lines...) Expand 10 before | Expand all | Expand 10 after
5457 Py_DECREF(l); 5377 Py_DECREF(l);
5458 return NULL; 5378 return NULL;
5459 } 5379 }
5460 PyList_SetItem(l, i, x); 5380 PyList_SetItem(l, i, x);
5461 } 5381 }
5462 return l; 5382 return l;
5463 #endif 5383 #endif
5464 } 5384 }
5465 5385
5466 #endif 5386 #endif
OLDNEW
« no previous file with comments | « Objects/abstract.c ('k') | Python/compile.c » ('j') | Python/compile.c » ('J')

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