• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdbool.h>
2 
3 #include "Python.h"
4 #include "code.h"
5 #include "opcode.h"
6 #include "structmember.h"
7 #include "pycore_code.h"
8 #include "pycore_pystate.h"
9 #include "pycore_tupleobject.h"
10 #include "clinic/codeobject.c.h"
11 
12 /* Holder for co_extra information */
13 typedef struct {
14     Py_ssize_t ce_size;
15     void *ce_extras[1];
16 } _PyCodeObjectExtra;
17 
18 /*[clinic input]
19 class code "PyCodeObject *" "&PyCode_Type"
20 [clinic start generated code]*/
21 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=78aa5d576683bb4b]*/
22 
23 /* all_name_chars(s): true iff s matches [a-zA-Z0-9_]* */
24 static int
all_name_chars(PyObject * o)25 all_name_chars(PyObject *o)
26 {
27     const unsigned char *s, *e;
28 
29     if (!PyUnicode_IS_ASCII(o))
30         return 0;
31 
32     s = PyUnicode_1BYTE_DATA(o);
33     e = s + PyUnicode_GET_LENGTH(o);
34     for (; s != e; s++) {
35         if (!Py_ISALNUM(*s) && *s != '_')
36             return 0;
37     }
38     return 1;
39 }
40 
41 static void
intern_strings(PyObject * tuple)42 intern_strings(PyObject *tuple)
43 {
44     Py_ssize_t i;
45 
46     for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
47         PyObject *v = PyTuple_GET_ITEM(tuple, i);
48         if (v == NULL || !PyUnicode_CheckExact(v)) {
49             Py_FatalError("non-string found in code slot");
50         }
51         PyUnicode_InternInPlace(&_PyTuple_ITEMS(tuple)[i]);
52     }
53 }
54 
55 /* Intern selected string constants */
56 static int
intern_string_constants(PyObject * tuple)57 intern_string_constants(PyObject *tuple)
58 {
59     int modified = 0;
60     Py_ssize_t i;
61 
62     for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
63         PyObject *v = PyTuple_GET_ITEM(tuple, i);
64         if (PyUnicode_CheckExact(v)) {
65             if (PyUnicode_READY(v) == -1) {
66                 PyErr_Clear();
67                 continue;
68             }
69             if (all_name_chars(v)) {
70                 PyObject *w = v;
71                 PyUnicode_InternInPlace(&v);
72                 if (w != v) {
73                     PyTuple_SET_ITEM(tuple, i, v);
74                     modified = 1;
75                 }
76             }
77         }
78         else if (PyTuple_CheckExact(v)) {
79             intern_string_constants(v);
80         }
81         else if (PyFrozenSet_CheckExact(v)) {
82             PyObject *w = v;
83             PyObject *tmp = PySequence_Tuple(v);
84             if (tmp == NULL) {
85                 PyErr_Clear();
86                 continue;
87             }
88             if (intern_string_constants(tmp)) {
89                 v = PyFrozenSet_New(tmp);
90                 if (v == NULL) {
91                     PyErr_Clear();
92                 }
93                 else {
94                     PyTuple_SET_ITEM(tuple, i, v);
95                     Py_DECREF(w);
96                     modified = 1;
97                 }
98             }
99             Py_DECREF(tmp);
100         }
101     }
102     return modified;
103 }
104 
105 PyCodeObject *
PyCode_NewWithPosOnlyArgs(int argcount,int posonlyargcount,int kwonlyargcount,int nlocals,int stacksize,int flags,PyObject * code,PyObject * consts,PyObject * names,PyObject * varnames,PyObject * freevars,PyObject * cellvars,PyObject * filename,PyObject * name,int firstlineno,PyObject * lnotab)106 PyCode_NewWithPosOnlyArgs(int argcount, int posonlyargcount, int kwonlyargcount,
107                           int nlocals, int stacksize, int flags,
108                           PyObject *code, PyObject *consts, PyObject *names,
109                           PyObject *varnames, PyObject *freevars, PyObject *cellvars,
110                           PyObject *filename, PyObject *name, int firstlineno,
111                           PyObject *lnotab)
112 {
113     PyCodeObject *co;
114     Py_ssize_t *cell2arg = NULL;
115     Py_ssize_t i, n_cellvars, n_varnames, total_args;
116 
117     /* Check argument types */
118     if (argcount < posonlyargcount || posonlyargcount < 0 ||
119         kwonlyargcount < 0 || nlocals < 0 ||
120         stacksize < 0 || flags < 0 ||
121         code == NULL || !PyBytes_Check(code) ||
122         consts == NULL || !PyTuple_Check(consts) ||
123         names == NULL || !PyTuple_Check(names) ||
124         varnames == NULL || !PyTuple_Check(varnames) ||
125         freevars == NULL || !PyTuple_Check(freevars) ||
126         cellvars == NULL || !PyTuple_Check(cellvars) ||
127         name == NULL || !PyUnicode_Check(name) ||
128         filename == NULL || !PyUnicode_Check(filename) ||
129         lnotab == NULL || !PyBytes_Check(lnotab)) {
130         PyErr_BadInternalCall();
131         return NULL;
132     }
133 
134     /* Ensure that strings are ready Unicode string */
135     if (PyUnicode_READY(name) < 0) {
136         return NULL;
137     }
138     if (PyUnicode_READY(filename) < 0) {
139         return NULL;
140     }
141 
142     intern_strings(names);
143     intern_strings(varnames);
144     intern_strings(freevars);
145     intern_strings(cellvars);
146     intern_string_constants(consts);
147 
148     /* Check for any inner or outer closure references */
149     n_cellvars = PyTuple_GET_SIZE(cellvars);
150     if (!n_cellvars && !PyTuple_GET_SIZE(freevars)) {
151         flags |= CO_NOFREE;
152     } else {
153         flags &= ~CO_NOFREE;
154     }
155 
156     n_varnames = PyTuple_GET_SIZE(varnames);
157     if (argcount <= n_varnames && kwonlyargcount <= n_varnames) {
158         /* Never overflows. */
159         total_args = (Py_ssize_t)argcount + (Py_ssize_t)kwonlyargcount +
160                       ((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0);
161     }
162     else {
163         total_args = n_varnames + 1;
164     }
165     if (total_args > n_varnames) {
166         PyErr_SetString(PyExc_ValueError, "code: varnames is too small");
167         return NULL;
168     }
169 
170     /* Create mapping between cells and arguments if needed. */
171     if (n_cellvars) {
172         bool used_cell2arg = false;
173         cell2arg = PyMem_NEW(Py_ssize_t, n_cellvars);
174         if (cell2arg == NULL) {
175             PyErr_NoMemory();
176             return NULL;
177         }
178         /* Find cells which are also arguments. */
179         for (i = 0; i < n_cellvars; i++) {
180             Py_ssize_t j;
181             PyObject *cell = PyTuple_GET_ITEM(cellvars, i);
182             cell2arg[i] = CO_CELL_NOT_AN_ARG;
183             for (j = 0; j < total_args; j++) {
184                 PyObject *arg = PyTuple_GET_ITEM(varnames, j);
185                 int cmp = PyUnicode_Compare(cell, arg);
186                 if (cmp == -1 && PyErr_Occurred()) {
187                     PyMem_FREE(cell2arg);
188                     return NULL;
189                 }
190                 if (cmp == 0) {
191                     cell2arg[i] = j;
192                     used_cell2arg = true;
193                     break;
194                 }
195             }
196         }
197         if (!used_cell2arg) {
198             PyMem_FREE(cell2arg);
199             cell2arg = NULL;
200         }
201     }
202     co = PyObject_NEW(PyCodeObject, &PyCode_Type);
203     if (co == NULL) {
204         if (cell2arg)
205             PyMem_FREE(cell2arg);
206         return NULL;
207     }
208     co->co_argcount = argcount;
209     co->co_posonlyargcount = posonlyargcount;
210     co->co_kwonlyargcount = kwonlyargcount;
211     co->co_nlocals = nlocals;
212     co->co_stacksize = stacksize;
213     co->co_flags = flags;
214     Py_INCREF(code);
215     co->co_code = code;
216     Py_INCREF(consts);
217     co->co_consts = consts;
218     Py_INCREF(names);
219     co->co_names = names;
220     Py_INCREF(varnames);
221     co->co_varnames = varnames;
222     Py_INCREF(freevars);
223     co->co_freevars = freevars;
224     Py_INCREF(cellvars);
225     co->co_cellvars = cellvars;
226     co->co_cell2arg = cell2arg;
227     Py_INCREF(filename);
228     co->co_filename = filename;
229     Py_INCREF(name);
230     co->co_name = name;
231     co->co_firstlineno = firstlineno;
232     Py_INCREF(lnotab);
233     co->co_lnotab = lnotab;
234     co->co_zombieframe = NULL;
235     co->co_weakreflist = NULL;
236     co->co_extra = NULL;
237 
238     co->co_opcache_map = NULL;
239     co->co_opcache = NULL;
240     co->co_opcache_flag = 0;
241     co->co_opcache_size = 0;
242     return co;
243 }
244 
245 PyCodeObject *
PyCode_New(int argcount,int kwonlyargcount,int nlocals,int stacksize,int flags,PyObject * code,PyObject * consts,PyObject * names,PyObject * varnames,PyObject * freevars,PyObject * cellvars,PyObject * filename,PyObject * name,int firstlineno,PyObject * lnotab)246 PyCode_New(int argcount, int kwonlyargcount,
247            int nlocals, int stacksize, int flags,
248            PyObject *code, PyObject *consts, PyObject *names,
249            PyObject *varnames, PyObject *freevars, PyObject *cellvars,
250            PyObject *filename, PyObject *name, int firstlineno,
251            PyObject *lnotab)
252 {
253     return PyCode_NewWithPosOnlyArgs(argcount, 0, kwonlyargcount, nlocals,
254                                      stacksize, flags, code, consts, names,
255                                      varnames, freevars, cellvars, filename,
256                                      name, firstlineno, lnotab);
257 }
258 
259 int
_PyCode_InitOpcache(PyCodeObject * co)260 _PyCode_InitOpcache(PyCodeObject *co)
261 {
262     Py_ssize_t co_size = PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT);
263     co->co_opcache_map = (unsigned char *)PyMem_Calloc(co_size, 1);
264     if (co->co_opcache_map == NULL) {
265         return -1;
266     }
267 
268     _Py_CODEUNIT *opcodes = (_Py_CODEUNIT*)PyBytes_AS_STRING(co->co_code);
269     Py_ssize_t opts = 0;
270 
271     for (Py_ssize_t i = 0; i < co_size;) {
272         unsigned char opcode = _Py_OPCODE(opcodes[i]);
273         i++;  // 'i' is now aligned to (next_instr - first_instr)
274 
275         // TODO: LOAD_METHOD, LOAD_ATTR
276         if (opcode == LOAD_GLOBAL) {
277             opts++;
278             co->co_opcache_map[i] = (unsigned char)opts;
279             if (opts > 254) {
280                 break;
281             }
282         }
283     }
284 
285     if (opts) {
286         co->co_opcache = (_PyOpcache *)PyMem_Calloc(opts, sizeof(_PyOpcache));
287         if (co->co_opcache == NULL) {
288             PyMem_FREE(co->co_opcache_map);
289             return -1;
290         }
291     }
292     else {
293         PyMem_FREE(co->co_opcache_map);
294         co->co_opcache_map = NULL;
295         co->co_opcache = NULL;
296     }
297 
298     co->co_opcache_size = (unsigned char)opts;
299     return 0;
300 }
301 
302 PyCodeObject *
PyCode_NewEmpty(const char * filename,const char * funcname,int firstlineno)303 PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
304 {
305     static PyObject *emptystring = NULL;
306     static PyObject *nulltuple = NULL;
307     PyObject *filename_ob = NULL;
308     PyObject *funcname_ob = NULL;
309     PyCodeObject *result = NULL;
310     if (emptystring == NULL) {
311         emptystring = PyBytes_FromString("");
312         if (emptystring == NULL)
313             goto failed;
314     }
315     if (nulltuple == NULL) {
316         nulltuple = PyTuple_New(0);
317         if (nulltuple == NULL)
318             goto failed;
319     }
320     funcname_ob = PyUnicode_FromString(funcname);
321     if (funcname_ob == NULL)
322         goto failed;
323     filename_ob = PyUnicode_DecodeFSDefault(filename);
324     if (filename_ob == NULL)
325         goto failed;
326 
327     result = PyCode_NewWithPosOnlyArgs(
328                 0,                    /* argcount */
329                 0,                              /* posonlyargcount */
330                 0,                              /* kwonlyargcount */
331                 0,                              /* nlocals */
332                 0,                              /* stacksize */
333                 0,                              /* flags */
334                 emptystring,                    /* code */
335                 nulltuple,                      /* consts */
336                 nulltuple,                      /* names */
337                 nulltuple,                      /* varnames */
338                 nulltuple,                      /* freevars */
339                 nulltuple,                      /* cellvars */
340                 filename_ob,                    /* filename */
341                 funcname_ob,                    /* name */
342                 firstlineno,                    /* firstlineno */
343                 emptystring                     /* lnotab */
344                 );
345 
346 failed:
347     Py_XDECREF(funcname_ob);
348     Py_XDECREF(filename_ob);
349     return result;
350 }
351 
352 #define OFF(x) offsetof(PyCodeObject, x)
353 
354 static PyMemberDef code_memberlist[] = {
355     {"co_argcount",     T_INT,          OFF(co_argcount),        READONLY},
356     {"co_posonlyargcount",      T_INT,  OFF(co_posonlyargcount), READONLY},
357     {"co_kwonlyargcount",       T_INT,  OFF(co_kwonlyargcount),  READONLY},
358     {"co_nlocals",      T_INT,          OFF(co_nlocals),         READONLY},
359     {"co_stacksize",T_INT,              OFF(co_stacksize),       READONLY},
360     {"co_flags",        T_INT,          OFF(co_flags),           READONLY},
361     {"co_code",         T_OBJECT,       OFF(co_code),            READONLY},
362     {"co_consts",       T_OBJECT,       OFF(co_consts),          READONLY},
363     {"co_names",        T_OBJECT,       OFF(co_names),           READONLY},
364     {"co_varnames",     T_OBJECT,       OFF(co_varnames),        READONLY},
365     {"co_freevars",     T_OBJECT,       OFF(co_freevars),        READONLY},
366     {"co_cellvars",     T_OBJECT,       OFF(co_cellvars),        READONLY},
367     {"co_filename",     T_OBJECT,       OFF(co_filename),        READONLY},
368     {"co_name",         T_OBJECT,       OFF(co_name),            READONLY},
369     {"co_firstlineno", T_INT,           OFF(co_firstlineno),     READONLY},
370     {"co_lnotab",       T_OBJECT,       OFF(co_lnotab),          READONLY},
371     {NULL}      /* Sentinel */
372 };
373 
374 /* Helper for code_new: return a shallow copy of a tuple that is
375    guaranteed to contain exact strings, by converting string subclasses
376    to exact strings and complaining if a non-string is found. */
377 static PyObject*
validate_and_copy_tuple(PyObject * tup)378 validate_and_copy_tuple(PyObject *tup)
379 {
380     PyObject *newtuple;
381     PyObject *item;
382     Py_ssize_t i, len;
383 
384     len = PyTuple_GET_SIZE(tup);
385     newtuple = PyTuple_New(len);
386     if (newtuple == NULL)
387         return NULL;
388 
389     for (i = 0; i < len; i++) {
390         item = PyTuple_GET_ITEM(tup, i);
391         if (PyUnicode_CheckExact(item)) {
392             Py_INCREF(item);
393         }
394         else if (!PyUnicode_Check(item)) {
395             PyErr_Format(
396                 PyExc_TypeError,
397                 "name tuples must contain only "
398                 "strings, not '%.500s'",
399                 item->ob_type->tp_name);
400             Py_DECREF(newtuple);
401             return NULL;
402         }
403         else {
404             item = _PyUnicode_Copy(item);
405             if (item == NULL) {
406                 Py_DECREF(newtuple);
407                 return NULL;
408             }
409         }
410         PyTuple_SET_ITEM(newtuple, i, item);
411     }
412 
413     return newtuple;
414 }
415 
416 PyDoc_STRVAR(code_doc,
417 "code(argcount, posonlyargcount, kwonlyargcount, nlocals, stacksize,\n\
418       flags, codestring, constants, names, varnames, filename, name,\n\
419       firstlineno, lnotab[, freevars[, cellvars]])\n\
420 \n\
421 Create a code object.  Not for the faint of heart.");
422 
423 static PyObject *
code_new(PyTypeObject * type,PyObject * args,PyObject * kw)424 code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
425 {
426     int argcount;
427     int posonlyargcount;
428     int kwonlyargcount;
429     int nlocals;
430     int stacksize;
431     int flags;
432     PyObject *co = NULL;
433     PyObject *code;
434     PyObject *consts;
435     PyObject *names, *ournames = NULL;
436     PyObject *varnames, *ourvarnames = NULL;
437     PyObject *freevars = NULL, *ourfreevars = NULL;
438     PyObject *cellvars = NULL, *ourcellvars = NULL;
439     PyObject *filename;
440     PyObject *name;
441     int firstlineno;
442     PyObject *lnotab;
443 
444     if (!PyArg_ParseTuple(args, "iiiiiiSO!O!O!UUiS|O!O!:code",
445                           &argcount, &posonlyargcount, &kwonlyargcount,
446                               &nlocals, &stacksize, &flags,
447                           &code,
448                           &PyTuple_Type, &consts,
449                           &PyTuple_Type, &names,
450                           &PyTuple_Type, &varnames,
451                           &filename, &name,
452                           &firstlineno, &lnotab,
453                           &PyTuple_Type, &freevars,
454                           &PyTuple_Type, &cellvars))
455         return NULL;
456 
457     if (PySys_Audit("code.__new__", "OOOiiiiii",
458                     code, filename, name, argcount, posonlyargcount,
459                     kwonlyargcount, nlocals, stacksize, flags) < 0) {
460         goto cleanup;
461     }
462 
463     if (argcount < 0) {
464         PyErr_SetString(
465             PyExc_ValueError,
466             "code: argcount must not be negative");
467         goto cleanup;
468     }
469 
470     if (posonlyargcount < 0) {
471         PyErr_SetString(
472             PyExc_ValueError,
473             "code: posonlyargcount must not be negative");
474         goto cleanup;
475     }
476 
477     if (kwonlyargcount < 0) {
478         PyErr_SetString(
479             PyExc_ValueError,
480             "code: kwonlyargcount must not be negative");
481         goto cleanup;
482     }
483     if (nlocals < 0) {
484         PyErr_SetString(
485             PyExc_ValueError,
486             "code: nlocals must not be negative");
487         goto cleanup;
488     }
489 
490     ournames = validate_and_copy_tuple(names);
491     if (ournames == NULL)
492         goto cleanup;
493     ourvarnames = validate_and_copy_tuple(varnames);
494     if (ourvarnames == NULL)
495         goto cleanup;
496     if (freevars)
497         ourfreevars = validate_and_copy_tuple(freevars);
498     else
499         ourfreevars = PyTuple_New(0);
500     if (ourfreevars == NULL)
501         goto cleanup;
502     if (cellvars)
503         ourcellvars = validate_and_copy_tuple(cellvars);
504     else
505         ourcellvars = PyTuple_New(0);
506     if (ourcellvars == NULL)
507         goto cleanup;
508 
509     co = (PyObject *)PyCode_NewWithPosOnlyArgs(argcount, posonlyargcount,
510                                                kwonlyargcount,
511                                                nlocals, stacksize, flags,
512                                                code, consts, ournames,
513                                                ourvarnames, ourfreevars,
514                                                ourcellvars, filename,
515                                                name, firstlineno, lnotab);
516   cleanup:
517     Py_XDECREF(ournames);
518     Py_XDECREF(ourvarnames);
519     Py_XDECREF(ourfreevars);
520     Py_XDECREF(ourcellvars);
521     return co;
522 }
523 
524 static void
code_dealloc(PyCodeObject * co)525 code_dealloc(PyCodeObject *co)
526 {
527     if (co->co_opcache != NULL) {
528         PyMem_FREE(co->co_opcache);
529     }
530     if (co->co_opcache_map != NULL) {
531         PyMem_FREE(co->co_opcache_map);
532     }
533     co->co_opcache_flag = 0;
534     co->co_opcache_size = 0;
535 
536     if (co->co_extra != NULL) {
537         PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
538         _PyCodeObjectExtra *co_extra = co->co_extra;
539 
540         for (Py_ssize_t i = 0; i < co_extra->ce_size; i++) {
541             freefunc free_extra = interp->co_extra_freefuncs[i];
542 
543             if (free_extra != NULL) {
544                 free_extra(co_extra->ce_extras[i]);
545             }
546         }
547 
548         PyMem_Free(co_extra);
549     }
550 
551     Py_XDECREF(co->co_code);
552     Py_XDECREF(co->co_consts);
553     Py_XDECREF(co->co_names);
554     Py_XDECREF(co->co_varnames);
555     Py_XDECREF(co->co_freevars);
556     Py_XDECREF(co->co_cellvars);
557     Py_XDECREF(co->co_filename);
558     Py_XDECREF(co->co_name);
559     Py_XDECREF(co->co_lnotab);
560     if (co->co_cell2arg != NULL)
561         PyMem_FREE(co->co_cell2arg);
562     if (co->co_zombieframe != NULL)
563         PyObject_GC_Del(co->co_zombieframe);
564     if (co->co_weakreflist != NULL)
565         PyObject_ClearWeakRefs((PyObject*)co);
566     PyObject_DEL(co);
567 }
568 
569 static PyObject *
code_sizeof(PyCodeObject * co,PyObject * Py_UNUSED (args))570 code_sizeof(PyCodeObject *co, PyObject *Py_UNUSED(args))
571 {
572     Py_ssize_t res = _PyObject_SIZE(Py_TYPE(co));
573     _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) co->co_extra;
574 
575     if (co->co_cell2arg != NULL && co->co_cellvars != NULL) {
576         res += PyTuple_GET_SIZE(co->co_cellvars) * sizeof(Py_ssize_t);
577     }
578     if (co_extra != NULL) {
579         res += sizeof(_PyCodeObjectExtra) +
580                (co_extra->ce_size-1) * sizeof(co_extra->ce_extras[0]);
581     }
582     if (co->co_opcache != NULL) {
583         assert(co->co_opcache_map != NULL);
584         // co_opcache_map
585         res += PyBytes_GET_SIZE(co->co_code) / sizeof(_Py_CODEUNIT);
586         // co_opcache
587         res += co->co_opcache_size * sizeof(_PyOpcache);
588     }
589     return PyLong_FromSsize_t(res);
590 }
591 
592 /*[clinic input]
593 code.replace
594 
595     *
596     co_argcount: int(c_default="self->co_argcount") = -1
597     co_posonlyargcount: int(c_default="self->co_posonlyargcount") = -1
598     co_kwonlyargcount: int(c_default="self->co_kwonlyargcount") = -1
599     co_nlocals: int(c_default="self->co_nlocals") = -1
600     co_stacksize: int(c_default="self->co_stacksize") = -1
601     co_flags: int(c_default="self->co_flags") = -1
602     co_firstlineno: int(c_default="self->co_firstlineno") = -1
603     co_code: PyBytesObject(c_default="(PyBytesObject *)self->co_code") = None
604     co_consts: object(subclass_of="&PyTuple_Type", c_default="self->co_consts") = None
605     co_names: object(subclass_of="&PyTuple_Type", c_default="self->co_names") = None
606     co_varnames: object(subclass_of="&PyTuple_Type", c_default="self->co_varnames") = None
607     co_freevars: object(subclass_of="&PyTuple_Type", c_default="self->co_freevars") = None
608     co_cellvars: object(subclass_of="&PyTuple_Type", c_default="self->co_cellvars") = None
609     co_filename: unicode(c_default="self->co_filename") = None
610     co_name: unicode(c_default="self->co_name") = None
611     co_lnotab: PyBytesObject(c_default="(PyBytesObject *)self->co_lnotab") = None
612 
613 Return a copy of the code object with new values for the specified fields.
614 [clinic start generated code]*/
615 
616 static PyObject *
code_replace_impl(PyCodeObject * self,int co_argcount,int co_posonlyargcount,int co_kwonlyargcount,int co_nlocals,int co_stacksize,int co_flags,int co_firstlineno,PyBytesObject * co_code,PyObject * co_consts,PyObject * co_names,PyObject * co_varnames,PyObject * co_freevars,PyObject * co_cellvars,PyObject * co_filename,PyObject * co_name,PyBytesObject * co_lnotab)617 code_replace_impl(PyCodeObject *self, int co_argcount,
618                   int co_posonlyargcount, int co_kwonlyargcount,
619                   int co_nlocals, int co_stacksize, int co_flags,
620                   int co_firstlineno, PyBytesObject *co_code,
621                   PyObject *co_consts, PyObject *co_names,
622                   PyObject *co_varnames, PyObject *co_freevars,
623                   PyObject *co_cellvars, PyObject *co_filename,
624                   PyObject *co_name, PyBytesObject *co_lnotab)
625 /*[clinic end generated code: output=25c8e303913bcace input=d9051bc8f24e6b28]*/
626 {
627 #define CHECK_INT_ARG(ARG) \
628         if (ARG < 0) { \
629             PyErr_SetString(PyExc_ValueError, \
630                             #ARG " must be a positive integer"); \
631             return NULL; \
632         }
633 
634     CHECK_INT_ARG(co_argcount);
635     CHECK_INT_ARG(co_posonlyargcount);
636     CHECK_INT_ARG(co_kwonlyargcount);
637     CHECK_INT_ARG(co_nlocals);
638     CHECK_INT_ARG(co_stacksize);
639     CHECK_INT_ARG(co_flags);
640     CHECK_INT_ARG(co_firstlineno);
641 
642 #undef CHECK_INT_ARG
643 
644     if (PySys_Audit("code.__new__", "OOOiiiiii",
645                     co_code, co_filename, co_name, co_argcount,
646                     co_posonlyargcount, co_kwonlyargcount, co_nlocals,
647                     co_stacksize, co_flags) < 0) {
648         return NULL;
649     }
650 
651     return (PyObject *)PyCode_NewWithPosOnlyArgs(
652         co_argcount, co_posonlyargcount, co_kwonlyargcount, co_nlocals,
653         co_stacksize, co_flags, (PyObject*)co_code, co_consts, co_names,
654         co_varnames, co_freevars, co_cellvars, co_filename, co_name,
655         co_firstlineno, (PyObject*)co_lnotab);
656 }
657 
658 static PyObject *
code_repr(PyCodeObject * co)659 code_repr(PyCodeObject *co)
660 {
661     int lineno;
662     if (co->co_firstlineno != 0)
663         lineno = co->co_firstlineno;
664     else
665         lineno = -1;
666     if (co->co_filename && PyUnicode_Check(co->co_filename)) {
667         return PyUnicode_FromFormat(
668             "<code object %U at %p, file \"%U\", line %d>",
669             co->co_name, co, co->co_filename, lineno);
670     } else {
671         return PyUnicode_FromFormat(
672             "<code object %U at %p, file ???, line %d>",
673             co->co_name, co, lineno);
674     }
675 }
676 
677 PyObject*
_PyCode_ConstantKey(PyObject * op)678 _PyCode_ConstantKey(PyObject *op)
679 {
680     PyObject *key;
681 
682     /* Py_None and Py_Ellipsis are singletons. */
683     if (op == Py_None || op == Py_Ellipsis
684        || PyLong_CheckExact(op)
685        || PyUnicode_CheckExact(op)
686           /* code_richcompare() uses _PyCode_ConstantKey() internally */
687        || PyCode_Check(op))
688     {
689         /* Objects of these types are always different from object of other
690          * type and from tuples. */
691         Py_INCREF(op);
692         key = op;
693     }
694     else if (PyBool_Check(op) || PyBytes_CheckExact(op)) {
695         /* Make booleans different from integers 0 and 1.
696          * Avoid BytesWarning from comparing bytes with strings. */
697         key = PyTuple_Pack(2, Py_TYPE(op), op);
698     }
699     else if (PyFloat_CheckExact(op)) {
700         double d = PyFloat_AS_DOUBLE(op);
701         /* all we need is to make the tuple different in either the 0.0
702          * or -0.0 case from all others, just to avoid the "coercion".
703          */
704         if (d == 0.0 && copysign(1.0, d) < 0.0)
705             key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
706         else
707             key = PyTuple_Pack(2, Py_TYPE(op), op);
708     }
709     else if (PyComplex_CheckExact(op)) {
710         Py_complex z;
711         int real_negzero, imag_negzero;
712         /* For the complex case we must make complex(x, 0.)
713            different from complex(x, -0.) and complex(0., y)
714            different from complex(-0., y), for any x and y.
715            All four complex zeros must be distinguished.*/
716         z = PyComplex_AsCComplex(op);
717         real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
718         imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
719         /* use True, False and None singleton as tags for the real and imag
720          * sign, to make tuples different */
721         if (real_negzero && imag_negzero) {
722             key = PyTuple_Pack(3, Py_TYPE(op), op, Py_True);
723         }
724         else if (imag_negzero) {
725             key = PyTuple_Pack(3, Py_TYPE(op), op, Py_False);
726         }
727         else if (real_negzero) {
728             key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
729         }
730         else {
731             key = PyTuple_Pack(2, Py_TYPE(op), op);
732         }
733     }
734     else if (PyTuple_CheckExact(op)) {
735         Py_ssize_t i, len;
736         PyObject *tuple;
737 
738         len = PyTuple_GET_SIZE(op);
739         tuple = PyTuple_New(len);
740         if (tuple == NULL)
741             return NULL;
742 
743         for (i=0; i < len; i++) {
744             PyObject *item, *item_key;
745 
746             item = PyTuple_GET_ITEM(op, i);
747             item_key = _PyCode_ConstantKey(item);
748             if (item_key == NULL) {
749                 Py_DECREF(tuple);
750                 return NULL;
751             }
752 
753             PyTuple_SET_ITEM(tuple, i, item_key);
754         }
755 
756         key = PyTuple_Pack(2, tuple, op);
757         Py_DECREF(tuple);
758     }
759     else if (PyFrozenSet_CheckExact(op)) {
760         Py_ssize_t pos = 0;
761         PyObject *item;
762         Py_hash_t hash;
763         Py_ssize_t i, len;
764         PyObject *tuple, *set;
765 
766         len = PySet_GET_SIZE(op);
767         tuple = PyTuple_New(len);
768         if (tuple == NULL)
769             return NULL;
770 
771         i = 0;
772         while (_PySet_NextEntry(op, &pos, &item, &hash)) {
773             PyObject *item_key;
774 
775             item_key = _PyCode_ConstantKey(item);
776             if (item_key == NULL) {
777                 Py_DECREF(tuple);
778                 return NULL;
779             }
780 
781             assert(i < len);
782             PyTuple_SET_ITEM(tuple, i, item_key);
783             i++;
784         }
785         set = PyFrozenSet_New(tuple);
786         Py_DECREF(tuple);
787         if (set == NULL)
788             return NULL;
789 
790         key = PyTuple_Pack(2, set, op);
791         Py_DECREF(set);
792         return key;
793     }
794     else {
795         /* for other types, use the object identifier as a unique identifier
796          * to ensure that they are seen as unequal. */
797         PyObject *obj_id = PyLong_FromVoidPtr(op);
798         if (obj_id == NULL)
799             return NULL;
800 
801         key = PyTuple_Pack(2, obj_id, op);
802         Py_DECREF(obj_id);
803     }
804     return key;
805 }
806 
807 static PyObject *
code_richcompare(PyObject * self,PyObject * other,int op)808 code_richcompare(PyObject *self, PyObject *other, int op)
809 {
810     PyCodeObject *co, *cp;
811     int eq;
812     PyObject *consts1, *consts2;
813     PyObject *res;
814 
815     if ((op != Py_EQ && op != Py_NE) ||
816         !PyCode_Check(self) ||
817         !PyCode_Check(other)) {
818         Py_RETURN_NOTIMPLEMENTED;
819     }
820 
821     co = (PyCodeObject *)self;
822     cp = (PyCodeObject *)other;
823 
824     eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
825     if (!eq) goto unequal;
826     eq = co->co_argcount == cp->co_argcount;
827     if (!eq) goto unequal;
828     eq = co->co_posonlyargcount == cp->co_posonlyargcount;
829     if (!eq) goto unequal;
830     eq = co->co_kwonlyargcount == cp->co_kwonlyargcount;
831     if (!eq) goto unequal;
832     eq = co->co_nlocals == cp->co_nlocals;
833     if (!eq) goto unequal;
834     eq = co->co_flags == cp->co_flags;
835     if (!eq) goto unequal;
836     eq = co->co_firstlineno == cp->co_firstlineno;
837     if (!eq) goto unequal;
838     eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);
839     if (eq <= 0) goto unequal;
840 
841     /* compare constants */
842     consts1 = _PyCode_ConstantKey(co->co_consts);
843     if (!consts1)
844         return NULL;
845     consts2 = _PyCode_ConstantKey(cp->co_consts);
846     if (!consts2) {
847         Py_DECREF(consts1);
848         return NULL;
849     }
850     eq = PyObject_RichCompareBool(consts1, consts2, Py_EQ);
851     Py_DECREF(consts1);
852     Py_DECREF(consts2);
853     if (eq <= 0) goto unequal;
854 
855     eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);
856     if (eq <= 0) goto unequal;
857     eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ);
858     if (eq <= 0) goto unequal;
859     eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ);
860     if (eq <= 0) goto unequal;
861     eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ);
862     if (eq <= 0) goto unequal;
863 
864     if (op == Py_EQ)
865         res = Py_True;
866     else
867         res = Py_False;
868     goto done;
869 
870   unequal:
871     if (eq < 0)
872         return NULL;
873     if (op == Py_NE)
874         res = Py_True;
875     else
876         res = Py_False;
877 
878   done:
879     Py_INCREF(res);
880     return res;
881 }
882 
883 static Py_hash_t
code_hash(PyCodeObject * co)884 code_hash(PyCodeObject *co)
885 {
886     Py_hash_t h, h0, h1, h2, h3, h4, h5, h6;
887     h0 = PyObject_Hash(co->co_name);
888     if (h0 == -1) return -1;
889     h1 = PyObject_Hash(co->co_code);
890     if (h1 == -1) return -1;
891     h2 = PyObject_Hash(co->co_consts);
892     if (h2 == -1) return -1;
893     h3 = PyObject_Hash(co->co_names);
894     if (h3 == -1) return -1;
895     h4 = PyObject_Hash(co->co_varnames);
896     if (h4 == -1) return -1;
897     h5 = PyObject_Hash(co->co_freevars);
898     if (h5 == -1) return -1;
899     h6 = PyObject_Hash(co->co_cellvars);
900     if (h6 == -1) return -1;
901     h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
902         co->co_argcount ^ co->co_posonlyargcount ^ co->co_kwonlyargcount ^
903         co->co_nlocals ^ co->co_flags;
904     if (h == -1) h = -2;
905     return h;
906 }
907 
908 /* XXX code objects need to participate in GC? */
909 
910 static struct PyMethodDef code_methods[] = {
911     {"__sizeof__", (PyCFunction)code_sizeof, METH_NOARGS},
912     CODE_REPLACE_METHODDEF
913     {NULL, NULL}                /* sentinel */
914 };
915 
916 PyTypeObject PyCode_Type = {
917     PyVarObject_HEAD_INIT(&PyType_Type, 0)
918     "code",
919     sizeof(PyCodeObject),
920     0,
921     (destructor)code_dealloc,           /* tp_dealloc */
922     0,                                  /* tp_vectorcall_offset */
923     0,                                  /* tp_getattr */
924     0,                                  /* tp_setattr */
925     0,                                  /* tp_as_async */
926     (reprfunc)code_repr,                /* tp_repr */
927     0,                                  /* tp_as_number */
928     0,                                  /* tp_as_sequence */
929     0,                                  /* tp_as_mapping */
930     (hashfunc)code_hash,                /* tp_hash */
931     0,                                  /* tp_call */
932     0,                                  /* tp_str */
933     PyObject_GenericGetAttr,            /* tp_getattro */
934     0,                                  /* tp_setattro */
935     0,                                  /* tp_as_buffer */
936     Py_TPFLAGS_DEFAULT,                 /* tp_flags */
937     code_doc,                           /* tp_doc */
938     0,                                  /* tp_traverse */
939     0,                                  /* tp_clear */
940     code_richcompare,                   /* tp_richcompare */
941     offsetof(PyCodeObject, co_weakreflist),     /* tp_weaklistoffset */
942     0,                                  /* tp_iter */
943     0,                                  /* tp_iternext */
944     code_methods,                       /* tp_methods */
945     code_memberlist,                    /* tp_members */
946     0,                                  /* tp_getset */
947     0,                                  /* tp_base */
948     0,                                  /* tp_dict */
949     0,                                  /* tp_descr_get */
950     0,                                  /* tp_descr_set */
951     0,                                  /* tp_dictoffset */
952     0,                                  /* tp_init */
953     0,                                  /* tp_alloc */
954     code_new,                           /* tp_new */
955 };
956 
957 /* Use co_lnotab to compute the line number from a bytecode index, addrq.  See
958    lnotab_notes.txt for the details of the lnotab representation.
959 */
960 
961 int
PyCode_Addr2Line(PyCodeObject * co,int addrq)962 PyCode_Addr2Line(PyCodeObject *co, int addrq)
963 {
964     Py_ssize_t size = PyBytes_Size(co->co_lnotab) / 2;
965     unsigned char *p = (unsigned char*)PyBytes_AsString(co->co_lnotab);
966     int line = co->co_firstlineno;
967     int addr = 0;
968     while (--size >= 0) {
969         addr += *p++;
970         if (addr > addrq)
971             break;
972         line += (signed char)*p;
973         p++;
974     }
975     return line;
976 }
977 
978 /* Update *bounds to describe the first and one-past-the-last instructions in
979    the same line as lasti.  Return the number of that line. */
980 int
_PyCode_CheckLineNumber(PyCodeObject * co,int lasti,PyAddrPair * bounds)981 _PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds)
982 {
983     Py_ssize_t size;
984     int addr, line;
985     unsigned char* p;
986 
987     p = (unsigned char*)PyBytes_AS_STRING(co->co_lnotab);
988     size = PyBytes_GET_SIZE(co->co_lnotab) / 2;
989 
990     addr = 0;
991     line = co->co_firstlineno;
992     assert(line > 0);
993 
994     /* possible optimization: if f->f_lasti == instr_ub
995        (likely to be a common case) then we already know
996        instr_lb -- if we stored the matching value of p
997        somewhere we could skip the first while loop. */
998 
999     /* See lnotab_notes.txt for the description of
1000        co_lnotab.  A point to remember: increments to p
1001        come in (addr, line) pairs. */
1002 
1003     bounds->ap_lower = 0;
1004     while (size > 0) {
1005         if (addr + *p > lasti)
1006             break;
1007         addr += *p++;
1008         if ((signed char)*p)
1009             bounds->ap_lower = addr;
1010         line += (signed char)*p;
1011         p++;
1012         --size;
1013     }
1014 
1015     if (size > 0) {
1016         while (--size >= 0) {
1017             addr += *p++;
1018             if ((signed char)*p)
1019                 break;
1020             p++;
1021         }
1022         bounds->ap_upper = addr;
1023     }
1024     else {
1025         bounds->ap_upper = INT_MAX;
1026     }
1027 
1028     return line;
1029 }
1030 
1031 
1032 int
_PyCode_GetExtra(PyObject * code,Py_ssize_t index,void ** extra)1033 _PyCode_GetExtra(PyObject *code, Py_ssize_t index, void **extra)
1034 {
1035     if (!PyCode_Check(code)) {
1036         PyErr_BadInternalCall();
1037         return -1;
1038     }
1039 
1040     PyCodeObject *o = (PyCodeObject*) code;
1041     _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) o->co_extra;
1042 
1043     if (co_extra == NULL || co_extra->ce_size <= index) {
1044         *extra = NULL;
1045         return 0;
1046     }
1047 
1048     *extra = co_extra->ce_extras[index];
1049     return 0;
1050 }
1051 
1052 
1053 int
_PyCode_SetExtra(PyObject * code,Py_ssize_t index,void * extra)1054 _PyCode_SetExtra(PyObject *code, Py_ssize_t index, void *extra)
1055 {
1056     PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
1057 
1058     if (!PyCode_Check(code) || index < 0 ||
1059             index >= interp->co_extra_user_count) {
1060         PyErr_BadInternalCall();
1061         return -1;
1062     }
1063 
1064     PyCodeObject *o = (PyCodeObject*) code;
1065     _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra *) o->co_extra;
1066 
1067     if (co_extra == NULL || co_extra->ce_size <= index) {
1068         Py_ssize_t i = (co_extra == NULL ? 0 : co_extra->ce_size);
1069         co_extra = PyMem_Realloc(
1070                 co_extra,
1071                 sizeof(_PyCodeObjectExtra) +
1072                 (interp->co_extra_user_count-1) * sizeof(void*));
1073         if (co_extra == NULL) {
1074             return -1;
1075         }
1076         for (; i < interp->co_extra_user_count; i++) {
1077             co_extra->ce_extras[i] = NULL;
1078         }
1079         co_extra->ce_size = interp->co_extra_user_count;
1080         o->co_extra = co_extra;
1081     }
1082 
1083     if (co_extra->ce_extras[index] != NULL) {
1084         freefunc free = interp->co_extra_freefuncs[index];
1085         if (free != NULL) {
1086             free(co_extra->ce_extras[index]);
1087         }
1088     }
1089 
1090     co_extra->ce_extras[index] = extra;
1091     return 0;
1092 }
1093