summaryrefslogtreecommitdiffstats
path: root/Python/sysmodule.c
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2021-01-25 12:24:42 (GMT)
committerGitHub <noreply@github.com>2021-01-25 12:24:42 (GMT)
commitdb584bdad32d81e42b71871077a8008036f5c048 (patch)
treeef018339067b08456b55a083547f9427b817f1b3 /Python/sysmodule.c
parent879986d8a932c4524cb6ff822afc9537de16e28d (diff)
downloadcpython-db584bdad32d81e42b71871077a8008036f5c048.zip
cpython-db584bdad32d81e42b71871077a8008036f5c048.tar.gz
cpython-db584bdad32d81e42b71871077a8008036f5c048.tar.bz2
bpo-42955: Add sys.modules_names (GH-24238)
Add sys.module_names, containing the list of the standard library module names.
Diffstat (limited to 'Python/sysmodule.c')
-rw-r--r--Python/sysmodule.c62
1 files changed, 47 insertions, 15 deletions
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index 720532e..e2f7e39 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -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;
+ }
+ 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;
}
- if (list) {
- PyObject *v = PyList_AsTuple(list);
- Py_DECREF(list);
- list = v;
+
+ 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);
}
- return list;
+
+ 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