Skip to content

Commit 82e5bcc

Browse files
Subbarao Garlapatimeta-codesync[bot]
authored andcommitted
Add Lazy Imports to 3.14
Summary: Add Lazy Imports to Python 3.14. This Diff is the result of squashing this stack - D82821767. __**File Regeneration**__ `./configure && make clean && make regen-all` `./third-party/python/3.14/build/gen-checked-in-artifacts.sh` `./third-party/python/3.14/build/gen-cinderx.sh` __**Patch Details**__ This diff adds a patch to the `3.14` Meta-internal fork. Lazy Imports 3.14 Reviewed By: itamaro Differential Revision: D83595369 fbshipit-source-id: 02361bc1b2503c8afdef6081956163bca3a65311
1 parent 8fb00a4 commit 82e5bcc

File tree

117 files changed

+4189
-136
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

117 files changed

+4189
-136
lines changed

Include/Python.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
#include "rangeobject.h"
9797
#include "memoryobject.h"
9898
#include "tupleobject.h"
99+
#include "lazyimportobject.h"
99100
#include "listobject.h"
100101
#include "dictobject.h"
101102
#include "cpython/odictobject.h"

Include/cpython/compile.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,16 @@
2020
#define PyCF_ALLOW_TOP_LEVEL_AWAIT 0x2000
2121
#define PyCF_ALLOW_INCOMPLETE_INPUT 0x4000
2222
#define PyCF_OPTIMIZED_AST (0x8000 | PyCF_ONLY_AST)
23+
#ifdef ENABLE_LAZY_IMPORTS
24+
#define PyCF_DISABLE_LAZY_IMPORTS 0x10000
25+
#define PyCF_COMPILE_MASK (PyCF_ONLY_AST | PyCF_ALLOW_TOP_LEVEL_AWAIT | \
26+
PyCF_TYPE_COMMENTS | PyCF_DONT_IMPLY_DEDENT | \
27+
PyCF_ALLOW_INCOMPLETE_INPUT | PyCF_OPTIMIZED_AST | PyCF_DISABLE_LAZY_IMPORTS)
28+
#else
2329
#define PyCF_COMPILE_MASK (PyCF_ONLY_AST | PyCF_ALLOW_TOP_LEVEL_AWAIT | \
2430
PyCF_TYPE_COMMENTS | PyCF_DONT_IMPLY_DEDENT | \
2531
PyCF_ALLOW_INCOMPLETE_INPUT | PyCF_OPTIMIZED_AST)
32+
#endif
2633

2734
typedef struct {
2835
int cf_flags; /* bitmask of CO_xxx flags relevant to future */

Include/cpython/dictobject.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ Py_DEPRECATED(3.14) PyAPI_FUNC(PyObject *) _PyDict_Pop(
7575
PyObject *key,
7676
PyObject *default_value);
7777

78+
#ifdef ENABLE_LAZY_IMPORTS
79+
PyAPI_FUNC(int) _PyDict_NextKeepLazy(
80+
PyObject *mp, Py_ssize_t *pos, PyObject **key, PyObject **value);
81+
PyAPI_FUNC(int) PyDict_NextWithError(
82+
PyObject *mp, Py_ssize_t *pos, PyObject **key, PyObject **value);
83+
PyAPI_FUNC(Py_ssize_t) PyDict_ResolveLazyImports(PyObject *);
84+
PyAPI_FUNC(int) PyDict_IsLazyImport(PyObject *mp, PyObject *name);
85+
PyAPI_FUNC(int) _PyDict_GetItemRefKeepLazy(PyObject *mp, PyObject *key, PyObject **result);
86+
#endif
87+
7888
/* Dictionary watchers */
7989

8090
#define PY_FOREACH_DICT_EVENT(V) \

Include/cpython/import.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22
# error "this header file must not be included directly"
33
#endif
44

5+
#ifdef ENABLE_LAZY_IMPORTS
6+
PyAPI_FUNC(PyObject *) _PyImport_GetModule(PyThreadState *tstate, PyObject *name);
7+
PyAPI_FUNC(PyObject *) PyImport_SetLazyImports(
8+
PyObject *enabled, PyObject *excluding, PyObject *eager);
9+
PyAPI_FUNC(PyObject *) _PyImport_SetLazyImportsInModule(
10+
PyObject *enabled);
11+
PyAPI_FUNC(int) _PyImport_IsLazyImportsActive(PyThreadState *tstate);
12+
PyAPI_FUNC(int) PyImport_IsLazyImportsEnabled(void);
13+
#endif
14+
515
struct _inittab {
616
const char *name; /* ASCII encoded string */
717
PyObject* (*initfunc)(void);

Include/cpython/initconfig.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@ typedef struct PyConfig {
241241
// PYTHON_PRESITE=package.module or -X presite=package.module
242242
wchar_t *run_presite;
243243
#endif
244+
245+
#ifdef ENABLE_LAZY_IMPORTS
246+
int lazy_imports;
247+
#endif
244248
} PyConfig;
245249

246250
PyAPI_FUNC(void) PyConfig_InitPythonConfig(PyConfig *config);

Include/cpython/pyerrors.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,7 @@ PyAPI_FUNC(void) PyErr_FormatUnraisable(const char *, ...);
130130
PyAPI_DATA(PyObject *) PyExc_PythonFinalizationError;
131131

132132
#define Py_FatalError(message) _Py_FatalErrorFunc(__func__, (message))
133+
134+
#ifdef ENABLE_LAZY_IMPORTS
135+
PyAPI_DATA(PyObject *) PyExc_ImportCycleError;
136+
#endif

Include/internal/pycore_ceval.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ extern void _PyEval_Fini(void);
8686

8787

8888
extern PyObject* _PyEval_GetBuiltins(PyThreadState *tstate);
89+
#ifdef ENABLE_LAZY_IMPORTS
90+
extern PyObject* _PyEval_GetGlobals(PyThreadState *tstate);
91+
#endif
8992

9093
// Trampoline API
9194

Include/internal/pycore_compile.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ int _PyCompile_ResolveNameop(struct _PyCompiler *c, PyObject *mangled, int scope
150150
int _PyCompile_IsInteractiveTopLevel(struct _PyCompiler *c);
151151
int _PyCompile_IsInInlinedComp(struct _PyCompiler *c);
152152
int _PyCompile_ScopeType(struct _PyCompiler *c);
153+
#ifdef ENABLE_LAZY_IMPORTS
154+
int _PyCompile_NFBlocks(struct _PyCompiler *c);
155+
int _PyCompile_CFFlags(struct _PyCompiler *c);
156+
#endif
153157
int _PyCompile_OptimizationLevel(struct _PyCompiler *c);
154158
int _PyCompile_LookupArg(struct _PyCompiler *c, PyCodeObject *co, PyObject *name);
155159
PyObject *_PyCompile_Qualname(struct _PyCompiler *c);

Include/internal/pycore_dict.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ extern void _PyDictKeys_DecRef(PyDictKeysObject *keys);
110110
* -1 when no entry found, -3 when compare raises error.
111111
*/
112112
extern Py_ssize_t _Py_dict_lookup(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject **value_addr);
113+
114+
#ifdef ENABLE_LAZY_IMPORTS
115+
/* _Py_dict_lookup_keep_lazy() is the same as _Py_dict_lookup(), but keeps lazy objects unresolved */
116+
extern Py_ssize_t _Py_dict_lookup_keep_lazy(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject **value_addr);
117+
extern int _PyDict_GetItemRefKeepLazy(PyObject *, PyObject *, PyObject **);
118+
#endif
119+
113120
extern Py_ssize_t _Py_dict_lookup_threadsafe(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject **value_addr);
114121
extern Py_ssize_t _Py_dict_lookup_threadsafe_stackref(PyDictObject *mp, PyObject *key, Py_hash_t hash, _PyStackRef *value_addr);
115122

@@ -160,6 +167,9 @@ PyAPI_FUNC(void) _PyDict_EnsureSharedOnRead(PyDictObject *mp);
160167
#define DKIX_DUMMY (-2) /* Used internally */
161168
#define DKIX_ERROR (-3)
162169
#define DKIX_KEY_CHANGED (-4) /* Used internally */
170+
#ifdef ENABLE_LAZY_IMPORTS
171+
#define DKIX_VALUE_ERROR (-5) /* Used internally */
172+
#endif
163173

164174
typedef enum {
165175
DICT_KEYS_GENERAL = 0,
@@ -178,7 +188,14 @@ struct _dictkeysobject {
178188
uint8_t dk_log2_index_bytes;
179189

180190
/* Kind of keys */
191+
#ifdef ENABLE_LAZY_IMPORTS
192+
uint8_t dk_kind: 7;
193+
194+
/* Contains lazy imports. Assume there are lazy import objects unless otherwise specified. */
195+
uint8_t dk_lazy_imports : 1;
196+
#else
181197
uint8_t dk_kind;
198+
#endif
182199

183200
#ifdef Py_GIL_DISABLED
184201
/* Lock used to protect shared keys */

Include/internal/pycore_global_objects_fini_generated.h

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)