Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32.2k
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-unicodeOct 20, 2022
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
7 commits Select commitHold shift + click to select a range
866bf67
Add unicode check for 'name' attribute in _imp_create_builtin
chgnrdvcf9fa50
Merge branch 'main' into _imp_create_builtin-check-name-for-unicode
chgnrdv4d0d69b
Added missing Py_DECREF
chgnrdvea78278
Merge branch '_imp_create_builtin-check-name-for-unicode' of https://…
chgnrdv48b46a4
Added tests
chgnrdv010add5
Fixed tests because sys module import breaks them
chgnrdv964da44
📜🤖 Added by blurb_it.
blurb-it[bot]File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
34 changes: 34 additions & 0 deletionsLib/test/test_imp.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
chgnrdv marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
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): | ||
1 change: 1 addition & 0 deletionsMisc/NEWS.d/next/Core and Builtins/2022-10-19-18-03-28.gh-issue-98354.GRGta3.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Added unicode check for ``name`` attribute of ``spec`` argument passed in :func:`_imp.create_builtin` function. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
chgnrdv marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
} | ||
PyObject *mod = create_builtin(tstate, name, spec); | ||
Py_DECREF(name); | ||
return mod; | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.