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

Commit9a927b3

Browse files
gh-135801: Add the module parameter to compile() etc
Many functions related to compiling or parsing Python code, such ascompile(), ast.parse(), symtable.symtable(),and importlib.InspectLoader.source_to_code() now allow to passthe module name used when filtering syntax warnings.
1 parent7ac94fc commit9a927b3

File tree

46 files changed

+404
-95
lines changed

Some content is hidden

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

46 files changed

+404
-95
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
@@ -307,6 +307,12 @@ Other language changes
307307
not only integers or floats, although this does not improve precision.
308308
(Contributed by Serhiy Storchaka in:gh:`67795`.)
309309

310+
* Many functions related to compiling or parsing Python code, such as
311+
:func:`compile`,:func:`ast.parse`,:func:`symtable.symtable`,
312+
and:meth:`importlib.InspectLoader.source_to_code` now allow to pass
313+
the module name used when filtering syntax warnings.
314+
(Contributed by Serhiy Storchaka in:gh:`135801`.)
315+
310316

311317
New modules
312318
===========

‎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
intoptimize,
35-
struct_arena*arena);
35+
struct_arena*arena,
36+
PyObject*module);
3637

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

4648
externint_PyAST_Preprocess(
4749
struct_mod*,
4850
struct_arena*arena,
4951
PyObject*filename,
5052
intoptimize,
5153
intff_features,
52-
intsyntax_check_only);
54+
intsyntax_check_only,
55+
PyObject*module);
5356

5457

5558
typedefstruct {

‎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
intmode,
5050
PyCompilerFlags*flags,
51-
PyArena*arena);
51+
PyArena*arena,
52+
PyObject*module);
5253

5354
externstruct_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
externPyObject*_PyErr_NoMemory(PyThreadState*tstate);
124124

125125
externint_PyErr_EmitSyntaxWarning(PyObject*msg,PyObject*filename,intlineno,intcol_offset,
126-
intend_lineno,intend_col_offset);
126+
intend_lineno,intend_col_offset,
127+
PyObject*module);
127128
externvoid_PyErr_RaiseSyntaxError(PyObject*msg,PyObject*filename,intlineno,intcol_offset,
128129
intend_lineno,intend_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+
externPyObject*_Py_CompileStringObjectWithModule(
37+
constchar*str,
38+
PyObject*filename,intstart,
39+
PyCompilerFlags*flags,intoptimize,
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

‎Include/internal/pycore_symtable.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ extern struct symtable* _Py_SymtableStringObjectFlags(
188188
constchar*str,
189189
PyObject*filename,
190190
intstart,
191-
PyCompilerFlags*flags);
191+
PyCompilerFlags*flags,
192+
PyObject*module);
192193

193194
int_PyFuture_FromAST(
194195
struct_mod*mod,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp