Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-98354: Add unicode check for 'name' attribute in _imp_create_builtin#98412

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
miss-islington merged 7 commits intopython:mainfromchgnrdv:_imp_create_builtin-check-name-for-unicode
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletionsLib/test/test_imp.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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):

Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
Added unicode check for ``name`` attribute of ``spec`` argument passed in :func:`_imp.create_builtin` function.
8 changes: 8 additions & 0 deletionsPython/import.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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;
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp