summaryrefslogtreecommitdiffstats
path: root/contrib/tools/python3/src/Objects/codeobject.c
diff options
context:
space:
mode:
authorshadchin <[email protected]>2022-02-10 16:44:39 +0300
committerDaniil Cherednik <[email protected]>2022-02-10 16:44:39 +0300
commite9656aae26e0358d5378e5b63dcac5c8dbe0e4d0 (patch)
tree64175d5cadab313b3e7039ebaa06c5bc3295e274 /contrib/tools/python3/src/Objects/codeobject.c
parent2598ef1d0aee359b4b6d5fdd1758916d5907d04f (diff)
Restoring authorship annotation for <[email protected]>. Commit 2 of 2.
Diffstat (limited to 'contrib/tools/python3/src/Objects/codeobject.c')
-rw-r--r--contrib/tools/python3/src/Objects/codeobject.c592
1 files changed, 296 insertions, 296 deletions
diff --git a/contrib/tools/python3/src/Objects/codeobject.c b/contrib/tools/python3/src/Objects/codeobject.c
index 9bc5a51c44d..cb4fb681243 100644
--- a/contrib/tools/python3/src/Objects/codeobject.c
+++ b/contrib/tools/python3/src/Objects/codeobject.c
@@ -2,13 +2,13 @@
#include "Python.h"
#include "code.h"
-#include "opcode.h"
-#include "structmember.h" // PyMemberDef
-#include "pycore_code.h"
-#include "pycore_interp.h" // PyInterpreterState.co_extra_freefuncs
-#include "pycore_pystate.h" // _PyInterpreterState_GET()
-#include "pycore_tupleobject.h"
-#include "clinic/codeobject.c.h"
+#include "opcode.h"
+#include "structmember.h" // PyMemberDef
+#include "pycore_code.h"
+#include "pycore_interp.h" // PyInterpreterState.co_extra_freefuncs
+#include "pycore_pystate.h" // _PyInterpreterState_GET()
+#include "pycore_tupleobject.h"
+#include "clinic/codeobject.c.h"
/* Holder for co_extra information */
typedef struct {
@@ -16,11 +16,11 @@ typedef struct {
void *ce_extras[1];
} _PyCodeObjectExtra;
-/*[clinic input]
-class code "PyCodeObject *" "&PyCode_Type"
-[clinic start generated code]*/
-/*[clinic end generated code: output=da39a3ee5e6b4b0d input=78aa5d576683bb4b]*/
-
+/*[clinic input]
+class code "PyCodeObject *" "&PyCode_Type"
+[clinic start generated code]*/
+/*[clinic end generated code: output=da39a3ee5e6b4b0d input=78aa5d576683bb4b]*/
+
/* all_name_chars(s): true iff s matches [a-zA-Z0-9_]* */
static int
all_name_chars(PyObject *o)
@@ -39,7 +39,7 @@ all_name_chars(PyObject *o)
return 1;
}
-static int
+static int
intern_strings(PyObject *tuple)
{
Py_ssize_t i;
@@ -47,88 +47,88 @@ intern_strings(PyObject *tuple)
for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
PyObject *v = PyTuple_GET_ITEM(tuple, i);
if (v == NULL || !PyUnicode_CheckExact(v)) {
- PyErr_SetString(PyExc_SystemError,
- "non-string found in code slot");
- return -1;
+ PyErr_SetString(PyExc_SystemError,
+ "non-string found in code slot");
+ return -1;
}
- PyUnicode_InternInPlace(&_PyTuple_ITEMS(tuple)[i]);
+ PyUnicode_InternInPlace(&_PyTuple_ITEMS(tuple)[i]);
}
- return 0;
+ return 0;
}
/* Intern selected string constants */
static int
-intern_string_constants(PyObject *tuple, int *modified)
+intern_string_constants(PyObject *tuple, int *modified)
{
- for (Py_ssize_t i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
+ for (Py_ssize_t i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
PyObject *v = PyTuple_GET_ITEM(tuple, i);
if (PyUnicode_CheckExact(v)) {
if (PyUnicode_READY(v) == -1) {
- return -1;
+ return -1;
}
-
+
if (all_name_chars(v)) {
PyObject *w = v;
PyUnicode_InternInPlace(&v);
if (w != v) {
PyTuple_SET_ITEM(tuple, i, v);
- if (modified) {
- *modified = 1;
- }
+ if (modified) {
+ *modified = 1;
+ }
}
}
}
else if (PyTuple_CheckExact(v)) {
- if (intern_string_constants(v, NULL) < 0) {
- return -1;
- }
+ if (intern_string_constants(v, NULL) < 0) {
+ return -1;
+ }
}
else if (PyFrozenSet_CheckExact(v)) {
PyObject *w = v;
PyObject *tmp = PySequence_Tuple(v);
if (tmp == NULL) {
- return -1;
+ return -1;
+ }
+ int tmp_modified = 0;
+ if (intern_string_constants(tmp, &tmp_modified) < 0) {
+ Py_DECREF(tmp);
+ return -1;
}
- int tmp_modified = 0;
- if (intern_string_constants(tmp, &tmp_modified) < 0) {
- Py_DECREF(tmp);
- return -1;
- }
- if (tmp_modified) {
+ if (tmp_modified) {
v = PyFrozenSet_New(tmp);
if (v == NULL) {
- Py_DECREF(tmp);
- return -1;
+ Py_DECREF(tmp);
+ return -1;
}
-
- PyTuple_SET_ITEM(tuple, i, v);
- Py_DECREF(w);
- if (modified) {
- *modified = 1;
+
+ PyTuple_SET_ITEM(tuple, i, v);
+ Py_DECREF(w);
+ if (modified) {
+ *modified = 1;
}
}
Py_DECREF(tmp);
}
}
- return 0;
+ return 0;
}
PyCodeObject *
-PyCode_NewWithPosOnlyArgs(int argcount, int posonlyargcount, int kwonlyargcount,
- int nlocals, int stacksize, int flags,
- PyObject *code, PyObject *consts, PyObject *names,
- PyObject *varnames, PyObject *freevars, PyObject *cellvars,
- PyObject *filename, PyObject *name, int firstlineno,
- PyObject *lnotab)
+PyCode_NewWithPosOnlyArgs(int argcount, int posonlyargcount, int kwonlyargcount,
+ int nlocals, int stacksize, int flags,
+ PyObject *code, PyObject *consts, PyObject *names,
+ PyObject *varnames, PyObject *freevars, PyObject *cellvars,
+ PyObject *filename, PyObject *name, int firstlineno,
+ PyObject *lnotab)
{
PyCodeObject *co;
Py_ssize_t *cell2arg = NULL;
Py_ssize_t i, n_cellvars, n_varnames, total_args;
/* Check argument types */
- if (argcount < posonlyargcount || posonlyargcount < 0 ||
- kwonlyargcount < 0 || nlocals < 0 ||
- stacksize < 0 || flags < 0 ||
+ if (argcount < posonlyargcount || posonlyargcount < 0 ||
+ kwonlyargcount < 0 || nlocals < 0 ||
+ stacksize < 0 || flags < 0 ||
code == NULL || !PyBytes_Check(code) ||
consts == NULL || !PyTuple_Check(consts) ||
names == NULL || !PyTuple_Check(names) ||
@@ -142,38 +142,38 @@ PyCode_NewWithPosOnlyArgs(int argcount, int posonlyargcount, int kwonlyargcount,
return NULL;
}
- /* Ensure that strings are ready Unicode string */
- if (PyUnicode_READY(name) < 0) {
+ /* Ensure that strings are ready Unicode string */
+ if (PyUnicode_READY(name) < 0) {
+ return NULL;
+ }
+ if (PyUnicode_READY(filename) < 0) {
+ return NULL;
+ }
+
+ if (intern_strings(names) < 0) {
+ return NULL;
+ }
+ if (intern_strings(varnames) < 0) {
+ return NULL;
+ }
+ if (intern_strings(freevars) < 0) {
+ return NULL;
+ }
+ if (intern_strings(cellvars) < 0) {
return NULL;
- }
- if (PyUnicode_READY(filename) < 0) {
- return NULL;
- }
+ }
+ if (intern_string_constants(consts, NULL) < 0) {
+ return NULL;
+ }
- if (intern_strings(names) < 0) {
- return NULL;
- }
- if (intern_strings(varnames) < 0) {
- return NULL;
- }
- if (intern_strings(freevars) < 0) {
- return NULL;
- }
- if (intern_strings(cellvars) < 0) {
- return NULL;
- }
- if (intern_string_constants(consts, NULL) < 0) {
- return NULL;
- }
+ /* Make sure that code is indexable with an int, this is
+ a long running assumption in ceval.c and many parts of
+ the interpreter. */
+ if (PyBytes_GET_SIZE(code) > INT_MAX) {
+ PyErr_SetString(PyExc_OverflowError, "co_code larger than INT_MAX");
+ return NULL;
+ }
- /* Make sure that code is indexable with an int, this is
- a long running assumption in ceval.c and many parts of
- the interpreter. */
- if (PyBytes_GET_SIZE(code) > INT_MAX) {
- PyErr_SetString(PyExc_OverflowError, "co_code larger than INT_MAX");
- return NULL;
- }
-
/* Check for any inner or outer closure references */
n_cellvars = PyTuple_GET_SIZE(cellvars);
if (!n_cellvars && !PyTuple_GET_SIZE(freevars)) {
@@ -186,7 +186,7 @@ PyCode_NewWithPosOnlyArgs(int argcount, int posonlyargcount, int kwonlyargcount,
if (argcount <= n_varnames && kwonlyargcount <= n_varnames) {
/* Never overflows. */
total_args = (Py_ssize_t)argcount + (Py_ssize_t)kwonlyargcount +
- ((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0);
+ ((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0);
}
else {
total_args = n_varnames + 1;
@@ -228,14 +228,14 @@ PyCode_NewWithPosOnlyArgs(int argcount, int posonlyargcount, int kwonlyargcount,
cell2arg = NULL;
}
}
- co = PyObject_New(PyCodeObject, &PyCode_Type);
+ co = PyObject_New(PyCodeObject, &PyCode_Type);
if (co == NULL) {
if (cell2arg)
PyMem_FREE(cell2arg);
return NULL;
}
co->co_argcount = argcount;
- co->co_posonlyargcount = posonlyargcount;
+ co->co_posonlyargcount = posonlyargcount;
co->co_kwonlyargcount = kwonlyargcount;
co->co_nlocals = nlocals;
co->co_stacksize = stacksize;
@@ -263,72 +263,72 @@ PyCode_NewWithPosOnlyArgs(int argcount, int posonlyargcount, int kwonlyargcount,
co->co_zombieframe = NULL;
co->co_weakreflist = NULL;
co->co_extra = NULL;
-
- co->co_opcache_map = NULL;
- co->co_opcache = NULL;
- co->co_opcache_flag = 0;
- co->co_opcache_size = 0;
+
+ co->co_opcache_map = NULL;
+ co->co_opcache = NULL;
+ co->co_opcache_flag = 0;
+ co->co_opcache_size = 0;
return co;
}
PyCodeObject *
-PyCode_New(int argcount, int kwonlyargcount,
- int nlocals, int stacksize, int flags,
- PyObject *code, PyObject *consts, PyObject *names,
- PyObject *varnames, PyObject *freevars, PyObject *cellvars,
- PyObject *filename, PyObject *name, int firstlineno,
- PyObject *lnotab)
-{
- return PyCode_NewWithPosOnlyArgs(argcount, 0, kwonlyargcount, nlocals,
- stacksize, flags, code, consts, names,
- varnames, freevars, cellvars, filename,
- name, firstlineno, lnotab);
-}
-
-int
-_PyCode_InitOpcache(PyCodeObject *co)
-{
- Py_ssize_t co_size = PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT);
- co->co_opcache_map = (unsigned char *)PyMem_Calloc(co_size, 1);
- if (co->co_opcache_map == NULL) {
- return -1;
- }
-
- _Py_CODEUNIT *opcodes = (_Py_CODEUNIT*)PyBytes_AS_STRING(co->co_code);
- Py_ssize_t opts = 0;
-
- for (Py_ssize_t i = 0; i < co_size;) {
- unsigned char opcode = _Py_OPCODE(opcodes[i]);
- i++; // 'i' is now aligned to (next_instr - first_instr)
-
- // TODO: LOAD_METHOD, LOAD_ATTR
- if (opcode == LOAD_GLOBAL) {
- opts++;
- co->co_opcache_map[i] = (unsigned char)opts;
- if (opts > 254) {
- break;
- }
- }
- }
-
- if (opts) {
- co->co_opcache = (_PyOpcache *)PyMem_Calloc(opts, sizeof(_PyOpcache));
- if (co->co_opcache == NULL) {
- PyMem_FREE(co->co_opcache_map);
- return -1;
- }
- }
- else {
- PyMem_FREE(co->co_opcache_map);
- co->co_opcache_map = NULL;
- co->co_opcache = NULL;
- }
-
- co->co_opcache_size = (unsigned char)opts;
- return 0;
-}
-
-PyCodeObject *
+PyCode_New(int argcount, int kwonlyargcount,
+ int nlocals, int stacksize, int flags,
+ PyObject *code, PyObject *consts, PyObject *names,
+ PyObject *varnames, PyObject *freevars, PyObject *cellvars,
+ PyObject *filename, PyObject *name, int firstlineno,
+ PyObject *lnotab)
+{
+ return PyCode_NewWithPosOnlyArgs(argcount, 0, kwonlyargcount, nlocals,
+ stacksize, flags, code, consts, names,
+ varnames, freevars, cellvars, filename,
+ name, firstlineno, lnotab);
+}
+
+int
+_PyCode_InitOpcache(PyCodeObject *co)
+{
+ Py_ssize_t co_size = PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT);
+ co->co_opcache_map = (unsigned char *)PyMem_Calloc(co_size, 1);
+ if (co->co_opcache_map == NULL) {
+ return -1;
+ }
+
+ _Py_CODEUNIT *opcodes = (_Py_CODEUNIT*)PyBytes_AS_STRING(co->co_code);
+ Py_ssize_t opts = 0;
+
+ for (Py_ssize_t i = 0; i < co_size;) {
+ unsigned char opcode = _Py_OPCODE(opcodes[i]);
+ i++; // 'i' is now aligned to (next_instr - first_instr)
+
+ // TODO: LOAD_METHOD, LOAD_ATTR
+ if (opcode == LOAD_GLOBAL) {
+ opts++;
+ co->co_opcache_map[i] = (unsigned char)opts;
+ if (opts > 254) {
+ break;
+ }
+ }
+ }
+
+ if (opts) {
+ co->co_opcache = (_PyOpcache *)PyMem_Calloc(opts, sizeof(_PyOpcache));
+ if (co->co_opcache == NULL) {
+ PyMem_FREE(co->co_opcache_map);
+ return -1;
+ }
+ }
+ else {
+ PyMem_FREE(co->co_opcache_map);
+ co->co_opcache_map = NULL;
+ co->co_opcache = NULL;
+ }
+
+ co->co_opcache_size = (unsigned char)opts;
+ return 0;
+}
+
+PyCodeObject *
PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
{
static PyObject *emptystring = NULL;
@@ -353,9 +353,9 @@ PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
if (filename_ob == NULL)
goto failed;
- result = PyCode_NewWithPosOnlyArgs(
- 0, /* argcount */
- 0, /* posonlyargcount */
+ result = PyCode_NewWithPosOnlyArgs(
+ 0, /* argcount */
+ 0, /* posonlyargcount */
0, /* kwonlyargcount */
0, /* nlocals */
0, /* stacksize */
@@ -381,22 +381,22 @@ failed:
#define OFF(x) offsetof(PyCodeObject, x)
static PyMemberDef code_memberlist[] = {
- {"co_argcount", T_INT, OFF(co_argcount), READONLY},
- {"co_posonlyargcount", T_INT, OFF(co_posonlyargcount), READONLY},
- {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY},
- {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
- {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
- {"co_flags", T_INT, OFF(co_flags), READONLY},
- {"co_code", T_OBJECT, OFF(co_code), READONLY},
- {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
- {"co_names", T_OBJECT, OFF(co_names), READONLY},
- {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
- {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
- {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
- {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
- {"co_name", T_OBJECT, OFF(co_name), READONLY},
- {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
- {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
+ {"co_argcount", T_INT, OFF(co_argcount), READONLY},
+ {"co_posonlyargcount", T_INT, OFF(co_posonlyargcount), READONLY},
+ {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY},
+ {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
+ {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
+ {"co_flags", T_INT, OFF(co_flags), READONLY},
+ {"co_code", T_OBJECT, OFF(co_code), READONLY},
+ {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
+ {"co_names", T_OBJECT, OFF(co_names), READONLY},
+ {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
+ {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
+ {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
+ {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
+ {"co_name", T_OBJECT, OFF(co_name), READONLY},
+ {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
+ {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
{NULL} /* Sentinel */
};
@@ -425,7 +425,7 @@ validate_and_copy_tuple(PyObject *tup)
PyExc_TypeError,
"name tuples must contain only "
"strings, not '%.500s'",
- Py_TYPE(item)->tp_name);
+ Py_TYPE(item)->tp_name);
Py_DECREF(newtuple);
return NULL;
}
@@ -443,9 +443,9 @@ validate_and_copy_tuple(PyObject *tup)
}
PyDoc_STRVAR(code_doc,
-"code(argcount, posonlyargcount, kwonlyargcount, nlocals, stacksize,\n\
- flags, codestring, constants, names, varnames, filename, name,\n\
- firstlineno, lnotab[, freevars[, cellvars]])\n\
+"code(argcount, posonlyargcount, kwonlyargcount, nlocals, stacksize,\n\
+ flags, codestring, constants, names, varnames, filename, name,\n\
+ firstlineno, lnotab[, freevars[, cellvars]])\n\
\n\
Create a code object. Not for the faint of heart.");
@@ -453,7 +453,7 @@ static PyObject *
code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
{
int argcount;
- int posonlyargcount;
+ int posonlyargcount;
int kwonlyargcount;
int nlocals;
int stacksize;
@@ -470,8 +470,8 @@ code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
int firstlineno;
PyObject *lnotab;
- if (!PyArg_ParseTuple(args, "iiiiiiSO!O!O!UUiS|O!O!:code",
- &argcount, &posonlyargcount, &kwonlyargcount,
+ if (!PyArg_ParseTuple(args, "iiiiiiSO!O!O!UUiS|O!O!:code",
+ &argcount, &posonlyargcount, &kwonlyargcount,
&nlocals, &stacksize, &flags,
&code,
&PyTuple_Type, &consts,
@@ -483,12 +483,12 @@ code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
&PyTuple_Type, &cellvars))
return NULL;
- if (PySys_Audit("code.__new__", "OOOiiiiii",
- code, filename, name, argcount, posonlyargcount,
- kwonlyargcount, nlocals, stacksize, flags) < 0) {
- goto cleanup;
- }
-
+ if (PySys_Audit("code.__new__", "OOOiiiiii",
+ code, filename, name, argcount, posonlyargcount,
+ kwonlyargcount, nlocals, stacksize, flags) < 0) {
+ goto cleanup;
+ }
+
if (argcount < 0) {
PyErr_SetString(
PyExc_ValueError,
@@ -496,13 +496,13 @@ code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
goto cleanup;
}
- if (posonlyargcount < 0) {
- PyErr_SetString(
- PyExc_ValueError,
- "code: posonlyargcount must not be negative");
- goto cleanup;
- }
-
+ if (posonlyargcount < 0) {
+ PyErr_SetString(
+ PyExc_ValueError,
+ "code: posonlyargcount must not be negative");
+ goto cleanup;
+ }
+
if (kwonlyargcount < 0) {
PyErr_SetString(
PyExc_ValueError,
@@ -535,14 +535,14 @@ code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
if (ourcellvars == NULL)
goto cleanup;
- co = (PyObject *)PyCode_NewWithPosOnlyArgs(argcount, posonlyargcount,
- kwonlyargcount,
- nlocals, stacksize, flags,
- code, consts, ournames,
- ourvarnames, ourfreevars,
- ourcellvars, filename,
- name, firstlineno, lnotab);
- cleanup:
+ co = (PyObject *)PyCode_NewWithPosOnlyArgs(argcount, posonlyargcount,
+ kwonlyargcount,
+ nlocals, stacksize, flags,
+ code, consts, ournames,
+ ourvarnames, ourfreevars,
+ ourcellvars, filename,
+ name, firstlineno, lnotab);
+ cleanup:
Py_XDECREF(ournames);
Py_XDECREF(ourvarnames);
Py_XDECREF(ourfreevars);
@@ -553,17 +553,17 @@ code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
static void
code_dealloc(PyCodeObject *co)
{
- if (co->co_opcache != NULL) {
- PyMem_FREE(co->co_opcache);
- }
- if (co->co_opcache_map != NULL) {
- PyMem_FREE(co->co_opcache_map);
- }
- co->co_opcache_flag = 0;
- co->co_opcache_size = 0;
-
+ if (co->co_opcache != NULL) {
+ PyMem_FREE(co->co_opcache);
+ }
+ if (co->co_opcache_map != NULL) {
+ PyMem_FREE(co->co_opcache_map);
+ }
+ co->co_opcache_flag = 0;
+ co->co_opcache_size = 0;
+
if (co->co_extra != NULL) {
- PyInterpreterState *interp = _PyInterpreterState_GET();
+ PyInterpreterState *interp = _PyInterpreterState_GET();
_PyCodeObjectExtra *co_extra = co->co_extra;
for (Py_ssize_t i = 0; i < co_extra->ce_size; i++) {
@@ -596,7 +596,7 @@ code_dealloc(PyCodeObject *co)
}
static PyObject *
-code_sizeof(PyCodeObject *co, PyObject *Py_UNUSED(args))
+code_sizeof(PyCodeObject *co, PyObject *Py_UNUSED(args))
{
Py_ssize_t res = _PyObject_SIZE(Py_TYPE(co));
_PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) co->co_extra;
@@ -608,83 +608,83 @@ code_sizeof(PyCodeObject *co, PyObject *Py_UNUSED(args))
res += sizeof(_PyCodeObjectExtra) +
(co_extra->ce_size-1) * sizeof(co_extra->ce_extras[0]);
}
- if (co->co_opcache != NULL) {
- assert(co->co_opcache_map != NULL);
- // co_opcache_map
- res += PyBytes_GET_SIZE(co->co_code) / sizeof(_Py_CODEUNIT);
- // co_opcache
- res += co->co_opcache_size * sizeof(_PyOpcache);
- }
+ if (co->co_opcache != NULL) {
+ assert(co->co_opcache_map != NULL);
+ // co_opcache_map
+ res += PyBytes_GET_SIZE(co->co_code) / sizeof(_Py_CODEUNIT);
+ // co_opcache
+ res += co->co_opcache_size * sizeof(_PyOpcache);
+ }
return PyLong_FromSsize_t(res);
}
-/*[clinic input]
-code.replace
-
- *
- co_argcount: int(c_default="self->co_argcount") = -1
- co_posonlyargcount: int(c_default="self->co_posonlyargcount") = -1
- co_kwonlyargcount: int(c_default="self->co_kwonlyargcount") = -1
- co_nlocals: int(c_default="self->co_nlocals") = -1
- co_stacksize: int(c_default="self->co_stacksize") = -1
- co_flags: int(c_default="self->co_flags") = -1
- co_firstlineno: int(c_default="self->co_firstlineno") = -1
- co_code: PyBytesObject(c_default="(PyBytesObject *)self->co_code") = None
- co_consts: object(subclass_of="&PyTuple_Type", c_default="self->co_consts") = None
- co_names: object(subclass_of="&PyTuple_Type", c_default="self->co_names") = None
- co_varnames: object(subclass_of="&PyTuple_Type", c_default="self->co_varnames") = None
- co_freevars: object(subclass_of="&PyTuple_Type", c_default="self->co_freevars") = None
- co_cellvars: object(subclass_of="&PyTuple_Type", c_default="self->co_cellvars") = None
- co_filename: unicode(c_default="self->co_filename") = None
- co_name: unicode(c_default="self->co_name") = None
- co_lnotab: PyBytesObject(c_default="(PyBytesObject *)self->co_lnotab") = None
-
-Return a copy of the code object with new values for the specified fields.
-[clinic start generated code]*/
-
+/*[clinic input]
+code.replace
+
+ *
+ co_argcount: int(c_default="self->co_argcount") = -1
+ co_posonlyargcount: int(c_default="self->co_posonlyargcount") = -1
+ co_kwonlyargcount: int(c_default="self->co_kwonlyargcount") = -1
+ co_nlocals: int(c_default="self->co_nlocals") = -1
+ co_stacksize: int(c_default="self->co_stacksize") = -1
+ co_flags: int(c_default="self->co_flags") = -1
+ co_firstlineno: int(c_default="self->co_firstlineno") = -1
+ co_code: PyBytesObject(c_default="(PyBytesObject *)self->co_code") = None
+ co_consts: object(subclass_of="&PyTuple_Type", c_default="self->co_consts") = None
+ co_names: object(subclass_of="&PyTuple_Type", c_default="self->co_names") = None
+ co_varnames: object(subclass_of="&PyTuple_Type", c_default="self->co_varnames") = None
+ co_freevars: object(subclass_of="&PyTuple_Type", c_default="self->co_freevars") = None
+ co_cellvars: object(subclass_of="&PyTuple_Type", c_default="self->co_cellvars") = None
+ co_filename: unicode(c_default="self->co_filename") = None
+ co_name: unicode(c_default="self->co_name") = None
+ co_lnotab: PyBytesObject(c_default="(PyBytesObject *)self->co_lnotab") = None
+
+Return a copy of the code object with new values for the specified fields.
+[clinic start generated code]*/
+
+static PyObject *
+code_replace_impl(PyCodeObject *self, int co_argcount,
+ int co_posonlyargcount, int co_kwonlyargcount,
+ int co_nlocals, int co_stacksize, int co_flags,
+ int co_firstlineno, PyBytesObject *co_code,
+ PyObject *co_consts, PyObject *co_names,
+ PyObject *co_varnames, PyObject *co_freevars,
+ PyObject *co_cellvars, PyObject *co_filename,
+ PyObject *co_name, PyBytesObject *co_lnotab)
+/*[clinic end generated code: output=25c8e303913bcace input=d9051bc8f24e6b28]*/
+{
+#define CHECK_INT_ARG(ARG) \
+ if (ARG < 0) { \
+ PyErr_SetString(PyExc_ValueError, \
+ #ARG " must be a positive integer"); \
+ return NULL; \
+ }
+
+ CHECK_INT_ARG(co_argcount);
+ CHECK_INT_ARG(co_posonlyargcount);
+ CHECK_INT_ARG(co_kwonlyargcount);
+ CHECK_INT_ARG(co_nlocals);
+ CHECK_INT_ARG(co_stacksize);
+ CHECK_INT_ARG(co_flags);
+ CHECK_INT_ARG(co_firstlineno);
+
+#undef CHECK_INT_ARG
+
+ if (PySys_Audit("code.__new__", "OOOiiiiii",
+ co_code, co_filename, co_name, co_argcount,
+ co_posonlyargcount, co_kwonlyargcount, co_nlocals,
+ co_stacksize, co_flags) < 0) {
+ return NULL;
+ }
+
+ return (PyObject *)PyCode_NewWithPosOnlyArgs(
+ co_argcount, co_posonlyargcount, co_kwonlyargcount, co_nlocals,
+ co_stacksize, co_flags, (PyObject*)co_code, co_consts, co_names,
+ co_varnames, co_freevars, co_cellvars, co_filename, co_name,
+ co_firstlineno, (PyObject*)co_lnotab);
+}
+
static PyObject *
-code_replace_impl(PyCodeObject *self, int co_argcount,
- int co_posonlyargcount, int co_kwonlyargcount,
- int co_nlocals, int co_stacksize, int co_flags,
- int co_firstlineno, PyBytesObject *co_code,
- PyObject *co_consts, PyObject *co_names,
- PyObject *co_varnames, PyObject *co_freevars,
- PyObject *co_cellvars, PyObject *co_filename,
- PyObject *co_name, PyBytesObject *co_lnotab)
-/*[clinic end generated code: output=25c8e303913bcace input=d9051bc8f24e6b28]*/
-{
-#define CHECK_INT_ARG(ARG) \
- if (ARG < 0) { \
- PyErr_SetString(PyExc_ValueError, \
- #ARG " must be a positive integer"); \
- return NULL; \
- }
-
- CHECK_INT_ARG(co_argcount);
- CHECK_INT_ARG(co_posonlyargcount);
- CHECK_INT_ARG(co_kwonlyargcount);
- CHECK_INT_ARG(co_nlocals);
- CHECK_INT_ARG(co_stacksize);
- CHECK_INT_ARG(co_flags);
- CHECK_INT_ARG(co_firstlineno);
-
-#undef CHECK_INT_ARG
-
- if (PySys_Audit("code.__new__", "OOOiiiiii",
- co_code, co_filename, co_name, co_argcount,
- co_posonlyargcount, co_kwonlyargcount, co_nlocals,
- co_stacksize, co_flags) < 0) {
- return NULL;
- }
-
- return (PyObject *)PyCode_NewWithPosOnlyArgs(
- co_argcount, co_posonlyargcount, co_kwonlyargcount, co_nlocals,
- co_stacksize, co_flags, (PyObject*)co_code, co_consts, co_names,
- co_varnames, co_freevars, co_cellvars, co_filename, co_name,
- co_firstlineno, (PyObject*)co_lnotab);
-}
-
-static PyObject *
code_repr(PyCodeObject *co)
{
int lineno;
@@ -708,21 +708,21 @@ _PyCode_ConstantKey(PyObject *op)
{
PyObject *key;
- /* Py_None and Py_Ellipsis are singletons. */
+ /* Py_None and Py_Ellipsis are singletons. */
if (op == Py_None || op == Py_Ellipsis
|| PyLong_CheckExact(op)
|| PyUnicode_CheckExact(op)
/* code_richcompare() uses _PyCode_ConstantKey() internally */
- || PyCode_Check(op))
- {
- /* Objects of these types are always different from object of other
- * type and from tuples. */
- Py_INCREF(op);
- key = op;
- }
- else if (PyBool_Check(op) || PyBytes_CheckExact(op)) {
- /* Make booleans different from integers 0 and 1.
- * Avoid BytesWarning from comparing bytes with strings. */
+ || PyCode_Check(op))
+ {
+ /* Objects of these types are always different from object of other
+ * type and from tuples. */
+ Py_INCREF(op);
+ key = op;
+ }
+ else if (PyBool_Check(op) || PyBytes_CheckExact(op)) {
+ /* Make booleans different from integers 0 and 1.
+ * Avoid BytesWarning from comparing bytes with strings. */
key = PyTuple_Pack(2, Py_TYPE(op), op);
}
else if (PyFloat_CheckExact(op)) {
@@ -851,11 +851,11 @@ code_richcompare(PyObject *self, PyObject *other, int op)
cp = (PyCodeObject *)other;
eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
- if (!eq) goto unequal;
+ if (!eq) goto unequal;
eq = co->co_argcount == cp->co_argcount;
if (!eq) goto unequal;
- eq = co->co_posonlyargcount == cp->co_posonlyargcount;
- if (!eq) goto unequal;
+ eq = co->co_posonlyargcount == cp->co_posonlyargcount;
+ if (!eq) goto unequal;
eq = co->co_kwonlyargcount == cp->co_kwonlyargcount;
if (!eq) goto unequal;
eq = co->co_nlocals == cp->co_nlocals;
@@ -928,7 +928,7 @@ code_hash(PyCodeObject *co)
h6 = PyObject_Hash(co->co_cellvars);
if (h6 == -1) return -1;
h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
- co->co_argcount ^ co->co_posonlyargcount ^ co->co_kwonlyargcount ^
+ co->co_argcount ^ co->co_posonlyargcount ^ co->co_kwonlyargcount ^
co->co_nlocals ^ co->co_flags;
if (h == -1) h = -2;
return h;
@@ -938,7 +938,7 @@ code_hash(PyCodeObject *co)
static struct PyMethodDef code_methods[] = {
{"__sizeof__", (PyCFunction)code_sizeof, METH_NOARGS},
- CODE_REPLACE_METHODDEF
+ CODE_REPLACE_METHODDEF
{NULL, NULL} /* sentinel */
};
@@ -948,10 +948,10 @@ PyTypeObject PyCode_Type = {
sizeof(PyCodeObject),
0,
(destructor)code_dealloc, /* tp_dealloc */
- 0, /* tp_vectorcall_offset */
+ 0, /* tp_vectorcall_offset */
0, /* tp_getattr */
0, /* tp_setattr */
- 0, /* tp_as_async */
+ 0, /* tp_as_async */
(reprfunc)code_repr, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
@@ -1082,7 +1082,7 @@ _PyCode_GetExtra(PyObject *code, Py_ssize_t index, void **extra)
int
_PyCode_SetExtra(PyObject *code, Py_ssize_t index, void *extra)
{
- PyInterpreterState *interp = _PyInterpreterState_GET();
+ PyInterpreterState *interp = _PyInterpreterState_GET();
if (!PyCode_Check(code) || index < 0 ||
index >= interp->co_extra_user_count) {