Export lazy_import in imp module

This commit is contained in:
Dino Viehland 2025-09-18 14:54:37 -07:00
parent 3b0d745e73
commit 9eef03cc80
3 changed files with 26 additions and 4 deletions

View file

@ -59,6 +59,8 @@
from ._bootstrap import __import__ from ._bootstrap import __import__
from _imp import lazy_import
def invalidate_caches(): def invalidate_caches():
"""Call the invalidate_caches() method on all meta path finders stored in """Call the invalidate_caches() method on all meta path finders stored in

View file

@ -1,6 +1,3 @@
/* Copyright (c) Meta, Inc. and its affiliates. All Rights Reserved */
/* File added for Lazy Imports */
/* Lazy object implementation */ /* Lazy object implementation */
#include "Python.h" #include "Python.h"
@ -86,6 +83,24 @@ lazy_import_clear(PyLazyImportObject *m)
return 0; return 0;
} }
static PyObject *
lazy_import_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
if (PyTuple_GET_SIZE(args) != 2 && PyTuple_GET_SIZE(args) != 3) {
PyErr_SetString(PyExc_ValueError, "lazy_import expected 2-3 arguments");
return NULL;
}
PyObject *builtins = PyTuple_GET_ITEM(args, 0);
PyObject *from = PyTuple_GET_ITEM(args, 1);
PyObject *attr = NULL;
if (PyTuple_GET_SIZE(args) == 3) {
attr = PyTuple_GET_ITEM(args, 2);
}
return _PyLazyImport_New(builtins, from, attr);
}
PyObject * PyObject *
_PyLazyImport_GetName(PyObject *lazy_import) _PyLazyImport_GetName(PyObject *lazy_import)
{ {
@ -132,6 +147,6 @@ PyTypeObject PyLazyImport_Type = {
0, /* tp_dictoffset */ 0, /* tp_dictoffset */
0, /* tp_init */ 0, /* tp_init */
PyType_GenericAlloc, /* tp_alloc */ PyType_GenericAlloc, /* tp_alloc */
PyType_GenericNew, /* tp_new */ lazy_import_new, /* tp_new */
PyObject_GC_Del, /* tp_free */ PyObject_GC_Del, /* tp_free */
}; };

View file

@ -4980,6 +4980,11 @@ imp_module_exec(PyObject *module)
return -1; return -1;
} }
if (PyModule_AddObjectRef(module, "lazy_import",
(PyObject *)&PyLazyImport_Type) < 0) {
return -1;
}
return 0; return 0;
} }