1 #include <stdbool.h>
2 
3 #include "Python.h"
4 #include "code.h"
5 #include "opcode.h"
6 #include "structmember.h"         // PyMemberDef
7 #include "pycore_code.h"          // _PyCodeConstructor
8 #include "pycore_interp.h"        // PyInterpreterState.co_extra_freefuncs
9 #include "pycore_pystate.h"       // _PyInterpreterState_GET()
10 #include "pycore_tuple.h"         // _PyTuple_ITEMS()
11 #include "clinic/codeobject.c.h"
12 
13 
14 /******************
15  * generic helpers
16  ******************/
17 
18 /* all_name_chars(s): true iff s matches [a-zA-Z0-9_]* */
19 static int
all_name_chars(PyObject * o)20 all_name_chars(PyObject *o)
21 {
22     const unsigned char *s, *e;
23 
24     if (!PyUnicode_IS_ASCII(o))
25         return 0;
26 
27     s = PyUnicode_1BYTE_DATA(o);
28     e = s + PyUnicode_GET_LENGTH(o);
29     for (; s != e; s++) {
30         if (!Py_ISALNUM(*s) && *s != '_')
31             return 0;
32     }
33     return 1;
34 }
35 
36 static int
intern_strings(PyObject * tuple)37 intern_strings(PyObject *tuple)
38 {
39     Py_ssize_t i;
40 
41     for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
42         PyObject *v = PyTuple_GET_ITEM(tuple, i);
43         if (v == NULL || !PyUnicode_CheckExact(v)) {
44             PyErr_SetString(PyExc_SystemError,
45                             "non-string found in code slot");
46             return -1;
47         }
48         PyUnicode_InternInPlace(&_PyTuple_ITEMS(tuple)[i]);
49     }
50     return 0;
51 }
52 
53 /* Intern selected string constants */
54 static int
intern_string_constants(PyObject * tuple,int * modified)55 intern_string_constants(PyObject *tuple, int *modified)
56 {
57     for (Py_ssize_t i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
58         PyObject *v = PyTuple_GET_ITEM(tuple, i);
59         if (PyUnicode_CheckExact(v)) {
60             if (PyUnicode_READY(v) == -1) {
61                 return -1;
62             }
63 
64             if (all_name_chars(v)) {
65                 PyObject *w = v;
66                 PyUnicode_InternInPlace(&v);
67                 if (w != v) {
68                     PyTuple_SET_ITEM(tuple, i, v);
69                     if (modified) {
70                         *modified = 1;
71                     }
72                 }
73             }
74         }
75         else if (PyTuple_CheckExact(v)) {
76             if (intern_string_constants(v, NULL) < 0) {
77                 return -1;
78             }
79         }
80         else if (PyFrozenSet_CheckExact(v)) {
81             PyObject *w = v;
82             PyObject *tmp = PySequence_Tuple(v);
83             if (tmp == NULL) {
84                 return -1;
85             }
86             int tmp_modified = 0;
87             if (intern_string_constants(tmp, &tmp_modified) < 0) {
88                 Py_DECREF(tmp);
89                 return -1;
90             }
91             if (tmp_modified) {
92                 v = PyFrozenSet_New(tmp);
93                 if (v == NULL) {
94                     Py_DECREF(tmp);
95                     return -1;
96                 }
97 
98                 PyTuple_SET_ITEM(tuple, i, v);
99                 Py_DECREF(w);
100                 if (modified) {
101                     *modified = 1;
102                 }
103             }
104             Py_DECREF(tmp);
105         }
106     }
107     return 0;
108 }
109 
110 /* Return a shallow copy of a tuple that is
111    guaranteed to contain exact strings, by converting string subclasses
112    to exact strings and complaining if a non-string is found. */
113 static PyObject*
validate_and_copy_tuple(PyObject * tup)114 validate_and_copy_tuple(PyObject *tup)
115 {
116     PyObject *newtuple;
117     PyObject *item;
118     Py_ssize_t i, len;
119 
120     len = PyTuple_GET_SIZE(tup);
121     newtuple = PyTuple_New(len);
122     if (newtuple == NULL)
123         return NULL;
124 
125     for (i = 0; i < len; i++) {
126         item = PyTuple_GET_ITEM(tup, i);
127         if (PyUnicode_CheckExact(item)) {
128             Py_INCREF(item);
129         }
130         else if (!PyUnicode_Check(item)) {
131             PyErr_Format(
132                 PyExc_TypeError,
133                 "name tuples must contain only "
134                 "strings, not '%.500s'",
135                 Py_TYPE(item)->tp_name);
136             Py_DECREF(newtuple);
137             return NULL;
138         }
139         else {
140             item = _PyUnicode_Copy(item);
141             if (item == NULL) {
142                 Py_DECREF(newtuple);
143                 return NULL;
144             }
145         }
146         PyTuple_SET_ITEM(newtuple, i, item);
147     }
148 
149     return newtuple;
150 }
151 
152 
153 /******************
154  * _PyCode_New()
155  ******************/
156 
157 // This is also used in compile.c.
158 void
_Py_set_localsplus_info(int offset,PyObject * name,_PyLocals_Kind kind,PyObject * names,PyObject * kinds)159 _Py_set_localsplus_info(int offset, PyObject *name, _PyLocals_Kind kind,
160                         PyObject *names, PyObject *kinds)
161 {
162     Py_INCREF(name);
163     PyTuple_SET_ITEM(names, offset, name);
164     _PyLocals_SetKind(kinds, offset, kind);
165 }
166 
167 static void
get_localsplus_counts(PyObject * names,PyObject * kinds,int * pnlocals,int * pnplaincellvars,int * pncellvars,int * pnfreevars)168 get_localsplus_counts(PyObject *names, PyObject *kinds,
169                       int *pnlocals, int *pnplaincellvars, int *pncellvars,
170                       int *pnfreevars)
171 {
172     int nlocals = 0;
173     int nplaincellvars = 0;
174     int ncellvars = 0;
175     int nfreevars = 0;
176     Py_ssize_t nlocalsplus = PyTuple_GET_SIZE(names);
177     for (int i = 0; i < nlocalsplus; i++) {
178         _PyLocals_Kind kind = _PyLocals_GetKind(kinds, i);
179         if (kind & CO_FAST_LOCAL) {
180             nlocals += 1;
181             if (kind & CO_FAST_CELL) {
182                 ncellvars += 1;
183             }
184         }
185         else if (kind & CO_FAST_CELL) {
186             ncellvars += 1;
187             nplaincellvars += 1;
188         }
189         else if (kind & CO_FAST_FREE) {
190             nfreevars += 1;
191         }
192     }
193     if (pnlocals != NULL) {
194         *pnlocals = nlocals;
195     }
196     if (pnplaincellvars != NULL) {
197         *pnplaincellvars = nplaincellvars;
198     }
199     if (pncellvars != NULL) {
200         *pncellvars = ncellvars;
201     }
202     if (pnfreevars != NULL) {
203         *pnfreevars = nfreevars;
204     }
205 }
206 
207 static PyObject *
get_localsplus_names(PyCodeObject * co,_PyLocals_Kind kind,int num)208 get_localsplus_names(PyCodeObject *co, _PyLocals_Kind kind, int num)
209 {
210     PyObject *names = PyTuple_New(num);
211     if (names == NULL) {
212         return NULL;
213     }
214     int index = 0;
215     for (int offset = 0; offset < co->co_nlocalsplus; offset++) {
216         _PyLocals_Kind k = _PyLocals_GetKind(co->co_localspluskinds, offset);
217         if ((k & kind) == 0) {
218             continue;
219         }
220         assert(index < num);
221         PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, offset);
222         Py_INCREF(name);
223         PyTuple_SET_ITEM(names, index, name);
224         index += 1;
225     }
226     assert(index == num);
227     return names;
228 }
229 
230 int
_PyCode_Validate(struct _PyCodeConstructor * con)231 _PyCode_Validate(struct _PyCodeConstructor *con)
232 {
233     /* Check argument types */
234     if (con->argcount < con->posonlyargcount || con->posonlyargcount < 0 ||
235         con->kwonlyargcount < 0 ||
236         con->stacksize < 0 || con->flags < 0 ||
237         con->code == NULL || !PyBytes_Check(con->code) ||
238         con->consts == NULL || !PyTuple_Check(con->consts) ||
239         con->names == NULL || !PyTuple_Check(con->names) ||
240         con->localsplusnames == NULL || !PyTuple_Check(con->localsplusnames) ||
241         con->localspluskinds == NULL || !PyBytes_Check(con->localspluskinds) ||
242         PyTuple_GET_SIZE(con->localsplusnames)
243             != PyBytes_GET_SIZE(con->localspluskinds) ||
244         con->name == NULL || !PyUnicode_Check(con->name) ||
245         con->qualname == NULL || !PyUnicode_Check(con->qualname) ||
246         con->filename == NULL || !PyUnicode_Check(con->filename) ||
247         con->linetable == NULL || !PyBytes_Check(con->linetable) ||
248         con->endlinetable == NULL ||
249         (con->endlinetable != Py_None && !PyBytes_Check(con->endlinetable)) ||
250         con->columntable == NULL ||
251         (con->columntable != Py_None && !PyBytes_Check(con->columntable)) ||
252         con->exceptiontable == NULL || !PyBytes_Check(con->exceptiontable)
253         ) {
254         PyErr_BadInternalCall();
255         return -1;
256     }
257 
258     /* Make sure that code is indexable with an int, this is
259        a long running assumption in ceval.c and many parts of
260        the interpreter. */
261     if (PyBytes_GET_SIZE(con->code) > INT_MAX) {
262         PyErr_SetString(PyExc_OverflowError,
263                         "code: co_code larger than INT_MAX");
264         return -1;
265     }
266     if (PyBytes_GET_SIZE(con->code) % sizeof(_Py_CODEUNIT) != 0 ||
267         !_Py_IS_ALIGNED(PyBytes_AS_STRING(con->code), sizeof(_Py_CODEUNIT))
268         ) {
269         PyErr_SetString(PyExc_ValueError, "code: co_code is malformed");
270         return -1;
271     }
272 
273     /* Ensure that the co_varnames has enough names to cover the arg counts.
274      * Note that totalargs = nlocals - nplainlocals.  We check nplainlocals
275      * here to avoid the possibility of overflow (however remote). */
276     int nlocals;
277     get_localsplus_counts(con->localsplusnames, con->localspluskinds,
278                           &nlocals, NULL, NULL, NULL);
279     int nplainlocals = nlocals -
280                        con->argcount -
281                        con->kwonlyargcount -
282                        ((con->flags & CO_VARARGS) != 0) -
283                        ((con->flags & CO_VARKEYWORDS) != 0);
284     if (nplainlocals < 0) {
285         PyErr_SetString(PyExc_ValueError, "code: co_varnames is too small");
286         return -1;
287     }
288 
289     return 0;
290 }
291 
292 static void
init_code(PyCodeObject * co,struct _PyCodeConstructor * con)293 init_code(PyCodeObject *co, struct _PyCodeConstructor *con)
294 {
295     int nlocalsplus = (int)PyTuple_GET_SIZE(con->localsplusnames);
296     int nlocals, nplaincellvars, ncellvars, nfreevars;
297     get_localsplus_counts(con->localsplusnames, con->localspluskinds,
298                           &nlocals, &nplaincellvars, &ncellvars, &nfreevars);
299 
300     Py_INCREF(con->filename);
301     co->co_filename = con->filename;
302     Py_INCREF(con->name);
303     co->co_name = con->name;
304     Py_INCREF(con->qualname);
305     co->co_qualname = con->qualname;
306     co->co_flags = con->flags;
307 
308     Py_INCREF(con->code);
309     co->co_code = con->code;
310     co->co_firstinstr = (_Py_CODEUNIT *)PyBytes_AS_STRING(con->code);
311     co->co_firstlineno = con->firstlineno;
312     Py_INCREF(con->linetable);
313     co->co_linetable = con->linetable;
314     Py_INCREF(con->endlinetable);
315     co->co_endlinetable = con->endlinetable;
316     Py_INCREF(con->columntable);
317     co->co_columntable = con->columntable;
318 
319     Py_INCREF(con->consts);
320     co->co_consts = con->consts;
321     Py_INCREF(con->names);
322     co->co_names = con->names;
323 
324     Py_INCREF(con->localsplusnames);
325     co->co_localsplusnames = con->localsplusnames;
326     Py_INCREF(con->localspluskinds);
327     co->co_localspluskinds = con->localspluskinds;
328 
329     co->co_argcount = con->argcount;
330     co->co_posonlyargcount = con->posonlyargcount;
331     co->co_kwonlyargcount = con->kwonlyargcount;
332 
333     co->co_stacksize = con->stacksize;
334 
335     Py_INCREF(con->exceptiontable);
336     co->co_exceptiontable = con->exceptiontable;
337 
338     /* derived values */
339     co->co_nlocalsplus = nlocalsplus;
340     co->co_nlocals = nlocals;
341     co->co_nplaincellvars = nplaincellvars;
342     co->co_ncellvars = ncellvars;
343     co->co_nfreevars = nfreevars;
344     co->co_varnames = NULL;
345     co->co_cellvars = NULL;
346     co->co_freevars = NULL;
347 
348     /* not set */
349     co->co_weakreflist = NULL;
350     co->co_extra = NULL;
351 
352     co->co_warmup = QUICKENING_INITIAL_WARMUP_VALUE;
353     co->co_quickened = NULL;
354 }
355 
356 /* The caller is responsible for ensuring that the given data is valid. */
357 
358 PyCodeObject *
_PyCode_New(struct _PyCodeConstructor * con)359 _PyCode_New(struct _PyCodeConstructor *con)
360 {
361     /* Ensure that strings are ready Unicode string */
362     if (PyUnicode_READY(con->name) < 0) {
363         return NULL;
364     }
365     if (PyUnicode_READY(con->qualname) < 0) {
366         return NULL;
367     }
368     if (PyUnicode_READY(con->filename) < 0) {
369         return NULL;
370     }
371 
372     if (intern_strings(con->names) < 0) {
373         return NULL;
374     }
375     if (intern_string_constants(con->consts, NULL) < 0) {
376         return NULL;
377     }
378     if (intern_strings(con->localsplusnames) < 0) {
379         return NULL;
380     }
381 
382     // Discard the endlinetable and columntable if we are opted out of debug
383     // ranges.
384     if (!_Py_GetConfig()->code_debug_ranges) {
385         con->endlinetable = Py_None;
386         con->columntable = Py_None;
387     }
388 
389     PyCodeObject *co = PyObject_New(PyCodeObject, &PyCode_Type);
390     if (co == NULL) {
391         PyErr_NoMemory();
392         return NULL;
393     }
394     init_code(co, con);
395 
396     return co;
397 }
398 
399 
400 /******************
401  * the legacy "constructors"
402  ******************/
403 
404 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,PyObject * qualname,int firstlineno,PyObject * linetable,PyObject * endlinetable,PyObject * columntable,PyObject * exceptiontable)405 PyCode_NewWithPosOnlyArgs(int argcount, int posonlyargcount, int kwonlyargcount,
406                           int nlocals, int stacksize, int flags,
407                           PyObject *code, PyObject *consts, PyObject *names,
408                           PyObject *varnames, PyObject *freevars, PyObject *cellvars,
409                           PyObject *filename, PyObject *name,
410                           PyObject *qualname, int firstlineno,
411                           PyObject *linetable, PyObject *endlinetable,
412                           PyObject *columntable, PyObject *exceptiontable)
413 {
414     PyCodeObject *co = NULL;
415     PyObject *localsplusnames = NULL;
416     PyObject *localspluskinds = NULL;
417 
418     if (varnames == NULL || !PyTuple_Check(varnames) ||
419         cellvars == NULL || !PyTuple_Check(cellvars) ||
420         freevars == NULL || !PyTuple_Check(freevars)
421         ) {
422         PyErr_BadInternalCall();
423         return NULL;
424     }
425 
426     // Set the "fast locals plus" info.
427     int nvarnames = (int)PyTuple_GET_SIZE(varnames);
428     int ncellvars = (int)PyTuple_GET_SIZE(cellvars);
429     int nfreevars = (int)PyTuple_GET_SIZE(freevars);
430     int nlocalsplus = nvarnames + ncellvars + nfreevars;
431     localsplusnames = PyTuple_New(nlocalsplus);
432     if (localsplusnames == NULL) {
433         goto error;
434     }
435     localspluskinds = PyBytes_FromStringAndSize(NULL, nlocalsplus);
436     if (localspluskinds == NULL) {
437         goto error;
438     }
439     int  offset = 0;
440     for (int i = 0; i < nvarnames; i++, offset++) {
441         PyObject *name = PyTuple_GET_ITEM(varnames, i);
442         _Py_set_localsplus_info(offset, name, CO_FAST_LOCAL,
443                                localsplusnames, localspluskinds);
444     }
445     for (int i = 0; i < ncellvars; i++, offset++) {
446         PyObject *name = PyTuple_GET_ITEM(cellvars, i);
447         int argoffset = -1;
448         for (int j = 0; j < nvarnames; j++) {
449             int cmp = PyUnicode_Compare(PyTuple_GET_ITEM(varnames, j),
450                                         name);
451             assert(!PyErr_Occurred());
452             if (cmp == 0) {
453                 argoffset = j;
454                 break;
455             }
456         }
457         if (argoffset >= 0) {
458             // Merge the localsplus indices.
459             nlocalsplus -= 1;
460             offset -= 1;
461             _PyLocals_Kind kind = _PyLocals_GetKind(localspluskinds, argoffset);
462             _PyLocals_SetKind(localspluskinds, argoffset, kind | CO_FAST_CELL);
463             continue;
464         }
465         _Py_set_localsplus_info(offset, name, CO_FAST_CELL,
466                                localsplusnames, localspluskinds);
467     }
468     for (int i = 0; i < nfreevars; i++, offset++) {
469         PyObject *name = PyTuple_GET_ITEM(freevars, i);
470         _Py_set_localsplus_info(offset, name, CO_FAST_FREE,
471                                localsplusnames, localspluskinds);
472     }
473     // If any cells were args then nlocalsplus will have shrunk.
474     if (nlocalsplus != PyTuple_GET_SIZE(localsplusnames)) {
475         if (_PyTuple_Resize(&localsplusnames, nlocalsplus) < 0
476                 || _PyBytes_Resize(&localspluskinds, nlocalsplus) < 0) {
477             goto error;
478         }
479     }
480 
481     struct _PyCodeConstructor con = {
482         .filename = filename,
483         .name = name,
484         .qualname = qualname,
485         .flags = flags,
486 
487         .code = code,
488         .firstlineno = firstlineno,
489         .linetable = linetable,
490         .endlinetable = endlinetable,
491         .columntable = columntable,
492 
493         .consts = consts,
494         .names = names,
495 
496         .localsplusnames = localsplusnames,
497         .localspluskinds = localspluskinds,
498 
499         .argcount = argcount,
500         .posonlyargcount = posonlyargcount,
501         .kwonlyargcount = kwonlyargcount,
502 
503         .stacksize = stacksize,
504 
505         .exceptiontable = exceptiontable,
506     };
507 
508     if (_PyCode_Validate(&con) < 0) {
509         goto error;
510     }
511     assert(PyBytes_GET_SIZE(code) % sizeof(_Py_CODEUNIT) == 0);
512     assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(code), sizeof(_Py_CODEUNIT)));
513     if (nlocals != PyTuple_GET_SIZE(varnames)) {
514         PyErr_SetString(PyExc_ValueError,
515                         "code: co_nlocals != len(co_varnames)");
516         goto error;
517     }
518 
519     co = _PyCode_New(&con);
520     if (co == NULL) {
521         goto error;
522     }
523 
524     Py_INCREF(varnames);
525     co->co_varnames = varnames;
526     Py_INCREF(cellvars);
527     co->co_cellvars = cellvars;
528     Py_INCREF(freevars);
529     co->co_freevars = freevars;
530 
531 error:
532     Py_XDECREF(localsplusnames);
533     Py_XDECREF(localspluskinds);
534     return co;
535 }
536 
537 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,PyObject * qualname,int firstlineno,PyObject * linetable,PyObject * endlinetable,PyObject * columntable,PyObject * exceptiontable)538 PyCode_New(int argcount, int kwonlyargcount,
539            int nlocals, int stacksize, int flags,
540            PyObject *code, PyObject *consts, PyObject *names,
541            PyObject *varnames, PyObject *freevars, PyObject *cellvars,
542            PyObject *filename, PyObject *name, PyObject *qualname,
543            int firstlineno, PyObject *linetable, PyObject *endlinetable,
544            PyObject *columntable, PyObject *exceptiontable)
545 {
546     return PyCode_NewWithPosOnlyArgs(argcount, 0, kwonlyargcount, nlocals,
547                                      stacksize, flags, code, consts, names,
548                                      varnames, freevars, cellvars, filename,
549                                      name, qualname, firstlineno, linetable,
550                                      endlinetable, columntable, exceptiontable);
551 }
552 
553 PyCodeObject *
PyCode_NewEmpty(const char * filename,const char * funcname,int firstlineno)554 PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
555 {
556     PyObject *emptystring = NULL;
557     PyObject *nulltuple = NULL;
558     PyObject *filename_ob = NULL;
559     PyObject *funcname_ob = NULL;
560     PyCodeObject *result = NULL;
561 
562     emptystring = PyBytes_FromString("");
563     if (emptystring == NULL) {
564         goto failed;
565     }
566     nulltuple = PyTuple_New(0);
567     if (nulltuple == NULL) {
568         goto failed;
569     }
570     funcname_ob = PyUnicode_FromString(funcname);
571     if (funcname_ob == NULL) {
572         goto failed;
573     }
574     filename_ob = PyUnicode_DecodeFSDefault(filename);
575     if (filename_ob == NULL) {
576         goto failed;
577     }
578 
579     struct _PyCodeConstructor con = {
580         .filename = filename_ob,
581         .name = funcname_ob,
582         .qualname = funcname_ob,
583         .code = emptystring,
584         .firstlineno = firstlineno,
585         .linetable = emptystring,
586         .endlinetable = emptystring,
587         .columntable = emptystring,
588         .consts = nulltuple,
589         .names = nulltuple,
590         .localsplusnames = nulltuple,
591         .localspluskinds = emptystring,
592         .exceptiontable = emptystring,
593     };
594     result = _PyCode_New(&con);
595 
596 failed:
597     Py_XDECREF(emptystring);
598     Py_XDECREF(nulltuple);
599     Py_XDECREF(funcname_ob);
600     Py_XDECREF(filename_ob);
601     return result;
602 }
603 
604 
605 /******************
606  * source location tracking (co_lines/co_positions)
607  ******************/
608 
609 /* Use co_linetable to compute the line number from a bytecode index, addrq.  See
610    lnotab_notes.txt for the details of the lnotab representation.
611 */
612 
613 int
PyCode_Addr2Line(PyCodeObject * co,int addrq)614 PyCode_Addr2Line(PyCodeObject *co, int addrq)
615 {
616     if (addrq < 0) {
617         return co->co_firstlineno;
618     }
619     assert(addrq >= 0 && addrq < PyBytes_GET_SIZE(co->co_code));
620     PyCodeAddressRange bounds;
621     _PyCode_InitAddressRange(co, &bounds);
622     return _PyCode_CheckLineNumber(addrq, &bounds);
623 }
624 
625 int
PyCode_Addr2Location(PyCodeObject * co,int addrq,int * start_line,int * start_column,int * end_line,int * end_column)626 PyCode_Addr2Location(PyCodeObject *co, int addrq,
627                      int *start_line, int *start_column,
628                      int *end_line, int *end_column)
629 {
630     *start_line = PyCode_Addr2Line(co, addrq);
631     *start_column = _PyCode_Addr2Offset(co, addrq);
632     *end_line = _PyCode_Addr2EndLine(co, addrq);
633     *end_column = _PyCode_Addr2EndOffset(co, addrq);
634     return 1;
635 }
636 
637 int
_PyCode_Addr2EndLine(PyCodeObject * co,int addrq)638 _PyCode_Addr2EndLine(PyCodeObject* co, int addrq)
639 {
640     if (addrq < 0) {
641         return co->co_firstlineno;
642     }
643     else if (co->co_endlinetable == Py_None) {
644         return -1;
645     }
646 
647     assert(addrq >= 0 && addrq < PyBytes_GET_SIZE(co->co_code));
648     PyCodeAddressRange bounds;
649     _PyCode_InitEndAddressRange(co, &bounds);
650     return _PyCode_CheckLineNumber(addrq, &bounds);
651 }
652 
653 int
_PyCode_Addr2Offset(PyCodeObject * co,int addrq)654 _PyCode_Addr2Offset(PyCodeObject* co, int addrq)
655 {
656     if (co->co_columntable == Py_None || addrq < 0) {
657         return -1;
658     }
659     addrq /= sizeof(_Py_CODEUNIT);
660     if (addrq*2 >= PyBytes_GET_SIZE(co->co_columntable)) {
661         return -1;
662     }
663 
664     unsigned char* bytes = (unsigned char*)PyBytes_AS_STRING(co->co_columntable);
665     return bytes[addrq*2] - 1;
666 }
667 
668 int
_PyCode_Addr2EndOffset(PyCodeObject * co,int addrq)669 _PyCode_Addr2EndOffset(PyCodeObject* co, int addrq)
670 {
671     if (co->co_columntable == Py_None || addrq < 0) {
672         return -1;
673     }
674     addrq /= sizeof(_Py_CODEUNIT);
675     if (addrq*2+1 >= PyBytes_GET_SIZE(co->co_columntable)) {
676         return -1;
677     }
678 
679     unsigned char* bytes = (unsigned char*)PyBytes_AS_STRING(co->co_columntable);
680     return bytes[addrq*2+1] - 1;
681 }
682 
683 void
PyLineTable_InitAddressRange(const char * linetable,Py_ssize_t length,int firstlineno,PyCodeAddressRange * range)684 PyLineTable_InitAddressRange(const char *linetable, Py_ssize_t length, int firstlineno, PyCodeAddressRange *range)
685 {
686     range->opaque.lo_next = linetable;
687     range->opaque.limit = range->opaque.lo_next + length;
688     range->ar_start = -1;
689     range->ar_end = 0;
690     range->opaque.computed_line = firstlineno;
691     range->ar_line = -1;
692 }
693 
694 int
_PyCode_InitAddressRange(PyCodeObject * co,PyCodeAddressRange * bounds)695 _PyCode_InitAddressRange(PyCodeObject* co, PyCodeAddressRange *bounds)
696 {
697     const char *linetable = PyBytes_AS_STRING(co->co_linetable);
698     Py_ssize_t length = PyBytes_GET_SIZE(co->co_linetable);
699     PyLineTable_InitAddressRange(linetable, length, co->co_firstlineno, bounds);
700     return bounds->ar_line;
701 }
702 
703 int
_PyCode_InitEndAddressRange(PyCodeObject * co,PyCodeAddressRange * bounds)704 _PyCode_InitEndAddressRange(PyCodeObject* co, PyCodeAddressRange* bounds)
705 {
706     char* linetable = PyBytes_AS_STRING(co->co_endlinetable);
707     Py_ssize_t length = PyBytes_GET_SIZE(co->co_endlinetable);
708     PyLineTable_InitAddressRange(linetable, length, co->co_firstlineno, bounds);
709     return bounds->ar_line;
710 }
711 
712 /* Update *bounds to describe the first and one-past-the-last instructions in
713    the same line as lasti.  Return the number of that line, or -1 if lasti is out of bounds. */
714 int
_PyCode_CheckLineNumber(int lasti,PyCodeAddressRange * bounds)715 _PyCode_CheckLineNumber(int lasti, PyCodeAddressRange *bounds)
716 {
717     while (bounds->ar_end <= lasti) {
718         if (!PyLineTable_NextAddressRange(bounds)) {
719             return -1;
720         }
721     }
722     while (bounds->ar_start > lasti) {
723         if (!PyLineTable_PreviousAddressRange(bounds)) {
724             return -1;
725         }
726     }
727     return bounds->ar_line;
728 }
729 
730 static void
retreat(PyCodeAddressRange * bounds)731 retreat(PyCodeAddressRange *bounds)
732 {
733     int ldelta = ((signed char *)bounds->opaque.lo_next)[-1];
734     if (ldelta == -128) {
735         ldelta = 0;
736     }
737     bounds->opaque.computed_line -= ldelta;
738     bounds->opaque.lo_next -= 2;
739     bounds->ar_end = bounds->ar_start;
740     bounds->ar_start -= ((unsigned char *)bounds->opaque.lo_next)[-2];
741     ldelta = ((signed char *)bounds->opaque.lo_next)[-1];
742     if (ldelta == -128) {
743         bounds->ar_line = -1;
744     }
745     else {
746         bounds->ar_line = bounds->opaque.computed_line;
747     }
748 }
749 
750 static void
advance(PyCodeAddressRange * bounds)751 advance(PyCodeAddressRange *bounds)
752 {
753     bounds->ar_start = bounds->ar_end;
754     int delta = ((unsigned char *)bounds->opaque.lo_next)[0];
755     bounds->ar_end += delta;
756     int ldelta = ((signed char *)bounds->opaque.lo_next)[1];
757     bounds->opaque.lo_next += 2;
758     if (ldelta == -128) {
759         bounds->ar_line = -1;
760     }
761     else {
762         bounds->opaque.computed_line += ldelta;
763         bounds->ar_line = bounds->opaque.computed_line;
764     }
765 }
766 
767 static inline int
at_end(PyCodeAddressRange * bounds)768 at_end(PyCodeAddressRange *bounds) {
769     return bounds->opaque.lo_next >= bounds->opaque.limit;
770 }
771 
772 int
PyLineTable_PreviousAddressRange(PyCodeAddressRange * range)773 PyLineTable_PreviousAddressRange(PyCodeAddressRange *range)
774 {
775     if (range->ar_start <= 0) {
776         return 0;
777     }
778     retreat(range);
779     while (range->ar_start == range->ar_end) {
780         assert(range->ar_start > 0);
781         retreat(range);
782     }
783     return 1;
784 }
785 
786 int
PyLineTable_NextAddressRange(PyCodeAddressRange * range)787 PyLineTable_NextAddressRange(PyCodeAddressRange *range)
788 {
789     if (at_end(range)) {
790         return 0;
791     }
792     advance(range);
793     while (range->ar_start == range->ar_end) {
794         assert(!at_end(range));
795         advance(range);
796     }
797     return 1;
798 }
799 
800 static int
emit_pair(PyObject ** bytes,int * offset,int a,int b)801 emit_pair(PyObject **bytes, int *offset, int a, int b)
802 {
803     Py_ssize_t len = PyBytes_GET_SIZE(*bytes);
804     if (*offset + 2 >= len) {
805         if (_PyBytes_Resize(bytes, len * 2) < 0)
806             return 0;
807     }
808     unsigned char *lnotab = (unsigned char *) PyBytes_AS_STRING(*bytes);
809     lnotab += *offset;
810     *lnotab++ = a;
811     *lnotab++ = b;
812     *offset += 2;
813     return 1;
814 }
815 
816 static int
emit_delta(PyObject ** bytes,int bdelta,int ldelta,int * offset)817 emit_delta(PyObject **bytes, int bdelta, int ldelta, int *offset)
818 {
819     while (bdelta > 255) {
820         if (!emit_pair(bytes, offset, 255, 0)) {
821             return 0;
822         }
823         bdelta -= 255;
824     }
825     while (ldelta > 127) {
826         if (!emit_pair(bytes, offset, bdelta, 127)) {
827             return 0;
828         }
829         bdelta = 0;
830         ldelta -= 127;
831     }
832     while (ldelta < -128) {
833         if (!emit_pair(bytes, offset, bdelta, -128)) {
834             return 0;
835         }
836         bdelta = 0;
837         ldelta += 128;
838     }
839     return emit_pair(bytes, offset, bdelta, ldelta);
840 }
841 
842 static PyObject *
decode_linetable(PyCodeObject * code)843 decode_linetable(PyCodeObject *code)
844 {
845     PyCodeAddressRange bounds;
846     PyObject *bytes;
847     int table_offset = 0;
848     int code_offset = 0;
849     int line = code->co_firstlineno;
850     bytes = PyBytes_FromStringAndSize(NULL, 64);
851     if (bytes == NULL) {
852         return NULL;
853     }
854     _PyCode_InitAddressRange(code, &bounds);
855     while (PyLineTable_NextAddressRange(&bounds)) {
856         if (bounds.opaque.computed_line != line) {
857             int bdelta = bounds.ar_start - code_offset;
858             int ldelta = bounds.opaque.computed_line - line;
859             if (!emit_delta(&bytes, bdelta, ldelta, &table_offset)) {
860                 Py_DECREF(bytes);
861                 return NULL;
862             }
863             code_offset = bounds.ar_start;
864             line = bounds.opaque.computed_line;
865         }
866     }
867     _PyBytes_Resize(&bytes, table_offset);
868     return bytes;
869 }
870 
871 
872 typedef struct {
873     PyObject_HEAD
874     PyCodeObject *li_code;
875     PyCodeAddressRange li_line;
876     char *li_end;
877 } lineiterator;
878 
879 
880 static void
lineiter_dealloc(lineiterator * li)881 lineiter_dealloc(lineiterator *li)
882 {
883     Py_DECREF(li->li_code);
884     Py_TYPE(li)->tp_free(li);
885 }
886 
887 static PyObject *
lineiter_next(lineiterator * li)888 lineiter_next(lineiterator *li)
889 {
890     PyCodeAddressRange *bounds = &li->li_line;
891     if (!PyLineTable_NextAddressRange(bounds)) {
892         return NULL;
893     }
894     PyObject *start = NULL;
895     PyObject *end = NULL;
896     PyObject *line = NULL;
897     PyObject *result = PyTuple_New(3);
898     start = PyLong_FromLong(bounds->ar_start);
899     end = PyLong_FromLong(bounds->ar_end);
900     if (bounds->ar_line < 0) {
901         Py_INCREF(Py_None);
902         line = Py_None;
903     }
904     else {
905         line = PyLong_FromLong(bounds->ar_line);
906     }
907     if (result == NULL || start == NULL || end == NULL || line == NULL) {
908         goto error;
909     }
910     PyTuple_SET_ITEM(result, 0, start);
911     PyTuple_SET_ITEM(result, 1, end);
912     PyTuple_SET_ITEM(result, 2, line);
913     return result;
914 error:
915     Py_XDECREF(start);
916     Py_XDECREF(end);
917     Py_XDECREF(line);
918     Py_XDECREF(result);
919     return result;
920 }
921 
922 static PyTypeObject LineIterator = {
923     PyVarObject_HEAD_INIT(&PyType_Type, 0)
924     "line_iterator",                    /* tp_name */
925     sizeof(lineiterator),               /* tp_basicsize */
926     0,                                  /* tp_itemsize */
927     /* methods */
928     (destructor)lineiter_dealloc,       /* tp_dealloc */
929     0,                                  /* tp_vectorcall_offset */
930     0,                                  /* tp_getattr */
931     0,                                  /* tp_setattr */
932     0,                                  /* tp_as_async */
933     0,                                  /* tp_repr */
934     0,                                  /* tp_as_number */
935     0,                                  /* tp_as_sequence */
936     0,                                  /* tp_as_mapping */
937     0,                                  /* tp_hash */
938     0,                                  /* tp_call */
939     0,                                  /* tp_str */
940     0,                                  /* tp_getattro */
941     0,                                  /* tp_setattro */
942     0,                                  /* tp_as_buffer */
943     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,       /* tp_flags */
944     0,                                  /* tp_doc */
945     0,                                  /* tp_traverse */
946     0,                                  /* tp_clear */
947     0,                                  /* tp_richcompare */
948     0,                                  /* tp_weaklistoffset */
949     PyObject_SelfIter,                  /* tp_iter */
950     (iternextfunc)lineiter_next,        /* tp_iternext */
951     0,                                  /* tp_methods */
952     0,                                  /* tp_members */
953     0,                                  /* tp_getset */
954     0,                                  /* tp_base */
955     0,                                  /* tp_dict */
956     0,                                  /* tp_descr_get */
957     0,                                  /* tp_descr_set */
958     0,                                  /* tp_dictoffset */
959     0,                                  /* tp_init */
960     0,                                  /* tp_alloc */
961     0,                                  /* tp_new */
962     PyObject_Del,                       /* tp_free */
963 };
964 
965 static lineiterator *
new_linesiterator(PyCodeObject * code)966 new_linesiterator(PyCodeObject *code)
967 {
968     lineiterator *li = (lineiterator *)PyType_GenericAlloc(&LineIterator, 0);
969     if (li == NULL) {
970         return NULL;
971     }
972     Py_INCREF(code);
973     li->li_code = code;
974     _PyCode_InitAddressRange(code, &li->li_line);
975     return li;
976 }
977 
978 /* co_positions iterator object. */
979 typedef struct {
980     PyObject_HEAD
981     PyCodeObject* pi_code;
982     int pi_offset;
983 } positionsiterator;
984 
985 static void
positionsiter_dealloc(positionsiterator * pi)986 positionsiter_dealloc(positionsiterator* pi)
987 {
988     Py_DECREF(pi->pi_code);
989     Py_TYPE(pi)->tp_free(pi);
990 }
991 
992 static PyObject*
_source_offset_converter(int * value)993 _source_offset_converter(int* value) {
994     if (*value == -1) {
995         Py_RETURN_NONE;
996     }
997     return PyLong_FromLong(*value);
998 }
999 
1000 static PyObject*
positionsiter_next(positionsiterator * pi)1001 positionsiter_next(positionsiterator* pi)
1002 {
1003     if (pi->pi_offset >= PyBytes_GET_SIZE(pi->pi_code->co_code)) {
1004         return NULL;
1005     }
1006 
1007     int start_line, start_col, end_line, end_col;
1008     if (!PyCode_Addr2Location(pi->pi_code, pi->pi_offset, &start_line,
1009                               &start_col, &end_line, &end_col)) {
1010         return NULL;
1011     }
1012 
1013     pi->pi_offset += 2;
1014     return Py_BuildValue("(O&O&O&O&)",
1015         _source_offset_converter, &start_line,
1016         _source_offset_converter, &end_line,
1017         _source_offset_converter, &start_col,
1018         _source_offset_converter, &end_col);
1019 }
1020 
1021 static PyTypeObject PositionsIterator = {
1022     PyVarObject_HEAD_INIT(&PyType_Type, 0)
1023     "poisitions_iterator",              /* tp_name */
1024     sizeof(positionsiterator),          /* tp_basicsize */
1025     0,                                  /* tp_itemsize */
1026     /* methods */
1027     (destructor)positionsiter_dealloc,  /* tp_dealloc */
1028     0,                                  /* tp_vectorcall_offset */
1029     0,                                  /* tp_getattr */
1030     0,                                  /* tp_setattr */
1031     0,                                  /* tp_as_async */
1032     0,                                  /* tp_repr */
1033     0,                                  /* tp_as_number */
1034     0,                                  /* tp_as_sequence */
1035     0,                                  /* tp_as_mapping */
1036     0,                                  /* tp_hash */
1037     0,                                  /* tp_call */
1038     0,                                  /* tp_str */
1039     0,                                  /* tp_getattro */
1040     0,                                  /* tp_setattro */
1041     0,                                  /* tp_as_buffer */
1042     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,       /* tp_flags */
1043     0,                                  /* tp_doc */
1044     0,                                  /* tp_traverse */
1045     0,                                  /* tp_clear */
1046     0,                                  /* tp_richcompare */
1047     0,                                  /* tp_weaklistoffset */
1048     PyObject_SelfIter,                  /* tp_iter */
1049     (iternextfunc)positionsiter_next,   /* tp_iternext */
1050     0,                                  /* tp_methods */
1051     0,                                  /* tp_members */
1052     0,                                  /* tp_getset */
1053     0,                                  /* tp_base */
1054     0,                                  /* tp_dict */
1055     0,                                  /* tp_descr_get */
1056     0,                                  /* tp_descr_set */
1057     0,                                  /* tp_dictoffset */
1058     0,                                  /* tp_init */
1059     0,                                  /* tp_alloc */
1060     0,                                  /* tp_new */
1061     PyObject_Del,                       /* tp_free */
1062 };
1063 
1064 static PyObject*
code_positionsiterator(PyCodeObject * code,PyObject * Py_UNUSED (args))1065 code_positionsiterator(PyCodeObject* code, PyObject* Py_UNUSED(args))
1066 {
1067     positionsiterator* pi = (positionsiterator*)PyType_GenericAlloc(&PositionsIterator, 0);
1068     if (pi == NULL) {
1069         return NULL;
1070     }
1071     Py_INCREF(code);
1072     pi->pi_code = code;
1073     pi->pi_offset = 0;
1074     return (PyObject*)pi;
1075 }
1076 
1077 
1078 /******************
1079  * "extra" frame eval info (see PEP 523)
1080  ******************/
1081 
1082 /* Holder for co_extra information */
1083 typedef struct {
1084     Py_ssize_t ce_size;
1085     void *ce_extras[1];
1086 } _PyCodeObjectExtra;
1087 
1088 
1089 int
_PyCode_GetExtra(PyObject * code,Py_ssize_t index,void ** extra)1090 _PyCode_GetExtra(PyObject *code, Py_ssize_t index, void **extra)
1091 {
1092     if (!PyCode_Check(code)) {
1093         PyErr_BadInternalCall();
1094         return -1;
1095     }
1096 
1097     PyCodeObject *o = (PyCodeObject*) code;
1098     _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) o->co_extra;
1099 
1100     if (co_extra == NULL || co_extra->ce_size <= index) {
1101         *extra = NULL;
1102         return 0;
1103     }
1104 
1105     *extra = co_extra->ce_extras[index];
1106     return 0;
1107 }
1108 
1109 
1110 int
_PyCode_SetExtra(PyObject * code,Py_ssize_t index,void * extra)1111 _PyCode_SetExtra(PyObject *code, Py_ssize_t index, void *extra)
1112 {
1113     PyInterpreterState *interp = _PyInterpreterState_GET();
1114 
1115     if (!PyCode_Check(code) || index < 0 ||
1116             index >= interp->co_extra_user_count) {
1117         PyErr_BadInternalCall();
1118         return -1;
1119     }
1120 
1121     PyCodeObject *o = (PyCodeObject*) code;
1122     _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra *) o->co_extra;
1123 
1124     if (co_extra == NULL || co_extra->ce_size <= index) {
1125         Py_ssize_t i = (co_extra == NULL ? 0 : co_extra->ce_size);
1126         co_extra = PyMem_Realloc(
1127                 co_extra,
1128                 sizeof(_PyCodeObjectExtra) +
1129                 (interp->co_extra_user_count-1) * sizeof(void*));
1130         if (co_extra == NULL) {
1131             return -1;
1132         }
1133         for (; i < interp->co_extra_user_count; i++) {
1134             co_extra->ce_extras[i] = NULL;
1135         }
1136         co_extra->ce_size = interp->co_extra_user_count;
1137         o->co_extra = co_extra;
1138     }
1139 
1140     if (co_extra->ce_extras[index] != NULL) {
1141         freefunc free = interp->co_extra_freefuncs[index];
1142         if (free != NULL) {
1143             free(co_extra->ce_extras[index]);
1144         }
1145     }
1146 
1147     co_extra->ce_extras[index] = extra;
1148     return 0;
1149 }
1150 
1151 
1152 /******************
1153  * other PyCodeObject accessor functions
1154  ******************/
1155 
1156 PyObject *
_PyCode_GetVarnames(PyCodeObject * co)1157 _PyCode_GetVarnames(PyCodeObject *co)
1158 {
1159     if (co->co_varnames == NULL) {
1160         // PyCodeObject owns this reference.
1161         co->co_varnames = get_localsplus_names(co, CO_FAST_LOCAL,
1162                                                co->co_nlocals);
1163         if (co->co_varnames == NULL) {
1164             return NULL;
1165         }
1166     }
1167     Py_INCREF(co->co_varnames);
1168     return co->co_varnames;
1169 }
1170 
1171 PyObject *
_PyCode_GetCellvars(PyCodeObject * co)1172 _PyCode_GetCellvars(PyCodeObject *co)
1173 {
1174     if (co->co_cellvars == NULL) {
1175         // PyCodeObject owns this reference.
1176         co->co_cellvars = get_localsplus_names(co, CO_FAST_CELL,
1177                                                co->co_ncellvars);
1178         if (co->co_cellvars == NULL) {
1179             return NULL;
1180         }
1181     }
1182     Py_INCREF(co->co_cellvars);
1183     return co->co_cellvars;
1184 }
1185 
1186 PyObject *
_PyCode_GetFreevars(PyCodeObject * co)1187 _PyCode_GetFreevars(PyCodeObject *co)
1188 {
1189     if (co->co_freevars == NULL) {
1190         // PyCodeObject owns this reference.
1191         co->co_freevars = get_localsplus_names(co, CO_FAST_FREE,
1192                                                co->co_nfreevars);
1193         if (co->co_freevars == NULL) {
1194             return NULL;
1195         }
1196     }
1197     Py_INCREF(co->co_freevars);
1198     return co->co_freevars;
1199 }
1200 
1201 
1202 /******************
1203  * PyCode_Type
1204  ******************/
1205 
1206 /*[clinic input]
1207 class code "PyCodeObject *" "&PyCode_Type"
1208 [clinic start generated code]*/
1209 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=78aa5d576683bb4b]*/
1210 
1211 /*[clinic input]
1212 @classmethod
1213 code.__new__ as code_new
1214 
1215     argcount: int
1216     posonlyargcount: int
1217     kwonlyargcount: int
1218     nlocals: int
1219     stacksize: int
1220     flags: int
1221     codestring as code: object(subclass_of="&PyBytes_Type")
1222     constants as consts: object(subclass_of="&PyTuple_Type")
1223     names: object(subclass_of="&PyTuple_Type")
1224     varnames: object(subclass_of="&PyTuple_Type")
1225     filename: unicode
1226     name: unicode
1227     qualname: unicode
1228     firstlineno: int
1229     linetable: object(subclass_of="&PyBytes_Type")
1230     endlinetable: object
1231     columntable: object
1232     exceptiontable: object(subclass_of="&PyBytes_Type")
1233     freevars: object(subclass_of="&PyTuple_Type", c_default="NULL") = ()
1234     cellvars: object(subclass_of="&PyTuple_Type", c_default="NULL") = ()
1235     /
1236 
1237 Create a code object.  Not for the faint of heart.
1238 [clinic start generated code]*/
1239 
1240 static PyObject *
code_new_impl(PyTypeObject * type,int argcount,int posonlyargcount,int kwonlyargcount,int nlocals,int stacksize,int flags,PyObject * code,PyObject * consts,PyObject * names,PyObject * varnames,PyObject * filename,PyObject * name,PyObject * qualname,int firstlineno,PyObject * linetable,PyObject * endlinetable,PyObject * columntable,PyObject * exceptiontable,PyObject * freevars,PyObject * cellvars)1241 code_new_impl(PyTypeObject *type, int argcount, int posonlyargcount,
1242               int kwonlyargcount, int nlocals, int stacksize, int flags,
1243               PyObject *code, PyObject *consts, PyObject *names,
1244               PyObject *varnames, PyObject *filename, PyObject *name,
1245               PyObject *qualname, int firstlineno, PyObject *linetable,
1246               PyObject *endlinetable, PyObject *columntable,
1247               PyObject *exceptiontable, PyObject *freevars,
1248               PyObject *cellvars)
1249 /*[clinic end generated code: output=e1d2086aa8da7c08 input=a06cd92369134063]*/
1250 {
1251     PyObject *co = NULL;
1252     PyObject *ournames = NULL;
1253     PyObject *ourvarnames = NULL;
1254     PyObject *ourfreevars = NULL;
1255     PyObject *ourcellvars = NULL;
1256 
1257     if (PySys_Audit("code.__new__", "OOOiiiiii",
1258                     code, filename, name, argcount, posonlyargcount,
1259                     kwonlyargcount, nlocals, stacksize, flags) < 0) {
1260         goto cleanup;
1261     }
1262 
1263     if (argcount < 0) {
1264         PyErr_SetString(
1265             PyExc_ValueError,
1266             "code: argcount must not be negative");
1267         goto cleanup;
1268     }
1269 
1270     if (posonlyargcount < 0) {
1271         PyErr_SetString(
1272             PyExc_ValueError,
1273             "code: posonlyargcount must not be negative");
1274         goto cleanup;
1275     }
1276 
1277     if (kwonlyargcount < 0) {
1278         PyErr_SetString(
1279             PyExc_ValueError,
1280             "code: kwonlyargcount must not be negative");
1281         goto cleanup;
1282     }
1283     if (nlocals < 0) {
1284         PyErr_SetString(
1285             PyExc_ValueError,
1286             "code: nlocals must not be negative");
1287         goto cleanup;
1288     }
1289 
1290     if (!Py_IsNone(endlinetable) && !PyBytes_Check(endlinetable)) {
1291         PyErr_SetString(PyExc_ValueError,
1292                         "code: endlinetable must be None or bytes");
1293         goto cleanup;
1294     }
1295     if (!Py_IsNone(columntable) && !PyBytes_Check(columntable)) {
1296         PyErr_SetString(PyExc_ValueError,
1297                         "code: columntable must be None or bytes");
1298         goto cleanup;
1299     }
1300 
1301     ournames = validate_and_copy_tuple(names);
1302     if (ournames == NULL)
1303         goto cleanup;
1304     ourvarnames = validate_and_copy_tuple(varnames);
1305     if (ourvarnames == NULL)
1306         goto cleanup;
1307     if (freevars)
1308         ourfreevars = validate_and_copy_tuple(freevars);
1309     else
1310         ourfreevars = PyTuple_New(0);
1311     if (ourfreevars == NULL)
1312         goto cleanup;
1313     if (cellvars)
1314         ourcellvars = validate_and_copy_tuple(cellvars);
1315     else
1316         ourcellvars = PyTuple_New(0);
1317     if (ourcellvars == NULL)
1318         goto cleanup;
1319 
1320     co = (PyObject *)PyCode_NewWithPosOnlyArgs(argcount, posonlyargcount,
1321                                                kwonlyargcount,
1322                                                nlocals, stacksize, flags,
1323                                                code, consts, ournames,
1324                                                ourvarnames, ourfreevars,
1325                                                ourcellvars, filename,
1326                                                name, qualname, firstlineno,
1327                                                linetable, endlinetable,
1328                                                columntable, exceptiontable
1329                                               );
1330   cleanup:
1331     Py_XDECREF(ournames);
1332     Py_XDECREF(ourvarnames);
1333     Py_XDECREF(ourfreevars);
1334     Py_XDECREF(ourcellvars);
1335     return co;
1336 }
1337 
1338 static void
code_dealloc(PyCodeObject * co)1339 code_dealloc(PyCodeObject *co)
1340 {
1341     if (co->co_extra != NULL) {
1342         PyInterpreterState *interp = _PyInterpreterState_GET();
1343         _PyCodeObjectExtra *co_extra = co->co_extra;
1344 
1345         for (Py_ssize_t i = 0; i < co_extra->ce_size; i++) {
1346             freefunc free_extra = interp->co_extra_freefuncs[i];
1347 
1348             if (free_extra != NULL) {
1349                 free_extra(co_extra->ce_extras[i]);
1350             }
1351         }
1352 
1353         PyMem_Free(co_extra);
1354     }
1355 
1356     Py_XDECREF(co->co_code);
1357     Py_XDECREF(co->co_consts);
1358     Py_XDECREF(co->co_names);
1359     Py_XDECREF(co->co_localsplusnames);
1360     Py_XDECREF(co->co_localspluskinds);
1361     Py_XDECREF(co->co_varnames);
1362     Py_XDECREF(co->co_freevars);
1363     Py_XDECREF(co->co_cellvars);
1364     Py_XDECREF(co->co_filename);
1365     Py_XDECREF(co->co_name);
1366     Py_XDECREF(co->co_qualname);
1367     Py_XDECREF(co->co_linetable);
1368     Py_XDECREF(co->co_endlinetable);
1369     Py_XDECREF(co->co_columntable);
1370     Py_XDECREF(co->co_exceptiontable);
1371     if (co->co_weakreflist != NULL)
1372         PyObject_ClearWeakRefs((PyObject*)co);
1373     if (co->co_quickened) {
1374         PyMem_Free(co->co_quickened);
1375         _Py_QuickenedCount--;
1376     }
1377     PyObject_Free(co);
1378 }
1379 
1380 static PyObject *
code_repr(PyCodeObject * co)1381 code_repr(PyCodeObject *co)
1382 {
1383     int lineno;
1384     if (co->co_firstlineno != 0)
1385         lineno = co->co_firstlineno;
1386     else
1387         lineno = -1;
1388     if (co->co_filename && PyUnicode_Check(co->co_filename)) {
1389         return PyUnicode_FromFormat(
1390             "<code object %U at %p, file \"%U\", line %d>",
1391             co->co_name, co, co->co_filename, lineno);
1392     } else {
1393         return PyUnicode_FromFormat(
1394             "<code object %U at %p, file ???, line %d>",
1395             co->co_name, co, lineno);
1396     }
1397 }
1398 
1399 static PyObject *
code_richcompare(PyObject * self,PyObject * other,int op)1400 code_richcompare(PyObject *self, PyObject *other, int op)
1401 {
1402     PyCodeObject *co, *cp;
1403     int eq;
1404     PyObject *consts1, *consts2;
1405     PyObject *res;
1406 
1407     if ((op != Py_EQ && op != Py_NE) ||
1408         !PyCode_Check(self) ||
1409         !PyCode_Check(other)) {
1410         Py_RETURN_NOTIMPLEMENTED;
1411     }
1412 
1413     co = (PyCodeObject *)self;
1414     cp = (PyCodeObject *)other;
1415 
1416     eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
1417     if (!eq) goto unequal;
1418     eq = co->co_argcount == cp->co_argcount;
1419     if (!eq) goto unequal;
1420     eq = co->co_posonlyargcount == cp->co_posonlyargcount;
1421     if (!eq) goto unequal;
1422     eq = co->co_kwonlyargcount == cp->co_kwonlyargcount;
1423     if (!eq) goto unequal;
1424     eq = co->co_flags == cp->co_flags;
1425     if (!eq) goto unequal;
1426     eq = co->co_firstlineno == cp->co_firstlineno;
1427     if (!eq) goto unequal;
1428     eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);
1429     if (eq <= 0) goto unequal;
1430 
1431     /* compare constants */
1432     consts1 = _PyCode_ConstantKey(co->co_consts);
1433     if (!consts1)
1434         return NULL;
1435     consts2 = _PyCode_ConstantKey(cp->co_consts);
1436     if (!consts2) {
1437         Py_DECREF(consts1);
1438         return NULL;
1439     }
1440     eq = PyObject_RichCompareBool(consts1, consts2, Py_EQ);
1441     Py_DECREF(consts1);
1442     Py_DECREF(consts2);
1443     if (eq <= 0) goto unequal;
1444 
1445     eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);
1446     if (eq <= 0) goto unequal;
1447     eq = PyObject_RichCompareBool(co->co_localsplusnames,
1448                                   cp->co_localsplusnames, Py_EQ);
1449     if (eq <= 0) goto unequal;
1450 
1451     if (op == Py_EQ)
1452         res = Py_True;
1453     else
1454         res = Py_False;
1455     goto done;
1456 
1457   unequal:
1458     if (eq < 0)
1459         return NULL;
1460     if (op == Py_NE)
1461         res = Py_True;
1462     else
1463         res = Py_False;
1464 
1465   done:
1466     Py_INCREF(res);
1467     return res;
1468 }
1469 
1470 static Py_hash_t
code_hash(PyCodeObject * co)1471 code_hash(PyCodeObject *co)
1472 {
1473     Py_hash_t h, h0, h1, h2, h3, h4;
1474     h0 = PyObject_Hash(co->co_name);
1475     if (h0 == -1) return -1;
1476     h1 = PyObject_Hash(co->co_code);
1477     if (h1 == -1) return -1;
1478     h2 = PyObject_Hash(co->co_consts);
1479     if (h2 == -1) return -1;
1480     h3 = PyObject_Hash(co->co_names);
1481     if (h3 == -1) return -1;
1482     h4 = PyObject_Hash(co->co_localsplusnames);
1483     if (h4 == -1) return -1;
1484     h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^
1485         co->co_argcount ^ co->co_posonlyargcount ^ co->co_kwonlyargcount ^
1486         co->co_flags;
1487     if (h == -1) h = -2;
1488     return h;
1489 }
1490 
1491 
1492 #define OFF(x) offsetof(PyCodeObject, x)
1493 
1494 static PyMemberDef code_memberlist[] = {
1495     {"co_argcount",     T_INT,          OFF(co_argcount),        READONLY},
1496     {"co_posonlyargcount",      T_INT,  OFF(co_posonlyargcount), READONLY},
1497     {"co_kwonlyargcount",       T_INT,  OFF(co_kwonlyargcount),  READONLY},
1498     {"co_stacksize",T_INT,              OFF(co_stacksize),       READONLY},
1499     {"co_flags",        T_INT,          OFF(co_flags),           READONLY},
1500     {"co_code",         T_OBJECT,       OFF(co_code),            READONLY},
1501     {"co_consts",       T_OBJECT,       OFF(co_consts),          READONLY},
1502     {"co_names",        T_OBJECT,       OFF(co_names),           READONLY},
1503     {"co_filename",     T_OBJECT,       OFF(co_filename),        READONLY},
1504     {"co_name",         T_OBJECT,       OFF(co_name),            READONLY},
1505     {"co_qualname",     T_OBJECT,       OFF(co_qualname),        READONLY},
1506     {"co_firstlineno",  T_INT,          OFF(co_firstlineno),     READONLY},
1507     {"co_linetable",    T_OBJECT,       OFF(co_linetable),       READONLY},
1508     {"co_endlinetable", T_OBJECT,       OFF(co_endlinetable),    READONLY},
1509     {"co_columntable",  T_OBJECT,       OFF(co_columntable),     READONLY},
1510     {"co_exceptiontable",    T_OBJECT,  OFF(co_exceptiontable),  READONLY},
1511     {NULL}      /* Sentinel */
1512 };
1513 
1514 
1515 static PyObject *
code_getlnotab(PyCodeObject * code,void * closure)1516 code_getlnotab(PyCodeObject *code, void *closure)
1517 {
1518     return decode_linetable(code);
1519 }
1520 
1521 static PyObject *
code_getnlocals(PyCodeObject * code,void * closure)1522 code_getnlocals(PyCodeObject *code, void *closure)
1523 {
1524     return PyLong_FromLong(code->co_nlocals);
1525 }
1526 
1527 static PyObject *
code_getvarnames(PyCodeObject * code,void * closure)1528 code_getvarnames(PyCodeObject *code, void *closure)
1529 {
1530     return _PyCode_GetVarnames(code);
1531 }
1532 
1533 static PyObject *
code_getcellvars(PyCodeObject * code,void * closure)1534 code_getcellvars(PyCodeObject *code, void *closure)
1535 {
1536     return _PyCode_GetCellvars(code);
1537 }
1538 
1539 static PyObject *
code_getfreevars(PyCodeObject * code,void * closure)1540 code_getfreevars(PyCodeObject *code, void *closure)
1541 {
1542     return _PyCode_GetFreevars(code);
1543 }
1544 
1545 static PyGetSetDef code_getsetlist[] = {
1546     {"co_lnotab",    (getter)code_getlnotab, NULL, NULL},
1547     // The following old names are kept for backward compatibility.
1548     {"co_nlocals",   (getter)code_getnlocals, NULL, NULL},
1549     {"co_varnames",  (getter)code_getvarnames, NULL, NULL},
1550     {"co_cellvars",  (getter)code_getcellvars, NULL, NULL},
1551     {"co_freevars",  (getter)code_getfreevars, NULL, NULL},
1552     {0}
1553 };
1554 
1555 
1556 static PyObject *
code_sizeof(PyCodeObject * co,PyObject * Py_UNUSED (args))1557 code_sizeof(PyCodeObject *co, PyObject *Py_UNUSED(args))
1558 {
1559     Py_ssize_t res = _PyObject_SIZE(Py_TYPE(co));
1560 
1561     _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) co->co_extra;
1562     if (co_extra != NULL) {
1563         res += sizeof(_PyCodeObjectExtra) +
1564                (co_extra->ce_size-1) * sizeof(co_extra->ce_extras[0]);
1565     }
1566 
1567     if (co->co_quickened != NULL) {
1568         Py_ssize_t count = co->co_quickened[0].entry.zero.cache_count;
1569         count += (PyBytes_GET_SIZE(co->co_code)+sizeof(SpecializedCacheEntry)-1)/
1570             sizeof(SpecializedCacheEntry);
1571         res += count * sizeof(SpecializedCacheEntry);
1572     }
1573 
1574     return PyLong_FromSsize_t(res);
1575 }
1576 
1577 static PyObject *
code_linesiterator(PyCodeObject * code,PyObject * Py_UNUSED (args))1578 code_linesiterator(PyCodeObject *code, PyObject *Py_UNUSED(args))
1579 {
1580     return (PyObject *)new_linesiterator(code);
1581 }
1582 
1583 /*[clinic input]
1584 code.replace
1585 
1586     *
1587     co_argcount: int(c_default="self->co_argcount") = -1
1588     co_posonlyargcount: int(c_default="self->co_posonlyargcount") = -1
1589     co_kwonlyargcount: int(c_default="self->co_kwonlyargcount") = -1
1590     co_nlocals: int(c_default="self->co_nlocals") = -1
1591     co_stacksize: int(c_default="self->co_stacksize") = -1
1592     co_flags: int(c_default="self->co_flags") = -1
1593     co_firstlineno: int(c_default="self->co_firstlineno") = -1
1594     co_code: PyBytesObject(c_default="(PyBytesObject *)self->co_code") = None
1595     co_consts: object(subclass_of="&PyTuple_Type", c_default="self->co_consts") = None
1596     co_names: object(subclass_of="&PyTuple_Type", c_default="self->co_names") = None
1597     co_varnames: object(subclass_of="&PyTuple_Type", c_default="self->co_varnames") = None
1598     co_freevars: object(subclass_of="&PyTuple_Type", c_default="self->co_freevars") = None
1599     co_cellvars: object(subclass_of="&PyTuple_Type", c_default="self->co_cellvars") = None
1600     co_filename: unicode(c_default="self->co_filename") = None
1601     co_name: unicode(c_default="self->co_name") = None
1602     co_qualname: unicode(c_default="self->co_qualname") = None
1603     co_linetable: PyBytesObject(c_default="(PyBytesObject *)self->co_linetable") = None
1604     co_endlinetable: object(c_default="self->co_endlinetable") = None
1605     co_columntable: object(c_default="self->co_columntable") = None
1606     co_exceptiontable: PyBytesObject(c_default="(PyBytesObject *)self->co_exceptiontable") = None
1607 
1608 Return a copy of the code object with new values for the specified fields.
1609 [clinic start generated code]*/
1610 
1611 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,PyObject * co_qualname,PyBytesObject * co_linetable,PyObject * co_endlinetable,PyObject * co_columntable,PyBytesObject * co_exceptiontable)1612 code_replace_impl(PyCodeObject *self, int co_argcount,
1613                   int co_posonlyargcount, int co_kwonlyargcount,
1614                   int co_nlocals, int co_stacksize, int co_flags,
1615                   int co_firstlineno, PyBytesObject *co_code,
1616                   PyObject *co_consts, PyObject *co_names,
1617                   PyObject *co_varnames, PyObject *co_freevars,
1618                   PyObject *co_cellvars, PyObject *co_filename,
1619                   PyObject *co_name, PyObject *co_qualname,
1620                   PyBytesObject *co_linetable, PyObject *co_endlinetable,
1621                   PyObject *co_columntable, PyBytesObject *co_exceptiontable)
1622 /*[clinic end generated code: output=f046bf0be3bab91f input=a63d09f248f00794]*/
1623 {
1624 #define CHECK_INT_ARG(ARG) \
1625         if (ARG < 0) { \
1626             PyErr_SetString(PyExc_ValueError, \
1627                             #ARG " must be a positive integer"); \
1628             return NULL; \
1629         }
1630 
1631     CHECK_INT_ARG(co_argcount);
1632     CHECK_INT_ARG(co_posonlyargcount);
1633     CHECK_INT_ARG(co_kwonlyargcount);
1634     CHECK_INT_ARG(co_nlocals);
1635     CHECK_INT_ARG(co_stacksize);
1636     CHECK_INT_ARG(co_flags);
1637     CHECK_INT_ARG(co_firstlineno);
1638 
1639 #undef CHECK_INT_ARG
1640 
1641     if (PySys_Audit("code.__new__", "OOOiiiiii",
1642                     co_code, co_filename, co_name, co_argcount,
1643                     co_posonlyargcount, co_kwonlyargcount, co_nlocals,
1644                     co_stacksize, co_flags) < 0) {
1645         return NULL;
1646     }
1647 
1648     PyCodeObject *co = NULL;
1649     PyObject *varnames = NULL;
1650     PyObject *cellvars = NULL;
1651     PyObject *freevars = NULL;
1652     if (co_varnames == NULL) {
1653         varnames = get_localsplus_names(self, CO_FAST_LOCAL, self->co_nlocals);
1654         if (varnames == NULL) {
1655             goto error;
1656         }
1657         co_varnames = varnames;
1658     }
1659     if (co_cellvars == NULL) {
1660         cellvars = get_localsplus_names(self, CO_FAST_CELL, self->co_ncellvars);
1661         if (cellvars == NULL) {
1662             goto error;
1663         }
1664         co_cellvars = cellvars;
1665     }
1666     if (co_freevars == NULL) {
1667         freevars = get_localsplus_names(self, CO_FAST_FREE, self->co_nfreevars);
1668         if (freevars == NULL) {
1669             goto error;
1670         }
1671         co_freevars = freevars;
1672     }
1673 
1674     if (!Py_IsNone(co_endlinetable) && !PyBytes_Check(co_endlinetable)) {
1675         PyErr_SetString(PyExc_ValueError,
1676                         "co_endlinetable must be None or bytes");
1677         goto error;
1678     }
1679     if (!Py_IsNone(co_columntable) && !PyBytes_Check(co_columntable)) {
1680         PyErr_SetString(PyExc_ValueError,
1681                         "co_columntable must be None or bytes");
1682         goto error;
1683     }
1684 
1685     co = PyCode_NewWithPosOnlyArgs(
1686         co_argcount, co_posonlyargcount, co_kwonlyargcount, co_nlocals,
1687         co_stacksize, co_flags, (PyObject*)co_code, co_consts, co_names,
1688         co_varnames, co_freevars, co_cellvars, co_filename, co_name,
1689         co_qualname, co_firstlineno, (PyObject*)co_linetable,
1690         (PyObject*)co_endlinetable, (PyObject*)co_columntable,
1691         (PyObject*)co_exceptiontable);
1692 
1693 error:
1694     Py_XDECREF(varnames);
1695     Py_XDECREF(cellvars);
1696     Py_XDECREF(freevars);
1697     return (PyObject *)co;
1698 }
1699 
1700 /*[clinic input]
1701 code._varname_from_oparg
1702 
1703     oparg: int
1704 
1705 (internal-only) Return the local variable name for the given oparg.
1706 
1707 WARNING: this method is for internal use only and may change or go away.
1708 [clinic start generated code]*/
1709 
1710 static PyObject *
code__varname_from_oparg_impl(PyCodeObject * self,int oparg)1711 code__varname_from_oparg_impl(PyCodeObject *self, int oparg)
1712 /*[clinic end generated code: output=1fd1130413184206 input=c5fa3ee9bac7d4ca]*/
1713 {
1714     PyObject *name = PyTuple_GetItem(self->co_localsplusnames, oparg);
1715     if (name == NULL) {
1716         return NULL;
1717     }
1718     Py_INCREF(name);
1719     return name;
1720 }
1721 
1722 /* XXX code objects need to participate in GC? */
1723 
1724 static struct PyMethodDef code_methods[] = {
1725     {"__sizeof__", (PyCFunction)code_sizeof, METH_NOARGS},
1726     {"co_lines", (PyCFunction)code_linesiterator, METH_NOARGS},
1727     {"co_positions", (PyCFunction)code_positionsiterator, METH_NOARGS},
1728     CODE_REPLACE_METHODDEF
1729     CODE__VARNAME_FROM_OPARG_METHODDEF
1730     {NULL, NULL}                /* sentinel */
1731 };
1732 
1733 
1734 PyTypeObject PyCode_Type = {
1735     PyVarObject_HEAD_INIT(&PyType_Type, 0)
1736     "code",
1737     sizeof(PyCodeObject),
1738     0,
1739     (destructor)code_dealloc,           /* tp_dealloc */
1740     0,                                  /* tp_vectorcall_offset */
1741     0,                                  /* tp_getattr */
1742     0,                                  /* tp_setattr */
1743     0,                                  /* tp_as_async */
1744     (reprfunc)code_repr,                /* tp_repr */
1745     0,                                  /* tp_as_number */
1746     0,                                  /* tp_as_sequence */
1747     0,                                  /* tp_as_mapping */
1748     (hashfunc)code_hash,                /* tp_hash */
1749     0,                                  /* tp_call */
1750     0,                                  /* tp_str */
1751     PyObject_GenericGetAttr,            /* tp_getattro */
1752     0,                                  /* tp_setattro */
1753     0,                                  /* tp_as_buffer */
1754     Py_TPFLAGS_DEFAULT,                 /* tp_flags */
1755     code_new__doc__,                    /* tp_doc */
1756     0,                                  /* tp_traverse */
1757     0,                                  /* tp_clear */
1758     code_richcompare,                   /* tp_richcompare */
1759     offsetof(PyCodeObject, co_weakreflist),     /* tp_weaklistoffset */
1760     0,                                  /* tp_iter */
1761     0,                                  /* tp_iternext */
1762     code_methods,                       /* tp_methods */
1763     code_memberlist,                    /* tp_members */
1764     code_getsetlist,                    /* tp_getset */
1765     0,                                  /* tp_base */
1766     0,                                  /* tp_dict */
1767     0,                                  /* tp_descr_get */
1768     0,                                  /* tp_descr_set */
1769     0,                                  /* tp_dictoffset */
1770     0,                                  /* tp_init */
1771     0,                                  /* tp_alloc */
1772     code_new,                           /* tp_new */
1773 };
1774 
1775 
1776 /******************
1777  * other API
1778  ******************/
1779 
1780 PyObject*
_PyCode_ConstantKey(PyObject * op)1781 _PyCode_ConstantKey(PyObject *op)
1782 {
1783     PyObject *key;
1784 
1785     /* Py_None and Py_Ellipsis are singletons. */
1786     if (op == Py_None || op == Py_Ellipsis
1787        || PyLong_CheckExact(op)
1788        || PyUnicode_CheckExact(op)
1789           /* code_richcompare() uses _PyCode_ConstantKey() internally */
1790        || PyCode_Check(op))
1791     {
1792         /* Objects of these types are always different from object of other
1793          * type and from tuples. */
1794         Py_INCREF(op);
1795         key = op;
1796     }
1797     else if (PyBool_Check(op) || PyBytes_CheckExact(op)) {
1798         /* Make booleans different from integers 0 and 1.
1799          * Avoid BytesWarning from comparing bytes with strings. */
1800         key = PyTuple_Pack(2, Py_TYPE(op), op);
1801     }
1802     else if (PyFloat_CheckExact(op)) {
1803         double d = PyFloat_AS_DOUBLE(op);
1804         /* all we need is to make the tuple different in either the 0.0
1805          * or -0.0 case from all others, just to avoid the "coercion".
1806          */
1807         if (d == 0.0 && copysign(1.0, d) < 0.0)
1808             key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
1809         else
1810             key = PyTuple_Pack(2, Py_TYPE(op), op);
1811     }
1812     else if (PyComplex_CheckExact(op)) {
1813         Py_complex z;
1814         int real_negzero, imag_negzero;
1815         /* For the complex case we must make complex(x, 0.)
1816            different from complex(x, -0.) and complex(0., y)
1817            different from complex(-0., y), for any x and y.
1818            All four complex zeros must be distinguished.*/
1819         z = PyComplex_AsCComplex(op);
1820         real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
1821         imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
1822         /* use True, False and None singleton as tags for the real and imag
1823          * sign, to make tuples different */
1824         if (real_negzero && imag_negzero) {
1825             key = PyTuple_Pack(3, Py_TYPE(op), op, Py_True);
1826         }
1827         else if (imag_negzero) {
1828             key = PyTuple_Pack(3, Py_TYPE(op), op, Py_False);
1829         }
1830         else if (real_negzero) {
1831             key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
1832         }
1833         else {
1834             key = PyTuple_Pack(2, Py_TYPE(op), op);
1835         }
1836     }
1837     else if (PyTuple_CheckExact(op)) {
1838         Py_ssize_t i, len;
1839         PyObject *tuple;
1840 
1841         len = PyTuple_GET_SIZE(op);
1842         tuple = PyTuple_New(len);
1843         if (tuple == NULL)
1844             return NULL;
1845 
1846         for (i=0; i < len; i++) {
1847             PyObject *item, *item_key;
1848 
1849             item = PyTuple_GET_ITEM(op, i);
1850             item_key = _PyCode_ConstantKey(item);
1851             if (item_key == NULL) {
1852                 Py_DECREF(tuple);
1853                 return NULL;
1854             }
1855 
1856             PyTuple_SET_ITEM(tuple, i, item_key);
1857         }
1858 
1859         key = PyTuple_Pack(2, tuple, op);
1860         Py_DECREF(tuple);
1861     }
1862     else if (PyFrozenSet_CheckExact(op)) {
1863         Py_ssize_t pos = 0;
1864         PyObject *item;
1865         Py_hash_t hash;
1866         Py_ssize_t i, len;
1867         PyObject *tuple, *set;
1868 
1869         len = PySet_GET_SIZE(op);
1870         tuple = PyTuple_New(len);
1871         if (tuple == NULL)
1872             return NULL;
1873 
1874         i = 0;
1875         while (_PySet_NextEntry(op, &pos, &item, &hash)) {
1876             PyObject *item_key;
1877 
1878             item_key = _PyCode_ConstantKey(item);
1879             if (item_key == NULL) {
1880                 Py_DECREF(tuple);
1881                 return NULL;
1882             }
1883 
1884             assert(i < len);
1885             PyTuple_SET_ITEM(tuple, i, item_key);
1886             i++;
1887         }
1888         set = PyFrozenSet_New(tuple);
1889         Py_DECREF(tuple);
1890         if (set == NULL)
1891             return NULL;
1892 
1893         key = PyTuple_Pack(2, set, op);
1894         Py_DECREF(set);
1895         return key;
1896     }
1897     else {
1898         /* for other types, use the object identifier as a unique identifier
1899          * to ensure that they are seen as unequal. */
1900         PyObject *obj_id = PyLong_FromVoidPtr(op);
1901         if (obj_id == NULL)
1902             return NULL;
1903 
1904         key = PyTuple_Pack(2, obj_id, op);
1905         Py_DECREF(obj_id);
1906     }
1907     return key;
1908 }
1909