Skip to content

Commit 140d582

Browse files
committed
Add a bunch of stdlib modules from CPython
1 parent 6768e3a commit 140d582

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+25681
-0
lines changed

Lib/__future__.py

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
"""Record of phased-in incompatible language changes.
2+
3+
Each line is of the form:
4+
5+
FeatureName = "_Feature(" OptionalRelease "," MandatoryRelease ","
6+
CompilerFlag ")"
7+
8+
where, normally, OptionalRelease < MandatoryRelease, and both are 5-tuples
9+
of the same form as sys.version_info:
10+
11+
(PY_MAJOR_VERSION, # the 2 in 2.1.0a3; an int
12+
PY_MINOR_VERSION, # the 1; an int
13+
PY_MICRO_VERSION, # the 0; an int
14+
PY_RELEASE_LEVEL, # "alpha", "beta", "candidate" or "final"; string
15+
PY_RELEASE_SERIAL # the 3; an int
16+
)
17+
18+
OptionalRelease records the first release in which
19+
20+
from __future__ import FeatureName
21+
22+
was accepted.
23+
24+
In the case of MandatoryReleases that have not yet occurred,
25+
MandatoryRelease predicts the release in which the feature will become part
26+
of the language.
27+
28+
Else MandatoryRelease records when the feature became part of the language;
29+
in releases at or after that, modules no longer need
30+
31+
from __future__ import FeatureName
32+
33+
to use the feature in question, but may continue to use such imports.
34+
35+
MandatoryRelease may also be None, meaning that a planned feature got
36+
dropped.
37+
38+
Instances of class _Feature have two corresponding methods,
39+
.getOptionalRelease() and .getMandatoryRelease().
40+
41+
CompilerFlag is the (bitfield) flag that should be passed in the fourth
42+
argument to the builtin function compile() to enable the feature in
43+
dynamically compiled code. This flag is stored in the .compiler_flag
44+
attribute on _Future instances. These values must match the appropriate
45+
#defines of CO_xxx flags in Include/compile.h.
46+
47+
No feature line is ever to be deleted from this file.
48+
"""
49+
50+
all_feature_names = [
51+
"nested_scopes",
52+
"generators",
53+
"division",
54+
"absolute_import",
55+
"with_statement",
56+
"print_function",
57+
"unicode_literals",
58+
"barry_as_FLUFL",
59+
"generator_stop",
60+
]
61+
62+
__all__ = ["all_feature_names"] + all_feature_names
63+
64+
# The CO_xxx symbols are defined here under the same names used by
65+
# compile.h, so that an editor search will find them here. However,
66+
# they're not exported in __all__, because they don't really belong to
67+
# this module.
68+
CO_NESTED = 0x0010 # nested_scopes
69+
CO_GENERATOR_ALLOWED = 0 # generators (obsolete, was 0x1000)
70+
CO_FUTURE_DIVISION = 0x2000 # division
71+
CO_FUTURE_ABSOLUTE_IMPORT = 0x4000 # perform absolute imports by default
72+
CO_FUTURE_WITH_STATEMENT = 0x8000 # with statement
73+
CO_FUTURE_PRINT_FUNCTION = 0x10000 # print function
74+
CO_FUTURE_UNICODE_LITERALS = 0x20000 # unicode string literals
75+
CO_FUTURE_BARRY_AS_BDFL = 0x40000
76+
CO_FUTURE_GENERATOR_STOP = 0x80000 # StopIteration becomes RuntimeError in generators
77+
78+
class _Feature:
79+
def __init__(self, optionalRelease, mandatoryRelease, compiler_flag):
80+
self.optional = optionalRelease
81+
self.mandatory = mandatoryRelease
82+
self.compiler_flag = compiler_flag
83+
84+
def getOptionalRelease(self):
85+
"""Return first release in which this feature was recognized.
86+
87+
This is a 5-tuple, of the same form as sys.version_info.
88+
"""
89+
90+
return self.optional
91+
92+
def getMandatoryRelease(self):
93+
"""Return release in which this feature will become mandatory.
94+
95+
This is a 5-tuple, of the same form as sys.version_info, or, if
96+
the feature was dropped, is None.
97+
"""
98+
99+
return self.mandatory
100+
101+
def __repr__(self):
102+
return "_Feature" + repr((self.optional,
103+
self.mandatory,
104+
self.compiler_flag))
105+
106+
nested_scopes = _Feature((2, 1, 0, "beta", 1),
107+
(2, 2, 0, "alpha", 0),
108+
CO_NESTED)
109+
110+
generators = _Feature((2, 2, 0, "alpha", 1),
111+
(2, 3, 0, "final", 0),
112+
CO_GENERATOR_ALLOWED)
113+
114+
division = _Feature((2, 2, 0, "alpha", 2),
115+
(3, 0, 0, "alpha", 0),
116+
CO_FUTURE_DIVISION)
117+
118+
absolute_import = _Feature((2, 5, 0, "alpha", 1),
119+
(3, 0, 0, "alpha", 0),
120+
CO_FUTURE_ABSOLUTE_IMPORT)
121+
122+
with_statement = _Feature((2, 5, 0, "alpha", 1),
123+
(2, 6, 0, "alpha", 0),
124+
CO_FUTURE_WITH_STATEMENT)
125+
126+
print_function = _Feature((2, 6, 0, "alpha", 2),
127+
(3, 0, 0, "alpha", 0),
128+
CO_FUTURE_PRINT_FUNCTION)
129+
130+
unicode_literals = _Feature((2, 6, 0, "alpha", 2),
131+
(3, 0, 0, "alpha", 0),
132+
CO_FUTURE_UNICODE_LITERALS)
133+
134+
barry_as_FLUFL = _Feature((3, 1, 0, "alpha", 2),
135+
(3, 9, 0, "alpha", 0),
136+
CO_FUTURE_BARRY_AS_BDFL)
137+
138+
generator_stop = _Feature((3, 5, 0, "beta", 1),
139+
(3, 7, 0, "alpha", 0),
140+
CO_FUTURE_GENERATOR_STOP)

