Skip to content

Commit 9211e2f

Browse files
pablogsalmiss-islington
authored andcommitted
bpo-37268: Add deprecation notice and a DeprecationWarning for the parser module (pythonGH-15017)
Deprecate the parser module and add a deprecation warning triggered on import and a warning block in the documentation. https://bugs.python.org/issue37268 Automerge-Triggered-By: @pablogsal
1 parent f35c51d commit 9211e2f

File tree

5 files changed

+25
-4
lines changed

5 files changed

+25
-4
lines changed

Doc/library/parser.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ from this. This is better than trying to parse and modify an arbitrary Python
2525
code fragment as a string because parsing is performed in a manner identical to
2626
the code forming the application. It is also faster.
2727

28-
.. note::
28+
.. warning::
2929

30-
From Python 2.5 onward, it's much more convenient to cut in at the Abstract
31-
Syntax Tree (AST) generation and compilation stage, using the :mod:`ast`
32-
module.
30+
The parser module is deprecated and will be removed in future versions of
31+
Python. For the majority of use cases you can leverage the Abstract Syntax
32+
Tree (AST) generation and compilation stage, using the :mod:`ast` module.
3333

3434
There are a few things to note about this module which are important to making
3535
use of the data structures created. This is not a tutorial on editing the parse

Doc/whatsnew/3.9.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ Deprecated
142142
Python versions it will raise a :exc:`TypeError` for all floats.
143143
(Contributed by Serhiy Storchaka in :issue:`37315`.)
144144

145+
* The :mod:`parser` module is deprecated and will be removed in future versions
146+
of Python. For the majority of use cases users can leverage the Abstract Syntax
147+
Tree (AST) generation and compilation stage, using the :mod:`ast` module.
148+
145149

146150
Removed
147151
=======

Lib/test/test_parser.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import struct
77
from test import support
88
from test.support.script_helper import assert_python_failure
9+
from test.support.script_helper import assert_python_ok
910

1011
#
1112
# First, we test that we can generate trees from valid source fragments,
@@ -987,5 +988,13 @@ def test_two_args_to_expr(self):
987988
with self.assertRaises(TypeError):
988989
parser.expr("a", "b")
989990

991+
992+
class TestDeprecation(unittest.TestCase):
993+
def test_deprecation_message(self):
994+
code = "def f():\n import parser\n\nf()"
995+
rc, out, err = assert_python_ok('-c', code)
996+
self.assertIn(b'<string>:2: DeprecationWarning', err)
997+
998+
990999
if __name__ == "__main__":
9911000
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The :mod:`parser` module is deprecated and will be removed in future
2+
versions of Python.

Modules/parsermodule.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,6 +1158,12 @@ PyInit_parser(void)
11581158
{
11591159
PyObject *module, *copyreg;
11601160

1161+
if (PyErr_WarnEx(PyExc_DeprecationWarning,
1162+
"The parser module is deprecated and will be removed "
1163+
"in future versions of Python", 7) != 0) {
1164+
return NULL;
1165+
}
1166+
11611167
if (PyType_Ready(&PyST_Type) < 0)
11621168
return NULL;
11631169
module = PyModule_Create(&parsermodule);

0 commit comments

Comments
 (0)