mirror of
https://github.com/python/cpython.git
synced 2025-10-06 23:21:06 +00:00
bpo-42955: Add sys.modules_names (GH-24238)
Add sys.module_names, containing the list of the standard library module names.
This commit is contained in:
parent
879986d8a9
commit
db584bdad3
10 changed files with 305 additions and 275 deletions
|
@ -29,6 +29,7 @@ Data members:
|
|||
#include "frameobject.h" // PyFrame_GetBack()
|
||||
#include "pydtrace.h"
|
||||
#include "osdefs.h" // DELIM
|
||||
#include "module_names.h" // _Py_module_names
|
||||
#include <locale.h>
|
||||
|
||||
#ifdef MS_WINDOWS
|
||||
|
@ -2020,33 +2021,63 @@ static PyMethodDef sys_methods[] = {
|
|||
{NULL, NULL} /* sentinel */
|
||||
};
|
||||
|
||||
|
||||
static PyObject *
|
||||
list_builtin_module_names(void)
|
||||
{
|
||||
PyObject *list = PyList_New(0);
|
||||
int i;
|
||||
if (list == NULL)
|
||||
if (list == NULL) {
|
||||
return NULL;
|
||||
for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
|
||||
PyObject *name = PyUnicode_FromString(
|
||||
PyImport_Inittab[i].name);
|
||||
if (name == NULL)
|
||||
break;
|
||||
PyList_Append(list, name);
|
||||
}
|
||||
for (Py_ssize_t i = 0; PyImport_Inittab[i].name != NULL; i++) {
|
||||
PyObject *name = PyUnicode_FromString(PyImport_Inittab[i].name);
|
||||
if (name == NULL) {
|
||||
goto error;
|
||||
}
|
||||
if (PyList_Append(list, name) < 0) {
|
||||
Py_DECREF(name);
|
||||
goto error;
|
||||
}
|
||||
Py_DECREF(name);
|
||||
}
|
||||
if (PyList_Sort(list) != 0) {
|
||||
Py_DECREF(list);
|
||||
list = NULL;
|
||||
goto error;
|
||||
}
|
||||
if (list) {
|
||||
PyObject *v = PyList_AsTuple(list);
|
||||
Py_DECREF(list);
|
||||
list = v;
|
||||
}
|
||||
return list;
|
||||
PyObject *tuple = PyList_AsTuple(list);
|
||||
Py_DECREF(list);
|
||||
return tuple;
|
||||
|
||||
error:
|
||||
Py_DECREF(list);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
static PyObject *
|
||||
list_module_names(void)
|
||||
{
|
||||
Py_ssize_t len = Py_ARRAY_LENGTH(_Py_module_names);
|
||||
PyObject *names = PyTuple_New(len);
|
||||
if (names == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (Py_ssize_t i = 0; i < len; i++) {
|
||||
PyObject *name = PyUnicode_FromString(_Py_module_names[i]);
|
||||
if (name == NULL) {
|
||||
Py_DECREF(names);
|
||||
return NULL;
|
||||
}
|
||||
PyTuple_SET_ITEM(names, i, name);
|
||||
}
|
||||
|
||||
PyObject *set = PyObject_CallFunction((PyObject *)&PyFrozenSet_Type,
|
||||
"(O)", names);
|
||||
Py_DECREF(names);
|
||||
return set;
|
||||
}
|
||||
|
||||
|
||||
/* Pre-initialization support for sys.warnoptions and sys._xoptions
|
||||
*
|
||||
* Modern internal code paths:
|
||||
|
@ -2753,6 +2784,7 @@ _PySys_InitCore(PyThreadState *tstate, PyObject *sysdict)
|
|||
SET_SYS("hash_info", get_hash_info(tstate));
|
||||
SET_SYS("maxunicode", PyLong_FromLong(0x10FFFF));
|
||||
SET_SYS("builtin_module_names", list_builtin_module_names());
|
||||
SET_SYS("module_names", list_module_names());
|
||||
#if PY_BIG_ENDIAN
|
||||
SET_SYS_FROM_STRING("byteorder", "big");
|
||||
#else
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue