Skip to content

Commit f8c2e01

Browse files
committed
Step 3: Add a way to request dehydrated code objects
Disable 'refs' in marshal. (There's no way to rehydrate yet, so this crashes when calling anything.)
1 parent b567e53 commit f8c2e01

File tree

7 files changed

+8846
-7435
lines changed

7 files changed

+8846
-7435
lines changed

Objects/codeobject.c

+42-19
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ intern_strings(PyObject *tuple)
3838
{
3939
Py_ssize_t i;
4040

41+
if (tuple == NULL)
42+
return 0;
43+
4144
for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
4245
PyObject *v = PyTuple_GET_ITEM(tuple, i);
4346
if (v == NULL || !PyUnicode_CheckExact(v)) {
@@ -54,6 +57,9 @@ intern_strings(PyObject *tuple)
5457
static int
5558
intern_string_constants(PyObject *tuple, int *modified)
5659
{
60+
if (tuple == NULL)
61+
return 0;
62+
5763
for (Py_ssize_t i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
5864
PyObject *v = PyTuple_GET_ITEM(tuple, i);
5965
if (PyUnicode_CheckExact(v)) {
@@ -292,10 +298,25 @@ _PyCode_Validate(struct _PyCodeConstructor *con)
292298
static void
293299
init_code(PyCodeObject *co, struct _PyCodeConstructor *con)
294300
{
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);
301+
if (con->localsplusnames) {
302+
int nlocalsplus = (int)PyTuple_GET_SIZE(con->localsplusnames);
303+
int nlocals, nplaincellvars, ncellvars, nfreevars;
304+
get_localsplus_counts(con->localsplusnames, con->localspluskinds,
305+
&nlocals, &nplaincellvars, &ncellvars, &nfreevars);
306+
co->co_nlocalsplus = nlocalsplus;
307+
co->co_nlocals = nlocals;
308+
co->co_nplaincellvars = nplaincellvars;
309+
co->co_ncellvars = ncellvars;
310+
co->co_nfreevars = nfreevars;
311+
}
312+
else {
313+
// These will be set by hydration
314+
co->co_nlocalsplus = 0;
315+
co->co_nlocals = 0;
316+
co->co_nplaincellvars = 0;
317+
co->co_ncellvars = 0;
318+
co->co_nfreevars = 0;
319+
}
299320

300321
Py_INCREF(con->filename);
301322
co->co_filename = con->filename;
@@ -305,25 +326,32 @@ init_code(PyCodeObject *co, struct _PyCodeConstructor *con)
305326
co->co_qualname = con->qualname;
306327
co->co_flags = con->flags;
307328

308-
Py_INCREF(con->code);
329+
Py_XINCREF(con->code);
309330
co->co_code = con->code;
310-
co->co_firstinstr = (_Py_CODEUNIT *)PyBytes_AS_STRING(con->code);
331+
if (con->code) {
332+
co->co_firstinstr = (_Py_CODEUNIT *)PyBytes_AS_STRING(con->code);
333+
}
334+
else {
335+
co->co_firstinstr = 0; // Will be set by hydration
336+
}
311337
co->co_firstlineno = con->firstlineno;
312-
Py_INCREF(con->linetable);
338+
339+
// These may be NULL, and set by hydration
340+
Py_XINCREF(con->linetable);
313341
co->co_linetable = con->linetable;
314-
Py_INCREF(con->endlinetable);
342+
Py_XINCREF(con->endlinetable);
315343
co->co_endlinetable = con->endlinetable;
316-
Py_INCREF(con->columntable);
344+
Py_XINCREF(con->columntable);
317345
co->co_columntable = con->columntable;
318346

319-
Py_INCREF(con->consts);
347+
Py_XINCREF(con->consts);
320348
co->co_consts = con->consts;
321-
Py_INCREF(con->names);
349+
Py_XINCREF(con->names);
322350
co->co_names = con->names;
323351

324-
Py_INCREF(con->localsplusnames);
352+
Py_XINCREF(con->localsplusnames);
325353
co->co_localsplusnames = con->localsplusnames;
326-
Py_INCREF(con->localspluskinds);
354+
Py_XINCREF(con->localspluskinds);
327355
co->co_localspluskinds = con->localspluskinds;
328356

329357
co->co_argcount = con->argcount;
@@ -332,15 +360,10 @@ init_code(PyCodeObject *co, struct _PyCodeConstructor *con)
332360

333361
co->co_stacksize = con->stacksize;
334362

335-
Py_INCREF(con->exceptiontable);
363+
Py_XINCREF(con->exceptiontable);
336364
co->co_exceptiontable = con->exceptiontable;
337365

338366
/* 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;
344367
co->co_varnames = NULL;
345368
co->co_cellvars = NULL;
346369
co->co_freevars = NULL;

Python/ceval.c

+2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
/* enable more aggressive intra-module optimizations, where available */
99
/* affects both release and debug builds - see bpo-43271 */
10+
#ifdef NDEBUG
1011
#define PY_LOCAL_AGGRESSIVE
12+
#endif
1113

1214
#include "Python.h"
1315
#include "pycore_abstract.h" // _PyIndex_Check()

Python/frozen_hello.h

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
/* Auto-generated by Programs/_freeze_importlib.c */
22
const unsigned char _Py_M__hello[] = {
3-
99,171,0,0,0,5,0,0,0,0,0,0,0,0,0,0,
3+
99,180,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
44
0,0,0,0,0,2,0,0,0,0,0,0,0,1,0,0,
5-
0,218,8,60,109,111,100,117,108,101,62,114,0,0,0,0,
6-
122,14,60,102,114,111,122,101,110,32,104,101,108,108,111,62,
7-
115,16,0,0,0,100,0,90,0,101,1,100,1,131,1,1,
8-
0,100,2,83,0,41,3,84,122,12,72,101,108,108,111,32,
9-
119,111,114,108,100,33,78,41,2,90,11,105,110,105,116,105,
10-
97,108,105,122,101,100,218,5,112,114,105,110,116,169,0,243,
11-
0,0,0,0,243,4,0,0,0,4,0,12,1,114,4,0,
12-
0,0,115,16,0,0,0,15,19,1,12,1,6,7,21,1,
13-
22,1,22,1,22,1,22,114,3,0,0,0,
5+
0,90,8,60,109,111,100,117,108,101,62,90,8,60,109,111,
6+
100,117,108,101,62,122,14,60,102,114,111,122,101,110,32,104,
7+
101,108,108,111,62,115,16,0,0,0,100,0,90,0,101,1,
8+
100,1,131,1,1,0,100,2,83,0,41,3,84,122,12,72,
9+
101,108,108,111,32,119,111,114,108,100,33,78,41,2,90,11,
10+
105,110,105,116,105,97,108,105,122,101,100,90,5,112,114,105,
11+
110,116,41,0,115,0,0,0,0,115,4,0,0,0,4,0,
12+
12,1,115,4,0,0,0,4,0,12,1,115,16,0,0,0,
13+
15,19,1,12,1,6,7,21,1,22,1,22,1,22,1,22,
14+
115,0,0,0,0,
1415
};

0 commit comments

Comments
 (0)