mirror of
https://github.com/python/cpython.git
synced 2025-12-09 10:37:17 +00:00
gh-126105: Fix crash in ast module, when ._fields is deleted (#126115)
Previously, if the `ast.AST._fields` attribute was deleted, attempts to create a new `as`t node would crash due to the assumption that `_fields` always had a non-NULL value. Now it has been fixed by adding an extra check to ensure that `_fields` does not have a NULL value (this can happen when you manually remove `_fields` attribute).
This commit is contained in:
parent
0bbbe15f56
commit
b2eaa75b17
4 changed files with 34 additions and 20 deletions
|
|
@ -84,6 +84,23 @@ class AST_Tests(unittest.TestCase):
|
|||
# "ast.AST constructor takes 0 positional arguments"
|
||||
ast.AST(2)
|
||||
|
||||
def test_AST_fields_NULL_check(self):
|
||||
# See: https://github.com/python/cpython/issues/126105
|
||||
old_value = ast.AST._fields
|
||||
|
||||
def cleanup():
|
||||
ast.AST._fields = old_value
|
||||
self.addCleanup(cleanup)
|
||||
|
||||
del ast.AST._fields
|
||||
|
||||
msg = "type object 'ast.AST' has no attribute '_fields'"
|
||||
# Both examples used to crash:
|
||||
with self.assertRaisesRegex(AttributeError, msg):
|
||||
ast.AST(arg1=123)
|
||||
with self.assertRaisesRegex(AttributeError, msg):
|
||||
ast.AST()
|
||||
|
||||
def test_AST_garbage_collection(self):
|
||||
class X:
|
||||
pass
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue