cpython/Python
Emily Morehouse 8f59ee01be
bpo-35224: PEP 572 Implementation (#10497)
* 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 (2b3acd37bd (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
2019-01-24 16:49:56 -07:00
..
clinic bpo-35582: Argument Clinic: Optimize the "all boring objects" case. (GH-11520) 2019-01-11 18:01:42 +02:00
Python-ast.c bpo-35224: PEP 572 Implementation (#10497) 2019-01-24 16:49:56 -07:00
README Issue #18093: Factor out the programs that embed the runtime 2014-07-25 21:52:14 +10:00
_warnings.c bpo-35454: Fix miscellaneous minor issues in error handling. (#11077) 2018-12-11 08:38:03 +02:00
asdl.c replace PY_SIZE_MAX with SIZE_MAX 2016-09-07 09:26:18 -07:00
ast.c bpo-35224: PEP 572 Implementation (#10497) 2019-01-24 16:49:56 -07:00
ast_opt.c bpo-33416: Add end positions to Python AST (GH-11605) 2019-01-22 11:18:22 +00:00
ast_unparse.c bpo-34854: Fix compiling string annotations containing lambdas. (GH-9645) 2018-09-30 21:07:05 +03:00
bltinmodule.c bpo-35582: Inline arguments tuple unpacking in handwritten code. (GH-11524) 2019-01-12 08:25:41 +02:00
bootstrap_hash.c bpo-35550: Fix incorrect Solaris define guards (GH-11275) 2018-12-30 18:16:40 -08:00
ceval.c bpo-35634: Raise an error when first passed kwargs contains duplicated keys. (GH-11438) 2019-01-12 10:12:24 +02:00
ceval_gil.h bpo-35081: Add pycore_ prefix to internal header files (GH-10263) 2018-11-01 00:52:28 +01:00
codecs.c bpo-35454: Fix miscellaneous minor issues in error handling. (#11077) 2018-12-11 08:38:03 +02:00
compile.c bpo-35224: PEP 572 Implementation (#10497) 2019-01-24 16:49:56 -07:00
condvar.h bpo-35081: Add pycore_ prefix to internal header files (GH-10263) 2018-11-01 00:52:28 +01:00
context.c bpo-33029: Fix signatures of getter and setter functions. (GH-10746) 2018-11-27 19:34:35 +02:00
coreconfig.c bpo-35336: Fix PYTHONCOERCECLOCALE=1 (GH-10806) 2018-11-30 11:34:47 +01:00
dtoa.c bpo-30923: Silence fall-through warnings included in -Wextra since gcc-7.0. (#3157) 2017-08-21 13:09:59 +02:00
dup2.c bpo-32150: Expand tabs to spaces in C files. (#4583) 2017-11-28 17:56:10 +02:00
dynamic_annotations.c bpo-32241: Add the const qualifire to declarations of umodifiable strings. (#4748) 2017-12-12 13:55:04 +02:00
dynload_aix.c PEP 489: Multi-phase extension module initialization 2015-05-23 22:24:10 +10:00
dynload_dl.c Issue #24268: Fix import naming when loading extension modules. Patch by Petr Viktorin. 2015-05-23 14:13:41 -07:00
dynload_hpux.c Issue #24268: Fix import naming when loading extension modules. Patch by Petr Viktorin. 2015-05-23 14:13:41 -07:00
dynload_shlib.c bpo-28401: prevent Py_DEBUG builds from trying to import limited ABI modules (GH-1766) 2018-11-16 15:52:52 -08:00
dynload_stub.c
dynload_win.c [security] bpo-13617: Reject embedded null characters in wchar* strings. (#2302) 2017-06-28 08:30:06 +03:00
errors.c bpo-35081: Rename internal headers (GH-10275) 2018-11-12 16:53:38 +01:00
fileutils.c bpo-34523: Fix C locale coercion on FreeBSD CURRENT (GH-10672) 2018-11-23 13:08:26 +01:00
formatter_unicode.c bpo-33954: Fix _PyUnicode_InsertThousandsGrouping() (GH-10623) 2018-11-26 13:40:01 +01:00
frozen.c bpo-25711: Rewrite zipimport in pure Python. (GH-6809) 2018-09-18 22:22:29 +03:00
frozenmain.c bpo-35081: Rename internal headers (GH-10275) 2018-11-12 16:53:38 +01:00
future.c bpo-34683: Make SyntaxError column offsets consistently 1-indexed (gh-9338) 2018-09-24 14:12:49 -07:00
getargs.c bpo-35582: Argument Clinic: inline parsing code for positional parameters. (GH-11313) 2019-01-11 16:01:14 +02:00
getcompiler.c closes bpo-31696: don't mention GCC in sys.version when building with clang (#3891) 2017-10-05 21:15:14 -07:00
getcopyright.c Bump copyright years to 2019. (GH-11404) 2019-01-02 07:46:53 -08:00
getopt.c bpo-35081: Add pycore_ prefix to internal header files (GH-10263) 2018-11-01 00:52:28 +01:00
getplatform.c bpo-32150: Expand tabs to spaces in C files. (#4583) 2017-11-28 17:56:10 +02:00
getversion.c bpo-32150: Expand tabs to spaces in C files. (#4583) 2017-11-28 17:56:10 +02:00
graminit.c bpo-35224: PEP 572 Implementation (#10497) 2019-01-24 16:49:56 -07:00
hamt.c bpo-35059: Cast void* to PyObject* (GH-10650) 2018-11-22 10:25:22 +01:00
import.c bpo-35470: Fix a reference counting bug in _PyImport_FindExtensionObjectEx(). (GH-11128) 2019-01-10 18:12:31 +02:00
importdl.c bpo-31315: Fix an assertion failure in imp.create_dynamic(), when spec.name is not a string. (#3257) 2017-09-19 14:39:47 +03:00
importdl.h PEP 489: Multi-phase extension module initialization 2015-05-23 22:24:10 +10:00
importlib.h bpo-16806: Fix `lineno` and `col_offset` for multi-line string tokens (GH-10021) 2019-01-13 13:05:13 +09:00
importlib_external.h bpo-16806: Fix `lineno` and `col_offset` for multi-line string tokens (GH-10021) 2019-01-13 13:05:13 +09:00
importlib_zipimport.h bpo-16806: Fix `lineno` and `col_offset` for multi-line string tokens (GH-10021) 2019-01-13 13:05:13 +09:00
makeopcodetargets.py makeopcodetargets.py: we need to import Lib/opcode.py 2016-03-26 01:04:37 +01:00
marshal.c bpo-35436: Add missing PyErr_NoMemory() calls and other minor bug fixes. (GH-11015) 2018-12-07 12:11:30 +02:00
modsupport.c bpo-29852: Argument Clinic Py_ssize_t converter now supports None (#716) 2017-03-30 09:15:31 +03:00
mysnprintf.c
mystrtoul.c bpo-31370: Remove support for threads-less builds (#3385) 2017-09-07 18:56:24 +02:00
opcode_targets.h bpo-33041: Rework compiling an "async for" loop. (#6142) 2018-03-23 14:34:35 +02:00
pathconfig.c bpo-34725: Adds _Py_SetProgramFullPath so embedders may override sys.executable (GH-9860) 2018-11-17 20:41:48 -08:00
peephole.c bpo-9566: Fix compiler warnings in peephole.c (GH-10652) 2018-12-07 17:54:18 +01:00
pyarena.c Fix typos in comments (GH-9905) 2018-10-19 23:30:03 +02:00
pyctype.c
pyfpe.c bpo-29137: Remove fpectl module (#4789) 2018-01-05 23:15:34 -08:00
pyhash.c bpo-31849: Fix warning in pyhash.c (GH-6799) 2018-06-04 19:57:08 +09:00
pylifecycle.c bpo-35713: Reorganize sys module initialization (GH-11658) 2019-01-23 15:04:40 +01:00
pymath.c bpo-35214: Add _Py_ prefix to MEMORY_SANITIZER def. (GH-10503) 2018-11-12 22:01:22 -08:00
pystate.c bpo-34569: Fix subinterpreter 32-bit ABI, pystate.c/_new_long_object() (gh-9127) 2019-01-11 11:17:03 -07:00
pystrcmp.c
pystrhex.c closes bpo-34646: Remove PyAPI_* macros from declarations. (GH-9218) 2018-09-12 12:06:42 -07:00
pystrtod.c bpo-35436: Add missing PyErr_NoMemory() calls and other minor bug fixes. (GH-11015) 2018-12-07 12:11:30 +02:00
pythonrun.c bpo-30167: Remove __cached__ from __main__ when removing __file__ (GH-7415) 2018-11-29 20:01:27 +09:00
pytime.c bpo-34373: fix test_mktime and test_pthread_getcpuclickid tests on AIX (GH-8726) 2018-12-28 23:57:37 +10:00
strdup.c bpo-32150: Expand tabs to spaces in C files. (#4583) 2017-11-28 17:56:10 +02:00
structmember.c Added the const qualifier to char* variables that refer to readonly internal 2016-11-20 10:16:47 +02:00
symtable.c bpo-35224: PEP 572 Implementation (#10497) 2019-01-24 16:49:56 -07:00
sysmodule.c bpo-35713: Reorganize sys module initialization (GH-11658) 2019-01-23 15:04:40 +01:00
thread.c bpo-35081: Rename internal headers (GH-10275) 2018-11-12 16:53:38 +01:00
thread_nt.h bpo-35081: Add _PyThreadState_GET() internal macro (GH-10266) 2018-11-01 01:51:40 +01:00
thread_pthread.h bpo-33015: Fix UB in pthread PyThread_start_new_thread (GH-6008) 2018-11-30 16:14:25 +01:00
traceback.c bpo-33029: Fix signatures of getter and setter functions. (GH-10746) 2018-11-27 19:34:35 +02:00
wordcode_helpers.h bpo-31338 (#3374) 2017-09-14 18:13:16 -07:00

README

Miscellaneous source files for the main Python shared library