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

Commitf64b60b

Browse files
committed
gh-126105: Fix crash inast module, when._fields is deleted
1 parent9b14083 commitf64b60b

File tree

4 files changed

+38
-16
lines changed

4 files changed

+38
-16
lines changed

‎Lib/test/test_ast/test_ast.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,23 @@ def test_AST_objects(self):
8484
# "ast.AST constructor takes 0 positional arguments"
8585
ast.AST(2)
8686

87+
deftest_AST_fields_NULL_check(self):
88+
# See: https://github.com/python/cpython/issues/126105
89+
old_value=ast.AST._fields
90+
91+
defcleanup():
92+
ast.AST._fields=old_value
93+
self.addCleanup(cleanup)
94+
95+
delast.AST._fields
96+
97+
msg='AST has no fields'
98+
# Both examples used to crash:
99+
withself.assertRaisesRegex(TypeError,msg):
100+
ast.AST(arg1=123)
101+
withself.assertRaisesRegex(TypeError,msg):
102+
ast.AST()
103+
87104
deftest_AST_garbage_collection(self):
88105
classX:
89106
pass
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a crash in:mod:`ast` when ``_fields`` attribute is deleted.

‎Parser/asdl_c.py‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -887,16 +887,18 @@ def visitModule(self, mod):
887887
if (PyObject_GetOptionalAttr((PyObject*)Py_TYPE(self), state->_fields, &fields) < 0) {
888888
goto cleanup;
889889
}
890-
if (fields) {
891-
numfields = PySequence_Size(fields);
892-
if (numfields == -1) {
893-
goto cleanup;
894-
}
895-
remaining_fields = PySet_New(fields);
890+
if (fields == NULL) {
891+
PyErr_Format(PyExc_TypeError,
892+
"%.400s has no fields",
893+
_PyType_Name(Py_TYPE(self)));
894+
goto cleanup;
896895
}
897-
else {
898-
remaining_fields = PySet_New(NULL);
896+
897+
numfields = PySequence_Size(fields);
898+
if (numfields == -1) {
899+
goto cleanup;
899900
}
901+
remaining_fields = PySet_New(fields);
900902
if (remaining_fields == NULL) {
901903
goto cleanup;
902904
}

‎Python/Python-ast.c‎

Lines changed: 10 additions & 8 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp