diff options
author | chgnrdv <52372310+chgnrdv@users.noreply.github.com> | 2022-10-20 03:25:10 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-19 17:25:10 -0700 |
commit | 1f369ad07ff44b1fd6db75b33298e20ad604efc8 (patch) | |
tree | 42ac5acd81c4097c33a7073b9c4ccfaf2997c900 /Lib/test/test_imp.py | |
parent | a8fe4bbd6b78517f640e25697338b9448c4675c1 (diff) | |
download | cpython-1f369ad07ff44b1fd6db75b33298e20ad604efc8.tar.gz cpython-1f369ad07ff44b1fd6db75b33298e20ad604efc8.zip |
gh-98354: Add unicode check for 'name' attribute in _imp_create_builtin (GH-98412)
Fixes #98354
Diffstat (limited to 'Lib/test/test_imp.py')
-rw-r--r-- | Lib/test/test_imp.py | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/Lib/test/test_imp.py b/Lib/test/test_imp.py index 35b6afa91eb..446e913e5bf 100644 --- a/Lib/test/test_imp.py +++ b/Lib/test/test_imp.py @@ -378,6 +378,40 @@ class ImportTests(unittest.TestCase): 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): |