summaryrefslogtreecommitdiff
path: root/Python/compile.c
AgeCommit message (Collapse)AuthorLines
2021-06-24Make sure that line number is set correctly for call to __exit__ when ↵Mark Shannon-2/+1
handling exception in body of a with statement. (GH-26890)
2021-06-21bpo-43693: Turn localspluskinds into an object (GH-26749)Guido van Rossum-12/+8
Managing it as a bare pointer to malloc'ed bytes is just too awkward in a few places.
2021-06-21bpo-44297: Fix missing line number in generator expressions (GH-26801)Mark Shannon-0/+1
* Make sure that line number is set when entering comprehension scope in compiler.
2021-06-15bpo-43693: Eliminate unused "fast locals". (gh-26587)Eric Snow-41/+144
Currently, if an arg value escapes (into the closure for an inner function) we end up allocating two indices in the fast locals even though only one gets used. Additionally, using the lower index would be better in some cases, such as with no-arg `super()`. To address this, we update the compiler to fix the offsets so each variable only gets one "fast local". As a consequence, now some cell offsets are interspersed with the locals (only when an arg escapes to an inner function). https://bugs.python.org/issue43693
2021-06-08 bpo-43693: Un-revert commit f3fa63e. (#26609)Eric Snow-19/+53
This was reverted in GH-26596 (commit 6d518bb) due to some bad memory accesses. * Add the MAKE_CELL opcode. (gh-26396) The memory accesses have been fixed. https://bugs.python.org/issue43693
2021-06-08Revert "bpo-43693: Add the MAKE_CELL opcode and interleave fast locals ↵Pablo Galindo-53/+19
offsets. (gh-26396)" (GH-26597) This reverts commit 631f9938b1604d4f893417ec339b9e0fa9196fb1.
2021-06-08Fix compiler errors for unused variables (GH-26601)Pablo Galindo-0/+1
2021-06-07bpo-43693: Add the MAKE_CELL opcode and interleave fast locals offsets. ↵Eric Snow-19/+53
(gh-26396) This moves logic out of the frame initialization code and into the compiler and eval loop. Doing so simplifies the runtime code and allows us to optimize it better. https://bugs.python.org/issue43693
2021-06-07bpo-43693: Un-revert commits 2c1e258 and b2bf2bc. (gh-26577)Eric Snow-37/+123
These were reverted in gh-26530 (commit 17c4edc) due to refleaks. * 2c1e258 - Compute deref offsets in compiler (gh-25152) * b2bf2bc - Add new internal code objects fields: co_fastlocalnames and co_fastlocalkinds. (gh-26388) This change fixes the refleaks. https://bugs.python.org/issue43693
2021-06-04bpo-43693: Revert commits 2c1e2583fdc4db6b43d163239ea42b0e8394171f and ↵Pablo Galindo-122/+37
b2bf2bc1ece673d387341e06c8d3c2bc6e259747 (GH-26530) * Revert "bpo-43693: Compute deref offsets in compiler (gh-25152)" This reverts commit b2bf2bc1ece673d387341e06c8d3c2bc6e259747. * Revert "bpo-43693: Add new internal code objects fields: co_fastlocalnames and co_fastlocalkinds. (gh-26388)" This reverts commit 2c1e2583fdc4db6b43d163239ea42b0e8394171f. These two commits are breaking the refleak buildbots.
2021-06-03bpo-43693: Compute deref offsets in compiler (gh-25152)Mark Shannon-0/+21
Merges locals and cells into a single array. Saves a pointer in the interpreter and means that we don't need the LOAD_CLOSURE opcode any more https://bugs.python.org/issue43693
2021-06-03bpo-43693: Add new internal code objects fields: co_fastlocalnames and ↵Eric Snow-37/+101
co_fastlocalkinds. (gh-26388) A number of places in the code base (notably ceval.c and frameobject.c) rely on mapping variable names to indices in the frame "locals plus" array (AKA fast locals), and thus opargs. Currently the compiler indirectly encodes that information on the code object as the tuples co_varnames, co_cellvars, and co_freevars. At runtime the dependent code must calculate the proper mapping from those, which isn't ideal and impacts performance-sensitive sections. This is something we can easily address in the compiler instead. This change addresses the situation by replacing internal use of co_varnames, etc. with a single combined tuple of names in locals-plus order, along with a minimal array mapping each to its kind (local vs. cell vs. free). These two new PyCodeObject fields, co_fastlocalnames and co_fastllocalkinds, are not exposed to Python code for now, but co_varnames, etc. are still available with the same values as before (though computed lazily). Aside from the (mild) performance impact, there are a number of other benefits: * there's now a clear, direct relationship between locals-plus and variables * code that relies on the locals-plus-to-name mapping is simpler * marshaled code objects are smaller and serialize/de-serialize faster Also note that we can take this approach further by expanding the possible values in co_fastlocalkinds to include specific argument types (e.g. positional-only, kwargs). Doing so would allow further speed-ups in _PyEval_MakeFrameVector(), which is where args get unpacked into the locals-plus array. It would also allow us to shrink marshaled code objects even further. https://bugs.python.org/issue43693
2021-06-03bpo-44298: Fix line numbers for early exits in with statements. (GH-26513)Mark Shannon-7/+17
2021-05-27bpo-43693: Add _PyCode_New(). (gh-26375)Eric Snow-15/+35
This is an internal-only API that helps us manage the many values used to create a code object. https://bugs.python.org/issue43693
2021-05-25bpo-44156: Make cached string constants in compile.c subinterpreter ↵Ken Jin-52/+44
compatible (GH-26161)
2021-05-15bpo-26110: Add ``CALL_METHOD_KW`` opcode to speedup method calls with ↵Ken Jin-18/+63
keywords (GH-26014) * Add CALL_METHOD_KW * Make CALL_METHOD branchless too since it shares the same code * Place parentheses in STACK_SHRINK
2021-05-12bpo-43933: Force RETURN_VALUE bytecodes to have line numbers (GH-26054)Mark Shannon-0/+29
2021-05-07bpo-44063: set the missing end locations on the compiler (GH-25956)Batuhan Taskaya-5/+13
2021-05-07bpo-40222: "Zero cost" exception handling (GH-25729)Mark Shannon-72/+410
"Zero cost" exception handling. * Uses a lookup table to determine how to handle exceptions. * Removes SETUP_FINALLY and POP_TOP block instructions, eliminating (most of) the runtime overhead of try statements. * Reduces the size of the frame object by about 60%.
2021-05-03bpo-43754: Fix compiler warning in Python/compile.c (GH-25855)Pablo Galindo-1/+1
This fixes the following warning: 'initializing': conversion from 'Py_ssize_t' to 'int', possible loss of data [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]
2021-05-02bpo-43754: Eliminate bindings for partial pattern matches (GH-25229)Brandt Bucher-228/+363
2021-04-30bpo-43933: Set frame.f_lineno during call to __exit__ (GH-25719)Mark Shannon-4/+8
* Set line number of __exit__ call in a with statement to be that of the with keyword.
2021-04-29bpo-43892: Validate the first term of complex literal value patterns (GH-25735)Brandt Bucher-37/+19
2021-04-29bpo-42739: Don't use sentinels to mark end of line table. (GH-25657)Mark Shannon-4/+0
* Add length parameter to PyLineTable_InitAddressRange and doen't use sentinel values at end of table. Makes the line number table more robust. * Update PyCodeAddressRange to match PEP 626.
2021-04-28bpo-43892: Make match patterns explicit in the AST (GH-25585)Nick Coghlan-157/+224
Co-authored-by: Brandt Bucher <brandtbucher@gmail.com>
2021-04-25bpo-42737: annotations with complex targets no longer causes any runtime ↵Batuhan Taskaya-0/+6
effects (GH-23952)
2021-04-23bpo-43914: Highlight invalid ranges in SyntaxErrors (#25525)Pablo Galindo-3/+10
To improve the user experience understanding what part of the error messages associated with SyntaxErrors is wrong, we can highlight the whole error range and not only place the caret at the first character. In this way: >>> foo(x, z for z in range(10), t, w) File "<stdin>", line 1 foo(x, z for z in range(10), t, w) ^ SyntaxError: Generator expression must be parenthesized becomes >>> foo(x, z for z in range(10), t, w) File "<stdin>", line 1 foo(x, z for z in range(10), t, w) ^^^^^^^^^^^^^^^^^^^^ SyntaxError: Generator expression must be parenthesized
2021-04-21bpo-38605: Revert making 'from __future__ import annotations' the default ↵Pablo Galindo-9/+22
(GH-25490) This reverts commits 044a1048ca93d466965afc027b91a5a9eb9ce23c and 1be456ae9d53bb1cba2b24fc86175c282d1c2169, adapting the code to changes that happened after it.
2021-04-16bpo-41323: compiler: Reuse tuple in constant tuple folding (GH-25419)Inada Naoki-13/+26
2021-04-15bpo-43846: Use less stack for large literals and calls (GH-25403)Mark Shannon-50/+144
* Modify compiler to reduce stack consumption for large expressions. * Add more tests for stack usage. * Add NEWS item. * Raise SystemError for truly excessive stack use.
2021-04-07bpo-43495 : Push missing frame block in compile.c (GH-24865)tomKPZ-1/+10
2021-04-07Correct micro release number and add a couple of asserts. (GH-25224)Mark Shannon-3/+3
2021-04-06bpo-43683: Handle generator entry in bytecode (GH-25138)Mark Shannon-1/+46
* Handle check for sending None to starting generator and coroutine into bytecode. * Document new bytecode and make it fail gracefully if mis-compiled.
2021-04-01bpo-27129: Use instruction offsets, not byte offsets, in bytecode and ↵Mark Shannon-1/+0
internally. (GH-25069) * Use instruction offset, rather than bytecode offset. Streamlines interpreter dispatch a bit, and removes most EXTENDED_ARGs for jumps. * Change some uses of PyCode_Addr2Line to PyFrame_GetLineNumber
2021-03-24bpo-43244: Add pycore_compile.h header file (GH-25000)Victor Stinner-28/+5
Remove the compiler functions using "struct _mod" type, because the public AST C API was removed: * PyAST_Compile() * PyAST_CompileEx() * PyAST_CompileObject() * PyFuture_FromAST() * PyFuture_FromASTObject() These functions were undocumented and excluded from the limited C API. Rename functions: * PyAST_CompileObject() => _PyAST_Compile() * PyFuture_FromASTObject() => _PyFuture_FromAST() Moreover, _PyFuture_FromAST() is no longer exported (replace PyAPI_FUNC() with extern). _PyAST_Compile() remains exported for test_peg_generator. Remove also compatibility functions: * PyAST_Compile() * PyAST_CompileEx() * PyFuture_FromAST()
2021-03-19bpo-43244: Remove symtable.h header file (GH-24910)Victor Stinner-7/+7
Rename Include/symtable.h to to Include/internal/pycore_symtable.h, don't export symbols anymore (replace PyAPI_FUNC and PyAPI_DATA with extern) and rename functions: * PyST_GetScope() to _PyST_GetScope() * PySymtable_BuildObject() to _PySymtable_Build() * PySymtable_Free() to _PySymtable_Free() Remove PySymtable_Build(), Py_SymtableString() and Py_SymtableStringObject() functions. The Py_SymtableString() function was part the stable ABI by mistake but it could not be used, since the symtable.h header file was excluded from the limited C API. The Python symtable module remains available and is unchanged.
2021-03-17bpo-43244: Add pycore_ast.h header file (GH-24908)Victor Stinner-6/+5
Move _PyAST_GetDocString() and _PyAST_ExprAsUnicode() functions the internal C API: from Include/ast.h to a new Include/internal/pycore_ast.h header file. Don't export these functions anymore: replace PyAPI_FUNC() with extern. Remove also unused includes.
2021-03-16bpo-43497: Emit SyntaxWarnings for assertions with tuple constants. (GH-24867)tsukasa-au-4/+8
* bpo-43497: Emit SyntaxWarnings for assertions with tuple constants. Add a test that shows that a tuple constant (a tuple, where all of its members are also compile-time constants) produces a SyntaxWarning. Then fix this failure. * Make SyntaxWarnings also work when "optimized". * Split tests for SyntaxWarning to SyntaxError conversion SyntaxWarnings emitted by the compiler when configured to be errors are actually raised as SyntaxError exceptions. Move these tests into their own method and add a test to ensure they are raised. Previously we only tested that they were not raised for a "valid" assertion statement.
2021-03-15Mark POP_TOP at end of expression statement as artificial, to conform to PEP ↵Mark Shannon-0/+2
626. (GH-24860)
2021-03-14bpo-39316: Make sure that attribute accesses and stores, including method ↵Mark Shannon-1/+17
calls, conform to PEP 626. (GH-24859)
2021-03-10Fix typo in compile.c (GH-24812)Ikko Ashimine-1/+1
guranteed -> guaranteed
2021-03-02bpo-43358: Fix bad free in assemble function (GH-24697)Alex Henrie-2/+2
2021-02-26bpo-42128: Structural Pattern Matching (PEP 634) (GH-22917)Brandt Bucher-26/+713
Co-authored-by: Guido van Rossum <guido@python.org> Co-authored-by: Talin <viridia@gmail.com> Co-authored-by: Pablo Galindo <pablogsal@gmail.com>
2021-02-10bpo-42217: compiler: merge same co_code and co_linetable objects (GH-23056)Inada Naoki-17/+27
2021-02-02Only eliminate jumps to successor block if jump is unconditional. (GH-24417)Mark Shannon-17/+4
* Prevents elimination of the sole test of a value in statements like: if x or True: ...
2021-01-30bpo-40455: Fix gcc10+ warning about writing into a section of offset 0 ↵Pablo Galindo-3/+2
(GH-24384)
2021-01-30bpo-38631: Replace compiler fatal errors with exceptions (GH-24369)Victor Stinner-34/+52
* Replace Py_FatalError() calls with regular SystemError exceptions. * compiler_exit_scope() calls _PyErr_WriteUnraisableMsg() to log the PySequence_DelItem() failure. * compiler_unit_check() uses _PyMem_IsPtrFreed(). * compiler_make_closure(): remove "(reftype == FREE)" comment since reftype can also be LOCAL or GLOBAL_EXPLICIT.
2021-01-29Fix a reference leak in the compiler for compiler_lambda() (GH-24382)Pablo Galindo-1/+3
2021-01-29bpo-42979: Use _Py_CheckSlotResult() to check slots result (GH-24356)Victor Stinner-7/+11
When Python is built in debug mode (with C assertions), calling a type slot like sq_length (__len__() in Python) now fails with a fatal error if the slot succeeded with an exception set, or failed with no exception set. The error message contains the slot, the type name, and the current exception (if an exception is set). * Check the result of all slots using _Py_CheckSlotResult(). * No longer pass op_name to ternary_op() in release mode. * Replace operator with dunder Python method name in error messages. For example, replace "*" with "__mul__". * Fix compiler_exit_scope() when an exception is set. * Fix bytearray.extend() when an exception is set: don't call bytearray_setslice() with an exception set.
2021-01-26bpo-33387: update documentation for exception handling opcode changes (GH-24334)Irit Katriel-2/+0
* bpo-33387: remove obsolete comment * bpo-33387: update SETUP_WITH opcode documentation