-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
gh-116021: Deprecate support for instantiating abstract AST nodes #137865
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
1b767fc
b72a362
c8dda97
e550c2a
90b7428
16db374
2f390c8
97dbdab
66586e7
250c33c
f8c0aef
b4ef16b
adcacdd
b7100a4
202a372
0eb074e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1877,7 +1877,8 @@ def test_pythontypes(self): | |
| check = self.check_sizeof | ||
| # _ast.AST | ||
| import _ast | ||
| check(_ast.AST(), size('P')) | ||
| with self.assertWarns(DeprecationWarning): | ||
| check(_ast.AST(), size('P')) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should probably be rewritten, but I'm not sure I understand the goal of this test well enough to do that. Suggestions welcome!
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's to check that objects in For now, leave it here. A separate issue should be opened.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's here because it's arguably testing
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @JelleZijlstra @picnixz any further thoughts re: above? Happy to update this is someone can confirm a good substitute
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, what you suggest should work (haven't verified the object size is right).
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking again |
||
| try: | ||
| raise TypeError | ||
| except TypeError as e: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Support for creating instances of abstract AST nodes from the :mod:`ast` module | ||
| is deprecated and scheduled for removal in Python 3.20. Patch by Brian Schubert. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -881,6 +881,21 @@ def visitModule(self, mod): | |
| return -1; | ||
| } | ||
|
|
||
| int contains = PySet_Contains(state->abstract_types, (PyObject *)Py_TYPE(self)); | ||
| if (contains == -1) { | ||
| return -1; | ||
| } | ||
| else if (contains == 1) { | ||
| if (PyErr_WarnFormat( | ||
| PyExc_DeprecationWarning, 1, | ||
| "Instantiating abstract AST node class %T is deprecated. " | ||
| "This will become an error in Python 3.20", | ||
| self | ||
| ) < 0) { | ||
| return -1; | ||
| } | ||
|
brianschubert marked this conversation as resolved.
|
||
| } | ||
|
|
||
| Py_ssize_t i, numfields = 0; | ||
| int res = -1; | ||
| PyObject *key, *value, *fields, *attributes = NULL, *remaining_fields = NULL; | ||
|
|
@@ -1443,6 +1458,23 @@ def visitModule(self, mod): | |
| return result; | ||
| } | ||
|
|
||
| /* Helper for checking if a node class is abstract in the tests. */ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should use clinic here, but this would create a new file as we don't use it at all. Maybe we can do it in a follow-up PR though. |
||
| static PyObject * | ||
| ast_is_abstract(PyObject *cls, void *Py_UNUSED(ignored)) { | ||
| struct ast_state *state = get_ast_state(); | ||
| if (state == NULL) { | ||
| return NULL; | ||
| } | ||
| int contains = PySet_Contains(state->abstract_types, cls); | ||
| if (contains == -1) { | ||
| return NULL; | ||
| } | ||
| else if (contains == 1) { | ||
| Py_RETURN_TRUE; | ||
| } | ||
| Py_RETURN_FALSE; | ||
| } | ||
|
|
||
| static PyMemberDef ast_type_members[] = { | ||
| {"__dictoffset__", Py_T_PYSSIZET, offsetof(AST_object, dict), Py_READONLY}, | ||
| {NULL} /* Sentinel */ | ||
|
|
@@ -1454,6 +1486,7 @@ def visitModule(self, mod): | |
| PyDoc_STR("__replace__($self, /, **fields)\\n--\\n\\n" | ||
| "Return a copy of the AST node with new values " | ||
| "for the specified fields.")}, | ||
| {"_is_abstract", _PyCFunction_CAST(ast_is_abstract), METH_CLASS | METH_NOARGS, NULL}, | ||
|
brianschubert marked this conversation as resolved.
Outdated
|
||
| {NULL} | ||
| }; | ||
|
|
||
|
|
@@ -1887,6 +1920,13 @@ def visitModule(self, mod): | |
| if (!state->AST_type) { | ||
| return -1; | ||
| } | ||
| state->abstract_types = PySet_New(NULL); | ||
| if (!state->abstract_types) { | ||
| return -1; | ||
| } | ||
| if (PySet_Add(state->abstract_types, state->AST_type) < 0) { | ||
| return -1; | ||
| } | ||
| if (add_ast_fields(state) < 0) { | ||
| return -1; | ||
| } | ||
|
|
@@ -1928,6 +1968,7 @@ def visitSum(self, sum, name): | |
| (name, name, len(sum.attributes)), 1) | ||
| else: | ||
| self.emit("if (add_attributes(state, state->%s_type, NULL, 0) < 0) return -1;" % name, 1) | ||
| self.emit("if (PySet_Add(state->abstract_types, state->%s_type) < 0) return -1;" % name, 1) | ||
| self.emit_defaults(name, sum.attributes, 1) | ||
| simple = is_simple(sum) | ||
| for t in sum.types: | ||
|
|
@@ -2289,6 +2330,7 @@ def generate_module_def(mod, metadata, f, internal_h): | |
| "%s_type" % type | ||
| for type in metadata.types | ||
| ) | ||
| module_state.add("abstract_types") | ||
|
|
||
| state_strings = sorted(state_strings) | ||
| module_state = sorted(module_state) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.