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

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 3224 matching lines...) Expand 10 before | Expand all | Expand 10 after
3235 while (stack_pointer > pfunc) { 3235 while (stack_pointer > pfunc) {
3236 PyObject *o = POP(); 3236 PyObject *o = POP();
3237 Py_DECREF(o); 3237 Py_DECREF(o);
3238 } 3238 }
3239 PUSH(res); 3239 PUSH(res);
3240 if (res == NULL) 3240 if (res == NULL)
3241 goto error; 3241 goto error;
3242 DISPATCH(); 3242 DISPATCH();
3243 } 3243 }
3244 3244
3245 TARGET(MAKE_CLOSURE)
3246 TARGET(MAKE_FUNCTION) { 3245 TARGET(MAKE_FUNCTION) {
3247 int posdefaults = oparg & 0xff; 3246 PyObject *qualname = POP();
3248 int kwdefaults = (oparg>>8) & 0xff; 3247 PyObject *codeobj = POP();
3249 int num_annotations = (oparg >> 16) & 0x7fff; 3248 PyFunctionObject *func = (PyFunctionObject *)
3249 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
3250 3250
3251 PyObject *qualname = POP(); /* qualname */ 3251 Py_DECREF(codeobj);
3252 PyObject *code = POP(); /* code object */
3253 PyObject *func = PyFunction_NewWithQualName(code, f->f_globals, qual name);
3254 Py_DECREF(code);
3255 Py_DECREF(qualname); 3252 Py_DECREF(qualname);
3256 3253 if (func == NULL) {
3257 if (func == NULL)
3258 goto error; 3254 goto error;
3259
3260 if (opcode == MAKE_CLOSURE) {
3261 PyObject *closure = POP();
3262 if (PyFunction_SetClosure(func, closure) != 0) {
3263 /* Can't happen unless bytecode is corrupt. */
3264 Py_DECREF(func);
3265 Py_DECREF(closure);
3266 goto error;
3267 }
3268 Py_DECREF(closure);
3269 } 3255 }
3270 3256
3271 if (num_annotations > 0) { 3257 /* NB: Py_None is not an acceptable value for these. */
3272 Py_ssize_t name_ix; 3258 if (oparg & 0x08) {
3273 PyObject *names = POP(); /* names of args with annotations */ 3259 func ->func_closure = POP();
storchaka 2016/05/27 22:13:11 Py_None was acceptable. In any case you should che
3274 PyObject *anns = PyDict_New(); 3260 }
3275 if (anns == NULL) { 3261 if (oparg & 0x04) {
3276 Py_DECREF(func); 3262 func->func_annotations = POP();
storchaka 2016/05/27 22:13:11 Needed check that the value is a dict (maybe exact
3277 Py_DECREF(names); 3263 }
3278 goto error; 3264 if (oparg & 0x02) {
3279 } 3265 func->func_kwdefaults = POP();
3280 name_ix = PyTuple_Size(names); 3266 }
3281 assert(num_annotations == name_ix+1); 3267 if (oparg & 0x01) {
3282 while (name_ix > 0) { 3268 func->func_defaults = POP();
3283 PyObject *name, *value;
3284 int err;
3285 --name_ix;
3286 name = PyTuple_GET_ITEM(names, name_ix);
3287 value = POP();
3288 err = PyDict_SetItem(anns, name, value);
3289 Py_DECREF(value);
3290 if (err != 0) {
3291 Py_DECREF(anns);
3292 Py_DECREF(func);
3293 Py_DECREF(names);
3294 goto error;
3295 }
3296 }
3297 Py_DECREF(names);
3298
3299 if (PyFunction_SetAnnotations(func, anns) != 0) {
3300 /* Can't happen unless
3301 PyFunction_SetAnnotations changes. */
3302 Py_DECREF(anns);
3303 Py_DECREF(func);
3304 goto error;
3305 }
3306 Py_DECREF(anns);
3307 } 3269 }
3308 3270
3309 /* XXX Maybe this should be a separate opcode? */ 3271 PUSH((PyObject *)func);
3310 if (kwdefaults > 0) {
3311 PyObject *defs = PyDict_New();
3312 if (defs == NULL) {
3313 Py_DECREF(func);
3314 goto error;
3315 }
3316 while (--kwdefaults >= 0) {
3317 PyObject *v = POP(); /* default value */
3318 PyObject *key = POP(); /* kw only arg name */
3319 int err = PyDict_SetItem(defs, key, v);
3320 Py_DECREF(v);
3321 Py_DECREF(key);
3322 if (err != 0) {
3323 Py_DECREF(defs);
3324 Py_DECREF(func);
3325 goto error;
3326 }
3327 }
3328 if (PyFunction_SetKwDefaults(func, defs) != 0) {
3329 /* Can't happen unless
3330 PyFunction_SetKwDefaults changes. */
3331 Py_DECREF(func);
3332 Py_DECREF(defs);
3333 goto error;
3334 }
3335 Py_DECREF(defs);
3336 }
3337 if (posdefaults > 0) {
3338 PyObject *defs = PyTuple_New(posdefaults);
3339 if (defs == NULL) {
3340 Py_DECREF(func);
3341 goto error;
3342 }
3343 while (--posdefaults >= 0)
3344 PyTuple_SET_ITEM(defs, posdefaults, POP());
3345 if (PyFunction_SetDefaults(func, defs) != 0) {
3346 /* Can't happen unless
3347 PyFunction_SetDefaults changes. */
3348 Py_DECREF(defs);
3349 Py_DECREF(func);
3350 goto error;
3351 }
3352 Py_DECREF(defs);
3353 }
3354 PUSH(func);
3355 DISPATCH(); 3272 DISPATCH();
3356 } 3273 }
3357 3274
3358 TARGET(BUILD_SLICE) { 3275 TARGET(BUILD_SLICE) {
3359 PyObject *start, *stop, *step, *slice; 3276 PyObject *start, *stop, *step, *slice;
3360 if (oparg == 3) 3277 if (oparg == 3)
3361 step = POP(); 3278 step = POP();
3362 else 3279 else
3363 step = NULL; 3280 step = NULL;
3364 stop = POP(); 3281 stop = POP();
(...skipping 2007 matching lines...) Expand 10 before | Expand all | Expand 10 after
5372 Py_DECREF(l); 5289 Py_DECREF(l);
5373 return NULL; 5290 return NULL;
5374 } 5291 }
5375 PyList_SetItem(l, i, x); 5292 PyList_SetItem(l, i, x);
5376 } 5293 }
5377 return l; 5294 return l;
5378 #endif 5295 #endif
5379 } 5296 }
5380 5297
5381 #endif 5298 #endif
OLDNEW

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