bpo-43693: Turn localspluskinds into an object (GH-26749)

Managing it as a bare pointer to malloc'ed bytes is just too awkward in a few places.
This commit is contained in:
Guido van Rossum 2021-06-21 13:53:04 -07:00 committed by GitHub
parent c5d700f0e2
commit 355f5dd36a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 5455 additions and 5395 deletions

View file

@ -7187,15 +7187,13 @@ merge_const_one(struct compiler *c, PyObject **obj)
}
// This is in codeobject.c.
extern void _Py_set_localsplus_info(int, PyObject *, _PyLocalsPlusKind,
PyObject *, _PyLocalsPlusKinds);
extern void _Py_set_localsplus_info(int, PyObject *, unsigned char,
PyObject *, PyObject *);
static void
compute_localsplus_info(struct compiler *c, int nlocalsplus,
PyObject *names, _PyLocalsPlusKinds kinds)
PyObject *names, PyObject *kinds)
{
assert(PyTuple_GET_SIZE(names) == nlocalsplus);
PyObject *k, *v;
Py_ssize_t pos = 0;
while (PyDict_Next(c->u->u_varnames, &pos, &k, &v)) {
@ -7203,7 +7201,7 @@ compute_localsplus_info(struct compiler *c, int nlocalsplus,
assert(offset >= 0);
assert(offset < nlocalsplus);
// For now we do not distinguish arg kinds.
_PyLocalsPlusKind kind = CO_FAST_LOCAL;
_PyLocals_Kind kind = CO_FAST_LOCAL;
if (PyDict_GetItem(c->u->u_cellvars, k) != NULL) {
kind |= CO_FAST_CELL;
}
@ -7245,7 +7243,7 @@ makecode(struct compiler *c, struct assembler *a, PyObject *constslist,
PyObject *names = NULL;
PyObject *consts = NULL;
PyObject *localsplusnames = NULL;
_PyLocalsPlusKinds localspluskinds = NULL;
PyObject *localspluskinds = NULL;
PyObject *name = NULL;
names = dict_keys_inorder(c->u->u_names, 0);
@ -7281,7 +7279,8 @@ makecode(struct compiler *c, struct assembler *a, PyObject *constslist,
if (localsplusnames == NULL) {
goto error;
}
if (_PyCode_InitLocalsPlusKinds(nlocalsplus, &localspluskinds) < 0) {
localspluskinds = PyBytes_FromStringAndSize(NULL, nlocalsplus);
if (localspluskinds == NULL) {
goto error;
}
compute_localsplus_info(c, nlocalsplus, localsplusnames, localspluskinds);
@ -7315,7 +7314,6 @@ makecode(struct compiler *c, struct assembler *a, PyObject *constslist,
}
if (!merge_const_one(c, &localsplusnames)) {
_PyCode_ClearLocalsPlusKinds(con.localspluskinds);
goto error;
}
con.localsplusnames = localsplusnames;
@ -7325,13 +7323,11 @@ makecode(struct compiler *c, struct assembler *a, PyObject *constslist,
goto error;
}
localspluskinds = NULL; // This keeps it from getting freed below.
error:
Py_XDECREF(names);
Py_XDECREF(consts);
Py_XDECREF(localsplusnames);
_PyCode_ClearLocalsPlusKinds(localspluskinds);
Py_XDECREF(localspluskinds);
Py_XDECREF(name);
return co;
}

View file

@ -5,7 +5,8 @@ const unsigned char _Py_M__hello[] = {
100,1,131,1,1,0,100,2,83,0,41,3,84,122,12,72,
101,108,108,111,32,119,111,114,108,100,33,78,41,2,90,11,
105,110,105,116,105,97,108,105,122,101,100,218,5,112,114,105,
110,116,169,0,122,14,60,102,114,111,122,101,110,32,104,101,
108,108,111,62,218,8,60,109,111,100,117,108,101,62,1,0,
0,0,115,4,0,0,0,4,0,12,1,243,0,0,0,0,
110,116,169,0,243,0,0,0,0,122,14,60,102,114,111,122,
101,110,32,104,101,108,108,111,62,218,8,60,109,111,100,117,
108,101,62,1,0,0,0,115,4,0,0,0,4,0,12,1,
114,2,0,0,0,
};

3525
Python/importlib.h generated

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -519,7 +519,7 @@ w_complex_object(PyObject *v, char flag, WFILE *p)
w_object(co->co_consts, p);
w_object(co->co_names, p);
w_object(co->co_localsplusnames, p);
w_string(co->co_localspluskinds, co->co_nlocalsplus, p);
w_object(co->co_localspluskinds, p);
w_object(co->co_filename, p);
w_object(co->co_name, p);
w_long(co->co_firstlineno, p);
@ -1306,7 +1306,7 @@ r_object(RFILE *p)
PyObject *consts = NULL;
PyObject *names = NULL;
PyObject *localsplusnames = NULL;
_PyLocalsPlusKinds localspluskinds = NULL;
PyObject *localspluskinds = NULL;
PyObject *filename = NULL;
PyObject *name = NULL;
int firstlineno;
@ -1348,19 +1348,9 @@ r_object(RFILE *p)
localsplusnames = r_object(p);
if (localsplusnames == NULL)
goto code_error;
assert(PyTuple_GET_SIZE(localsplusnames) < INT_MAX);
int nlocalsplus = (int)PyTuple_GET_SIZE(localsplusnames);
if (nlocalsplus) {
if (_PyCode_InitLocalsPlusKinds(nlocalsplus,
&localspluskinds) < 0) {
goto code_error;
}
for (int i = 0; i < nlocalsplus; i++) {
localspluskinds[i] = r_byte(p);
}
}
localspluskinds = r_object(p);
if (localspluskinds == NULL)
goto code_error;
filename = r_object(p);
if (filename == NULL)
goto code_error;
@ -1377,6 +1367,7 @@ r_object(RFILE *p)
if (exceptiontable == NULL)
goto code_error;
Py_ssize_t nlocalsplus = PyTuple_GET_SIZE(localsplusnames);
if (PySys_Audit("code.__new__", "OOOiiiiii",
code, filename, name, argcount, posonlyargcount,
kwonlyargcount, nlocalsplus, stacksize,
@ -1417,8 +1408,6 @@ r_object(RFILE *p)
goto code_error;
}
localspluskinds = NULL; // This keeps it from getting freed below.
v = r_ref_insert(v, idx, flag, p);
code_error:
@ -1426,7 +1415,7 @@ r_object(RFILE *p)
Py_XDECREF(consts);
Py_XDECREF(names);
Py_XDECREF(localsplusnames);
_PyCode_ClearLocalsPlusKinds(localspluskinds);
Py_XDECREF(localspluskinds);
Py_XDECREF(filename);
Py_XDECREF(name);
Py_XDECREF(linetable);