ยปCore Development>Code coverage>Objects/codeobject.c

Python code coverage for Objects/codeobject.c

#countcontent
1n/a#include <stdbool.h>
2n/a
3n/a#include "Python.h"
4n/a#include "code.h"
5n/a#include "structmember.h"
6n/a
7n/a#define NAME_CHARS \
8n/a "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
9n/a
10n/a/* Holder for co_extra information */
11n/atypedef struct {
12n/a Py_ssize_t ce_size;
13n/a void **ce_extras;
14n/a} _PyCodeObjectExtra;
15n/a
16n/a/* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */
17n/a
18n/astatic int
19n/aall_name_chars(PyObject *o)
20n/a{
21n/a static char ok_name_char[256];
22n/a static const unsigned char *name_chars = (unsigned char *)NAME_CHARS;
23n/a const unsigned char *s, *e;
24n/a
25n/a if (!PyUnicode_Check(o) || PyUnicode_READY(o) == -1 ||
26n/a !PyUnicode_IS_ASCII(o))
27n/a return 0;
28n/a
29n/a if (ok_name_char[*name_chars] == 0) {
30n/a const unsigned char *p;
31n/a for (p = name_chars; *p; p++)
32n/a ok_name_char[*p] = 1;
33n/a }
34n/a s = PyUnicode_1BYTE_DATA(o);
35n/a e = s + PyUnicode_GET_LENGTH(o);
36n/a while (s != e) {
37n/a if (ok_name_char[*s++] == 0)
38n/a return 0;
39n/a }
40n/a return 1;
41n/a}
42n/a
43n/astatic void
44n/aintern_strings(PyObject *tuple)
45n/a{
46n/a Py_ssize_t i;
47n/a
48n/a for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
49n/a PyObject *v = PyTuple_GET_ITEM(tuple, i);
50n/a if (v == NULL || !PyUnicode_CheckExact(v)) {
51n/a Py_FatalError("non-string found in code slot");
52n/a }
53n/a PyUnicode_InternInPlace(&PyTuple_GET_ITEM(tuple, i));
54n/a }
55n/a}
56n/a
57n/a/* Intern selected string constants */
58n/astatic int
59n/aintern_string_constants(PyObject *tuple)
60n/a{
61n/a int modified = 0;
62n/a Py_ssize_t i;
63n/a
64n/a for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
65n/a PyObject *v = PyTuple_GET_ITEM(tuple, i);
66n/a if (PyUnicode_CheckExact(v)) {
67n/a if (all_name_chars(v)) {
68n/a PyObject *w = v;
69n/a PyUnicode_InternInPlace(&v);
70n/a if (w != v) {
71n/a PyTuple_SET_ITEM(tuple, i, v);
72n/a modified = 1;
73n/a }
74n/a }
75n/a }
76n/a else if (PyTuple_CheckExact(v)) {
77n/a intern_string_constants(v);
78n/a }
79n/a else if (PyFrozenSet_CheckExact(v)) {
80n/a PyObject *w = v;
81n/a PyObject *tmp = PySequence_Tuple(v);
82n/a if (tmp == NULL) {
83n/a PyErr_Clear();
84n/a continue;
85n/a }
86n/a if (intern_string_constants(tmp)) {
87n/a v = PyFrozenSet_New(tmp);
88n/a if (v == NULL) {
89n/a PyErr_Clear();
90n/a }
91n/a else {
92n/a PyTuple_SET_ITEM(tuple, i, v);
93n/a Py_DECREF(w);
94n/a modified = 1;
95n/a }
96n/a }
97n/a Py_DECREF(tmp);
98n/a }
99n/a }
100n/a return modified;
101n/a}
102n/a
103n/a
104n/aPyCodeObject *
105n/aPyCode_New(int argcount, int kwonlyargcount,
106n/a int nlocals, int stacksize, int flags,
107n/a PyObject *code, PyObject *consts, PyObject *names,
108n/a PyObject *varnames, PyObject *freevars, PyObject *cellvars,
109n/a PyObject *filename, PyObject *name, int firstlineno,
110n/a PyObject *lnotab)
111n/a{
112n/a PyCodeObject *co;
113n/a Py_ssize_t *cell2arg = NULL;
114n/a Py_ssize_t i, n_cellvars;
115n/a
116n/a /* Check argument types */
117n/a if (argcount < 0 || kwonlyargcount < 0 || nlocals < 0 ||
118n/a code == NULL || !PyBytes_Check(code) ||
119n/a consts == NULL || !PyTuple_Check(consts) ||
120n/a names == NULL || !PyTuple_Check(names) ||
121n/a varnames == NULL || !PyTuple_Check(varnames) ||
122n/a freevars == NULL || !PyTuple_Check(freevars) ||
123n/a cellvars == NULL || !PyTuple_Check(cellvars) ||
124n/a name == NULL || !PyUnicode_Check(name) ||
125n/a filename == NULL || !PyUnicode_Check(filename) ||
126n/a lnotab == NULL || !PyBytes_Check(lnotab)) {
127n/a PyErr_BadInternalCall();
128n/a return NULL;
129n/a }
130n/a
131n/a /* Ensure that the filename is a ready Unicode string */
132n/a if (PyUnicode_READY(filename) < 0)
133n/a return NULL;
134n/a
135n/a n_cellvars = PyTuple_GET_SIZE(cellvars);
136n/a intern_strings(names);
137n/a intern_strings(varnames);
138n/a intern_strings(freevars);
139n/a intern_strings(cellvars);
140n/a intern_string_constants(consts);
141n/a /* Create mapping between cells and arguments if needed. */
142n/a if (n_cellvars) {
143n/a Py_ssize_t total_args = argcount + kwonlyargcount +
144n/a ((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0);
145n/a bool used_cell2arg = false;
146n/a cell2arg = PyMem_NEW(Py_ssize_t, n_cellvars);
147n/a if (cell2arg == NULL) {
148n/a PyErr_NoMemory();
149n/a return NULL;
150n/a }
151n/a /* Find cells which are also arguments. */
152n/a for (i = 0; i < n_cellvars; i++) {
153n/a Py_ssize_t j;
154n/a PyObject *cell = PyTuple_GET_ITEM(cellvars, i);
155n/a cell2arg[i] = CO_CELL_NOT_AN_ARG;
156n/a for (j = 0; j < total_args; j++) {
157n/a PyObject *arg = PyTuple_GET_ITEM(varnames, j);
158n/a int cmp = PyUnicode_Compare(cell, arg);
159n/a if (cmp == -1 && PyErr_Occurred()) {
160n/a PyMem_FREE(cell2arg);
161n/a return NULL;
162n/a }
163n/a if (cmp == 0) {
164n/a cell2arg[i] = j;
165n/a used_cell2arg = true;
166n/a break;
167n/a }
168n/a }
169n/a }
170n/a if (!used_cell2arg) {
171n/a PyMem_FREE(cell2arg);
172n/a cell2arg = NULL;
173n/a }
174n/a }
175n/a co = PyObject_NEW(PyCodeObject, &PyCode_Type);
176n/a if (co == NULL) {
177n/a if (cell2arg)
178n/a PyMem_FREE(cell2arg);
179n/a return NULL;
180n/a }
181n/a co->co_argcount = argcount;
182n/a co->co_kwonlyargcount = kwonlyargcount;
183n/a co->co_nlocals = nlocals;
184n/a co->co_stacksize = stacksize;
185n/a co->co_flags = flags;
186n/a Py_INCREF(code);
187n/a co->co_code = code;
188n/a Py_INCREF(consts);
189n/a co->co_consts = consts;
190n/a Py_INCREF(names);
191n/a co->co_names = names;
192n/a Py_INCREF(varnames);
193n/a co->co_varnames = varnames;
194n/a Py_INCREF(freevars);
195n/a co->co_freevars = freevars;
196n/a Py_INCREF(cellvars);
197n/a co->co_cellvars = cellvars;
198n/a co->co_cell2arg = cell2arg;
199n/a Py_INCREF(filename);
200n/a co->co_filename = filename;
201n/a Py_INCREF(name);
202n/a co->co_name = name;
203n/a co->co_firstlineno = firstlineno;
204n/a Py_INCREF(lnotab);
205n/a co->co_lnotab = lnotab;
206n/a co->co_zombieframe = NULL;
207n/a co->co_weakreflist = NULL;
208n/a co->co_extra = NULL;
209n/a return co;
210n/a}
211n/a
212n/aPyCodeObject *
213n/aPyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
214n/a{
215n/a static PyObject *emptystring = NULL;
216n/a static PyObject *nulltuple = NULL;
217n/a PyObject *filename_ob = NULL;
218n/a PyObject *funcname_ob = NULL;
219n/a PyCodeObject *result = NULL;
220n/a if (emptystring == NULL) {
221n/a emptystring = PyBytes_FromString("");
222n/a if (emptystring == NULL)
223n/a goto failed;
224n/a }
225n/a if (nulltuple == NULL) {
226n/a nulltuple = PyTuple_New(0);
227n/a if (nulltuple == NULL)
228n/a goto failed;
229n/a }
230n/a funcname_ob = PyUnicode_FromString(funcname);
231n/a if (funcname_ob == NULL)
232n/a goto failed;
233n/a filename_ob = PyUnicode_DecodeFSDefault(filename);
234n/a if (filename_ob == NULL)
235n/a goto failed;
236n/a
237n/a result = PyCode_New(0, /* argcount */
238n/a 0, /* kwonlyargcount */
239n/a 0, /* nlocals */
240n/a 0, /* stacksize */
241n/a 0, /* flags */
242n/a emptystring, /* code */
243n/a nulltuple, /* consts */
244n/a nulltuple, /* names */
245n/a nulltuple, /* varnames */
246n/a nulltuple, /* freevars */
247n/a nulltuple, /* cellvars */
248n/a filename_ob, /* filename */
249n/a funcname_ob, /* name */
250n/a firstlineno, /* firstlineno */
251n/a emptystring /* lnotab */
252n/a );
253n/a
254n/afailed:
255n/a Py_XDECREF(funcname_ob);
256n/a Py_XDECREF(filename_ob);
257n/a return result;
258n/a}
259n/a
260n/a#define OFF(x) offsetof(PyCodeObject, x)
261n/a
262n/astatic PyMemberDef code_memberlist[] = {
263n/a {"co_argcount", T_INT, OFF(co_argcount), READONLY},
264n/a {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY},
265n/a {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
266n/a {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
267n/a {"co_flags", T_INT, OFF(co_flags), READONLY},
268n/a {"co_code", T_OBJECT, OFF(co_code), READONLY},
269n/a {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
270n/a {"co_names", T_OBJECT, OFF(co_names), READONLY},
271n/a {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
272n/a {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
273n/a {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
274n/a {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
275n/a {"co_name", T_OBJECT, OFF(co_name), READONLY},
276n/a {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
277n/a {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
278n/a {NULL} /* Sentinel */
279n/a};
280n/a
281n/a/* Helper for code_new: return a shallow copy of a tuple that is
282n/a guaranteed to contain exact strings, by converting string subclasses
283n/a to exact strings and complaining if a non-string is found. */
284n/astatic PyObject*
285n/avalidate_and_copy_tuple(PyObject *tup)
286n/a{
287n/a PyObject *newtuple;
288n/a PyObject *item;
289n/a Py_ssize_t i, len;
290n/a
291n/a len = PyTuple_GET_SIZE(tup);
292n/a newtuple = PyTuple_New(len);
293n/a if (newtuple == NULL)
294n/a return NULL;
295n/a
296n/a for (i = 0; i < len; i++) {
297n/a item = PyTuple_GET_ITEM(tup, i);
298n/a if (PyUnicode_CheckExact(item)) {
299n/a Py_INCREF(item);
300n/a }
301n/a else if (!PyUnicode_Check(item)) {
302n/a PyErr_Format(
303n/a PyExc_TypeError,
304n/a "name tuples must contain only "
305n/a "strings, not '%.500s'",
306n/a item->ob_type->tp_name);
307n/a Py_DECREF(newtuple);
308n/a return NULL;
309n/a }
310n/a else {
311n/a item = _PyUnicode_Copy(item);
312n/a if (item == NULL) {
313n/a Py_DECREF(newtuple);
314n/a return NULL;
315n/a }
316n/a }
317n/a PyTuple_SET_ITEM(newtuple, i, item);
318n/a }
319n/a
320n/a return newtuple;
321n/a}
322n/a
323n/aPyDoc_STRVAR(code_doc,
324n/a"code(argcount, kwonlyargcount, nlocals, stacksize, flags, codestring,\n\
325n/a constants, names, varnames, filename, name, firstlineno,\n\
326n/a lnotab[, freevars[, cellvars]])\n\
327n/a\n\
328n/aCreate a code object. Not for the faint of heart.");
329n/a
330n/astatic PyObject *
331n/acode_new(PyTypeObject *type, PyObject *args, PyObject *kw)
332n/a{
333n/a int argcount;
334n/a int kwonlyargcount;
335n/a int nlocals;
336n/a int stacksize;
337n/a int flags;
338n/a PyObject *co = NULL;
339n/a PyObject *code;
340n/a PyObject *consts;
341n/a PyObject *names, *ournames = NULL;
342n/a PyObject *varnames, *ourvarnames = NULL;
343n/a PyObject *freevars = NULL, *ourfreevars = NULL;
344n/a PyObject *cellvars = NULL, *ourcellvars = NULL;
345n/a PyObject *filename;
346n/a PyObject *name;
347n/a int firstlineno;
348n/a PyObject *lnotab;
349n/a
350n/a if (!PyArg_ParseTuple(args, "iiiiiSO!O!O!UUiS|O!O!:code",
351n/a &argcount, &kwonlyargcount,
352n/a &nlocals, &stacksize, &flags,
353n/a &code,
354n/a &PyTuple_Type, &consts,
355n/a &PyTuple_Type, &names,
356n/a &PyTuple_Type, &varnames,
357n/a &filename, &name,
358n/a &firstlineno, &lnotab,
359n/a &PyTuple_Type, &freevars,
360n/a &PyTuple_Type, &cellvars))
361n/a return NULL;
362n/a
363n/a if (argcount < 0) {
364n/a PyErr_SetString(
365n/a PyExc_ValueError,
366n/a "code: argcount must not be negative");
367n/a goto cleanup;
368n/a }
369n/a
370n/a if (kwonlyargcount < 0) {
371n/a PyErr_SetString(
372n/a PyExc_ValueError,
373n/a "code: kwonlyargcount must not be negative");
374n/a goto cleanup;
375n/a }
376n/a if (nlocals < 0) {
377n/a PyErr_SetString(
378n/a PyExc_ValueError,
379n/a "code: nlocals must not be negative");
380n/a goto cleanup;
381n/a }
382n/a
383n/a ournames = validate_and_copy_tuple(names);
384n/a if (ournames == NULL)
385n/a goto cleanup;
386n/a ourvarnames = validate_and_copy_tuple(varnames);
387n/a if (ourvarnames == NULL)
388n/a goto cleanup;
389n/a if (freevars)
390n/a ourfreevars = validate_and_copy_tuple(freevars);
391n/a else
392n/a ourfreevars = PyTuple_New(0);
393n/a if (ourfreevars == NULL)
394n/a goto cleanup;
395n/a if (cellvars)
396n/a ourcellvars = validate_and_copy_tuple(cellvars);
397n/a else
398n/a ourcellvars = PyTuple_New(0);
399n/a if (ourcellvars == NULL)
400n/a goto cleanup;
401n/a
402n/a co = (PyObject *)PyCode_New(argcount, kwonlyargcount,
403n/a nlocals, stacksize, flags,
404n/a code, consts, ournames, ourvarnames,
405n/a ourfreevars, ourcellvars, filename,
406n/a name, firstlineno, lnotab);
407n/a cleanup:
408n/a Py_XDECREF(ournames);
409n/a Py_XDECREF(ourvarnames);
410n/a Py_XDECREF(ourfreevars);
411n/a Py_XDECREF(ourcellvars);
412n/a return co;
413n/a}
414n/a
415n/astatic void
416n/acode_dealloc(PyCodeObject *co)
417n/a{
418n/a if (co->co_extra != NULL) {
419n/a PyThreadState *tstate = PyThreadState_Get();
420n/a _PyCodeObjectExtra *co_extra = co->co_extra;
421n/a
422n/a for (Py_ssize_t i = 0; i < co_extra->ce_size; i++) {
423n/a freefunc free_extra = tstate->co_extra_freefuncs[i];
424n/a
425n/a if (free_extra != NULL) {
426n/a free_extra(co_extra->ce_extras[i]);
427n/a }
428n/a }
429n/a
430n/a PyMem_FREE(co->co_extra);
431n/a }
432n/a
433n/a Py_XDECREF(co->co_code);
434n/a Py_XDECREF(co->co_consts);
435n/a Py_XDECREF(co->co_names);
436n/a Py_XDECREF(co->co_varnames);
437n/a Py_XDECREF(co->co_freevars);
438n/a Py_XDECREF(co->co_cellvars);
439n/a Py_XDECREF(co->co_filename);
440n/a Py_XDECREF(co->co_name);
441n/a Py_XDECREF(co->co_lnotab);
442n/a if (co->co_cell2arg != NULL)
443n/a PyMem_FREE(co->co_cell2arg);
444n/a if (co->co_zombieframe != NULL)
445n/a PyObject_GC_Del(co->co_zombieframe);
446n/a if (co->co_weakreflist != NULL)
447n/a PyObject_ClearWeakRefs((PyObject*)co);
448n/a PyObject_DEL(co);
449n/a}
450n/a
451n/astatic PyObject *
452n/acode_sizeof(PyCodeObject *co, void *unused)
453n/a{
454n/a Py_ssize_t res;
455n/a
456n/a res = _PyObject_SIZE(Py_TYPE(co));
457n/a if (co->co_cell2arg != NULL && co->co_cellvars != NULL)
458n/a res += PyTuple_GET_SIZE(co->co_cellvars) * sizeof(Py_ssize_t);
459n/a return PyLong_FromSsize_t(res);
460n/a}
461n/a
462n/astatic PyObject *
463n/acode_repr(PyCodeObject *co)
464n/a{
465n/a int lineno;
466n/a if (co->co_firstlineno != 0)
467n/a lineno = co->co_firstlineno;
468n/a else
469n/a lineno = -1;
470n/a if (co->co_filename && PyUnicode_Check(co->co_filename)) {
471n/a return PyUnicode_FromFormat(
472n/a "<code object %U at %p, file \"%U\", line %d>",
473n/a co->co_name, co, co->co_filename, lineno);
474n/a } else {
475n/a return PyUnicode_FromFormat(
476n/a "<code object %U at %p, file ???, line %d>",
477n/a co->co_name, co, lineno);
478n/a }
479n/a}
480n/a
481n/aPyObject*
482n/a_PyCode_ConstantKey(PyObject *op)
483n/a{
484n/a PyObject *key;
485n/a
486n/a /* Py_None and Py_Ellipsis are singleton */
487n/a if (op == Py_None || op == Py_Ellipsis
488n/a || PyLong_CheckExact(op)
489n/a || PyBool_Check(op)
490n/a || PyBytes_CheckExact(op)
491n/a || PyUnicode_CheckExact(op)
492n/a /* code_richcompare() uses _PyCode_ConstantKey() internally */
493n/a || PyCode_Check(op)) {
494n/a key = PyTuple_Pack(2, Py_TYPE(op), op);
495n/a }
496n/a else if (PyFloat_CheckExact(op)) {
497n/a double d = PyFloat_AS_DOUBLE(op);
498n/a /* all we need is to make the tuple different in either the 0.0
499n/a * or -0.0 case from all others, just to avoid the "coercion".
500n/a */
501n/a if (d == 0.0 && copysign(1.0, d) < 0.0)
502n/a key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
503n/a else
504n/a key = PyTuple_Pack(2, Py_TYPE(op), op);
505n/a }
506n/a else if (PyComplex_CheckExact(op)) {
507n/a Py_complex z;
508n/a int real_negzero, imag_negzero;
509n/a /* For the complex case we must make complex(x, 0.)
510n/a different from complex(x, -0.) and complex(0., y)
511n/a different from complex(-0., y), for any x and y.
512n/a All four complex zeros must be distinguished.*/
513n/a z = PyComplex_AsCComplex(op);
514n/a real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
515n/a imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
516n/a /* use True, False and None singleton as tags for the real and imag
517n/a * sign, to make tuples different */
518n/a if (real_negzero && imag_negzero) {
519n/a key = PyTuple_Pack(3, Py_TYPE(op), op, Py_True);
520n/a }
521n/a else if (imag_negzero) {
522n/a key = PyTuple_Pack(3, Py_TYPE(op), op, Py_False);
523n/a }
524n/a else if (real_negzero) {
525n/a key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
526n/a }
527n/a else {
528n/a key = PyTuple_Pack(2, Py_TYPE(op), op);
529n/a }
530n/a }
531n/a else if (PyTuple_CheckExact(op)) {
532n/a Py_ssize_t i, len;
533n/a PyObject *tuple;
534n/a
535n/a len = PyTuple_GET_SIZE(op);
536n/a tuple = PyTuple_New(len);
537n/a if (tuple == NULL)
538n/a return NULL;
539n/a
540n/a for (i=0; i < len; i++) {
541n/a PyObject *item, *item_key;
542n/a
543n/a item = PyTuple_GET_ITEM(op, i);
544n/a item_key = _PyCode_ConstantKey(item);
545n/a if (item_key == NULL) {
546n/a Py_DECREF(tuple);
547n/a return NULL;
548n/a }
549n/a
550n/a PyTuple_SET_ITEM(tuple, i, item_key);
551n/a }
552n/a
553n/a key = PyTuple_Pack(2, tuple, op);
554n/a Py_DECREF(tuple);
555n/a }
556n/a else if (PyFrozenSet_CheckExact(op)) {
557n/a Py_ssize_t pos = 0;
558n/a PyObject *item;
559n/a Py_hash_t hash;
560n/a Py_ssize_t i, len;
561n/a PyObject *tuple, *set;
562n/a
563n/a len = PySet_GET_SIZE(op);
564n/a tuple = PyTuple_New(len);
565n/a if (tuple == NULL)
566n/a return NULL;
567n/a
568n/a i = 0;
569n/a while (_PySet_NextEntry(op, &pos, &item, &hash)) {
570n/a PyObject *item_key;
571n/a
572n/a item_key = _PyCode_ConstantKey(item);
573n/a if (item_key == NULL) {
574n/a Py_DECREF(tuple);
575n/a return NULL;
576n/a }
577n/a
578n/a assert(i < len);
579n/a PyTuple_SET_ITEM(tuple, i, item_key);
580n/a i++;
581n/a }
582n/a set = PyFrozenSet_New(tuple);
583n/a Py_DECREF(tuple);
584n/a if (set == NULL)
585n/a return NULL;
586n/a
587n/a key = PyTuple_Pack(2, set, op);
588n/a Py_DECREF(set);
589n/a return key;
590n/a }
591n/a else {
592n/a /* for other types, use the object identifier as a unique identifier
593n/a * to ensure that they are seen as unequal. */
594n/a PyObject *obj_id = PyLong_FromVoidPtr(op);
595n/a if (obj_id == NULL)
596n/a return NULL;
597n/a
598n/a key = PyTuple_Pack(2, obj_id, op);
599n/a Py_DECREF(obj_id);
600n/a }
601n/a return key;
602n/a}
603n/a
604n/astatic PyObject *
605n/acode_richcompare(PyObject *self, PyObject *other, int op)
606n/a{
607n/a PyCodeObject *co, *cp;
608n/a int eq;
609n/a PyObject *consts1, *consts2;
610n/a PyObject *res;
611n/a
612n/a if ((op != Py_EQ && op != Py_NE) ||
613n/a !PyCode_Check(self) ||
614n/a !PyCode_Check(other)) {
615n/a Py_RETURN_NOTIMPLEMENTED;
616n/a }
617n/a
618n/a co = (PyCodeObject *)self;
619n/a cp = (PyCodeObject *)other;
620n/a
621n/a eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
622n/a if (eq <= 0) goto unequal;
623n/a eq = co->co_argcount == cp->co_argcount;
624n/a if (!eq) goto unequal;
625n/a eq = co->co_kwonlyargcount == cp->co_kwonlyargcount;
626n/a if (!eq) goto unequal;
627n/a eq = co->co_nlocals == cp->co_nlocals;
628n/a if (!eq) goto unequal;
629n/a eq = co->co_flags == cp->co_flags;
630n/a if (!eq) goto unequal;
631n/a eq = co->co_firstlineno == cp->co_firstlineno;
632n/a if (!eq) goto unequal;
633n/a eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);
634n/a if (eq <= 0) goto unequal;
635n/a
636n/a /* compare constants */
637n/a consts1 = _PyCode_ConstantKey(co->co_consts);
638n/a if (!consts1)
639n/a return NULL;
640n/a consts2 = _PyCode_ConstantKey(cp->co_consts);
641n/a if (!consts2) {
642n/a Py_DECREF(consts1);
643n/a return NULL;
644n/a }
645n/a eq = PyObject_RichCompareBool(consts1, consts2, Py_EQ);
646n/a Py_DECREF(consts1);
647n/a Py_DECREF(consts2);
648n/a if (eq <= 0) goto unequal;
649n/a
650n/a eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);
651n/a if (eq <= 0) goto unequal;
652n/a eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ);
653n/a if (eq <= 0) goto unequal;
654n/a eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ);
655n/a if (eq <= 0) goto unequal;
656n/a eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ);
657n/a if (eq <= 0) goto unequal;
658n/a
659n/a if (op == Py_EQ)
660n/a res = Py_True;
661n/a else
662n/a res = Py_False;
663n/a goto done;
664n/a
665n/a unequal:
666n/a if (eq < 0)
667n/a return NULL;
668n/a if (op == Py_NE)
669n/a res = Py_True;
670n/a else
671n/a res = Py_False;
672n/a
673n/a done:
674n/a Py_INCREF(res);
675n/a return res;
676n/a}
677n/a
678n/astatic Py_hash_t
679n/acode_hash(PyCodeObject *co)
680n/a{
681n/a Py_hash_t h, h0, h1, h2, h3, h4, h5, h6;
682n/a h0 = PyObject_Hash(co->co_name);
683n/a if (h0 == -1) return -1;
684n/a h1 = PyObject_Hash(co->co_code);
685n/a if (h1 == -1) return -1;
686n/a h2 = PyObject_Hash(co->co_consts);
687n/a if (h2 == -1) return -1;
688n/a h3 = PyObject_Hash(co->co_names);
689n/a if (h3 == -1) return -1;
690n/a h4 = PyObject_Hash(co->co_varnames);
691n/a if (h4 == -1) return -1;
692n/a h5 = PyObject_Hash(co->co_freevars);
693n/a if (h5 == -1) return -1;
694n/a h6 = PyObject_Hash(co->co_cellvars);
695n/a if (h6 == -1) return -1;
696n/a h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
697n/a co->co_argcount ^ co->co_kwonlyargcount ^
698n/a co->co_nlocals ^ co->co_flags;
699n/a if (h == -1) h = -2;
700n/a return h;
701n/a}
702n/a
703n/a/* XXX code objects need to participate in GC? */
704n/a
705n/astatic struct PyMethodDef code_methods[] = {
706n/a {"__sizeof__", (PyCFunction)code_sizeof, METH_NOARGS},
707n/a {NULL, NULL} /* sentinel */
708n/a};
709n/a
710n/aPyTypeObject PyCode_Type = {
711n/a PyVarObject_HEAD_INIT(&PyType_Type, 0)
712n/a "code",
713n/a sizeof(PyCodeObject),
714n/a 0,
715n/a (destructor)code_dealloc, /* tp_dealloc */
716n/a 0, /* tp_print */
717n/a 0, /* tp_getattr */
718n/a 0, /* tp_setattr */
719n/a 0, /* tp_reserved */
720n/a (reprfunc)code_repr, /* tp_repr */
721n/a 0, /* tp_as_number */
722n/a 0, /* tp_as_sequence */
723n/a 0, /* tp_as_mapping */
724n/a (hashfunc)code_hash, /* tp_hash */
725n/a 0, /* tp_call */
726n/a 0, /* tp_str */
727n/a PyObject_GenericGetAttr, /* tp_getattro */
728n/a 0, /* tp_setattro */
729n/a 0, /* tp_as_buffer */
730n/a Py_TPFLAGS_DEFAULT, /* tp_flags */
731n/a code_doc, /* tp_doc */
732n/a 0, /* tp_traverse */
733n/a 0, /* tp_clear */
734n/a code_richcompare, /* tp_richcompare */
735n/a offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */
736n/a 0, /* tp_iter */
737n/a 0, /* tp_iternext */
738n/a code_methods, /* tp_methods */
739n/a code_memberlist, /* tp_members */
740n/a 0, /* tp_getset */
741n/a 0, /* tp_base */
742n/a 0, /* tp_dict */
743n/a 0, /* tp_descr_get */
744n/a 0, /* tp_descr_set */
745n/a 0, /* tp_dictoffset */
746n/a 0, /* tp_init */
747n/a 0, /* tp_alloc */
748n/a code_new, /* tp_new */
749n/a};
750n/a
751n/a/* Use co_lnotab to compute the line number from a bytecode index, addrq. See
752n/a lnotab_notes.txt for the details of the lnotab representation.
753n/a*/
754n/a
755n/aint
756n/aPyCode_Addr2Line(PyCodeObject *co, int addrq)
757n/a{
758n/a Py_ssize_t size = PyBytes_Size(co->co_lnotab) / 2;
759n/a unsigned char *p = (unsigned char*)PyBytes_AsString(co->co_lnotab);
760n/a int line = co->co_firstlineno;
761n/a int addr = 0;
762n/a while (--size >= 0) {
763n/a addr += *p++;
764n/a if (addr > addrq)
765n/a break;
766n/a line += (signed char)*p;
767n/a p++;
768n/a }
769n/a return line;
770n/a}
771n/a
772n/a/* Update *bounds to describe the first and one-past-the-last instructions in
773n/a the same line as lasti. Return the number of that line. */
774n/aint
775n/a_PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds)
776n/a{
777n/a Py_ssize_t size;
778n/a int addr, line;
779n/a unsigned char* p;
780n/a
781n/a p = (unsigned char*)PyBytes_AS_STRING(co->co_lnotab);
782n/a size = PyBytes_GET_SIZE(co->co_lnotab) / 2;
783n/a
784n/a addr = 0;
785n/a line = co->co_firstlineno;
786n/a assert(line > 0);
787n/a
788n/a /* possible optimization: if f->f_lasti == instr_ub
789n/a (likely to be a common case) then we already know
790n/a instr_lb -- if we stored the matching value of p
791n/a somewhere we could skip the first while loop. */
792n/a
793n/a /* See lnotab_notes.txt for the description of
794n/a co_lnotab. A point to remember: increments to p
795n/a come in (addr, line) pairs. */
796n/a
797n/a bounds->ap_lower = 0;
798n/a while (size > 0) {
799n/a if (addr + *p > lasti)
800n/a break;
801n/a addr += *p++;
802n/a if ((signed char)*p)
803n/a bounds->ap_lower = addr;
804n/a line += (signed char)*p;
805n/a p++;
806n/a --size;
807n/a }
808n/a
809n/a if (size > 0) {
810n/a while (--size >= 0) {
811n/a addr += *p++;
812n/a if ((signed char)*p)
813n/a break;
814n/a p++;
815n/a }
816n/a bounds->ap_upper = addr;
817n/a }
818n/a else {
819n/a bounds->ap_upper = INT_MAX;
820n/a }
821n/a
822n/a return line;
823n/a}
824n/a
825n/a
826n/aint
827n/a_PyCode_GetExtra(PyObject *code, Py_ssize_t index, void **extra)
828n/a{
829n/a assert(*extra == NULL);
830n/a
831n/a if (!PyCode_Check(code)) {
832n/a PyErr_BadInternalCall();
833n/a return -1;
834n/a }
835n/a
836n/a PyCodeObject *o = (PyCodeObject*) code;
837n/a _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) o->co_extra;
838n/a
839n/a
840n/a if (co_extra == NULL || co_extra->ce_size <= index) {
841n/a return 0;
842n/a }
843n/a
844n/a *extra = co_extra->ce_extras[index];
845n/a return 0;
846n/a}
847n/a
848n/a
849n/aint
850n/a_PyCode_SetExtra(PyObject *code, Py_ssize_t index, void *extra)
851n/a{
852n/a PyThreadState *tstate = PyThreadState_Get();
853n/a
854n/a if (!PyCode_Check(code) || index < 0 ||
855n/a index >= tstate->co_extra_user_count) {
856n/a PyErr_BadInternalCall();
857n/a return -1;
858n/a }
859n/a
860n/a PyCodeObject *o = (PyCodeObject*) code;
861n/a _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra *) o->co_extra;
862n/a
863n/a if (co_extra == NULL) {
864n/a o->co_extra = (_PyCodeObjectExtra*) PyMem_Malloc(
865n/a sizeof(_PyCodeObjectExtra));
866n/a if (o->co_extra == NULL) {
867n/a return -1;
868n/a }
869n/a co_extra = (_PyCodeObjectExtra *) o->co_extra;
870n/a
871n/a co_extra->ce_extras = PyMem_Malloc(
872n/a tstate->co_extra_user_count * sizeof(void*));
873n/a if (co_extra->ce_extras == NULL) {
874n/a return -1;
875n/a }
876n/a
877n/a co_extra->ce_size = tstate->co_extra_user_count;
878n/a
879n/a for (Py_ssize_t i = 0; i < co_extra->ce_size; i++) {
880n/a co_extra->ce_extras[i] = NULL;
881n/a }
882n/a }
883n/a else if (co_extra->ce_size <= index) {
884n/a co_extra->ce_extras = PyMem_Realloc(
885n/a co_extra->ce_extras, tstate->co_extra_user_count * sizeof(void*));
886n/a
887n/a if (co_extra->ce_extras == NULL) {
888n/a return -1;
889n/a }
890n/a
891n/a co_extra->ce_size = tstate->co_extra_user_count;
892n/a
893n/a for (Py_ssize_t i = co_extra->ce_size; i < co_extra->ce_size; i++) {
894n/a co_extra->ce_extras[i] = NULL;
895n/a }
896n/a }
897n/a
898n/a co_extra->ce_extras[index] = extra;
899n/a return 0;
900n/a}