Lib/_compat_pickle.py

Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
# This module is used to map the old Python 2 names to the new names used in
2+
# Python 3 for the pickle module. This needed to make pickle streams
3+
# generated with Python 2 loadable by Python 3.
4+
5+
# This is a copy of lib2to3.fixes.fix_imports.MAPPING. We cannot import
6+
# lib2to3 and use the mapping defined there, because lib2to3 uses pickle.
7+
# Thus, this could cause the module to be imported recursively.
8+
IMPORT_MAPPING = {
9+
'__builtin__' : 'builtins',
10+
'copy_reg': 'copyreg',
11+
'Queue': 'queue',
12+
'SocketServer': 'socketserver',
13+
'ConfigParser': 'configparser',
14+
'repr': 'reprlib',
15+
'tkFileDialog': 'tkinter.filedialog',
16+
'tkSimpleDialog': 'tkinter.simpledialog',
17+
'tkColorChooser': 'tkinter.colorchooser',
18+
'tkCommonDialog': 'tkinter.commondialog',
19+
'Dialog': 'tkinter.dialog',
20+
'Tkdnd': 'tkinter.dnd',
21+
'tkFont': 'tkinter.font',
22+
'tkMessageBox': 'tkinter.messagebox',
23+
'ScrolledText': 'tkinter.scrolledtext',
24+
'Tkconstants': 'tkinter.constants',
25+
'Tix': 'tkinter.tix',
26+
'ttk': 'tkinter.ttk',
27+
'Tkinter': 'tkinter',
28+
'markupbase': '_markupbase',
29+
'_winreg': 'winreg',
30+
'thread': '_thread',
31+
'dummy_thread': '_dummy_thread',
32+
'dbhash': 'dbm.bsd',
33+
'dumbdbm': 'dbm.dumb',
34+
'dbm': 'dbm.ndbm',
35+
'gdbm': 'dbm.gnu',
36+
'xmlrpclib': 'xmlrpc.client',
37+
'SimpleXMLRPCServer': 'xmlrpc.server',
38+
'httplib': 'http.client',
39+
'htmlentitydefs' : 'html.entities',
40+
'HTMLParser' : 'html.parser',
41+
'Cookie': 'http.cookies',
42+
'cookielib': 'http.cookiejar',
43+
'BaseHTTPServer': 'http.server',
44+
'test.test_support': 'test.support',
45+
'commands': 'subprocess',
46+
'urlparse' : 'urllib.parse',
47+
'robotparser' : 'urllib.robotparser',
48+
'urllib2': 'urllib.request',
49+
'anydbm': 'dbm',
50+
'_abcoll' : 'collections.abc',
51+
}
52+
53+
54+
# This contains rename rules that are easy to handle. We ignore the more
55+
# complex stuff (e.g. mapping the names in the urllib and types modules).
56+
# These rules should be run before import names are fixed.
57+
NAME_MAPPING = {
58+
('__builtin__', 'xrange'): ('builtins', 'range'),
59+
('__builtin__', 'reduce'): ('functools', 'reduce'),
60+
('__builtin__', 'intern'): ('sys', 'intern'),
61+
('__builtin__', 'unichr'): ('builtins', 'chr'),
62+
('__builtin__', 'unicode'): ('builtins', 'str'),
63+
('__builtin__', 'long'): ('builtins', 'int'),
64+
('itertools', 'izip'): ('builtins', 'zip'),
65+
('itertools', 'imap'): ('builtins', 'map'),
66+
('itertools', 'ifilter'): ('builtins', 'filter'),
67+
('itertools', 'ifilterfalse'): ('itertools', 'filterfalse'),
68+
('itertools', 'izip_longest'): ('itertools', 'zip_longest'),
69+
('UserDict', 'IterableUserDict'): ('collections', 'UserDict'),
70+
('UserList', 'UserList'): ('collections', 'UserList'),
71+
('UserString', 'UserString'): ('collections', 'UserString'),
72+
('whichdb', 'whichdb'): ('dbm', 'whichdb'),
73+
('_socket', 'fromfd'): ('socket', 'fromfd'),
74+
('_multiprocessing', 'Connection'): ('multiprocessing.connection', 'Connection'),
75+
('multiprocessing.process', 'Process'): ('multiprocessing.context', 'Process'),
76+
('multiprocessing.forking', 'Popen'): ('multiprocessing.popen_fork', 'Popen'),
77+
('urllib', 'ContentTooShortError'): ('urllib.error', 'ContentTooShortError'),
78+
('urllib', 'getproxies'): ('urllib.request', 'getproxies'),
79+
('urllib', 'pathname2url'): ('urllib.request', 'pathname2url'),
80+
('urllib', 'quote_plus'): ('urllib.parse', 'quote_plus'),
81+
('urllib', 'quote'): ('urllib.parse', 'quote'),
82+
('urllib', 'unquote_plus'): ('urllib.parse', 'unquote_plus'),
83+
('urllib', 'unquote'): ('urllib.parse', 'unquote'),
84+
('urllib', 'url2pathname'): ('urllib.request', 'url2pathname'),
85+
('urllib', 'urlcleanup'): ('urllib.request', 'urlcleanup'),
86+
('urllib', 'urlencode'): ('urllib.parse', 'urlencode'),
87+
('urllib', 'urlopen'): ('urllib.request', 'urlopen'),
88+
('urllib', 'urlretrieve'): ('urllib.request', 'urlretrieve'),
89+
('urllib2', 'HTTPError'): ('urllib.error', 'HTTPError'),
90+
('urllib2', 'URLError'): ('urllib.error', 'URLError'),
91+
}
92+
93+
PYTHON2_EXCEPTIONS = (
94+
"ArithmeticError",
95+
"AssertionError",
96+
"AttributeError",
97+
"BaseException",
98+
"BufferError",
99+
"BytesWarning",
100+
"DeprecationWarning",
101+
"EOFError",
102+
"EnvironmentError",
103+
"Exception",
104+
"FloatingPointError",
105+
"FutureWarning",
106+
"GeneratorExit",
107+
"IOError",
108+
"ImportError",
109+
"ImportWarning",
110+
"IndentationError",
111+
"IndexError",
112+
"KeyError",
113+
"KeyboardInterrupt",
114+
"LookupError",
115+
"MemoryError",
116+
"NameError",
117+
"NotImplementedError",
118+
"OSError",
119+
"OverflowError",
120+
"PendingDeprecationWarning",
121+
"ReferenceError",
122+
"RuntimeError",
123+
"RuntimeWarning",
124+
# StandardError is gone in Python 3, so we map it to Exception
125+
"StopIteration",
126+
"SyntaxError",
127+
"SyntaxWarning",
128+
"SystemError",
129+
"SystemExit",
130+
"TabError",
131+
"TypeError",
132+
"UnboundLocalError",
133+
"UnicodeDecodeError",
134+
"UnicodeEncodeError",
135+
"UnicodeError",
136+
"UnicodeTranslateError",
137+
"UnicodeWarning",
138+
"UserWarning",
139+
"ValueError",
140+
"Warning",
141+
"ZeroDivisionError",
142+
)
143+
144+
try:
145+
WindowsError
146+
except NameError:
147+
pass
148+
else:
149+
PYTHON2_EXCEPTIONS += ("WindowsError",)
150+
151+
for excname in PYTHON2_EXCEPTIONS:
152+
NAME_MAPPING[("exceptions", excname)] = ("builtins", excname)
153+
154+
MULTIPROCESSING_EXCEPTIONS = (
155+
'AuthenticationError',
156+
'BufferTooShort',
157+
'ProcessError',
158+
'TimeoutError',
159+
)
160+
161+
for excname in MULTIPROCESSING_EXCEPTIONS:
162+
NAME_MAPPING[("multiprocessing", excname)] = ("multiprocessing.context", excname)
163+
164+
# Same, but for 3.x to 2.x
165+
REVERSE_IMPORT_MAPPING = dict((v, k) for (k, v) in IMPORT_MAPPING.items())
166+
assert len(REVERSE_IMPORT_MAPPING) == len(IMPORT_MAPPING)
167+
REVERSE_NAME_MAPPING = dict((v, k) for (k, v) in NAME_MAPPING.items())
168+
assert len(REVERSE_NAME_MAPPING) == len(NAME_MAPPING)
169+
170+
# Non-mutual mappings.
171+
172+
IMPORT_MAPPING.update({
173+
'cPickle': 'pickle',
174+
'_elementtree': 'xml.etree.ElementTree',
175+
'FileDialog': 'tkinter.filedialog',
176+
'SimpleDialog': 'tkinter.simpledialog',
177+
'DocXMLRPCServer': 'xmlrpc.server',
178+
'SimpleHTTPServer': 'http.server',
179+
'CGIHTTPServer': 'http.server',
180+
# For compatibility with broken pickles saved in old Python 3 versions
181+
'UserDict': 'collections',
182+
'UserList': 'collections',
183+
'UserString': 'collections',
184+
'whichdb': 'dbm',
185+
'StringIO': 'io',
186+
'cStringIO': 'io',
187+
})
188+
189+
REVERSE_IMPORT_MAPPING.update({
190+
'_bz2': 'bz2',
191+
'_dbm': 'dbm',
192+
'_functools': 'functools',
193+
'_gdbm': 'gdbm',
194+
'_pickle': 'pickle',
195+
})
196+
197+
NAME_MAPPING.update({
198+
('__builtin__', 'basestring'): ('builtins', 'str'),
199+
('exceptions', 'StandardError'): ('builtins', 'Exception'),
200+
('UserDict', 'UserDict'): ('collections', 'UserDict'),
201+
('socket', '_socketobject'): ('socket', 'SocketType'),
202+
})
203+
204+
REVERSE_NAME_MAPPING.update({
205+
('_functools', 'reduce'): ('__builtin__', 'reduce'),
206+
('tkinter.filedialog', 'FileDialog'): ('FileDialog', 'FileDialog'),
207+
('tkinter.filedialog', 'LoadFileDialog'): ('FileDialog', 'LoadFileDialog'),
208+
('tkinter.filedialog', 'SaveFileDialog'): ('FileDialog', 'SaveFileDialog'),
209+
('tkinter.simpledialog', 'SimpleDialog'): ('SimpleDialog', 'SimpleDialog'),
210+
('xmlrpc.server', 'ServerHTMLDoc'): ('DocXMLRPCServer', 'ServerHTMLDoc'),
211+
('xmlrpc.server', 'XMLRPCDocGenerator'):
212+
('DocXMLRPCServer', 'XMLRPCDocGenerator'),
213+
('xmlrpc.server', 'DocXMLRPCRequestHandler'):
214+
('DocXMLRPCServer', 'DocXMLRPCRequestHandler'),
215+
('xmlrpc.server', 'DocXMLRPCServer'):
216+
('DocXMLRPCServer', 'DocXMLRPCServer'),
217+
('xmlrpc.server', 'DocCGIXMLRPCRequestHandler'):
218+
('DocXMLRPCServer', 'DocCGIXMLRPCRequestHandler'),
219+
('http.server', 'SimpleHTTPRequestHandler'):
220+
('SimpleHTTPServer', 'SimpleHTTPRequestHandler'),
221+
('http.server', 'CGIHTTPRequestHandler'):
222+
('CGIHTTPServer', 'CGIHTTPRequestHandler'),
223+
('_socket', 'socket'): ('socket', '_socketobject'),
224+
})
225+
226+
PYTHON3_OSERROR_EXCEPTIONS = (
227+
'BrokenPipeError',
228+
'ChildProcessError',
229+
'ConnectionAbortedError',
230+
'ConnectionError',
231+
'ConnectionRefusedError',
232+
'ConnectionResetError',
233+
'FileExistsError',
234+
'FileNotFoundError',
235+
'InterruptedError',
236+
'IsADirectoryError',
237+
'NotADirectoryError',
238+
'PermissionError',
239+
'ProcessLookupError',
240+
'TimeoutError',
241+
)
242+
243+
for excname in PYTHON3_OSERROR_EXCEPTIONS:
244+
REVERSE_NAME_MAPPING[('builtins', excname)] = ('exceptions', 'OSError')
245+
246+
PYTHON3_IMPORTERROR_EXCEPTIONS = (
247+
'ModuleNotFoundError',
248+
)
249+
250+
for excname in PYTHON3_IMPORTERROR_EXCEPTIONS:
251+
REVERSE_NAME_MAPPING[('builtins', excname)] = ('exceptions', 'ImportError')

0 commit comments

Comments
 (0)