summaryrefslogtreecommitdiffstats
path: root/Python/symtable.c
Commit message (Collapse)AuthorAgeFilesLines
* bpo-37947: Avoid double-decrement in symtable recursion counting (GH-15593)Miss Islington (bot)2019-08-291-4/+14
| | | | | | | | | | | With `symtable_visit_expr` now correctly adjusting the recursion depth for named expressions, `symtable_handle_namedexpr` should be leaving it alone. Also adds a new check to `PySymtable_BuildObject` that raises `SystemError` if a successful first symbol analysis pass fails to keep the stack depth accounting clean. (cherry picked from commit 06145230c833c3db5dab8858e11bcd550a37c57f) Co-authored-by: Nick Coghlan <ncoghlan@gmail.com>
* bpo-37954: Fix reference leak in the symtable (GH-15514)Miss Islington (bot)2019-08-261-1/+3
| | | | | (cherry picked from commit 4901dc46da5ecb131f8d902a0fbd704934f209e1) Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
* [3.8] bpo-37947: Adjust correctly the recursion level in symtable for named ↵Pablo Galindo2019-08-261-1/+3
| | | | | | | expressions (GH-15499) (GH-15515) (cherry picked from commit 0e4ea16336685cf3fa353d8c54af59b45b2d5c33) Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
* [3.8] bpo-37757: Disallow PEP 572 cases that expose implementation details ↵Nick Coghlan2019-08-251-24/+97
| | | | | | | | | | | | | | | | | | | (GH-15491) - drop TargetScopeError in favour of raising SyntaxError directly as per the updated PEP 572 - comprehension iteration variables are explicitly local, but named expression targets in comprehensions are nonlocal or global. Raise SyntaxError as specified in PEP 572 - named expression targets in the outermost iterable of a comprehension have an ambiguous target scope. Avoid resolving that question now by raising SyntaxError. PEP 572 originally required this only for cases where the bound name conflicts with the iteration variable in the comprehension, but CPython can't easily restrict the exception to that case (as it doesn't know the target variable names when visiting the outermost iterator expression) (cherry picked from commit 5dbe0f59b7a4f39c7c606b48056bc29e406ebf78)
* bpo-36974: tp_print -> tp_vectorcall_offset and tp_reserved -> tp_as_async ↵Jeroen Demeyer2019-05-311-2/+2
| | | | | | | | | (GH-13464) Automatically replace tp_print -> tp_vectorcall_offset tp_compare -> tp_as_async tp_reserved -> tp_as_async
* bpo-36540: PEP 570 -- Implementation (GH-12701)Pablo Galindo2019-04-291-0/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This commit contains the implementation of PEP570: Python positional-only parameters. * Update Grammar/Grammar with new typedarglist and varargslist * Regenerate grammar files * Update and regenerate AST related files * Update code object * Update marshal.c * Update compiler and symtable * Regenerate importlib files * Update callable objects * Implement positional-only args logic in ceval.c * Regenerate frozen data * Update standard library to account for positional-only args * Add test file for positional-only args * Update other test files to account for positional-only args * Add News entry * Update inspect module and related tests
* bpo-36187: Remove NamedStore. (GH-12167)Serhiy Storchaka2019-03-051-5/+4
| | | | | NamedStore has been replaced with Store. The difference between NamedStore and Store is handled when precess the NamedExpr node one level upper.
* bpo-35459: Use PyDict_GetItemWithError() instead of PyDict_GetItem(). (GH-11112)Serhiy Storchaka2019-02-251-5/+13
|
* Complete switch cases in symtable.c; fixes bpo-35963 (GH-11821)Guido van Rossum2019-02-111-0/+4
|
* bpo-35224: PEP 572 Implementation (#10497)Emily Morehouse2019-01-241-9/+83
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * Add tokenization of := - Add token to Include/token.h. Add token to documentation in Doc/library/token.rst. - Run `./python Lib/token.py` to regenerate Lib/token.py. - Update Parser/tokenizer.c: add case to handle `:=`. * Add initial usage of := in grammar. * Update Python.asdl to match the grammar updates. Regenerated Include/Python-ast.h and Python/Python-ast.c * Update AST and compiler files in Python/ast.c and Python/compile.c. Basic functionality, this isn't scoped properly * Regenerate Lib/symbol.py using `./python Lib/symbol.py` * Tests - Fix failing tests in test_parser.py due to changes in token numbers for internal representation * Tests - Add simple test for := token * Tests - Add simple tests for named expressions using expr and suite * Tests - Update number of levels for nested expressions to prevent stack overflow * Update symbol table to handle NamedExpr * Update Grammar to allow assignment expressions in if statements. Regenerate Python/graminit.c accordingly using `make regen-grammar` * Tests - Add additional tests for named expressions in RoundtripLegalSyntaxTestCase, based on examples and information directly from PEP 572 Note: failing tests are currently commented out (4 out of 24 tests currently fail) * Tests - Add temporary syntax test failure tests in test_parser.py Note: There is an outstanding TODO for this -- syntax tests need to be moved to a different file (presumably test_syntax.py), but this is covering what needs to be tested at the moment, and it's more convenient to run a single test for the time being * Add support for allowing assignment expressions as function argument annotations. Uncomment tests for these cases because they all pass now! * Tests - Move existing syntax tests out of test_parser.py and into test_named_expressions.py. Refactor syntax tests to use unittest * Add TargetScopeError exception to extend SyntaxError Note: This simply creates the TargetScopeError exception, it is not yet used anywhere * Tests - Update tests per PEP 572 Continue refactoring test suite: The named expression test suite now checks for any invalid cases that throw exceptions (no longer limited to SyntaxErrors), assignment tests to ensure that variables are properly assigned, and scope tests to ensure that variable availability and values are correct Note: - There are still tests that are marked to skip, as they are not yet implemented - There are approximately 300 lines of the PEP that have not yet been addressed, though these may be deferred * Documentation - Small updates to XXX/todo comments - Remove XXX from child description in ast.c - Add comment with number of previously supported nested expressions for 3.7.X in test_parser.py * Fix assert in seq_for_testlist() * Cleanup - Denote "Not implemented -- No keyword args" on failing test case. Fix PEP8 error for blank lines at beginning of test classes in test_parser.py * Tests - Wrap all file opens in `with...as` to ensure files are closed * WIP: handle f(a := 1) * Tests and Cleanup - No longer skips keyword arg test. Keyword arg test now uses a simpler test case and does not rely on an external file. Remove print statements from ast.c * Tests - Refactor last remaining test case that relied on on external file to use a simpler test case without the dependency * Tests - Add better description of remaning skipped tests. Add test checking scope when using assignment expression in a function argument * Tests - Add test for nested comprehension, testing value and scope. Fix variable name in skipped comprehension scope test * Handle restriction of LHS for named expressions - can only assign to LHS of type NAME. Specifically, restrict assignment to tuples This adds an alternative set_context specifically for named expressions, set_namedexpr_context. Thus, context is now set differently for standard assignment versus assignment for named expressions in order to handle restrictions. * Tests - Update negative test case for assigning to lambda to match new error message. Add negative test case for assigning to tuple * Tests - Reorder test cases to group invalid syntax cases and named assignment target errors * Tests - Update test case for named expression in function argument - check that result and variable are set correctly * Todo - Add todo for TargetScopeError based on Guido's comment (https://github.com/python/cpython/commit/2b3acd37bdfc2d35e5094228c6684050d2aa8b0a#r30472562) * Tests - Add named expression tests for assignment operator in function arguments Note: One of two tests are skipped, as function arguments are currently treating an assignment expression inside of parenthesis as one child, which does not properly catch the named expression, nor does it count arguments properly * Add NamedStore to expr_context. Regenerate related code with `make regen-ast` * Add usage of NamedStore to ast_for_named_expr in ast.c. Update occurances of checking for Store to also handle NamedStore where appropriate * Add ste_comprehension to _symtable_entry to track if the namespace is a comprehension. Initialize ste_comprehension to 0. Set set_comprehension to 1 in symtable_handle_comprehension * s/symtable_add_def/symtable_add_def_helper. Add symtable_add_def to handle grabbing st->st_cur and passing it to symtable_add_def_helper. This now allows us to call the original code from symtable_add_def by instead calling symtable_add_def_helper with a different ste. * Refactor symtable_record_directive to take lineno and col_offset as arguments instead of stmt_ty. This allows symtable_record_directive to be used for stmt_ty and expr_ty * Handle elevating scope for named expressions in comprehensions. * Handle error for usage of named expression inside a class block * Tests - No longer skip scope tests. Add additional scope tests * Cleanup - Update error message for named expression within a comprehension within a class. Update comments. Add assert for symtable_extend_namedexpr_scope to validate that we always find at least a ModuleScope if we don't find a Class or FunctionScope * Cleanup - Add missing case for NamedStore in expr_context_name. Remove unused var in set_namedexpr_content * Refactor - Consolidate set_context and set_namedexpr_context to reduce duplicated code. Special cases for named expressions are handled by checking if ctx is NamedStore * Cleanup - Add additional use cases for ast_for_namedexpr in usage comment. Fix multiple blank lines in test_named_expressions * Tests - Remove unnecessary test case. Renumber test case function names * Remove TargetScopeError for now. Will add back if needed * Cleanup - Small comment nit for consistency * Handle positional argument check with named expression * Add TargetScopeError exception definition. Add documentation for TargetScopeError in c-api docs. Throw TargetScopeError instead of SyntaxError when using a named expression in a comprehension within a class scope * Increase stack size for parser by 200. This is a minimal change (approx. 5kb) and should not have an impact on any systems. Update parser test to allow 99 nested levels again * Add TargetScopeError to exception_hierarchy.txt for test_baseexception.py_ * Tests - Major update for named expression tests, both in test_named_expressions and test_parser - Add test for TargetScopeError - Add tests for named expressions in comprehension scope and edge cases - Add tests for named expressions in function arguments (declarations and call sites) - Reorganize tests to group them more logically * Cleanup - Remove unnecessary comment * Cleanup - Comment nitpicks * Explicitly disallow assignment expressions to a name inside parentheses, e.g.: ((x) := 0) - Add check for LHS types to detect a parenthesis then a name (see note) - Add test for this scenario - Update tests for changed error message for named assignment to a tuple (also, see note) Note: This caused issues with the previous error handling for named assignment to a LHS that contained an expression, such as a tuple. Thus, the check for the LHS of a named expression must be changed to be more specific if we wish to maintain the previous error messages * Cleanup - Wrap lines more strictly in test file * Revert "Explicitly disallow assignment expressions to a name inside parentheses, e.g.: ((x) := 0)" This reverts commit f1531400ca7d7a2d148830c8ac703f041740896d. * Add NEWS.d entry * Tests - Fix error in test_pickle.test_exceptions by adding TargetScopeError to list of exceptions * Tests - Update error message tests to reflect improved messaging convention (s/can't/cannot) * Remove cases that cannot be reached in compile.c. Small linting update. * Update Grammar/Tokens to add COLONEQUAL. Regenerate all files * Update TargetScopeError PRE_INIT and POST_INIT, as this was purposefully left out when fixing rebase conflicts * Add NamedStore back and regenerate files * Pass along line number and end col info for named expression * Simplify News entry * Fix compiler warning and explicity mark fallthrough
* bpo-35177, Python-ast.h: Fix "Yield" compiler warning (GH-10664)Victor Stinner2018-11-221-0/+1
| | | | | | | | Partially revert commit 5f2df88b63e50d23914e97ec778861a52abdeaad: add "#undef Yield" to .c files after including Python-ast.h. Fix the warning: winbase.h(102): warning C4005: 'Yield': macro redefinition
* Add a missed PyErr_NoMemory() in symtable_new(). (GH-10576)Zackery Spytz2018-11-161-1/+3
| | | | This missed PyErr_NoMemory() could cause a SystemError when calling _symtable.symtable().
* bpo-35081: Rename internal headers (GH-10275)Victor Stinner2018-11-121-1/+1
| | | | | | | | | | | | | | Rename Include/internal/ headers: * pycore_hash.h -> pycore_pyhash.h * pycore_lifecycle.h -> pycore_pylifecycle.h * pycore_mem.h -> pycore_pymem.h * pycore_state.h -> pycore_pystate.h Add missing headers to Makefile.pre.in and PCbuild: * pycore_condvar.h. * pycore_hamt.h * pycore_pyhash.h
* bpo-35177: Add dependencies between header files (GH-10361)Victor Stinner2018-11-111-5/+0
| | | | | | | | | | | | | | * ast.h now includes Python-ast.h and node.h * parsetok.h now includes node.h and grammar.h * symtable.h now includes Python-ast.h * Modify asdl_c.py to enhance Python-ast.h: * Add #ifndef/#define Py_PYTHON_AST_H to be able to include the header twice * Add "extern { ... }" for C++ * Undefine "Yield" macro conflicting with winbase.h * Remove "#undef Yield" from C files, it's now done in Python-ast.h * Remove now useless includes in C files
* bpo-35081: Add _PyThreadState_GET() internal macro (GH-10266)Victor Stinner2018-11-011-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | If Py_BUILD_CORE is defined, the PyThreadState_GET() macro access _PyRuntime which comes from the internal pycore_state.h header. Public headers must not require internal headers. Move PyThreadState_GET() and _PyInterpreterState_GET_UNSAFE() from Include/pystate.h to Include/internal/pycore_state.h, and rename PyThreadState_GET() to _PyThreadState_GET() there. The PyThreadState_GET() macro of pystate.h is now redefined when pycore_state.h is included, to use the fast _PyThreadState_GET(). Changes: * Add _PyThreadState_GET() macro * Replace "PyThreadState_GET()->interp" with _PyInterpreterState_GET_UNSAFE() * Replace PyThreadState_GET() with _PyThreadState_GET() in internal C files (compiled with Py_BUILD_CORE defined), but keep PyThreadState_GET() in the public header files. * _testcapimodule.c: replace PyThreadState_GET() with PyThreadState_Get(); the module is not compiled with Py_BUILD_CORE defined. * pycore_state.h now requires Py_BUILD_CORE to be defined.
* bpo-35081: Add pycore_ prefix to internal header files (GH-10263)Victor Stinner2018-10-311-1/+1
| | | | | | | | | | | | | | | | | | | | * Rename Include/internal/ header files: * pyatomic.h -> pycore_atomic.h * ceval.h -> pycore_ceval.h * condvar.h -> pycore_condvar.h * context.h -> pycore_context.h * pygetopt.h -> pycore_getopt.h * gil.h -> pycore_gil.h * hamt.h -> pycore_hamt.h * hash.h -> pycore_hash.h * mem.h -> pycore_mem.h * pystate.h -> pycore_state.h * warnings.h -> pycore_warnings.h * PCbuild project, Makefile.pre.in, Modules/Setup: add the Include/internal/ directory to the search paths of header files. * Update includes. For example, replace #include "internal/mem.h" with #include "pycore_mem.h".
* bpo-34939: Allow annotated global names in module namespace (GH-9844)Pablo Galindo2018-10-141-0/+1
| | | | | | | Allow annotated global names in the module namespace after the symbol is declared as global. Previously, only symbols annotated before they are declared as global (i.e. inside a function) were allowed. This change allows symbols to be declared as global before the annotation happens in the global scope.
* Fix a possible decref of a borrowed reference in symtable.c. (GH-9786)Zackery Spytz2018-10-111-2/+4
|
* bpo-32892: Use ast.Constant instead of specific constant AST types. (GH-9445)Serhiy Storchaka2018-09-271-5/+0
|
* bpo-34683: Make SyntaxError column offsets consistently 1-indexed (gh-9338)Ammar Askar2018-09-241-7/+7
| | | | | | Also point to start of tokens in parsing errors. Fixes bpo-34683
* bpo-32836: Remove obsolete code from symtable pass (GH-5680)Nitish Chandra2018-02-261-25/+0
| | | | | | | When comprehensions switched to using a nested scope, the old code for generating a temporary name to hold the accumulation target became redundant, but was never actually removed. Patch by Nitish Chandra.
* bpo-10544: Disallow "yield" in comprehensions and generator expressions. ↵Serhiy Storchaka2018-02-041-24/+7
| | | | (GH-4564)
* bpo-10544: Deprecate "yield" in comprehensions and generator expressions. ↵Serhiy Storchaka2017-12-011-1/+30
| | | | | | | | | | | | | | (GH-4579) The current behaviour of yield expressions inside comprehensions and generator expressions is essentially an accident of implementation - it arises implicitly from the way the compiler handles yield expressions inside nested functions and generators. Since the current behaviour wasn't deliberately designed, and is inherently confusing, we're deprecating it, with no current plans to reintroduce it. Instead, our advice will be to use a named nested generator definition for cases where this behaviour is desired.
* bpo-28936: Detect lexically first syntax error first (#4097)Ivan Levkivskyi2017-10-261-18/+16
| | | | Lexically first global and nonlocal syntax errors at given scope should be detected first.
* bpo-30860: Consolidate stateful runtime globals. (#3397)Eric Snow2017-09-081-0/+4
| | | | | | | * group the (stateful) runtime globals into various topical structs * consolidate the topical structs under a single top-level _PyRuntimeState struct * add a check-c-globals.py script that helps identify runtime globals Other globals are excluded (see globals.txt and check-c-globals.py).
* Issue #28701: Replace PyUnicode_CompareWithASCIIString with ↵Serhiy Storchaka2016-11-161-2/+2
|\ | | | | | | | | | | _PyUnicode_EqualToASCIIString. The latter function is more readable, faster and doesn't raise exceptions.
| * Issue #28701: Replace PyUnicode_CompareWithASCIIString with ↵Serhiy Storchaka2016-11-161-2/+2
| | | | | | | | | | | | _PyUnicode_EqualToASCIIString. The latter function is more readable, faster and doesn't raise exceptions.
* | Issue #28100: Refactor error messages, patch by Ivan LevkivskyiChristian Heimes2016-09-231-12/+8
| |
* | Issue #28008: Implement PEP 530 -- asynchronous comprehensions.Yury Selivanov2016-09-091-0/+6
| |
* | Issue #27999: Make "global after use" a SyntaxError, and ditto for nonlocal.Guido van Rossum2016-09-091-68/+36
| | | | | | | | Patch by Ivan Levkivskyi.
* | Issue #28003: Implement PEP 525 -- Asynchronous Generators.Yury Selivanov2016-09-091-2/+4
| |
* | Issue #27985: Implement PEP 526 -- Syntax for Variable Annotations.Yury Selivanov2016-09-091-0/+56
| | | | | | | | Patch by Ivan Levkivskyi.
* | Fix potential NULL pointer dereference in update_symbols()Christian Heimes2016-09-081-1/+1
| | | | | | | | | | | | symtable_analyze() calls analyze_block() with bound=NULL. Theoretically that NULL can be passed down to update_symbols(). update_symbols() may deference NULL and pass it to PySet_Contains()
* | Merge typo fixes from 3.5Martin Panter2016-06-041-1/+1
|\ \ | |/
| * Fix typos in code comment and documentationMartin Panter2016-06-041-1/+1
| |
* | Add ast.ConstantVictor Stinner2016-01-251-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Issue #26146: Add a new kind of AST node: ast.Constant. It can be used by external AST optimizers, but the compiler does not emit directly such node. An optimizer can replace the following AST nodes with ast.Constant: * ast.NameConstant: None, False, True * ast.Num: int, float, complex * ast.Str: str * ast.Bytes: bytes * ast.Tuple if items are constants too: tuple * frozenset Update code to accept ast.Constant instead of ast.Num and/or ast.Str: * compiler * docstrings * ast.literal_eval() * Tools/parser/unparse.py
* | merge 3.5 (#25973)Benjamin Peterson2015-12-291-8/+16
|\ \ | |/
| * make recording and reporting errors and nonlocal and global directives more ↵Benjamin Peterson2015-12-291-8/+16
| | | | | | | | robust (closes #25973)
* | Issue #25923: Added more const qualifiers to signatures of static and ↵Serhiy Storchaka2015-12-251-2/+2
| | | | | | | | private functions.
* | Issue #24965: Implement PEP 498 "Literal String Interpolation". ↵Eric V. Smith2015-09-191-0/+8
|/ | | | Documentation is still needed, I'll open an issue for that.
* Issue #24619: More tests; fix nits in compiler.cYury Selivanov2015-07-221-1/+1
|
* Issue #19235: Add new RecursionError exception. Patch by Georg Brandl.Yury Selivanov2015-07-031-2/+2
|
* PEP 0492 -- Coroutines with async and await syntax. Issue #24017.Yury Selivanov2015-05-121-6/+43
|
* PEP 448: additional unpacking generalizations (closes #2292)Benjamin Peterson2015-05-061-18/+8
| | | | Patch by Neil Girdhar.
* remove the concept of an unoptimized function scope from the compiler, since ↵Benjamin Peterson2015-04-281-16/+0
| | | | it can't happen anymore
* remove dead *-import checking code (closes #24049)Benjamin Peterson2015-04-241-31/+0
|
* merge 3.3 (#19098)Benjamin Peterson2013-09-271-2/+6
|\
| * don't scale compiler stack frames if the recursion limit is huge (closes #19098)Benjamin Peterson2013-09-271-2/+6
| |
| * move definition to top of blockBenjamin Peterson2013-05-171-1/+2
| |
| * complain about "global __class__" in a class body (closes #17983)Benjamin Peterson2013-05-151-0/+6
| |