#define _Py_SET_OPCODE(word, opcode) \
do { ((unsigned char *)&(word))[0] = (opcode); } while (0)
+typedef struct {
+ PyObject *_co_code;
+ PyObject *_co_varnames;
+ PyObject *_co_cellvars;
+ PyObject *_co_freevars;
+} _PyCoCached;
+
// To avoid repeating ourselves in deepfreeze.py, all PyCodeObject members are
// defined in this macro:
#define _PyCode_DEF(SIZE) { \
PyObject *co_qualname; /* unicode (qualname, for reference) */ \
PyObject *co_linetable; /* bytes object that holds location info */ \
PyObject *co_weakreflist; /* to support weakrefs to code objects */ \
- PyObject *_co_code; /* cached co_code object/attribute */ \
+ _PyCoCached *_co_cached; /* cached co_* attributes */ \
int _co_firsttraceable; /* index of first traceable instruction */ \
char *_co_linearray; /* array of line offsets */ \
/* Scratch space for extra data relating to the code object. \
return newtuple;
}
+static int
+init_co_cached(PyCodeObject *self) {
+ if (self->_co_cached == NULL) {
+ self->_co_cached = PyMem_New(_PyCoCached, 1);
+ if (self->_co_cached == NULL) {
+ PyErr_NoMemory();
+ return -1;
+ }
+ self->_co_cached->_co_code = NULL;
+ self->_co_cached->_co_cellvars = NULL;
+ self->_co_cached->_co_freevars = NULL;
+ self->_co_cached->_co_varnames = NULL;
+ }
+ return 0;
+}
/******************
* _PyCode_New()
******************/
/* not set */
co->co_weakreflist = NULL;
co->co_extra = NULL;
- co->_co_code = NULL;
+ co->_co_cached = NULL;
co->co_warmup = QUICKENING_INITIAL_WARMUP_VALUE;
co->_co_linearray_entry_size = 0;
* other PyCodeObject accessor functions
******************/
+static PyObject *
+get_cached_locals(PyCodeObject *co, PyObject **cached_field,
+ _PyLocals_Kind kind, int num)
+{
+ assert(cached_field != NULL);
+ assert(co->_co_cached != NULL);
+ if (*cached_field != NULL) {
+ return Py_NewRef(*cached_field);
+ }
+ assert(*cached_field == NULL);
+ PyObject *varnames = get_localsplus_names(co, kind, num);
+ if (varnames == NULL) {
+ return NULL;
+ }
+ *cached_field = Py_NewRef(varnames);
+ return varnames;
+}
+
PyObject *
_PyCode_GetVarnames(PyCodeObject *co)
{
- return get_localsplus_names(co, CO_FAST_LOCAL, co->co_nlocals);
+ if (init_co_cached(co)) {
+ return NULL;
+ }
+ return get_cached_locals(co, &co->_co_cached->_co_varnames, CO_FAST_LOCAL, co->co_nlocals);
}
PyObject *
PyObject *
_PyCode_GetCellvars(PyCodeObject *co)
{
- return get_localsplus_names(co, CO_FAST_CELL, co->co_ncellvars);
+ if (init_co_cached(co)) {
+ return NULL;
+ }
+ return get_cached_locals(co, &co->_co_cached->_co_cellvars, CO_FAST_CELL, co->co_ncellvars);
}
PyObject *
PyObject *
_PyCode_GetFreevars(PyCodeObject *co)
{
- return get_localsplus_names(co, CO_FAST_FREE, co->co_nfreevars);
+ if (init_co_cached(co)) {
+ return NULL;
+ }
+ return get_cached_locals(co, &co->_co_cached->_co_freevars, CO_FAST_FREE, co->co_nfreevars);
}
PyObject *
PyObject *
_PyCode_GetCode(PyCodeObject *co)
{
- if (co->_co_code != NULL) {
- return Py_NewRef(co->_co_code);
+ if (init_co_cached(co)) {
+ return NULL;
+ }
+ if (co->_co_cached->_co_code != NULL) {
+ return Py_NewRef(co->_co_cached->_co_code);
}
PyObject *code = PyBytes_FromStringAndSize((const char *)_PyCode_CODE(co),
_PyCode_NBYTES(co));
return NULL;
}
deopt_code((_Py_CODEUNIT *)PyBytes_AS_STRING(code), Py_SIZE(co));
- assert(co->_co_code == NULL);
- co->_co_code = Py_NewRef(code);
+ assert(co->_co_cached->_co_code == NULL);
+ co->_co_cached->_co_code = Py_NewRef(code);
return code;
}
Py_XDECREF(co->co_qualname);
Py_XDECREF(co->co_linetable);
Py_XDECREF(co->co_exceptiontable);
- Py_XDECREF(co->_co_code);
+ if (co->_co_cached != NULL) {
+ Py_XDECREF(co->_co_cached->_co_code);
+ Py_XDECREF(co->_co_cached->_co_cellvars);
+ Py_XDECREF(co->_co_cached->_co_freevars);
+ Py_XDECREF(co->_co_cached->_co_varnames);
+ PyMem_Free(co->_co_cached);
+ }
if (co->co_weakreflist != NULL) {
PyObject_ClearWeakRefs((PyObject*)co);
}
deopt_code(_PyCode_CODE(co), Py_SIZE(co));
co->co_warmup = QUICKENING_INITIAL_WARMUP_VALUE;
PyMem_Free(co->co_extra);
- Py_CLEAR(co->_co_code);
+ if (co->_co_cached != NULL) {
+ Py_CLEAR(co->_co_cached->_co_code);
+ Py_CLEAR(co->_co_cached->_co_cellvars);
+ Py_CLEAR(co->_co_cached->_co_freevars);
+ Py_CLEAR(co->_co_cached->_co_varnames);
+ PyMem_Free(co->_co_cached);
+ }
co->co_extra = NULL;
if (co->co_weakreflist != NULL) {
PyObject_ClearWeakRefs((PyObject *)co);