2014-05-30 12:21:14 -03:00
|
|
|
/*[clinic input]
|
|
|
|
preserve
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
PyDoc_STRVAR(_imp_lock_held__doc__,
|
|
|
|
"lock_held($module, /)\n"
|
|
|
|
"--\n"
|
|
|
|
"\n"
|
|
|
|
"Return True if the import lock is currently held, else False.\n"
|
|
|
|
"\n"
|
|
|
|
"On platforms without threads, return False.");
|
|
|
|
|
|
|
|
#define _IMP_LOCK_HELD_METHODDEF \
|
|
|
|
{"lock_held", (PyCFunction)_imp_lock_held, METH_NOARGS, _imp_lock_held__doc__},
|
|
|
|
|
|
|
|
static PyObject *
|
2016-07-07 11:35:15 -03:00
|
|
|
_imp_lock_held_impl(PyObject *module);
|
2014-05-30 12:21:14 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2016-07-07 11:35:15 -03:00
|
|
|
_imp_lock_held(PyObject *module, PyObject *Py_UNUSED(ignored))
|
2014-05-30 12:21:14 -03:00
|
|
|
{
|
|
|
|
return _imp_lock_held_impl(module);
|
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(_imp_acquire_lock__doc__,
|
|
|
|
"acquire_lock($module, /)\n"
|
|
|
|
"--\n"
|
|
|
|
"\n"
|
|
|
|
"Acquires the interpreter\'s import lock for the current thread.\n"
|
|
|
|
"\n"
|
|
|
|
"This lock should be used by import hooks to ensure thread-safety when importing\n"
|
|
|
|
"modules. On platforms without threads, this function does nothing.");
|
|
|
|
|
|
|
|
#define _IMP_ACQUIRE_LOCK_METHODDEF \
|
|
|
|
{"acquire_lock", (PyCFunction)_imp_acquire_lock, METH_NOARGS, _imp_acquire_lock__doc__},
|
|
|
|
|
|
|
|
static PyObject *
|
2016-07-07 11:35:15 -03:00
|
|
|
_imp_acquire_lock_impl(PyObject *module);
|
2014-05-30 12:21:14 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2016-07-07 11:35:15 -03:00
|
|
|
_imp_acquire_lock(PyObject *module, PyObject *Py_UNUSED(ignored))
|
2014-05-30 12:21:14 -03:00
|
|
|
{
|
|
|
|
return _imp_acquire_lock_impl(module);
|
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(_imp_release_lock__doc__,
|
|
|
|
"release_lock($module, /)\n"
|
|
|
|
"--\n"
|
|
|
|
"\n"
|
|
|
|
"Release the interpreter\'s import lock.\n"
|
|
|
|
"\n"
|
|
|
|
"On platforms without threads, this function does nothing.");
|
|
|
|
|
|
|
|
#define _IMP_RELEASE_LOCK_METHODDEF \
|
|
|
|
{"release_lock", (PyCFunction)_imp_release_lock, METH_NOARGS, _imp_release_lock__doc__},
|
|
|
|
|
|
|
|
static PyObject *
|
2016-07-07 11:35:15 -03:00
|
|
|
_imp_release_lock_impl(PyObject *module);
|
2014-05-30 12:21:14 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2016-07-07 11:35:15 -03:00
|
|
|
_imp_release_lock(PyObject *module, PyObject *Py_UNUSED(ignored))
|
2014-05-30 12:21:14 -03:00
|
|
|
{
|
|
|
|
return _imp_release_lock_impl(module);
|
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(_imp__fix_co_filename__doc__,
|
|
|
|
"_fix_co_filename($module, code, path, /)\n"
|
|
|
|
"--\n"
|
|
|
|
"\n"
|
|
|
|
"Changes code.co_filename to specify the passed-in file path.\n"
|
|
|
|
"\n"
|
|
|
|
" code\n"
|
|
|
|
" Code object to change.\n"
|
|
|
|
" path\n"
|
|
|
|
" File path to use.");
|
|
|
|
|
|
|
|
#define _IMP__FIX_CO_FILENAME_METHODDEF \
|
2018-11-27 05:27:36 -04:00
|
|
|
{"_fix_co_filename", (PyCFunction)(void(*)(void))_imp__fix_co_filename, METH_FASTCALL, _imp__fix_co_filename__doc__},
|
2014-05-30 12:21:14 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2016-07-07 11:35:15 -03:00
|
|
|
_imp__fix_co_filename_impl(PyObject *module, PyCodeObject *code,
|
2015-04-14 19:07:59 -03:00
|
|
|
PyObject *path);
|
2014-05-30 12:21:14 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2017-12-15 07:11:11 -04:00
|
|
|
_imp__fix_co_filename(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
|
2014-05-30 12:21:14 -03:00
|
|
|
{
|
|
|
|
PyObject *return_value = NULL;
|
|
|
|
PyCodeObject *code;
|
|
|
|
PyObject *path;
|
|
|
|
|
2019-01-11 10:01:14 -04:00
|
|
|
if (!_PyArg_CheckPositional("_fix_co_filename", nargs, 2, 2)) {
|
2017-01-16 20:35:17 -04:00
|
|
|
goto exit;
|
|
|
|
}
|
2019-01-11 10:01:14 -04:00
|
|
|
if (!PyObject_TypeCheck(args[0], &PyCode_Type)) {
|
2019-08-29 11:49:08 -03:00
|
|
|
_PyArg_BadArgument("_fix_co_filename", "argument 1", (&PyCode_Type)->tp_name, args[0]);
|
2019-01-11 10:01:14 -04:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
code = (PyCodeObject *)args[0];
|
|
|
|
if (!PyUnicode_Check(args[1])) {
|
2019-08-29 11:49:08 -03:00
|
|
|
_PyArg_BadArgument("_fix_co_filename", "argument 2", "str", args[1]);
|
2019-01-11 10:01:14 -04:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
if (PyUnicode_READY(args[1]) == -1) {
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
path = args[1];
|
2014-05-30 12:21:14 -03:00
|
|
|
return_value = _imp__fix_co_filename_impl(module, code, path);
|
|
|
|
|
|
|
|
exit:
|
|
|
|
return return_value;
|
|
|
|
}
|
|
|
|
|
2015-05-23 09:24:10 -03:00
|
|
|
PyDoc_STRVAR(_imp_create_builtin__doc__,
|
|
|
|
"create_builtin($module, spec, /)\n"
|
|
|
|
"--\n"
|
|
|
|
"\n"
|
|
|
|
"Create an extension module.");
|
|
|
|
|
|
|
|
#define _IMP_CREATE_BUILTIN_METHODDEF \
|
|
|
|
{"create_builtin", (PyCFunction)_imp_create_builtin, METH_O, _imp_create_builtin__doc__},
|
|
|
|
|
2014-05-30 12:21:14 -03:00
|
|
|
PyDoc_STRVAR(_imp_extension_suffixes__doc__,
|
|
|
|
"extension_suffixes($module, /)\n"
|
|
|
|
"--\n"
|
|
|
|
"\n"
|
|
|
|
"Returns the list of file suffixes used to identify extension modules.");
|
|
|
|
|
|
|
|
#define _IMP_EXTENSION_SUFFIXES_METHODDEF \
|
|
|
|
{"extension_suffixes", (PyCFunction)_imp_extension_suffixes, METH_NOARGS, _imp_extension_suffixes__doc__},
|
|
|
|
|
|
|
|
static PyObject *
|
2016-07-07 11:35:15 -03:00
|
|
|
_imp_extension_suffixes_impl(PyObject *module);
|
2014-05-30 12:21:14 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2016-07-07 11:35:15 -03:00
|
|
|
_imp_extension_suffixes(PyObject *module, PyObject *Py_UNUSED(ignored))
|
2014-05-30 12:21:14 -03:00
|
|
|
{
|
|
|
|
return _imp_extension_suffixes_impl(module);
|
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(_imp_init_frozen__doc__,
|
|
|
|
"init_frozen($module, name, /)\n"
|
|
|
|
"--\n"
|
|
|
|
"\n"
|
|
|
|
"Initializes a frozen module.");
|
|
|
|
|
|
|
|
#define _IMP_INIT_FROZEN_METHODDEF \
|
2015-04-03 18:12:11 -03:00
|
|
|
{"init_frozen", (PyCFunction)_imp_init_frozen, METH_O, _imp_init_frozen__doc__},
|
2014-05-30 12:21:14 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2016-07-07 11:35:15 -03:00
|
|
|
_imp_init_frozen_impl(PyObject *module, PyObject *name);
|
2014-05-30 12:21:14 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2016-07-07 11:35:15 -03:00
|
|
|
_imp_init_frozen(PyObject *module, PyObject *arg)
|
2014-05-30 12:21:14 -03:00
|
|
|
{
|
|
|
|
PyObject *return_value = NULL;
|
|
|
|
PyObject *name;
|
|
|
|
|
2018-12-25 07:23:47 -04:00
|
|
|
if (!PyUnicode_Check(arg)) {
|
2019-08-29 11:49:08 -03:00
|
|
|
_PyArg_BadArgument("init_frozen", "argument", "str", arg);
|
2014-05-30 12:21:14 -03:00
|
|
|
goto exit;
|
2016-06-09 10:16:06 -03:00
|
|
|
}
|
2018-12-25 07:23:47 -04:00
|
|
|
if (PyUnicode_READY(arg) == -1) {
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
name = arg;
|
2014-05-30 12:21:14 -03:00
|
|
|
return_value = _imp_init_frozen_impl(module, name);
|
|
|
|
|
|
|
|
exit:
|
|
|
|
return return_value;
|
|
|
|
}
|
|
|
|
|
2021-10-05 13:01:27 -03:00
|
|
|
PyDoc_STRVAR(_imp_find_frozen__doc__,
|
bpo-21736: Set __file__ on frozen stdlib modules. (gh-28656)
Currently frozen modules do not have __file__ set. In their spec, origin is set to "frozen" and they are marked as not having a location. (Similarly, for frozen packages __path__ is set to an empty list.) However, for frozen stdlib modules we are able to extrapolate __file__ as long as we can determine the stdlib directory at runtime. (We now do so since gh-28586.) Having __file__ set is helpful for a number of reasons. Likewise, having a non-empty __path__ means we can import submodules of a frozen package from the filesystem (e.g. we could partially freeze the encodings module).
This change sets __file__ (and adds to __path__) for frozen stdlib modules. It uses sys._stdlibdir (from gh-28586) and the frozen module alias information (from gh-28655). All that work is done in FrozenImporter (in Lib/importlib/_bootstrap.py).
Also, if a frozen module is imported before importlib is bootstrapped (during interpreter initialization) then we fix up that module and its spec during the importlib bootstrapping step (i.e. imporlib._bootstrap._setup()) to match what gets set by FrozenImporter, including setting the file info (if the stdlib dir is known). To facilitate this, modules imported using PyImport_ImportFrozenModule() have __origname__ set using the frozen module alias info. __origname__ is popped off during importlib bootstrap.
(To be clear, even with this change the new code to set __file__ during fixups in imporlib._bootstrap._setup() doesn't actually get triggered yet. This is because sys._stdlibdir hasn't been set yet in interpreter initialization at the point importlib is bootstrapped. However, we do fix up such modules at that point to otherwise match the result of importing through FrozenImporter, just not the __file__ and __path__ parts. Doing so will require changes in the order in which things happen during interpreter initialization. That can be addressed separately. Once it is, the file-related fixup code from this PR will kick in.)
Here are things this change does not do:
* set __file__ for non-stdlib modules (no way of knowing the parent dir)
* set __file__ if the stdlib dir is not known (nor assume the expense of finding it)
* relatedly, set __file__ if the stdlib is in a zip file
* verify that the filename set to __file__ actually exists (too expensive)
* update __path__ for frozen packages that alias a non-package (since there is no package dir)
Other things this change skips, but we may do later:
* set __file__ on modules imported using PyImport_ImportFrozenModule()
* set co_filename when we unmarshal the frozen code object while importing the module (e.g. in FrozenImporter.exec_module()) -- this would allow tracebacks to show source lines
* implement FrozenImporter.get_filename() and FrozenImporter.get_source()
https://bugs.python.org/issue21736
2021-10-14 18:32:18 -03:00
|
|
|
"find_frozen($module, name, /, *, withdata=False)\n"
|
2014-05-30 12:21:14 -03:00
|
|
|
"--\n"
|
|
|
|
"\n"
|
2021-10-05 13:01:27 -03:00
|
|
|
"Return info about the corresponding frozen module (if there is one) or None.\n"
|
|
|
|
"\n"
|
|
|
|
"The returned info (a 2-tuple):\n"
|
|
|
|
"\n"
|
|
|
|
" * data the raw marshalled bytes\n"
|
2021-10-05 14:26:37 -03:00
|
|
|
" * is_package whether or not it is a package\n"
|
|
|
|
" * origname the originally frozen module\'s name, or None if not\n"
|
|
|
|
" a stdlib module (this will usually be the same as\n"
|
|
|
|
" the module\'s current name)");
|
2014-05-30 12:21:14 -03:00
|
|
|
|
2021-10-05 13:01:27 -03:00
|
|
|
#define _IMP_FIND_FROZEN_METHODDEF \
|
bpo-21736: Set __file__ on frozen stdlib modules. (gh-28656)
Currently frozen modules do not have __file__ set. In their spec, origin is set to "frozen" and they are marked as not having a location. (Similarly, for frozen packages __path__ is set to an empty list.) However, for frozen stdlib modules we are able to extrapolate __file__ as long as we can determine the stdlib directory at runtime. (We now do so since gh-28586.) Having __file__ set is helpful for a number of reasons. Likewise, having a non-empty __path__ means we can import submodules of a frozen package from the filesystem (e.g. we could partially freeze the encodings module).
This change sets __file__ (and adds to __path__) for frozen stdlib modules. It uses sys._stdlibdir (from gh-28586) and the frozen module alias information (from gh-28655). All that work is done in FrozenImporter (in Lib/importlib/_bootstrap.py).
Also, if a frozen module is imported before importlib is bootstrapped (during interpreter initialization) then we fix up that module and its spec during the importlib bootstrapping step (i.e. imporlib._bootstrap._setup()) to match what gets set by FrozenImporter, including setting the file info (if the stdlib dir is known). To facilitate this, modules imported using PyImport_ImportFrozenModule() have __origname__ set using the frozen module alias info. __origname__ is popped off during importlib bootstrap.
(To be clear, even with this change the new code to set __file__ during fixups in imporlib._bootstrap._setup() doesn't actually get triggered yet. This is because sys._stdlibdir hasn't been set yet in interpreter initialization at the point importlib is bootstrapped. However, we do fix up such modules at that point to otherwise match the result of importing through FrozenImporter, just not the __file__ and __path__ parts. Doing so will require changes in the order in which things happen during interpreter initialization. That can be addressed separately. Once it is, the file-related fixup code from this PR will kick in.)
Here are things this change does not do:
* set __file__ for non-stdlib modules (no way of knowing the parent dir)
* set __file__ if the stdlib dir is not known (nor assume the expense of finding it)
* relatedly, set __file__ if the stdlib is in a zip file
* verify that the filename set to __file__ actually exists (too expensive)
* update __path__ for frozen packages that alias a non-package (since there is no package dir)
Other things this change skips, but we may do later:
* set __file__ on modules imported using PyImport_ImportFrozenModule()
* set co_filename when we unmarshal the frozen code object while importing the module (e.g. in FrozenImporter.exec_module()) -- this would allow tracebacks to show source lines
* implement FrozenImporter.get_filename() and FrozenImporter.get_source()
https://bugs.python.org/issue21736
2021-10-14 18:32:18 -03:00
|
|
|
{"find_frozen", (PyCFunction)(void(*)(void))_imp_find_frozen, METH_FASTCALL|METH_KEYWORDS, _imp_find_frozen__doc__},
|
2014-05-30 12:21:14 -03:00
|
|
|
|
|
|
|
static PyObject *
|
bpo-21736: Set __file__ on frozen stdlib modules. (gh-28656)
Currently frozen modules do not have __file__ set. In their spec, origin is set to "frozen" and they are marked as not having a location. (Similarly, for frozen packages __path__ is set to an empty list.) However, for frozen stdlib modules we are able to extrapolate __file__ as long as we can determine the stdlib directory at runtime. (We now do so since gh-28586.) Having __file__ set is helpful for a number of reasons. Likewise, having a non-empty __path__ means we can import submodules of a frozen package from the filesystem (e.g. we could partially freeze the encodings module).
This change sets __file__ (and adds to __path__) for frozen stdlib modules. It uses sys._stdlibdir (from gh-28586) and the frozen module alias information (from gh-28655). All that work is done in FrozenImporter (in Lib/importlib/_bootstrap.py).
Also, if a frozen module is imported before importlib is bootstrapped (during interpreter initialization) then we fix up that module and its spec during the importlib bootstrapping step (i.e. imporlib._bootstrap._setup()) to match what gets set by FrozenImporter, including setting the file info (if the stdlib dir is known). To facilitate this, modules imported using PyImport_ImportFrozenModule() have __origname__ set using the frozen module alias info. __origname__ is popped off during importlib bootstrap.
(To be clear, even with this change the new code to set __file__ during fixups in imporlib._bootstrap._setup() doesn't actually get triggered yet. This is because sys._stdlibdir hasn't been set yet in interpreter initialization at the point importlib is bootstrapped. However, we do fix up such modules at that point to otherwise match the result of importing through FrozenImporter, just not the __file__ and __path__ parts. Doing so will require changes in the order in which things happen during interpreter initialization. That can be addressed separately. Once it is, the file-related fixup code from this PR will kick in.)
Here are things this change does not do:
* set __file__ for non-stdlib modules (no way of knowing the parent dir)
* set __file__ if the stdlib dir is not known (nor assume the expense of finding it)
* relatedly, set __file__ if the stdlib is in a zip file
* verify that the filename set to __file__ actually exists (too expensive)
* update __path__ for frozen packages that alias a non-package (since there is no package dir)
Other things this change skips, but we may do later:
* set __file__ on modules imported using PyImport_ImportFrozenModule()
* set co_filename when we unmarshal the frozen code object while importing the module (e.g. in FrozenImporter.exec_module()) -- this would allow tracebacks to show source lines
* implement FrozenImporter.get_filename() and FrozenImporter.get_source()
https://bugs.python.org/issue21736
2021-10-14 18:32:18 -03:00
|
|
|
_imp_find_frozen_impl(PyObject *module, PyObject *name, int withdata);
|
2014-05-30 12:21:14 -03:00
|
|
|
|
|
|
|
static PyObject *
|
bpo-21736: Set __file__ on frozen stdlib modules. (gh-28656)
Currently frozen modules do not have __file__ set. In their spec, origin is set to "frozen" and they are marked as not having a location. (Similarly, for frozen packages __path__ is set to an empty list.) However, for frozen stdlib modules we are able to extrapolate __file__ as long as we can determine the stdlib directory at runtime. (We now do so since gh-28586.) Having __file__ set is helpful for a number of reasons. Likewise, having a non-empty __path__ means we can import submodules of a frozen package from the filesystem (e.g. we could partially freeze the encodings module).
This change sets __file__ (and adds to __path__) for frozen stdlib modules. It uses sys._stdlibdir (from gh-28586) and the frozen module alias information (from gh-28655). All that work is done in FrozenImporter (in Lib/importlib/_bootstrap.py).
Also, if a frozen module is imported before importlib is bootstrapped (during interpreter initialization) then we fix up that module and its spec during the importlib bootstrapping step (i.e. imporlib._bootstrap._setup()) to match what gets set by FrozenImporter, including setting the file info (if the stdlib dir is known). To facilitate this, modules imported using PyImport_ImportFrozenModule() have __origname__ set using the frozen module alias info. __origname__ is popped off during importlib bootstrap.
(To be clear, even with this change the new code to set __file__ during fixups in imporlib._bootstrap._setup() doesn't actually get triggered yet. This is because sys._stdlibdir hasn't been set yet in interpreter initialization at the point importlib is bootstrapped. However, we do fix up such modules at that point to otherwise match the result of importing through FrozenImporter, just not the __file__ and __path__ parts. Doing so will require changes in the order in which things happen during interpreter initialization. That can be addressed separately. Once it is, the file-related fixup code from this PR will kick in.)
Here are things this change does not do:
* set __file__ for non-stdlib modules (no way of knowing the parent dir)
* set __file__ if the stdlib dir is not known (nor assume the expense of finding it)
* relatedly, set __file__ if the stdlib is in a zip file
* verify that the filename set to __file__ actually exists (too expensive)
* update __path__ for frozen packages that alias a non-package (since there is no package dir)
Other things this change skips, but we may do later:
* set __file__ on modules imported using PyImport_ImportFrozenModule()
* set co_filename when we unmarshal the frozen code object while importing the module (e.g. in FrozenImporter.exec_module()) -- this would allow tracebacks to show source lines
* implement FrozenImporter.get_filename() and FrozenImporter.get_source()
https://bugs.python.org/issue21736
2021-10-14 18:32:18 -03:00
|
|
|
_imp_find_frozen(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
|
2014-05-30 12:21:14 -03:00
|
|
|
{
|
|
|
|
PyObject *return_value = NULL;
|
bpo-21736: Set __file__ on frozen stdlib modules. (gh-28656)
Currently frozen modules do not have __file__ set. In their spec, origin is set to "frozen" and they are marked as not having a location. (Similarly, for frozen packages __path__ is set to an empty list.) However, for frozen stdlib modules we are able to extrapolate __file__ as long as we can determine the stdlib directory at runtime. (We now do so since gh-28586.) Having __file__ set is helpful for a number of reasons. Likewise, having a non-empty __path__ means we can import submodules of a frozen package from the filesystem (e.g. we could partially freeze the encodings module).
This change sets __file__ (and adds to __path__) for frozen stdlib modules. It uses sys._stdlibdir (from gh-28586) and the frozen module alias information (from gh-28655). All that work is done in FrozenImporter (in Lib/importlib/_bootstrap.py).
Also, if a frozen module is imported before importlib is bootstrapped (during interpreter initialization) then we fix up that module and its spec during the importlib bootstrapping step (i.e. imporlib._bootstrap._setup()) to match what gets set by FrozenImporter, including setting the file info (if the stdlib dir is known). To facilitate this, modules imported using PyImport_ImportFrozenModule() have __origname__ set using the frozen module alias info. __origname__ is popped off during importlib bootstrap.
(To be clear, even with this change the new code to set __file__ during fixups in imporlib._bootstrap._setup() doesn't actually get triggered yet. This is because sys._stdlibdir hasn't been set yet in interpreter initialization at the point importlib is bootstrapped. However, we do fix up such modules at that point to otherwise match the result of importing through FrozenImporter, just not the __file__ and __path__ parts. Doing so will require changes in the order in which things happen during interpreter initialization. That can be addressed separately. Once it is, the file-related fixup code from this PR will kick in.)
Here are things this change does not do:
* set __file__ for non-stdlib modules (no way of knowing the parent dir)
* set __file__ if the stdlib dir is not known (nor assume the expense of finding it)
* relatedly, set __file__ if the stdlib is in a zip file
* verify that the filename set to __file__ actually exists (too expensive)
* update __path__ for frozen packages that alias a non-package (since there is no package dir)
Other things this change skips, but we may do later:
* set __file__ on modules imported using PyImport_ImportFrozenModule()
* set co_filename when we unmarshal the frozen code object while importing the module (e.g. in FrozenImporter.exec_module()) -- this would allow tracebacks to show source lines
* implement FrozenImporter.get_filename() and FrozenImporter.get_source()
https://bugs.python.org/issue21736
2021-10-14 18:32:18 -03:00
|
|
|
static const char * const _keywords[] = {"", "withdata", NULL};
|
|
|
|
static _PyArg_Parser _parser = {NULL, _keywords, "find_frozen", 0};
|
|
|
|
PyObject *argsbuf[2];
|
|
|
|
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
|
2014-05-30 12:21:14 -03:00
|
|
|
PyObject *name;
|
bpo-21736: Set __file__ on frozen stdlib modules. (gh-28656)
Currently frozen modules do not have __file__ set. In their spec, origin is set to "frozen" and they are marked as not having a location. (Similarly, for frozen packages __path__ is set to an empty list.) However, for frozen stdlib modules we are able to extrapolate __file__ as long as we can determine the stdlib directory at runtime. (We now do so since gh-28586.) Having __file__ set is helpful for a number of reasons. Likewise, having a non-empty __path__ means we can import submodules of a frozen package from the filesystem (e.g. we could partially freeze the encodings module).
This change sets __file__ (and adds to __path__) for frozen stdlib modules. It uses sys._stdlibdir (from gh-28586) and the frozen module alias information (from gh-28655). All that work is done in FrozenImporter (in Lib/importlib/_bootstrap.py).
Also, if a frozen module is imported before importlib is bootstrapped (during interpreter initialization) then we fix up that module and its spec during the importlib bootstrapping step (i.e. imporlib._bootstrap._setup()) to match what gets set by FrozenImporter, including setting the file info (if the stdlib dir is known). To facilitate this, modules imported using PyImport_ImportFrozenModule() have __origname__ set using the frozen module alias info. __origname__ is popped off during importlib bootstrap.
(To be clear, even with this change the new code to set __file__ during fixups in imporlib._bootstrap._setup() doesn't actually get triggered yet. This is because sys._stdlibdir hasn't been set yet in interpreter initialization at the point importlib is bootstrapped. However, we do fix up such modules at that point to otherwise match the result of importing through FrozenImporter, just not the __file__ and __path__ parts. Doing so will require changes in the order in which things happen during interpreter initialization. That can be addressed separately. Once it is, the file-related fixup code from this PR will kick in.)
Here are things this change does not do:
* set __file__ for non-stdlib modules (no way of knowing the parent dir)
* set __file__ if the stdlib dir is not known (nor assume the expense of finding it)
* relatedly, set __file__ if the stdlib is in a zip file
* verify that the filename set to __file__ actually exists (too expensive)
* update __path__ for frozen packages that alias a non-package (since there is no package dir)
Other things this change skips, but we may do later:
* set __file__ on modules imported using PyImport_ImportFrozenModule()
* set co_filename when we unmarshal the frozen code object while importing the module (e.g. in FrozenImporter.exec_module()) -- this would allow tracebacks to show source lines
* implement FrozenImporter.get_filename() and FrozenImporter.get_source()
https://bugs.python.org/issue21736
2021-10-14 18:32:18 -03:00
|
|
|
int withdata = 0;
|
2014-05-30 12:21:14 -03:00
|
|
|
|
bpo-21736: Set __file__ on frozen stdlib modules. (gh-28656)
Currently frozen modules do not have __file__ set. In their spec, origin is set to "frozen" and they are marked as not having a location. (Similarly, for frozen packages __path__ is set to an empty list.) However, for frozen stdlib modules we are able to extrapolate __file__ as long as we can determine the stdlib directory at runtime. (We now do so since gh-28586.) Having __file__ set is helpful for a number of reasons. Likewise, having a non-empty __path__ means we can import submodules of a frozen package from the filesystem (e.g. we could partially freeze the encodings module).
This change sets __file__ (and adds to __path__) for frozen stdlib modules. It uses sys._stdlibdir (from gh-28586) and the frozen module alias information (from gh-28655). All that work is done in FrozenImporter (in Lib/importlib/_bootstrap.py).
Also, if a frozen module is imported before importlib is bootstrapped (during interpreter initialization) then we fix up that module and its spec during the importlib bootstrapping step (i.e. imporlib._bootstrap._setup()) to match what gets set by FrozenImporter, including setting the file info (if the stdlib dir is known). To facilitate this, modules imported using PyImport_ImportFrozenModule() have __origname__ set using the frozen module alias info. __origname__ is popped off during importlib bootstrap.
(To be clear, even with this change the new code to set __file__ during fixups in imporlib._bootstrap._setup() doesn't actually get triggered yet. This is because sys._stdlibdir hasn't been set yet in interpreter initialization at the point importlib is bootstrapped. However, we do fix up such modules at that point to otherwise match the result of importing through FrozenImporter, just not the __file__ and __path__ parts. Doing so will require changes in the order in which things happen during interpreter initialization. That can be addressed separately. Once it is, the file-related fixup code from this PR will kick in.)
Here are things this change does not do:
* set __file__ for non-stdlib modules (no way of knowing the parent dir)
* set __file__ if the stdlib dir is not known (nor assume the expense of finding it)
* relatedly, set __file__ if the stdlib is in a zip file
* verify that the filename set to __file__ actually exists (too expensive)
* update __path__ for frozen packages that alias a non-package (since there is no package dir)
Other things this change skips, but we may do later:
* set __file__ on modules imported using PyImport_ImportFrozenModule()
* set co_filename when we unmarshal the frozen code object while importing the module (e.g. in FrozenImporter.exec_module()) -- this would allow tracebacks to show source lines
* implement FrozenImporter.get_filename() and FrozenImporter.get_source()
https://bugs.python.org/issue21736
2021-10-14 18:32:18 -03:00
|
|
|
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
|
|
|
|
if (!args) {
|
2014-05-30 12:21:14 -03:00
|
|
|
goto exit;
|
2016-06-09 10:16:06 -03:00
|
|
|
}
|
bpo-21736: Set __file__ on frozen stdlib modules. (gh-28656)
Currently frozen modules do not have __file__ set. In their spec, origin is set to "frozen" and they are marked as not having a location. (Similarly, for frozen packages __path__ is set to an empty list.) However, for frozen stdlib modules we are able to extrapolate __file__ as long as we can determine the stdlib directory at runtime. (We now do so since gh-28586.) Having __file__ set is helpful for a number of reasons. Likewise, having a non-empty __path__ means we can import submodules of a frozen package from the filesystem (e.g. we could partially freeze the encodings module).
This change sets __file__ (and adds to __path__) for frozen stdlib modules. It uses sys._stdlibdir (from gh-28586) and the frozen module alias information (from gh-28655). All that work is done in FrozenImporter (in Lib/importlib/_bootstrap.py).
Also, if a frozen module is imported before importlib is bootstrapped (during interpreter initialization) then we fix up that module and its spec during the importlib bootstrapping step (i.e. imporlib._bootstrap._setup()) to match what gets set by FrozenImporter, including setting the file info (if the stdlib dir is known). To facilitate this, modules imported using PyImport_ImportFrozenModule() have __origname__ set using the frozen module alias info. __origname__ is popped off during importlib bootstrap.
(To be clear, even with this change the new code to set __file__ during fixups in imporlib._bootstrap._setup() doesn't actually get triggered yet. This is because sys._stdlibdir hasn't been set yet in interpreter initialization at the point importlib is bootstrapped. However, we do fix up such modules at that point to otherwise match the result of importing through FrozenImporter, just not the __file__ and __path__ parts. Doing so will require changes in the order in which things happen during interpreter initialization. That can be addressed separately. Once it is, the file-related fixup code from this PR will kick in.)
Here are things this change does not do:
* set __file__ for non-stdlib modules (no way of knowing the parent dir)
* set __file__ if the stdlib dir is not known (nor assume the expense of finding it)
* relatedly, set __file__ if the stdlib is in a zip file
* verify that the filename set to __file__ actually exists (too expensive)
* update __path__ for frozen packages that alias a non-package (since there is no package dir)
Other things this change skips, but we may do later:
* set __file__ on modules imported using PyImport_ImportFrozenModule()
* set co_filename when we unmarshal the frozen code object while importing the module (e.g. in FrozenImporter.exec_module()) -- this would allow tracebacks to show source lines
* implement FrozenImporter.get_filename() and FrozenImporter.get_source()
https://bugs.python.org/issue21736
2021-10-14 18:32:18 -03:00
|
|
|
if (!PyUnicode_Check(args[0])) {
|
|
|
|
_PyArg_BadArgument("find_frozen", "argument 1", "str", args[0]);
|
2018-12-25 07:23:47 -04:00
|
|
|
goto exit;
|
|
|
|
}
|
bpo-21736: Set __file__ on frozen stdlib modules. (gh-28656)
Currently frozen modules do not have __file__ set. In their spec, origin is set to "frozen" and they are marked as not having a location. (Similarly, for frozen packages __path__ is set to an empty list.) However, for frozen stdlib modules we are able to extrapolate __file__ as long as we can determine the stdlib directory at runtime. (We now do so since gh-28586.) Having __file__ set is helpful for a number of reasons. Likewise, having a non-empty __path__ means we can import submodules of a frozen package from the filesystem (e.g. we could partially freeze the encodings module).
This change sets __file__ (and adds to __path__) for frozen stdlib modules. It uses sys._stdlibdir (from gh-28586) and the frozen module alias information (from gh-28655). All that work is done in FrozenImporter (in Lib/importlib/_bootstrap.py).
Also, if a frozen module is imported before importlib is bootstrapped (during interpreter initialization) then we fix up that module and its spec during the importlib bootstrapping step (i.e. imporlib._bootstrap._setup()) to match what gets set by FrozenImporter, including setting the file info (if the stdlib dir is known). To facilitate this, modules imported using PyImport_ImportFrozenModule() have __origname__ set using the frozen module alias info. __origname__ is popped off during importlib bootstrap.
(To be clear, even with this change the new code to set __file__ during fixups in imporlib._bootstrap._setup() doesn't actually get triggered yet. This is because sys._stdlibdir hasn't been set yet in interpreter initialization at the point importlib is bootstrapped. However, we do fix up such modules at that point to otherwise match the result of importing through FrozenImporter, just not the __file__ and __path__ parts. Doing so will require changes in the order in which things happen during interpreter initialization. That can be addressed separately. Once it is, the file-related fixup code from this PR will kick in.)
Here are things this change does not do:
* set __file__ for non-stdlib modules (no way of knowing the parent dir)
* set __file__ if the stdlib dir is not known (nor assume the expense of finding it)
* relatedly, set __file__ if the stdlib is in a zip file
* verify that the filename set to __file__ actually exists (too expensive)
* update __path__ for frozen packages that alias a non-package (since there is no package dir)
Other things this change skips, but we may do later:
* set __file__ on modules imported using PyImport_ImportFrozenModule()
* set co_filename when we unmarshal the frozen code object while importing the module (e.g. in FrozenImporter.exec_module()) -- this would allow tracebacks to show source lines
* implement FrozenImporter.get_filename() and FrozenImporter.get_source()
https://bugs.python.org/issue21736
2021-10-14 18:32:18 -03:00
|
|
|
if (PyUnicode_READY(args[0]) == -1) {
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
name = args[0];
|
|
|
|
if (!noptargs) {
|
|
|
|
goto skip_optional_kwonly;
|
|
|
|
}
|
|
|
|
withdata = PyObject_IsTrue(args[1]);
|
|
|
|
if (withdata < 0) {
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
skip_optional_kwonly:
|
|
|
|
return_value = _imp_find_frozen_impl(module, name, withdata);
|
2021-10-05 13:01:27 -03:00
|
|
|
|
|
|
|
exit:
|
|
|
|
return return_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(_imp_get_frozen_object__doc__,
|
|
|
|
"get_frozen_object($module, name, data=None, /)\n"
|
|
|
|
"--\n"
|
|
|
|
"\n"
|
|
|
|
"Create a code object for a frozen module.");
|
|
|
|
|
|
|
|
#define _IMP_GET_FROZEN_OBJECT_METHODDEF \
|
|
|
|
{"get_frozen_object", (PyCFunction)(void(*)(void))_imp_get_frozen_object, METH_FASTCALL, _imp_get_frozen_object__doc__},
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_imp_get_frozen_object_impl(PyObject *module, PyObject *name,
|
|
|
|
PyObject *dataobj);
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_imp_get_frozen_object(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
|
|
|
|
{
|
|
|
|
PyObject *return_value = NULL;
|
|
|
|
PyObject *name;
|
|
|
|
PyObject *dataobj = Py_None;
|
|
|
|
|
|
|
|
if (!_PyArg_CheckPositional("get_frozen_object", nargs, 1, 2)) {
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
if (!PyUnicode_Check(args[0])) {
|
|
|
|
_PyArg_BadArgument("get_frozen_object", "argument 1", "str", args[0]);
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
if (PyUnicode_READY(args[0]) == -1) {
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
name = args[0];
|
|
|
|
if (nargs < 2) {
|
|
|
|
goto skip_optional;
|
|
|
|
}
|
|
|
|
dataobj = args[1];
|
|
|
|
skip_optional:
|
|
|
|
return_value = _imp_get_frozen_object_impl(module, name, dataobj);
|
2014-05-30 12:21:14 -03:00
|
|
|
|
|
|
|
exit:
|
|
|
|
return return_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(_imp_is_frozen_package__doc__,
|
|
|
|
"is_frozen_package($module, name, /)\n"
|
|
|
|
"--\n"
|
|
|
|
"\n"
|
|
|
|
"Returns True if the module name is of a frozen package.");
|
|
|
|
|
|
|
|
#define _IMP_IS_FROZEN_PACKAGE_METHODDEF \
|
2015-04-03 18:12:11 -03:00
|
|
|
{"is_frozen_package", (PyCFunction)_imp_is_frozen_package, METH_O, _imp_is_frozen_package__doc__},
|
2014-05-30 12:21:14 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2016-07-07 11:35:15 -03:00
|
|
|
_imp_is_frozen_package_impl(PyObject *module, PyObject *name);
|
2014-05-30 12:21:14 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2016-07-07 11:35:15 -03:00
|
|
|
_imp_is_frozen_package(PyObject *module, PyObject *arg)
|
2014-05-30 12:21:14 -03:00
|
|
|
{
|
|
|
|
PyObject *return_value = NULL;
|
|
|
|
PyObject *name;
|
|
|
|
|
2018-12-25 07:23:47 -04:00
|
|
|
if (!PyUnicode_Check(arg)) {
|
2019-08-29 11:49:08 -03:00
|
|
|
_PyArg_BadArgument("is_frozen_package", "argument", "str", arg);
|
2018-12-25 07:23:47 -04:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
if (PyUnicode_READY(arg) == -1) {
|
2014-05-30 12:21:14 -03:00
|
|
|
goto exit;
|
2016-06-09 10:16:06 -03:00
|
|
|
}
|
2018-12-25 07:23:47 -04:00
|
|
|
name = arg;
|
2014-05-30 12:21:14 -03:00
|
|
|
return_value = _imp_is_frozen_package_impl(module, name);
|
|
|
|
|
|
|
|
exit:
|
|
|
|
return return_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(_imp_is_builtin__doc__,
|
|
|
|
"is_builtin($module, name, /)\n"
|
|
|
|
"--\n"
|
|
|
|
"\n"
|
|
|
|
"Returns True if the module name corresponds to a built-in module.");
|
|
|
|
|
|
|
|
#define _IMP_IS_BUILTIN_METHODDEF \
|
2015-04-03 18:12:11 -03:00
|
|
|
{"is_builtin", (PyCFunction)_imp_is_builtin, METH_O, _imp_is_builtin__doc__},
|
2014-05-30 12:21:14 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2016-07-07 11:35:15 -03:00
|
|
|
_imp_is_builtin_impl(PyObject *module, PyObject *name);
|
2014-05-30 12:21:14 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2016-07-07 11:35:15 -03:00
|
|
|
_imp_is_builtin(PyObject *module, PyObject *arg)
|
2014-05-30 12:21:14 -03:00
|
|
|
{
|
|
|
|
PyObject *return_value = NULL;
|
|
|
|
PyObject *name;
|
|
|
|
|
2018-12-25 07:23:47 -04:00
|
|
|
if (!PyUnicode_Check(arg)) {
|
2019-08-29 11:49:08 -03:00
|
|
|
_PyArg_BadArgument("is_builtin", "argument", "str", arg);
|
2018-12-25 07:23:47 -04:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
if (PyUnicode_READY(arg) == -1) {
|
2014-05-30 12:21:14 -03:00
|
|
|
goto exit;
|
2016-06-09 10:16:06 -03:00
|
|
|
}
|
2018-12-25 07:23:47 -04:00
|
|
|
name = arg;
|
2014-05-30 12:21:14 -03:00
|
|
|
return_value = _imp_is_builtin_impl(module, name);
|
|
|
|
|
|
|
|
exit:
|
|
|
|
return return_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(_imp_is_frozen__doc__,
|
|
|
|
"is_frozen($module, name, /)\n"
|
|
|
|
"--\n"
|
|
|
|
"\n"
|
|
|
|
"Returns True if the module name corresponds to a frozen module.");
|
|
|
|
|
|
|
|
#define _IMP_IS_FROZEN_METHODDEF \
|
2015-04-03 18:12:11 -03:00
|
|
|
{"is_frozen", (PyCFunction)_imp_is_frozen, METH_O, _imp_is_frozen__doc__},
|
2014-05-30 12:21:14 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2016-07-07 11:35:15 -03:00
|
|
|
_imp_is_frozen_impl(PyObject *module, PyObject *name);
|
2014-05-30 12:21:14 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2016-07-07 11:35:15 -03:00
|
|
|
_imp_is_frozen(PyObject *module, PyObject *arg)
|
2014-05-30 12:21:14 -03:00
|
|
|
{
|
|
|
|
PyObject *return_value = NULL;
|
|
|
|
PyObject *name;
|
|
|
|
|
2018-12-25 07:23:47 -04:00
|
|
|
if (!PyUnicode_Check(arg)) {
|
2019-08-29 11:49:08 -03:00
|
|
|
_PyArg_BadArgument("is_frozen", "argument", "str", arg);
|
2018-12-25 07:23:47 -04:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
if (PyUnicode_READY(arg) == -1) {
|
2014-05-30 12:21:14 -03:00
|
|
|
goto exit;
|
2016-06-09 10:16:06 -03:00
|
|
|
}
|
2018-12-25 07:23:47 -04:00
|
|
|
name = arg;
|
2014-05-30 12:21:14 -03:00
|
|
|
return_value = _imp_is_frozen_impl(module, name);
|
|
|
|
|
|
|
|
exit:
|
|
|
|
return return_value;
|
|
|
|
}
|
|
|
|
|
2021-09-13 19:18:37 -03:00
|
|
|
PyDoc_STRVAR(_imp__frozen_module_names__doc__,
|
|
|
|
"_frozen_module_names($module, /)\n"
|
|
|
|
"--\n"
|
|
|
|
"\n"
|
|
|
|
"Returns the list of available frozen modules.");
|
|
|
|
|
|
|
|
#define _IMP__FROZEN_MODULE_NAMES_METHODDEF \
|
|
|
|
{"_frozen_module_names", (PyCFunction)_imp__frozen_module_names, METH_NOARGS, _imp__frozen_module_names__doc__},
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_imp__frozen_module_names_impl(PyObject *module);
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_imp__frozen_module_names(PyObject *module, PyObject *Py_UNUSED(ignored))
|
|
|
|
{
|
|
|
|
return _imp__frozen_module_names_impl(module);
|
|
|
|
}
|
|
|
|
|
2021-09-14 20:31:45 -03:00
|
|
|
PyDoc_STRVAR(_imp__override_frozen_modules_for_tests__doc__,
|
|
|
|
"_override_frozen_modules_for_tests($module, override, /)\n"
|
|
|
|
"--\n"
|
|
|
|
"\n"
|
|
|
|
"(internal-only) Override PyConfig.use_frozen_modules.\n"
|
|
|
|
"\n"
|
|
|
|
"(-1: \"off\", 1: \"on\", 0: no override)\n"
|
|
|
|
"See frozen_modules() in Lib/test/support/import_helper.py.");
|
|
|
|
|
|
|
|
#define _IMP__OVERRIDE_FROZEN_MODULES_FOR_TESTS_METHODDEF \
|
|
|
|
{"_override_frozen_modules_for_tests", (PyCFunction)_imp__override_frozen_modules_for_tests, METH_O, _imp__override_frozen_modules_for_tests__doc__},
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_imp__override_frozen_modules_for_tests_impl(PyObject *module, int override);
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_imp__override_frozen_modules_for_tests(PyObject *module, PyObject *arg)
|
|
|
|
{
|
|
|
|
PyObject *return_value = NULL;
|
|
|
|
int override;
|
|
|
|
|
|
|
|
override = _PyLong_AsInt(arg);
|
|
|
|
if (override == -1 && PyErr_Occurred()) {
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
return_value = _imp__override_frozen_modules_for_tests_impl(module, override);
|
|
|
|
|
|
|
|
exit:
|
|
|
|
return return_value;
|
|
|
|
}
|
|
|
|
|
2014-05-30 12:21:14 -03:00
|
|
|
#if defined(HAVE_DYNAMIC_LOADING)
|
|
|
|
|
2015-05-23 09:24:10 -03:00
|
|
|
PyDoc_STRVAR(_imp_create_dynamic__doc__,
|
2019-09-14 06:24:05 -03:00
|
|
|
"create_dynamic($module, spec, file=<unrepresentable>, /)\n"
|
2014-05-30 12:21:14 -03:00
|
|
|
"--\n"
|
|
|
|
"\n"
|
2015-05-23 09:24:10 -03:00
|
|
|
"Create an extension module.");
|
2014-05-30 12:21:14 -03:00
|
|
|
|
2015-05-23 09:24:10 -03:00
|
|
|
#define _IMP_CREATE_DYNAMIC_METHODDEF \
|
2018-11-27 05:27:36 -04:00
|
|
|
{"create_dynamic", (PyCFunction)(void(*)(void))_imp_create_dynamic, METH_FASTCALL, _imp_create_dynamic__doc__},
|
2014-05-30 12:21:14 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2016-07-07 11:35:15 -03:00
|
|
|
_imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file);
|
2014-05-30 12:21:14 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2017-12-15 07:11:11 -04:00
|
|
|
_imp_create_dynamic(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
|
2014-05-30 12:21:14 -03:00
|
|
|
{
|
|
|
|
PyObject *return_value = NULL;
|
2015-05-23 09:24:10 -03:00
|
|
|
PyObject *spec;
|
2014-05-30 12:21:14 -03:00
|
|
|
PyObject *file = NULL;
|
|
|
|
|
2019-01-11 12:01:42 -04:00
|
|
|
if (!_PyArg_CheckPositional("create_dynamic", nargs, 1, 2)) {
|
2017-01-16 21:21:47 -04:00
|
|
|
goto exit;
|
|
|
|
}
|
2019-01-11 12:01:42 -04:00
|
|
|
spec = args[0];
|
|
|
|
if (nargs < 2) {
|
|
|
|
goto skip_optional;
|
|
|
|
}
|
|
|
|
file = args[1];
|
|
|
|
skip_optional:
|
2015-05-23 09:24:10 -03:00
|
|
|
return_value = _imp_create_dynamic_impl(module, spec, file);
|
2014-05-30 12:21:14 -03:00
|
|
|
|
|
|
|
exit:
|
|
|
|
return return_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* defined(HAVE_DYNAMIC_LOADING) */
|
|
|
|
|
2015-05-23 09:24:10 -03:00
|
|
|
#if defined(HAVE_DYNAMIC_LOADING)
|
|
|
|
|
|
|
|
PyDoc_STRVAR(_imp_exec_dynamic__doc__,
|
|
|
|
"exec_dynamic($module, mod, /)\n"
|
|
|
|
"--\n"
|
|
|
|
"\n"
|
|
|
|
"Initialize an extension module.");
|
|
|
|
|
|
|
|
#define _IMP_EXEC_DYNAMIC_METHODDEF \
|
|
|
|
{"exec_dynamic", (PyCFunction)_imp_exec_dynamic, METH_O, _imp_exec_dynamic__doc__},
|
|
|
|
|
|
|
|
static int
|
2016-07-07 11:35:15 -03:00
|
|
|
_imp_exec_dynamic_impl(PyObject *module, PyObject *mod);
|
2015-05-23 09:24:10 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2016-07-07 11:35:15 -03:00
|
|
|
_imp_exec_dynamic(PyObject *module, PyObject *mod)
|
2015-05-23 09:24:10 -03:00
|
|
|
{
|
|
|
|
PyObject *return_value = NULL;
|
|
|
|
int _return_value;
|
|
|
|
|
|
|
|
_return_value = _imp_exec_dynamic_impl(module, mod);
|
2016-06-09 10:16:06 -03:00
|
|
|
if ((_return_value == -1) && PyErr_Occurred()) {
|
2015-05-23 09:24:10 -03:00
|
|
|
goto exit;
|
2016-06-09 10:16:06 -03:00
|
|
|
}
|
2015-05-23 09:24:10 -03:00
|
|
|
return_value = PyLong_FromLong((long)_return_value);
|
|
|
|
|
|
|
|
exit:
|
|
|
|
return return_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* defined(HAVE_DYNAMIC_LOADING) */
|
|
|
|
|
2015-08-24 23:53:56 -03:00
|
|
|
PyDoc_STRVAR(_imp_exec_builtin__doc__,
|
|
|
|
"exec_builtin($module, mod, /)\n"
|
|
|
|
"--\n"
|
|
|
|
"\n"
|
2015-08-25 00:23:27 -03:00
|
|
|
"Initialize a built-in module.");
|
2015-08-24 23:53:56 -03:00
|
|
|
|
|
|
|
#define _IMP_EXEC_BUILTIN_METHODDEF \
|
|
|
|
{"exec_builtin", (PyCFunction)_imp_exec_builtin, METH_O, _imp_exec_builtin__doc__},
|
|
|
|
|
|
|
|
static int
|
2016-07-07 11:35:15 -03:00
|
|
|
_imp_exec_builtin_impl(PyObject *module, PyObject *mod);
|
2015-08-24 23:53:56 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2016-07-07 11:35:15 -03:00
|
|
|
_imp_exec_builtin(PyObject *module, PyObject *mod)
|
2015-08-24 23:53:56 -03:00
|
|
|
{
|
|
|
|
PyObject *return_value = NULL;
|
|
|
|
int _return_value;
|
|
|
|
|
|
|
|
_return_value = _imp_exec_builtin_impl(module, mod);
|
2016-06-09 10:16:06 -03:00
|
|
|
if ((_return_value == -1) && PyErr_Occurred()) {
|
2015-08-24 23:53:56 -03:00
|
|
|
goto exit;
|
2016-06-09 10:16:06 -03:00
|
|
|
}
|
2015-08-24 23:53:56 -03:00
|
|
|
return_value = PyLong_FromLong((long)_return_value);
|
|
|
|
|
|
|
|
exit:
|
|
|
|
return return_value;
|
|
|
|
}
|
|
|
|
|
2017-12-09 14:26:52 -04:00
|
|
|
PyDoc_STRVAR(_imp_source_hash__doc__,
|
|
|
|
"source_hash($module, /, key, source)\n"
|
|
|
|
"--\n"
|
|
|
|
"\n");
|
|
|
|
|
|
|
|
#define _IMP_SOURCE_HASH_METHODDEF \
|
2018-11-27 05:27:36 -04:00
|
|
|
{"source_hash", (PyCFunction)(void(*)(void))_imp_source_hash, METH_FASTCALL|METH_KEYWORDS, _imp_source_hash__doc__},
|
2017-12-09 14:26:52 -04:00
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_imp_source_hash_impl(PyObject *module, long key, Py_buffer *source);
|
|
|
|
|
|
|
|
static PyObject *
|
2017-12-15 07:11:11 -04:00
|
|
|
_imp_source_hash(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
|
2017-12-09 14:26:52 -04:00
|
|
|
{
|
|
|
|
PyObject *return_value = NULL;
|
|
|
|
static const char * const _keywords[] = {"key", "source", NULL};
|
2019-03-14 05:32:22 -03:00
|
|
|
static _PyArg_Parser _parser = {NULL, _keywords, "source_hash", 0};
|
|
|
|
PyObject *argsbuf[2];
|
2017-12-09 14:26:52 -04:00
|
|
|
long key;
|
|
|
|
Py_buffer source = {NULL, NULL};
|
|
|
|
|
2019-03-14 05:32:22 -03:00
|
|
|
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
|
|
|
|
if (!args) {
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
key = PyLong_AsLong(args[0]);
|
|
|
|
if (key == -1 && PyErr_Occurred()) {
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
if (PyObject_GetBuffer(args[1], &source, PyBUF_SIMPLE) != 0) {
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
if (!PyBuffer_IsContiguous(&source, 'C')) {
|
2019-08-29 11:49:08 -03:00
|
|
|
_PyArg_BadArgument("source_hash", "argument 'source'", "contiguous buffer", args[1]);
|
2017-12-09 14:26:52 -04:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
return_value = _imp_source_hash_impl(module, key, &source);
|
|
|
|
|
|
|
|
exit:
|
|
|
|
/* Cleanup for source */
|
|
|
|
if (source.obj) {
|
|
|
|
PyBuffer_Release(&source);
|
|
|
|
}
|
|
|
|
|
|
|
|
return return_value;
|
|
|
|
}
|
|
|
|
|
2015-05-23 09:24:10 -03:00
|
|
|
#ifndef _IMP_CREATE_DYNAMIC_METHODDEF
|
|
|
|
#define _IMP_CREATE_DYNAMIC_METHODDEF
|
|
|
|
#endif /* !defined(_IMP_CREATE_DYNAMIC_METHODDEF) */
|
|
|
|
|
|
|
|
#ifndef _IMP_EXEC_DYNAMIC_METHODDEF
|
|
|
|
#define _IMP_EXEC_DYNAMIC_METHODDEF
|
|
|
|
#endif /* !defined(_IMP_EXEC_DYNAMIC_METHODDEF) */
|
bpo-21736: Set __file__ on frozen stdlib modules. (gh-28656)
Currently frozen modules do not have __file__ set. In their spec, origin is set to "frozen" and they are marked as not having a location. (Similarly, for frozen packages __path__ is set to an empty list.) However, for frozen stdlib modules we are able to extrapolate __file__ as long as we can determine the stdlib directory at runtime. (We now do so since gh-28586.) Having __file__ set is helpful for a number of reasons. Likewise, having a non-empty __path__ means we can import submodules of a frozen package from the filesystem (e.g. we could partially freeze the encodings module).
This change sets __file__ (and adds to __path__) for frozen stdlib modules. It uses sys._stdlibdir (from gh-28586) and the frozen module alias information (from gh-28655). All that work is done in FrozenImporter (in Lib/importlib/_bootstrap.py).
Also, if a frozen module is imported before importlib is bootstrapped (during interpreter initialization) then we fix up that module and its spec during the importlib bootstrapping step (i.e. imporlib._bootstrap._setup()) to match what gets set by FrozenImporter, including setting the file info (if the stdlib dir is known). To facilitate this, modules imported using PyImport_ImportFrozenModule() have __origname__ set using the frozen module alias info. __origname__ is popped off during importlib bootstrap.
(To be clear, even with this change the new code to set __file__ during fixups in imporlib._bootstrap._setup() doesn't actually get triggered yet. This is because sys._stdlibdir hasn't been set yet in interpreter initialization at the point importlib is bootstrapped. However, we do fix up such modules at that point to otherwise match the result of importing through FrozenImporter, just not the __file__ and __path__ parts. Doing so will require changes in the order in which things happen during interpreter initialization. That can be addressed separately. Once it is, the file-related fixup code from this PR will kick in.)
Here are things this change does not do:
* set __file__ for non-stdlib modules (no way of knowing the parent dir)
* set __file__ if the stdlib dir is not known (nor assume the expense of finding it)
* relatedly, set __file__ if the stdlib is in a zip file
* verify that the filename set to __file__ actually exists (too expensive)
* update __path__ for frozen packages that alias a non-package (since there is no package dir)
Other things this change skips, but we may do later:
* set __file__ on modules imported using PyImport_ImportFrozenModule()
* set co_filename when we unmarshal the frozen code object while importing the module (e.g. in FrozenImporter.exec_module()) -- this would allow tracebacks to show source lines
* implement FrozenImporter.get_filename() and FrozenImporter.get_source()
https://bugs.python.org/issue21736
2021-10-14 18:32:18 -03:00
|
|
|
/*[clinic end generated code: output=adcf787969a11353 input=a9049054013a1b77]*/
|