Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
forked frompython/cpython

Commit350c456

Browse files
committed
Lazy imports
1 parent4b81139 commit350c456

File tree

125 files changed

+2787
-255
lines changed

Some content is hidden

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

125 files changed

+2787
-255
lines changed

‎Doc/data/stable_abi.dat‎

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

‎Doc/library/dis.rst‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,16 @@ iterations of the loop.
973973
modifies the namespace.
974974

975975

976+
..opcode::EAGER_IMPORT_NAME (namei)
977+
978+
Imports the module ``co_names[namei]`` eagerly (i.e. even if lazy imports are
979+
enabled, the module is imported immediately.) TOS and TOS1 are popped and
980+
provide the *fromlist* and *level* arguments of:func:`__import__`. The
981+
module object is pushed onto the stack. The current namespace is not
982+
affected: for a proper import statement, a subsequent:opcode:`STORE_FAST`
983+
instruction modifies the namespace.
984+
985+
976986
..opcode::IMPORT_FROM (namei)
977987

978988
Loads the attribute ``co_names[namei]`` from the module found in TOS. The

‎Doc/library/sys.rst‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ always available.
522522
:const:`utf8_mode`:option:`-X utf8 <-X>`
523523
:const:`safe_path`:option:`-P`
524524
:const:`int_max_str_digits`:option:`-X int_max_str_digits <-X>` (:ref:`integer string conversion length limitation<int_max_str_digits>`)
525+
:const:`lazy_imports`:option:`-L`
525526
============================= ==============================================================================================================
526527

527528
..versionchanged::3.2
@@ -547,6 +548,9 @@ always available.
547548
..versionchanged::3.11
548549
Added the ``int_max_str_digits`` attribute.
549550

551+
..versionchanged::3.12
552+
Added the ``lazy_imports`` attribute for:option:`-L` option.
553+
550554

551555
..data::float_info
552556

‎Doc/using/cmdline.rst‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,13 @@ Miscellaneous options
310310
..versionadded::3.4
311311

312312

313+
..cmdoption::-L
314+
315+
Enable lazy imports.
316+
317+
..versionadded::3.12
318+
319+
313320
..cmdoption::-O
314321

315322
Remove assert statements and any code conditional on the value of

‎Include/Python.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#include"rangeobject.h"
5858
#include"memoryobject.h"
5959
#include"tupleobject.h"
60+
#include"lazyimportobject.h"
6061
#include"listobject.h"
6162
#include"dictobject.h"
6263
#include"cpython/odictobject.h"

‎Include/cpython/import.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ PyMODINIT_FUNC PyInit__imp(void);
66

77
PyAPI_FUNC(int)_PyImport_IsInitialized(PyInterpreterState*);
88

9+
PyAPI_FUNC(PyObject*)_PyImport_GetModule(PyThreadState*tstate,PyObject*name);
910
PyAPI_FUNC(PyObject*)_PyImport_GetModuleId(_Py_Identifier*name);
1011
PyAPI_FUNC(int)_PyImport_SetModule(PyObject*name,PyObject*module);
1112
PyAPI_FUNC(int)_PyImport_SetModuleString(constchar*name,PyObject*module);
@@ -21,6 +22,8 @@ PyAPI_FUNC(int) _PyImport_FixupBuiltin(
2122
PyAPI_FUNC(int)_PyImport_FixupExtensionObject(PyObject*,PyObject*,
2223
PyObject*,PyObject*);
2324

25+
PyAPI_FUNC(int)_PyImport_IsLazyImportsEnabled(PyThreadState*tstate);
26+
2427
struct_inittab {
2528
constchar*name;/* ASCII encoded string */
2629
PyObject* (*initfunc)(void);

‎Include/cpython/initconfig.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ typedef struct PyConfig {
178178
wchar_t*check_hash_pycs_mode;
179179
intuse_frozen_modules;
180180
intsafe_path;
181+
intlazy_imports;
181182

182183
/* --- Path configuration inputs ------------ */
183184
intpathconfig_warnings;

‎Include/dictobject.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,19 @@ PyAPI_FUNC(int) PyDict_DelItem(PyObject *mp, PyObject *key);
2626
PyAPI_FUNC(void)PyDict_Clear(PyObject*mp);
2727
PyAPI_FUNC(int)PyDict_Next(
2828
PyObject*mp,Py_ssize_t*pos,PyObject**key,PyObject**value);
29+
PyAPI_FUNC(int)PyDict_NextWithError(
30+
PyObject*mp,Py_ssize_t*pos,PyObject**key,PyObject**value);
2931
PyAPI_FUNC(PyObject*)PyDict_Keys(PyObject*mp);
3032
PyAPI_FUNC(PyObject*)PyDict_Values(PyObject*mp);
3133
PyAPI_FUNC(PyObject*)PyDict_Items(PyObject*mp);
3234
PyAPI_FUNC(Py_ssize_t)PyDict_Size(PyObject*mp);
3335
PyAPI_FUNC(PyObject*)PyDict_Copy(PyObject*mp);
3436
PyAPI_FUNC(int)PyDict_Contains(PyObject*mp,PyObject*key);
37+
PyAPI_FUNC(int)PyDict_IsLazyImport(PyObject*mp,PyObject*name);
38+
39+
/* Forces resolving all lazy objects in a dictionary */
40+
PyAPI_FUNC(Py_ssize_t)PyDict_ResolveLazyImports(PyObject*);
41+
3542

3643
/* PyDict_Update(mp, other) is equivalent to PyDict_Merge(mp, other, 1). */
3744
PyAPI_FUNC(int)PyDict_Update(PyObject*mp,PyObject*other);

‎Include/import.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ PyAPI_FUNC(int) PyImport_AppendInittab(
8686
PyObject* (*initfunc)(void)
8787
);
8888

89+
PyAPI_FUNC(int)PyImport_IsLazyImportsEnabled(void);
90+
PyAPI_FUNC(PyObject*)PyImport_SetLazyImports(
91+
PyObject*enabled,PyObject*excluding);
92+
PyAPI_FUNC(PyObject*)PyImport_SetLazyImportsInModule(
93+
PyObject*enabled);
94+
8995
#ifndefPy_LIMITED_API
9096
# definePy_CPYTHON_IMPORT_H
9197
# include"cpython/import.h"

‎Include/internal/pycore_dict.h‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,13 @@ extern Py_ssize_t _PyDict_KeysSize(PyDictKeysObject *keys);
6262
*/
6363
externPy_ssize_t_Py_dict_lookup(PyDictObject*mp,PyObject*key,Py_hash_thash,PyObject**value_addr);
6464

65+
/* _Py_dict_lookup_keep_lazy() is the same as _Py_dict_lookup(), but keeps lazy objects unresolved */
66+
externPy_ssize_t_Py_dict_lookup_keep_lazy(PyDictObject*mp,PyObject*key,Py_hash_thash,PyObject**value_addr);
67+
6568
externPy_ssize_t_PyDict_LookupIndex(PyDictObject*,PyObject*);
6669
externPy_ssize_t_PyDictKeys_StringLookup(PyDictKeysObject*dictkeys,PyObject*key);
6770
externPyObject*_PyDict_LoadGlobal(PyDictObject*,PyDictObject*,PyObject*);
71+
externPyObject*_PyDict_GetItemKeepLazy(PyObject*,PyObject*);
6872

6973
/* Consumes references to key and value */
7074
externint_PyDict_SetItem_Take2(PyDictObject*op,PyObject*key,PyObject*value);
@@ -76,6 +80,7 @@ extern PyObject *_PyDict_Pop_KnownHash(PyObject *, PyObject *, Py_hash_t, PyObje
7680
#defineDKIX_DUMMY (-2)/* Used internally */
7781
#defineDKIX_ERROR (-3)
7882
#defineDKIX_KEY_CHANGED (-4)/* Used internally */
83+
#defineDKIX_VALUE_ERROR (-5)/* Used internally */
7984

8085
typedefenum {
8186
DICT_KEYS_GENERAL=0,
@@ -94,7 +99,10 @@ struct _dictkeysobject {
9499
uint8_tdk_log2_index_bytes;
95100

96101
/* Kind of keys */
97-
uint8_tdk_kind;
102+
uint8_tdk_kind :7;
103+
104+
/* Contains lazy imports */
105+
uint8_tdk_lazy_imports :1;
98106

99107
/* Version number -- Reset to 0 by any modification to keys */
100108
uint32_tdk_version;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp