8000 Use a compiler flag instead of an extra argument · python/cpython@c558ad5 · GitHub
[go: up one dir, main page]

Skip to content

Commit c558ad5

Browse files
committed
Use a compiler flag instead of an extra argument
1 parent 1b9e8f6 commit c558ad5

File tree

10 files changed

+14 10000 43
-1452
lines changed

10 files changed

+1443
-1452
lines changed

Doc/library/functions.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ are always available. They are listed here in alphabetical order.
242242
Class methods can now wrap other :term:`descriptors <descriptor>` such as
243243
:func:`property`.
244244

245-
.. function:: compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1, noopt=None)
245+
.. function:: compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1)
246246

247247
Compile the *source* into a code or AST object. Code objects can be executed
248248
by :func:`exec` or :func:`eval`. *source* can either be a normal string, a
@@ -280,15 +280,16 @@ are always available. They are listed here in alphabetical order.
280280
object has ``CO_COROUTINE`` set in ``co_code``, and can be interactively
281281
executed via ``await eval(code_object)``.
282282

283+
If the *ast.PyCF_DISABLE_ALL_OPTIMIZATIONS* bit is set in *flags*, all compiler
284+
optimizations will be disabled and the value of *optimize* will be ignored. If
285+
this bit is not set, the value of ``sys.flags.noopt`` will be used.
286+
283287
The argument *optimize* specifies the optimization level of the compiler; the
284288
default value of ``-1`` selects the optimization level of the interpreter as
285289
given by :option:`-O` options. Explicit levels are ``0`` (no optimization;
286290
``__debug__`` is true), ``1`` (asserts are removed, ``__debug__`` is false)
287291
or ``2`` (docstrings are removed too).
288292

289-
If *noopt* is false, disable compiler optimizations and ignore *optimize*
290-
argument. If it is ``None``, use ``sys.flags.noopt`` value.
291-
292293
This function raises :exc:`SyntaxError` if the compiled source is invalid,
293294
and :exc:`ValueError` if the source contains null bytes.
294295

Doc/whatsnew/3.10.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ Base32 Encoding with Extended Hex Alphabet.
117117
builtins
118118
--------
119119

120-
The :func:`compile` function gets a new optional keyword-only *noopt* parameter
121-
to disable compiler optimizations.
120+
The :func:`compile` function gets a new optional bit in the *flags* argument
121+
(`ast.PyCF_DISABLE_ALL_OPTIMIZATIONS) to disable all compiler optimizations.
122122

123123
curses
124124
------

Include/compile.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ extern "C" {
2323
#define PyCF_IGNORE_COOKIE 0x0800
2424
#define PyCF_TYPE_COMMENTS 0x1000
2525
#define PyCF_ALLOW_TOP_LEVEL_AWAIT 0x2000
26+
#define PyCF_DISABLE_ALL_OPTIMIZATIONS 0x3000
2627
#define PyCF_COMPILE_MASK (PyCF_ONLY_AST | PyCF_ALLOW_TOP_LEVEL_AWAIT | \
27-
PyCF_TYPE_COMMENTS | PyCF_DONT_IMPLY_DEDENT)
28+
PyCF_TYPE_COMMENTS | PyCF_DONT_IMPLY_DEDENT | \
29+
PyCF_DISABLE_ALL_OPTIMIZATIONS)
2830

2931
#ifndef Py_LIMITED_API
3032
typedef struct {

Lib/importlib/_bootstrap_external.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -853,14 +853,18 @@ def get_source(self, fullname):
853853
name=fullname) from exc
854854
return decode_source(source_bytes)
855855

856-
def source_to_code(self, data, path, *, _optimize=-1, _noopt=None):
856+
def source_to_code(self, data, path, *, _optimize=-1, _noopt=False):
857857
"""Return the code object compiled from source.
858858
859859
The 'data' argument can be any object type that compile() supports.
860860
"""
861+
if _noopt:
862+
import ast
863+
return _bootstrap._call_with_frames_removed(compile, data, path, 'exec',
864+
dont_inherit=True, optimize=_optimize,
865+
flags=ast.PyCF_DISABLE_ALL_OPTIMIZATIONS)
861866
return _bootstrap._call_with_frames_removed(compile, data, path, 'exec',
862-
dont_inherit=True, optimize=_optimize,
863-
noopt=_noopt)
867+
dont_inherit=True, optimize=_optimize)
864868

865869
def get_code(self, fullname):
866870
"""Concrete implementation of InspectLoader.get_code.

Lib/test/test_compile.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from ast import PyCF_DISABLE_ALL_OPTIMIZATIONS
12
import _ast
23
import dis
34
import io
@@ -769,10 +770,12 @@ def test_big_dict_literal(self):
769770
self.assertEqual(len(eval(the_dict)), dict_size)
770771

771772
def test_noopt(self):
772-
for noopt in (None, False, True):
773+
for flags in (0, PyCF_DISABLE_ALL_OPTIMIZATIONS):
773774
out = io.StringIO()
774-
code = compile("if 0: breakpoint()", "<string>", "exec", noopt=noopt)
775+
code = compile("if 0: breakpoint()", "<string>", "exec",
776+
flags=flags)
775777
dis.dis(code, file=out)
778+
noopt = (flags & PyCF_DISABLE_ALL_OPTIMIZATIONS)
776779
if noopt is not None:
777780
optimize = not noopt
778781
else:

Parser/asdl_c.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,9 @@ def visitModule(self, mod):
10751075
self.emit('if (PyModule_AddIntMacro(m, PyCF_TYPE_COMMENTS) < 0) {', 1)
10761076
self.emit("return -1;", 2)
10771077
self.emit('}', 1)
1078+
self.emit('if (PyModule_AddIntMacro(m, PyCF_DISABLE_ALL_OPTIMIZATIONS) < 0) {', 1)
1079+
self.emit("return -1;", 2)
1080+
self.emit('}', 1)
10781081
for dfn in mod.dfns:
10791082
self.visit(dfn)
10801083
self.emit("return 0;", 1)

Python/Python-ast.c

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

Python/bltinmodule.c

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,6 @@ compile as builtin_compile
704704
optimize: int = -1
705705
*
706706
_feature_version as feature_version: int = -1
707-
noopt as noopt_obj: object = None
708707
709708
Compile source into a code object that can be executed by exec() or eval().
710709
@@ -723,44 +722,22 @@ in addition to any features explicitly specified.
723722
static PyObject *
724723
builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename,
725724
const char *mode, int flags, int dont_inherit,
726-
int optimize, int feature_version, PyObject *noopt_obj)
727-
/*[clinic end generated code: output=0fac52056ff4025d input=5c58fcfcf38274ac]*/
725+
int optimize, int feature_version)
726+
/*[clinic end generated code: output=b0c09c84f116d3d7 input=40171fb92c1d580d]*/
728727
{
729728
PyObject *source_copy;
730729
const char *str;
731730
int compile_mode = -1;
732731
int is_ast;
733732
int start[] = {Py_file_input, Py_eval_input, Py_single_input, Py_func_type_input};
734733
PyObject *result;
735-
int noopt;
736-
737-
if (noopt_obj == Py_None) {
734+
int noopt = flags & PyCF_DISABLE_ALL_OPTIMIZATIONS;
735+
if (!noopt) {
738736
const PyConfig *config = &_PyInterpreterState_GET()->config;
739737
noopt = !config->optimize;
740738
}
741-
else {
742-
if (optimize != -1) {
743-
PyErr_SetString(PyExc_ValueError,
744-
"compile(): either 'noopt' or 'optimize' can be "
745-
"used at the same time");
746-
goto error;
747-
}
748-
noopt = _PyLong_AsInt(noopt_obj);
749-
if (noopt == -1 && PyErr_Occurred()) {
750-
goto error;
751-
}
752-
if (noopt < -1) {
753-
PyErr_SetString(PyExc_ValueError,
754-
"compile(): invalid noopt value");
755-
goto error;
756-
}
757-
/* normalize noopt value (ex: noopt=4 becomes noopt=1) */
758-
if (noopt > 1) {
759-
noopt = 1;
760-
}
761-
}
762739

763-
if (noopt == 1) {
740+
if (noopt) {
764741
optimize = _PyCompiler_disable_all_optimizations;
765742
}
766743

Python/clinic/bltinmodule.c.h

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

0 commit comments

Comments
 (0)
0