mirror of
https://github.com/python/cpython
synced 2024-10-14 20:02:02 +00:00
gh-98354: Add unicode check for 'name' attribute in _imp_create_builtin (GH-98412)
Fixes #98354
This commit is contained in:
parent
a8fe4bbd6b
commit
1f369ad07f
|
@ -378,6 +378,40 @@ def test_find_and_load_checked_pyc(self):
|
|||
mod = imp.load_module('mymod', file, path, description)
|
||||
self.assertEqual(mod.x, 42)
|
||||
|
||||
def test_issue98354(self):
|
||||
# _imp.create_builtin should raise TypeError
|
||||
# if 'name' attribute of 'spec' argument is not a 'str' instance
|
||||
|
||||
create_builtin = support.get_attribute(_imp, "create_builtin")
|
||||
|
||||
class FakeSpec:
|
||||
def __init__(self, name):
|
||||
self.name = self
|
||||
spec = FakeSpec("time")
|
||||
with self.assertRaises(TypeError):
|
||||
create_builtin(spec)
|
||||
|
||||
class FakeSpec2:
|
||||
name = [1, 2, 3, 4]
|
||||
spec = FakeSpec2()
|
||||
with self.assertRaises(TypeError):
|
||||
create_builtin(spec)
|
||||
|
||||
import builtins
|
||||
class UnicodeSubclass(str):
|
||||
pass
|
||||
class GoodSpec:
|
||||
name = UnicodeSubclass("builtins")
|
||||
spec = GoodSpec()
|
||||
bltin = create_builtin(spec)
|
||||
self.assertEqual(bltin, builtins)
|
||||
|
||||
class UnicodeSubclassFakeSpec(str):
|
||||
def __init__(self, name):
|
||||
self.name = self
|
||||
spec = UnicodeSubclassFakeSpec("builtins")
|
||||
bltin = create_builtin(spec)
|
||||
self.assertEqual(bltin, builtins)
|
||||
|
||||
class ReloadTests(unittest.TestCase):
|
||||
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
Added unicode check for ``name`` attribute of ``spec`` argument passed in :func:`_imp.create_builtin` function.
|
|
@ -1021,6 +1021,14 @@ _imp_create_builtin(PyObject *module, PyObject *spec)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
if (!PyUnicode_Check(name)) {
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"name must be string, not %.200s",
|
||||
Py_TYPE(name)->tp_name);
|
||||
Py_DECREF(name);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
PyObject *mod = create_builtin(tstate, name, spec);
|
||||
Py_DECREF(name);
|
||||
return mod;
|
||||
|
|
Loading…
Reference in a new issue