2009-06-04 17:32:06 -03:00
|
|
|
# This module is used to map the old Python 2 names to the new names used in
|
|
|
|
# Python 3 for the pickle module. This needed to make pickle streams
|
|
|
|
# generated with Python 2 loadable by Python 3.
|
|
|
|
|
|
|
|
# This is a copy of lib2to3.fixes.fix_imports.MAPPING. We cannot import
|
|
|
|
# lib2to3 and use the mapping defined there, because lib2to3 uses pickle.
|
|
|
|
# Thus, this could cause the module to be imported recursively.
|
|
|
|
IMPORT_MAPPING = {
|
|
|
|
'__builtin__' : 'builtins',
|
|
|
|
'copy_reg': 'copyreg',
|
|
|
|
'Queue': 'queue',
|
|
|
|
'SocketServer': 'socketserver',
|
|
|
|
'ConfigParser': 'configparser',
|
|
|
|
'repr': 'reprlib',
|
|
|
|
'tkFileDialog': 'tkinter.filedialog',
|
|
|
|
'tkSimpleDialog': 'tkinter.simpledialog',
|
|
|
|
'tkColorChooser': 'tkinter.colorchooser',
|
|
|
|
'tkCommonDialog': 'tkinter.commondialog',
|
|
|
|
'Dialog': 'tkinter.dialog',
|
|
|
|
'Tkdnd': 'tkinter.dnd',
|
|
|
|
'tkFont': 'tkinter.font',
|
|
|
|
'tkMessageBox': 'tkinter.messagebox',
|
|
|
|
'ScrolledText': 'tkinter.scrolledtext',
|
|
|
|
'Tkconstants': 'tkinter.constants',
|
|
|
|
'Tix': 'tkinter.tix',
|
|
|
|
'ttk': 'tkinter.ttk',
|
|
|
|
'Tkinter': 'tkinter',
|
|
|
|
'markupbase': '_markupbase',
|
|
|
|
'_winreg': 'winreg',
|
|
|
|
'thread': '_thread',
|
|
|
|
'dummy_thread': '_dummy_thread',
|
|
|
|
'dbhash': 'dbm.bsd',
|
|
|
|
'dumbdbm': 'dbm.dumb',
|
|
|
|
'dbm': 'dbm.ndbm',
|
|
|
|
'gdbm': 'dbm.gnu',
|
|
|
|
'xmlrpclib': 'xmlrpc.client',
|
|
|
|
'SimpleXMLRPCServer': 'xmlrpc.server',
|
|
|
|
'httplib': 'http.client',
|
|
|
|
'htmlentitydefs' : 'html.entities',
|
|
|
|
'HTMLParser' : 'html.parser',
|
|
|
|
'Cookie': 'http.cookies',
|
|
|
|
'cookielib': 'http.cookiejar',
|
|
|
|
'BaseHTTPServer': 'http.server',
|
|
|
|
'test.test_support': 'test.support',
|
|
|
|
'commands': 'subprocess',
|
|
|
|
'urlparse' : 'urllib.parse',
|
|
|
|
'robotparser' : 'urllib.robotparser',
|
2015-03-31 07:12:37 -03:00
|
|
|
'urllib2': 'urllib.request',
|
|
|
|
'anydbm': 'dbm',
|
|
|
|
'_abcoll' : 'collections.abc',
|
2009-06-04 17:32:06 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# This contains rename rules that are easy to handle. We ignore the more
|
|
|
|
# complex stuff (e.g. mapping the names in the urllib and types modules).
|
|
|
|
# These rules should be run before import names are fixed.
|
|
|
|
NAME_MAPPING = {
|
|
|
|
('__builtin__', 'xrange'): ('builtins', 'range'),
|
|
|
|
('__builtin__', 'reduce'): ('functools', 'reduce'),
|
|
|
|
('__builtin__', 'intern'): ('sys', 'intern'),
|
|
|
|
('__builtin__', 'unichr'): ('builtins', 'chr'),
|
2015-03-31 07:12:37 -03:00
|
|
|
('__builtin__', 'unicode'): ('builtins', 'str'),
|
2009-06-04 17:32:06 -03:00
|
|
|
('__builtin__', 'long'): ('builtins', 'int'),
|
|
|
|
('itertools', 'izip'): ('builtins', 'zip'),
|
|
|
|
('itertools', 'imap'): ('builtins', 'map'),
|
|
|
|
('itertools', 'ifilter'): ('builtins', 'filter'),
|
|
|
|
('itertools', 'ifilterfalse'): ('itertools', 'filterfalse'),
|
2015-03-31 07:12:37 -03:00
|
|
|
('itertools', 'izip_longest'): ('itertools', 'zip_longest'),
|
|
|
|
('UserDict', 'IterableUserDict'): ('collections', 'UserDict'),
|
|
|
|
('UserList', 'UserList'): ('collections', 'UserList'),
|
|
|
|
('UserString', 'UserString'): ('collections', 'UserString'),
|
|
|
|
('whichdb', 'whichdb'): ('dbm', 'whichdb'),
|
|
|
|
('_socket', 'fromfd'): ('socket', 'fromfd'),
|
|
|
|
('_multiprocessing', 'Connection'): ('multiprocessing.connection', 'Connection'),
|
|
|
|
('multiprocessing.process', 'Process'): ('multiprocessing.context', 'Process'),
|
|
|
|
('multiprocessing.forking', 'Popen'): ('multiprocessing.popen_fork', 'Popen'),
|
|
|
|
('urllib', 'ContentTooShortError'): ('urllib.error', 'ContentTooShortError'),
|
|
|
|
('urllib', 'getproxies'): ('urllib.request', 'getproxies'),
|
|
|
|
('urllib', 'pathname2url'): ('urllib.request', 'pathname2url'),
|
|
|
|
('urllib', 'quote_plus'): ('urllib.parse', 'quote_plus'),
|
|
|
|
('urllib', 'quote'): ('urllib.parse', 'quote'),
|
|
|
|
('urllib', 'unquote_plus'): ('urllib.parse', 'unquote_plus'),
|
|
|
|
('urllib', 'unquote'): ('urllib.parse', 'unquote'),
|
|
|
|
('urllib', 'url2pathname'): ('urllib.request', 'url2pathname'),
|
|
|
|
('urllib', 'urlcleanup'): ('urllib.request', 'urlcleanup'),
|
|
|
|
('urllib', 'urlencode'): ('urllib.parse', 'urlencode'),
|
|
|
|
('urllib', 'urlopen'): ('urllib.request', 'urlopen'),
|
|
|
|
('urllib', 'urlretrieve'): ('urllib.request', 'urlretrieve'),
|
|
|
|
('urllib2', 'HTTPError'): ('urllib.error', 'HTTPError'),
|
|
|
|
('urllib2', 'URLError'): ('urllib.error', 'URLError'),
|
2009-06-04 17:32:06 -03:00
|
|
|
}
|
|
|
|
|
2013-12-02 06:41:01 -04:00
|
|
|
PYTHON2_EXCEPTIONS = (
|
|
|
|
"ArithmeticError",
|
|
|
|
"AssertionError",
|
|
|
|
"AttributeError",
|
|
|
|
"BaseException",
|
|
|
|
"BufferError",
|
|
|
|
"BytesWarning",
|
|
|
|
"DeprecationWarning",
|
|
|
|
"EOFError",
|
|
|
|
"EnvironmentError",
|
|
|
|
"Exception",
|
|
|
|
"FloatingPointError",
|
|
|
|
"FutureWarning",
|
|
|
|
"GeneratorExit",
|
|
|
|
"IOError",
|
|
|
|
"ImportError",
|
|
|
|
"ImportWarning",
|
|
|
|
"IndentationError",
|
|
|
|
"IndexError",
|
|
|
|
"KeyError",
|
|
|
|
"KeyboardInterrupt",
|
|
|
|
"LookupError",
|
|
|
|
"MemoryError",
|
|
|
|
"NameError",
|
|
|
|
"NotImplementedError",
|
|
|
|
"OSError",
|
|
|
|
"OverflowError",
|
|
|
|
"PendingDeprecationWarning",
|
|
|
|
"ReferenceError",
|
|
|
|
"RuntimeError",
|
|
|
|
"RuntimeWarning",
|
|
|
|
# StandardError is gone in Python 3, so we map it to Exception
|
|
|
|
"StopIteration",
|
|
|
|
"SyntaxError",
|
|
|
|
"SyntaxWarning",
|
|
|
|
"SystemError",
|
|
|
|
"SystemExit",
|
|
|
|
"TabError",
|
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 (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
2019-01-24 19:49:56 -04:00
|
|
|
"TargetScopeError",
|
2013-12-02 06:41:01 -04:00
|
|
|
"TypeError",
|
|
|
|
"UnboundLocalError",
|
|
|
|
"UnicodeDecodeError",
|
|
|
|
"UnicodeEncodeError",
|
|
|
|
"UnicodeError",
|
|
|
|
"UnicodeTranslateError",
|
|
|
|
"UnicodeWarning",
|
|
|
|
"UserWarning",
|
|
|
|
"ValueError",
|
|
|
|
"Warning",
|
|
|
|
"ZeroDivisionError",
|
|
|
|
)
|
|
|
|
|
2015-03-31 10:49:26 -03:00
|
|
|
try:
|
|
|
|
WindowsError
|
|
|
|
except NameError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
PYTHON2_EXCEPTIONS += ("WindowsError",)
|
|
|
|
|
2013-12-02 06:41:01 -04:00
|
|
|
for excname in PYTHON2_EXCEPTIONS:
|
|
|
|
NAME_MAPPING[("exceptions", excname)] = ("builtins", excname)
|
|
|
|
|
2015-03-31 07:12:37 -03:00
|
|
|
MULTIPROCESSING_EXCEPTIONS = (
|
|
|
|
'AuthenticationError',
|
|
|
|
'BufferTooShort',
|
|
|
|
'ProcessError',
|
|
|
|
'TimeoutError',
|
|
|
|
)
|
|
|
|
|
|
|
|
for excname in MULTIPROCESSING_EXCEPTIONS:
|
|
|
|
NAME_MAPPING[("multiprocessing", excname)] = ("multiprocessing.context", excname)
|
2013-12-02 06:41:01 -04:00
|
|
|
|
2009-06-04 17:32:06 -03:00
|
|
|
# Same, but for 3.x to 2.x
|
|
|
|
REVERSE_IMPORT_MAPPING = dict((v, k) for (k, v) in IMPORT_MAPPING.items())
|
2015-03-31 07:12:37 -03:00
|
|
|
assert len(REVERSE_IMPORT_MAPPING) == len(IMPORT_MAPPING)
|
2009-06-04 17:32:06 -03:00
|
|
|
REVERSE_NAME_MAPPING = dict((v, k) for (k, v) in NAME_MAPPING.items())
|
2015-03-31 07:12:37 -03:00
|
|
|
assert len(REVERSE_NAME_MAPPING) == len(NAME_MAPPING)
|
|
|
|
|
|
|
|
# Non-mutual mappings.
|
|
|
|
|
|
|
|
IMPORT_MAPPING.update({
|
|
|
|
'cPickle': 'pickle',
|
|
|
|
'_elementtree': 'xml.etree.ElementTree',
|
|
|
|
'FileDialog': 'tkinter.filedialog',
|
|
|
|
'SimpleDialog': 'tkinter.simpledialog',
|
|
|
|
'DocXMLRPCServer': 'xmlrpc.server',
|
|
|
|
'SimpleHTTPServer': 'http.server',
|
|
|
|
'CGIHTTPServer': 'http.server',
|
2016-01-18 15:35:22 -04:00
|
|
|
# For compatibility with broken pickles saved in old Python 3 versions
|
|
|
|
'UserDict': 'collections',
|
|
|
|
'UserList': 'collections',
|
|
|
|
'UserString': 'collections',
|
|
|
|
'whichdb': 'dbm',
|
|
|
|
'StringIO': 'io',
|
|
|
|
'cStringIO': 'io',
|
2015-03-31 07:12:37 -03:00
|
|
|
})
|
|
|
|
|
|
|
|
REVERSE_IMPORT_MAPPING.update({
|
|
|
|
'_bz2': 'bz2',
|
|
|
|
'_dbm': 'dbm',
|
|
|
|
'_functools': 'functools',
|
|
|
|
'_gdbm': 'gdbm',
|
|
|
|
'_pickle': 'pickle',
|
|
|
|
})
|
|
|
|
|
|
|
|
NAME_MAPPING.update({
|
|
|
|
('__builtin__', 'basestring'): ('builtins', 'str'),
|
|
|
|
('exceptions', 'StandardError'): ('builtins', 'Exception'),
|
|
|
|
('UserDict', 'UserDict'): ('collections', 'UserDict'),
|
|
|
|
('socket', '_socketobject'): ('socket', 'SocketType'),
|
|
|
|
})
|
|
|
|
|
|
|
|
REVERSE_NAME_MAPPING.update({
|
|
|
|
('_functools', 'reduce'): ('__builtin__', 'reduce'),
|
|
|
|
('tkinter.filedialog', 'FileDialog'): ('FileDialog', 'FileDialog'),
|
|
|
|
('tkinter.filedialog', 'LoadFileDialog'): ('FileDialog', 'LoadFileDialog'),
|
|
|
|
('tkinter.filedialog', 'SaveFileDialog'): ('FileDialog', 'SaveFileDialog'),
|
|
|
|
('tkinter.simpledialog', 'SimpleDialog'): ('SimpleDialog', 'SimpleDialog'),
|
|
|
|
('xmlrpc.server', 'ServerHTMLDoc'): ('DocXMLRPCServer', 'ServerHTMLDoc'),
|
|
|
|
('xmlrpc.server', 'XMLRPCDocGenerator'):
|
|
|
|
('DocXMLRPCServer', 'XMLRPCDocGenerator'),
|
|
|
|
('xmlrpc.server', 'DocXMLRPCRequestHandler'):
|
|
|
|
('DocXMLRPCServer', 'DocXMLRPCRequestHandler'),
|
|
|
|
('xmlrpc.server', 'DocXMLRPCServer'):
|
|
|
|
('DocXMLRPCServer', 'DocXMLRPCServer'),
|
|
|
|
('xmlrpc.server', 'DocCGIXMLRPCRequestHandler'):
|
|
|
|
('DocXMLRPCServer', 'DocCGIXMLRPCRequestHandler'),
|
|
|
|
('http.server', 'SimpleHTTPRequestHandler'):
|
|
|
|
('SimpleHTTPServer', 'SimpleHTTPRequestHandler'),
|
|
|
|
('http.server', 'CGIHTTPRequestHandler'):
|
|
|
|
('CGIHTTPServer', 'CGIHTTPRequestHandler'),
|
|
|
|
('_socket', 'socket'): ('socket', '_socketobject'),
|
|
|
|
})
|
|
|
|
|
|
|
|
PYTHON3_OSERROR_EXCEPTIONS = (
|
|
|
|
'BrokenPipeError',
|
|
|
|
'ChildProcessError',
|
|
|
|
'ConnectionAbortedError',
|
|
|
|
'ConnectionError',
|
|
|
|
'ConnectionRefusedError',
|
|
|
|
'ConnectionResetError',
|
|
|
|
'FileExistsError',
|
|
|
|
'FileNotFoundError',
|
|
|
|
'InterruptedError',
|
|
|
|
'IsADirectoryError',
|
|
|
|
'NotADirectoryError',
|
|
|
|
'PermissionError',
|
|
|
|
'ProcessLookupError',
|
|
|
|
'TimeoutError',
|
|
|
|
)
|
|
|
|
|
|
|
|
for excname in PYTHON3_OSERROR_EXCEPTIONS:
|
|
|
|
REVERSE_NAME_MAPPING[('builtins', excname)] = ('exceptions', 'OSError')
|
2016-09-07 19:42:32 -03:00
|
|
|
|
|
|
|
PYTHON3_IMPORTERROR_EXCEPTIONS = (
|
|
|
|
'ModuleNotFoundError',
|
|
|
|
)
|
|
|
|
|
|
|
|
for excname in PYTHON3_IMPORTERROR_EXCEPTIONS:
|
|
|
|
REVERSE_NAME_MAPPING[('builtins', excname)] = ('exceptions', 'ImportError')
|