Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bace59d

Browse files
bpo-39999: Improve compatibility of the ast module. (GH-19056)
* Re-add removed classes Suite, slice, Param, AugLoad and AugStore. * Add docstrings for dummy classes. * Add docstrings for attribute aliases. * Set __module__ to "ast" instead of "_ast".
1 parent 044cf94 commit bace59d

10 files changed

Lines changed: 49 additions & 22 deletions

Doc/whatsnew/3.9.rst

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -593,12 +593,19 @@ Deprecated
593593

594594
(Contributed by Victor Stinner in :issue:`39353`.)
595595

596-
* :mod:`ast` classes ``Index`` and ``ExtSlice`` are considered deprecated
596+
* :mod:`ast` classes ``slice``, ``Index`` and ``ExtSlice`` are considered deprecated
597597
and will be removed in future Python versions. ``value`` itself should be
598598
used instead of ``Index(value)``. ``Tuple(slices, Load())`` should be
599599
used instead of ``ExtSlice(slices)``.
600600
(Contributed by Serhiy Storchaka in :issue:`32892`.)
601601

602+
* :mod:`ast` classes ``Suite``, ``Param``, ``AugLoad`` and ``AugStore``
603+
are considered deprecated and will be removed in future Python versions.
604+
They were not generated by the parser and not accepted by the code
605+
generator in Python 3.
606+
(Contributed by Batuhan Taskaya in :issue:`39639` and :issue:`39969`
607+
and Serhiy Storchaka in :issue:`39988`.)
608+
602609
* The :c:func:`PyEval_InitThreads` and :c:func:`PyEval_ThreadsInitialized`
603610
functions are now deprecated and will be removed in Python 3.11. Calling
604611
:c:func:`PyEval_InitThreads` now does nothing. The :term:`GIL` is initialized
@@ -704,11 +711,6 @@ Removed
704711
defining ``COUNT_ALLOCS`` macro.
705712
(Contributed by Victor Stinner in :issue:`39489`.)
706713

707-
* The ``ast.Suite``, ``ast.Param``, ``ast.AugLoad`` and ``ast.AugStore``
708-
node classes have been removed due to no longer being needed.
709-
(Contributed by Batuhan Taskaya in :issue:`39639` and :issue:`39969`
710-
and Serhiy Storchaka in :issue:`39988`.)
711-
712714

713715
Porting to Python 3.9
714716
=====================

Lib/ast.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,7 @@ def generic_visit(self, node):
489489
# It will be removed in future.
490490

491491
def _getter(self):
492+
"""Deprecated. Use value instead."""
492493
return self.value
493494

494495
def _setter(self, value):
@@ -499,6 +500,9 @@ def _setter(self, value):
499500

500501
class _ABC(type):
501502

503+
def __init__(cls, *args):
504+
cls.__doc__ = """Deprecated AST node class. Use ast.Constant instead"""
505+
502506
def __instancecheck__(cls, inst):
503507
if not isinstance(inst, Constant):
504508
return False
@@ -564,22 +568,40 @@ def __new__(cls, *args, **kwargs):
564568
type(...): 'Ellipsis',
565569
}
566570

567-
class Index(AST):
571+
class slice(AST):
572+
"""Deprecated AST node class."""
573+
574+
class Index(slice):
575+
"""Deprecated AST node class. Use the index value directly instead."""
568576
def __new__(cls, value, **kwargs):
569577
return value
570578

571-
class ExtSlice(AST):
579+
class ExtSlice(slice):
580+
"""Deprecated AST node class. Use ast.Tuple instead."""
572581
def __new__(cls, dims=(), **kwargs):
573582
return Tuple(list(dims), Load(), **kwargs)
574583

575584
def _dims_getter(self):
585+
"""Deprecated. Use elts instead."""
576586
return self.elts
577587

578588
def _dims_setter(self, value):
579589
self.elts = value
580590

581591
Tuple.dims = property(_dims_getter, _dims_setter)
582592

593+
class Suite(mod):
594+
"""Deprecated AST node class. Unused in Python 3."""
595+
596+
class AugLoad(expr_context):
597+
"""Deprecated AST node class. Unused in Python 3."""
598+
599+
class AugStore(expr_context):
600+
"""Deprecated AST node class. Unused in Python 3."""
601+
602+
class Param(expr_context):
603+
"""Deprecated AST node class. Unused in Python 3."""
604+
583605

584606
# Large float and imaginary literals get turned into infinities in the AST.
585607
# We unparse those infinities to INFSTR.

Lib/test/test_ast.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ def test_AST_objects(self):
283283
x.vararg
284284

285285
with self.assertRaises(TypeError):
286-
# "_ast.AST constructor takes 0 positional arguments"
286+
# "ast.AST constructor takes 0 positional arguments"
287287
ast.AST(2)
288288

289289
def test_AST_garbage_collection(self):
@@ -573,7 +573,7 @@ def test_invalid_sum(self):
573573
m = ast.Module([ast.Expr(ast.expr(**pos), **pos)], [])
574574
with self.assertRaises(TypeError) as cm:
575575
compile(m, "<test>", "exec")
576-
self.assertIn("but got <_ast.expr", str(cm.exception))
576+
self.assertIn("but got <ast.expr", str(cm.exception))
577577

578578
def test_invalid_identifier(self):
579579
m = ast.Module([ast.Expr(ast.Name(42, ast.Load()))], [])

Misc/NEWS.d/next/Core and Builtins/2020-02-15-15-29-34.bpo-39639.3mqJjm.rst

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Deprecated ``ast.Suite`` node class because it's no longer used. Patch by Batuhan Taskaya.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Remove ``ast.Param`` node class because it's no longer used. Patch by
1+
Deprecated ``ast.Param`` node class because it's no longer used. Patch by
22
Batuhan Taskaya.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Removed ``ast.AugLoad`` and ``ast.AugStore`` node classes because they are
1+
Deprecated ``ast.AugLoad`` and ``ast.AugStore`` node classes because they are
22
no longer used.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
``__module__`` of the AST node classes is now set to "ast" instead of
2+
"_ast". Added docstrings for dummy AST node classes and deprecated
3+
attributes.

Parser/asdl_c.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ def visitModule(self, mod):
774774
};
775775
776776
static PyType_Spec AST_type_spec = {
777-
"_ast.AST",
777+
"ast.AST",
778778
sizeof(AST_object),
779779
0,
780780
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
@@ -800,7 +800,7 @@ def visitModule(self, mod):
800800
type, base,
801801
astmodulestate_global->_fields, fnames,
802802
astmodulestate_global->__module__,
803-
astmodulestate_global->_ast,
803+
astmodulestate_global->ast,
804804
astmodulestate_global->__doc__, doc);
805805
Py_DECREF(fnames);
806806
return result;
@@ -1302,7 +1302,7 @@ def generate_module_def(f, mod):
13021302
visitor_list.add(visitor)
13031303

13041304
state_strings = {
1305-
"_ast",
1305+
"ast",
13061306
"_fields",
13071307
"__doc__",
13081308
"__dict__",

Python/Python-ast.c

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)