Skip to content

Commit f6bd00b

Browse files
gh-135801: Add the module parameter to compile() etc
Many functions related to compiling or parsing Python code, such as compile(), ast.parse(), symtable.symtable(), and importlib.InspectLoader.source_to_code() now allow to pass the module name used when filtering syntax warnings.
1 parent 6f3dae0 commit f6bd00b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+454
-122
lines changed

Doc/library/ast.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2200,10 +2200,10 @@ Async and await
22002200
Apart from the node classes, the :mod:`ast` module defines these utility functions
22012201
and classes for traversing abstract syntax trees:
22022202

2203-
.. function:: parse(source, filename='<unknown>', mode='exec', *, type_comments=False, feature_version=None, optimize=-1)
2203+
.. function:: parse(source, filename='<unknown>', mode='exec', *, type_comments=False, feature_version=None, optimize=-1, module=None)
22042204

22052205
Parse the source into an AST node. Equivalent to ``compile(source,
2206-
filename, mode, flags=FLAGS_VALUE, optimize=optimize)``,
2206+
filename, mode, flags=FLAGS_VALUE, optimize=optimize, module=module)``,
22072207
where ``FLAGS_VALUE`` is ``ast.PyCF_ONLY_AST`` if ``optimize <= 0``
22082208
and ``ast.PyCF_OPTIMIZED_AST`` otherwise.
22092209

@@ -2256,6 +2256,9 @@ and classes for traversing abstract syntax trees:
22562256
The minimum supported version for ``feature_version`` is now ``(3, 7)``.
22572257
The ``optimize`` argument was added.
22582258

2259+
.. versionadded:: next
2260+
Added the *module* parameter.
2261+
22592262

22602263
.. function:: unparse(ast_obj)
22612264

Doc/library/functions.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,9 @@ are always available. They are listed here in alphabetical order.
292292
:func:`property`.
293293

294294

295-
.. function:: compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1)
295+
.. function:: compile(source, filename, mode, flags=0,
296+
dont_inherit=False, optimize=-1,
297+
*, module=None)
296298
297299
Compile the *source* into a code or AST object. Code objects can be executed
298300
by :func:`exec` or :func:`eval`. *source* can either be a normal string, a
@@ -334,6 +336,9 @@ are always available. They are listed here in alphabetical order.
334336
``__debug__`` is true), ``1`` (asserts are removed, ``__debug__`` is false)
335337
or ``2`` (docstrings are removed too).
336338

339+
The optional argument *module* specifies the module name used
340+
when filtering syntax warnings.
341+
337342
This function raises :exc:`SyntaxError` if the compiled source is invalid,
338343
and :exc:`ValueError` if the source contains null bytes.
339344

@@ -371,6 +376,9 @@ are always available. They are listed here in alphabetical order.
371376
``ast.PyCF_ALLOW_TOP_LEVEL_AWAIT`` can now be passed in flags to enable
372377
support for top-level ``await``, ``async for``, and ``async with``.
373378

379+
.. versionadded:: next
380+
Added the *module* parameter.
381+
374382

375383
.. class:: complex(number=0, /)
376384
complex(string, /)

Doc/library/importlib.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ ABC hierarchy::
459459
.. versionchanged:: 3.4
460460
Raises :exc:`ImportError` instead of :exc:`NotImplementedError`.
461461

462-
.. staticmethod:: source_to_code(data, path='<string>')
462+
.. staticmethod:: source_to_code(data, path='<string>', fullname=None)
463463

464464
Create a code object from Python source.
465465

@@ -471,11 +471,18 @@ ABC hierarchy::
471471
With the subsequent code object one can execute it in a module by
472472
running ``exec(code, module.__dict__)``.
473473

474+
The optional argument *fullname* specifies the name of the module used
475+
when filtering syntax warnings.
476+
474477
.. versionadded:: 3.4
475478

476479
.. versionchanged:: 3.5
477480
Made the method static.
478481

482+
.. versionadded:: next
483+
Added the *fullname* parameter.
484+
485+
479486
.. method:: exec_module(module)
480487

481488
Implementation of :meth:`Loader.exec_module`.

Doc/library/symtable.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,16 @@ tables.
2121
Generating Symbol Tables
2222
------------------------
2323

24-
.. function:: symtable(code, filename, compile_type)
24+
.. function:: symtable(code, filename, compile_type, *, module=None)
2525

2626
Return the toplevel :class:`SymbolTable` for the Python source *code*.
2727
*filename* is the name of the file containing the code. *compile_type* is
2828
like the *mode* argument to :func:`compile`.
29+
The optional argument *module* specifies the module name used
30+
when filtering syntax warnings.
31+
32+
.. versionadded:: next
33+
Added the *module* parameter.
2934

3035

3136
Examining Symbol Tables

Doc/whatsnew/3.15.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,12 @@ Other language changes
284284
not only integers or floats, although this does not improve precision.
285285
(Contributed by Serhiy Storchaka in :gh:`67795`.)
286286

287+
* Many functions related to compiling or parsing Python code, such as
288+
:func:`compile`, :func:`ast.parse`, :func:`symtable.symtable`,
289+
and :func:`importlib.InspectLoader.source_to_code` now allow to pass
290+
the module name used when filtering syntax warnings.
291+
(Contributed by Serhiy Storchaka in :gh:`135801`.)
292+
287293

288294
New modules
289295
===========

Include/cpython/warnings.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,3 @@ PyAPI_FUNC(int) PyErr_WarnExplicitFormat(
1818

1919
// DEPRECATED: Use PyErr_WarnEx() instead.
2020
#define PyErr_Warn(category, msg) PyErr_WarnEx((category), (msg), 1)
21-
22-
int _PyErr_WarnExplicitObjectWithContext(
23-
PyObject *category,
24-
PyObject *message,
25-
PyObject *filename,
26-
int lineno);

Include/internal/pycore_compile.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ PyAPI_FUNC(PyCodeObject*) _PyAST_Compile(
3232
PyObject *filename,
3333
PyCompilerFlags *flags,
3434
int optimize,
35-
struct _arena *arena);
35+
struct _arena *arena,
36+
PyObject *module);
3637

3738
/* AST preprocessing */
3839
extern int _PyCompile_AstPreprocess(
@@ -41,15 +42,17 @@ extern int _PyCompile_AstPreprocess(
4142
PyCompilerFlags *flags,
4243
int optimize,
4344
struct _arena *arena,
44-
int syntax_check_only);
45+
int syntax_check_only,
46+
PyObject *module);
4547

4648
extern int _PyAST_Preprocess(
4749
struct _mod *,
4850
struct _arena *arena,
4951
PyObject *filename,
5052
int optimize,
5153
int ff_features,
52-
int syntax_check_only);
54+
int syntax_check_only,
55+
PyObject *module);
5356

5457

5558
typedef struct {

Include/internal/pycore_parser.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ extern struct _mod* _PyParser_ASTFromString(
4848
PyObject* filename,
4949
int mode,
5050
PyCompilerFlags *flags,
51-
PyArena *arena);
51+
PyArena *arena,
52+
PyObject *module);
5253

5354
extern struct _mod* _PyParser_ASTFromFile(
5455
FILE *fp,

Include/internal/pycore_pyerrors.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ extern void _PyErr_SetNone(PyThreadState *tstate, PyObject *exception);
123123
extern PyObject* _PyErr_NoMemory(PyThreadState *tstate);
124124

125125
extern int _PyErr_EmitSyntaxWarning(PyObject *msg, PyObject *filename, int lineno, int col_offset,
126-
int end_lineno, int end_col_offset);
126+
int end_lineno, int end_col_offset,
127+
PyObject *module);
127128
extern void _PyErr_RaiseSyntaxError(PyObject *msg, PyObject *filename, int lineno, int col_offset,
128129
int end_lineno, int end_col_offset);
129130

Include/internal/pycore_pythonrun.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ extern const char* _Py_SourceAsString(
3333
PyCompilerFlags *cf,
3434
PyObject **cmd_copy);
3535

36+
extern PyObject * _Py_CompileStringObjectWithModule(
37+
const char *str,
38+
PyObject *filename, int start,
39+
PyCompilerFlags *flags, int optimize,
40+
PyObject *module);
41+
3642

3743
/* Stack size, in "pointers". This must be large enough, so
3844
* no two calls to check recursion depth are more than this far

0 commit comments

Comments
 (0)