Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions Lib/ctypes/test/test_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@ class struct_frozen(Structure):
# found, but don't worry about its size too much. The same
# applies to _frozen_importlib_external.
bootstrap_seen = []
bootstrap_expected = [
b'_frozen_importlib',
b'_frozen_importlib_external',
]
bootstrap_expected = []
for entry in ft:
# This is dangerous. We *can* iterate over a pointer, but
# the loop will not terminate (maybe with an access
Expand Down
8 changes: 8 additions & 0 deletions Makefile.pre.in
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ PYTHON_OBJS= \
Python/symtable.o \
Python/sysmodule.o \
Python/thread.o \
Python/frozenmodules.o \
Python/traceback.o \
Python/getopt.o \
Python/pystrcmp.o \
Expand Down Expand Up @@ -786,6 +787,12 @@ Python/sysmodule.o: $(srcdir)/Python/sysmodule.c Makefile
$(MULTIARCH_CPPFLAGS) \
-o $@ $(srcdir)/Python/sysmodule.c

Python/frozenmodules.o: $(srcdir)/Python/frozenmodules.c $(srcdir)/Python/frozenmodules.h Makefile
$(CC) -c $(PY_CORE_CFLAGS) \
-DABIFLAGS='"$(ABIFLAGS)"' \
$(MULTIARCH_CPPFLAGS) \
-o $@ $(srcdir)/Python/frozenmodules.c

$(IO_OBJS): $(IO_H)

$(PGEN): $(PGENOBJS)
Expand Down Expand Up @@ -1026,6 +1033,7 @@ PYTHON_HEADERS= \
$(srcdir)/Include/internal/pystate.h \
$(srcdir)/Include/internal/context.h \
$(srcdir)/Include/internal/warnings.h \
$(srcdir)/Python/frozenmodules.h \
$(DTRACE_HEADERS)

$(LIBRARY_OBJS) $(MODOBJS) Programs/python.o: $(PYTHON_HEADERS)
Expand Down
6 changes: 0 additions & 6 deletions Python/frozen.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
/* Dummy frozen modules initializer */

#include "Python.h"
#include "importlib.h"
#include "importlib_external.h"

/* In order to test the support for frozen modules, by default we
define a single frozen module, __hello__. Loading it will print
Expand All @@ -28,10 +26,6 @@ static unsigned char M___hello__[] = {
#define SIZE (int)sizeof(M___hello__)

static const struct _frozen _PyImport_FrozenModules[] = {
/* importlib */
{"_frozen_importlib", _Py_M__importlib, (int)sizeof(_Py_M__importlib)},
{"_frozen_importlib_external", _Py_M__importlib_external,
(int)sizeof(_Py_M__importlib_external)},
/* Test module */
{"__hello__", M___hello__, SIZE},
/* Test package (negative size indicates package-ness) */
Expand Down
42,618 changes: 42,618 additions & 0 deletions Python/frozenmodules.c

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions Python/frozenmodules.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* Frozen python modules */

#ifndef Py_CHILLEDMODULES_H
#define Py_CHILLEDMODULES_H
#ifdef __cplusplus
extern "C" {
#endif


extern void _PyFrozenModules_Init(void);
extern PyObject* _PyFrozenModule_Lookup(PyObject* name);
extern void _PyFrozenModules_Finalize(void);
extern int _PyFrozenModules_ImportBootstrap(void);
extern void _PyFrozenModules_Disable(void);
extern void _PyFrozenModules_Enable(void);

#ifdef __cplusplus
}
#endif
#endif /* !Py_CHILLEDMODULES_H */
10 changes: 10 additions & 0 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
#include "osdefs.h"
#include "importdl.h"
#include "pydtrace.h"
#include "frozenmodules.h"

#include <stdbool.h>
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
Expand Down Expand Up @@ -1682,6 +1684,7 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
PyObject *package = NULL;
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
int has_from;
_Bool from_frozen_module = false;

if (name == NULL) {
PyErr_SetString(PyExc_ValueError, "Empty module name");
Expand Down Expand Up @@ -1718,6 +1721,10 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
}

mod = PyImport_GetModule(abs_name);
if (mod == NULL) {
mod = _PyFrozenModule_Lookup(abs_name);
from_frozen_module = true;
}
if (mod != NULL && mod != Py_None) {
_Py_IDENTIFIER(__spec__);
_Py_IDENTIFIER(_initializing);
Expand All @@ -1726,6 +1733,9 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
PyObject *spec;
int initializing = 0;

if (!from_frozen_module)
Py_INCREF(mod);

/* Optimization: only call _bootstrap._lock_unlock_module() if
__spec__._initializing is true.
NOTE: because of this, initializing must be set *before*
Expand Down
18 changes: 16 additions & 2 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include "osdefs.h"
#include <locale.h>

#include "frozenmodules.h"

#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif
Expand All @@ -35,6 +37,7 @@
#undef BYTE
#include "windows.h"


extern PyTypeObject PyWindowsConsoleIO_Type;
#define PyWindowsConsoleIO_Check(op) (PyObject_TypeCheck((op), &PyWindowsConsoleIO_Type))
#endif
Expand Down Expand Up @@ -193,8 +196,8 @@ initimport(PyInterpreterState *interp, PyObject *sysmod)
PyObject *value;
_PyInitError err;

/* Import _importlib through its frozen version, _frozen_importlib. */
if (PyImport_ImportFrozenModule("_frozen_importlib") <= 0) {
/* Import _importlib through its frozen version */
if (_PyFrozenModules_ImportBootstrap() <= 0) {
return _Py_INIT_ERR("can't import _frozen_importlib");
}
else if (Py_VerboseFlag) {
Expand Down Expand Up @@ -675,6 +678,8 @@ _Py_InitializeCore_impl(PyInterpreterState **interp_p,
PySys_SetObject("__stderr__", pstderr);
Py_DECREF(pstderr);

_PyFrozenModules_Init();

err = _PyImport_Init(interp);
if (_Py_INIT_FAILED(err)) {
return err;
Expand Down Expand Up @@ -821,6 +826,8 @@ _Py_InitializeMainInterpreter(PyInterpreterState *interp,
return err;
}



/* initialize the faulthandler module */
err = _PyFaulthandler_Init(core_config->faulthandler);
if (_Py_INIT_FAILED(err)) {
Expand Down Expand Up @@ -1013,6 +1020,8 @@ Py_FinalizeEx(void)

wait_for_thread_shutdown();

_PyFrozenModules_Finalize();

/* Get current thread state and interpreter pointer */
tstate = PyThreadState_GET();
interp = tstate->interp;
Expand Down Expand Up @@ -1262,6 +1271,8 @@ new_interpreter(PyThreadState **tstate_p)

save_tstate = PyThreadState_Swap(tstate);

_PyFrozenModules_Disable();

/* Copy the current interpreter config into the new interpreter */
_PyCoreConfig *core_config;
_PyMainInterpreterConfig *config;
Expand Down Expand Up @@ -1366,12 +1377,15 @@ new_interpreter(PyThreadState **tstate_p)
goto handle_error;
}

_PyFrozenModules_Enable();
*tstate_p = tstate;
return _Py_INIT_OK();

handle_error:
/* Oops, it didn't work. Undo it all. */

_PyFrozenModules_Enable();

PyErr_PrintEx(0);
PyThreadState_Clear(tstate);
PyThreadState_Swap(save_tstate);
Expand Down
9 changes: 9 additions & 0 deletions frozen_module/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Frozen Modules patch for cPython 3.6

## Getting started

1. Get a clean checkout of Python 3.6 and build it.
2. Use either `patch` or `git apply` to apply the patchfile (`frozenmodules.patch`) onto the cPython checkout.
3. Run `setup.py install` (or `setup.py develop`) to install the helper module.
4. Run `python -m frozen_module freeze-startup-modules -o <path_to_cpython_checkout>/Python/frozenmodules.c <path_to_cpython_checkout>/python`. This generates a C file with all the startup modules baked in.
5. Build cPython again.
Loading