Skip to content

Commit 9587286

Browse files
author
Victor Stinner
committed
Issue #3080: Import builtins using Unicode strings
- is_builtin(), init_builtin(), load_builtin() and other builtin related functions use Unicode strings, instead of byte strings - Rename _PyImport_FixupExtensionUnicode() to _PyImport_FixupExtensionObject() - Rename _PyImport_FindExtensionUnicode() to _PyImport_FindExtensionObject()
1 parent 53dc735 commit 9587286

File tree

3 files changed

+80
-60
lines changed

3 files changed

+80
-60
lines changed

Include/import.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ PyAPI_FUNC(void) _PyImport_ReInitLock(void);
7979
PyAPI_FUNC(PyObject *)_PyImport_FindBuiltin(
8080
const char *name /* UTF-8 encoded string */
8181
);
82-
PyAPI_FUNC(PyObject *)_PyImport_FindExtensionUnicode(const char *, PyObject *);
82+
PyAPI_FUNC(PyObject *)_PyImport_FindExtensionObject(PyObject *, PyObject *);
8383
PyAPI_FUNC(int)_PyImport_FixupBuiltin(
8484
PyObject *mod,
8585
char *name /* UTF-8 encoded string */
8686
);
87-
PyAPI_FUNC(int)_PyImport_FixupExtensionUnicode(PyObject*, char *, PyObject *);
87+
PyAPI_FUNC(int)_PyImport_FixupExtensionObject(PyObject*, PyObject *, PyObject *);
8888

8989
struct _inittab {
9090
char *name;

Python/import.c

Lines changed: 60 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ typedef unsigned short mode_t;
117117
static long pyc_magic = MAGIC;
118118
static const char *pyc_tag = TAG;
119119

120-
/* See _PyImport_FixupExtensionUnicode() below */
120+
/* See _PyImport_FixupExtensionObject() below */
121121
static PyObject *extensions = NULL;
122122

123123
/* This table is defined in config.c: */
@@ -563,10 +563,10 @@ PyImport_GetMagicTag(void)
563563
once, we keep a static dictionary 'extensions' keyed by module name
564564
(for built-in modules) or by filename (for dynamically loaded
565565
modules), containing these modules. A copy of the module's
566-
dictionary is stored by calling _PyImport_FixupExtensionUnicode()
566+
dictionary is stored by calling _PyImport_FixupExtensionObject()
567567
immediately after the module initialization function succeeds. A
568568
copy can be retrieved from there by calling
569-
_PyImport_FindExtensionUnicode().
569+
_PyImport_FindExtensionObject().
570570
571571
Modules which do support multiple initialization set their m_size
572572
field to a non-negative number (indicating the size of the
@@ -575,7 +575,8 @@ PyImport_GetMagicTag(void)
575575
*/
576576

577577
int
578-
_PyImport_FixupExtensionUnicode(PyObject *mod, char *name, PyObject *filename)
578+
_PyImport_FixupExtensionObject(PyObject *mod, PyObject *name,
579+
PyObject *filename)
579580
{
580581
PyObject *modules, *dict;
581582
struct PyModuleDef *def;
@@ -594,10 +595,10 @@ _PyImport_FixupExtensionUnicode(PyObject *mod, char *name, PyObject *filename)
594595
return -1;
595596
}
596597
modules = PyImport_GetModuleDict();
597-
if (PyDict_SetItemString(modules, name, mod) < 0)
598+
if (PyDict_SetItem(modules, name, mod) < 0)
598599
return -1;
599600
if (_PyState_AddModule(mod, def) < 0) {
600-
PyDict_DelItemString(modules, name);
601+
PyDict_DelItem(modules, name);
601602
return -1;
602603
}
603604
if (def->m_size == -1) {
@@ -623,17 +624,17 @@ int
623624
_PyImport_FixupBuiltin(PyObject *mod, char *name)
624625
{
625626
int res;
626-
PyObject *filename;
627-
filename = PyUnicode_FromString(name);
628-
if (filename == NULL)
627+
PyObject *nameobj;
628+
nameobj = PyUnicode_FromString(name);
629+
if (nameobj == NULL)
629630
return -1;
630-
res = _PyImport_FixupExtensionUnicode(mod, name, filename);
631-
Py_DECREF(filename);
631+
res = _PyImport_FixupExtensionObject(mod, nameobj, nameobj);
632+
Py_DECREF(nameobj);
632633
return res;
633634
}
634635

635636
PyObject *
636-
_PyImport_FindExtensionUnicode(const char *name, PyObject *filename)
637+
_PyImport_FindExtensionObject(PyObject *name, PyObject *filename)
637638
{
638639
PyObject *mod, *mdict;
639640
PyModuleDef* def;
@@ -646,7 +647,7 @@ _PyImport_FindExtensionUnicode(const char *name, PyObject *filename)
646647
/* Module does not support repeated initialization */
647648
if (def->m_base.m_copy == NULL)
648649
return NULL;
649-
mod = PyImport_AddModule(name);
650+
mod = PyImport_AddModuleObject(name);
650651
if (mod == NULL)
651652
return NULL;
652653
mdict = PyModule_GetDict(mod);
@@ -661,16 +662,16 @@ _PyImport_FindExtensionUnicode(const char *name, PyObject *filename)
661662
mod = def->m_base.m_init();
662663
if (mod == NULL)
663664
return NULL;
664-
PyDict_SetItemString(PyImport_GetModuleDict(), name, mod);
665+
PyDict_SetItem(PyImport_GetModuleDict(), name, mod);
665666
Py_DECREF(mod);
666667
}
667668
if (_PyState_AddModule(mod, def) < 0) {
668-
PyDict_DelItemString(PyImport_GetModuleDict(), name);
669+
PyDict_DelItem(PyImport_GetModuleDict(), name);
669670
Py_DECREF(mod);
670671
return NULL;
671672
}
672673
if (Py_VerboseFlag)
673-
PySys_FormatStderr("import %s # previously loaded (%U)\n",
674+
PySys_FormatStderr("import %U # previously loaded (%R)\n",
674675
name, filename);
675676
return mod;
676677

@@ -679,12 +680,12 @@ _PyImport_FindExtensionUnicode(const char *name, PyObject *filename)
679680
PyObject *
680681
_PyImport_FindBuiltin(const char *name)
681682
{
682-
PyObject *res, *filename;
683-
filename = PyUnicode_FromString(name);
684-
if (filename == NULL)
683+
PyObject *res, *nameobj;
684+
nameobj = PyUnicode_FromString(name);
685+
if (nameobj == NULL)
685686
return NULL;
686-
res = _PyImport_FindExtensionUnicode(name, filename);
687-
Py_DECREF(filename);
687+
res = _PyImport_FindExtensionObject(nameobj, nameobj);
688+
Py_DECREF(nameobj);
688689
return res;
689690
}
690691

@@ -1491,11 +1492,12 @@ load_package(char *name, char *pathname)
14911492
/* Helper to test for built-in module */
14921493

14931494
static int
1494-
is_builtin(char *name)
1495+
is_builtin(PyObject *name)
14951496
{
1496-
int i;
1497+
int i, cmp;
14971498
for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
1498-
if (strcmp(name, PyImport_Inittab[i].name) == 0) {
1499+
cmp = PyUnicode_CompareWithASCIIString(name, PyImport_Inittab[i].name);
1500+
if (cmp == 0) {
14991501
if (PyImport_Inittab[i].initfunc == NULL)
15001502
return -1;
15011503
else
@@ -1617,7 +1619,7 @@ find_module(char *fullname, char *subname, PyObject *path, char *buf,
16171619
size_t saved_namelen;
16181620
char *saved_buf = NULL;
16191621
#endif
1620-
PyObject *fullname_obj;
1622+
PyObject *fullname_obj, *nameobj;
16211623

16221624
if (p_loader != NULL)
16231625
*p_loader = NULL;
@@ -1677,10 +1679,15 @@ find_module(char *fullname, char *subname, PyObject *path, char *buf,
16771679
}
16781680

16791681
if (path == NULL) {
1680-
if (is_builtin(name)) {
1682+
nameobj = PyUnicode_FromString(name);
1683+
if (nameobj == NULL)
1684+
return NULL;
1685+
if (is_builtin(nameobj)) {
1686+
Py_DECREF(nameobj);
16811687
strcpy(buf, name);
16821688
return &fd_builtin;
16831689
}
1690+
Py_DECREF(nameobj);
16841691
#ifdef MS_COREDLL
16851692
fp = _PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
16861693
if (fp != NULL) {
@@ -2086,40 +2093,35 @@ find_init_module(char *buf)
20862093
#endif /* HAVE_STAT */
20872094

20882095

2089-
static int init_builtin(char *); /* Forward */
2096+
static int init_builtin(PyObject *); /* Forward */
20902097

20912098
static PyObject*
2092-
load_builtin(char *name, char *pathname, int type)
2099+
load_builtin(PyObject *name, int type)
20932100
{
20942101
PyObject *m, *modules;
20952102
int err;
20962103

2097-
if (pathname != NULL && pathname[0] != '\0')
2098-
name = pathname;
2099-
21002104
if (type == C_BUILTIN)
21012105
err = init_builtin(name);
21022106
else
2103-
err = PyImport_ImportFrozenModule(name);
2107+
err = PyImport_ImportFrozenModuleObject(name);
21042108
if (err < 0)
21052109
return NULL;
21062110
if (err == 0) {
21072111
PyErr_Format(PyExc_ImportError,
2108-
"Purported %s module %.200s not found",
2109-
type == C_BUILTIN ?
2110-
"builtin" : "frozen",
2112+
"Purported %s module %R not found",
2113+
type == C_BUILTIN ? "builtin" : "frozen",
21112114
name);
21122115
return NULL;
21132116
}
21142117

21152118
modules = PyImport_GetModuleDict();
2116-
m = PyDict_GetItemString(modules, name);
2119+
m = PyDict_GetItem(modules, name);
21172120
if (m == NULL) {
21182121
PyErr_Format(
21192122
PyExc_ImportError,
2120-
"%s module %.200s not properly initialized",
2121-
type == C_BUILTIN ?
2122-
"builtin" : "frozen",
2123+
"%s module %R not properly initialized",
2124+
type == C_BUILTIN ? "builtin" : "frozen",
21232125
name);
21242126
return NULL;
21252127
}
@@ -2168,9 +2170,15 @@ load_module(char *name, FILE *fp, char *pathname, int type, PyObject *loader)
21682170
break;
21692171

21702172
case C_BUILTIN:
2171-
case PY_FROZEN:
2172-
m = load_builtin(name, pathname, type);
2173+
case PY_FROZEN: {
2174+
PyObject *nameobj = PyUnicode_FromString(name);
2175+
if (nameobj != NULL) {
2176+
m = load_builtin(nameobj, type);
2177+
Py_DECREF(nameobj);
2178+
} else
2179+
m = NULL;
21732180
break;
2181+
}
21742182

21752183
case IMP_HOOK: {
21762184
if (loader == NULL) {
@@ -2199,28 +2207,28 @@ load_module(char *name, FILE *fp, char *pathname, int type, PyObject *loader)
21992207
an exception set if the initialization failed. */
22002208

22012209
static int
2202-
init_builtin(char *name)
2210+
init_builtin(PyObject *name)
22032211
{
22042212
struct _inittab *p;
22052213

2206-
if (_PyImport_FindBuiltin(name) != NULL)
2214+
if (_PyImport_FindExtensionObject(name, name) != NULL)
22072215
return 1;
22082216

22092217
for (p = PyImport_Inittab; p->name != NULL; p++) {
22102218
PyObject *mod;
2211-
if (strcmp(name, p->name) == 0) {
2219+
if (PyUnicode_CompareWithASCIIString(name, p->name) == 0) {
22122220
if (p->initfunc == NULL) {
22132221
PyErr_Format(PyExc_ImportError,
2214-
"Cannot re-init internal module %.200s",
2222+
"Cannot re-init internal module %R",
22152223
name);
22162224
return -1;
22172225
}
22182226
if (Py_VerboseFlag)
2219-
PySys_WriteStderr("import %s # builtin\n", name);
2227+
PySys_FormatStderr("import %U # builtin\n", name);
22202228
mod = (*p->initfunc)();
22212229
if (mod == 0)
22222230
return -1;
2223-
if (_PyImport_FixupBuiltin(mod, name) < 0)
2231+
if (_PyImport_FixupExtensionObject(mod, name, name) < 0)
22242232
return -1;
22252233
/* FixupExtension has put the module into sys.modules,
22262234
so we can release our own reference. */
@@ -3286,10 +3294,10 @@ imp_find_module(PyObject *self, PyObject *args)
32863294
static PyObject *
32873295
imp_init_builtin(PyObject *self, PyObject *args)
32883296
{
3289-
char *name;
3297+
PyObject *name;
32903298
int ret;
32913299
PyObject *m;
3292-
if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
3300+
if (!PyArg_ParseTuple(args, "U:init_builtin", &name))
32933301
return NULL;
32943302
ret = init_builtin(name);
32953303
if (ret < 0)
@@ -3298,7 +3306,7 @@ imp_init_builtin(PyObject *self, PyObject *args)
32983306
Py_INCREF(Py_None);
32993307
return Py_None;
33003308
}
3301-
m = PyImport_AddModule(name);
3309+
m = PyImport_AddModuleObject(name);
33023310
Py_XINCREF(m);
33033311
return m;
33043312
}
@@ -3346,8 +3354,8 @@ imp_is_frozen_package(PyObject *self, PyObject *args)
33463354
static PyObject *
33473355
imp_is_builtin(PyObject *self, PyObject *args)
33483356
{
3349-
char *name;
3350-
if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
3357+
PyObject *name;
3358+
if (!PyArg_ParseTuple(args, "U:is_builtin", &name))
33513359
return NULL;
33523360
return PyLong_FromLong(is_builtin(name));
33533361
}

Python/importdl.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,23 @@ _PyImport_LoadDynamicModule(char *name, char *pathname, FILE *fp)
2626
dl_funcptr p0;
2727
PyObject* (*p)(void);
2828
struct PyModuleDef *def;
29-
PyObject *result;
29+
PyObject *nameobj, *result;
3030

3131
path = PyUnicode_DecodeFSDefault(pathname);
3232
if (path == NULL)
3333
return NULL;
3434

35-
if ((m = _PyImport_FindExtensionUnicode(name, path)) != NULL) {
35+
nameobj = PyUnicode_FromString(name);
36+
if (nameobj == NULL)
37+
return NULL;
38+
m = _PyImport_FindExtensionObject(nameobj, path);
39+
if (m != NULL) {
40+
Py_DECREF(nameobj);
3641
Py_INCREF(m);
3742
result = m;
3843
goto finally;
3944
}
45+
Py_DECREF(nameobj);
4046
lastdot = strrchr(name, '.');
4147
if (lastdot == NULL) {
4248
packagecontext = NULL;
@@ -82,12 +88,18 @@ _PyImport_LoadDynamicModule(char *name, char *pathname, FILE *fp)
8288
else
8389
Py_INCREF(path);
8490

85-
if (_PyImport_FixupExtensionUnicode(m, name, path) < 0)
91+
nameobj = PyUnicode_FromString(name);
92+
if (nameobj == NULL)
93+
return NULL;
94+
if (_PyImport_FixupExtensionObject(m, nameobj, path) < 0) {
95+
Py_DECREF(nameobj);
8696
goto error;
97+
}
8798
if (Py_VerboseFlag)
88-
PySys_WriteStderr(
89-
"import %s # dynamically loaded from %s\n",
90-
name, pathname);
99+
PySys_FormatStderr(
100+
"import %U # dynamically loaded from %s\n",
101+
nameobj, pathname);
102+
Py_DECREF(nameobj);
91103
result = m;
92104
goto finally;
93105

0 commit comments

Comments
 (0)