summaryrefslogtreecommitdiffstats
path: root/Lib/dis.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/dis.py')
0 files changed, 0 insertions, 0 deletions
: 0.0%;'/> -rw-r--r--.gitignore3
-rw-r--r--.hgignore4
-rw-r--r--.hgtags11
-rw-r--r--Doc/Makefile2
-rw-r--r--Doc/c-api/arg.rst37
-rw-r--r--Doc/c-api/exceptions.rst17
-rw-r--r--Doc/c-api/init.rst33
-rw-r--r--Doc/c-api/intro.rst11
-rw-r--r--Doc/c-api/long.rst18
-rw-r--r--Doc/c-api/memory.rst113
-rw-r--r--Doc/c-api/sys.rst25
-rw-r--r--Doc/c-api/unicode.rst53
-rw-r--r--Doc/data/refcounts.dat3
-rw-r--r--Doc/extending/embedding.rst6
-rw-r--r--Doc/faq/design.rst32
-rw-r--r--Doc/faq/programming.rst3
-rw-r--r--Doc/glossary.rst54
-rw-r--r--Doc/howto/argparse.rst3
-rw-r--r--Doc/howto/clinic.rst4
-rw-r--r--Doc/howto/index.rst1
-rw-r--r--Doc/howto/instrumentation.rst408
-rw-r--r--Doc/includes/email-alternative-new-api.py56
-rw-r--r--[-rwxr-xr-x]Doc/includes/email-alternative.py76
-rw-r--r--Doc/includes/email-dir.py55
-rw-r--r--Doc/includes/email-headers.py22
-rw-r--r--Doc/includes/email-mime.py29
-rw-r--r--Doc/includes/email-read-alternative.py (renamed from Doc/includes/email-read-alternative-new-api.py)0
-rw-r--r--Doc/includes/email-simple.py8
-rw-r--r--Doc/includes/email-unpack.py8
-rw-r--r--Doc/includes/run-func.c4
-rw-r--r--Doc/includes/test.py2
-rw-r--r--Doc/includes/tzinfo-examples.py130
-rw-r--r--Doc/installing/index.rst61
-rw-r--r--Doc/library/asynchat.rst19
-rw-r--r--Doc/library/asyncio-eventloop.rst23
-rw-r--r--Doc/library/asyncio-protocol.rst3
-rw-r--r--Doc/library/asyncio.rst7
-rw-r--r--Doc/library/asyncore.rst3
-rw-r--r--Doc/library/binascii.rst11
-rw-r--r--Doc/library/bz2.rst6
-rw-r--r--Doc/library/cmath.rst36
-rw-r--r--Doc/library/code.rst19
-rw-r--r--Doc/library/codecs.rst10
-rw-r--r--Doc/library/collections.abc.rst42
-rw-r--r--Doc/library/collections.rst40
-rw-r--r--Doc/library/compileall.rst18
-rw-r--r--Doc/library/concurrent.futures.rst6
-rw-r--r--Doc/library/constants.rst22
-rw-r--r--Doc/library/contextlib.rst20
-rw-r--r--Doc/library/crypt.rst2
-rw-r--r--Doc/library/crypto.rst1
-rw-r--r--Doc/library/csv.rst59
-rw-r--r--Doc/library/ctypes.rst17
-rw-r--r--Doc/library/datetime.rst305
-rw-r--r--Doc/library/dbm.rst4
-rw-r--r--Doc/library/decimal.rst23
-rw-r--r--Doc/library/dis.rst86
-rw-r--r--Doc/library/email.charset.rst5
-rw-r--r--Doc/library/email.compat32-message.rst754
-rw-r--r--Doc/library/email.contentmanager.rst304
-rw-r--r--Doc/library/email.encoders.rst6
-rw-r--r--Doc/library/email.errors.rst38
-rw-r--r--Doc/library/email.examples.rst (renamed from Doc/library/email-examples.rst)33
-rw-r--r--Doc/library/email.generator.rst376
-rw-r--r--Doc/library/email.header.rst8
-rw-r--r--Doc/library/email.headerregistry.rst49
-rw-r--r--Doc/library/email.message.rst719
-rw-r--r--Doc/library/email.mime.rst58
-rw-r--r--Doc/library/email.parser.rst358
-rw-r--r--Doc/library/email.policy.rst280
-rw-r--r--Doc/library/email.rst464
-rw-r--r--Doc/library/email.util.rst68
-rw-r--r--Doc/library/enum.rst623
-rw-r--r--Doc/library/exceptions.rst38
-rw-r--r--Doc/library/faulthandler.rst3
-rw-r--r--Doc/library/fileinput.rst19
-rw-r--r--Doc/library/fnmatch.rst2
-rw-r--r--Doc/library/ftplib.rst7
-rw-r--r--Doc/library/functions.rst68
-rw-r--r--Doc/library/glob.rst2
-rw-r--r--Doc/library/grp.rst3
-rw-r--r--Doc/library/gzip.rst5
-rw-r--r--Doc/library/hashlib-blake2-tree.pngbin0 -> 11148 bytes-rw-r--r--Doc/library/hashlib.rst511
-rw-r--r--Doc/library/http.client.rst115
-rw-r--r--Doc/library/http.server.rst14
-rw-r--r--Doc/library/imaplib.rst19
-rw-r--r--Doc/library/imghdr.rst3
-rw-r--r--Doc/library/imp.rst15
-rw-r--r--Doc/library/importlib.rst199
-rw-r--r--Doc/library/inspect.rst132
-rw-r--r--Doc/library/itertools.rst8
-rw-r--r--Doc/library/json.rst34
-rw-r--r--Doc/library/logging.handlers.rst104
-rw-r--r--Doc/library/lzma.rst18
-rw-r--r--Doc/library/math.rst9
-rw-r--r--Doc/library/mmap.rst9
-rw-r--r--Doc/library/multiprocessing.rst97
-rw-r--r--Doc/library/os.path.rst87
-rw-r--r--Doc/library/os.rst385
-rw-r--r--Doc/library/pathlib.rst37
-rw-r--r--Doc/library/pdb.rst8
-rw-r--r--Doc/library/pickle.rst19
-rw-r--r--Doc/library/pkgutil.rst9
-rw-r--r--Doc/library/poplib.rst7
-rw-r--r--Doc/library/queue.rst11
-rw-r--r--Doc/library/random.rst263
-rw-r--r--Doc/library/re.rst65
-rw-r--r--Doc/library/readline.rst14
-rw-r--r--Doc/library/secrets.rst198
-rw-r--r--Doc/library/select.rst69
-rw-r--r--Doc/library/shlex.rst107
-rw-r--r--Doc/library/shutil.rst2
-rw-r--r--Doc/library/site.rst3
-rw-r--r--Doc/library/smtpd.rst50
-rw-r--r--Doc/library/smtplib.rst8
-rw-r--r--Doc/library/socket.rst76
-rw-r--r--Doc/library/socketserver.rst68
-rw-r--r--Doc/library/spwd.rst3
-rw-r--r--Doc/library/sqlite3.rst20
-rw-r--r--Doc/library/ssl.rst275
-rw-r--r--Doc/library/statistics.rst43
-rw-r--r--Doc/library/stdtypes.rst27
-rw-r--r--Doc/library/string.rst23
-rw-r--r--Doc/library/struct.rst27
-rw-r--r--Doc/library/subprocess.rst107
-rw-r--r--Doc/library/sys.rst123
-rw-r--r--Doc/library/sysconfig.rst2
-rw-r--r--Doc/library/telnetlib.rst11
-rw-r--r--Doc/library/test.rst42
-rw-r--r--Doc/library/time.rst12
-rw-r--r--Doc/library/timeit.rst21
-rw-r--r--Doc/library/tkinter.tix.rst4
-rw-r--r--Doc/library/traceback.rst14
-rw-r--r--Doc/library/tracemalloc.rst45
-rw-r--r--Doc/library/types.rst14
-rw-r--r--Doc/library/typing.rst50
-rw-r--r--Doc/library/undoc.rst2
-rw-r--r--Doc/library/unicodedata.rst8
-rw-r--r--Doc/library/unittest.mock.rst40
-rw-r--r--Doc/library/unittest.rst6
-rw-r--r--Doc/library/urllib.parse.rst18
-rw-r--r--Doc/library/urllib.request.rst65
-rw-r--r--Doc/library/urllib.robotparser.rst30
-rw-r--r--Doc/library/venv.rst134
-rw-r--r--Doc/library/warnings.rst16
-rw-r--r--Doc/library/winreg.rst14
-rw-r--r--Doc/library/winsound.rst8
-rw-r--r--Doc/library/wsgiref.rst32
-rw-r--r--Doc/library/xmlrpc.client.rst17
-rw-r--r--Doc/library/xmlrpc.server.rst59
-rw-r--r--Doc/library/zipfile.rst129
-rw-r--r--Doc/library/zlib.rst31
-rw-r--r--Doc/make.bat2
-rw-r--r--Doc/reference/compound_stmts.rst26
-rw-r--r--Doc/reference/datamodel.rst183
-rw-r--r--Doc/reference/expressions.rst166
-rw-r--r--Doc/reference/import.rst37
-rw-r--r--Doc/reference/lexical_analysis.rst163
-rw-r--r--Doc/reference/simple_stmts.rst56
-rw-r--r--Doc/tools/extensions/pyspecific.py2
-rwxr-xr-xDoc/tools/rstlint.py2
-rw-r--r--Doc/tools/susp-ignored.csv63
-rw-r--r--Doc/tools/templates/indexsidebar.html2
-rw-r--r--Doc/tutorial/controlflow.rst3
-rw-r--r--Doc/tutorial/datastructures.rst53
-rw-r--r--Doc/tutorial/errors.rst2
-rw-r--r--Doc/tutorial/inputoutput.rst3
-rw-r--r--Doc/tutorial/interpreter.rst12
-rw-r--r--Doc/tutorial/introduction.rst3
-rw-r--r--Doc/tutorial/stdlib.rst2
-rw-r--r--Doc/tutorial/stdlib2.rst2
-rw-r--r--Doc/tutorial/venv.rst111
-rw-r--r--Doc/using/cmdline.rst99
-rw-r--r--Doc/using/index.rst1
-rw-r--r--Doc/using/mac.rst6
-rw-r--r--Doc/using/scripts.rst12
-rw-r--r--Doc/using/venv-create.inc95
-rw-r--r--Doc/using/windows.rst143
-rw-r--r--Doc/whatsnew/2.1.rst2
-rw-r--r--Doc/whatsnew/3.2.rst4
-rw-r--r--Doc/whatsnew/3.3.rst2
-rw-r--r--Doc/whatsnew/3.4.rst10
-rw-r--r--Doc/whatsnew/3.5.rst6
-rw-r--r--Doc/whatsnew/3.6.rst2307
-rw-r--r--Doc/whatsnew/index.rst1
-rw-r--r--Grammar/Grammar24
-rw-r--r--Include/Python-ast.h56
-rw-r--r--Include/Python.h1
-rw-r--r--Include/abstract.h98
-rw-r--r--Include/asdl.h1
-rw-r--r--Include/bytes_methods.h27
-rw-r--r--Include/bytesobject.h97
-rw-r--r--Include/ceval.h23
-rw-r--r--Include/code.h32
-rw-r--r--Include/codecs.h4
-rw-r--r--Include/datetime.h17
-rw-r--r--Include/descrobject.h2
-rw-r--r--Include/dictobject.h29
-rw-r--r--Include/fileobject.h3
-rw-r--r--Include/fileutils.h22
-rw-r--r--Include/floatobject.h10
-rw-r--r--Include/funcobject.h14
-rw-r--r--Include/genobject.h33
-rw-r--r--Include/graminit.h137
-rw-r--r--Include/grammar.h1
-rw-r--r--Include/import.h10
-rw-r--r--Include/intrcheck.h3
-rw-r--r--Include/longintrepr.h12
-rw-r--r--Include/longobject.h26
-rw-r--r--Include/memoryobject.h2
-rw-r--r--Include/methodobject.h16
-rw-r--r--Include/modsupport.h30
-rw-r--r--Include/moduleobject.h4
-rw-r--r--Include/namespaceobject.h2
-rw-r--r--Include/object.h22
-rw-r--r--Include/objimpl.h9
-rw-r--r--Include/odictobject.h9
-rw-r--r--Include/opcode.h10
-rw-r--r--Include/osmodule.h17
-rw-r--r--Include/patchlevel.h6
-rw-r--r--Include/pgenheaders.h1
-rw-r--r--Include/py_curses.h4
-rw-r--r--Include/pyatomic.h4
-rw-r--r--Include/pydebug.h4
-rw-r--r--Include/pydtrace.d19
-rw-r--r--Include/pydtrace.h53
-rw-r--r--Include/pyerrors.h34
-rw-r--r--Include/pygetopt.h2
-rw-r--r--Include/pyhash.h16
-rw-r--r--Include/pylifecycle.h7
-rw-r--r--Include/pymacro.h15
-rw-r--r--Include/pymath.h8
-rw-r--r--Include/pymem.h45
-rw-r--r--Include/pyport.h190
-rw-r--r--Include/pystate.h49
-rw-r--r--Include/pystrhex.h2
-rw-r--r--Include/pystrtod.h4
-rw-r--r--Include/pythonrun.h10
-rw-r--r--Include/pythread.h13
-rw-r--r--Include/pytime.h21
-rw-r--r--Include/setobject.h11
-rw-r--r--Include/structmember.h2
-rw-r--r--Include/symtable.h2
-rw-r--r--Include/sysmodule.h4
-rw-r--r--Include/traceback.h52
-rw-r--r--Include/unicodeobject.h138
-rw-r--r--Include/warnings.h9
-rw-r--r--Lib/_collections_abc.py170
-rw-r--r--Lib/_compat_pickle.py7
-rw-r--r--Lib/_osx_support.py10
-rw-r--r--Lib/_pydecimal.py62
-rw-r--r--Lib/_pyio.py9
-rw-r--r--Lib/_strptime.py83
-rw-r--r--Lib/aifc.py9
-rw-r--r--Lib/argparse.py10
-rw-r--r--Lib/ast.py52
-rw-r--r--Lib/asynchat.py29
-rw-r--r--Lib/asyncio/base_events.py5
-rw-r--r--Lib/asyncio/base_futures.py71
-rw-r--r--Lib/asyncio/base_subprocess.py3
-rw-r--r--Lib/asyncio/base_tasks.py76
-rw-r--r--Lib/asyncio/coroutines.py4
-rw-r--r--Lib/asyncio/futures.py92
-rw-r--r--Lib/asyncio/proactor_events.py3
-rw-r--r--Lib/asyncio/selector_events.py3
-rw-r--r--Lib/asyncio/sslproto.py3
-rw-r--r--Lib/asyncio/tasks.py80
-rw-r--r--Lib/asyncio/test_utils.py8
-rw-r--r--Lib/asyncio/unix_events.py6
-rw-r--r--Lib/asyncio/windows_events.py9
-rw-r--r--Lib/asyncio/windows_utils.py3
-rw-r--r--Lib/asyncore.py5
-rwxr-xr-xLib/base64.py5
-rw-r--r--Lib/bz2.py16
-rw-r--r--Lib/calendar.py85
-rwxr-xr-xLib/cgi.py2
-rw-r--r--Lib/code.py16
-rw-r--r--Lib/collections/__init__.py22
-rw-r--r--Lib/collections/__main__.py38
-rw-r--r--Lib/compileall.py20
-rw-r--r--Lib/concurrent/futures/thread.py11
-rw-r--r--Lib/contextlib.py42
-rw-r--r--Lib/copy.py115
-rw-r--r--Lib/crypt.py3
-rw-r--r--Lib/csv.py24
-rw-r--r--Lib/ctypes/__init__.py17
-rw-r--r--Lib/ctypes/test/test_bitfields.py5
-rw-r--r--Lib/ctypes/test/test_find.py43
-rw-r--r--Lib/ctypes/test/test_funcptr.py4
-rw-r--r--Lib/ctypes/test/test_loading.py17
-rw-r--r--Lib/ctypes/test/test_objects.py2
-rw-r--r--Lib/ctypes/test/test_parameters.py8
-rw-r--r--Lib/ctypes/test/test_pep3118.py2
-rw-r--r--Lib/ctypes/test/test_returnfuncptrs.py1
-rw-r--r--Lib/ctypes/test/test_sizes.py1
-rw-r--r--Lib/ctypes/test/test_structures.py7
-rw-r--r--Lib/ctypes/test/test_values.py6
-rw-r--r--Lib/ctypes/util.py40
-rw-r--r--Lib/datetime.py312
-rw-r--r--Lib/dbm/dumb.py17
-rw-r--r--Lib/difflib.py2
-rw-r--r--Lib/dis.py61
-rw-r--r--Lib/distutils/archive_util.py2
-rw-r--r--Lib/distutils/cmd.py5
-rw-r--r--Lib/distutils/command/bdist_dumb.py2
-rw-r--r--Lib/distutils/command/bdist_msi.py6
-rw-r--r--Lib/distutils/command/bdist_wininst.py2
-rw-r--r--Lib/distutils/command/build.py4
-rw-r--r--Lib/distutils/command/build_ext.py6
-rw-r--r--Lib/distutils/command/build_scripts.py2
-rw-r--r--Lib/distutils/command/config.py4
-rw-r--r--Lib/distutils/command/install.py12
-rw-r--r--Lib/distutils/command/install_egg_info.py4
-rw-r--r--Lib/distutils/command/register.py8
-rw-r--r--Lib/distutils/command/sdist.py15
-rw-r--r--Lib/distutils/core.py5
-rw-r--r--Lib/distutils/cygwinccompiler.py2
-rw-r--r--Lib/distutils/dist.py3
-rw-r--r--Lib/distutils/extension.py1
-rw-r--r--Lib/distutils/filelist.py15
-rw-r--r--Lib/distutils/msvc9compiler.py2
-rw-r--r--Lib/distutils/sysconfig.py48
-rw-r--r--Lib/distutils/tests/test_bdist_rpm.py2
-rw-r--r--Lib/distutils/tests/test_build.py5
-rw-r--r--Lib/distutils/tests/test_build_clib.py20
-rw-r--r--Lib/distutils/tests/test_build_ext.py7
-rw-r--r--Lib/distutils/tests/test_build_py.py3
-rw-r--r--Lib/distutils/tests/test_clean.py2
-rw-r--r--Lib/distutils/tests/test_config.py2
-rw-r--r--Lib/distutils/tests/test_config_cmd.py5
-rw-r--r--Lib/distutils/tests/test_core.py30
-rw-r--r--Lib/distutils/tests/test_cygwinccompiler.py3
-rw-r--r--Lib/distutils/tests/test_dep_util.py1
-rw-r--r--Lib/distutils/tests/test_file_util.py1
-rw-r--r--Lib/distutils/tests/test_filelist.py14
-rw-r--r--Lib/distutils/tests/test_install.py4
-rw-r--r--Lib/distutils/tests/test_install_data.py2
-rw-r--r--Lib/distutils/tests/test_install_headers.py2
-rw-r--r--Lib/distutils/tests/test_install_lib.py3
-rw-r--r--Lib/distutils/tests/test_spawn.py14
-rw-r--r--Lib/distutils/tests/test_sysconfig.py9
-rw-r--r--Lib/distutils/tests/test_unixccompiler.py1
-rw-r--r--Lib/distutils/text_file.py2
-rw-r--r--Lib/distutils/util.py15
-rw-r--r--Lib/distutils/versionpredicate.py2
-rw-r--r--Lib/doctest.py25
-rw-r--r--Lib/email/_header_value_parser.py16
-rw-r--r--Lib/email/_policybase.py4
-rw-r--r--Lib/email/contentmanager.py6
-rw-r--r--Lib/email/feedparser.py19
-rw-r--r--Lib/email/generator.py6
-rw-r--r--Lib/email/header.py4
-rw-r--r--Lib/email/message.py25
-rw-r--r--Lib/email/mime/application.py5
-rw-r--r--Lib/email/mime/audio.py5
-rw-r--r--Lib/email/mime/base.py8
-rw-r--r--Lib/email/mime/image.py5
-rw-r--r--Lib/email/mime/message.py4
-rw-r--r--Lib/email/mime/multipart.py3
-rw-r--r--Lib/email/mime/text.py4
-rw-r--r--Lib/email/policy.py2
-rw-r--r--Lib/encodings/__init__.py17
-rw-r--r--Lib/encodings/aliases.py1
-rw-r--r--Lib/encodings/oem.py41
-rw-r--r--Lib/ensurepip/__init__.py20
-rw-r--r--Lib/enum.py328
-rw-r--r--Lib/fileinput.py14
-rw-r--r--Lib/fnmatch.py2
-rw-r--r--Lib/fractions.py34
-rw-r--r--Lib/ftplib.py11
-rw-r--r--Lib/functools.py106
-rw-r--r--Lib/genericpath.py6
-rw-r--r--Lib/glob.py70
-rw-r--r--Lib/gzip.py8
-rw-r--r--Lib/hashlib.py54
-rw-r--r--Lib/hmac.py2
-rw-r--r--Lib/html/parser.py4
-rw-r--r--Lib/http/client.py241
-rw-r--r--Lib/http/cookiejar.py6
-rw-r--r--Lib/http/cookies.py5
-rw-r--r--Lib/http/server.py66
-rw-r--r--Lib/idlelib/NEWS.txt96
-rw-r--r--Lib/idlelib/README.txt287
-rw-r--r--Lib/idlelib/__init__.py2
-rw-r--r--Lib/idlelib/__main__.py4
-rw-r--r--Lib/idlelib/autocomplete.py (renamed from Lib/idlelib/AutoComplete.py)67
-rw-r--r--Lib/idlelib/autocomplete_w.py (renamed from Lib/idlelib/AutoCompleteWindow.py)41
-rw-r--r--Lib/idlelib/autoexpand.py (renamed from Lib/idlelib/AutoExpand.py)7
-rw-r--r--Lib/idlelib/browser.py (renamed from Lib/idlelib/ClassBrowser.py)18
-rw-r--r--Lib/idlelib/calltip_w.py (renamed from Lib/idlelib/CallTipWindow.py)8
-rw-r--r--Lib/idlelib/calltips.py (renamed from Lib/idlelib/CallTips.py)12
-rw-r--r--Lib/idlelib/codecontext.py (renamed from Lib/idlelib/CodeContext.py)12
-rw-r--r--Lib/idlelib/colorizer.py (renamed from Lib/idlelib/ColorDelegator.py)40
-rw-r--r--Lib/idlelib/config-keys.def51
-rw-r--r--Lib/idlelib/config-main.def74
-rw-r--r--Lib/idlelib/config.py (renamed from Lib/idlelib/configHandler.py)185
-rw-r--r--Lib/idlelib/configHelpSourceEdit.py170
-rw-r--r--Lib/idlelib/configSectionNameDialog.py98
-rw-r--r--Lib/idlelib/config_key.py (renamed from Lib/idlelib/keybindingDialog.py)21
-rw-r--r--Lib/idlelib/configdialog.py (renamed from Lib/idlelib/configDialog.py)163
-rw-r--r--Lib/idlelib/debugger.py (renamed from Lib/idlelib/Debugger.py)21
-rw-r--r--Lib/idlelib/debugger_r.py (renamed from Lib/idlelib/RemoteDebugger.py)8
-rw-r--r--Lib/idlelib/debugobj.py (renamed from Lib/idlelib/ObjectBrowser.py)26
-rw-r--r--Lib/idlelib/debugobj_r.py (renamed from Lib/idlelib/RemoteObjectBrowser.py)0
-rw-r--r--Lib/idlelib/delegator.py (renamed from Lib/idlelib/Delegator.py)0
-rw-r--r--Lib/idlelib/dynoption.py (renamed from Lib/idlelib/dynOptionMenuWidget.py)9
-rw-r--r--Lib/idlelib/editor.py (renamed from Lib/idlelib/EditorWindow.py)276
-rw-r--r--Lib/idlelib/filelist.py (renamed from Lib/idlelib/FileList.py)5
-rw-r--r--Lib/idlelib/grep.py (renamed from Lib/idlelib/GrepDialog.py)48
-rw-r--r--Lib/idlelib/help.py26
-rw-r--r--Lib/idlelib/help.txt372
-rw-r--r--Lib/idlelib/help_about.py (renamed from Lib/idlelib/aboutDialog.py)13
-rw-r--r--Lib/idlelib/history.py (renamed from Lib/idlelib/IdleHistory.py)8
-rw-r--r--Lib/idlelib/hyperparser.py (renamed from Lib/idlelib/HyperParser.py)7
-rw-r--r--Lib/idlelib/idle.py3
-rw-r--r--Lib/idlelib/idle.pyw12
-rw-r--r--Lib/idlelib/idle_test/__init__.py2
-rw-r--r--Lib/idlelib/idle_test/htest.py145
-rw-r--r--Lib/idlelib/idle_test/mock_idle.py4
-rw-r--r--Lib/idlelib/idle_test/test_autocomplete.py13
-rw-r--r--Lib/idlelib/idle_test/test_autoexpand.py6
-rw-r--r--Lib/idlelib/idle_test/test_calltips.py2
-rw-r--r--Lib/idlelib/idle_test/test_colorizer.py56
-rw-r--r--Lib/idlelib/idle_test/test_config.py160
-rw-r--r--Lib/idlelib/idle_test/test_config_help.py106
-rw-r--r--Lib/idlelib/idle_test/test_config_key.py33
-rw-r--r--Lib/idlelib/idle_test/test_config_name.py75
-rw-r--r--Lib/idlelib/idle_test/test_configdialog.py135
-rw-r--r--Lib/idlelib/idle_test/test_debugger.py29
-rw-r--r--Lib/idlelib/idle_test/test_delegator.py2
-rw-r--r--Lib/idlelib/idle_test/test_editmenu.py30
-rw-r--r--Lib/idlelib/idle_test/test_editor.py2
-rw-r--r--Lib/idlelib/idle_test/test_grep.py6
-rw-r--r--Lib/idlelib/idle_test/test_help.py34
-rw-r--r--Lib/idlelib/idle_test/test_help_about.py24
-rw-r--r--Lib/idlelib/idle_test/test_history.py (renamed from Lib/idlelib/idle_test/test_idlehistory.py)4
-rw-r--r--Lib/idlelib/idle_test/test_hyperparser.py6
-rw-r--r--Lib/idlelib/idle_test/test_iomenu.py (renamed from Lib/idlelib/idle_test/test_io.py)5
-rw-r--r--Lib/idlelib/idle_test/test_macosx.py103
-rw-r--r--Lib/idlelib/idle_test/test_paragraph.py (renamed from Lib/idlelib/idle_test/test_formatparagraph.py)8
-rw-r--r--Lib/idlelib/idle_test/test_parenmatch.py17
-rw-r--r--Lib/idlelib/idle_test/test_pathbrowser.py8
-rw-r--r--Lib/idlelib/idle_test/test_percolator.py4
-rw-r--r--Lib/idlelib/idle_test/test_query.py353
-rw-r--r--Lib/idlelib/idle_test/test_redirector.py (renamed from Lib/idlelib/idle_test/test_widgetredir.py)5
-rw-r--r--Lib/idlelib/idle_test/test_replace.py (renamed from Lib/idlelib/idle_test/test_replacedialog.py)16
-rw-r--r--Lib/idlelib/idle_test/test_rstrip.py4
-rw-r--r--Lib/idlelib/idle_test/test_scrolledlist.py29
-rw-r--r--Lib/idlelib/idle_test/test_search.py (renamed from Lib/idlelib/idle_test/test_searchdialog.py)8
-rw-r--r--Lib/idlelib/idle_test/test_searchbase.py (renamed from Lib/idlelib/idle_test/test_searchdialogbase.py)27
-rw-r--r--Lib/idlelib/idle_test/test_searchengine.py16
-rw-r--r--Lib/idlelib/idle_test/test_text.py21
-rw-r--r--Lib/idlelib/idle_test/test_textview.py20
-rw-r--r--Lib/idlelib/idle_test/test_tree.py36
-rw-r--r--Lib/idlelib/idle_test/test_undo.py (renamed from Lib/idlelib/idle_test/test_undodelegator.py)8
-rw-r--r--Lib/idlelib/idle_test/test_warning.py6
-rw-r--r--Lib/idlelib/idlever.py12
-rw-r--r--Lib/idlelib/iomenu.py (renamed from Lib/idlelib/IOBinding.py)95
-rw-r--r--Lib/idlelib/macosx.py (renamed from Lib/idlelib/macosxSupport.py)99
-rw-r--r--Lib/idlelib/mainmenu.py (renamed from Lib/idlelib/Bindings.py)4
-rw-r--r--Lib/idlelib/multicall.py (renamed from Lib/idlelib/MultiCall.py)17
-rw-r--r--Lib/idlelib/outwin.py (renamed from Lib/idlelib/OutputWindow.py)11
-rw-r--r--Lib/idlelib/paragraph.py (renamed from Lib/idlelib/FormatParagraph.py)10
-rw-r--r--Lib/idlelib/parenmatch.py (renamed from Lib/idlelib/ParenMatch.py)14
-rw-r--r--Lib/idlelib/pathbrowser.py (renamed from Lib/idlelib/PathBrowser.py)11
-rw-r--r--Lib/idlelib/percolator.py (renamed from Lib/idlelib/Percolator.py)13
-rw-r--r--Lib/idlelib/pyparse.py (renamed from Lib/idlelib/PyParse.py)2
-rwxr-xr-xLib/idlelib/pyshell.py (renamed from Lib/idlelib/PyShell.py)220
-rw-r--r--Lib/idlelib/query.py308
-rw-r--r--Lib/idlelib/redirector.py (renamed from Lib/idlelib/WidgetRedirector.py)21
-rw-r--r--Lib/idlelib/replace.py (renamed from Lib/idlelib/ReplaceDialog.py)26
-rw-r--r--Lib/idlelib/rpc.py26
-rw-r--r--Lib/idlelib/rstrip.py (renamed from Lib/idlelib/RstripExtension.py)0
-rw-r--r--Lib/idlelib/run.py159
-rw-r--r--Lib/idlelib/runscript.py (renamed from Lib/idlelib/ScriptBinding.py)17
-rw-r--r--Lib/idlelib/scrolledlist.py (renamed from Lib/idlelib/ScrolledList.py)21
-rw-r--r--Lib/idlelib/search.py (renamed from Lib/idlelib/SearchDialog.py)29
-rw-r--r--Lib/idlelib/searchbase.py (renamed from Lib/idlelib/SearchDialogBase.py)40
-rw-r--r--Lib/idlelib/searchengine.py (renamed from Lib/idlelib/SearchEngine.py)5
-rw-r--r--Lib/idlelib/stackviewer.py (renamed from Lib/idlelib/StackViewer.py)30
-rw-r--r--Lib/idlelib/statusbar.py (renamed from Lib/idlelib/MultiStatusBar.py)35
-rw-r--r--Lib/idlelib/tabbedpages.py26
-rw-r--r--Lib/idlelib/textview.py (renamed from Lib/idlelib/textView.py)34
-rw-r--r--Lib/idlelib/tooltip.py (renamed from Lib/idlelib/ToolTip.py)19
-rw-r--r--Lib/idlelib/tree.py (renamed from Lib/idlelib/TreeWidget.py)23
-rw-r--r--Lib/idlelib/undo.py (renamed from Lib/idlelib/UndoDelegator.py)26
-rw-r--r--Lib/idlelib/windows.py (renamed from Lib/idlelib/WindowList.py)2
-rw-r--r--Lib/idlelib/zoomheight.py (renamed from Lib/idlelib/ZoomHeight.py)6
-rw-r--r--Lib/imaplib.py19
-rw-r--r--Lib/imghdr.py4
-rw-r--r--Lib/imp.py2
-rw-r--r--Lib/importlib/_bootstrap.py70
-rw-r--r--Lib/importlib/_bootstrap_external.py102
-rw-r--r--Lib/importlib/util.py28
-rw-r--r--Lib/inspect.py96
-rw-r--r--Lib/io.py7
-rw-r--r--Lib/ipaddress.py4
-rw-r--r--Lib/json/__init__.py59
-rw-r--r--Lib/json/decoder.py2
-rw-r--r--Lib/json/encoder.py5
-rw-r--r--Lib/lib2to3/Grammar.txt7
-rw-r--r--Lib/lib2to3/fixer_util.py2
-rw-r--r--Lib/lib2to3/fixes/fix_dict.py3
-rw-r--r--Lib/lib2to3/fixes/fix_exec.py1
-rw-r--r--Lib/lib2to3/fixes/fix_filter.py1
-rw-r--r--Lib/lib2to3/fixes/fix_has_key.py1
-rw-r--r--Lib/lib2to3/fixes/fix_metaclass.py2
-rw-r--r--Lib/lib2to3/fixes/fix_nonzero.py2
-rw-r--r--Lib/lib2to3/fixes/fix_print.py2
-rw-r--r--Lib/lib2to3/fixes/fix_types.py3
-rw-r--r--Lib/lib2to3/fixes/fix_urllib.py1
-rw-r--r--Lib/lib2to3/refactor.py3
-rw-r--r--Lib/lib2to3/tests/data/py3_test_grammar.py2
-rw-r--r--Lib/lib2to3/tests/support.py2
-rw-r--r--Lib/lib2to3/tests/test_all_fixers.py1
-rw-r--r--Lib/lib2to3/tests/test_fixers.py7
-rw-r--r--Lib/lib2to3/tests/test_parser.py50
-rw-r--r--Lib/lib2to3/tests/test_pytree.py5
-rw-r--r--Lib/lib2to3/tests/test_refactor.py6
-rw-r--r--Lib/lib2to3/tests/test_util.py3
-rw-r--r--Lib/locale.py8
-rw-r--r--Lib/logging/__init__.py35
-rw-r--r--Lib/logging/handlers.py34
-rw-r--r--Lib/lzma.py18
-rw-r--r--Lib/mailbox.py28
-rw-r--r--Lib/mimetypes.py6
-rw-r--r--Lib/modulefinder.py7
-rw-r--r--Lib/msilib/__init__.py12
-rw-r--r--Lib/multiprocessing/connection.py8
-rw-r--r--Lib/multiprocessing/context.py13
-rw-r--r--Lib/multiprocessing/forkserver.py2
-rw-r--r--Lib/multiprocessing/heap.py5
-rw-r--r--Lib/multiprocessing/managers.py100
-rw-r--r--Lib/multiprocessing/pool.py20
-rw-r--r--Lib/multiprocessing/popen_forkserver.py7
-rw-r--r--Lib/multiprocessing/popen_spawn_posix.py7
-rw-r--r--Lib/multiprocessing/popen_spawn_win32.py9
-rw-r--r--Lib/multiprocessing/queues.py10
-rw-r--r--Lib/multiprocessing/reduction.py34
-rw-r--r--Lib/multiprocessing/resource_sharer.py2
-rw-r--r--Lib/multiprocessing/sharedctypes.py6
-rw-r--r--Lib/multiprocessing/spawn.py9
-rw-r--r--Lib/ntpath.py20
-rw-r--r--Lib/opcode.py32
-rw-r--r--Lib/optparse.py3
-rw-r--r--Lib/os.py253
-rw-r--r--Lib/pathlib.py147
-rwxr-xr-xLib/pdb.py22
-rw-r--r--Lib/pickle.py105
-rw-r--r--Lib/pickletools.py3
-rw-r--r--Lib/pkgutil.py31
-rw-r--r--Lib/plat-aix4/IN.py165
-rwxr-xr-xLib/plat-aix4/regen8
-rw-r--r--Lib/plat-darwin/IN.py662
-rwxr-xr-xLib/plat-darwin/regen3
-rw-r--r--Lib/plat-freebsd4/IN.py355
-rwxr-xr-xLib/plat-freebsd4/regen3
-rw-r--r--Lib/plat-freebsd5/IN.py355
-rwxr-xr-xLib/plat-freebsd5/regen3
-rw-r--r--Lib/plat-freebsd6/IN.py551
-rwxr-xr-xLib/plat-freebsd6/regen3
-rw-r--r--Lib/plat-freebsd7/IN.py571
-rwxr-xr-xLib/plat-freebsd7/regen3
-rw-r--r--Lib/plat-freebsd8/IN.py571
-rwxr-xr-xLib/plat-freebsd8/regen3
-rwxr-xr-xLib/plat-generic/regen3
-rw-r--r--Lib/plat-linux/CDROM.py207
-rw-r--r--Lib/plat-linux/DLFCN.py83
-rw-r--r--Lib/plat-linux/IN.py615
-rw-r--r--Lib/plat-linux/TYPES.py170
-rwxr-xr-xLib/plat-linux/regen8
-rw-r--r--Lib/plat-netbsd1/IN.py56
-rwxr-xr-xLib/plat-netbsd1/regen3
-rwxr-xr-xLib/plat-next3/regen6
-rw-r--r--Lib/plat-sunos5/CDIO.py73
-rw-r--r--Lib/plat-sunos5/DLFCN.py27
-rw-r--r--Lib/plat-sunos5/IN.py1421
-rw-r--r--Lib/plat-sunos5/STROPTS.py1813
-rw-r--r--Lib/plat-sunos5/TYPES.py313
-rwxr-xr-xLib/plat-sunos5/regen9
-rw-r--r--Lib/plat-unixware7/IN.py836
-rw-r--r--Lib/plat-unixware7/STROPTS.py328
-rwxr-xr-xLib/plat-unixware7/regen9
-rwxr-xr-xLib/platform.py37
-rw-r--r--Lib/plistlib.py2
-rw-r--r--Lib/poplib.py4
-rw-r--r--Lib/posixpath.py19
-rw-r--r--Lib/pyclbr.py3
-rw-r--r--[-rwxr-xr-x]Lib/pydoc.py36
-rw-r--r--Lib/pydoc_data/topics.py13059
-rw-r--r--Lib/random.py56
-rw-r--r--Lib/re.py51
-rw-r--r--Lib/rlcompleter.py38
-rw-r--r--Lib/sched.py17
-rw-r--r--Lib/secrets.py73
-rw-r--r--Lib/shlex.py72
-rw-r--r--Lib/shutil.py3
-rw-r--r--Lib/site-packages/README.txt2
-rw-r--r--Lib/site.py93
-rwxr-xr-xLib/smtpd.py42
-rwxr-xr-xLib/smtplib.py8
-rw-r--r--Lib/sndhdr.py12
-rw-r--r--Lib/socket.py12
-rw-r--r--Lib/socketserver.py210
-rw-r--r--Lib/sqlite3/test/dbapi.py43
-rw-r--r--Lib/sqlite3/test/transactions.py36
-rw-r--r--Lib/sre_compile.py140
-rw-r--r--Lib/sre_parse.py191
-rw-r--r--Lib/ssl.py179
-rw-r--r--Lib/statistics.py151
-rw-r--r--Lib/string.py11
-rw-r--r--Lib/subprocess.py115
-rwxr-xr-xLib/symbol.py137
-rw-r--r--Lib/symtable.py5
-rw-r--r--Lib/sysconfig.py28
-rwxr-xr-xLib/tarfile.py139
-rw-r--r--Lib/telnetlib.py15
-rw-r--r--Lib/tempfile.py1
-rw-r--r--Lib/test/__main__.py5
-rw-r--r--Lib/test/_test_multiprocessing.py96
-rw-r--r--Lib/test/ann_module.py53
-rw-r--r--Lib/test/ann_module2.py36
-rw-r--r--Lib/test/ann_module3.py18
-rw-r--r--Lib/test/audiotests.py3
-rw-r--r--Lib/test/autotest.py5
-rw-r--r--Lib/test/badsyntax_async1.py2
-rw-r--r--Lib/test/badsyntax_async2.py2
-rw-r--r--Lib/test/badsyntax_async3.py2
-rw-r--r--Lib/test/badsyntax_async4.py2
-rw-r--r--Lib/test/badsyntax_async5.py2
-rw-r--r--Lib/test/badsyntax_async6.py2
-rw-r--r--Lib/test/badsyntax_async7.py2
-rw-r--r--Lib/test/badsyntax_async8.py2
-rw-r--r--Lib/test/capath/0e4015b9.016
-rw-r--r--Lib/test/capath/b1930218.021
-rw-r--r--Lib/test/capath/ce7b8643.016
-rw-r--r--Lib/test/capath/ceff1710.021
-rw-r--r--Lib/test/cmath_testcases.txt141
-rw-r--r--Lib/test/datetimetester.py900
-rw-r--r--Lib/test/dtracedata/assert_usable.d5
-rw-r--r--Lib/test/dtracedata/assert_usable.stp5
-rw-r--r--Lib/test/dtracedata/call_stack.d31
-rw-r--r--Lib/test/dtracedata/call_stack.d.expected18
-rw-r--r--Lib/test/dtracedata/call_stack.py30
-rw-r--r--Lib/test/dtracedata/call_stack.stp41
-rw-r--r--Lib/test/dtracedata/call_stack.stp.expected14
-rw-r--r--Lib/test/dtracedata/gc.d18
-rw-r--r--Lib/test/dtracedata/gc.d.expected8
-rw-r--r--Lib/test/dtracedata/gc.py13
-rw-r--r--Lib/test/dtracedata/gc.stp26
-rw-r--r--Lib/test/dtracedata/gc.stp.expected8
-rw-r--r--Lib/test/dtracedata/instance.py24
-rw-r--r--Lib/test/dtracedata/line.d7
-rw-r--r--Lib/test/dtracedata/line.d.expected20
-rw-r--r--Lib/test/dtracedata/line.py17
-rw-r--r--Lib/test/eintrdata/eintr_tester.py15
-rw-r--r--Lib/test/exception_hierarchy.txt1
-rw-r--r--Lib/test/libregrtest/__init__.py5
-rw-r--r--Lib/test/libregrtest/cmdline.py347
-rw-r--r--Lib/test/libregrtest/main.py532
-rw-r--r--Lib/test/libregrtest/refleak.py269
-rw-r--r--Lib/test/libregrtest/runtest.py245
-rw-r--r--Lib/test/libregrtest/runtest_mp.py245
-rw-r--r--Lib/test/libregrtest/save_env.py285
-rw-r--r--Lib/test/libregrtest/setup.py121
-rw-r--r--Lib/test/list_tests.py14
-rw-r--r--Lib/test/lock_tests.py12
-rw-r--r--Lib/test/make_ssl_certs.py1
-rw-r--r--Lib/test/pickletester.py23
-rw-r--r--Lib/test/pydoc_mod.py2
-rwxr-xr-xLib/test/re_tests.py12
-rwxr-xr-xLib/test/regrtest.py1659
-rw-r--r--Lib/test/seq_tests.py1
-rw-r--r--Lib/test/signalinterproctester.py84
-rw-r--r--Lib/test/sortperf.py2
-rw-r--r--Lib/test/support/__init__.py146
-rw-r--r--Lib/test/test___all__.py2
-rw-r--r--Lib/test/test__locale.py1
-rw-r--r--Lib/test/test__osx_support.py1
-rw-r--r--Lib/test/test_aifc.py1
-rw-r--r--Lib/test/test_argparse.py15
-rw-r--r--Lib/test/test_array.py23
-rw-r--r--Lib/test/test_ast.py177
-rw-r--r--Lib/test/test_asyncgen.py1044
-rw-r--r--Lib/test/test_asynchat.py32
-rw-r--r--Lib/test/test_asyncio/test_futures.py186
-rw-r--r--Lib/test/test_asyncio/test_tasks.py454
-rw-r--r--Lib/test/test_asyncore.py7
-rw-r--r--Lib/test/test_augassign.py8
-rw-r--r--Lib/test/test_baseexception.py (renamed from Lib/test/test_pep352.py)1
-rw-r--r--Lib/test/test_bigmem.py1
-rw-r--r--Lib/test/test_binascii.py26
-rw-r--r--Lib/test/test_binhex.py1
-rw-r--r--Lib/test/test_binop.py50
-rw-r--r--Lib/test/test_bool.py18
-rw-r--r--Lib/test/test_buffer.py1
-rw-r--r--Lib/test/test_bufio.py2
-rw-r--r--Lib/test/test_builtin.py34
-rw-r--r--Lib/test/test_bytes.py243
-rw-r--r--Lib/test/test_bz2.py12
-rw-r--r--Lib/test/test_calendar.py17
-rw-r--r--Lib/test/test_capi.py120
-rw-r--r--Lib/test/test_cgi.py8
-rw-r--r--Lib/test/test_charmapcodec.py2
-rw-r--r--Lib/test/test_cmath.py37
-rw-r--r--Lib/test/test_cmd_line.py14
-rw-r--r--Lib/test/test_cmd_line_script.py7
-rw-r--r--Lib/test/test_code_module.py30
-rw-r--r--Lib/test/test_codeccallbacks.py5
-rw-r--r--Lib/test/test_codecencodings_cn.py1
-rw-r--r--Lib/test/test_codecencodings_hk.py1
-rw-r--r--Lib/test/test_codecencodings_iso2022.py1
-rw-r--r--Lib/test/test_codecencodings_jp.py1
-rw-r--r--Lib/test/test_codecencodings_kr.py1
-rw-r--r--Lib/test/test_codecencodings_tw.py1
-rw-r--r--Lib/test/test_codecmaps_cn.py1
-rw-r--r--Lib/test/test_codecmaps_hk.py1
-rw-r--r--Lib/test/test_codecmaps_jp.py1
-rw-r--r--Lib/test/test_codecmaps_kr.py1
-rw-r--r--Lib/test/test_codecmaps_tw.py1
-rw-r--r--Lib/test/test_codecs.py255
-rw-r--r--Lib/test/test_codeop.py1
-rw-r--r--Lib/test/test_collections.py299
-rw-r--r--Lib/test/test_compile.py17
-rw-r--r--Lib/test/test_compileall.py57
-rw-r--r--Lib/test/test_complex.py14
-rw-r--r--Lib/test/test_concurrent_futures.py24
-rw-r--r--Lib/test/test_configparser.py6
-rw-r--r--Lib/test/test_contains.py25
-rw-r--r--Lib/test/test_contextlib.py35
-rw-r--r--Lib/test/test_copy.py2
-rw-r--r--Lib/test/test_coroutines.py489
-rw-r--r--Lib/test/test_cprofile.py2
-rw-r--r--Lib/test/test_csv.py101
-rw-r--r--Lib/test/test_datetime.py10
-rw-r--r--Lib/test/test_dbm.py1
-rw-r--r--Lib/test/test_dbm_dumb.py27
-rw-r--r--Lib/test/test_dbm_gnu.py2
-rw-r--r--Lib/test/test_dbm_ndbm.py2
-rw-r--r--Lib/test/test_decimal.py43
-rw-r--r--Lib/test/test_deque.py79
-rw-r--r--Lib/test/test_descr.py15
-rw-r--r--Lib/test/test_descrtut.py1
-rw-r--r--Lib/test/test_devpoll.py3
-rw-r--r--Lib/test/test_dict.py157
-rw-r--r--Lib/test/test_dict_version.py186
-rw-r--r--Lib/test/test_dictcomps.py2
-rw-r--r--Lib/test/test_dictviews.py22
-rw-r--r--Lib/test/test_difflib.py12
-rw-r--r--Lib/test/test_difflib_expect.html12
-rw-r--r--Lib/test/test_dis.py589
-rw-r--r--Lib/test/test_doctest.py9
-rw-r--r--Lib/test/test_dtrace.py178
-rw-r--r--Lib/test/test_dynamic.py1
-rw-r--r--Lib/test/test_eintr.py12
-rw-r--r--Lib/test/test_email/__init__.py1
-rw-r--r--Lib/test/test_email/test__header_value_parser.py4
-rw-r--r--Lib/test/test_email/test_asian_codecs.py1
-rw-r--r--Lib/test/test_email/test_email.py44
-rw-r--r--Lib/test/test_email/test_generator.py2
-rw-r--r--Lib/test/test_email/test_headerregistry.py1
-rw-r--r--Lib/test/test_email/test_message.py20
-rw-r--r--Lib/test/test_email/test_parser.py91
-rw-r--r--Lib/test/test_email/test_policy.py42
-rw-r--r--Lib/test/test_ensurepip.py65
-rw-r--r--Lib/test/test_enum.py777
-rw-r--r--Lib/test/test_enumerate.py9
-rw-r--r--Lib/test/test_epoll.py1
-rw-r--r--Lib/test/test_exception_hierarchy.py (renamed from Lib/test/test_pep3151.py)1
-rw-r--r--Lib/test/test_exceptions.py1
-rw-r--r--Lib/test/test_extcall.py5
-rw-r--r--Lib/test/test_faulthandler.py112
-rw-r--r--Lib/test/test_file.py4
-rw-r--r--Lib/test/test_fileinput.py36
-rw-r--r--Lib/test/test_float.py46
-rw-r--r--Lib/test/test_fnmatch.py16
-rw-r--r--Lib/test/test_format.py4
-rw-r--r--Lib/test/test_fractions.py15
-rw-r--r--Lib/test/test_fstring.py760
-rw-r--r--Lib/test/test_ftplib.py21
-rw-r--r--Lib/test/test_functools.py204
-rw-r--r--Lib/test/test_future.py2
-rw-r--r--Lib/test/test_gc.py2
-rw-r--r--Lib/test/test_gdb.py59
-rw-r--r--Lib/test/test_generator_stop.py (renamed from Lib/test/test_pep479.py)0
-rw-r--r--Lib/test/test_generators.py6
-rw-r--r--Lib/test/test_genericpath.py342
-rw-r--r--Lib/test/test_getargs2.py64
-rw-r--r--Lib/test/test_gettext.py7
-rw-r--r--Lib/test/test_global.py6
-rw-r--r--Lib/test/test_grammar.py316
-rw-r--r--Lib/test/test_grp.py10
-rw-r--r--Lib/test/test_gzip.py26
-rw-r--r--Lib/test/test_hashlib.py428
-rw-r--r--Lib/test/test_heapq.py1
-rw-r--r--Lib/test/test_hmac.py1
-rw-r--r--Lib/test/test_htmlparser.py3
-rw-r--r--Lib/test/test_http_cookiejar.py6
-rw-r--r--Lib/test/test_httplib.py198
-rw-r--r--Lib/test/test_httpservers.py50
-rw-r--r--Lib/test/test_idle.py23
-rw-r--r--Lib/test/test_imaplib.py61
-rw-r--r--Lib/test/test_imghdr.py7
-rw-r--r--Lib/test/test_imp.py3
-rw-r--r--Lib/test/test_import/__init__.py12
-rw-r--r--Lib/test/test_importlib/extension/test_case_sensitivity.py3
-rw-r--r--Lib/test/test_importlib/extension/test_finder.py1
-rw-r--r--Lib/test/test_importlib/extension/test_path_hook.py2
-rw-r--r--Lib/test/test_importlib/frozen/test_loader.py2
-rw-r--r--Lib/test/test_importlib/import_/test___package__.py55
-rw-r--r--Lib/test/test_importlib/import_/test_api.py4
-rw-r--r--Lib/test/test_importlib/import_/test_fromlist.py10
-rw-r--r--Lib/test/test_importlib/import_/test_packages.py1
-rw-r--r--Lib/test/test_importlib/import_/test_path.py68
-rw-r--r--Lib/test/test_importlib/import_/test_relative_imports.py24
-rw-r--r--Lib/test/test_importlib/regrtest.py17
-rw-r--r--Lib/test/test_importlib/source/test_case_sensitivity.py1
-rw-r--r--Lib/test/test_importlib/source/test_file_loader.py9
-rw-r--r--Lib/test/test_importlib/source/test_path_hook.py11
-rw-r--r--Lib/test/test_importlib/source/test_source_encoding.py1
-rw-r--r--Lib/test/test_importlib/test_abc.py22
-rw-r--r--Lib/test/test_importlib/test_api.py31
-rw-r--r--Lib/test/test_importlib/test_lazy.py2
-rw-r--r--Lib/test/test_importlib/test_locks.py3
-rw-r--r--Lib/test/test_importlib/test_namespace_pkgs.py38
-rw-r--r--Lib/test/test_importlib/test_spec.py6
-rw-r--r--Lib/test/test_importlib/test_util.py27
-rw-r--r--Lib/test/test_importlib/test_windows.py2
-rw-r--r--Lib/test/test_inspect.py49
-rw-r--r--Lib/test/test_int.py26
-rw-r--r--Lib/test/test_io.py42
-rw-r--r--Lib/test/test_ipaddress.py11
-rw-r--r--Lib/test/test_iter.py12
-rw-r--r--Lib/test/test_iterlen.py1
-rw-r--r--Lib/test/test_itertools.py65
-rw-r--r--Lib/test/test_json/__init__.py1
-rw-r--r--Lib/test/test_json/test_decode.py4
-rw-r--r--Lib/test/test_json/test_fail.py1
-rw-r--r--Lib/test/test_json/test_unicode.py29
-rw-r--r--Lib/test/test_kqueue.py1
-rw-r--r--Lib/test/test_linecache.py75
-rw-r--r--Lib/test/test_list.py2
-rw-r--r--Lib/test/test_locale.py4
-rw-r--r--Lib/test/test_logging.py158
-rw-r--r--Lib/test/test_long.py76
-rw-r--r--Lib/test/test_lzma.py26
-rw-r--r--Lib/test/test_macpath.py2
-rw-r--r--Lib/test/test_mailbox.py17
-rw-r--r--Lib/test/test_mailcap.py3
-rw-r--r--Lib/test/test_marshal.py7
-rw-r--r--Lib/test/test_math.py310
-rw-r--r--Lib/test/test_mimetypes.py6
-rw-r--r--Lib/test/test_mmap.py7
-rw-r--r--Lib/test/test_msilib.py1
-rw-r--r--Lib/test/test_multibytecodec.py2
-rw-r--r--Lib/test/test_multiprocessing_fork.py6
-rw-r--r--Lib/test/test_multiprocessing_forkserver.py5
-rw-r--r--Lib/test/test_multiprocessing_main_handling.py6
-rw-r--r--Lib/test/test_multiprocessing_spawn.py5
-rw-r--r--Lib/test/test_nis.py1
-rw-r--r--Lib/test/test_nntplib.py6
-rw-r--r--Lib/test/test_normalization.py1
-rw-r--r--Lib/test/test_ntpath.py83
-rw-r--r--Lib/test/test_opcodes.py27
-rw-r--r--Lib/test/test_operator.py50
-rw-r--r--Lib/test/test_optparse.py7
-rw-r--r--Lib/test/test_ordered_dict.py67
-rw-r--r--Lib/test/test_os.py747
-rw-r--r--Lib/test/test_parser.py50
-rw-r--r--Lib/test/test_pathlib.py78
-rw-r--r--Lib/test/test_pdb.py70
-rw-r--r--Lib/test/test_peepholer.py20
-rw-r--r--Lib/test/test_pep247.py66
-rw-r--r--Lib/test/test_pickle.py16
-rw-r--r--Lib/test/test_pickletools.py34
-rw-r--r--Lib/test/test_pipes.py7
-rw-r--r--Lib/test/test_pkgutil.py6
-rw-r--r--Lib/test/test_platform.py5
-rw-r--r--Lib/test/test_plistlib.py9
-rw-r--r--Lib/test/test_poplib.py11
-rw-r--r--Lib/test/test_posix.py40
-rw-r--r--Lib/test/test_posixpath.py80
-rw-r--r--Lib/test/test_pow.py2
-rw-r--r--Lib/test/test_pty.py1
-rw-r--r--Lib/test/test_pulldom.py1
-rw-r--r--Lib/test/test_pwd.py26
-rw-r--r--Lib/test/test_pyclbr.py4
-rw-r--r--Lib/test/test_pydoc.py25
-rw-r--r--Lib/test/test_quopri.py2
-rw-r--r--Lib/test/test_random.py112
-rw-r--r--Lib/test/test_range.py52
-rw-r--r--Lib/test/test_re.py465
-rw-r--r--Lib/test/test_readline.py15
-rw-r--r--Lib/test/test_regrtest.py630
-rw-r--r--Lib/test/test_richcmp.py25
-rw-r--r--Lib/test/test_rlcompleter.py55
-rw-r--r--Lib/test/test_robotparser.py396
-rw-r--r--Lib/test/test_runpy.py11
-rw-r--r--Lib/test/test_sched.py21
-rw-r--r--Lib/test/test_secrets.py124
-rw-r--r--Lib/test/test_set.py22
-rw-r--r--Lib/test/test_shlex.py118
-rw-r--r--Lib/test/test_shutil.py17
-rw-r--r--Lib/test/test_signal.py171
-rw-r--r--Lib/test/test_site.py112
-rw-r--r--Lib/test/test_smtpd.py52
-rw-r--r--Lib/test/test_socket.py253
-rw-r--r--Lib/test/test_socketserver.py183
-rw-r--r--Lib/test/test_sort.py1
-rw-r--r--Lib/test/test_spwd.py15
-rw-r--r--Lib/test/test_ssl.py997
-rw-r--r--Lib/test/test_stat.py3
-rw-r--r--Lib/test/test_statistics.py200
-rw-r--r--Lib/test/test_strftime.py6
-rw-r--r--Lib/test/test_string_literals.py (renamed from Lib/test/test_strlit.py)51
-rw-r--r--Lib/test/test_strptime.py89
-rw-r--r--Lib/test/test_struct.py128
-rw-r--r--Lib/test/test_subclassinit.py284
-rw-r--r--Lib/test/test_subprocess.py247
-rw-r--r--Lib/test/test_sunau.py1
-rw-r--r--Lib/test/test_super.py164
-rw-r--r--Lib/test/test_support.py27
-rw-r--r--Lib/test/test_symbol.py54
-rw-r--r--Lib/test/test_symtable.py17
-rw-r--r--Lib/test/test_syntax.py32
-rw-r--r--Lib/test/test_sys.py47
-rw-r--r--Lib/test/test_sys_settrace.py4
-rw-r--r--Lib/test/test_sysconfig.py8
-rw-r--r--Lib/test/test_tarfile.py18
-rw-r--r--Lib/test/test_telnetlib.py6
-rw-r--r--Lib/test/test_threading.py11
-rw-r--r--Lib/test/test_time.py598
-rw-r--r--Lib/test/test_timeit.py22
-rw-r--r--Lib/test/test_tokenize.py76
-rw-r--r--Lib/test/test_tools/__init__.py1
-rw-r--r--Lib/test/test_tools/test_gprof2html.py3
-rw-r--r--Lib/test/test_tools/test_md5sum.py3
-rw-r--r--Lib/test/test_tools/test_pdeps.py4
-rw-r--r--Lib/test/test_tools/test_unparse.py19
-rw-r--r--Lib/test/test_trace.py66
-rw-r--r--Lib/test/test_traceback.py147
-rw-r--r--Lib/test/test_tracemalloc.py219
-rw-r--r--Lib/test/test_ttk_guionly.py1
-rw-r--r--Lib/test/test_ttk_textonly.py1
-rw-r--r--Lib/test/test_types.py19
-rw-r--r--Lib/test/test_unicode.py29
-rw-r--r--Lib/test/test_unicode_file_functions.py (renamed from Lib/test/test_pep277.py)0
-rw-r--r--Lib/test/test_unicode_identifiers.py (renamed from Lib/test/test_pep3131.py)0
-rw-r--r--Lib/test/test_unicodedata.py8
-rw-r--r--Lib/test/test_unpack.py21
-rw-r--r--Lib/test/test_unpack_ex.py1
-rw-r--r--Lib/test/test_urllib.py13
-rw-r--r--Lib/test/test_urllib2.py121
-rw-r--r--Lib/test/test_urllib2_localnet.py34
-rw-r--r--Lib/test/test_urllibnet.py1
-rw-r--r--Lib/test/test_urlparse.py36
-rw-r--r--Lib/test/test_userdict.py6
-rw-r--r--Lib/test/test_userlist.py2
-rw-r--r--Lib/test/test_userstring.py3
-rw-r--r--Lib/test/test_utf8source.py (renamed from Lib/test/test_pep3120.py)0
-rw-r--r--Lib/test/test_uu.py1
-rw-r--r--Lib/test/test_venv.py41
-rw-r--r--Lib/test/test_warnings/__init__.py90
-rw-r--r--Lib/test/test_wave.py8
-rw-r--r--Lib/test/test_weakref.py9
-rw-r--r--Lib/test/test_weakset.py9
-rw-r--r--Lib/test/test_winconsoleio.py147
-rw-r--r--Lib/test/test_winreg.py15
-rw-r--r--Lib/test/test_winsound.py26
-rw-r--r--Lib/test/test_with.py24
-rw-r--r--Lib/test/test_wsgiref.py2
-rw-r--r--Lib/test/test_xml_etree.py16
-rw-r--r--Lib/test/test_xml_etree_c.py4
-rw-r--r--Lib/test/test_xmlrpc.py53
-rw-r--r--Lib/test/test_xmlrpc_net.py3
-rw-r--r--Lib/test/test_yield_from.py (renamed from Lib/test/test_pep380.py)93
-rw-r--r--Lib/test/test_zipfile.py288
-rw-r--r--Lib/test/test_zipimport.py27
-rw-r--r--Lib/test/test_zipimport_support.py1
-rw-r--r--Lib/test/test_zlib.py35
-rw-r--r--Lib/threading.py8
-rw-r--r--[-rwxr-xr-x]Lib/timeit.py73
-rw-r--r--Lib/tkinter/__init__.py218
-rw-r--r--Lib/tkinter/commondialog.py5
-rw-r--r--Lib/tkinter/dialog.py5
-rw-r--r--Lib/tkinter/test/runtktests.py2
-rw-r--r--Lib/tkinter/test/test_tkinter/test_misc.py8
-rw-r--r--Lib/tkinter/test/test_tkinter/test_variables.py52
-rw-r--r--Lib/tkinter/test/test_tkinter/test_widgets.py7
-rw-r--r--Lib/tkinter/test/test_ttk/test_functions.py1
-rw-r--r--Lib/tkinter/test/test_ttk/test_widgets.py49
-rw-r--r--Lib/tkinter/tix.py36
-rw-r--r--Lib/tkinter/ttk.py66
-rw-r--r--Lib/tokenize.py135
-rwxr-xr-xLib/trace.py382
-rw-r--r--Lib/traceback.py28
-rw-r--r--Lib/tracemalloc.py73
-rw-r--r--Lib/turtledemo/__main__.py7
-rwxr-xr-xLib/turtledemo/bytedesign.py1
-rwxr-xr-xLib/turtledemo/planet_and_moon.py1
-rw-r--r--Lib/types.py5
-rw-r--r--Lib/unittest/mock.py51
-rw-r--r--Lib/unittest/runner.py2
-rw-r--r--Lib/unittest/test/test_assertions.py18
-rw-r--r--Lib/unittest/test/test_loader.py8
-rw-r--r--Lib/unittest/test/testmock/support.py2
-rw-r--r--Lib/unittest/test/testmock/testhelpers.py14
-rw-r--r--Lib/unittest/test/testmock/testmagicmethods.py1
-rw-r--r--Lib/unittest/test/testmock/testmock.py39
-rw-r--r--Lib/unittest/test/testmock/testpatch.py10
-rw-r--r--Lib/urllib/parse.py70
-rw-r--r--Lib/urllib/request.py90
-rw-r--r--Lib/urllib/robotparser.py43
-rw-r--r--Lib/uuid.py1
-rw-r--r--Lib/venv/__init__.py58
-rw-r--r--Lib/warnings.py127
-rw-r--r--Lib/wave.py2
-rw-r--r--Lib/wsgiref/simple_server.py30
-rw-r--r--Lib/xml/dom/xmlbuilder.py6
-rw-r--r--Lib/xml/etree/ElementPath.py22
-rw-r--r--Lib/xml/etree/ElementTree.py98
-rw-r--r--Lib/xmlrpc/client.py34
-rw-r--r--Lib/xmlrpc/server.py25
-rw-r--r--Lib/zipfile.py444
-rwxr-xr-xMac/BuildScript/build-installer.py121
-rw-r--r--Mac/BuildScript/resources/ReadMe.rtf108
-rw-r--r--Mac/BuildScript/resources/Welcome.rtf13
-rwxr-xr-xMac/BuildScript/resources/install_certificates.command48
-rw-r--r--Mac/IDLE/IDLE.app/Contents/Resources/idlemain.py2
-rw-r--r--Makefile.pre.in143
-rw-r--r--Misc/ACKS33
-rw-r--r--Misc/HISTORY6467
-rw-r--r--Misc/NEWS7593
-rw-r--r--Misc/README.valgrind3
-rw-r--r--Misc/SpecialBuilds.txt34
-rw-r--r--Misc/coverity_model.c5
-rw-r--r--Misc/python.man3
-rw-r--r--Modules/Setup.config.in3
-rw-r--r--Modules/Setup.dist4
-rw-r--r--Modules/_asynciomodule.c2469
-rw-r--r--Modules/_bisectmodule.c6
-rwxr-xr-xModules/_blake2/blake2b2s.py49
-rw-r--r--Modules/_blake2/blake2b_impl.c460
-rw-r--r--Modules/_blake2/blake2module.c105
-rw-r--r--Modules/_blake2/blake2ns.h32
-rw-r--r--Modules/_blake2/blake2s_impl.c460
-rw-r--r--Modules/_blake2/clinic/blake2b_impl.c.h125
-rw-r--r--Modules/_blake2/clinic/blake2s_impl.c.h125
-rw-r--r--Modules/_blake2/impl/blake2-config.h74
-rw-r--r--Modules/_blake2/impl/blake2-impl.h139
-rw-r--r--Modules/_blake2/impl/blake2.h161
-rw-r--r--Modules/_blake2/impl/blake2b-load-sse2.h70
-rw-r--r--Modules/_blake2/impl/blake2b-load-sse41.h404
-rw-r--r--Modules/_blake2/impl/blake2b-ref.c420
-rw-r--r--Modules/_blake2/impl/blake2b-round.h159
-rw-r--r--Modules/_blake2/impl/blake2b.c453
-rw-r--r--Modules/_blake2/impl/blake2s-load-sse2.h61
-rw-r--r--Modules/_blake2/impl/blake2s-load-sse41.h231
-rw-r--r--Modules/_blake2/impl/blake2s-load-xop.h191
-rw-r--r--Modules/_blake2/impl/blake2s-ref.c411
-rw-r--r--Modules/_blake2/impl/blake2s-round.h90
-rw-r--r--Modules/_blake2/impl/blake2s.c435
-rw-r--r--Modules/_bz2module.c3
-rw-r--r--Modules/_codecsmodule.c303
-rw-r--r--Modules/_collectionsmodule.c651
-rw-r--r--Modules/_csv.c24
-rw-r--r--Modules/_ctypes/_ctypes.c48
-rw-r--r--Modules/_ctypes/_ctypes_test.c43
-rw-r--r--Modules/_ctypes/callbacks.c2
-rw-r--r--Modules/_ctypes/callproc.c28
-rw-r--r--Modules/_ctypes/cfield.c76
-rw-r--r--Modules/_ctypes/ctypes.h10
-rw-r--r--Modules/_ctypes/libffi/src/dlmalloc.c2
-rw-r--r--Modules/_ctypes/libffi_arm_wince/debug.c59
-rw-r--r--Modules/_ctypes/libffi_arm_wince/ffi.c310
-rw-r--r--Modules/_ctypes/libffi_arm_wince/ffi.h317
-rw-r--r--Modules/_ctypes/libffi_arm_wince/ffi_common.h111
-rw-r--r--Modules/_ctypes/libffi_arm_wince/fficonfig.h152
-rw-r--r--Modules/_ctypes/libffi_arm_wince/ffitarget.h49
-rw-r--r--Modules/_ctypes/libffi_arm_wince/prep_cif.c175
-rw-r--r--Modules/_ctypes/libffi_arm_wince/sysv.asm228
-rw-r--r--Modules/_ctypes/stgdict.c2
-rw-r--r--Modules/_curses_panel.c4
-rw-r--r--Modules/_cursesmodule.c2
-rw-r--r--Modules/_datetimemodule.c1075
-rw-r--r--Modules/_dbmmodule.c8
-rw-r--r--Modules/_decimal/_decimal.c113
-rw-r--r--Modules/_decimal/docstrings.h9
-rw-r--r--Modules/_decimal/libmpdec/basearith.c1
-rw-r--r--Modules/_decimal/libmpdec/io.c1
-rw-r--r--Modules/_decimal/libmpdec/memory.c7
-rw-r--r--Modules/_decimal/libmpdec/mpalloc.h (renamed from Modules/_decimal/libmpdec/memory.h)4
-rw-r--r--Modules/_decimal/libmpdec/mpdecimal.c2
-rw-r--r--Modules/_decimal/libmpdec/mpdecimal.h12
-rw-r--r--Modules/_decimal/libmpdec/vccompat.h5
-rw-r--r--Modules/_decimal/tests/deccheck.py6
-rw-r--r--Modules/_elementtree.c585
-rw-r--r--Modules/_functoolsmodule.c111
-rw-r--r--Modules/_gdbmmodule.c2
-rw-r--r--Modules/_hashopenssl.c129
-rw-r--r--Modules/_heapqmodule.c10
-rw-r--r--Modules/_io/_iomodule.c83
-rw-r--r--Modules/_io/_iomodule.h29
-rw-r--r--Modules/_io/bufferedio.c2
-rw-r--r--Modules/_io/bytesio.c6
-rw-r--r--Modules/_io/clinic/_iomodule.c.h14
-rw-r--r--Modules/_io/clinic/bufferedio.c.h80
-rw-r--r--Modules/_io/clinic/bytesio.c.h31
-rw-r--r--Modules/_io/clinic/fileio.c.h31
-rw-r--r--Modules/_io/clinic/iobase.c.h11
-rw-r--r--Modules/_io/clinic/stringio.c.h22
-rw-r--r--Modules/_io/clinic/textio.c.h45
-rw-r--r--Modules/_io/clinic/winconsoleio.c.h331
-rw-r--r--Modules/_io/fileio.c49
-rw-r--r--Modules/_io/iobase.c2
-rw-r--r--Modules/_io/stringio.c4
-rw-r--r--Modules/_io/textio.c34
-rw-r--r--Modules/_io/winconsoleio.c1154
-rw-r--r--Modules/_json.c4
-rw-r--r--Modules/_localemodule.c56
-rw-r--r--Modules/_lsprof.c41
-rw-r--r--Modules/_lzmamodule.c30
-rw-r--r--Modules/_multiprocessing/multiprocessing.c2
-rw-r--r--Modules/_multiprocessing/multiprocessing.h2
-rw-r--r--Modules/_operator.c18
-rw-r--r--Modules/_pickle.c412
-rw-r--r--Modules/_posixsubprocess.c5
-rw-r--r--Modules/_randommodule.c101
-rw-r--r--Modules/_scproxy.c16
-rw-r--r--Modules/_sha3/README.txt11
-rwxr-xr-xModules/_sha3/cleanup.py50
-rw-r--r--Modules/_sha3/clinic/sha3module.c.h154
-rw-r--r--Modules/_sha3/kcp/KeccakHash.c82
-rw-r--r--Modules/_sha3/kcp/KeccakHash.h114
-rw-r--r--Modules/_sha3/kcp/KeccakP-1600-64.macros2208
-rw-r--r--Modules/_sha3/kcp/KeccakP-1600-SnP-opt32.h37
-rw-r--r--Modules/_sha3/kcp/KeccakP-1600-SnP-opt64.h49
-rw-r--r--Modules/_sha3/kcp/KeccakP-1600-SnP.h7
-rw-r--r--Modules/_sha3/kcp/KeccakP-1600-inplace32BI.c1162
-rw-r--r--Modules/_sha3/kcp/KeccakP-1600-opt64-config.h3
-rw-r--r--Modules/_sha3/kcp/KeccakP-1600-opt64.c474
-rw-r--r--Modules/_sha3/kcp/KeccakP-1600-unrolling.macros185
-rw-r--r--Modules/_sha3/kcp/KeccakSponge.c92
-rw-r--r--Modules/_sha3/kcp/KeccakSponge.h172
-rw-r--r--Modules/_sha3/kcp/KeccakSponge.inc332
-rw-r--r--Modules/_sha3/kcp/PlSnP-Fallback.inc257
-rw-r--r--Modules/_sha3/kcp/SnP-Relaned.h134
-rw-r--r--Modules/_sha3/kcp/align.h35
-rw-r--r--Modules/_sha3/sha3module.c757
-rw-r--r--Modules/_sqlite/cache.c3
-rw-r--r--Modules/_sqlite/connection.c95
-rw-r--r--Modules/_sqlite/connection.h4
-rw-r--r--Modules/_sqlite/cursor.c154
-rw-r--r--Modules/_sqlite/cursor.h6
-rw-r--r--Modules/_sqlite/module.c15
-rw-r--r--Modules/_sqlite/row.c4
-rw-r--r--Modules/_sqlite/statement.c24
-rw-r--r--Modules/_sqlite/statement.h1
-rw-r--r--Modules/_sqlite/util.c21
-rw-r--r--Modules/_sre.c129
-rw-r--r--Modules/_ssl.c601
-rw-r--r--Modules/_struct.c198
-rw-r--r--Modules/_testbuffer.c1
-rw-r--r--Modules/_testcapimodule.c314
-rw-r--r--Modules/_testmultiphase.c2
-rw-r--r--Modules/_tkinter.c77
-rw-r--r--Modules/_tracemalloc.c586
-rw-r--r--Modules/_winapi.c16
-rw-r--r--Modules/addrinfo.h12
-rw-r--r--Modules/arraymodule.c136
-rw-r--r--Modules/audioop.c76
-rw-r--r--Modules/binascii.c287
-rw-r--r--Modules/cjkcodecs/cjkcodecs.h2
-rw-r--r--Modules/cjkcodecs/clinic/multibytecodec.c.h65
-rw-r--r--Modules/cjkcodecs/multibytecodec.c4
-rw-r--r--Modules/clinic/_asynciomodule.c.h520
-rw-r--r--Modules/clinic/_bz2module.c.h35
-rw-r--r--Modules/clinic/_codecsmodule.c.h334
-rw-r--r--Modules/clinic/_cryptmodule.c.h5
-rw-r--r--Modules/clinic/_cursesmodule.c.h14
-rw-r--r--Modules/clinic/_datetimemodule.c.h14
-rw-r--r--Modules/clinic/_dbmmodule.c.h11
-rw-r--r--Modules/clinic/_elementtree.c.h120
-rw-r--r--Modules/clinic/_gdbmmodule.c.h14
-rw-r--r--Modules/clinic/_hashopenssl.c.h60
-rw-r--r--Modules/clinic/_lzmamodule.c.h43
-rw-r--r--Modules/clinic/_opcode.c.h8
-rw-r--r--Modules/clinic/_pickle.c.h75
-rw-r--r--Modules/clinic/_sre.c.h206
-rw-r--r--Modules/clinic/_ssl.c.h198
-rw-r--r--Modules/clinic/_tkinter.c.h53
-rw-r--r--Modules/clinic/_weakref.c.h8
-rw-r--r--Modules/clinic/_winapi.c.h143
-rw-r--r--Modules/clinic/arraymodule.c.h29
-rw-r--r--Modules/clinic/audioop.c.h167
-rw-r--r--Modules/clinic/binascii.c.h118
-rw-r--r--Modules/clinic/cmathmodule.c.h83
-rw-r--r--Modules/clinic/fcntlmodule.c.h14
-rw-r--r--Modules/clinic/grpmodule.c.h26
-rw-r--r--Modules/clinic/md5module.c.h14
-rw-r--r--Modules/clinic/posixmodule.c.h1216
-rw-r--r--Modules/clinic/pwdmodule.c.h5
-rw-r--r--Modules/clinic/pyexpat.c.h32
-rw-r--r--Modules/clinic/sha1module.c.h14
-rw-r--r--Modules/clinic/sha256module.c.h26
-rw-r--r--Modules/clinic/sha512module.c.h74
-rw-r--r--Modules/clinic/signalmodule.c.h32
-rw-r--r--Modules/clinic/spwdmodule.c.h5
-rw-r--r--Modules/clinic/unicodedata.c.h44
-rw-r--r--Modules/clinic/zlibmodule.c.h115
-rw-r--r--Modules/cmathmodule.c55
-rw-r--r--Modules/faulthandler.c316
-rw-r--r--Modules/gcmodule.c20
-rw-r--r--Modules/getaddrinfo.c4
-rw-r--r--Modules/getpath.c4
-rw-r--r--Modules/grpmodule.c21
-rw-r--r--Modules/hashlib.h19
-rw-r--r--Modules/hashtable.c196
-rw-r--r--Modules/hashtable.h181
-rw-r--r--Modules/itertoolsmodule.c387
-rw-r--r--Modules/main.c59
-rw-r--r--Modules/mathmodule.c5
-rw-r--r--Modules/md5module.c2
-rw-r--r--Modules/mmapmodule.c19
-rw-r--r--Modules/nismodule.c6
-rw-r--r--Modules/ossaudiodev.c6
-rw-r--r--Modules/overlapped.c10
-rw-r--r--Modules/parsermodule.c2559
-rw-r--r--Modules/posixmodule.c1883
-rw-r--r--Modules/pwdmodule.c8
-rw-r--r--Modules/pyexpat.c38
-rw-r--r--Modules/readline.c27
-rw-r--r--Modules/resource.c10
-rw-r--r--Modules/selectmodule.c42
-rw-r--r--Modules/sha1module.c2
-rw-r--r--Modules/sha512module.c172
-rw-r--r--Modules/signalmodule.c226
-rw-r--r--Modules/socketmodule.c725
-rw-r--r--Modules/spwdmodule.c5
-rw-r--r--Modules/sre_lib.h61
-rw-r--r--Modules/symtablemodule.c1
-rw-r--r--Modules/syslogmodule.c4
-rw-r--r--Modules/timemodule.c180
-rw-r--r--Modules/unicodedata.c7
-rw-r--r--Modules/unicodedata_db.h3239
-rw-r--r--Modules/unicodename_db.h45498
-rw-r--r--Modules/winreparse.h17
-rw-r--r--Modules/xxlimited.c2
-rw-r--r--Modules/xxmodule.c2
-rw-r--r--Modules/zipimport.c6
-rw-r--r--Modules/zlibmodule.c30
-rw-r--r--Objects/abstract.c653
-rw-r--r--Objects/bytearrayobject.c1048
-rw-r--r--Objects/bytes_methods.c431
-rw-r--r--Objects/bytesobject.c2340
-rw-r--r--Objects/classobject.c29
-rw-r--r--Objects/clinic/bytearrayobject.c.h134
-rw-r--r--Objects/clinic/bytesobject.c.h137
-rw-r--r--Objects/clinic/dictobject.c.h5
-rw-r--r--Objects/clinic/unicodeobject.c.h5
-rw-r--r--Objects/codeobject.c116
-rw-r--r--Objects/complexobject.c63
-rw-r--r--Objects/descrobject.c38
-rw-r--r--Objects/dict-common.h60
-rw-r--r--Objects/dictobject.c1868
-rw-r--r--Objects/enumobject.c12
-rw-r--r--Objects/exceptions.c40
-rw-r--r--Objects/fileobject.c11
-rw-r--r--Objects/floatobject.c352
-rw-r--r--Objects/frameobject.c14
-rw-r--r--Objects/funcobject.c35
-rw-r--r--Objects/genobject.c1065
-rw-r--r--Objects/iterobject.c40
-rw-r--r--Objects/listobject.c66
-rw-r--r--Objects/listsort.txt2
-rw-r--r--Objects/lnotab_notes.txt47
-rw-r--r--Objects/longobject.c542
-rw-r--r--Objects/memoryobject.c100
-rw-r--r--Objects/methodobject.c152
-rw-r--r--Objects/moduleobject.c13
-rw-r--r--Objects/namespaceobject.c8
-rw-r--r--Objects/object.c85
-rw-r--r--Objects/obmalloc.c432
-rw-r--r--Objects/odictobject.c73
-rw-r--r--Objects/rangeobject.c34
-rw-r--r--Objects/setobject.c625
-rw-r--r--Objects/stringlib/codecs.h220
-rw-r--r--Objects/stringlib/ctype.h5
-rw-r--r--Objects/stringlib/fastsearch.h150
-rw-r--r--Objects/stringlib/find.h82
-rw-r--r--Objects/stringlib/find_max_char.h5
-rw-r--r--Objects/stringlib/join.h8
-rw-r--r--Objects/stringlib/localeutil.h4
-rw-r--r--Objects/stringlib/transmogrify.h615
-rw-r--r--Objects/stringlib/unicode_format.h2
-rw-r--r--Objects/structseq.c32
-rw-r--r--Objects/tupleobject.c19
-rw-r--r--Objects/typeobject.c544
-rw-r--r--Objects/unicodeobject.c2486
-rw-r--r--Objects/unicodetype_db.h1573
-rw-r--r--Objects/weakrefobject.c4
-rw-r--r--PC/_msi.c12
-rw-r--r--PC/_testconsole.c131
-rw-r--r--PC/bdist_wininst/install.c18
-rw-r--r--PC/clinic/_testconsole.c.h82
-rw-r--r--PC/clinic/msvcrtmodule.c.h67
-rw-r--r--PC/clinic/winreg.c.h135
-rw-r--r--PC/clinic/winsound.c.h51
-rw-r--r--PC/config.c4
-rw-r--r--PC/getpathp.c276
-rw-r--r--PC/icons/launcher.icnsbin0 -> 264476 bytes-rw-r--r--PC/icons/launcher.icobin0 -> 87263 bytes-rw-r--r--PC/icons/launcher.svg1
-rw-r--r--PC/icons/py.icnsbin0 -> 195977 bytes-rw-r--r--PC/icons/py.icobin0 -> 75809 bytes-rw-r--r--PC/icons/py.svg1
-rw-r--r--PC/icons/pyc.icnsbin0 -> 212125 bytes-rw-r--r--PC/icons/pyc.icobin0 -> 78396 bytes-rw-r--r--PC/icons/pyc.svg1
-rw-r--r--PC/icons/pyd.icnsbin0 -> 223199 bytes-rw-r--r--PC/icons/pyd.icobin0 -> 83351 bytes-rw-r--r--PC/icons/pyd.svg1
-rw-r--r--PC/icons/python.icnsbin0 -> 201868 bytes-rw-r--r--PC/icons/python.icobin0 -> 77671 bytes-rw-r--r--PC/icons/python.svg1
-rw-r--r--PC/icons/pythonw.icnsbin0 -> 193222 bytes-rw-r--r--PC/icons/pythonw.icobin0 -> 76102 bytes-rw-r--r--PC/icons/pythonw.svg1
-rw-r--r--PC/icons/setup.icnsbin0 -> 220699 bytes-rw-r--r--PC/icons/setup.icobin0 -> 78328 bytes-rw-r--r--PC/icons/setup.svg1
-rw-r--r--PC/launcher.c33
-rw-r--r--PC/launcher.icobin19790 -> 0 bytes-rw-r--r--PC/msvcrtmodule.c34
-rw-r--r--PC/py.icobin19790 -> 0 bytes-rw-r--r--PC/pyc.icobin19790 -> 0 bytes-rw-r--r--PC/pycon.icobin19790 -> 0 bytes-rw-r--r--PC/pyconfig.h92
-rw-r--r--PC/pylauncher.rc10
-rw-r--r--PC/python.manifest7
-rw-r--r--PC/python3.def1562
-rw-r--r--PC/python_exe.rc2
-rw-r--r--PC/pythonw_exe.rc49
-rw-r--r--PC/testpy.py4
-rw-r--r--PC/winreg.c53
-rw-r--r--PC/winsound.c74
-rw-r--r--PCbuild/_asyncio.vcxproj77
-rw-r--r--PCbuild/_asyncio.vcxproj.filters16
-rw-r--r--PCbuild/_freeze_importlib.vcxproj37
-rw-r--r--PCbuild/_lzma.vcxproj12
-rw-r--r--PCbuild/_testconsole.vcxproj83
-rw-r--r--PCbuild/_testconsole.vcxproj.filters22
-rw-r--r--PCbuild/build.bat2
-rw-r--r--PCbuild/build_pgo.bat6
-rw-r--r--PCbuild/get_externals.bat8
-rw-r--r--PCbuild/liblzma.vcxproj216
-rw-r--r--PCbuild/openssl.props2
-rw-r--r--PCbuild/pcbuild.proj14
-rw-r--r--PCbuild/pcbuild.sln20
-rw-r--r--PCbuild/prepare_ssl.bat4
-rw-r--r--PCbuild/pyproject.props13
-rw-r--r--PCbuild/python.props5
-rw-r--r--PCbuild/pythoncore.vcxproj14
-rw-r--r--PCbuild/pythoncore.vcxproj.filters28
-rw-r--r--PCbuild/pythonw.vcxproj2
-rw-r--r--PCbuild/readme.txt6
-rw-r--r--PCbuild/rt.bat5
-rw-r--r--PCbuild/sqlite3.vcxproj4
-rw-r--r--PCbuild/tcltk.props6
-rw-r--r--PCbuild/xxlimited.vcxproj2
-rw-r--r--Parser/Python.asdl13
-rw-r--r--Parser/asdl.py3
-rw-r--r--[-rwxr-xr-x]Parser/asdl_c.py16
-rw-r--r--Parser/grammar.c27
-rw-r--r--Parser/myreadline.c139
-rw-r--r--Parser/node.c2
-rw-r--r--Parser/parser.c9
-rw-r--r--Parser/parsetok.c6
-rw-r--r--Parser/pgen.c24
-rw-r--r--Parser/pgenmain.c14
-rw-r--r--Parser/tokenizer.c248
-rw-r--r--Programs/_freeze_importlib.c2
-rw-r--r--Programs/_testembed.c2
-rw-r--r--Programs/python.c9
-rw-r--r--Python/Python-ast.c394
-rw-r--r--Python/_warnings.c221
-rw-r--r--Python/asdl.c8
-rw-r--r--Python/ast.c1460
-rw-r--r--Python/bltinmodule.c166
-rw-r--r--Python/ceval.c1947
-rw-r--r--Python/ceval_gil.h4
-rw-r--r--Python/clinic/bltinmodule.c.h53
-rw-r--r--Python/clinic/import.c.h29
-rw-r--r--Python/compile.c1322
-rw-r--r--Python/condvar.h6
-rw-r--r--Python/dtoa.c125
-rw-r--r--Python/dynload_win.c8
-rw-r--r--Python/errors.c144
-rw-r--r--Python/fileutils.c189
-rw-r--r--Python/formatter_unicode.c107
-rw-r--r--Python/frozen.c20
-rw-r--r--Python/frozenmain.c4
-rw-r--r--Python/future.c7
-rw-r--r--Python/getargs.c867
-rw-r--r--Python/graminit.c1924
-rw-r--r--Python/import.c423
-rw-r--r--Python/importdl.c4
-rw-r--r--Python/importlib.h3688
-rw-r--r--Python/importlib_external.h4724
-rwxr-xr-xPython/makeopcodetargets.py48
-rw-r--r--Python/marshal.c122
-rw-r--r--Python/modsupport.c25
-rw-r--r--Python/mystrtoul.c6
-rw-r--r--Python/opcode_targets.h18
-rw-r--r--Python/peephole.c661
-rw-r--r--Python/pyhash.c53
-rw-r--r--Python/pylifecycle.c176
-rw-r--r--Python/pystate.c50
-rw-r--r--Python/pystrtod.c75
-rw-r--r--Python/pythonrun.c44
-rw-r--r--Python/pytime.c348
-rw-r--r--Python/random.c70
-rw-r--r--Python/structmember.c22
-rw-r--r--Python/symtable.c155
-rw-r--r--Python/sysmodule.c297
-rw-r--r--Python/thread_nt.h24
-rw-r--r--Python/traceback.c225
-rw-r--r--Python/wordcode_helpers.h41
-rw-r--r--README55
-rw-r--r--Tools/buildbot/build-amd64.bat5
-rw-r--r--Tools/buildbot/clean-amd64.bat5
-rw-r--r--Tools/buildbot/clean.bat1
-rw-r--r--Tools/buildbot/external-amd64.bat3
-rw-r--r--Tools/buildbot/external.bat3
-rw-r--r--Tools/buildbot/test-amd64.bat6
-rw-r--r--Tools/buildbot/test.bat4
-rwxr-xr-xTools/clinic/clinic.py128
-rwxr-xr-xTools/demo/ss1.py2
-rwxr-xr-xTools/freeze/freeze.py6
-rw-r--r--Tools/freeze/winmakemakefile.py6
-rwxr-xr-xTools/gdb/libpython.py81
-rw-r--r--Tools/hg/hgtouch.py1
-rwxr-xr-xTools/i18n/pygettext.py51
-rw-r--r--Tools/msi/buildrelease.bat87
-rw-r--r--Tools/msi/bundle/Default.thm7
-rw-r--r--Tools/msi/bundle/Default.wxl3
-rw-r--r--Tools/msi/bundle/bootstrap/PythonBootstrapperApplication.cpp155
-rw-r--r--Tools/msi/bundle/bundle.icobin19790 -> 0 bytes-rw-r--r--Tools/msi/bundle/bundle.wxs2
-rw-r--r--Tools/msi/common.wxs12
-rw-r--r--Tools/msi/common_en-US.wxl_template1
-rw-r--r--Tools/msi/exe/exe.wixproj1
-rw-r--r--Tools/msi/exe/exe.wxs2
-rw-r--r--Tools/msi/exe/exe_d.wixproj1
-rw-r--r--Tools/msi/exe/exe_en-US.wxl_template1
-rw-r--r--Tools/msi/exe/exe_files.wxs10
-rw-r--r--Tools/msi/exe/exe_pdb.wixproj1
-rw-r--r--Tools/msi/exe/exe_reg.wxs25
-rw-r--r--Tools/msi/launcher/launcher.wixproj2
-rw-r--r--Tools/msi/launcher/launcher.wxs16
-rw-r--r--Tools/msi/launcher/launcher_en-US.wxl1
-rw-r--r--Tools/msi/launcher/launcher_reg.wxs8
-rw-r--r--Tools/msi/lib/lib.wixproj1
-rw-r--r--Tools/msi/lib/lib.wxs1
-rw-r--r--Tools/msi/lib/lib_files.wxs15
-rw-r--r--Tools/msi/make_zip.proj2
-rw-r--r--Tools/msi/make_zip.py54
-rw-r--r--Tools/msi/msi.props11
-rw-r--r--Tools/msi/msi.targets33
-rw-r--r--Tools/msi/test/test_files.wxs77
-rw-r--r--Tools/msi/tools/tools.wixproj1
-rw-r--r--Tools/msi/tools/tools.wxs1
-rw-r--r--Tools/msi/tools/tools_files.wxs7
-rw-r--r--Tools/nuget/make_pkg.proj2
-rw-r--r--Tools/parser/unparse.py86
-rw-r--r--Tools/pybench/CommandLine.py4
-rw-r--r--Tools/pybench/With.py1
-rw-r--r--Tools/pynche/ColorDB.py6
-rwxr-xr-xTools/scripts/combinerefs.py4
-rwxr-xr-xTools/scripts/diff.py2
-rwxr-xr-xTools/scripts/fixdiv.py4
-rw-r--r--Tools/scripts/generate_opcode_h.py2
-rwxr-xr-xTools/scripts/h2py.py8
-rwxr-xr-xTools/scripts/highlight.py16
-rwxr-xr-xTools/scripts/idle32
-rwxr-xr-xTools/scripts/mailerdaemon.py2
-rwxr-xr-xTools/scripts/nm2def.py4
-rwxr-xr-xTools/scripts/parseentities.py4
-rwxr-xr-xTools/scripts/pathfix.py2
-rwxr-xr-xTools/scripts/ptags.py2
-rwxr-xr-xTools/scripts/pyvenv6
-rwxr-xr-xTools/scripts/svneol.py2
-rwxr-xr-xTools/scripts/texi2html.py10
-rw-r--r--Tools/ssl/test_multiple_versions.py6
-rw-r--r--Tools/tz/zdump.py81
-rw-r--r--Tools/unicode/gencodec.py10
-rw-r--r--Tools/unicode/makeunicodedata.py11
-rw-r--r--aclocal.m44
-rwxr-xr-xconfig.guess174
-rwxr-xr-xconfig.sub75
-rwxr-xr-xconfigure1325
-rw-r--r--configure.ac473
-rw-r--r--pyconfig.h.in109
-rw-r--r--setup.py71
1506 files changed, 138573 insertions, 80949 deletions
diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md
new file mode 100644
index 0000000..4ce80d8
--- /dev/null
+++ b/.github/PULL_REQUEST_TEMPLATE.md
@@ -0,0 +1,9 @@
+## CPython Mirror
+
+https://github.com/python/cpython is a cpython mirror repository. Pull requests
+are not accepted on this repo and will be automatically closed.
+
+### Submit patches at https://bugs.python.org
+
+For additional information about contributing to CPython, see the
+[developer's guide](https://docs.python.org/devguide/#contributing).
diff --git a/.gitignore b/.gitignore
index d267d15..ed4ebfb 100644
--- a/.gitignore
+++ b/.gitignore
@@ -21,8 +21,6 @@ Doc/venv/
Lib/distutils/command/*.pdb
Lib/lib2to3/*.pickle
Lib/test/data/*
-Lib/_sysconfigdata.py
-Lib/plat-mac/errors.rsrc.df.rsrc
Makefile
Makefile.pre
Misc/python.pc
@@ -73,6 +71,7 @@ db_home
ipch/
libpython*.a
libpython*.so*
+libpython*.dylib
platform
pybuilddir.txt
pyconfig.h
diff --git a/.hgignore b/.hgignore
index cf9453a..92896b7 100644
--- a/.hgignore
+++ b/.hgignore
@@ -2,6 +2,7 @@
.purify
.svn/
^.idea/
+^.vscode/
.DS_Store
Makefile$
Makefile.pre$
@@ -25,7 +26,6 @@ python-config$
python-config.py$
reflog.txt$
tags$
-Lib/plat-mac/errors.rsrc.df.rsrc
Misc/python.pc
Misc/python-config.sh$
Modules/Setup$
@@ -43,6 +43,7 @@ Parser/pgen$
syntax: glob
libpython*.a
libpython*.so*
+libpython*.dylib
*.swp
*.o
*.pyc
@@ -54,6 +55,7 @@ libpython*.so*
*.profclang?
*.profraw
*.dyn
+Include/pydtrace_probes.h
Lib/distutils/command/*.pdb
Lib/lib2to3/*.pickle
Lib/test/data/*
diff --git a/.hgtags b/.hgtags
index b7ef041..8f51c2c 100644
--- a/.hgtags
+++ b/.hgtags
@@ -169,3 +169,14 @@ cc15d736d860303b9da90d43cd32db39bab048df v3.5.0rc2
4def2a2901a5618ea45bcc8f2a1411ef33af18ad v3.5.2
de530d7f21c0398bb2a2b67716e0638e5fadf727 v3.5.3rc1
1880cb95a742cd001c67677de5c4efeab169416c v3.5.3
+5896da372fb044e38595fb74495de1e1e7c8fb3c v3.6.0a1
+37889342355223e2fc1438de3dc7ffcd625c60f7 v3.6.0a2
+f3edf13dc339b8942ae6b309771ab197dd8ce6fa v3.6.0a3
+017cf260936b444788c9b671d195b7bfd83dbd25 v3.6.0a4
+5b0ca4ed5e2f0669d76ece7ef975c544580f12b4 v3.6.0b1
+b9fadc7d1c3f9c3c77f32f35afbe1a1cc38070e6 v3.6.0b2
+8345e066c0ed713c3e510cbc8fafc1c38d6d306b v3.6.0b3
+18496abdb3d5c2730a659b747a89261b2219fecf v3.6.0b4
+29a273eee9a523ee178f6a66c4ac9d317c8fc84f v3.6.0rc1
+800a67f7806de45a7abd5273359e704bf147c079 v3.6.0rc2
+41df79263a11f2429d1dd0cfe12553de3dcb5508 v3.6.0
diff --git a/Doc/Makefile b/Doc/Makefile
index 10c3288..91f937f 100644
--- a/Doc/Makefile
+++ b/Doc/Makefile
@@ -153,7 +153,7 @@ dist:
cp -pPR build/epub/Python.epub dist/python-$(DISTVERSION)-docs.epub
check:
- $(PYTHON) tools/rstlint.py -i tools
+ $(PYTHON) tools/rstlint.py -i tools -i venv
serve:
../Tools/scripts/serve.py build/html
diff --git a/Doc/c-api/arg.rst b/Doc/c-api/arg.rst
index 1123972..e8e7e0d 100644
--- a/Doc/c-api/arg.rst
+++ b/Doc/c-api/arg.rst
@@ -223,8 +223,7 @@ which disallows mutable objects such as :class:`bytearray`.
:c:func:`PyArg_ParseTuple` will use this location as the buffer and interpret the
initial value of *\*buffer_length* as the buffer size. It will then copy the
encoded data into the buffer and NUL-terminate it. If the buffer is not large
- enough, a :exc:`TypeError` will be set.
- Note: starting from Python 3.6 a :exc:`ValueError` will be set.
+ enough, a :exc:`ValueError` will be set.
In both cases, *\*buffer_length* is set to the length of the encoded data
without the trailing NUL byte.
@@ -266,15 +265,12 @@ Numbers
Convert a Python integer to a C :c:type:`unsigned long` without
overflow checking.
-``L`` (:class:`int`) [PY_LONG_LONG]
- Convert a Python integer to a C :c:type:`long long`. This format is only
- available on platforms that support :c:type:`long long` (or :c:type:`_int64` on
- Windows).
+``L`` (:class:`int`) [long long]
+ Convert a Python integer to a C :c:type:`long long`.
-``K`` (:class:`int`) [unsigned PY_LONG_LONG]
+``K`` (:class:`int`) [unsigned long long]
Convert a Python integer to a C :c:type:`unsigned long long`
- without overflow checking. This format is only available on platforms that
- support :c:type:`unsigned long long` (or :c:type:`unsigned _int64` on Windows).
+ without overflow checking.
``n`` (:class:`int`) [Py_ssize_t]
Convert a Python integer to a C :c:type:`Py_ssize_t`.
@@ -424,8 +420,15 @@ API Functions
.. c:function:: int PyArg_ParseTupleAndKeywords(PyObject *args, PyObject *kw, const char *format, char *keywords[], ...)
Parse the parameters of a function that takes both positional and keyword
- parameters into local variables. Returns true on success; on failure, it
- returns false and raises the appropriate exception.
+ parameters into local variables. The *keywords* argument is a
+ *NULL*-terminated array of keyword parameter names. Empty names denote
+ :ref:`positional-only parameters <positional-only_parameter>`.
+ Returns true on success; on failure, it returns false and raises the
+ appropriate exception.
+
+ .. versionchanged:: 3.6
+ Added support for :ref:`positional-only parameters
+ <positional-only_parameter>`.
.. c:function:: int PyArg_VaParseTupleAndKeywords(PyObject *args, PyObject *kw, const char *format, char *keywords[], va_list vargs)
@@ -588,15 +591,11 @@ Building values
``k`` (:class:`int`) [unsigned long]
Convert a C :c:type:`unsigned long` to a Python integer object.
- ``L`` (:class:`int`) [PY_LONG_LONG]
- Convert a C :c:type:`long long` to a Python integer object. Only available
- on platforms that support :c:type:`long long` (or :c:type:`_int64` on
- Windows).
+ ``L`` (:class:`int`) [long long]
+ Convert a C :c:type:`long long` to a Python integer object.
- ``K`` (:class:`int`) [unsigned PY_LONG_LONG]
- Convert a C :c:type:`unsigned long long` to a Python integer object. Only
- available on platforms that support :c:type:`unsigned long long` (or
- :c:type:`unsigned _int64` on Windows).
+ ``K`` (:class:`int`) [unsigned long long]
+ Convert a C :c:type:`unsigned long long` to a Python integer object.
``n`` (:class:`int`) [Py_ssize_t]
Convert a C :c:type:`Py_ssize_t` to a Python integer.
diff --git a/Doc/c-api/exceptions.rst b/Doc/c-api/exceptions.rst
index c389888..037b85c 100644
--- a/Doc/c-api/exceptions.rst
+++ b/Doc/c-api/exceptions.rst
@@ -306,6 +306,13 @@ an error value).
:mod:`warnings` module and the :option:`-W` option in the command line
documentation. There is no C API for warning control.
+.. c:function:: PyObject* PyErr_SetImportErrorSubclass(PyObject *msg, PyObject *name, PyObject *path)
+
+ Much like :c:func:`PyErr_SetImportError` but this function allows for
+ specifying a subclass of :exc:`ImportError` to raise.
+
+ .. versionadded:: 3.6
+
.. c:function:: int PyErr_WarnExplicitObject(PyObject *category, PyObject *message, PyObject *filename, int lineno, PyObject *module, PyObject *registry)
@@ -334,6 +341,14 @@ an error value).
.. versionadded:: 3.2
+.. c:function:: int PyErr_ResourceWarning(PyObject *source, Py_ssize_t stack_level, const char *format, ...)
+
+ Function similar to :c:func:`PyErr_WarnFormat`, but *category* is
+ :exc:`ResourceWarning` and pass *source* to :func:`warnings.WarningMessage`.
+
+ .. versionadded:: 3.6
+
+
Querying the error indicator
============================
@@ -774,6 +789,8 @@ the variables:
+-----------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_ImportError` | :exc:`ImportError` | |
+-----------------------------------------+---------------------------------+----------+
+| :c:data:`PyExc_ModuleNotFoundError` | :exc:`ModuleNotFoundError` | |
++-----------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_IndexError` | :exc:`IndexError` | |
+-----------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_InterruptedError` | :exc:`InterruptedError` | |
diff --git a/Doc/c-api/init.rst b/Doc/c-api/init.rst
index 70b98ae..2965bc9 100644
--- a/Doc/c-api/init.rst
+++ b/Doc/c-api/init.rst
@@ -25,7 +25,7 @@ Initializing and finalizing the interpreter
triple: module; search; path
single: PySys_SetArgv()
single: PySys_SetArgvEx()
- single: Py_Finalize()
+ single: Py_FinalizeEx()
Initialize the Python interpreter. In an application embedding Python, this
should be called before using any other Python/C API functions; with the
@@ -34,7 +34,7 @@ Initializing and finalizing the interpreter
modules :mod:`builtins`, :mod:`__main__` and :mod:`sys`. It also initializes
the module search path (``sys.path``). It does not set ``sys.argv``; use
:c:func:`PySys_SetArgvEx` for that. This is a no-op when called for a second time
- (without calling :c:func:`Py_Finalize` first). There is no return value; it is a
+ (without calling :c:func:`Py_FinalizeEx` first). There is no return value; it is a
fatal error if the initialization fails.
.. note::
@@ -52,19 +52,20 @@ Initializing and finalizing the interpreter
.. c:function:: int Py_IsInitialized()
Return true (nonzero) when the Python interpreter has been initialized, false
- (zero) if not. After :c:func:`Py_Finalize` is called, this returns false until
+ (zero) if not. After :c:func:`Py_FinalizeEx` is called, this returns false until
:c:func:`Py_Initialize` is called again.
-.. c:function:: void Py_Finalize()
+.. c:function:: int Py_FinalizeEx()
Undo all initializations made by :c:func:`Py_Initialize` and subsequent use of
Python/C API functions, and destroy all sub-interpreters (see
:c:func:`Py_NewInterpreter` below) that were created and not yet destroyed since
the last call to :c:func:`Py_Initialize`. Ideally, this frees all memory
allocated by the Python interpreter. This is a no-op when called for a second
- time (without calling :c:func:`Py_Initialize` again first). There is no return
- value; errors during finalization are ignored.
+ time (without calling :c:func:`Py_Initialize` again first). Normally the
+ return value is 0. If there were errors during finalization
+ (flushing buffered data), -1 is returned.
This function is provided for a number of reasons. An embedding application
might want to restart Python without having to restart the application itself.
@@ -83,7 +84,15 @@ Initializing and finalizing the interpreter
freed. Some memory allocated by extension modules may not be freed. Some
extensions may not work properly if their initialization routine is called more
than once; this can happen if an application calls :c:func:`Py_Initialize` and
- :c:func:`Py_Finalize` more than once.
+ :c:func:`Py_FinalizeEx` more than once.
+
+ .. versionadded:: 3.6
+
+
+.. c:function:: void Py_Finalize()
+
+ This is a backwards-compatible version of :c:func:`Py_FinalizeEx` that
+ disregards the return value.
Process-wide parameters
@@ -111,7 +120,7 @@ Process-wide parameters
Note that :data:`sys.stderr` always uses the "backslashreplace" error
handler, regardless of this (or any other) setting.
- If :c:func:`Py_Finalize` is called, this function will need to be called
+ If :c:func:`Py_FinalizeEx` is called, this function will need to be called
again in order to affect subsequent calls to :c:func:`Py_Initialize`.
Returns ``0`` if successful, a nonzero value on error (e.g. calling after the
@@ -922,7 +931,7 @@ using the following functions:
entry.)
.. index::
- single: Py_Finalize()
+ single: Py_FinalizeEx()
single: Py_Initialize()
Extension modules are shared between (sub-)interpreters as follows: the first
@@ -932,7 +941,7 @@ using the following functions:
and filled with the contents of this copy; the extension's ``init`` function is
not called. Note that this is different from what happens when an extension is
imported after the interpreter has been completely re-initialized by calling
- :c:func:`Py_Finalize` and :c:func:`Py_Initialize`; in that case, the extension's
+ :c:func:`Py_FinalizeEx` and :c:func:`Py_Initialize`; in that case, the extension's
``initmodule`` function *is* called again.
.. index:: single: close() (in module os)
@@ -940,14 +949,14 @@ using the following functions:
.. c:function:: void Py_EndInterpreter(PyThreadState *tstate)
- .. index:: single: Py_Finalize()
+ .. index:: single: Py_FinalizeEx()
Destroy the (sub-)interpreter represented by the given thread state. The given
thread state must be the current thread state. See the discussion of thread
states below. When the call returns, the current thread state is *NULL*. All
thread states associated with this interpreter are destroyed. (The global
interpreter lock must be held before calling this function and is still held
- when it returns.) :c:func:`Py_Finalize` will destroy all sub-interpreters that
+ when it returns.) :c:func:`Py_FinalizeEx` will destroy all sub-interpreters that
haven't been explicitly destroyed at that point.
diff --git a/Doc/c-api/intro.rst b/Doc/c-api/intro.rst
index bc3a752..74681d2 100644
--- a/Doc/c-api/intro.rst
+++ b/Doc/c-api/intro.rst
@@ -64,9 +64,10 @@ The header files are typically installed with Python. On Unix, these are
located in the directories :file:`{prefix}/include/pythonversion/` and
:file:`{exec_prefix}/include/pythonversion/`, where :envvar:`prefix` and
:envvar:`exec_prefix` are defined by the corresponding parameters to Python's
-:program:`configure` script and *version* is ``sys.version[:3]``. On Windows,
-the headers are installed in :file:`{prefix}/include`, where :envvar:`prefix` is
-the installation directory specified to the installer.
+:program:`configure` script and *version* is
+``'%d.%d' % sys.version_info[:2]``. On Windows, the headers are installed
+in :file:`{prefix}/include`, where :envvar:`prefix` is the installation
+directory specified to the installer.
To include the headers, place both directories (if different) on your compiler's
search path for includes. Do *not* place the parent directories on the search
@@ -578,9 +579,9 @@ Sometimes, it is desirable to "uninitialize" Python. For instance, the
application may want to start over (make another call to
:c:func:`Py_Initialize`) or the application is simply done with its use of
Python and wants to free memory allocated by Python. This can be accomplished
-by calling :c:func:`Py_Finalize`. The function :c:func:`Py_IsInitialized` returns
+by calling :c:func:`Py_FinalizeEx`. The function :c:func:`Py_IsInitialized` returns
true if Python is currently in the initialized state. More information about
-these functions is given in a later chapter. Notice that :c:func:`Py_Finalize`
+these functions is given in a later chapter. Notice that :c:func:`Py_FinalizeEx`
does *not* free all memory allocated by the Python interpreter, e.g. memory
allocated by extension modules currently cannot be released.
diff --git a/Doc/c-api/long.rst b/Doc/c-api/long.rst
index 68f6a8e..f592cb6 100644
--- a/Doc/c-api/long.rst
+++ b/Doc/c-api/long.rst
@@ -62,13 +62,13 @@ All integers are implemented as "long" integer objects of arbitrary size.
*NULL* on failure.
-.. c:function:: PyObject* PyLong_FromLongLong(PY_LONG_LONG v)
+.. c:function:: PyObject* PyLong_FromLongLong(long long v)
Return a new :c:type:`PyLongObject` object from a C :c:type:`long long`, or *NULL*
on failure.
-.. c:function:: PyObject* PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG v)
+.. c:function:: PyObject* PyLong_FromUnsignedLongLong(unsigned long long v)
Return a new :c:type:`PyLongObject` object from a C :c:type:`unsigned long long`,
or *NULL* on failure.
@@ -148,7 +148,7 @@ All integers are implemented as "long" integer objects of arbitrary size.
occurs set *\*overflow* to ``0`` and return ``-1`` as usual.
-.. c:function:: PY_LONG_LONG PyLong_AsLongLong(PyObject *obj)
+.. c:function:: long long PyLong_AsLongLong(PyObject *obj)
.. index::
single: OverflowError (built-in exception)
@@ -161,7 +161,7 @@ All integers are implemented as "long" integer objects of arbitrary size.
:c:type:`long`.
-.. c:function:: PY_LONG_LONG PyLong_AsLongLongAndOverflow(PyObject *obj, int *overflow)
+.. c:function:: long long PyLong_AsLongLongAndOverflow(PyObject *obj, int *overflow)
Return a C :c:type:`long long` representation of *obj*. If *obj* is not an
instance of :c:type:`PyLongObject`, first call its :meth:`__int__` method
@@ -210,16 +210,16 @@ All integers are implemented as "long" integer objects of arbitrary size.
:c:type:`size_t`.
-.. c:function:: unsigned PY_LONG_LONG PyLong_AsUnsignedLongLong(PyObject *pylong)
+.. c:function:: unsigned long long PyLong_AsUnsignedLongLong(PyObject *pylong)
.. index::
single: OverflowError (built-in exception)
- Return a C :c:type:`unsigned PY_LONG_LONG` representation of *pylong*.
- *pylong* must be an instance of :c:type:`PyLongObject`.
+ Return a C :c:type:`unsigned long long` representation of *pylong*. *pylong*
+ must be an instance of :c:type:`PyLongObject`.
Raise :exc:`OverflowError` if the value of *pylong* is out of range for an
- :c:type:`unsigned PY_LONG_LONG`.
+ :c:type:`unsigned long long`.
.. versionchanged:: 3.1
A negative *pylong* now raises :exc:`OverflowError`, not :exc:`TypeError`.
@@ -235,7 +235,7 @@ All integers are implemented as "long" integer objects of arbitrary size.
return the reduction of that value modulo ``ULONG_MAX + 1``.
-.. c:function:: unsigned PY_LONG_LONG PyLong_AsUnsignedLongLongMask(PyObject *obj)
+.. c:function:: unsigned long long PyLong_AsUnsignedLongLongMask(PyObject *obj)
Return a C :c:type:`unsigned long long` representation of *obj*. If *obj*
is not an instance of :c:type:`PyLongObject`, first call its :meth:`__int__`
diff --git a/Doc/c-api/memory.rst b/Doc/c-api/memory.rst
index 290ef09..3ff5452 100644
--- a/Doc/c-api/memory.rst
+++ b/Doc/c-api/memory.rst
@@ -85,9 +85,12 @@ for the I/O buffer escapes completely the Python memory manager.
.. seealso::
+ The :envvar:`PYTHONMALLOC` environment variable can be used to configure
+ the memory allocators used by Python.
+
The :envvar:`PYTHONMALLOCSTATS` environment variable can be used to print
- memory allocation statistics every time a new object arena is created, and
- on shutdown.
+ statistics of the :ref:`pymalloc memory allocator <pymalloc>` every time a
+ new pymalloc object arena is created, and on shutdown.
Raw Memory Interface
@@ -162,15 +165,17 @@ The following function sets, modeled after the ANSI C standard, but specifying
behavior when requesting zero bytes, are available for allocating and releasing
memory from the Python heap.
-The default memory block allocator uses the following functions:
-:c:func:`malloc`, :c:func:`calloc`, :c:func:`realloc` and :c:func:`free`; call
-``malloc(1)`` (or ``calloc(1, 1)``) when requesting zero bytes.
+By default, these functions use :ref:`pymalloc memory allocator <pymalloc>`.
.. warning::
The :term:`GIL <global interpreter lock>` must be held when using these
functions.
+.. versionchanged:: 3.6
+
+ The default allocator is now pymalloc instead of system :c:func:`malloc`.
+
.. c:function:: void* PyMem_Malloc(size_t n)
Allocates *n* bytes and returns a pointer of type :c:type:`void\*` to the
@@ -292,15 +297,32 @@ Customize Memory Allocators
Enum used to identify an allocator domain. Domains:
- * :c:data:`PYMEM_DOMAIN_RAW`: functions :c:func:`PyMem_RawMalloc`,
- :c:func:`PyMem_RawRealloc`, :c:func:`PyMem_RawCalloc` and
- :c:func:`PyMem_RawFree`
- * :c:data:`PYMEM_DOMAIN_MEM`: functions :c:func:`PyMem_Malloc`,
- :c:func:`PyMem_Realloc`, :c:func:`PyMem_Calloc` and :c:func:`PyMem_Free`
- * :c:data:`PYMEM_DOMAIN_OBJ`: functions :c:func:`PyObject_Malloc`,
- :c:func:`PyObject_Realloc`, :c:func:`PyObject_Calloc` and
- :c:func:`PyObject_Free`
+ .. c:var:: PYMEM_DOMAIN_RAW
+
+ Functions:
+
+ * :c:func:`PyMem_RawMalloc`
+ * :c:func:`PyMem_RawRealloc`
+ * :c:func:`PyMem_RawCalloc`
+ * :c:func:`PyMem_RawFree`
+
+ .. c:var:: PYMEM_DOMAIN_MEM
+
+ Functions:
+
+ * :c:func:`PyMem_Malloc`,
+ * :c:func:`PyMem_Realloc`
+ * :c:func:`PyMem_Calloc`
+ * :c:func:`PyMem_Free`
+ .. c:var:: PYMEM_DOMAIN_OBJ
+
+ Functions:
+
+ * :c:func:`PyObject_Malloc`
+ * :c:func:`PyObject_Realloc`
+ * :c:func:`PyObject_Calloc`
+ * :c:func:`PyObject_Free`
.. c:function:: void PyMem_GetAllocator(PyMemAllocatorDomain domain, PyMemAllocatorEx *allocator)
@@ -325,43 +347,62 @@ Customize Memory Allocators
.. c:function:: void PyMem_SetupDebugHooks(void)
- Setup hooks to detect bugs in the following Python memory allocator
- functions:
-
- - :c:func:`PyMem_RawMalloc`, :c:func:`PyMem_RawRealloc`,
- :c:func:`PyMem_RawCalloc`, :c:func:`PyMem_RawFree`
- - :c:func:`PyMem_Malloc`, :c:func:`PyMem_Realloc`, :c:func:`PyMem_Calloc`,
- :c:func:`PyMem_Free`
- - :c:func:`PyObject_Malloc`, :c:func:`PyObject_Realloc`,
- :c:func:`PyObject_Calloc`, :c:func:`PyObject_Free`
+ Setup hooks to detect bugs in the Python memory allocator functions.
Newly allocated memory is filled with the byte ``0xCB``, freed memory is
- filled with the byte ``0xDB``. Additional checks:
+ filled with the byte ``0xDB``.
- - detect API violations, ex: :c:func:`PyObject_Free` called on a buffer
+ Runtime checks:
+
+ - Detect API violations, ex: :c:func:`PyObject_Free` called on a buffer
allocated by :c:func:`PyMem_Malloc`
- - detect write before the start of the buffer (buffer underflow)
- - detect write after the end of the buffer (buffer overflow)
+ - Detect write before the start of the buffer (buffer underflow)
+ - Detect write after the end of the buffer (buffer overflow)
+ - Check that the :term:`GIL <global interpreter lock>` is held when
+ allocator functions of :c:data:`PYMEM_DOMAIN_OBJ` (ex:
+ :c:func:`PyObject_Malloc`) and :c:data:`PYMEM_DOMAIN_MEM` (ex:
+ :c:func:`PyMem_Malloc`) domains are called
+
+ On error, the debug hooks use the :mod:`tracemalloc` module to get the
+ traceback where a memory block was allocated. The traceback is only
+ displayed if :mod:`tracemalloc` is tracing Python memory allocations and the
+ memory block was traced.
- The function does nothing if Python is not compiled is debug mode.
+ These hooks are installed by default if Python is compiled in debug
+ mode. The :envvar:`PYTHONMALLOC` environment variable can be used to install
+ debug hooks on a Python compiled in release mode.
+ .. versionchanged:: 3.6
+ This function now also works on Python compiled in release mode.
+ On error, the debug hooks now use :mod:`tracemalloc` to get the traceback
+ where a memory block was allocated. The debug hooks now also check
+ if the GIL is held when functions of :c:data:`PYMEM_DOMAIN_OBJ` and
+ :c:data:`PYMEM_DOMAIN_MEM` domains are called.
-Customize PyObject Arena Allocator
-==================================
-Python has a *pymalloc* allocator for allocations smaller than 512 bytes. This
-allocator is optimized for small objects with a short lifetime. It uses memory
-mappings called "arenas" with a fixed size of 256 KB. It falls back to
-:c:func:`PyMem_RawMalloc` and :c:func:`PyMem_RawRealloc` for allocations larger
-than 512 bytes. *pymalloc* is the default allocator used by
-:c:func:`PyObject_Malloc`.
+.. _pymalloc:
-The default arena allocator uses the following functions:
+The pymalloc allocator
+======================
+
+Python has a *pymalloc* allocator optimized for small objects (smaller or equal
+to 512 bytes) with a short lifetime. It uses memory mappings called "arenas"
+with a fixed size of 256 KB. It falls back to :c:func:`PyMem_RawMalloc` and
+:c:func:`PyMem_RawRealloc` for allocations larger than 512 bytes.
+
+*pymalloc* is the default allocator of the :c:data:`PYMEM_DOMAIN_MEM` (ex:
+:c:func:`PyObject_Malloc`) and :c:data:`PYMEM_DOMAIN_OBJ` (ex:
+:c:func:`PyObject_Malloc`) domains.
+
+The arena allocator uses the following functions:
* :c:func:`VirtualAlloc` and :c:func:`VirtualFree` on Windows,
* :c:func:`mmap` and :c:func:`munmap` if available,
* :c:func:`malloc` and :c:func:`free` otherwise.
+Customize pymalloc Arena Allocator
+----------------------------------
+
.. versionadded:: 3.4
.. c:type:: PyObjectArenaAllocator
diff --git a/Doc/c-api/sys.rst b/Doc/c-api/sys.rst
index 3d83b27..035cdc1 100644
--- a/Doc/c-api/sys.rst
+++ b/Doc/c-api/sys.rst
@@ -5,6 +5,17 @@
Operating System Utilities
==========================
+.. c:function:: PyObject* PyOS_FSPath(PyObject *path)
+
+ Return the file system representation for *path*. If the object is a
+ :class:`str` or :class:`bytes` object, then its reference count is
+ incremented. If the object implements the :class:`os.PathLike` interface,
+ then :meth:`~os.PathLike.__fspath__` is returned as long as it is a
+ :class:`str` or :class:`bytes` object. Otherwise :exc:`TypeError` is raised
+ and ``NULL`` is returned.
+
+ .. versionadded:: 3.6
+
.. c:function:: int Py_FdIsInteractive(FILE *fp, const char *filename)
@@ -212,20 +223,24 @@ Process Control
.. c:function:: void Py_Exit(int status)
.. index::
- single: Py_Finalize()
+ single: Py_FinalizeEx()
single: exit()
- Exit the current process. This calls :c:func:`Py_Finalize` and then calls the
- standard C library function ``exit(status)``.
+ Exit the current process. This calls :c:func:`Py_FinalizeEx` and then calls the
+ standard C library function ``exit(status)``. If :c:func:`Py_FinalizeEx`
+ indicates an error, the exit status is set to 120.
+
+ .. versionchanged:: 3.6
+ Errors from finalization no longer ignored.
.. c:function:: int Py_AtExit(void (*func) ())
.. index::
- single: Py_Finalize()
+ single: Py_FinalizeEx()
single: cleanup functions
- Register a cleanup function to be called by :c:func:`Py_Finalize`. The cleanup
+ Register a cleanup function to be called by :c:func:`Py_FinalizeEx`. The cleanup
function will be called with no arguments and should return no value. At most
32 cleanup functions can be registered. When the registration is successful,
:c:func:`Py_AtExit` returns ``0``; on failure, it returns ``-1``. The cleanup
diff --git a/Doc/c-api/unicode.rst b/Doc/c-api/unicode.rst
index 256fef3..02f7ada 100644
--- a/Doc/c-api/unicode.rst
+++ b/Doc/c-api/unicode.rst
@@ -440,7 +440,6 @@ APIs:
.. % because not all compilers support the %z width modifier -- we fake it
.. % when necessary via interpolating PY_FORMAT_SIZE_T.
.. % Similar comments apply to the %ll width modifier and
- .. % PY_FORMAT_LONG_LONG.
.. tabularcolumns:: |l|l|L|
@@ -525,11 +524,6 @@ APIs:
copied as-is to the result string, and any extra arguments discarded.
.. note::
-
- The `"%lld"` and `"%llu"` format specifiers are only available
- when :const:`HAVE_LONG_LONG` is defined.
-
- .. note::
The width formatter unit is number of characters rather than bytes.
The precision formatter unit is number of bytes for ``"%s"`` and
``"%V"`` (if the ``PyObject*`` argument is NULL), and a number of
@@ -811,38 +805,47 @@ File System Encoding
""""""""""""""""""""
To encode and decode file names and other environment strings,
-:c:data:`Py_FileSystemEncoding` should be used as the encoding, and
-``"surrogateescape"`` should be used as the error handler (:pep:`383`). To
-encode file names during argument parsing, the ``"O&"`` converter should be
-used, passing :c:func:`PyUnicode_FSConverter` as the conversion function:
+:c:data:`Py_FileSystemDefaultEncoding` should be used as the encoding, and
+:c:data:`Py_FileSystemDefaultEncodeErrors` should be used as the error handler
+(:pep:`383` and :pep:`529`). To encode file names to :class:`bytes` during
+argument parsing, the ``"O&"`` converter should be used, passing
+:c:func:`PyUnicode_FSConverter` as the conversion function:
.. c:function:: int PyUnicode_FSConverter(PyObject* obj, void* result)
- ParseTuple converter: encode :class:`str` objects to :class:`bytes` using
+ ParseTuple converter: encode :class:`str` objects -- obtained directly or
+ through the :class:`os.PathLike` interface -- to :class:`bytes` using
:c:func:`PyUnicode_EncodeFSDefault`; :class:`bytes` objects are output as-is.
*result* must be a :c:type:`PyBytesObject*` which must be released when it is
no longer used.
.. versionadded:: 3.1
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
-To decode file names during argument parsing, the ``"O&"`` converter should be
-used, passing :c:func:`PyUnicode_FSDecoder` as the conversion function:
+To decode file names to :class:`str` during argument parsing, the ``"O&"``
+converter should be used, passing :c:func:`PyUnicode_FSDecoder` as the
+conversion function:
.. c:function:: int PyUnicode_FSDecoder(PyObject* obj, void* result)
- ParseTuple converter: decode :class:`bytes` objects to :class:`str` using
- :c:func:`PyUnicode_DecodeFSDefaultAndSize`; :class:`str` objects are output
- as-is. *result* must be a :c:type:`PyUnicodeObject*` which must be released
- when it is no longer used.
+ ParseTuple converter: decode :class:`bytes` objects -- obtained either
+ directly or indirectly through the :class:`os.PathLike` interface -- to
+ :class:`str` using :c:func:`PyUnicode_DecodeFSDefaultAndSize`; :class:`str`
+ objects are output as-is. *result* must be a :c:type:`PyUnicodeObject*` which
+ must be released when it is no longer used.
.. versionadded:: 3.2
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. c:function:: PyObject* PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size)
Decode a string using :c:data:`Py_FileSystemDefaultEncoding` and the
- ``"surrogateescape"`` error handler, or ``"strict"`` on Windows.
+ :c:data:`Py_FileSystemDefaultEncodeErrors` error handler.
If :c:data:`Py_FileSystemDefaultEncoding` is not set, fall back to the
locale encoding.
@@ -856,28 +859,28 @@ used, passing :c:func:`PyUnicode_FSDecoder` as the conversion function:
The :c:func:`Py_DecodeLocale` function.
- .. versionchanged:: 3.2
- Use ``"strict"`` error handler on Windows.
+ .. versionchanged:: 3.6
+ Use :c:data:`Py_FileSystemDefaultEncodeErrors` error handler.
.. c:function:: PyObject* PyUnicode_DecodeFSDefault(const char *s)
Decode a null-terminated string using :c:data:`Py_FileSystemDefaultEncoding`
- and the ``"surrogateescape"`` error handler, or ``"strict"`` on Windows.
+ and the :c:data:`Py_FileSystemDefaultEncodeErrors` error handler.
If :c:data:`Py_FileSystemDefaultEncoding` is not set, fall back to the
locale encoding.
Use :c:func:`PyUnicode_DecodeFSDefaultAndSize` if you know the string length.
- .. versionchanged:: 3.2
- Use ``"strict"`` error handler on Windows.
+ .. versionchanged:: 3.6
+ Use :c:data:`Py_FileSystemDefaultEncodeErrors` error handler.
.. c:function:: PyObject* PyUnicode_EncodeFSDefault(PyObject *unicode)
Encode a Unicode object to :c:data:`Py_FileSystemDefaultEncoding` with the
- ``"surrogateescape"`` error handler, or ``"strict"`` on Windows, and return
+ :c:data:`Py_FileSystemDefaultEncodeErrors` error handler, and return
:class:`bytes`. Note that the resulting :class:`bytes` object may contain
null bytes.
@@ -894,6 +897,8 @@ used, passing :c:func:`PyUnicode_FSDecoder` as the conversion function:
.. versionadded:: 3.2
+ .. versionchanged:: 3.6
+ Use :c:data:`Py_FileSystemDefaultEncodeErrors` error handler.
wchar_t Support
"""""""""""""""
diff --git a/Doc/data/refcounts.dat b/Doc/data/refcounts.dat
index e388195..8b469f4 100644
--- a/Doc/data/refcounts.dat
+++ b/Doc/data/refcounts.dat
@@ -921,6 +921,9 @@ PyNumber_Xor:PyObject*:o2:0:
PyObject_AsFileDescriptor:int:::
PyObject_AsFileDescriptor:PyObject*:o:0:
+PyOS_FSPath:PyObject*::+1:
+PyOS_FSPath:PyObject*:path:0:
+
PyObject_Call:PyObject*::+1:
PyObject_Call:PyObject*:callable_object:0:
PyObject_Call:PyObject*:args:0:
diff --git a/Doc/extending/embedding.rst b/Doc/extending/embedding.rst
index 64033dc..ab2f616 100644
--- a/Doc/extending/embedding.rst
+++ b/Doc/extending/embedding.rst
@@ -67,7 +67,9 @@ perform some operation on a file. ::
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
"print('Today is', ctime(time()))\n");
- Py_Finalize();
+ if (Py_FinalizeEx() < 0) {
+ exit(120);
+ }
PyMem_RawFree(program);
return 0;
}
@@ -76,7 +78,7 @@ The :c:func:`Py_SetProgramName` function should be called before
:c:func:`Py_Initialize` to inform the interpreter about paths to Python run-time
libraries. Next, the Python interpreter is initialized with
:c:func:`Py_Initialize`, followed by the execution of a hard-coded Python script
-that prints the date and time. Afterwards, the :c:func:`Py_Finalize` call shuts
+that prints the date and time. Afterwards, the :c:func:`Py_FinalizeEx` call shuts
the interpreter down, followed by the end of the program. In a real program,
you may want to get the Python script from another source, perhaps a text-editor
routine, a file, or a database. Getting the Python code from a file can better
diff --git a/Doc/faq/design.rst b/Doc/faq/design.rst
index 108df6d..1bd800b 100644
--- a/Doc/faq/design.rst
+++ b/Doc/faq/design.rst
@@ -366,33 +366,11 @@ is exactly the same type of object that a lambda expression yields) is assigned!
Can Python be compiled to machine code, C or some other language?
-----------------------------------------------------------------
-Practical answer:
-
-`Cython <http://cython.org/>`_ and `Pyrex <https://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/>`_
-compile a modified version of Python with optional annotations into C
-extensions. `Weave <https://scipy.github.io/devdocs/tutorial/weave.html>`_ makes it easy to
-intermingle Python and C code in various ways to increase performance.
-`Nuitka <http://www.nuitka.net/>`_ is an up-and-coming compiler of Python
-into C++ code, aiming to support the full Python language.
-
-Theoretical answer:
-
- .. XXX not sure what to make of this
-
-Not trivially. Python's high level data types, dynamic typing of objects and
-run-time invocation of the interpreter (using :func:`eval` or :func:`exec`)
-together mean that a naïvely "compiled" Python program would probably consist
-mostly of calls into the Python run-time system, even for seemingly simple
-operations like ``x+1``.
-
-Several projects described in the Python newsgroup or at past `Python
-conferences <https://www.python.org/community/workshops/>`_ have shown that this
-approach is feasible, although the speedups reached so far are only modest
-(e.g. 2x). Jython uses the same strategy for compiling to Java bytecode. (Jim
-Hugunin has demonstrated that in combination with whole-program analysis,
-speedups of 1000x are feasible for small demo programs. See the proceedings
-from the `1997 Python conference
-<http://legacy.python.org/workshops/1997-10/proceedings/>`_ for more information.)
+`Cython <http://cython.org/>`_ compiles a modified version of Python with
+optional annotations into C extensions. `Nuitka <http://www.nuitka.net/>`_ is
+an up-and-coming compiler of Python into C++ code, aiming to support the full
+Python language. For compiling to Java you can consider
+`VOC <https://voc.readthedocs.io>`_.
How does Python manage memory?
diff --git a/Doc/faq/programming.rst b/Doc/faq/programming.rst
index 694753e..9c5e20d 100644
--- a/Doc/faq/programming.rst
+++ b/Doc/faq/programming.rst
@@ -838,7 +838,8 @@ How do I convert a number to a string?
To convert, e.g., the number 144 to the string '144', use the built-in type
constructor :func:`str`. If you want a hexadecimal or octal representation, use
the built-in functions :func:`hex` or :func:`oct`. For fancy formatting, see
-the :ref:`formatstrings` section, e.g. ``"{:04d}".format(144)`` yields
+the :ref:`f-strings` and :ref:`formatstrings` sections,
+e.g. ``"{:04d}".format(144)`` yields
``'0144'`` and ``"{:.3f}".format(1.0/3.0)`` yields ``'0.333'``.
diff --git a/Doc/glossary.rst b/Doc/glossary.rst
index 45b794f..41ee3d8 100644
--- a/Doc/glossary.rst
+++ b/Doc/glossary.rst
@@ -74,6 +74,34 @@ Glossary
:keyword:`async with` statement by defining :meth:`__aenter__` and
:meth:`__aexit__` methods. Introduced by :pep:`492`.
+ asynchronous generator
+ A function which returns an :term:`asynchronous generator iterator`. It
+ looks like a coroutine function defined with :keyword:`async def` except
+ that it contains :keyword:`yield` expressions for producing a series of
+ values usable in an :keyword:`async for` loop.
+
+ Usually refers to a asynchronous generator function, but may refer to an
+ *asynchronous generator iterator* in some contexts. In cases where the
+ intended meaning isn't clear, using the full terms avoids ambiguity.
+
+ An asynchronous generator function may contain :keyword:`await`
+ expressions as well as :keyword:`async for`, and :keyword:`async with`
+ statements.
+
+ asynchronous generator iterator
+ An object created by a :term:`asynchronous generator` function.
+
+ This is an :term:`asynchronous iterator` which when called using the
+ :meth:`__anext__` method returns an awaitable object which will execute
+ that the body of the asynchronous generator function until the
+ next :keyword:`yield` expression.
+
+ Each :keyword:`yield` temporarily suspends processing, remembering the
+ location execution state (including local variables and pending
+ try-statements). When the *asynchronous generator iterator* effectively
+ resumes with another awaitable returned by :meth:`__anext__`, it
+ picks-up where it left-off. See :pep:`492` and :pep:`525`.
+
asynchronous iterable
An object, that can be used in an :keyword:`async for` statement.
Must return an :term:`asynchronous iterator` from its
@@ -718,6 +746,8 @@ Glossary
def func(foo, bar=None): ...
+ .. _positional-only_parameter:
+
* :dfn:`positional-only`: specifies an argument that can be supplied only
by position. Python has no syntax for defining positional-only
parameters. However, some built-in functions have positional-only
@@ -776,6 +806,16 @@ Glossary
One of the default :term:`meta path finders <meta path finder>` which
searches an :term:`import path` for modules.
+ path-like object
+ An object representing a file system path. A path-like object is either
+ a :class:`str` or :class:`bytes` object representing a path, or an object
+ implementing the :class:`os.PathLike` protocol. An object that supports
+ the :class:`os.PathLike` protocol can be converted to a :class:`str` or
+ :class:`bytes` file system path by calling the :func:`os.fspath` function;
+ :func:`os.fsdecode` and :func:`os.fsencode` can be used to guarantee a
+ :class:`str` or :class:`bytes` result instead, respectively. Introduced
+ by :pep:`519`.
+
portion
A set of files in a single directory (possibly stored in a zip file)
that contribute to a namespace package, as defined in :pep:`420`.
@@ -952,13 +992,25 @@ Glossary
``'\r'``. See :pep:`278` and :pep:`3116`, as well as
:func:`bytes.splitlines` for an additional use.
+ variable annotation
+ A type metadata value associated with a module global variable or
+ a class attribute. Its syntax is explained in section :ref:`annassign`.
+ Annotations are stored in the :attr:`__annotations__` special
+ attribute of a class or module object and can be accessed using
+ :func:`typing.get_type_hints`.
+
+ Python itself does not assign any particular meaning to variable
+ annotations. They are intended to be interpreted by third-party libraries
+ or type checking tools. See :pep:`526`, :pep:`484` which describe
+ some of their potential uses.
+
virtual environment
A cooperatively isolated runtime environment that allows Python users
and applications to install and upgrade Python distribution packages
without interfering with the behaviour of other Python applications
running on the same system.
- See also :ref:`scripts-pyvenv`.
+ See also :mod:`venv`.
virtual machine
A computer defined entirely in software. Python's virtual machine
diff --git a/Doc/howto/argparse.rst b/Doc/howto/argparse.rst
index 3b79b92..7e161a5 100644
--- a/Doc/howto/argparse.rst
+++ b/Doc/howto/argparse.rst
@@ -547,7 +547,8 @@ And this is what it gives:
Traceback (most recent call last):
File "prog.py", line 11, in <module>
if args.verbosity >= 2:
- TypeError: unorderable types: NoneType() >= int()
+ TypeError: '>=' not supported between instances of 'NoneType' and 'int'
+
* First output went well, and fixes the bug we had before.
That is, we want any value >= 2 to be as verbose as possible.
diff --git a/Doc/howto/clinic.rst b/Doc/howto/clinic.rst
index 491c5a2..eaab20a 100644
--- a/Doc/howto/clinic.rst
+++ b/Doc/howto/clinic.rst
@@ -834,9 +834,9 @@ on the right is the text you'd replace it with.
``'i'`` ``int``
``'I'`` ``unsigned_int(bitwise=True)``
``'k'`` ``unsigned_long(bitwise=True)``
-``'K'`` ``unsigned_PY_LONG_LONG(bitwise=True)``
+``'K'`` ``unsigned_long_long(bitwise=True)``
``'l'`` ``long``
-``'L'`` ``PY_LONG_LONG``
+``'L'`` ``long long``
``'n'`` ``Py_ssize_t``
``'O'`` ``object``
``'O!'`` ``object(subclass_of='&PySomething_Type')``
diff --git a/Doc/howto/index.rst b/Doc/howto/index.rst
index de65950..593341c 100644
--- a/Doc/howto/index.rst
+++ b/Doc/howto/index.rst
@@ -28,4 +28,5 @@ Currently, the HOWTOs are:
argparse.rst
ipaddress.rst
clinic.rst
+ instrumentation.rst
diff --git a/Doc/howto/instrumentation.rst b/Doc/howto/instrumentation.rst
new file mode 100644
index 0000000..7fca9aa
--- /dev/null
+++ b/Doc/howto/instrumentation.rst
@@ -0,0 +1,408 @@
+.. highlight:: shell-session
+
+.. _instrumentation:
+
+===============================================
+Instrumenting CPython with DTrace and SystemTap
+===============================================
+
+:author: David Malcolm
+:author: Łukasz Langa
+
+DTrace and SystemTap are monitoring tools, each providing a way to inspect
+what the processes on a computer system are doing. They both use
+domain-specific languages allowing a user to write scripts which:
+
+ - filter which processes are to be observed
+ - gather data from the processes of interest
+ - generate reports on the data
+
+As of Python 3.6, CPython can be built with embedded "markers", also
+known as "probes", that can be observed by a DTrace or SystemTap script,
+making it easier to monitor what the CPython processes on a system are
+doing.
+
+.. impl-detail::
+
+ DTrace markers are implementation details of the CPython interpreter.
+ No guarantees are made about probe compatibility between versions of
+ CPython. DTrace scripts can stop working or work incorrectly without
+ warning when changing CPython versions.
+
+
+Enabling the static markers
+---------------------------
+
+macOS comes with built-in support for DTrace. On Linux, in order to
+build CPython with the embedded markers for SystemTap, the SystemTap
+development tools must be installed.
+
+On a Linux machine, this can be done via::
+
+ $ yum install systemtap-sdt-devel
+
+or::
+
+ $ sudo apt-get install systemtap-sdt-dev
+
+
+CPython must then be configured ``--with-dtrace``:
+
+.. code-block:: none
+
+ checking for --with-dtrace... yes
+
+On macOS, you can list available DTrace probes by running a Python
+process in the background and listing all probes made available by the
+Python provider::
+
+ $ python3.6 -q &
+ $ sudo dtrace -l -P python$! # or: dtrace -l -m python3.6
+
+ ID PROVIDER MODULE FUNCTION NAME
+ 29564 python18035 python3.6 _PyEval_EvalFrameDefault function-entry
+ 29565 python18035 python3.6 dtrace_function_entry function-entry
+ 29566 python18035 python3.6 _PyEval_EvalFrameDefault function-return
+ 29567 python18035 python3.6 dtrace_function_return function-return
+ 29568 python18035 python3.6 collect gc-done
+ 29569 python18035 python3.6 collect gc-start
+ 29570 python18035 python3.6 _PyEval_EvalFrameDefault line
+ 29571 python18035 python3.6 maybe_dtrace_line line
+
+On Linux, you can verify if the SystemTap static markers are present in
+the built binary by seeing if it contains a ".note.stapsdt" section.
+
+::
+
+ $ readelf -S ./python | grep .note.stapsdt
+ [30] .note.stapsdt NOTE 0000000000000000 00308d78
+
+If you've built Python as a shared library (with --enable-shared), you
+need to look instead within the shared library. For example::
+
+ $ readelf -S libpython3.3dm.so.1.0 | grep .note.stapsdt
+ [29] .note.stapsdt NOTE 0000000000000000 00365b68
+
+Sufficiently modern readelf can print the metadata::
+
+ $ readelf -n ./python
+
+ Displaying notes found at file offset 0x00000254 with length 0x00000020:
+ Owner Data size Description
+ GNU 0x00000010 NT_GNU_ABI_TAG (ABI version tag)
+ OS: Linux, ABI: 2.6.32
+
+ Displaying notes found at file offset 0x00000274 with length 0x00000024:
+ Owner Data size Description
+ GNU 0x00000014 NT_GNU_BUILD_ID (unique build ID bitstring)
+ Build ID: df924a2b08a7e89f6e11251d4602022977af2670
+
+ Displaying notes found at file offset 0x002d6c30 with length 0x00000144:
+ Owner Data size Description
+ stapsdt 0x00000031 NT_STAPSDT (SystemTap probe descriptors)
+ Provider: python
+ Name: gc__start
+ Location: 0x00000000004371c3, Base: 0x0000000000630ce2, Semaphore: 0x00000000008d6bf6
+ Arguments: -4@%ebx
+ stapsdt 0x00000030 NT_STAPSDT (SystemTap probe descriptors)
+ Provider: python
+ Name: gc__done
+ Location: 0x00000000004374e1, Base: 0x0000000000630ce2, Semaphore: 0x00000000008d6bf8
+ Arguments: -8@%rax
+ stapsdt 0x00000045 NT_STAPSDT (SystemTap probe descriptors)
+ Provider: python
+ Name: function__entry
+ Location: 0x000000000053db6c, Base: 0x0000000000630ce2, Semaphore: 0x00000000008d6be8
+ Arguments: 8@%rbp 8@%r12 -4@%eax
+ stapsdt 0x00000046 NT_STAPSDT (SystemTap probe descriptors)
+ Provider: python
+ Name: function__return
+ Location: 0x000000000053dba8, Base: 0x0000000000630ce2, Semaphore: 0x00000000008d6bea
+ Arguments: 8@%rbp 8@%r12 -4@%eax
+
+The above metadata contains information for SystemTap describing how it
+can patch strategically-placed machine code instructions to enable the
+tracing hooks used by a SystemTap script.
+
+
+Static DTrace probes
+--------------------
+
+The following example DTrace script can be used to show the call/return
+hierarchy of a Python script, only tracing within the invocation of
+a function called "start". In other words, import-time function
+invocations are not going to be listed:
+
+.. code-block:: none
+
+ self int indent;
+
+ python$target:::function-entry
+ /copyinstr(arg1) == "start"/
+ {
+ self->trace = 1;
+ }
+
+ python$target:::function-entry
+ /self->trace/
+ {
+ printf("%d\t%*s:", timestamp, 15, probename);
+ printf("%*s", self->indent, "");
+ printf("%s:%s:%d\n", basename(copyinstr(arg0)), copyinstr(arg1), arg2);
+ self->indent++;
+ }
+
+ python$target:::function-return
+ /self->trace/
+ {
+ self->indent--;
+ printf("%d\t%*s:", timestamp, 15, probename);
+ printf("%*s", self->indent, "");
+ printf("%s:%s:%d\n", basename(copyinstr(arg0)), copyinstr(arg1), arg2);
+ }
+
+ python$target:::function-return
+ /copyinstr(arg1) == "start"/
+ {
+ self->trace = 0;
+ }
+
+It can be invoked like this::
+
+ $ sudo dtrace -q -s call_stack.d -c "python3.6 script.py"
+
+The output looks like this:
+
+.. code-block:: none
+
+ 156641360502280 function-entry:call_stack.py:start:23
+ 156641360518804 function-entry: call_stack.py:function_1:1
+ 156641360532797 function-entry: call_stack.py:function_3:9
+ 156641360546807 function-return: call_stack.py:function_3:10
+ 156641360563367 function-return: call_stack.py:function_1:2
+ 156641360578365 function-entry: call_stack.py:function_2:5
+ 156641360591757 function-entry: call_stack.py:function_1:1
+ 156641360605556 function-entry: call_stack.py:function_3:9
+ 156641360617482 function-return: call_stack.py:function_3:10
+ 156641360629814 function-return: call_stack.py:function_1:2
+ 156641360642285 function-return: call_stack.py:function_2:6
+ 156641360656770 function-entry: call_stack.py:function_3:9
+ 156641360669707 function-return: call_stack.py:function_3:10
+ 156641360687853 function-entry: call_stack.py:function_4:13
+ 156641360700719 function-return: call_stack.py:function_4:14
+ 156641360719640 function-entry: call_stack.py:function_5:18
+ 156641360732567 function-return: call_stack.py:function_5:21
+ 156641360747370 function-return:call_stack.py:start:28
+
+
+Static SystemTap markers
+------------------------
+
+The low-level way to use the SystemTap integration is to use the static
+markers directly. This requires you to explicitly state the binary file
+containing them.
+
+For example, this SystemTap script can be used to show the call/return
+hierarchy of a Python script:
+
+.. code-block:: none
+
+ probe process("python").mark("function__entry") {
+ filename = user_string($arg1);
+ funcname = user_string($arg2);
+ lineno = $arg3;
+
+ printf("%s => %s in %s:%d\\n",
+ thread_indent(1), funcname, filename, lineno);
+ }
+
+ probe process("python").mark("function__return") {
+ filename = user_string($arg1);
+ funcname = user_string($arg2);
+ lineno = $arg3;
+
+ printf("%s <= %s in %s:%d\\n",
+ thread_indent(-1), funcname, filename, lineno);
+ }
+
+It can be invoked like this::
+
+ $ stap \
+ show-call-hierarchy.stp \
+ -c "./python test.py"
+
+The output looks like this:
+
+.. code-block:: none
+
+ 11408 python(8274): => __contains__ in Lib/_abcoll.py:362
+ 11414 python(8274): => __getitem__ in Lib/os.py:425
+ 11418 python(8274): => encode in Lib/os.py:490
+ 11424 python(8274): <= encode in Lib/os.py:493
+ 11428 python(8274): <= __getitem__ in Lib/os.py:426
+ 11433 python(8274): <= __contains__ in Lib/_abcoll.py:366
+
+where the columns are:
+
+ - time in microseconds since start of script
+
+ - name of executable
+
+ - PID of process
+
+and the remainder indicates the call/return hierarchy as the script executes.
+
+For a `--enable-shared` build of CPython, the markers are contained within the
+libpython shared library, and the probe's dotted path needs to reflect this. For
+example, this line from the above example::
+
+ probe process("python").mark("function__entry") {
+
+should instead read::
+
+ probe process("python").library("libpython3.6dm.so.1.0").mark("function__entry") {
+
+(assuming a debug build of CPython 3.6)
+
+
+Available static markers
+------------------------
+
+.. I'm reusing the "c:function" type for markers
+
+.. c:function:: function__entry(str filename, str funcname, int lineno)
+
+ This marker indicates that execution of a Python function has begun.
+ It is only triggered for pure-Python (bytecode) functions.
+
+ The filename, function name, and line number are provided back to the
+ tracing script as positional arguments, which must be accessed using
+ ``$arg1``, ``$arg2``, ``$arg3``:
+
+ * ``$arg1`` : ``(const char *)`` filename, accessible using ``user_string($arg1)``
+
+ * ``$arg2`` : ``(const char *)`` function name, accessible using
+ ``user_string($arg2)``
+
+ * ``$arg3`` : ``int`` line number
+
+.. c:function:: function__return(str filename, str funcname, int lineno)
+
+ This marker is the converse of :c:func:`function__entry`, and indicates that
+ execution of a Python function has ended (either via ``return``, or via an
+ exception). It is only triggered for pure-Python (bytecode) functions.
+
+ The arguments are the same as for :c:func:`function__entry`
+
+.. c:function:: line(str filename, str funcname, int lineno)
+
+ This marker indicates a Python line is about to be executed. It is
+ the equivalent of line-by-line tracing with a Python profiler. It is
+ not triggered within C functions.
+
+ The arguments are the same as for :c:func:`function__entry`.
+
+.. c:function:: gc__start(int generation)
+
+ Fires when the Python interpreter starts a garbage collection cycle.
+ ``arg0`` is the generation to scan, like :func:`gc.collect()`.
+
+.. c:function:: gc__done(long collected)
+
+ Fires when the Python interpreter finishes a garbage collection
+ cycle. ``arg0`` is the number of collected objects.
+
+
+SystemTap Tapsets
+-----------------
+
+The higher-level way to use the SystemTap integration is to use a "tapset":
+SystemTap's equivalent of a library, which hides some of the lower-level
+details of the static markers.
+
+Here is a tapset file, based on a non-shared build of CPython:
+
+.. code-block:: none
+
+ /*
+ Provide a higher-level wrapping around the function__entry and
+ function__return markers:
+ \*/
+ probe python.function.entry = process("python").mark("function__entry")
+ {
+ filename = user_string($arg1);
+ funcname = user_string($arg2);
+ lineno = $arg3;
+ frameptr = $arg4
+ }
+ probe python.function.return = process("python").mark("function__return")
+ {
+ filename = user_string($arg1);
+ funcname = user_string($arg2);
+ lineno = $arg3;
+ frameptr = $arg4
+ }
+
+If this file is installed in SystemTap's tapset directory (e.g.
+``/usr/share/systemtap/tapset``), then these additional probepoints become
+available:
+
+.. c:function:: python.function.entry(str filename, str funcname, int lineno, frameptr)
+
+ This probe point indicates that execution of a Python function has begun.
+ It is only triggered for pure-python (bytecode) functions.
+
+.. c:function:: python.function.return(str filename, str funcname, int lineno, frameptr)
+
+ This probe point is the converse of :c:func:`python.function.return`, and
+ indicates that execution of a Python function has ended (either via
+ ``return``, or via an exception). It is only triggered for pure-python
+ (bytecode) functions.
+
+
+Examples
+--------
+This SystemTap script uses the tapset above to more cleanly implement the
+example given above of tracing the Python function-call hierarchy, without
+needing to directly name the static markers:
+
+.. code-block:: none
+
+ probe python.function.entry
+ {
+ printf("%s => %s in %s:%d\n",
+ thread_indent(1), funcname, filename, lineno);
+ }
+
+ probe python.function.return
+ {
+ printf("%s <= %s in %s:%d\n",
+ thread_indent(-1), funcname, filename, lineno);
+ }
+
+
+The following script uses the tapset above to provide a top-like view of all
+running CPython code, showing the top 20 most frequently-entered bytecode
+frames, each second, across the whole system:
+
+.. code-block:: none
+
+ global fn_calls;
+
+ probe python.function.entry
+ {
+ fn_calls[pid(), filename, funcname, lineno] += 1;
+ }
+
+ probe timer.ms(1000) {
+ printf("\033[2J\033[1;1H") /* clear screen \*/
+ printf("%6s %80s %6s %30s %6s\n",
+ "PID", "FILENAME", "LINE", "FUNCTION", "CALLS")
+ foreach ([pid, filename, funcname, lineno] in fn_calls- limit 20) {
+ printf("%6d %80s %6d %30s %6d\n",
+ pid, filename, lineno, funcname,
+ fn_calls[pid, filename, funcname, lineno]);
+ }
+ delete fn_calls;
+ }
+
diff --git a/Doc/includes/email-alternative-new-api.py b/Doc/includes/email-alternative-new-api.py
deleted file mode 100644
index 321f727..0000000
--- a/Doc/includes/email-alternative-new-api.py
+++ /dev/null
@@ -1,56 +0,0 @@
-#!/usr/bin/env python3
-
-import smtplib
-
-from email.message import EmailMessage
-from email.headerregistry import Address
-from email.utils import make_msgid
-
-# Create the base text message.
-msg = EmailMessage()
-msg['Subject'] = "Ayons asperges pour le déjeuner"
-msg['From'] = Address("Pepé Le Pew", "pepe", "example.com")
-msg['To'] = (Address("Penelope Pussycat", "penelope", "example.com"),
- Address("Fabrette Pussycat", "fabrette", "example.com"))
-msg.set_content("""\
-Salut!
-
-Cela ressemble à un excellent recipie[1] déjeuner.
-
-[1] http://www.yummly.com/recipe/Roasted-Asparagus-Epicurious-203718
-
---Pepé
-""")
-
-# Add the html version. This converts the message into a multipart/alternative
-# container, with the original text message as the first part and the new html
-# message as the second part.
-asparagus_cid = make_msgid()
-msg.add_alternative("""\
-<html>
- <head></head>
- <body>
- <p>Salut!<\p>
- <p>Cela ressemble à un excellent
- <a href="http://www.yummly.com/recipe/Roasted-Asparagus-Epicurious-203718>
- recipie
- </a> déjeuner.
- </p>
- <img src="cid:{asparagus_cid}" \>
- </body>
-</html>
-""".format(asparagus_cid=asparagus_cid[1:-1]), subtype='html')
-# note that we needed to peel the <> off the msgid for use in the html.
-
-# Now add the related image to the html part.
-with open("roasted-asparagus.jpg", 'rb') as img:
- msg.get_payload()[1].add_related(img.read(), 'image', 'jpeg',
- cid=asparagus_cid)
-
-# Make a local copy of what we are going to send.
-with open('outgoing.msg', 'wb') as f:
- f.write(bytes(msg))
-
-# Send the message via local SMTP server.
-with smtplib.SMTP('localhost') as s:
- s.send_message(msg)
diff --git a/Doc/includes/email-alternative.py b/Doc/includes/email-alternative.py
index 85070f3..2e142b1 100755..100644
--- a/Doc/includes/email-alternative.py
+++ b/Doc/includes/email-alternative.py
@@ -2,47 +2,55 @@
import smtplib
-from email.mime.multipart import MIMEMultipart
-from email.mime.text import MIMEText
-
-# me == my email address
-# you == recipient's email address
-me = "my@email.com"
-you = "your@email.com"
-
-# Create message container - the correct MIME type is multipart/alternative.
-msg = MIMEMultipart('alternative')
-msg['Subject'] = "Link"
-msg['From'] = me
-msg['To'] = you
-
-# Create the body of the message (a plain-text and an HTML version).
-text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttps://www.python.org"
-html = """\
+from email.message import EmailMessage
+from email.headerregistry import Address
+from email.utils import make_msgid
+
+# Create the base text message.
+msg = EmailMessage()
+msg['Subject'] = "Ayons asperges pour le déjeuner"
+msg['From'] = Address("Pepé Le Pew", "pepe", "example.com")
+msg['To'] = (Address("Penelope Pussycat", "penelope", "example.com"),
+ Address("Fabrette Pussycat", "fabrette", "example.com"))
+msg.set_content("""\
+Salut!
+
+Cela ressemble à un excellent recipie[1] déjeuner.
+
+[1] http://www.yummly.com/recipe/Roasted-Asparagus-Epicurious-203718
+
+--Pepé
+""")
+
+# Add the html version. This converts the message into a multipart/alternative
+# container, with the original text message as the first part and the new html
+# message as the second part.
+asparagus_cid = make_msgid()
+msg.add_alternative("""\
<html>
<head></head>
<body>
- <p>Hi!<br>
- How are you?<br>
- Here is the <a href="https://www.python.org">link</a> you wanted.
+ <p>Salut!</p>
+ <p>Cela ressemble à un excellent
+ <a href="http://www.yummly.com/recipe/Roasted-Asparagus-Epicurious-203718>
+ recipie
+ </a> déjeuner.
</p>
+ <img src="cid:{asparagus_cid}" />
</body>
</html>
-"""
+""".format(asparagus_cid=asparagus_cid[1:-1]), subtype='html')
+# note that we needed to peel the <> off the msgid for use in the html.
-# Record the MIME types of both parts - text/plain and text/html.
-part1 = MIMEText(text, 'plain')
-part2 = MIMEText(html, 'html')
+# Now add the related image to the html part.
+with open("roasted-asparagus.jpg", 'rb') as img:
+ msg.get_payload()[1].add_related(img.read(), 'image', 'jpeg',
+ cid=asparagus_cid)
-# Attach parts into message container.
-# According to RFC 2046, the last part of a multipart message, in this case
-# the HTML message, is best and preferred.
-msg.attach(part1)
-msg.attach(part2)
+# Make a local copy of what we are going to send.
+with open('outgoing.msg', 'wb') as f:
+ f.write(bytes(msg))
# Send the message via local SMTP server.
-s = smtplib.SMTP('localhost')
-# sendmail function takes 3 arguments: sender's address, recipient's address
-# and message to send - here it is sent as one string.
-s.sendmail(me, you, msg.as_string())
-s.quit()
+with smtplib.SMTP('localhost') as s:
+ s.send_message(msg)
diff --git a/Doc/includes/email-dir.py b/Doc/includes/email-dir.py
index 3c7c770..0dcfbfb 100644
--- a/Doc/includes/email-dir.py
+++ b/Doc/includes/email-dir.py
@@ -3,22 +3,14 @@
"""Send the contents of a directory as a MIME message."""
import os
-import sys
import smtplib
# For guessing MIME type based on file name extension
import mimetypes
from argparse import ArgumentParser
-from email import encoders
-from email.message import Message
-from email.mime.audio import MIMEAudio
-from email.mime.base import MIMEBase
-from email.mime.image import MIMEImage
-from email.mime.multipart import MIMEMultipart
-from email.mime.text import MIMEText
-
-COMMASPACE = ', '
+from email.message import EmailMessage
+from email.policy import SMTP
def main():
@@ -47,12 +39,12 @@ must be running an SMTP server.
directory = args.directory
if not directory:
directory = '.'
- # Create the enclosing (outer) message
- outer = MIMEMultipart()
- outer['Subject'] = 'Contents of directory %s' % os.path.abspath(directory)
- outer['To'] = COMMASPACE.join(args.recipients)
- outer['From'] = args.sender
- outer.preamble = 'You will not see this in a MIME-aware mail reader.\n'
+ # Create the message
+ msg = EmailMessage()
+ msg['Subject'] = 'Contents of directory %s' % os.path.abspath(directory)
+ msg['To'] = ', '.join(args.recipients)
+ msg['From'] = args.sender
+ msg.preamble = 'You will not see this in a MIME-aware mail reader.\n'
for filename in os.listdir(directory):
path = os.path.join(directory, filename)
@@ -67,33 +59,18 @@ must be running an SMTP server.
# use a generic bag-of-bits type.
ctype = 'application/octet-stream'
maintype, subtype = ctype.split('/', 1)
- if maintype == 'text':
- with open(path) as fp:
- # Note: we should handle calculating the charset
- msg = MIMEText(fp.read(), _subtype=subtype)
- elif maintype == 'image':
- with open(path, 'rb') as fp:
- msg = MIMEImage(fp.read(), _subtype=subtype)
- elif maintype == 'audio':
- with open(path, 'rb') as fp:
- msg = MIMEAudio(fp.read(), _subtype=subtype)
- else:
- with open(path, 'rb') as fp:
- msg = MIMEBase(maintype, subtype)
- msg.set_payload(fp.read())
- # Encode the payload using Base64
- encoders.encode_base64(msg)
- # Set the filename parameter
- msg.add_header('Content-Disposition', 'attachment', filename=filename)
- outer.attach(msg)
+ with open(path, 'rb') as fp:
+ msg.add_attachment(fp.read(),
+ maintype=maintype,
+ subtype=subtype,
+ filename=filename)
# Now send or store the message
- composed = outer.as_string()
if args.output:
- with open(args.output, 'w') as fp:
- fp.write(composed)
+ with open(args.output, 'wb') as fp:
+ fp.write(msg.as_bytes(policy=SMTP))
else:
with smtplib.SMTP('localhost') as s:
- s.sendmail(args.sender, args.recipients, composed)
+ s.send_message(msg)
if __name__ == '__main__':
diff --git a/Doc/includes/email-headers.py b/Doc/includes/email-headers.py
index 89c8f3a..2c42145 100644
--- a/Doc/includes/email-headers.py
+++ b/Doc/includes/email-headers.py
@@ -1,18 +1,24 @@
# Import the email modules we'll need
-from email.parser import Parser
+from email.parser import BytesParser, Parser
+from email.policy import default
# If the e-mail headers are in a file, uncomment these two lines:
-# with open(messagefile) as fp:
-# headers = Parser().parse(fp)
+# with open(messagefile, 'rb') as fp:
+# headers = BytesParser(policy=default).parse(fp)
-# Or for parsing headers in a string, use:
-headers = Parser().parsestr('From: <user@example.com>\n'
+# Or for parsing headers in a string (this is an uncommon operation), use:
+headers = Parser(policy=default).parsestr(
+ 'From: Foo Bar <user@example.com>\n'
'To: <someone_else@example.com>\n'
'Subject: Test message\n'
'\n'
'Body would go here\n')
# Now the header items can be accessed as a dictionary:
-print('To: %s' % headers['to'])
-print('From: %s' % headers['from'])
-print('Subject: %s' % headers['subject'])
+print('To: {}'.format(headers['to']))
+print('From: {}'.format(headers['from']))
+print('Subject: {}'.format(headers['subject']))
+
+# You can also access the parts of the addresses:
+print('Recipient username: {}'.format(headers['to'].addresses[0].username))
+print('Sender name: {}'.format(headers['from'].addresses[0].display_name))
diff --git a/Doc/includes/email-mime.py b/Doc/includes/email-mime.py
index 61d0830..c610242 100644
--- a/Doc/includes/email-mime.py
+++ b/Doc/includes/email-mime.py
@@ -1,30 +1,29 @@
# Import smtplib for the actual sending function
import smtplib
-# Here are the email package modules we'll need
-from email.mime.image import MIMEImage
-from email.mime.multipart import MIMEMultipart
+# And imghdr to find the types of our images
+import imghdr
-COMMASPACE = ', '
+# Here are the email package modules we'll need
+from email.message import EmailMessage
-# Create the container (outer) email message.
-msg = MIMEMultipart()
+# Create the container email message.
+msg = EmailMessage()
msg['Subject'] = 'Our family reunion'
# me == the sender's email address
# family = the list of all recipients' email addresses
msg['From'] = me
-msg['To'] = COMMASPACE.join(family)
+msg['To'] = ', '.join(family)
msg.preamble = 'Our family reunion'
-# Assume we know that the image files are all in PNG format
+# Open the files in binary mode. Use imghdr to figure out the
+# MIME subtype for each specific image.
for file in pngfiles:
- # Open the files in binary mode. Let the MIMEImage class automatically
- # guess the specific image type.
with open(file, 'rb') as fp:
- img = MIMEImage(fp.read())
- msg.attach(img)
+ img_data = fp.read()
+ msg.add_attachment(img_data, maintype='image',
+ subtype=imghdr.what(None, img_data))
# Send the email via our own SMTP server.
-s = smtplib.SMTP('localhost')
-s.send_message(msg)
-s.quit()
+with smtplib.SMTP('localhost') as s:
+ s.send_message(msg)
diff --git a/Doc/includes/email-read-alternative-new-api.py b/Doc/includes/email-read-alternative.py
index 3f5ab24..3f5ab24 100644
--- a/Doc/includes/email-read-alternative-new-api.py
+++ b/Doc/includes/email-read-alternative.py
diff --git a/Doc/includes/email-simple.py b/Doc/includes/email-simple.py
index b9b8b41..f69ef40 100644
--- a/Doc/includes/email-simple.py
+++ b/Doc/includes/email-simple.py
@@ -2,13 +2,13 @@
import smtplib
# Import the email modules we'll need
-from email.mime.text import MIMEText
+from email.message import EmailMessage
-# Open a plain text file for reading. For this example, assume that
-# the text file contains only ASCII characters.
+# Open the plain text file whose name is in textfile for reading.
with open(textfile) as fp:
# Create a text/plain message
- msg = MIMEText(fp.read())
+ msg = EmailMessage()
+ msg.set_content(fp.read())
# me == the sender's email address
# you == the recipient's email address
diff --git a/Doc/includes/email-unpack.py b/Doc/includes/email-unpack.py
index 574a0b6..e0a7f01 100644
--- a/Doc/includes/email-unpack.py
+++ b/Doc/includes/email-unpack.py
@@ -3,11 +3,11 @@
"""Unpack a MIME message into a directory of files."""
import os
-import sys
import email
-import errno
import mimetypes
+from email.policy import default
+
from argparse import ArgumentParser
@@ -22,8 +22,8 @@ Unpack a MIME message into a directory of files.
parser.add_argument('msgfile')
args = parser.parse_args()
- with open(args.msgfile) as fp:
- msg = email.message_from_file(fp)
+ with open(args.msgfile, 'rb') as fp:
+ msg = email.message_from_binary_file(fp, policy=default)
try:
os.mkdir(args.directory)
diff --git a/Doc/includes/run-func.c b/Doc/includes/run-func.c
index 986d670..ead7bdd 100644
--- a/Doc/includes/run-func.c
+++ b/Doc/includes/run-func.c
@@ -63,6 +63,8 @@ main(int argc, char *argv[])
fprintf(stderr, "Failed to load \"%s\"\n", argv[1]);
return 1;
}
- Py_Finalize();
+ if (Py_FinalizeEx() < 0) {
+ return 120;
+ }
return 0;
}
diff --git a/Doc/includes/test.py b/Doc/includes/test.py
index 7ebf46a..9e9d4a6 100644
--- a/Doc/includes/test.py
+++ b/Doc/includes/test.py
@@ -204,7 +204,7 @@ Test cyclic gc(?)
import os
import sys
from distutils.util import get_platform
-PLAT_SPEC = "%s-%s" % (get_platform(), sys.version[0:3])
+PLAT_SPEC = "%s-%d.%d" % (get_platform(), *sys.version_info[:2])
src = os.path.join("build", "lib.%s" % PLAT_SPEC)
sys.path.append(src)
diff --git a/Doc/includes/tzinfo-examples.py b/Doc/includes/tzinfo-examples.py
index 3a8cf47..ae5a509 100644
--- a/Doc/includes/tzinfo-examples.py
+++ b/Doc/includes/tzinfo-examples.py
@@ -1,46 +1,13 @@
-from datetime import tzinfo, timedelta, datetime
+from datetime import tzinfo, timedelta, datetime, timezone
ZERO = timedelta(0)
HOUR = timedelta(hours=1)
-
-# A UTC class.
-
-class UTC(tzinfo):
- """UTC"""
-
- def utcoffset(self, dt):
- return ZERO
-
- def tzname(self, dt):
- return "UTC"
-
- def dst(self, dt):
- return ZERO
-
-utc = UTC()
-
-# A class building tzinfo objects for fixed-offset time zones.
-# Note that FixedOffset(0, "UTC") is a different way to build a
-# UTC tzinfo object.
-
-class FixedOffset(tzinfo):
- """Fixed offset in minutes east from UTC."""
-
- def __init__(self, offset, name):
- self.__offset = timedelta(minutes=offset)
- self.__name = name
-
- def utcoffset(self, dt):
- return self.__offset
-
- def tzname(self, dt):
- return self.__name
-
- def dst(self, dt):
- return ZERO
+SECOND = timedelta(seconds=1)
# A class capturing the platform's idea of local time.
-
+# (May result in wrong values on historical times in
+# timezones where UTC offset and/or the DST rules had
+# changed in the past.)
import time as _time
STDOFFSET = timedelta(seconds = -_time.timezone)
@@ -53,6 +20,16 @@ DSTDIFF = DSTOFFSET - STDOFFSET
class LocalTimezone(tzinfo):
+ def fromutc(self, dt):
+ assert dt.tzinfo is self
+ stamp = (dt - datetime(1970, 1, 1, tzinfo=self)) // SECOND
+ args = _time.localtime(stamp)[:6]
+ dst_diff = DSTDIFF // SECOND
+ # Detect fold
+ fold = (args == _time.localtime(stamp - dst_diff))
+ return datetime(*args, microsecond=dt.microsecond,
+ tzinfo=self, fold=fold)
+
def utcoffset(self, dt):
if self._isdst(dt):
return DSTOFFSET
@@ -99,20 +76,37 @@ def first_sunday_on_or_after(dt):
# In the US, since 2007, DST starts at 2am (standard time) on the second
# Sunday in March, which is the first Sunday on or after Mar 8.
DSTSTART_2007 = datetime(1, 3, 8, 2)
-# and ends at 2am (DST time; 1am standard time) on the first Sunday of Nov.
-DSTEND_2007 = datetime(1, 11, 1, 1)
+# and ends at 2am (DST time) on the first Sunday of Nov.
+DSTEND_2007 = datetime(1, 11, 1, 2)
# From 1987 to 2006, DST used to start at 2am (standard time) on the first
-# Sunday in April and to end at 2am (DST time; 1am standard time) on the last
+# Sunday in April and to end at 2am (DST time) on the last
# Sunday of October, which is the first Sunday on or after Oct 25.
DSTSTART_1987_2006 = datetime(1, 4, 1, 2)
-DSTEND_1987_2006 = datetime(1, 10, 25, 1)
+DSTEND_1987_2006 = datetime(1, 10, 25, 2)
# From 1967 to 1986, DST used to start at 2am (standard time) on the last
-# Sunday in April (the one on or after April 24) and to end at 2am (DST time;
-# 1am standard time) on the last Sunday of October, which is the first Sunday
+# Sunday in April (the one on or after April 24) and to end at 2am (DST time)
+# on the last Sunday of October, which is the first Sunday
# on or after Oct 25.
DSTSTART_1967_1986 = datetime(1, 4, 24, 2)
DSTEND_1967_1986 = DSTEND_1987_2006
+def us_dst_range(year):
+ # Find start and end times for US DST. For years before 1967, return
+ # start = end for no DST.
+ if 2006 < year:
+ dststart, dstend = DSTSTART_2007, DSTEND_2007
+ elif 1986 < year < 2007:
+ dststart, dstend = DSTSTART_1987_2006, DSTEND_1987_2006
+ elif 1966 < year < 1987:
+ dststart, dstend = DSTSTART_1967_1986, DSTEND_1967_1986
+ else:
+ return (datetime(year, 1, 1), ) * 2
+
+ start = first_sunday_on_or_after(dststart.replace(year=year))
+ end = first_sunday_on_or_after(dstend.replace(year=year))
+ return start, end
+
+
class USTimeZone(tzinfo):
def __init__(self, hours, reprname, stdname, dstname):
@@ -141,27 +135,39 @@ class USTimeZone(tzinfo):
# implementation) passes a datetime with dt.tzinfo is self.
return ZERO
assert dt.tzinfo is self
-
- # Find start and end times for US DST. For years before 1967, return
- # ZERO for no DST.
- if 2006 < dt.year:
- dststart, dstend = DSTSTART_2007, DSTEND_2007
- elif 1986 < dt.year < 2007:
- dststart, dstend = DSTSTART_1987_2006, DSTEND_1987_2006
- elif 1966 < dt.year < 1987:
- dststart, dstend = DSTSTART_1967_1986, DSTEND_1967_1986
- else:
- return ZERO
-
- start = first_sunday_on_or_after(dststart.replace(year=dt.year))
- end = first_sunday_on_or_after(dstend.replace(year=dt.year))
-
+ start, end = us_dst_range(dt.year)
# Can't compare naive to aware objects, so strip the timezone from
# dt first.
- if start <= dt.replace(tzinfo=None) < end:
+ dt = dt.replace(tzinfo=None)
+ if start + HOUR <= dt < end - HOUR:
+ # DST is in effect.
return HOUR
- else:
- return ZERO
+ if end - HOUR <= dt < end:
+ # Fold (an ambiguous hour): use dt.fold to disambiguate.
+ return ZERO if dt.fold else HOUR
+ if start <= dt < start + HOUR:
+ # Gap (a non-existent hour): reverse the fold rule.
+ return HOUR if dt.fold else ZERO
+ # DST is off.
+ return ZERO
+
+ def fromutc(self, dt):
+ assert dt.tzinfo is self
+ start, end = us_dst_range(dt.year)
+ start = start.replace(tzinfo=self)
+ end = end.replace(tzinfo=self)
+ std_time = dt + self.stdoffset
+ dst_time = std_time + HOUR
+ if end <= dst_time < end + HOUR:
+ # Repeated hour
+ return std_time.replace(fold=1)
+ if std_time < start or dst_time >= end:
+ # Standard time
+ return std_time
+ if start <= std_time < end - HOUR:
+ # Daylight saving time
+ return dst_time
+
Eastern = USTimeZone(-5, "Eastern", "EST", "EDT")
Central = USTimeZone(-6, "Central", "CST", "CDT")
diff --git a/Doc/installing/index.rst b/Doc/installing/index.rst
index 1ef3149..b22465d 100644
--- a/Doc/installing/index.rst
+++ b/Doc/installing/index.rst
@@ -2,9 +2,9 @@
.. _installing-index:
-*****************************
- Installing Python Modules
-*****************************
+*************************
+Installing Python Modules
+*************************
:Email: distutils-sig@python.org
@@ -34,24 +34,24 @@ Key terms
* ``pip`` is the preferred installer program. Starting with Python 3.4, it
is included by default with the Python binary installers.
-* a virtual environment is a semi-isolated Python environment that allows
+* A *virtual environment* is a semi-isolated Python environment that allows
packages to be installed for use by a particular application, rather than
- being installed system wide
-* ``pyvenv`` is the standard tool for creating virtual environments, and has
+ being installed system wide.
+* ``venv`` is the standard tool for creating virtual environments, and has
been part of Python since Python 3.3. Starting with Python 3.4, it
- defaults to installing ``pip`` into all created virtual environments
+ defaults to installing ``pip`` into all created virtual environments.
* ``virtualenv`` is a third party alternative (and predecessor) to
- ``pyvenv``. It allows virtual environments to be used on versions of
- Python prior to 3.4, which either don't provide ``pyvenv`` at all, or
+ ``venv``. It allows virtual environments to be used on versions of
+ Python prior to 3.4, which either don't provide ``venv`` at all, or
aren't able to automatically install ``pip`` into created environments.
-* the `Python Packaging Index <https://pypi.python.org/pypi>`__ is a public
+* The `Python Packaging Index <https://pypi.python.org/pypi>`__ is a public
repository of open source licensed packages made available for use by
- other Python users
+ other Python users.
* the `Python Packaging Authority
<https://www.pypa.io/en/latest/>`__ are the group of
developers and documentation authors responsible for the maintenance and
evolution of the standard packaging tools and the associated metadata and
- file format standards. They maintain a variety of tools, documentation
+ file format standards. They maintain a variety of tools, documentation,
and issue trackers on both `GitHub <https://github.com/pypa>`__ and
`BitBucket <https://bitbucket.org/pypa/>`__.
* ``distutils`` is the original build and distribution system first added to
@@ -62,6 +62,19 @@ Key terms
of the mailing list used to coordinate Python packaging standards
development).
+.. deprecated:: 3.6
+ ``pyvenv`` was the recommended tool for creating virtual environments for
+ Python 3.3 and 3.4, and is `deprecated in Python 3.6
+ <https://docs.python.org/dev/whatsnew/3.6.html#deprecated-features>`_.
+
+.. versionchanged:: 3.5
+ The use of ``venv`` is now recommended for creating virtual environments.
+
+.. seealso::
+
+ `Python Packaging User Guide: Creating and using virtual environments
+ <https://packaging.python.org/installing/#creating-virtual-environments>`__
+
Basic usage
===========
@@ -100,13 +113,14 @@ explicitly::
More information and resources regarding ``pip`` and its capabilities can be
found in the `Python Packaging User Guide <https://packaging.python.org>`__.
-``pyvenv`` has its own documentation at :ref:`scripts-pyvenv`. Installing
-into an active virtual environment uses the commands shown above.
+Creation of virtual environments is done through the :mod:`venv` module.
+Installing packages into an active virtual environment uses the commands shown
+above.
.. seealso::
`Python Packaging User Guide: Installing Python Distribution Packages
- <https://packaging.python.org/en/latest/installing/>`__
+ <https://packaging.python.org/installing/>`__
How do I ...?
@@ -124,7 +138,7 @@ User Guide.
.. seealso::
`Python Packaging User Guide: Requirements for Installing Packages
- <https://packaging.python.org/en/latest/installing/#requirements-for-installing-packages>`__
+ <https://packaging.python.org/installing/#requirements-for-installing-packages>`__
.. installing-per-user-installation:
@@ -142,20 +156,19 @@ package just for the current user, rather than for all users of the system.
A number of scientific Python packages have complex binary dependencies, and
aren't currently easy to install using ``pip`` directly. At this point in
time, it will often be easier for users to install these packages by
-`other means
-<https://packaging.python.org/en/latest/science/>`__
+`other means <https://packaging.python.org/science/>`__
rather than attempting to install them with ``pip``.
.. seealso::
`Python Packaging User Guide: Installing Scientific Packages
- <https://packaging.python.org/en/latest/science/>`__
+ <https://packaging.python.org/science/>`__
... work with multiple versions of Python installed in parallel?
----------------------------------------------------------------
-On Linux, Mac OS X and other POSIX systems, use the versioned Python commands
+On Linux, Mac OS X, and other POSIX systems, use the versioned Python commands
in combination with the ``-m`` switch to run the appropriate copy of
``pip``::
@@ -164,7 +177,7 @@ in combination with the ``-m`` switch to run the appropriate copy of
python3 -m pip install SomePackage # default Python 3
python3.4 -m pip install SomePackage # specifically Python 3.4
-(appropriately versioned ``pip`` commands may also be available)
+Appropriately versioned ``pip`` commands may also be available.
On Windows, use the ``py`` Python launcher in combination with the ``-m``
switch::
@@ -212,11 +225,11 @@ as users are more regularly able to install pre-built extensions rather
than needing to build them themselves.
Some of the solutions for installing `scientific software
-<https://packaging.python.org/en/latest/science/>`__
-that is not yet available as pre-built ``wheel`` files may also help with
+<https://packaging.python.org/science/>`__
+that are not yet available as pre-built ``wheel`` files may also help with
obtaining other binary extensions without needing to build them locally.
.. seealso::
`Python Packaging User Guide: Binary Extensions
- <https://packaging.python.org/en/latest/extensions/>`__
+ <https://packaging.python.org/extensions/>`__
diff --git a/Doc/library/asynchat.rst b/Doc/library/asynchat.rst
index ae72d26..9e51416 100644
--- a/Doc/library/asynchat.rst
+++ b/Doc/library/asynchat.rst
@@ -9,6 +9,9 @@
**Source code:** :source:`Lib/asynchat.py`
+.. deprecated:: 3.6
+ Please use :mod:`asyncio` instead.
+
--------------
.. note::
@@ -58,13 +61,13 @@ connection requests.
The asynchronous output buffer size (default ``4096``).
Unlike :class:`asyncore.dispatcher`, :class:`async_chat` allows you to
- define a first-in-first-out queue (fifo) of *producers*. A producer need
+ define a :abbr:`FIFO (first-in, first-out)` queue of *producers*. A producer need
have only one method, :meth:`more`, which should return data to be
transmitted on the channel.
The producer indicates exhaustion (*i.e.* that it contains no more data) by
having its :meth:`more` method return the empty bytes object. At this point
- the :class:`async_chat` object removes the producer from the fifo and starts
- using the next producer, if any. When the producer fifo is empty the
+ the :class:`async_chat` object removes the producer from the queue and starts
+ using the next producer, if any. When the producer queue is empty the
:meth:`handle_write` method does nothing. You use the channel object's
:meth:`set_terminator` method to describe how to recognize the end of, or
an important breakpoint in, an incoming transmission from the remote
@@ -78,8 +81,8 @@ connection requests.
.. method:: async_chat.close_when_done()
- Pushes a ``None`` on to the producer fifo. When this producer is popped off
- the fifo it causes the channel to be closed.
+ Pushes a ``None`` on to the producer queue. When this producer is popped off
+ the queue it causes the channel to be closed.
.. method:: async_chat.collect_incoming_data(data)
@@ -92,7 +95,7 @@ connection requests.
.. method:: async_chat.discard_buffers()
In emergencies this method will discard any data held in the input and/or
- output buffers and the producer fifo.
+ output buffers and the producer queue.
.. method:: async_chat.found_terminator()
@@ -110,7 +113,7 @@ connection requests.
.. method:: async_chat.push(data)
- Pushes data on to the channel's fifo to ensure its transmission.
+ Pushes data on to the channel's queue to ensure its transmission.
This is all you need to do to have the channel write the data out to the
network, although it is possible to use your own producers in more complex
schemes to implement encryption and chunking, for example.
@@ -118,7 +121,7 @@ connection requests.
.. method:: async_chat.push_with_producer(producer)
- Takes a producer object and adds it to the producer fifo associated with
+ Takes a producer object and adds it to the producer queue associated with
the channel. When all currently-pushed producers have been exhausted the
channel will consume this producer's data by calling its :meth:`more`
method and send the data to the remote endpoint.
diff --git a/Doc/library/asyncio-eventloop.rst b/Doc/library/asyncio-eventloop.rst
index d720160..fa6a296 100644
--- a/Doc/library/asyncio-eventloop.rst
+++ b/Doc/library/asyncio-eventloop.rst
@@ -88,6 +88,24 @@ Run an event loop
This is idempotent and irreversible. No other methods should be called after
this one.
+
+.. coroutinemethod:: AbstractEventLoop.shutdown_asyncgens()
+
+ Schedule all currently open :term:`asynchronous generator` objects to
+ close with an :meth:`~agen.aclose()` call. After calling this method,
+ the event loop will issue a warning whenever a new asynchronous generator
+ is iterated. Should be used to finalize all scheduled asynchronous
+ generators reliably. Example::
+
+ try:
+ loop.run_forever()
+ finally:
+ loop.run_until_complete(loop.shutdown_asyncgens())
+ loop.close()
+
+ .. versionadded:: 3.6
+
+
.. _asyncio-pass-keywords:
Calls
@@ -110,8 +128,9 @@ keywords to your callback, use :func:`functools.partial`. For example,
called after :meth:`call_soon` returns, when control returns to the event
loop.
- This operates as a FIFO queue, callbacks are called in the order in
- which they are registered. Each callback will be called exactly once.
+ This operates as a :abbr:`FIFO (first-in, first-out)` queue, callbacks
+ are called in the order in which they are registered. Each callback
+ will be called exactly once.
Any positional arguments after the callback will be passed to the
callback when it is called.
diff --git a/Doc/library/asyncio-protocol.rst b/Doc/library/asyncio-protocol.rst
index c0342f7..3fbf510 100644
--- a/Doc/library/asyncio-protocol.rst
+++ b/Doc/library/asyncio-protocol.rst
@@ -25,6 +25,9 @@ the transport's kind.
The transport classes are :ref:`not thread safe <asyncio-multithreading>`.
+.. versionchanged:: 3.6
+ The socket option ``TCP_NODELAY`` is now set by default.
+
BaseTransport
-------------
diff --git a/Doc/library/asyncio.rst b/Doc/library/asyncio.rst
index 76bd9e9..b076b7d 100644
--- a/Doc/library/asyncio.rst
+++ b/Doc/library/asyncio.rst
@@ -8,13 +8,6 @@
**Source code:** :source:`Lib/asyncio/`
-.. note::
-
- The asyncio package has been included in the standard library on a
- :term:`provisional basis <provisional package>`. Backwards incompatible
- changes (up to and including removal of the module) may occur if deemed
- necessary by the core developers.
-
--------------
This module provides infrastructure for writing single-threaded concurrent
diff --git a/Doc/library/asyncore.rst b/Doc/library/asyncore.rst
index c838be7..11d3616 100644
--- a/Doc/library/asyncore.rst
+++ b/Doc/library/asyncore.rst
@@ -12,6 +12,9 @@
**Source code:** :source:`Lib/asyncore.py`
+.. deprecated:: 3.6
+ Please use :mod:`asyncio` instead.
+
--------------
.. note::
diff --git a/Doc/library/binascii.rst b/Doc/library/binascii.rst
index 49da59a..0476f50 100644
--- a/Doc/library/binascii.rst
+++ b/Doc/library/binascii.rst
@@ -53,13 +53,14 @@ The :mod:`binascii` module defines the following functions:
than one line may be passed at a time.
-.. function:: b2a_base64(data)
+.. function:: b2a_base64(data, \*, newline=True)
Convert binary data to a line of ASCII characters in base64 coding. The return
- value is the converted line, including a newline char. The newline is
- added because the original use case for this function was to feed it a
- series of 57 byte input lines to get output lines that conform to the
- MIME-base64 standard. Otherwise the output conforms to :rfc:`3548`.
+ value is the converted line, including a newline char if *newline* is
+ true. The output of this function conforms to :rfc:`3548`.
+
+ .. versionchanged:: 3.6
+ Added the *newline* parameter.
.. function:: a2b_qp(data, header=False)
diff --git a/Doc/library/bz2.rst b/Doc/library/bz2.rst
index 6c49d9f..d5f6225 100644
--- a/Doc/library/bz2.rst
+++ b/Doc/library/bz2.rst
@@ -61,6 +61,9 @@ All of the classes in this module may safely be accessed from multiple threads.
.. versionchanged:: 3.4
The ``'x'`` (exclusive creation) mode was added.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. class:: BZ2File(filename, mode='r', buffering=None, compresslevel=9)
@@ -128,6 +131,9 @@ All of the classes in this module may safely be accessed from multiple threads.
The :meth:`~io.BufferedIOBase.read` method now accepts an argument of
``None``.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
Incremental (de)compression
---------------------------
diff --git a/Doc/library/cmath.rst b/Doc/library/cmath.rst
index e113ffc..c819a66 100644
--- a/Doc/library/cmath.rst
+++ b/Doc/library/cmath.rst
@@ -253,6 +253,40 @@ Constants
The mathematical constant *e*, as a float.
+.. data:: tau
+
+ The mathematical constant *τ*, as a float.
+
+ .. versionadded:: 3.6
+
+.. data:: inf
+
+ Floating-point positive infinity. Equivalent to ``float('inf')``.
+
+ .. versionadded:: 3.6
+
+.. data:: infj
+
+ Complex number with zero real part and positive infinity imaginary
+ part. Equivalent to ``complex(0.0, float('inf'))``.
+
+ .. versionadded:: 3.6
+
+.. data:: nan
+
+ A floating-point "not a number" (NaN) value. Equivalent to
+ ``float('nan')``.
+
+ .. versionadded:: 3.6
+
+.. data:: nanj
+
+ Complex number with zero real part and NaN imaginary part. Equivalent to
+ ``complex(0.0, float('nan'))``.
+
+ .. versionadded:: 3.6
+
+
.. index:: module: math
Note that the selection of functions is similar, but not identical, to that in
@@ -276,5 +310,3 @@ cuts for numerical purposes, a good reference should be the following:
Kahan, W: Branch cuts for complex elementary functions; or, Much ado about
nothing's sign bit. In Iserles, A., and Powell, M. (eds.), The state of the art
in numerical analysis. Clarendon Press (1987) pp165--211.
-
-
diff --git a/Doc/library/code.rst b/Doc/library/code.rst
index 443af69..e2c47ba 100644
--- a/Doc/library/code.rst
+++ b/Doc/library/code.rst
@@ -30,15 +30,19 @@ build applications which provide an interactive interpreter prompt.
``sys.ps1`` and ``sys.ps2``, and input buffering.
-.. function:: interact(banner=None, readfunc=None, local=None)
+.. function:: interact(banner=None, readfunc=None, local=None, exitmsg=None)
Convenience function to run a read-eval-print loop. This creates a new
instance of :class:`InteractiveConsole` and sets *readfunc* to be used as
the :meth:`InteractiveConsole.raw_input` method, if provided. If *local* is
provided, it is passed to the :class:`InteractiveConsole` constructor for
use as the default namespace for the interpreter loop. The :meth:`interact`
- method of the instance is then run with *banner* passed as the banner to
- use, if provided. The console object is discarded after use.
+ method of the instance is then run with *banner* and *exitmsg* passed as the
+ banner and exit message to use, if provided. The console object is discarded
+ after use.
+
+ .. versionchanged:: 3.6
+ Added *exitmsg* parameter.
.. function:: compile_command(source, filename="<input>", symbol="single")
@@ -136,7 +140,7 @@ The :class:`InteractiveConsole` class is a subclass of
interpreter objects as well as the following additions.
-.. method:: InteractiveConsole.interact(banner=None)
+.. method:: InteractiveConsole.interact(banner=None, exitmsg=None)
Closely emulate the interactive Python console. The optional *banner* argument
specify the banner to print before the first interaction; by default it prints a
@@ -144,9 +148,16 @@ interpreter objects as well as the following additions.
by the class name of the console object in parentheses (so as not to confuse
this with the real interpreter -- since it's so close!).
+ The optional *exitmsg* argument specifies an exit message printed when exiting.
+ Pass the empty string to suppress the exit message. If *exitmsg* is not given or
+ ``None``, a default message is printed.
+
.. versionchanged:: 3.4
To suppress printing any banner, pass an empty string.
+ .. versionchanged:: 3.6
+ Print an exit message when exiting.
+
.. method:: InteractiveConsole.push(line)
diff --git a/Doc/library/codecs.rst b/Doc/library/codecs.rst
index f38e41b..0ecd89b 100644
--- a/Doc/library/codecs.rst
+++ b/Doc/library/codecs.rst
@@ -1263,10 +1263,16 @@ encodings.
| | | Only ``errors='strict'`` |
| | | is supported. |
+--------------------+---------+---------------------------+
-| mbcs | dbcs | Windows only: Encode |
-| | | operand according to the |
+| mbcs | ansi, | Windows only: Encode |
+| | dbcs | operand according to the |
| | | ANSI codepage (CP_ACP) |
+--------------------+---------+---------------------------+
+| oem | | Windows only: Encode |
+| | | operand according to the |
+| | | OEM codepage (CP_OEMCP) |
+| | | |
+| | | .. versionadded:: 3.6 |
++--------------------+---------+---------------------------+
| palmos | | Encoding of PalmOS 3.5 |
+--------------------+---------+---------------------------+
| punycode | | Implements :rfc:`3492`. |
diff --git a/Doc/library/collections.abc.rst b/Doc/library/collections.abc.rst
index aeb6a73..58b03b9 100644
--- a/Doc/library/collections.abc.rst
+++ b/Doc/library/collections.abc.rst
@@ -41,13 +41,16 @@ ABC Inherits from Abstract Methods Mixin
:class:`Hashable` ``__hash__``
:class:`Iterable` ``__iter__``
:class:`Iterator` :class:`Iterable` ``__next__`` ``__iter__``
+:class:`Reversible` :class:`Iterable` ``__reversed__``
:class:`Generator` :class:`Iterator` ``send``, ``throw`` ``close``, ``__iter__``, ``__next__``
:class:`Sized` ``__len__``
:class:`Callable` ``__call__``
+:class:`Collection` :class:`Sized`, ``__contains__``,
+ :class:`Iterable`, ``__iter__``,
+ :class:`Container` ``__len__``
-:class:`Sequence` :class:`Sized`, ``__getitem__``, ``__contains__``, ``__iter__``, ``__reversed__``,
- :class:`Iterable`, ``__len__`` ``index``, and ``count``
- :class:`Container`
+:class:`Sequence` :class:`Reversible`, ``__getitem__``, ``__contains__``, ``__iter__``, ``__reversed__``,
+ :class:`Collection` ``__len__`` ``index``, and ``count``
:class:`MutableSequence` :class:`Sequence` ``__getitem__``, Inherited :class:`Sequence` methods and
``__setitem__``, ``append``, ``reverse``, ``extend``, ``pop``,
@@ -58,9 +61,9 @@ ABC Inherits from Abstract Methods Mixin
:class:`ByteString` :class:`Sequence` ``__getitem__``, Inherited :class:`Sequence` methods
``__len__``
-:class:`Set` :class:`Sized`, ``__contains__``, ``__le__``, ``__lt__``, ``__eq__``, ``__ne__``,
- :class:`Iterable`, ``__iter__``, ``__gt__``, ``__ge__``, ``__and__``, ``__or__``,
- :class:`Container` ``__len__`` ``__sub__``, ``__xor__``, and ``isdisjoint``
+:class:`Set` :class:`Collection` ``__contains__``, ``__le__``, ``__lt__``, ``__eq__``, ``__ne__``,
+ ``__iter__``, ``__gt__``, ``__ge__``, ``__and__``, ``__or__``,
+ ``__len__`` ``__sub__``, ``__xor__``, and ``isdisjoint``
:class:`MutableSet` :class:`Set` ``__contains__``, Inherited :class:`Set` methods and
``__iter__``, ``clear``, ``pop``, ``remove``, ``__ior__``,
@@ -68,9 +71,9 @@ ABC Inherits from Abstract Methods Mixin
``add``,
``discard``
-:class:`Mapping` :class:`Sized`, ``__getitem__``, ``__contains__``, ``keys``, ``items``, ``values``,
- :class:`Iterable`, ``__iter__``, ``get``, ``__eq__``, and ``__ne__``
- :class:`Container` ``__len__``
+:class:`Mapping` :class:`Collection` ``__getitem__``, ``__contains__``, ``keys``, ``items``, ``values``,
+ ``__iter__``, ``get``, ``__eq__``, and ``__ne__``
+ ``__len__``
:class:`MutableMapping` :class:`Mapping` ``__getitem__``, Inherited :class:`Mapping` methods and
``__setitem__``, ``pop``, ``popitem``, ``clear``, ``update``,
@@ -89,6 +92,7 @@ ABC Inherits from Abstract Methods Mixin
:class:`Coroutine` :class:`Awaitable` ``send``, ``throw`` ``close``
:class:`AsyncIterable` ``__aiter__``
:class:`AsyncIterator` :class:`AsyncIterable` ``__anext__`` ``__aiter__``
+:class:`AsyncGenerator` :class:`AsyncIterator` ``asend``, ``athrow`` ``aclose``, ``__aiter__``, ``__anext__``
========================== ====================== ======================= ====================================================
@@ -105,12 +109,25 @@ ABC Inherits from Abstract Methods Mixin
ABC for classes that provide the :meth:`__iter__` method.
See also the definition of :term:`iterable`.
+.. class:: Collection
+
+ ABC for sized iterable container classes.
+
+ .. versionadded:: 3.6
+
.. class:: Iterator
ABC for classes that provide the :meth:`~iterator.__iter__` and
:meth:`~iterator.__next__` methods. See also the definition of
:term:`iterator`.
+.. class:: Reversible
+
+ ABC for iterable classes that also provide the :meth:`__reversed__`
+ method.
+
+ .. versionadded:: 3.6
+
.. class:: Generator
ABC for generator classes that implement the protocol defined in
@@ -206,6 +223,13 @@ ABC Inherits from Abstract Methods Mixin
.. versionadded:: 3.5
+.. class:: AsyncGenerator
+
+ ABC for asynchronous generator classes that implement the protocol
+ defined in :pep:`525` and :pep:`492`.
+
+ .. versionadded:: 3.6
+
These ABCs allow us to ask classes or instances if they provide
particular functionality, for example::
diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst
index d0aa62e..8e2eb4d 100644
--- a/Doc/library/collections.rst
+++ b/Doc/library/collections.rst
@@ -763,7 +763,7 @@ Named tuples assign meaning to each position in a tuple and allow for more reada
self-documenting code. They can be used wherever regular tuples are used, and
they add the ability to access fields by name instead of position index.
-.. function:: namedtuple(typename, field_names, verbose=False, rename=False)
+.. function:: namedtuple(typename, field_names, *, verbose=False, rename=False, module=None)
Returns a new tuple subclass named *typename*. The new subclass is used to
create tuple-like objects that have fields accessible by attribute lookup as
@@ -790,12 +790,21 @@ they add the ability to access fields by name instead of position index.
built. This option is outdated; instead, it is simpler to print the
:attr:`_source` attribute.
+ If *module* is defined, the ``__module__`` attribute of the named tuple is
+ set to that value.
+
Named tuple instances do not have per-instance dictionaries, so they are
lightweight and require no more memory than regular tuples.
.. versionchanged:: 3.1
- Added support for *rename*.
+ Added support for *rename*.
+
+ .. versionchanged:: 3.6
+ The *verbose* and *rename* parameters became
+ :ref:`keyword-only arguments <keyword-only_parameter>`.
+ .. versionchanged:: 3.6
+ Added the *module* parameter.
.. doctest::
:options: +NORMALIZE_WHITESPACE
@@ -846,7 +855,9 @@ field names, the method and attribute names start with an underscore.
.. method:: somenamedtuple._asdict()
Return a new :class:`OrderedDict` which maps field names to their corresponding
- values::
+ values:
+
+ .. doctest::
>>> p = Point(x=11, y=22)
>>> p._asdict()
@@ -908,7 +919,9 @@ Since a named tuple is a regular Python class, it is easy to add or change
functionality with a subclass. Here is how to add a calculated field and
a fixed-width print format:
- >>> class Point(namedtuple('Point', 'x y')):
+.. doctest::
+
+ >>> class Point(namedtuple('Point', ['x', 'y'])):
... __slots__ = ()
... @property
... def hypot(self):
@@ -959,6 +972,11 @@ customize a prototype instance:
constructor that is convenient for use cases where named tuples are being
subclassed.
+ * See :meth:`types.SimpleNamespace` for a mutable namespace based on an
+ underlying dictionary instead of a tuple.
+
+ * See :meth:`typing.NamedTuple` for a way to add type hints for named tuples.
+
:class:`OrderedDict` objects
----------------------------
@@ -980,8 +998,9 @@ the items are returned in the order their keys were first added.
.. method:: popitem(last=True)
The :meth:`popitem` method for ordered dictionaries returns and removes a
- (key, value) pair. The pairs are returned in LIFO order if *last* is true
- or FIFO order if false.
+ (key, value) pair. The pairs are returned in
+ :abbr:`LIFO (last-in, first-out)` order if *last* is true
+ or :abbr:`FIFO (first-in, first-out)` order if false.
.. method:: move_to_end(key, last=True)
@@ -1010,14 +1029,15 @@ Equality tests between :class:`OrderedDict` objects and other
dictionaries. This allows :class:`OrderedDict` objects to be substituted
anywhere a regular dictionary is used.
-The :class:`OrderedDict` constructor and :meth:`update` method both accept
-keyword arguments, but their order is lost because Python's function call
-semantics pass in keyword arguments using a regular unordered dictionary.
-
.. versionchanged:: 3.5
The items, keys, and values :term:`views <dictionary view>`
of :class:`OrderedDict` now support reverse iteration using :func:`reversed`.
+.. versionchanged:: 3.6
+ With the acceptance of :pep:`468`, order is retained for keyword arguments
+ passed to the :class:`OrderedDict` constructor and its :meth:`update`
+ method.
+
:class:`OrderedDict` Examples and Recipes
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/Doc/library/compileall.rst b/Doc/library/compileall.rst
index 511c581..c1af02b 100644
--- a/Doc/library/compileall.rst
+++ b/Doc/library/compileall.rst
@@ -102,7 +102,8 @@ Public functions
.. function:: compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None, quiet=0, legacy=False, optimize=-1, workers=1)
Recursively descend the directory tree named by *dir*, compiling all :file:`.py`
- files along the way.
+ files along the way. Return a true value if all the files compiled successfully,
+ and a false value otherwise.
The *maxlevels* parameter is used to limit the depth of the recursion; it
defaults to ``10``.
@@ -152,9 +153,13 @@ Public functions
The *legacy* parameter only writes out ``.pyc`` files, not ``.pyo`` files
no matter what the value of *optimize* is.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: compile_file(fullname, ddir=None, force=False, rx=None, quiet=0, legacy=False, optimize=-1)
- Compile the file with path *fullname*.
+ Compile the file with path *fullname*. Return a true value if the file
+ compiled successfully, and a false value otherwise.
If *ddir* is given, it is prepended to the path to the file being compiled
for use in compilation time tracebacks, and is also compiled in to the
@@ -190,8 +195,10 @@ Public functions
.. function:: compile_path(skip_curdir=True, maxlevels=0, force=False, quiet=0, legacy=False, optimize=-1)
- Byte-compile all the :file:`.py` files found along ``sys.path``. If
- *skip_curdir* is true (the default), the current directory is not included
+ Byte-compile all the :file:`.py` files found along ``sys.path``. Return a
+ true value if all the files compiled successfully, and a false value otherwise.
+
+ If *skip_curdir* is true (the default), the current directory is not included
in the search. All other parameters are passed to the :func:`compile_dir`
function. Note that unlike the other compile functions, ``maxlevels``
defaults to ``0``.
@@ -217,6 +224,9 @@ subdirectory and all its subdirectories::
import re
compileall.compile_dir('Lib/', rx=re.compile(r'[/\\][.]svn'), force=True)
+ # pathlib.Path objects can also be used.
+ import pathlib
+ compileall.compile_dir(pathlib.Path('Lib/'), force=True)
.. seealso::
diff --git a/Doc/library/concurrent.futures.rst b/Doc/library/concurrent.futures.rst
index ae03f4b..d85576b 100644
--- a/Doc/library/concurrent.futures.rst
+++ b/Doc/library/concurrent.futures.rst
@@ -124,7 +124,7 @@ And::
executor.submit(wait_on_future)
-.. class:: ThreadPoolExecutor(max_workers=None)
+.. class:: ThreadPoolExecutor(max_workers=None, thread_name_prefix='')
An :class:`Executor` subclass that uses a pool of at most *max_workers*
threads to execute calls asynchronously.
@@ -137,6 +137,10 @@ And::
should be higher than the number of workers
for :class:`ProcessPoolExecutor`.
+ .. versionadded:: 3.6
+ The *thread_name_prefix* argument was added to allow users to
+ control the threading.Thread names for worker threads created by
+ the pool for easier debugging.
.. _threadpoolexecutor-example:
diff --git a/Doc/library/constants.rst b/Doc/library/constants.rst
index d5a0f09..f0742ce 100644
--- a/Doc/library/constants.rst
+++ b/Doc/library/constants.rst
@@ -33,16 +33,22 @@ A small number of constants live in the built-in namespace. They are:
(e.g. :meth:`__imul__`, :meth:`__iand__`, etc.) for the same purpose.
Its truth value is true.
-.. note::
+ .. note::
+
+ When a binary (or in-place) method returns ``NotImplemented`` the
+ interpreter will try the reflected operation on the other type (or some
+ other fallback, depending on the operator). If all attempts return
+ ``NotImplemented``, the interpreter will raise an appropriate exception.
+ Incorrectly returning ``NotImplemented`` will result in a misleading
+ error message or the ``NotImplemented`` value being returned to Python code.
+
+ See :ref:`implementing-the-arithmetic-operations` for examples.
- When ``NotImplemented`` is returned, the interpreter will then try the
- reflected operation on the other type, or some other fallback, depending
- on the operator. If all attempted operations return ``NotImplemented``, the
- interpreter will raise an appropriate exception.
+ .. note::
- See
- :ref:`implementing-the-arithmetic-operations`
- for more details.
+ ``NotImplentedError`` and ``NotImplemented`` are not interchangeable,
+ even though they have similar names and purposes.
+ See :exc:`NotImplementedError` for details on when to use it.
.. data:: Ellipsis
diff --git a/Doc/library/contextlib.rst b/Doc/library/contextlib.rst
index cf85fcd..dd34c96 100644
--- a/Doc/library/contextlib.rst
+++ b/Doc/library/contextlib.rst
@@ -18,6 +18,18 @@ Utilities
Functions and classes provided:
+.. class:: AbstractContextManager
+
+ An :term:`abstract base class` for classes that implement
+ :meth:`object.__enter__` and :meth:`object.__exit__`. A default
+ implementation for :meth:`object.__enter__` is provided which returns
+ ``self`` while :meth:`object.__exit__` is an abstract method which by default
+ returns ``None``. See also the definition of :ref:`typecontextmanager`.
+
+ .. versionadded:: 3.6
+
+
+
.. decorator:: contextmanager
This function is a :term:`decorator` that can be used to define a factory
@@ -447,9 +459,9 @@ Here's an example of doing this for a context manager that accepts resource
acquisition and release functions, along with an optional validation function,
and maps them to the context management protocol::
- from contextlib import contextmanager, ExitStack
+ from contextlib import contextmanager, AbstractContextManager, ExitStack
- class ResourceManager:
+ class ResourceManager(AbstractContextManager):
def __init__(self, acquire_resource, release_resource, check_resource_ok=None):
self.acquire_resource = acquire_resource
@@ -578,10 +590,10 @@ single definition::
self.name = name
def __enter__(self):
- logging.info('Entering: {}'.format(self.name))
+ logging.info('Entering: %s', self.name)
def __exit__(self, exc_type, exc, exc_tb):
- logging.info('Exiting: {}'.format(self.name))
+ logging.info('Exiting: %s', self.name)
Instances of this class can be used as both a context manager::
diff --git a/Doc/library/crypt.rst b/Doc/library/crypt.rst
index a21c1e7..dbd4274 100644
--- a/Doc/library/crypt.rst
+++ b/Doc/library/crypt.rst
@@ -68,7 +68,7 @@ Module Attributes
A list of available password hashing algorithms, as
``crypt.METHOD_*`` objects. This list is sorted from strongest to
- weakest, and is guaranteed to have at least ``crypt.METHOD_CRYPT``.
+ weakest.
Module Functions
diff --git a/Doc/library/crypto.rst b/Doc/library/crypto.rst
index 1eddfdc..ae45549 100644
--- a/Doc/library/crypto.rst
+++ b/Doc/library/crypto.rst
@@ -16,3 +16,4 @@ Here's an overview:
hashlib.rst
hmac.rst
+ secrets.rst
diff --git a/Doc/library/csv.rst b/Doc/library/csv.rst
index 7fb4fc8..52a8a31 100644
--- a/Doc/library/csv.rst
+++ b/Doc/library/csv.rst
@@ -146,21 +146,28 @@ The :mod:`csv` module defines the following functions:
The :mod:`csv` module defines the following classes:
-.. class:: DictReader(csvfile, fieldnames=None, restkey=None, restval=None, \
+.. class:: DictReader(f, fieldnames=None, restkey=None, restval=None, \
dialect='excel', *args, **kwds)
- Create an object which operates like a regular reader but maps the
- information read into a dict whose keys are given by the optional
- *fieldnames* parameter. The *fieldnames* parameter is a :mod:`sequence
- <collections.abc>` whose elements are associated with the fields of the
- input data in order. These elements become the keys of the resulting
- dictionary. If the *fieldnames* parameter is omitted, the values in the
- first row of the *csvfile* will be used as the fieldnames. If the row read
- has more fields than the fieldnames sequence, the remaining data is added as
- a sequence keyed by the value of *restkey*. If the row read has fewer
- fields than the fieldnames sequence, the remaining keys take the value of
- the optional *restval* parameter. Any other optional or keyword arguments
- are passed to the underlying :class:`reader` instance.
+ Create an object that operates like a regular reader but maps the
+ information in each row to an :mod:`OrderedDict <collections.OrderedDict>`
+ whose keys are given by the optional *fieldnames* parameter.
+
+ The *fieldnames* parameter is a :term:`sequence`. If *fieldnames* is
+ omitted, the values in the first row of file *f* will be used as the
+ fieldnames. Regardless of how the fieldnames are determined, the ordered
+ dictionary preserves their original ordering.
+
+ If a row has more fields than fieldnames, the remaining data is put in a
+ list and stored with the fieldname specified by *restkey* (which defaults
+ to ``None``). If a non-blank row has fewer fields than fieldnames, the
+ missing values are filled-in with ``None``.
+
+ All other optional or keyword arguments are passed to the underlying
+ :class:`reader` instance.
+
+ .. versionchanged:: 3.6
+ Returned rows are now of type :class:`OrderedDict`.
A short usage example::
@@ -170,31 +177,35 @@ The :mod:`csv` module defines the following classes:
... for row in reader:
... print(row['first_name'], row['last_name'])
...
- Baked Beans
- Lovely Spam
- Wonderful Spam
+ Eric Idle
+ John Cleese
+
+ >>> print(row)
+ OrderedDict([('first_name', 'John'), ('last_name', 'Cleese')])
-.. class:: DictWriter(csvfile, fieldnames, restval='', extrasaction='raise', \
+.. class:: DictWriter(f, fieldnames, restval='', extrasaction='raise', \
dialect='excel', *args, **kwds)
Create an object which operates like a regular writer but maps dictionaries
onto output rows. The *fieldnames* parameter is a :mod:`sequence
<collections.abc>` of keys that identify the order in which values in the
- dictionary passed to the :meth:`writerow` method are written to the
- *csvfile*. The optional *restval* parameter specifies the value to be
+ dictionary passed to the :meth:`writerow` method are written to file
+ *f*. The optional *restval* parameter specifies the value to be
written if the dictionary is missing a key in *fieldnames*. If the
dictionary passed to the :meth:`writerow` method contains a key not found in
*fieldnames*, the optional *extrasaction* parameter indicates what action to
- take. If it is set to ``'raise'`` a :exc:`ValueError` is raised. If it is
- set to ``'ignore'``, extra values in the dictionary are ignored. Any other
- optional or keyword arguments are passed to the underlying :class:`writer`
- instance.
+ take.
+ If it is set to ``'raise'``, the default value, a :exc:`ValueError`
+ is raised.
+ If it is set to ``'ignore'``, extra values in the dictionary are ignored.
+ Any other optional or keyword arguments are passed to the underlying
+ :class:`writer` instance.
Note that unlike the :class:`DictReader` class, the *fieldnames* parameter
of the :class:`DictWriter` is not optional. Since Python's :class:`dict`
objects are not ordered, there is not enough information available to deduce
- the order in which the row should be written to the *csvfile*.
+ the order in which the row should be written to file *f*.
A short usage example::
diff --git a/Doc/library/ctypes.rst b/Doc/library/ctypes.rst
index b25fbbb..3840935 100644
--- a/Doc/library/ctypes.rst
+++ b/Doc/library/ctypes.rst
@@ -1239,9 +1239,10 @@ When programming in a compiled language, shared libraries are accessed when
compiling/linking a program, and when the program is run.
The purpose of the :func:`find_library` function is to locate a library in a way
-similar to what the compiler does (on platforms with several versions of a
-shared library the most recent should be loaded), while the ctypes library
-loaders act like when a program is run, and call the runtime loader directly.
+similar to what the compiler or runtime loader does (on platforms with several
+versions of a shared library the most recent should be loaded), while the ctypes
+library loaders act like when a program is run, and call the runtime loader
+directly.
The :mod:`ctypes.util` module provides a function which can help to determine
the library to load.
@@ -1259,8 +1260,14 @@ the library to load.
The exact functionality is system dependent.
On Linux, :func:`find_library` tries to run external programs
-(``/sbin/ldconfig``, ``gcc``, and ``objdump``) to find the library file. It
-returns the filename of the library file. Here are some examples::
+(``/sbin/ldconfig``, ``gcc``, ``objdump`` and ``ld``) to find the library file.
+It returns the filename of the library file.
+
+.. versionchanged:: 3.6
+ On Linux, the value of the environment variable ``LD_LIBRARY_PATH`` is used
+ when searching for libraries, if a library cannot be found by any other means.
+
+Here are some examples::
>>> from ctypes.util import find_library
>>> find_library("m")
diff --git a/Doc/library/datetime.rst b/Doc/library/datetime.rst
index 4f3cfe3..c931855 100644
--- a/Doc/library/datetime.rst
+++ b/Doc/library/datetime.rst
@@ -522,7 +522,7 @@ objects are considered to be true.
Instance methods:
-.. method:: date.replace(year, month, day)
+.. method:: date.replace(year=self.year, month=self.month, day=self.day)
Return a date with the same value, except for those parameters given new
values by whichever keyword arguments are specified. For example, if ``d ==
@@ -610,7 +610,8 @@ Instance methods:
.. method:: date.__format__(format)
Same as :meth:`.date.strftime`. This makes it possible to specify a format
- string for a :class:`.date` object when using :meth:`str.format`. For a
+ string for a :class:`.date` object in :ref:`formatted string
+ literals <f-strings>` and when using :meth:`str.format`. For a
complete list of formatting directives, see
:ref:`strftime-strptime-behavior`.
@@ -682,22 +683,26 @@ both directions; like a time object, :class:`.datetime` assumes there are exactl
Constructor:
-.. class:: datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None)
+.. class:: datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)
The year, month and day arguments are required. *tzinfo* may be ``None``, or an
instance of a :class:`tzinfo` subclass. The remaining arguments may be integers,
in the following ranges:
- * ``MINYEAR <= year <= MAXYEAR``
- * ``1 <= month <= 12``
- * ``1 <= day <= number of days in the given month and year``
- * ``0 <= hour < 24``
- * ``0 <= minute < 60``
- * ``0 <= second < 60``
- * ``0 <= microsecond < 1000000``
+ * ``MINYEAR <= year <= MAXYEAR``,
+ * ``1 <= month <= 12``,
+ * ``1 <= day <= number of days in the given month and year``,
+ * ``0 <= hour < 24``,
+ * ``0 <= minute < 60``,
+ * ``0 <= second < 60``,
+ * ``0 <= microsecond < 1000000``,
+ * ``fold in [0, 1]``.
If an argument outside those ranges is given, :exc:`ValueError` is raised.
+ .. versionadded:: 3.6
+ Added the ``fold`` argument.
+
Other constructors, all class methods:
.. classmethod:: datetime.today()
@@ -757,6 +762,8 @@ Other constructors, all class methods:
instead of :exc:`ValueError` on :c:func:`localtime` or :c:func:`gmtime`
failure.
+ .. versionchanged:: 3.6
+ :meth:`fromtimestamp` may return instances with :attr:`.fold` set to 1.
.. classmethod:: datetime.utcfromtimestamp(timestamp)
@@ -793,16 +800,23 @@ Other constructors, all class methods:
microsecond of the result are all 0, and :attr:`.tzinfo` is ``None``.
-.. classmethod:: datetime.combine(date, time)
+.. classmethod:: datetime.combine(date, time, tzinfo=self.tzinfo)
Return a new :class:`.datetime` object whose date components are equal to the
- given :class:`date` object's, and whose time components and :attr:`.tzinfo`
- attributes are equal to the given :class:`.time` object's. For any
- :class:`.datetime` object *d*,
- ``d == datetime.combine(d.date(), d.timetz())``. If date is a
+ given :class:`date` object's, and whose time components
+ are equal to the given :class:`.time` object's. If the *tzinfo*
+ argument is provided, its value is used to set the :attr:`.tzinfo` attribute
+ of the result, otherwise the :attr:`~.time.tzinfo` attribute of the *time* argument
+ is used.
+
+ For any :class:`.datetime` object *d*,
+ ``d == datetime.combine(d.date(), d.time(), d.tzinfo)``. If date is a
:class:`.datetime` object, its time components and :attr:`.tzinfo` attributes
are ignored.
+ .. versionchanged:: 3.6
+ Added the *tzinfo* argument.
+
.. classmethod:: datetime.strptime(date_string, format)
@@ -878,6 +892,16 @@ Instance attributes (read-only):
or ``None`` if none was passed.
+.. attribute:: datetime.fold
+
+ In ``[0, 1]``. Used to disambiguate wall times during a repeated interval. (A
+ repeated interval occurs when clocks are rolled back at the end of daylight saving
+ time or when the UTC offset for the current zone is decreased for political reasons.)
+ The value 0 (1) represents the earlier (later) of the two moments with the same wall
+ time representation.
+
+ .. versionadded:: 3.6
+
Supported operations:
+---------------------------------------+--------------------------------+
@@ -966,23 +990,34 @@ Instance methods:
.. method:: datetime.time()
- Return :class:`.time` object with same hour, minute, second and microsecond.
+ Return :class:`.time` object with same hour, minute, second, microsecond and fold.
:attr:`.tzinfo` is ``None``. See also method :meth:`timetz`.
+ .. versionchanged:: 3.6
+ The fold value is copied to the returned :class:`.time` object.
+
.. method:: datetime.timetz()
- Return :class:`.time` object with same hour, minute, second, microsecond, and
+ Return :class:`.time` object with same hour, minute, second, microsecond, fold, and
tzinfo attributes. See also method :meth:`time`.
+ .. versionchanged:: 3.6
+ The fold value is copied to the returned :class:`.time` object.
-.. method:: datetime.replace([year[, month[, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]]]]])
+
+.. method:: datetime.replace(year=self.year, month=self.month, day=self.day, \
+ hour=self.hour, minute=self.minute, second=self.second, microsecond=self.microsecond, \
+ tzinfo=self.tzinfo, * fold=0)
Return a datetime with the same attributes, except for those attributes given
new values by whichever keyword arguments are specified. Note that
``tzinfo=None`` can be specified to create a naive datetime from an aware
datetime with no conversion of date and time data.
+ .. versionadded:: 3.6
+ Added the ``fold`` argument.
+
.. method:: datetime.astimezone(tz=None)
@@ -991,23 +1026,20 @@ Instance methods:
*self*, but in *tz*'s local time.
If provided, *tz* must be an instance of a :class:`tzinfo` subclass, and its
- :meth:`utcoffset` and :meth:`dst` methods must not return ``None``. *self* must
- be aware (``self.tzinfo`` must not be ``None``, and ``self.utcoffset()`` must
- not return ``None``).
+ :meth:`utcoffset` and :meth:`dst` methods must not return ``None``. If *self*
+ is naive (``self.tzinfo is None``), it is presumed to represent time in the
+ system timezone.
If called without arguments (or with ``tz=None``) the system local
- timezone is assumed. The ``.tzinfo`` attribute of the converted
+ timezone is assumed for the target timezone. The ``.tzinfo`` attribute of the converted
datetime instance will be set to an instance of :class:`timezone`
with the zone name and offset obtained from the OS.
If ``self.tzinfo`` is *tz*, ``self.astimezone(tz)`` is equal to *self*: no
adjustment of date or time data is performed. Else the result is local
- time in time zone *tz*, representing the same UTC time as *self*: after
- ``astz = dt.astimezone(tz)``, ``astz - astz.utcoffset()`` will usually have
- the same date and time data as ``dt - dt.utcoffset()``. The discussion
- of class :class:`tzinfo` explains the cases at Daylight Saving Time transition
- boundaries where this cannot be achieved (an issue only if *tz* models both
- standard and daylight time).
+ time in the timezone *tz*, representing the same UTC time as *self*: after
+ ``astz = dt.astimezone(tz)``, ``astz - astz.utcoffset()`` will have
+ the same date and time data as ``dt - dt.utcoffset()``.
If you merely want to attach a time zone object *tz* to a datetime *dt* without
adjustment of date and time data, use ``dt.replace(tzinfo=tz)``. If you
@@ -1029,6 +1061,10 @@ Instance methods:
.. versionchanged:: 3.3
*tz* now can be omitted.
+ .. versionchanged:: 3.6
+ The :meth:`astimezone` method can now be called on naive instances that
+ are presumed to represent system local time.
+
.. method:: datetime.utcoffset()
@@ -1105,6 +1141,10 @@ Instance methods:
.. versionadded:: 3.3
+ .. versionchanged:: 3.6
+ The :meth:`timestamp` method uses the :attr:`.fold` attribute to
+ disambiguate the times during a repeated interval.
+
.. note::
There is no method to obtain the POSIX timestamp directly from a
@@ -1138,7 +1178,7 @@ Instance methods:
``self.date().isocalendar()``.
-.. method:: datetime.isoformat(sep='T')
+.. method:: datetime.isoformat(sep='T', timespec='auto')
Return a string representing the date and time in ISO 8601 format,
YYYY-MM-DDTHH:MM:SS.mmmmmm or, if :attr:`microsecond` is 0,
@@ -1159,6 +1199,37 @@ Instance methods:
>>> datetime(2002, 12, 25, tzinfo=TZ()).isoformat(' ')
'2002-12-25 00:00:00-06:39'
+ The optional argument *timespec* specifies the number of additional
+ components of the time to include (the default is ``'auto'``).
+ It can be one of the following:
+
+ - ``'auto'``: Same as ``'seconds'`` if :attr:`microsecond` is 0,
+ same as ``'microseconds'`` otherwise.
+ - ``'hours'``: Include the :attr:`hour` in the two-digit HH format.
+ - ``'minutes'``: Include :attr:`hour` and :attr:`minute` in HH:MM format.
+ - ``'seconds'``: Include :attr:`hour`, :attr:`minute`, and :attr:`second`
+ in HH:MM:SS format.
+ - ``'milliseconds'``: Include full time, but truncate fractional second
+ part to milliseconds. HH:MM:SS.sss format.
+ - ``'microseconds'``: Include full time in HH:MM:SS.mmmmmm format.
+
+ .. note::
+
+ Excluded time components are truncated, not rounded.
+
+ :exc:`ValueError` will be raised on an invalid *timespec* argument.
+
+
+ >>> from datetime import datetime
+ >>> datetime.now().isoformat(timespec='minutes')
+ '2002-12-25T00:00'
+ >>> dt = datetime(2015, 1, 1, 12, 30, 59, 0)
+ >>> dt.isoformat(timespec='microseconds')
+ '2015-01-01T12:30:59.000000'
+
+ .. versionadded:: 3.6
+ Added the *timespec* argument.
+
.. method:: datetime.__str__()
@@ -1185,7 +1256,8 @@ Instance methods:
.. method:: datetime.__format__(format)
Same as :meth:`.datetime.strftime`. This makes it possible to specify a format
- string for a :class:`.datetime` object when using :meth:`str.format`. For a
+ string for a :class:`.datetime` object in :ref:`formatted string
+ literals <f-strings>` and when using :meth:`str.format`. For a
complete list of formatting directives, see
:ref:`strftime-strptime-behavior`.
@@ -1302,16 +1374,17 @@ Using datetime with tzinfo:
A time object represents a (local) time of day, independent of any particular
day, and subject to adjustment via a :class:`tzinfo` object.
-.. class:: time(hour=0, minute=0, second=0, microsecond=0, tzinfo=None)
+.. class:: time(hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)
All arguments are optional. *tzinfo* may be ``None``, or an instance of a
:class:`tzinfo` subclass. The remaining arguments may be integers, in the
following ranges:
- * ``0 <= hour < 24``
- * ``0 <= minute < 60``
- * ``0 <= second < 60``
- * ``0 <= microsecond < 1000000``.
+ * ``0 <= hour < 24``,
+ * ``0 <= minute < 60``,
+ * ``0 <= second < 60``,
+ * ``0 <= microsecond < 1000000``,
+ * ``fold in [0, 1]``.
If an argument outside those ranges is given, :exc:`ValueError` is raised. All
default to ``0`` except *tzinfo*, which defaults to :const:`None`.
@@ -1364,6 +1437,17 @@ Instance attributes (read-only):
``None`` if none was passed.
+.. attribute:: time.fold
+
+ In ``[0, 1]``. Used to disambiguate wall times during a repeated interval. (A
+ repeated interval occurs when clocks are rolled back at the end of daylight saving
+ time or when the UTC offset for the current zone is decreased for political reasons.)
+ The value 0 (1) represents the earlier (later) of the two moments with the same wall
+ time representation.
+
+ .. versionadded:: 3.6
+
+
Supported operations:
* comparison of :class:`.time` to :class:`.time`, where *a* is considered less
@@ -1399,21 +1483,58 @@ In boolean contexts, a :class:`.time` object is always considered to be true.
Instance methods:
-.. method:: time.replace([hour[, minute[, second[, microsecond[, tzinfo]]]]])
+.. method:: time.replace(hour=self.hour, minute=self.minute, second=self.second, \
+ microsecond=self.microsecond, tzinfo=self.tzinfo, * fold=0)
Return a :class:`.time` with the same value, except for those attributes given
new values by whichever keyword arguments are specified. Note that
``tzinfo=None`` can be specified to create a naive :class:`.time` from an
aware :class:`.time`, without conversion of the time data.
+ .. versionadded:: 3.6
+ Added the ``fold`` argument.
+
-.. method:: time.isoformat()
+.. method:: time.isoformat(timespec='auto')
Return a string representing the time in ISO 8601 format, HH:MM:SS.mmmmmm or, if
- self.microsecond is 0, HH:MM:SS If :meth:`utcoffset` does not return ``None``, a
+ :attr:`microsecond` is 0, HH:MM:SS If :meth:`utcoffset` does not return ``None``, a
6-character string is appended, giving the UTC offset in (signed) hours and
minutes: HH:MM:SS.mmmmmm+HH:MM or, if self.microsecond is 0, HH:MM:SS+HH:MM
+ The optional argument *timespec* specifies the number of additional
+ components of the time to include (the default is ``'auto'``).
+ It can be one of the following:
+
+ - ``'auto'``: Same as ``'seconds'`` if :attr:`microsecond` is 0,
+ same as ``'microseconds'`` otherwise.
+ - ``'hours'``: Include the :attr:`hour` in the two-digit HH format.
+ - ``'minutes'``: Include :attr:`hour` and :attr:`minute` in HH:MM format.
+ - ``'seconds'``: Include :attr:`hour`, :attr:`minute`, and :attr:`second`
+ in HH:MM:SS format.
+ - ``'milliseconds'``: Include full time, but truncate fractional second
+ part to milliseconds. HH:MM:SS.sss format.
+ - ``'microseconds'``: Include full time in HH:MM:SS.mmmmmm format.
+
+ .. note::
+
+ Excluded time components are truncated, not rounded.
+
+ :exc:`ValueError` will be raised on an invalid *timespec* argument.
+
+
+ >>> from datetime import time
+ >>> time(hour=12, minute=34, second=56, microsecond=123456).isoformat(timespec='minutes')
+ '12:34'
+ >>> dt = time(hour=12, minute=34, second=56, microsecond=0)
+ >>> dt.isoformat(timespec='microseconds')
+ '12:34:56.000000'
+ >>> dt.isoformat(timespec='auto')
+ '12:34:56'
+
+ .. versionadded:: 3.6
+ Added the *timespec* argument.
+
.. method:: time.__str__()
@@ -1430,7 +1551,8 @@ Instance methods:
.. method:: time.__format__(format)
Same as :meth:`.time.strftime`. This makes it possible to specify a format string
- for a :class:`.time` object when using :meth:`str.format`. For a
+ for a :class:`.time` object in :ref:`formatted string
+ literals <f-strings>` and when using :meth:`str.format`. For a
complete list of formatting directives, see
:ref:`strftime-strptime-behavior`.
@@ -1680,9 +1802,19 @@ minute after 1:59 (EST) on the second Sunday in March, and ends the minute after
When DST starts (the "start" line), the local wall clock leaps from 1:59 to
3:00. A wall time of the form 2:MM doesn't really make sense on that day, so
``astimezone(Eastern)`` won't deliver a result with ``hour == 2`` on the day DST
-begins. In order for :meth:`astimezone` to make this guarantee, the
-:meth:`tzinfo.dst` method must consider times in the "missing hour" (2:MM for
-Eastern) to be in daylight time.
+begins. For example, at the Spring forward transition of 2016, we get
+
+ >>> u0 = datetime(2016, 3, 13, 5, tzinfo=timezone.utc)
+ >>> for i in range(4):
+ ... u = u0 + i*HOUR
+ ... t = u.astimezone(Eastern)
+ ... print(u.time(), 'UTC =', t.time(), t.tzname())
+ ...
+ 05:00:00 UTC = 00:00:00 EST
+ 06:00:00 UTC = 01:00:00 EST
+ 07:00:00 UTC = 03:00:00 EDT
+ 08:00:00 UTC = 04:00:00 EDT
+
When DST ends (the "end" line), there's a potentially worse problem: there's an
hour that can't be spelled unambiguously in local wall time: the last hour of
@@ -1691,28 +1823,41 @@ daylight time ends. The local wall clock leaps from 1:59 (daylight time) back
to 1:00 (standard time) again. Local times of the form 1:MM are ambiguous.
:meth:`astimezone` mimics the local clock's behavior by mapping two adjacent UTC
hours into the same local hour then. In the Eastern example, UTC times of the
-form 5:MM and 6:MM both map to 1:MM when converted to Eastern. In order for
-:meth:`astimezone` to make this guarantee, the :meth:`tzinfo.dst` method must
-consider times in the "repeated hour" to be in standard time. This is easily
-arranged, as in the example, by expressing DST switch times in the time zone's
-standard local time.
+form 5:MM and 6:MM both map to 1:MM when converted to Eastern, but earlier times
+have the :attr:`~datetime.fold` attribute set to 0 and the later times have it set to 1.
+For example, at the Fall back transition of 2016, we get
+
+ >>> u0 = datetime(2016, 11, 6, 4, tzinfo=timezone.utc)
+ >>> for i in range(4):
+ ... u = u0 + i*HOUR
+ ... t = u.astimezone(Eastern)
+ ... print(u.time(), 'UTC =', t.time(), t.tzname(), t.fold)
+ ...
+ 04:00:00 UTC = 00:00:00 EDT 0
+ 05:00:00 UTC = 01:00:00 EDT 0
+ 06:00:00 UTC = 01:00:00 EST 1
+ 07:00:00 UTC = 02:00:00 EST 0
+
+Note that the :class:`datetime` instances that differ only by the value of the
+:attr:`~datetime.fold` attribute are considered equal in comparisons.
-Applications that can't bear such ambiguities should avoid using hybrid
+Applications that can't bear wall-time ambiguities should explicitly check the
+value of the :attr:`~datetime.fold` attribute or avoid using hybrid
:class:`tzinfo` subclasses; there are no ambiguities when using :class:`timezone`,
or any other fixed-offset :class:`tzinfo` subclass (such as a class representing
only EST (fixed offset -5 hours), or only EDT (fixed offset -4 hours)).
.. seealso::
- `pytz <https://pypi.python.org/pypi/pytz/>`_
+ `datetuil.tz <https://dateutil.readthedocs.io/en/stable/tz.html>`_
The standard library has :class:`timezone` class for handling arbitrary
fixed offsets from UTC and :attr:`timezone.utc` as UTC timezone instance.
- *pytz* library brings the *IANA timezone database* (also known as the
+ *datetuil.tz* library brings the *IANA timezone database* (also known as the
Olson database) to Python and its usage is recommended.
`IANA timezone database <https://www.iana.org/time-zones>`_
- The Time Zone Database (often called tz or zoneinfo) contains code and
+ The Time Zone Database (often called tz, tzdata or zoneinfo) contains code and
data that represent the history of local time for many representative
locations around the globe. It is updated periodically to reflect changes
made by political bodies to time zone boundaries, UTC offsets, and
@@ -1732,7 +1877,7 @@ in different days of the year or where historical changes have been
made to civil time.
-.. class:: timezone(offset[, name])
+.. class:: timezone(offset, name=None)
The *offset* argument must be specified as a :class:`timedelta`
object representing the difference between the local time and UTC. It must
@@ -1741,10 +1886,7 @@ made to civil time.
otherwise :exc:`ValueError` is raised.
The *name* argument is optional. If specified it must be a string that
- is used as the value returned by the ``tzname(dt)`` method. Otherwise,
- ``tzname(dt)`` returns a string 'UTCsHH:MM', where s is the sign of
- *offset*, HH and MM are two digits of ``offset.hours`` and
- ``offset.minutes`` respectively.
+ will be used as the value returned by the :meth:`datetime.tzname` method.
.. versionadded:: 3.2
@@ -1757,11 +1899,19 @@ made to civil time.
.. method:: timezone.tzname(dt)
- Return the fixed value specified when the :class:`timezone` instance is
- constructed or a string 'UTCsHH:MM', where s is the sign of
- *offset*, HH and MM are two digits of ``offset.hours`` and
+ Return the fixed value specified when the :class:`timezone` instance
+ is constructed. If *name* is not provided in the constructor, the
+ name returned by ``tzname(dt)`` is generated from the value of the
+ ``offset`` as follows. If *offset* is ``timedelta(0)``, the name
+ is "UTC", otherwise it is a string 'UTC±HH:MM', where ± is the sign
+ of ``offset``, HH and MM are two digits of ``offset.hours`` and
``offset.minutes`` respectively.
+ .. versionchanged:: 3.6
+ Name generated from ``offset=timedelta(0)`` is now plain 'UTC', not
+ 'UTC+00:00'.
+
+
.. method:: timezone.dst(dt)
Always returns ``None``.
@@ -1911,6 +2061,34 @@ format codes.
| ``%%`` | A literal ``'%'`` character. | % | |
+-----------+--------------------------------+------------------------+-------+
+Several additional directives not required by the C89 standard are included for
+convenience. These parameters all correspond to ISO 8601 date values. These
+may not be available on all platforms when used with the :meth:`strftime`
+method. The ISO 8601 year and ISO 8601 week directives are not interchangeable
+with the year and week number directives above. Calling :meth:`strptime` with
+incomplete or ambiguous ISO 8601 directives will raise a :exc:`ValueError`.
+
++-----------+--------------------------------+------------------------+-------+
+| Directive | Meaning | Example | Notes |
++===========+================================+========================+=======+
+| ``%G`` | ISO 8601 year with century | 0001, 0002, ..., 2013, | \(8) |
+| | representing the year that | 2014, ..., 9998, 9999 | |
+| | contains the greater part of | | |
+| | the ISO week (``%V``). | | |
++-----------+--------------------------------+------------------------+-------+
+| ``%u`` | ISO 8601 weekday as a decimal | 1, 2, ..., 7 | |
+| | number where 1 is Monday. | | |
++-----------+--------------------------------+------------------------+-------+
+| ``%V`` | ISO 8601 week as a decimal | 01, 02, ..., 53 | \(8) |
+| | number with Monday as | | |
+| | the first day of the week. | | |
+| | Week 01 is the week containing | | |
+| | Jan 4. | | |
++-----------+--------------------------------+------------------------+-------+
+
+.. versionadded:: 3.6
+ ``%G``, ``%u`` and ``%V`` were added.
+
Notes:
(1)
@@ -1975,7 +2153,14 @@ Notes:
(7)
When used with the :meth:`strptime` method, ``%U`` and ``%W`` are only used
- in calculations when the day of the week and the year are specified.
+ in calculations when the day of the week and the calendar year (``%Y``)
+ are specified.
+
+(8)
+ Similar to ``%U`` and ``%W``, ``%V`` is only used in calculations when the
+ day of the week and the ISO year (``%G``) are specified in a
+ :meth:`strptime` format string. Also note that ``%G`` and ``%Y`` are not
+ interchangeable.
.. rubric:: Footnotes
diff --git a/Doc/library/dbm.rst b/Doc/library/dbm.rst
index 2a1db91..32e80b2 100644
--- a/Doc/library/dbm.rst
+++ b/Doc/library/dbm.rst
@@ -351,6 +351,10 @@ The module defines the following:
:func:`.open` always creates a new database when the flag has the value
``'n'``.
+ .. deprecated-removed:: 3.6 3.8
+ Creating database in ``'r'`` and ``'w'`` modes. Modifying database in
+ ``'r'`` mode.
+
In addition to the methods provided by the
:class:`collections.abc.MutableMapping` class, :class:`dumbdbm` objects
provide the following methods:
diff --git a/Doc/library/decimal.rst b/Doc/library/decimal.rst
index b5ce0b1..e984edc 100644
--- a/Doc/library/decimal.rst
+++ b/Doc/library/decimal.rst
@@ -345,7 +345,7 @@ Decimal objects
*value* can be an integer, string, tuple, :class:`float`, or another :class:`Decimal`
object. If no *value* is given, returns ``Decimal('0')``. If *value* is a
string, it should conform to the decimal numeric string syntax after leading
- and trailing whitespace characters are removed::
+ and trailing whitespace characters, as well as underscores throughout, are removed::
sign ::= '+' | '-'
digit ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
@@ -394,6 +394,10 @@ Decimal objects
:class:`float` arguments raise an exception if the :exc:`FloatOperation`
trap is set. By default the trap is off.
+ .. versionchanged:: 3.6
+ Underscores are allowed for grouping, as with integral and floating-point
+ literals in code.
+
Decimal floating point objects share many properties with the other built-in
numeric types such as :class:`float` and :class:`int`. All of the usual math
operations and special methods apply. Likewise, decimal objects can be
@@ -447,6 +451,19 @@ Decimal objects
``Decimal('321e+5').adjusted()`` returns seven. Used for determining the
position of the most significant digit with respect to the decimal point.
+ .. method:: as_integer_ratio()
+
+ Return a pair ``(n, d)`` of integers that represent the given
+ :class:`Decimal` instance as a fraction, in lowest terms and
+ with a positive denominator::
+
+ >>> Decimal('-3.14').as_integer_ratio()
+ (-157, 50)
+
+ The conversion is exact. Raise OverflowError on infinities and ValueError
+ on NaNs.
+
+ .. versionadded:: 3.6
.. method:: as_tuple()
@@ -1062,8 +1079,8 @@ In addition to the three supplied contexts, new contexts can be created with the
Decimal('4.44')
This method implements the to-number operation of the IBM specification.
- If the argument is a string, no leading or trailing whitespace is
- permitted.
+ If the argument is a string, no leading or trailing whitespace or
+ underscores are permitted.
.. method:: create_decimal_from_float(f)
diff --git a/Doc/library/dis.rst b/Doc/library/dis.rst
index f86725b..a15690b 100644
--- a/Doc/library/dis.rst
+++ b/Doc/library/dis.rst
@@ -31,9 +31,9 @@ the following command can be used to display the disassembly of
>>> dis.dis(myfunc)
2 0 LOAD_GLOBAL 0 (len)
- 3 LOAD_FAST 0 (alist)
- 6 CALL_FUNCTION 1
- 9 RETURN_VALUE
+ 2 LOAD_FAST 0 (alist)
+ 4 CALL_FUNCTION 1
+ 6 RETURN_VALUE
(The "2" is a line number).
@@ -607,6 +607,14 @@ iterations of the loop.
.. versionadded:: 3.3
+.. opcode:: SETUP_ANNOTATIONS
+
+ Checks whether ``__annotations__`` is defined in ``locals()``, if not it is
+ set up to an empty ``dict``. This opcode is only emitted if a class
+ or module body contains :term:`variable annotations <variable annotation>`
+ statically.
+
+ .. versionadded:: 3.6
.. opcode:: IMPORT_STAR
@@ -682,8 +690,7 @@ iterations of the loop.
.. XXX explain the WHY stuff!
-All of the following opcodes expect arguments. An argument is two bytes, with
-the more significant byte last.
+All of the following opcodes use their arguments.
.. opcode:: STORE_NAME (namei)
@@ -769,6 +776,23 @@ the more significant byte last.
to hold *count* entries.
+.. opcode:: BUILD_CONST_KEY_MAP (count)
+
+ The version of :opcode:`BUILD_MAP` specialized for constant keys. *count*
+ values are consumed from the stack. The top element on the stack contains
+ a tuple of keys.
+
+ .. versionadded:: 3.6
+
+
+.. opcode:: BUILD_STRING (count)
+
+ Concatenates *count* strings from the stack and pushes the resulting string
+ onto the stack.
+
+ .. versionadded:: 3.6
+
+
.. opcode:: LOAD_ATTR (namei)
Replaces TOS with ``getattr(TOS, co_names[namei])``.
@@ -874,6 +898,13 @@ the more significant byte last.
Deletes local ``co_varnames[var_num]``.
+.. opcode:: STORE_ANNOTATION (namei)
+
+ Stores TOS as ``locals()['__annotations__'][co_names[namei]] = TOS``.
+
+ .. versionadded:: 3.6
+
+
.. opcode:: LOAD_CLOSURE (i)
Pushes a reference to the cell contained in slot *i* of the cell and free
@@ -929,27 +960,16 @@ the more significant byte last.
.. opcode:: MAKE_FUNCTION (argc)
Pushes a new function object on the stack. From bottom to top, the consumed
- stack must consist of
-
- * ``argc & 0xFF`` default argument objects in positional order
- * ``(argc >> 8) & 0xFF`` pairs of name and default argument, with the name
- just below the object on the stack, for keyword-only parameters
- * ``(argc >> 16) & 0x7FFF`` parameter annotation objects
- * a tuple listing the parameter names for the annotations (only if there are
- ony annotation objects)
+ stack must consist of values if the argument carries a specified flag value
+
+ * ``0x01`` a tuple of default argument objects in positional order
+ * ``0x02`` a dictionary of keyword-only parameters' default values
+ * ``0x04`` an annotation dictionary
+ * ``0x08`` a tuple containing cells for free variables, making a closure
* the code associated with the function (at TOS1)
* the :term:`qualified name` of the function (at TOS)
-.. opcode:: MAKE_CLOSURE (argc)
-
- Creates a new function object, sets its *__closure__* slot, and pushes it on
- the stack. TOS is the :term:`qualified name` of the function, TOS1 is the
- code associated with the function, and TOS2 is the tuple containing cells for
- the closure's free variables. *argc* is interpreted as in ``MAKE_FUNCTION``;
- the annotations and defaults are also in the same order below TOS2.
-
-
.. opcode:: BUILD_SLICE (argc)
.. index:: builtin: slice
@@ -989,6 +1009,28 @@ the more significant byte last.
arguments.
+.. opcode:: FORMAT_VALUE (flags)
+
+ Used for implementing formatted literal strings (f-strings). Pops
+ an optional *fmt_spec* from the stack, then a required *value*.
+ *flags* is interpreted as follows:
+
+ * ``(flags & 0x03) == 0x00``: *value* is formatted as-is.
+ * ``(flags & 0x03) == 0x01``: call :func:`str` on *value* before
+ formatting it.
+ * ``(flags & 0x03) == 0x02``: call :func:`repr` on *value* before
+ formatting it.
+ * ``(flags & 0x03) == 0x03``: call :func:`ascii` on *value* before
+ formatting it.
+ * ``(flags & 0x04) == 0x04``: pop *fmt_spec* from the stack and use
+ it, else use an empty *fmt_spec*.
+
+ Formatting is performed using :c:func:`PyObject_Format`. The
+ result is pushed on the stack.
+
+ .. versionadded:: 3.6
+
+
.. opcode:: HAVE_ARGUMENT
This is not really an opcode. It identifies the dividing line between
diff --git a/Doc/library/email.charset.rst b/Doc/library/email.charset.rst
index 161d86a..053463f 100644
--- a/Doc/library/email.charset.rst
+++ b/Doc/library/email.charset.rst
@@ -8,6 +8,11 @@
--------------
+This module is part of the legacy (``Compat32``) email API. In the new
+API only the aliases table is used.
+
+The remaining text in this section is the original documentation of the module.
+
This module provides a class :class:`Charset` for representing character sets
and character set conversions in email messages, as well as a character set
registry and several convenience methods for manipulating this registry.
diff --git a/Doc/library/email.compat32-message.rst b/Doc/library/email.compat32-message.rst
new file mode 100644
index 0000000..2c65079
--- /dev/null
+++ b/Doc/library/email.compat32-message.rst
@@ -0,0 +1,754 @@
+.. _compat32_message:
+
+:mod:`email.message.Message`: Representing an email message using the :data:`~email.policy.compat32` API
+--------------------------------------------------------------------------------------------------------
+
+.. module:: email.message
+ :synopsis: The base class representing email messages in a fashion
+ backward compatible with python3.2
+
+
+The :class:`Message` class is very similar to the
+:class:`~email.message.EmailMessage` class, without the methods added by that
+class, and with the default behavior of certain other methods being slightly
+different. We also document here some methods that, while supported by the
+:class:`~email.message.EmailMessage` class, are not recommended unless you are
+dealing with legacy code.
+
+The philosophy and structure of the two classes is otherwise the same.
+
+This document describes the behavior under the default (for :class:`Message`)
+policy :attr:`~email.policy.Compat32`. If you are going to use another policy,
+you should be using the :class:`~email.message.EmailMessage` class instead.
+
+An email message consists of *headers* and a *payload*. Headers must be
+:rfc:`5233` style names and values, where the field name and value are
+separated by a colon. The colon is not part of either the field name or the
+field value. The payload may be a simple text message, or a binary object, or
+a structured sequence of sub-messages each with their own set of headers and
+their own payload. The latter type of payload is indicated by the message
+having a MIME type such as :mimetype:`multipart/\*` or
+:mimetype:`message/rfc822`.
+
+The conceptual model provided by a :class:`Message` object is that of an
+ordered dictionary of headers with additional methods for accessing both
+specialized information from the headers, for accessing the payload, for
+generating a serialized version of the mssage, and for recursively walking over
+the object tree. Note that duplicate headers are supported but special methods
+must be used to access them.
+
+The :class:`Message` psuedo-dictionary is indexed by the header names, which
+must be ASCII values. The values of the dictionary are strings that are
+supposed to contain only ASCII characters; there is some special handling for
+non-ASCII input, but it doesn't always produce the correct results. Headers
+are stored and returned in case-preserving form, but field names are matched
+case-insensitively. There may also be a single envelope header, also known as
+the *Unix-From* header or the ``From_`` header. The *payload* is either a
+string or bytes, in the case of simple message objects, or a list of
+:class:`Message` objects, for MIME container documents (e.g.
+:mimetype:`multipart/\*` and :mimetype:`message/rfc822`).
+
+Here are the methods of the :class:`Message` class:
+
+
+.. class:: Message(policy=compat32)
+
+ If *policy* is specified (it must be an instance of a :mod:`~email.policy`
+ class) use the rules it specifies to update and serialize the representation
+ of the message. If *policy* is not set, use the :class:`compat32
+ <email.policy.Compat32>` policy, which maintains backward compatibility with
+ the Python 3.2 version of the email package. For more information see the
+ :mod:`~email.policy` documentation.
+
+ .. versionchanged:: 3.3 The *policy* keyword argument was added.
+
+
+ .. method:: as_string(unixfrom=False, maxheaderlen=0, policy=None)
+
+ Return the entire message flattened as a string. When optional *unixfrom*
+ is true, the envelope header is included in the returned string.
+ *unixfrom* defaults to ``False``. For backward compabitility reasons,
+ *maxheaderlen* defaults to ``0``, so if you want a different value you
+ must override it explicitly (the value specified for *max_line_length* in
+ the policy will be ignored by this method). The *policy* argument may be
+ used to override the default policy obtained from the message instance.
+ This can be used to control some of the formatting produced by the
+ method, since the specified *policy* will be passed to the ``Generator``.
+
+ Flattening the message may trigger changes to the :class:`Message` if
+ defaults need to be filled in to complete the transformation to a string
+ (for example, MIME boundaries may be generated or modified).
+
+ Note that this method is provided as a convenience and may not always
+ format the message the way you want. For example, by default it does
+ not do the mangling of lines that begin with ``From`` that is
+ required by the unix mbox format. For more flexibility, instantiate a
+ :class:`~email.generator.Generator` instance and use its
+ :meth:`~email.generator.Generator.flatten` method directly. For example::
+
+ from io import StringIO
+ from email.generator import Generator
+ fp = StringIO()
+ g = Generator(fp, mangle_from_=True, maxheaderlen=60)
+ g.flatten(msg)
+ text = fp.getvalue()
+
+ If the message object contains binary data that is not encoded according
+ to RFC standards, the non-compliant data will be replaced by unicode
+ "unknown character" code points. (See also :meth:`.as_bytes` and
+ :class:`~email.generator.BytesGenerator`.)
+
+ .. versionchanged:: 3.4 the *policy* keyword argument was added.
+
+
+ .. method:: __str__()
+
+ Equivalent to :meth:`.as_string()`. Allows ``str(msg)`` to produce a
+ string containing the formatted message.
+
+
+ .. method:: as_bytes(unixfrom=False, policy=None)
+
+ Return the entire message flattened as a bytes object. When optional
+ *unixfrom* is true, the envelope header is included in the returned
+ string. *unixfrom* defaults to ``False``. The *policy* argument may be
+ used to override the default policy obtained from the message instance.
+ This can be used to control some of the formatting produced by the
+ method, since the specified *policy* will be passed to the
+ ``BytesGenerator``.
+
+ Flattening the message may trigger changes to the :class:`Message` if
+ defaults need to be filled in to complete the transformation to a string
+ (for example, MIME boundaries may be generated or modified).
+
+ Note that this method is provided as a convenience and may not always
+ format the message the way you want. For example, by default it does
+ not do the mangling of lines that begin with ``From`` that is
+ required by the unix mbox format. For more flexibility, instantiate a
+ :class:`~email.generator.BytesGenerator` instance and use its
+ :meth:`~email.generator.BytesGenerator.flatten` method directly.
+ For example::
+
+ from io import BytesIO
+ from email.generator import BytesGenerator
+ fp = BytesIO()
+ g = BytesGenerator(fp, mangle_from_=True, maxheaderlen=60)
+ g.flatten(msg)
+ text = fp.getvalue()
+
+ .. versionadded:: 3.4
+
+
+ .. method:: __bytes__()
+
+ Equivalent to :meth:`.as_bytes()`. Allows ``bytes(msg)`` to produce a
+ bytes object containing the formatted message.
+
+ .. versionadded:: 3.4
+
+
+ .. method:: is_multipart()
+
+ Return ``True`` if the message's payload is a list of sub-\
+ :class:`Message` objects, otherwise return ``False``. When
+ :meth:`is_multipart` returns ``False``, the payload should be a string
+ object (which might be a CTE encoded binary payload. (Note that
+ :meth:`is_multipart` returning ``True`` does not necessarily mean that
+ "msg.get_content_maintype() == 'multipart'" will return the ``True``.
+ For example, ``is_multipart`` will return ``True`` when the
+ :class:`Message` is of type ``message/rfc822``.)
+
+
+ .. method:: set_unixfrom(unixfrom)
+
+ Set the message's envelope header to *unixfrom*, which should be a string.
+
+
+ .. method:: get_unixfrom()
+
+ Return the message's envelope header. Defaults to ``None`` if the
+ envelope header was never set.
+
+
+ .. method:: attach(payload)
+
+ Add the given *payload* to the current payload, which must be ``None`` or
+ a list of :class:`Message` objects before the call. After the call, the
+ payload will always be a list of :class:`Message` objects. If you want to
+ set the payload to a scalar object (e.g. a string), use
+ :meth:`set_payload` instead.
+
+ This is a legacy method. On the
+ :class:`~email.emailmessage.EmailMessage` class its functionality is
+ replaced by :meth:`~email.message.EmailMessage.set_content` and the
+ realted ``make`` and ``add`` methods.
+
+
+ .. method:: get_payload(i=None, decode=False)
+
+ Return the current payload, which will be a list of
+ :class:`Message` objects when :meth:`is_multipart` is ``True``, or a
+ string when :meth:`is_multipart` is ``False``. If the payload is a list
+ and you mutate the list object, you modify the message's payload in place.
+
+ With optional argument *i*, :meth:`get_payload` will return the *i*-th
+ element of the payload, counting from zero, if :meth:`is_multipart` is
+ ``True``. An :exc:`IndexError` will be raised if *i* is less than 0 or
+ greater than or equal to the number of items in the payload. If the
+ payload is a string (i.e. :meth:`is_multipart` is ``False``) and *i* is
+ given, a :exc:`TypeError` is raised.
+
+ Optional *decode* is a flag indicating whether the payload should be
+ decoded or not, according to the :mailheader:`Content-Transfer-Encoding`
+ header. When ``True`` and the message is not a multipart, the payload will
+ be decoded if this header's value is ``quoted-printable`` or ``base64``.
+ If some other encoding is used, or :mailheader:`Content-Transfer-Encoding`
+ header is missing, the payload is
+ returned as-is (undecoded). In all cases the returned value is binary
+ data. If the message is a multipart and the *decode* flag is ``True``,
+ then ``None`` is returned. If the payload is base64 and it was not
+ perfectly formed (missing padding, characters outside the base64
+ alphabet), then an appropriate defect will be added to the message's
+ defect property (:class:`~email.errors.InvalidBase64PaddingDefect` or
+ :class:`~email.errors.InvalidBase64CharactersDefect`, respectively).
+
+ When *decode* is ``False`` (the default) the body is returned as a string
+ without decoding the :mailheader:`Content-Transfer-Encoding`. However,
+ for a :mailheader:`Content-Transfer-Encoding` of 8bit, an attempt is made
+ to decode the original bytes using the ``charset`` specified by the
+ :mailheader:`Content-Type` header, using the ``replace`` error handler.
+ If no ``charset`` is specified, or if the ``charset`` given is not
+ recognized by the email package, the body is decoded using the default
+ ASCII charset.
+
+ This is a legacy method. On the
+ :class:`~email.emailmessage.EmailMessage` class its functionality is
+ replaced by :meth:`~email.message.EmailMessage.get_content` and
+ :meth:`~email.message.EmailMessage.iter_parts`.
+
+
+ .. method:: set_payload(payload, charset=None)
+
+ Set the entire message object's payload to *payload*. It is the client's
+ responsibility to ensure the payload invariants. Optional *charset* sets
+ the message's default character set; see :meth:`set_charset` for details.
+
+ This is a legacy method. On the
+ :class:`~email.emailmessage.EmailMessage` class its functionality is
+ replaced by :meth:`~email.message.EmailMessage.set_content`.
+
+
+ .. method:: set_charset(charset)
+
+ Set the character set of the payload to *charset*, which can either be a
+ :class:`~email.charset.Charset` instance (see :mod:`email.charset`), a
+ string naming a character set, or ``None``. If it is a string, it will
+ be converted to a :class:`~email.charset.Charset` instance. If *charset*
+ is ``None``, the ``charset`` parameter will be removed from the
+ :mailheader:`Content-Type` header (the message will not be otherwise
+ modified). Anything else will generate a :exc:`TypeError`.
+
+ If there is no existing :mailheader:`MIME-Version` header one will be
+ added. If there is no existing :mailheader:`Content-Type` header, one
+ will be added with a value of :mimetype:`text/plain`. Whether the
+ :mailheader:`Content-Type` header already exists or not, its ``charset``
+ parameter will be set to *charset.output_charset*. If
+ *charset.input_charset* and *charset.output_charset* differ, the payload
+ will be re-encoded to the *output_charset*. If there is no existing
+ :mailheader:`Content-Transfer-Encoding` header, then the payload will be
+ transfer-encoded, if needed, using the specified
+ :class:`~email.charset.Charset`, and a header with the appropriate value
+ will be added. If a :mailheader:`Content-Transfer-Encoding` header
+ already exists, the payload is assumed to already be correctly encoded
+ using that :mailheader:`Content-Transfer-Encoding` and is not modified.
+
+ This is a legacy method. On the
+ :class:`~email.emailmessage.EmailMessage` class its functionality is
+ replaced by the *charset* parameter of the
+ :meth:`email.emailmessage.EmailMessage.set_content` method.
+
+
+ .. method:: get_charset()
+
+ Return the :class:`~email.charset.Charset` instance associated with the
+ message's payload.
+
+ This is a legacy method. On the
+ :class:`~email.emailmessage.EmailMessage` class it always returns
+ ``None``.
+
+
+ The following methods implement a mapping-like interface for accessing the
+ message's :rfc:`2822` headers. Note that there are some semantic differences
+ between these methods and a normal mapping (i.e. dictionary) interface. For
+ example, in a dictionary there are no duplicate keys, but here there may be
+ duplicate message headers. Also, in dictionaries there is no guaranteed
+ order to the keys returned by :meth:`keys`, but in a :class:`Message` object,
+ headers are always returned in the order they appeared in the original
+ message, or were added to the message later. Any header deleted and then
+ re-added are always appended to the end of the header list.
+
+ These semantic differences are intentional and are biased toward maximal
+ convenience.
+
+ Note that in all cases, any envelope header present in the message is not
+ included in the mapping interface.
+
+ In a model generated from bytes, any header values that (in contravention of
+ the RFCs) contain non-ASCII bytes will, when retrieved through this
+ interface, be represented as :class:`~email.header.Header` objects with
+ a charset of `unknown-8bit`.
+
+
+ .. method:: __len__()
+
+ Return the total number of headers, including duplicates.
+
+
+ .. method:: __contains__(name)
+
+ Return true if the message object has a field named *name*. Matching is
+ done case-insensitively and *name* should not include the trailing colon.
+ Used for the ``in`` operator, e.g.::
+
+ if 'message-id' in myMessage:
+ print('Message-ID:', myMessage['message-id'])
+
+
+ .. method:: __getitem__(name)
+
+ Return the value of the named header field. *name* should not include the
+ colon field separator. If the header is missing, ``None`` is returned; a
+ :exc:`KeyError` is never raised.
+
+ Note that if the named field appears more than once in the message's
+ headers, exactly which of those field values will be returned is
+ undefined. Use the :meth:`get_all` method to get the values of all the
+ extant named headers.
+
+
+ .. method:: __setitem__(name, val)
+
+ Add a header to the message with field name *name* and value *val*. The
+ field is appended to the end of the message's existing fields.
+
+ Note that this does *not* overwrite or delete any existing header with the same
+ name. If you want to ensure that the new header is the only one present in the
+ message with field name *name*, delete the field first, e.g.::
+
+ del msg['subject']
+ msg['subject'] = 'Python roolz!'
+
+
+ .. method:: __delitem__(name)
+
+ Delete all occurrences of the field with name *name* from the message's
+ headers. No exception is raised if the named field isn't present in the
+ headers.
+
+
+ .. method:: keys()
+
+ Return a list of all the message's header field names.
+
+
+ .. method:: values()
+
+ Return a list of all the message's field values.
+
+
+ .. method:: items()
+
+ Return a list of 2-tuples containing all the message's field headers and
+ values.
+
+
+ .. method:: get(name, failobj=None)
+
+ Return the value of the named header field. This is identical to
+ :meth:`__getitem__` except that optional *failobj* is returned if the
+ named header is missing (defaults to ``None``).
+
+ Here are some additional useful methods:
+
+
+ .. method:: get_all(name, failobj=None)
+
+ Return a list of all the values for the field named *name*. If there are
+ no such named headers in the message, *failobj* is returned (defaults to
+ ``None``).
+
+
+ .. method:: add_header(_name, _value, **_params)
+
+ Extended header setting. This method is similar to :meth:`__setitem__`
+ except that additional header parameters can be provided as keyword
+ arguments. *_name* is the header field to add and *_value* is the
+ *primary* value for the header.
+
+ For each item in the keyword argument dictionary *_params*, the key is
+ taken as the parameter name, with underscores converted to dashes (since
+ dashes are illegal in Python identifiers). Normally, the parameter will
+ be added as ``key="value"`` unless the value is ``None``, in which case
+ only the key will be added. If the value contains non-ASCII characters,
+ it can be specified as a three tuple in the format
+ ``(CHARSET, LANGUAGE, VALUE)``, where ``CHARSET`` is a string naming the
+ charset to be used to encode the value, ``LANGUAGE`` can usually be set
+ to ``None`` or the empty string (see :rfc:`2231` for other possibilities),
+ and ``VALUE`` is the string value containing non-ASCII code points. If
+ a three tuple is not passed and the value contains non-ASCII characters,
+ it is automatically encoded in :rfc:`2231` format using a ``CHARSET``
+ of ``utf-8`` and a ``LANGUAGE`` of ``None``.
+
+ Here's an example::
+
+ msg.add_header('Content-Disposition', 'attachment', filename='bud.gif')
+
+ This will add a header that looks like ::
+
+ Content-Disposition: attachment; filename="bud.gif"
+
+ An example with non-ASCII characters::
+
+ msg.add_header('Content-Disposition', 'attachment',
+ filename=('iso-8859-1', '', 'Fußballer.ppt'))
+
+ Which produces ::
+
+ Content-Disposition: attachment; filename*="iso-8859-1''Fu%DFballer.ppt"
+
+
+ .. method:: replace_header(_name, _value)
+
+ Replace a header. Replace the first header found in the message that
+ matches *_name*, retaining header order and field name case. If no
+ matching header was found, a :exc:`KeyError` is raised.
+
+
+ .. method:: get_content_type()
+
+ Return the message's content type. The returned string is coerced to
+ lower case of the form :mimetype:`maintype/subtype`. If there was no
+ :mailheader:`Content-Type` header in the message the default type as given
+ by :meth:`get_default_type` will be returned. Since according to
+ :rfc:`2045`, messages always have a default type, :meth:`get_content_type`
+ will always return a value.
+
+ :rfc:`2045` defines a message's default type to be :mimetype:`text/plain`
+ unless it appears inside a :mimetype:`multipart/digest` container, in
+ which case it would be :mimetype:`message/rfc822`. If the
+ :mailheader:`Content-Type` header has an invalid type specification,
+ :rfc:`2045` mandates that the default type be :mimetype:`text/plain`.
+
+
+ .. method:: get_content_maintype()
+
+ Return the message's main content type. This is the :mimetype:`maintype`
+ part of the string returned by :meth:`get_content_type`.
+
+
+ .. method:: get_content_subtype()
+
+ Return the message's sub-content type. This is the :mimetype:`subtype`
+ part of the string returned by :meth:`get_content_type`.
+
+
+ .. method:: get_default_type()
+
+ Return the default content type. Most messages have a default content
+ type of :mimetype:`text/plain`, except for messages that are subparts of
+ :mimetype:`multipart/digest` containers. Such subparts have a default
+ content type of :mimetype:`message/rfc822`.
+
+
+ .. method:: set_default_type(ctype)
+
+ Set the default content type. *ctype* should either be
+ :mimetype:`text/plain` or :mimetype:`message/rfc822`, although this is not
+ enforced. The default content type is not stored in the
+ :mailheader:`Content-Type` header.
+
+
+ .. method:: get_params(failobj=None, header='content-type', unquote=True)
+
+ Return the message's :mailheader:`Content-Type` parameters, as a list.
+ The elements of the returned list are 2-tuples of key/value pairs, as
+ split on the ``'='`` sign. The left hand side of the ``'='`` is the key,
+ while the right hand side is the value. If there is no ``'='`` sign in
+ the parameter the value is the empty string, otherwise the value is as
+ described in :meth:`get_param` and is unquoted if optional *unquote* is
+ ``True`` (the default).
+
+ Optional *failobj* is the object to return if there is no
+ :mailheader:`Content-Type` header. Optional *header* is the header to
+ search instead of :mailheader:`Content-Type`.
+
+ This is a legacy method. On the
+ :class:`~email.emailmessage.EmailMessage` class its functionality is
+ replaced by the *params* property of the individual header objects
+ returned by the header access methods.
+
+
+ .. method:: get_param(param, failobj=None, header='content-type', unquote=True)
+
+ Return the value of the :mailheader:`Content-Type` header's parameter
+ *param* as a string. If the message has no :mailheader:`Content-Type`
+ header or if there is no such parameter, then *failobj* is returned
+ (defaults to ``None``).
+
+ Optional *header* if given, specifies the message header to use instead of
+ :mailheader:`Content-Type`.
+
+ Parameter keys are always compared case insensitively. The return value
+ can either be a string, or a 3-tuple if the parameter was :rfc:`2231`
+ encoded. When it's a 3-tuple, the elements of the value are of the form
+ ``(CHARSET, LANGUAGE, VALUE)``. Note that both ``CHARSET`` and
+ ``LANGUAGE`` can be ``None``, in which case you should consider ``VALUE``
+ to be encoded in the ``us-ascii`` charset. You can usually ignore
+ ``LANGUAGE``.
+
+ If your application doesn't care whether the parameter was encoded as in
+ :rfc:`2231`, you can collapse the parameter value by calling
+ :func:`email.utils.collapse_rfc2231_value`, passing in the return value
+ from :meth:`get_param`. This will return a suitably decoded Unicode
+ string when the value is a tuple, or the original string unquoted if it
+ isn't. For example::
+
+ rawparam = msg.get_param('foo')
+ param = email.utils.collapse_rfc2231_value(rawparam)
+
+ In any case, the parameter value (either the returned string, or the
+ ``VALUE`` item in the 3-tuple) is always unquoted, unless *unquote* is set
+ to ``False``.
+
+ This is a legacy method. On the
+ :class:`~email.emailmessage.EmailMessage` class its functionality is
+ replaced by the *params* property of the individual header objects
+ returned by the header access methods.
+
+
+ .. method:: set_param(param, value, header='Content-Type', requote=True, \
+ charset=None, language='', replace=False)
+
+ Set a parameter in the :mailheader:`Content-Type` header. If the
+ parameter already exists in the header, its value will be replaced with
+ *value*. If the :mailheader:`Content-Type` header as not yet been defined
+ for this message, it will be set to :mimetype:`text/plain` and the new
+ parameter value will be appended as per :rfc:`2045`.
+
+ Optional *header* specifies an alternative header to
+ :mailheader:`Content-Type`, and all parameters will be quoted as necessary
+ unless optional *requote* is ``False`` (the default is ``True``).
+
+ If optional *charset* is specified, the parameter will be encoded
+ according to :rfc:`2231`. Optional *language* specifies the RFC 2231
+ language, defaulting to the empty string. Both *charset* and *language*
+ should be strings.
+
+ If *replace* is ``False`` (the default) the header is moved to the
+ end of the list of headers. If *replace* is ``True``, the header
+ will be updated in place.
+
+ .. versionchanged:: 3.4 ``replace`` keyword was added.
+
+
+ .. method:: del_param(param, header='content-type', requote=True)
+
+ Remove the given parameter completely from the :mailheader:`Content-Type`
+ header. The header will be re-written in place without the parameter or
+ its value. All values will be quoted as necessary unless *requote* is
+ ``False`` (the default is ``True``). Optional *header* specifies an
+ alternative to :mailheader:`Content-Type`.
+
+
+ .. method:: set_type(type, header='Content-Type', requote=True)
+
+ Set the main type and subtype for the :mailheader:`Content-Type`
+ header. *type* must be a string in the form :mimetype:`maintype/subtype`,
+ otherwise a :exc:`ValueError` is raised.
+
+ This method replaces the :mailheader:`Content-Type` header, keeping all
+ the parameters in place. If *requote* is ``False``, this leaves the
+ existing header's quoting as is, otherwise the parameters will be quoted
+ (the default).
+
+ An alternative header can be specified in the *header* argument. When the
+ :mailheader:`Content-Type` header is set a :mailheader:`MIME-Version`
+ header is also added.
+
+ This is a legacy method. On the
+ :class:`~email.emailmessage.EmailMessage` class its functionality is
+ replaced by the ``make_`` and ``add_`` methods.
+
+
+ .. method:: get_filename(failobj=None)
+
+ Return the value of the ``filename`` parameter of the
+ :mailheader:`Content-Disposition` header of the message. If the header
+ does not have a ``filename`` parameter, this method falls back to looking
+ for the ``name`` parameter on the :mailheader:`Content-Type` header. If
+ neither is found, or the header is missing, then *failobj* is returned.
+ The returned string will always be unquoted as per
+ :func:`email.utils.unquote`.
+
+
+ .. method:: get_boundary(failobj=None)
+
+ Return the value of the ``boundary`` parameter of the
+ :mailheader:`Content-Type` header of the message, or *failobj* if either
+ the header is missing, or has no ``boundary`` parameter. The returned
+ string will always be unquoted as per :func:`email.utils.unquote`.
+
+
+ .. method:: set_boundary(boundary)
+
+ Set the ``boundary`` parameter of the :mailheader:`Content-Type` header to
+ *boundary*. :meth:`set_boundary` will always quote *boundary* if
+ necessary. A :exc:`~email.errors.HeaderParseError` is raised if the
+ message object has no :mailheader:`Content-Type` header.
+
+ Note that using this method is subtly different than deleting the old
+ :mailheader:`Content-Type` header and adding a new one with the new
+ boundary via :meth:`add_header`, because :meth:`set_boundary` preserves
+ the order of the :mailheader:`Content-Type` header in the list of
+ headers. However, it does *not* preserve any continuation lines which may
+ have been present in the original :mailheader:`Content-Type` header.
+
+
+ .. method:: get_content_charset(failobj=None)
+
+ Return the ``charset`` parameter of the :mailheader:`Content-Type` header,
+ coerced to lower case. If there is no :mailheader:`Content-Type` header, or if
+ that header has no ``charset`` parameter, *failobj* is returned.
+
+ Note that this method differs from :meth:`get_charset` which returns the
+ :class:`~email.charset.Charset` instance for the default encoding of the message body.
+
+
+ .. method:: get_charsets(failobj=None)
+
+ Return a list containing the character set names in the message. If the
+ message is a :mimetype:`multipart`, then the list will contain one element
+ for each subpart in the payload, otherwise, it will be a list of length 1.
+
+ Each item in the list will be a string which is the value of the
+ ``charset`` parameter in the :mailheader:`Content-Type` header for the
+ represented subpart. However, if the subpart has no
+ :mailheader:`Content-Type` header, no ``charset`` parameter, or is not of
+ the :mimetype:`text` main MIME type, then that item in the returned list
+ will be *failobj*.
+
+
+ .. method:: get_content_disposition()
+
+ Return the lowercased value (without parameters) of the message's
+ :mailheader:`Content-Disposition` header if it has one, or ``None``. The
+ possible values for this method are *inline*, *attachment* or ``None``
+ if the message follows :rfc:`2183`.
+
+ .. versionadded:: 3.5
+
+ .. method:: walk()
+
+ The :meth:`walk` method is an all-purpose generator which can be used to
+ iterate over all the parts and subparts of a message object tree, in
+ depth-first traversal order. You will typically use :meth:`walk` as the
+ iterator in a ``for`` loop; each iteration returns the next subpart.
+
+ Here's an example that prints the MIME type of every part of a multipart
+ message structure:
+
+ .. testsetup::
+
+ >>> from email import message_from_binary_file
+ >>> with open('Lib/test/test_email/data/msg_16.txt', 'rb') as f:
+ ... msg = message_from_binary_file(f)
+ >>> from email.iterators import _structure
+
+ .. doctest::
+
+ >>> for part in msg.walk():
+ ... print(part.get_content_type())
+ multipart/report
+ text/plain
+ message/delivery-status
+ text/plain
+ text/plain
+ message/rfc822
+ text/plain
+
+ ``walk`` iterates over the subparts of any part where
+ :meth:`is_multipart` returns ``True``, even though
+ ``msg.get_content_maintype() == 'multipart'`` may return ``False``. We
+ can see this in our example by making use of the ``_structure`` debug
+ helper function:
+
+ .. doctest::
+
+ >>> for part in msg.walk():
+ ... print(part.get_content_maintype() == 'multipart'),
+ ... part.is_multipart())
+ True True
+ False False
+ False True
+ False False
+ False False
+ False True
+ False False
+ >>> _structure(msg)
+ multipart/report
+ text/plain
+ message/delivery-status
+ text/plain
+ text/plain
+ message/rfc822
+ text/plain
+
+ Here the ``message`` parts are not ``multiparts``, but they do contain
+ subparts. ``is_multipart()`` returns ``True`` and ``walk`` descends
+ into the subparts.
+
+
+ :class:`Message` objects can also optionally contain two instance attributes,
+ which can be used when generating the plain text of a MIME message.
+
+
+ .. attribute:: preamble
+
+ The format of a MIME document allows for some text between the blank line
+ following the headers, and the first multipart boundary string. Normally,
+ this text is never visible in a MIME-aware mail reader because it falls
+ outside the standard MIME armor. However, when viewing the raw text of
+ the message, or when viewing the message in a non-MIME aware reader, this
+ text can become visible.
+
+ The *preamble* attribute contains this leading extra-armor text for MIME
+ documents. When the :class:`~email.parser.Parser` discovers some text
+ after the headers but before the first boundary string, it assigns this
+ text to the message's *preamble* attribute. When the
+ :class:`~email.generator.Generator` is writing out the plain text
+ representation of a MIME message, and it finds the
+ message has a *preamble* attribute, it will write this text in the area
+ between the headers and the first boundary. See :mod:`email.parser` and
+ :mod:`email.generator` for details.
+
+ Note that if the message object has no preamble, the *preamble* attribute
+ will be ``None``.
+
+
+ .. attribute:: epilogue
+
+ The *epilogue* attribute acts the same way as the *preamble* attribute,
+ except that it contains text that appears between the last boundary and
+ the end of the message.
+
+ You do not need to set the epilogue to the empty string in order for the
+ :class:`~email.generator.Generator` to print a newline at the end of the
+ file.
+
+
+ .. attribute:: defects
+
+ The *defects* attribute contains a list of all the problems found when
+ parsing this message. See :mod:`email.errors` for a detailed description
+ of the possible parsing defects.
diff --git a/Doc/library/email.contentmanager.rst b/Doc/library/email.contentmanager.rst
index a9c078b..57743d5 100644
--- a/Doc/library/email.contentmanager.rst
+++ b/Doc/library/email.contentmanager.rst
@@ -7,251 +7,12 @@
.. moduleauthor:: R. David Murray <rdmurray@bitdance.com>
.. sectionauthor:: R. David Murray <rdmurray@bitdance.com>
-.. versionadded:: 3.4
- as a :term:`provisional module <provisional package>`.
-
**Source code:** :source:`Lib/email/contentmanager.py`
-.. note::
-
- The contentmanager module has been included in the standard library on a
- :term:`provisional basis <provisional package>`. Backwards incompatible
- changes (up to and including removal of the module) may occur if deemed
- necessary by the core developers.
-
---------------
-
-The :mod:`~email.message` module provides a class that can represent an
-arbitrary email message. That basic message model has a useful and flexible
-API, but it provides only a lower-level API for interacting with the generic
-parts of a message (the headers, generic header parameters, and the payload,
-which may be a list of sub-parts). This module provides classes and tools
-that provide an enhanced and extensible API for dealing with various specific
-types of content, including the ability to retrieve the content of the message
-as a specialized object type rather than as a simple bytes object. The module
-automatically takes care of the RFC-specified MIME details (required headers
-and parameters, etc.) for the certain common content types content properties,
-and support for additional types can be added by an application using the
-extension mechanisms.
-
-This module defines the eponymous "Content Manager" classes. The base
-:class:`.ContentManager` class defines an API for registering content
-management functions which extract data from ``Message`` objects or insert data
-and headers into ``Message`` objects, thus providing a way of converting
-between ``Message`` objects containing data and other representations of that
-data (Python data types, specialized Python objects, external files, etc). The
-module also defines one concrete content manager: :data:`raw_data_manager`
-converts between MIME content types and ``str`` or ``bytes`` data. It also
-provides a convenient API for managing the MIME parameters when inserting
-content into ``Message``\ s. It also handles inserting and extracting
-``Message`` objects when dealing with the ``message/rfc822`` content type.
-
-Another part of the enhanced interface is subclasses of
-:class:`~email.message.Message` that provide new convenience API functions,
-including convenience methods for calling the Content Managers derived from
-this module.
-
-.. note::
-
- Although :class:`.EmailMessage` and :class:`.MIMEPart` are currently
- documented in this module because of the provisional nature of the code, the
- implementation lives in the :mod:`email.message` module.
-
-.. currentmodule:: email.message
-
-.. class:: EmailMessage(policy=default)
-
- If *policy* is specified (it must be an instance of a :mod:`~email.policy`
- class) use the rules it specifies to udpate and serialize the representation
- of the message. If *policy* is not set, use the
- :class:`~email.policy.default` policy, which follows the rules of the email
- RFCs except for line endings (instead of the RFC mandated ``\r\n``, it uses
- the Python standard ``\n`` line endings). For more information see the
- :mod:`~email.policy` documentation.
-
- This class is a subclass of :class:`~email.message.Message`. It adds
- the following methods:
-
-
- .. method:: is_attachment
-
- Return ``True`` if there is a :mailheader:`Content-Disposition` header
- and its (case insensitive) value is ``attachment``, ``False`` otherwise.
-
- .. versionchanged:: 3.4.2
- is_attachment is now a method instead of a property, for consistency
- with :meth:`~email.message.Message.is_multipart`.
-
-
- .. method:: get_body(preferencelist=('related', 'html', 'plain'))
-
- Return the MIME part that is the best candidate to be the "body" of the
- message.
-
- *preferencelist* must be a sequence of strings from the set ``related``,
- ``html``, and ``plain``, and indicates the order of preference for the
- content type of the part returned.
-
- Start looking for candidate matches with the object on which the
- ``get_body`` method is called.
-
- If ``related`` is not included in *preferencelist*, consider the root
- part (or subpart of the root part) of any related encountered as a
- candidate if the (sub-)part matches a preference.
-
- When encountering a ``multipart/related``, check the ``start`` parameter
- and if a part with a matching :mailheader:`Content-ID` is found, consider
- only it when looking for candidate matches. Otherwise consider only the
- first (default root) part of the ``multipart/related``.
-
- If a part has a :mailheader:`Content-Disposition` header, only consider
- the part a candidate match if the value of the header is ``inline``.
-
- If none of the candidates matches any of the preferences in
- *preferneclist*, return ``None``.
-
- Notes: (1) For most applications the only *preferencelist* combinations
- that really make sense are ``('plain',)``, ``('html', 'plain')``, and the
- default, ``('related', 'html', 'plain')``. (2) Because matching starts
- with the object on which ``get_body`` is called, calling ``get_body`` on
- a ``multipart/related`` will return the object itself unless
- *preferencelist* has a non-default value. (3) Messages (or message parts)
- that do not specify a :mailheader:`Content-Type` or whose
- :mailheader:`Content-Type` header is invalid will be treated as if they
- are of type ``text/plain``, which may occasionally cause ``get_body`` to
- return unexpected results.
-
-
- .. method:: iter_attachments()
-
- Return an iterator over all of the parts of the message that are not
- candidate "body" parts. That is, skip the first occurrence of each of
- ``text/plain``, ``text/html``, ``multipart/related``, or
- ``multipart/alternative`` (unless they are explicitly marked as
- attachments via :mailheader:`Content-Disposition: attachment`), and
- return all remaining parts. When applied directly to a
- ``multipart/related``, return an iterator over the all the related parts
- except the root part (ie: the part pointed to by the ``start`` parameter,
- or the first part if there is no ``start`` parameter or the ``start``
- parameter doesn't match the :mailheader:`Content-ID` of any of the
- parts). When applied directly to a ``multipart/alternative`` or a
- non-``multipart``, return an empty iterator.
-
-
- .. method:: iter_parts()
-
- Return an iterator over all of the immediate sub-parts of the message,
- which will be empty for a non-``multipart``. (See also
- :meth:`~email.message.walk`.)
-
-
- .. method:: get_content(*args, content_manager=None, **kw)
-
- Call the ``get_content`` method of the *content_manager*, passing self
- as the message object, and passing along any other arguments or keywords
- as additional arguments. If *content_manager* is not specified, use
- the ``content_manager`` specified by the current :mod:`~email.policy`.
+------------
+.. versionadded:: 3.6 [1]_
- .. method:: set_content(*args, content_manager=None, **kw)
-
- Call the ``set_content`` method of the *content_manager*, passing self
- as the message object, and passing along any other arguments or keywords
- as additional arguments. If *content_manager* is not specified, use
- the ``content_manager`` specified by the current :mod:`~email.policy`.
-
-
- .. method:: make_related(boundary=None)
-
- Convert a non-``multipart`` message into a ``multipart/related`` message,
- moving any existing :mailheader:`Content-` headers and payload into a
- (new) first part of the ``multipart``. If *boundary* is specified, use
- it as the boundary string in the multipart, otherwise leave the boundary
- to be automatically created when it is needed (for example, when the
- message is serialized).
-
-
- .. method:: make_alternative(boundary=None)
-
- Convert a non-``multipart`` or a ``multipart/related`` into a
- ``multipart/alternative``, moving any existing :mailheader:`Content-`
- headers and payload into a (new) first part of the ``multipart``. If
- *boundary* is specified, use it as the boundary string in the multipart,
- otherwise leave the boundary to be automatically created when it is
- needed (for example, when the message is serialized).
-
-
- .. method:: make_mixed(boundary=None)
-
- Convert a non-``multipart``, a ``multipart/related``, or a
- ``multipart-alternative`` into a ``multipart/mixed``, moving any existing
- :mailheader:`Content-` headers and payload into a (new) first part of the
- ``multipart``. If *boundary* is specified, use it as the boundary string
- in the multipart, otherwise leave the boundary to be automatically
- created when it is needed (for example, when the message is serialized).
-
-
- .. method:: add_related(*args, content_manager=None, **kw)
-
- If the message is a ``multipart/related``, create a new message
- object, pass all of the arguments to its :meth:`set_content` method,
- and :meth:`~email.message.Message.attach` it to the ``multipart``. If
- the message is a non-``multipart``, call :meth:`make_related` and then
- proceed as above. If the message is any other type of ``multipart``,
- raise a :exc:`TypeError`. If *content_manager* is not specified, use
- the ``content_manager`` specified by the current :mod:`~email.policy`.
- If the added part has no :mailheader:`Content-Disposition` header,
- add one with the value ``inline``.
-
-
- .. method:: add_alternative(*args, content_manager=None, **kw)
-
- If the message is a ``multipart/alternative``, create a new message
- object, pass all of the arguments to its :meth:`set_content` method, and
- :meth:`~email.message.Message.attach` it to the ``multipart``. If the
- message is a non-``multipart`` or ``multipart/related``, call
- :meth:`make_alternative` and then proceed as above. If the message is
- any other type of ``multipart``, raise a :exc:`TypeError`. If
- *content_manager* is not specified, use the ``content_manager`` specified
- by the current :mod:`~email.policy`.
-
-
- .. method:: add_attachment(*args, content_manager=None, **kw)
-
- If the message is a ``multipart/mixed``, create a new message object,
- pass all of the arguments to its :meth:`set_content` method, and
- :meth:`~email.message.Message.attach` it to the ``multipart``. If the
- message is a non-``multipart``, ``multipart/related``, or
- ``multipart/alternative``, call :meth:`make_mixed` and then proceed as
- above. If *content_manager* is not specified, use the ``content_manager``
- specified by the current :mod:`~email.policy`. If the added part
- has no :mailheader:`Content-Disposition` header, add one with the value
- ``attachment``. This method can be used both for explicit attachments
- (:mailheader:`Content-Disposition: attachment` and ``inline`` attachments
- (:mailheader:`Content-Disposition: inline`), by passing appropriate
- options to the ``content_manager``.
-
-
- .. method:: clear()
-
- Remove the payload and all of the headers.
-
-
- .. method:: clear_content()
-
- Remove the payload and all of the :exc:`Content-` headers, leaving
- all other headers intact and in their original order.
-
-
-.. class:: MIMEPart(policy=default)
-
- This class represents a subpart of a MIME message. It is identical to
- :class:`EmailMessage`, except that no :mailheader:`MIME-Version` headers are
- added when :meth:`~EmailMessage.set_content` is called, since sub-parts do
- not need their own :mailheader:`MIME-Version` headers.
-
-
-.. currentmodule:: email.contentmanager
.. class:: ContentManager()
@@ -362,7 +123,7 @@ Currently the email package provides only one concrete content manager,
set_content(msg, <'bytes'>, maintype, subtype, cte="base64", \
disposition=None, filename=None, cid=None, \
params=None, headers=None)
- set_content(msg, <'Message'>, cte=None, \
+ set_content(msg, <'EmailMessage'>, cte=None, \
disposition=None, filename=None, cid=None, \
params=None, headers=None)
set_content(msg, <'list'>, subtype='mixed', \
@@ -378,14 +139,14 @@ Currently the email package provides only one concrete content manager,
subtype to *subtype* if it is specified, or ``plain`` if it is not.
* For ``bytes``, use the specified *maintype* and *subtype*, or
raise a :exc:`TypeError` if they are not specified.
- * For :class:`~email.message.Message` objects, set the maintype to
- ``message``, and set the subtype to *subtype* if it is specified
- or ``rfc822`` if it is not. If *subtype* is ``partial``, raise an
- error (``bytes`` objects must be used to construct
- ``message/partial`` parts).
+ * For :class:`~email.message.EmailMessage` objects, set the maintype
+ to ``message``, and set the subtype to *subtype* if it is
+ specified or ``rfc822`` if it is not. If *subtype* is
+ ``partial``, raise an error (``bytes`` objects must be used to
+ construct ``message/partial`` parts).
* For *<'list'>*, which should be a list of
- :class:`~email.message.Message` objects, set the ``maintype`` to
- ``multipart``, and the ``subtype`` to *subtype* if it is
+ :class:`~email.message.EmailMessage` objects, set the ``maintype``
+ to ``multipart``, and the ``subtype`` to *subtype* if it is
specified, and ``mixed`` if it is not. If the message parts in
the *<'list'>* have :mailheader:`MIME-Version` headers, remove
them.
@@ -397,32 +158,35 @@ Currently the email package provides only one concrete content manager,
If *cte* is set, encode the payload using the specified content transfer
encoding, and set the :mailheader:`Content-Transfer-Endcoding` header to
- that value. For ``str`` objects, if it is not set use heuristics to
- determine the most compact encoding. Possible values for *cte* are
- ``quoted-printable``, ``base64``, ``7bit``, ``8bit``, and ``binary``.
- If the input cannot be encoded in the specified encoding (eg: ``7bit``),
- raise a :exc:`ValueError`. For :class:`~email.message.Message`, per
- :rfc:`2046`, raise an error if a *cte* of ``quoted-printable`` or
- ``base64`` is requested for *subtype* ``rfc822``, and for any *cte*
- other than ``7bit`` for *subtype* ``external-body``. For
- ``message/rfc822``, use ``8bit`` if *cte* is not specified. For all
- other values of *subtype*, use ``7bit``.
+ that value. Possible values for *cte* are ``quoted-printable``,
+ ``base64``, ``7bit``, ``8bit``, and ``binary``. If the input cannot be
+ encoded in the specified encoding (for example, specifying a *cte* of
+ ``7bit`` for an input that contains non-ASCII values), raise a
+ :exc:`ValueError`.
+
+ * For ``str`` objects, if *cte* is not set use heuristics to
+ determine the most compact encoding.
+ * For :class:`~email.message.EmailMessage`, per :rfc:`2046`, raise
+ an error if a *cte* of ``quoted-printable`` or ``base64`` is
+ requested for *subtype* ``rfc822``, and for any *cte* other than
+ ``7bit`` for *subtype* ``external-body``. For
+ ``message/rfc822``, use ``8bit`` if *cte* is not specified. For
+ all other values of *subtype*, use ``7bit``.
.. note:: A *cte* of ``binary`` does not actually work correctly yet.
- The ``Message`` object as modified by ``set_content`` is correct, but
- :class:`~email.generator.BytesGenerator` does not serialize it
- correctly.
+ The ``EmailMessage`` object as modified by ``set_content`` is
+ correct, but :class:`~email.generator.BytesGenerator` does not
+ serialize it correctly.
If *disposition* is set, use it as the value of the
:mailheader:`Content-Disposition` header. If not specified, and
*filename* is specified, add the header with the value ``attachment``.
- If it is not specified and *filename* is also not specified, do not add
- the header. The only valid values for *disposition* are ``attachment``
- and ``inline``.
+ If *disposition* is not specified and *filename* is also not specified,
+ do not add the header. The only valid values for *disposition* are
+ ``attachment`` and ``inline``.
If *filename* is specified, use it as the value of the ``filename``
- parameter of the :mailheader:`Content-Disposition` header. There is no
- default.
+ parameter of the :mailheader:`Content-Disposition` header.
If *cid* is specified, add a :mailheader:`Content-ID` header with
*cid* as its value.
@@ -435,3 +199,9 @@ Currently the email package provides only one concrete content manager,
``headername: headervalue`` or a list of ``header`` objects
(distinguished from strings by having a ``name`` attribute), add the
headers to *msg*.
+
+
+.. rubric:: Footnotes
+
+.. [1] Oringally added in 3.4 as a :term:`provisional module <provisional
+ package>`
diff --git a/Doc/library/email.encoders.rst b/Doc/library/email.encoders.rst
index 9d7f9bf..e24ac7b 100644
--- a/Doc/library/email.encoders.rst
+++ b/Doc/library/email.encoders.rst
@@ -8,6 +8,12 @@
--------------
+This module is part of the legacy (``Compat32``) email API. In the
+new API the functionality is provided by the *cte* parameter of
+the :meth:`~email.message.EmailMessage.set_content` method.
+
+The remaining text in this section is the original documentation of the module.
+
When creating :class:`~email.message.Message` objects from scratch, you often
need to encode the payloads for transport through compliant mail servers. This
is especially true for :mimetype:`image/\*` and :mimetype:`text/\*` type messages
diff --git a/Doc/library/email.errors.rst b/Doc/library/email.errors.rst
index 8470783..2d0d192 100644
--- a/Doc/library/email.errors.rst
+++ b/Doc/library/email.errors.rst
@@ -20,33 +20,27 @@ The following exception classes are defined in the :mod:`email.errors` module:
.. exception:: MessageParseError()
- This is the base class for exceptions raised by the :class:`~email.parser.Parser`
- class. It is derived from :exc:`MessageError`.
+ This is the base class for exceptions raised by the
+ :class:`~email.parser.Parser` class. It is derived from
+ :exc:`MessageError`. This class is also used internally by the parser used
+ by :mod:`~email.headerregistry`.
.. exception:: HeaderParseError()
- Raised under some error conditions when parsing the :rfc:`2822` headers of a
- message, this class is derived from :exc:`MessageParseError`. It can be raised
- from the :meth:`Parser.parse <email.parser.Parser.parse>` or
- :meth:`Parser.parsestr <email.parser.Parser.parsestr>` methods.
-
- Situations where it can be raised include finding an envelope header after the
- first :rfc:`2822` header of the message, finding a continuation line before the
- first :rfc:`2822` header is found, or finding a line in the headers which is
- neither a header or a continuation line.
+ Raised under some error conditions when parsing the :rfc:`5322` headers of a
+ message, this class is derived from :exc:`MessageParseError`. The
+ :meth:`~email.message.EmailMessage.set_boundary` method will raise this
+ error if the content type is unknown when the method is called.
+ :class:`~email.header.Header` may raise this error for certain base64
+ decoding errors, and when an attempt is made to create a header that appears
+ to contain an embedded header (that is, there is what is supposed to be a
+ continuation line that has no leading whitespace and looks like a header).
.. exception:: BoundaryError()
- Raised under some error conditions when parsing the :rfc:`2822` headers of a
- message, this class is derived from :exc:`MessageParseError`. It can be raised
- from the :meth:`Parser.parse <email.parser.Parser.parse>` or
- :meth:`Parser.parsestr <email.parser.Parser.parsestr>` methods.
-
- Situations where it can be raised include not being able to find the starting or
- terminating boundary in a :mimetype:`multipart/\*` message when strict parsing
- is used.
+ Deprecated and no longer used.
.. exception:: MultipartConversionError()
@@ -64,14 +58,14 @@ The following exception classes are defined in the :mod:`email.errors` module:
:class:`~email.mime.nonmultipart.MIMENonMultipart` (e.g.
:class:`~email.mime.image.MIMEImage`).
-Here's the list of the defects that the :class:`~email.parser.FeedParser`
+
+Here is the list of the defects that the :class:`~email.parser.FeedParser`
can find while parsing messages. Note that the defects are added to the message
where the problem was found, so for example, if a message nested inside a
:mimetype:`multipart/alternative` had a malformed header, that nested message
object would have a defect, but the containing messages would not.
-All defect classes are subclassed from :class:`email.errors.MessageDefect`, but
-this class is *not* an exception!
+All defect classes are subclassed from :class:`email.errors.MessageDefect`.
* :class:`NoBoundaryInMultipartDefect` -- A message claimed to be a multipart,
but had no :mimetype:`boundary` parameter.
diff --git a/Doc/library/email-examples.rst b/Doc/library/email.examples.rst
index ad93b5c..84e9aee 100644
--- a/Doc/library/email-examples.rst
+++ b/Doc/library/email.examples.rst
@@ -6,13 +6,14 @@
Here are a few examples of how to use the :mod:`email` package to read, write,
and send simple email messages, as well as more complex MIME messages.
-First, let's see how to create and send a simple text message:
+First, let's see how to create and send a simple text message (both the
+text content and the addresses may contain unicode characters):
.. literalinclude:: ../includes/email-simple.py
-And parsing RFC822 headers can easily be done by the parse(filename) or
-parsestr(message_as_string) methods of the Parser() class:
+Parsing RFC822 headers can easily be done by the using the classes
+from the :mod:`~email.parser` module:
.. literalinclude:: ../includes/email-headers.py
@@ -34,30 +35,19 @@ above, into a directory of files:
.. literalinclude:: ../includes/email-unpack.py
+
Here's an example of how to create an HTML message with an alternative plain
-text version: [2]_
+text version. To make things a bit more interesting, we include a related
+image in the html part, and we save a copy of what we are going to send to
+disk, as well as sending it.
.. literalinclude:: ../includes/email-alternative.py
-.. _email-contentmanager-api-examples:
-
-Examples using the Provisional API
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-Here is a reworking of the last example using the provisional API. To make
-things a bit more interesting, we include a related image in the html part, and
-we save a copy of what we are going to send to disk, as well as sending it.
-
-This example also shows how easy it is to include non-ASCII, and simplifies the
-sending of the message using the :meth:`.send_message` method of the
-:mod:`smtplib` module.
-
-.. literalinclude:: ../includes/email-alternative-new-api.py
-
-If we were instead sent the message from the last example, here is one
-way we could process it:
+If we were sent the message from the last example, here is one way we could
+process it:
-.. literalinclude:: ../includes/email-read-alternative-new-api.py
+.. literalinclude:: ../includes/email-read-alternative.py
Up to the prompt, the output from the above is:
@@ -75,4 +65,3 @@ Up to the prompt, the output from the above is:
.. rubric:: Footnotes
.. [1] Thanks to Matthew Dixon Cowles for the original inspiration and examples.
-.. [2] Contributed by Martin Matejek.
diff --git a/Doc/library/email.generator.rst b/Doc/library/email.generator.rst
index d596ed8..ab0fbc2 100644
--- a/Doc/library/email.generator.rst
+++ b/Doc/library/email.generator.rst
@@ -8,210 +8,244 @@
--------------
-One of the most common tasks is to generate the flat text of the email message
-represented by a message object structure. You will need to do this if you want
-to send your message via the :mod:`smtplib` module or the :mod:`nntplib` module,
-or print the message on the console. Taking a message object structure and
-producing a flat text document is the job of the :class:`Generator` class.
-
-Again, as with the :mod:`email.parser` module, you aren't limited to the
-functionality of the bundled generator; you could write one from scratch
-yourself. However the bundled generator knows how to generate most email in a
-standards-compliant way, should handle MIME and non-MIME email messages just
-fine, and is designed so that the transformation from flat text, to a message
-structure via the :class:`~email.parser.Parser` class, and back to flat text,
-is idempotent (the input is identical to the output) [#]_. On the other hand,
-using the Generator on a :class:`~email.message.Message` constructed by program
-may result in changes to the :class:`~email.message.Message` object as defaults
-are filled in.
-
-:class:`bytes` output can be generated using the :class:`BytesGenerator` class.
-If the message object structure contains non-ASCII bytes, this generator's
-:meth:`~BytesGenerator.flatten` method will emit the original bytes. Parsing a
-binary message and then flattening it with :class:`BytesGenerator` should be
-idempotent for standards compliant messages.
-
-Here are the public methods of the :class:`Generator` class, imported from the
-:mod:`email.generator` module:
-
-
-.. class:: Generator(outfp, mangle_from_=True, maxheaderlen=78, *, policy=None)
-
- The constructor for the :class:`Generator` class takes a :term:`file-like object`
- called *outfp* for an argument. *outfp* must support the :meth:`write` method
- and be usable as the output file for the :func:`print` function.
-
- Optional *mangle_from_* is a flag that, when ``True``, puts a ``>`` character in
- front of any line in the body that starts exactly as ``From``, i.e. ``From``
- followed by a space at the beginning of the line. This is the only guaranteed
- portable way to avoid having such lines be mistaken for a Unix mailbox format
- envelope header separator (see `WHY THE CONTENT-LENGTH FORMAT IS BAD
- <https://www.jwz.org/doc/content-length.html>`_ for details). *mangle_from_*
- defaults to ``True``, but you might want to set this to ``False`` if you are not
- writing Unix mailbox format files.
-
- Optional *maxheaderlen* specifies the longest length for a non-continued header.
- When a header line is longer than *maxheaderlen* (in characters, with tabs
- expanded to 8 spaces), the header will be split as defined in the
- :class:`~email.header.Header` class. Set to zero to disable header wrapping.
- The default is 78, as recommended (but not required) by :rfc:`2822`.
-
- The *policy* keyword specifies a :mod:`~email.policy` object that controls a
- number of aspects of the generator's operation. If no *policy* is specified,
- then the *policy* attached to the message object passed to :attr:`flatten`
- is used.
+One of the most common tasks is to generate the flat (serialized) version of
+the email message represented by a message object structure. You will need to
+do this if you want to send your message via :meth:`smtplib.SMTP.sendmail` or
+the :mod:`nntplib` module, or print the message on the console. Taking a
+message object structure and producing a serialized representation is the job
+of the generator classes.
+
+As with the :mod:`email.parser` module, you aren't limited to the functionality
+of the bundled generator; you could write one from scratch yourself. However
+the bundled generator knows how to generate most email in a standards-compliant
+way, should handle MIME and non-MIME email messages just fine, and is designed
+so that the bytes-oriented parsing and generation operations are inverses,
+assuming the same non-transforming :mod:`~email.policy` is used for both. That
+is, parsing the serialized byte stream via the
+:class:`~email.parser.BytesParser` class and then regenerating the serialized
+byte stream using :class:`BytesGenerator` should produce output identical to
+the input [#]_. (On the other hand, using the generator on an
+:class:`~email.message.EmailMessage` constructed by program may result in
+changes to the :class:`~email.message.EmailMessage` object as defaults are
+filled in.)
+
+The :class:`Generator` class can be used to flatten a message into a text (as
+opposed to binary) serialized representation, but since Unicode cannot
+represent binary data directly, the message is of necessity transformed into
+something that contains only ASCII characters, using the standard email RFC
+Content Transfer Encoding techniques for encoding email messages for transport
+over channels that are not "8 bit clean".
+
+
+.. class:: BytesGenerator(outfp, mangle_from_=None, maxheaderlen=None, *, \
+ policy=None)
- .. versionchanged:: 3.3 Added the *policy* keyword.
+ Return a :class:`BytesGenerator` object that will write any message provided
+ to the :meth:`flatten` method, or any surrogateescape encoded text provided
+ to the :meth:`write` method, to the :term:`file-like object` *outfp*.
+ *outfp* must support a ``write`` method that accepts binary data.
+
+ If optional *mangle_from_* is ``True``, put a ``>`` character in front of
+ any line in the body that starts with the exact string ``"From "``, that is
+ ``From`` followed by a space at the beginning of a line. *mangle_from_*
+ defaults to the value of the :attr:`~email.policy.Policy.mangle_from_`
+ setting of the *policy* (which is ``True`` for the
+ :data:`~email.policy.compat32` policy and ``False`` for all others).
+ *mangle_from_* is intended for use when messages are stored in unix mbox
+ format (see :mod:`mailbox` and `WHY THE CONTENT-LENGTH FORMAT IS BAD
+ <http://www.jwz.org/doc/content-length.html>`_).
+
+ If *maxheaderlen* is not ``None``, refold any header lines that are longer
+ than *maxheaderlen*, or if ``0``, do not rewrap any headers. If
+ *manheaderlen* is ``None`` (the default), wrap headers and other message
+ lines according to the *policy* settings.
+
+ If *policy* is specified, use that policy to control message generation. If
+ *policy* is ``None`` (the default), use the policy associated with the
+ :class:`~email.message.Message` or :class:`~email.message.EmailMessage`
+ object passed to ``flatten`` to control the message generation. See
+ :mod:`email.policy` for details on what *policy* controls.
- The other public :class:`Generator` methods are:
+ .. versionadded:: 3.2
+ .. versionchanged:: 3.3 Added the *policy* keyword.
- .. method:: flatten(msg, unixfrom=False, linesep=None)
+ .. versionchanged:: 3.6 The default behavior of the *mangle_from_*
+ and *maxheaderlen* parameters is to follow the policy.
- Print the textual representation of the message object structure rooted at
- *msg* to the output file specified when the :class:`Generator` instance
- was created. Subparts are visited depth-first and the resulting text will
- be properly MIME encoded.
- Optional *unixfrom* is a flag that forces the printing of the envelope
- header delimiter before the first :rfc:`2822` header of the root message
- object. If the root object has no envelope header, a standard one is
- crafted. By default, this is set to ``False`` to inhibit the printing of
- the envelope delimiter.
+ .. method:: flatten(msg, unixfrom=False, linesep=None)
+ Print the textual representation of the message object structure rooted
+ at *msg* to the output file specified when the :class:`BytesGenerator`
+ instance was created.
+
+ If the :mod:`~email.policy` option :attr:`~email.policy.Policy.cte_type`
+ is ``8bit`` (the default), copy any headers in the original parsed
+ message that have not been modified to the output with any bytes with the
+ high bit set reproduced as in the original, and preserve the non-ASCII
+ :mailheader:`Content-Transfer-Encoding` of any body parts that have them.
+ If ``cte_type`` is ``7bit``, convert the bytes with the high bit set as
+ needed using an ASCII-compatible :mailheader:`Content-Transfer-Encoding`.
+ That is, transform parts with non-ASCII
+ :mailheader:`Cotnent-Transfer-Encoding`
+ (:mailheader:`Content-Transfer-Encoding: 8bit`) to an ASCII compatibile
+ :mailheader:`Content-Transfer-Encoding`, and encode RFC-invalid non-ASCII
+ bytes in headers using the MIME ``unknown-8bit`` character set, thus
+ rendering them RFC-compliant.
+
+ .. XXX: There should be an option that just does the RFC
+ compliance transformation on headers but leaves CTE 8bit parts alone.
+
+ If *unixfrom* is ``True``, print the envelope header delimiter used by
+ the Unix mailbox format (see :mod:`mailbox`) before the first of the
+ :rfc:`5322` headers of the root message object. If the root object has
+ no envelope header, craft a standard one. The default is ``False``.
Note that for subparts, no envelope header is ever printed.
- Optional *linesep* specifies the line separator character used to
- terminate lines in the output. If specified it overrides the value
- specified by the *msg*\'s or ``Generator``\'s ``policy``.
+ If *linesep* is not ``None``, use it as the separator character between
+ all the lines of the flattened message. If *linesep* is ``None`` (the
+ default), use the value specified in the *policy*.
- Because strings cannot represent non-ASCII bytes, if the policy that
- applies when ``flatten`` is run has :attr:`~email.policy.Policy.cte_type`
- set to ``8bit``, ``Generator`` will operate as if it were set to
- ``7bit``. This means that messages parsed with a Bytes parser that have
- a :mailheader:`Content-Transfer-Encoding` of ``8bit`` will be converted
- to a use a ``7bit`` Content-Transfer-Encoding. Non-ASCII bytes in the
- headers will be :rfc:`2047` encoded with a charset of ``unknown-8bit``.
+ .. XXX: flatten should take a *policy* keyword.
- .. versionchanged:: 3.2
- Added support for re-encoding ``8bit`` message bodies, and the
- *linesep* argument.
.. method:: clone(fp)
- Return an independent clone of this :class:`Generator` instance with the
- exact same options.
-
- .. method:: write(s)
-
- Write the string *s* to the underlying file object, i.e. *outfp* passed to
- :class:`Generator`'s constructor. This provides just enough file-like API
- for :class:`Generator` instances to be used in the :func:`print` function.
+ Return an independent clone of this :class:`BytesGenerator` instance with
+ the exact same option settings, and *fp* as the new *outfp*.
-As a convenience, see the :class:`~email.message.Message` methods
-:meth:`~email.message.Message.as_string` and ``str(aMessage)``, a.k.a.
-:meth:`~email.message.Message.__str__`, which simplify the generation of a
-formatted string representation of a message object. For more detail, see
-:mod:`email.message`.
-.. class:: BytesGenerator(outfp, mangle_from_=True, maxheaderlen=78, *, \
- policy=None)
+ .. method:: write(s)
- The constructor for the :class:`BytesGenerator` class takes a binary
- :term:`file-like object` called *outfp* for an argument. *outfp* must
- support a :meth:`write` method that accepts binary data.
+ Encode *s* using the ``ASCII`` codec and the ``surrogateescape`` error
+ handler, and pass it to the *write* method of the *outfp* passed to the
+ :class:`BytesGenerator`'s constructor.
- Optional *mangle_from_* is a flag that, when ``True``, puts a ``>``
- character in front of any line in the body that starts exactly as ``From``,
- i.e. ``From`` followed by a space at the beginning of the line. This is the
- only guaranteed portable way to avoid having such lines be mistaken for a
- Unix mailbox format envelope header separator (see `WHY THE CONTENT-LENGTH
- FORMAT IS BAD <https://www.jwz.org/doc/content-length.html>`_ for details).
- *mangle_from_* defaults to ``True``, but you might want to set this to
- ``False`` if you are not writing Unix mailbox format files.
- Optional *maxheaderlen* specifies the longest length for a non-continued
- header. When a header line is longer than *maxheaderlen* (in characters,
- with tabs expanded to 8 spaces), the header will be split as defined in the
- :class:`~email.header.Header` class. Set to zero to disable header
- wrapping. The default is 78, as recommended (but not required) by
- :rfc:`2822`.
+As a convenience, :class:`~email.message.EmailMessage` provides the methods
+:meth:`~email.message.EmailMessage.as_bytes` and ``bytes(aMessage)`` (a.k.a.
+:meth:`~email.message.EmailMessage.__bytes__`), which simplify the generation of
+a serialized binary representation of a message object. For more detail, see
+:mod:`email.message`.
- The *policy* keyword specifies a :mod:`~email.policy` object that controls a
- number of aspects of the generator's operation. If no *policy* is specified,
- then the *policy* attached to the message object passed to :attr:`flatten`
- is used.
+Because strings cannot represent binary data, the :class:`Generator` class must
+convert any binary data in any message it flattens to an ASCII compatible
+format, by converting them to an ASCII compatible
+:mailheader:`Content-Transfer_Encoding`. Using the terminology of the email
+RFCs, you can think of this as :class:`Generator` serializing to an I/O stream
+that is not "8 bit clean". In other words, most applications will want
+to be using :class:`BytesGenerator`, and not :class:`Generator`.
+
+.. class:: Generator(outfp, mangle_from_=None, maxheaderlen=None, *, \
+ policy=None)
+
+ Return a :class:`Generator` object that will write any message provided
+ to the :meth:`flatten` method, or any text provided to the :meth:`write`
+ method, to the :term:`file-like object` *outfp*. *outfp* must support a
+ ``write`` method that accepts string data.
+
+ If optional *mangle_from_* is ``True``, put a ``>`` character in front of
+ any line in the body that starts with the exact string ``"From "``, that is
+ ``From`` followed by a space at the beginning of a line. *mangle_from_*
+ defaults to the value of the :attr:`~email.policy.Policy.mangle_from_`
+ setting of the *policy* (which is ``True`` for the
+ :data:`~email.policy.compat32` policy and ``False`` for all others).
+ *mangle_from_* is intended for use when messages are stored in unix mbox
+ format (see :mod:`mailbox` and `WHY THE CONTENT-LENGTH FORMAT IS BAD
+ <http://www.jwz.org/doc/content-length.html>`_).
+
+ If *maxheaderlen* is not ``None``, refold any header lines that are longer
+ than *maxheaderlen*, or if ``0``, do not rewrap any headers. If
+ *manheaderlen* is ``None`` (the default), wrap headers and other message
+ lines according to the *policy* settings.
+
+ If *policy* is specified, use that policy to control message generation. If
+ *policy* is ``None`` (the default), use the policy associated with the
+ :class:`~email.message.Message` or :class:`~email.message.EmailMessage`
+ object passed to ``flatten`` to control the message generation. See
+ :mod:`email.policy` for details on what *policy* controls.
.. versionchanged:: 3.3 Added the *policy* keyword.
- The other public :class:`BytesGenerator` methods are:
+ .. versionchanged:: 3.6 The default behavior of the *mangle_from_*
+ and *maxheaderlen* parameters is to follow the policy.
.. method:: flatten(msg, unixfrom=False, linesep=None)
Print the textual representation of the message object structure rooted
- at *msg* to the output file specified when the :class:`BytesGenerator`
- instance was created. Subparts are visited depth-first and the resulting
- text will be properly MIME encoded. If the :mod:`~email.policy` option
- :attr:`~email.policy.Policy.cte_type` is ``8bit`` (the default),
- then any bytes with the high bit set in the original parsed message that
- have not been modified will be copied faithfully to the output. If
- ``cte_type`` is ``7bit``, the bytes will be converted as needed
- using an ASCII-compatible Content-Transfer-Encoding. In particular,
- RFC-invalid non-ASCII bytes in headers will be encoded using the MIME
- ``unknown-8bit`` character set, thus rendering them RFC-compliant.
-
- .. XXX: There should be a complementary option that just does the RFC
- compliance transformation but leaves CTE 8bit parts alone.
-
- Messages parsed with a Bytes parser that have a
- :mailheader:`Content-Transfer-Encoding` of 8bit will be reconstructed
- as 8bit if they have not been modified.
-
- Optional *unixfrom* is a flag that forces the printing of the envelope
- header delimiter before the first :rfc:`2822` header of the root message
- object. If the root object has no envelope header, a standard one is
- crafted. By default, this is set to ``False`` to inhibit the printing of
- the envelope delimiter.
-
+ at *msg* to the output file specified when the :class:`Generator`
+ instance was created.
+
+ If the :mod:`~email.policy` option :attr:`~email.policy.Policy.cte_type`
+ is ``8bit``, generate the message as if the option were set to ``7bit``.
+ (This is required because strings cannot represent non-ASCII bytes.)
+ Convert any bytes with the high bit set as needed using an
+ ASCII-compatible :mailheader:`Content-Transfer-Encoding`. That is,
+ transform parts with non-ASCII :mailheader:`Cotnent-Transfer-Encoding`
+ (:mailheader:`Content-Transfer-Encoding: 8bit`) to an ASCII compatibile
+ :mailheader:`Content-Transfer-Encoding`, and encode RFC-invalid non-ASCII
+ bytes in headers using the MIME ``unknown-8bit`` character set, thus
+ rendering them RFC-compliant.
+
+ If *unixfrom* is ``True``, print the envelope header delimiter used by
+ the Unix mailbox format (see :mod:`mailbox`) before the first of the
+ :rfc:`5322` headers of the root message object. If the root object has
+ no envelope header, craft a standard one. The default is ``False``.
Note that for subparts, no envelope header is ever printed.
- Optional *linesep* specifies the line separator character used to
- terminate lines in the output. If specified it overrides the value
- specified by the ``Generator``\ or *msg*\ 's ``policy``.
+ If *linesep* is not ``None``, use it as the separator character between
+ all the lines of the flattened message. If *linesep* is ``None`` (the
+ default), use the value specified in the *policy*.
+
+ .. XXX: flatten should take a *policy* keyword.
+
+ .. versionchanged:: 3.2
+ Added support for re-encoding ``8bit`` message bodies, and the
+ *linesep* argument.
+
.. method:: clone(fp)
- Return an independent clone of this :class:`BytesGenerator` instance with
- the exact same options.
+ Return an independent clone of this :class:`Generator` instance with the
+ exact same options, and *fp* as the new *outfp*.
+
.. method:: write(s)
- Write the string *s* to the underlying file object. *s* is encoded using
- the ``ASCII`` codec and written to the *write* method of the *outfp*
- *outfp* passed to the :class:`BytesGenerator`'s constructor. This
- provides just enough file-like API for :class:`BytesGenerator` instances
- to be used in the :func:`print` function.
+ Write *s* to the *write* method of the *outfp* passed to the
+ :class:`Generator`'s constructor. This provides just enough file-like
+ API for :class:`Generator` instances to be used in the :func:`print`
+ function.
- .. versionadded:: 3.2
-The :mod:`email.generator` module also provides a derived class, called
-:class:`DecodedGenerator` which is like the :class:`Generator` base class,
-except that non-\ :mimetype:`text` parts are substituted with a format string
-representing the part.
+As a convenience, :class:`~email.message.EmailMessage` provides the methods
+:meth:`~email.message.EmailMessage.as_string` and ``str(aMessage)`` (a.k.a.
+:meth:`~email.message.EmailMessage.__str__`), which simplify the generation of
+a formatted string representation of a message object. For more detail, see
+:mod:`email.message`.
+
+The :mod:`email.generator` module also provides a derived class,
+:class:`DecodedGenerator`, which is like the :class:`Generator` base class,
+except that non-\ :mimetype:`text` parts are not serialized, but are instead
+represented in the output stream by a string derived from a template filled
+in with information about the part.
-.. class:: DecodedGenerator(outfp, mangle_from_=True, maxheaderlen=78, fmt=None)
+.. class:: DecodedGenerator(outfp, mangle_from_=None, maxheaderlen=None, \
+ fmt=None, *, policy=None)
- This class, derived from :class:`Generator` walks through all the subparts of a
- message. If the subpart is of main type :mimetype:`text`, then it prints the
- decoded payload of the subpart. Optional *_mangle_from_* and *maxheaderlen* are
- as with the :class:`Generator` base class.
+ Act like :class:`Generator`, except that for any subpart of the message
+ passed to :meth:`Generator.flatten`, if the subpart is of main type
+ :mimetype:`text`, print the decoded payload of the subpart, and if the main
+ type is not :mimetype:`text`, instead of printing it fill in the string
+ *fmt* using information from the part and print the resulting
+ filled-in string.
- If the subpart is not of main type :mimetype:`text`, optional *fmt* is a format
- string that is used instead of the message payload. *fmt* is expanded with the
- following keywords, ``%(keyword)s`` format:
+ To fill in *fmt*, execute ``fmt % part_info``, where ``part_info``
+ is a dictionary composed of the following keys and values:
* ``type`` -- Full MIME type of the non-\ :mimetype:`text` part
@@ -225,15 +259,21 @@ representing the part.
* ``encoding`` -- Content transfer encoding of the non-\ :mimetype:`text` part
- The default value for *fmt* is ``None``, meaning ::
+ If *fmt* is ``None``, use the following default *fmt*:
+
+ "[Non-text (%(type)s) part of message omitted, filename %(filename)s]"
- [Non-text (%(type)s) part of message omitted, filename %(filename)s]
+ Optional *_mangle_from_* and *maxheaderlen* are as with the
+ :class:`Generator` base class.
.. rubric:: Footnotes
-.. [#] This statement assumes that you use the appropriate setting for the
- ``unixfrom`` argument, and that you set maxheaderlen=0 (which will
- preserve whatever the input line lengths were). It is also not strictly
- true, since in many cases runs of whitespace in headers are collapsed
- into single blanks. The latter is a bug that will eventually be fixed.
+.. [#] This statement assumes that you use the appropriate setting for
+ ``unixfrom``, and that there are no :mod:`policy` settings calling for
+ automatic adjustments (for example,
+ :attr:`~email.policy.Policy.refold_source` must be ``none``, which is
+ *not* the default). It is also not 100% true, since if the message
+ does not conform to the RFC standards occasionally information about the
+ exact original text is lost during parsing error recovery. It is a goal
+ to fix these latter edge cases when possible.
diff --git a/Doc/library/email.header.rst b/Doc/library/email.header.rst
index e94837c..07152c2 100644
--- a/Doc/library/email.header.rst
+++ b/Doc/library/email.header.rst
@@ -8,6 +8,14 @@
--------------
+This module is part of the legacy (``Compat32``) email API. In the current API
+encoding and decoding of headers is handled transparently by the
+dictionary-like API of the :class:`~email.message.EmailMessage` class. In
+addition to uses in legacy code, this module can be useful in applications that
+need to completely control the character sets used when encoding headers.
+
+The remaining text in this section is the original documentation of the module.
+
:rfc:`2822` is the base standard that describes the format of email messages.
It derives from the older :rfc:`822` standard which came into widespread use at
a time when most email was composed of ASCII characters only. :rfc:`2822` is a
diff --git a/Doc/library/email.headerregistry.rst b/Doc/library/email.headerregistry.rst
index 0707bd8..2c830cf 100644
--- a/Doc/library/email.headerregistry.rst
+++ b/Doc/library/email.headerregistry.rst
@@ -7,20 +7,12 @@
.. moduleauthor:: R. David Murray <rdmurray@bitdance.com>
.. sectionauthor:: R. David Murray <rdmurray@bitdance.com>
-.. versionadded:: 3.3
- as a :term:`provisional module <provisional package>`.
-
**Source code:** :source:`Lib/email/headerregistry.py`
-.. note::
-
- The headerregistry module has been included in the standard library on a
- :term:`provisional basis <provisional package>`. Backwards incompatible
- changes (up to and including removal of the module) may occur if deemed
- necessary by the core developers.
-
--------------
+.. versionadded:: 3.6 [1]_
+
Headers are represented by customized subclasses of :class:`str`. The
particular class used to represent a given header is determined by the
:attr:`~email.policy.EmailPolicy.header_factory` of the :mod:`~email.policy` in
@@ -86,10 +78,11 @@ headers.
.. method:: fold(*, policy)
Return a string containing :attr:`~email.policy.Policy.linesep`
- characters as required to correctly fold the header according
- to *policy*. A :attr:`~email.policy.Policy.cte_type` of
- ``8bit`` will be treated as if it were ``7bit``, since strings
- may not contain binary data.
+ characters as required to correctly fold the header according to
+ *policy*. A :attr:`~email.policy.Policy.cte_type` of ``8bit`` will be
+ treated as if it were ``7bit``, since headers may not contain arbitrary
+ binary data. If :attr:`~email.policy.EmailPolicy.utf8` is ``False``,
+ non-ASCII data will be :rfc:`2047` encoded.
``BaseHeader`` by itself cannot be used to create a header object. It
@@ -106,7 +99,7 @@ headers.
values for at least the keys ``decoded`` and ``defects``. ``decoded``
should be the string value for the header (that is, the header value fully
decoded to unicode). The parse method should assume that *string* may
- contain transport encoded parts, but should correctly handle all valid
+ contain content-transfer-encoded parts, but should correctly handle all valid
unicode characters as well so that it can parse un-encoded header values.
``BaseHeader``'s ``__new__`` then creates the header instance, and calls its
@@ -135,11 +128,10 @@ headers.
mechanism for encoding non-ASCII text as ASCII characters within a header
value. When a *value* containing encoded words is passed to the
constructor, the ``UnstructuredHeader`` parser converts such encoded words
- back in to the original unicode, following the :rfc:`2047` rules for
- unstructured text. The parser uses heuristics to attempt to decode certain
- non-compliant encoded words. Defects are registered in such cases, as well
- as defects for issues such as invalid characters within the encoded words or
- the non-encoded text.
+ into unicode, following the :rfc:`2047` rules for unstructured text. The
+ parser uses heuristics to attempt to decode certain non-compliant encoded
+ words. Defects are registered in such cases, as well as defects for issues
+ such as invalid characters within the encoded words or the non-encoded text.
This header type provides no additional attributes.
@@ -213,15 +205,16 @@ headers.
the list of addresses is "flattened" into a one dimensional list).
The ``decoded`` value of the header will have all encoded words decoded to
- unicode. :class:`~encodings.idna` encoded domain names are also decoded to unicode. The
- ``decoded`` value is set by :attr:`~str.join`\ ing the :class:`str` value of
- the elements of the ``groups`` attribute with ``', '``.
+ unicode. :class:`~encodings.idna` encoded domain names are also decoded to
+ unicode. The ``decoded`` value is set by :attr:`~str.join`\ ing the
+ :class:`str` value of the elements of the ``groups`` attribute with ``',
+ '``.
A list of :class:`.Address` and :class:`.Group` objects in any combination
may be used to set the value of an address header. ``Group`` objects whose
``display_name`` is ``None`` will be interpreted as single addresses, which
allows an address list to be copied with groups intact by using the list
- obtained ``groups`` attribute of the source header.
+ obtained from the ``groups`` attribute of the source header.
.. class:: SingleAddressHeader
@@ -267,7 +260,7 @@ variant, :attr:`~.BaseHeader.max_count` is set to 1.
.. class:: ParameterizedMIMEHeader
- MOME headers all start with the prefix 'Content-'. Each specific header has
+ MIME headers all start with the prefix 'Content-'. Each specific header has
a certain value, described under the class for that header. Some can
also take a list of supplemental parameters, which have a common format.
This class serves as a base for all the MIME headers that take parameters.
@@ -454,3 +447,9 @@ construct structured values to assign to specific headers.
``display_name`` is none and there is a single ``Address`` in the
``addresses`` list, the ``str`` value will be the same as the ``str`` of
that single ``Address``.
+
+
+.. rubric:: Footnotes
+
+.. [1] Oringally added in 3.3 as a :term:`provisional module <provisional
+ package>`
diff --git a/Doc/library/email.message.rst b/Doc/library/email.message.rst
index 2907975..32852e7 100644
--- a/Doc/library/email.message.rst
+++ b/Doc/library/email.message.rst
@@ -3,91 +3,102 @@
.. module:: email.message
:synopsis: The base class representing email messages.
+.. moduleauthor:: R. David Murray <rdmurray@bitdance.com>
+.. sectionauthor:: R. David Murray <rdmurray@bitdance.com>,
+ Barry A. Warsaw <barry@python.org>
**Source code:** :source:`Lib/email/message.py`
--------------
-The central class in the :mod:`email` package is the :class:`Message` class,
-imported from the :mod:`email.message` module. It is the base class for the
-:mod:`email` object model. :class:`Message` provides the core functionality for
-setting and querying header fields, and for accessing message bodies.
-
-Conceptually, a :class:`Message` object consists of *headers* and *payloads*.
-Headers are :rfc:`2822` style field names and values where the field name and
-value are separated by a colon. The colon is not part of either the field name
-or the field value.
-
-Headers are stored and returned in case-preserving form but are matched
-case-insensitively. There may also be a single envelope header, also known as
-the *Unix-From* header or the ``From_`` header. The payload is either a string
-in the case of simple message objects or a list of :class:`Message` objects for
-MIME container documents (e.g. :mimetype:`multipart/\*` and
-:mimetype:`message/rfc822`).
-
-:class:`Message` objects provide a mapping style interface for accessing the
-message headers, and an explicit interface for accessing both the headers and
-the payload. It provides convenience methods for generating a flat text
-representation of the message object tree, for accessing commonly used header
-parameters, and for recursively walking over the object tree.
-
-Here are the methods of the :class:`Message` class:
-
-
-.. class:: Message(policy=compat32)
-
- If *policy* is specified (it must be an instance of a :mod:`~email.policy`
- class) use the rules it specifies to update and serialize the representation
- of the message. If *policy* is not set, use the :class:`compat32
- <email.policy.Compat32>` policy, which maintains backward compatibility with
- the Python 3.2 version of the email package. For more information see the
+.. versionadded:: 3.6 [1]_
+
+The central class in the :mod:`email` package is the :class:`EmailMessage`
+class, imported from the :mod:`email.message` module. It is the base class for
+the :mod:`email` object model. :class:`EmailMessage` provides the core
+functionality for setting and querying header fields, for accessing message
+bodies, and for creating or modifying structured messages.
+
+An email message consists of *headers* and a *payload* (which is also referred
+to as the *content*). Headers are :rfc:`5322` or :rfc:`6532` style field names
+and values, where the field name and value are separated by a colon. The colon
+is not part of either the field name or the field value. The payload may be a
+simple text message, or a binary object, or a structured sequence of
+sub-messages each with their own set of headers and their own payload. The
+latter type of payload is indicated by the message having a MIME type such as
+:mimetype:`multipart/\*` or :mimetype:`message/rfc822`.
+
+The conceptual model provided by an :class:`EmailMessage` object is that of an
+ordered dictionary of headers coupled with a *payload* that represents the
+:rfc:`5322` body of the message, which might be a list of sub-``EmailMessage``
+objects. In addition to the normal dictionary methods for accessing the header
+names and values, there are methods for accessing specialized information from
+the headers (for example the MIME content type), for operating on the payload,
+for generating a serialized version of the message, and for recursively walking
+over the object tree.
+
+The :class:`EmailMessage` dictionary-like interface is indexed by the header
+names, which must be ASCII values. The values of the dictionary are strings
+with some extra methods. Headers are stored and returned in case-preserving
+form, but field names are matched case-insensitively. Unlike a real dict,
+there is an ordering to the keys, and there can be duplicate keys. Additional
+methods are provided for working with headers that have duplicate keys.
+
+The *payload* is either a string or bytes object, in the case of simple message
+objects, or a list of :class:`EmailMessage` objects, for MIME container
+documents such as :mimetype:`multipart/\*` and :mimetype:`message/rfc822`
+message objects.
+
+
+.. class:: EmailMessage(policy=default)
+
+ If *policy* is specified use the rules it specifies to udpate and serialize
+ the representation of the message. If *policy* is not set, use the
+ :class:`~email.policy.default` policy, which follows the rules of the email
+ RFCs except for line endings (instead of the RFC mandated ``\r\n``, it uses
+ the Python standard ``\n`` line endings). For more information see the
:mod:`~email.policy` documentation.
- .. versionchanged:: 3.3 The *policy* keyword argument was added.
-
-
- .. method:: as_string(unixfrom=False, maxheaderlen=0, policy=None)
-
- Return the entire message flattened as a string. When optional *unixfrom*
- is true, the envelope header is included in the returned string.
- *unixfrom* defaults to ``False``. For backward compabitility reasons,
- *maxheaderlen* defaults to ``0``, so if you want a different value you
- must override it explicitly (the value specified for *max_line_length* in
- the policy will be ignored by this method). The *policy* argument may be
- used to override the default policy obtained from the message instance.
- This can be used to control some of the formatting produced by the
- method, since the specified *policy* will be passed to the ``Generator``.
-
- Flattening the message may trigger changes to the :class:`Message` if
- defaults need to be filled in to complete the transformation to a string
- (for example, MIME boundaries may be generated or modified).
-
- Note that this method is provided as a convenience and may not always
- format the message the way you want. For example, by default it does
- not do the mangling of lines that begin with ``From`` that is
- required by the unix mbox format. For more flexibility, instantiate a
- :class:`~email.generator.Generator` instance and use its
- :meth:`~email.generator.Generator.flatten` method directly. For example::
-
- from io import StringIO
- from email.generator import Generator
- fp = StringIO()
- g = Generator(fp, mangle_from_=True, maxheaderlen=60)
- g.flatten(msg)
- text = fp.getvalue()
+ .. method:: as_string(unixfrom=False, maxheaderlen=None, policy=None)
- If the message object contains binary data that is not encoded according
- to RFC standards, the non-compliant data will be replaced by unicode
- "unknown character" code points. (See also :meth:`.as_bytes` and
- :class:`~email.generator.BytesGenerator`.)
-
- .. versionchanged:: 3.4 the *policy* keyword argument was added.
+ Return the entire message flattened as a string. When optional
+ *unixfrom* is true, the envelope header is included in the returned
+ string. *unixfrom* defaults to ``False``. For backward compabitility
+ with the base :class:`~email.message.Message` class *maxheaderlen* is
+ accepted, but defaults to ``None``, which means that by default the line
+ length is controlled by the
+ :attr:`~email.policy.EmailPolicy.max_line_length` of the policy. The
+ *policy* argument may be used to override the default policy obtained
+ from the message instance. This can be used to control some of the
+ formatting produced by the method, since the specified *policy* will be
+ passed to the :class:`~email.generator.Generator`.
+
+ Flattening the message may trigger changes to the :class:`EmailMessage`
+ if defaults need to be filled in to complete the transformation to a
+ string (for example, MIME boundaries may be generated or modified).
+
+ Note that this method is provided as a convenience and may not be the
+ most useful way to serialize messages in your application, especially if
+ you are dealing with multiple messages. See
+ :class:`email.generator.Generator` for a more flexible API for
+ serializing messages. Note also that this method is restricted to
+ producing messages serialized as "7 bit clean" when
+ :attr:`~email.policy.EmailPolicy.utf8` is ``False``, which is the default.
+
+ .. versionchanged:: 3.6 the default behavior when *maxheaderlen*
+ is not specified was changed from defaulting to 0 to defaulting
+ to the value of *max_line_length* from the policy.
.. method:: __str__()
- Equivalent to :meth:`.as_string()`. Allows ``str(msg)`` to produce a
- string containing the formatted message.
+ Equivalent to `as_string(policy=self.policy.clone(utf8=True)`. Allows
+ ``str(msg)`` to produce a string containing the serialized message in a
+ readable format.
+
+ .. versionchanged:: 3.4 the method was changed to use ``utf8=True``,
+ thus producing an :rfc:`6531`-like message representation, instead of
+ being a direct alias for :meth:`as_string`.
.. method:: as_bytes(unixfrom=False, policy=None)
@@ -98,52 +109,42 @@ Here are the methods of the :class:`Message` class:
used to override the default policy obtained from the message instance.
This can be used to control some of the formatting produced by the
method, since the specified *policy* will be passed to the
- ``BytesGenerator``.
-
- Flattening the message may trigger changes to the :class:`Message` if
- defaults need to be filled in to complete the transformation to a string
- (for example, MIME boundaries may be generated or modified).
+ :class:`~email.generator.BytesGenerator`.
- Note that this method is provided as a convenience and may not always
- format the message the way you want. For example, by default it does
- not do the mangling of lines that begin with ``From`` that is
- required by the unix mbox format. For more flexibility, instantiate a
- :class:`~email.generator.BytesGenerator` instance and use its
- :meth:`~email.generator.BytesGenerator.flatten` method directly.
- For example::
+ Flattening the message may trigger changes to the :class:`EmailMessage`
+ if defaults need to be filled in to complete the transformation to a
+ string (for example, MIME boundaries may be generated or modified).
- from io import BytesIO
- from email.generator import BytesGenerator
- fp = BytesIO()
- g = BytesGenerator(fp, mangle_from_=True, maxheaderlen=60)
- g.flatten(msg)
- text = fp.getvalue()
-
- .. versionadded:: 3.4
+ Note that this method is provided as a convenience and may not be the
+ most useful way to serialize messages in your application, especially if
+ you are dealing with multiple messages. See
+ :class:`email.generator.BytesGenerator` for a more flexible API for
+ serializing messages.
.. method:: __bytes__()
Equivalent to :meth:`.as_bytes()`. Allows ``bytes(msg)`` to produce a
- bytes object containing the formatted message.
-
- .. versionadded:: 3.4
+ bytes object containing the serialized message.
.. method:: is_multipart()
Return ``True`` if the message's payload is a list of sub-\
- :class:`Message` objects, otherwise return ``False``. When
+ :class:`EmailMessage` objects, otherwise return ``False``. When
:meth:`is_multipart` returns ``False``, the payload should be a string
- object. (Note that :meth:`is_multipart` returning ``True`` does not
- necessarily mean that "msg.get_content_maintype() == 'multipart'" will
- return the ``True``. For example, ``is_multipart`` will return ``True``
- when the :class:`Message` is of type ``message/rfc822``.)
+ object (which might be a CTE encoded binary payload). Note that
+ :meth:`is_multipart` returning ``True`` does not necessarily mean that
+ "msg.get_content_maintype() == 'multipart'" will return the ``True``.
+ For example, ``is_multipart`` will return ``True`` when the
+ :class:`EmailMessage` is of type ``message/rfc822``.
.. method:: set_unixfrom(unixfrom)
- Set the message's envelope header to *unixfrom*, which should be a string.
+ Set the message's envelope header to *unixfrom*, which should be a
+ string. (See :class:`~mailbox.mboxMessage` for a brief description of
+ this header.)
.. method:: get_unixfrom()
@@ -152,109 +153,23 @@ Here are the methods of the :class:`Message` class:
envelope header was never set.
- .. method:: attach(payload)
-
- Add the given *payload* to the current payload, which must be ``None`` or
- a list of :class:`Message` objects before the call. After the call, the
- payload will always be a list of :class:`Message` objects. If you want to
- set the payload to a scalar object (e.g. a string), use
- :meth:`set_payload` instead.
-
-
- .. method:: get_payload(i=None, decode=False)
-
- Return the current payload, which will be a list of
- :class:`Message` objects when :meth:`is_multipart` is ``True``, or a
- string when :meth:`is_multipart` is ``False``. If the payload is a list
- and you mutate the list object, you modify the message's payload in place.
-
- With optional argument *i*, :meth:`get_payload` will return the *i*-th
- element of the payload, counting from zero, if :meth:`is_multipart` is
- ``True``. An :exc:`IndexError` will be raised if *i* is less than 0 or
- greater than or equal to the number of items in the payload. If the
- payload is a string (i.e. :meth:`is_multipart` is ``False``) and *i* is
- given, a :exc:`TypeError` is raised.
-
- Optional *decode* is a flag indicating whether the payload should be
- decoded or not, according to the :mailheader:`Content-Transfer-Encoding`
- header. When ``True`` and the message is not a multipart, the payload will
- be decoded if this header's value is ``quoted-printable`` or ``base64``.
- If some other encoding is used, or :mailheader:`Content-Transfer-Encoding`
- header is missing, the payload is
- returned as-is (undecoded). In all cases the returned value is binary
- data. If the message is a multipart and the *decode* flag is ``True``,
- then ``None`` is returned. If the payload is base64 and it was not
- perfectly formed (missing padding, characters outside the base64
- alphabet), then an appropriate defect will be added to the message's
- defect property (:class:`~email.errors.InvalidBase64PaddingDefect` or
- :class:`~email.errors.InvalidBase64CharactersDefect`, respectively).
-
- When *decode* is ``False`` (the default) the body is returned as a string
- without decoding the :mailheader:`Content-Transfer-Encoding`. However,
- for a :mailheader:`Content-Transfer-Encoding` of 8bit, an attempt is made
- to decode the original bytes using the ``charset`` specified by the
- :mailheader:`Content-Type` header, using the ``replace`` error handler.
- If no ``charset`` is specified, or if the ``charset`` given is not
- recognized by the email package, the body is decoded using the default
- ASCII charset.
-
-
- .. method:: set_payload(payload, charset=None)
-
- Set the entire message object's payload to *payload*. It is the client's
- responsibility to ensure the payload invariants. Optional *charset* sets
- the message's default character set; see :meth:`set_charset` for details.
-
- .. method:: set_charset(charset)
-
- Set the character set of the payload to *charset*, which can either be a
- :class:`~email.charset.Charset` instance (see :mod:`email.charset`), a
- string naming a character set, or ``None``. If it is a string, it will
- be converted to a :class:`~email.charset.Charset` instance. If *charset*
- is ``None``, the ``charset`` parameter will be removed from the
- :mailheader:`Content-Type` header (the message will not be otherwise
- modified). Anything else will generate a :exc:`TypeError`.
-
- If there is no existing :mailheader:`MIME-Version` header one will be
- added. If there is no existing :mailheader:`Content-Type` header, one
- will be added with a value of :mimetype:`text/plain`. Whether the
- :mailheader:`Content-Type` header already exists or not, its ``charset``
- parameter will be set to *charset.output_charset*. If
- *charset.input_charset* and *charset.output_charset* differ, the payload
- will be re-encoded to the *output_charset*. If there is no existing
- :mailheader:`Content-Transfer-Encoding` header, then the payload will be
- transfer-encoded, if needed, using the specified
- :class:`~email.charset.Charset`, and a header with the appropriate value
- will be added. If a :mailheader:`Content-Transfer-Encoding` header
- already exists, the payload is assumed to already be correctly encoded
- using that :mailheader:`Content-Transfer-Encoding` and is not modified.
-
- .. method:: get_charset()
-
- Return the :class:`~email.charset.Charset` instance associated with the
- message's payload.
-
- The following methods implement a mapping-like interface for accessing the
- message's :rfc:`2822` headers. Note that there are some semantic differences
+ The following methods implement the mapping-like interface for accessing the
+ message's headers. Note that there are some semantic differences
between these methods and a normal mapping (i.e. dictionary) interface. For
example, in a dictionary there are no duplicate keys, but here there may be
duplicate message headers. Also, in dictionaries there is no guaranteed
- order to the keys returned by :meth:`keys`, but in a :class:`Message` object,
- headers are always returned in the order they appeared in the original
- message, or were added to the message later. Any header deleted and then
- re-added are always appended to the end of the header list.
+ order to the keys returned by :meth:`keys`, but in an :class:`EmailMessage`
+ object, headers are always returned in the order they appeared in the
+ original message, or in which they were added to the message later. Any
+ header deleted and then re-added is always appended to the end of the
+ header list.
- These semantic differences are intentional and are biased toward maximal
- convenience.
+ These semantic differences are intentional and are biased toward
+ convenience in the most common use cases.
Note that in all cases, any envelope header present in the message is not
included in the mapping interface.
- In a model generated from bytes, any header values that (in contravention of
- the RFCs) contain non-ASCII bytes will, when retrieved through this
- interface, be represented as :class:`~email.header.Header` objects with
- a charset of `unknown-8bit`.
-
.. method:: __len__()
@@ -264,8 +179,8 @@ Here are the methods of the :class:`Message` class:
.. method:: __contains__(name)
Return true if the message object has a field named *name*. Matching is
- done case-insensitively and *name* should not include the trailing colon.
- Used for the ``in`` operator, e.g.::
+ done without regard to case and *name* does not include the trailing
+ colon. Used for the ``in`` operator. For example::
if 'message-id' in myMessage:
print('Message-ID:', myMessage['message-id'])
@@ -273,20 +188,23 @@ Here are the methods of the :class:`Message` class:
.. method:: __getitem__(name)
- Return the value of the named header field. *name* should not include the
+ Return the value of the named header field. *name* does not include the
colon field separator. If the header is missing, ``None`` is returned; a
:exc:`KeyError` is never raised.
Note that if the named field appears more than once in the message's
headers, exactly which of those field values will be returned is
undefined. Use the :meth:`get_all` method to get the values of all the
- extant named headers.
+ extant headers named *name*.
+
+ Using the standard (non-``compat32``) policies, the returned value is an
+ instance of a subclass of :class:`email.headerregistry.BaseHeader`.
.. method:: __setitem__(name, val)
Add a header to the message with field name *name* and value *val*. The
- field is appended to the end of the message's existing fields.
+ field is appended to the end of the message's existing headers.
Note that this does *not* overwrite or delete any existing header with the same
name. If you want to ensure that the new header is the only one present in the
@@ -295,6 +213,13 @@ Here are the methods of the :class:`Message` class:
del msg['subject']
msg['subject'] = 'Python roolz!'
+ If the :mod:`policy` defines certain haders to be unique (as the standard
+ policies do), this method may raise a :exc:`ValueError` when an attempt
+ is made to assign a value to such a header when one already exists. This
+ behavior is intentional for consistency's sake, but do not depend on it
+ as we may choose to make such assignments do an automatic deletion of the
+ existing header in the future.
+
.. method:: __delitem__(name)
@@ -323,9 +248,10 @@ Here are the methods of the :class:`Message` class:
Return the value of the named header field. This is identical to
:meth:`__getitem__` except that optional *failobj* is returned if the
- named header is missing (defaults to ``None``).
+ named header is missing (*failobj* defaults to ``None``).
- Here are some additional useful methods:
+
+ Here are some additional useful header related methods:
.. method:: get_all(name, failobj=None)
@@ -346,17 +272,19 @@ Here are the methods of the :class:`Message` class:
taken as the parameter name, with underscores converted to dashes (since
dashes are illegal in Python identifiers). Normally, the parameter will
be added as ``key="value"`` unless the value is ``None``, in which case
- only the key will be added. If the value contains non-ASCII characters,
- it can be specified as a three tuple in the format
- ``(CHARSET, LANGUAGE, VALUE)``, where ``CHARSET`` is a string naming the
- charset to be used to encode the value, ``LANGUAGE`` can usually be set
- to ``None`` or the empty string (see :rfc:`2231` for other possibilities),
- and ``VALUE`` is the string value containing non-ASCII code points. If
- a three tuple is not passed and the value contains non-ASCII characters,
- it is automatically encoded in :rfc:`2231` format using a ``CHARSET``
- of ``utf-8`` and a ``LANGUAGE`` of ``None``.
-
- Here's an example::
+ only the key will be added.
+
+ If the value contains non-ASCII characters, the charset and language may
+ be explicitly controlled by specifying the value as a three tuple in the
+ format ``(CHARSET, LANGUAGE, VALUE)``, where ``CHARSET`` is a string
+ naming the charset to be used to encode the value, ``LANGUAGE`` can
+ usually be set to ``None`` or the empty string (see :rfc:`2231` for other
+ possibilities), and ``VALUE`` is the string value containing non-ASCII
+ code points. If a three tuple is not passed and the value contains
+ non-ASCII characters, it is automatically encoded in :rfc:`2231` format
+ using a ``CHARSET`` of ``utf-8`` and a ``LANGUAGE`` of ``None``.
+
+ Here is an example::
msg.add_header('Content-Disposition', 'attachment', filename='bud.gif')
@@ -364,37 +292,35 @@ Here are the methods of the :class:`Message` class:
Content-Disposition: attachment; filename="bud.gif"
- An example with non-ASCII characters::
+ An example of the extended interface with non-ASCII characters::
msg.add_header('Content-Disposition', 'attachment',
filename=('iso-8859-1', '', 'Fußballer.ppt'))
- Which produces ::
-
- Content-Disposition: attachment; filename*="iso-8859-1''Fu%DFballer.ppt"
-
.. method:: replace_header(_name, _value)
Replace a header. Replace the first header found in the message that
- matches *_name*, retaining header order and field name case. If no
- matching header was found, a :exc:`KeyError` is raised.
+ matches *_name*, retaining header order and field name case of the
+ original header. If no matching header is found, raise a
+ :exc:`KeyError`.
.. method:: get_content_type()
- Return the message's content type. The returned string is coerced to
- lower case of the form :mimetype:`maintype/subtype`. If there was no
- :mailheader:`Content-Type` header in the message the default type as given
- by :meth:`get_default_type` will be returned. Since according to
- :rfc:`2045`, messages always have a default type, :meth:`get_content_type`
- will always return a value.
+ Return the message's content type, coerced to lower case of the form
+ :mimetype:`maintype/subtype`. If there is no :mailheader:`Content-Type`
+ header in the message return the value returned by
+ :meth:`get_default_type`. If the :mailheader:`Content-Type` header is
+ invalid, return ``text/plain``.
- :rfc:`2045` defines a message's default type to be :mimetype:`text/plain`
- unless it appears inside a :mimetype:`multipart/digest` container, in
- which case it would be :mimetype:`message/rfc822`. If the
- :mailheader:`Content-Type` header has an invalid type specification,
- :rfc:`2045` mandates that the default type be :mimetype:`text/plain`.
+ (According to :rfc:`2045`, messages always have a default type,
+ :meth:`get_content_type` will always return a value. :rfc:`2045` defines
+ a message's default type to be :mimetype:`text/plain` unless it appears
+ inside a :mimetype:`multipart/digest` container, in which case it would
+ be :mimetype:`message/rfc822`. If the :mailheader:`Content-Type` header
+ has an invalid type specification, :rfc:`2045` mandates that the default
+ type be :mimetype:`text/plain`.)
.. method:: get_content_maintype()
@@ -420,81 +346,41 @@ Here are the methods of the :class:`Message` class:
.. method:: set_default_type(ctype)
Set the default content type. *ctype* should either be
- :mimetype:`text/plain` or :mimetype:`message/rfc822`, although this is not
- enforced. The default content type is not stored in the
- :mailheader:`Content-Type` header.
-
-
- .. method:: get_params(failobj=None, header='content-type', unquote=True)
-
- Return the message's :mailheader:`Content-Type` parameters, as a list.
- The elements of the returned list are 2-tuples of key/value pairs, as
- split on the ``'='`` sign. The left hand side of the ``'='`` is the key,
- while the right hand side is the value. If there is no ``'='`` sign in
- the parameter the value is the empty string, otherwise the value is as
- described in :meth:`get_param` and is unquoted if optional *unquote* is
- ``True`` (the default).
-
- Optional *failobj* is the object to return if there is no
- :mailheader:`Content-Type` header. Optional *header* is the header to
- search instead of :mailheader:`Content-Type`.
-
-
- .. method:: get_param(param, failobj=None, header='content-type', unquote=True)
-
- Return the value of the :mailheader:`Content-Type` header's parameter
- *param* as a string. If the message has no :mailheader:`Content-Type`
- header or if there is no such parameter, then *failobj* is returned
- (defaults to ``None``).
-
- Optional *header* if given, specifies the message header to use instead of
- :mailheader:`Content-Type`.
-
- Parameter keys are always compared case insensitively. The return value
- can either be a string, or a 3-tuple if the parameter was :rfc:`2231`
- encoded. When it's a 3-tuple, the elements of the value are of the form
- ``(CHARSET, LANGUAGE, VALUE)``. Note that both ``CHARSET`` and
- ``LANGUAGE`` can be ``None``, in which case you should consider ``VALUE``
- to be encoded in the ``us-ascii`` charset. You can usually ignore
- ``LANGUAGE``.
-
- If your application doesn't care whether the parameter was encoded as in
- :rfc:`2231`, you can collapse the parameter value by calling
- :func:`email.utils.collapse_rfc2231_value`, passing in the return value
- from :meth:`get_param`. This will return a suitably decoded Unicode
- string when the value is a tuple, or the original string unquoted if it
- isn't. For example::
-
- rawparam = msg.get_param('foo')
- param = email.utils.collapse_rfc2231_value(rawparam)
-
- In any case, the parameter value (either the returned string, or the
- ``VALUE`` item in the 3-tuple) is always unquoted, unless *unquote* is set
- to ``False``.
+ :mimetype:`text/plain` or :mimetype:`message/rfc822`, although this is
+ not enforced. The default content type is not stored in the
+ :mailheader:`Content-Type` header, so it only affects the return value of
+ the ``get_content_type`` methods when no :mailheader:`Content-Type`
+ header is present in the message.
.. method:: set_param(param, value, header='Content-Type', requote=True, \
charset=None, language='', replace=False)
Set a parameter in the :mailheader:`Content-Type` header. If the
- parameter already exists in the header, its value will be replaced with
- *value*. If the :mailheader:`Content-Type` header as not yet been defined
- for this message, it will be set to :mimetype:`text/plain` and the new
- parameter value will be appended as per :rfc:`2045`.
-
- Optional *header* specifies an alternative header to
- :mailheader:`Content-Type`, and all parameters will be quoted as necessary
- unless optional *requote* is ``False`` (the default is ``True``).
-
- If optional *charset* is specified, the parameter will be encoded
- according to :rfc:`2231`. Optional *language* specifies the RFC 2231
- language, defaulting to the empty string. Both *charset* and *language*
- should be strings.
+ parameter already exists in the header, replace its value with *value*.
+ When *header* is ``Content-Type`` (the default) and the header does not
+ yet exist in the message, add it, set its value to
+ :mimetype:`text/plain`, and append the new parameter value. Optional
+ *header* specifies an alternative header to :mailheader:`Content-Type`.
+
+ If the value contains non-ASCII characters, the charset and language may
+ be explicity specified using the optional *charset* and *language*
+ parameters. Optional *language* specifies the :rfc:`2231` language,
+ defaulting to the empty string. Both *charset* and *language* should be
+ strings. The default is to use the ``utf8`` *charset* and ``None`` for
+ the *language*.
If *replace* is ``False`` (the default) the header is moved to the
end of the list of headers. If *replace* is ``True``, the header
will be updated in place.
+ Use of the *requote* parameter with :class:`EmailMessage` objects is
+ deprecated.
+
+ Note that existing parameter values of headers may be accessed through
+ the :attr:`~email.headerregistry.BaseHeader.params` attribute of the
+ header value (for example, ``msg['Content-Type'].params['charset']``.
+
.. versionchanged:: 3.4 ``replace`` keyword was added.
@@ -502,25 +388,11 @@ Here are the methods of the :class:`Message` class:
Remove the given parameter completely from the :mailheader:`Content-Type`
header. The header will be re-written in place without the parameter or
- its value. All values will be quoted as necessary unless *requote* is
- ``False`` (the default is ``True``). Optional *header* specifies an
- alternative to :mailheader:`Content-Type`.
-
-
- .. method:: set_type(type, header='Content-Type', requote=True)
-
- Set the main type and subtype for the :mailheader:`Content-Type`
- header. *type* must be a string in the form :mimetype:`maintype/subtype`,
- otherwise a :exc:`ValueError` is raised.
-
- This method replaces the :mailheader:`Content-Type` header, keeping all
- the parameters in place. If *requote* is ``False``, this leaves the
- existing header's quoting as is, otherwise the parameters will be quoted
- (the default).
+ its value. Optional *header* specifies an alternative to
+ :mailheader:`Content-Type`.
- An alternative header can be specified in the *header* argument. When the
- :mailheader:`Content-Type` header is set a :mailheader:`MIME-Version`
- header is also added.
+ Use of the *requote* parameter with :class:`EmailMessage` objects is
+ deprecated.
.. method:: get_filename(failobj=None)
@@ -549,12 +421,11 @@ Here are the methods of the :class:`Message` class:
necessary. A :exc:`~email.errors.HeaderParseError` is raised if the
message object has no :mailheader:`Content-Type` header.
- Note that using this method is subtly different than deleting the old
+ Note that using this method is subtly different from deleting the old
:mailheader:`Content-Type` header and adding a new one with the new
boundary via :meth:`add_header`, because :meth:`set_boundary` preserves
the order of the :mailheader:`Content-Type` header in the list of
- headers. However, it does *not* preserve any continuation lines which may
- have been present in the original :mailheader:`Content-Type` header.
+ headers.
.. method:: get_content_charset(failobj=None)
@@ -563,9 +434,6 @@ Here are the methods of the :class:`Message` class:
coerced to lower case. If there is no :mailheader:`Content-Type` header, or if
that header has no ``charset`` parameter, *failobj* is returned.
- Note that this method differs from :meth:`get_charset` which returns the
- :class:`~email.charset.Charset` instance for the default encoding of the message body.
-
.. method:: get_charsets(failobj=None)
@@ -575,10 +443,19 @@ Here are the methods of the :class:`Message` class:
Each item in the list will be a string which is the value of the
``charset`` parameter in the :mailheader:`Content-Type` header for the
- represented subpart. However, if the subpart has no
- :mailheader:`Content-Type` header, no ``charset`` parameter, or is not of
- the :mimetype:`text` main MIME type, then that item in the returned list
- will be *failobj*.
+ represented subpart. If the subpart has no :mailheader:`Content-Type`
+ header, no ``charset`` parameter, or is not of the :mimetype:`text` main
+ MIME type, then that item in the returned list will be *failobj*.
+
+
+ .. method:: is_attachment
+
+ Return ``True`` if there is a :mailheader:`Content-Disposition` header
+ and its (case insensitive) value is ``attachment``, ``False`` otherwise.
+
+ .. versionchanged:: 3.4.2
+ is_attachment is now a method instead of a property, for consistency
+ with :meth:`~email.message.Message.is_multipart`.
.. method:: get_content_disposition()
@@ -590,6 +467,11 @@ Here are the methods of the :class:`Message` class:
.. versionadded:: 3.5
+
+ The following methods relate to interrogating and manipulating the content
+ (payload) of the message.
+
+
.. method:: walk()
The :meth:`walk` method is an all-purpose generator which can be used to
@@ -651,8 +533,169 @@ Here are the methods of the :class:`Message` class:
into the subparts.
- :class:`Message` objects can also optionally contain two instance attributes,
- which can be used when generating the plain text of a MIME message.
+ .. method:: get_body(preferencelist=('related', 'html', 'plain'))
+
+ Return the MIME part that is the best candidate to be the "body" of the
+ message.
+
+ *preferencelist* must be a sequence of strings from the set ``related``,
+ ``html``, and ``plain``, and indicates the order of preference for the
+ content type of the part returned.
+
+ Start looking for candidate matches with the object on which the
+ ``get_body`` method is called.
+
+ If ``related`` is not included in *preferencelist*, consider the root
+ part (or subpart of the root part) of any related encountered as a
+ candidate if the (sub-)part matches a preference.
+
+ When encountering a ``multipart/related``, check the ``start`` parameter
+ and if a part with a matching :mailheader:`Content-ID` is found, consider
+ only it when looking for candidate matches. Otherwise consider only the
+ first (default root) part of the ``multipart/related``.
+
+ If a part has a :mailheader:`Content-Disposition` header, only consider
+ the part a candidate match if the value of the header is ``inline``.
+
+ If none of the candidates matches any of the preferences in
+ *preferneclist*, return ``None``.
+
+ Notes: (1) For most applications the only *preferencelist* combinations
+ that really make sense are ``('plain',)``, ``('html', 'plain')``, and the
+ default ``('related', 'html', 'plain')``. (2) Because matching starts
+ with the object on which ``get_body`` is called, calling ``get_body`` on
+ a ``multipart/related`` will return the object itself unless
+ *preferencelist* has a non-default value. (3) Messages (or message parts)
+ that do not specify a :mailheader:`Content-Type` or whose
+ :mailheader:`Content-Type` header is invalid will be treated as if they
+ are of type ``text/plain``, which may occasionally cause ``get_body`` to
+ return unexpected results.
+
+
+ .. method:: iter_attachments()
+
+ Return an iterator over all of the immediate sub-parts of the message
+ that are not candidate "body" parts. That is, skip the first occurrence
+ of each of ``text/plain``, ``text/html``, ``multipart/related``, or
+ ``multipart/alternative`` (unless they are explicitly marked as
+ attachments via :mailheader:`Content-Disposition: attachment`), and
+ return all remaining parts. When applied directly to a
+ ``multipart/related``, return an iterator over the all the related parts
+ except the root part (ie: the part pointed to by the ``start`` parameter,
+ or the first part if there is no ``start`` parameter or the ``start``
+ parameter doesn't match the :mailheader:`Content-ID` of any of the
+ parts). When applied directly to a ``multipart/alternative`` or a
+ non-``multipart``, return an empty iterator.
+
+
+ .. method:: iter_parts()
+
+ Return an iterator over all of the immediate sub-parts of the message,
+ which will be empty for a non-``multipart``. (See also
+ :meth:`~email.message.EmailMessage.walk`.)
+
+
+ .. method:: get_content(*args, content_manager=None, **kw)
+
+ Call the :meth:`~email.contentmanager.ContentManager.get_content` method
+ of the *content_manager*, passing self as the message object, and passing
+ along any other arguments or keywords as additional arguments. If
+ *content_manager* is not specified, use the ``content_manager`` specified
+ by the current :mod:`~email.policy`.
+
+
+ .. method:: set_content(*args, content_manager=None, **kw)
+
+ Call the :meth:`~email.contentmanager.ContentManager.set_content` method
+ of the *content_manager*, passing self as the message object, and passing
+ along any other arguments or keywords as additional arguments. If
+ *content_manager* is not specified, use the ``content_manager`` specified
+ by the current :mod:`~email.policy`.
+
+
+ .. method:: make_related(boundary=None)
+
+ Convert a non-``multipart`` message into a ``multipart/related`` message,
+ moving any existing :mailheader:`Content-` headers and payload into a
+ (new) first part of the ``multipart``. If *boundary* is specified, use
+ it as the boundary string in the multipart, otherwise leave the boundary
+ to be automatically created when it is needed (for example, when the
+ message is serialized).
+
+
+ .. method:: make_alternative(boundary=None)
+
+ Convert a non-``multipart`` or a ``multipart/related`` into a
+ ``multipart/alternative``, moving any existing :mailheader:`Content-`
+ headers and payload into a (new) first part of the ``multipart``. If
+ *boundary* is specified, use it as the boundary string in the multipart,
+ otherwise leave the boundary to be automatically created when it is
+ needed (for example, when the message is serialized).
+
+
+ .. method:: make_mixed(boundary=None)
+
+ Convert a non-``multipart``, a ``multipart/related``, or a
+ ``multipart-alternative`` into a ``multipart/mixed``, moving any existing
+ :mailheader:`Content-` headers and payload into a (new) first part of the
+ ``multipart``. If *boundary* is specified, use it as the boundary string
+ in the multipart, otherwise leave the boundary to be automatically
+ created when it is needed (for example, when the message is serialized).
+
+
+ .. method:: add_related(*args, content_manager=None, **kw)
+
+ If the message is a ``multipart/related``, create a new message
+ object, pass all of the arguments to its :meth:`set_content` method,
+ and :meth:`~email.message.Message.attach` it to the ``multipart``. If
+ the message is a non-``multipart``, call :meth:`make_related` and then
+ proceed as above. If the message is any other type of ``multipart``,
+ raise a :exc:`TypeError`. If *content_manager* is not specified, use
+ the ``content_manager`` specified by the current :mod:`~email.policy`.
+ If the added part has no :mailheader:`Content-Disposition` header,
+ add one with the value ``inline``.
+
+
+ .. method:: add_alternative(*args, content_manager=None, **kw)
+
+ If the message is a ``multipart/alternative``, create a new message
+ object, pass all of the arguments to its :meth:`set_content` method, and
+ :meth:`~email.message.Message.attach` it to the ``multipart``. If the
+ message is a non-``multipart`` or ``multipart/related``, call
+ :meth:`make_alternative` and then proceed as above. If the message is
+ any other type of ``multipart``, raise a :exc:`TypeError`. If
+ *content_manager* is not specified, use the ``content_manager`` specified
+ by the current :mod:`~email.policy`.
+
+
+ .. method:: add_attachment(*args, content_manager=None, **kw)
+
+ If the message is a ``multipart/mixed``, create a new message object,
+ pass all of the arguments to its :meth:`set_content` method, and
+ :meth:`~email.message.Message.attach` it to the ``multipart``. If the
+ message is a non-``multipart``, ``multipart/related``, or
+ ``multipart/alternative``, call :meth:`make_mixed` and then proceed as
+ above. If *content_manager* is not specified, use the ``content_manager``
+ specified by the current :mod:`~email.policy`. If the added part
+ has no :mailheader:`Content-Disposition` header, add one with the value
+ ``attachment``. This method can be used both for explicit attachments
+ (:mailheader:`Content-Disposition: attachment` and ``inline`` attachments
+ (:mailheader:`Content-Disposition: inline`), by passing appropriate
+ options to the ``content_manager``.
+
+
+ .. method:: clear()
+
+ Remove the payload and all of the headers.
+
+
+ .. method:: clear_content()
+
+ Remove the payload and all of the :exc:`Content-` headers, leaving
+ all other headers intact and in their original order.
+
+
+ :class:`EmailMessage` objects have the following instance attributes:
.. attribute:: preamble
@@ -682,11 +725,8 @@ Here are the methods of the :class:`Message` class:
The *epilogue* attribute acts the same way as the *preamble* attribute,
except that it contains text that appears between the last boundary and
- the end of the message.
-
- You do not need to set the epilogue to the empty string in order for the
- :class:`~email.generator.Generator` to print a newline at the end of the
- file.
+ the end of the message. As with the :attr:`~EmailMessage.preamble`,
+ if there is no epilog text this attribute will be ``None``.
.. attribute:: defects
@@ -694,3 +734,18 @@ Here are the methods of the :class:`Message` class:
The *defects* attribute contains a list of all the problems found when
parsing this message. See :mod:`email.errors` for a detailed description
of the possible parsing defects.
+
+
+.. class:: MIMEPart(policy=default)
+
+ This class represents a subpart of a MIME message. It is identical to
+ :class:`EmailMessage`, except that no :mailheader:`MIME-Version` headers are
+ added when :meth:`~EmailMessage.set_content` is called, since sub-parts do
+ not need their own :mailheader:`MIME-Version` headers.
+
+
+.. rubric:: Footnotes
+
+.. [1] Oringally added in 3.4 as a :term:`provisional module <provisional
+ package>`. Docs for legacy message class moved to
+ :ref:`compat32_message`.
diff --git a/Doc/library/email.mime.rst b/Doc/library/email.mime.rst
index 8297dea..d9dae9f 100644
--- a/Doc/library/email.mime.rst
+++ b/Doc/library/email.mime.rst
@@ -8,6 +8,11 @@
--------------
+This module is part of the legacy (``Compat32``) email API. Its functionality
+is partially replaced by the :mod:`~email.contentmanager` in the new API, but
+in certain applications these classes may still be useful, even in non-legacy
+code.
+
Ordinarily, you get a message object structure by passing a file or some text to
a parser, which parses the text and returns the root message object. However
you can also build a complete message structure from scratch, or even individual
@@ -25,7 +30,7 @@ Here are the classes:
.. currentmodule:: email.mime.base
-.. class:: MIMEBase(_maintype, _subtype, **_params)
+.. class:: MIMEBase(_maintype, _subtype, *, policy=compat32, **_params)
Module: :mod:`email.mime.base`
@@ -41,10 +46,17 @@ Here are the classes:
key/value dictionary and is passed directly to :meth:`Message.add_header
<email.message.Message.add_header>`.
+ If *policy* is specified, (defaults to the
+ :class:`compat32 <email.policy.Compat32>` policy) it will be passed to
+ :class:`~email.message.Message`.
+
The :class:`MIMEBase` class always adds a :mailheader:`Content-Type` header
(based on *_maintype*, *_subtype*, and *_params*), and a
:mailheader:`MIME-Version` header (always set to ``1.0``).
+ .. versionchanged:: 3.6
+ Added *policy* keyword-only parameter.
+
.. currentmodule:: email.mime.nonmultipart
@@ -62,7 +74,8 @@ Here are the classes:
.. currentmodule:: email.mime.multipart
-.. class:: MIMEMultipart(_subtype='mixed', boundary=None, _subparts=None, **_params)
+.. class:: MIMEMultipart(_subtype='mixed', boundary=None, _subparts=None, \
+ *, policy=compat32, **_params)
Module: :mod:`email.mime.multipart`
@@ -82,14 +95,20 @@ Here are the classes:
to the message by using the :meth:`Message.attach
<email.message.Message.attach>` method.
+ Optional *policy* argument defaults to :class:`compat32 <email.policy.Compat32>`.
+
Additional parameters for the :mailheader:`Content-Type` header are taken from
the keyword arguments, or passed into the *_params* argument, which is a keyword
dictionary.
+ .. versionchanged:: 3.6
+ Added *policy* keyword-only parameter.
.. currentmodule:: email.mime.application
-.. class:: MIMEApplication(_data, _subtype='octet-stream', _encoder=email.encoders.encode_base64, **_params)
+.. class:: MIMEApplication(_data, _subtype='octet-stream', \
+ _encoder=email.encoders.encode_base64, \
+ *, policy=compat32, **_params)
Module: :mod:`email.mime.application`
@@ -109,12 +128,18 @@ Here are the classes:
object as necessary. The default encoding is base64. See the
:mod:`email.encoders` module for a list of the built-in encoders.
+ Optional *policy* argument defaults to :class:`compat32 <email.policy.Compat32>`.
+
*_params* are passed straight through to the base class constructor.
+ .. versionchanged:: 3.6
+ Added *policy* keyword-only parameter.
.. currentmodule:: email.mime.audio
-.. class:: MIMEAudio(_audiodata, _subtype=None, _encoder=email.encoders.encode_base64, **_params)
+.. class:: MIMEAudio(_audiodata, _subtype=None, \
+ _encoder=email.encoders.encode_base64, \
+ *, policy=compat32, **_params)
Module: :mod:`email.mime.audio`
@@ -137,12 +162,18 @@ Here are the classes:
object as necessary. The default encoding is base64. See the
:mod:`email.encoders` module for a list of the built-in encoders.
+ Optional *policy* argument defaults to :class:`compat32 <email.policy.Compat32>`.
+
*_params* are passed straight through to the base class constructor.
+ .. versionchanged:: 3.6
+ Added *policy* keyword-only parameter.
.. currentmodule:: email.mime.image
-.. class:: MIMEImage(_imagedata, _subtype=None, _encoder=email.encoders.encode_base64, **_params)
+.. class:: MIMEImage(_imagedata, _subtype=None, \
+ _encoder=email.encoders.encode_base64, \
+ *, policy=compat32, **_params)
Module: :mod:`email.mime.image`
@@ -165,13 +196,17 @@ Here are the classes:
object as necessary. The default encoding is base64. See the
:mod:`email.encoders` module for a list of the built-in encoders.
+ Optional *policy* argument defaults to :class:`compat32 <email.policy.Compat32>`.
+
*_params* are passed straight through to the :class:`~email.mime.base.MIMEBase`
constructor.
+ .. versionchanged:: 3.6
+ Added *policy* keyword-only parameter.
.. currentmodule:: email.mime.message
-.. class:: MIMEMessage(_msg, _subtype='rfc822')
+.. class:: MIMEMessage(_msg, _subtype='rfc822', *, policy=compat32)
Module: :mod:`email.mime.message`
@@ -184,10 +219,14 @@ Here are the classes:
Optional *_subtype* sets the subtype of the message; it defaults to
:mimetype:`rfc822`.
+ Optional *policy* argument defaults to :class:`compat32 <email.policy.Compat32>`.
+
+ .. versionchanged:: 3.6
+ Added *policy* keyword-only parameter.
.. currentmodule:: email.mime.text
-.. class:: MIMEText(_text, _subtype='plain', _charset=None)
+.. class:: MIMEText(_text, _subtype='plain', _charset=None, *, policy=compat32)
Module: :mod:`email.mime.text`
@@ -211,5 +250,10 @@ Here are the classes:
will automatically encode the new payload (and add a new
:mailheader:`Content-Transfer-Encoding` header).
+ Optional *policy* argument defaults to :class:`compat32 <email.policy.Compat32>`.
+
.. versionchanged:: 3.5
*_charset* also accepts :class:`~email.charset.Charset` instances.
+
+ .. versionchanged:: 3.6
+ Added *policy* keyword-only parameter.
diff --git a/Doc/library/email.parser.rst b/Doc/library/email.parser.rst
index b8eb7c5..c323ebc 100644
--- a/Doc/library/email.parser.rst
+++ b/Doc/library/email.parser.rst
@@ -8,210 +8,223 @@
--------------
-Message object structures can be created in one of two ways: they can be created
-from whole cloth by instantiating :class:`~email.message.Message` objects and
-stringing them together via :meth:`~email.message.Message.attach` and
-:meth:`~email.message.Message.set_payload` calls, or they
-can be created by parsing a flat text representation of the email message.
+Message object structures can be created in one of two ways: they can be
+created from whole cloth by creating an :class:`~email.message.EmailMessage`
+object, adding headers using the dictionary interface, and adding payload(s)
+using :meth:`~email.message.EmailMessage.set_content` and related methods, or
+they can be created by parsing a serialized representation of the email
+message.
The :mod:`email` package provides a standard parser that understands most email
-document structures, including MIME documents. You can pass the parser a string
-or a file object, and the parser will return to you the root
-:class:`~email.message.Message` instance of the object structure. For simple,
-non-MIME messages the payload of this root object will likely be a string
-containing the text of the message. For MIME messages, the root object will
-return ``True`` from its :meth:`~email.message.Message.is_multipart` method, and
-the subparts can be accessed via the :meth:`~email.message.Message.get_payload`
-and :meth:`~email.message.Message.walk` methods.
-
-There are actually two parser interfaces available for use, the classic
-:class:`Parser` API and the incremental :class:`FeedParser` API. The classic
-:class:`Parser` API is fine if you have the entire text of the message in memory
-as a string, or if the entire message lives in a file on the file system.
-:class:`FeedParser` is more appropriate for when you're reading the message from
-a stream which might block waiting for more input (e.g. reading an email message
-from a socket). The :class:`FeedParser` can consume and parse the message
-incrementally, and only returns the root object when you close the parser [#]_.
+document structures, including MIME documents. You can pass the parser a
+bytes, string or file object, and the parser will return to you the root
+:class:`~email.message.EmailMessage` instance of the object structure. For
+simple, non-MIME messages the payload of this root object will likely be a
+string containing the text of the message. For MIME messages, the root object
+will return ``True`` from its :meth:`~email.message.EmailMessage.is_multipart`
+method, and the subparts can be accessed via the payload manipulation methods,
+such as :meth:`~email.message.EmailMessage.get_body`,
+:meth:`~email.message.EmailMessage.iter_parts`, and
+:meth:`~email.message.EmailMessage.walk`.
+
+There are actually two parser interfaces available for use, the :class:`Parser`
+API and the incremental :class:`FeedParser` API. The :class:`Parser` API is
+most useful if you have the entire text of the message in memory, or if the
+entire message lives in a file on the file system. :class:`FeedParser` is more
+appropriate when you are reading the message from a stream which might block
+waiting for more input (such as reading an email message from a socket). The
+:class:`FeedParser` can consume and parse the message incrementally, and only
+returns the root object when you close the parser.
Note that the parser can be extended in limited ways, and of course you can
-implement your own parser completely from scratch. There is no magical
-connection between the :mod:`email` package's bundled parser and the
-:class:`~email.message.Message` class, so your custom parser can create message
-object trees any way it finds necessary.
+implement your own parser completely from scratch. All of the logic that
+connects the :mod:`email` package's bundled parser and the
+:class:`~email.message.EmailMessage` class is embodied in the :mod:`policy`
+class, so a custom parser can create message object trees any way it finds
+necessary by implementing custom versions of the appropriate :mod:`policy`
+methods.
FeedParser API
^^^^^^^^^^^^^^
-The :class:`FeedParser`, imported from the :mod:`email.feedparser` module,
-provides an API that is conducive to incremental parsing of email messages, such
-as would be necessary when reading the text of an email message from a source
-that can block (e.g. a socket). The :class:`FeedParser` can of course be used
-to parse an email message fully contained in a string or a file, but the classic
-:class:`Parser` API may be more convenient for such use cases. The semantics
-and results of the two parser APIs are identical.
-
-The :class:`FeedParser`'s API is simple; you create an instance, feed it a bunch
-of text until there's no more to feed it, then close the parser to retrieve the
-root message object. The :class:`FeedParser` is extremely accurate when parsing
-standards-compliant messages, and it does a very good job of parsing
-non-compliant messages, providing information about how a message was deemed
-broken. It will populate a message object's *defects* attribute with a list of
-any problems it found in a message. See the :mod:`email.errors` module for the
+The :class:`BytesFeedParser`, imported from the :mod:`email.feedparser` module,
+provides an API that is conducive to incremental parsing of email messages,
+such as would be necessary when reading the text of an email message from a
+source that can block (such as a socket). The :class:`BytesFeedParser` can of
+course be used to parse an email message fully contained in a :term:`bytes-like
+object`, string, or file, but the :class:`BytesParser` API may be more
+convenient for such use cases. The semantics and results of the two parser
+APIs are identical.
+
+The :class:`BytesFeedParser`'s API is simple; you create an instance, feed it a
+bunch of bytes until there's no more to feed it, then close the parser to
+retrieve the root message object. The :class:`BytesFeedParser` is extremely
+accurate when parsing standards-compliant messages, and it does a very good job
+of parsing non-compliant messages, providing information about how a message
+was deemed broken. It will populate a message object's
+:attr:`~email.message.EmailMessage.defects` attribute with a list of any
+problems it found in a message. See the :mod:`email.errors` module for the
list of defects that it can find.
-Here is the API for the :class:`FeedParser`:
+Here is the API for the :class:`BytesFeedParser`:
-.. class:: FeedParser(_factory=email.message.Message, *, policy=policy.compat32)
+.. class:: BytesFeedParser(_factory=None, *, policy=policy.compat32)
- Create a :class:`FeedParser` instance. Optional *_factory* is a no-argument
- callable that will be called whenever a new message object is needed. It
- defaults to the :class:`email.message.Message` class.
+ Create a :class:`BytesFeedParser` instance. Optional *_factory* is a
+ no-argument callable; if not specified use the
+ :attr:`~email.policy.Policy.message_factory` from the *policy*. Call
+ *_factory* whenever a new message object is needed.
- If *policy* is specified (it must be an instance of a :mod:`~email.policy`
- class) use the rules it specifies to update the representation of the
- message. If *policy* is not set, use the :class:`compat32
- <email.policy.Compat32>` policy, which maintains backward compatibility with
- the Python 3.2 version of the email package. For more information see the
+ If *policy* is specified use the rules it specifies to update the
+ representation of the message. If *policy* is not set, use the
+ :class:`compat32 <email.policy.Compat32>` policy, which maintains backward
+ compatibility with the Python 3.2 version of the email package and provides
+ :class:`~email.message.Message` as the default factory. All other policies
+ provide :class:`~email.message.EmailMessage` as the default *_factory*. For
+ more information on what else *policy* controls, see the
:mod:`~email.policy` documentation.
+ Note: **The policy keyword should always be specified**; The default will
+ change to :data:`email.policy.default` in a future version of Python.
+
+ .. versionadded:: 3.2
+
.. versionchanged:: 3.3 Added the *policy* keyword.
+ .. versionchanged:: 3.6 *_factory* defaults to the policy ``message_factory``.
+
.. method:: feed(data)
- Feed the :class:`FeedParser` some more data. *data* should be a string
- containing one or more lines. The lines can be partial and the
- :class:`FeedParser` will stitch such partial lines together properly. The
- lines in the string can have any of the common three line endings,
- carriage return, newline, or carriage return and newline (they can even be
- mixed).
+ Feed the parser some more data. *data* should be a :term:`bytes-like
+ object` containing one or more lines. The lines can be partial and the
+ parser will stitch such partial lines together properly. The lines can
+ have any of the three common line endings: carriage return, newline, or
+ carriage return and newline (they can even be mixed).
+
.. method:: close()
- Closing a :class:`FeedParser` completes the parsing of all previously fed
- data, and returns the root message object. It is undefined what happens
- if you feed more data to a closed :class:`FeedParser`.
+ Complete the parsing of all previously fed data and return the root
+ message object. It is undefined what happens if :meth:`~feed` is called
+ after this method has been called.
-.. class:: BytesFeedParser(_factory=email.message.Message)
+.. class:: FeedParser(_factory=None, *, policy=policy.compat32)
- Works exactly like :class:`FeedParser` except that the input to the
- :meth:`~FeedParser.feed` method must be bytes and not string.
+ Works like :class:`BytesFeedParser` except that the input to the
+ :meth:`~BytesFeedParser.feed` method must be a string. This is of limited
+ utility, since the only way for such a message to be valid is for it to
+ contain only ASCII text or, if :attr:`~email.policy.Policy.utf8` is
+ ``True``, no binary attachments.
- .. versionadded:: 3.2
+ .. versionchanged:: 3.3 Added the *policy* keyword.
-Parser class API
-^^^^^^^^^^^^^^^^
+Parser API
+^^^^^^^^^^
-The :class:`Parser` class, imported from the :mod:`email.parser` module,
+The :class:`BytesParser` class, imported from the :mod:`email.parser` module,
provides an API that can be used to parse a message when the complete contents
-of the message are available in a string or file. The :mod:`email.parser`
-module also provides header-only parsers, called :class:`HeaderParser` and
-:class:`BytesHeaderParser`, which can be used if you're only interested in the
-headers of the message. :class:`HeaderParser` and :class:`BytesHeaderParser`
+of the message are available in a :term:`bytes-like object` or file. The
+:mod:`email.parser` module also provides :class:`Parser` for parsing strings,
+and header-only parsers, :class:`BytesHeaderParser` and
+:class:`HeaderParser`, which can be used if you're only interested in the
+headers of the message. :class:`BytesHeaderParser` and :class:`HeaderParser`
can be much faster in these situations, since they do not attempt to parse the
-message body, instead setting the payload to the raw body as a string. They
-have the same API as the :class:`Parser` and :class:`BytesParser` classes.
+message body, instead setting the payload to the raw body.
-.. versionadded:: 3.3
- The BytesHeaderParser class.
+.. class:: BytesParser(_class=None, *, policy=policy.compat32)
-.. class:: Parser(_class=email.message.Message, *, policy=policy.compat32)
+ Create a :class:`BytesParser` instance. The *_class* and *policy*
+ arguments have the same meaning and sematnics as the *_factory*
+ and *policy* arguments of :class:`BytesFeedParser`.
- The constructor for the :class:`Parser` class takes an optional argument
- *_class*. This must be a callable factory (such as a function or a class), and
- it is used whenever a sub-message object needs to be created. It defaults to
- :class:`~email.message.Message` (see :mod:`email.message`). The factory will
- be called without arguments.
-
- If *policy* is specified (it must be an instance of a :mod:`~email.policy`
- class) use the rules it specifies to update the representation of the
- message. If *policy* is not set, use the :class:`compat32
- <email.policy.Compat32>` policy, which maintains backward compatibility with
- the Python 3.2 version of the email package. For more information see the
- :mod:`~email.policy` documentation.
+ Note: **The policy keyword should always be specified**; The default will
+ change to :data:`email.policy.default` in a future version of Python.
.. versionchanged:: 3.3
Removed the *strict* argument that was deprecated in 2.4. Added the
*policy* keyword.
-
- The other public :class:`Parser` methods are:
+ .. versionchanged:: 3.6 *_class* defaults to the policy ``message_factory``.
.. method:: parse(fp, headersonly=False)
- Read all the data from the file-like object *fp*, parse the resulting
- text, and return the root message object. *fp* must support both the
- :meth:`~io.TextIOBase.readline` and the :meth:`~io.TextIOBase.read`
- methods on file-like objects.
+ Read all the data from the binary file-like object *fp*, parse the
+ resulting bytes, and return the message object. *fp* must support
+ both the :meth:`~io.IOBase.readline` and the :meth:`~io.IOBase.read`
+ methods.
- The text contained in *fp* must be formatted as a block of :rfc:`2822`
+ The bytes contained in *fp* must be formatted as a block of :rfc:`5322`
+ (or, if :attr:`~email.policy.Policy.utf8` is ``True``, :rfc:`6532`)
style headers and header continuation lines, optionally preceded by an
envelope header. The header block is terminated either by the end of the
data or by a blank line. Following the header block is the body of the
- message (which may contain MIME-encoded subparts).
+ message (which may contain MIME-encoded subparts, including subparts
+ with a :mailheader:`Content-Transfer-Encoding` of ``8bit``.
Optional *headersonly* is a flag specifying whether to stop parsing after
reading the headers or not. The default is ``False``, meaning it parses
the entire contents of the file.
- .. method:: parsestr(text, headersonly=False)
- Similar to the :meth:`parse` method, except it takes a string object
- instead of a file-like object. Calling this method on a string is exactly
- equivalent to wrapping *text* in a :class:`~io.StringIO` instance first and
- calling :meth:`parse`.
+ .. method:: parsebytes(bytes, headersonly=False)
+
+ Similar to the :meth:`parse` method, except it takes a :term:`bytes-like
+ object` instead of a file-like object. Calling this method on a
+ :term:`bytes-like object` is equivalent to wrapping *bytes* in a
+ :class:`~io.BytesIO` instance first and calling :meth:`parse`.
Optional *headersonly* is as with the :meth:`parse` method.
+ .. versionadded:: 3.2
-.. class:: BytesParser(_class=email.message.Message, *, policy=policy.compat32)
- This class is exactly parallel to :class:`Parser`, but handles bytes input.
- The *_class* and *strict* arguments are interpreted in the same way as for
- the :class:`Parser` constructor.
+.. class:: BytesHeaderParser(_class=None, *, policy=policy.compat32)
- If *policy* is specified (it must be an instance of a :mod:`~email.policy`
- class) use the rules it specifies to update the representation of the
- message. If *policy* is not set, use the :class:`compat32
- <email.policy.Compat32>` policy, which maintains backward compatibility with
- the Python 3.2 version of the email package. For more information see the
- :mod:`~email.policy` documentation.
+ Exactly like :class:`BytesParser`, except that *headersonly*
+ defaults to ``True``.
+
+ .. versionadded:: 3.3
+
+
+.. class:: Parser(_class=None, *, policy=policy.compat32)
+
+ This class is parallel to :class:`BytesParser`, but handles string input.
.. versionchanged:: 3.3
Removed the *strict* argument. Added the *policy* keyword.
+ .. versionchanged:: 3.6 *_class* defaults to the policy ``message_factory``.
+
.. method:: parse(fp, headersonly=False)
- Read all the data from the binary file-like object *fp*, parse the
- resulting bytes, and return the message object. *fp* must support
- both the :meth:`~io.IOBase.readline` and the :meth:`~io.IOBase.read`
- methods on file-like objects.
+ Read all the data from the text-mode file-like object *fp*, parse the
+ resulting text, and return the root message object. *fp* must support
+ both the :meth:`~io.TextIOBase.readline` and the
+ :meth:`~io.TextIOBase.read` methods on file-like objects.
- The bytes contained in *fp* must be formatted as a block of :rfc:`2822`
- style headers and header continuation lines, optionally preceded by an
- envelope header. The header block is terminated either by the end of the
- data or by a blank line. Following the header block is the body of the
- message (which may contain MIME-encoded subparts, including subparts
- with a :mailheader:`Content-Transfer-Encoding` of ``8bit``.
+ Other than the text mode requirement, this method operates like
+ :meth:`BytesParser.parse`.
- Optional *headersonly* is a flag specifying whether to stop parsing after
- reading the headers or not. The default is ``False``, meaning it parses
- the entire contents of the file.
- .. method:: parsebytes(text, headersonly=False)
+ .. method:: parsestr(text, headersonly=False)
- Similar to the :meth:`parse` method, except it takes a :term:`bytes-like
- object` instead of a file-like object. Calling this method is equivalent
- to wrapping *text* in a :class:`~io.BytesIO` instance first and calling
- :meth:`parse`.
+ Similar to the :meth:`parse` method, except it takes a string object
+ instead of a file-like object. Calling this method on a string is
+ equivalent to wrapping *text* in a :class:`~io.StringIO` instance first
+ and calling :meth:`parse`.
Optional *headersonly* is as with the :meth:`parse` method.
- .. versionadded:: 3.2
+
+.. class:: HeaderParser(_class=None, *, policy=policy.compat32)
+
+ Exactly like :class:`Parser`, except that *headersonly*
+ defaults to ``True``.
Since creating a message object structure from a string or a file object is such
@@ -220,55 +233,58 @@ in the top-level :mod:`email` package namespace.
.. currentmodule:: email
-.. function:: message_from_string(s, _class=email.message.Message, *, \
- policy=policy.compat32)
- Return a message object structure from a string. This is exactly equivalent to
- ``Parser().parsestr(s)``. *_class* and *policy* are interpreted as
- with the :class:`~email.parser.Parser` class constructor.
+.. function:: message_from_bytes(s, _class=None, *, policy=policy.compat32)
+
+ Return a message object structure from a :term:`bytes-like object`. This is
+ equivalent to ``BytesParser().parsebytes(s)``. Optional *_class* and
+ *strict* are interpreted as with the :class:`~email.parser.BytesParser` class
+ constructor.
+ .. versionadded:: 3.2
.. versionchanged:: 3.3
Removed the *strict* argument. Added the *policy* keyword.
-.. function:: message_from_bytes(s, _class=email.message.Message, *, \
- policy=policy.compat32)
- Return a message object structure from a :term:`bytes-like object`. This is exactly
- equivalent to ``BytesParser().parsebytes(s)``. Optional *_class* and
- *strict* are interpreted as with the :class:`~email.parser.Parser` class
+.. function:: message_from_binary_file(fp, _class=None, *,
+ policy=policy.compat32)
+
+ Return a message object structure tree from an open binary :term:`file
+ object`. This is equivalent to ``BytesParser().parse(fp)``. *_class* and
+ *policy* are interpreted as with the :class:`~email.parser.BytesParser` class
constructor.
.. versionadded:: 3.2
.. versionchanged:: 3.3
Removed the *strict* argument. Added the *policy* keyword.
-.. function:: message_from_file(fp, _class=email.message.Message, *, \
- policy=policy.compat32)
- Return a message object structure tree from an open :term:`file object`.
- This is exactly equivalent to ``Parser().parse(fp)``. *_class*
- and *policy* are interpreted as with the :class:`~email.parser.Parser` class
- constructor.
+.. function:: message_from_string(s, _class=None, *, policy=policy.compat32)
+
+ Return a message object structure from a string. This is equivalent to
+ ``Parser().parsestr(s)``. *_class* and *policy* are interpreted as
+ with the :class:`~email.parser.Parser` class constructor.
.. versionchanged:: 3.3
Removed the *strict* argument. Added the *policy* keyword.
-.. function:: message_from_binary_file(fp, _class=email.message.Message, *, \
- policy=policy.compat32)
- Return a message object structure tree from an open binary :term:`file
- object`. This is exactly equivalent to ``BytesParser().parse(fp)``.
- *_class* and *policy* are interpreted as with the
- :class:`~email.parser.Parser` class constructor.
+.. function:: message_from_file(fp, _class=None, *, policy=policy.compat32)
+
+ Return a message object structure tree from an open :term:`file object`.
+ This is equivalent to ``Parser().parse(fp)``. *_class* and *policy* are
+ interpreted as with the :class:`~email.parser.Parser` class constructor.
- .. versionadded:: 3.2
.. versionchanged:: 3.3
Removed the *strict* argument. Added the *policy* keyword.
+ .. versionchanged:: 3.6 *_class* defaults to the policy ``message_factory``.
-Here's an example of how you might use this at an interactive Python prompt::
+
+Here's an example of how you might use :func:`message_from_bytes` at an
+interactive Python prompt::
>>> import email
- >>> msg = email.message_from_string(myString) # doctest: +SKIP
+ >>> msg = email.message_from_bytes(myBytes) # doctest: +SKIP
Additional notes
@@ -278,35 +294,27 @@ Here are some notes on the parsing semantics:
* Most non-\ :mimetype:`multipart` type messages are parsed as a single message
object with a string payload. These objects will return ``False`` for
- :meth:`~email.message.Message.is_multipart`. Their
- :meth:`~email.message.Message.get_payload` method will return a string object.
+ :meth:`~email.message.EmailMessage.is_multipart`, and
+ :meth:`~email.message.EmailMessage.iter_parts` will yield an empty list.
* All :mimetype:`multipart` type messages will be parsed as a container message
object with a list of sub-message objects for their payload. The outer
container message will return ``True`` for
- :meth:`~email.message.Message.is_multipart` and their
- :meth:`~email.message.Message.get_payload` method will return the list of
- :class:`~email.message.Message` subparts.
+ :meth:`~email.message.EmailMessage.is_multipart`, and
+ :meth:`~email.message.EmailMessage.iter_parts` will yield a list of subparts.
-* Most messages with a content type of :mimetype:`message/\*` (e.g.
- :mimetype:`message/delivery-status` and :mimetype:`message/rfc822`) will also be
- parsed as container object containing a list payload of length 1. Their
- :meth:`~email.message.Message.is_multipart` method will return ``True``.
- The single element in the list payload will be a sub-message object.
+* Most messages with a content type of :mimetype:`message/\*` (such as
+ :mimetype:`message/delivery-status` and :mimetype:`message/rfc822`) will also
+ be parsed as container object containing a list payload of length 1. Their
+ :meth:`~email.message.EmailMessage.is_multipart` method will return ``True``.
+ The single element yielded by :meth:`~email.message.EmailMessage.iter_parts`
+ will be a sub-message object.
-* Some non-standards compliant messages may not be internally consistent about
+* Some non-standards-compliant messages may not be internally consistent about
their :mimetype:`multipart`\ -edness. Such messages may have a
:mailheader:`Content-Type` header of type :mimetype:`multipart`, but their
- :meth:`~email.message.Message.is_multipart` method may return ``False``.
+ :meth:`~email.message.EmailMessage.is_multipart` method may return ``False``.
If such messages were parsed with the :class:`~email.parser.FeedParser`,
they will have an instance of the
:class:`~email.errors.MultipartInvariantViolationDefect` class in their
*defects* attribute list. See :mod:`email.errors` for details.
-
-.. rubric:: Footnotes
-
-.. [#] As of email package version 3.0, introduced in Python 2.4, the classic
- :class:`~email.parser.Parser` was re-implemented in terms of the
- :class:`~email.parser.FeedParser`, so the semantics and results are
- identical between the two parsers.
-
diff --git a/Doc/library/email.policy.rst b/Doc/library/email.policy.rst
index 2a6047d..8a41877 100644
--- a/Doc/library/email.policy.rst
+++ b/Doc/library/email.policy.rst
@@ -18,9 +18,12 @@ described by the various email and MIME RFCs. However, the general format of
email messages (a block of header fields each consisting of a name followed by
a colon followed by a value, the whole block followed by a blank line and an
arbitrary 'body'), is a format that has found utility outside of the realm of
-email. Some of these uses conform fairly closely to the main RFCs, some do
-not. And even when working with email, there are times when it is desirable to
-break strict compliance with the RFCs.
+email. Some of these uses conform fairly closely to the main email RFCs, some
+do not. Even when working with email, there are times when it is desirable to
+break strict compliance with the RFCs, such as generating emails that
+interoperate with email servers that do not themselves follow the standards, or
+that implement extensions you want to use in ways that violate the
+standards.
Policy objects give the email package the flexibility to handle all these
disparate use cases.
@@ -31,27 +34,40 @@ control the behavior of various components of the email package during use.
email package to alter the default behavior. The settable values and their
defaults are described below.
-There is a default policy used by all classes in the email package. This
-policy is named :class:`Compat32`, with a corresponding pre-defined instance
-named :const:`compat32`. It provides for complete backward compatibility (in
-some cases, including bug compatibility) with the pre-Python3.3 version of the
-email package.
+There is a default policy used by all classes in the email package. For all of
+the :mod:`~email.parser` classes and the related convenience functions, and for
+the :class:`~email.message.Message` class, this is the :class:`Compat32`
+policy, via its corresponding pre-defined instance :const:`compat32`. This
+policy provides for complete backward compatibility (in some cases, including
+bug compatibility) with the pre-Python3.3 version of the email package.
+
+This default value for the *policy* keyword to
+:class:`~email.message.EmailMessage` is the :class:`EmailPolicy` policy, via
+its pre-defined instance :data:`~default`.
+
+When a :class:`~email.message.Message` or :class:`~email.message.EmailMessage`
+object is created, it acquires a policy. If the message is created by a
+:mod:`~email.parser`, a policy passed to the parser will be the policy used by
+the message it creates. If the message is created by the program, then the
+policy can be specified when it is created. When a message is passed to a
+:mod:`~email.generator`, the generator uses the policy from the message by
+default, but you can also pass a specific policy to the generator that will
+override the one stored on the message object.
+
+The default value for the *policy* keyword for the :mod:`email.parser` classes
+and the parser convenience functions **will be changing** in a future version of
+Python. Therefore you should **always specify explicitly which policy you want
+to use** when calling any of the classes and functions described in the
+:mod:`~email.parser` module.
The first part of this documentation covers the features of :class:`Policy`, an
-:term:`abstract base class` that defines the features that are common to all
+:term:`abstract base class` that defines the features that are common to all
policy objects, including :const:`compat32`. This includes certain hook
methods that are called internally by the email package, which a custom policy
-could override to obtain different behavior.
-
-When a :class:`~email.message.Message` object is created, it acquires a policy.
-By default this will be :const:`compat32`, but a different policy can be
-specified. If the ``Message`` is created by a :mod:`~email.parser`, a policy
-passed to the parser will be the policy used by the ``Message`` it creates. If
-the ``Message`` is created by the program, then the policy can be specified
-when it is created. When a ``Message`` is passed to a :mod:`~email.generator`,
-the generator uses the policy from the ``Message`` by default, but you can also
-pass a specific policy to the generator that will override the one stored on
-the ``Message`` object.
+could override to obtain different behavior. The second part describes the
+concrete classes :class:`EmailPolicy` and :class:`Compat32`, which implement
+the hooks that provide the standard behavior and the backward compatible
+behavior and features, respectively.
:class:`Policy` instances are immutable, but they can be cloned, accepting the
same keyword arguments as the class constructor and returning a new
@@ -147,6 +163,7 @@ added matters. To illustrate::
This class defines the following properties, and thus values for the
following may be passed in the constructor of any policy class:
+
.. attribute:: max_line_length
The maximum length of any line in the serialized output, not counting the
@@ -154,12 +171,14 @@ added matters. To illustrate::
``0`` or :const:`None` indicates that no line wrapping should be
done at all.
+
.. attribute:: linesep
The string to be used to terminate lines in serialized output. The
default is ``\n`` because that's the internal end-of-line discipline used
by Python, though ``\r\n`` is required by the RFCs.
+
.. attribute:: cte_type
Controls the type of Content Transfer Encodings that may be or are
@@ -174,8 +193,8 @@ added matters. To illustrate::
``8bit`` data is not constrained to be 7 bit clean. Data in headers is
still required to be ASCII-only and so will be encoded (see
- 'binary_fold' below for an exception), but body parts may use
- the ``8bit`` CTE.
+ :meth:`fold_binary` and :attr:`~EmailPolicy.utf8` below for
+ exceptions), but body parts may use the ``8bit`` CTE.
======== ===============================================================
A ``cte_type`` value of ``8bit`` only works with ``BytesGenerator``, not
@@ -183,6 +202,7 @@ added matters. To illustrate::
``Generator`` is operating under a policy that specifies
``cte_type=8bit``, it will act as if ``cte_type`` is ``7bit``.
+
.. attribute:: raise_on_defect
If :const:`True`, any defects encountered will be raised as errors. If
@@ -190,7 +210,6 @@ added matters. To illustrate::
:meth:`register_defect` method.
-
.. attribute:: mangle_from\_
If :const:`True`, lines starting with *"From "* in the body are
@@ -201,19 +220,31 @@ added matters. To illustrate::
.. versionadded:: 3.5
The *mangle_from_* parameter.
+
+ .. attribute:: message_factory
+
+ A factory function for constructing a new empty message object. Used
+ by the parser when building messages. Defaults to ``None``, in
+ which case :class:`~email.message.Message` is used.
+
+ .. versionadded:: 3.6
+
The following :class:`Policy` method is intended to be called by code using
the email library to create policy instances with custom settings:
+
.. method:: clone(**kw)
Return a new :class:`Policy` instance whose attributes have the same
values as the current instance, except where those attributes are
given new values by the keyword arguments.
+
The remaining :class:`Policy` methods are called by the email package code,
and are not intended to be called by an application using the email package.
A custom policy must implement all of these methods.
+
.. method:: handle_defect(obj, defect)
Handle a *defect* found on *obj*. When the email package calls this
@@ -224,6 +255,7 @@ added matters. To illustrate::
it is ``True``, *defect* is raised as an exception. If it is ``False``
(the default), *obj* and *defect* are passed to :meth:`register_defect`.
+
.. method:: register_defect(obj, defect)
Register a *defect* on *obj*. In the email package, *defect* will always
@@ -236,14 +268,16 @@ added matters. To illustrate::
custom ``Message`` objects) should also provide such an attribute,
otherwise defects in parsed messages will raise unexpected errors.
+
.. method:: header_max_count(name)
Return the maximum allowed number of headers named *name*.
- Called when a header is added to a :class:`~email.message.Message`
- object. If the returned value is not ``0`` or ``None``, and there are
- already a number of headers with the name *name* equal to the value
- returned, a :exc:`ValueError` is raised.
+ Called when a header is added to an :class:`~email.message.EmailMessage`
+ or :class:`~email.message.Message` object. If the returned value is not
+ ``0`` or ``None``, and there are already a number of headers with the
+ name *name* greather than or equal to the value returned, a
+ :exc:`ValueError` is raised.
Because the default behavior of ``Message.__setitem__`` is to append the
value to the list of headers, it is easy to create duplicate headers
@@ -255,6 +289,7 @@ added matters. To illustrate::
The default implementation returns ``None`` for all header names.
+
.. method:: header_source_parse(sourcelines)
The email package calls this method with a list of strings, each string
@@ -274,6 +309,7 @@ added matters. To illustrate::
There is no default implementation
+
.. method:: header_store_parse(name, value)
The email package calls this method with the name and value provided by
@@ -289,6 +325,7 @@ added matters. To illustrate::
There is no default implementation
+
.. method:: header_fetch_parse(name, value)
The email package calls this method with the *name* and *value* currently
@@ -304,6 +341,7 @@ added matters. To illustrate::
There is no default implementation
+
.. method:: fold(name, value)
The email package calls this method with the *name* and *value* currently
@@ -316,6 +354,7 @@ added matters. To illustrate::
*value* may contain surrogateescaped binary data. There should be no
surrogateescaped binary data in the string returned by the method.
+
.. method:: fold_binary(name, value)
The same as :meth:`fold`, except that the returned value should be a
@@ -325,73 +364,6 @@ added matters. To illustrate::
converted back into binary data in the returned bytes object.
-.. class:: Compat32(**kw)
-
- This concrete :class:`Policy` is the backward compatibility policy. It
- replicates the behavior of the email package in Python 3.2. The
- :mod:`~email.policy` module also defines an instance of this class,
- :const:`compat32`, that is used as the default policy. Thus the default
- behavior of the email package is to maintain compatibility with Python 3.2.
-
- The following attributes have values that are different from the
- :class:`Policy` default:
-
- .. attribute:: mangle_from_
-
- The default is ``True``.
-
- The class provides the following concrete implementations of the
- abstract methods of :class:`Policy`:
-
- .. method:: header_source_parse(sourcelines)
-
- The name is parsed as everything up to the '``:``' and returned
- unmodified. The value is determined by stripping leading whitespace off
- the remainder of the first line, joining all subsequent lines together,
- and stripping any trailing carriage return or linefeed characters.
-
- .. method:: header_store_parse(name, value)
-
- The name and value are returned unmodified.
-
- .. method:: header_fetch_parse(name, value)
-
- If the value contains binary data, it is converted into a
- :class:`~email.header.Header` object using the ``unknown-8bit`` charset.
- Otherwise it is returned unmodified.
-
- .. method:: fold(name, value)
-
- Headers are folded using the :class:`~email.header.Header` folding
- algorithm, which preserves existing line breaks in the value, and wraps
- each resulting line to the ``max_line_length``. Non-ASCII binary data are
- CTE encoded using the ``unknown-8bit`` charset.
-
- .. method:: fold_binary(name, value)
-
- Headers are folded using the :class:`~email.header.Header` folding
- algorithm, which preserves existing line breaks in the value, and wraps
- each resulting line to the ``max_line_length``. If ``cte_type`` is
- ``7bit``, non-ascii binary data is CTE encoded using the ``unknown-8bit``
- charset. Otherwise the original source header is used, with its existing
- line breaks and any (RFC invalid) binary data it may contain.
-
-
-An instance of :class:`Compat32` is provided as a module constant:
-
-.. data:: compat32
-
- An instance of :class:`Compat32`, providing backward compatibility with the
- behavior of the email package in Python 3.2.
-
-
-.. note::
-
- The documentation below describes new policies that are included in the
- standard library on a :term:`provisional basis <provisional package>`.
- Backwards incompatible changes (up to and including removal of the feature)
- may occur if deemed necessary by the core developers.
-
.. class:: EmailPolicy(**kw)
@@ -404,9 +376,15 @@ An instance of :class:`Compat32` is provided as a module constant:
on the type of the field. The parsing and folding algorithm fully implement
:rfc:`2047` and :rfc:`5322`.
+ The default value for the :attr:`~email.policy.Policy.message_factory`
+ attribute is :class:`~email.message.EmailMessage`.
+
In addition to the settable attributes listed above that apply to all
policies, this policy adds the following additional attributes:
+ .. versionadded:: 3.6 [1]_
+
+
.. attribute:: utf8
If ``False``, follow :rfc:`5322`, supporting non-ASCII characters in
@@ -415,13 +393,14 @@ An instance of :class:`Compat32` is provided as a module constant:
formatted in this way may be passed to SMTP servers that support
the ``SMTPUTF8`` extension (:rfc:`6531`).
+
.. attribute:: refold_source
If the value for a header in the ``Message`` object originated from a
:mod:`~email.parser` (as opposed to being set by a program), this
attribute indicates whether or not a generator should refold that value
- when transforming the message back into stream form. The possible values
- are:
+ when transforming the message back into serialized form. The possible
+ values are:
======== ===============================================================
``none`` all source values use original folding
@@ -434,23 +413,24 @@ An instance of :class:`Compat32` is provided as a module constant:
The default is ``long``.
+
.. attribute:: header_factory
A callable that takes two arguments, ``name`` and ``value``, where
``name`` is a header field name and ``value`` is an unfolded header field
value, and returns a string subclass that represents that header. A
default ``header_factory`` (see :mod:`~email.headerregistry`) is provided
- that understands some of the :RFC:`5322` header field types. (Currently
- address fields and date fields have special treatment, while all other
- fields are treated as unstructured. This list will be completed before
- the extension is marked stable.)
+ that supports custom parsing for the various address and date :RFC:`5322`
+ header field types, and the major MIME header field stypes. Support for
+ additional custom parsing will be added in the future.
+
.. attribute:: content_manager
An object with at least two methods: get_content and set_content. When
- the :meth:`~email.message.Message.get_content` or
- :meth:`~email.message.Message.set_content` method of a
- :class:`~email.message.Message` object is called, it calls the
+ the :meth:`~email.message.EmailMessage.get_content` or
+ :meth:`~email.message.EmailMessage.set_content` method of an
+ :class:`~email.message.EmailMessage` object is called, it calls the
corresponding method of this object, passing it the message object as its
first argument, and any arguments or keywords that were passed to it as
additional arguments. By default ``content_manager`` is set to
@@ -462,16 +442,22 @@ An instance of :class:`Compat32` is provided as a module constant:
The class provides the following concrete implementations of the abstract
methods of :class:`Policy`:
+
.. method:: header_max_count(name)
Returns the value of the
:attr:`~email.headerregistry.BaseHeader.max_count` attribute of the
specialized class used to represent the header with the given name.
+
.. method:: header_source_parse(sourcelines)
- The implementation of this method is the same as that for the
- :class:`Compat32` policy.
+
+ The name is parsed as everything up to the '``:``' and returned
+ unmodified. The value is determined by stripping leading whitespace off
+ the remainder of the first line, joining all subsequent lines together,
+ and stripping any trailing carriage return or linefeed characters.
+
.. method:: header_store_parse(name, value)
@@ -482,6 +468,7 @@ An instance of :class:`Compat32` is provided as a module constant:
the value. In this case a ``ValueError`` is raised if the input value
contains CR or LF characters.
+
.. method:: header_fetch_parse(name, value)
If the value has a ``name`` attribute, it is returned to unmodified.
@@ -490,6 +477,7 @@ An instance of :class:`Compat32` is provided as a module constant:
header object is returned. Any surrogateescaped bytes get turned into
the unicode unknown-character glyph.
+
.. method:: fold(name, value)
Header folding is controlled by the :attr:`refold_source` policy setting.
@@ -508,6 +496,7 @@ An instance of :class:`Compat32` is provided as a module constant:
regardless of the ``refold_source`` setting, which causes the binary data
to be CTE encoded using the ``unknown-8bit`` charset.
+
.. method:: fold_binary(name, value)
The same as :meth:`fold` if :attr:`~Policy.cte_type` is ``7bit``, except
@@ -519,23 +508,27 @@ An instance of :class:`Compat32` is provided as a module constant:
``refold_header`` setting, since there is no way to know whether the
binary data consists of single byte characters or multibyte characters.
+
The following instances of :class:`EmailPolicy` provide defaults suitable for
specific application domains. Note that in the future the behavior of these
instances (in particular the ``HTTP`` instance) may be adjusted to conform even
more closely to the RFCs relevant to their domains.
+
.. data:: default
An instance of ``EmailPolicy`` with all defaults unchanged. This policy
uses the standard Python ``\n`` line endings rather than the RFC-correct
``\r\n``.
+
.. data:: SMTP
Suitable for serializing messages in conformance with the email RFCs.
Like ``default``, but with ``linesep`` set to ``\r\n``, which is RFC
compliant.
+
.. data:: SMTPUTF8
The same as ``SMTP`` except that :attr:`~EmailPolicy.utf8` is ``True``.
@@ -544,11 +537,13 @@ more closely to the RFCs relevant to their domains.
sender or recipient addresses have non-ASCII characters (the
:meth:`smtplib.SMTP.send_message` method handles this automatically).
+
.. data:: HTTP
Suitable for serializing headers with for use in HTTP traffic. Like
``SMTP`` except that ``max_line_length`` is set to ``None`` (unlimited).
+
.. data:: strict
Convenience instance. The same as ``default`` except that
@@ -557,6 +552,7 @@ more closely to the RFCs relevant to their domains.
somepolicy + policy.strict
+
With all of these :class:`EmailPolicies <.EmailPolicy>`, the effective API of
the email package is changed from the Python 3.2 API in the following ways:
@@ -573,7 +569,7 @@ the email package is changed from the Python 3.2 API in the following ways:
and allowed.
From the application view, this means that any header obtained through the
-:class:`~email.message.Message` is a header object with extra
+:class:`~email.message.EmailMessage` is a header object with extra
attributes, whose string value is the fully decoded unicode value of the
header. Likewise, a header may be assigned a new value, or a new header
created, using a unicode string, and the policy will take care of converting
@@ -581,3 +577,75 @@ the unicode string into the correct RFC encoded form.
The header objects and their attributes are described in
:mod:`~email.headerregistry`.
+
+
+
+.. class:: Compat32(**kw)
+
+ This concrete :class:`Policy` is the backward compatibility policy. It
+ replicates the behavior of the email package in Python 3.2. The
+ :mod:`~email.policy` module also defines an instance of this class,
+ :const:`compat32`, that is used as the default policy. Thus the default
+ behavior of the email package is to maintain compatibility with Python 3.2.
+
+ The following attributes have values that are different from the
+ :class:`Policy` default:
+
+
+ .. attribute:: mangle_from_
+
+ The default is ``True``.
+
+
+ The class provides the following concrete implementations of the
+ abstract methods of :class:`Policy`:
+
+
+ .. method:: header_source_parse(sourcelines)
+
+ The name is parsed as everything up to the '``:``' and returned
+ unmodified. The value is determined by stripping leading whitespace off
+ the remainder of the first line, joining all subsequent lines together,
+ and stripping any trailing carriage return or linefeed characters.
+
+
+ .. method:: header_store_parse(name, value)
+
+ The name and value are returned unmodified.
+
+
+ .. method:: header_fetch_parse(name, value)
+
+ If the value contains binary data, it is converted into a
+ :class:`~email.header.Header` object using the ``unknown-8bit`` charset.
+ Otherwise it is returned unmodified.
+
+
+ .. method:: fold(name, value)
+
+ Headers are folded using the :class:`~email.header.Header` folding
+ algorithm, which preserves existing line breaks in the value, and wraps
+ each resulting line to the ``max_line_length``. Non-ASCII binary data are
+ CTE encoded using the ``unknown-8bit`` charset.
+
+
+ .. method:: fold_binary(name, value)
+
+ Headers are folded using the :class:`~email.header.Header` folding
+ algorithm, which preserves existing line breaks in the value, and wraps
+ each resulting line to the ``max_line_length``. If ``cte_type`` is
+ ``7bit``, non-ascii binary data is CTE encoded using the ``unknown-8bit``
+ charset. Otherwise the original source header is used, with its existing
+ line breaks and any (RFC invalid) binary data it may contain.
+
+
+.. data:: compat32
+
+ An instance of :class:`Compat32`, providing backward compatibility with the
+ behavior of the email package in Python 3.2.
+
+
+.. rubric:: Footnotes
+
+.. [1] Oringally added in 3.3 as a :term:`provisional feature <provisional
+ package>`.
diff --git a/Doc/library/email.rst b/Doc/library/email.rst
index e8bb02b..c4187dd 100644
--- a/Doc/library/email.rst
+++ b/Doc/library/email.rst
@@ -3,50 +3,104 @@
.. module:: email
:synopsis: Package supporting the parsing, manipulating, and generating
- email messages, including MIME documents.
-
-.. moduleauthor:: Barry A. Warsaw <barry@python.org>
-.. sectionauthor:: Barry A. Warsaw <barry@python.org>
-.. Copyright (C) 2001-2010 Python Software Foundation
+ email messages.
+.. moduleauthor:: Barry A. Warsaw <barry@python.org>,
+ R. David Murray <rdmurray@bitdance.com>
+.. sectionauthor:: R. David Murray <rdmurray@bitdance.com>
**Source code:** :source:`Lib/email/__init__.py`
--------------
-The :mod:`email` package is a library for managing email messages, including
-MIME and other :rfc:`2822`\ -based message documents. It is specifically *not*
-designed to do any sending of email messages to SMTP (:rfc:`2821`), NNTP, or
-other servers; those are functions of modules such as :mod:`smtplib` and
-:mod:`nntplib`. The :mod:`email` package attempts to be as RFC-compliant as
-possible, supporting in addition to :rfc:`2822`, such MIME-related RFCs as
-:rfc:`2045`, :rfc:`2046`, :rfc:`2047`, and :rfc:`2231`.
-
-The primary distinguishing feature of the :mod:`email` package is that it splits
-the parsing and generating of email messages from the internal *object model*
-representation of email. Applications using the :mod:`email` package deal
-primarily with objects; you can add sub-objects to messages, remove sub-objects
-from messages, completely re-arrange the contents, etc. There is a separate
-parser and a separate generator which handles the transformation from flat text
-to the object model, and then back to flat text again. There are also handy
-subclasses for some common MIME object types, and a few miscellaneous utilities
-that help with such common tasks as extracting and parsing message field values,
-creating RFC-compliant dates, etc.
+The :mod:`email` package is a library for managing email messages. It is
+specifically *not* designed to do any sending of email messages to SMTP
+(:rfc:`2821`), NNTP, or other servers; those are functions of modules such as
+:mod:`smtplib` and :mod:`nntplib`. The :mod:`email` package attempts to be as
+RFC-compliant as possible, supporting :rfc:`5233` and :rfc:`6532`, as well as
+such MIME-related RFCs as :rfc:`2045`, :rfc:`2046`, :rfc:`2047`, :rfc:`2183`,
+and :rfc:`2231`.
+
+The overall structure of the email package can be divided into three major
+components, plus a fourth component that controls the behavior of the other
+components.
+
+The central component of the package is an "object model" that represents email
+messages. An application interacts with the package primarily through the
+object model interface defined in the :mod:`~email.message` sub-module. The
+application can use this API to ask questions about an existing email, to
+construct a new email, or to add or remove email subcomponents that themselves
+use the same object model interface. That is, following the nature of email
+messages and their MIME subcomponents, the email object model is a tree
+structure of objects that all provide the :class:`~email.message.EmailMessage`
+API.
+
+The other two major components of the package are the :mod:`~email.parser` and
+the :mod:`~email.generator`. The parser takes the serialized version of an
+email message (a stream of bytes) and converts it into a tree of
+:class:`~email.message.EmailMessage` objects. The generator takes an
+:class:`~email.message.EmailMessage` and turns it back into a serialized byte
+stream. (The parser and generator also handle streams of text characters, but
+this usage is discouraged as it is too easy to end up with messages that are
+not valid in one way or another.)
+
+The control component is the :mod:`~email.policy` module. Every
+:class:`~email.message.EmailMessage`, every :mod:`~email.generator`, and every
+:mod:`~email.parser` has an associated :mod:`~email.policy` object that
+controls its behavior. Usually an application only needs to specify the policy
+when an :class:`~email.message.EmailMessage` is created, either by directly
+instantiating an :class:`~email.message.EmailMessage` to create a new email,
+or by parsing an input stream using a :mod:`~email.parser`. But the policy can
+be changed when the message is serialized using a :mod:`~email.generator`.
+This allows, for example, a generic email message to be parsed from disk, but
+to serialize it using standard SMTP settings when sending it to an email
+server.
+
+The email package does its best to hide the details of the various governing
+RFCs from the application. Conceptually the application should be able to
+treat the email message as a structured tree of unicode text and binary
+attachments, without having to worry about how these are represented when
+serialized. In practice, however, it is often necessary to be aware of at
+least some of the rules governing MIME messages and their structure,
+specifically the names and nature of the MIME "content types" and how they
+identify multipart documents. For the most part this knowledge should only be
+required for more complex applications, and even then it should only be the
+high level structure in question, and not the details of how those structures
+are represented. Since MIME content types are used widely in modern internet
+software (not just email), this will be a familiar concept to many programmers.
The following sections describe the functionality of the :mod:`email` package.
-The ordering follows a progression that should be common in applications: an
-email message is read as flat text from a file or other source, the text is
-parsed to produce the object structure of the email message, this structure is
-manipulated, and finally, the object tree is rendered back into flat text.
-
-It is perfectly feasible to create the object structure out of whole cloth ---
-i.e. completely from scratch. From there, a similar progression can be taken as
-above.
-
-Also included are detailed specifications of all the classes and modules that
-the :mod:`email` package provides, the exception classes you might encounter
-while using the :mod:`email` package, some auxiliary utilities, and a few
-examples. For users of the older :mod:`mimelib` package, or previous versions
-of the :mod:`email` package, a section on differences and porting is provided.
+We start with the :mod:`~email.message` object model, which is the primary
+interface an application will use, and follow that with the
+:mod:`~email.parser` and :mod:`~email.generator` components. Then we cover the
+:mod:`~email.policy` controls, which completes the treatment of the main
+components of the library.
+
+The next three sections cover the exceptions the package may raise and the
+defects (non-compliance with the RFCs) that the :mod:`~email.parser` may
+detect. Then we cover the :mod:`~email.headerregistry` and the
+:mod:`~email.contentmanager` sub-components, which provide tools for doing more
+detailed manipulation of headers and payloads, respectively. Both of these
+components contain features relevant to consuming and producing non-trivial
+messages, but also document their extensibility APIs, which will be of interest
+to advanced applications.
+
+Following those is a set of examples of using the fundamental parts of the APIs
+covered in the preceding sections.
+
+The forgoing represent the modern (unicode friendly) API of the email package.
+The remaining sections, starting with the :class:`~email.message.Message`
+class, cover the legacy :data:`~email.policy.compat32` API that deals much more
+directly with the details of how email messages are represented. The
+:data:`~email.policy.compat32` API does *not* hide the details of the RFCs from
+the application, but for applications that need to operate at that level, they
+can be useful tools. This documentation is also relevant for applications that
+are still using the :mod:`~email.policy.compat32` API for backward
+compatibility reasons.
+
+.. versionchanged:: 3.6
+ Docs reorganized and rewritten to promote the new
+ :class:`~email.message.EmailMessage`/:class:`~email.policy.EmailPolicy`
+ API.
Contents of the :mod:`email` package documentation:
@@ -56,335 +110,43 @@ Contents of the :mod:`email` package documentation:
email.parser.rst
email.generator.rst
email.policy.rst
+
+ email.errors.rst
email.headerregistry.rst
email.contentmanager.rst
+
+ email.examples.rst
+
+Legacy API:
+
+.. toctree::
+
+ email.compat32-message.rst
email.mime.rst
email.header.rst
email.charset.rst
email.encoders.rst
- email.errors.rst
email.util.rst
email.iterators.rst
- email-examples.rst
.. seealso::
Module :mod:`smtplib`
- SMTP protocol client
-
- Module :mod:`nntplib`
- NNTP protocol client
-
-
-.. _email-pkg-history:
-
-Package History
----------------
-
-This table describes the release history of the email package, corresponding to
-the version of Python that the package was released with. For purposes of this
-document, when you see a note about change or added versions, these refer to the
-Python version the change was made in, *not* the email package version. This
-table also describes the Python compatibility of each version of the package.
-
-+---------------+------------------------------+-----------------------+
-| email version | distributed with | compatible with |
-+===============+==============================+=======================+
-| :const:`1.x` | Python 2.2.0 to Python 2.2.1 | *no longer supported* |
-+---------------+------------------------------+-----------------------+
-| :const:`2.5` | Python 2.2.2+ and Python 2.3 | Python 2.1 to 2.5 |
-+---------------+------------------------------+-----------------------+
-| :const:`3.0` | Python 2.4 and Python 2.5 | Python 2.3 to 2.6 |
-+---------------+------------------------------+-----------------------+
-| :const:`4.0` | Python 2.5 to Python 2.7 | Python 2.3 to 2.7 |
-+---------------+------------------------------+-----------------------+
-| :const:`5.0` | Python 3.0 and Python 3.1 | Python 3.0 to 3.2 |
-+---------------+------------------------------+-----------------------+
-| :const:`5.1` | Python 3.2 | Python 3.2 |
-+---------------+------------------------------+-----------------------+
-
-After Version 5.1 (Python 3.2), the email package no longer has a version that
-is separate from the Python version. (See the :ref:`whatsnew-index` documents
-for the respective Python versions for details on changes.)
-
-Here are the major differences between :mod:`email` version 5.1 and
-version 5.0:
-
-* It is once again possible to parse messages containing non-ASCII bytes,
- and to reproduce such messages if the data containing the non-ASCII
- bytes is not modified.
-
-* New functions :func:`message_from_bytes` and :func:`message_from_binary_file`,
- and new classes :class:`~email.parser.BytesFeedParser` and
- :class:`~email.parser.BytesParser` allow binary message data to be parsed
- into model objects.
-
-* Given bytes input to the model, :meth:`~email.message.Message.get_payload`
- will by default decode a message body that has a
- :mailheader:`Content-Transfer-Encoding` of ``8bit`` using the charset
- specified in the MIME headers and return the resulting string.
-
-* Given bytes input to the model, :class:`~email.generator.Generator` will
- convert message bodies that have a :mailheader:`Content-Transfer-Encoding` of
- 8bit to instead have a 7bit Content-Transfer-Encoding.
-
-* New class :class:`~email.generator.BytesGenerator` produces bytes
- as output, preserving any unchanged non-ASCII data that was
- present in the input used to build the model, including message bodies
- with a :mailheader:`Content-Transfer-Encoding` of 8bit.
-
-Here are the major differences between :mod:`email` version 5.0 and version 4:
-
-* All operations are on unicode strings. Text inputs must be strings,
- text outputs are strings. Outputs are limited to the ASCII character
- set and so can be encoded to ASCII for transmission. Inputs are also
- limited to ASCII; this is an acknowledged limitation of email 5.0 and
- means it can only be used to parse email that is 7bit clean.
-
-Here are the major differences between :mod:`email` version 4 and version 3:
-
-* All modules have been renamed according to :pep:`8` standards. For example,
- the version 3 module :mod:`email.Message` was renamed to :mod:`email.message` in
- version 4.
-
-* A new subpackage :mod:`email.mime` was added and all the version 3
- :mod:`email.MIME\*` modules were renamed and situated into the :mod:`email.mime`
- subpackage. For example, the version 3 module :mod:`email.MIMEText` was renamed
- to :mod:`email.mime.text`.
-
- *Note that the version 3 names will continue to work until Python 2.6*.
-
-* The :mod:`email.mime.application` module was added, which contains the
- :class:`~email.mime.application.MIMEApplication` class.
-
-* Methods that were deprecated in version 3 have been removed. These include
- :meth:`Generator.__call__`, :meth:`Message.get_type`,
- :meth:`Message.get_main_type`, :meth:`Message.get_subtype`.
-
-* Fixes have been added for :rfc:`2231` support which can change some of the
- return types for :func:`Message.get_param <email.message.Message.get_param>`
- and friends. Under some
- circumstances, values which used to return a 3-tuple now return simple strings
- (specifically, if all extended parameter segments were unencoded, there is no
- language and charset designation expected, so the return type is now a simple
- string). Also, %-decoding used to be done for both encoded and unencoded
- segments; this decoding is now done only for encoded segments.
-
-Here are the major differences between :mod:`email` version 3 and version 2:
-
-* The :class:`~email.parser.FeedParser` class was introduced, and the
- :class:`~email.parser.Parser` class was implemented in terms of the
- :class:`~email.parser.FeedParser`. All parsing therefore is
- non-strict, and parsing will make a best effort never to raise an exception.
- Problems found while parsing messages are stored in the message's *defect*
- attribute.
-
-* All aspects of the API which raised :exc:`DeprecationWarning`\ s in version 2
- have been removed. These include the *_encoder* argument to the
- :class:`~email.mime.text.MIMEText` constructor, the
- :meth:`Message.add_payload` method, the :func:`Utils.dump_address_pair`
- function, and the functions :func:`Utils.decode` and :func:`Utils.encode`.
-
-* New :exc:`DeprecationWarning`\ s have been added to:
- :meth:`Generator.__call__`, :meth:`Message.get_type`,
- :meth:`Message.get_main_type`, :meth:`Message.get_subtype`, and the *strict*
- argument to the :class:`~email.parser.Parser` class. These are expected to
- be removed in future versions.
-
-* Support for Pythons earlier than 2.3 has been removed.
-
-Here are the differences between :mod:`email` version 2 and version 1:
-
-* The :mod:`email.Header` and :mod:`email.Charset` modules have been added.
-
-* The pickle format for :class:`~email.message.Message` instances has changed.
- Since this was never (and still isn't) formally defined, this isn't
- considered a backward incompatibility. However if your application pickles
- and unpickles :class:`~email.message.Message` instances, be aware that in
- :mod:`email` version 2, :class:`~email.message.Message` instances now have
- private variables *_charset* and *_default_type*.
-
-* Several methods in the :class:`~email.message.Message` class have been
- deprecated, or their signatures changed. Also, many new methods have been
- added. See the documentation for the :class:`~email.message.Message` class
- for details. The changes should be completely backward compatible.
+ SMTP (Simple Mail Transport Protcol) client
-* The object structure has changed in the face of :mimetype:`message/rfc822`
- content types. In :mod:`email` version 1, such a type would be represented
- by a scalar payload, i.e. the container message's
- :meth:`~email.message.Message.is_multipart` returned false,
- :meth:`~email.message.Message.get_payload` was not a list object, but a
- single :class:`~email.message.Message` instance.
+ Module :mod:`poplib`
+ POP (Post Office Protocol) client
- This structure was inconsistent with the rest of the package, so the object
- representation for :mimetype:`message/rfc822` content types was changed. In
- :mod:`email` version 2, the container *does* return ``True`` from
- :meth:`~email.message.Message.is_multipart`, and
- :meth:`~email.message.Message.get_payload` returns a list containing a single
- :class:`~email.message.Message` item.
+ Module :mod:`imaplib`
+ IMAP (Internet Message Access Protocol) client
- Note that this is one place that backward compatibility could not be
- completely maintained. However, if you're already testing the return type of
- :meth:`~email.message.Message.get_payload`, you should be fine. You just need
- to make sure your code doesn't do a :meth:`~email.message.Message.set_payload`
- with a :class:`~email.message.Message` instance on a container with a content
- type of :mimetype:`message/rfc822`.
-
-* The :class:`~email.parser.Parser` constructor's *strict* argument was added,
- and its :meth:`~email.parser.Parser.parse` and
- :meth:`~email.parser.Parser.parsestr` methods grew a *headersonly* argument.
- The *strict* flag was also added to functions :func:`email.message_from_file`
- and :func:`email.message_from_string`.
-
-* :meth:`Generator.__call__` is deprecated; use :meth:`Generator.flatten
- <email.generator.Generator.flatten>` instead. The
- :class:`~email.generator.Generator` class has also grown the
- :meth:`~email.generator.Generator.clone` method.
-
-* The :class:`~email.generator.DecodedGenerator` class in the
- :mod:`email.generator` module was added.
-
-* The intermediate base classes
- :class:`~email.mime.nonmultipart.MIMENonMultipart` and
- :class:`~email.mime.multipart.MIMEMultipart` have been added, and interposed
- in the class hierarchy for most of the other MIME-related derived classes.
-
-* The *_encoder* argument to the :class:`~email.mime.text.MIMEText` constructor
- has been deprecated. Encoding now happens implicitly based on the
- *_charset* argument.
-
-* The following functions in the :mod:`email.Utils` module have been deprecated:
- :func:`dump_address_pairs`, :func:`decode`, and :func:`encode`. The following
- functions have been added to the module: :func:`make_msgid`,
- :func:`decode_rfc2231`, :func:`encode_rfc2231`, and :func:`decode_params`.
-
-* The non-public function :func:`email.Iterators._structure` was added.
-
-
-Differences from :mod:`mimelib`
--------------------------------
-
-The :mod:`email` package was originally prototyped as a separate library called
-`mimelib <http://mimelib.sourceforge.net/>`_. Changes have been made so that method names
-are more consistent, and some methods or modules have either been added or
-removed. The semantics of some of the methods have also changed. For the most
-part, any functionality available in :mod:`mimelib` is still available in the
-:mod:`email` package, albeit often in a different way. Backward compatibility
-between the :mod:`mimelib` package and the :mod:`email` package was not a
-priority.
-
-Here is a brief description of the differences between the :mod:`mimelib` and
-the :mod:`email` packages, along with hints on how to port your applications.
-
-Of course, the most visible difference between the two packages is that the
-package name has been changed to :mod:`email`. In addition, the top-level
-package has the following differences:
-
-* :func:`messageFromString` has been renamed to :func:`message_from_string`.
-
-* :func:`messageFromFile` has been renamed to :func:`message_from_file`.
-
-The :class:`~email.message.Message` class has the following differences:
-
-* The method :meth:`asString` was renamed to
- :meth:`~email.message.Message.as_string`.
-
-* The method :meth:`ismultipart` was renamed to
- :meth:`~email.message.Message.is_multipart`.
-
-* The :meth:`~email.message.Message.get_payload` method has grown a *decode*
- optional argument.
-
-* The method :meth:`getall` was renamed to
- :meth:`~email.message.Message.get_all`.
-
-* The method :meth:`addheader` was renamed to
- :meth:`~email.message.Message.add_header`.
-
-* The method :meth:`gettype` was renamed to :meth:`get_type`.
-
-* The method :meth:`getmaintype` was renamed to :meth:`get_main_type`.
-
-* The method :meth:`getsubtype` was renamed to :meth:`get_subtype`.
-
-* The method :meth:`getparams` was renamed to
- :meth:`~email.message.Message.get_params`. Also, whereas :meth:`getparams`
- returned a list of strings, :meth:`~email.message.Message.get_params` returns
- a list of 2-tuples, effectively the key/value pairs of the parameters, split
- on the ``'='`` sign.
-
-* The method :meth:`getparam` was renamed to
- :meth:`~email.message.Message.get_param`.
-
-* The method :meth:`getcharsets` was renamed to
- :meth:`~email.message.Message.get_charsets`.
-
-* The method :meth:`getfilename` was renamed to
- :meth:`~email.message.Message.get_filename`.
-
-* The method :meth:`getboundary` was renamed to
- :meth:`~email.message.Message.get_boundary`.
-
-* The method :meth:`setboundary` was renamed to
- :meth:`~email.message.Message.set_boundary`.
-
-* The method :meth:`getdecodedpayload` was removed. To get similar
- functionality, pass the value 1 to the *decode* flag of the
- :meth:`~email.message.Message.get_payload` method.
-
-* The method :meth:`getpayloadastext` was removed. Similar functionality is
- supported by the :class:`~email.generator.DecodedGenerator` class in the
- :mod:`email.generator` module.
-
-* The method :meth:`getbodyastext` was removed. You can get similar
- functionality by creating an iterator with
- :func:`~email.iterators.typed_subpart_iterator` in the :mod:`email.iterators`
- module.
-
-The :class:`~email.parser.Parser` class has no differences in its public
-interface. It does have some additional smarts to recognize
-:mimetype:`message/delivery-status` type messages, which it represents as a
-:class:`~email.message.Message` instance containing separate
-:class:`~email.message.Message` subparts for each header block in the delivery
-status notification [#]_.
-
-The :class:`~email.generator.Generator` class has no differences in its public
-interface. There is a new class in the :mod:`email.generator` module though,
-called :class:`~email.generator.DecodedGenerator` which provides most of the
-functionality previously available in the :meth:`Message.getpayloadastext`
-method.
-
-The following modules and classes have been changed:
-
-* The :class:`~email.mime.base.MIMEBase` class constructor arguments *_major*
- and *_minor* have changed to *_maintype* and *_subtype* respectively.
-
-* The ``Image`` class/module has been renamed to ``MIMEImage``. The *_minor*
- argument has been renamed to *_subtype*.
-
-* The ``Text`` class/module has been renamed to ``MIMEText``. The *_minor*
- argument has been renamed to *_subtype*.
-
-* The ``MessageRFC822`` class/module has been renamed to ``MIMEMessage``. Note
- that an earlier version of :mod:`mimelib` called this class/module ``RFC822``,
- but that clashed with the Python standard library module :mod:`rfc822` on some
- case-insensitive file systems.
-
- Also, the :class:`~email.mime.message.MIMEMessage` class now represents any
- kind of MIME message
- with main type :mimetype:`message`. It takes an optional argument *_subtype*
- which is used to set the MIME subtype. *_subtype* defaults to
- :mimetype:`rfc822`.
-
-:mod:`mimelib` provided some utility functions in its :mod:`address` and
-:mod:`date` modules. All of these functions have been moved to the
-:mod:`email.utils` module.
-
-The ``MsgReader`` class/module has been removed. Its functionality is most
-closely supported in the :func:`~email.iterators.body_line_iterator` function
-in the :mod:`email.iterators` module.
+ Module :mod:`nntplib`
+ NNTP (Net News Transport Protocol) client
-.. rubric:: Footnotes
+ Module :mod:`mailbox`
+ Tools for creating, reading, and managing collections of messages on disk
+ using a variety standard formats.
-.. [#] Delivery Status Notifications (DSN) are defined in :rfc:`1894`.
+ Module :mod:`smtpd`
+ SMTP server framework (primarily useful for testing)
diff --git a/Doc/library/email.util.rst b/Doc/library/email.util.rst
index 5cff746..63fae2a 100644
--- a/Doc/library/email.util.rst
+++ b/Doc/library/email.util.rst
@@ -8,7 +8,43 @@
--------------
-There are several useful utilities provided in the :mod:`email.utils` module:
+There are a couple of useful utilities provided in the :mod:`email.utils`
+module:
+
+.. function:: localtime(dt=None)
+
+ Return local time as an aware datetime object. If called without
+ arguments, return current time. Otherwise *dt* argument should be a
+ :class:`~datetime.datetime` instance, and it is converted to the local time
+ zone according to the system time zone database. If *dt* is naive (that
+ is, ``dt.tzinfo`` is ``None``), it is assumed to be in local time. In this
+ case, a positive or zero value for *isdst* causes ``localtime`` to presume
+ initially that summer time (for example, Daylight Saving Time) is or is not
+ (respectively) in effect for the specified time. A negative value for
+ *isdst* causes the ``localtime`` to attempt to divine whether summer time
+ is in effect for the specified time.
+
+ .. versionadded:: 3.3
+
+
+.. function:: make_msgid(idstring=None, domain=None)
+
+ Returns a string suitable for an :rfc:`2822`\ -compliant
+ :mailheader:`Message-ID` header. Optional *idstring* if given, is a string
+ used to strengthen the uniqueness of the message id. Optional *domain* if
+ given provides the portion of the msgid after the '@'. The default is the
+ local hostname. It is not normally necessary to override this default, but
+ may be useful certain cases, such as a constructing distributed system that
+ uses a consistent domain name across multiple hosts.
+
+ .. versionchanged:: 3.2
+ Added the *domain* keyword.
+
+
+The remaining functions are part of the legacy (``Compat32``) email API. There
+is no need to directly use these with the new API, since the parsing and
+formatting they provide is done automatically by the header parsing machinery
+of the new API.
.. function:: quote(str)
@@ -141,36 +177,6 @@ There are several useful utilities provided in the :mod:`email.utils` module:
.. versionadded:: 3.3
-.. function:: localtime(dt=None)
-
- Return local time as an aware datetime object. If called without
- arguments, return current time. Otherwise *dt* argument should be a
- :class:`~datetime.datetime` instance, and it is converted to the local time
- zone according to the system time zone database. If *dt* is naive (that
- is, ``dt.tzinfo`` is ``None``), it is assumed to be in local time. In this
- case, a positive or zero value for *isdst* causes ``localtime`` to presume
- initially that summer time (for example, Daylight Saving Time) is or is not
- (respectively) in effect for the specified time. A negative value for
- *isdst* causes the ``localtime`` to attempt to divine whether summer time
- is in effect for the specified time.
-
- .. versionadded:: 3.3
-
-
-.. function:: make_msgid(idstring=None, domain=None)
-
- Returns a string suitable for an :rfc:`2822`\ -compliant
- :mailheader:`Message-ID` header. Optional *idstring* if given, is a string
- used to strengthen the uniqueness of the message id. Optional *domain* if
- given provides the portion of the msgid after the '@'. The default is the
- local hostname. It is not normally necessary to override this default, but
- may be useful certain cases, such as a constructing distributed system that
- uses a consistent domain name across multiple hosts.
-
- .. versionchanged:: 3.2
- Added the *domain* keyword.
-
-
.. function:: decode_rfc2231(s)
Decode the string *s* according to :rfc:`2231`.
diff --git a/Doc/library/enum.rst b/Doc/library/enum.rst
index a3d5afc..5cd6472 100644
--- a/Doc/library/enum.rst
+++ b/Doc/library/enum.rst
@@ -23,9 +23,10 @@ by identity, and the enumeration itself can be iterated over.
Module Contents
---------------
-This module defines two enumeration classes that can be used to define unique
-sets of names and values: :class:`Enum` and :class:`IntEnum`. It also defines
-one decorator, :func:`unique`.
+This module defines four enumeration classes that can be used to define unique
+sets of names and values: :class:`Enum`, :class:`IntEnum`, and
+:class:`IntFlags`. It also defines one decorator, :func:`unique`, and one
+helper, :class:`auto`.
.. class:: Enum
@@ -37,10 +38,27 @@ one decorator, :func:`unique`.
Base class for creating enumerated constants that are also
subclasses of :class:`int`.
+.. class:: IntFlag
+
+ Base class for creating enumerated constants that can be combined using
+ the bitwise operators without losing their :class:`IntFlag` membership.
+ :class:`IntFlag` members are also subclasses of :class:`int`.
+
+.. class:: Flag
+
+ Base class for creating enumerated constants that can be combined using
+ the bitwise operations without losing their :class:`Flag` membership.
+
.. function:: unique
Enum class decorator that ensures only one name is bound to any one value.
+.. class:: auto
+
+ Instances are replaced with an appropriate value for Enum members.
+
+.. versionadded:: 3.6 ``Flag``, ``IntFlag``, ``auto``
+
Creating an Enum
----------------
@@ -52,18 +70,25 @@ follows::
>>> from enum import Enum
>>> class Color(Enum):
- ... red = 1
- ... green = 2
- ... blue = 3
+ ... RED = 1
+ ... GREEN = 2
+ ... BLUE = 3
...
+.. note:: Enum member values
+
+ Member values can be anything: :class:`int`, :class:`str`, etc.. If
+ the exact value is unimportant you may use :class:`auto` instances and an
+ appropriate value will be chosen for you. Care must be taken if you mix
+ :class:`auto` with other values.
+
.. note:: Nomenclature
- The class :class:`Color` is an *enumeration* (or *enum*)
- - The attributes :attr:`Color.red`, :attr:`Color.green`, etc., are
- *enumeration members* (or *enum members*).
+ - The attributes :attr:`Color.RED`, :attr:`Color.GREEN`, etc., are
+ *enumeration members* (or *enum members*) and are functionally constants.
- The enum members have *names* and *values* (the name of
- :attr:`Color.red` is ``red``, the value of :attr:`Color.blue` is
+ :attr:`Color.RED` is ``RED``, the value of :attr:`Color.BLUE` is
``3``, etc.)
.. note::
@@ -74,49 +99,49 @@ follows::
Enumeration members have human readable string representations::
- >>> print(Color.red)
- Color.red
+ >>> print(Color.RED)
+ Color.RED
...while their ``repr`` has more information::
- >>> print(repr(Color.red))
- <Color.red: 1>
+ >>> print(repr(Color.RED))
+ <Color.RED: 1>
The *type* of an enumeration member is the enumeration it belongs to::
- >>> type(Color.red)
+ >>> type(Color.RED)
<enum 'Color'>
- >>> isinstance(Color.green, Color)
+ >>> isinstance(Color.GREEN, Color)
True
>>>
Enum members also have a property that contains just their item name::
- >>> print(Color.red.name)
- red
+ >>> print(Color.RED.name)
+ RED
Enumerations support iteration, in definition order::
>>> class Shake(Enum):
- ... vanilla = 7
- ... chocolate = 4
- ... cookies = 9
- ... mint = 3
+ ... VANILLA = 7
+ ... CHOCOLATE = 4
+ ... COOKIES = 9
+ ... MINT = 3
...
>>> for shake in Shake:
... print(shake)
...
- Shake.vanilla
- Shake.chocolate
- Shake.cookies
- Shake.mint
+ Shake.VANILLA
+ Shake.CHOCOLATE
+ Shake.COOKIES
+ Shake.MINT
Enumeration members are hashable, so they can be used in dictionaries and sets::
>>> apples = {}
- >>> apples[Color.red] = 'red delicious'
- >>> apples[Color.green] = 'granny smith'
- >>> apples == {Color.red: 'red delicious', Color.green: 'granny smith'}
+ >>> apples[Color.RED] = 'red delicious'
+ >>> apples[Color.GREEN] = 'granny smith'
+ >>> apples == {Color.RED: 'red delicious', Color.GREEN: 'granny smith'}
True
@@ -124,26 +149,26 @@ Programmatic access to enumeration members and their attributes
---------------------------------------------------------------
Sometimes it's useful to access members in enumerations programmatically (i.e.
-situations where ``Color.red`` won't do because the exact color is not known
+situations where ``Color.RED`` won't do because the exact color is not known
at program-writing time). ``Enum`` allows such access::
>>> Color(1)
- <Color.red: 1>
+ <Color.RED: 1>
>>> Color(3)
- <Color.blue: 3>
+ <Color.BLUE: 3>
If you want to access enum members by *name*, use item access::
- >>> Color['red']
- <Color.red: 1>
- >>> Color['green']
- <Color.green: 2>
+ >>> Color['RED']
+ <Color.RED: 1>
+ >>> Color['GREEN']
+ <Color.GREEN: 2>
If you have an enum member and need its :attr:`name` or :attr:`value`::
- >>> member = Color.red
+ >>> member = Color.RED
>>> member.name
- 'red'
+ 'RED'
>>> member.value
1
@@ -154,12 +179,12 @@ Duplicating enum members and values
Having two enum members with the same name is invalid::
>>> class Shape(Enum):
- ... square = 2
- ... square = 3
+ ... SQUARE = 2
+ ... SQUARE = 3
...
Traceback (most recent call last):
...
- TypeError: Attempted to reuse key: 'square'
+ TypeError: Attempted to reuse key: 'SQUARE'
However, two enum members are allowed to have the same value. Given two members
A and B with the same value (and A defined first), B is an alias to A. By-value
@@ -167,17 +192,17 @@ lookup of the value of A and B will return A. By-name lookup of B will also
return A::
>>> class Shape(Enum):
- ... square = 2
- ... diamond = 1
- ... circle = 3
- ... alias_for_square = 2
- ...
- >>> Shape.square
- <Shape.square: 2>
- >>> Shape.alias_for_square
- <Shape.square: 2>
+ ... SQUARE = 2
+ ... DIAMOND = 1
+ ... CIRCLE = 3
+ ... ALIAS_FOR_SQUARE = 2
+ ...
+ >>> Shape.SQUARE
+ <Shape.SQUARE: 2>
+ >>> Shape.ALIAS_FOR_SQUARE
+ <Shape.SQUARE: 2>
>>> Shape(2)
- <Shape.square: 2>
+ <Shape.SQUARE: 2>
.. note::
@@ -202,15 +227,51 @@ found :exc:`ValueError` is raised with the details::
>>> from enum import Enum, unique
>>> @unique
... class Mistake(Enum):
- ... one = 1
- ... two = 2
- ... three = 3
- ... four = 3
+ ... ONE = 1
+ ... TWO = 2
+ ... THREE = 3
+ ... FOUR = 3
...
Traceback (most recent call last):
...
- ValueError: duplicate values found in <enum 'Mistake'>: four -> three
+ ValueError: duplicate values found in <enum 'Mistake'>: FOUR -> THREE
+
+
+Using automatic values
+----------------------
+
+If the exact value is unimportant you can use :class:`auto`::
+
+ >>> from enum import Enum, auto
+ >>> class Color(Enum):
+ ... RED = auto()
+ ... BLUE = auto()
+ ... GREEN = auto()
+ ...
+ >>> list(Color)
+ [<Color.RED: 1>, <Color.BLUE: 2>, <Color.GREEN: 3>]
+
+The values are chosen by :func:`_generate_next_value_`, which can be
+overridden::
+
+ >>> class AutoName(Enum):
+ ... def _generate_next_value_(name, start, count, last_values):
+ ... return name
+ ...
+ >>> class Ordinal(AutoName):
+ ... NORTH = auto()
+ ... SOUTH = auto()
+ ... EAST = auto()
+ ... WEST = auto()
+ ...
+ >>> list(Ordinal)
+ [<Ordinal.NORTH: 'NORTH'>, <Ordinal.SOUTH: 'SOUTH'>, <Ordinal.EAST: 'EAST'>, <Ordinal.WEST: 'WEST'>]
+
+.. note::
+ The goal of the default :meth:`_generate_next_value_` methods is to provide
+ the next :class:`int` in sequence with the last :class:`int` provided, but
+ the way it does this is an implementation detail and may change.
Iteration
---------
@@ -218,7 +279,7 @@ Iteration
Iterating over the members of an enum does not provide the aliases::
>>> list(Shape)
- [<Shape.square: 2>, <Shape.diamond: 1>, <Shape.circle: 3>]
+ [<Shape.SQUARE: 2>, <Shape.DIAMOND: 1>, <Shape.CIRCLE: 3>]
The special attribute ``__members__`` is an ordered dictionary mapping names
to members. It includes all names defined in the enumeration, including the
@@ -227,16 +288,16 @@ aliases::
>>> for name, member in Shape.__members__.items():
... name, member
...
- ('square', <Shape.square: 2>)
- ('diamond', <Shape.diamond: 1>)
- ('circle', <Shape.circle: 3>)
- ('alias_for_square', <Shape.square: 2>)
+ ('SQUARE', <Shape.SQUARE: 2>)
+ ('DIAMOND', <Shape.DIAMOND: 1>)
+ ('CIRCLE', <Shape.CIRCLE: 3>)
+ ('ALIAS_FOR_SQUARE', <Shape.SQUARE: 2>)
The ``__members__`` attribute can be used for detailed programmatic access to
the enumeration members. For example, finding all the aliases::
>>> [name for name, member in Shape.__members__.items() if member.name != name]
- ['alias_for_square']
+ ['ALIAS_FOR_SQUARE']
Comparisons
@@ -244,35 +305,35 @@ Comparisons
Enumeration members are compared by identity::
- >>> Color.red is Color.red
+ >>> Color.RED is Color.RED
True
- >>> Color.red is Color.blue
+ >>> Color.RED is Color.BLUE
False
- >>> Color.red is not Color.blue
+ >>> Color.RED is not Color.BLUE
True
Ordered comparisons between enumeration values are *not* supported. Enum
members are not integers (but see `IntEnum`_ below)::
- >>> Color.red < Color.blue
+ >>> Color.RED < Color.BLUE
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
- TypeError: unorderable types: Color() < Color()
+ TypeError: '<' not supported between instances of 'Color' and 'Color'
Equality comparisons are defined though::
- >>> Color.blue == Color.red
+ >>> Color.BLUE == Color.RED
False
- >>> Color.blue != Color.red
+ >>> Color.BLUE != Color.RED
True
- >>> Color.blue == Color.blue
+ >>> Color.BLUE == Color.BLUE
True
Comparisons against non-enumeration values will always compare not equal
(again, :class:`IntEnum` was explicitly designed to behave differently, see
below)::
- >>> Color.blue == 2
+ >>> Color.BLUE == 2
False
@@ -289,8 +350,8 @@ Enumerations are Python classes, and can have methods and special methods as
usual. If we have this enumeration::
>>> class Mood(Enum):
- ... funky = 1
- ... happy = 3
+ ... FUNKY = 1
+ ... HAPPY = 3
...
... def describe(self):
... # self is the member here
@@ -302,16 +363,16 @@ usual. If we have this enumeration::
... @classmethod
... def favorite_mood(cls):
... # cls here is the enumeration
- ... return cls.happy
+ ... return cls.HAPPY
...
Then::
>>> Mood.favorite_mood()
- <Mood.happy: 3>
- >>> Mood.happy.describe()
- ('happy', 3)
- >>> str(Mood.funky)
+ <Mood.HAPPY: 3>
+ >>> Mood.HAPPY.describe()
+ ('HAPPY', 3)
+ >>> str(Mood.FUNKY)
'my custom str! 1'
The rules for what is allowed are as follows: names that start and end with
@@ -332,7 +393,7 @@ Subclassing an enumeration is allowed only if the enumeration does not define
any members. So this is forbidden::
>>> class MoreColor(Color):
- ... pink = 17
+ ... PINK = 17
...
Traceback (most recent call last):
...
@@ -345,8 +406,8 @@ But this is allowed::
... pass
...
>>> class Bar(Foo):
- ... happy = 1
- ... sad = 2
+ ... HAPPY = 1
+ ... SAD = 2
...
Allowing subclassing of enums that define members would lead to a violation of
@@ -362,7 +423,7 @@ Enumerations can be pickled and unpickled::
>>> from test.test_enum import Fruit
>>> from pickle import dumps, loads
- >>> Fruit.tomato is loads(dumps(Fruit.tomato))
+ >>> Fruit.TOMATO is loads(dumps(Fruit.TOMATO))
True
The usual restrictions for pickling apply: picklable enums must be defined in
@@ -383,15 +444,15 @@ Functional API
The :class:`Enum` class is callable, providing the following functional API::
- >>> Animal = Enum('Animal', 'ant bee cat dog')
+ >>> Animal = Enum('Animal', 'ANT BEE CAT DOG')
>>> Animal
<enum 'Animal'>
- >>> Animal.ant
- <Animal.ant: 1>
- >>> Animal.ant.value
+ >>> Animal.ANT
+ <Animal.ANT: 1>
+ >>> Animal.ANT.value
1
>>> list(Animal)
- [<Animal.ant: 1>, <Animal.bee: 2>, <Animal.cat: 3>, <Animal.dog: 4>]
+ [<Animal.ANT: 1>, <Animal.BEE: 2>, <Animal.CAT: 3>, <Animal.DOG: 4>]
The semantics of this API resemble :class:`~collections.namedtuple`. The first
argument of the call to :class:`Enum` is the name of the enumeration.
@@ -406,10 +467,10 @@ new class derived from :class:`Enum` is returned. In other words, the above
assignment to :class:`Animal` is equivalent to::
>>> class Animal(Enum):
- ... ant = 1
- ... bee = 2
- ... cat = 3
- ... dog = 4
+ ... ANT = 1
+ ... BEE = 2
+ ... CAT = 3
+ ... DOG = 4
...
The reason for defaulting to ``1`` as the starting number and not ``0`` is
@@ -422,7 +483,7 @@ enumeration is being created in (e.g. it will fail if you use a utility
function in separate module, and also may not work on IronPython or Jython).
The solution is to specify the module name explicitly as follows::
- >>> Animal = Enum('Animal', 'ant bee cat dog', module=__name__)
+ >>> Animal = Enum('Animal', 'ANT BEE CAT DOG', module=__name__)
.. warning::
@@ -435,7 +496,7 @@ The new pickle protocol 4 also, in some circumstances, relies on
to find the class. For example, if the class was made available in class
SomeData in the global scope::
- >>> Animal = Enum('Animal', 'ant bee cat dog', qualname='SomeData.Animal')
+ >>> Animal = Enum('Animal', 'ANT BEE CAT DOG', qualname='SomeData.Animal')
The complete signature is::
@@ -446,19 +507,19 @@ The complete signature is::
:names: The Enum members. This can be a whitespace or comma separated string
(values will start at 1 unless otherwise specified)::
- 'red green blue' | 'red,green,blue' | 'red, green, blue'
+ 'RED GREEN BLUE' | 'RED,GREEN,BLUE' | 'RED, GREEN, BLUE'
or an iterator of names::
- ['red', 'green', 'blue']
+ ['RED', 'GREEN', 'BLUE']
or an iterator of (name, value) pairs::
- [('cyan', 4), ('magenta', 5), ('yellow', 6)]
+ [('CYAN', 4), ('MAGENTA', 5), ('YELLOW', 6)]
or a mapping::
- {'chartreuse': 7, 'sea_green': 11, 'rosemary': 42}
+ {'CHARTREUSE': 7, 'SEA_GREEN': 11, 'ROSEMARY': 42}
:module: name of module where new Enum class can be found.
@@ -478,56 +539,167 @@ Derived Enumerations
IntEnum
^^^^^^^
-A variation of :class:`Enum` is provided which is also a subclass of
+The first variation of :class:`Enum` that is provided is also a subclass of
:class:`int`. Members of an :class:`IntEnum` can be compared to integers;
by extension, integer enumerations of different types can also be compared
to each other::
>>> from enum import IntEnum
>>> class Shape(IntEnum):
- ... circle = 1
- ... square = 2
+ ... CIRCLE = 1
+ ... SQUARE = 2
...
>>> class Request(IntEnum):
- ... post = 1
- ... get = 2
+ ... POST = 1
+ ... GET = 2
...
>>> Shape == 1
False
- >>> Shape.circle == 1
+ >>> Shape.CIRCLE == 1
True
- >>> Shape.circle == Request.post
+ >>> Shape.CIRCLE == Request.POST
True
However, they still can't be compared to standard :class:`Enum` enumerations::
>>> class Shape(IntEnum):
- ... circle = 1
- ... square = 2
+ ... CIRCLE = 1
+ ... SQUARE = 2
...
>>> class Color(Enum):
- ... red = 1
- ... green = 2
+ ... RED = 1
+ ... GREEN = 2
...
- >>> Shape.circle == Color.red
+ >>> Shape.CIRCLE == Color.RED
False
:class:`IntEnum` values behave like integers in other ways you'd expect::
- >>> int(Shape.circle)
+ >>> int(Shape.CIRCLE)
1
- >>> ['a', 'b', 'c'][Shape.circle]
+ >>> ['a', 'b', 'c'][Shape.CIRCLE]
'b'
- >>> [i for i in range(Shape.square)]
+ >>> [i for i in range(Shape.SQUARE)]
[0, 1]
-For the vast majority of code, :class:`Enum` is strongly recommended,
-since :class:`IntEnum` breaks some semantic promises of an enumeration (by
-being comparable to integers, and thus by transitivity to other
-unrelated enumerations). It should be used only in special cases where
-there's no other choice; for example, when integer constants are
-replaced with enumerations and backwards compatibility is required with code
-that still expects integers.
+
+IntFlag
+^^^^^^^
+
+The next variation of :class:`Enum` provided, :class:`IntFlag`, is also based
+on :class:`int`. The difference being :class:`IntFlag` members can be combined
+using the bitwise operators (&, \|, ^, ~) and the result is still an
+:class:`IntFlag` member. However, as the name implies, :class:`IntFlag`
+members also subclass :class:`int` and can be used wherever an :class:`int` is
+used. Any operation on an :class:`IntFlag` member besides the bit-wise
+operations will lose the :class:`IntFlag` membership.
+
+.. versionadded:: 3.6
+
+Sample :class:`IntFlag` class::
+
+ >>> from enum import IntFlag
+ >>> class Perm(IntFlag):
+ ... R = 4
+ ... W = 2
+ ... X = 1
+ ...
+ >>> Perm.R | Perm.W
+ <Perm.R|W: 6>
+ >>> Perm.R + Perm.W
+ 6
+ >>> RW = Perm.R | Perm.W
+ >>> Perm.R in RW
+ True
+
+It is also possible to name the combinations::
+
+ >>> class Perm(IntFlag):
+ ... R = 4
+ ... W = 2
+ ... X = 1
+ ... RWX = 7
+ >>> Perm.RWX
+ <Perm.RWX: 7>
+ >>> ~Perm.RWX
+ <Perm.-8: -8>
+
+Another important difference between :class:`IntFlag` and :class:`Enum` is that
+if no flags are set (the value is 0), its boolean evaluation is :data:`False`::
+
+ >>> Perm.R & Perm.X
+ <Perm.0: 0>
+ >>> bool(Perm.R & Perm.X)
+ False
+
+Because :class:`IntFlag` members are also subclasses of :class:`int` they can
+be combined with them::
+
+ >>> Perm.X | 8
+ <Perm.8|X: 9>
+
+
+Flag
+^^^^
+
+The last variation is :class:`Flag`. Like :class:`IntFlag`, :class:`Flag`
+members can be combined using the bitwise operators (&, \|, ^, ~). Unlike
+:class:`IntFlag`, they cannot be combined with, nor compared against, any
+other :class:`Flag` enumeration, nor :class:`int`. While it is possible to
+specify the values directly it is recommended to use :class:`auto` as the
+value and let :class:`Flag` select an appropriate value.
+
+.. versionadded:: 3.6
+
+Like :class:`IntFlag`, if a combination of :class:`Flag` members results in no
+flags being set, the boolean evaluation is :data:`False`::
+
+ >>> from enum import Flag
+ >>> class Color(Flag):
+ ... RED = auto()
+ ... BLUE = auto()
+ ... GREEN = auto()
+ ...
+ >>> Color.RED & Color.GREEN
+ <Color.0: 0>
+ >>> bool(Color.RED & Color.GREEN)
+ False
+
+Individual flags should have values that are powers of two (1, 2, 4, 8, ...),
+while combinations of flags won't::
+
+ >>> class Color(Flag):
+ ... RED = auto()
+ ... BLUE = auto()
+ ... GREEN = auto()
+ ... WHITE = RED | BLUE | GREEN
+ ...
+ >>> Color.WHITE
+ <Color.WHITE: 7>
+
+Giving a name to the "no flags set" condition does not change its boolean
+value::
+
+ >>> class Color(Flag):
+ ... BLACK = 0
+ ... RED = auto()
+ ... BLUE = auto()
+ ... GREEN = auto()
+ ...
+ >>> Color.BLACK
+ <Color.BLACK: 0>
+ >>> bool(Color.BLACK)
+ False
+
+.. note::
+
+ For the majority of new code, :class:`Enum` and :class:`Flag` are strongly
+ recommended, since :class:`IntEnum` and :class:`IntFlag` break some
+ semantic promises of an enumeration (by being comparable to integers, and
+ thus by transitivity to other unrelated enumerations). :class:`IntEnum`
+ and :class:`IntFlag` should be used only in cases where :class:`Enum` and
+ :class:`Flag` will not do; for example, when integer constants are replaced
+ with enumerations, or for interoperability with other systems.
Others
@@ -558,7 +730,8 @@ Some rules:
4. %-style formatting: `%s` and `%r` call the :class:`Enum` class's
:meth:`__str__` and :meth:`__repr__` respectively; other codes (such as
`%i` or `%h` for IntEnum) treat the enum member as its mixed-in type.
-5. :meth:`str.format` (or :func:`format`) will use the mixed-in
+5. :ref:`Formatted string literals <f-strings>`, :meth:`str.format`,
+ and :func:`format` will use the mixed-in
type's :meth:`__format__`. If the :class:`Enum` class's :func:`str` or
:func:`repr` is desired, use the `!s` or `!r` format codes.
@@ -566,18 +739,87 @@ Some rules:
Interesting examples
--------------------
-While :class:`Enum` and :class:`IntEnum` are expected to cover the majority of
-use-cases, they cannot cover them all. Here are recipes for some different
-types of enumerations that can be used directly, or as examples for creating
-one's own.
+While :class:`Enum`, :class:`IntEnum`, :class:`IntFlag`, and :class:`Flag` are
+expected to cover the majority of use-cases, they cannot cover them all. Here
+are recipes for some different types of enumerations that can be used directly,
+or as examples for creating one's own.
-AutoNumber
-^^^^^^^^^^
+Omitting values
+^^^^^^^^^^^^^^^
-Avoids having to specify the value for each enumeration member::
+In many use-cases one doesn't care what the actual value of an enumeration
+is. There are several ways to define this type of simple enumeration:
- >>> class AutoNumber(Enum):
+- use instances of :class:`auto` for the value
+- use instances of :class:`object` as the value
+- use a descriptive string as the value
+- use a tuple as the value and a custom :meth:`__new__` to replace the
+ tuple with an :class:`int` value
+
+Using any of these methods signifies to the user that these values are not
+important, and also enables one to add, remove, or reorder members without
+having to renumber the remaining members.
+
+Whichever method you choose, you should provide a :meth:`repr` that also hides
+the (unimportant) value::
+
+ >>> class NoValue(Enum):
+ ... def __repr__(self):
+ ... return '<%s.%s>' % (self.__class__.__name__, self.name)
+ ...
+
+
+Using :class:`auto`
+"""""""""""""""""""
+
+Using :class:`auto` would look like::
+
+ >>> class Color(NoValue):
+ ... RED = auto()
+ ... BLUE = auto()
+ ... GREEN = auto()
+ ...
+ >>> Color.GREEN
+ <Color.GREEN>
+
+
+Using :class:`object`
+"""""""""""""""""""""
+
+Using :class:`object` would look like::
+
+ >>> class Color(NoValue):
+ ... RED = object()
+ ... GREEN = object()
+ ... BLUE = object()
+ ...
+ >>> Color.GREEN
+ <Color.GREEN>
+
+
+Using a descriptive string
+""""""""""""""""""""""""""
+
+Using a string as the value would look like::
+
+ >>> class Color(NoValue):
+ ... RED = 'stop'
+ ... GREEN = 'go'
+ ... BLUE = 'too fast!'
+ ...
+ >>> Color.GREEN
+ <Color.GREEN>
+ >>> Color.GREEN.value
+ 'go'
+
+
+Using a custom :meth:`__new__`
+""""""""""""""""""""""""""""""
+
+Using an auto-numbering :meth:`__new__` would look like::
+
+ >>> class AutoNumber(NoValue):
... def __new__(cls):
... value = len(cls.__members__) + 1
... obj = object.__new__(cls)
@@ -585,12 +827,15 @@ Avoids having to specify the value for each enumeration member::
... return obj
...
>>> class Color(AutoNumber):
- ... red = ()
- ... green = ()
- ... blue = ()
+ ... RED = ()
+ ... GREEN = ()
+ ... BLUE = ()
...
- >>> Color.green.value == 2
- True
+ >>> Color.GREEN
+ <Color.GREEN>
+ >>> Color.GREEN.value
+ 2
+
.. note::
@@ -652,14 +897,14 @@ alias::
... % (a, e))
...
>>> class Color(DuplicateFreeEnum):
- ... red = 1
- ... green = 2
- ... blue = 3
- ... grene = 2
+ ... RED = 1
+ ... GREEN = 2
+ ... BLUE = 3
+ ... GRENE = 2
...
Traceback (most recent call last):
...
- ValueError: aliases not allowed in DuplicateFreeEnum: 'grene' --> 'green'
+ ValueError: aliases not allowed in DuplicateFreeEnum: 'GRENE' --> 'GREEN'
.. note::
@@ -730,10 +975,61 @@ member instances.
Finer Points
^^^^^^^^^^^^
-:class:`Enum` members are instances of an :class:`Enum` class, and even
-though they are accessible as `EnumClass.member`, they should not be accessed
-directly from the member as that lookup may fail or, worse, return something
-besides the :class:`Enum` member you looking for::
+Supported ``__dunder__`` names
+""""""""""""""""""""""""""""""
+
+:attr:`__members__` is an :class:`OrderedDict` of ``member_name``:``member``
+items. It is only available on the class.
+
+:meth:`__new__`, if specified, must create and return the enum members; it is
+also a very good idea to set the member's :attr:`_value_` appropriately. Once
+all the members are created it is no longer used.
+
+
+Supported ``_sunder_`` names
+""""""""""""""""""""""""""""
+
+- ``_name_`` -- name of the member
+- ``_value_`` -- value of the member; can be set / modified in ``__new__``
+
+- ``_missing_`` -- a lookup function used when a value is not found; may be
+ overridden
+- ``_order_`` -- used in Python 2/3 code to ensure member order is consistent
+ (class attribute, removed during class creation)
+- ``_generate_next_value_`` -- used by the `Functional API`_ and by
+ :class:`auto` to get an appropriate value for an enum member; may be
+ overridden
+
+.. versionadded:: 3.6 ``_missing_``, ``_order_``, ``_generate_next_value_``
+
+To help keep Python 2 / Python 3 code in sync an :attr:`_order_` attribute can
+be provided. It will be checked against the actual order of the enumeration
+and raise an error if the two do not match::
+
+ >>> class Color(Enum):
+ ... _order_ = 'RED GREEN BLUE'
+ ... RED = 1
+ ... BLUE = 3
+ ... GREEN = 2
+ ...
+ Traceback (most recent call last):
+ ...
+ TypeError: member order does not match _order_
+
+.. note::
+
+ In Python 2 code the :attr:`_order_` attribute is necessary as definition
+ order is lost before it can be recorded.
+
+``Enum`` member type
+""""""""""""""""""""
+
+:class:`Enum` members are instances of their :class:`Enum` class, and are
+normally accessed as ``EnumClass.member``. Under certain circumstances they
+can also be accessed as ``EnumClass.member.member``, but you should never do
+this as that lookup may fail or, worse, return something besides the
+:class:`Enum` member you are looking for (this is another good reason to use
+all-uppercase names for members)::
>>> class FieldTypes(Enum):
... name = 0
@@ -747,7 +1043,24 @@ besides the :class:`Enum` member you looking for::
.. versionchanged:: 3.5
-The :attr:`__members__` attribute is only available on the class.
+
+Boolean value of ``Enum`` classes and members
+"""""""""""""""""""""""""""""""""""""""""""""
+
+:class:`Enum` members that are mixed with non-:class:`Enum` types (such as
+:class:`int`, :class:`str`, etc.) are evaluated according to the mixed-in
+type's rules; otherwise, all members evaluate as :data:`True`. To make your
+own Enum's boolean evaluation depend on the member's value add the following to
+your class::
+
+ def __bool__(self):
+ return bool(self.value)
+
+:class:`Enum` classes always evaluate as :data:`True`.
+
+
+``Enum`` classes with methods
+"""""""""""""""""""""""""""""
If you give your :class:`Enum` subclass extra methods, like the `Planet`_
class above, those methods will show up in a :func:`dir` of the member,
@@ -758,11 +1071,23 @@ but not of the class::
>>> dir(Planet.EARTH)
['__class__', '__doc__', '__module__', 'name', 'surface_gravity', 'value']
-The :meth:`__new__` method will only be used for the creation of the
-:class:`Enum` members -- after that it is replaced. Any custom :meth:`__new__`
-method must create the object and set the :attr:`_value_` attribute
-appropriately.
-If you wish to change how :class:`Enum` members are looked up you should either
-write a helper function or a :func:`classmethod` for the :class:`Enum`
-subclass.
+Combining members of ``Flag``
+"""""""""""""""""""""""""""""
+
+If a combination of Flag members is not named, the :func:`repr` will include
+all named flags and all named combinations of flags that are in the value::
+
+ >>> class Color(Flag):
+ ... RED = auto()
+ ... GREEN = auto()
+ ... BLUE = auto()
+ ... MAGENTA = RED | BLUE
+ ... YELLOW = RED | GREEN
+ ... CYAN = GREEN | BLUE
+ ...
+ >>> Color(3) # named combination
+ <Color.YELLOW: 3>
+ >>> Color(7) # not named combination
+ <Color.CYAN|MAGENTA|BLUE|YELLOW|GREEN|RED: 7>
+
diff --git a/Doc/library/exceptions.rst b/Doc/library/exceptions.rst
index 5a71933..a428f51 100644
--- a/Doc/library/exceptions.rst
+++ b/Doc/library/exceptions.rst
@@ -170,8 +170,9 @@ The following exceptions are the exceptions that are usually raised.
.. exception:: ImportError
- Raised when an :keyword:`import` statement fails to find the module definition
- or when a ``from ... import`` fails to find a name that is to be imported.
+ Raised when the :keyword:`import` statement has troubles trying to
+ load a module. Also raised when the "from list" in ``from ... import``
+ has a name that cannot be found.
The :attr:`name` and :attr:`path` attributes can be set using keyword-only
arguments to the constructor. When set they represent the name of the module
@@ -181,6 +182,14 @@ The following exceptions are the exceptions that are usually raised.
.. versionchanged:: 3.3
Added the :attr:`name` and :attr:`path` attributes.
+.. exception:: ModuleNotFoundError
+
+ A subclass of :exc:`ImportError` which is raised by :keyword:`import`
+ when a module could not be located. It is also raised when ``None``
+ is found in :data:`sys.modules`.
+
+ .. versionadded:: 3.6
+
.. exception:: IndexError
@@ -228,9 +237,21 @@ The following exceptions are the exceptions that are usually raised.
.. exception:: NotImplementedError
This exception is derived from :exc:`RuntimeError`. In user defined base
- classes, abstract methods should raise this exception when they require derived
- classes to override the method.
+ classes, abstract methods should raise this exception when they require
+ derived classes to override the method, or while the class is being
+ developed to indicate that the real implementation still needs to be added.
+
+ .. note::
+ It should not be used to indicate that an operater or method is not
+ meant to be supported at all -- in that case either leave the operator /
+ method undefined or, if a subclass, set it to :data:`None`.
+
+ .. note::
+
+ ``NotImplementedError`` and ``NotImplemented`` are not interchangeable,
+ even though they have similar names and purposes. See
+ :data:`NotImplemented` for details on when to use it.
.. exception:: OSError([arg])
OSError(errno, strerror[, filename[, winerror[, filename2]]])
@@ -436,6 +457,15 @@ The following exceptions are the exceptions that are usually raised.
Raised when an operation or function is applied to an object of inappropriate
type. The associated value is a string giving details about the type mismatch.
+ This exception may be raised by user code to indicate that an attempted
+ operation on an object is not supported, and is not meant to be. If an object
+ is meant to support a given operation but has not yet provided an
+ implementation, :exc:`NotImplementedError` is the proper exception to raise.
+
+ Passing arguments of the wrong type (e.g. passing a :class:`list` when an
+ :class:`int` is expected) should result in a :exc:`TypeError`, but passing
+ arguments with the wrong value (e.g. a number outside expected boundaries)
+ should result in a :exc:`ValueError`.
.. exception:: UnboundLocalError
diff --git a/Doc/library/faulthandler.rst b/Doc/library/faulthandler.rst
index deedea1..d0c4cd0 100644
--- a/Doc/library/faulthandler.rst
+++ b/Doc/library/faulthandler.rst
@@ -70,6 +70,9 @@ Fault handler state
.. versionchanged:: 3.5
Added support for passing file descriptor to this function.
+ .. versionchanged:: 3.6
+ On Windows, a handler for Windows exception is also installed.
+
.. function:: disable()
Disable the fault handler: uninstall the signal handlers installed by
diff --git a/Doc/library/fileinput.rst b/Doc/library/fileinput.rst
index aa4c529..5881fef 100644
--- a/Doc/library/fileinput.rst
+++ b/Doc/library/fileinput.rst
@@ -72,9 +72,8 @@ The following function is the primary interface of this module:
.. versionchanged:: 3.2
Can be used as a context manager.
- .. versionchanged:: 3.5.2
- The *bufsize* parameter is no longer used.
-
+ .. deprecated-removed:: 3.6 3.8
+ The *bufsize* parameter.
The following functions use the global state created by :func:`fileinput.input`;
if there is no active state, :exc:`RuntimeError` is raised.
@@ -167,8 +166,8 @@ available for subclassing as well:
.. deprecated:: 3.4
The ``'rU'`` and ``'U'`` modes.
- .. versionchanged:: 3.5.2
- The *bufsize* parameter is no longer used.
+ .. deprecated-removed:: 3.6 3.8
+ The *bufsize* parameter.
**Optional in-place filtering:** if the keyword argument ``inplace=True`` is
@@ -195,10 +194,14 @@ The two following opening hooks are provided by this module:
Usage example: ``fi = fileinput.FileInput(openhook=fileinput.hook_compressed)``
-.. function:: hook_encoded(encoding)
+.. function:: hook_encoded(encoding, errors=None)
Returns a hook which opens each file with :func:`open`, using the given
- *encoding* to read the file.
+ *encoding* and *errors* to read the file.
Usage example: ``fi =
- fileinput.FileInput(openhook=fileinput.hook_encoded("iso-8859-1"))``
+ fileinput.FileInput(openhook=fileinput.hook_encoded("utf-8",
+ "surrogateescape"))``
+
+ .. versionchanged:: 3.6
+ Added the optional *errors* parameter.
diff --git a/Doc/library/fnmatch.rst b/Doc/library/fnmatch.rst
index 9fc9c7c..c03a9d3 100644
--- a/Doc/library/fnmatch.rst
+++ b/Doc/library/fnmatch.rst
@@ -83,7 +83,7 @@ patterns.
>>>
>>> regex = fnmatch.translate('*.txt')
>>> regex
- '.*\\.txt\\Z(?ms)'
+ '(?s:.*\\.txt)\\Z'
>>> reobj = re.compile(regex)
>>> reobj.match('foobar.txt')
<_sre.SRE_Match object; span=(0, 10), match='foobar.txt'>
diff --git a/Doc/library/ftplib.rst b/Doc/library/ftplib.rst
index 1e35f37..b8c1dcf 100644
--- a/Doc/library/ftplib.rst
+++ b/Doc/library/ftplib.rst
@@ -97,6 +97,13 @@ The module defines the following items:
:attr:`ssl.SSLContext.check_hostname` and *Server Name Indication* (see
:data:`ssl.HAS_SNI`).
+ .. deprecated:: 3.6
+
+ *keyfile* and *certfile* are deprecated in favor of *context*.
+ Please use :meth:`ssl.SSLContext.load_cert_chain` instead, or let
+ :func:`ssl.create_default_context` select the system's trusted CA
+ certificates for you.
+
Here's a sample session using the :class:`FTP_TLS` class::
>>> ftps = FTP_TLS('ftp.pureftpd.org')
diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst
index 2f14949..c26037b 100644
--- a/Doc/library/functions.rst
+++ b/Doc/library/functions.rst
@@ -271,6 +271,9 @@ are always available. They are listed here in alphabetical order.
The complex type is described in :ref:`typesnumeric`.
+ .. versionchanged:: 3.6
+ Grouping digits with underscores as in code literals is allowed.
+
.. function:: delattr(object, name)
@@ -531,11 +534,14 @@ are always available. They are listed here in alphabetical order.
The float type is described in :ref:`typesnumeric`.
- .. index::
- single: __format__
- single: string; format() (built-in function)
+ .. versionchanged:: 3.6
+ Grouping digits with underscores as in code literals is allowed.
+.. index::
+ single: __format__
+ single: string; format() (built-in function)
+
.. function:: format(value[, format_spec])
Convert a *value* to a "formatted" representation, as controlled by
@@ -702,6 +708,10 @@ are always available. They are listed here in alphabetical order.
:meth:`base.__int__ <object.__int__>` instead of :meth:`base.__index__
<object.__index__>`.
+ .. versionchanged:: 3.6
+ Grouping digits with underscores as in code literals is allowed.
+
+
.. function:: isinstance(object, classinfo)
Return true if the *object* argument is an instance of the *classinfo*
@@ -878,11 +888,11 @@ are always available. They are listed here in alphabetical order.
Open *file* and return a corresponding :term:`file object`. If the file
cannot be opened, an :exc:`OSError` is raised.
- *file* is either a string or bytes object giving the pathname (absolute or
- relative to the current working directory) of the file to be opened or
- an integer file descriptor of the file to be wrapped. (If a file descriptor
- is given, it is closed when the returned I/O object is closed, unless
- *closefd* is set to ``False``.)
+ *file* is a :term:`path-like object` giving the pathname (absolute or
+ relative to the current working directory) of the file to be opened or an
+ integer file descriptor of the file to be wrapped. (If a file descriptor is
+ given, it is closed when the returned I/O object is closed, unless *closefd*
+ is set to ``False``.)
*mode* is an optional string that specifies the mode in which the file is
opened. It defaults to ``'r'`` which means open for reading in text mode.
@@ -1055,27 +1065,38 @@ are always available. They are listed here in alphabetical order.
(where :func:`open` is declared), :mod:`os`, :mod:`os.path`, :mod:`tempfile`,
and :mod:`shutil`.
- .. versionchanged:: 3.3
- The *opener* parameter was added.
- The ``'x'`` mode was added.
- :exc:`IOError` used to be raised, it is now an alias of :exc:`OSError`.
- :exc:`FileExistsError` is now raised if the file opened in exclusive
- creation mode (``'x'``) already exists.
+ .. versionchanged::
+ 3.3
- .. versionchanged:: 3.4
- The file is now non-inheritable.
+ * The *opener* parameter was added.
+ * The ``'x'`` mode was added.
+ * :exc:`IOError` used to be raised, it is now an alias of :exc:`OSError`.
+ * :exc:`FileExistsError` is now raised if the file opened in exclusive
+ * creation mode (``'x'``) already exists.
+
+ .. versionchanged::
+ 3.4
+
+ * The file is now non-inheritable.
.. deprecated-removed:: 3.4 4.0
The ``'U'`` mode.
- .. versionchanged:: 3.5
- If the system call is interrupted and the signal handler does not raise an
- exception, the function now retries the system call instead of raising an
- :exc:`InterruptedError` exception (see :pep:`475` for the rationale).
+ .. versionchanged::
+ 3.5
- .. versionchanged:: 3.5
- The ``'namereplace'`` error handler was added.
+ * If the system call is interrupted and the signal handler does not raise an
+ exception, the function now retries the system call instead of raising an
+ :exc:`InterruptedError` exception (see :pep:`475` for the rationale).
+ * The ``'namereplace'`` error handler was added.
+
+ .. versionchanged::
+ 3.6
+
+ * Support added to accept objects implementing :class:`os.PathLike`.
+ * On Windows, opening a console buffer may return a subclass of
+ :class:`io.RawIOBase` other than :class:`io.FileIO`.
.. function:: ord(c)
@@ -1460,6 +1481,9 @@ are always available. They are listed here in alphabetical order.
See also :ref:`bltin-type-objects`.
+ .. versionchanged:: 3.6
+ Subclasses of :class:`type` which don't override ``type.__new__`` may no
+ longer use the one-argument form to get the type of an object.
.. function:: vars([object])
diff --git a/Doc/library/glob.rst b/Doc/library/glob.rst
index 328eef3..a8a5a50 100644
--- a/Doc/library/glob.rst
+++ b/Doc/library/glob.rst
@@ -14,7 +14,7 @@ The :mod:`glob` module finds all the pathnames matching a specified pattern
according to the rules used by the Unix shell, although results are returned in
arbitrary order. No tilde expansion is done, but ``*``, ``?``, and character
ranges expressed with ``[]`` will be correctly matched. This is done by using
-the :func:`os.listdir` and :func:`fnmatch.fnmatch` functions in concert, and
+the :func:`os.scandir` and :func:`fnmatch.fnmatch` functions in concert, and
not by actually invoking a subshell. Note that unlike :func:`fnmatch.fnmatch`,
:mod:`glob` treats filenames beginning with a dot (``.``) as special cases.
(For tilde and shell variable expansion, use :func:`os.path.expanduser` and
diff --git a/Doc/library/grp.rst b/Doc/library/grp.rst
index a30e622..74de3f9 100644
--- a/Doc/library/grp.rst
+++ b/Doc/library/grp.rst
@@ -43,6 +43,9 @@ It defines the following items:
Return the group database entry for the given numeric group ID. :exc:`KeyError`
is raised if the entry asked for cannot be found.
+ .. deprecated:: 3.6
+ Since Python 3.6 the support of non-integer arguments like floats or
+ strings in :func:`getgrgid` is deprecated.
.. function:: getgrnam(name)
diff --git a/Doc/library/gzip.rst b/Doc/library/gzip.rst
index 792d57a..9c6b722 100644
--- a/Doc/library/gzip.rst
+++ b/Doc/library/gzip.rst
@@ -56,6 +56,8 @@ The module defines the following items:
.. versionchanged:: 3.4
Added support for the ``'x'``, ``'xb'`` and ``'xt'`` modes.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
.. class:: GzipFile(filename=None, mode=None, compresslevel=9, fileobj=None, mtime=None)
@@ -151,6 +153,9 @@ The module defines the following items:
The :meth:`~io.BufferedIOBase.read` method now accepts an argument of
``None``.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: compress(data, compresslevel=9)
diff --git a/Doc/library/hashlib-blake2-tree.png b/Doc/library/hashlib-blake2-tree.png
new file mode 100644
index 0000000..010dcba
--- /dev/null
+++ b/Doc/library/hashlib-blake2-tree.png
Binary files differ
diff --git a/Doc/library/hashlib.rst b/Doc/library/hashlib.rst
index a2e96ca..9d56356 100644
--- a/Doc/library/hashlib.rst
+++ b/Doc/library/hashlib.rst
@@ -44,8 +44,8 @@ Hash algorithms
---------------
There is one constructor method named for each type of :dfn:`hash`. All return
-a hash object with the same simple interface. For example: use :func:`sha1` to
-create a SHA1 hash object. You can now feed this object with :term:`bytes-like
+a hash object with the same simple interface. For example: use :func:`sha256` to
+create a SHA-256 hash object. You can now feed this object with :term:`bytes-like
objects <bytes-like object>` (normally :class:`bytes`) using the :meth:`update` method.
At any point you can ask it for the :dfn:`digest` of the
concatenation of the data fed to it so far using the :meth:`digest` or
@@ -64,21 +64,33 @@ concatenation of the data fed to it so far using the :meth:`digest` or
.. index:: single: OpenSSL; (use in module hashlib)
Constructors for hash algorithms that are always present in this module are
-:func:`md5`, :func:`sha1`, :func:`sha224`, :func:`sha256`, :func:`sha384`,
-and :func:`sha512`. Additional algorithms may also be available depending upon
-the OpenSSL library that Python uses on your platform.
+:func:`sha1`, :func:`sha224`, :func:`sha256`, :func:`sha384`,
+:func:`sha512`, :func:`blake2b`, and :func:`blake2s`.
+:func:`md5` is normally available as well, though it
+may be missing if you are using a rare "FIPS compliant" build of Python.
+Additional algorithms may also be available depending upon the OpenSSL
+library that Python uses on your platform. On most platforms the
+:func:`sha3_224`, :func:`sha3_256`, :func:`sha3_384`, :func:`sha3_512`,
+:func:`shake_128`, :func:`shake_256` are also available.
+
+.. versionadded:: 3.6
+ SHA3 (Keccak) and SHAKE constructors :func:`sha3_224`, :func:`sha3_256`,
+ :func:`sha3_384`, :func:`sha3_512`, :func:`shake_128`, :func:`shake_256`.
+
+.. versionadded:: 3.6
+ :func:`blake2b` and :func:`blake2s` were added.
For example, to obtain the digest of the byte string ``b'Nobody inspects the
spammish repetition'``::
>>> import hashlib
- >>> m = hashlib.md5()
+ >>> m = hashlib.sha256()
>>> m.update(b"Nobody inspects")
>>> m.update(b" the spammish repetition")
>>> m.digest()
- b'\xbbd\x9c\x83\xdd\x1e\xa5\xc9\xd9\xde\xc9\xa1\x8d\xf0\xff\xe9'
+ b'\x03\x1e\xdd}Ae\x15\x93\xc5\xfe\\\x00o\xa5u+7\xfd\xdf\xf7\xbcN\x84:\xa6\xaf\x0c\x95\x0fK\x94\x06'
>>> m.digest_size
- 16
+ 32
>>> m.block_size
64
@@ -107,7 +119,9 @@ Hashlib provides the following constant attributes:
.. data:: algorithms_guaranteed
A set containing the names of the hash algorithms guaranteed to be supported
- by this module on all platforms.
+ by this module on all platforms. Note that 'md5' is in this list despite
+ some upstream vendors offering an odd "FIPS compliant" Python build that
+ excludes it.
.. versionadded:: 3.2
@@ -181,6 +195,28 @@ A hash object has the following methods:
compute the digests of data sharing a common initial substring.
+SHAKE variable length digests
+-----------------------------
+
+The :func:`shake_128` and :func:`shake_256` algorithms provide variable
+length digests with length_in_bits//2 up to 128 or 256 bits of security.
+As such, their digest methods require a length. Maximum length is not limited
+by the SHAKE algorithm.
+
+.. method:: shake.digest(length)
+
+ Return the digest of the data passed to the :meth:`update` method so far.
+ This is a bytes object of size ``length`` which may contain bytes in
+ the whole range from 0 to 255.
+
+
+.. method:: shake.hexdigest(length)
+
+ Like :meth:`digest` except the digest is returned as a string object of
+ double length, containing only hexadecimal digits. This may be used to
+ exchange the value safely in email or other non-binary environments.
+
+
Key derivation
--------------
@@ -221,6 +257,460 @@ include a `salt <https://en.wikipedia.org/wiki/Salt_%28cryptography%29>`_.
Python implementation uses an inline version of :mod:`hmac`. It is about
three times slower and doesn't release the GIL.
+.. function:: scrypt(password, *, salt, n, r, p, maxmem=0, dklen=64)
+
+ The function provides scrypt password-based key derivation function as
+ defined in :rfc:`7914`.
+
+ *password* and *salt* must be bytes-like objects. Applications and
+ libraries should limit *password* to a sensible length (e.g. 1024). *salt*
+ should be about 16 or more bytes from a proper source, e.g. :func:`os.urandom`.
+
+ *n* is the CPU/Memory cost factor, *r* the block size, *p* parallelization
+ factor and *maxmem* limits memory (OpenSSL 1.1.0 defaults to 32 MB).
+ *dklen* is the length of the derived key.
+
+ Availability: OpenSSL 1.1+
+
+ .. versionadded:: 3.6
+
+
+BLAKE2
+------
+
+.. sectionauthor:: Dmitry Chestnykh
+
+.. index::
+ single: blake2b, blake2s
+
+BLAKE2_ is a cryptographic hash function defined in RFC-7693_ that comes in two
+flavors:
+
+* **BLAKE2b**, optimized for 64-bit platforms and produces digests of any size
+ between 1 and 64 bytes,
+
+* **BLAKE2s**, optimized for 8- to 32-bit platforms and produces digests of any
+ size between 1 and 32 bytes.
+
+BLAKE2 supports **keyed mode** (a faster and simpler replacement for HMAC_),
+**salted hashing**, **personalization**, and **tree hashing**.
+
+Hash objects from this module follow the API of standard library's
+:mod:`hashlib` objects.
+
+
+Creating hash objects
+^^^^^^^^^^^^^^^^^^^^^
+
+New hash objects are created by calling constructor functions:
+
+
+.. function:: blake2b(data=b'', digest_size=64, key=b'', salt=b'', \
+ person=b'', fanout=1, depth=1, leaf_size=0, node_offset=0, \
+ node_depth=0, inner_size=0, last_node=False)
+
+.. function:: blake2s(data=b'', digest_size=32, key=b'', salt=b'', \
+ person=b'', fanout=1, depth=1, leaf_size=0, node_offset=0, \
+ node_depth=0, inner_size=0, last_node=False)
+
+
+These functions return the corresponding hash objects for calculating
+BLAKE2b or BLAKE2s. They optionally take these general parameters:
+
+* *data*: initial chunk of data to hash, which must be interpretable as buffer
+ of bytes.
+
+* *digest_size*: size of output digest in bytes.
+
+* *key*: key for keyed hashing (up to 64 bytes for BLAKE2b, up to 32 bytes for
+ BLAKE2s).
+
+* *salt*: salt for randomized hashing (up to 16 bytes for BLAKE2b, up to 8
+ bytes for BLAKE2s).
+
+* *person*: personalization string (up to 16 bytes for BLAKE2b, up to 8 bytes
+ for BLAKE2s).
+
+The following table shows limits for general parameters (in bytes):
+
+======= =========== ======== ========= ===========
+Hash digest_size len(key) len(salt) len(person)
+======= =========== ======== ========= ===========
+BLAKE2b 64 64 16 16
+BLAKE2s 32 32 8 8
+======= =========== ======== ========= ===========
+
+.. note::
+
+ BLAKE2 specification defines constant lengths for salt and personalization
+ parameters, however, for convenience, this implementation accepts byte
+ strings of any size up to the specified length. If the length of the
+ parameter is less than specified, it is padded with zeros, thus, for
+ example, ``b'salt'`` and ``b'salt\x00'`` is the same value. (This is not
+ the case for *key*.)
+
+These sizes are available as module `constants`_ described below.
+
+Constructor functions also accept the following tree hashing parameters:
+
+* *fanout*: fanout (0 to 255, 0 if unlimited, 1 in sequential mode).
+
+* *depth*: maximal depth of tree (1 to 255, 255 if unlimited, 1 in
+ sequential mode).
+
+* *leaf_size*: maximal byte length of leaf (0 to 2**32-1, 0 if unlimited or in
+ sequential mode).
+
+* *node_offset*: node offset (0 to 2**64-1 for BLAKE2b, 0 to 2**48-1 for
+ BLAKE2s, 0 for the first, leftmost, leaf, or in sequential mode).
+
+* *node_depth*: node depth (0 to 255, 0 for leaves, or in sequential mode).
+
+* *inner_size*: inner digest size (0 to 64 for BLAKE2b, 0 to 32 for
+ BLAKE2s, 0 in sequential mode).
+
+* *last_node*: boolean indicating whether the processed node is the last
+ one (`False` for sequential mode).
+
+.. figure:: hashlib-blake2-tree.png
+ :alt: Explanation of tree mode parameters.
+
+See section 2.10 in `BLAKE2 specification
+<https://blake2.net/blake2_20130129.pdf>`_ for comprehensive review of tree
+hashing.
+
+
+Constants
+^^^^^^^^^
+
+.. data:: blake2b.SALT_SIZE
+.. data:: blake2s.SALT_SIZE
+
+Salt length (maximum length accepted by constructors).
+
+
+.. data:: blake2b.PERSON_SIZE
+.. data:: blake2s.PERSON_SIZE
+
+Personalization string length (maximum length accepted by constructors).
+
+
+.. data:: blake2b.MAX_KEY_SIZE
+.. data:: blake2s.MAX_KEY_SIZE
+
+Maximum key size.
+
+
+.. data:: blake2b.MAX_DIGEST_SIZE
+.. data:: blake2s.MAX_DIGEST_SIZE
+
+Maximum digest size that the hash function can output.
+
+
+Examples
+^^^^^^^^
+
+Simple hashing
+""""""""""""""
+
+To calculate hash of some data, you should first construct a hash object by
+calling the appropriate constructor function (:func:`blake2b` or
+:func:`blake2s`), then update it with the data by calling :meth:`update` on the
+object, and, finally, get the digest out of the object by calling
+:meth:`digest` (or :meth:`hexdigest` for hex-encoded string).
+
+ >>> from hashlib import blake2b
+ >>> h = blake2b()
+ >>> h.update(b'Hello world')
+ >>> h.hexdigest()
+ '6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183'
+
+
+As a shortcut, you can pass the first chunk of data to update directly to the
+constructor as the first argument (or as *data* keyword argument):
+
+ >>> from hashlib import blake2b
+ >>> blake2b(b'Hello world').hexdigest()
+ '6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183'
+
+You can call :meth:`hash.update` as many times as you need to iteratively
+update the hash:
+
+ >>> from hashlib import blake2b
+ >>> items = [b'Hello', b' ', b'world']
+ >>> h = blake2b()
+ >>> for item in items:
+ ... h.update(item)
+ >>> h.hexdigest()
+ '6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183'
+
+
+Using different digest sizes
+""""""""""""""""""""""""""""
+
+BLAKE2 has configurable size of digests up to 64 bytes for BLAKE2b and up to 32
+bytes for BLAKE2s. For example, to replace SHA-1 with BLAKE2b without changing
+the size of output, we can tell BLAKE2b to produce 20-byte digests:
+
+ >>> from hashlib import blake2b
+ >>> h = blake2b(digest_size=20)
+ >>> h.update(b'Replacing SHA1 with the more secure function')
+ >>> h.hexdigest()
+ 'd24f26cf8de66472d58d4e1b1774b4c9158b1f4c'
+ >>> h.digest_size
+ 20
+ >>> len(h.digest())
+ 20
+
+Hash objects with different digest sizes have completely different outputs
+(shorter hashes are *not* prefixes of longer hashes); BLAKE2b and BLAKE2s
+produce different outputs even if the output length is the same:
+
+ >>> from hashlib import blake2b, blake2s
+ >>> blake2b(digest_size=10).hexdigest()
+ '6fa1d8fcfd719046d762'
+ >>> blake2b(digest_size=11).hexdigest()
+ 'eb6ec15daf9546254f0809'
+ >>> blake2s(digest_size=10).hexdigest()
+ '1bf21a98c78a1c376ae9'
+ >>> blake2s(digest_size=11).hexdigest()
+ '567004bf96e4a25773ebf4'
+
+
+Keyed hashing
+"""""""""""""
+
+Keyed hashing can be used for authentication as a faster and simpler
+replacement for `Hash-based message authentication code
+<http://en.wikipedia.org/wiki/Hash-based_message_authentication_code>`_ (HMAC).
+BLAKE2 can be securely used in prefix-MAC mode thanks to the
+indifferentiability property inherited from BLAKE.
+
+This example shows how to get a (hex-encoded) 128-bit authentication code for
+message ``b'message data'`` with key ``b'pseudorandom key'``::
+
+ >>> from hashlib import blake2b
+ >>> h = blake2b(key=b'pseudorandom key', digest_size=16)
+ >>> h.update(b'message data')
+ >>> h.hexdigest()
+ '3d363ff7401e02026f4a4687d4863ced'
+
+
+As a practical example, a web application can symmetrically sign cookies sent
+to users and later verify them to make sure they weren't tampered with::
+
+ >>> from hashlib import blake2b
+ >>> from hmac import compare_digest
+ >>>
+ >>> SECRET_KEY = b'pseudorandomly generated server secret key'
+ >>> AUTH_SIZE = 16
+ >>>
+ >>> def sign(cookie):
+ ... h = blake2b(data=cookie, digest_size=AUTH_SIZE, key=SECRET_KEY)
+ ... return h.hexdigest()
+ >>>
+ >>> cookie = b'user:vatrogasac'
+ >>> sig = sign(cookie)
+ >>> print("{0},{1}".format(cookie.decode('utf-8'), sig))
+ user:vatrogasac,349cf904533767ed2d755279a8df84d0
+ >>> compare_digest(cookie, sig)
+ True
+ >>> compare_digest(b'user:policajac', sig)
+ False
+ >>> compare_digesty(cookie, '0102030405060708090a0b0c0d0e0f00')
+ False
+
+Even though there's a native keyed hashing mode, BLAKE2 can, of course, be used
+in HMAC construction with :mod:`hmac` module::
+
+ >>> import hmac, hashlib
+ >>> m = hmac.new(b'secret key', digestmod=hashlib.blake2s)
+ >>> m.update(b'message')
+ >>> m.hexdigest()
+ 'e3c8102868d28b5ff85fc35dda07329970d1a01e273c37481326fe0c861c8142'
+
+
+Randomized hashing
+""""""""""""""""""
+
+By setting *salt* parameter users can introduce randomization to the hash
+function. Randomized hashing is useful for protecting against collision attacks
+on the hash function used in digital signatures.
+
+ Randomized hashing is designed for situations where one party, the message
+ preparer, generates all or part of a message to be signed by a second
+ party, the message signer. If the message preparer is able to find
+ cryptographic hash function collisions (i.e., two messages producing the
+ same hash value), then she might prepare meaningful versions of the message
+ that would produce the same hash value and digital signature, but with
+ different results (e.g., transferring $1,000,000 to an account, rather than
+ $10). Cryptographic hash functions have been designed with collision
+ resistance as a major goal, but the current concentration on attacking
+ cryptographic hash functions may result in a given cryptographic hash
+ function providing less collision resistance than expected. Randomized
+ hashing offers the signer additional protection by reducing the likelihood
+ that a preparer can generate two or more messages that ultimately yield the
+ same hash value during the digital signature generation process --- even if
+ it is practical to find collisions for the hash function. However, the use
+ of randomized hashing may reduce the amount of security provided by a
+ digital signature when all portions of the message are prepared
+ by the signer.
+
+ (`NIST SP-800-106 "Randomized Hashing for Digital Signatures"
+ <http://csrc.nist.gov/publications/nistpubs/800-106/NIST-SP-800-106.pdf>`_)
+
+In BLAKE2 the salt is processed as a one-time input to the hash function during
+initialization, rather than as an input to each compression function.
+
+.. warning::
+
+ *Salted hashing* (or just hashing) with BLAKE2 or any other general-purpose
+ cryptographic hash function, such as SHA-256, is not suitable for hashing
+ passwords. See `BLAKE2 FAQ <https://blake2.net/#qa>`_ for more
+ information.
+..
+
+ >>> import os
+ >>> from hashlib import blake2b
+ >>> msg = b'some message'
+ >>> # Calculate the first hash with a random salt.
+ >>> salt1 = os.urandom(blake2b.SALT_SIZE)
+ >>> h1 = blake2b(salt=salt1)
+ >>> h1.update(msg)
+ >>> # Calculate the second hash with a different random salt.
+ >>> salt2 = os.urandom(blake2b.SALT_SIZE)
+ >>> h2 = blake2b(salt=salt2)
+ >>> h2.update(msg)
+ >>> # The digests are different.
+ >>> h1.digest() != h2.digest()
+ True
+
+
+Personalization
+"""""""""""""""
+
+Sometimes it is useful to force hash function to produce different digests for
+the same input for different purposes. Quoting the authors of the Skein hash
+function:
+
+ We recommend that all application designers seriously consider doing this;
+ we have seen many protocols where a hash that is computed in one part of
+ the protocol can be used in an entirely different part because two hash
+ computations were done on similar or related data, and the attacker can
+ force the application to make the hash inputs the same. Personalizing each
+ hash function used in the protocol summarily stops this type of attack.
+
+ (`The Skein Hash Function Family
+ <http://www.skein-hash.info/sites/default/files/skein1.3.pdf>`_,
+ p. 21)
+
+BLAKE2 can be personalized by passing bytes to the *person* argument::
+
+ >>> from hashlib import blake2b
+ >>> FILES_HASH_PERSON = b'MyApp Files Hash'
+ >>> BLOCK_HASH_PERSON = b'MyApp Block Hash'
+ >>> h = blake2b(digest_size=32, person=FILES_HASH_PERSON)
+ >>> h.update(b'the same content')
+ >>> h.hexdigest()
+ '20d9cd024d4fb086aae819a1432dd2466de12947831b75c5a30cf2676095d3b4'
+ >>> h = blake2b(digest_size=32, person=BLOCK_HASH_PERSON)
+ >>> h.update(b'the same content')
+ >>> h.hexdigest()
+ 'cf68fb5761b9c44e7878bfb2c4c9aea52264a80b75005e65619778de59f383a3'
+
+Personalization together with the keyed mode can also be used to derive different
+keys from a single one.
+
+ >>> from hashlib import blake2s
+ >>> from base64 import b64decode, b64encode
+ >>> orig_key = b64decode(b'Rm5EPJai72qcK3RGBpW3vPNfZy5OZothY+kHY6h21KM=')
+ >>> enc_key = blake2s(key=orig_key, person=b'kEncrypt').digest()
+ >>> mac_key = blake2s(key=orig_key, person=b'kMAC').digest()
+ >>> print(b64encode(enc_key).decode('utf-8'))
+ rbPb15S/Z9t+agffno5wuhB77VbRi6F9Iv2qIxU7WHw=
+ >>> print(b64encode(mac_key).decode('utf-8'))
+ G9GtHFE1YluXY1zWPlYk1e/nWfu0WSEb0KRcjhDeP/o=
+
+Tree mode
+"""""""""
+
+Here's an example of hashing a minimal tree with two leaf nodes::
+
+ 10
+ / \
+ 00 01
+
+This example uses 64-byte internal digests, and returns the 32-byte final
+digest::
+
+ >>> from hashlib import blake2b
+ >>>
+ >>> FANOUT = 2
+ >>> DEPTH = 2
+ >>> LEAF_SIZE = 4096
+ >>> INNER_SIZE = 64
+ >>>
+ >>> buf = bytearray(6000)
+ >>>
+ >>> # Left leaf
+ ... h00 = blake2b(buf[0:LEAF_SIZE], fanout=FANOUT, depth=DEPTH,
+ ... leaf_size=LEAF_SIZE, inner_size=INNER_SIZE,
+ ... node_offset=0, node_depth=0, last_node=False)
+ >>> # Right leaf
+ ... h01 = blake2b(buf[LEAF_SIZE:], fanout=FANOUT, depth=DEPTH,
+ ... leaf_size=LEAF_SIZE, inner_size=INNER_SIZE,
+ ... node_offset=1, node_depth=0, last_node=True)
+ >>> # Root node
+ ... h10 = blake2b(digest_size=32, fanout=FANOUT, depth=DEPTH,
+ ... leaf_size=LEAF_SIZE, inner_size=INNER_SIZE,
+ ... node_offset=0, node_depth=1, last_node=True)
+ >>> h10.update(h00.digest())
+ >>> h10.update(h01.digest())
+ >>> h10.hexdigest()
+ '3ad2a9b37c6070e374c7a8c508fe20ca86b6ed54e286e93a0318e95e881db5aa'
+
+Credits
+^^^^^^^
+
+BLAKE2_ was designed by *Jean-Philippe Aumasson*, *Samuel Neves*, *Zooko
+Wilcox-O'Hearn*, and *Christian Winnerlein* based on SHA-3_ finalist BLAKE_
+created by *Jean-Philippe Aumasson*, *Luca Henzen*, *Willi Meier*, and
+*Raphael C.-W. Phan*.
+
+It uses core algorithm from ChaCha_ cipher designed by *Daniel J. Bernstein*.
+
+The stdlib implementation is based on pyblake2_ module. It was written by
+*Dmitry Chestnykh* based on C implementation written by *Samuel Neves*. The
+documentation was copied from pyblake2_ and written by *Dmitry Chestnykh*.
+
+The C code was partly rewritten for Python by *Christian Heimes*.
+
+The following public domain dedication applies for both C hash function
+implementation, extension code, and this documentation:
+
+ To the extent possible under law, the author(s) have dedicated all copyright
+ and related and neighboring rights to this software to the public domain
+ worldwide. This software is distributed without any warranty.
+
+ You should have received a copy of the CC0 Public Domain Dedication along
+ with this software. If not, see
+ http://creativecommons.org/publicdomain/zero/1.0/.
+
+The following people have helped with development or contributed their changes
+to the project and the public domain according to the Creative Commons Public
+Domain Dedication 1.0 Universal:
+
+* *Alexandr Sokolovskiy*
+
+.. _RFC-7693: https://tools.ietf.org/html/rfc7693
+.. _BLAKE2: https://blake2.net
+.. _HMAC: https://en.wikipedia.org/wiki/Hash-based_message_authentication_code
+.. _BLAKE: https://131002.net/blake/
+.. _SHA-3: https://en.wikipedia.org/wiki/NIST_hash_function_competition
+.. _ChaCha: https://cr.yp.to/chacha.html
+.. _pyblake2: https://pythonhosted.org/pyblake2/
+
+
.. seealso::
@@ -230,6 +720,9 @@ include a `salt <https://en.wikipedia.org/wiki/Salt_%28cryptography%29>`_.
Module :mod:`base64`
Another way to encode binary hashes for non-binary environments.
+ https://blake2.net
+ Official BLAKE2 website.
+
http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
The FIPS 180-2 publication on Secure Hash Algorithms.
diff --git a/Doc/library/http.client.rst b/Doc/library/http.client.rst
index e1acf92..2f59ece 100644
--- a/Doc/library/http.client.rst
+++ b/Doc/library/http.client.rst
@@ -69,13 +69,6 @@ The module provides the following classes:
must be a :class:`ssl.SSLContext` instance describing the various SSL
options.
- *key_file* and *cert_file* are deprecated, please use
- :meth:`ssl.SSLContext.load_cert_chain` instead, or let
- :func:`ssl.create_default_context` select the system's trusted CA
- certificates for you. The *check_hostname* parameter is also deprecated; the
- :attr:`ssl.SSLContext.check_hostname` attribute of *context* should be used
- instead.
-
Please read :ref:`ssl-security` for more information on best practices.
.. versionchanged:: 3.2
@@ -95,6 +88,17 @@ The module provides the following classes:
:func:`ssl._create_unverified_context` can be passed to the *context*
parameter.
+ .. deprecated:: 3.6
+
+ *key_file* and *cert_file* are deprecated in favor of *context*.
+ Please use :meth:`ssl.SSLContext.load_cert_chain` instead, or let
+ :func:`ssl.create_default_context` select the system's trusted CA
+ certificates for you.
+
+ The *check_hostname* parameter is also deprecated; the
+ :attr:`ssl.SSLContext.check_hostname` attribute of *context* should
+ be used instead.
+
.. class:: HTTPResponse(sock, debuglevel=0, method=None, url=None)
@@ -217,39 +221,61 @@ HTTPConnection Objects
:class:`HTTPConnection` instances have the following methods:
-.. method:: HTTPConnection.request(method, url, body=None, headers={})
+.. method:: HTTPConnection.request(method, url, body=None, headers={}, *, \
+ encode_chunked=False)
This will send a request to the server using the HTTP request
method *method* and the selector *url*.
If *body* is specified, the specified data is sent after the headers are
- finished. It may be a string, a :term:`bytes-like object`, an open
- :term:`file object`, or an iterable of :term:`bytes-like object`\s. If
- *body* is a string, it is encoded as ISO-8859-1, the default for HTTP. If
- it is a bytes-like object the bytes are sent as is. If it is a :term:`file
- object`, the contents of the file is sent; this file object should support
- at least the ``read()`` method. If the file object has a ``mode``
- attribute, the data returned by the ``read()`` method will be encoded as
- ISO-8859-1 unless the ``mode`` attribute contains the substring ``b``,
- otherwise the data returned by ``read()`` is sent as is. If *body* is an
- iterable, the elements of the iterable are sent as is until the iterable is
- exhausted.
-
- The *headers* argument should be a mapping of extra HTTP
- headers to send with the request.
-
- If *headers* does not contain a Content-Length item, one is added
- automatically if possible. If *body* is ``None``, the Content-Length header
- is set to ``0`` for methods that expect a body (``PUT``, ``POST``, and
- ``PATCH``). If *body* is a string or bytes object, the Content-Length
- header is set to its length. If *body* is a :term:`file object` and it
- works to call :func:`~os.fstat` on the result of its ``fileno()`` method,
- then the Content-Length header is set to the ``st_size`` reported by the
- ``fstat`` call. Otherwise no Content-Length header is added.
+ finished. It may be a :class:`str`, a :term:`bytes-like object`, an
+ open :term:`file object`, or an iterable of :class:`bytes`. If *body*
+ is a string, it is encoded as ISO-8859-1, the default for HTTP. If it
+ is a bytes-like object, the bytes are sent as is. If it is a :term:`file
+ object`, the contents of the file is sent; this file object should
+ support at least the ``read()`` method. If the file object is an
+ instance of :class:`io.TextIOBase`, the data returned by the ``read()``
+ method will be encoded as ISO-8859-1, otherwise the data returned by
+ ``read()`` is sent as is. If *body* is an iterable, the elements of the
+ iterable are sent as is until the iterable is exhausted.
+
+ The *headers* argument should be a mapping of extra HTTP headers to send
+ with the request.
+
+ If *headers* contains neither Content-Length nor Transfer-Encoding,
+ but there is a request body, one of those
+ header fields will be added automatically. If
+ *body* is ``None``, the Content-Length header is set to ``0`` for
+ methods that expect a body (``PUT``, ``POST``, and ``PATCH``). If
+ *body* is a string or a bytes-like object that is not also a
+ :term:`file <file object>`, the Content-Length header is
+ set to its length. Any other type of *body* (files
+ and iterables in general) will be chunk-encoded, and the
+ Transfer-Encoding header will automatically be set instead of
+ Content-Length.
+
+ The *encode_chunked* argument is only relevant if Transfer-Encoding is
+ specified in *headers*. If *encode_chunked* is ``False``, the
+ HTTPConnection object assumes that all encoding is handled by the
+ calling code. If it is ``True``, the body will be chunk-encoded.
+
+ .. note::
+ Chunked transfer encoding has been added to the HTTP protocol
+ version 1.1. Unless the HTTP server is known to handle HTTP 1.1,
+ the caller must either specify the Content-Length, or must pass a
+ :class:`str` or bytes-like object that is not also a file as the
+ body representation.
.. versionadded:: 3.2
*body* can now be an iterable.
+ .. versionchanged:: 3.6
+ If neither Content-Length nor Transfer-Encoding are set in
+ *headers*, file and iterable *body* objects are now chunk-encoded.
+ The *encode_chunked* argument was added.
+ No attempt is made to determine the Content-Length for file
+ objects.
+
.. method:: HTTPConnection.getresponse()
Should be called after a request is sent to get the response from the server.
@@ -335,13 +361,32 @@ also send your request step by step, by using the four functions below.
an argument.
-.. method:: HTTPConnection.endheaders(message_body=None)
+.. method:: HTTPConnection.endheaders(message_body=None, *, encode_chunked=False)
Send a blank line to the server, signalling the end of the headers. The
optional *message_body* argument can be used to pass a message body
- associated with the request. The message body will be sent in the same
- packet as the message headers if it is string, otherwise it is sent in a
- separate packet.
+ associated with the request.
+
+ If *encode_chunked* is ``True``, the result of each iteration of
+ *message_body* will be chunk-encoded as specified in :rfc:`7230`,
+ Section 3.3.1. How the data is encoded is dependent on the type of
+ *message_body*. If *message_body* implements the :ref:`buffer interface
+ <bufferobjects>` the encoding will result in a single chunk.
+ If *message_body* is a :class:`collections.Iterable`, each iteration
+ of *message_body* will result in a chunk. If *message_body* is a
+ :term:`file object`, each call to ``.read()`` will result in a chunk.
+ The method automatically signals the end of the chunk-encoded data
+ immediately after *message_body*.
+
+ .. note:: Due to the chunked encoding specification, empty chunks
+ yielded by an iterator body will be ignored by the chunk-encoder.
+ This is to avoid premature termination of the read of the request by
+ the target server due to malformed encoding.
+
+ .. versionadded:: 3.6
+ Chunked encoding support. The *encode_chunked* parameter was
+ added.
+
.. method:: HTTPConnection.send(data)
diff --git a/Doc/library/http.server.rst b/Doc/library/http.server.rst
index ae7fb97..fb5c1df 100644
--- a/Doc/library/http.server.rst
+++ b/Doc/library/http.server.rst
@@ -98,8 +98,8 @@ of which this module provides three different variants:
.. attribute:: rfile
- Contains an input stream, positioned at the start of the optional input
- data.
+ An :class:`io.BufferedIOBase` input stream, ready to read from
+ the start of the optional input data.
.. attribute:: wfile
@@ -107,6 +107,9 @@ of which this module provides three different variants:
client. Proper adherence to the HTTP protocol must be used when writing to
this stream.
+ .. versionchanged:: 3.6
+ This is an :class:`io.BufferedIOBase` stream.
+
:class:`BaseHTTPRequestHandler` has the following attributes:
.. attribute:: server_version
@@ -369,10 +372,9 @@ the current directory::
Handler = http.server.SimpleHTTPRequestHandler
- httpd = socketserver.TCPServer(("", PORT), Handler)
-
- print("serving at port", PORT)
- httpd.serve_forever()
+ with socketserver.TCPServer(("", PORT), Handler) as httpd:
+ print("serving at port", PORT)
+ httpd.serve_forever()
.. _http-server-cli:
diff --git a/Doc/library/imaplib.rst b/Doc/library/imaplib.rst
index 771ca43..1632eb7 100644
--- a/Doc/library/imaplib.rst
+++ b/Doc/library/imaplib.rst
@@ -103,6 +103,14 @@ There's also a subclass for secure connections:
:attr:`ssl.SSLContext.check_hostname` and *Server Name Indication* (see
:data:`ssl.HAS_SNI`).
+ .. deprecated:: 3.6
+
+ *keyfile* and *certfile* are deprecated in favor of *ssl_context*.
+ Please use :meth:`ssl.SSLContext.load_cert_chain` instead, or let
+ :func:`ssl.create_default_context` select the system's trusted CA
+ certificates for you.
+
+
The second subclass allows for connections created by a child process:
@@ -500,6 +508,17 @@ An :class:`IMAP4` instance has the following methods:
M.store(num, '+FLAGS', '\\Deleted')
M.expunge()
+ .. note::
+
+ Creating flags containing ']' (for example: "[test]") violates
+ :rfc:`3501` (the IMAP protocol). However, imaplib has historically
+ allowed creation of such tags, and popular IMAP servers, such as Gmail,
+ accept and produce such flags. There are non-Python programs which also
+ create such tags. Although it is an RFC violation and IMAP clients and
+ servers are supposed to be strict, imaplib nonetheless continues to allow
+ such tags to be created for backward compatibility reasons, and as of
+ python 3.6, handles them if they are sent from the server, since this
+ improves real-world compatibility.
.. method:: IMAP4.subscribe(mailbox)
diff --git a/Doc/library/imghdr.rst b/Doc/library/imghdr.rst
index f11f6dc..800e919 100644
--- a/Doc/library/imghdr.rst
+++ b/Doc/library/imghdr.rst
@@ -20,6 +20,9 @@ The :mod:`imghdr` module defines the following function:
string describing the image type. If optional *h* is provided, the *filename*
is ignored and *h* is assumed to contain the byte stream to test.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
The following image types are recognized, as listed below with the return value
from :func:`what`:
diff --git a/Doc/library/imp.rst b/Doc/library/imp.rst
index 9828ba6..ccf5f92 100644
--- a/Doc/library/imp.rst
+++ b/Doc/library/imp.rst
@@ -85,7 +85,9 @@ This module provides an interface to the mechanisms used to implement the
.. deprecated:: 3.3
Use :func:`importlib.util.find_spec` instead unless Python 3.3
compatibility is required, in which case use
- :func:`importlib.find_loader`.
+ :func:`importlib.find_loader`. For example usage of the former case,
+ see the :ref:`importlib-examples` section of the :mod:`importlib`
+ documentation.
.. function:: load_module(name, file, pathname, description)
@@ -112,9 +114,12 @@ This module provides an interface to the mechanisms used to implement the
If previously used in conjunction with :func:`imp.find_module` then
consider using :func:`importlib.import_module`, otherwise use the loader
returned by the replacement you chose for :func:`imp.find_module`. If you
- called :func:`imp.load_module` and related functions directly then use the
- classes in :mod:`importlib.machinery`, e.g.
- ``importlib.machinery.SourceFileLoader(name, path).load_module()``.
+ called :func:`imp.load_module` and related functions directly with file
+ path arguments then use a combination of
+ :func:`importlib.util.spec_from_file_location` and
+ :func:`importlib.util.module_from_spec`. See the :ref:`importlib-examples`
+ section of the :mod:`importlib` documentation for details of the various
+ approaches.
.. function:: new_module(name)
@@ -123,7 +128,7 @@ This module provides an interface to the mechanisms used to implement the
in ``sys.modules``.
.. deprecated:: 3.4
- Use :class:`types.ModuleType` instead.
+ Use :func:`importlib.util.module_from_spec` instead.
.. function:: reload(module)
diff --git a/Doc/library/importlib.rst b/Doc/library/importlib.rst
index 526c9f3..1fd5698 100644
--- a/Doc/library/importlib.rst
+++ b/Doc/library/importlib.rst
@@ -267,7 +267,7 @@ ABC hierarchy::
module and *path* will be the value of :attr:`__path__` from the
parent package. If a spec cannot be found, ``None`` is returned.
When passed in, ``target`` is a module object that the finder may
- use to make a more educated about what spec to return.
+ use to make a more educated guess about what spec to return.
.. versionadded:: 3.4
@@ -317,7 +317,7 @@ ABC hierarchy::
within the :term:`path entry` to which it is assigned. If a spec
cannot be found, ``None`` is returned. When passed in, ``target``
is a module object that the finder may use to make a more educated
- about what spec to return.
+ guess about what spec to return.
.. versionadded:: 3.4
@@ -379,10 +379,14 @@ ABC hierarchy::
An abstract method that executes the module in its own namespace
when a module is imported or reloaded. The module should already
- be initialized when exec_module() is called.
+ be initialized when ``exec_module()`` is called. When this method exists,
+ :meth:`~importlib.abc.Loader.create_module` must be defined.
.. versionadded:: 3.4
+ .. versionchanged:: 3.6
+ :meth:`~importlib.abc.Loader.create_module` must also be defined.
+
.. method:: load_module(fullname)
A legacy method for loading a module. If the module cannot be
@@ -802,6 +806,10 @@ find and load modules.
.. versionadded:: 3.3
+ .. deprecated:: 3.6
+ Use :mod:`site` configuration instead. Future versions of Python may
+ not enable this finder by default.
+
.. class:: PathFinder
@@ -936,6 +944,10 @@ find and load modules.
Concrete implementation of :meth:`importlib.abc.Loader.load_module` where
specifying the name of the module to load is optional.
+ .. deprecated:: 3.6
+
+ Use :meth:`importlib.abc.Loader.exec_module` instead.
+
.. class:: SourcelessFileLoader(fullname, path)
@@ -975,6 +987,10 @@ find and load modules.
Concrete implementation of :meth:`importlib.abc.Loader.load_module` where
specifying the name of the module to load is optional.
+ .. deprecated:: 3.6
+
+ Use :meth:`importlib.abc.Loader.exec_module` instead.
+
.. class:: ExtensionFileLoader(fullname, path)
@@ -1137,6 +1153,9 @@ an :term:`importer`.
The *optimization* parameter was added and the *debug_override* parameter
was deprecated.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: source_from_cache(path)
@@ -1150,6 +1169,9 @@ an :term:`importer`.
.. versionadded:: 3.4
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: decode_source(source_bytes)
Decode the given bytes representing source code and return it as a string
@@ -1192,12 +1214,13 @@ an :term:`importer`.
.. function:: module_from_spec(spec)
- Create a new module based on **spec** and ``spec.loader.create_module()``.
+ Create a new module based on **spec** and
+ :meth:`spec.loader.create_module <importlib.abc.Loader.create_module>`.
- If ``spec.loader.create_module()`` does not return ``None``, then any
- pre-existing attributes will not be reset. Also, no :exc:`AttributeError`
- will be raised if triggered while accessing **spec** or setting an attribute
- on the module.
+ If :meth:`spec.loader.create_module <importlib.abc.Loader.create_module>`
+ does not return ``None``, then any pre-existing attributes will not be reset.
+ Also, no :exc:`AttributeError` will be raised if triggered while accessing
+ **spec** or setting an attribute on the module.
This function is preferred over using :class:`types.ModuleType` to create a
new module as **spec** is used to set as many import-controlled attributes on
@@ -1259,7 +1282,8 @@ an :term:`importer`.
.. decorator:: set_package
- A :term:`decorator` for :meth:`importlib.abc.Loader.load_module` to set the :attr:`__package__` attribute on the returned module. If :attr:`__package__`
+ A :term:`decorator` for :meth:`importlib.abc.Loader.load_module` to set the
+ :attr:`__package__` attribute on the returned module. If :attr:`__package__`
is set and has a value other than ``None`` it will not be changed.
.. deprecated:: 3.4
@@ -1284,6 +1308,9 @@ an :term:`importer`.
.. versionadded:: 3.4
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. class:: LazyLoader(loader)
A class which postpones the execution of the loader of a module until the
@@ -1292,13 +1319,12 @@ an :term:`importer`.
This class **only** works with loaders that define
:meth:`~importlib.abc.Loader.exec_module` as control over what module type
is used for the module is required. For those same reasons, the loader's
- :meth:`~importlib.abc.Loader.create_module` method will be ignored (i.e., the
- loader's method should only return ``None``; this excludes
- :class:`BuiltinImporter` and :class:`ExtensionFileLoader`). Finally,
- modules which substitute the object placed into :attr:`sys.modules` will
- not work as there is no way to properly replace the module references
- throughout the interpreter safely; :exc:`ValueError` is raised if such a
- substitution is detected.
+ :meth:`~importlib.abc.Loader.create_module` method must return ``None`` or a
+ type for which its ``__class__`` attribute can be mutated along with not
+ using :term:`slots <__slots__>`. Finally, modules which substitute the object
+ placed into :attr:`sys.modules` will not work as there is no way to properly
+ replace the module references throughout the interpreter safely;
+ :exc:`ValueError` is raised if such a substitution is detected.
.. note::
For projects where startup time is critical, this class allows for
@@ -1309,6 +1335,11 @@ an :term:`importer`.
.. versionadded:: 3.5
+ .. versionchanged:: 3.6
+ Began calling :meth:`~importlib.abc.Loader.create_module`, removing the
+ compatibility warning for :class:`importlib.machinery.BuiltinImporter` and
+ :class:`importlib.machinery.ExtensionFileLoader`.
+
.. classmethod:: factory(loader)
A static method which returns a callable that creates a lazy loader. This
@@ -1320,3 +1351,139 @@ an :term:`importer`.
loader = importlib.machinery.SourceFileLoader
lazy_loader = importlib.util.LazyLoader.factory(loader)
finder = importlib.machinery.FileFinder(path, (lazy_loader, suffixes))
+
+.. _importlib-examples:
+
+Examples
+--------
+
+Importing programmatically
+''''''''''''''''''''''''''
+
+To programmatically import a module, use :func:`importlib.import_module`.
+::
+
+ import importlib
+
+ itertools = importlib.import_module('itertools')
+
+
+Checking if a module can be imported
+''''''''''''''''''''''''''''''''''''
+
+If you need to find out if a module can be imported without actually doing the
+import, then you should use :func:`importlib.util.find_spec`.
+::
+
+ import importlib.util
+ import sys
+
+ # For illustrative purposes.
+ name = 'itertools'
+
+ spec = importlib.util.find_spec(name)
+ if spec is None:
+ print("can't find the itertools module")
+ else:
+ # If you chose to perform the actual import ...
+ module = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(module)
+ # Adding the module to sys.modules is optional.
+ sys.modules[name] = module
+
+
+Importing a source file directly
+''''''''''''''''''''''''''''''''
+
+To import a Python source file directly, use the following recipe
+(Python 3.4 and newer only)::
+
+ import importlib.util
+ import sys
+
+ # For illustrative purposes.
+ import tokenize
+ file_path = tokenize.__file__
+ module_name = tokenize.__name__
+
+ spec = importlib.util.spec_from_file_location(module_name, file_path)
+ module = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(module)
+ # Optional; only necessary if you want to be able to import the module
+ # by name later.
+ sys.modules[module_name] = module
+
+
+Setting up an importer
+''''''''''''''''''''''
+
+For deep customizations of import, you typically want to implement an
+:term:`importer`. This means managing both the :term:`finder` and :term:`loader`
+side of things. For finders there are two flavours to choose from depending on
+your needs: a :term:`meta path finder` or a :term:`path entry finder`. The
+former is what you would put on :attr:`sys.meta_path` while the latter is what
+you create using a :term:`path entry hook` on :attr:`sys.path_hooks` which works
+with :attr:`sys.path` entries to potentially create a finder. This example will
+show you how to register your own importers so that import will use them (for
+creating an importer for yourself, read the documentation for the appropriate
+classes defined within this package)::
+
+ import importlib.machinery
+ import sys
+
+ # For illustrative purposes only.
+ SpamMetaPathFinder = importlib.machinery.PathFinder
+ SpamPathEntryFinder = importlib.machinery.FileFinder
+ loader_details = (importlib.machinery.SourceFileLoader,
+ importlib.machinery.SOURCE_SUFFIXES)
+
+ # Setting up a meta path finder.
+ # Make sure to put the finder in the proper location in the list in terms of
+ # priority.
+ sys.meta_path.append(SpamMetaPathFinder)
+
+ # Setting up a path entry finder.
+ # Make sure to put the path hook in the proper location in the list in terms
+ # of priority.
+ sys.path_hooks.append(SpamPathEntryFinder.path_hook(loader_details))
+
+
+Approximating :func:`importlib.import_module`
+'''''''''''''''''''''''''''''''''''''''''''''
+
+Import itself is implemented in Python code, making it possible to
+expose most of the import machinery through importlib. The following
+helps illustrate the various APIs that importlib exposes by providing an
+approximate implementation of
+:func:`importlib.import_module` (Python 3.4 and newer for the importlib usage,
+Python 3.6 and newer for other parts of the code).
+::
+
+ import importlib.util
+ import sys
+
+ def import_module(name, package=None):
+ """An approximate implementation of import."""
+ absolute_name = importlib.util.resolve_name(name, package)
+ try:
+ return sys.modules[absolute_name]
+ except KeyError:
+ pass
+
+ path = None
+ if '.' in absolute_name:
+ parent_name, _, child_name = absolute_name.rpartition('.')
+ parent_module = import_module(parent_name)
+ path = parent_module.spec.submodule_search_locations
+ for finder in sys.meta_path:
+ spec = finder.find_spec(absolute_name, path)
+ if spec is not None:
+ break
+ else:
+ raise ImportError(f'No module named {absolute_name!r}')
+ module = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(module)
+ sys.modules[absolute_name] = module
+ if path is not None:
+ setattr(parent_module, child_name, module)
+ return module
diff --git a/Doc/library/inspect.rst b/Doc/library/inspect.rst
index 62a3988..41a784d 100644
--- a/Doc/library/inspect.rst
+++ b/Doc/library/inspect.rst
@@ -235,24 +235,6 @@ attributes:
listed in the metaclass' custom :meth:`__dir__`.
-.. function:: getmoduleinfo(path)
-
- Returns a :term:`named tuple` ``ModuleInfo(name, suffix, mode, module_type)``
- of values that describe how Python will interpret the file identified by
- *path* if it is a module, or ``None`` if it would not be identified as a
- module. In that tuple, *name* is the name of the module without the name of
- any enclosing package, *suffix* is the trailing part of the file name (which
- may not be a dot-delimited extension), *mode* is the :func:`open` mode that
- would be used (``'r'`` or ``'rb'``), and *module_type* is an integer giving
- the type of the module. *module_type* will have a value which can be
- compared to the constants defined in the :mod:`imp` module; see the
- documentation for that module for more information on module types.
-
- .. deprecated:: 3.3
- You may check the file path's suffix against the supported suffixes
- listed in :mod:`importlib.machinery` to infer the same information.
-
-
.. function:: getmodulename(path)
Return the name of the module named by the file *path*, without including the
@@ -266,8 +248,7 @@ attributes:
still return ``None``.
.. versionchanged:: 3.3
- This function is now based directly on :mod:`importlib` rather than the
- deprecated :func:`getmoduleinfo`.
+ The function is based directly on :mod:`importlib`.
.. function:: ismodule(object)
@@ -337,6 +318,27 @@ attributes:
.. versionadded:: 3.5
+.. function:: isasyncgenfunction(object)
+
+ Return true if the object is an :term:`asynchronous generator` function,
+ for example::
+
+ >>> async def agen():
+ ... yield 1
+ ...
+ >>> inspect.isasyncgenfunction(agen)
+ True
+
+ .. versionadded:: 3.6
+
+
+.. function:: isasyncgen(object)
+
+ Return true if the object is an :term:`asynchronous generator iterator`
+ created by an :term:`asynchronous generator` function.
+
+ .. versionadded:: 3.6
+
.. function:: istraceback(object)
Return true if the object is a traceback.
@@ -646,6 +648,16 @@ function.
The name of the parameter as a string. The name must be a valid
Python identifier.
+ .. impl-detail::
+
+ CPython generates implicit parameter names of the form ``.0`` on the
+ code objects used to implement comprehensions and generator
+ expressions.
+
+ .. versionchanged:: 3.6
+ These parameter names are exposed by this module as names like
+ ``implicit0``.
+
.. attribute:: Parameter.default
The default value for the parameter. If the parameter has no default
@@ -824,47 +836,65 @@ Classes and functions
.. function:: getargspec(func)
- Get the names and default values of a Python function's arguments. A
+ Get the names and default values of a Python function's parameters. A
:term:`named tuple` ``ArgSpec(args, varargs, keywords, defaults)`` is
- returned. *args* is a list of the argument names. *varargs* and *keywords*
- are the names of the ``*`` and ``**`` arguments or ``None``. *defaults* is a
+ returned. *args* is a list of the parameter names. *varargs* and *keywords*
+ are the names of the ``*`` and ``**`` parameters or ``None``. *defaults* is a
tuple of default argument values or ``None`` if there are no default
arguments; if this tuple has *n* elements, they correspond to the last
*n* elements listed in *args*.
.. deprecated:: 3.0
- Use :func:`signature` and
+ Use :func:`getfullargspec` for an updated API that is usually a drop-in
+ replacement, but also correctly handles function annotations and
+ keyword-only parameters.
+
+ Alternatively, use :func:`signature` and
:ref:`Signature Object <inspect-signature-object>`, which provide a
- better introspecting API for callables.
+ more structured introspection API for callables.
.. function:: getfullargspec(func)
- Get the names and default values of a Python function's arguments. A
+ Get the names and default values of a Python function's parameters. A
:term:`named tuple` is returned:
``FullArgSpec(args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults,
annotations)``
- *args* is a list of the argument names. *varargs* and *varkw* are the names
- of the ``*`` and ``**`` arguments or ``None``. *defaults* is an *n*-tuple
- of the default values of the last *n* arguments, or ``None`` if there are no
- default arguments. *kwonlyargs* is a list of
- keyword-only argument names. *kwonlydefaults* is a dictionary mapping names
- from kwonlyargs to defaults. *annotations* is a dictionary mapping argument
- names to annotations.
-
- The first four items in the tuple correspond to :func:`getargspec`.
+ *args* is a list of the positional parameter names.
+ *varargs* is the name of the ``*`` parameter or ``None`` if arbitrary
+ positional arguments are not accepted.
+ *varkw* is the name of the ``**`` parameter or ``None`` if arbitrary
+ keyword arguments are not accepted.
+ *defaults* is an *n*-tuple of default argument values corresponding to the
+ last *n* positional parameters, or ``None`` if there are no such defaults
+ defined.
+ *kwonlyargs* is a list of keyword-only parameter names.
+ *kwonlydefaults* is a dictionary mapping parameter names from *kwonlyargs*
+ to the default values used if no argument is supplied.
+ *annotations* is a dictionary mapping parameter names to annotations.
+ The special key ``"return"`` is used to report the function return value
+ annotation (if any).
+
+ Note that :func:`signature` and
+ :ref:`Signature Object <inspect-signature-object>` provide the recommended
+ API for callable introspection, and support additional behaviours (like
+ positional-only arguments) that are sometimes encountered in extension module
+ APIs. This function is retained primarily for use in code that needs to
+ maintain compatibility with the Python 2 ``inspect`` module API.
.. versionchanged:: 3.4
This function is now based on :func:`signature`, but still ignores
``__wrapped__`` attributes and includes the already bound first
parameter in the signature output for bound methods.
- .. deprecated:: 3.5
- Use :func:`signature` and
- :ref:`Signature Object <inspect-signature-object>`, which provide a
- better introspecting API for callables.
+ .. versionchanged:: 3.6
+ This method was previously documented as deprecated in favour of
+ :func:`signature` in Python 3.5, but that decision has been reversed
+ in order to restore a clearly supported standard interface for
+ single-source Python 2/3 code migrating away from the legacy
+ :func:`getargspec` API.
.. function:: getargvalues(frame)
@@ -884,7 +914,7 @@ Classes and functions
.. function:: formatargspec(args[, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations[, formatarg, formatvarargs, formatvarkw, formatvalue, formatreturns, formatannotations]])
Format a pretty argument spec from the values returned by
- :func:`getargspec` or :func:`getfullargspec`.
+ :func:`getfullargspec`.
The first seven arguments are (``args``, ``varargs``, ``varkw``,
``defaults``, ``kwonlyargs``, ``kwonlydefaults``, ``annotations``).
@@ -1264,20 +1294,29 @@ the following flags:
.. data:: CO_COROUTINE
- The flag is set when the code object is a coroutine function, i.e.
- a coroutine object is returned when the code object is executed. See
- :pep:`492` for more details.
+ The flag is set when the code object is a coroutine function.
+ When the code object is executed it returns a coroutine object.
+ See :pep:`492` for more details.
.. versionadded:: 3.5
.. data:: CO_ITERABLE_COROUTINE
- Used to turn generators into generator-based coroutines. Generator
- objects with this flag can be used in ``await`` expression, and can
- ``yield from`` coroutine objects. See :pep:`492` for more details.
+ The flag is used to transform generators into generator-based
+ coroutines. Generator objects with this flag can be used in
+ ``await`` expression, and can ``yield from`` coroutine objects.
+ See :pep:`492` for more details.
.. versionadded:: 3.5
+.. data:: CO_ASYNC_GENERATOR
+
+ The flag is set when the code object is an asynchronous generator
+ function. When the code object is executed it returns an
+ asynchronous generator object. See :pep:`525` for more details.
+
+ .. versionadded:: 3.6
+
.. note::
The flags are specific to CPython, and may not be defined in other
Python implementations. Furthermore, the flags are an implementation
@@ -1286,7 +1325,6 @@ the following flags:
for any introspection needs.
-
.. _inspect-module-cli:
Command Line Interface
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst
index dfc1ddc..b0d0a8c 100644
--- a/Doc/library/itertools.rst
+++ b/Doc/library/itertools.rst
@@ -591,7 +591,13 @@ loops that truncate the stream.
.. function:: tee(iterable, n=2)
- Return *n* independent iterators from a single iterable. Roughly equivalent to::
+ Return *n* independent iterators from a single iterable.
+
+ The following Python code helps explain what *tee* does (although the actual
+ implementation is more complex and uses only a single underlying
+ :abbr:`FIFO (first-in, first-out)` queue).
+
+ Roughly equivalent to::
def tee(iterable, n=2):
it = iter(iterable)
diff --git a/Doc/library/json.rst b/Doc/library/json.rst
index f487535..ed238e2 100644
--- a/Doc/library/json.rst
+++ b/Doc/library/json.rst
@@ -126,7 +126,7 @@ See :ref:`json-commandline` for detailed documentation.
Basic Usage
-----------
-.. function:: dump(obj, fp, skipkeys=False, ensure_ascii=True, \
+.. function:: dump(obj, fp, *, skipkeys=False, ensure_ascii=True, \
check_circular=True, allow_nan=True, cls=None, \
indent=None, separators=None, default=None, \
sort_keys=False, **kw)
@@ -187,8 +187,11 @@ Basic Usage
:meth:`default` method to serialize additional types), specify it with the
*cls* kwarg; otherwise :class:`JSONEncoder` is used.
+ .. versionchanged:: 3.6
+ All optional parameters are now :ref:`keyword-only <keyword-only_parameter>`.
-.. function:: dumps(obj, skipkeys=False, ensure_ascii=True, \
+
+.. function:: dumps(obj, *, skipkeys=False, ensure_ascii=True, \
check_circular=True, allow_nan=True, cls=None, \
indent=None, separators=None, default=None, \
sort_keys=False, **kw)
@@ -212,7 +215,7 @@ Basic Usage
the original one. That is, ``loads(dumps(x)) != x`` if x has non-string
keys.
-.. function:: load(fp, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
+.. function:: load(fp, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
Deserialize *fp* (a ``.read()``-supporting :term:`file-like object`
containing a JSON document) to a Python object using this :ref:`conversion
@@ -260,10 +263,14 @@ Basic Usage
If the data being deserialized is not a valid JSON document, a
:exc:`JSONDecodeError` will be raised.
-.. function:: loads(s, encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
+ .. versionchanged:: 3.6
+ All optional parameters are now :ref:`keyword-only <keyword-only_parameter>`.
+
+.. function:: loads(s, *, encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
- Deserialize *s* (a :class:`str` instance containing a JSON document) to a
- Python object using this :ref:`conversion table <json-to-py-table>`.
+ Deserialize *s* (a :class:`str`, :class:`bytes` or :class:`bytearray`
+ instance containing a JSON document) to a Python object using this
+ :ref:`conversion table <json-to-py-table>`.
The other arguments have the same meaning as in :func:`load`, except
*encoding* which is ignored and deprecated.
@@ -271,10 +278,15 @@ Basic Usage
If the data being deserialized is not a valid JSON document, a
:exc:`JSONDecodeError` will be raised.
+ .. versionchanged:: 3.6
+ *s* can now be of type :class:`bytes` or :class:`bytearray`. The
+ input encoding should be UTF-8, UTF-16 or UTF-32.
+
+
Encoders and Decoders
---------------------
-.. class:: JSONDecoder(object_hook=None, parse_float=None, parse_int=None, parse_constant=None, strict=True, object_pairs_hook=None)
+.. class:: JSONDecoder(*, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, strict=True, object_pairs_hook=None)
Simple JSON decoder.
@@ -344,6 +356,9 @@ Encoders and Decoders
If the data being deserialized is not a valid JSON document, a
:exc:`JSONDecodeError` will be raised.
+ .. versionchanged:: 3.6
+ All parameters are now :ref:`keyword-only <keyword-only_parameter>`.
+
.. method:: decode(s)
Return the Python representation of *s* (a :class:`str` instance
@@ -362,7 +377,7 @@ Encoders and Decoders
extraneous data at the end.
-.. class:: JSONEncoder(skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)
+.. class:: JSONEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)
Extensible JSON encoder for Python data structures.
@@ -443,6 +458,9 @@ Encoders and Decoders
the object or raise a :exc:`TypeError`. If not specified, :exc:`TypeError`
is raised.
+ .. versionchanged:: 3.6
+ All parameters are now :ref:`keyword-only <keyword-only_parameter>`.
+
.. method:: default(o)
diff --git a/Doc/library/logging.handlers.rst b/Doc/library/logging.handlers.rst
index b16eef0..f13f765 100644
--- a/Doc/library/logging.handlers.rst
+++ b/Doc/library/logging.handlers.rst
@@ -84,6 +84,9 @@ sends logging output to a disk file. It inherits the output functionality from
with that encoding. If *delay* is true, then file opening is deferred until the
first call to :meth:`emit`. By default, the file grows indefinitely.
+ .. versionchanged:: 3.6
+ As well as string values, :class:`~pathlib.Path` objects are also accepted
+ for the *filename* argument.
.. method:: close()
@@ -160,12 +163,23 @@ for this value.
with that encoding. If *delay* is true, then file opening is deferred until the
first call to :meth:`emit`. By default, the file grows indefinitely.
+ .. versionchanged:: 3.6
+ As well as string values, :class:`~pathlib.Path` objects are also accepted
+ for the *filename* argument.
+
+ .. method:: reopenIfNeeded()
+
+ Checks to see if the file has changed. If it has, the existing stream is
+ flushed and closed and the file opened again, typically as a precursor to
+ outputting the record to the file.
+
+ .. versionadded:: 3.6
+
.. method:: emit(record)
- Outputs the record to the file, but first checks to see if the file has
- changed. If it has, the existing stream is flushed and closed and the
- file opened again, before outputting the record to the file.
+ Outputs the record to the file, but first calls :meth:`reopenIfNeeded` to
+ reopen the file if it has changed.
.. _base-rotating-handler:
@@ -268,17 +282,21 @@ module, supports rotation of disk log files.
You can use the *maxBytes* and *backupCount* values to allow the file to
:dfn:`rollover` at a predetermined size. When the size is about to be exceeded,
the file is closed and a new file is silently opened for output. Rollover occurs
- whenever the current log file is nearly *maxBytes* in length; if either of
- *maxBytes* or *backupCount* is zero, rollover never occurs. If *backupCount*
- is non-zero, the system will save old log files by appending the extensions
- '.1', '.2' etc., to the filename. For example, with a *backupCount* of 5 and
- a base file name of :file:`app.log`, you would get :file:`app.log`,
+ whenever the current log file is nearly *maxBytes* in length; but if either of
+ *maxBytes* or *backupCount* is zero, rollover never occurs, so you generally want
+ to set *backupCount* to at least 1, and have a non-zero *maxBytes*.
+ When *backupCount* is non-zero, the system will save old log files by appending
+ the extensions '.1', '.2' etc., to the filename. For example, with a *backupCount*
+ of 5 and a base file name of :file:`app.log`, you would get :file:`app.log`,
:file:`app.log.1`, :file:`app.log.2`, up to :file:`app.log.5`. The file being
written to is always :file:`app.log`. When this file is filled, it is closed
and renamed to :file:`app.log.1`, and if files :file:`app.log.1`,
- :file:`app.log.2`, etc. exist, then they are renamed to :file:`app.log.2`,
- :file:`app.log.3` etc. respectively.
+ :file:`app.log.2`, etc. exist, then they are renamed to :file:`app.log.2`,
+ :file:`app.log.3` etc. respectively.
+ .. versionchanged:: 3.6
+ As well as string values, :class:`~pathlib.Path` objects are also accepted
+ for the *filename* argument.
.. method:: doRollover()
@@ -310,21 +328,24 @@ timed intervals.
You can use the *when* to specify the type of *interval*. The list of possible
values is below. Note that they are not case sensitive.
- +----------------+-----------------------+
- | Value | Type of interval |
- +================+=======================+
- | ``'S'`` | Seconds |
- +----------------+-----------------------+
- | ``'M'`` | Minutes |
- +----------------+-----------------------+
- | ``'H'`` | Hours |
- +----------------+-----------------------+
- | ``'D'`` | Days |
- +----------------+-----------------------+
- | ``'W0'-'W6'`` | Weekday (0=Monday) |
- +----------------+-----------------------+
- | ``'midnight'`` | Roll over at midnight |
- +----------------+-----------------------+
+ +----------------+----------------------------+-------------------------+
+ | Value | Type of interval | If/how *atTime* is used |
+ +================+============================+=========================+
+ | ``'S'`` | Seconds | Ignored |
+ +----------------+----------------------------+-------------------------+
+ | ``'M'`` | Minutes | Ignored |
+ +----------------+----------------------------+-------------------------+
+ | ``'H'`` | Hours | Ignored |
+ +----------------+----------------------------+-------------------------+
+ | ``'D'`` | Days | Ignored |
+ +----------------+----------------------------+-------------------------+
+ | ``'W0'-'W6'`` | Weekday (0=Monday) | Used to compute initial |
+ | | | rollover time |
+ +----------------+----------------------------+-------------------------+
+ | ``'midnight'`` | Roll over at midnight, if | Used to compute initial |
+ | | *atTime* not specified, | rollover time |
+ | | else at time *atTime* | |
+ +----------------+----------------------------+-------------------------+
When using weekday-based rotation, specify 'W0' for Monday, 'W1' for
Tuesday, and so on up to 'W6' for Sunday. In this case, the value passed for
@@ -352,16 +373,35 @@ timed intervals.
If *atTime* is not ``None``, it must be a ``datetime.time`` instance which
specifies the time of day when rollover occurs, for the cases where rollover
- is set to happen "at midnight" or "on a particular weekday".
+ is set to happen "at midnight" or "on a particular weekday". Note that in
+ these cases, the *atTime* value is effectively used to compute the *initial*
+ rollover, and subsequent rollovers would be calculated via the normal
+ interval calculation.
+
+ .. note:: Calculation of the initial rollover time is done when the handler
+ is initialised. Calculation of subsequent rollover times is done only
+ when rollover occurs, and rollover occurs only when emitting output. If
+ this is not kept in mind, it might lead to some confusion. For example,
+ if an interval of "every minute" is set, that does not mean you will
+ always see log files with times (in the filename) separated by a minute;
+ if, during application execution, logging output is generated more
+ frequently than once a minute, *then* you can expect to see log files
+ with times separated by a minute. If, on the other hand, logging messages
+ are only output once every five minutes (say), then there will be gaps in
+ the file times corresponding to the minutes where no output (and hence no
+ rollover) occurred.
.. versionchanged:: 3.4
*atTime* parameter was added.
+ .. versionchanged:: 3.6
+ As well as string values, :class:`~pathlib.Path` objects are also accepted
+ for the *filename* argument.
+
.. method:: doRollover()
Does a rollover, as described above.
-
.. method:: emit(record)
Outputs the record to the file, catering for rollover as described above.
@@ -798,12 +838,18 @@ should, then :meth:`flush` is expected to do the flushing.
overridden to implement custom flushing strategies.
-.. class:: MemoryHandler(capacity, flushLevel=ERROR, target=None)
+.. class:: MemoryHandler(capacity, flushLevel=ERROR, target=None, flushOnClose=True)
Returns a new instance of the :class:`MemoryHandler` class. The instance is
initialized with a buffer size of *capacity*. If *flushLevel* is not specified,
:const:`ERROR` is used. If no *target* is specified, the target will need to be
- set using :meth:`setTarget` before this handler does anything useful.
+ set using :meth:`setTarget` before this handler does anything useful. If
+ *flushOnClose* is specified as ``False``, then the buffer is *not* flushed when
+ the handler is closed. If not specified or specified as ``True``, the previous
+ behaviour of flushing the buffer will occur when the handler is closed.
+
+ .. versionchanged:: 3.6
+ The *flushOnClose* parameter was added.
.. method:: close()
diff --git a/Doc/library/lzma.rst b/Doc/library/lzma.rst
index 61b3ba3..5edb23d 100644
--- a/Doc/library/lzma.rst
+++ b/Doc/library/lzma.rst
@@ -39,8 +39,9 @@ Reading and writing compressed files
object`.
The *filename* argument can be either an actual file name (given as a
- :class:`str` or :class:`bytes` object), in which case the named file is
- opened, or it can be an existing file object to read from or write to.
+ :class:`str`, :class:`bytes` or :term:`path-like object` object), in
+ which case the named file is opened, or it can be an existing file object
+ to read from or write to.
The *mode* argument can be any of ``"r"``, ``"rb"``, ``"w"``, ``"wb"``,
``"x"``, ``"xb"``, ``"a"`` or ``"ab"`` for binary mode, or ``"rt"``,
@@ -64,6 +65,9 @@ Reading and writing compressed files
.. versionchanged:: 3.4
Added support for the ``"x"``, ``"xb"`` and ``"xt"`` modes.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. class:: LZMAFile(filename=None, mode="r", \*, format=None, check=-1, preset=None, filters=None)
@@ -71,9 +75,10 @@ Reading and writing compressed files
An :class:`LZMAFile` can wrap an already-open :term:`file object`, or operate
directly on a named file. The *filename* argument specifies either the file
- object to wrap, or the name of the file to open (as a :class:`str` or
- :class:`bytes` object). When wrapping an existing file object, the wrapped
- file will not be closed when the :class:`LZMAFile` is closed.
+ object to wrap, or the name of the file to open (as a :class:`str`,
+ :class:`bytes` or :term:`path-like object` object). When wrapping an
+ existing file object, the wrapped file will not be closed when the
+ :class:`LZMAFile` is closed.
The *mode* argument can be either ``"r"`` for reading (default), ``"w"`` for
overwriting, ``"x"`` for exclusive creation, or ``"a"`` for appending. These
@@ -118,6 +123,9 @@ Reading and writing compressed files
The :meth:`~io.BufferedIOBase.read` method now accepts an argument of
``None``.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
Compressing and decompressing data in memory
--------------------------------------------
diff --git a/Doc/library/math.rst b/Doc/library/math.rst
index 3fdea18..da2b8cc 100644
--- a/Doc/library/math.rst
+++ b/Doc/library/math.rst
@@ -426,6 +426,15 @@ Constants
The mathematical constant e = 2.718281..., to available precision.
+.. data:: tau
+
+ The mathematical constant τ = 6.283185..., to available precision.
+ Tau is a circle constant equal to 2π, the ratio of a circle's circumference to
+ its radius. To learn more about Tau, check out Vi Hart's video `Pi is (still)
+ Wrong <https://www.youtube.com/watch?v=jG7vhMMXagQ>`_, and start celebrating
+ `Tau day <http://tauday.com/>`_ by eating twice as much pie!
+
+ .. versionadded:: 3.6
.. data:: inf
diff --git a/Doc/library/mmap.rst b/Doc/library/mmap.rst
index 2f2945f..f46bf66 100644
--- a/Doc/library/mmap.rst
+++ b/Doc/library/mmap.rst
@@ -264,13 +264,18 @@ To map anonymous memory, -1 should be passed as the fileno along with the length
.. method:: write(bytes)
Write the bytes in *bytes* into memory at the current position of the
- file pointer; the file position is updated to point after the bytes that
- were written. If the mmap was created with :const:`ACCESS_READ`, then
+ file pointer and return the number of bytes written (never less than
+ ``len(bytes)``, since if the write fails, a :exc:`ValueError` will be
+ raised). The file position is updated to point after the bytes that
+ were written. If the mmap was created with :const:`ACCESS_READ`, then
writing to it will raise a :exc:`TypeError` exception.
.. versionchanged:: 3.5
Writable :term:`bytes-like object` is now accepted.
+ .. versionchanged:: 3.6
+ The number of bytes written is now returned.
+
.. method:: write_byte(byte)
diff --git a/Doc/library/multiprocessing.rst b/Doc/library/multiprocessing.rst
index d45bc20..96d1424 100644
--- a/Doc/library/multiprocessing.rst
+++ b/Doc/library/multiprocessing.rst
@@ -650,8 +650,9 @@ primitives like locks.
For passing messages one can use :func:`Pipe` (for a connection between two
processes) or a queue (which allows multiple producers and consumers).
-The :class:`Queue`, :class:`SimpleQueue` and :class:`JoinableQueue` types are multi-producer,
-multi-consumer FIFO queues modelled on the :class:`queue.Queue` class in the
+The :class:`Queue`, :class:`SimpleQueue` and :class:`JoinableQueue` types
+are multi-producer, multi-consumer :abbr:`FIFO (first-in, first-out)`
+queues modelled on the :class:`queue.Queue` class in the
standard library. They differ in that :class:`Queue` lacks the
:meth:`~queue.Queue.task_done` and :meth:`~queue.Queue.join` methods introduced
into Python 2.5's :class:`queue.Queue` class.
@@ -889,8 +890,13 @@ Miscellaneous
.. function:: cpu_count()
- Return the number of CPUs in the system. May raise
- :exc:`NotImplementedError`.
+ Return the number of CPUs in the system.
+
+ This number is not equivalent to the number of CPUs the current process can
+ use. The number of usable CPUs can be obtained with
+ ``len(os.sched_getaffinity(0))``
+
+ May raise :exc:`NotImplementedError`.
.. seealso::
:func:`os.cpu_count`
@@ -1679,7 +1685,9 @@ their parent process exits. The manager classes are defined in the
of processes. Objects of this type are returned by
:func:`multiprocessing.Manager`.
- It also supports creation of shared lists and dictionaries.
+ Its methods create and return :ref:`multiprocessing-proxy_objects` for a
+ number of commonly used data types to be synchronized across processes.
+ This notably includes shared lists and dictionaries.
.. method:: Barrier(parties[, action[, timeout]])
@@ -1742,31 +1750,17 @@ their parent process exits. The manager classes are defined in the
dict(mapping)
dict(sequence)
- Create a shared ``dict`` object and return a proxy for it.
+ Create a shared :class:`dict` object and return a proxy for it.
.. method:: list()
list(sequence)
- Create a shared ``list`` object and return a proxy for it.
-
- .. note::
-
- Modifications to mutable values or items in dict and list proxies will not
- be propagated through the manager, because the proxy has no way of knowing
- when its values or items are modified. To modify such an item, you can
- re-assign the modified object to the container proxy::
-
- # create a list proxy and append a mutable object (a dictionary)
- lproxy = manager.list()
- lproxy.append({})
- # now mutate the dictionary
- d = lproxy[0]
- d['a'] = 1
- d['b'] = 2
- # at this point, the changes to d are not yet synced, but by
- # reassigning the dictionary, the proxy is notified of the change
- lproxy[0] = d
+ Create a shared :class:`list` object and return a proxy for it.
+ .. versionchanged:: 3.6
+ Shared objects are capable of being nested. For example, a shared
+ container object such as a shared list can contain other shared objects
+ which will all be managed and synchronized by the :class:`SyncManager`.
.. class:: Namespace
@@ -1878,6 +1872,8 @@ client to access it remotely::
>>> s = m.get_server()
>>> s.serve_forever()
+.. _multiprocessing-proxy_objects:
+
Proxy Objects
~~~~~~~~~~~~~
@@ -1887,8 +1883,7 @@ proxy. Multiple proxy objects may have the same referent.
A proxy object has methods which invoke corresponding methods of its referent
(although not every method of the referent will necessarily be available through
-the proxy). A proxy can usually be used in most of the same ways that its
-referent can:
+the proxy). In this way, a proxy can be used just like its referent can:
.. doctest::
@@ -1909,9 +1904,9 @@ the referent, whereas applying :func:`repr` will return the representation of
the proxy.
An important feature of proxy objects is that they are picklable so they can be
-passed between processes. Note, however, that if a proxy is sent to the
-corresponding manager's process then unpickling it will produce the referent
-itself. This means, for example, that one shared object can contain a second:
+passed between processes. As such, a referent can contain
+:ref:`multiprocessing-proxy_objects`. This permits nesting of these managed
+lists, dicts, and other :ref:`multiprocessing-proxy_objects`:
.. doctest::
@@ -1919,10 +1914,46 @@ itself. This means, for example, that one shared object can contain a second:
>>> b = manager.list()
>>> a.append(b) # referent of a now contains referent of b
>>> print(a, b)
- [[]] []
+ [<ListProxy object, typeid 'list' at ...>] []
>>> b.append('hello')
- >>> print(a, b)
- [['hello']] ['hello']
+ >>> print(a[0], b)
+ ['hello'] ['hello']
+
+Similarly, dict and list proxies may be nested inside one another::
+
+ >>> l_outer = manager.list([ manager.dict() for i in range(2) ])
+ >>> d_first_inner = l_outer[0]
+ >>> d_first_inner['a'] = 1
+ >>> d_first_inner['b'] = 2
+ >>> l_outer[1]['c'] = 3
+ >>> l_outer[1]['z'] = 26
+ >>> print(l_outer[0])
+ {'a': 1, 'b': 2}
+ >>> print(l_outer[1])
+ {'c': 3, 'z': 26}
+
+If standard (non-proxy) :class:`list` or :class:`dict` objects are contained
+in a referent, modifications to those mutable values will not be propagated
+through the manager because the proxy has no way of knowing when the values
+contained within are modified. However, storing a value in a container proxy
+(which triggers a ``__setitem__`` on the proxy object) does propagate through
+the manager and so to effectively modify such an item, one could re-assign the
+modified value to the container proxy::
+
+ # create a list proxy and append a mutable object (a dictionary)
+ lproxy = manager.list()
+ lproxy.append({})
+ # now mutate the dictionary
+ d = lproxy[0]
+ d['a'] = 1
+ d['b'] = 2
+ # at this point, the changes to d are not yet synced, but by
+ # updating the dictionary, the proxy is notified of the change
+ lproxy[0] = d
+
+This approach is perhaps less convenient than employing nested
+:ref:`multiprocessing-proxy_objects` for most use cases but also
+demonstrates a level of control over the synchronization.
.. note::
diff --git a/Doc/library/os.path.rst b/Doc/library/os.path.rst
index bf0dfac..406054e 100644
--- a/Doc/library/os.path.rst
+++ b/Doc/library/os.path.rst
@@ -61,6 +61,9 @@ the :mod:`glob` module.)
platforms, this is equivalent to calling the function :func:`normpath` as
follows: ``normpath(join(os.getcwd(), path))``.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: basename(path)
@@ -71,6 +74,9 @@ the :mod:`glob` module.)
``'/foo/bar/'`` returns ``'bar'``, the :func:`basename` function returns an
empty string (``''``).
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: commonpath(paths)
@@ -83,6 +89,9 @@ the :mod:`glob` module.)
.. versionadded:: 3.5
+ .. versionchanged:: 3.6
+ Accepts a sequence of :term:`path-like objects <path-like object>`.
+
.. function:: commonprefix(list)
@@ -104,12 +113,18 @@ the :mod:`glob` module.)
>>> os.path.commonpath(['/usr/lib', '/usr/local/lib'])
'/usr'
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: dirname(path)
Return the directory name of pathname *path*. This is the first element of
the pair returned by passing *path* to the function :func:`split`.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: exists(path)
@@ -123,6 +138,9 @@ the :mod:`glob` module.)
*path* can now be an integer: ``True`` is returned if it is an
open file descriptor, ``False`` otherwise.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: lexists(path)
@@ -130,6 +148,9 @@ the :mod:`glob` module.)
broken symbolic links. Equivalent to :func:`exists` on platforms lacking
:func:`os.lstat`.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: expanduser(path)
@@ -151,6 +172,9 @@ the :mod:`glob` module.)
If the expansion fails or if the path does not begin with a tilde, the path is
returned unchanged.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: expandvars(path)
@@ -162,6 +186,9 @@ the :mod:`glob` module.)
On Windows, ``%name%`` expansions are supported in addition to ``$name`` and
``${name}``.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: getatime(path)
@@ -182,6 +209,9 @@ the :mod:`glob` module.)
If :func:`os.stat_float_times` returns ``True``, the result is a floating point
number.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: getctime(path)
@@ -191,12 +221,18 @@ the :mod:`glob` module.)
the :mod:`time` module). Raise :exc:`OSError` if the file does not exist or
is inaccessible.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: getsize(path)
Return the size, in bytes, of *path*. Raise :exc:`OSError` if the file does
not exist or is inaccessible.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: isabs(path)
@@ -204,24 +240,36 @@ the :mod:`glob` module.)
begins with a slash, on Windows that it begins with a (back)slash after chopping
off a potential drive letter.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: isfile(path)
Return ``True`` if *path* is an existing regular file. This follows symbolic
links, so both :func:`islink` and :func:`isfile` can be true for the same path.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: isdir(path)
Return ``True`` if *path* is an existing directory. This follows symbolic
links, so both :func:`islink` and :func:`isdir` can be true for the same path.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: islink(path)
Return ``True`` if *path* refers to a directory entry that is a symbolic link.
Always ``False`` if symbolic links are not supported by the Python runtime.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: ismount(path)
@@ -237,6 +285,9 @@ the :mod:`glob` module.)
.. versionadded:: 3.4
Support for detecting non-root mount points on Windows.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: join(path, *paths)
@@ -255,13 +306,20 @@ the :mod:`glob` module.)
``os.path.join("c:", "foo")`` represents a path relative to the current
directory on drive :file:`C:` (:file:`c:foo`), not :file:`c:\\foo`.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object` for *path* and *paths*.
+
.. function:: normcase(path)
Normalize the case of a pathname. On Unix and Mac OS X, this returns the
path unchanged; on case-insensitive filesystems, it converts the path to
lowercase. On Windows, it also converts forward slashes to backward slashes.
- Raise a TypeError if the type of *path* is not ``str`` or ``bytes``.
+ Raise a TypeError if the type of *path* is not ``str`` or ``bytes`` (directly
+ or indirectly through the :class:`os.PathLike` interface).
+
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
.. function:: normpath(path)
@@ -272,12 +330,18 @@ the :mod:`glob` module.)
that contains symbolic links. On Windows, it converts forward slashes to
backward slashes. To normalize case, use :func:`normcase`.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: realpath(path)
Return the canonical path of the specified filename, eliminating any symbolic
links encountered in the path (if they are supported by the operating system).
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: relpath(path, start=os.curdir)
@@ -290,6 +354,9 @@ the :mod:`glob` module.)
Availability: Unix, Windows.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: samefile(path1, path2)
@@ -305,6 +372,9 @@ the :mod:`glob` module.)
.. versionchanged:: 3.4
Windows now uses the same implementation as all other platforms.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: sameopenfile(fp1, fp2)
@@ -315,6 +385,9 @@ the :mod:`glob` module.)
.. versionchanged:: 3.2
Added Windows support.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: samestat(stat1, stat2)
@@ -328,6 +401,9 @@ the :mod:`glob` module.)
.. versionchanged:: 3.4
Added Windows support.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: split(path)
@@ -341,6 +417,9 @@ the :mod:`glob` module.)
(but the strings may differ). Also see the functions :func:`dirname` and
:func:`basename`.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: splitdrive(path)
@@ -359,6 +438,9 @@ the :mod:`glob` module.)
and share, up to but not including the fourth separator.
e.g. ``splitdrive("//host/computer/dir")`` returns ``("//host/computer", "/dir")``
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: splitext(path)
@@ -367,6 +449,9 @@ the :mod:`glob` module.)
period. Leading periods on the basename are ignored; ``splitext('.cshrc')``
returns ``('.cshrc', '')``.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: splitunc(path)
diff --git a/Doc/library/os.rst b/Doc/library/os.rst
index 5a67f93..988cb7c 100644
--- a/Doc/library/os.rst
+++ b/Doc/library/os.rst
@@ -57,7 +57,7 @@ Notes on the availability of these functions:
The name of the operating system dependent module imported. The following
names have currently been registered: ``'posix'``, ``'nt'``,
- ``'ce'``, ``'java'``.
+ ``'java'``.
.. seealso::
:attr:`sys.platform` has a finer granularity. :func:`os.uname` gives
@@ -171,23 +171,60 @@ process and user.
.. function:: fsencode(filename)
- Encode *filename* to the filesystem encoding with ``'surrogateescape'``
- error handler, or ``'strict'`` on Windows; return :class:`bytes` unchanged.
+ Encode :term:`path-like <path-like object>` *filename* to the filesystem
+ encoding with ``'surrogateescape'`` error handler, or ``'strict'`` on
+ Windows; return :class:`bytes` unchanged.
:func:`fsdecode` is the reverse function.
.. versionadded:: 3.2
+ .. versionchanged:: 3.6
+ Support added to accept objects implementing the :class:`os.PathLike`
+ interface.
+
.. function:: fsdecode(filename)
- Decode *filename* from the filesystem encoding with ``'surrogateescape'``
- error handler, or ``'strict'`` on Windows; return :class:`str` unchanged.
+ Decode the :term:`path-like <path-like object>` *filename* from the
+ filesystem encoding with ``'surrogateescape'`` error handler, or ``'strict'``
+ on Windows; return :class:`str` unchanged.
:func:`fsencode` is the reverse function.
.. versionadded:: 3.2
+ .. versionchanged:: 3.6
+ Support added to accept objects implementing the :class:`os.PathLike`
+ interface.
+
+
+.. function:: fspath(path)
+
+ Return the file system representation of the path.
+
+ If :class:`str` or :class:`bytes` is passed in, it is returned unchanged.
+ Otherwise :meth:`~os.PathLike.__fspath__` is called and its value is
+ returned as long as it is a :class:`str` or :class:`bytes` object.
+ In all other cases, :exc:`TypeError` is raised.
+
+ .. versionadded:: 3.6
+
+
+.. class:: PathLike
+
+ An :term:`abstract base class` for objects representing a file system path,
+ e.g. :class:`pathlib.PurePath`.
+
+ .. versionadded:: 3.6
+
+ .. abstractmethod:: __fspath__()
+
+ Return the file system path representation of the object.
+
+ The method should only return a :class:`str` or :class:`bytes` object,
+ with the preference being for :class:`str`.
+
.. function:: getenv(key, default=None)
@@ -896,6 +933,9 @@ as internal buffering of data.
exception, the function now retries the system call instead of raising an
:exc:`InterruptedError` exception (see :pep:`475` for the rationale).
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
The following constants are options for the *flags* parameter to the
:func:`~os.open` function. They can be combined using the bitwise OR operator
``|``. Some of them are not available on all platforms. For descriptions of
@@ -1392,6 +1432,9 @@ features:
.. versionchanged:: 3.3
Added the *dir_fd*, *effective_ids*, and *follow_symlinks* parameters.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. data:: F_OK
R_OK
@@ -1416,6 +1459,9 @@ features:
Added support for specifying *path* as a file descriptor
on some platforms.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: chflags(path, flags, *, follow_symlinks=True)
@@ -1442,6 +1488,9 @@ features:
.. versionadded:: 3.3
The *follow_symlinks* argument.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: chmod(path, mode, *, dir_fd=None, follow_symlinks=True)
@@ -1483,6 +1532,9 @@ features:
Added support for specifying *path* as an open file descriptor,
and the *dir_fd* and *follow_symlinks* arguments.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: chown(path, uid, gid, *, dir_fd=None, follow_symlinks=True)
@@ -1502,6 +1554,9 @@ features:
Added support for specifying an open file descriptor for *path*,
and the *dir_fd* and *follow_symlinks* arguments.
+ .. versionchanged:: 3.6
+ Supports a :term:`path-like object`.
+
.. function:: chroot(path)
@@ -1509,6 +1564,9 @@ features:
Availability: Unix.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: fchdir(fd)
@@ -1537,6 +1595,9 @@ features:
Availability: Unix.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: lchmod(path, mode)
@@ -1547,6 +1608,8 @@ features:
Availability: Unix.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
.. function:: lchown(path, uid, gid)
@@ -1556,6 +1619,9 @@ features:
Availability: Unix.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: link(src, dst, *, src_dir_fd=None, dst_dir_fd=None, follow_symlinks=True)
@@ -1573,6 +1639,9 @@ features:
.. versionadded:: 3.3
Added the *src_dir_fd*, *dst_dir_fd*, and *follow_symlinks* arguments.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object` for *src* and *dst*.
+
.. function:: listdir(path='.')
@@ -1580,8 +1649,9 @@ features:
*path*. The list is in arbitrary order, and does not include the special
entries ``'.'`` and ``'..'`` even if they are present in the directory.
- *path* may be either of type ``str`` or of type ``bytes``. If *path*
- is of type ``bytes``, the filenames returned will also be of type ``bytes``;
+ *path* may be a :term:`path-like object`. If *path* is of type ``bytes``
+ (directly or indirectly through the :class:`PathLike` interface),
+ the filenames returned will also be of type ``bytes``;
in all other circumstances, they will be of type ``str``.
This function can also support :ref:`specifying a file descriptor
@@ -1602,6 +1672,9 @@ features:
.. versionadded:: 3.3
Added support for specifying an open file descriptor for *path*.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: lstat(path, \*, dir_fd=None)
@@ -1628,6 +1701,9 @@ features:
.. versionchanged:: 3.3
Added the *dir_fd* parameter.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object` for *src* and *dst*.
+
.. function:: mkdir(path, mode=0o777, *, dir_fd=None)
@@ -1652,6 +1728,9 @@ features:
.. versionadded:: 3.3
The *dir_fd* argument.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: makedirs(name, mode=0o777, exist_ok=False)
@@ -1685,6 +1764,9 @@ features:
mode of the existing directory. Since this behavior was impossible to
implement safely, it was removed in Python 3.4.1. See :issue:`21082`.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: mkfifo(path, mode=0o666, *, dir_fd=None)
@@ -1705,6 +1787,9 @@ features:
.. versionadded:: 3.3
The *dir_fd* argument.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: mknod(path, mode=0o600, device=0, *, dir_fd=None)
@@ -1724,6 +1809,9 @@ features:
.. versionadded:: 3.3
The *dir_fd* argument.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: major(device)
@@ -1762,6 +1850,9 @@ features:
Availability: Unix.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. data:: pathconf_names
@@ -1779,9 +1870,10 @@ features:
may be converted to an absolute pathname using
``os.path.join(os.path.dirname(path), result)``.
- If the *path* is a string object, the result will also be a string object,
+ If the *path* is a string object (directly or indirectly through a
+ :class:`PathLike` interface), the result will also be a string object,
and the call may raise a UnicodeDecodeError. If the *path* is a bytes
- object, the result will be a bytes object.
+ object (direct or indirectly), the result will be a bytes object.
This function can also support :ref:`paths relative to directory descriptors
<dir_fd>`.
@@ -1794,6 +1886,9 @@ features:
.. versionadded:: 3.3
The *dir_fd* argument.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: remove(path, *, dir_fd=None)
@@ -1812,6 +1907,9 @@ features:
.. versionadded:: 3.3
The *dir_fd* argument.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: removedirs(name)
@@ -1826,6 +1924,9 @@ features:
they are empty. Raises :exc:`OSError` if the leaf directory could not be
successfully removed.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None)
@@ -1845,6 +1946,9 @@ features:
.. versionadded:: 3.3
The *src_dir_fd* and *dst_dir_fd* arguments.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object` for *src* and *dst*.
+
.. function:: renames(old, new)
@@ -1858,6 +1962,9 @@ features:
This function can fail with the new directory structure made if you lack
permissions needed to remove the leaf directory or file.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object` for *old* and *new*.
+
.. function:: replace(src, dst, *, src_dir_fd=None, dst_dir_fd=None)
@@ -1872,6 +1979,9 @@ features:
.. versionadded:: 3.3
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object` for *src* and *dst*.
+
.. function:: rmdir(path, *, dir_fd=None)
@@ -1885,38 +1995,56 @@ features:
.. versionadded:: 3.3
The *dir_fd* parameter.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: scandir(path='.')
- Return an iterator of :class:`DirEntry` objects corresponding to the entries
- in the directory given by *path*. The entries are yielded in arbitrary
- order, and the special entries ``'.'`` and ``'..'`` are not included.
+ Return an iterator of :class:`os.DirEntry` objects corresponding to the
+ entries in the directory given by *path*. The entries are yielded in
+ arbitrary order, and the special entries ``'.'`` and ``'..'`` are not
+ included.
Using :func:`scandir` instead of :func:`listdir` can significantly
increase the performance of code that also needs file type or file
- attribute information, because :class:`DirEntry` objects expose this
+ attribute information, because :class:`os.DirEntry` objects expose this
information if the operating system provides it when scanning a directory.
- All :class:`DirEntry` methods may perform a system call, but
- :func:`~DirEntry.is_dir` and :func:`~DirEntry.is_file` usually only
- require a system call for symbolic links; :func:`DirEntry.stat`
+ All :class:`os.DirEntry` methods may perform a system call, but
+ :func:`~os.DirEntry.is_dir` and :func:`~os.DirEntry.is_file` usually only
+ require a system call for symbolic links; :func:`os.DirEntry.stat`
always requires a system call on Unix but only requires one for
symbolic links on Windows.
- On Unix, *path* can be of type :class:`str` or :class:`bytes` (use
- :func:`~os.fsencode` and :func:`~os.fsdecode` to encode and decode
- :class:`bytes` paths). On Windows, *path* must be of type :class:`str`.
- On both systems, the type of the :attr:`~DirEntry.name` and
- :attr:`~DirEntry.path` attributes of each :class:`DirEntry` will be of
- the same type as *path*.
+ *path* may be a :term:`path-like object`. If *path* is of type ``bytes``
+ (directly or indirectly through the :class:`PathLike` interface),
+ the type of the :attr:`~os.DirEntry.name` and :attr:`~os.DirEntry.path`
+ attributes of each :class:`os.DirEntry` will be ``bytes``; in all other
+ circumstances, they will be of type ``str``.
+
+ The :func:`scandir` iterator supports the :term:`context manager` protocol
+ and has the following method:
+
+ .. method:: scandir.close()
+
+ Close the iterator and free acquired resources.
+
+ This is called automatically when the iterator is exhausted or garbage
+ collected, or when an error happens during iterating. However it
+ is advisable to call it explicitly or use the :keyword:`with`
+ statement.
+
+ .. versionadded:: 3.6
The following example shows a simple use of :func:`scandir` to display all
the files (excluding directories) in the given *path* that don't start with
``'.'``. The ``entry.is_file()`` call will generally not make an additional
system call::
- for entry in os.scandir(path):
- if not entry.name.startswith('.') and entry.is_file():
- print(entry.name)
+ with os.scandir(path) as it:
+ for entry in it:
+ if not entry.name.startswith('.') and entry.is_file():
+ print(entry.name)
.. note::
@@ -1932,6 +2060,14 @@ features:
.. versionadded:: 3.5
+ .. versionadded:: 3.6
+ Added support for the :term:`context manager` protocol and the
+ :func:`~scandir.close()` method. If a :func:`scandir` iterator is neither
+ exhausted nor explicitly closed a :exc:`ResourceWarning` will be emitted
+ in its destructor.
+
+ The function accepts a :term:`path-like object`.
+
.. class:: DirEntry
@@ -1940,27 +2076,30 @@ features:
:func:`scandir` will provide as much of this information as possible without
making additional system calls. When a ``stat()`` or ``lstat()`` system call
- is made, the ``DirEntry`` object will cache the result.
+ is made, the ``os.DirEntry`` object will cache the result.
- ``DirEntry`` instances are not intended to be stored in long-lived data
+ ``os.DirEntry`` instances are not intended to be stored in long-lived data
structures; if you know the file metadata has changed or if a long time has
elapsed since calling :func:`scandir`, call ``os.stat(entry.path)`` to fetch
up-to-date information.
- Because the ``DirEntry`` methods can make operating system calls, they may
+ Because the ``os.DirEntry`` methods can make operating system calls, they may
also raise :exc:`OSError`. If you need very fine-grained
control over errors, you can catch :exc:`OSError` when calling one of the
- ``DirEntry`` methods and handle as appropriate.
+ ``os.DirEntry`` methods and handle as appropriate.
- Attributes and methods on a ``DirEntry`` instance are as follows:
+ To be directly usable as a :term:`path-like object`, ``os.DirEntry``
+ implements the :class:`PathLike` interface.
+
+ Attributes and methods on a ``os.DirEntry`` instance are as follows:
.. attribute:: name
The entry's base filename, relative to the :func:`scandir` *path*
argument.
- The :attr:`name` attribute will be of the same type (``str`` or
- ``bytes``) as the :func:`scandir` *path* argument. Use
+ The :attr:`name` attribute will be ``bytes`` if the :func:`scandir`
+ *path* argument is of type ``bytes`` and ``str`` otherwise. Use
:func:`~os.fsdecode` to decode byte filenames.
.. attribute:: path
@@ -1970,16 +2109,17 @@ features:
argument. The path is only absolute if the :func:`scandir` *path*
argument was absolute.
- The :attr:`path` attribute will be of the same type (``str`` or
- ``bytes``) as the :func:`scandir` *path* argument. Use
+ The :attr:`path` attribute will be ``bytes`` if the :func:`scandir`
+ *path* argument is of type ``bytes`` and ``str`` otherwise. Use
:func:`~os.fsdecode` to decode byte filenames.
.. method:: inode()
Return the inode number of the entry.
- The result is cached on the ``DirEntry`` object. Use ``os.stat(entry.path,
- follow_symlinks=False).st_ino`` to fetch up-to-date information.
+ The result is cached on the ``os.DirEntry`` object. Use
+ ``os.stat(entry.path, follow_symlinks=False).st_ino`` to fetch up-to-date
+ information.
On the first, uncached call, a system call is required on Windows but
not on Unix.
@@ -1994,7 +2134,7 @@ features:
is a directory (without following symlinks); return ``False`` if the
entry is any other kind of file or if it doesn't exist anymore.
- The result is cached on the ``DirEntry`` object, with a separate cache
+ The result is cached on the ``os.DirEntry`` object, with a separate cache
for *follow_symlinks* ``True`` and ``False``. Call :func:`os.stat` along
with :func:`stat.S_ISDIR` to fetch up-to-date information.
@@ -2018,8 +2158,8 @@ features:
is a file (without following symlinks); return ``False`` if the entry is
a directory or other non-file entry, or if it doesn't exist anymore.
- The result is cached on the ``DirEntry`` object. Caching, system calls
- made, and exceptions raised are as per :func:`~DirEntry.is_dir`.
+ The result is cached on the ``os.DirEntry`` object. Caching, system calls
+ made, and exceptions raised are as per :func:`~os.DirEntry.is_dir`.
.. method:: is_symlink()
@@ -2027,7 +2167,7 @@ features:
return ``False`` if the entry points to a directory or any kind of file,
or if it doesn't exist anymore.
- The result is cached on the ``DirEntry`` object. Call
+ The result is cached on the ``os.DirEntry`` object. Call
:func:`os.path.islink` to fetch up-to-date information.
On the first, uncached call, no system call is required in most cases.
@@ -2052,23 +2192,29 @@ features:
:class:`stat_result` are always set to zero. Call :func:`os.stat` to
get these attributes.
- The result is cached on the ``DirEntry`` object, with a separate cache
+ The result is cached on the ``os.DirEntry`` object, with a separate cache
for *follow_symlinks* ``True`` and ``False``. Call :func:`os.stat` to
fetch up-to-date information.
Note that there is a nice correspondence between several attributes
- and methods of ``DirEntry`` and of :class:`pathlib.Path`. In
- particular, the ``name`` attribute has the same meaning, as do the
- ``is_dir()``, ``is_file()``, ``is_symlink()`` and ``stat()`` methods.
+ and methods of ``os.DirEntry`` and of :class:`pathlib.Path`. In
+ particular, the ``name`` attribute has the same
+ meaning, as do the ``is_dir()``, ``is_file()``, ``is_symlink()``
+ and ``stat()`` methods.
.. versionadded:: 3.5
+ .. versionchanged:: 3.6
+ Added support for the :class:`~os.PathLike` interface. Added support
+ for :class:`bytes` paths on Windows.
+
.. function:: stat(path, \*, dir_fd=None, follow_symlinks=True)
Get the status of a file or a file descriptor. Perform the equivalent of a
:c:func:`stat` system call on the given path. *path* may be specified as
- either a string or as an open file descriptor. Return a :class:`stat_result`
+ either a string -- directly or indirectly through the :class:`PathLike`
+ interface -- or as an open file descriptor. Return a :class:`stat_result`
object.
This function normally follows symlinks; to stat a symlink add the argument
@@ -2098,6 +2244,9 @@ features:
Added the *dir_fd* and *follow_symlinks* arguments, specifying a file
descriptor instead of a path.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. class:: stat_result
@@ -2319,19 +2468,22 @@ features:
This function can support :ref:`specifying a file descriptor <path_fd>`.
+ Availability: Unix.
+
.. versionchanged:: 3.2
The :const:`ST_RDONLY` and :const:`ST_NOSUID` constants were added.
+ .. versionadded:: 3.3
+ Added support for specifying an open file descriptor for *path*.
+
.. versionchanged:: 3.4
The :const:`ST_NODEV`, :const:`ST_NOEXEC`, :const:`ST_SYNCHRONOUS`,
:const:`ST_MANDLOCK`, :const:`ST_WRITE`, :const:`ST_APPEND`,
:const:`ST_IMMUTABLE`, :const:`ST_NOATIME`, :const:`ST_NODIRATIME`,
and :const:`ST_RELATIME` constants were added.
- Availability: Unix.
-
- .. versionadded:: 3.3
- Added support for specifying an open file descriptor for *path*.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
.. data:: supports_dir_fd
@@ -2452,6 +2604,9 @@ features:
Added the *dir_fd* argument, and now allow *target_is_directory*
on non-Windows platforms.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object` for *src* and *dst*.
+
.. function:: sync()
@@ -2476,6 +2631,10 @@ features:
.. versionchanged:: 3.5
Added support for Windows
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
+
.. function:: unlink(path, *, dir_fd=None)
Remove (delete) the file *path*. This function is semantically
@@ -2486,6 +2645,9 @@ features:
.. versionadded:: 3.3
The *dir_fd* parameter.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)
@@ -2523,6 +2685,9 @@ features:
Added support for specifying an open file descriptor for *path*,
and the *dir_fd*, *follow_symlinks*, and *ns* parameters.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: walk(top, topdown=True, onerror=None, followlinks=False)
@@ -2613,6 +2778,9 @@ features:
This function now calls :func:`os.scandir` instead of :func:`os.listdir`,
making it faster by reducing the number of calls to :func:`os.stat`.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: fwalk(top='.', topdown=True, onerror=None, *, follow_symlinks=False, dir_fd=None)
@@ -2669,6 +2837,9 @@ features:
.. versionadded:: 3.3
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
Linux extended attributes
~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -2680,12 +2851,16 @@ These functions are all available on Linux only.
.. function:: getxattr(path, attribute, *, follow_symlinks=True)
Return the value of the extended filesystem attribute *attribute* for
- *path*. *attribute* can be bytes or str. If it is str, it is encoded
- with the filesystem encoding.
+ *path*. *attribute* can be bytes or str (directly or indirectly through the
+ :class:`PathLike` interface). If it is str, it is encoded with the filesystem
+ encoding.
This function can support :ref:`specifying a file descriptor <path_fd>` and
:ref:`not following symlinks <follow_symlinks>`.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object` fpr *path* and *attribute*.
+
.. function:: listxattr(path=None, *, follow_symlinks=True)
@@ -2697,21 +2872,29 @@ These functions are all available on Linux only.
This function can support :ref:`specifying a file descriptor <path_fd>` and
:ref:`not following symlinks <follow_symlinks>`.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: removexattr(path, attribute, *, follow_symlinks=True)
Removes the extended filesystem attribute *attribute* from *path*.
- *attribute* should be bytes or str. If it is a string, it is encoded
+ *attribute* should be bytes or str (directly or indirectly through the
+ :class:`PathLike` interface). If it is a string, it is encoded
with the filesystem encoding.
This function can support :ref:`specifying a file descriptor <path_fd>` and
:ref:`not following symlinks <follow_symlinks>`.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object` for *path* and *attribute*.
+
.. function:: setxattr(path, attribute, value, flags=0, *, follow_symlinks=True)
Set the extended filesystem attribute *attribute* on *path* to *value*.
- *attribute* must be a bytes or str with no embedded NULs. If it is a str,
+ *attribute* must be a bytes or str with no embedded NULs (directly or
+ indirectly through the :class:`PathLike` interface). If it is a str,
it is encoded with the filesystem encoding. *flags* may be
:data:`XATTR_REPLACE` or :data:`XATTR_CREATE`. If :data:`XATTR_REPLACE` is
given and the attribute does not exist, ``EEXISTS`` will be raised.
@@ -2726,6 +2909,9 @@ These functions are all available on Linux only.
A bug in Linux kernel versions less than 2.6.39 caused the flags argument
to be ignored on some filesystems.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object` for *path* and *attribute*.
+
.. data:: XATTR_SIZE_MAX
@@ -2827,6 +3013,9 @@ to be ignored.
Added support for specifying an open file descriptor for *path*
for :func:`execve`.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. function:: _exit(n)
Exit the process with status *n*, without calling cleanup handlers, flushing
@@ -3137,6 +3326,9 @@ written in Python, such as a mail server's external command delivery program.
:func:`spawnve` are not thread-safe on Windows; we advise you to use the
:mod:`subprocess` module instead.
+ .. versionchanged:: 3.6
+ Accepts a :term:`path-like object`.
+
.. data:: P_NOWAIT
P_NOWAITO
@@ -3623,6 +3815,11 @@ Miscellaneous System Information
Return the number of CPUs in the system. Returns ``None`` if undetermined.
+ This number is not equivalent to the number of CPUs the current process can
+ use. The number of usable CPUs can be obtained with
+ ``len(os.sched_getaffinity(0))``
+
+
.. versionadded:: 3.4
@@ -3738,35 +3935,89 @@ Higher-level operations on pathnames are defined in the :mod:`os.path` module.
.. versionadded:: 3.3
-.. _os-miscfunc:
-Miscellaneous Functions
------------------------
+Random numbers
+--------------
+
+
+.. function:: getrandom(size, flags=0)
+
+ Get up to *size* random bytes. The function can return less bytes than
+ requested.
+
+ These bytes can be used to seed user-space random number generators or for
+ cryptographic purposes.
+
+ ``getrandom()`` relies on entropy gathered from device drivers and other
+ sources of environmental noise. Unnecessarily reading large quantities of
+ data will have a negative impact on other users of the ``/dev/random`` and
+ ``/dev/urandom`` devices.
+
+ The flags argument is a bit mask that can contain zero or more of the
+ following values ORed together: :py:data:`os.GRND_RANDOM` and
+ :py:data:`GRND_NONBLOCK`.
+
+ See also the `Linux getrandom() manual page
+ <http://man7.org/linux/man-pages/man2/getrandom.2.html>`_.
+
+ Availability: Linux 3.17 and newer.
+ .. versionadded:: 3.6
-.. function:: urandom(n)
+.. function:: urandom(size)
- Return a string of *n* random bytes suitable for cryptographic use.
+ Return a string of *size* random bytes suitable for cryptographic use.
This function returns random bytes from an OS-specific randomness source. The
returned data should be unpredictable enough for cryptographic applications,
though its exact quality depends on the OS implementation.
- On Linux, ``getrandom()`` syscall is used if available and the urandom
- entropy pool is initialized (``getrandom()`` does not block).
- On a Unix-like system this will query ``/dev/urandom``. On Windows, it
- will use ``CryptGenRandom()``. If a randomness source is not found,
- :exc:`NotImplementedError` will be raised.
+ On Linux, if the ``getrandom()`` syscall is available, it is used in
+ blocking mode: block until the system urandom entropy pool is initialized
+ (128 bits of entropy are collected by the kernel). See the :pep:`524` for
+ the rationale. On Linux, the :func:`getrandom` function can be used to get
+ random bytes in non-blocking mode (using the :data:`GRND_NONBLOCK` flag) or
+ to poll until the system urandom entropy pool is initialized.
- For an easy-to-use interface to the random number generator
- provided by your platform, please see :class:`random.SystemRandom`.
+ On a Unix-like system, random bytes are read from the ``/dev/urandom``
+ device. If the ``/dev/urandom`` device is not available or not readable, the
+ :exc:`NotImplementedError` exception is raised.
+
+ On Windows, it will use ``CryptGenRandom()``.
+
+ .. seealso::
+ The :mod:`secrets` module provides higher level functions. For an
+ easy-to-use interface to the random number generator provided by your
+ platform, please see :class:`random.SystemRandom`.
+
+ .. versionchanged:: 3.6.0
+ On Linux, ``getrandom()`` is now used in blocking mode to increase the
+ security.
.. versionchanged:: 3.5.2
- On Linux, if ``getrandom()`` blocks (the urandom entropy pool is not
- initialized yet), fall back on reading ``/dev/urandom``.
+ On Linux, if the ``getrandom()`` syscall blocks (the urandom entropy pool
+ is not initialized yet), fall back on reading ``/dev/urandom``.
.. versionchanged:: 3.5
On Linux 3.17 and newer, the ``getrandom()`` syscall is now used
when available. On OpenBSD 5.6 and newer, the C ``getentropy()``
function is now used. These functions avoid the usage of an internal file
descriptor.
+
+.. data:: GRND_NONBLOCK
+
+ By default, when reading from ``/dev/random``, :func:`getrandom` blocks if
+ no random bytes are available, and when reading from ``/dev/urandom``, it blocks
+ if the entropy pool has not yet been initialized.
+
+ If the :py:data:`GRND_NONBLOCK` flag is set, then :func:`getrandom` does not
+ block in these cases, but instead immediately raises :exc:`BlockingIOError`.
+
+ .. versionadded:: 3.6
+
+.. data:: GRND_RANDOM
+
+ If this bit is set, then random bytes are drawn from the
+ ``/dev/random`` pool instead of the ``/dev/urandom`` pool.
+
+ .. versionadded:: 3.6
diff --git a/Doc/library/pathlib.rst b/Doc/library/pathlib.rst
index 7b90a1f..34ab3b8 100644
--- a/Doc/library/pathlib.rst
+++ b/Doc/library/pathlib.rst
@@ -35,12 +35,6 @@ Pure paths are useful in some special cases; for example:
accessing the OS. In this case, instantiating one of the pure classes may be
useful since those simply don't have any OS-accessing operations.
-.. note::
- This module has been included in the standard library on a
- :term:`provisional basis <provisional package>`. Backwards incompatible
- changes (up to and including removal of the package) may occur if deemed
- necessary by the core developers.
-
.. seealso::
:pep:`428`: The pathlib module -- object-oriented filesystem paths.
@@ -111,7 +105,8 @@ we also call *flavours*:
PurePosixPath('setup.py')
Each element of *pathsegments* can be either a string representing a
- path segment, or another path object::
+ path segment, an object implementing the :class:`os.PathLike` interface
+ which returns a string, or another path object::
>>> PurePath('foo', 'some/path', 'bar')
PurePosixPath('foo/some/path/bar')
@@ -152,6 +147,12 @@ we also call *flavours*:
to ``PurePosixPath('bar')``, which is wrong if ``foo`` is a symbolic link
to another directory)
+ Pure path objects implement the :class:`os.PathLike` interface, allowing them
+ to be used anywhere the interface is accepted.
+
+ .. versionchanged:: 3.6
+ Added support for the :class:`os.PathLike` interface.
+
.. class:: PurePosixPath(*pathsegments)
A subclass of :class:`PurePath`, this path flavour represents non-Windows
@@ -199,7 +200,7 @@ Paths of a different flavour compare unequal and cannot be ordered::
>>> PureWindowsPath('foo') < PurePosixPath('foo')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
- TypeError: unorderable types: PureWindowsPath() < PurePosixPath()
+ TypeError: '<' not supported between instances of 'PureWindowsPath' and 'PurePosixPath'
Operators
@@ -216,6 +217,14 @@ The slash operator helps create child paths, similarly to :func:`os.path.join`::
>>> '/usr' / q
PurePosixPath('/usr/bin')
+A path object can be used anywhere an object implementing :class:`os.PathLike`
+is accepted::
+
+ >>> import os
+ >>> p = PurePath('/etc')
+ >>> os.fspath(p)
+ '/etc'
+
The string representation of a path is the raw filesystem path itself
(in native form, e.g. with backslashes under Windows), which you can
pass to any function taking a file path as a string::
@@ -910,7 +919,7 @@ call fails (for example because the path doesn't exist):
to an existing file or directory, it will be unconditionally replaced.
-.. method:: Path.resolve()
+.. method:: Path.resolve(strict=False)
Make the path absolute, resolving any symlinks. A new path object is
returned::
@@ -927,10 +936,14 @@ call fails (for example because the path doesn't exist):
>>> p.resolve()
PosixPath('/home/antoine/pathlib/setup.py')
- If the path doesn't exist, :exc:`FileNotFoundError` is raised. If an
- infinite loop is encountered along the resolution path,
- :exc:`RuntimeError` is raised.
+ If the path doesn't exist and *strict* is ``True``, :exc:`FileNotFoundError`
+ is raised. If *strict* is ``False``, the path is resolved as far as possible
+ and any remainder is appended without checking whether it exists. If an
+ infinite loop is encountered along the resolution path, :exc:`RuntimeError`
+ is raised.
+ .. versionadded:: 3.6
+ The *strict* argument.
.. method:: Path.rglob(pattern)
diff --git a/Doc/library/pdb.rst b/Doc/library/pdb.rst
index ba9e547..7c37bb7 100644
--- a/Doc/library/pdb.rst
+++ b/Doc/library/pdb.rst
@@ -144,7 +144,7 @@ The ``run*`` functions and :func:`set_trace` are aliases for instantiating the
access further features, you have to do this yourself:
.. class:: Pdb(completekey='tab', stdin=None, stdout=None, skip=None, \
- nosigint=False)
+ nosigint=False, readrc=True)
:class:`Pdb` is the debugger class.
@@ -160,6 +160,9 @@ access further features, you have to do this yourself:
This allows you to break into the debugger again by pressing :kbd:`Ctrl-C`. If you
want Pdb not to touch the SIGINT handler, set *nosigint* to true.
+ The *readrc* argument defaults to true and controls whether Pdb will load
+ .pdbrc files from the filesystem.
+
Example call to enable tracing with *skip*::
import pdb; pdb.Pdb(skip=['django.*']).set_trace()
@@ -171,6 +174,9 @@ access further features, you have to do this yourself:
The *nosigint* argument. Previously, a SIGINT handler was never set by
Pdb.
+ .. versionchanged:: 3.6
+ The *readrc* argument.
+
.. method:: run(statement, globals=None, locals=None)
runeval(expression, globals=None, locals=None)
runcall(function, *args, **kwds)
diff --git a/Doc/library/pickle.rst b/Doc/library/pickle.rst
index 0d64191..6e8430f 100644
--- a/Doc/library/pickle.rst
+++ b/Doc/library/pickle.rst
@@ -492,7 +492,7 @@ methods:
.. method:: object.__getnewargs_ex__()
- In protocols 4 and newer, classes that implements the
+ In protocols 2 and newer, classes that implements the
:meth:`__getnewargs_ex__` method can dictate the values passed to the
:meth:`__new__` method upon unpickling. The method must return a pair
``(args, kwargs)`` where *args* is a tuple of positional arguments
@@ -504,15 +504,22 @@ methods:
class requires keyword-only arguments. Otherwise, it is recommended for
compatibility to implement :meth:`__getnewargs__`.
+ .. versionchanged:: 3.6
+ :meth:`__getnewargs_ex__` is now used in protocols 2 and 3.
+
.. method:: object.__getnewargs__()
- This method serve a similar purpose as :meth:`__getnewargs_ex__` but
- for protocols 2 and newer. It must return a tuple of arguments ``args``
- which will be passed to the :meth:`__new__` method upon unpickling.
+ This method serve a similar purpose as :meth:`__getnewargs_ex__`, but
+ supports only positional arguments. It must return a tuple of arguments
+ ``args`` which will be passed to the :meth:`__new__` method upon unpickling.
+
+ :meth:`__getnewargs__` will not be called if :meth:`__getnewargs_ex__` is
+ defined.
- In protocols 4 and newer, :meth:`__getnewargs__` will not be called if
- :meth:`__getnewargs_ex__` is defined.
+ .. versionchanged:: 3.6
+ Before Python 3.6, :meth:`__getnewargs__` was called instead of
+ :meth:`__getnewargs_ex__` in protocols 2 and 3.
.. method:: object.__getstate__()
diff --git a/Doc/library/pkgutil.rst b/Doc/library/pkgutil.rst
index b9e76f7..fba0ea6 100644
--- a/Doc/library/pkgutil.rst
+++ b/Doc/library/pkgutil.rst
@@ -11,6 +11,11 @@
This module provides utilities for the import system, in particular package
support.
+.. class:: ModuleInfo(module_finder, name, ispkg)
+
+ A namedtuple that holds a brief summary of a module's info.
+
+ .. versionadded:: 3.6
.. function:: extend_path(path, name)
@@ -139,7 +144,7 @@ support.
.. function:: iter_modules(path=None, prefix='')
- Yields ``(module_finder, name, ispkg)`` for all submodules on *path*, or, if
+ Yields :class:`ModuleInfo` for all submodules on *path*, or, if
*path* is ``None``, all top-level modules on ``sys.path``.
*path* should be either ``None`` or a list of paths to look for modules in.
@@ -160,7 +165,7 @@ support.
.. function:: walk_packages(path=None, prefix='', onerror=None)
- Yields ``(module_finder, name, ispkg)`` for all modules recursively on
+ Yields :class:`ModuleInfo` for all modules recursively on
*path*, or, if *path* is ``None``, all accessible modules.
*path* should be either ``None`` or a list of paths to look for modules in.
diff --git a/Doc/library/poplib.rst b/Doc/library/poplib.rst
index ffabc32..d72b660 100644
--- a/Doc/library/poplib.rst
+++ b/Doc/library/poplib.rst
@@ -62,6 +62,13 @@ The :mod:`poplib` module provides two classes:
:attr:`ssl.SSLContext.check_hostname` and *Server Name Indication* (see
:data:`ssl.HAS_SNI`).
+ .. deprecated:: 3.6
+
+ *keyfile* and *certfile* are deprecated in favor of *context*.
+ Please use :meth:`ssl.SSLContext.load_cert_chain` instead, or let
+ :func:`ssl.create_default_context` select the system's trusted CA
+ certificates for you.
+
One exception is defined as an attribute of the :mod:`poplib` module:
diff --git a/Doc/library/queue.rst b/Doc/library/queue.rst
index 924be5a..bd0fc2d 100644
--- a/Doc/library/queue.rst
+++ b/Doc/library/queue.rst
@@ -16,8 +16,9 @@ availability of thread support in Python; see the :mod:`threading`
module.
The module implements three types of queue, which differ only in the order in
-which the entries are retrieved. In a FIFO queue, the first tasks added are
-the first retrieved. In a LIFO queue, the most recently added entry is
+which the entries are retrieved. In a :abbr:`FIFO (first-in, first-out)`
+queue, the first tasks added are the first retrieved. In a
+:abbr:`LIFO (last-in, first-out)` queue, the most recently added entry is
the first retrieved (operating like a stack). With a priority queue,
the entries are kept sorted (using the :mod:`heapq` module) and the
lowest valued entry is retrieved first.
@@ -29,14 +30,16 @@ The :mod:`queue` module defines the following classes and exceptions:
.. class:: Queue(maxsize=0)
- Constructor for a FIFO queue. *maxsize* is an integer that sets the upperbound
+ Constructor for a :abbr:`FIFO (first-in, first-out)` queue. *maxsize* is
+ an integer that sets the upperbound
limit on the number of items that can be placed in the queue. Insertion will
block once this size has been reached, until queue items are consumed. If
*maxsize* is less than or equal to zero, the queue size is infinite.
.. class:: LifoQueue(maxsize=0)
- Constructor for a LIFO queue. *maxsize* is an integer that sets the upperbound
+ Constructor for a :abbr:`LIFO (last-in, first-out)` queue. *maxsize* is
+ an integer that sets the upperbound
limit on the number of items that can be placed in the queue. Insertion will
block once this size has been reached, until queue items are consumed. If
*maxsize* is less than or equal to zero, the queue size is infinite.
diff --git a/Doc/library/random.rst b/Doc/library/random.rst
index 8ae242c..4f25157 100644
--- a/Doc/library/random.rst
+++ b/Doc/library/random.rst
@@ -46,10 +46,24 @@ from sources provided by the operating system.
.. warning::
The pseudo-random generators of this module should not be used for
- security purposes.
+ security purposes. For security or cryptographic uses, see the
+ :mod:`secrets` module.
+.. seealso::
+
+ M. Matsumoto and T. Nishimura, "Mersenne Twister: A 623-dimensionally
+ equidistributed uniform pseudorandom number generator", ACM Transactions on
+ Modeling and Computer Simulation Vol. 8, No. 1, January pp.3--30 1998.
+
+
+ `Complementary-Multiply-with-Carry recipe
+ <https://code.activestate.com/recipes/576707/>`_ for a compatible alternative
+ random number generator with a long period and comparatively simple update
+ operations.
-Bookkeeping functions:
+
+Bookkeeping functions
+---------------------
.. function:: seed(a=None, version=2)
@@ -93,7 +107,8 @@ Bookkeeping functions:
:meth:`randrange` to handle arbitrarily large ranges.
-Functions for integers:
+Functions for integers
+----------------------
.. function:: randrange(stop)
randrange(start, stop[, step])
@@ -116,23 +131,55 @@ Functions for integers:
``randrange(a, b+1)``.
-Functions for sequences:
+Functions for sequences
+-----------------------
.. function:: choice(seq)
Return a random element from the non-empty sequence *seq*. If *seq* is empty,
raises :exc:`IndexError`.
+.. function:: choices(population, weights=None, *, cum_weights=None, k=1)
+
+ Return a *k* sized list of elements chosen from the *population* with replacement.
+ If the *population* is empty, raises :exc:`IndexError`.
+
+ If a *weights* sequence is specified, selections are made according to the
+ relative weights. Alternatively, if a *cum_weights* sequence is given, the
+ selections are made according to the cumulative weights (perhaps computed
+ using :func:`itertools.accumulate`). For example, the relative weights
+ ``[10, 5, 30, 5]`` are equivalent to the cumulative weights
+ ``[10, 15, 45, 50]``. Internally, the relative weights are converted to
+ cumulative weights before making selections, so supplying the cumulative
+ weights saves work.
+
+ If neither *weights* nor *cum_weights* are specified, selections are made
+ with equal probability. If a weights sequence is supplied, it must be
+ the same length as the *population* sequence. It is a :exc:`TypeError`
+ to specify both *weights* and *cum_weights*.
+
+ The *weights* or *cum_weights* can use any numeric type that interoperates
+ with the :class:`float` values returned by :func:`random` (that includes
+ integers, floats, and fractions but excludes decimals).
+
+ .. versionadded:: 3.6
+
.. function:: shuffle(x[, random])
- Shuffle the sequence *x* in place. The optional argument *random* is a
- 0-argument function returning a random float in [0.0, 1.0); by default, this is
- the function :func:`.random`.
+ Shuffle the sequence *x* in place.
+
+ The optional argument *random* is a 0-argument function returning a random
+ float in [0.0, 1.0); by default, this is the function :func:`.random`.
+
+ To shuffle an immutable sequence and return a new shuffled list, use
+ ``sample(x, k=len(x))`` instead.
- Note that for even rather small ``len(x)``, the total number of permutations of
- *x* is larger than the period of most random number generators; this implies
- that most permutations of a long sequence can never be generated.
+ Note that even for small ``len(x)``, the total number of permutations of *x*
+ can quickly grow larger than the period of most random number generators.
+ This implies that most permutations of a long sequence can never be
+ generated. For example, a sequence of length 2080 is the largest that
+ can fit within the period of the Mersenne Twister random number generator.
.. function:: sample(population, k)
@@ -149,13 +196,16 @@ Functions for sequences:
Members of the population need not be :term:`hashable` or unique. If the population
contains repeats, then each occurrence is a possible selection in the sample.
- To choose a sample from a range of integers, use an :func:`range` object as an
+ To choose a sample from a range of integers, use a :func:`range` object as an
argument. This is especially fast and space efficient for sampling from a large
- population: ``sample(range(10000000), 60)``.
+ population: ``sample(range(10000000), k=60)``.
If the sample size is larger than the population size, a :exc:`ValueError`
is raised.
+Real-valued distributions
+-------------------------
+
The following functions generate specific real-valued distributions. Function
parameters are named after the corresponding variables in the distribution's
equation, as used in common mathematical practice; most of these equations can
@@ -250,7 +300,8 @@ be found in any statistics text.
parameter.
-Alternative Generator:
+Alternative Generator
+---------------------
.. class:: SystemRandom([seed])
@@ -262,19 +313,6 @@ Alternative Generator:
:exc:`NotImplementedError` if called.
-.. seealso::
-
- M. Matsumoto and T. Nishimura, "Mersenne Twister: A 623-dimensionally
- equidistributed uniform pseudorandom number generator", ACM Transactions on
- Modeling and Computer Simulation Vol. 8, No. 1, January pp.3--30 1998.
-
-
- `Complementary-Multiply-with-Carry recipe
- <https://code.activestate.com/recipes/576707/>`_ for a compatible alternative
- random number generator with a long period and comparatively simple update
- operations.
-
-
Notes on Reproducibility
------------------------
@@ -296,53 +334,146 @@ change across Python versions, but two aspects are guaranteed not to change:
Examples and Recipes
--------------------
-Basic usage::
+Basic examples::
- >>> random.random() # Random float x, 0.0 <= x < 1.0
+ >>> random() # Random float: 0.0 <= x < 1.0
0.37444887175646646
- >>> random.uniform(1, 10) # Random float x, 1.0 <= x < 10.0
- 1.1800146073117523
+ >>> uniform(2.5, 10.0) # Random float: 2.5 <= x < 10.0
+ 3.1800146073117523
+
+ >>> expovariate(1 / 5) # Interval between arrivals averaging 5 seconds
+ 5.148957571865031
- >>> random.randrange(10) # Integer from 0 to 9
+ >>> randrange(10) # Integer from 0 to 9 inclusive
7
- >>> random.randrange(0, 101, 2) # Even integer from 0 to 100
+ >>> randrange(0, 101, 2) # Even integer from 0 to 100 inclusive
26
- >>> random.choice('abcdefghij') # Single random element
- 'c'
-
- >>> items = [1, 2, 3, 4, 5, 6, 7]
- >>> random.shuffle(items)
- >>> items
- [7, 3, 2, 5, 6, 4, 1]
-
- >>> random.sample([1, 2, 3, 4, 5], 3) # Three samples without replacement
- [4, 1, 5]
-
-A common task is to make a :func:`random.choice` with weighted probabilities.
+ >>> choice(['win', 'lose', 'draw']) # Single random element from a sequence
+ 'draw'
+
+ >>> deck = 'ace two three four'.split()
+ >>> shuffle(deck) # Shuffle a list
+ >>> deck
+ ['four', 'two', 'ace', 'three']
+
+ >>> sample([10, 20, 30, 40, 50], k=4) # Four samples without replacement
+ [40, 10, 50, 30]
+
+Simulations::
+
+ >>> # Six roulette wheel spins (weighted sampling with replacement)
+ >>> choices(['red', 'black', 'green'], [18, 18, 2], k=6)
+ ['red', 'green', 'black', 'black', 'red', 'black']
+
+ >>> # Deal 20 cards without replacement from a deck of 52 playing cards
+ >>> # and determine the proportion of cards with a ten-value
+ >>> # (a ten, jack, queen, or king).
+ >>> deck = collections.Counter(tens=16, low_cards=36)
+ >>> seen = sample(list(deck.elements()), k=20)
+ >>> seen.count('tens') / 20
+ 0.15
+
+ >>> # Estimate the probability of getting 5 or more heads from 7 spins
+ >>> # of a biased coin that settles on heads 60% of the time.
+ >>> trial = lambda: choices('HT', cum_weights=(0.60, 1.00), k=7).count('H') >= 5
+ >>> sum(trial() for i in range(10000)) / 10000
+ 0.4169
+
+ >>> # Probability of the median of 5 samples being in middle two quartiles
+ >>> trial = lambda : 2500 <= sorted(choices(range(10000), k=5))[2] < 7500
+ >>> sum(trial() for i in range(10000)) / 10000
+ 0.7958
+
+Example of `statistical bootstrapping
+<https://en.wikipedia.org/wiki/Bootstrapping_(statistics)>`_ using resampling
+with replacement to estimate a confidence interval for the mean of a sample of
+size five::
+
+ # http://statistics.about.com/od/Applications/a/Example-Of-Bootstrapping.htm
+ from statistics import mean
+ from random import choices
+
+ data = 1, 2, 4, 4, 10
+ means = sorted(mean(choices(data, k=5)) for i in range(20))
+ print(f'The sample mean of {mean(data):.1f} has a 90% confidence '
+ f'interval from {means[1]:.1f} to {means[-2]:.1f}')
+
+Example of a `resampling permutation test
+<https://en.wikipedia.org/wiki/Resampling_(statistics)#Permutation_tests>`_
+to determine the statistical significance or `p-value
+<https://en.wikipedia.org/wiki/P-value>`_ of an observed difference
+between the effects of a drug versus a placebo::
+
+ # Example from "Statistics is Easy" by Dennis Shasha and Manda Wilson
+ from statistics import mean
+ from random import shuffle
+
+ drug = [54, 73, 53, 70, 73, 68, 52, 65, 65]
+ placebo = [54, 51, 58, 44, 55, 52, 42, 47, 58, 46]
+ observed_diff = mean(drug) - mean(placebo)
+
+ n = 10000
+ count = 0
+ combined = drug + placebo
+ for i in range(n):
+ shuffle(combined)
+ new_diff = mean(combined[:len(drug)]) - mean(combined[len(drug):])
+ count += (new_diff >= observed_diff)
+
+ print(f'{n} label reshufflings produced only {count} instances with a difference')
+ print(f'at least as extreme as the observed difference of {observed_diff:.1f}.')
+ print(f'The one-sided p-value of {count / n:.4f} leads us to reject the null')
+ print(f'hypothesis that there is no difference between the drug and the placebo.')
+
+Simulation of arrival times and service deliveries in a single server queue::
+
+ from random import expovariate, gauss
+ from statistics import mean, median, stdev
+
+ average_arrival_interval = 5.6
+ average_service_time = 5.0
+ stdev_service_time = 0.5
+
+ num_waiting = 0
+ arrivals = []
+ starts = []
+ arrival = service_end = 0.0
+ for i in range(20000):
+ if arrival <= service_end:
+ num_waiting += 1
+ arrival += expovariate(1.0 / average_arrival_interval)
+ arrivals.append(arrival)
+ else:
+ num_waiting -= 1
+ service_start = service_end if num_waiting else arrival
+ service_time = gauss(average_service_time, stdev_service_time)
+ service_end = service_start + service_time
+ starts.append(service_start)
+
+ waits = [start - arrival for arrival, start in zip(arrivals, starts)]
+ print(f'Mean wait: {mean(waits):.1f}. Stdev wait: {stdev(waits):.1f}.')
+ print(f'Median wait: {median(waits):.1f}. Max wait: {max(waits):.1f}.')
-If the weights are small integer ratios, a simple technique is to build a sample
-population with repeats::
-
- >>> weighted_choices = [('Red', 3), ('Blue', 2), ('Yellow', 1), ('Green', 4)]
- >>> population = [val for val, cnt in weighted_choices for i in range(cnt)]
- >>> population
- ['Red', 'Red', 'Red', 'Blue', 'Blue', 'Yellow', 'Green', 'Green', 'Green', 'Green']
-
- >>> random.choice(population)
- 'Green'
-
-A more general approach is to arrange the weights in a cumulative distribution
-with :func:`itertools.accumulate`, and then locate the random value with
-:func:`bisect.bisect`::
-
- >>> choices, weights = zip(*weighted_choices)
- >>> cumdist = list(itertools.accumulate(weights))
- >>> cumdist # [3, 3+2, 3+2+1, 3+2+1+4]
- [3, 5, 6, 10]
+.. seealso::
- >>> x = random.random() * cumdist[-1]
- >>> choices[bisect.bisect(cumdist, x)]
- 'Blue'
+ `Statistics for Hackers <https://www.youtube.com/watch?v=Iq9DzN6mvYA>`_
+ a video tutorial by
+ `Jake Vanderplas <https://us.pycon.org/2016/speaker/profile/295/>`_
+ on statistical analysis using just a few fundamental concepts
+ including simulation, sampling, shuffling, and cross-validation.
+
+ `Economics Simulation
+ <http://nbviewer.jupyter.org/url/norvig.com/ipython/Economics.ipynb>`_
+ a simulation of a marketplace by
+ `Peter Norvig <http://norvig.com/bio.html>`_ that shows effective
+ use of many of the tools and distributions provided by this module
+ (gauss, uniform, sample, betavariate, choice, triangular, and randrange).
+
+ `A Concrete Introduction to Probability (using Python)
+ <http://nbviewer.jupyter.org/url/norvig.com/ipython/Probability.ipynb>`_
+ a tutorial by `Peter Norvig <http://norvig.com/bio.html>`_ covering
+ the basics of probability theory, how to write simulations, and
+ how to perform data analysis using Python.
diff --git a/Doc/library/re.rst b/Doc/library/re.rst
index 1ca621e..7ef4cbe 100644
--- a/Doc/library/re.rst
+++ b/Doc/library/re.rst
@@ -230,12 +230,8 @@ The special characters are:
flags are described in :ref:`contents-of-module-re`.) This
is useful if you wish to include the flags as part of the regular
expression, instead of passing a *flag* argument to the
- :func:`re.compile` function.
-
- Note that the ``(?x)`` flag changes how the expression is parsed. It should be
- used first in the expression string, or after one or more whitespace characters.
- If there are non-whitespace characters before the flag, the results are
- undefined.
+ :func:`re.compile` function. Flags should be used first in the
+ expression string.
``(?:...)``
A non-capturing version of regular parentheses. Matches whatever regular
@@ -243,6 +239,16 @@ The special characters are:
*cannot* be retrieved after performing a match or referenced later in the
pattern.
+``(?imsx-imsx:...)``
+ (Zero or more letters from the set ``'i'``, ``'m'``, ``'s'``, ``'x'``,
+ optionally followed by ``'-'`` followed by one or more letters from the
+ same set.) The letters set or removes the corresponding flags:
+ :const:`re.I` (ignore case), :const:`re.M` (multi-line), :const:`re.S`
+ (dot matches all), and :const:`re.X` (verbose), for the part of the
+ expression. (The flags are described in :ref:`contents-of-module-re`.)
+
+ .. versionadded:: 3.6
+
``(?P<name>...)``
Similar to regular parentheses, but the substring matched by the group is
accessible via the symbolic group name *name*. Group names must be valid
@@ -327,8 +333,9 @@ The special characters are:
The special sequences consist of ``'\'`` and a character from the list below.
-If the ordinary character is not on the list, then the resulting RE will match
-the second character. For example, ``\$`` matches the character ``'$'``.
+If the ordinary character is not an ASCII digit or an ASCII letter, then the
+resulting RE will match the second character. For example, ``\$`` matches the
+character ``'$'``.
``\number``
Matches the contents of the group of the same number. Groups are numbered
@@ -448,9 +455,8 @@ three digits in length.
.. versionchanged:: 3.3
The ``'\u'`` and ``'\U'`` escape sequences have been added.
-.. deprecated-removed:: 3.5 3.6
- Unknown escapes consisting of ``'\'`` and ASCII letter now raise a
- deprecation warning and will be forbidden in Python 3.6.
+.. versionchanged:: 3.6
+ Unknown escapes consisting of ``'\'`` and an ASCII letter now are errors.
.. seealso::
@@ -472,6 +478,9 @@ functions are simplified versions of the full featured methods for compiled
regular expressions. Most non-trivial applications always use the compiled
form.
+.. versionchanged:: 3.6
+ Flag constants are now instances of :class:`RegexFlag`, which is a subclass of
+ :class:`enum.IntFlag`.
.. function:: compile(pattern, flags=0)
@@ -538,11 +547,11 @@ form.
current locale. The use of this flag is discouraged as the locale mechanism
is very unreliable, and it only handles one "culture" at a time anyway;
you should use Unicode matching instead, which is the default in Python 3
- for Unicode (str) patterns. This flag makes sense only with bytes patterns.
+ for Unicode (str) patterns. This flag can be used only with bytes patterns.
- .. deprecated-removed:: 3.5 3.6
- Deprecated the use of :const:`re.LOCALE` with string patterns or
- :const:`re.ASCII`.
+ .. versionchanged:: 3.6
+ :const:`re.LOCALE` can be used only with bytes patterns and is
+ not compatible with :const:`re.ASCII`.
.. data:: M
@@ -748,9 +757,13 @@ form.
.. versionchanged:: 3.5
Unmatched groups are replaced with an empty string.
- .. deprecated-removed:: 3.5 3.6
- Unknown escapes consist of ``'\'`` and ASCII letter now raise a
- deprecation warning and will be forbidden in Python 3.6.
+ .. versionchanged:: 3.6
+ Unknown escapes in *pattern* consisting of ``'\'`` and an ASCII letter
+ now are errors.
+
+ .. deprecated-removed:: 3.5 3.7
+ Unknown escapes in *repl* consisting of ``'\'`` and an ASCII letter now raise
+ a deprecation warning and will be forbidden in Python 3.7.
.. function:: subn(pattern, repl, string, count=0, flags=0)
@@ -1016,6 +1029,22 @@ Match objects support the following methods and attributes:
'c3'
+.. method:: match.__getitem__(g)
+
+ This is identical to ``m.group(g)``. This allows easier access to
+ an individual group from a match:
+
+ >>> m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
+ >>> m[0] # The entire match
+ 'Isaac Newton'
+ >>> m[1] # The first parenthesized subgroup.
+ 'Isaac'
+ >>> m[2] # The second parenthesized subgroup.
+ 'Newton'
+
+ .. versionadded:: 3.6
+
+
.. method:: match.groups(default=None)
Return a tuple containing all the subgroups of the match, from 1 up to however
diff --git a/Doc/library/readline.rst b/Doc/library/readline.rst
index 4d3c099..54c54b4 100644
--- a/Doc/library/readline.rst
+++ b/Doc/library/readline.rst
@@ -167,6 +167,20 @@ The following functions operate on a global history list:
This calls :c:func:`add_history` in the underlying library.
+.. function:: set_auto_history(enabled)
+
+ Enable or disable automatic calls to :c:func:`add_history` when reading
+ input via readline. The *enabled* argument should be a Boolean value
+ that when true, enables auto history, and that when false, disables
+ auto history.
+
+ .. versionadded:: 3.6
+
+ .. impl-detail::
+ Auto history is enabled by default, and changes to this do not persist
+ across multiple sessions.
+
+
Startup hooks
-------------
diff --git a/Doc/library/secrets.rst b/Doc/library/secrets.rst
new file mode 100644
index 0000000..9bf848f
--- /dev/null
+++ b/Doc/library/secrets.rst
@@ -0,0 +1,198 @@
+:mod:`secrets` --- Generate secure random numbers for managing secrets
+======================================================================
+
+.. module:: secrets
+ :synopsis: Generate secure random numbers for managing secrets.
+
+.. moduleauthor:: Steven D'Aprano <steve+python@pearwood.info>
+.. sectionauthor:: Steven D'Aprano <steve+python@pearwood.info>
+.. versionadded:: 3.6
+
+.. testsetup::
+
+ from secrets import *
+ __name__ = '<doctest>'
+
+**Source code:** :source:`Lib/secrets.py`
+
+-------------
+
+The :mod:`secrets` module is used for generating cryptographically strong
+random numbers suitable for managing data such as passwords, account
+authentication, security tokens, and related secrets.
+
+In particularly, :mod:`secrets` should be used in preference to the
+default pseudo-random number generator in the :mod:`random` module, which
+is designed for modelling and simulation, not security or cryptography.
+
+.. seealso::
+
+ :pep:`506`
+
+
+Random numbers
+--------------
+
+The :mod:`secrets` module provides access to the most secure source of
+randomness that your operating system provides.
+
+.. class:: SystemRandom
+
+ A class for generating random numbers using the highest-quality
+ sources provided by the operating system. See
+ :class:`random.SystemRandom` for additional details.
+
+.. function:: choice(sequence)
+
+ Return a randomly-chosen element from a non-empty sequence.
+
+.. function:: randbelow(n)
+
+ Return a random int in the range [0, *n*).
+
+.. function:: randbits(k)
+
+ Return an int with *k* random bits.
+
+
+Generating tokens
+-----------------
+
+The :mod:`secrets` module provides functions for generating secure
+tokens, suitable for applications such as password resets,
+hard-to-guess URLs, and similar.
+
+.. function:: token_bytes([nbytes=None])
+
+ Return a random byte string containing *nbytes* number of bytes.
+ If *nbytes* is ``None`` or not supplied, a reasonable default is
+ used.
+
+ .. doctest::
+
+ >>> token_bytes(16) #doctest:+SKIP
+ b'\xebr\x17D*t\xae\xd4\xe3S\xb6\xe2\xebP1\x8b'
+
+
+.. function:: token_hex([nbytes=None])
+
+ Return a random text string, in hexadecimal. The string has *nbytes*
+ random bytes, each byte converted to two hex digits. If *nbytes* is
+ ``None`` or not supplied, a reasonable default is used.
+
+ .. doctest::
+
+ >>> token_hex(16) #doctest:+SKIP
+ 'f9bf78b9a18ce6d46a0cd2b0b86df9da'
+
+.. function:: token_urlsafe([nbytes=None])
+
+ Return a random URL-safe text string, containing *nbytes* random
+ bytes. The text is Base64 encoded, so on average each byte results
+ in approximately 1.3 characters. If *nbytes* is ``None`` or not
+ supplied, a reasonable default is used.
+
+ .. doctest::
+
+ >>> token_urlsafe(16) #doctest:+SKIP
+ 'Drmhze6EPcv0fN_81Bj-nA'
+
+
+How many bytes should tokens use?
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+To be secure against
+`brute-force attacks <https://en.wikipedia.org/wiki/Brute-force_attack>`_,
+tokens need to have sufficient randomness. Unfortunately, what is
+considered sufficient will necessarily increase as computers get more
+powerful and able to make more guesses in a shorter period. As of 2015,
+it is believed that 32 bytes (256 bits) of randomness is sufficient for
+the typical use-case expected for the :mod:`secrets` module.
+
+For those who want to manage their own token length, you can explicitly
+specify how much randomness is used for tokens by giving an :class:`int`
+argument to the various ``token_*`` functions. That argument is taken
+as the number of bytes of randomness to use.
+
+Otherwise, if no argument is provided, or if the argument is ``None``,
+the ``token_*`` functions will use a reasonable default instead.
+
+.. note::
+
+ That default is subject to change at any time, including during
+ maintenance releases.
+
+
+Other functions
+---------------
+
+.. function:: compare_digest(a, b)
+
+ Return ``True`` if strings *a* and *b* are equal, otherwise ``False``,
+ in such a way as to reduce the risk of
+ `timing attacks <http://codahale.com/a-lesson-in-timing-attacks/>`_.
+ See :func:`hmac.compare_digest` for additional details.
+
+
+Recipes and best practices
+--------------------------
+
+This section shows recipes and best practices for using :mod:`secrets`
+to manage a basic level of security.
+
+Generate an eight-character alphanumeric password:
+
+.. testcode::
+
+ import string
+ alphabet = string.ascii_letters + string.digits
+ password = ''.join(choice(alphabet) for i in range(8))
+
+
+.. note::
+
+ Applications should not
+ `store passwords in a recoverable format <http://cwe.mitre.org/data/definitions/257.html>`_,
+ whether plain text or encrypted. They should be salted and hashed
+ using a cryptographically-strong one-way (irreversible) hash function.
+
+
+Generate a ten-character alphanumeric password with at least one
+lowercase character, at least one uppercase character, and at least
+three digits:
+
+.. testcode::
+
+ import string
+ alphabet = string.ascii_letters + string.digits
+ while True:
+ password = ''.join(choice(alphabet) for i in range(10))
+ if (any(c.islower() for c in password)
+ and any(c.isupper() for c in password)
+ and sum(c.isdigit() for c in password) >= 3):
+ break
+
+
+Generate an `XKCD-style passphrase <http://xkcd.com/936/>`_:
+
+.. testcode::
+
+ # On standard Linux systems, use a convenient dictionary file.
+ # Other platforms may need to provide their own word-list.
+ with open('/usr/share/dict/words') as f:
+ words = [word.strip() for word in f]
+ password = ' '.join(choice(words) for i in range(4))
+
+
+Generate a hard-to-guess temporary URL containing a security token
+suitable for password recovery applications:
+
+.. testcode::
+
+ url = 'https://mydomain.com/reset=' + token_urlsafe()
+
+
+
+..
+ # This modeline must appear within the last ten lines of the file.
+ kate: indent-width 3; remove-trailing-space on; replace-tabs on; encoding utf-8;
diff --git a/Doc/library/select.rst b/Doc/library/select.rst
index a81386a..f97118e 100644
--- a/Doc/library/select.rst
+++ b/Doc/library/select.rst
@@ -268,35 +268,43 @@ Edge and Level Trigger Polling (epoll) Objects
*eventmask*
- +-----------------------+-----------------------------------------------+
- | Constant | Meaning |
- +=======================+===============================================+
- | :const:`EPOLLIN` | Available for read |
- +-----------------------+-----------------------------------------------+
- | :const:`EPOLLOUT` | Available for write |
- +-----------------------+-----------------------------------------------+
- | :const:`EPOLLPRI` | Urgent data for read |
- +-----------------------+-----------------------------------------------+
- | :const:`EPOLLERR` | Error condition happened on the assoc. fd |
- +-----------------------+-----------------------------------------------+
- | :const:`EPOLLHUP` | Hang up happened on the assoc. fd |
- +-----------------------+-----------------------------------------------+
- | :const:`EPOLLET` | Set Edge Trigger behavior, the default is |
- | | Level Trigger behavior |
- +-----------------------+-----------------------------------------------+
- | :const:`EPOLLONESHOT` | Set one-shot behavior. After one event is |
- | | pulled out, the fd is internally disabled |
- +-----------------------+-----------------------------------------------+
- | :const:`EPOLLRDNORM` | Equivalent to :const:`EPOLLIN` |
- +-----------------------+-----------------------------------------------+
- | :const:`EPOLLRDBAND` | Priority data band can be read. |
- +-----------------------+-----------------------------------------------+
- | :const:`EPOLLWRNORM` | Equivalent to :const:`EPOLLOUT` |
- +-----------------------+-----------------------------------------------+
- | :const:`EPOLLWRBAND` | Priority data may be written. |
- +-----------------------+-----------------------------------------------+
- | :const:`EPOLLMSG` | Ignored. |
- +-----------------------+-----------------------------------------------+
+ +-------------------------+-----------------------------------------------+
+ | Constant | Meaning |
+ +=========================+===============================================+
+ | :const:`EPOLLIN` | Available for read |
+ +-------------------------+-----------------------------------------------+
+ | :const:`EPOLLOUT` | Available for write |
+ +-------------------------+-----------------------------------------------+
+ | :const:`EPOLLPRI` | Urgent data for read |
+ +-------------------------+-----------------------------------------------+
+ | :const:`EPOLLERR` | Error condition happened on the assoc. fd |
+ +-------------------------+-----------------------------------------------+
+ | :const:`EPOLLHUP` | Hang up happened on the assoc. fd |
+ +-------------------------+-----------------------------------------------+
+ | :const:`EPOLLET` | Set Edge Trigger behavior, the default is |
+ | | Level Trigger behavior |
+ +-------------------------+-----------------------------------------------+
+ | :const:`EPOLLONESHOT` | Set one-shot behavior. After one event is |
+ | | pulled out, the fd is internally disabled |
+ +-------------------------+-----------------------------------------------+
+ | :const:`EPOLLEXCLUSIVE` | Wake only one epoll object when the |
+ | | associated fd has an event. The default (if |
+ | | this flag is not set) is to wake all epoll |
+ | | objects polling on on a fd. |
+ +-------------------------+-----------------------------------------------+
+ | :const:`EPOLLRDHUP` | Stream socket peer closed connection or shut |
+ | | down writing half of connection. |
+ +-------------------------+-----------------------------------------------+
+ | :const:`EPOLLRDNORM` | Equivalent to :const:`EPOLLIN` |
+ +-------------------------+-----------------------------------------------+
+ | :const:`EPOLLRDBAND` | Priority data band can be read. |
+ +-------------------------+-----------------------------------------------+
+ | :const:`EPOLLWRNORM` | Equivalent to :const:`EPOLLOUT` |
+ +-------------------------+-----------------------------------------------+
+ | :const:`EPOLLWRBAND` | Priority data may be written. |
+ +-------------------------+-----------------------------------------------+
+ | :const:`EPOLLMSG` | Ignored. |
+ +-------------------------+-----------------------------------------------+
.. method:: epoll.close()
@@ -385,6 +393,9 @@ linearly scanned again. :c:func:`select` is O(highest file descriptor), while
+-------------------+------------------------------------------+
| :const:`POLLHUP` | Hung up |
+-------------------+------------------------------------------+
+ | :const:`POLLRDHUP`| Stream socket peer closed connection, or |
+ | | shut down writing half of connection |
+ +-------------------+------------------------------------------+
| :const:`POLLNVAL` | Invalid request: descriptor not open |
+-------------------+------------------------------------------+
diff --git a/Doc/library/shlex.rst b/Doc/library/shlex.rst
index e81f982..4926f04 100644
--- a/Doc/library/shlex.rst
+++ b/Doc/library/shlex.rst
@@ -73,11 +73,11 @@ The :mod:`shlex` module defines the following functions:
The :mod:`shlex` module defines the following class:
-.. class:: shlex(instream=None, infile=None, posix=False)
+.. class:: shlex(instream=None, infile=None, posix=False, punctuation_chars=False)
A :class:`~shlex.shlex` instance or subclass instance is a lexical analyzer
object. The initialization argument, if present, specifies where to read
- characters from. It must be a file-/stream-like object with
+ characters from. It must be a file-/stream-like object with
:meth:`~io.TextIOBase.read` and :meth:`~io.TextIOBase.readline` methods, or
a string. If no argument is given, input will be taken from ``sys.stdin``.
The second optional argument is a filename string, which sets the initial
@@ -87,8 +87,19 @@ The :mod:`shlex` module defines the following class:
when *posix* is not true (default), the :class:`~shlex.shlex` instance will
operate in compatibility mode. When operating in POSIX mode,
:class:`~shlex.shlex` will try to be as close as possible to the POSIX shell
- parsing rules.
-
+ parsing rules. The *punctuation_chars* argument provides a way to make the
+ behaviour even closer to how real shells parse. This can take a number of
+ values: the default value, ``False``, preserves the behaviour seen under
+ Python 3.5 and earlier. If set to ``True``, then parsing of the characters
+ ``();<>|&`` is changed: any run of these characters (considered punctuation
+ characters) is returned as a single token. If set to a non-empty string of
+ characters, those characters will be used as the punctuation characters. Any
+ characters in the :attr:`wordchars` attribute that appear in
+ *punctuation_chars* will be removed from :attr:`wordchars`. See
+ :ref:`improved-shell-compatibility` for more information.
+
+ .. versionchanged:: 3.6
+ The *punctuation_chars* parameter was added.
.. seealso::
@@ -191,7 +202,13 @@ variables which either control lexical analysis or can be used for debugging:
.. attribute:: shlex.wordchars
The string of characters that will accumulate into multi-character tokens. By
- default, includes all ASCII alphanumerics and underscore.
+ default, includes all ASCII alphanumerics and underscore. In POSIX mode, the
+ accented characters in the Latin-1 set are also included. If
+ :attr:`punctuation_chars` is not empty, the characters ``~-./*?=``, which can
+ appear in filename specifications and command line parameters, will also be
+ included in this attribute, and any characters which appear in
+ ``punctuation_chars`` will be removed from ``wordchars`` if they are present
+ there.
.. attribute:: shlex.whitespace
@@ -222,9 +239,13 @@ variables which either control lexical analysis or can be used for debugging:
.. attribute:: shlex.whitespace_split
- If ``True``, tokens will only be split in whitespaces. This is useful, for
+ If ``True``, tokens will only be split in whitespaces. This is useful, for
example, for parsing command lines with :class:`~shlex.shlex`, getting
- tokens in a similar way to shell arguments.
+ tokens in a similar way to shell arguments. If this attribute is ``True``,
+ :attr:`punctuation_chars` will have no effect, and splitting will happen
+ only on whitespaces. When using :attr:`punctuation_chars`, which is
+ intended to provide parsing closer to that implemented by shells, it is
+ advisable to leave ``whitespace_split`` as ``False`` (the default value).
.. attribute:: shlex.infile
@@ -245,10 +266,9 @@ variables which either control lexical analysis or can be used for debugging:
This attribute is ``None`` by default. If you assign a string to it, that
string will be recognized as a lexical-level inclusion request similar to the
``source`` keyword in various shells. That is, the immediately following token
- will be opened as a filename and input will
- be taken from that stream until EOF, at which
- point the :meth:`~io.IOBase.close` method of that stream will be called and
- the input source will again become the original input stream. Source
+ will be opened as a filename and input will be taken from that stream until
+ EOF, at which point the :meth:`~io.IOBase.close` method of that stream will be
+ called and the input source will again become the original input stream. Source
requests may be stacked any number of levels deep.
@@ -275,6 +295,16 @@ variables which either control lexical analysis or can be used for debugging:
(``''``), in non-POSIX mode, and to ``None`` in POSIX mode.
+.. attribute:: shlex.punctuation_chars
+
+ Characters that will be considered punctuation. Runs of punctuation
+ characters will be returned as a single token. However, note that no
+ semantic validity checking will be performed: for example, '>>>' could be
+ returned as a token, even though it may not be recognised as such by shells.
+
+ .. versionadded:: 3.6
+
+
.. _shlex-parsing-rules:
Parsing Rules
@@ -327,3 +357,58 @@ following parsing rules.
* EOF is signaled with a :const:`None` value;
* Quoted empty strings (``''``) are allowed.
+
+.. _improved-shell-compatibility:
+
+Improved Compatibility with Shells
+----------------------------------
+
+.. versionadded:: 3.6
+
+The :class:`shlex` class provides compatibility with the parsing performed by
+common Unix shells like ``bash``, ``dash``, and ``sh``. To take advantage of
+this compatibility, specify the ``punctuation_chars`` argument in the
+constructor. This defaults to ``False``, which preserves pre-3.6 behaviour.
+However, if it is set to ``True``, then parsing of the characters ``();<>|&``
+is changed: any run of these characters is returned as a single token. While
+this is short of a full parser for shells (which would be out of scope for the
+standard library, given the multiplicity of shells out there), it does allow
+you to perform processing of command lines more easily than you could
+otherwise. To illustrate, you can see the difference in the following snippet:
+
+.. doctest::
+ :options: +NORMALIZE_WHITESPACE
+
+ >>> import shlex
+ >>> text = "a && b; c && d || e; f >'abc'; (def \"ghi\")"
+ >>> list(shlex.shlex(text))
+ ['a', '&', '&', 'b', ';', 'c', '&', '&', 'd', '|', '|', 'e', ';', 'f', '>',
+ "'abc'", ';', '(', 'def', '"ghi"', ')']
+ >>> list(shlex.shlex(text, punctuation_chars=True))
+ ['a', '&&', 'b', ';', 'c', '&&', 'd', '||', 'e', ';', 'f', '>', "'abc'",
+ ';', '(', 'def', '"ghi"', ')']
+
+Of course, tokens will be returned which are not valid for shells, and you'll
+need to implement your own error checks on the returned tokens.
+
+Instead of passing ``True`` as the value for the punctuation_chars parameter,
+you can pass a string with specific characters, which will be used to determine
+which characters constitute punctuation. For example::
+
+ >>> import shlex
+ >>> s = shlex.shlex("a && b || c", punctuation_chars="|")
+ >>> list(s)
+ ['a', '&', '&', 'b', '||', 'c']
+
+.. note:: When ``punctuation_chars`` is specified, the :attr:`~shlex.wordchars`
+ attribute is augmented with the characters ``~-./*?=``. That is because these
+ characters can appear in file names (including wildcards) and command-line
+ arguments (e.g. ``--color=auto``). Hence::
+
+ >>> import shlex
+ >>> s = shlex.shlex('~/a && b-c --color=auto || d *.py?',
+ ... punctuation_chars=True)
+ >>> list(s)
+ ['~/a', '&&', 'b-c', '--color=auto', '||', 'd', '*.py?']
+
+
diff --git a/Doc/library/shutil.rst b/Doc/library/shutil.rst
index b020bb3..a85bf33 100644
--- a/Doc/library/shutil.rst
+++ b/Doc/library/shutil.rst
@@ -425,7 +425,7 @@ Another example that uses the *ignore* argument to add a logging call::
import logging
def _logpath(path, names):
- logging.info('Working in %s' % path)
+ logging.info('Working in %s', path)
return [] # nothing will be ignored
copytree(source, destination, ignore=_logpath)
diff --git a/Doc/library/site.rst b/Doc/library/site.rst
index 0a73f5a..43daf79 100644
--- a/Doc/library/site.rst
+++ b/Doc/library/site.rst
@@ -52,7 +52,8 @@ searched for site-packages; otherwise they won't.
A path configuration file is a file whose name has the form :file:`{name}.pth`
and exists in one of the four directories mentioned above; its contents are
additional items (one per line) to be added to ``sys.path``. Non-existing items
-are never added to ``sys.path``. No item is added to ``sys.path`` more than
+are never added to ``sys.path``, and no check is made that the item refers to a
+directory rather than a file. No item is added to ``sys.path`` more than
once. Blank lines and lines beginning with ``#`` are skipped. Lines starting
with ``import`` (followed by space or tab) are executed.
diff --git a/Doc/library/smtpd.rst b/Doc/library/smtpd.rst
index a096de0..e383201 100644
--- a/Doc/library/smtpd.rst
+++ b/Doc/library/smtpd.rst
@@ -29,7 +29,7 @@ SMTPServer Objects
.. class:: SMTPServer(localaddr, remoteaddr, data_size_limit=33554432,\
- map=None, enable_SMTPUTF8=False, decode_data=True)
+ map=None, enable_SMTPUTF8=False, decode_data=False)
Create a new :class:`SMTPServer` object, which binds to local address
*localaddr*. It will treat *remoteaddr* as an upstream SMTP relayer. Both
@@ -46,20 +46,19 @@ SMTPServer Objects
global socket map is used.
*enable_SMTPUTF8* determines whether the ``SMTPUTF8`` extension (as defined
- in :RFC:`6531`) should be enabled. The default is ``False``. If set to
- ``True``, *decode_data* must be ``False`` (otherwise an error is raised).
+ in :RFC:`6531`) should be enabled. The default is ``False``.
When ``True``, ``SMTPUTF8`` is accepted as a parameter to the ``MAIL``
command and when present is passed to :meth:`process_message` in the
- ``kwargs['mail_options']`` list.
+ ``kwargs['mail_options']`` list. *decode_data* and *enable_SMTPUTF8*
+ cannot be set to ``True`` at the same time.
*decode_data* specifies whether the data portion of the SMTP transaction
- should be decoded using UTF-8. The default is ``True`` for backward
- compatibility reasons, but will change to ``False`` in Python 3.6; specify
- the keyword value explicitly to avoid the :exc:`DeprecationWarning`. When
- *decode_data* is set to ``False`` the server advertises the ``8BITMIME``
+ should be decoded using UTF-8. When *decode_data* is ``False`` (the
+ default), the server advertises the ``8BITMIME``
extension (:rfc:`6152`), accepts the ``BODY=8BITMIME`` parameter to
the ``MAIL`` command, and when present passes it to :meth:`process_message`
- in the ``kwargs['mail_options']`` list.
+ in the ``kwargs['mail_options']`` list. *decode_data* and *enable_SMTPUTF8*
+ cannot be set to ``True`` at the same time.
.. method:: process_message(peer, mailfrom, rcpttos, data, **kwargs)
@@ -76,9 +75,8 @@ SMTPServer Objects
will be a bytes object.
*kwargs* is a dictionary containing additional information. It is empty
- unless at least one of ``decode_data=False`` or ``enable_SMTPUTF8=True``
- was given as an init parameter, in which case it contains the following
- keys:
+ if ``decode_data=True`` was given as an init argument, otherwise
+ it contains the following keys:
*mail_options*:
a list of all received parameters to the ``MAIL``
@@ -109,9 +107,12 @@ SMTPServer Objects
*localaddr* and *remoteaddr* may now contain IPv6 addresses.
.. versionadded:: 3.5
- the *decode_data* and *enable_SMTPUTF8* constructor arguments, and the
- *kwargs* argument to :meth:`process_message` when one or more of these is
- specified.
+ The *decode_data* and *enable_SMTPUTF8* constructor parameters, and the
+ *kwargs* parameter to :meth:`process_message` when *decode_data* is
+ ``False``.
+
+ .. versionchanged:: 3.6
+ *decode_data* is now ``False`` by default.
DebuggingServer Objects
@@ -151,7 +152,7 @@ SMTPChannel Objects
-------------------
.. class:: SMTPChannel(server, conn, addr, data_size_limit=33554432,\
- map=None, enable_SMTPUTF8=False, decode_data=True)
+ map=None, enable_SMTPUTF8=False, decode_data=False)
Create a new :class:`SMTPChannel` object which manages the communication
between the server and a single SMTP client.
@@ -163,22 +164,25 @@ SMTPChannel Objects
limit.
*enable_SMTPUTF8* determines whether the ``SMTPUTF8`` extension (as defined
- in :RFC:`6531`) should be enabled. The default is ``False``. A
- :exc:`ValueError` is raised if both *enable_SMTPUTF8* and *decode_data* are
- set to ``True`` at the same time.
+ in :RFC:`6531`) should be enabled. The default is ``False``.
+ *decode_data* and *enable_SMTPUTF8* cannot be set to ``True`` at the same
+ time.
A dictionary can be specified in *map* to avoid using a global socket map.
*decode_data* specifies whether the data portion of the SMTP transaction
- should be decoded using UTF-8. The default is ``True`` for backward
- compatibility reasons, but will change to ``False`` in Python 3.6. Specify
- the keyword value explicitly to avoid the :exc:`DeprecationWarning`.
+ should be decoded using UTF-8. The default is ``False``.
+ *decode_data* and *enable_SMTPUTF8* cannot be set to ``True`` at the same
+ time.
To use a custom SMTPChannel implementation you need to override the
:attr:`SMTPServer.channel_class` of your :class:`SMTPServer`.
.. versionchanged:: 3.5
- the *decode_data* and *enable_SMTPUTF8* arguments were added.
+ The *decode_data* and *enable_SMTPUTF8* parameters were added.
+
+ .. versionchanged:: 3.6
+ *decode_data* is now ``False`` by default.
The :class:`SMTPChannel` has the following instance variables:
diff --git a/Doc/library/smtplib.rst b/Doc/library/smtplib.rst
index 8b98ccf..86e769e 100644
--- a/Doc/library/smtplib.rst
+++ b/Doc/library/smtplib.rst
@@ -95,6 +95,14 @@ Protocol) and :rfc:`1869` (SMTP Service Extensions).
:attr:`ssl.SSLContext.check_hostname` and *Server Name Indication* (see
:data:`ssl.HAS_SNI`).
+ .. deprecated:: 3.6
+
+ *keyfile* and *certfile* are deprecated in favor of *context*.
+ Please use :meth:`ssl.SSLContext.load_cert_chain` instead, or let
+ :func:`ssl.create_default_context` select the system's trusted CA
+ certificates for you.
+
+
.. class:: LMTP(host='', port=LMTP_PORT, local_hostname=None, source_address=None)
The LMTP protocol, which is very similar to ESMTP, is heavily based on the
diff --git a/Doc/library/socket.rst b/Doc/library/socket.rst
index f8ba847..c21cb7f 100644
--- a/Doc/library/socket.rst
+++ b/Doc/library/socket.rst
@@ -133,6 +133,22 @@ created. Socket addresses are represented as follows:
string format. (ex. ``b'12:23:34:45:56:67'``) This protocol is not
supported under FreeBSD.
+- :const:`AF_ALG` is a Linux-only socket based interface to Kernel
+ cryptography. An algorithm socket is configured with a tuple of two to four
+ elements ``(type, name [, feat [, mask]])``, where:
+
+ - *type* is the algorithm type as string, e.g. ``aead``, ``hash``,
+ ``skcipher`` or ``rng``.
+
+ - *name* is the algorithm name and operation mode as string, e.g.
+ ``sha256``, ``hmac(sha256)``, ``cbc(aes)`` or ``drbg_nopr_ctr_aes256``.
+
+ - *feat* and *mask* are unsigned 32bit integers.
+
+ Availability Linux 2.6.38, some algorithm types require more recent Kernels.
+
+ .. versionadded:: 3.6
+
- Certain other address families (:const:`AF_PACKET`, :const:`AF_CAN`)
support specific representations.
@@ -283,6 +299,10 @@ Constants
in the Unix header files are defined; for a few symbols, default values are
provided.
+ .. versionchanged:: 3.6
+ ``SO_DOMAIN``, ``SO_PROTOCOL``, ``SO_PEERSEC``, ``SO_PASSSEC``
+ were added.
+
.. data:: AF_CAN
PF_CAN
SOL_CAN_*
@@ -331,18 +351,33 @@ Constants
.. versionadded:: 3.3
-.. data:: SIO_*
+.. data:: SIO_RCVALL
+ SIO_KEEPALIVE_VALS
+ SIO_LOOPBACK_FAST_PATH
RCVALL_*
Constants for Windows' WSAIoctl(). The constants are used as arguments to the
:meth:`~socket.socket.ioctl` method of socket objects.
+ .. versionchanged:: 3.6
+ ``SIO_LOOPBACK_FAST_PATH`` was added.
+
.. data:: TIPC_*
TIPC related constants, matching the ones exported by the C socket API. See
the TIPC documentation for more information.
+.. data:: AF_ALG
+ SOL_ALG
+ ALG_*
+
+ Constants for Linux Kernel cryptography.
+
+ Availability: Linux >= 2.6.38.
+
+ .. versionadded:: 3.6
+
.. data:: AF_LINK
Availability: BSD, OSX.
@@ -874,6 +909,10 @@ to sockets.
it is recommended to :meth:`close` them explicitly, or to use a
:keyword:`with` statement around them.
+ .. versionchanged:: 3.6
+ :exc:`OSError` is now raised if an error occurs when the underlying
+ :c:func:`close` call is made.
+
.. note::
:meth:`close()` releases the resource associated with a connection but
@@ -994,6 +1033,12 @@ to sockets.
On other platforms, the generic :func:`fcntl.fcntl` and :func:`fcntl.ioctl`
functions may be used; they accept a socket object as their first argument.
+ Currently only the following control codes are supported:
+ ``SIO_RCVALL``, ``SIO_KEEPALIVE_VALS``, and ``SIO_LOOPBACK_FAST_PATH``.
+
+ .. versionchanged:: 3.6
+ ``SIO_LOOPBACK_FAST_PATH`` was added.
+
.. method:: socket.listen([backlog])
Enable a server to accept connections. If *backlog* is specified, it must
@@ -1277,6 +1322,15 @@ to sockets.
an exception, the method now retries the system call instead of raising
an :exc:`InterruptedError` exception (see :pep:`475` for the rationale).
+.. method:: socket.sendmsg_afalg([msg], *, op[, iv[, assoclen[, flags]]])
+
+ Specialized version of :meth:`~socket.sendmsg` for :const:`AF_ALG` socket.
+ Set mode, IV, AEAD associated data length and flags for :const:`AF_ALG` socket.
+
+ Availability: Linux >= 2.6.38
+
+ .. versionadded:: 3.6
+
.. method:: socket.sendfile(file, offset=0, count=None)
Send a file until EOF is reached by using high-performance
@@ -1325,21 +1379,29 @@ to sockets.
For further information, please consult the :ref:`notes on socket timeouts <socket-timeouts>`.
-.. method:: socket.setsockopt(level, optname, value)
+.. method:: socket.setsockopt(level, optname, value: int)
+.. method:: socket.setsockopt(level, optname, value: buffer)
+.. method:: socket.setsockopt(level, optname, None, optlen: int)
.. index:: module: struct
Set the value of the given socket option (see the Unix manual page
:manpage:`setsockopt(2)`). The needed symbolic constants are defined in the
- :mod:`socket` module (:const:`SO_\*` etc.). The value can be an integer or
- a :term:`bytes-like object` representing a buffer. In the latter case it is
- up to the caller to
- ensure that the bytestring contains the proper bits (see the optional built-in
- module :mod:`struct` for a way to encode C structures as bytestrings).
+ :mod:`socket` module (:const:`SO_\*` etc.). The value can be an integer,
+ ``None`` or a :term:`bytes-like object` representing a buffer. In the later
+ case it is up to the caller to ensure that the bytestring contains the
+ proper bits (see the optional built-in module :mod:`struct` for a way to
+ encode C structures as bytestrings). When value is set to ``None``,
+ optlen argument is required. It's equivalent to call setsockopt C
+ function with optval=NULL and optlen=optlen.
+
.. versionchanged:: 3.5
Writable :term:`bytes-like object` is now accepted.
+ .. versionchanged:: 3.6
+ setsockopt(level, optname, None, optlen: int) form added.
+
.. method:: socket.shutdown(how)
diff --git a/Doc/library/socketserver.rst b/Doc/library/socketserver.rst
index 087f4e0..218a31c 100644
--- a/Doc/library/socketserver.rst
+++ b/Doc/library/socketserver.rst
@@ -52,11 +52,12 @@ handler class by subclassing the :class:`BaseRequestHandler` class and
overriding its :meth:`~BaseRequestHandler.handle` method;
this method will process incoming
requests. Second, you must instantiate one of the server classes, passing it
-the server's address and the request handler class. Then call the
+the server's address and the request handler class. It is recommended to use
+the server in a :keyword:`with` statement. Then call the
:meth:`~BaseServer.handle_request` or
:meth:`~BaseServer.serve_forever` method of the server object to
process one or many requests. Finally, call :meth:`~BaseServer.server_close`
-to close the socket.
+to close the socket (unless you used a :keyword:`with` statement).
When inheriting from :class:`ThreadingMixIn` for threaded connection behavior,
you should explicitly declare how you want your threads to behave on an abrupt
@@ -111,6 +112,8 @@ server classes.
:class:`UDPServer`. Setting the various attributes also changes the
behavior of the underlying server mechanism.
+ :class:`ForkingMixIn` and the Forking classes mentioned below are
+ only available on POSIX platforms that support :func:`~os.fork`.
.. class:: ForkingTCPServer
ForkingUDPServer
@@ -304,7 +307,11 @@ Server Objects
This function is called if the :meth:`~BaseRequestHandler.handle`
method of a :attr:`RequestHandlerClass` instance raises
an exception. The default action is to print the traceback to
- standard output and continue handling further requests.
+ standard error and continue handling further requests.
+
+ .. versionchanged:: 3.6
+ Now only called for exceptions derived from the :exc:`Exception`
+ class.
.. method:: handle_timeout()
@@ -349,6 +356,11 @@ Server Objects
default implementation always returns :const:`True`.
+ .. versionchanged:: 3.6
+ Support for the :term:`context manager` protocol was added. Exiting the
+ context manager is equivalent to calling :meth:`server_close`.
+
+
Request Handler Objects
-----------------------
@@ -397,6 +409,15 @@ Request Handler Objects
read or written, respectively, to get the request data or return data
to the client.
+ The :attr:`rfile` attributes of both classes support the
+ :class:`io.BufferedIOBase` readable interface, and
+ :attr:`DatagramRequestHandler.wfile` supports the
+ :class:`io.BufferedIOBase` writable interface.
+
+ .. versionchanged:: 3.6
+ :attr:`StreamRequestHandler.wfile` also supports the
+ :class:`io.BufferedIOBase` writable interface.
+
Examples
--------
@@ -429,11 +450,10 @@ This is the server side::
HOST, PORT = "localhost", 9999
# Create the server, binding to localhost on port 9999
- server = socketserver.TCPServer((HOST, PORT), MyTCPHandler)
-
- # Activate the server; this will keep running until you
- # interrupt the program with Ctrl-C
- server.serve_forever()
+ with socketserver.TCPServer((HOST, PORT), MyTCPHandler) as server:
+ # Activate the server; this will keep running until you
+ # interrupt the program with Ctrl-C
+ server.serve_forever()
An alternative request handler class that makes use of streams (file-like
objects that simplify communication by providing the standard file interface)::
@@ -525,8 +545,8 @@ This is the server side::
if __name__ == "__main__":
HOST, PORT = "localhost", 9999
- server = socketserver.UDPServer((HOST, PORT), MyUDPHandler)
- server.serve_forever()
+ with socketserver.UDPServer((HOST, PORT), MyUDPHandler) as server:
+ server.serve_forever()
This is the client side::
@@ -585,22 +605,22 @@ An example for the :class:`ThreadingMixIn` class::
HOST, PORT = "localhost", 0
server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler)
- ip, port = server.server_address
+ with server:
+ ip, port = server.server_address
- # Start a thread with the server -- that thread will then start one
- # more thread for each request
- server_thread = threading.Thread(target=server.serve_forever)
- # Exit the server thread when the main thread terminates
- server_thread.daemon = True
- server_thread.start()
- print("Server loop running in thread:", server_thread.name)
+ # Start a thread with the server -- that thread will then start one
+ # more thread for each request
+ server_thread = threading.Thread(target=server.serve_forever)
+ # Exit the server thread when the main thread terminates
+ server_thread.daemon = True
+ server_thread.start()
+ print("Server loop running in thread:", server_thread.name)
- client(ip, port, "Hello World 1")
- client(ip, port, "Hello World 2")
- client(ip, port, "Hello World 3")
+ client(ip, port, "Hello World 1")
+ client(ip, port, "Hello World 2")
+ client(ip, port, "Hello World 3")
- server.shutdown()
- server.server_close()
+ server.shutdown()
The output of the example should look something like this:
@@ -616,3 +636,5 @@ The output of the example should look something like this:
The :class:`ForkingMixIn` class is used in the same way, except that the server
will spawn a new process for each request.
+Available only on POSIX platforms that support :func:`~os.fork`.
+
diff --git a/Doc/library/spwd.rst b/Doc/library/spwd.rst
index fd3c9ad..c6cad2a 100644
--- a/Doc/library/spwd.rst
+++ b/Doc/library/spwd.rst
@@ -55,6 +55,9 @@ The following functions are defined:
Return the shadow password database entry for the given user name.
+ .. versionchanged:: 3.6
+ Raises a :exc:`PermissionError` instead of :exc:`KeyError` if the user
+ doesn't have privileges.
.. function:: getspall()
diff --git a/Doc/library/sqlite3.rst b/Doc/library/sqlite3.rst
index 84a4783..5635577 100644
--- a/Doc/library/sqlite3.rst
+++ b/Doc/library/sqlite3.rst
@@ -629,9 +629,16 @@ Cursor Objects
.. attribute:: lastrowid
This read-only attribute provides the rowid of the last modified row. It is
- only set if you issued an ``INSERT`` statement using the :meth:`execute`
- method. For operations other than ``INSERT`` or when :meth:`executemany` is
- called, :attr:`lastrowid` is set to :const:`None`.
+ only set if you issued an ``INSERT`` or a ``REPLACE`` statement using the
+ :meth:`execute` method. For operations other than ``INSERT`` or
+ ``REPLACE`` or when :meth:`executemany` is called, :attr:`lastrowid` is
+ set to :const:`None`.
+
+ If the ``INSERT`` or ``REPLACE`` statement failed to insert the previous
+ successful rowid is returned.
+
+ .. versionchanged:: 3.6
+ Added support for the ``REPLACE`` statement.
.. attribute:: description
@@ -918,9 +925,7 @@ Controlling Transactions
By default, the :mod:`sqlite3` module opens transactions implicitly before a
Data Modification Language (DML) statement (i.e.
-``INSERT``/``UPDATE``/``DELETE``/``REPLACE``), and commits transactions
-implicitly before a non-DML, non-query statement (i. e.
-anything other than ``SELECT`` or the aforementioned).
+``INSERT``/``UPDATE``/``DELETE``/``REPLACE``).
So if you are within a transaction and issue a command like ``CREATE TABLE
...``, ``VACUUM``, ``PRAGMA``, the :mod:`sqlite3` module will commit implicitly
@@ -940,6 +945,9 @@ Otherwise leave it at its default, which will result in a plain "BEGIN"
statement, or set it to one of SQLite's supported isolation levels: "DEFERRED",
"IMMEDIATE" or "EXCLUSIVE".
+.. versionchanged:: 3.6
+ :mod:`sqlite3` used to implicitly commit an open transaction before DDL
+ statements. This is no longer the case.
Using :mod:`sqlite3` efficiently
diff --git a/Doc/library/ssl.rst b/Doc/library/ssl.rst
index a2f0083..eee4849 100644
--- a/Doc/library/ssl.rst
+++ b/Doc/library/ssl.rst
@@ -49,6 +49,12 @@ For more sophisticated applications, the :class:`ssl.SSLContext` class
helps manage settings and certificates, which can then be inherited
by SSL sockets created through the :meth:`SSLContext.wrap_socket` method.
+.. versionchanged:: 3.6
+
+ OpenSSL 0.9.8, 1.0.0 and 1.0.1 are deprecated and no longer supported.
+ In the future the ssl module will require at least OpenSSL 1.0.2 or
+ 1.1.0.
+
Functions, Constants, and Exceptions
------------------------------------
@@ -186,16 +192,20 @@ instead.
.. table::
- ======================== ========= ========= ========== ========= =========== ===========
- *client* / **server** **SSLv2** **SSLv3** **TLS** **TLSv1** **TLSv1.1** **TLSv1.2**
- ------------------------ --------- --------- ---------- --------- ----------- -----------
- *SSLv2* yes no yes no no no
- *SSLv3* no yes yes no no no
- *TLS* (*SSLv23*) no yes yes yes yes yes
- *TLSv1* no no yes yes no no
- *TLSv1.1* no no yes no yes no
- *TLSv1.2* no no yes no no yes
- ======================== ========= ========= ========== ========= =========== ===========
+ ======================== ============ ============ ============= ========= =========== ===========
+ *client* / **server** **SSLv2** **SSLv3** **TLS** **TLSv1** **TLSv1.1** **TLSv1.2**
+ ------------------------ ------------ ------------ ------------- --------- ----------- -----------
+ *SSLv2* yes no no [1]_ no no no
+ *SSLv3* no yes no [2]_ no no no
+ *TLS* (*SSLv23*) no [1]_ no [2]_ yes yes yes yes
+ *TLSv1* no no yes yes no no
+ *TLSv1.1* no no yes no yes no
+ *TLSv1.2* no no yes no no yes
+ ======================== ============ ============ ============= ========= =========== ===========
+
+ .. rubric:: Footnotes
+ .. [1] :class:`SSLContext` disables SSLv2 with :data:`OP_NO_SSLv2` by default.
+ .. [2] :class:`SSLContext` disables SSLv3 with :data:`OP_NO_SSLv3` by default.
.. note::
@@ -224,7 +234,6 @@ instead.
.. versionchanged:: 3.2
New optional argument *ciphers*.
-
Context creation
^^^^^^^^^^^^^^^^
@@ -279,7 +288,7 @@ purposes.
RC4 was dropped from the default cipher string.
- .. versionchanged:: 3.5.3
+ .. versionchanged:: 3.6
ChaCha20/Poly1305 was added to the default cipher string.
@@ -322,7 +331,7 @@ Random generation
.. versionadded:: 3.3
- .. deprecated:: 3.5.3
+ .. deprecated:: 3.6
OpenSSL has deprecated :func:`ssl.RAND_pseudo_bytes`, use
:func:`ssl.RAND_bytes` instead.
@@ -509,6 +518,10 @@ Certificate handling
Constants
^^^^^^^^^
+ All constants are now :class:`enum.IntEnum` or :class:`enum.IntFlag` collections.
+
+ .. versionadded:: 3.6
+
.. data:: CERT_NONE
Possible value for :attr:`SSLContext.verify_mode`, or the ``cert_reqs``
@@ -542,6 +555,12 @@ Constants
be passed, either to :meth:`SSLContext.load_verify_locations` or as a
value of the ``ca_certs`` parameter to :func:`wrap_socket`.
+.. class:: VerifyMode
+
+ :class:`enum.IntEnum` collection of CERT_* constants.
+
+ .. versionadded:: 3.6
+
.. data:: VERIFY_DEFAULT
Possible value for :attr:`SSLContext.verify_flags`. In this mode, certificate
@@ -582,18 +601,40 @@ Constants
.. versionadded:: 3.4.4
+.. class:: VerifyFlags
+
+ :class:`enum.IntFlag` collection of VERIFY_* constants.
+
+ .. versionadded:: 3.6
+
.. data:: PROTOCOL_TLS
Selects the highest protocol version that both the client and server support.
Despite the name, this option can select "TLS" protocols as well as "SSL".
- .. versionadded:: 3.5.3
+ .. versionadded:: 3.6
+
+.. data:: PROTOCOL_TLS_CLIENT
+
+ Auto-negotiate the the highest protocol version like :data:`PROTOCOL_SSLv23`,
+ but only support client-side :class:`SSLSocket` connections. The protocol
+ enables :data:`CERT_REQUIRED` and :attr:`~SSLContext.check_hostname` by
+ default.
+
+ .. versionadded:: 3.6
+
+.. data:: PROTOCOL_TLS_SERVER
+
+ Auto-negotiate the the highest protocol version like :data:`PROTOCOL_SSLv23`,
+ but only support server-side :class:`SSLSocket` connections.
+
+ .. versionadded:: 3.6
.. data:: PROTOCOL_SSLv23
Alias for data:`PROTOCOL_TLS`.
- .. deprecated:: 3.5.3
+ .. deprecated:: 3.6
Use data:`PROTOCOL_TLS` instead.
@@ -608,7 +649,7 @@ Constants
SSL version 2 is insecure. Its use is highly discouraged.
- .. deprecated:: 3.5.3
+ .. deprecated:: 3.6
OpenSSL has removed support for SSLv2.
@@ -623,7 +664,7 @@ Constants
SSL version 3 is insecure. Its use is highly discouraged.
- .. deprecated:: 3.5.3
+ .. deprecated:: 3.6
OpenSSL has deprecated all version specific protocols. Use the default
protocol data:`PROTOCOL_TLS` with flags like data:`OP_NO_SSLv3` instead.
@@ -632,7 +673,7 @@ Constants
Selects TLS version 1.0 as the channel encryption protocol.
- .. deprecated:: 3.5.3
+ .. deprecated:: 3.6
OpenSSL has deprecated all version specific protocols. Use the default
protocol data:`PROTOCOL_TLS` with flags like data:`OP_NO_SSLv3` instead.
@@ -644,7 +685,7 @@ Constants
.. versionadded:: 3.4
- .. deprecated:: 3.5.3
+ .. deprecated:: 3.6
OpenSSL has deprecated all version specific protocols. Use the default
protocol data:`PROTOCOL_TLS` with flags like data:`OP_NO_SSLv3` instead.
@@ -657,7 +698,7 @@ Constants
.. versionadded:: 3.4
- .. deprecated:: 3.5.3
+ .. deprecated:: 3.6
OpenSSL has deprecated all version specific protocols. Use the default
protocol data:`PROTOCOL_TLS` with flags like data:`OP_NO_SSLv3` instead.
@@ -678,7 +719,7 @@ Constants
.. versionadded:: 3.2
- .. deprecated:: 3.5.3
+ .. deprecated:: 3.6
SSLv2 is deprecated
@@ -691,7 +732,7 @@ Constants
.. versionadded:: 3.2
- .. deprecated:: 3.5.3
+ .. deprecated:: 3.6
SSLv3 is deprecated
@@ -751,6 +792,16 @@ Constants
.. versionadded:: 3.3
+.. class:: Options
+
+ :class:`enum.IntFlag` collection of OP_* constants.
+
+.. data:: OP_NO_TICKET
+
+ Prevent client side from requesting a session ticket.
+
+ .. versionadded:: 3.6
+
.. data:: HAS_ALPN
Whether the OpenSSL library has built-in support for the *Application-Layer
@@ -833,6 +884,12 @@ Constants
.. versionadded:: 3.4
+.. class:: AlertDescription
+
+ :class:`enum.IntEnum` collection of ALERT_DESCRIPTION_* constants.
+
+ .. versionadded:: 3.6
+
.. data:: Purpose.SERVER_AUTH
Option for :func:`create_default_context` and
@@ -851,6 +908,12 @@ Constants
.. versionadded:: 3.4
+.. class:: SSLErrorNumber
+
+ :class:`enum.IntEnum` collection of SSL_ERROR_* constants.
+
+ .. versionadded:: 3.6
+
SSL Sockets
-----------
@@ -885,7 +948,7 @@ SSL Sockets
:ref:`notes on non-blocking sockets <ssl-nonblocking>`.
Usually, :class:`SSLSocket` are not created directly, but using the
- :func:`wrap_socket` function or the :meth:`SSLContext.wrap_socket` method.
+ the :meth:`SSLContext.wrap_socket` method.
.. versionchanged:: 3.5
The :meth:`sendfile` method was added.
@@ -895,6 +958,10 @@ SSL Sockets
are received or sent. The socket timeout is now to maximum total duration
of the shutdown.
+ .. deprecated:: 3.6
+ It is deprecated to create a :class:`SSLSocket` instance directly, use
+ :meth:`SSLContext.wrap_socket` to wrap a socket.
+
SSL sockets also have the following additional methods and attributes:
@@ -915,6 +982,9 @@ SSL sockets also have the following additional methods and attributes:
The socket timeout is now to maximum total duration to read up to *len*
bytes.
+ .. deprecated:: 3.6
+ Use :meth:`~SSLSocket.recv` instead of :meth:`~SSLSocket.read`.
+
.. method:: SSLSocket.write(buf)
Write *buf* to the SSL socket and return the number of bytes written. The
@@ -930,6 +1000,9 @@ SSL sockets also have the following additional methods and attributes:
The socket timeout is no more reset each time bytes are received or sent.
The socket timeout is now to maximum total duration to write *buf*.
+ .. deprecated:: 3.6
+ Use :meth:`~SSLSocket.send` instead of :meth:`~SSLSocket.write`.
+
.. note::
The :meth:`~SSLSocket.read` and :meth:`~SSLSocket.write` methods are the
@@ -1127,6 +1200,19 @@ SSL sockets also have the following additional methods and attributes:
.. versionadded:: 3.2
+.. attribute:: SSLSocket.session
+
+ The :class:`SSLSession` for this SSL connection. The session is available
+ for client and server side sockets after the TLS handshake has been
+ performed. For client sockets the session can be set before
+ :meth:`~SSLSocket.do_handshake` has been called to reuse a session.
+
+ .. versionadded:: 3.6
+
+.. attribute:: SSLSocket.session_reused
+
+ .. versionadded:: 3.6
+
SSL Contexts
------------
@@ -1149,9 +1235,16 @@ to speed up repeated connections from the same clients.
:func:`create_default_context` lets the :mod:`ssl` module choose
security settings for a given purpose.
- .. versionchanged:: 3.5.3
+ .. versionchanged:: 3.6
- :data:`PROTOCOL_TLS` is the default value.
+ The context is created with secure default values. The options
+ :data:`OP_NO_COMPRESSION`, :data:`OP_CIPHER_SERVER_PREFERENCE`,
+ :data:`OP_SINGLE_DH_USE`, :data:`OP_SINGLE_ECDH_USE`,
+ :data:`OP_NO_SSLv2` (except for :data:`PROTOCOL_SSLv2`),
+ and :data:`OP_NO_SSLv3` (except for :data:`PROTOCOL_SSLv3`) are
+ set by default. The initial cipher suite list contains only ``HIGH``
+ ciphers, no ``NULL`` ciphers and no ``MD5`` ciphers (except for
+ :data:`PROTOCOL_SSLv2`).
:class:`SSLContext` objects have the following methods and attributes:
@@ -1259,6 +1352,62 @@ to speed up repeated connections from the same clients.
.. versionadded:: 3.4
+.. method:: SSLContext.get_ciphers()
+
+ Get a list of enabled ciphers. The list is in order of cipher priority.
+ See :meth:`SSLContext.set_ciphers`.
+
+ Example::
+
+ >>> ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
+ >>> ctx.set_ciphers('ECDHE+AESGCM:!ECDSA')
+ >>> ctx.get_ciphers() # OpenSSL 1.0.x
+ [{'alg_bits': 256,
+ 'description': 'ECDHE-RSA-AES256-GCM-SHA384 TLSv1.2 Kx=ECDH Au=RSA '
+ 'Enc=AESGCM(256) Mac=AEAD',
+ 'id': 50380848,
+ 'name': 'ECDHE-RSA-AES256-GCM-SHA384',
+ 'protocol': 'TLSv1/SSLv3',
+ 'strength_bits': 256},
+ {'alg_bits': 128,
+ 'description': 'ECDHE-RSA-AES128-GCM-SHA256 TLSv1.2 Kx=ECDH Au=RSA '
+ 'Enc=AESGCM(128) Mac=AEAD',
+ 'id': 50380847,
+ 'name': 'ECDHE-RSA-AES128-GCM-SHA256',
+ 'protocol': 'TLSv1/SSLv3',
+ 'strength_bits': 128}]
+
+ On OpenSSL 1.1 and newer the cipher dict contains additional fields::
+ >>> ctx.get_ciphers() # OpenSSL 1.1+
+ [{'aead': True,
+ 'alg_bits': 256,
+ 'auth': 'auth-rsa',
+ 'description': 'ECDHE-RSA-AES256-GCM-SHA384 TLSv1.2 Kx=ECDH Au=RSA '
+ 'Enc=AESGCM(256) Mac=AEAD',
+ 'digest': None,
+ 'id': 50380848,
+ 'kea': 'kx-ecdhe',
+ 'name': 'ECDHE-RSA-AES256-GCM-SHA384',
+ 'protocol': 'TLSv1.2',
+ 'strength_bits': 256,
+ 'symmetric': 'aes-256-gcm'},
+ {'aead': True,
+ 'alg_bits': 128,
+ 'auth': 'auth-rsa',
+ 'description': 'ECDHE-RSA-AES128-GCM-SHA256 TLSv1.2 Kx=ECDH Au=RSA '
+ 'Enc=AESGCM(128) Mac=AEAD',
+ 'digest': None,
+ 'id': 50380847,
+ 'kea': 'kx-ecdhe',
+ 'name': 'ECDHE-RSA-AES128-GCM-SHA256',
+ 'protocol': 'TLSv1.2',
+ 'strength_bits': 128,
+ 'symmetric': 'aes-128-gcm'}]
+
+ Availability: OpenSSL 1.0.2+
+
+ .. versionadded:: 3.6
+
.. method:: SSLContext.set_default_verify_paths()
Load a set of default "certification authority" (CA) certificates from
@@ -1397,7 +1546,7 @@ to speed up repeated connections from the same clients.
.. method:: SSLContext.wrap_socket(sock, server_side=False, \
do_handshake_on_connect=True, suppress_ragged_eofs=True, \
- server_hostname=None)
+ server_hostname=None, session=None)
Wrap an existing Python socket *sock* and return an :class:`SSLSocket`
object. *sock* must be a :data:`~socket.SOCK_STREAM` socket; other socket
@@ -1414,19 +1563,27 @@ to speed up repeated connections from the same clients.
quite similarly to HTTP virtual hosts. Specifying *server_hostname* will
raise a :exc:`ValueError` if *server_side* is true.
+ *session*, see :attr:`~SSLSocket.session`.
+
.. versionchanged:: 3.5
Always allow a server_hostname to be passed, even if OpenSSL does not
have SNI.
+ .. versionchanged:: 3.6
+ *session* argument was added.
+
.. method:: SSLContext.wrap_bio(incoming, outgoing, server_side=False, \
- server_hostname=None)
+ server_hostname=None, session=None)
Create a new :class:`SSLObject` instance by wrapping the BIO objects
*incoming* and *outgoing*. The SSL routines will read input data from the
incoming BIO and write data to the outgoing BIO.
- The *server_side* and *server_hostname* parameters have the same meaning as
- in :meth:`SSLContext.wrap_socket`.
+ The *server_side*, *server_hostname* and *session* parameters have the
+ same meaning as in :meth:`SSLContext.wrap_socket`.
+
+ .. versionchanged:: 3.6
+ *session* argument was added.
.. method:: SSLContext.session_stats()
@@ -1478,6 +1635,12 @@ to speed up repeated connections from the same clients.
to set options, not to clear them. Attempting to clear an option
(by resetting the corresponding bits) will raise a ``ValueError``.
+ .. versionchanged:: 3.6
+ :attr:`SSLContext.options` returns :class:`Options` flags:
+
+ >>> ssl.create_default_context().options
+ <Options.OP_ALL|OP_NO_SSLv3|OP_NO_SSLv2|OP_NO_COMPRESSION: 2197947391>
+
.. attribute:: SSLContext.protocol
The protocol version chosen when constructing the context. This attribute
@@ -1492,12 +1655,23 @@ to speed up repeated connections from the same clients.
.. versionadded:: 3.4
+ .. versionchanged:: 3.6
+ :attr:`SSLContext.verify_flags` returns :class:`VerifyFlags` flags:
+
+ >>> ssl.create_default_context().verify_flags
+ <VerifyFlags.VERIFY_X509_TRUSTED_FIRST: 32768>
+
.. attribute:: SSLContext.verify_mode
Whether to try to verify other peers' certificates and how to behave
if verification fails. This attribute must be one of
:data:`CERT_NONE`, :data:`CERT_OPTIONAL` or :data:`CERT_REQUIRED`.
+ .. versionchanged:: 3.6
+ :attr:`SSLContext.verify_mode` returns :class:`VerifyMode` enum:
+
+ >>> ssl.create_default_context().verify_mode
+ <VerifyMode.CERT_REQUIRED: 2>
.. index:: single: certificates
@@ -1916,6 +2090,8 @@ provided.
- :attr:`~SSLSocket.context`
- :attr:`~SSLSocket.server_side`
- :attr:`~SSLSocket.server_hostname`
+ - :attr:`~SSLSocket.session`
+ - :attr:`~SSLSocket.session_reused`
- :meth:`~SSLSocket.read`
- :meth:`~SSLSocket.write`
- :meth:`~SSLSocket.getpeercert`
@@ -1997,6 +2173,22 @@ purpose. It wraps an OpenSSL memory BIO (Basic IO) object:
become true after all data currently in the buffer has been read.
+SSL session
+-----------
+
+.. versionadded:: 3.6
+
+.. class:: SSLSession
+
+ Session object used by :attr:`~SSLSocket.session`.
+
+ .. attribute:: id
+ .. attribute:: time
+ .. attribute:: timeout
+ .. attribute:: ticket_lifetime_hint
+ .. attribute:: has_ticket
+
+
.. _ssl-security:
Security considerations
@@ -2063,18 +2255,20 @@ Protocol versions
SSL versions 2 and 3 are considered insecure and are therefore dangerous to
use. If you want maximum compatibility between clients and servers, it is
-recommended to use :const:`PROTOCOL_TLS` as the protocol version and then
-disable SSLv2 and SSLv3 explicitly using the :data:`SSLContext.options`
-attribute::
+recommended to use :const:`PROTOCOL_TLS_CLIENT` or
+:const:`PROTOCOL_TLS_SERVER` as the protocol version. SSLv2 and SSLv3 are
+disabled by default.
+
+ >>> client_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
+ >>> client_context.options |= ssl.OP_NO_TLSv1
+ >>> client_context.options |= ssl.OP_NO_TLSv1_1
- context = ssl.SSLContext(ssl.PROTOCOL_TLS)
- context.options |= ssl.OP_NO_SSLv2
- context.options |= ssl.OP_NO_SSLv3
- context.options |= ssl.OP_NO_TLSv1
- context.options |= ssl.OP_NO_TLSv1_1
The SSL context created above will only allow TLSv1.2 and later (if
-supported by your system) connections.
+supported by your system) connections to a server. :const:`PROTOCOL_TLS_CLIENT`
+implies certificate validation and hostname checks by default. You have to
+load certificates into the context.
+
Cipher selection
''''''''''''''''
@@ -2085,8 +2279,9 @@ enabled when negotiating a SSL session is possible through the
ssl module disables certain weak ciphers by default, but you may want
to further restrict the cipher choice. Be sure to read OpenSSL's documentation
about the `cipher list format <https://www.openssl.org/docs/apps/ciphers.html#CIPHER-LIST-FORMAT>`_.
-If you want to check which ciphers are enabled by a given cipher list, use the
-``openssl ciphers`` command on your system.
+If you want to check which ciphers are enabled by a given cipher list, use
+:meth:`SSLContext.get_ciphers` or the ``openssl ciphers`` command on your
+system.
Multi-processing
^^^^^^^^^^^^^^^^
diff --git a/Doc/library/statistics.rst b/Doc/library/statistics.rst
index 7685621..2aa778c 100644
--- a/Doc/library/statistics.rst
+++ b/Doc/library/statistics.rst
@@ -39,6 +39,7 @@ or sample.
======================= =============================================
:func:`mean` Arithmetic mean ("average") of data.
+:func:`harmonic_mean` Harmonic mean of data.
:func:`median` Median (middle value) of data.
:func:`median_low` Low median of data.
:func:`median_high` High median of data.
@@ -68,8 +69,7 @@ However, for reading convenience, most of the examples show sorted sequences.
.. function:: mean(data)
- Return the sample arithmetic mean of *data*, a sequence or iterator of
- real-valued numbers.
+ Return the sample arithmetic mean of *data* which can be a sequence or iterator.
The arithmetic mean is the sum of the data divided by the number of data
points. It is commonly called "the average", although it is only one of many
@@ -111,10 +111,43 @@ However, for reading convenience, most of the examples show sorted sequences.
``mean(data)`` is equivalent to calculating the true population mean μ.
+.. function:: harmonic_mean(data)
+
+ Return the harmonic mean of *data*, a sequence or iterator of
+ real-valued numbers.
+
+ The harmonic mean, sometimes called the subcontrary mean, is the
+ reciprocal of the arithmetic :func:`mean` of the reciprocals of the
+ data. For example, the harmonic mean of three values *a*, *b* and *c*
+ will be equivalent to ``3/(1/a + 1/b + 1/c)``.
+
+ The harmonic mean is a type of average, a measure of the central
+ location of the data. It is often appropriate when averaging quantities
+ which are rates or ratios, for example speeds. For example:
+
+ Suppose an investor purchases an equal value of shares in each of
+ three companies, with P/E (price/earning) ratios of 2.5, 3 and 10.
+ What is the average P/E ratio for the investor's portfolio?
+
+ .. doctest::
+
+ >>> harmonic_mean([2.5, 3, 10]) # For an equal investment portfolio.
+ 3.6
+
+ Using the arithmetic mean would give an average of about 5.167, which
+ is too high.
+
+ :exc:`StatisticsError` is raised if *data* is empty, or any element
+ is less than zero.
+
+ .. versionadded:: 3.6
+
+
.. function:: median(data)
Return the median (middle value) of numeric data, using the common "mean of
middle two" method. If *data* is empty, :exc:`StatisticsError` is raised.
+ *data* can be a sequence or iterator.
The median is a robust measure of central location, and is less affected by
the presence of outliers in your data. When the number of data points is
@@ -142,7 +175,7 @@ However, for reading convenience, most of the examples show sorted sequences.
.. function:: median_low(data)
Return the low median of numeric data. If *data* is empty,
- :exc:`StatisticsError` is raised.
+ :exc:`StatisticsError` is raised. *data* can be a sequence or iterator.
The low median is always a member of the data set. When the number of data
points is odd, the middle value is returned. When it is even, the smaller of
@@ -162,7 +195,7 @@ However, for reading convenience, most of the examples show sorted sequences.
.. function:: median_high(data)
Return the high median of data. If *data* is empty, :exc:`StatisticsError`
- is raised.
+ is raised. *data* can be a sequence or iterator.
The high median is always a member of the data set. When the number of data
points is odd, the middle value is returned. When it is even, the larger of
@@ -183,7 +216,7 @@ However, for reading convenience, most of the examples show sorted sequences.
Return the median of grouped continuous data, calculated as the 50th
percentile, using interpolation. If *data* is empty, :exc:`StatisticsError`
- is raised.
+ is raised. *data* can be a sequence or iterator.
.. doctest::
diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst
index 71cb7f2..9a4f42c 100644
--- a/Doc/library/stdtypes.rst
+++ b/Doc/library/stdtypes.rst
@@ -1365,6 +1365,11 @@ objects that compare equal might have different :attr:`~range.start`,
The :attr:`~range.start`, :attr:`~range.stop` and :attr:`~range.step`
attributes.
+.. seealso::
+
+ * The `linspace recipe <http://code.activestate.com/recipes/579000/>`_
+ shows how to implement a lazy version of range that suitable for floating
+ point applications.
.. index::
single: string; text sequence type
@@ -1455,8 +1460,8 @@ multiple fragments.
For more information on the ``str`` class and its methods, see
:ref:`textseq` and the :ref:`string-methods` section below. To output
- formatted strings, see the :ref:`formatstrings` section. In addition,
- see the :ref:`stringservices` section.
+ formatted strings, see the :ref:`f-strings` and :ref:`formatstrings`
+ sections. In addition, see the :ref:`stringservices` section.
.. index::
@@ -2060,8 +2065,8 @@ expression support in the :mod:`re` module).
.. index::
single: formatting, string (%)
single: interpolation, string (%)
- single: string; formatting
- single: string; interpolation
+ single: string; formatting, printf
+ single: string; interpolation, printf
single: printf-style formatting
single: sprintf-style formatting
single: % formatting
@@ -2071,9 +2076,10 @@ expression support in the :mod:`re` module).
The formatting operations described here exhibit a variety of quirks that
lead to a number of common errors (such as failing to display tuples and
- dictionaries correctly). Using the newer :meth:`str.format` interface
- helps avoid these errors, and also provides a generally more powerful,
- flexible and extensible approach to formatting text.
+ dictionaries correctly). Using the newer :ref:`formatted
+ string literals <f-strings>` or the :meth:`str.format` interface
+ helps avoid these errors. These alternatives also provide more powerful,
+ flexible and extensible approaches to formatting text.
String objects have one unique built-in operation: the ``%`` operator (modulo).
This is also known as the string *formatting* or *interpolation* operator.
@@ -2630,8 +2636,8 @@ arbitrary binary data.
The prefix(es) to search for may be any :term:`bytes-like object`.
-.. method:: bytes.translate(table[, delete])
- bytearray.translate(table[, delete])
+.. method:: bytes.translate(table, delete=b'')
+ bytearray.translate(table, delete=b'')
Return a copy of the bytes or bytearray object where all bytes occurring in
the optional argument *delete* are removed, and the remaining bytes have
@@ -2647,6 +2653,9 @@ arbitrary binary data.
>>> b'read this short text'.translate(None, b'aeiou')
b'rd ths shrt txt'
+ .. versionchanged:: 3.6
+ *delete* is now supported as a keyword argument.
+
The following methods on bytes and bytearray objects have default behaviours
that assume the use of ASCII compatible binary formats, but can still be used
diff --git a/Doc/library/string.rst b/Doc/library/string.rst
index 05418a9..a0977b6 100644
--- a/Doc/library/string.rst
+++ b/Doc/library/string.rst
@@ -188,7 +188,9 @@ Format String Syntax
The :meth:`str.format` method and the :class:`Formatter` class share the same
syntax for format strings (although in the case of :class:`Formatter`,
-subclasses can define their own format string syntax).
+subclasses can define their own format string syntax). The syntax is
+related to that of :ref:`formatted string literals <f-strings>`, but
+there are differences.
Format strings contain "replacement fields" surrounded by curly braces ``{}``.
Anything that is not contained in braces is considered literal text, which is
@@ -283,7 +285,8 @@ Format Specification Mini-Language
"Format specifications" are used within replacement fields contained within a
format string to define how individual values are presented (see
-:ref:`formatstrings`). They can also be passed directly to the built-in
+:ref:`formatstrings` and :ref:`f-strings`).
+They can also be passed directly to the built-in
:func:`format` function. Each formattable type may define how the format
specification is to be interpreted.
@@ -297,18 +300,20 @@ non-empty format string typically modifies the result.
The general form of a *standard format specifier* is:
.. productionlist:: sf
- format_spec: [[`fill`]`align`][`sign`][#][0][`width`][,][.`precision`][`type`]
+ format_spec: [[`fill`]`align`][`sign`][#][0][`width`][`grouping_option`][.`precision`][`type`]
fill: <any character>
align: "<" | ">" | "=" | "^"
sign: "+" | "-" | " "
width: `integer`
+ grouping_option: "_" | ","
precision: `integer`
type: "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
If a valid *align* value is specified, it can be preceded by a *fill*
character that can be any character and defaults to a space if omitted.
It is not possible to use a literal curly brace ("``{``" or "``}``") as
-the *fill* character when using the :meth:`str.format`
+the *fill* character in a :ref:`formatted string literal
+<f-strings>` or when using the :meth:`str.format`
method. However, it is possible to insert a curly brace
with a nested replacement field. This limitation doesn't
affect the :func:`format` function.
@@ -374,6 +379,16 @@ instead.
.. versionchanged:: 3.1
Added the ``','`` option (see also :pep:`378`).
+The ``'_'`` option signals the use of an underscore for a thousands
+separator for floating point presentation types and for integer
+presentation type ``'d'``. For integer presentation types ``'b'``,
+``'o'``, ``'x'``, and ``'X'``, underscores will be inserted every 4
+digits. For other presentation types, specifying this option is an
+error.
+
+.. versionchanged:: 3.6
+ Added the ``'_'`` option (see also :pep:`515`).
+
*width* is a decimal integer defining the minimum field width. If not
specified, then the field width will be determined by the content.
diff --git a/Doc/library/struct.rst b/Doc/library/struct.rst
index ae2e38f..cc3017b 100644
--- a/Doc/library/struct.rst
+++ b/Doc/library/struct.rst
@@ -216,6 +216,8 @@ platform-dependent.
+--------+--------------------------+--------------------+----------------+------------+
| ``N`` | :c:type:`size_t` | integer | | \(4) |
+--------+--------------------------+--------------------+----------------+------------+
+| ``e`` | \(7) | float | 2 | \(5) |
++--------+--------------------------+--------------------+----------------+------------+
| ``f`` | :c:type:`float` | float | 4 | \(5) |
+--------+--------------------------+--------------------+----------------+------------+
| ``d`` | :c:type:`double` | float | 8 | \(5) |
@@ -230,6 +232,10 @@ platform-dependent.
.. versionchanged:: 3.3
Added support for the ``'n'`` and ``'N'`` formats.
+.. versionchanged:: 3.6
+ Added support for the ``'e'`` format.
+
+
Notes:
(1)
@@ -257,9 +263,10 @@ Notes:
fits your application.
(5)
- For the ``'f'`` and ``'d'`` conversion codes, the packed representation uses
- the IEEE 754 binary32 (for ``'f'``) or binary64 (for ``'d'``) format,
- regardless of the floating-point format used by the platform.
+ For the ``'f'``, ``'d'`` and ``'e'`` conversion codes, the packed
+ representation uses the IEEE 754 binary32, binary64 or binary16 format (for
+ ``'f'``, ``'d'`` or ``'e'`` respectively), regardless of the floating-point
+ format used by the platform.
(6)
The ``'P'`` format character is only available for the native byte ordering
@@ -268,6 +275,16 @@ Notes:
on the host system. The struct module does not interpret this as native
ordering, so the ``'P'`` format is not available.
+(7)
+ The IEEE 754 binary16 "half precision" type was introduced in the 2008
+ revision of the `IEEE 754 standard <ieee 754 standard_>`_. It has a sign
+ bit, a 5-bit exponent and 11-bit precision (with 10 bits explicitly stored),
+ and can represent numbers between approximately ``6.1e-05`` and ``6.5e+04``
+ at full precision. This type is not widely supported by C compilers: on a
+ typical machine, an unsigned short can be used for storage, but not for math
+ operations. See the Wikipedia page on the `half-precision floating-point
+ format <half precision format_>`_ for more information.
+
A format character may be preceded by an integral repeat count. For example,
the format string ``'4h'`` means exactly the same as ``'hhhh'``.
@@ -430,3 +447,7 @@ The :mod:`struct` module also defines the following type:
The calculated size of the struct (and hence of the bytes object produced
by the :meth:`pack` method) corresponding to :attr:`format`.
+
+.. _half precision format: https://en.wikipedia.org/wiki/Half-precision_floating-point_format
+
+.. _ieee 754 standard: https://en.wikipedia.org/wiki/IEEE_floating_point#IEEE_754-2008
diff --git a/Doc/library/subprocess.rst b/Doc/library/subprocess.rst
index c1b28f8..ad2abe8 100644
--- a/Doc/library/subprocess.rst
+++ b/Doc/library/subprocess.rst
@@ -38,7 +38,8 @@ compatibility with older versions, see the :ref:`call-function-trio` section.
.. function:: run(args, *, stdin=None, input=None, stdout=None, stderr=None,\
- shell=False, timeout=None, check=False)
+ shell=False, timeout=None, check=False, \
+ encoding=None, errors=None)
Run the command described by *args*. Wait for command to complete, then
return a :class:`CompletedProcess` instance.
@@ -60,15 +61,20 @@ compatibility with older versions, see the :ref:`call-function-trio` section.
The *input* argument is passed to :meth:`Popen.communicate` and thus to the
subprocess's stdin. If used it must be a byte sequence, or a string if
- ``universal_newlines=True``. When used, the internal :class:`Popen` object
- is automatically created with ``stdin=PIPE``, and the *stdin* argument may
- not be used as well.
+ *encoding* or *errors* is specified or *universal_newlines* is true. When
+ used, the internal :class:`Popen` object is automatically created with
+ ``stdin=PIPE``, and the *stdin* argument may not be used as well.
If *check* is true, and the process exits with a non-zero exit code, a
:exc:`CalledProcessError` exception will be raised. Attributes of that
exception hold the arguments, the exit code, and stdout and stderr if they
were captured.
+ If *encoding* or *errors* are specified, or *universal_newlines* is true,
+ file objects for stdin, stdout and stderr are opened in text mode using the
+ specified *encoding* and *errors* or the :class:`io.TextIOWrapper` default.
+ Otherwise, file objects are opened in binary mode.
+
Examples::
>>> subprocess.run(["ls", "-l"]) # doesn't capture output
@@ -85,6 +91,10 @@ compatibility with older versions, see the :ref:`call-function-trio` section.
.. versionadded:: 3.5
+ .. versionchanged:: 3.6
+
+ Added *encoding* and *errors* parameters
+
.. class:: CompletedProcess
The return value from :func:`run`, representing a process that has finished.
@@ -104,8 +114,8 @@ compatibility with older versions, see the :ref:`call-function-trio` section.
.. attribute:: stdout
Captured stdout from the child process. A bytes sequence, or a string if
- :func:`run` was called with ``universal_newlines=True``. ``None`` if stdout
- was not captured.
+ :func:`run` was called with an encoding or errors. ``None`` if stdout was not
+ captured.
If you ran the process with ``stderr=subprocess.STDOUT``, stdout and
stderr will be combined in this attribute, and :attr:`stderr` will be
@@ -114,8 +124,8 @@ compatibility with older versions, see the :ref:`call-function-trio` section.
.. attribute:: stderr
Captured stderr from the child process. A bytes sequence, or a string if
- :func:`run` was called with ``universal_newlines=True``. ``None`` if stderr
- was not captured.
+ :func:`run` was called with an encoding or errors. ``None`` if stderr was not
+ captured.
.. method:: check_returncode()
@@ -249,19 +259,22 @@ default values. The arguments that are most commonly needed are:
.. index::
single: universal newlines; subprocess module
- If *universal_newlines* is ``False`` the file objects *stdin*, *stdout* and
- *stderr* will be opened as binary streams, and no line ending conversion is
- done.
+ If *encoding* or *errors* are specified, or *universal_newlines* is true,
+ the file objects *stdin*, *stdout* and *stderr* will be opened in text
+ mode using the *encoding* and *errors* specified in the call or the
+ defaults for :class:`io.TextIOWrapper`.
+
+ For *stdin*, line ending characters ``'\n'`` in the input will be converted
+ to the default line separator :data:`os.linesep`. For *stdout* and *stderr*,
+ all line endings in the output will be converted to ``'\n'``. For more
+ information see the documentation of the :class:`io.TextIOWrapper` class
+ when the *newline* argument to its constructor is ``None``.
- If *universal_newlines* is ``True``, these file objects
- will be opened as text streams in :term:`universal newlines` mode
- using the encoding returned by :func:`locale.getpreferredencoding(False)
- <locale.getpreferredencoding>`. For *stdin*, line ending characters
- ``'\n'`` in the input will be converted to the default line separator
- :data:`os.linesep`. For *stdout* and *stderr*, all line endings in the
- output will be converted to ``'\n'``. For more information see the
- documentation of the :class:`io.TextIOWrapper` class when the *newline*
- argument to its constructor is ``None``.
+ If text mode is not used, *stdin*, *stdout* and *stderr* will be opened as
+ binary streams. No encoding or line ending conversion is performed.
+
+ .. versionadded:: 3.6
+ Added *encoding* and *errors* parameters.
.. note::
@@ -306,7 +319,8 @@ functions.
stderr=None, preexec_fn=None, close_fds=True, shell=False, \
cwd=None, env=None, universal_newlines=False, \
startupinfo=None, creationflags=0, restore_signals=True, \
- start_new_session=False, pass_fds=())
+ start_new_session=False, pass_fds=(), *, \
+ encoding=None, errors=None)
Execute a child program in a new process. On POSIX, the class uses
:meth:`os.execvp`-like behavior to execute the child program. On Windows,
@@ -482,10 +496,14 @@ functions.
.. _side-by-side assembly: https://en.wikipedia.org/wiki/Side-by-Side_Assembly
- If *universal_newlines* is ``True``, the file objects *stdin*, *stdout*
- and *stderr* are opened as text streams in universal newlines mode, as
- described above in :ref:`frequently-used-arguments`, otherwise they are
- opened as binary streams.
+ If *encoding* or *errors* are specified, the file objects *stdin*, *stdout*
+ and *stderr* are opened in text mode with the specified encoding and
+ *errors*, as described above in :ref:`frequently-used-arguments`. If
+ *universal_newlines* is ``True``, they are opened in text mode with default
+ encoding. Otherwise, they are opened as binary streams.
+
+ .. versionadded:: 3.6
+ *encoding* and *errors* were added.
If given, *startupinfo* will be a :class:`STARTUPINFO` object, which is
passed to the underlying ``CreateProcess`` function.
@@ -502,6 +520,10 @@ functions.
.. versionchanged:: 3.2
Added context manager support.
+ .. versionchanged:: 3.6
+ Popen destructor now emits a :exc:`ResourceWarning` warning if the child
+ process is still running.
+
Exceptions
^^^^^^^^^^
@@ -597,11 +619,12 @@ Instances of the :class:`Popen` class have the following methods:
Interact with process: Send data to stdin. Read data from stdout and stderr,
until end-of-file is reached. Wait for process to terminate. The optional
*input* argument should be data to be sent to the child process, or
- ``None``, if no data should be sent to the child. The type of *input*
- must be bytes or, if *universal_newlines* was ``True``, a string.
+ ``None``, if no data should be sent to the child. If streams were opened in
+ text mode, *input* must be a string. Otherwise, it must be bytes.
:meth:`communicate` returns a tuple ``(stdout_data, stderr_data)``.
- The data will be bytes or, if *universal_newlines* was ``True``, strings.
+ The data will be strings if streams were opened in text mode; otherwise,
+ bytes.
Note that if you want to send data to the process's stdin, you need to create
the Popen object with ``stdin=PIPE``. Similarly, to get anything other than
@@ -668,28 +691,30 @@ The following attributes are also available:
.. attribute:: Popen.stdin
If the *stdin* argument was :data:`PIPE`, this attribute is a writeable
- stream object as returned by :func:`open`. If the *universal_newlines*
- argument was ``True``, the stream is a text stream, otherwise it is a byte
- stream. If the *stdin* argument was not :data:`PIPE`, this attribute is
- ``None``.
+ stream object as returned by :func:`open`. If the *encoding* or *errors*
+ arguments were specified or the *universal_newlines* argument was ``True``,
+ the stream is a text stream, otherwise it is a byte stream. If the *stdin*
+ argument was not :data:`PIPE`, this attribute is ``None``.
.. attribute:: Popen.stdout
If the *stdout* argument was :data:`PIPE`, this attribute is a readable
stream object as returned by :func:`open`. Reading from the stream provides
- output from the child process. If the *universal_newlines* argument was
- ``True``, the stream is a text stream, otherwise it is a byte stream. If the
- *stdout* argument was not :data:`PIPE`, this attribute is ``None``.
+ output from the child process. If the *encoding* or *errors* arguments were
+ specified or the *universal_newlines* argument was ``True``, the stream is a
+ text stream, otherwise it is a byte stream. If the *stdout* argument was not
+ :data:`PIPE`, this attribute is ``None``.
.. attribute:: Popen.stderr
If the *stderr* argument was :data:`PIPE`, this attribute is a readable
stream object as returned by :func:`open`. Reading from the stream provides
- error output from the child process. If the *universal_newlines* argument was
- ``True``, the stream is a text stream, otherwise it is a byte stream. If the
- *stderr* argument was not :data:`PIPE`, this attribute is ``None``.
+ error output from the child process. If the *encoding* or *errors* arguments
+ were specified or the *universal_newlines* argument was ``True``, the stream
+ is a text stream, otherwise it is a byte stream. If the *stderr* argument was
+ not :data:`PIPE`, this attribute is ``None``.
.. warning::
@@ -882,7 +907,9 @@ calls these functions.
*timeout* was added.
-.. function:: check_output(args, *, stdin=None, stderr=None, shell=False, universal_newlines=False, timeout=None)
+.. function:: check_output(args, *, stdin=None, stderr=None, shell=False, \
+ encoding=None, errors=None, \
+ universal_newlines=False, timeout=None)
Run command with arguments and return its output.
@@ -1138,7 +1165,7 @@ handling consistency are valid for these functions.
Return ``(status, output)`` of executing *cmd* in a shell.
Execute the string *cmd* in a shell with :meth:`Popen.check_output` and
- return a 2-tuple ``(status, output)``. Universal newlines mode is used;
+ return a 2-tuple ``(status, output)``. The locale encoding is used;
see the notes on :ref:`frequently-used-arguments` for more details.
A trailing newline is stripped from the output.
diff --git a/Doc/library/sys.rst b/Doc/library/sys.rst
index d28ea5d..dd51ffd 100644
--- a/Doc/library/sys.rst
+++ b/Doc/library/sys.rst
@@ -269,6 +269,11 @@ always available.
the process when called from the main thread, and the exception is not
intercepted.
+ .. versionchanged:: 3.6
+ If an error occurs in the cleanup after the Python interpreter
+ has caught :exc:`SystemExit` (such as an error flushing buffered data
+ in the standard streams), the exit status is changed to 120.
+
.. data:: flags
@@ -423,25 +428,42 @@ always available.
.. function:: getfilesystemencoding()
- Return the name of the encoding used to convert Unicode filenames into
- system file names. The result value depends on the operating system:
+ Return the name of the encoding used to convert between Unicode
+ filenames and bytes filenames. For best compatibility, str should be
+ used for filenames in all cases, although representing filenames as bytes
+ is also supported. Functions accepting or returning filenames should support
+ either str or bytes and internally convert to the system's preferred
+ representation.
- * On Mac OS X, the encoding is ``'utf-8'``.
+ This encoding is always ASCII-compatible.
- * On Unix, the encoding is the user's preference according to the result of
- nl_langinfo(CODESET).
+ :func:`os.fsencode` and :func:`os.fsdecode` should be used to ensure that
+ the correct encoding and errors mode are used.
+
+ * On Mac OS X, the encoding is ``'utf-8'``.
- * On Windows NT+, file names are Unicode natively, so no conversion is
- performed. :func:`getfilesystemencoding` still returns ``'mbcs'``, as
- this is the encoding that applications should use when they explicitly
- want to convert Unicode strings to byte strings that are equivalent when
- used as file names.
+ * On Unix, the encoding is the locale encoding.
- * On Windows 9x, the encoding is ``'mbcs'``.
+ * On Windows, the encoding may be ``'utf-8'`` or ``'mbcs'``, depending
+ on user configuration.
.. versionchanged:: 3.2
:func:`getfilesystemencoding` result cannot be ``None`` anymore.
+ .. versionchanged:: 3.6
+ Windows is no longer guaranteed to return ``'mbcs'``. See :pep:`529`
+ and :func:`_enablelegacywindowsfsencoding` for more information.
+
+.. function:: getfilesystemencodeerrors()
+
+ Return the name of the error mode used to convert between Unicode filenames
+ and bytes filenames. The encoding name is returned from
+ :func:`getfilesystemencoding`.
+
+ :func:`os.fsencode` and :func:`os.fsdecode` should be used to ensure that
+ the correct encoding and errors mode are used.
+
+ .. versionadded:: 3.6
.. function:: getrefcount(object)
@@ -530,26 +552,15 @@ always available.
Return a named tuple describing the Windows version
currently running. The named elements are *major*, *minor*,
*build*, *platform*, *service_pack*, *service_pack_minor*,
- *service_pack_major*, *suite_mask*, and *product_type*.
- *service_pack* contains a string while all other values are
+ *service_pack_major*, *suite_mask*, *product_type* and
+ *platform_version*. *service_pack* contains a string,
+ *platform_version* a 3-tuple and all other values are
integers. The components can also be accessed by name, so
``sys.getwindowsversion()[0]`` is equivalent to
``sys.getwindowsversion().major``. For compatibility with prior
versions, only the first 5 elements are retrievable by indexing.
- *platform* may be one of the following values:
-
- +-----------------------------------------+-------------------------+
- | Constant | Platform |
- +=========================================+=========================+
- | :const:`0 (VER_PLATFORM_WIN32s)` | Win32s on Windows 3.1 |
- +-----------------------------------------+-------------------------+
- | :const:`1 (VER_PLATFORM_WIN32_WINDOWS)` | Windows 95/98/ME |
- +-----------------------------------------+-------------------------+
- | :const:`2 (VER_PLATFORM_WIN32_NT)` | Windows NT/2000/XP/x64 |
- +-----------------------------------------+-------------------------+
- | :const:`3 (VER_PLATFORM_WIN32_CE)` | Windows CE |
- +-----------------------------------------+-------------------------+
+ *platform* will be :const:`2 (VER_PLATFORM_WIN32_NT)`.
*product_type* may be one of the following values:
@@ -565,17 +576,41 @@ always available.
| | a domain controller. |
+---------------------------------------+---------------------------------+
-
This function wraps the Win32 :c:func:`GetVersionEx` function; see the
Microsoft documentation on :c:func:`OSVERSIONINFOEX` for more information
about these fields.
+ *platform_version* returns the accurate major version, minor version and
+ build number of the current operating system, rather than the version that
+ is being emulated for the process. It is intended for use in logging rather
+ than for feature detection.
+
Availability: Windows.
.. versionchanged:: 3.2
Changed to a named tuple and added *service_pack_minor*,
*service_pack_major*, *suite_mask*, and *product_type*.
+ .. versionchanged:: 3.6
+ Added *platform_version*
+
+
+.. function:: get_asyncgen_hooks()
+
+ Returns an *asyncgen_hooks* object, which is similar to a
+ :class:`~collections.namedtuple` of the form `(firstiter, finalizer)`,
+ where *firstiter* and *finalizer* are expected to be either ``None`` or
+ functions which take an :term:`asynchronous generator iterator` as an
+ argument, and are used to schedule finalization of an asychronous
+ generator by an event loop.
+
+ .. versionadded:: 3.6
+ See :pep:`525` for more details.
+
+ .. note::
+ This function has been added on a provisional basis (see :pep:`411`
+ for details.)
+
.. function:: get_coroutine_wrapper()
@@ -1081,17 +1116,23 @@ always available.
implementation platform, rather than part of the language definition, and
thus may not be available in all Python implementations.
+.. function:: set_asyncgen_hooks(firstiter, finalizer)
-.. function:: settscdump(on_flag)
+ Accepts two optional keyword arguments which are callables that accept an
+ :term:`asynchronous generator iterator` as an argument. The *firstiter*
+ callable will be called when an asynchronous generator is iterated for the
+ first time. The *finalizer* will be called when an asynchronous generator
+ is about to be garbage collected.
- Activate dumping of VM measurements using the Pentium timestamp counter, if
- *on_flag* is true. Deactivate these dumps if *on_flag* is off. The function is
- available only if Python was compiled with ``--with-tsc``. To understand
- the output of this dump, read :file:`Python/ceval.c` in the Python sources.
+ .. versionadded:: 3.6
+ See :pep:`525` for more details, and for a reference example of a
+ *finalizer* method see the implementation of
+ ``asyncio.Loop.shutdown_asyncgens`` in
+ :source:`Lib/asyncio/base_events.py`
- .. impl-detail::
- This function is intimately bound to CPython implementation details and
- thus not likely to be implemented elsewhere.
+ .. note::
+ This function has been added on a provisional basis (see :pep:`411`
+ for details.)
.. function:: set_coroutine_wrapper(wrapper)
@@ -1133,6 +1174,18 @@ always available.
This function has been added on a provisional basis (see :pep:`411`
for details.) Use it only for debugging purposes.
+.. function:: _enablelegacywindowsfsencoding()
+
+ Changes the default filesystem encoding and errors mode to 'mbcs' and
+ 'replace' respectively, for consistency with versions of Python prior to 3.6.
+
+ This is equivalent to defining the :envvar:`PYTHONLEGACYWINDOWSFSENCODING`
+ environment variable before launching Python.
+
+ Availability: Windows
+
+ .. versionadded:: 3.6
+ See :pep:`529` for more details.
.. data:: stdin
stdout
diff --git a/Doc/library/sysconfig.rst b/Doc/library/sysconfig.rst
index 02aaab3..08b74a9 100644
--- a/Doc/library/sysconfig.rst
+++ b/Doc/library/sysconfig.rst
@@ -164,7 +164,7 @@ Other functions
.. function:: get_python_version()
Return the ``MAJOR.MINOR`` Python version number as a string. Similar to
- ``sys.version[:3]``.
+ ``'%d.%d' % sys.version_info[:2]``.
.. function:: get_platform()
diff --git a/Doc/library/telnetlib.rst b/Doc/library/telnetlib.rst
index b950e41..f9c5153 100644
--- a/Doc/library/telnetlib.rst
+++ b/Doc/library/telnetlib.rst
@@ -43,6 +43,17 @@ Character), EL (Erase Line), GA (Go Ahead), SB (Subnegotiation Begin).
:exc:`EOFError` when the end of the connection is read, because they can return
an empty string for other reasons. See the individual descriptions below.
+ A :class:`Telnet` object is a context manager and can be used in a
+ :keyword:`with` statement. When the :keyword:`with` block ends, the
+ :meth:`close` method is called::
+
+ >>> from telnetlib import Telnet
+ >>> with Telnet('localhost', 23) as tn:
+ ... tn.interact()
+ ...
+
+ .. versionchanged:: 3.6 Context manager support added
+
.. seealso::
diff --git a/Doc/library/test.rst b/Doc/library/test.rst
index 59577f0..fab3e1f 100644
--- a/Doc/library/test.rst
+++ b/Doc/library/test.rst
@@ -582,6 +582,48 @@ The :mod:`test.support` module defines the following functions:
.. versionadded:: 3.5
+.. function:: check__all__(test_case, module, name_of_module=None, extra=(), blacklist=())
+
+ Assert that the ``__all__`` variable of *module* contains all public names.
+
+ The module's public names (its API) are detected automatically
+ based on whether they match the public name convention and were defined in
+ *module*.
+
+ The *name_of_module* argument can specify (as a string or tuple thereof) what
+ module(s) an API could be defined in in order to be detected as a public
+ API. One case for this is when *module* imports part of its public API from
+ other modules, possibly a C backend (like ``csv`` and its ``_csv``).
+
+ The *extra* argument can be a set of names that wouldn't otherwise be automatically
+ detected as "public", like objects without a proper ``__module__``
+ attribute. If provided, it will be added to the automatically detected ones.
+
+ The *blacklist* argument can be a set of names that must not be treated as part of
+ the public API even though their names indicate otherwise.
+
+ Example use::
+
+ import bar
+ import foo
+ import unittest
+ from test import support
+
+ class MiscTestCase(unittest.TestCase):
+ def test__all__(self):
+ support.check__all__(self, foo)
+
+ class OtherTestCase(unittest.TestCase):
+ def test__all__(self):
+ extra = {'BAR_CONST', 'FOO_CONST'}
+ blacklist = {'baz'} # Undocumented name.
+ # bar imports part of its API from _bar.
+ support.check__all__(self, bar, ('bar', '_bar'),
+ extra=extra, blacklist=blacklist)
+
+ .. versionadded:: 3.6
+
+
The :mod:`test.support` module defines the following classes:
.. class:: TransientResource(exc, **kwargs)
diff --git a/Doc/library/time.rst b/Doc/library/time.rst
index e6626f2..ae17f6f 100644
--- a/Doc/library/time.rst
+++ b/Doc/library/time.rst
@@ -83,6 +83,10 @@ An explanation of some terminology and conventions is in order.
and :attr:`tm_zone` attributes when platform supports corresponding
``struct tm`` members.
+ .. versionchanged:: 3.6
+ The :class:`struct_time` attributes :attr:`tm_gmtoff` and :attr:`tm_zone`
+ are now available on all platforms.
+
* Use the following functions to convert between time representations:
+-------------------------+-------------------------+-------------------------+
@@ -566,10 +570,6 @@ The module defines the following functions and data items:
:class:`struct_time`, or having elements of the wrong type, a
:exc:`TypeError` is raised.
- .. versionchanged:: 3.3
- :attr:`tm_gmtoff` and :attr:`tm_zone` attributes are available on platforms
- with C library supporting the corresponding fields in ``struct tm``.
-
.. function:: time()
Return the time in seconds since the epoch as a floating point number.
@@ -637,11 +637,11 @@ The module defines the following functions and data items:
it is possible to refer to February 29.
:samp:`M{m}.{n}.{d}`
- The *d*'th day (0 <= *d* <= 6) or week *n* of month *m* of the year (1
+ The *d*'th day (0 <= *d* <= 6) of week *n* of month *m* of the year (1
<= *n* <= 5, 1 <= *m* <= 12, where week 5 means "the last *d* day in
month *m*" which may occur in either the fourth or the fifth
week). Week 1 is the first week in which the *d*'th day occurs. Day
- zero is Sunday.
+ zero is a Sunday.
``time`` has the same format as ``offset`` except that no leading sign
('-' or '+') is allowed. The default, if time is not given, is 02:00:00.
diff --git a/Doc/library/timeit.rst b/Doc/library/timeit.rst
index 57a4834..3b77276 100644
--- a/Doc/library/timeit.rst
+++ b/Doc/library/timeit.rst
@@ -100,8 +100,8 @@ The module defines three convenience functions and a public class:
can be controlled by passing a namespace to *globals*.
To measure the execution time of the first statement, use the :meth:`.timeit`
- method. The :meth:`.repeat` method is a convenience to call :meth:`.timeit`
- multiple times and return a list of results.
+ method. The :meth:`.repeat` and :meth:`.autorange` methods are convenience
+ methods to call :meth:`.timeit` multiple times.
The execution time of *setup* is excluded from the overall timed execution run.
@@ -134,6 +134,23 @@ The module defines three convenience functions and a public class:
timeit.Timer('for i in range(10): oct(i)', 'gc.enable()').timeit()
+ .. method:: Timer.autorange(callback=None)
+
+ Automatically determine how many times to call :meth:`.timeit`.
+
+ This is a convenience function that calls :meth:`.timeit` repeatedly
+ so that the total time >= 0.2 second, returning the eventual
+ (number of loops, time taken for that number of loops). It calls
+ :meth:`.timeit` with *number* set to successive powers of ten (10,
+ 100, 1000, ...) up to a maximum of one billion, until the time taken
+ is at least 0.2 second, or the maximum is reached.
+
+ If *callback* is given and is not ``None``, it will be called after
+ each trial with two arguments: ``callback(number, time_taken)``.
+
+ .. versionadded:: 3.6
+
+
.. method:: Timer.repeat(repeat=3, number=1000000)
Call :meth:`.timeit` a few times.
diff --git a/Doc/library/tkinter.tix.rst b/Doc/library/tkinter.tix.rst
index 41f20dd..11ed755 100644
--- a/Doc/library/tkinter.tix.rst
+++ b/Doc/library/tkinter.tix.rst
@@ -10,6 +10,10 @@
.. index:: single: Tix
+.. deprecated:: 3.6
+ This Tk extension is unmaintained and should not be used in new code. Use
+ :mod:`tkinter.ttk` instead.
+
--------------
The :mod:`tkinter.tix` (Tk Interface Extension) module provides an additional
diff --git a/Doc/library/traceback.rst b/Doc/library/traceback.rst
index 3c1d9bb..066ee96 100644
--- a/Doc/library/traceback.rst
+++ b/Doc/library/traceback.rst
@@ -291,6 +291,20 @@ capture data for later printing in a lightweight fashion.
of tuples. Each tuple should be a 4-tuple with filename, lineno, name,
line as the elements.
+ .. method:: format()
+
+ Returns a list of strings ready for printing. Each string in the
+ resulting list corresponds to a single frame from the stack.
+ Each string ends in a newline; the strings may contain internal
+ newlines as well, for those items with source text lines.
+
+ For long sequences of the same frame and line, the first few
+ repetitions are shown, followed by a summary line stating the exact
+ number of further repetitions.
+
+ .. versionchanged:: 3.6
+ Long sequences of repeated frames are now abbreviated.
+
:class:`FrameSummary` Objects
-----------------------------
diff --git a/Doc/library/tracemalloc.rst b/Doc/library/tracemalloc.rst
index 2c8ad0e..e165669 100644
--- a/Doc/library/tracemalloc.rst
+++ b/Doc/library/tracemalloc.rst
@@ -359,10 +359,32 @@ Functions
See also the :func:`get_object_traceback` function.
+DomainFilter
+^^^^^^^^^^^^
+
+.. class:: DomainFilter(inclusive: bool, domain: int)
+
+ Filter traces of memory blocks by their address space (domain).
+
+ .. versionadded:: 3.6
+
+ .. attribute:: inclusive
+
+ If *inclusive* is ``True`` (include), match memory blocks allocated
+ in the address space :attr:`domain`.
+
+ If *inclusive* is ``False`` (exclude), match memory blocks not allocated
+ in the address space :attr:`domain`.
+
+ .. attribute:: domain
+
+ Address space of a memory block (``int``). Read-only property.
+
+
Filter
^^^^^^
-.. class:: Filter(inclusive: bool, filename_pattern: str, lineno: int=None, all_frames: bool=False)
+.. class:: Filter(inclusive: bool, filename_pattern: str, lineno: int=None, all_frames: bool=False, domain: int=None)
Filter on traces of memory blocks.
@@ -382,9 +404,17 @@ Filter
.. versionchanged:: 3.5
The ``'.pyo'`` file extension is no longer replaced with ``'.py'``.
+ .. versionchanged:: 3.6
+ Added the :attr:`domain` attribute.
+
+
+ .. attribute:: domain
+
+ Address space of a memory block (``int`` or ``None``).
+
.. attribute:: inclusive
- If *inclusive* is ``True`` (include), only trace memory blocks allocated
+ If *inclusive* is ``True`` (include), only match memory blocks allocated
in a file with a name matching :attr:`filename_pattern` at line number
:attr:`lineno`.
@@ -399,7 +429,7 @@ Filter
.. attribute:: filename_pattern
- Filename pattern of the filter (``str``).
+ Filename pattern of the filter (``str``). Read-only property.
.. attribute:: all_frames
@@ -462,14 +492,17 @@ Snapshot
.. method:: filter_traces(filters)
Create a new :class:`Snapshot` instance with a filtered :attr:`traces`
- sequence, *filters* is a list of :class:`Filter` instances. If *filters*
- is an empty list, return a new :class:`Snapshot` instance with a copy of
- the traces.
+ sequence, *filters* is a list of :class:`DomainFilter` and
+ :class:`Filter` instances. If *filters* is an empty list, return a new
+ :class:`Snapshot` instance with a copy of the traces.
All inclusive filters are applied at once, a trace is ignored if no
inclusive filters match it. A trace is ignored if at least one exclusive
filter matches it.
+ .. versionchanged:: 3.6
+ :class:`DomainFilter` instances are now also accepted in *filters*.
+
.. classmethod:: load(filename)
diff --git a/Doc/library/types.rst b/Doc/library/types.rst
index 118bc4c..0c5619c 100644
--- a/Doc/library/types.rst
+++ b/Doc/library/types.rst
@@ -55,6 +55,12 @@ Dynamic Type Creation
.. versionadded:: 3.3
+ .. versionchanged:: 3.6
+
+ The default value for the ``namespace`` element of the returned
+ tuple has changed. Now an insertion-order-preserving mapping is
+ used when the metaclass does not have a ``__prepare__`` method,
+
.. seealso::
:ref:`metaclasses`
@@ -98,6 +104,14 @@ Standard names are defined for the following types:
.. versionadded:: 3.5
+.. data:: AsyncGeneratorType
+
+ The type of :term:`asynchronous generator`-iterator objects, created by
+ asynchronous generator functions.
+
+ .. versionadded:: 3.6
+
+
.. data:: CodeType
.. index:: builtin: compile
diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst
index f89d886..cd59d10 100644
--- a/Doc/library/typing.rst
+++ b/Doc/library/typing.rst
@@ -8,10 +8,17 @@
**Source code:** :source:`Lib/typing.py`
+.. note::
+
+ The typing module has been included in the standard library on a
+ :term:`provisional basis <provisional api>`. New features might
+ be added and API may change even between minor releases if deemed
+ necessary by the core developers.
+
--------------
-This module supports type hints as specified by :pep:`484`. The most
-fundamental support consists of the types :data:`Any`, :data:`Union`,
+This module supports type hints as specified by :pep:`484` and :pep:`526`.
+The most fundamental support consists of the types :data:`Any`, :data:`Union`,
:data:`Tuple`, :data:`Callable`, :class:`TypeVar`, and
:class:`Generic`. For full specification please see :pep:`484`. For
a simplified introduction to type hints see :pep:`483`.
@@ -523,7 +530,13 @@ The module defines the following classes, functions and decorators:
An alias to :class:`collections.abc.Sized`
-.. class:: AbstractSet(Sized, Iterable[T_co], Container[T_co])
+.. class:: Collection(Sized, Iterable[T_co], Container[T_co])
+
+ A generic version of :class:`collections.abc.Collection`
+
+ .. versionadded:: 3.6
+
+.. class:: AbstractSet(Sized, Collection[T_co])
A generic version of :class:`collections.abc.Set`.
@@ -531,7 +544,7 @@ The module defines the following classes, functions and decorators:
A generic version of :class:`collections.abc.MutableSet`.
-.. class:: Mapping(Sized, Iterable[KT], Container[KT], Generic[VT_co])
+.. class:: Mapping(Sized, Collection[KT], Generic[VT_co])
A generic version of :class:`collections.abc.Mapping`.
@@ -539,7 +552,7 @@ The module defines the following classes, functions and decorators:
A generic version of :class:`collections.abc.MutableMapping`.
-.. class:: Sequence(Sized, Iterable[T_co], Container[T_co])
+.. class:: Sequence(Reversible[T_co], Collection[T_co])
A generic version of :class:`collections.abc.Sequence`.
@@ -627,6 +640,12 @@ The module defines the following classes, functions and decorators:
A generic version of :class:`collections.abc.AsyncIterator`.
+.. class:: ContextManager(Generic[T_co])
+
+ A generic version of :class:`contextlib.AbstractContextManager`.
+
+ .. versionadded:: 3.6
+
.. class:: Dict(dict, MutableMapping[KT, VT])
A generic version of :class:`dict`.
@@ -702,23 +721,32 @@ The module defines the following classes, functions and decorators:
``Pattern[str]``, ``Pattern[bytes]``, ``Match[str]``, or
``Match[bytes]``.
-.. function:: NamedTuple(typename, fields)
+.. class:: NamedTuple
Typed version of namedtuple.
Usage::
- Employee = typing.NamedTuple('Employee', [('name', str), ('id', int)])
+ class Employee(NamedTuple):
+ name: str
+ id: int
This is equivalent to::
Employee = collections.namedtuple('Employee', ['name', 'id'])
- The resulting class has one extra attribute: _field_types,
+ The resulting class has one extra attribute: ``_field_types``,
giving a dict mapping field names to types. (The field names
- are in the _fields attribute, which is part of the namedtuple
+ are in the ``_fields`` attribute, which is part of the namedtuple
API.)
+ Backward-compatible usage::
+
+ Employee = NamedTuple('Employee', [('name', str), ('id', int)])
+
+ .. versionchanged:: 3.6
+ Added support for :pep:`526` variable annotation syntax.
+
.. function:: NewType(typ)
A helper function to indicate a distinct types to a typechecker,
@@ -888,8 +916,8 @@ The module defines the following classes, functions and decorators:
and should not be set on instances of that class. Usage::
class Starship:
- stats = {} # type: ClassVar[Dict[str, int]] # class variable
- damage = 10 # type: int # instance variable
+ stats: ClassVar[Dict[str, int]] = {} # class variable
+ damage: int = 10 # instance variable
:data:`ClassVar` accepts only types and cannot be further subscribed.
diff --git a/Doc/library/undoc.rst b/Doc/library/undoc.rst
index 20830e2..2444080 100644
--- a/Doc/library/undoc.rst
+++ b/Doc/library/undoc.rst
@@ -20,7 +20,7 @@ These modules are used to implement the :mod:`os.path` module, and are not
documented beyond this mention. There's little need to document these.
:mod:`ntpath`
- --- Implementation of :mod:`os.path` on Win32, Win64, and WinCE platforms.
+ --- Implementation of :mod:`os.path` on Win32 and Win64 platforms.
:mod:`posixpath`
--- Implementation of :mod:`os.path` on POSIX.
diff --git a/Doc/library/unicodedata.rst b/Doc/library/unicodedata.rst
index 6cd8132..6431809 100644
--- a/Doc/library/unicodedata.rst
+++ b/Doc/library/unicodedata.rst
@@ -17,8 +17,8 @@
This module provides access to the Unicode Character Database (UCD) which
defines character properties for all Unicode characters. The data contained in
-this database is compiled from the `UCD version 8.0.0
-<http://www.unicode.org/Public/8.0.0/ucd>`_.
+this database is compiled from the `UCD version 9.0.0
+<http://www.unicode.org/Public/9.0.0/ucd>`_.
The module uses the same names and symbols as defined by Unicode
Standard Annex #44, `"Unicode Character Database"
@@ -168,6 +168,6 @@ Examples:
.. rubric:: Footnotes
-.. [#] http://www.unicode.org/Public/8.0.0/ucd/NameAliases.txt
+.. [#] http://www.unicode.org/Public/9.0.0/ucd/NameAliases.txt
-.. [#] http://www.unicode.org/Public/8.0.0/ucd/NamedSequences.txt
+.. [#] http://www.unicode.org/Public/9.0.0/ucd/NamedSequences.txt
diff --git a/Doc/library/unittest.mock.rst b/Doc/library/unittest.mock.rst
index 4b9dac4..c6d0ec9 100644
--- a/Doc/library/unittest.mock.rst
+++ b/Doc/library/unittest.mock.rst
@@ -262,6 +262,34 @@ the *new_callable* argument to :func:`patch`.
used to set attributes on the mock after it is created. See the
:meth:`configure_mock` method for details.
+ .. method:: assert_called(*args, **kwargs)
+
+ Assert that the mock was called at least once.
+
+ >>> mock = Mock()
+ >>> mock.method()
+ <Mock name='mock.method()' id='...'>
+ >>> mock.method.assert_called()
+
+ .. versionadded:: 3.6
+
+ .. method:: assert_called_once(*args, **kwargs)
+
+ Assert that the mock was called exactly once.
+
+ >>> mock = Mock()
+ >>> mock.method()
+ <Mock name='mock.method()' id='...'>
+ >>> mock.method.assert_called_once()
+ >>> mock.method()
+ <Mock name='mock.method()' id='...'>
+ >>> mock.method.assert_called_once()
+ Traceback (most recent call last):
+ ...
+ AssertionError: Expected 'method' to have been called once. Called 2 times.
+
+ .. versionadded:: 3.6
+
.. method:: assert_called_with(*args, **kwargs)
@@ -339,7 +367,7 @@ the *new_callable* argument to :func:`patch`.
.. versionadded:: 3.5
- .. method:: reset_mock()
+ .. method:: reset_mock(*, return_value=False, side_effect=False)
The reset_mock method resets all the call attributes on a mock object:
@@ -351,12 +379,20 @@ the *new_callable* argument to :func:`patch`.
>>> mock.called
False
+ .. versionchanged:: 3.6
+ Added two keyword only argument to the reset_mock function.
+
This can be useful where you want to make a series of assertions that
reuse the same object. Note that :meth:`reset_mock` *doesn't* clear the
return value, :attr:`side_effect` or any child attributes you have
- set using normal assignment. Child mocks and the return value mock
+ set using normal assignment by default. In case you want to reset
+ *return_value* or :attr:`side_effect`, then pass the corresponding
+ parameter as ``True``. Child mocks and the return value mock
(if any) are reset as well.
+ .. note:: *return_value*, and :attr:`side_effect` are keyword only
+ argument.
+
.. method:: mock_add_spec(spec, spec_set=False)
diff --git a/Doc/library/unittest.rst b/Doc/library/unittest.rst
index 76008c4..92e567d 100644
--- a/Doc/library/unittest.rst
+++ b/Doc/library/unittest.rst
@@ -1391,9 +1391,9 @@ Test cases
Add a function to be called after :meth:`tearDown` to cleanup resources
used during the test. Functions will be called in reverse order to the
- order they are added (LIFO). They are called with any arguments and
- keyword arguments passed into :meth:`addCleanup` when they are
- added.
+ order they are added (:abbr:`LIFO (last-in, first-out)`). They
+ are called with any arguments and keyword arguments passed into
+ :meth:`addCleanup` when they are added.
If :meth:`setUp` fails, meaning that :meth:`tearDown` is not called,
then any cleanup functions added will still be called.
diff --git a/Doc/library/urllib.parse.rst b/Doc/library/urllib.parse.rst
index 499b211..676321b 100644
--- a/Doc/library/urllib.parse.rst
+++ b/Doc/library/urllib.parse.rst
@@ -114,8 +114,9 @@ or on combining URL components into a URL string.
| | | if present | |
+------------------+-------+--------------------------+----------------------+
- See section :ref:`urlparse-result-object` for more information on the result
- object.
+ Reading the :attr:`port` attribute will raise a :exc:`ValueError` if
+ an invalid port is specified in the URL. See section
+ :ref:`urlparse-result-object` for more information on the result object.
.. versionchanged:: 3.2
Added IPv6 URL parsing capabilities.
@@ -125,6 +126,10 @@ or on combining URL components into a URL string.
false), in accordance with :rfc:`3986`. Previously, a whitelist of
schemes that support fragments existed.
+ .. versionchanged:: 3.6
+ Out-of-range port numbers now raise :exc:`ValueError`, instead of
+ returning :const:`None`.
+
.. function:: parse_qs(qs, keep_blank_values=False, strict_parsing=False, encoding='utf-8', errors='replace')
@@ -227,8 +232,13 @@ or on combining URL components into a URL string.
| | | if present | |
+------------------+-------+-------------------------+----------------------+
- See section :ref:`urlparse-result-object` for more information on the result
- object.
+ Reading the :attr:`port` attribute will raise a :exc:`ValueError` if
+ an invalid port is specified in the URL. See section
+ :ref:`urlparse-result-object` for more information on the result object.
+
+ .. versionchanged:: 3.6
+ Out-of-range port numbers now raise :exc:`ValueError`, instead of
+ returning :const:`None`.
.. function:: urlunsplit(parts)
diff --git a/Doc/library/urllib.request.rst b/Doc/library/urllib.request.rst
index 618c69d..473effc 100644
--- a/Doc/library/urllib.request.rst
+++ b/Doc/library/urllib.request.rst
@@ -30,18 +30,9 @@ The :mod:`urllib.request` module defines the following functions:
Open the URL *url*, which can be either a string or a
:class:`Request` object.
- *data* must be a bytes object specifying additional data to be sent to the
- server, or ``None`` if no such data is needed. *data* may also be an
- iterable object and in that case Content-Length value must be specified in
- the headers. Currently HTTP requests are the only ones that use *data*; the
- HTTP request will be a POST instead of a GET when the *data* parameter is
- provided.
-
- *data* should be a buffer in the standard
- :mimetype:`application/x-www-form-urlencoded` format. The
- :func:`urllib.parse.urlencode` function takes a mapping or sequence of
- 2-tuples and returns an ASCII text string in this format. It should
- be encoded to bytes before being used as the *data* parameter.
+ *data* must be an object specifying additional data to be sent to the
+ server, or ``None`` if no such data is needed. See :class:`Request`
+ for details.
urllib.request module uses HTTP/1.1 and includes ``Connection:close`` header
in its HTTP requests.
@@ -120,6 +111,12 @@ The :mod:`urllib.request` module defines the following functions:
.. versionchanged:: 3.4.3
*context* was added.
+ .. deprecated:: 3.6
+
+ *cafile*, *capath* and *cadefault* are deprecated in favor of *context*.
+ Please use :meth:`ssl.SSLContext.load_cert_chain` instead, or let
+ :func:`ssl.create_default_context` select the system's trusted CA
+ certificates for you.
.. function:: install_opener(opener)
@@ -192,14 +189,21 @@ The following classes are provided:
*url* should be a string containing a valid URL.
- *data* must be a bytes object specifying additional data to send to the
- server, or ``None`` if no such data is needed. Currently HTTP requests are
- the only ones that use *data*; the HTTP request will be a POST instead of a
- GET when the *data* parameter is provided. *data* should be a buffer in the
- standard :mimetype:`application/x-www-form-urlencoded` format.
- The :func:`urllib.parse.urlencode` function takes a mapping or sequence of
- 2-tuples and returns an ASCII string in this format. It should be
- encoded to bytes before being used as the *data* parameter.
+ *data* must be an object specifying additional data to send to the
+ server, or ``None`` if no such data is needed. Currently HTTP
+ requests are the only ones that use *data*. The supported object
+ types include bytes, file-like objects, and iterables. If no
+ ``Content-Length`` nor ``Transfer-Encoding`` header field
+ has been provided, :class:`HTTPHandler` will set these headers according
+ to the type of *data*. ``Content-Length`` will be used to send
+ bytes objects, while ``Transfer-Encoding: chunked`` as specified in
+ :rfc:`7230`, Section 3.3.1 will be used to send files and other iterables.
+
+ For an HTTP POST request method, *data* should be a buffer in the
+ standard :mimetype:`application/x-www-form-urlencoded` format. The
+ :func:`urllib.parse.urlencode` function takes a mapping or sequence
+ of 2-tuples and returns an ASCII string in this format. It should
+ be encoded to bytes before being used as the *data* parameter.
*headers* should be a dictionary, and will be treated as if
:meth:`add_header` was called with each key and value as arguments.
@@ -211,8 +215,10 @@ The following classes are provided:
:mod:`urllib`'s default user agent string is
``"Python-urllib/2.6"`` (on Python 2.6).
- An example of using ``Content-Type`` header with *data* argument would be
- sending a dictionary like ``{"Content-Type": "application/x-www-form-urlencoded"}``.
+ An appropriate ``Content-Type`` header should be included if the *data*
+ argument is present. If this header has not been provided and *data*
+ is not None, ``Content-Type: application/x-www-form-urlencoded`` will
+ be added as a default.
The final two arguments are only of interest for correct handling
of third-party HTTP cookies:
@@ -235,15 +241,28 @@ The following classes are provided:
*method* should be a string that indicates the HTTP request method that
will be used (e.g. ``'HEAD'``). If provided, its value is stored in the
:attr:`~Request.method` attribute and is used by :meth:`get_method()`.
- Subclasses may indicate a default method by setting the
+ The default is ``'GET'`` if *data* is ``None`` or ``'POST'`` otherwise.
+ Subclasses may indicate a different default method by setting the
:attr:`~Request.method` attribute in the class itself.
+ .. note::
+ The request will not work as expected if the data object is unable
+ to deliver its content more than once (e.g. a file or an iterable
+ that can produce the content only once) and the request is retried
+ for HTTP redirects or authentication. The *data* is sent to the
+ HTTP server right away after the headers. There is no support for
+ a 100-continue expectation in the library.
+
.. versionchanged:: 3.3
:attr:`Request.method` argument is added to the Request class.
.. versionchanged:: 3.4
Default :attr:`Request.method` may be indicated at the class level.
+ .. versionchanged:: 3.6
+ Do not raise an error if the ``Content-Length`` has not been
+ provided and *data* is neither ``None`` nor a bytes object.
+ Fall back to use chunked transfer encoding instead.
.. class:: OpenerDirector()
diff --git a/Doc/library/urllib.robotparser.rst b/Doc/library/urllib.robotparser.rst
index ba701c3..7d31932 100644
--- a/Doc/library/urllib.robotparser.rst
+++ b/Doc/library/urllib.robotparser.rst
@@ -57,15 +57,41 @@ structure of :file:`robots.txt` files, see http://www.robotstxt.org/orig.html.
Sets the time the ``robots.txt`` file was last fetched to the current
time.
+ .. method:: crawl_delay(useragent)
-The following example demonstrates basic use of the RobotFileParser class.
+ Returns the value of the ``Crawl-delay`` parameter from ``robots.txt``
+ for the *useragent* in question. If there is no such parameter or it
+ doesn't apply to the *useragent* specified or the ``robots.txt`` entry
+ for this parameter has invalid syntax, return ``None``.
+
+ .. versionadded:: 3.6
+
+ .. method:: request_rate(useragent)
+
+ Returns the contents of the ``Request-rate`` parameter from
+ ``robots.txt`` in the form of a :func:`~collections.namedtuple`
+ ``(requests, seconds)``. If there is no such parameter or it doesn't
+ apply to the *useragent* specified or the ``robots.txt`` entry for this
+ parameter has invalid syntax, return ``None``.
+
+ .. versionadded:: 3.6
+
+
+The following example demonstrates basic use of the :class:`RobotFileParser`
+class::
>>> import urllib.robotparser
>>> rp = urllib.robotparser.RobotFileParser()
>>> rp.set_url("http://www.musi-cal.com/robots.txt")
>>> rp.read()
+ >>> rrate = rp.request_rate("*")
+ >>> rrate.requests
+ 3
+ >>> rrate.seconds
+ 20
+ >>> rp.crawl_delay("*")
+ 6
>>> rp.can_fetch("*", "http://www.musi-cal.com/cgi-bin/search?city=San+Francisco")
False
>>> rp.can_fetch("*", "http://www.musi-cal.com/")
True
-
diff --git a/Doc/library/venv.rst b/Doc/library/venv.rst
index 0327737..17c80c8 100644
--- a/Doc/library/venv.rst
+++ b/Doc/library/venv.rst
@@ -37,44 +37,50 @@ Creating virtual environments
.. _venv-def:
-.. note:: A virtual environment (also called a ``venv``) is a Python
- environment such that the Python interpreter, libraries and scripts
- installed into it are isolated from those installed in other virtual
- environments, and (by default) any libraries installed in a "system" Python,
- i.e. one which is installed as part of your operating system.
+.. note:: A virtual environment is a Python environment such that the Python
+ interpreter, libraries and scripts installed into it are isolated from those
+ installed in other virtual environments, and (by default) any libraries
+ installed in a "system" Python, i.e., one which is installed as part of your
+ operating system.
- A venv is a directory tree which contains Python executable files and
- other files which indicate that it is a venv.
+ A virtual environment is a directory tree which contains Python executable
+ files and other files which indicate that it is a virtual environment.
Common installation tools such as ``Setuptools`` and ``pip`` work as
- expected with venvs - i.e. when a venv is active, they install Python
- packages into the venv without needing to be told to do so explicitly.
-
- When a venv is active (i.e. the venv's Python interpreter is running), the
- attributes :attr:`sys.prefix` and :attr:`sys.exec_prefix` point to the base
- directory of the venv, whereas :attr:`sys.base_prefix` and
- :attr:`sys.base_exec_prefix` point to the non-venv Python installation
- which was used to create the venv. If a venv is not active, then
- :attr:`sys.prefix` is the same as :attr:`sys.base_prefix` and
- :attr:`sys.exec_prefix` is the same as :attr:`sys.base_exec_prefix` (they
- all point to a non-venv Python installation).
-
- When a venv is active, any options that change the installation path will be
- ignored from all distutils configuration files to prevent projects being
- inadvertently installed outside of the virtual environment.
-
- When working in a command shell, users can make a venv active by running an
- ``activate`` script in the venv's executables directory (the precise filename
- is shell-dependent), which prepends the venv's directory for executables to
- the ``PATH`` environment variable for the running shell. There should be no
- need in other circumstances to activate a venv -- scripts installed into
- venvs have a shebang line which points to the venv's Python interpreter. This
- means that the script will run with that interpreter regardless of the value
- of ``PATH``. On Windows, shebang line processing is supported if you have the
- Python Launcher for Windows installed (this was added to Python in 3.3 - see
- :pep:`397` for more details). Thus, double-clicking an installed script in
- a Windows Explorer window should run the script with the correct interpreter
- without there needing to be any reference to its venv in ``PATH``.
+ expected with virtual environments. In other words, when a virtual
+ environment is active, they install Python packages into the virtual
+ environment without needing to be told to do so explicitly.
+
+ When a virtual environment is active (i.e., the virtual environment's Python
+ interpreter is running), the attributes :attr:`sys.prefix` and
+ :attr:`sys.exec_prefix` point to the base directory of the virtual
+ environment, whereas :attr:`sys.base_prefix` and
+ :attr:`sys.base_exec_prefix` point to the non-virtual environment Python
+ installation which was used to create the virtual environment. If a virtual
+ environment is not active, then :attr:`sys.prefix` is the same as
+ :attr:`sys.base_prefix` and :attr:`sys.exec_prefix` is the same as
+ :attr:`sys.base_exec_prefix` (they all point to a non-virtual environment
+ Python installation).
+
+ When a virtual environment is active, any options that change the
+ installation path will be ignored from all distutils configuration files to
+ prevent projects being inadvertently installed outside of the virtual
+ environment.
+
+ When working in a command shell, users can make a virtual environment active
+ by running an ``activate`` script in the virtual environment's executables
+ directory (the precise filename is shell-dependent), which prepends the
+ virtual environment's directory for executables to the ``PATH`` environment
+ variable for the running shell. There should be no need in other
+ circumstances to activate a virtual environment—scripts installed into
+ virtual environments have a "shebang" line which points to the virtual
+ environment's Python interpreter. This means that the script will run with
+ that interpreter regardless of the value of ``PATH``. On Windows, "shebang"
+ line processing is supported if you have the Python Launcher for Windows
+ installed (this was added to Python in 3.3 - see :pep:`397` for more
+ details). Thus, double-clicking an installed script in a Windows Explorer
+ window should run the script with the correct interpreter without there
+ needing to be any reference to its virtual environment in ``PATH``.
.. _venv-api:
@@ -89,7 +95,8 @@ mechanisms for third-party virtual environment creators to customize environment
creation according to their needs, the :class:`EnvBuilder` class.
.. class:: EnvBuilder(system_site_packages=False, clear=False, \
- symlinks=False, upgrade=False, with_pip=False)
+ symlinks=False, upgrade=False, with_pip=False, \
+ prompt=None)
The :class:`EnvBuilder` class accepts the following keyword arguments on
instantiation:
@@ -113,9 +120,16 @@ creation according to their needs, the :class:`EnvBuilder` class.
installed in the virtual environment. This uses :mod:`ensurepip` with
the ``--default-pip`` option.
+ * ``prompt`` -- a String to be used after virtual environment is activated
+ (defaults to ``None`` which means directory name of the environment would
+ be used).
+
.. versionchanged:: 3.4
Added the ``with_pip`` parameter
+ .. versionadded:: 3.6
+ Added the ``prompt`` parameter
+
Creators of third-party virtual environment tools will be free to use the
provided ``EnvBuilder`` class as a base class.
@@ -225,7 +239,7 @@ An example of extending ``EnvBuilder``
--------------------------------------
The following script shows how to extend :class:`EnvBuilder` by implementing a
-subclass which installs setuptools and pip into a created venv::
+subclass which installs setuptools and pip into a created virtual environment::
import os
import os.path
@@ -239,12 +253,12 @@ subclass which installs setuptools and pip into a created venv::
class ExtendedEnvBuilder(venv.EnvBuilder):
"""
This builder installs setuptools and pip so that you can pip or
- easy_install other packages into the created environment.
+ easy_install other packages into the created virtual environment.
:param nodist: If True, setuptools and pip are not installed into the
- created environment.
+ created virtual environment.
:param nopip: If True, pip is not installed into the created
- environment.
+ virtual environment.
:param progress: If setuptools or pip are installed, the progress of the
installation can be monitored by passing a progress
callable. If specified, it is called with two
@@ -270,10 +284,10 @@ subclass which installs setuptools and pip into a created venv::
def post_setup(self, context):
"""
Set up any packages which need to be pre-installed into the
- environment being created.
+ virtual environment being created.
- :param context: The information for the environment creation request
- being processed.
+ :param context: The information for the virtual environment
+ creation request being processed.
"""
os.environ['VIRTUAL_ENV'] = context.env_dir
if not self.nodist:
@@ -307,7 +321,7 @@ subclass which installs setuptools and pip into a created venv::
fn = os.path.split(path)[-1]
binpath = context.bin_path
distpath = os.path.join(binpath, fn)
- # Download script into the env's binaries folder
+ # Download script into the virtual environment's binaries folder
urlretrieve(url, distpath)
progress = self.progress
if self.verbose:
@@ -319,7 +333,7 @@ subclass which installs setuptools and pip into a created venv::
else:
sys.stderr.write('Installing %s ...%s' % (name, term))
sys.stderr.flush()
- # Install in the env
+ # Install in the virtual environment
args = [context.env_exe, fn]
p = Popen(args, stdout=PIPE, stderr=PIPE, cwd=binpath)
t1 = Thread(target=self.reader, args=(p.stdout, 'stdout'))
@@ -338,10 +352,10 @@ subclass which installs setuptools and pip into a created venv::
def install_setuptools(self, context):
"""
- Install setuptools in the environment.
+ Install setuptools in the virtual environment.
- :param context: The information for the environment creation request
- being processed.
+ :param context: The information for the virtual environment
+ creation request being processed.
"""
url = 'https://bitbucket.org/pypa/setuptools/downloads/ez_setup.py'
self.install_script(context, 'setuptools', url)
@@ -354,10 +368,10 @@ subclass which installs setuptools and pip into a created venv::
def install_pip(self, context):
"""
- Install pip in the environment.
+ Install pip in the virtual environment.
- :param context: The information for the environment creation request
- being processed.
+ :param context: The information for the virtual environment
+ creation request being processed.
"""
url = 'https://raw.github.com/pypa/pip/master/contrib/get-pip.py'
self.install_script(context, 'pip', url)
@@ -380,7 +394,8 @@ subclass which installs setuptools and pip into a created venv::
'more target '
'directories.')
parser.add_argument('dirs', metavar='ENV_DIR', nargs='+',
- help='A directory to create the environment in.')
+ help='A directory in which to create the
+ 'virtual environment.')
parser.add_argument('--no-setuptools', default=False,
action='store_true', dest='nodist',
help="Don't install setuptools or pip in the "
@@ -404,14 +419,17 @@ subclass which installs setuptools and pip into a created venv::
'the platform.')
parser.add_argument('--clear', default=False, action='store_true',
dest='clear', help='Delete the contents of the '
- 'environment directory if it '
- 'already exists, before '
+ 'virtual environment '
+ 'directory if it already '
+ 'exists, before virtual '
'environment creation.')
parser.add_argument('--upgrade', default=False, action='store_true',
- dest='upgrade', help='Upgrade the environment '
- 'directory to use this version '
- 'of Python, assuming Python '
- 'has been upgraded in-place.')
+ dest='upgrade', help='Upgrade the virtual '
+ 'environment directory to '
+ 'use this version of '
+ 'Python, assuming Python '
+ 'has been upgraded '
+ 'in-place.')
parser.add_argument('--verbose', default=False, action='store_true',
dest='verbose', help='Display the output '
'from the scripts which '
diff --git a/Doc/library/warnings.rst b/Doc/library/warnings.rst
index cec55f1..f67f4bc 100644
--- a/Doc/library/warnings.rst
+++ b/Doc/library/warnings.rst
@@ -301,7 +301,7 @@ Available Functions
-------------------
-.. function:: warn(message, category=None, stacklevel=1)
+.. function:: warn(message, category=None, stacklevel=1, source=None)
Issue a warning, or maybe ignore it or raise an exception. The *category*
argument, if given, must be a warning category class (see above); it defaults to
@@ -319,8 +319,14 @@ Available Functions
source of :func:`deprecation` itself (since the latter would defeat the purpose
of the warning message).
+ *source*, if supplied, is the destroyed object which emitted a
+ :exc:`ResourceWarning`.
-.. function:: warn_explicit(message, category, filename, lineno, module=None, registry=None, module_globals=None)
+ .. versionchanged:: 3.6
+ Added *source* parameter.
+
+
+.. function:: warn_explicit(message, category, filename, lineno, module=None, registry=None, module_globals=None, source=None)
This is a low-level interface to the functionality of :func:`warn`, passing in
explicitly the message, category, filename and line number, and optionally the
@@ -336,6 +342,12 @@ Available Functions
source for modules found in zipfiles or other non-filesystem import
sources).
+ *source*, if supplied, is the destroyed object which emitted a
+ :exc:`ResourceWarning`.
+
+ .. versionchanged:: 3.6
+ Add the *source* parameter.
+
.. function:: showwarning(message, category, filename, lineno, file=None, line=None)
diff --git a/Doc/library/winreg.rst b/Doc/library/winreg.rst
index 767ea31..14f7896 100644
--- a/Doc/library/winreg.rst
+++ b/Doc/library/winreg.rst
@@ -635,7 +635,7 @@ For more information, see `Registry Value Types
.. data:: REG_DWORD_LITTLE_ENDIAN
- A 32-bit number in little-endian format.
+ A 32-bit number in little-endian format. Equivalent to :const:`REG_DWORD`.
.. data:: REG_DWORD_BIG_ENDIAN
@@ -659,6 +659,18 @@ For more information, see `Registry Value Types
No defined value type.
+.. data:: REG_QWORD
+
+ A 64-bit number.
+
+ .. versionadded:: 3.6
+
+.. data:: REG_QWORD_LITTLE_ENDIAN
+
+ A 64-bit number in little-endian format. Equivalent to :const:`REG_QWORD`.
+
+ .. versionadded:: 3.6
+
.. data:: REG_RESOURCE_LIST
A device-driver resource list.
diff --git a/Doc/library/winsound.rst b/Doc/library/winsound.rst
index d2c4210..372f792 100644
--- a/Doc/library/winsound.rst
+++ b/Doc/library/winsound.rst
@@ -25,7 +25,8 @@ provided by Windows platforms. It includes functions and several constants.
.. function:: PlaySound(sound, flags)
Call the underlying :c:func:`PlaySound` function from the Platform API. The
- *sound* parameter may be a filename, audio data as a string, or ``None``. Its
+ *sound* parameter may be a filename, a system sound alias, audio data as a
+ :term:`bytes-like object`, or ``None``. Its
interpretation depends on the value of *flags*, which can be a bitwise ORed
combination of the constants described below. If the *sound* parameter is
``None``, any currently playing waveform sound is stopped. If the system
@@ -39,7 +40,8 @@ provided by Windows platforms. It includes functions and several constants.
sound to play; possible values are ``-1``, ``MB_ICONASTERISK``,
``MB_ICONEXCLAMATION``, ``MB_ICONHAND``, ``MB_ICONQUESTION``, and ``MB_OK``, all
described below. The value ``-1`` produces a "simple beep"; this is the final
- fallback if a sound cannot be played otherwise.
+ fallback if a sound cannot be played otherwise. If the system indicates an
+ error, :exc:`RuntimeError` is raised.
.. data:: SND_FILENAME
@@ -92,7 +94,7 @@ provided by Windows platforms. It includes functions and several constants.
.. data:: SND_MEMORY
The *sound* parameter to :func:`PlaySound` is a memory image of a WAV file, as a
- string.
+ :term:`bytes-like object`.
.. note::
diff --git a/Doc/library/wsgiref.rst b/Doc/library/wsgiref.rst
index aad27a8..a1d4469 100644
--- a/Doc/library/wsgiref.rst
+++ b/Doc/library/wsgiref.rst
@@ -133,9 +133,9 @@ parameter expect a WSGI-compliant dictionary to be supplied; please see
for key, value in environ.items()]
return ret
- httpd = make_server('', 8000, simple_app)
- print("Serving on port 8000...")
- httpd.serve_forever()
+ with make_server('', 8000, simple_app) as httpd:
+ print("Serving on port 8000...")
+ httpd.serve_forever()
In addition to the environment functions above, the :mod:`wsgiref.util` module
@@ -285,14 +285,14 @@ request. (E.g., using the :func:`shift_path_info` function from
from wsgiref.simple_server import make_server, demo_app
- httpd = make_server('', 8000, demo_app)
- print("Serving HTTP on port 8000...")
+ with make_server('', 8000, demo_app) as httpd:
+ print("Serving HTTP on port 8000...")
- # Respond to requests until process is killed
- httpd.serve_forever()
+ # Respond to requests until process is killed
+ httpd.serve_forever()
- # Alternative: serve one request, then exit
- httpd.handle_request()
+ # Alternative: serve one request, then exit
+ httpd.handle_request()
.. function:: demo_app(environ, start_response)
@@ -432,9 +432,9 @@ Paste" library.
# This is the application wrapped in a validator
validator_app = validator(simple_app)
- httpd = make_server('', 8000, validator_app)
- print("Listening on port 8000....")
- httpd.serve_forever()
+ with make_server('', 8000, validator_app) as httpd:
+ print("Listening on port 8000....")
+ httpd.serve_forever()
:mod:`wsgiref.handlers` -- server/gateway base classes
@@ -774,8 +774,8 @@ This is a working "Hello World" WSGI application::
# The returned object is going to be printed
return [b"Hello World"]
- httpd = make_server('', 8000, hello_world_app)
- print("Serving on port 8000...")
+ with make_server('', 8000, hello_world_app) as httpd:
+ print("Serving on port 8000...")
- # Serve until process is killed
- httpd.serve_forever()
+ # Serve until process is killed
+ httpd.serve_forever()
diff --git a/Doc/library/xmlrpc.client.rst b/Doc/library/xmlrpc.client.rst
index dad7a02..390828e 100644
--- a/Doc/library/xmlrpc.client.rst
+++ b/Doc/library/xmlrpc.client.rst
@@ -88,9 +88,13 @@ between conformable Python objects and XML on the wire.
+======================+=======================================================+
| ``boolean`` | :class:`bool` |
+----------------------+-------------------------------------------------------+
- | ``int`` or ``i4`` | :class:`int` in range from -2147483648 to 2147483647. |
+ | ``int``, ``i1``, | :class:`int` in range from -2147483648 to 2147483647. |
+ | ``i2``, ``i4``, | Values get the ``<int>`` tag. |
+ | ``i8`` or | |
+ | ``biginteger`` | |
+----------------------+-------------------------------------------------------+
- | ``double`` | :class:`float` |
+ | ``double`` or | :class:`float`. Values get the ``<double>`` tag. |
+ | ``float`` | |
+----------------------+-------------------------------------------------------+
| ``string`` | :class:`str` |
+----------------------+-------------------------------------------------------+
@@ -114,6 +118,8 @@ between conformable Python objects and XML on the wire.
| ``nil`` | The ``None`` constant. Passing is allowed only if |
| | *allow_none* is true. |
+----------------------+-------------------------------------------------------+
+ | ``bigdecimal`` | :class:`decimal.Decimal`. Returned type only. |
+ +----------------------+-------------------------------------------------------+
This is the full set of data types supported by XML-RPC. Method calls may also
raise a special :exc:`Fault` instance, used to signal XML-RPC server errors, or
@@ -137,6 +143,13 @@ between conformable Python objects and XML on the wire.
.. versionchanged:: 3.5
Added the *context* argument.
+ .. versionchanged:: 3.6
+ Added support of type tags with prefixes (e.g. ``ex:nil``).
+ Added support of unmarsalling additional types used by Apache XML-RPC
+ implementation for numerics: ``i1``, ``i2``, ``i8``, ``biginteger``,
+ ``float`` and ``bigdecimal``.
+ See http://ws.apache.org/xmlrpc/types.html for a description.
+
.. seealso::
diff --git a/Doc/library/xmlrpc.server.rst b/Doc/library/xmlrpc.server.rst
index 1c77e84..0511ddf 100644
--- a/Doc/library/xmlrpc.server.rst
+++ b/Doc/library/xmlrpc.server.rst
@@ -148,29 +148,29 @@ Server code::
rpc_paths = ('/RPC2',)
# Create server
- server = SimpleXMLRPCServer(("localhost", 8000),
- requestHandler=RequestHandler)
- server.register_introspection_functions()
+ with SimpleXMLRPCServer(("localhost", 8000),
+ requestHandler=RequestHandler) as server:
+ server.register_introspection_functions()
- # Register pow() function; this will use the value of
- # pow.__name__ as the name, which is just 'pow'.
- server.register_function(pow)
+ # Register pow() function; this will use the value of
+ # pow.__name__ as the name, which is just 'pow'.
+ server.register_function(pow)
- # Register a function under a different name
- def adder_function(x,y):
- return x + y
- server.register_function(adder_function, 'add')
+ # Register a function under a different name
+ def adder_function(x,y):
+ return x + y
+ server.register_function(adder_function, 'add')
- # Register an instance; all the methods of the instance are
- # published as XML-RPC methods (in this case, just 'mul').
- class MyFuncs:
- def mul(self, x, y):
- return x * y
+ # Register an instance; all the methods of the instance are
+ # published as XML-RPC methods (in this case, just 'mul').
+ class MyFuncs:
+ def mul(self, x, y):
+ return x * y
- server.register_instance(MyFuncs())
+ server.register_instance(MyFuncs())
- # Run the server's main loop
- server.serve_forever()
+ # Run the server's main loop
+ server.serve_forever()
The following client code will call the methods made available by the preceding
server::
@@ -207,18 +207,17 @@ a server allowing dotted names and registering a multicall function.
def getCurrentTime():
return datetime.datetime.now()
- server = SimpleXMLRPCServer(("localhost", 8000))
- server.register_function(pow)
- server.register_function(lambda x,y: x+y, 'add')
- server.register_instance(ExampleService(), allow_dotted_names=True)
- server.register_multicall_functions()
- print('Serving XML-RPC on localhost port 8000')
- try:
- server.serve_forever()
- except KeyboardInterrupt:
- print("\nKeyboard interrupt received, exiting.")
- server.server_close()
- sys.exit(0)
+ with SimpleXMLRPCServer(("localhost", 8000)) as server:
+ server.register_function(pow)
+ server.register_function(lambda x,y: x+y, 'add')
+ server.register_instance(ExampleService(), allow_dotted_names=True)
+ server.register_multicall_functions()
+ print('Serving XML-RPC on localhost port 8000')
+ try:
+ server.serve_forever()
+ except KeyboardInterrupt:
+ print("\nKeyboard interrupt received, exiting.")
+ sys.exit(0)
This ExampleService demo can be invoked from the command line::
diff --git a/Doc/library/zipfile.rst b/Doc/library/zipfile.rst
index d144ae3..5eb6f10 100644
--- a/Doc/library/zipfile.rst
+++ b/Doc/library/zipfile.rst
@@ -147,10 +147,10 @@ ZipFile Objects
*compression* is the ZIP compression method to use when writing the archive,
and should be :const:`ZIP_STORED`, :const:`ZIP_DEFLATED`,
:const:`ZIP_BZIP2` or :const:`ZIP_LZMA`; unrecognized
- values will cause :exc:`RuntimeError` to be raised. If :const:`ZIP_DEFLATED`,
+ values will cause :exc:`NotImplementedError` to be raised. If :const:`ZIP_DEFLATED`,
:const:`ZIP_BZIP2` or :const:`ZIP_LZMA` is specified but the corresponding module
(:mod:`zlib`, :mod:`bz2` or :mod:`lzma`) is not available, :exc:`RuntimeError`
- is also raised. The default is :const:`ZIP_STORED`. If *allowZip64* is
+ is raised. The default is :const:`ZIP_STORED`. If *allowZip64* is
``True`` (the default) zipfile will create ZIP files that use the ZIP64
extensions when the zipfile is larger than 4 GiB. If it is false :mod:`zipfile`
will raise an exception when the ZIP file would require ZIP64 extensions.
@@ -179,6 +179,10 @@ ZipFile Objects
Added support for writing to unseekable streams.
Added support for the ``'x'`` mode.
+ .. versionchanged:: 3.6
+ Previously, a plain :exc:`RuntimeError` was raised for unrecognized
+ compression values.
+
.. method:: ZipFile.close()
@@ -205,18 +209,12 @@ ZipFile Objects
Return a list of archive members by name.
-.. index::
- single: universal newlines; zipfile.ZipFile.open method
-
-.. method:: ZipFile.open(name, mode='r', pwd=None)
+.. method:: ZipFile.open(name, mode='r', pwd=None, *, force_zip64=False)
- Extract a member from the archive as a file-like object (ZipExtFile). *name*
- is the name of the file in the archive, or a :class:`ZipInfo` object. The
- *mode* parameter, if included, must be one of the following: ``'r'`` (the
- default), ``'U'``, or ``'rU'``. Choosing ``'U'`` or ``'rU'`` will enable
- :term:`universal newlines` support in the read-only object. *pwd* is the
- password used for encrypted files. Calling :meth:`.open` on a closed
- ZipFile will raise a :exc:`RuntimeError`.
+ Access a member of the archive as a binary file-like object. *name*
+ can be either the name of a file within the archive or a :class:`ZipInfo`
+ object. The *mode* parameter, if included, must be ``'r'`` (the default)
+ or ``'w'``. *pwd* is the password used to decrypt encrypted ZIP files.
:meth:`~ZipFile.open` is also a context manager and therefore supports the
:keyword:`with` statement::
@@ -225,17 +223,23 @@ ZipFile Objects
with myzip.open('eggs.txt') as myfile:
print(myfile.read())
- .. note::
+ With *mode* ``'r'`` the file-like object
+ (``ZipExtFile``) is read-only and provides the following methods:
+ :meth:`~io.BufferedIOBase.read`, :meth:`~io.IOBase.readline`,
+ :meth:`~io.IOBase.readlines`, :meth:`__iter__`,
+ :meth:`~iterator.__next__`. These objects can operate independently of
+ the ZipFile.
- The file-like object is read-only and provides the following methods:
- :meth:`~io.BufferedIOBase.read`, :meth:`~io.IOBase.readline`,
- :meth:`~io.IOBase.readlines`, :meth:`__iter__`,
- :meth:`~iterator.__next__`.
+ With ``mode='w'``, a writable file handle is returned, which supports the
+ :meth:`~io.BufferedIOBase.write` method. While a writable file handle is open,
+ attempting to read or write other files in the ZIP file will raise a
+ :exc:`ValueError`.
- .. note::
-
- Objects returned by :meth:`.open` can operate independently of the
- ZipFile.
+ When writing a file, if the file size is not known in advance but may exceed
+ 2 GiB, pass ``force_zip64=True`` to ensure that the header format is
+ capable of supporting large files. If the file size is known in advance,
+ construct a :class:`ZipInfo` object with :attr:`~ZipInfo.file_size` set, and
+ use that as the *name* parameter.
.. note::
@@ -243,10 +247,19 @@ ZipFile Objects
or a :class:`ZipInfo` object. You will appreciate this when trying to read a
ZIP file that contains members with duplicate names.
- .. deprecated-removed:: 3.4 3.6
- The ``'U'`` or ``'rU'`` mode. Use :class:`io.TextIOWrapper` for reading
+ .. versionchanged:: 3.6
+ Removed support of ``mode='U'``. Use :class:`io.TextIOWrapper` for reading
compressed text files in :term:`universal newlines` mode.
+ .. versionchanged:: 3.6
+ :meth:`open` can now be used to write files into the archive with the
+ ``mode='w'`` option.
+
+ .. versionchanged:: 3.6
+ Calling :meth:`.open` on a closed ZipFile will raise a :exc:`ValueError`.
+ Previously, a :exc:`RuntimeError` was raised.
+
+
.. method:: ZipFile.extract(member, path=None, pwd=None)
Extract a member from the archive to the current working directory; *member*
@@ -267,6 +280,10 @@ ZipFile Objects
characters (``:``, ``<``, ``>``, ``|``, ``"``, ``?``, and ``*``)
replaced by underscore (``_``).
+ .. versionchanged:: 3.6
+ Calling :meth:`extract` on a closed ZipFile will raise a
+ :exc:`ValueError`. Previously, a :exc:`RuntimeError` was raised.
+
.. method:: ZipFile.extractall(path=None, members=None, pwd=None)
@@ -283,6 +300,10 @@ ZipFile Objects
dots ``".."``. This module attempts to prevent that.
See :meth:`extract` note.
+ .. versionchanged:: 3.6
+ Calling :meth:`extractall` on a closed ZipFile will raise a
+ :exc:`ValueError`. Previously, a :exc:`RuntimeError` was raised.
+
.. method:: ZipFile.printdir()
@@ -300,18 +321,24 @@ ZipFile Objects
file in the archive, or a :class:`ZipInfo` object. The archive must be open for
read or append. *pwd* is the password used for encrypted files and, if specified,
it will override the default password set with :meth:`setpassword`. Calling
- :meth:`read` on a closed ZipFile will raise a :exc:`RuntimeError`. Calling
:meth:`read` on a ZipFile that uses a compression method other than
:const:`ZIP_STORED`, :const:`ZIP_DEFLATED`, :const:`ZIP_BZIP2` or
:const:`ZIP_LZMA` will raise a :exc:`NotImplementedError`. An error will also
be raised if the corresponding compression module is not available.
+ .. versionchanged:: 3.6
+ Calling :meth:`read` on a closed ZipFile will raise a :exc:`ValueError`.
+ Previously, a :exc:`RuntimeError` was raised.
+
.. method:: ZipFile.testzip()
Read all the files in the archive and check their CRC's and file headers.
- Return the name of the first bad file, or else return ``None``. Calling
- :meth:`testzip` on a closed ZipFile will raise a :exc:`RuntimeError`.
+ Return the name of the first bad file, or else return ``None``.
+
+ .. versionchanged:: 3.6
+ Calling :meth:`testfile` on a closed ZipFile will raise a
+ :exc:`ValueError`. Previously, a :exc:`RuntimeError` was raised.
.. method:: ZipFile.write(filename, arcname=None, compress_type=None)
@@ -321,10 +348,7 @@ ZipFile Objects
letter and with leading path separators removed). If given, *compress_type*
overrides the value given for the *compression* parameter to the constructor for
the new entry.
- The archive must be open with mode ``'w'``, ``'x'`` or ``'a'`` -- calling
- :meth:`write` on a ZipFile created with mode ``'r'`` will raise a
- :exc:`RuntimeError`. Calling :meth:`write` on a closed ZipFile will raise a
- :exc:`RuntimeError`.
+ The archive must be open with mode ``'w'``, ``'x'`` or ``'a'``.
.. note::
@@ -343,16 +367,19 @@ ZipFile Objects
If ``arcname`` (or ``filename``, if ``arcname`` is not given) contains a null
byte, the name of the file in the archive will be truncated at the null byte.
+ .. versionchanged:: 3.6
+ Calling :meth:`write` on a ZipFile created with mode ``'r'`` or
+ a closed ZipFile will raise a :exc:`ValueError`. Previously,
+ a :exc:`RuntimeError` was raised.
+
+
.. method:: ZipFile.writestr(zinfo_or_arcname, data[, compress_type])
Write the string *data* to the archive; *zinfo_or_arcname* is either the file
name it will be given in the archive, or a :class:`ZipInfo` instance. If it's
an instance, at least the filename, date, and time must be given. If it's a
name, the date and time is set to the current date and time.
- The archive must be opened with mode ``'w'``, ``'x'`` or ``'a'`` -- calling
- :meth:`writestr` on a ZipFile created with mode ``'r'`` will raise a
- :exc:`RuntimeError`. Calling :meth:`writestr` on a closed ZipFile will
- raise a :exc:`RuntimeError`.
+ The archive must be opened with mode ``'w'``, ``'x'`` or ``'a'``.
If given, *compress_type* overrides the value given for the *compression*
parameter to the constructor for the new entry, or in the *zinfo_or_arcname*
@@ -368,6 +395,12 @@ ZipFile Objects
.. versionchanged:: 3.2
The *compress_type* argument.
+ .. versionchanged:: 3.6
+ Calling :meth:`writestr` on a ZipFile created with mode ``'r'`` or
+ a closed ZipFile will raise a :exc:`ValueError`. Previously,
+ a :exc:`RuntimeError` was raised.
+
+
The following data attributes are also available:
@@ -465,7 +498,31 @@ Instances of the :class:`ZipInfo` class are returned by the :meth:`.getinfo` and
:meth:`.infolist` methods of :class:`ZipFile` objects. Each object stores
information about a single member of the ZIP archive.
-Instances have the following attributes:
+There is one classmethod to make a :class:`ZipInfo` instance for a filesystem
+file:
+
+.. classmethod:: ZipInfo.from_file(filename, arcname=None)
+
+ Construct a :class:`ZipInfo` instance for a file on the filesystem, in
+ preparation for adding it to a zip file.
+
+ *filename* should be the path to a file or directory on the filesystem.
+
+ If *arcname* is specified, it is used as the name within the archive.
+ If *arcname* is not specified, the name will be the same as *filename*, but
+ with any drive letter and leading path separators removed.
+
+ .. versionadded:: 3.6
+
+Instances have the following methods and attributes:
+
+.. method:: ZipInfo.is_dir()
+
+ Return ``True`` if this archive member is a directory.
+
+ This uses the entry's name: directories should always end with ``/``.
+
+ .. versionadded:: 3.6
.. attribute:: ZipInfo.filename
diff --git a/Doc/library/zlib.rst b/Doc/library/zlib.rst
index 1de7bae..3d742ab 100644
--- a/Doc/library/zlib.rst
+++ b/Doc/library/zlib.rst
@@ -47,14 +47,19 @@ The available exception and functions in this module are:
platforms, use ``adler32(data) & 0xffffffff``.
-.. function:: compress(data[, level])
+.. function:: compress(data, level=-1)
Compresses the bytes in *data*, returning a bytes object containing compressed data.
- *level* is an integer from ``0`` to ``9`` controlling the level of compression;
+ *level* is an integer from ``0`` to ``9`` or ``-1`` controlling the level of compression;
``1`` is fastest and produces the least compression, ``9`` is slowest and
- produces the most. ``0`` is no compression. The default value is ``6``.
+ produces the most. ``0`` is no compression. The default value is ``-1``
+ (Z_DEFAULT_COMPRESSION). Z_DEFAULT_COMPRESSION represents a default
+ compromise between speed and compression (currently equivalent to level 6).
Raises the :exc:`error` exception if any error occurs.
+ .. versionchanged:: 3.6
+ *level* can now be used as a keyword parameter.
+
.. function:: compressobj(level=-1, method=DEFLATED, wbits=15, memLevel=8, strategy=Z_DEFAULT_STRATEGY[, zdict])
@@ -124,7 +129,7 @@ The available exception and functions in this module are:
platforms, use ``crc32(data) & 0xffffffff``.
-.. function:: decompress(data[, wbits[, bufsize]])
+.. function:: decompress(data, wbits=MAX_WBITS, bufsize=DEF_BUF_SIZE)
Decompresses the bytes in *data*, returning a bytes object containing the
uncompressed data. The *wbits* parameter depends on
@@ -159,14 +164,16 @@ The available exception and functions in this module are:
When decompressing a stream, the window size must not be smaller
than the size originally used to compress the stream; using a too-small
value may result in an :exc:`error` exception. The default *wbits* value
- is 15, which corresponds to the largest window size and requires a zlib
- header and trailer to be included.
+ corresponds to the largest window size and requires a zlib header and
+ trailer to be included.
*bufsize* is the initial size of the buffer used to hold decompressed data. If
more space is required, the buffer size will be increased as needed, so you
don't have to get this value exactly right; tuning it will only save a few calls
- to :c:func:`malloc`. The default size is 16384.
+ to :c:func:`malloc`.
+ .. versionchanged:: 3.6
+ *wbits* and *bufsize* can be used as keyword arguments.
.. function:: decompressobj(wbits=15[, zdict])
@@ -252,7 +259,7 @@ Decompression objects support the following methods and attributes:
.. versionadded:: 3.3
-.. method:: Decompress.decompress(data[, max_length])
+.. method:: Decompress.decompress(data, max_length=0)
Decompress *data*, returning a bytes object containing the uncompressed data
corresponding to at least part of the data in *string*. This data should be
@@ -264,9 +271,11 @@ Decompression objects support the following methods and attributes:
no longer than *max_length*. This may mean that not all of the compressed input
can be processed; and unconsumed data will be stored in the attribute
:attr:`unconsumed_tail`. This bytestring must be passed to a subsequent call to
- :meth:`decompress` if decompression is to continue. If *max_length* is not
- supplied then the whole input is decompressed, and :attr:`unconsumed_tail` is
- empty.
+ :meth:`decompress` if decompression is to continue. If *max_length* is zero
+ then the whole input is decompressed, and :attr:`unconsumed_tail` is empty.
+
+ .. versionchanged:: 3.6
+ *max_length* can be used as a keyword argument.
.. method:: Decompress.flush([length])
diff --git a/Doc/make.bat b/Doc/make.bat
index 5ab8085..da1f876 100644
--- a/Doc/make.bat
+++ b/Doc/make.bat
@@ -40,7 +40,7 @@ if "%1" == "clean" (
goto end
)
-%SPHINXBUILD% 2> nul
+%SPHINXBUILD% >nul 2> nul
if errorlevel 9009 (
echo.
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
diff --git a/Doc/reference/compound_stmts.rst b/Doc/reference/compound_stmts.rst
index 88b94ea..4b425a4 100644
--- a/Doc/reference/compound_stmts.rst
+++ b/Doc/reference/compound_stmts.rst
@@ -471,10 +471,10 @@ A function definition defines a user-defined function object (see section
decorators: `decorator`+
decorator: "@" `dotted_name` ["(" [`argument_list` [","]] ")"] NEWLINE
dotted_name: `identifier` ("." `identifier`)*
- parameter_list: (`defparameter` ",")*
- : | "*" [`parameter`] ("," `defparameter`)* ["," "**" `parameter`]
- : | "**" `parameter`
- : | `defparameter` [","] )
+ parameter_list: `defparameter` ("," `defparameter`)* ["," [`parameter_list_starargs`]]
+ : | `parameter_list_starargs`
+ parameter_list_starargs: "*" [`parameter`] ("," `defparameter`)* ["," ["**" `parameter` [","]]]
+ : | "**" `parameter` [","]
parameter: `identifier` [":" `expression`]
defparameter: `parameter` ["=" `expression`]
funcname: `identifier`
@@ -546,11 +546,12 @@ Function call semantics are described in more detail in section :ref:`calls`. A
function call always assigns values to all parameters mentioned in the parameter
list, either from position arguments, from keyword arguments, or from default
values. If the form "``*identifier``" is present, it is initialized to a tuple
-receiving any excess positional parameters, defaulting to the empty tuple. If
-the form "``**identifier``" is present, it is initialized to a new dictionary
-receiving any excess keyword arguments, defaulting to a new empty dictionary.
-Parameters after "``*``" or "``*identifier``" are keyword-only parameters and
-may only be passed used keyword arguments.
+receiving any excess positional parameters, defaulting to the empty tuple.
+If the form "``**identifier``" is present, it is initialized to a new
+ordered mapping receiving any excess keyword arguments, defaulting to a
+new empty mapping of the same type. Parameters after "``*``" or
+"``*identifier``" are keyword-only parameters and may only be passed
+used keyword arguments.
.. index:: pair: function; annotations
@@ -632,6 +633,11 @@ list for the base classes and the saved local namespace for the attribute
dictionary. The class name is bound to this class object in the original local
namespace.
+The order in which attributes are defined in the class body is preserved
+in the new class's ``__dict__``. Note that this is reliable only right
+after the class is created and only for classes that were defined using
+the definition syntax.
+
Class creation can be customized heavily using :ref:`metaclasses <metaclasses>`.
Classes can also be decorated: just like when decorating functions, ::
@@ -691,7 +697,7 @@ coroutine bodies.
Functions defined with ``async def`` syntax are always coroutine functions,
even if they do not contain ``await`` or ``async`` keywords.
-It is a :exc:`SyntaxError` to use :keyword:`yield` expressions in
+It is a :exc:`SyntaxError` to use ``yield from`` expressions in
``async def`` coroutines.
An example of a coroutine function::
diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst
index cd28c14..095a238 100644
--- a/Doc/reference/datamodel.rst
+++ b/Doc/reference/datamodel.rst
@@ -627,6 +627,25 @@ Callable types
as well as :keyword:`async with` and :keyword:`async for` statements. See
also the :ref:`coroutine-objects` section.
+ Asynchronous generator functions
+ .. index::
+ single: asynchronous generator; function
+ single: asynchronous generator; asynchronous iterator
+
+ A function or method which is defined using :keyword:`async def` and
+ which uses the :keyword:`yield` statement is called a
+ :dfn:`asynchronous generator function`. Such a function, when called,
+ returns an asynchronous iterator object which can be used in an
+ :keyword:`async for` statement to execute the body of the function.
+
+ Calling the asynchronous iterator's :meth:`aiterator.__anext__` method
+ will return an :term:`awaitable` which when awaited
+ will execute until it provides a value using the :keyword:`yield`
+ expression. When the function executes an empty :keyword:`return`
+ statement or falls off the end, a :exc:`StopAsyncIteration` exception
+ is raised and the asynchronous iterator will have reached the end of
+ the set of values to be yielded.
+
Built-in functions
.. index::
object: built-in function
@@ -686,33 +705,36 @@ Modules
Attribute assignment updates the module's namespace dictionary, e.g.,
``m.x = 1`` is equivalent to ``m.__dict__["x"] = 1``.
- .. index:: single: __dict__ (module attribute)
-
- Special read-only attribute: :attr:`~object.__dict__` is the module's namespace as a
- dictionary object.
-
- .. impl-detail::
-
- Because of the way CPython clears module dictionaries, the module
- dictionary will be cleared when the module falls out of scope even if the
- dictionary still has live references. To avoid this, copy the dictionary
- or keep the module around while using its dictionary directly.
-
.. index::
single: __name__ (module attribute)
single: __doc__ (module attribute)
single: __file__ (module attribute)
+ single: __annotations__ (module attribute)
pair: module; namespace
Predefined (writable) attributes: :attr:`__name__` is the module's name;
:attr:`__doc__` is the module's documentation string, or ``None`` if
- unavailable; :attr:`__file__` is the pathname of the file from which the
+ unavailable; :attr:`__annotations__` (optional) is a dictionary containing
+ :term:`variable annotations <variable annotation>` collected during module
+ body execution; :attr:`__file__` is the pathname of the file from which the
module was loaded, if it was loaded from a file. The :attr:`__file__`
attribute may be missing for certain types of modules, such as C modules
that are statically linked into the interpreter; for extension modules
loaded dynamically from a shared library, it is the pathname of the shared
library file.
+ .. index:: single: __dict__ (module attribute)
+
+ Special read-only attribute: :attr:`~object.__dict__` is the module's
+ namespace as a dictionary object.
+
+ .. impl-detail::
+
+ Because of the way CPython clears module dictionaries, the module
+ dictionary will be cleared when the module falls out of scope even if the
+ dictionary still has live references. To avoid this, copy the dictionary
+ or keep the module around while using its dictionary directly.
+
Custom classes
Custom class types are typically created by class definitions (see section
:ref:`class`). A class has a namespace implemented by a dictionary object.
@@ -761,13 +783,16 @@ Custom classes
single: __dict__ (class attribute)
single: __bases__ (class attribute)
single: __doc__ (class attribute)
+ single: __annotations__ (class attribute)
Special attributes: :attr:`~definition.__name__` is the class name; :attr:`__module__` is
the module name in which the class was defined; :attr:`~object.__dict__` is the
dictionary containing the class's namespace; :attr:`~class.__bases__` is a
tuple containing the base classes, in the order of their occurrence in the
- base class list; :attr:`__doc__` is the class's documentation string, or
- ``None`` if undefined.
+ base class list; :attr:`__doc__` is the class's documentation string,
+ or ``None`` if undefined; :attr:`__annotations__` (optional) is a dictionary
+ containing :term:`variable annotations <variable annotation>` collected during
+ class body execution.
Class instances
.. index::
@@ -1063,6 +1088,12 @@ to ``type(x).__getitem__(x, i)``. Except where mentioned, attempts to execute a
operation raise an exception when no appropriate method is defined (typically
:exc:`AttributeError` or :exc:`TypeError`).
+Setting a special method to ``None`` indicates that the corresponding
+operation is not available. For example, if a class sets
+:meth:`__iter__` to ``None``, the class is not iterable, so calling
+:func:`iter` on its instances will raise a :exc:`TypeError` (without
+falling back to :meth:`__getitem__`). [#]_
+
When implementing a class that emulates any built-in type, it is important that
the emulation only be implemented to the degree that it makes sense for the
object being modelled. For example, some sequences may work well with retrieval
@@ -1233,8 +1264,9 @@ Basic customization
.. method:: object.__format__(self, format_spec)
- Called by the :func:`format` built-in function (and by extension, the
- :meth:`str.format` method of class :class:`str`) to produce a "formatted"
+ Called by the :func:`format` built-in function,
+ and by extension, evaluation of :ref:`formatted string literals
+ <f-strings>` and the :meth:`str.format` method, to produce a "formatted"
string representation of an object. The ``format_spec`` argument is
a string that contains a description of the formatting options desired.
The interpretation of the ``format_spec`` argument is up to the type
@@ -1494,6 +1526,14 @@ class' :attr:`~object.__dict__`.
Called to delete the attribute on an instance *instance* of the owner class.
+.. method:: object.__set_name__(self, owner, name)
+
+ Called at the time the owning class *owner* is created. The
+ descriptor has been assigned to *name*.
+
+ .. versionadded:: 3.6
+
+
The attribute :attr:`__objclass__` is interpreted by the :mod:`inspect` module
as specifying the class where this object was defined (setting this
appropriately can assist in runtime introspection of dynamic class attributes).
@@ -1631,11 +1671,60 @@ Notes on using *__slots__*
* *__class__* assignment works only if both classes have the same *__slots__*.
-.. _metaclasses:
+.. _class-customization:
Customizing class creation
--------------------------
+Whenever a class inherits from another class, *__init_subclass__* is
+called on that class. This way, it is possible to write classes which
+change the behavior of subclasses. This is closely related to class
+decorators, but where class decorators only affect the specific class they're
+applied to, ``__init_subclass__`` solely applies to future subclasses of the
+class defining the method.
+
+.. classmethod:: object.__init_subclass__(cls)
+
+ This method is called whenever the containing class is subclassed.
+ *cls* is then the new subclass. If defined as a normal instance method,
+ this method is implicitly converted to a class method.
+
+ Keyword arguments which are given to a new class are passed to
+ the parent's class ``__init_subclass__``. For compatibility with
+ other classes using ``__init_subclass__``, one should take out the
+ needed keyword arguments and pass the others over to the base
+ class, as in::
+
+ class Philosopher:
+ def __init_subclass__(cls, default_name, **kwargs):
+ super().__init_subclass__(**kwargs)
+ cls.default_name = default_name
+
+ class AustralianPhilosopher(Philosopher, default_name="Bruce"):
+ pass
+
+ The default implementation ``object.__init_subclass__`` does
+ nothing, but raises an error if it is called with any arguments.
+
+ .. note::
+
+ The metaclass hint ``metaclass`` is consumed by the rest of the type
+ machinery, and is never passed to ``__init_subclass__`` implementations.
+ The actual metaclass (rather than the explicit hint) can be accessed as
+ ``type(cls)``.
+
+ .. versionadded:: 3.6
+
+
+.. _metaclasses:
+
+Metaclasses
+^^^^^^^^^^^
+
+.. index::
+ single: metaclass
+ builtin: type
+
By default, classes are constructed using :func:`type`. The class body is
executed in a new namespace and the class name is bound locally to the
result of ``type(name, bases, namespace)``.
@@ -1666,6 +1755,8 @@ When a class definition is executed, the following steps occur:
Determining the appropriate metaclass
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+.. index::
+ single: metaclass hint
The appropriate metaclass for a class definition is determined as follows:
@@ -1687,13 +1778,16 @@ that criterion, then the class definition will fail with ``TypeError``.
Preparing the class namespace
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+.. index::
+ single: __prepare__ (metaclass method)
+
Once the appropriate metaclass has been identified, then the class namespace
is prepared. If the metaclass has a ``__prepare__`` attribute, it is called
as ``namespace = metaclass.__prepare__(name, bases, **kwds)`` (where the
additional keyword arguments, if any, come from the class definition).
If the metaclass has no ``__prepare__`` attribute, then the class namespace
-is initialised as an empty :func:`dict` instance.
+is initialised as an empty ordered mapping.
.. seealso::
@@ -1704,6 +1798,9 @@ is initialised as an empty :func:`dict` instance.
Executing the class body
^^^^^^^^^^^^^^^^^^^^^^^^
+.. index::
+ single: class; body
+
The class body is executed (approximately) as
``exec(body, globals(), namespace)``. The key difference from a normal
call to :func:`exec` is that lexical scoping allows the class body (including
@@ -1713,12 +1810,19 @@ class definition occurs inside a function.
However, even when the class definition occurs inside the function, methods
defined inside the class still cannot see names defined at the class scope.
Class variables must be accessed through the first parameter of instance or
-class methods, and cannot be accessed at all from static methods.
+class methods, or through the implicit lexically scoped ``__class__`` reference
+described in the next section.
+.. _class-object-creation:
Creating the class object
^^^^^^^^^^^^^^^^^^^^^^^^^
+.. index::
+ single: __class__ (method cell)
+ single: __classcell__ (class namespace entry)
+
+
Once the class namespace has been populated by executing the class body,
the class object is created by calling
``metaclass(name, bases, namespace, **kwds)`` (the additional keywords
@@ -1732,14 +1836,34 @@ created by the compiler if any methods in a class body refer to either
lexical scoping, while the class or instance that was used to make the
current call is identified based on the first argument passed to the method.
+.. impl-detail::
+
+ In CPython 3.6 and later, the ``__class__`` cell is passed to the metaclass
+ as a ``__classcell__`` entry in the class namespace. If present, this must
+ be propagated up to the ``type.__new__`` call in order for the class to be
+ initialised correctly.
+ Failing to do so will result in a :exc:`DeprecationWarning` in Python 3.6,
+ and a :exc:`RuntimeWarning` in the future.
+
+When using the default metaclass :class:`type`, or any metaclass that ultimately
+calls ``type.__new__``, the following additional customisation steps are
+invoked after creating the class object:
+
+* first, ``type.__new__`` collects all of the descriptors in the class
+ namespace that define a :meth:`~object.__set_name__` method;
+* second, all of these ``__set_name__`` methods are called with the class
+ being defined and the assigned name of that particular descriptor; and
+* finally, the :meth:`~object.__init_subclass__` hook is called on the
+ immediate parent of the new class in its method resolution order.
+
After the class object is created, it is passed to the class decorators
included in the class definition (if any) and the resulting object is bound
in the local namespace as the defined class.
When a new class is created by ``type.__new__``, the object provided as the
-namespace parameter is copied to a standard Python dictionary and the original
-object is discarded. The new copy becomes the :attr:`~object.__dict__` attribute
-of the class object.
+namespace parameter is copied to a new ordered mapping and the original
+object is discarded. The new copy is wrapped in a read-only proxy, which
+becomes the :attr:`~object.__dict__` attribute of the class object.
.. seealso::
@@ -2062,7 +2186,7 @@ left undefined.
(``+``, ``-``, ``*``, ``@``, ``/``, ``//``, ``%``, :func:`divmod`,
:func:`pow`, ``**``, ``<<``, ``>>``, ``&``, ``^``, ``|``) with reflected
(swapped) operands. These functions are only called if the left operand does
- not support the corresponding operation and the operands are of different
+ not support the corresponding operation [#]_ and the operands are of different
types. [#]_ For instance, to evaluate the expression ``x - y``, where *y* is
an instance of a class that has an :meth:`__rsub__` method, ``y.__rsub__(x)``
is called if ``x.__sub__(y)`` returns *NotImplemented*.
@@ -2478,6 +2602,17 @@ An example of an asynchronous context manager class::
controlled conditions. It generally isn't a good idea though, since it can
lead to some very strange behaviour if it is handled incorrectly.
+.. [#] The :meth:`__hash__`, :meth:`__iter__`, :meth:`__reversed__`, and
+ :meth:`__contains__` methods have special handling for this; others
+ will still raise a :exc:`TypeError`, but may do so by relying on
+ the behavior that ``None`` is not callable.
+
+.. [#] "Does not support" here means that the class has no such method, or
+ the method returns ``NotImplemented``. Do not set the method to
+ ``None`` if you want to force fallback to the right operand's reflected
+ method—that will instead have the opposite effect of explicitly
+ *blocking* such fallback.
+
.. [#] For operands of the same type, it is assumed that if the non-reflected method
(such as :meth:`__add__`) fails the operation is not supported, which is why the
reflected method is not called.
diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst
index 9792399..b37df92 100644
--- a/Doc/reference/expressions.rst
+++ b/Doc/reference/expressions.rst
@@ -172,7 +172,7 @@ Common syntax elements for comprehensions are:
.. productionlist::
comprehension: `expression` `comp_for`
- comp_for: "for" `target_list` "in" `or_test` [`comp_iter`]
+ comp_for: [ASYNC] "for" `target_list` "in" `or_test` [`comp_iter`]
comp_iter: `comp_for` | `comp_if`
comp_if: "if" `expression_nocond` [`comp_iter`]
@@ -186,6 +186,17 @@ each time the innermost block is reached.
Note that the comprehension is executed in a separate scope, so names assigned
to in the target list don't "leak" into the enclosing scope.
+Since Python 3.6, in an :keyword:`async def` function, an :keyword:`async for`
+clause may be used to iterate over a :term:`asynchronous iterator`.
+A comprehension in an :keyword:`async def` function may consist of either a
+:keyword:`for` or :keyword:`async for` clause following the leading
+expression, may contan additonal :keyword:`for` or :keyword:`async for`
+clauses, and may also use :keyword:`await` expressions.
+If a comprehension contains either :keyword:`async for` clauses
+or :keyword:`await` expressions it is called an
+:dfn:`asynchronous comprehension`. An asynchronous comprehension may
+suspend the execution of the coroutine function in which it appears.
+See also :pep:`530`.
.. _lists:
@@ -315,6 +326,14 @@ range(10) for y in bar(x))``.
The parentheses can be omitted on calls with only one argument. See section
:ref:`calls` for details.
+Since Python 3.6, if the generator appears in an :keyword:`async def` function,
+then :keyword:`async for` clauses and :keyword:`await` expressions are permitted
+as with an asynchronous comprehension. If a generator expression
+contains either :keyword:`async for` clauses or :keyword:`await` expressions
+it is called an :dfn:`asynchronous generator expression`.
+An asynchronous generator expression yields a new asynchronous
+generator object, which is an asynchronous iterator
+(see :ref:`async-iterators`).
.. _yieldexpr:
@@ -330,9 +349,22 @@ Yield expressions
yield_atom: "(" `yield_expression` ")"
yield_expression: "yield" [`expression_list` | "from" `expression`]
-The yield expression is only used when defining a :term:`generator` function and
+The yield expression is used when defining a :term:`generator` function
+or an :term:`asynchronous generator` function and
thus can only be used in the body of a function definition. Using a yield
-expression in a function's body causes that function to be a generator.
+expression in a function's body causes that function to be a generator,
+and using it in an :keyword:`async def` function's body causes that
+coroutine function to be an asynchronous generator. For example::
+
+ def gen(): # defines a generator function
+ yield 123
+
+ async def agen(): # defines an asynchronous generator function (PEP 525)
+ yield 123
+
+Generator functions are described below, while asynchronous generator
+functions are described separately in section
+:ref:`asynchronous-generator-functions`.
When a generator function is called, it returns an iterator known as a
generator. That generator then controls the execution of the generator function.
@@ -496,6 +528,134 @@ generator functions::
For examples using ``yield from``, see :ref:`pep-380` in "What's New in
Python."
+.. _asynchronous-generator-functions:
+
+Asynchronous generator functions
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+The presence of a yield expression in a function or method defined using
+:keyword:`async def` further defines the function as a
+:term:`asynchronous generator` function.
+
+When an asynchronous generator function is called, it returns an
+asynchronous iterator known as an asynchronous generator object.
+That object then controls the execution of the generator function.
+An asynchronous generator object is typically used in an
+:keyword:`async for` statement in a coroutine function analogously to
+how a generator object would be used in a :keyword:`for` statement.
+
+Calling one of the asynchronous generator's methods returns an
+:term:`awaitable` object, and the execution starts when this object
+is awaited on. At that time, the execution proceeds to the first yield
+expression, where it is suspended again, returning the value of
+:token:`expression_list` to the awaiting coroutine. As with a generator,
+suspension means that all local state is retained, including the
+current bindings of local variables, the instruction pointer, the internal
+evaluation stack, and the state of any exception handling. When the execution
+is resumed by awaiting on the next object returned by the asynchronous
+generator's methods, the function can proceed exactly as if the yield
+expression were just another external call. The value of the yield expression
+after resuming depends on the method which resumed the execution. If
+:meth:`~agen.__anext__` is used then the result is :const:`None`. Otherwise, if
+:meth:`~agen.asend` is used, then the result will be the value passed in to
+that method.
+
+In an asynchronous generator function, yield expressions are allowed anywhere
+in a :keyword:`try` construct. However, if an asynchronous generator is not
+resumed before it is finalized (by reaching a zero reference count or by
+being garbage collected), then a yield expression within a :keyword:`try`
+construct could result in a failure to execute pending :keyword:`finally`
+clauses. In this case, it is the responsibility of the event loop or
+scheduler running the asynchronous generator to call the asynchronous
+generator-iterator's :meth:`~agen.aclose` method and run the resulting
+coroutine object, thus allowing any pending :keyword:`finally` clauses
+to execute.
+
+To take care of finalization, an event loop should define
+a *finalizer* function which takes an asynchronous generator-iterator
+and presumably calls :meth:`~agen.aclose` and executes the coroutine.
+This *finalizer* may be registered by calling :func:`sys.set_asyncgen_hooks`.
+When first iterated over, an asynchronous generator-iterator will store the
+registered *finalizer* to be called upon finalization. For a reference example
+of a *finalizer* method see the implementation of
+``asyncio.Loop.shutdown_asyncgens`` in :source:`Lib/asyncio/base_events.py`.
+
+The expression ``yield from <expr>`` is a syntax error when used in an
+asynchronous generator function.
+
+.. index:: object: asynchronous-generator
+.. _asynchronous-generator-methods:
+
+Asynchronous generator-iterator methods
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+This subsection describes the methods of an asynchronous generator iterator,
+which are used to control the execution of a generator function.
+
+
+.. index:: exception: StopAsyncIteration
+
+.. coroutinemethod:: agen.__anext__()
+
+ Returns an awaitable which when run starts to execute the asynchronous
+ generator or resumes it at the last executed yield expression. When an
+ asynchronous generator function is resumed with a :meth:`~agen.__anext__`
+ method, the current yield expression always evaluates to :const:`None` in
+ the returned awaitable, which when run will continue to the next yield
+ expression. The value of the :token:`expression_list` of the yield
+ expression is the value of the :exc:`StopIteration` exception raised by
+ the completing coroutine. If the asynchronous generator exits without
+ yielding another value, the awaitable instead raises an
+ :exc:`StopAsyncIteration` exception, signalling that the asynchronous
+ iteration has completed.
+
+ This method is normally called implicitly by a :keyword:`async for` loop.
+
+
+.. coroutinemethod:: agen.asend(value)
+
+ Returns an awaitable which when run resumes the execution of the
+ asynchronous generator. As with the :meth:`~generator.send()` method for a
+ generator, this "sends" a value into the asynchronous generator function,
+ and the *value* argument becomes the result of the current yield expression.
+ The awaitable returned by the :meth:`asend` method will return the next
+ value yielded by the generator as the value of the raised
+ :exc:`StopIteration`, or raises :exc:`StopAsyncIteration` if the
+ asynchronous generator exits without yielding another value. When
+ :meth:`asend` is called to start the asynchronous
+ generator, it must be called with :const:`None` as the argument,
+ because there is no yield expression that could receive the value.
+
+
+.. coroutinemethod:: agen.athrow(type[, value[, traceback]])
+
+ Returns an awaitable that raises an exception of type ``type`` at the point
+ where the asynchronous generator was paused, and returns the next value
+ yielded by the generator function as the value of the raised
+ :exc:`StopIteration` exception. If the asynchronous generator exits
+ without yielding another value, an :exc:`StopAsyncIteration` exception is
+ raised by the awaitable.
+ If the generator function does not catch the passed-in exception, or
+ raises a different exception, then when the awaitalbe is run that exception
+ propagates to the caller of the awaitable.
+
+.. index:: exception: GeneratorExit
+
+
+.. coroutinemethod:: agen.aclose()
+
+ Returns an awaitable that when run will throw a :exc:`GeneratorExit` into
+ the asynchronous generator function at the point where it was paused.
+ If the asynchronous generator function then exits gracefully, is already
+ closed, or raises :exc:`GeneratorExit` (by not catching the exception),
+ then the returned awaitable will raise a :exc:`StopIteration` exception.
+ Any further awaitables returned by subsequent calls to the asynchronous
+ generator will raise a :exc:`StopAsyncIteration` exception. If the
+ asynchronous generator yields a value, a :exc:`RuntimeError` is raised
+ by the awaitable. If the asynchronous generator raises any other exception,
+ it is propagated to the caller of the awaitable. If the asynchronous
+ generator has already exited due to an exception or normal exit, then
+ further calls to :meth:`aclose` will return an awaitable that does nothing.
.. _primaries:
diff --git a/Doc/reference/import.rst b/Doc/reference/import.rst
index 64302b8..5e2c1c8 100644
--- a/Doc/reference/import.rst
+++ b/Doc/reference/import.rst
@@ -36,7 +36,7 @@ implement import semantics.
When a module is first imported, Python searches for the module and if found,
it creates a module object [#fnmo]_, initializing it. If the named module
-cannot be found, an :exc:`ImportError` is raised. Python implements various
+cannot be found, an :exc:`ModuleNotFoundError` is raised. Python implements various
strategies to search for the named module when the import machinery is
invoked. These strategies can be modified and extended by using various hooks
described in the sections below.
@@ -167,7 +167,7 @@ arguments to the :keyword:`import` statement, or from the parameters to the
This name will be used in various phases of the import search, and it may be
the dotted path to a submodule, e.g. ``foo.bar.baz``. In this case, Python
first tries to import ``foo``, then ``foo.bar``, and finally ``foo.bar.baz``.
-If any of the intermediate imports fail, an :exc:`ImportError` is raised.
+If any of the intermediate imports fail, an :exc:`ModuleNotFoundError` is raised.
The module cache
@@ -186,7 +186,7 @@ object.
During import, the module name is looked up in :data:`sys.modules` and if
present, the associated value is the module satisfying the import, and the
process completes. However, if the value is ``None``, then an
-:exc:`ImportError` is raised. If the module name is missing, Python will
+:exc:`ModuleNotFoundError` is raised. If the module name is missing, Python will
continue searching for the module.
:data:`sys.modules` is writable. Deleting a key may not destroy the
@@ -194,7 +194,7 @@ associated module (as other modules may hold references to it),
but it will invalidate the cache entry for the named module, causing
Python to search anew for the named module upon its next
import. The key can also be assigned to ``None``, forcing the next import
-of the module to result in an :exc:`ImportError`.
+of the module to result in an :exc:`ModuleNotFoundError`.
Beware though, as if you keep a reference to the module object,
invalidate its cache entry in :data:`sys.modules`, and then re-import the
@@ -288,8 +288,8 @@ the named module or not.
If the meta path finder knows how to handle the named module, it returns a
spec object. If it cannot handle the named module, it returns ``None``. If
:data:`sys.meta_path` processing reaches the end of its list without returning
-a spec, then an :exc:`ImportError` is raised. Any other exceptions raised
-are simply propagated up, aborting the import process.
+a spec, then a :exc:`ModuleNotFoundError` is raised. Any other exceptions
+raised are simply propagated up, aborting the import process.
The :meth:`~importlib.abc.MetaPathFinder.find_spec()` method of meta path
finders is called with two or three arguments. The first is the fully
@@ -298,9 +298,9 @@ The second argument is the path entries to use for the module search. For
top-level modules, the second argument is ``None``, but for submodules or
subpackages, the second argument is the value of the parent package's
``__path__`` attribute. If the appropriate ``__path__`` attribute cannot
-be accessed, an :exc:`ImportError` is raised. The third argument is an
-existing module object that will be the target of loading later. The
-import system passes in a target module only during reload.
+be accessed, an :exc:`ModuleNotFoundError` is raised. The third argument
+is an existing module object that will be the target of loading later.
+The import system passes in a target module only during reload.
The meta path may be traversed multiple times for a single import request.
For example, assuming none of the modules involved has already been cached,
@@ -554,19 +554,30 @@ the module.
details.
This attribute is used instead of ``__name__`` to calculate explicit
- relative imports for main modules, as defined in :pep:`366`.
+ relative imports for main modules, as defined in :pep:`366`. It is
+ expected to have the same value as ``__spec__.parent``.
+
+ .. versionchanged:: 3.6
+ The value of ``__package__`` is expected to be the same as
+ ``__spec__.parent``.
.. attribute:: __spec__
The ``__spec__`` attribute must be set to the module spec that was
- used when importing the module. This is used primarily for
- introspection and during reloading. Setting ``__spec__``
+ used when importing the module. Setting ``__spec__``
appropriately applies equally to :ref:`modules initialized during
interpreter startup <programs>`. The one exception is ``__main__``,
where ``__spec__`` is :ref:`set to None in some cases <main_spec>`.
+ When ``__package__`` is not defined, ``__spec__.parent`` is used as
+ a fallback.
+
.. versionadded:: 3.4
+ .. versionchanged:: 3.6
+ ``__spec__.parent`` is used as a fallback when ``__package__`` is
+ not defined.
+
.. attribute:: __path__
If the module is a package (either regular or namespace), the module
@@ -876,7 +887,7 @@ import statements within that module.
To selectively prevent import of some modules from a hook early on the
meta path (rather than disabling the standard import system entirely),
-it is sufficient to raise :exc:`ImportError` directly from
+it is sufficient to raise :exc:`ModuleNoFoundError` directly from
:meth:`~importlib.abc.MetaPathFinder.find_spec` instead of returning
``None``. The latter indicates that the meta path search should continue,
while raising an exception terminates it immediately.
diff --git a/Doc/reference/lexical_analysis.rst b/Doc/reference/lexical_analysis.rst
index 37f25f1..da7017a 100644
--- a/Doc/reference/lexical_analysis.rst
+++ b/Doc/reference/lexical_analysis.rst
@@ -405,7 +405,8 @@ String literals are described by the following lexical definitions:
.. productionlist::
stringliteral: [`stringprefix`](`shortstring` | `longstring`)
- stringprefix: "r" | "u" | "R" | "U"
+ stringprefix: "r" | "u" | "R" | "U" | "f" | "F"
+ : | "fr" | "Fr" | "fR" | "FR" | "rf" | "rF" | "Rf" | "RF"
shortstring: "'" `shortstringitem`* "'" | '"' `shortstringitem`* '"'
longstring: "'''" `longstringitem`* "'''" | '"""' `longstringitem`* '"""'
shortstringitem: `shortstringchar` | `stringescapeseq`
@@ -464,6 +465,11 @@ is not supported.
to simplify the maintenance of dual Python 2.x and 3.x codebases.
See :pep:`414` for more information.
+A string literal with ``'f'`` or ``'F'`` in its prefix is a
+:dfn:`formatted string literal`; see :ref:`f-strings`. The ``'f'`` may be
+combined with ``'r'``, but not with ``'b'`` or ``'u'``, therefore raw
+formatted strings are possible, but formatted bytes literals are not.
+
In triple-quoted literals, unescaped newlines and quotes are allowed (and are
retained), except that three unescaped quotes in a row terminate the literal. (A
"quote" is the character used to open the literal, i.e. either ``'`` or ``"``.)
@@ -554,6 +560,10 @@ is more easily recognized as broken.) It is also important to note that the
escape sequences only recognized in string literals fall into the category of
unrecognized escapes for bytes literals.
+ .. versionchanged:: 3.6
+ Unrecognized escape sequences produce a DeprecationWarning. In
+ some future version of Python they will be a SyntaxError.
+
Even in a raw literal, quotes can be escaped with a backslash, but the
backslash remains in the result; for example, ``r"\""`` is a valid string
literal consisting of two characters: a backslash and a double quote; ``r"\"``
@@ -583,7 +593,111 @@ comments to parts of strings, for example::
Note that this feature is defined at the syntactical level, but implemented at
compile time. The '+' operator must be used to concatenate string expressions
at run time. Also note that literal concatenation can use different quoting
-styles for each component (even mixing raw strings and triple quoted strings).
+styles for each component (even mixing raw strings and triple quoted strings),
+and formatted string literals may be concatenated with plain string literals.
+
+
+.. index::
+ single: formatted string literal
+ single: interpolated string literal
+ single: string; formatted literal
+ single: string; interpolated literal
+ single: f-string
+.. _f-strings:
+
+Formatted string literals
+-------------------------
+
+.. versionadded:: 3.6
+
+A :dfn:`formatted string literal` or :dfn:`f-string` is a string literal
+that is prefixed with ``'f'`` or ``'F'``. These strings may contain
+replacement fields, which are expressions delimited by curly braces ``{}``.
+While other string literals always have a constant value, formatted strings
+are really expressions evaluated at run time.
+
+Escape sequences are decoded like in ordinary string literals (except when
+a literal is also marked as a raw string). After decoding, the grammar
+for the contents of the string is:
+
+.. productionlist::
+ f_string: (`literal_char` | "{{" | "}}" | `replacement_field`)*
+ replacement_field: "{" `f_expression` ["!" `conversion`] [":" `format_spec`] "}"
+ f_expression: (`conditional_expression` | "*" `or_expr`)
+ : ("," `conditional_expression` | "," "*" `or_expr`)* [","]
+ : | `yield_expression`
+ conversion: "s" | "r" | "a"
+ format_spec: (`literal_char` | NULL | `replacement_field`)*
+ literal_char: <any code point except "{", "}" or NULL>
+
+The parts of the string outside curly braces are treated literally,
+except that any doubled curly braces ``'{{'`` or ``'}}'`` are replaced
+with the corresponding single curly brace. A single opening curly
+bracket ``'{'`` marks a replacement field, which starts with a
+Python expression. After the expression, there may be a conversion field,
+introduced by an exclamation point ``'!'``. A format specifier may also
+be appended, introduced by a colon ``':'``. A replacement field ends
+with a closing curly bracket ``'}'``.
+
+Expressions in formatted string literals are treated like regular
+Python expressions surrounded by parentheses, with a few exceptions.
+An empty expression is not allowed, and a :keyword:`lambda` expression
+must be surrounded by explicit parentheses. Replacement expressions
+can contain line breaks (e.g. in triple-quoted strings), but they
+cannot contain comments. Each expression is evaluated in the context
+where the formatted string literal appears, in order from left to right.
+
+If a conversion is specified, the result of evaluating the expression
+is converted before formatting. Conversion ``'!s'`` calls :func:`str` on
+the result, ``'!r'`` calls :func:`repr`, and ``'!a'`` calls :func:`ascii`.
+
+The result is then formatted using the :func:`format` protocol. The
+format specifier is passed to the :meth:`__format__` method of the
+expression or conversion result. An empty string is passed when the
+format specifier is omitted. The formatted result is then included in
+the final value of the whole string.
+
+Top-level format specifiers may include nested replacement fields.
+These nested fields may include their own conversion fields and
+format specifiers, but may not include more deeply-nested replacement fields.
+
+Formatted string literals may be concatenated, but replacement fields
+cannot be split across literals.
+
+Some examples of formatted string literals::
+
+ >>> name = "Fred"
+ >>> f"He said his name is {name!r}."
+ "He said his name is 'Fred'."
+ >>> f"He said his name is {repr(name)}." # repr() is equivalent to !r
+ "He said his name is 'Fred'."
+ >>> width = 10
+ >>> precision = 4
+ >>> value = decimal.Decimal("12.34567")
+ >>> f"result: {value:{width}.{precision}}" # nested fields
+ 'result: 12.35'
+
+A consequence of sharing the same syntax as regular string literals is
+that characters in the replacement fields must not conflict with the
+quoting used in the outer formatted string literal::
+
+ f"abc {a["x"]} def" # error: outer string literal ended prematurely
+ f"abc {a['x']} def" # workaround: use different quoting
+
+Backslashes are not allowed in format expressions and will raise
+an error::
+
+ f"newline: {ord('\n')}" # raises SyntaxError
+
+To include a value in which a backslash escape is required, create
+a temporary variable.
+
+ >>> newline = ord('\n')
+ >>> f"newline: {newline}"
+ 'newline: 10'
+
+See also :pep:`498` for the proposal that added formatted string literals,
+and :meth:`str.format`, which uses a related format string mechanism.
.. _numbers:
@@ -612,20 +726,24 @@ Integer literals
Integer literals are described by the following lexical definitions:
.. productionlist::
- integer: `decimalinteger` | `octinteger` | `hexinteger` | `bininteger`
- decimalinteger: `nonzerodigit` `digit`* | "0"+
+ integer: `decinteger` | `bininteger` | `octinteger` | `hexinteger`
+ decinteger: `nonzerodigit` (["_"] `digit`)* | "0"+ (["_"] "0")*
+ bininteger: "0" ("b" | "B") (["_"] `bindigit`)+
+ octinteger: "0" ("o" | "O") (["_"] `octdigit`)+
+ hexinteger: "0" ("x" | "X") (["_"] `hexdigit`)+
nonzerodigit: "1"..."9"
digit: "0"..."9"
- octinteger: "0" ("o" | "O") `octdigit`+
- hexinteger: "0" ("x" | "X") `hexdigit`+
- bininteger: "0" ("b" | "B") `bindigit`+
+ bindigit: "0" | "1"
octdigit: "0"..."7"
hexdigit: `digit` | "a"..."f" | "A"..."F"
- bindigit: "0" | "1"
There is no limit for the length of integer literals apart from what can be
stored in available memory.
+Underscores are ignored for determining the numeric value of the literal. They
+can be used to group digits for enhanced readability. One underscore can occur
+between digits, and after base specifiers like ``0x``.
+
Note that leading zeros in a non-zero decimal number are not allowed. This is
for disambiguation with C-style octal literals, which Python used before version
3.0.
@@ -634,6 +752,10 @@ Some examples of integer literals::
7 2147483647 0o177 0b100110111
3 79228162514264337593543950336 0o377 0xdeadbeef
+ 100_000_000_000 0b_1110_0101
+
+.. versionchanged:: 3.6
+ Underscores are now allowed for grouping purposes in literals.
.. _floating:
@@ -645,23 +767,28 @@ Floating point literals are described by the following lexical definitions:
.. productionlist::
floatnumber: `pointfloat` | `exponentfloat`
- pointfloat: [`intpart`] `fraction` | `intpart` "."
- exponentfloat: (`intpart` | `pointfloat`) `exponent`
- intpart: `digit`+
- fraction: "." `digit`+
- exponent: ("e" | "E") ["+" | "-"] `digit`+
+ pointfloat: [`digitpart`] `fraction` | `digitpart` "."
+ exponentfloat: (`digitpart` | `pointfloat`) `exponent`
+ digitpart: `digit` (["_"] `digit`)*
+ fraction: "." `digitpart`
+ exponent: ("e" | "E") ["+" | "-"] `digitpart`
Note that the integer and exponent parts are always interpreted using radix 10.
For example, ``077e010`` is legal, and denotes the same number as ``77e10``. The
-allowed range of floating point literals is implementation-dependent. Some
-examples of floating point literals::
+allowed range of floating point literals is implementation-dependent. As in
+integer literals, underscores are supported for digit grouping.
- 3.14 10. .001 1e100 3.14e-10 0e0
+Some examples of floating point literals::
+
+ 3.14 10. .001 1e100 3.14e-10 0e0 3.14_15_93
Note that numeric literals do not include a sign; a phrase like ``-1`` is
actually an expression composed of the unary operator ``-`` and the literal
``1``.
+.. versionchanged:: 3.6
+ Underscores are now allowed for grouping purposes in literals.
+
.. _imaginary:
@@ -671,7 +798,7 @@ Imaginary literals
Imaginary literals are described by the following lexical definitions:
.. productionlist::
- imagnumber: (`floatnumber` | `intpart`) ("j" | "J")
+ imagnumber: (`floatnumber` | `digitpart`) ("j" | "J")
An imaginary literal yields a complex number with a real part of 0.0. Complex
numbers are represented as a pair of floating point numbers and have the same
@@ -679,7 +806,7 @@ restrictions on their range. To create a complex number with a nonzero real
part, add a floating point number to it, e.g., ``(3+4j)``. Some examples of
imaginary literals::
- 3.14j 10.j 10j .001j 1e100j 3.14e-10j
+ 3.14j 10.j 10j .001j 1e100j 3.14e-10j 3.14_15_93j
.. _operators:
diff --git a/Doc/reference/simple_stmts.rst b/Doc/reference/simple_stmts.rst
index d403c4d..e152b16 100644
--- a/Doc/reference/simple_stmts.rst
+++ b/Doc/reference/simple_stmts.rst
@@ -16,6 +16,7 @@ simple statements is:
: | `assert_stmt`
: | `assignment_stmt`
: | `augmented_assignment_stmt`
+ : | `annotated_assignment_stmt`
: | `pass_stmt`
: | `del_stmt`
: | `return_stmt`
@@ -84,7 +85,7 @@ attributes or items of mutable objects:
assignment_stmt: (`target_list` "=")+ (`starred_expression` | `yield_expression`)
target_list: `target` ("," `target`)* [","]
target: `identifier`
- : | "(" `target_list` ")"
+ : | "(" [`target_list`] ")"
: | "[" [`target_list`] "]"
: | `attributeref`
: | `subscription`
@@ -312,6 +313,50 @@ For targets which are attribute references, the same :ref:`caveat about class
and instance attributes <attr-target-note>` applies as for regular assignments.
+.. _annassign:
+
+Annotated assignment statements
+-------------------------------
+
+.. index::
+ pair: annotated; assignment
+ single: statement; assignment, annotated
+
+Annotation assignment is the combination, in a single statement,
+of a variable or attribute annotation and an optional assignment statement:
+
+.. productionlist::
+ annotated_assignment_stmt: `augtarget` ":" `expression` ["=" `expression`]
+
+The difference from normal :ref:`assignment` is that only single target and
+only single right hand side value is allowed.
+
+For simple names as assignment targets, if in class or module scope,
+the annotations are evaluated and stored in a special class or module
+attribute :attr:`__annotations__`
+that is a dictionary mapping from variable names (mangled if private) to
+evaluated annotations. This attribute is writable and is automatically
+created at the start of class or module body execution, if annotations
+are found statically.
+
+For expressions as assignment targets, the annotations are evaluated if
+in class or module scope, but not stored.
+
+If a name is annotated in a function scope, then this name is local for
+that scope. Annotations are never evaluated and stored in function scopes.
+
+If the right hand side is present, an annotated
+assignment performs the actual assignment before evaluating annotations
+(where applicable). If the right hand side is not present for an expression
+target, then the interpreter evaluates the target except for the last
+:meth:`__setitem__` or :meth:`__setattr__` call.
+
+.. seealso::
+
+ :pep:`526` - Variable and attribute annotation syntax
+ :pep:`484` - Type hints
+
+
.. _assert:
The :keyword:`assert` statement
@@ -447,6 +492,10 @@ generator is done and will cause :exc:`StopIteration` to be raised. The returned
value (if any) is used as an argument to construct :exc:`StopIteration` and
becomes the :attr:`StopIteration.value` attribute.
+In an asynchronous generator function, an empty :keyword:`return` statement
+indicates that the asynchronous generator is done and will cause
+:exc:`StopAsyncIteration` to be raised. A non-empty :keyword:`return`
+statement is a syntax error in an asynchronous generator function.
.. _yield:
@@ -859,11 +908,12 @@ block textually preceding that :keyword:`global` statement.
Names listed in a :keyword:`global` statement must not be defined as formal
parameters or in a :keyword:`for` loop control target, :keyword:`class`
-definition, function definition, or :keyword:`import` statement.
+definition, function definition, :keyword:`import` statement, or variable
+annotation.
.. impl-detail::
- The current implementation does not enforce the two restrictions, but
+ The current implementation does not enforce some of these restriction, but
programs should not abuse this freedom, as future implementations may enforce
them or silently change the meaning of the program.
diff --git a/Doc/tools/extensions/pyspecific.py b/Doc/tools/extensions/pyspecific.py
index 6311283..273191b 100644
--- a/Doc/tools/extensions/pyspecific.py
+++ b/Doc/tools/extensions/pyspecific.py
@@ -34,7 +34,7 @@ import suspicious
ISSUE_URI = 'https://bugs.python.org/issue%s'
-SOURCE_URI = 'https://hg.python.org/cpython/file/3.5/%s'
+SOURCE_URI = 'https://hg.python.org/cpython/file/3.6/%s'
# monkey-patch reST parser to disable alphabetic and roman enumerated lists
from docutils.parsers.rst.states import Body
diff --git a/Doc/tools/rstlint.py b/Doc/tools/rstlint.py
index de78041..2c1816e 100755
--- a/Doc/tools/rstlint.py
+++ b/Doc/tools/rstlint.py
@@ -9,8 +9,6 @@
# TODO: - wrong versions in versionadded/changed
# - wrong markup after versionchanged directive
-from __future__ import with_statement
-
import os
import re
import sys
diff --git a/Doc/tools/susp-ignored.csv b/Doc/tools/susp-ignored.csv
index dba93bf..c6e0311 100644
--- a/Doc/tools/susp-ignored.csv
+++ b/Doc/tools/susp-ignored.csv
@@ -25,6 +25,30 @@ howto/curses,,:blue,"2:green, 3:yellow, 4:blue, 5:magenta, 6:cyan, and 7:white.
howto/curses,,:magenta,"2:green, 3:yellow, 4:blue, 5:magenta, 6:cyan, and 7:white. The"
howto/curses,,:cyan,"2:green, 3:yellow, 4:blue, 5:magenta, 6:cyan, and 7:white. The"
howto/curses,,:white,"2:green, 3:yellow, 4:blue, 5:magenta, 6:cyan, and 7:white. The"
+howto/instrumentation,,::,python$target:::function-entry
+howto/instrumentation,,:function,python$target:::function-entry
+howto/instrumentation,,::,python$target:::function-return
+howto/instrumentation,,:function,python$target:::function-return
+howto/instrumentation,,:call,156641360502280 function-entry:call_stack.py:start:23
+howto/instrumentation,,:start,156641360502280 function-entry:call_stack.py:start:23
+howto/instrumentation,,:function,156641360518804 function-entry: call_stack.py:function_1:1
+howto/instrumentation,,:function,156641360532797 function-entry: call_stack.py:function_3:9
+howto/instrumentation,,:function,156641360546807 function-return: call_stack.py:function_3:10
+howto/instrumentation,,:function,156641360563367 function-return: call_stack.py:function_1:2
+howto/instrumentation,,:function,156641360578365 function-entry: call_stack.py:function_2:5
+howto/instrumentation,,:function,156641360591757 function-entry: call_stack.py:function_1:1
+howto/instrumentation,,:function,156641360605556 function-entry: call_stack.py:function_3:9
+howto/instrumentation,,:function,156641360617482 function-return: call_stack.py:function_3:10
+howto/instrumentation,,:function,156641360629814 function-return: call_stack.py:function_1:2
+howto/instrumentation,,:function,156641360642285 function-return: call_stack.py:function_2:6
+howto/instrumentation,,:function,156641360656770 function-entry: call_stack.py:function_3:9
+howto/instrumentation,,:function,156641360669707 function-return: call_stack.py:function_3:10
+howto/instrumentation,,:function,156641360687853 function-entry: call_stack.py:function_4:13
+howto/instrumentation,,:function,156641360700719 function-return: call_stack.py:function_4:14
+howto/instrumentation,,:function,156641360719640 function-entry: call_stack.py:function_5:18
+howto/instrumentation,,:function,156641360732567 function-return: call_stack.py:function_5:21
+howto/instrumentation,,:call,156641360747370 function-return:call_stack.py:start:28
+howto/instrumentation,,:start,156641360747370 function-return:call_stack.py:start:28
howto/ipaddress,,:DB8,>>> ipaddress.ip_address('2001:DB8::1')
howto/ipaddress,,::,>>> ipaddress.ip_address('2001:DB8::1')
howto/ipaddress,,:db8,IPv6Address('2001:db8::1')
@@ -106,6 +130,10 @@ library/exceptions,,:err,err.object[err.start:err.end]
library/functions,,:step,a[start:stop:step]
library/functions,,:stop,"a[start:stop, i]"
library/functions,,:stop,a[start:stop:step]
+library/hashlib,,:vatrogasac,>>> cookie = b'user:vatrogasac'
+library/hashlib,,:vatrogasac,"user:vatrogasac,349cf904533767ed2d755279a8df84d0"
+library/hashlib,,:policajac,">>> compare_digest(b'user:policajac', sig)"
+library/hashlib,,:LEAF,"h00 = blake2b(buf[0:LEAF_SIZE], fanout=FANOUT, depth=DEPTH,"
library/http.client,,:port,host:port
library/http.cookies,,`,!#$%&'*+-.^_`|~:
library/imaplib,,:MM,"""DD-Mmm-YYYY HH:MM:SS"
@@ -161,6 +189,7 @@ library/pprint,,::,"'Programming Language :: Python :: 2.7',"
library/profile,,:lineno,filename:lineno(function)
library/pyexpat,,:elem1,<py:elem1 />
library/pyexpat,,:py,"xmlns:py = ""http://www.python.org/ns/"">"
+library/random,,:len,new_diff = mean(combined[:len(drug)]) - mean(combined[len(drug):])
library/smtplib,,:port,method must support that as well as a regular host:port
library/socket,,::,'5aef:2b::8'
library/socket,,:can,"return (can_id, can_dlc, data[:can_dlc])"
@@ -205,7 +234,8 @@ library/uuid,,:uuid,urn:uuid:12345678-1234-5678-1234-567812345678
library/venv,,:param,":param nodist: If True, setuptools and pip are not installed into the"
library/venv,,:param,":param progress: If setuptools or pip are installed, the progress of the"
library/venv,,:param,":param nopip: If True, pip is not installed into the created"
-library/venv,,:param,:param context: The information for the environment creation request
+library/venv,,:param,:param context: The information for the virtual environment
+library/xmlrpc.client,,:nil,ex:nil
library/xmlrpc.client,,:pass,http://user:pass@host:port/path
library/xmlrpc.client,,:pass,user:pass
library/xmlrpc.client,,:port,http://user:pass@host:port/path
@@ -239,7 +269,7 @@ using/cmdline,,:line,file:line: category: message
using/cmdline,,:message,action:message:category:module:line
using/cmdline,,:module,action:message:category:module:line
using/unix,,:Packaging,https://en.opensuse.org/Portal:Packaging
-whatsnew/2.0,418,:len,
+whatsnew/2.0,,:len,
whatsnew/2.3,,::,
whatsnew/2.3,,:config,
whatsnew/2.3,,:Critical,
@@ -272,27 +302,28 @@ whatsnew/3.2,,:feed,>>> urllib.parse.urlparse('http://[dead:beef:cafe:5417:affe:
whatsnew/3.2,,:gz,">>> with tarfile.open(name='myarchive.tar.gz', mode='w:gz') as tf:"
whatsnew/3.2,,:location,zope9-location = ${zope9:location}
whatsnew/3.2,,:prefix,zope-conf = ${custom:prefix}/etc/zope.conf
-whatsnew/changelog,,:gz,": TarFile opened with external fileobj and ""w:gz"" mode didn't"
-whatsnew/changelog,,::,": Use ""127.0.0.1"" or ""::1"" instead of ""localhost"" as much as"
-library/tarfile,149,:xz,'x:xz'
-library/xml.etree.elementtree,290,:sometag,prefix:sometag
-library/xml.etree.elementtree,301,:fictional,"<actors xmlns:fictional=""http://characters.example.com"""
-library/xml.etree.elementtree,301,:character,<fictional:character>Lancelot</fictional:character>
-library/xml.etree.elementtree,301,:character,<fictional:character>Archie Leach</fictional:character>
-library/xml.etree.elementtree,301,:character,<fictional:character>Sir Robin</fictional:character>
-library/xml.etree.elementtree,301,:character,<fictional:character>Gunther</fictional:character>
-library/xml.etree.elementtree,301,:character,<fictional:character>Commander Clement</fictional:character>
+library/tarfile,,:xz,'x:xz'
+library/xml.etree.elementtree,,:sometag,prefix:sometag
+library/xml.etree.elementtree,,:fictional,"<actors xmlns:fictional=""http://characters.example.com"""
+library/xml.etree.elementtree,,:character,<fictional:character>Lancelot</fictional:character>
+library/xml.etree.elementtree,,:character,<fictional:character>Archie Leach</fictional:character>
+library/xml.etree.elementtree,,:character,<fictional:character>Sir Robin</fictional:character>
+library/xml.etree.elementtree,,:character,<fictional:character>Gunther</fictional:character>
+library/xml.etree.elementtree,,:character,<fictional:character>Commander Clement</fictional:character>
library/xml.etree.elementtree,,:actor,"for actor in root.findall('real_person:actor', ns):"
library/xml.etree.elementtree,,:name,"name = actor.find('real_person:name', ns)"
library/xml.etree.elementtree,,:character,"for char in actor.findall('role:character', ns):"
-library/zipapp,31,:main,"$ python -m zipapp myapp -m ""myapp:main"""
-library/zipapp,82,:fn,"argument should have the form ""pkg.mod:fn"", where ""pkg.mod"" is a"
-library/zipapp,155,:callable,"""pkg.module:callable"" and the archive will be run by importing"
+library/zipapp,,:main,"$ python -m zipapp myapp -m ""myapp:main"""
+library/zipapp,,:fn,"argument should have the form ""pkg.mod:fn"", where ""pkg.mod"" is a"
+library/zipapp,,:callable,"""pkg.module:callable"" and the archive will be run by importing"
library/stdtypes,,::,>>> m[::2].tolist()
library/sys,,`,# ``wrapper`` creates a ``wrap(coro)`` coroutine:
-tutorial/venv,77,:c7b9645a6f35,"Python 3.4.3+ (3.4:c7b9645a6f35+, May 22 2015, 09:31:25)"
whatsnew/3.5,,:root,'WARNING:root:warning\n'
whatsnew/3.5,,:warning,'WARNING:root:warning\n'
whatsnew/3.5,,::,>>> addr6 = ipaddress.IPv6Address('::1')
whatsnew/3.5,,:root,ERROR:root:exception
whatsnew/3.5,,:exception,ERROR:root:exception
+whatsnew/changelog,,:version,import sys; I = version[:version.index(' ')]
+whatsnew/changelog,,:gz,": TarFile opened with external fileobj and ""w:gz"" mode didn't"
+whatsnew/changelog,,::,": Use ""127.0.0.1"" or ""::1"" instead of ""localhost"" as much as"
+whatsnew/changelog,,`,"for readability (was ""`"")."
diff --git a/Doc/tools/templates/indexsidebar.html b/Doc/tools/templates/indexsidebar.html
index b7bcd74..413c0a7 100644
--- a/Doc/tools/templates/indexsidebar.html
+++ b/Doc/tools/templates/indexsidebar.html
@@ -3,7 +3,7 @@
<h3>{% trans %}Docs for other versions{% endtrans %}</h3>
<ul>
<li><a href="https://docs.python.org/2.7/">{% trans %}Python 2.7 (stable){% endtrans %}</a></li>
- <li><a href="https://docs.python.org/3.6/">{% trans %}Python 3.6 (stable){% endtrans %}</a></li>
+ <li><a href="https://docs.python.org/3.5/">{% trans %}Python 3.5 (stable){% endtrans %}</a></li>
<li><a href="https://docs.python.org/3.7/">{% trans %}Python 3.7 (in development){% endtrans %}</a></li>
<li><a href="https://www.python.org/doc/versions/">{% trans %}Old versions{% endtrans %}</a></li>
</ul>
diff --git a/Doc/tutorial/controlflow.rst b/Doc/tutorial/controlflow.rst
index 12989b2..d434618 100644
--- a/Doc/tutorial/controlflow.rst
+++ b/Doc/tutorial/controlflow.rst
@@ -78,6 +78,9 @@ slice notation makes this especially convenient::
>>> words
['defenestrate', 'cat', 'window', 'defenestrate']
+With ``for w in words:``, the example would attempt to create an infinite list,
+inserting ``defenestrate`` over and over again.
+
.. _tut-range:
diff --git a/Doc/tutorial/datastructures.rst b/Doc/tutorial/datastructures.rst
index b39bdf4..953a68b 100644
--- a/Doc/tutorial/datastructures.rst
+++ b/Doc/tutorial/datastructures.rst
@@ -60,11 +60,16 @@ objects:
Remove all items from the list. Equivalent to ``del a[:]``.
-.. method:: list.index(x)
+.. method:: list.index(x[, start[, end]])
:noindex:
- Return the index in the list of the first item whose value is *x*. It is an
- error if there is no such item.
+ Return zero-based index in the list of the first item whose value is *x*.
+ Raises a :exc:`ValueError` if there is no such item.
+
+ The optional arguments *start* and *end* are interpreted as in the slice
+ notation and are used to limit the search to a particular subsequence of
+ *x*. The returned index is computed relative to the beginning of the full
+ sequence rather than the *start* argument.
.. method:: list.count(x)
@@ -94,28 +99,26 @@ objects:
An example that uses most of the list methods::
- >>> a = [66.25, 333, 333, 1, 1234.5]
- >>> print(a.count(333), a.count(66.25), a.count('x'))
- 2 1 0
- >>> a.insert(2, -1)
- >>> a.append(333)
- >>> a
- [66.25, 333, -1, 333, 1, 1234.5, 333]
- >>> a.index(333)
- 1
- >>> a.remove(333)
- >>> a
- [66.25, -1, 333, 1, 1234.5, 333]
- >>> a.reverse()
- >>> a
- [333, 1234.5, 1, 333, -1, 66.25]
- >>> a.sort()
- >>> a
- [-1, 1, 66.25, 333, 333, 1234.5]
- >>> a.pop()
- 1234.5
- >>> a
- [-1, 1, 66.25, 333, 333]
+ >>> fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
+ >>> fruits.count('apple')
+ 2
+ >>> fruits.count('tangerine')
+ 0
+ >>> fruits.index('banana')
+ 3
+ >>> fruits.index('banana', 4) # Find next banana starting a position 4
+ 6
+ >>> fruits.reverse()
+ >>> fruits
+ ['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange']
+ >>> fruits.append('grape')
+ >>> fruits
+ ['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange', 'grape']
+ >>> fruits.sort()
+ >>> fruits
+ ['apple', 'apple', 'banana', 'banana', 'grape', 'kiwi', 'orange', 'pear']
+ >>> fruits.pop()
+ 'pear'
You might have noticed that methods like ``insert``, ``remove`` or ``sort`` that
only modify the list have no return value printed -- they return the default
diff --git a/Doc/tutorial/errors.rst b/Doc/tutorial/errors.rst
index 759588f..aba61da 100644
--- a/Doc/tutorial/errors.rst
+++ b/Doc/tutorial/errors.rst
@@ -174,7 +174,7 @@ example::
for arg in sys.argv[1:]:
try:
f = open(arg, 'r')
- except IOError:
+ except OSError:
print('cannot open', arg)
else:
print(arg, 'has', len(f.readlines()), 'lines')
diff --git a/Doc/tutorial/inputoutput.rst b/Doc/tutorial/inputoutput.rst
index dd9c7cd..beeaac3 100644
--- a/Doc/tutorial/inputoutput.rst
+++ b/Doc/tutorial/inputoutput.rst
@@ -25,7 +25,8 @@ first way is to do all the string handling yourself; using string slicing and
concatenation operations you can create any layout you can imagine. The
string type has some methods that perform useful operations for padding
strings to a given column width; these will be discussed shortly. The second
-way is to use the :meth:`str.format` method.
+way is to use :ref:`formatted string literals <f-strings>`, or the
+:meth:`str.format` method.
The :mod:`string` module contains a :class:`~string.Template` class which offers
yet another way to substitute values into strings.
diff --git a/Doc/tutorial/interpreter.rst b/Doc/tutorial/interpreter.rst
index e8d8e2b..faf57a3 100644
--- a/Doc/tutorial/interpreter.rst
+++ b/Doc/tutorial/interpreter.rst
@@ -10,13 +10,13 @@ Using the Python Interpreter
Invoking the Interpreter
========================
-The Python interpreter is usually installed as :file:`/usr/local/bin/python3.5`
+The Python interpreter is usually installed as :file:`/usr/local/bin/python3.6`
on those machines where it is available; putting :file:`/usr/local/bin` in your
Unix shell's search path makes it possible to start it by typing the command:
.. code-block:: text
- python3.5
+ python3.6
to the shell. [#]_ Since the choice of the directory where the interpreter lives
is an installation option, other places are possible; check with your local
@@ -24,11 +24,11 @@ Python guru or system administrator. (E.g., :file:`/usr/local/python` is a
popular alternative location.)
On Windows machines, the Python installation is usually placed in
-:file:`C:\\Python35`, though you can change this when you're running the
+:file:`C:\\Python36`, though you can change this when you're running the
installer. To add this directory to your path, you can type the following
command into the command prompt in a DOS box::
- set path=%path%;C:\python35
+ set path=%path%;C:\python36
Typing an end-of-file character (:kbd:`Control-D` on Unix, :kbd:`Control-Z` on
Windows) at the primary prompt causes the interpreter to exit with a zero exit
@@ -98,8 +98,8 @@ before printing the first prompt:
.. code-block:: shell-session
- $ python3.5
- Python 3.5 (default, Sep 16 2015, 09:25:04)
+ $ python3.6
+ Python 3.6 (default, Sep 16 2015, 09:25:04)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
diff --git a/Doc/tutorial/introduction.rst b/Doc/tutorial/introduction.rst
index 2140329..7e8ee3e 100644
--- a/Doc/tutorial/introduction.rst
+++ b/Doc/tutorial/introduction.rst
@@ -352,6 +352,9 @@ The built-in function :func:`len` returns the length of a string::
Strings support a large number of methods for
basic transformations and searching.
+ :ref:`f-strings`
+ String literals that have embedded expressions.
+
:ref:`formatstrings`
Information about string formatting with :meth:`str.format`.
diff --git a/Doc/tutorial/stdlib.rst b/Doc/tutorial/stdlib.rst
index 52ffdbe..1dd06c2 100644
--- a/Doc/tutorial/stdlib.rst
+++ b/Doc/tutorial/stdlib.rst
@@ -15,7 +15,7 @@ operating system::
>>> import os
>>> os.getcwd() # Return the current working directory
- 'C:\\Python35'
+ 'C:\\Python36'
>>> os.chdir('/server/accesslogs') # Change current working directory
>>> os.system('mkdir today') # Run the command mkdir in the system shell
0
diff --git a/Doc/tutorial/stdlib2.rst b/Doc/tutorial/stdlib2.rst
index f733ffc..bf02c71 100644
--- a/Doc/tutorial/stdlib2.rst
+++ b/Doc/tutorial/stdlib2.rst
@@ -278,7 +278,7 @@ applications include caching objects that are expensive to create::
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
d['primary'] # entry was automatically removed
- File "C:/python35/lib/weakref.py", line 46, in __getitem__
+ File "C:/python36/lib/weakref.py", line 46, in __getitem__
o = self.data[key]()
KeyError: 'primary'
diff --git a/Doc/tutorial/venv.rst b/Doc/tutorial/venv.rst
index 3b2ee2e..e2dd57d 100644
--- a/Doc/tutorial/venv.rst
+++ b/Doc/tutorial/venv.rst
@@ -20,15 +20,14 @@ the requirements of every application. If application A needs version
the requirements are in conflict and installing either version 1.0 or 2.0
will leave one application unable to run.
-The solution for this problem is to create a :term:`virtual
-environment` (often shortened to "virtualenv"), a self-contained
-directory tree that contains a Python installation for a particular
-version of Python, plus a number of additional packages.
+The solution for this problem is to create a :term:`virtual environment`, a
+self-contained directory tree that contains a Python installation for a
+particular version of Python, plus a number of additional packages.
Different applications can then use different virtual environments.
To resolve the earlier example of conflicting requirements,
application A can have its own virtual environment with version 1.0
-installed while application B has another virtualenv with version 2.0.
+installed while application B has another virtual environment with version 2.0.
If application B requires a library be upgraded to version 3.0, this will
not affect application A's environment.
@@ -36,29 +35,26 @@ not affect application A's environment.
Creating Virtual Environments
=============================
-The script used to create and manage virtual environments is called
-:program:`pyvenv`. :program:`pyvenv` will usually install the most
-recent version of Python that you have available; the script is also
-installed with a version number, so if you have multiple versions of
-Python on your system you can select a specific Python version by
-running ``pyvenv-3.4`` or whichever version you want.
+The module used to create and manage virtual environments is called
+:mod:`venv`. :mod:`venv` will usually install the most recent version of
+Python that you have available. If you have multiple versions of Python on your
+system, you can select a specific Python version by running ``python3`` or
+whichever version you want.
-To create a virtualenv, decide upon a directory
-where you want to place it and run :program:`pyvenv` with the
-directory path::
+To create a virtual environment, decide upon a directory where you want to
+place it, and run the :mod:`venv` module as a script with the directory path::
- pyvenv tutorial-env
+ python3 -m venv tutorial-env
This will create the ``tutorial-env`` directory if it doesn't exist,
and also create directories inside it containing a copy of the Python
interpreter, the standard library, and various supporting files.
-Once you've created a virtual environment, you need to
-activate it.
+Once you've created a virtual environment, you may activate it.
On Windows, run::
- tutorial-env/Scripts/activate
+ tutorial-env\Scripts\activate.bat
On Unix or MacOS, run::
@@ -69,33 +65,36 @@ On Unix or MacOS, run::
``activate.csh`` and ``activate.fish`` scripts you should use
instead.)
-Activating the virtualenv will change your shell's prompt to show what
-virtualenv you're using, and modify the environment so that running
-``python`` will get you that particular version and installation of
-Python. For example::
+Activating the virtual environment will change your shell's prompt to show what
+virtual environment you're using, and modify the environment so that running
+``python`` will get you that particular version and installation of Python.
+For example:
- -> source ~/envs/tutorial-env/bin/activate
- (tutorial-env) -> python
- Python 3.4.3+ (3.4:c7b9645a6f35+, May 22 2015, 09:31:25)
+.. code-block:: bash
+
+ $ source ~/envs/tutorial-env/bin/activate
+ (tutorial-env) $ python
+ Python 3.5.1 (default, May 6 2016, 10:59:36)
...
>>> import sys
>>> sys.path
- ['', '/usr/local/lib/python34.zip', ...,
- '~/envs/tutorial-env/lib/python3.4/site-packages']
+ ['', '/usr/local/lib/python35.zip', ...,
+ '~/envs/tutorial-env/lib/python3.5/site-packages']
>>>
Managing Packages with pip
==========================
-Once you've activated a virtual environment, you can install, upgrade,
-and remove packages using a program called :program:`pip`. By default
-``pip`` will install packages from the Python Package Index,
-<https://pypi.python.org/pypi>. You can browse the Python Package Index
-by going to it in your web browser, or you can use ``pip``'s
-limited search feature::
+You can install, upgrade, and remove packages using a program called
+:program:`pip`. By default ``pip`` will install packages from the Python
+Package Index, <https://pypi.python.org/pypi>. You can browse the Python
+Package Index by going to it in your web browser, or you can use ``pip``'s
+limited search feature:
+
+.. code-block:: bash
- (tutorial-env) -> pip search astronomy
+ (tutorial-env) $ pip search astronomy
skyfield - Elegant astronomy for Python
gary - Galactic astronomy and gravitational dynamics.
novas - The United States Naval Observatory NOVAS astronomy library
@@ -107,9 +106,11 @@ limited search feature::
"freeze", etc. (Consult the :ref:`installing-index` guide for
complete documentation for ``pip``.)
-You can install the latest version of a package by specifying a package's name::
+You can install the latest version of a package by specifying a package's name:
+
+.. code-block:: bash
- -> pip install novas
+ (tutorial-env) $ pip install novas
Collecting novas
Downloading novas-3.1.1.3.tar.gz (136kB)
Installing collected packages: novas
@@ -117,9 +118,11 @@ You can install the latest version of a package by specifying a package's name::
Successfully installed novas-3.1.1.3
You can also install a specific version of a package by giving the
-package name followed by ``==`` and the version number::
+package name followed by ``==`` and the version number:
- -> pip install requests==2.6.0
+.. code-block:: bash
+
+ (tutorial-env) $ pip install requests==2.6.0
Collecting requests==2.6.0
Using cached requests-2.6.0-py2.py3-none-any.whl
Installing collected packages: requests
@@ -128,9 +131,11 @@ package name followed by ``==`` and the version number::
If you re-run this command, ``pip`` will notice that the requested
version is already installed and do nothing. You can supply a
different version number to get that version, or you can run ``pip
-install --upgrade`` to upgrade the package to the latest version::
+install --upgrade`` to upgrade the package to the latest version:
+
+.. code-block:: bash
- -> pip install --upgrade requests
+ (tutorial-env) $ pip install --upgrade requests
Collecting requests
Installing collected packages: requests
Found existing installation: requests 2.6.0
@@ -141,9 +146,11 @@ install --upgrade`` to upgrade the package to the latest version::
``pip uninstall`` followed by one or more package names will remove the
packages from the virtual environment.
-``pip show`` will display information about a particular package::
+``pip show`` will display information about a particular package:
- (tutorial-env) -> pip show requests
+.. code-block:: bash
+
+ (tutorial-env) $ pip show requests
---
Metadata-Version: 2.0
Name: requests
@@ -157,9 +164,11 @@ packages from the virtual environment.
Requires:
``pip list`` will display all of the packages installed in the virtual
-environment::
+environment:
+
+.. code-block:: bash
- (tutorial-env) -> pip list
+ (tutorial-env) $ pip list
novas (3.1.1.3)
numpy (1.9.2)
pip (7.0.3)
@@ -168,19 +177,23 @@ environment::
``pip freeze`` will produce a similar list of the installed packages,
but the output uses the format that ``pip install`` expects.
-A common convention is to put this list in a ``requirements.txt`` file::
+A common convention is to put this list in a ``requirements.txt`` file:
- (tutorial-env) -> pip freeze > requirements.txt
- (tutorial-env) -> cat requirements.txt
+.. code-block:: bash
+
+ (tutorial-env) $ pip freeze > requirements.txt
+ (tutorial-env) $ cat requirements.txt
novas==3.1.1.3
numpy==1.9.2
requests==2.7.0
The ``requirements.txt`` can then be committed to version control and
shipped as part of an application. Users can then install all the
-necessary packages with ``install -r``::
+necessary packages with ``install -r``:
+
+.. code-block:: bash
- -> pip install -r requirements.txt
+ (tutorial-env) $ pip install -r requirements.txt
Collecting novas==3.1.1.3 (from -r requirements.txt (line 1))
...
Collecting numpy==1.9.2 (from -r requirements.txt (line 2))
diff --git a/Doc/using/cmdline.rst b/Doc/using/cmdline.rst
index 6117796..c0e64d6 100644
--- a/Doc/using/cmdline.rst
+++ b/Doc/using/cmdline.rst
@@ -180,8 +180,15 @@ Generic options
Print the Python version number and exit. Example output could be::
- Python 3.0
+ Python 3.6.0b2+
+ When given twice, print more information about the build, like::
+
+ Python 3.6.0b2+ (3.6:84a3c5003510+, Oct 26 2016, 02:33:55)
+ [GCC 6.2.0 20161005]
+
+ .. versionadded:: 3.6
+ The ``-VV`` option.
.. _using-on-misc-options:
@@ -396,6 +403,8 @@ Miscellaneous options
stored in a traceback of a trace. Use ``-X tracemalloc=NFRAME`` to start
tracing with a traceback limit of *NFRAME* frames. See the
:func:`tracemalloc.start` for more information.
+ * ``-X showalloccount`` to enable the output of the total count of allocated
+ objects for each type (only works when built with ``COUNT_ALLOCS`` defined);
It also allows passing arbitrary values and retrieving them through the
:data:`sys._xoptions` dictionary.
@@ -409,6 +418,9 @@ Miscellaneous options
.. versionadded:: 3.4
The ``-X showrefcount`` and ``-X tracemalloc`` options.
+ .. versionadded:: 3.6
+ The ``-X showalloccount`` option.
+
Options you shouldn't use
~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -555,6 +567,10 @@ conflict.
.. versionchanged:: 3.4
The ``encodingname`` part is now optional.
+ .. versionchanged:: 3.6
+ On Windows, the encoding specified by this variable is ignored for interactive
+ console buffers unless :envvar:`PYTHONLEGACYWINDOWSIOENCODING` is also specified.
+ Files and pipes redirected through the standard streams are not affected.
.. envvar:: PYTHONNOUSERSITE
@@ -620,6 +636,81 @@ conflict.
.. versionadded:: 3.4
+.. envvar:: PYTHONMALLOC
+
+ Set the Python memory allocators and/or install debug hooks.
+
+ Set the family of memory allocators used by Python:
+
+ * ``malloc``: use the :c:func:`malloc` function of the C library
+ for all domains (:c:data:`PYMEM_DOMAIN_RAW`, :c:data:`PYMEM_DOMAIN_MEM`,
+ :c:data:`PYMEM_DOMAIN_OBJ`).
+ * ``pymalloc``: use the :ref:`pymalloc allocator <pymalloc>` for
+ :c:data:`PYMEM_DOMAIN_MEM` and :c:data:`PYMEM_DOMAIN_OBJ` domains and use
+ the :c:func:`malloc` function for the :c:data:`PYMEM_DOMAIN_RAW` domain.
+
+ Install debug hooks:
+
+ * ``debug``: install debug hooks on top of the default memory allocator
+ * ``malloc_debug``: same as ``malloc`` but also install debug hooks
+ * ``pymalloc_debug``: same as ``pymalloc`` but also install debug hooks
+
+ When Python is compiled in release mode, the default is ``pymalloc``. When
+ compiled in debug mode, the default is ``pymalloc_debug`` and the debug hooks
+ are used automatically.
+
+ If Python is configured without ``pymalloc`` support, ``pymalloc`` and
+ ``pymalloc_debug`` are not available, the default is ``malloc`` in release
+ mode and ``malloc_debug`` in debug mode.
+
+ See the :c:func:`PyMem_SetupDebugHooks` function for debug hooks on Python
+ memory allocators.
+
+ .. versionadded:: 3.6
+
+
+.. envvar:: PYTHONMALLOCSTATS
+
+ If set to a non-empty string, Python will print statistics of the
+ :ref:`pymalloc memory allocator <pymalloc>` every time a new pymalloc object
+ arena is created, and on shutdown.
+
+ This variable is ignored if the :envvar:`PYTHONMALLOC` environment variable
+ is used to force the :c:func:`malloc` allocator of the C library, or if
+ Python is configured without ``pymalloc`` support.
+
+ .. versionchanged:: 3.6
+ This variable can now also be used on Python compiled in release mode.
+ It now has no effect if set to an empty string.
+
+
+.. envvar:: PYTHONLEGACYWINDOWSFSENCODING
+
+ If set to a non-empty string, the default filesystem encoding and errors mode
+ will revert to their pre-3.6 values of 'mbcs' and 'replace', respectively.
+ Otherwise, the new defaults 'utf-8' and 'surrogatepass' are used.
+
+ This may also be enabled at runtime with
+ :func:`sys._enablelegacywindowsfsencoding()`.
+
+ Availability: Windows
+
+ .. versionadded:: 3.6
+ See :pep:`529` for more details.
+
+.. envvar:: PYTHONLEGACYWINDOWSIOENCODING
+
+ If set to a non-empty string, does not use the new console reader and
+ writer. This means that Unicode characters will be encoded according to
+ the active console code page, rather than using utf-8.
+
+ This variable is ignored if the standard streams are redirected (to files
+ or pipes) rather than referring to console buffers.
+
+ Availability: Windows
+
+ .. versionadded:: 3.6
+
Debug-mode variables
~~~~~~~~~~~~~~~~~~~~
@@ -635,9 +726,3 @@ if Python was configured with the ``--with-pydebug`` build option.
If set, Python will dump objects and reference counts still alive after
shutting down the interpreter.
-
-
-.. envvar:: PYTHONMALLOCSTATS
-
- If set, Python will print memory allocation statistics every time a new
- object arena is created, and on shutdown.
diff --git a/Doc/using/index.rst b/Doc/using/index.rst
index 502afa9..a5df713 100644
--- a/Doc/using/index.rst
+++ b/Doc/using/index.rst
@@ -17,4 +17,3 @@ interpreter and things that make working with Python easier.
unix.rst
windows.rst
mac.rst
- scripts.rst
diff --git a/Doc/using/mac.rst b/Doc/using/mac.rst
index 05c91bb..8f1ac3f 100644
--- a/Doc/using/mac.rst
+++ b/Doc/using/mac.rst
@@ -25,7 +25,7 @@ there.
What you get after installing is a number of things:
-* A :file:`MacPython 3.5` folder in your :file:`Applications` folder. In here
+* A :file:`MacPython 3.6` folder in your :file:`Applications` folder. In here
you find IDLE, the development environment that is a standard part of official
Python distributions; PythonLauncher, which handles double-clicking Python
scripts from the Finder; and the "Build Applet" tool, which allows you to
@@ -93,7 +93,7 @@ aware of: programs that talk to the Aqua window manager (in other words,
anything that has a GUI) need to be run in a special way. Use :program:`pythonw`
instead of :program:`python` to start such scripts.
-With Python 3.5, you can use either :program:`python` or :program:`pythonw`.
+With Python 3.6, you can use either :program:`python` or :program:`pythonw`.
Configuration
@@ -159,7 +159,7 @@ https://riverbankcomputing.com/software/pyqt/intro.
Distributing Python Applications on the Mac
===========================================
-The "Build Applet" tool that is placed in the MacPython 3.5 folder is fine for
+The "Build Applet" tool that is placed in the MacPython 3.6 folder is fine for
packaging small Python scripts on your own machine to run as a standard Mac
application. This tool, however, is not robust enough to distribute Python
applications to other users.
diff --git a/Doc/using/scripts.rst b/Doc/using/scripts.rst
deleted file mode 100644
index 2c87416..0000000
--- a/Doc/using/scripts.rst
+++ /dev/null
@@ -1,12 +0,0 @@
-.. _tools-and-scripts:
-
-Additional Tools and Scripts
-============================
-
-.. _scripts-pyvenv:
-
-pyvenv - Creating virtual environments
---------------------------------------
-
-.. include:: venv-create.inc
-
diff --git a/Doc/using/venv-create.inc b/Doc/using/venv-create.inc
index 7ad3008..53f431b 100644
--- a/Doc/using/venv-create.inc
+++ b/Doc/using/venv-create.inc
@@ -1,31 +1,39 @@
Creation of :ref:`virtual environments <venv-def>` is done by executing the
-``pyvenv`` script::
+command ``venv``::
- pyvenv /path/to/new/virtual/environment
+ python3 -m venv /path/to/new/virtual/environment
Running this command creates the target directory (creating any parent
directories that don't exist already) and places a ``pyvenv.cfg`` file in it
-with a ``home`` key pointing to the Python installation the command was run
-from. It also creates a ``bin`` (or ``Scripts`` on Windows) subdirectory
+with a ``home`` key pointing to the Python installation from which the command
+was run. It also creates a ``bin`` (or ``Scripts`` on Windows) subdirectory
containing a copy of the ``python`` binary (or binaries, in the case of
Windows). It also creates an (initially empty) ``lib/pythonX.Y/site-packages``
subdirectory (on Windows, this is ``Lib\site-packages``).
+.. deprecated:: 3.6
+ ``pyvenv`` was the recommended tool for creating virtual environments for
+ Python 3.3 and 3.4, and is `deprecated in Python 3.6
+ <https://docs.python.org/dev/whatsnew/3.6.html#deprecated-features>`_.
+
+.. versionchanged:: 3.5
+ The use of ``venv`` is now recommended for creating virtual environments.
+
.. seealso::
`Python Packaging User Guide: Creating and using virtual environments
- <https://packaging.python.org/en/latest/installing/#creating-virtual-environments>`__
+ <https://packaging.python.org/installing/#creating-virtual-environments>`__
.. highlight:: none
-On Windows, you may have to invoke the ``pyvenv`` script as follows, if you
-don't have the relevant PATH and PATHEXT settings::
+On Windows, invoke the ``venv`` command as follows::
- c:\Temp>c:\Python35\python c:\Python35\Tools\Scripts\pyvenv.py myenv
+ c:\>c:\Python35\python -m venv c:\path\to\myenv
-or equivalently::
+Alternatively, if you configured the ``PATH`` and ``PATHEXT`` variables for
+your :ref:`Python installation <using-on-windows>`::
- c:\Temp>c:\Python35\python -m venv myenv
+ c:\>python -m venv myenv c:\path\to\myenv
The command, if run with ``-h``, will show the available options::
@@ -36,25 +44,26 @@ The command, if run with ``-h``, will show the available options::
Creates virtual Python environments in one or more target directories.
positional arguments:
- ENV_DIR A directory to create the environment in.
+ ENV_DIR A directory to create the environment in.
optional arguments:
- -h, --help show this help message and exit
- --system-site-packages Give the virtual environment access to the system
- site-packages dir.
- --symlinks Try to use symlinks rather than copies, when symlinks
- are not the default for the platform.
- --copies Try to use copies rather than symlinks, even when
- symlinks are the default for the platform.
- --clear Delete the contents of the environment directory if it
- already exists, before environment creation.
- --upgrade Upgrade the environment directory to use this version
- of Python, assuming Python has been upgraded in-place.
- --without-pip Skips installing or upgrading pip in the virtual
- environment (pip is bootstrapped by default)
-
-Depending on how the ``venv`` functionality has been invoked, the usage message
-may vary slightly, e.g. referencing ``pyvenv`` rather than ``venv``.
+ -h, --help show this help message and exit
+ --system-site-packages
+ Give the virtual environment access to the system
+ site-packages dir.
+ --symlinks Try to use symlinks rather than copies, when symlinks
+ are not the default for the platform.
+ --copies Try to use copies rather than symlinks, even when
+ symlinks are the default for the platform.
+ --clear Delete the contents of the environment directory if it
+ already exists, before environment creation.
+ --upgrade Upgrade the environment directory to use this version
+ of Python, assuming Python has been upgraded in-place.
+ --without-pip Skips installing or upgrading pip in the virtual
+ environment (pip is bootstrapped by default)
+
+ Once an environment has been created, you may wish to activate it, e.g. by
+ sourcing an activate script in its bin directory.
.. versionchanged:: 3.4
Installs pip by default, added the ``--without-pip`` and ``--copies``
@@ -73,12 +82,13 @@ run with the ``--system-site-packages`` option, ``false`` otherwise.
Unless the ``--without-pip`` option is given, :mod:`ensurepip` will be
invoked to bootstrap ``pip`` into the virtual environment.
-Multiple paths can be given to ``pyvenv``, in which case an identical
-virtualenv will be created, according to the given options, at each
-provided path.
+Multiple paths can be given to ``venv``, in which case an identical virtual
+environment will be created, according to the given options, at each provided
+path.
-Once a venv has been created, it can be "activated" using a script in the
-venv's binary directory. The invocation of the script is platform-specific:
+Once a virtual environment has been created, it can be "activated" using a
+script in the virtual environment's binary directory. The invocation of the
+script is platform-specific:
+-------------+-----------------+-----------------------------------------+
| Platform | Shell | Command to activate virtual environment |
@@ -95,16 +105,17 @@ venv's binary directory. The invocation of the script is platform-specific:
+-------------+-----------------+-----------------------------------------+
You don't specifically *need* to activate an environment; activation just
-prepends the venv's binary directory to your path, so that "python" invokes the
-venv's Python interpreter and you can run installed scripts without having to
-use their full path. However, all scripts installed in a venv should be
-runnable without activating it, and run with the venv's Python automatically.
-
-You can deactivate a venv by typing "deactivate" in your shell. The exact
-mechanism is platform-specific: for example, the Bash activation script defines
-a "deactivate" function, whereas on Windows there are separate scripts called
-``deactivate.bat`` and ``Deactivate.ps1`` which are installed when the venv is
-created.
+prepends the virtual environment's binary directory to your path, so that
+"python" invokes the virtual environment's Python interpreter and you can run
+installed scripts without having to use their full path. However, all scripts
+installed in a virtual environment should be runnable without activating it,
+and run with the virtual environment's Python automatically.
+
+You can deactivate a virtual environment by typing "deactivate" in your shell.
+The exact mechanism is platform-specific: for example, the Bash activation
+script defines a "deactivate" function, whereas on Windows there are separate
+scripts called ``deactivate.bat`` and ``Deactivate.ps1`` which are installed
+when the virtual environment is created.
.. versionadded:: 3.4
``fish`` and ``csh`` activation scripts.
diff --git a/Doc/using/windows.rst b/Doc/using/windows.rst
index a4a6a30..3e4b70e 100644
--- a/Doc/using/windows.rst
+++ b/Doc/using/windows.rst
@@ -29,15 +29,15 @@ Supported Versions
As specified in :pep:`11`, a Python release only supports a Windows platform
while Microsoft considers the platform under extended support. This means that
-Python 3.5 supports Windows Vista and newer. If you require Windows XP support
-then please install Python 3.4.
+Python |version| supports Windows Vista and newer. If you require Windows XP
+support then please install Python 3.4.
Installation Steps
------------------
-Four Python 3.5 installers are available for download - two each for the 32-bit
-and 64-bit versions of the interpreter. The *web installer* is a small initial
-download, and it will automatically download the required components as
+Four Python |version| installers are available for download - two each for the
+32-bit and 64-bit versions of the interpreter. The *web installer* is a small
+initial download, and it will automatically download the required components as
necessary. The *offline installer* includes the components necessary for a
default installation and only requires an internet connection for optional
features. See :ref:`install-layout-option` for other ways to avoid downloading
@@ -74,6 +74,31 @@ installation". In this case:
* If selected, the install directory will be added to the system :envvar:`PATH`
* Shortcuts are available for all users
+.. _max-path:
+
+Removing the MAX_PATH Limitation
+--------------------------------
+
+Windows historically has limited path lengths to 260 characters. This meant that
+paths longer than this would not resolve and errors would result.
+
+In the latest versions of Windows, this limitation can be expanded to
+approximately 32,000 characters. Your administrator will need to activate the
+"Enable Win32 long paths" group policy, or set the registry value
+``HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem@LongPathsEnabled``
+to ``1``.
+
+This allows the :func:`open` function, the :mod:`os` module and most other
+path functionality to accept and return paths longer than 260 characters when
+using strings. (Use of bytes as paths is deprecated on Windows, and this feature
+is not available when using bytes.)
+
+After changing the above option, no further configuration is required.
+
+.. versionchanged:: 3.6
+
+ Support for long paths was enabled in Python.
+
.. _install-quiet-option:
Installing Without UI
@@ -168,13 +193,13 @@ of available options is shown below.
For example, to silently install a default, system-wide Python installation,
you could use the following command (from an elevated command prompt)::
- python-3.5.0.exe /quiet InstallAllUsers=1 PrependPath=1 Include_test=0
+ python-3.6.0.exe /quiet InstallAllUsers=1 PrependPath=1 Include_test=0
To allow users to easily install a personal copy of Python without the test
suite, you could provide a shortcut with the following command. This will
display a simplified initial page and disallow customization::
- python-3.5.0.exe InstallAllUsers=0 Include_launcher=0 Include_test=0
+ python-3.6.0.exe InstallAllUsers=0 Include_launcher=0 Include_test=0
SimpleInstall=1 SimpleInstallDescription="Just for me, no test suite."
(Note that omitting the launcher also omits file associations, and is only
@@ -209,13 +234,13 @@ where a large number of installations are going to be performed it is very
useful to have a locally cached copy.
Execute the following command from Command Prompt to download all possible
-required files. Remember to substitute ``python-3.5.0.exe`` for the actual
+required files. Remember to substitute ``python-3.6.0.exe`` for the actual
name of your installer, and to create layouts in their own directories to
avoid collisions between files with the same name.
::
- python-3.5.0.exe /layout [optional target directory]
+ python-3.6.0.exe /layout [optional target directory]
You may also specify the ``/quiet`` option to hide the progress display.
@@ -320,7 +345,7 @@ User level and the System level, or temporarily in a command prompt.
To temporarily set environment variables, open Command Prompt and use the
:command:`set` command::
- C:\>set PATH=C:\Program Files\Python 3.5;%PATH%
+ C:\>set PATH=C:\Program Files\Python 3.6;%PATH%
C:\>set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib
C:\>python
@@ -376,10 +401,10 @@ Finding the Python executable
Besides using the automatically created start menu entry for the Python
interpreter, you might want to start Python in the command prompt. The
-installer for Python 3.5 and later has an option to set that up for you.
+installer has an option to set that up for you.
-On the first page of the installer, an option labelled "Add Python 3.5 to
-PATH" can be selected to have the installer add the install location into the
+On the first page of the installer, an option labelled "Add Python to PATH"
+may be selected to have the installer add the install location into the
:envvar:`PATH`. The location of the :file:`Scripts\\` folder is also added.
This allows you to type :command:`python` to run the interpreter, and
:command:`pip` for the package installer. Thus, you can also execute your
@@ -393,7 +418,7 @@ of your Python installation, delimited by a semicolon from other entries. An
example variable could look like this (assuming the first two entries already
existed)::
- C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Python 3.5
+ C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Python 3.6
.. _launcher:
@@ -418,6 +443,8 @@ Getting started
From the command-line
^^^^^^^^^^^^^^^^^^^^^
+.. versionchanged:: 3.6
+
System-wide installations of Python 3.3 and later will put the launcher on your
:envvar:`PATH`. The launcher is compatible with all available versions of
Python, so it does not matter which version is installed. To check that the
@@ -427,25 +454,26 @@ launcher is available, execute the following command in Command Prompt:
py
-You should find that the latest version of Python 2.x you have installed is
+You should find that the latest version of Python you have installed is
started - it can be exited as normal, and any additional command-line
arguments specified will be sent directly to Python.
-If you have multiple versions of Python 2.x installed (e.g., 2.6 and 2.7) you
-will have noticed that Python 2.7 was started - to launch Python 2.6, try the
-command:
+If you have multiple versions of Python installed (e.g., 2.7 and |version|) you
+will have noticed that Python |version| was started - to launch Python 2.7, try
+the command:
::
- py -2.6
+ py -2.7
-If you have a Python 3.x installed, try the command:
+If you want the latest version of Python 2.x you have installed, try the
+command:
::
- py -3
+ py -2
-You should find the latest version of Python 3.x starts.
+You should find the latest version of Python 2.x starts.
If you see the following error, you do not have the launcher installed:
@@ -500,6 +528,11 @@ version qualifier. Assuming you have Python 2.6 installed, try changing the
first line to ``#! python2.6`` and you should find the 2.6 version
information printed.
+Note that unlike interactive use, a bare "python" will use the latest
+version of Python 2.x that you have installed. This is for backward
+compatibility and for compatibility with Unix, where the command ``python``
+typically refers to Python 2.
+
From file associations
^^^^^^^^^^^^^^^^^^^^^^
@@ -676,7 +709,7 @@ target Python.
-.. finding_modules:
+.. _finding_modules:
Finding modules
===============
@@ -687,7 +720,24 @@ installation directory. So, if you had installed Python to
:file:`C:\\Python\\Lib\\` and third-party modules should be stored in
:file:`C:\\Python\\Lib\\site-packages\\`.
-This is how :data:`sys.path` is populated on Windows:
+To completely override :data:`sys.path`, create a ``._pth`` file with the same
+name as the DLL (``python36._pth``) or the executable (``python._pth``) and
+specify one line for each path to add to :data:`sys.path`. The file based on the
+DLL name overrides the one based on the executable, which allows paths to be
+restricted for any program loading the runtime if desired.
+
+When the file exists, all registry and environment variables are ignored,
+isolated mode is enabled, and :mod:`site` is not imported unless one line in the
+file specifies ``import site``. Blank paths and lines starting with ``#`` are
+ignored. Each path may be absolute or relative to the location of the file.
+Import statements other than to ``site`` are not permitted, and arbitrary code
+cannot be specified.
+
+Note that ``.pth`` files (without leading underscore) will be processed normally
+by the :mod:`site` module.
+
+When no ``._pth`` file is found, this is how :data:`sys.path` is populated on
+Windows:
* An empty entry is added at the start, which corresponds to the current
directory.
@@ -706,10 +756,11 @@ This is how :data:`sys.path` is populated on Windows:
* If the environment variable :envvar:`PYTHONHOME` is set, it is assumed as
"Python Home". Otherwise, the path of the main Python executable is used to
- locate a "landmark file" (``Lib\os.py``) to deduce the "Python Home". If a
- Python home is found, the relevant sub-directories added to :data:`sys.path`
- (``Lib``, ``plat-win``, etc) are based on that folder. Otherwise, the core
- Python path is constructed from the PythonPath stored in the registry.
+ locate a "landmark file" (either ``Lib\os.py`` or ``pythonXY.zip``) to deduce
+ the "Python Home". If a Python home is found, the relevant sub-directories
+ added to :data:`sys.path` (``Lib``, ``plat-win``, etc) are based on that
+ folder. Otherwise, the core Python path is constructed from the PythonPath
+ stored in the registry.
* If the Python Home cannot be located, no :envvar:`PYTHONPATH` is specified in
the environment, and no registry entries can be found, a default path with
@@ -722,10 +773,6 @@ directory one level above the executable, the following variations apply:
path is used instead of the path to the main executable when deducing the
home location.
-* If ``applocal`` is set to true, the ``home`` property or the main executable
- is always used as the home path, and all environment variables or registry
- values affecting the path are ignored. The landmark file is not checked.
-
The end result of all this is:
* When running :file:`python.exe`, or any other .exe in the main Python
@@ -744,13 +791,12 @@ The end result of all this is:
For those who want to bundle Python into their application or distribution, the
following advice will prevent conflicts with other installations:
-* Include a ``pyvenv.cfg`` file alongside your executable containing
- ``applocal = true``. This will ensure that your own directory will be used to
- resolve paths even if you have included the standard library in a ZIP file.
- It will also ignore user site-packages and other paths listed in the
- registry.
+* Include a ``._pth`` file alongside your executable containing the
+ directories to include. This will ignore paths listed in the registry and
+ environment variables, and also ignore :mod:`site` unless ``import site`` is
+ listed.
-* If you are loading :file:`python3.dll` or :file:`python35.dll` in your own
+* If you are loading :file:`python3.dll` or :file:`python36.dll` in your own
executable, explicitly call :c:func:`Py_SetPath` or (at least)
:c:func:`Py_SetProgramName` before :c:func:`Py_Initialize`.
@@ -760,7 +806,8 @@ following advice will prevent conflicts with other installations:
* If you cannot use the previous suggestions (for example, you are a
distribution that allows people to run :file:`python.exe` directly), ensure
that the landmark file (:file:`Lib\\os.py`) exists in your install directory.
- (Note that it will not be detected inside a ZIP file.)
+ (Note that it will not be detected inside a ZIP file, but a correctly named
+ ZIP file will be detected instead.)
These will ensure that the files in a system-wide installation will not take
precedence over the copy of the standard library bundled with your application.
@@ -768,6 +815,22 @@ Otherwise, your users may experience problems using your application. Note that
the first suggestion is the best, as the other may still be susceptible to
non-standard paths in the registry and user site-packages.
+.. versionchanged::
+ 3.6
+
+ * Adds ``._pth`` file support and removes ``applocal`` option from
+ ``pyvenv.cfg``.
+ * Adds ``pythonXX.zip`` as a potential landmark when directly adjacent
+ to the executable.
+
+.. deprecated::
+ 3.6
+
+ Modules specified in the registry under ``Modules`` (not ``PythonPath``)
+ may be imported by :class:`importlib.machinery.WindowsRegistryFinder`.
+ This finder is enabled on Windows in 3.6.0 and earlier, but may need to
+ be explicitly added to :attr:`sys.meta_path` in the future.
+
Additional modules
==================
@@ -867,7 +930,7 @@ directly accessed by end-users.
When extracted, the embedded distribution is (almost) fully isolated from the
user's system, including environment variables, system registry settings, and
installed packages. The standard library is included as pre-compiled and
-optimized ``.pyc`` files in a ZIP, and ``python3.dll``, ``python35.dll``,
+optimized ``.pyc`` files in a ZIP, and ``python3.dll``, ``python36.dll``,
``python.exe`` and ``pythonw.exe`` are all provided. Tcl/tk (including all
dependants, such as Idle), pip and the Python documentation are not included.
diff --git a/Doc/whatsnew/2.1.rst b/Doc/whatsnew/2.1.rst
index ad4c6d5..3486cdd 100644
--- a/Doc/whatsnew/2.1.rst
+++ b/Doc/whatsnew/2.1.rst
@@ -367,7 +367,7 @@ dictionary::
This version works for simple things such as integers, but it has a side effect;
the ``_cache`` dictionary holds a reference to the return values, so they'll
-never be deallocated until the Python process exits and cleans up This isn't
+never be deallocated until the Python process exits and cleans up. This isn't
very noticeable for integers, but if :func:`f` returns an object, or a data
structure that takes up a lot of memory, this can be a problem.
diff --git a/Doc/whatsnew/3.2.rst b/Doc/whatsnew/3.2.rst
index 9acc734..b900e77 100644
--- a/Doc/whatsnew/3.2.rst
+++ b/Doc/whatsnew/3.2.rst
@@ -1253,9 +1253,9 @@ definition::
@contextmanager
def track_entry_and_exit(name):
- logging.info('Entering: {}'.format(name))
+ logging.info('Entering: %s', name)
yield
- logging.info('Exiting: {}'.format(name))
+ logging.info('Exiting: %s', name)
Formerly, this would have only been usable as a context manager::
diff --git a/Doc/whatsnew/3.3.rst b/Doc/whatsnew/3.3.rst
index 0c9f2bb..93b297c 100644
--- a/Doc/whatsnew/3.3.rst
+++ b/Doc/whatsnew/3.3.rst
@@ -108,7 +108,7 @@ packages. Their concept and implementation are inspired by the popular
with the interpreter core.
This PEP adds the :mod:`venv` module for programmatic access, and the
-:ref:`pyvenv <scripts-pyvenv>` script for command-line access and
+``pyvenv`` script for command-line access and
administration. The Python interpreter checks for a ``pyvenv.cfg``,
file whose existence signals the base of a virtual environment's directory
tree.
diff --git a/Doc/whatsnew/3.4.rst b/Doc/whatsnew/3.4.rst
index 1e5c9d1..72398f9 100644
--- a/Doc/whatsnew/3.4.rst
+++ b/Doc/whatsnew/3.4.rst
@@ -197,7 +197,7 @@ will also be installed. On other platforms, the system wide unversioned
``pip`` command typically refers to the separately installed Python 2
version.
-The :ref:`pyvenv <scripts-pyvenv>` command line utility and the :mod:`venv`
+The ``pyvenv`` command line utility and the :mod:`venv`
module make use of the :mod:`ensurepip` module to make ``pip`` readily
available in virtual environments. When using the command line utility,
``pip`` is installed by default, while when using the :mod:`venv` module
@@ -824,7 +824,7 @@ currently in the new module, which is being added as part of email's new
:term:`provisional API`. These classes provide a number of new methods that
make extracting content from and inserting content into email messages much
easier. For details, see the :mod:`~email.contentmanager` documentation and
-the :ref:`email-contentmanager-api-examples`. These API additions complete the
+the :ref:`email-examples`. These API additions complete the
bulk of the work that was planned as part of the email6 project. The currently
provisional API is scheduled to become final in Python 3.5 (possibly with a few
minor additions in the area of error handling). (Contributed by R. David
@@ -1989,11 +1989,11 @@ Other Improvements
Stinner using his :pep:`445`-based ``pyfailmalloc`` tool (:issue:`18408`,
:issue:`18520`).
-* The :ref:`pyvenv <scripts-pyvenv>` command now accepts a ``--copies`` option
+* The ``pyvenv`` command now accepts a ``--copies`` option
to use copies rather than symlinks even on systems where symlinks are the
default. (Contributed by Vinay Sajip in :issue:`18807`.)
-* The :ref:`pyvenv <scripts-pyvenv>` command also accepts a ``--without-pip``
+* The ``pyvenv`` command also accepts a ``--without-pip``
option to suppress the otherwise-automatic bootstrapping of pip into
the virtual environment. (Contributed by Nick Coghlan in :issue:`19552`
as part of the :pep:`453` implementation.)
@@ -2459,7 +2459,7 @@ Changes in the Python API
stream in :mod:`~io.TextIOWrapper` to use its *newline* argument
(:issue:`15204`).
-* If you use :ref:`pyvenv <scripts-pyvenv>` in a script and desire that pip
+* If you use ``pyvenv`` in a script and desire that pip
*not* be installed, you must add ``--without-pip`` to your command
invocation.
diff --git a/Doc/whatsnew/3.5.rst b/Doc/whatsnew/3.5.rst
index ab61d7a..edb74f0 100644
--- a/Doc/whatsnew/3.5.rst
+++ b/Doc/whatsnew/3.5.rst
@@ -46,7 +46,8 @@
This article explains the new features in Python 3.5, compared to 3.4.
Python 3.5 was released on September 13, 2015.  See the
-:ref:`changelog <changelog>` for a full list of changes.
+`changelog <https://docs.python.org/3.5/whatsnew/changelog.html>`_ for a full
+list of changes.
.. seealso::
@@ -2175,8 +2176,7 @@ New ``calloc`` functions were added:
* :c:func:`PyMem_RawCalloc`,
* :c:func:`PyMem_Calloc`,
-* :c:func:`PyObject_Calloc`,
-* :c:func:`_PyObject_GC_Calloc`.
+* :c:func:`PyObject_Calloc`.
(Contributed by Victor Stinner in :issue:`21233`.)
diff --git a/Doc/whatsnew/3.6.rst b/Doc/whatsnew/3.6.rst
new file mode 100644
index 0000000..f6c71d3
--- /dev/null
+++ b/Doc/whatsnew/3.6.rst
@@ -0,0 +1,2307 @@
+****************************
+ What's New In Python 3.6
+****************************
+
+:Release: |release|
+:Date: |today|
+:Editors: Elvis Pranskevichus <elvis@magic.io>, Yury Selivanov <yury@magic.io>
+
+.. Rules for maintenance:
+
+ * Anyone can add text to this document. Do not spend very much time
+ on the wording of your changes, because your text will probably
+ get rewritten to some degree.
+
+ * The maintainer will go through Misc/NEWS periodically and add
+ changes; it's therefore more important to add your changes to
+ Misc/NEWS than to this file.
+
+ * This is not a complete list of every single change; completeness
+ is the purpose of Misc/NEWS. Some changes I consider too small
+ or esoteric to include. If such a change is added to the text,
+ I'll just remove it. (This is another reason you shouldn't spend
+ too much time on writing your addition.)
+
+ * If you want to draw your new text to the attention of the
+ maintainer, add 'XXX' to the beginning of the paragraph or
+ section.
+
+ * It's OK to just add a fragmentary note about a change. For
+ example: "XXX Describe the transmogrify() function added to the
+ socket module." The maintainer will research the change and
+ write the necessary text.
+
+ * You can comment out your additions if you like, but it's not
+ necessary (especially when a final release is some months away).
+
+ * Credit the author of a patch or bugfix. Just the name is
+ sufficient; the e-mail address isn't necessary.
+
+ * It's helpful to add the bug/patch number as a comment:
+
+ XXX Describe the transmogrify() function added to the socket
+ module.
+ (Contributed by P.Y. Developer in :issue:`12345`.)
+
+ This saves the maintainer the effort of going through the Mercurial log
+ when researching a change.
+
+This article explains the new features in Python 3.6, compared to 3.5.
+Python 3.6 was released on December 23, 2016.  See the
+`changelog <https://docs.python.org/3.6/whatsnew/changelog.html>`_ for a full
+list of changes.
+
+.. seealso::
+
+ :pep:`494` - Python 3.6 Release Schedule
+
+
+Summary -- Release highlights
+=============================
+
+New syntax features:
+
+* :ref:`PEP 498 <whatsnew36-pep498>`, formatted string literals.
+
+* :ref:`PEP 515 <whatsnew36-pep515>`, underscores in numeric literals.
+
+* :ref:`PEP 526 <whatsnew36-pep526>`, syntax for variable annotations.
+
+* :ref:`PEP 525 <whatsnew36-pep525>`, asynchronous generators.
+
+* :ref:`PEP 530 <whatsnew36-pep530>`: asynchronous comprehensions.
+
+
+New library modules:
+
+* :mod:`secrets`: :ref:`PEP 506 -- Adding A Secrets Module To The Standard Library <whatsnew36-pep506>`.
+
+
+CPython implementation improvements:
+
+* The :ref:`dict <typesmapping>` type has been reimplemented to use
+ a :ref:`more compact representation <whatsnew36-compactdict>`
+ based on `a proposal by Raymond Hettinger
+ <https://mail.python.org/pipermail/python-dev/2012-December/123028.html>`_
+ and similar to the `PyPy dict implementation`_. This resulted in dictionaries
+ using 20% to 25% less memory when compared to Python 3.5.
+
+* Customization of class creation has been simplified with the
+ :ref:`new protocol <whatsnew36-pep487>`.
+
+* The class attribute definition order is
+ :ref:`now preserved <whatsnew36-pep520>`.
+
+* The order of elements in ``**kwargs`` now
+ :ref:`corresponds to the order <whatsnew36-pep468>` in which keyword
+ arguments were passed to the function.
+
+* DTrace and SystemTap :ref:`probing support <whatsnew36-tracing>` has
+ been added.
+
+* The new :ref:`PYTHONMALLOC <whatsnew36-pythonmalloc>` environment variable
+ can now be used to debug the interpreter memory allocation and access
+ errors.
+
+
+Significant improvements in the standard library:
+
+* The :mod:`asyncio` module has received new features, significant
+ usability and performance improvements, and a fair amount of bug fixes.
+ Starting with Python 3.6 the ``asyncio`` module is no longer provisional
+ and its API is considered stable.
+
+* A new :ref:`file system path protocol <whatsnew36-pep519>` has been
+ implemented to support :term:`path-like objects <path-like object>`.
+ All standard library functions operating on paths have been updated to
+ work with the new protocol.
+
+* The :mod:`datetime` module has gained support for
+ :ref:`Local Time Disambiguation <whatsnew36-pep495>`.
+
+* The :mod:`typing` module received a number of
+ :ref:`improvements <whatsnew36-typing>`.
+
+* The :mod:`tracemalloc` module has been significantly reworked
+ and is now used to provide better output for :exc:`ResourceWarning`
+ as well as provide better diagnostics for memory allocation errors.
+ See the :ref:`PYTHONMALLOC section <whatsnew36-pythonmalloc>` for more
+ information.
+
+
+Security improvements:
+
+* The new :mod:`secrets` module has been added to simplify the generation of
+ cryptographically strong pseudo-random numbers suitable for
+ managing secrets such as account authentication, tokens, and similar.
+
+* On Linux, :func:`os.urandom` now blocks until the system urandom entropy
+ pool is initialized to increase the security. See the :pep:`524` for the
+ rationale.
+
+* The :mod:`hashlib` and :mod:`ssl` modules now support OpenSSL 1.1.0.
+
+* The default settings and feature set of the :mod:`ssl` module have been
+ improved.
+
+* The :mod:`hashlib` module received support for the BLAKE2, SHA-3 and SHAKE
+ hash algorithms and the :func:`~hashlib.scrypt` key derivation function.
+
+
+Windows improvements:
+
+* :ref:`PEP 528 <whatsnew36-pep529>` and :ref:`PEP 529 <whatsnew36-pep529>`,
+ Windows filesystem and console encoding changed to UTF-8.
+
+* The ``py.exe`` launcher, when used interactively, no longer prefers
+ Python 2 over Python 3 when the user doesn't specify a version (via
+ command line arguments or a config file). Handling of shebang lines
+ remains unchanged - "python" refers to Python 2 in that case.
+
+* ``python.exe`` and ``pythonw.exe`` have been marked as long-path aware,
+ which means that the 260 character path limit may no longer apply.
+ See :ref:`removing the MAX_PATH limitation <max-path>` for details.
+
+* A ``._pth`` file can be added to force isolated mode and fully specify
+ all search paths to avoid registry and environment lookup. See
+ :ref:`the documentation <finding_modules>` for more information.
+
+* A ``python36.zip`` file now works as a landmark to infer
+ :envvar:`PYTHONHOME`. See :ref:`the documentation <finding_modules>` for
+ more information.
+
+
+.. _PyPy dict implementation: https://morepypy.blogspot.com/2015/01/faster-more-memory-efficient-and-more.html
+
+
+New Features
+============
+
+.. _whatsnew36-pep498:
+
+PEP 498: Formatted string literals
+----------------------------------
+
+:pep:`498` introduces a new kind of string literals: *f-strings*, or
+:ref:`formatted string literals <f-strings>`.
+
+Formatted string literals are prefixed with ``'f'`` and are similar to
+the format strings accepted by :meth:`str.format`. They contain replacement
+fields surrounded by curly braces. The replacement fields are expressions,
+which are evaluated at run time, and then formatted using the
+:func:`format` protocol::
+
+ >>> name = "Fred"
+ >>> f"He said his name is {name}."
+ 'He said his name is Fred.'
+ >>> width = 10
+ >>> precision = 4
+ >>> value = decimal.Decimal("12.34567")
+ >>> f"result: {value:{width}.{precision}}" # nested fields
+ 'result: 12.35'
+
+.. seealso::
+
+ :pep:`498` -- Literal String Interpolation.
+ PEP written and implemented by Eric V. Smith.
+
+ :ref:`Feature documentation <f-strings>`.
+
+
+.. _whatsnew36-pep526:
+
+PEP 526: Syntax for variable annotations
+----------------------------------------
+
+:pep:`484` introduced the standard for type annotations of function
+parameters, a.k.a. type hints. This PEP adds syntax to Python for annotating
+the types of variables including class variables and instance variables::
+
+ primes: List[int] = []
+
+ captain: str # Note: no initial value!
+
+ class Starship:
+ stats: Dict[str, int] = {}
+
+Just as for function annotations, the Python interpreter does not attach any
+particular meaning to variable annotations and only stores them in the
+``__annotations__`` attribute of a class or module.
+
+In contrast to variable declarations in statically typed languages,
+the goal of annotation syntax is to provide an easy way to specify structured
+type metadata for third party tools and libraries via the abstract syntax tree
+and the ``__annotations__`` attribute.
+
+.. seealso::
+
+ :pep:`526` -- Syntax for variable annotations.
+ PEP written by Ryan Gonzalez, Philip House, Ivan Levkivskyi, Lisa Roach,
+ and Guido van Rossum. Implemented by Ivan Levkivskyi.
+
+ Tools that use or will use the new syntax:
+ `mypy <http://github.com/python/mypy>`_,
+ `pytype <http://github.com/google/pytype>`_, PyCharm, etc.
+
+
+.. _whatsnew36-pep515:
+
+PEP 515: Underscores in Numeric Literals
+----------------------------------------
+
+:pep:`515` adds the ability to use underscores in numeric literals for
+improved readability. For example::
+
+ >>> 1_000_000_000_000_000
+ 1000000000000000
+ >>> 0x_FF_FF_FF_FF
+ 4294967295
+
+Single underscores are allowed between digits and after any base
+specifier. Leading, trailing, or multiple underscores in a row are not
+allowed.
+
+The :ref:`string formatting <formatspec>` language also now has support
+for the ``'_'`` option to signal the use of an underscore for a thousands
+separator for floating point presentation types and for integer
+presentation type ``'d'``. For integer presentation types ``'b'``,
+``'o'``, ``'x'``, and ``'X'``, underscores will be inserted every 4
+digits::
+
+ >>> '{:_}'.format(1000000)
+ '1_000_000'
+ >>> '{:_x}'.format(0xFFFFFFFF)
+ 'ffff_ffff'
+
+.. seealso::
+
+ :pep:`515` -- Underscores in Numeric Literals
+ PEP written by Georg Brandl and Serhiy Storchaka.
+
+
+.. _whatsnew36-pep525:
+
+PEP 525: Asynchronous Generators
+--------------------------------
+
+:pep:`492` introduced support for native coroutines and ``async`` / ``await``
+syntax to Python 3.5. A notable limitation of the Python 3.5 implementation
+is that it was not possible to use ``await`` and ``yield`` in the same
+function body. In Python 3.6 this restriction has been lifted, making it
+possible to define *asynchronous generators*::
+
+ async def ticker(delay, to):
+ """Yield numbers from 0 to *to* every *delay* seconds."""
+ for i in range(to):
+ yield i
+ await asyncio.sleep(delay)
+
+The new syntax allows for faster and more concise code.
+
+.. seealso::
+
+ :pep:`525` -- Asynchronous Generators
+ PEP written and implemented by Yury Selivanov.
+
+
+.. _whatsnew36-pep530:
+
+PEP 530: Asynchronous Comprehensions
+------------------------------------
+
+:pep:`530` adds support for using ``async for`` in list, set, dict
+comprehensions and generator expressions::
+
+ result = [i async for i in aiter() if i % 2]
+
+Additionally, ``await`` expressions are supported in all kinds
+of comprehensions::
+
+ result = [await fun() for fun in funcs if await condition()]
+
+.. seealso::
+
+ :pep:`530` -- Asynchronous Comprehensions
+ PEP written and implemented by Yury Selivanov.
+
+
+.. _whatsnew36-pep487:
+
+PEP 487: Simpler customization of class creation
+------------------------------------------------
+
+It is now possible to customize subclass creation without using a metaclass.
+The new ``__init_subclass__`` classmethod will be called on the base class
+whenever a new subclass is created::
+
+ class PluginBase:
+ subclasses = []
+
+ def __init_subclass__(cls, **kwargs):
+ super().__init_subclass__(**kwargs)
+ cls.subclasses.append(cls)
+
+ class Plugin1(PluginBase):
+ pass
+
+ class Plugin2(PluginBase):
+ pass
+
+In order to allow zero-argument :func:`super` calls to work correctly from
+:meth:`~object.__init_subclass__` implementations, custom metaclasses must
+ensure that the new ``__classcell__`` namespace entry is propagated to
+``type.__new__`` (as described in :ref:`class-object-creation`).
+
+.. seealso::
+
+ :pep:`487` -- Simpler customization of class creation
+ PEP written and implemented by Martin Teichmann.
+
+ :ref:`Feature documentation <class-customization>`
+
+
+.. _whatsnew36-pep487-descriptors:
+
+PEP 487: Descriptor Protocol Enhancements
+-----------------------------------------
+
+:pep:`487` extends the descriptor protocol to include the new optional
+:meth:`~object.__set_name__` method. Whenever a new class is defined, the new
+method will be called on all descriptors included in the definition, providing
+them with a reference to the class being defined and the name given to the
+descriptor within the class namespace. In other words, instances of
+descriptors can now know the attribute name of the descriptor in the
+owner class::
+
+ class IntField:
+ def __get__(self, instance, owner):
+ return instance.__dict__[self.name]
+
+ def __set__(self, instance, value):
+ if not isinstance(value, int):
+ raise ValueError(f'expecting integer in {self.name}')
+ instance.__dict__[self.name] = value
+
+ # this is the new initializer:
+ def __set_name__(self, owner, name):
+ self.name = name
+
+ class Model:
+ int_field = IntField()
+
+
+.. seealso::
+
+ :pep:`487` -- Simpler customization of class creation
+ PEP written and implemented by Martin Teichmann.
+
+ :ref:`Feature documentation <descriptors>`
+
+
+.. _whatsnew36-pep519:
+
+PEP 519: Adding a file system path protocol
+-------------------------------------------
+
+File system paths have historically been represented as :class:`str`
+or :class:`bytes` objects. This has led to people who write code which
+operate on file system paths to assume that such objects are only one
+of those two types (an :class:`int` representing a file descriptor
+does not count as that is not a file path). Unfortunately that
+assumption prevents alternative object representations of file system
+paths like :mod:`pathlib` from working with pre-existing code,
+including Python's standard library.
+
+To fix this situation, a new interface represented by
+:class:`os.PathLike` has been defined. By implementing the
+:meth:`~os.PathLike.__fspath__` method, an object signals that it
+represents a path. An object can then provide a low-level
+representation of a file system path as a :class:`str` or
+:class:`bytes` object. This means an object is considered
+:term:`path-like <path-like object>` if it implements
+:class:`os.PathLike` or is a :class:`str` or :class:`bytes` object
+which represents a file system path. Code can use :func:`os.fspath`,
+:func:`os.fsdecode`, or :func:`os.fsencode` to explicitly get a
+:class:`str` and/or :class:`bytes` representation of a path-like
+object.
+
+The built-in :func:`open` function has been updated to accept
+:class:`os.PathLike` objects, as have all relevant functions in the
+:mod:`os` and :mod:`os.path` modules, and most other functions and
+classes in the standard library. The :class:`os.DirEntry` class
+and relevant classes in :mod:`pathlib` have also been updated to
+implement :class:`os.PathLike`.
+
+The hope is that updating the fundamental functions for operating
+on file system paths will lead to third-party code to implicitly
+support all :term:`path-like objects <path-like object>` without any
+code changes, or at least very minimal ones (e.g. calling
+:func:`os.fspath` at the beginning of code before operating on a
+path-like object).
+
+Here are some examples of how the new interface allows for
+:class:`pathlib.Path` to be used more easily and transparently with
+pre-existing code::
+
+ >>> import pathlib
+ >>> with open(pathlib.Path("README")) as f:
+ ... contents = f.read()
+ ...
+ >>> import os.path
+ >>> os.path.splitext(pathlib.Path("some_file.txt"))
+ ('some_file', '.txt')
+ >>> os.path.join("/a/b", pathlib.Path("c"))
+ '/a/b/c'
+ >>> import os
+ >>> os.fspath(pathlib.Path("some_file.txt"))
+ 'some_file.txt'
+
+(Implemented by Brett Cannon, Ethan Furman, Dusty Phillips, and Jelle Zijlstra.)
+
+.. seealso::
+
+ :pep:`519` -- Adding a file system path protocol
+ PEP written by Brett Cannon and Koos Zevenhoven.
+
+
+.. _whatsnew36-pep495:
+
+PEP 495: Local Time Disambiguation
+----------------------------------
+
+In most world locations, there have been and will be times when local clocks
+are moved back. In those times, intervals are introduced in which local
+clocks show the same time twice in the same day. In these situations, the
+information displayed on a local clock (or stored in a Python datetime
+instance) is insufficient to identify a particular moment in time.
+
+:pep:`495` adds the new *fold* attribute to instances of
+:class:`datetime.datetime` and :class:`datetime.time` classes to differentiate
+between two moments in time for which local times are the same::
+
+ >>> u0 = datetime(2016, 11, 6, 4, tzinfo=timezone.utc)
+ >>> for i in range(4):
+ ... u = u0 + i*HOUR
+ ... t = u.astimezone(Eastern)
+ ... print(u.time(), 'UTC =', t.time(), t.tzname(), t.fold)
+ ...
+ 04:00:00 UTC = 00:00:00 EDT 0
+ 05:00:00 UTC = 01:00:00 EDT 0
+ 06:00:00 UTC = 01:00:00 EST 1
+ 07:00:00 UTC = 02:00:00 EST 0
+
+The values of the :attr:`fold <datetime.datetime.fold>` attribute have the
+value ``0`` for all instances except those that represent the second
+(chronologically) moment in time in an ambiguous case.
+
+.. seealso::
+
+ :pep:`495` -- Local Time Disambiguation
+ PEP written by Alexander Belopolsky and Tim Peters, implementation
+ by Alexander Belopolsky.
+
+
+.. _whatsnew36-pep529:
+
+PEP 529: Change Windows filesystem encoding to UTF-8
+----------------------------------------------------
+
+Representing filesystem paths is best performed with str (Unicode) rather than
+bytes. However, there are some situations where using bytes is sufficient and
+correct.
+
+Prior to Python 3.6, data loss could result when using bytes paths on Windows.
+With this change, using bytes to represent paths is now supported on Windows,
+provided those bytes are encoded with the encoding returned by
+:func:`sys.getfilesystemencoding()`, which now defaults to ``'utf-8'``.
+
+Applications that do not use str to represent paths should use
+:func:`os.fsencode()` and :func:`os.fsdecode()` to ensure their bytes are
+correctly encoded. To revert to the previous behaviour, set
+:envvar:`PYTHONLEGACYWINDOWSFSENCODING` or call
+:func:`sys._enablelegacywindowsfsencoding`.
+
+See :pep:`529` for more information and discussion of code modifications that
+may be required.
+
+
+.. _whatsnew36-pep528:
+
+PEP 528: Change Windows console encoding to UTF-8
+-------------------------------------------------
+
+The default console on Windows will now accept all Unicode characters and
+provide correctly read str objects to Python code. ``sys.stdin``,
+``sys.stdout`` and ``sys.stderr`` now default to utf-8 encoding.
+
+This change only applies when using an interactive console, and not when
+redirecting files or pipes. To revert to the previous behaviour for interactive
+console use, set :envvar:`PYTHONLEGACYWINDOWSIOENCODING`.
+
+.. seealso::
+
+ :pep:`528` -- Change Windows console encoding to UTF-8
+ PEP written and implemented by Steve Dower.
+
+
+.. _whatsnew36-pep520:
+
+PEP 520: Preserving Class Attribute Definition Order
+----------------------------------------------------
+
+Attributes in a class definition body have a natural ordering: the same
+order in which the names appear in the source. This order is now
+preserved in the new class's :attr:`~object.__dict__` attribute.
+
+Also, the effective default class *execution* namespace (returned from
+:ref:`type.__prepare__() <prepare>`) is now an insertion-order-preserving
+mapping.
+
+.. seealso::
+
+ :pep:`520` -- Preserving Class Attribute Definition Order
+ PEP written and implemented by Eric Snow.
+
+
+.. _whatsnew36-pep468:
+
+PEP 468: Preserving Keyword Argument Order
+------------------------------------------
+
+``**kwargs`` in a function signature is now guaranteed to be an
+insertion-order-preserving mapping.
+
+.. seealso::
+
+ :pep:`468` -- Preserving Keyword Argument Order
+ PEP written and implemented by Eric Snow.
+
+
+.. _whatsnew36-compactdict:
+
+New :ref:`dict <typesmapping>` implementation
+---------------------------------------------
+
+The :ref:`dict <typesmapping>` type now uses a "compact" representation
+based on `a proposal by Raymond Hettinger
+<https://mail.python.org/pipermail/python-dev/2012-December/123028.html>`_
+which was `first implemented by PyPy
+<https://morepypy.blogspot.com/2015/01/faster-more-memory-efficient-and-more.html>`_.
+The memory usage of the new :func:`dict` is between 20% and 25% smaller
+compared to Python 3.5.
+
+The order-preserving aspect of this new implementation is considered an
+implementation detail and should not be relied upon (this may change in
+the future, but it is desired to have this new dict implementation in
+the language for a few releases before changing the language spec to mandate
+order-preserving semantics for all current and future Python
+implementations; this also helps preserve backwards-compatibility
+with older versions of the language where random iteration order is
+still in effect, e.g. Python 3.5).
+
+(Contributed by INADA Naoki in :issue:`27350`. Idea
+`originally suggested by Raymond Hettinger
+<https://mail.python.org/pipermail/python-dev/2012-December/123028.html>`_.)
+
+
+.. _whatsnew36-pep523:
+
+PEP 523: Adding a frame evaluation API to CPython
+-------------------------------------------------
+
+While Python provides extensive support to customize how code
+executes, one place it has not done so is in the evaluation of frame
+objects. If you wanted some way to intercept frame evaluation in
+Python there really wasn't any way without directly manipulating
+function pointers for defined functions.
+
+:pep:`523` changes this by providing an API to make frame
+evaluation pluggable at the C level. This will allow for tools such
+as debuggers and JITs to intercept frame evaluation before the
+execution of Python code begins. This enables the use of alternative
+evaluation implementations for Python code, tracking frame
+evaluation, etc.
+
+This API is not part of the limited C API and is marked as private to
+signal that usage of this API is expected to be limited and only
+applicable to very select, low-level use-cases. Semantics of the
+API will change with Python as necessary.
+
+.. seealso::
+
+ :pep:`523` -- Adding a frame evaluation API to CPython
+ PEP written by Brett Cannon and Dino Viehland.
+
+
+.. _whatsnew36-pythonmalloc:
+
+PYTHONMALLOC environment variable
+---------------------------------
+
+The new :envvar:`PYTHONMALLOC` environment variable allows setting the Python
+memory allocators and installing debug hooks.
+
+It is now possible to install debug hooks on Python memory allocators on Python
+compiled in release mode using ``PYTHONMALLOC=debug``. Effects of debug hooks:
+
+* Newly allocated memory is filled with the byte ``0xCB``
+* Freed memory is filled with the byte ``0xDB``
+* Detect violations of the Python memory allocator API. For example,
+ :c:func:`PyObject_Free` called on a memory block allocated by
+ :c:func:`PyMem_Malloc`.
+* Detect writes before the start of a buffer (buffer underflows)
+* Detect writes after the end of a buffer (buffer overflows)
+* Check that the :term:`GIL <global interpreter lock>` is held when allocator
+ functions of :c:data:`PYMEM_DOMAIN_OBJ` (ex: :c:func:`PyObject_Malloc`) and
+ :c:data:`PYMEM_DOMAIN_MEM` (ex: :c:func:`PyMem_Malloc`) domains are called.
+
+Checking if the GIL is held is also a new feature of Python 3.6.
+
+See the :c:func:`PyMem_SetupDebugHooks` function for debug hooks on Python
+memory allocators.
+
+It is now also possible to force the usage of the :c:func:`malloc` allocator of
+the C library for all Python memory allocations using ``PYTHONMALLOC=malloc``.
+This is helpful when using external memory debuggers like Valgrind on
+a Python compiled in release mode.
+
+On error, the debug hooks on Python memory allocators now use the
+:mod:`tracemalloc` module to get the traceback where a memory block was
+allocated.
+
+Example of fatal error on buffer overflow using
+``python3.6 -X tracemalloc=5`` (store 5 frames in traces)::
+
+ Debug memory block at address p=0x7fbcd41666f8: API 'o'
+ 4 bytes originally requested
+ The 7 pad bytes at p-7 are FORBIDDENBYTE, as expected.
+ The 8 pad bytes at tail=0x7fbcd41666fc are not all FORBIDDENBYTE (0xfb):
+ at tail+0: 0x02 *** OUCH
+ at tail+1: 0xfb
+ at tail+2: 0xfb
+ at tail+3: 0xfb
+ at tail+4: 0xfb
+ at tail+5: 0xfb
+ at tail+6: 0xfb
+ at tail+7: 0xfb
+ The block was made by call #1233329 to debug malloc/realloc.
+ Data at p: 1a 2b 30 00
+
+ Memory block allocated at (most recent call first):
+ File "test/test_bytes.py", line 323
+ File "unittest/case.py", line 600
+ File "unittest/case.py", line 648
+ File "unittest/suite.py", line 122
+ File "unittest/suite.py", line 84
+
+ Fatal Python error: bad trailing pad byte
+
+ Current thread 0x00007fbcdbd32700 (most recent call first):
+ File "test/test_bytes.py", line 323 in test_hex
+ File "unittest/case.py", line 600 in run
+ File "unittest/case.py", line 648 in __call__
+ File "unittest/suite.py", line 122 in run
+ File "unittest/suite.py", line 84 in __call__
+ File "unittest/suite.py", line 122 in run
+ File "unittest/suite.py", line 84 in __call__
+ ...
+
+(Contributed by Victor Stinner in :issue:`26516` and :issue:`26564`.)
+
+
+.. _whatsnew36-tracing:
+
+DTrace and SystemTap probing support
+------------------------------------
+
+Python can now be built ``--with-dtrace`` which enables static markers
+for the following events in the interpreter:
+
+* function call/return
+
+* garbage collection started/finished
+
+* line of code executed.
+
+This can be used to instrument running interpreters in production,
+without the need to recompile specific debug builds or providing
+application-specific profiling/debugging code.
+
+More details in :ref:`instrumentation`.
+
+The current implementation is tested on Linux and macOS. Additional
+markers may be added in the future.
+
+(Contributed by Łukasz Langa in :issue:`21590`, based on patches by
+Jesús Cea Avión, David Malcolm, and Nikhil Benesch.)
+
+
+Other Language Changes
+======================
+
+Some smaller changes made to the core Python language are:
+
+* A ``global`` or ``nonlocal`` statement must now textually appear
+ before the first use of the affected name in the same scope.
+ Previously this was a ``SyntaxWarning``.
+
+* It is now possible to set a :ref:`special method <specialnames>` to
+ ``None`` to indicate that the corresponding operation is not available.
+ For example, if a class sets :meth:`__iter__` to ``None``, the class
+ is not iterable.
+ (Contributed by Andrew Barnert and Ivan Levkivskyi in :issue:`25958`.)
+
+* Long sequences of repeated traceback lines are now abbreviated as
+ ``"[Previous line repeated {count} more times]"`` (see
+ :ref:`whatsnew36-traceback` for an example).
+ (Contributed by Emanuel Barry in :issue:`26823`.)
+
+* Import now raises the new exception :exc:`ModuleNotFoundError`
+ (subclass of :exc:`ImportError`) when it cannot find a module. Code
+ that currently checks for ImportError (in try-except) will still work.
+ (Contributed by Eric Snow in :issue:`15767`.)
+
+* Class methods relying on zero-argument ``super()`` will now work correctly
+ when called from metaclass methods during class creation.
+ (Contributed by Martin Teichmann in :issue:`23722`.)
+
+
+New Modules
+===========
+
+.. _whatsnew36-pep506:
+
+secrets
+-------
+
+The main purpose of the new :mod:`secrets` module is to provide an obvious way
+to reliably generate cryptographically strong pseudo-random values suitable
+for managing secrets, such as account authentication, tokens, and similar.
+
+.. warning::
+
+ Note that the pseudo-random generators in the :mod:`random` module
+ should *NOT* be used for security purposes. Use :mod:`secrets`
+ on Python 3.6+ and :func:`os.urandom()` on Python 3.5 and earlier.
+
+.. seealso::
+
+ :pep:`506` -- Adding A Secrets Module To The Standard Library
+ PEP written and implemented by Steven D'Aprano.
+
+
+Improved Modules
+================
+
+array
+-----
+
+Exhausted iterators of :class:`array.array` will now stay exhausted even
+if the iterated array is extended. This is consistent with the behavior
+of other mutable sequences.
+
+Contributed by Serhiy Storchaka in :issue:`26492`.
+
+ast
+---
+
+The new :class:`ast.Constant` AST node has been added. It can be used
+by external AST optimizers for the purposes of constant folding.
+
+Contributed by Victor Stinner in :issue:`26146`.
+
+
+asyncio
+-------
+
+Starting with Python 3.6 the ``asyncio`` module is no longer provisional and its
+API is considered stable.
+
+Notable changes in the :mod:`asyncio` module since Python 3.5.0
+(all backported to 3.5.x due to the provisional status):
+
+* The :func:`~asyncio.get_event_loop` function has been changed to
+ always return the currently running loop when called from couroutines
+ and callbacks.
+ (Contributed by Yury Selivanov in :issue:`28613`.)
+
+* The :func:`~asyncio.ensure_future` function and all functions that
+ use it, such as :meth:`loop.run_until_complete() <asyncio.BaseEventLoop.run_until_complete>`,
+ now accept all kinds of :term:`awaitable objects <awaitable>`.
+ (Contributed by Yury Selivanov.)
+
+* New :func:`~asyncio.run_coroutine_threadsafe` function to submit
+ coroutines to event loops from other threads.
+ (Contributed by Vincent Michel.)
+
+* New :meth:`Transport.is_closing() <asyncio.BaseTransport.is_closing>`
+ method to check if the transport is closing or closed.
+ (Contributed by Yury Selivanov.)
+
+* The :meth:`loop.create_server() <asyncio.BaseEventLoop.create_server>`
+ method can now accept a list of hosts.
+ (Contributed by Yann Sionneau.)
+
+* New :meth:`loop.create_future() <asyncio.BaseEventLoop.create_future>`
+ method to create Future objects. This allows alternative event
+ loop implementations, such as
+ `uvloop <https://github.com/MagicStack/uvloop>`_, to provide a faster
+ :class:`asyncio.Future` implementation.
+ (Contributed by Yury Selivanov in :issue:`27041`.)
+
+* New :meth:`loop.get_exception_handler() <asyncio.BaseEventLoop.get_exception_handler>`
+ method to get the current exception handler.
+ (Contributed by Yury Selivanov in :issue:`27040`.)
+
+* New :meth:`StreamReader.readuntil() <asyncio.StreamReader.readuntil>`
+ method to read data from the stream until a separator bytes
+ sequence appears.
+ (Contributed by Mark Korenberg.)
+
+* The performance of :meth:`StreamReader.readexactly() <asyncio.StreamReader.readexactly>`
+ has been improved.
+ (Contributed by Mark Korenberg in :issue:`28370`.)
+
+* The :meth:`loop.getaddrinfo() <asyncio.BaseEventLoop.getaddrinfo>`
+ method is optimized to avoid calling the system ``getaddrinfo``
+ function if the address is already resolved.
+ (Contributed by A. Jesse Jiryu Davis.)
+
+* The :meth:`loop.stop() <asyncio.BaseEventLoop.stop>`
+ method has been changed to stop the loop immediately after
+ the current iteration. Any new callbacks scheduled as a result
+ of the last iteration will be discarded.
+ (Contributed by Guido van Rossum in :issue:`25593`.)
+
+* :meth:`Future.set_exception <asyncio.futures.Future.set_exception>`
+ will now raise :exc:`TypeError` when passed an instance of
+ the :exc:`StopIteration` exception.
+ (Contributed by Chris Angelico in :issue:`26221`.)
+
+* New :meth:`loop.connect_accepted_socket() <asyncio.BaseEventLoop.connect_accepted_socket>`
+ method to be used by servers that accept connections outside of asyncio,
+ but that use asyncio to handle them.
+ (Contributed by Jim Fulton in :issue:`27392`.)
+
+* ``TCP_NODELAY`` flag is now set for all TCP transports by default.
+ (Contributed by Yury Selivanov in :issue:`27456`.)
+
+* New :meth:`loop.shutdown_asyncgens() <asyncio.AbstractEventLoop.shutdown_asyncgens>`
+ to properly close pending asynchronous generators before closing the
+ loop.
+ (Contributed by Yury Selivanov in :issue:`28003`.)
+
+* :class:`Future <asyncio.Future>` and :class:`Task <asyncio.Task>`
+ classes now have an optimized C implementation which makes asyncio
+ code up to 30% faster.
+ (Contributed by Yury Selivanov and INADA Naoki in :issue:`26081`
+ and :issue:`28544`.)
+
+
+binascii
+--------
+
+The :func:`~binascii.b2a_base64` function now accepts an optional *newline*
+keyword argument to control whether the newline character is appended to the
+return value.
+(Contributed by Victor Stinner in :issue:`25357`.)
+
+
+cmath
+-----
+
+The new :const:`cmath.tau` (τ) constant has been added.
+(Contributed by Lisa Roach in :issue:`12345`, see :pep:`628` for details.)
+
+New constants: :const:`cmath.inf` and :const:`cmath.nan` to
+match :const:`math.inf` and :const:`math.nan`, and also :const:`cmath.infj`
+and :const:`cmath.nanj` to match the format used by complex repr.
+(Contributed by Mark Dickinson in :issue:`23229`.)
+
+
+collections
+-----------
+
+The new :class:`~collections.abc.Collection` abstract base class has been
+added to represent sized iterable container classes.
+(Contributed by Ivan Levkivskyi, docs by Neil Girdhar in :issue:`27598`.)
+
+The new :class:`~collections.abc.Reversible` abstract base class represents
+iterable classes that also provide the :meth:`__reversed__` method.
+(Contributed by Ivan Levkivskyi in :issue:`25987`.)
+
+The new :class:`~collections.abc.AsyncGenerator` abstract base class represents
+asynchronous generators.
+(Contributed by Yury Selivanov in :issue:`28720`.)
+
+The :func:`~collections.namedtuple` function now accepts an optional
+keyword argument *module*, which, when specified, is used for
+the ``__module__`` attribute of the returned named tuple class.
+(Contributed by Raymond Hettinger in :issue:`17941`.)
+
+The *verbose* and *rename* arguments for
+:func:`~collections.namedtuple` are now keyword-only.
+(Contributed by Raymond Hettinger in :issue:`25628`.)
+
+Recursive :class:`collections.deque` instances can now be pickled.
+(Contributed by Serhiy Storchaka in :issue:`26482`.)
+
+
+concurrent.futures
+------------------
+
+The :class:`ThreadPoolExecutor <concurrent.futures.ThreadPoolExecutor>`
+class constructor now accepts an optional *thread_name_prefix* argument
+to make it possible to customize the names of the threads created by the
+pool.
+(Contributed by Gregory P. Smith in :issue:`27664`.)
+
+
+contextlib
+----------
+
+The :class:`contextlib.AbstractContextManager` class has been added to
+provide an abstract base class for context managers. It provides a
+sensible default implementation for `__enter__()` which returns
+``self`` and leaves `__exit__()` an abstract method. A matching
+class has been added to the :mod:`typing` module as
+:class:`typing.ContextManager`.
+(Contributed by Brett Cannon in :issue:`25609`.)
+
+
+datetime
+--------
+
+The :class:`~datetime.datetime` and :class:`~datetime.time` classes have
+the new :attr:`~time.fold` attribute used to disambiguate local time
+when necessary. Many functions in the :mod:`datetime` have been
+updated to support local time disambiguation.
+See :ref:`Local Time Disambiguation <whatsnew36-pep495>` section for more
+information.
+(Contributed by Alexander Belopolsky in :issue:`24773`.)
+
+The :meth:`datetime.strftime() <datetime.datetime.strftime>` and
+:meth:`date.strftime() <datetime.date.strftime>` methods now support
+ISO 8601 date directives ``%G``, ``%u`` and ``%V``.
+(Contributed by Ashley Anderson in :issue:`12006`.)
+
+The :func:`datetime.isoformat() <datetime.datetime.isoformat>` function
+now accepts an optional *timespec* argument that specifies the number
+of additional components of the time value to include.
+(Contributed by Alessandro Cucci and Alexander Belopolsky in :issue:`19475`.)
+
+The :meth:`datetime.combine() <datetime.datetime.combine>` now
+accepts an optional *tzinfo* argument.
+(Contributed by Alexander Belopolsky in :issue:`27661`.)
+
+
+decimal
+-------
+
+New :meth:`Decimal.as_integer_ratio() <decimal.Decimal.as_integer_ratio>`
+method that returns a pair ``(n, d)`` of integers that represent the given
+:class:`~decimal.Decimal` instance as a fraction, in lowest terms and
+with a positive denominator::
+
+ >>> Decimal('-3.14').as_integer_ratio()
+ (-157, 50)
+
+(Contributed by Stefan Krah amd Mark Dickinson in :issue:`25928`.)
+
+
+
+distutils
+---------
+
+The ``default_format`` attribute has been removed from
+:class:`distutils.command.sdist.sdist` and the ``formats``
+attribute defaults to ``['gztar']``. Although not anticipated,
+any code relying on the presence of ``default_format`` may
+need to be adapted. See :issue:`27819` for more details.
+
+
+email
+-----
+
+The new email API, enabled via the *policy* keyword to various constructors, is
+no longer provisional. The :mod:`email` documentation has been reorganized and
+rewritten to focus on the new API, while retaining the old documentation for
+the legacy API. (Contributed by R. David Murray in :issue:`24277`.)
+
+The :mod:`email.mime` classes now all accept an optional *policy* keyword.
+(Contributed by Berker Peksag in :issue:`27331`.)
+
+The :class:`~email.generator.DecodedGenerator` now supports the *policy*
+keyword.
+
+There is a new :mod:`~email.policy` attribute,
+:attr:`~email.policy.Policy.message_factory`, that controls what class is used
+by default when the parser creates new message objects. For the
+:attr:`email.policy.compat32` policy this is :class:`~email.message.Message`,
+for the new policies it is :class:`~email.message.EmailMessage`.
+(Contributed by R. David Murray in :issue:`20476`.)
+
+
+encodings
+---------
+
+On Windows, added the ``'oem'`` encoding to use ``CP_OEMCP``, and the ``'ansi'``
+alias for the existing ``'mbcs'`` encoding, which uses the ``CP_ACP`` code page.
+(Contributed by Steve Dower in :issue:`27959`.)
+
+
+enum
+----
+
+Two new enumeration base classes have been added to the :mod:`enum` module:
+:class:`~enum.Flag` and :class:`~enum.IntFlags`. Both are used to define
+constants that can be combined using the bitwise operators.
+(Contributed by Ethan Furman in :issue:`23591`.)
+
+Many standard library modules have been updated to use the
+:class:`~enum.IntFlags` class for their constants.
+
+The new :class:`enum.auto` value can be used to assign values to enum
+members automatically::
+
+ >>> from enum import Enum, auto
+ >>> class Color(Enum):
+ ... red = auto()
+ ... blue = auto()
+ ... green = auto()
+ ...
+ >>> list(Color)
+ [<Color.red: 1>, <Color.blue: 2>, <Color.green: 3>]
+
+
+faulthandler
+------------
+
+On Windows, the :mod:`faulthandler` module now installs a handler for Windows
+exceptions: see :func:`faulthandler.enable`. (Contributed by Victor Stinner in
+:issue:`23848`.)
+
+
+fileinput
+---------
+
+:func:`~fileinput.hook_encoded` now supports the *errors* argument.
+(Contributed by Joseph Hackman in :issue:`25788`.)
+
+
+hashlib
+-------
+
+:mod:`hashlib` supports OpenSSL 1.1.0. The minimum recommend version is 1.0.2.
+(Contributed by Christian Heimes in :issue:`26470`.)
+
+BLAKE2 hash functions were added to the module. :func:`~hashlib.blake2b`
+and :func:`~hashlib.blake2s` are always available and support the full
+feature set of BLAKE2.
+(Contributed by Christian Heimes in :issue:`26798` based on code by
+Dmitry Chestnykh and Samuel Neves. Documentation written by Dmitry Chestnykh.)
+
+The SHA-3 hash functions :func:`~hashlib.sha3_224`, :func:`~hashlib.sha3_256`,
+:func:`~hashlib.sha3_384`, :func:`~hashlib.sha3_512`, and SHAKE hash functions
+:func:`~hashlib.shake_128` and :func:`~hashlib.shake_256` were added.
+(Contributed by Christian Heimes in :issue:`16113`. Keccak Code Package
+by Guido Bertoni, Joan Daemen, Michaël Peeters, Gilles Van Assche, and
+Ronny Van Keer.)
+
+The password-based key derivation function :func:`~hashlib.scrypt` is now
+available with OpenSSL 1.1.0 and newer.
+(Contributed by Christian Heimes in :issue:`27928`.)
+
+http.client
+-----------
+
+:meth:`HTTPConnection.request() <http.client.HTTPConnection.request>` and
+:meth:`~http.client.HTTPConnection.endheaders` both now support
+chunked encoding request bodies.
+(Contributed by Demian Brecht and Rolf Krahl in :issue:`12319`.)
+
+
+idlelib and IDLE
+----------------
+
+The idlelib package is being modernized and refactored to make IDLE look and
+work better and to make the code easier to understand, test, and improve. Part
+of making IDLE look better, especially on Linux and Mac, is using ttk widgets,
+mostly in the dialogs. As a result, IDLE no longer runs with tcl/tk 8.4. It
+now requires tcl/tk 8.5 or 8.6. We recommend running the latest release of
+either.
+
+'Modernizing' includes renaming and consolidation of idlelib modules. The
+renaming of files with partial uppercase names is similar to the renaming of,
+for instance, Tkinter and TkFont to tkinter and tkinter.font in 3.0. As a
+result, imports of idlelib files that worked in 3.5 will usually not work in
+3.6. At least a module name change will be needed (see idlelib/README.txt),
+sometimes more. (Name changes contributed by Al Swiegart and Terry Reedy in
+:issue:`24225`. Most idlelib patches since have been and will be part of the
+process.)
+
+In compensation, the eventual result with be that some idlelib classes will be
+easier to use, with better APIs and docstrings explaining them. Additional
+useful information will be added to idlelib when available.
+
+
+importlib
+---------
+
+Import now raises the new exception :exc:`ModuleNotFoundError`
+(subclass of :exc:`ImportError`) when it cannot find a module. Code
+that current checks for ``ImportError`` (in try-except) will still work.
+(Contributed by Eric Snow in :issue:`15767`.)
+
+:class:`importlib.util.LazyLoader` now calls
+:meth:`~importlib.abc.Loader.create_module` on the wrapped loader, removing the
+restriction that :class:`importlib.machinery.BuiltinImporter` and
+:class:`importlib.machinery.ExtensionFileLoader` couldn't be used with
+:class:`importlib.util.LazyLoader`.
+
+:func:`importlib.util.cache_from_source`,
+:func:`importlib.util.source_from_cache`, and
+:func:`importlib.util.spec_from_file_location` now accept a
+:term:`path-like object`.
+
+
+inspect
+-------
+
+The :func:`inspect.signature() <inspect.signature>` function now reports the
+implicit ``.0`` parameters generated by the compiler for comprehension and
+generator expression scopes as if they were positional-only parameters called
+``implicit0``. (Contributed by Jelle Zijlstra in :issue:`19611`.)
+
+To reduce code churn when upgrading from Python 2.7 and the legacy
+:func:`inspect.getargspec` API, the previously documented deprecation of
+:func:`inspect.getfullargspec` has been reversed. While this function is
+convenient for single/source Python 2/3 code bases, the richer
+:func:`inspect.signature` interface remains the recommended approach for new
+code. (Contributed by Nick Coghlan in :issue:`27172`)
+
+
+json
+----
+
+:func:`json.load` and :func:`json.loads` now support binary input. Encoded
+JSON should be represented using either UTF-8, UTF-16, or UTF-32.
+(Contributed by Serhiy Storchaka in :issue:`17909`.)
+
+
+logging
+-------
+
+The new :meth:`WatchedFileHandler.reopenIfNeeded() <logging.handlers.WatchedFileHandler.reopenIfNeeded>`
+method has been added to add the ability to check if the log file needs to
+be reopened.
+(Contributed by Marian Horban in :issue:`24884`.)
+
+
+math
+----
+
+The tau (τ) constant has been added to the :mod:`math` and :mod:`cmath`
+modules.
+(Contributed by Lisa Roach in :issue:`12345`, see :pep:`628` for details.)
+
+
+multiprocessing
+---------------
+
+:ref:`Proxy Objects <multiprocessing-proxy_objects>` returned by
+:func:`multiprocessing.Manager` can now be nested.
+(Contributed by Davin Potts in :issue:`6766`.)
+
+
+os
+--
+
+See the summary of :ref:`PEP 519 <whatsnew36-pep519>` for details on how the
+:mod:`os` and :mod:`os.path` modules now support
+:term:`path-like objects <path-like object>`.
+
+:func:`~os.scandir` now supports :class:`bytes` paths on Windows.
+
+A new :meth:`~os.scandir.close` method allows explicitly closing a
+:func:`~os.scandir` iterator. The :func:`~os.scandir` iterator now
+supports the :term:`context manager` protocol. If a :func:`scandir`
+iterator is neither exhausted nor explicitly closed a :exc:`ResourceWarning`
+will be emitted in its destructor.
+(Contributed by Serhiy Storchaka in :issue:`25994`.)
+
+On Linux, :func:`os.urandom` now blocks until the system urandom entropy pool
+is initialized to increase the security. See the :pep:`524` for the rationale.
+
+The Linux ``getrandom()`` syscall (get random bytes) is now exposed as the new
+:func:`os.getrandom` function.
+(Contributed by Victor Stinner, part of the :pep:`524`)
+
+
+pathlib
+-------
+
+:mod:`pathlib` now supports :term:`path-like objects <path-like object>`.
+(Contributed by Brett Cannon in :issue:`27186`.)
+
+See the summary of :ref:`PEP 519 <whatsnew36-pep519>` for details.
+
+
+pdb
+---
+
+The :class:`~pdb.Pdb` class constructor has a new optional *readrc* argument
+to control whether ``.pdbrc`` files should be read.
+
+
+pickle
+------
+
+Objects that need ``__new__`` called with keyword arguments can now be pickled
+using :ref:`pickle protocols <pickle-protocols>` older than protocol version 4.
+Protocol version 4 already supports this case. (Contributed by Serhiy
+Storchaka in :issue:`24164`.)
+
+
+pickletools
+-----------
+
+:func:`pickletools.dis()` now outputs the implicit memo index for the
+``MEMOIZE`` opcode.
+(Contributed by Serhiy Storchaka in :issue:`25382`.)
+
+
+pydoc
+-----
+
+The :mod:`pydoc` module has learned to respect the ``MANPAGER``
+environment variable.
+(Contributed by Matthias Klose in :issue:`8637`.)
+
+:func:`help` and :mod:`pydoc` can now list named tuple fields in the
+order they were defined rather than alphabetically.
+(Contributed by Raymond Hettinger in :issue:`24879`.)
+
+
+random
+-------
+
+The new :func:`~random.choices` function returns a list of elements of
+specified size from the given population with optional weights.
+(Contributed by Raymond Hettinger in :issue:`18844`.)
+
+
+re
+--
+
+Added support of modifier spans in regular expressions. Examples:
+``'(?i:p)ython'`` matches ``'python'`` and ``'Python'``, but not ``'PYTHON'``;
+``'(?i)g(?-i:v)r'`` matches ``'GvR'`` and ``'gvr'``, but not ``'GVR'``.
+(Contributed by Serhiy Storchaka in :issue:`433028`.)
+
+Match object groups can be accessed by ``__getitem__``, which is
+equivalent to ``group()``. So ``mo['name']`` is now equivalent to
+``mo.group('name')``. (Contributed by Eric Smith in :issue:`24454`.)
+
+:class:`~re.Match` objects now support
+:meth:`index-like objects <object.__index__>` as group
+indices.
+(Contributed by Jeroen Demeyer and Xiang Zhang in :issue:`27177`.)
+
+
+readline
+--------
+
+Added :func:`~readline.set_auto_history` to enable or disable
+automatic addition of input to the history list. (Contributed by
+Tyler Crompton in :issue:`26870`.)
+
+
+rlcompleter
+-----------
+
+Private and special attribute names now are omitted unless the prefix starts
+with underscores. A space or a colon is added after some completed keywords.
+(Contributed by Serhiy Storchaka in :issue:`25011` and :issue:`25209`.)
+
+
+shlex
+-----
+
+The :class:`~shlex.shlex` has much
+:ref:`improved shell compatibility <improved-shell-compatibility>`
+through the new *punctuation_chars* argument to control which characters
+are treated as punctuation.
+(Contributed by Vinay Sajip in :issue:`1521950`.)
+
+
+site
+----
+
+When specifying paths to add to :attr:`sys.path` in a `.pth` file,
+you may now specify file paths on top of directories (e.g. zip files).
+(Contributed by Wolfgang Langner in :issue:`26587`).
+
+
+sqlite3
+-------
+
+:attr:`sqlite3.Cursor.lastrowid` now supports the ``REPLACE`` statement.
+(Contributed by Alex LordThorsen in :issue:`16864`.)
+
+
+socket
+------
+
+The :func:`~socket.socket.ioctl` function now supports the
+:data:`~socket.SIO_LOOPBACK_FAST_PATH` control code.
+(Contributed by Daniel Stokes in :issue:`26536`.)
+
+The :meth:`~socket.socket.getsockopt` constants ``SO_DOMAIN``,
+``SO_PROTOCOL``, ``SO_PEERSEC``, and ``SO_PASSSEC`` are now supported.
+(Contributed by Christian Heimes in :issue:`26907`.)
+
+The :meth:`~socket.socket.setsockopt` now supports the
+``setsockopt(level, optname, None, optlen: int)`` form.
+(Contributed by Christian Heimes in :issue:`27744`.)
+
+The socket module now supports the address family
+:data:`~socket.AF_ALG` to interface with Linux Kernel crypto API. ``ALG_*``,
+``SOL_ALG`` and :meth:`~socket.socket.sendmsg_afalg` were added.
+(Contributed by Christian Heimes in :issue:`27744` with support from
+Victor Stinner.)
+
+
+socketserver
+------------
+
+Servers based on the :mod:`socketserver` module, including those
+defined in :mod:`http.server`, :mod:`xmlrpc.server` and
+:mod:`wsgiref.simple_server`, now support the :term:`context manager`
+protocol.
+(Contributed by Aviv Palivoda in :issue:`26404`.)
+
+The :attr:`~socketserver.StreamRequestHandler.wfile` attribute of
+:class:`~socketserver.StreamRequestHandler` classes now implements
+the :class:`io.BufferedIOBase` writable interface. In particular,
+calling :meth:`~io.BufferedIOBase.write` is now guaranteed to send the
+data in full. (Contributed by Martin Panter in :issue:`26721`.)
+
+
+ssl
+---
+
+:mod:`ssl` supports OpenSSL 1.1.0. The minimum recommend version is 1.0.2.
+(Contributed by Christian Heimes in :issue:`26470`.)
+
+3DES has been removed from the default cipher suites and ChaCha20 Poly1305
+cipher suites have been added.
+(Contributed by Christian Heimes in :issue:`27850` and :issue:`27766`.)
+
+:class:`~ssl.SSLContext` has better default configuration for options
+and ciphers.
+(Contributed by Christian Heimes in :issue:`28043`.)
+
+SSL session can be copied from one client-side connection to another
+with the new :class:`~ssl.SSLSession` class. TLS session resumption can
+speed up the initial handshake, reduce latency and improve performance
+(Contributed by Christian Heimes in :issue:`19500` based on a draft by
+Alex Warhawk.)
+
+The new :meth:`~ssl.SSLContext.get_ciphers` method can be used to
+get a list of enabled ciphers in order of cipher priority.
+
+All constants and flags have been converted to :class:`~enum.IntEnum` and
+:class:`~enum.IntFlags`.
+(Contributed by Christian Heimes in :issue:`28025`.)
+
+Server and client-side specific TLS protocols for :class:`~ssl.SSLContext`
+were added.
+(Contributed by Christian Heimes in :issue:`28085`.)
+
+
+statistics
+----------
+
+A new :func:`~statistics.harmonic_mean` function has been added.
+(Contributed by Steven D'Aprano in :issue:`27181`.)
+
+
+struct
+------
+
+:mod:`struct` now supports IEEE 754 half-precision floats via the ``'e'``
+format specifier.
+(Contributed by Eli Stevens, Mark Dickinson in :issue:`11734`.)
+
+
+subprocess
+----------
+
+:class:`subprocess.Popen` destructor now emits a :exc:`ResourceWarning` warning
+if the child process is still running. Use the context manager protocol (``with
+proc: ...``) or explicitly call the :meth:`~subprocess.Popen.wait` method to
+read the exit status of the child process. (Contributed by Victor Stinner in
+:issue:`26741`.)
+
+The :class:`subprocess.Popen` constructor and all functions that pass arguments
+through to it now accept *encoding* and *errors* arguments. Specifying either
+of these will enable text mode for the *stdin*, *stdout* and *stderr* streams.
+(Contributed by Steve Dower in :issue:`6135`.)
+
+
+sys
+---
+
+The new :func:`~sys.getfilesystemencodeerrors` function returns the name of
+the error mode used to convert between Unicode filenames and bytes filenames.
+(Contributed by Steve Dower in :issue:`27781`.)
+
+On Windows the return value of the :func:`~sys.getwindowsversion` function
+now includes the *platform_version* field which contains the accurate major
+version, minor version and build number of the current operating system,
+rather than the version that is being emulated for the process
+(Contributed by Steve Dower in :issue:`27932`.)
+
+
+telnetlib
+---------
+
+:class:`~telnetlib.Telnet` is now a context manager (contributed by
+Stéphane Wirtel in :issue:`25485`).
+
+
+time
+----
+
+The :class:`~time.struct_time` attributes :attr:`tm_gmtoff` and
+:attr:`tm_zone` are now available on all platforms.
+
+
+timeit
+------
+
+The new :meth:`Timer.autorange() <timeit.Timer.autorange>` convenience
+method has been added to call :meth:`Timer.timeit() <timeit.Timer.timeit>`
+repeatedly so that the total run time is greater or equal to 200 milliseconds.
+(Contributed by Steven D'Aprano in :issue:`6422`.)
+
+:mod:`timeit` now warns when there is substantial (4x) variance
+between best and worst times.
+(Contributed by Serhiy Storchaka in :issue:`23552`.)
+
+
+tkinter
+-------
+
+Added methods :meth:`~tkinter.Variable.trace_add`,
+:meth:`~tkinter.Variable.trace_remove` and :meth:`~tkinter.Variable.trace_info`
+in the :class:`tkinter.Variable` class. They replace old methods
+:meth:`~tkinter.Variable.trace_variable`, :meth:`~tkinter.Variable.trace`,
+:meth:`~tkinter.Variable.trace_vdelete` and
+:meth:`~tkinter.Variable.trace_vinfo` that use obsolete Tcl commands and might
+not work in future versions of Tcl.
+(Contributed by Serhiy Storchaka in :issue:`22115`).
+
+
+.. _whatsnew36-traceback:
+
+traceback
+---------
+
+Both the traceback module and the interpreter's builtin exception display now
+abbreviate long sequences of repeated lines in tracebacks as shown in the
+following example::
+
+ >>> def f(): f()
+ ...
+ >>> f()
+ Traceback (most recent call last):
+ File "<stdin>", line 1, in <module>
+ File "<stdin>", line 1, in f
+ File "<stdin>", line 1, in f
+ File "<stdin>", line 1, in f
+ [Previous line repeated 995 more times]
+ RecursionError: maximum recursion depth exceeded
+
+(Contributed by Emanuel Barry in :issue:`26823`.)
+
+
+tracemalloc
+-----------
+
+The :mod:`tracemalloc` module now supports tracing memory allocations in
+multiple different address spaces.
+
+The new :class:`~tracemalloc.DomainFilter` filter class has been added
+to filter block traces by their address space (domain).
+
+(Contributed by Victor Stinner in :issue:`26588`.)
+
+
+.. _whatsnew36-typing:
+
+typing
+------
+
+Since the :mod:`typing` module is :term:`provisional <provisional api>`,
+all changes introduced in Python 3.6 have also been
+backported to Python 3.5.x.
+
+The :mod:`typing` module has a much improved support for generic type
+aliases. For example ``Dict[str, Tuple[S, T]]`` is now a valid
+type annotation.
+(Contributed by Guido van Rossum in `Github #195
+<https://github.com/python/typing/pull/195>`_.)
+
+The :class:`typing.ContextManager` class has been added for
+representing :class:`contextlib.AbstractContextManager`.
+(Contributed by Brett Cannon in :issue:`25609`.)
+
+The :class:`typing.Collection` class has been added for
+representing :class:`collections.abc.Collection`.
+(Contributed by Ivan Levkivskyi in :issue:`27598`.)
+
+The :const:`typing.ClassVar` type construct has been added to
+mark class variables. As introduced in :pep:`526`, a variable annotation
+wrapped in ClassVar indicates that a given attribute is intended to be used as
+a class variable and should not be set on instances of that class.
+(Contributed by Ivan Levkivskyi in `Github #280
+<https://github.com/python/typing/issues/280>`_.)
+
+A new :const:`~typing.TYPE_CHECKING` constant that is assumed to be
+``True`` by the static type chekers, but is ``False`` at runtime.
+(Contributed by Guido van Rossum in `Github #230
+<https://github.com/python/typing/issues/230>`_.)
+
+A new :func:`~typing.NewType` helper function has been added to create
+lightweight distinct types for annotations::
+
+ from typing import NewType
+
+ UserId = NewType('UserId', int)
+ some_id = UserId(524313)
+
+The static type checker will treat the new type as if it were a subclass
+of the original type. (Contributed by Ivan Levkivskyi in `Github #189
+<https://github.com/python/typing/issues/189>`_.)
+
+
+unicodedata
+-----------
+
+The :mod:`unicodedata` module now uses data from `Unicode 9.0.0
+<http://unicode.org/versions/Unicode9.0.0/>`_.
+(Contributed by Benjamin Peterson.)
+
+
+unittest.mock
+-------------
+
+The :class:`~unittest.mock.Mock` class has the following improvements:
+
+* Two new methods, :meth:`Mock.assert_called()
+ <unittest.mock.Mock.assert_called>` and :meth:`Mock.assert_called_once()
+ <unittest.mock.Mock.assert_called_once>` to check if the mock object
+ was called.
+ (Contributed by Amit Saha in :issue:`26323`.)
+
+* The :meth:`Mock.reset_mock() <unittest.mock.Mock.reset_mock>` method
+ now has two optional keyword only arguments: *return_value* and
+ *side_effect*.
+ (Contributed by Kushal Das in :issue:`21271`.)
+
+
+urllib.request
+--------------
+
+If a HTTP request has a file or iterable body (other than a
+bytes object) but no ``Content-Length`` header, rather than
+throwing an error, :class:`~urllib.request.AbstractHTTPHandler` now
+falls back to use chunked transfer encoding.
+(Contributed by Demian Brecht and Rolf Krahl in :issue:`12319`.)
+
+
+urllib.robotparser
+------------------
+
+:class:`~urllib.robotparser.RobotFileParser` now supports the ``Crawl-delay`` and
+``Request-rate`` extensions.
+(Contributed by Nikolay Bogoychev in :issue:`16099`.)
+
+
+venv
+----
+
+:mod:`venv` accepts a new parameter ``--prompt``. This parameter provides an
+alternative prefix for the virtual environment. (Proposed by Łukasz Balcerzak
+and ported to 3.6 by Stéphane Wirtel in :issue:`22829`.)
+
+
+warnings
+--------
+
+A new optional *source* parameter has been added to the
+:func:`warnings.warn_explicit` function: the destroyed object which emitted a
+:exc:`ResourceWarning`. A *source* attribute has also been added to
+:class:`warnings.WarningMessage` (contributed by Victor Stinner in
+:issue:`26568` and :issue:`26567`).
+
+When a :exc:`ResourceWarning` warning is logged, the :mod:`tracemalloc` module is now
+used to try to retrieve the traceback where the destroyed object was allocated.
+
+Example with the script ``example.py``::
+
+ import warnings
+
+ def func():
+ return open(__file__)
+
+ f = func()
+ f = None
+
+Output of the command ``python3.6 -Wd -X tracemalloc=5 example.py``::
+
+ example.py:7: ResourceWarning: unclosed file <_io.TextIOWrapper name='example.py' mode='r' encoding='UTF-8'>
+ f = None
+ Object allocated at (most recent call first):
+ File "example.py", lineno 4
+ return open(__file__)
+ File "example.py", lineno 6
+ f = func()
+
+The "Object allocated at" traceback is new and is only displayed if
+:mod:`tracemalloc` is tracing Python memory allocations and if the
+:mod:`warnings` module was already imported.
+
+
+winreg
+------
+
+Added the 64-bit integer type :data:`REG_QWORD <winreg.REG_QWORD>`.
+(Contributed by Clement Rouault in :issue:`23026`.)
+
+
+winsound
+--------
+
+Allowed keyword arguments to be passed to :func:`Beep <winsound.Beep>`,
+:func:`MessageBeep <winsound.MessageBeep>`, and :func:`PlaySound
+<winsound.PlaySound>` (:issue:`27982`).
+
+
+xmlrpc.client
+-------------
+
+The :mod:`xmlrpc.client` module now supports unmarshalling
+additional data types used by the Apache XML-RPC implementation
+for numerics and ``None``.
+(Contributed by Serhiy Storchaka in :issue:`26885`.)
+
+
+zipfile
+-------
+
+A new :meth:`ZipInfo.from_file() <zipfile.ZipInfo.from_file>` class method
+allows making a :class:`~zipfile.ZipInfo` instance from a filesystem file.
+A new :meth:`ZipInfo.is_dir() <zipfile.ZipInfo.is_dir>` method can be used
+to check if the :class:`~zipfile.ZipInfo` instance represents a directory.
+(Contributed by Thomas Kluyver in :issue:`26039`.)
+
+The :meth:`ZipFile.open() <zipfile.ZipFile.open>` method can now be used to
+write data into a ZIP file, as well as for extracting data.
+(Contributed by Thomas Kluyver in :issue:`26039`.)
+
+
+zlib
+----
+
+The :func:`~zlib.compress` and :func:`~zlib.decompress` functions now accept
+keyword arguments.
+(Contributed by Aviv Palivoda in :issue:`26243` and
+Xiang Zhang in :issue:`16764` respectively.)
+
+
+Optimizations
+=============
+
+* The Python interpreter now uses a 16-bit wordcode instead of bytecode which
+ made a number of opcode optimizations possible.
+ (Contributed by Demur Rumed with input and reviews from
+ Serhiy Storchaka and Victor Stinner in :issue:`26647` and :issue:`28050`.)
+
+* The :class:`asyncio.Future` class now has an optimized C implementation.
+ (Contributed by Yury Selivanov and INADA Naoki in :issue:`26081`.)
+
+* The :class:`asyncio.Task` class now has an optimized
+ C implementation. (Contributed by Yury Selivanov in :issue:`28544`.)
+
+* Various implementation improvements in the :mod:`typing` module
+ (such as caching of generic types) allow up to 30 times performance
+ improvements and reduced memory footprint.
+
+* The ASCII decoder is now up to 60 times as fast for error handlers
+ ``surrogateescape``, ``ignore`` and ``replace`` (Contributed
+ by Victor Stinner in :issue:`24870`).
+
+* The ASCII and the Latin1 encoders are now up to 3 times as fast for the
+ error handler ``surrogateescape``
+ (Contributed by Victor Stinner in :issue:`25227`).
+
+* The UTF-8 encoder is now up to 75 times as fast for error handlers
+ ``ignore``, ``replace``, ``surrogateescape``, ``surrogatepass`` (Contributed
+ by Victor Stinner in :issue:`25267`).
+
+* The UTF-8 decoder is now up to 15 times as fast for error handlers
+ ``ignore``, ``replace`` and ``surrogateescape`` (Contributed
+ by Victor Stinner in :issue:`25301`).
+
+* ``bytes % args`` is now up to 2 times faster. (Contributed by Victor Stinner
+ in :issue:`25349`).
+
+* ``bytearray % args`` is now between 2.5 and 5 times faster. (Contributed by
+ Victor Stinner in :issue:`25399`).
+
+* Optimize :meth:`bytes.fromhex` and :meth:`bytearray.fromhex`: they are now
+ between 2x and 3.5x faster. (Contributed by Victor Stinner in :issue:`25401`).
+
+* Optimize ``bytes.replace(b'', b'.')`` and ``bytearray.replace(b'', b'.')``:
+ up to 80% faster. (Contributed by Josh Snider in :issue:`26574`).
+
+* Allocator functions of the :c:func:`PyMem_Malloc` domain
+ (:c:data:`PYMEM_DOMAIN_MEM`) now use the :ref:`pymalloc memory allocator
+ <pymalloc>` instead of :c:func:`malloc` function of the C library. The
+ pymalloc allocator is optimized for objects smaller or equal to 512 bytes
+ with a short lifetime, and use :c:func:`malloc` for larger memory blocks.
+ (Contributed by Victor Stinner in :issue:`26249`).
+
+* :func:`pickle.load` and :func:`pickle.loads` are now up to 10% faster when
+ deserializing many small objects (Contributed by Victor Stinner in
+ :issue:`27056`).
+
+* Passing :term:`keyword arguments <keyword argument>` to a function has an
+ overhead in comparison with passing :term:`positional arguments
+ <positional argument>`. Now in extension functions implemented with using
+ Argument Clinic this overhead is significantly decreased.
+ (Contributed by Serhiy Storchaka in :issue:`27574`).
+
+* Optimized :func:`~glob.glob` and :func:`~glob.iglob` functions in the
+ :mod:`glob` module; they are now about 3--6 times faster.
+ (Contributed by Serhiy Storchaka in :issue:`25596`).
+
+* Optimized globbing in :mod:`pathlib` by using :func:`os.scandir`;
+ it is now about 1.5--4 times faster.
+ (Contributed by Serhiy Storchaka in :issue:`26032`).
+
+* :class:`xml.etree.ElementTree` parsing, iteration and deepcopy performance
+ has been significantly improved.
+ (Contributed by Serhiy Storchaka in :issue:`25638`, :issue:`25873`,
+ and :issue:`25869`.)
+
+* Creation of :class:`fractions.Fraction` instances from floats and
+ decimals is now 2 to 3 times faster.
+ (Contributed by Serhiy Storchaka in :issue:`25971`.)
+
+
+Build and C API Changes
+=======================
+
+* Python now requires some C99 support in the toolchain to build.
+ Most notably, Python now uses standard integer types and macros in
+ place of custom macros like ``PY_LONG_LONG``.
+ For more information, see :pep:`7` and :issue:`17884`.
+
+* Cross-compiling CPython with the Android NDK and the Android API level set to
+ 21 (Android 5.0 Lollilop) or greater runs successfully. While Android is not
+ yet a supported platform, the Python test suite runs on the Android emulator
+ with only about 16 tests failures. See the Android meta-issue :issue:`26865`.
+
+* The ``--enable-optimizations`` configure flag has been added. Turning it on
+ will activate expensive optimizations like PGO.
+ (Original patch by Alecsandru Patrascu of Intel in :issue:`26359`.)
+
+* The :term:`GIL <global interpreter lock>` must now be held when allocator
+ functions of :c:data:`PYMEM_DOMAIN_OBJ` (ex: :c:func:`PyObject_Malloc`) and
+ :c:data:`PYMEM_DOMAIN_MEM` (ex: :c:func:`PyMem_Malloc`) domains are called.
+
+* New :c:func:`Py_FinalizeEx` API which indicates if flushing buffered data
+ failed.
+ (Contributed by Martin Panter in :issue:`5319`.)
+
+* :c:func:`PyArg_ParseTupleAndKeywords` now supports :ref:`positional-only
+ parameters <positional-only_parameter>`. Positional-only parameters are
+ defined by empty names.
+ (Contributed by Serhiy Storchaka in :issue:`26282`).
+
+* ``PyTraceback_Print`` method now abbreviates long sequences of repeated lines
+ as ``"[Previous line repeated {count} more times]"``.
+ (Contributed by Emanuel Barry in :issue:`26823`.)
+
+* The new :c:func:`PyErr_SetImportErrorSubclass` function allows for
+ specifying a subclass of :exc:`ImportError` to raise.
+ (Contributed by Eric Snow in :issue:`15767`.)
+
+* The new :c:func:`PyErr_ResourceWarning` function can be used to generate
+ a :exc:`ResourceWarning` providing the source of the resource allocation.
+ (Contributed by Victor Stinner in :issue:`26567`.)
+
+* The new :c:func:`PyOS_FSPath` function returns the file system
+ representation of a :term:`path-like object`.
+ (Contributed by Brett Cannon in :issue:`27186`.)
+
+* The :c:func:`PyUnicode_FSConverter` and :c:func:`PyUnicode_FSDecoder`
+ functions will now accept :term:`path-like objects <path-like object>`.
+
+
+Other Improvements
+==================
+
+* When :option:`--version` (short form: :option:`-V`) is supplied twice,
+ Python prints :data:`sys.version` for detailed information.
+
+ .. code-block:: shell-session
+
+ $ ./python -VV
+ Python 3.6.0b4+ (3.6:223967b49e49+, Nov 21 2016, 20:55:04)
+ [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)]
+
+
+Deprecated
+==========
+
+New Keywords
+------------
+
+``async`` and ``await`` are not recommended to be used as variable, class,
+function or module names. Introduced by :pep:`492` in Python 3.5, they will
+become proper keywords in Python 3.7. Starting in Python 3.6, the use of
+``async`` or ``await`` as names will generate a :exc:`DeprecationWarning`.
+
+
+Deprecated Python behavior
+--------------------------
+
+Raising the :exc:`StopIteration` exception inside a generator will now
+generate a :exc:`DeprecationWarning`, and will trigger a :exc:`RuntimeError`
+in Python 3.7. See :ref:`whatsnew-pep-479` for details.
+
+The :meth:`__aiter__` method is now expected to return an asynchronous
+iterator directly instead of returning an awaitable as previously.
+Doing the former will trigger a :exc:`DeprecationWarning`. Backward
+compatibility will be removed in Python 3.7.
+(Contributed by Yury Selivanov in :issue:`27243`.)
+
+A backslash-character pair that is not a valid escape sequence now generates
+a :exc:`DeprecationWarning`. Although this will eventually become a
+:exc:`SyntaxError`, that will not be for several Python releases.
+(Contributed by Emanuel Barry in :issue:`27364`.)
+
+When performing a relative import, falling back on ``__name__`` and
+``__path__`` from the calling module when ``__spec__`` or
+``__package__`` are not defined now raises an :exc:`ImportWarning`.
+(Contributed by Rose Ames in :issue:`25791`.)
+
+
+Deprecated Python modules, functions and methods
+------------------------------------------------
+
+asynchat
+~~~~~~~~
+
+The :mod:`asynchat` has been deprecated in favor of :mod:`asyncio`.
+(Contributed by Mariatta in :issue:`25002`.)
+
+
+asyncore
+~~~~~~~~
+
+The :mod:`asyncore` has been deprecated in favor of :mod:`asyncio`.
+(Contributed by Mariatta in :issue:`25002`.)
+
+
+dbm
+~~~
+
+Unlike other :mod:`dbm` implementations, the :mod:`dbm.dumb` module
+creates databases with the ``'rw'`` mode and allows modifying the database
+opened with the ``'r'`` mode. This behavior is now deprecated and will
+be removed in 3.8.
+(Contributed by Serhiy Storchaka in :issue:`21708`.)
+
+
+distutils
+~~~~~~~~~
+
+The undocumented ``extra_path`` argument to the
+:class:`~distutils.Distribution` constructor is now considered deprecated
+and will raise a warning if set. Support for this parameter will be
+removed in a future Python release. See :issue:`27919` for details.
+
+
+grp
+~~~
+
+The support of non-integer arguments in :func:`~grp.getgrgid` has been
+deprecated.
+(Contributed by Serhiy Storchaka in :issue:`26129`.)
+
+
+importlib
+~~~~~~~~~
+
+The :meth:`importlib.machinery.SourceFileLoader.load_module` and
+:meth:`importlib.machinery.SourcelessFileLoader.load_module` methods
+are now deprecated. They were the only remaining implementations of
+:meth:`importlib.abc.Loader.load_module` in :mod:`importlib` that had not
+been deprecated in previous versions of Python in favour of
+:meth:`importlib.abc.Loader.exec_module`.
+
+The :class:`importlib.machinery.WindowsRegistryFinder` class is now
+deprecated. As of 3.6.0, it is still added to :attr:`sys.meta_path` by
+default (on Windows), but this may change in future releases.
+
+os
+~~
+
+Undocumented support of general :term:`bytes-like objects <bytes-like object>`
+as paths in :mod:`os` functions, :func:`compile` and similar functions is
+now deprecated.
+(Contributed by Serhiy Storchaka in :issue:`25791` and :issue:`26754`.)
+
+re
+~~
+
+Support for inline flags ``(?letters)`` in the middle of the regular
+expression has been deprecated and will be removed in a future Python
+version. Flags at the start of a regular expression are still allowed.
+(Contributed by Serhiy Storchaka in :issue:`22493`.)
+
+ssl
+~~~
+
+OpenSSL 0.9.8, 1.0.0 and 1.0.1 are deprecated and no longer supported.
+In the future the :mod:`ssl` module will require at least OpenSSL 1.0.2 or
+1.1.0.
+
+SSL-related arguments like ``certfile``, ``keyfile`` and ``check_hostname``
+in :mod:`ftplib`, :mod:`http.client`, :mod:`imaplib`, :mod:`poplib`,
+and :mod:`smtplib` have been deprecated in favor of ``context``.
+(Contributed by Christian Heimes in :issue:`28022`.)
+
+A couple of protocols and functions of the :mod:`ssl` module are now
+deprecated. Some features will no longer be available in future versions
+of OpenSSL. Other features are deprecated in favor of a different API.
+(Contributed by Christian Heimes in :issue:`28022` and :issue:`26470`.)
+
+tkinter
+~~~~~~~
+
+The :mod:`tkinter.tix` module is now deprecated. :mod:`tkinter` users
+should use :mod:`tkinter.ttk` instead.
+
+venv
+~~~~
+
+The ``pyvenv`` script has been deprecated in favour of ``python3 -m venv``.
+This prevents confusion as to what Python interpreter ``pyvenv`` is
+connected to and thus what Python interpreter will be used by the virtual
+environment. (Contributed by Brett Cannon in :issue:`25154`.)
+
+
+Deprecated functions and types of the C API
+-------------------------------------------
+
+Undocumented functions :c:func:`PyUnicode_AsEncodedObject`,
+:c:func:`PyUnicode_AsDecodedObject`, :c:func:`PyUnicode_AsEncodedUnicode`
+and :c:func:`PyUnicode_AsDecodedUnicode` are deprecated now.
+Use the :ref:`generic codec based API <codec-registry>` instead.
+
+
+Deprecated Build Options
+------------------------
+
+The ``--with-system-ffi`` configure flag is now on by default on non-macOS
+UNIX platforms. It may be disabled by using ``--without-system-ffi``, but
+using the flag is deprecated and will not be accepted in Python 3.7.
+macOS is unaffected by this change. Note that many OS distributors already
+use the ``--with-system-ffi`` flag when building their system Python.
+
+
+Removed
+=======
+
+API and Feature Removals
+------------------------
+
+* Unknown escapes consisting of ``'\'`` and an ASCII letter in
+ regular expressions will now cause an error. In replacement templates for
+ :func:`re.sub` they are still allowed, but deprecated.
+ The :const:`re.LOCALE` flag can now only be used with binary patterns.
+
+* ``inspect.getmoduleinfo()`` was removed (was deprecated since CPython 3.3).
+ :func:`inspect.getmodulename` should be used for obtaining the module
+ name for a given path.
+ (Contributed by Yury Selivanov in :issue:`13248`.)
+
+* ``traceback.Ignore`` class and ``traceback.usage``, ``traceback.modname``,
+ ``traceback.fullmodname``, ``traceback.find_lines_from_code``,
+ ``traceback.find_lines``, ``traceback.find_strings``,
+ ``traceback.find_executable_lines`` methods were removed from the
+ :mod:`traceback` module. They were undocumented methods deprecated since
+ Python 3.2 and equivalent functionality is available from private methods.
+
+* The ``tk_menuBar()`` and ``tk_bindForTraversal()`` dummy methods in
+ :mod:`tkinter` widget classes were removed (corresponding Tk commands
+ were obsolete since Tk 4.0).
+
+* The :meth:`~zipfile.ZipFile.open` method of the :class:`zipfile.ZipFile`
+ class no longer supports the ``'U'`` mode (was deprecated since Python 3.4).
+ Use :class:`io.TextIOWrapper` for reading compressed text files in
+ :term:`universal newlines` mode.
+
+* The undocumented ``IN``, ``CDROM``, ``DLFCN``, ``TYPES``, ``CDIO``, and
+ ``STROPTS`` modules have been removed. They had been available in the
+ platform specific ``Lib/plat-*/`` directories, but were chronically out of
+ date, inconsistently available across platforms, and unmaintained. The
+ script that created these modules is still available in the source
+ distribution at :source:`Tools/scripts/h2py.py`.
+
+* The deprecated ``asynchat.fifo`` class has been removed.
+
+
+Porting to Python 3.6
+=====================
+
+This section lists previously described changes and other bugfixes
+that may require changes to your code.
+
+Changes in 'python' Command Behavior
+------------------------------------
+
+* The output of a special Python build with defined ``COUNT_ALLOCS``,
+ ``SHOW_ALLOC_COUNT`` or ``SHOW_TRACK_COUNT`` macros is now off by
+ default. It can be re-enabled using the ``-X showalloccount`` option.
+ It now outputs to ``stderr`` instead of ``stdout``.
+ (Contributed by Serhiy Storchaka in :issue:`23034`.)
+
+
+Changes in the Python API
+-------------------------
+
+* :func:`open() <open>` will no longer allow combining the ``'U'`` mode flag
+ with ``'+'``.
+ (Contributed by Jeff Balogh and John O'Connor in :issue:`2091`.)
+
+* :mod:`sqlite3` no longer implicitly commits an open transaction before DDL
+ statements.
+
+* On Linux, :func:`os.urandom` now blocks until the system urandom entropy pool
+ is initialized to increase the security.
+
+* When :meth:`importlib.abc.Loader.exec_module` is defined,
+ :meth:`importlib.abc.Loader.create_module` must also be defined.
+
+* :c:func:`PyErr_SetImportError` now sets :exc:`TypeError` when its **msg**
+ argument is not set. Previously only ``NULL`` was returned.
+
+* The format of the ``co_lnotab`` attribute of code objects changed to support
+ a negative line number delta. By default, Python does not emit bytecode with
+ a negative line number delta. Functions using ``frame.f_lineno``,
+ ``PyFrame_GetLineNumber()`` or ``PyCode_Addr2Line()`` are not affected.
+ Functions directly decoding ``co_lnotab`` should be updated to use a signed
+ 8-bit integer type for the line number delta, but this is only required to
+ support applications using a negative line number delta. See
+ ``Objects/lnotab_notes.txt`` for the ``co_lnotab`` format and how to decode
+ it, and see the :pep:`511` for the rationale.
+
+* The functions in the :mod:`compileall` module now return booleans instead
+ of ``1`` or ``0`` to represent success or failure, respectively. Thanks to
+ booleans being a subclass of integers, this should only be an issue if you
+ were doing identity checks for ``1`` or ``0``. See :issue:`25768`.
+
+* Reading the :attr:`~urllib.parse.SplitResult.port` attribute of
+ :func:`urllib.parse.urlsplit` and :func:`~urllib.parse.urlparse` results
+ now raises :exc:`ValueError` for out-of-range values, rather than
+ returning :const:`None`. See :issue:`20059`.
+
+* The :mod:`imp` module now raises a :exc:`DeprecationWarning` instead of
+ :exc:`PendingDeprecationWarning`.
+
+* The following modules have had missing APIs added to their :attr:`__all__`
+ attributes to match the documented APIs:
+ :mod:`calendar`, :mod:`cgi`, :mod:`csv`,
+ :mod:`~xml.etree.ElementTree`, :mod:`enum`,
+ :mod:`fileinput`, :mod:`ftplib`, :mod:`logging`, :mod:`mailbox`,
+ :mod:`mimetypes`, :mod:`optparse`, :mod:`plistlib`, :mod:`smtpd`,
+ :mod:`subprocess`, :mod:`tarfile`, :mod:`threading` and
+ :mod:`wave`. This means they will export new symbols when ``import *``
+ is used.
+ (Contributed by Joel Taddei and Jacek Kołodziej in :issue:`23883`.)
+
+* When performing a relative import, if ``__package__`` does not compare equal
+ to ``__spec__.parent`` then :exc:`ImportWarning` is raised.
+ (Contributed by Brett Cannon in :issue:`25791`.)
+
+* When a relative import is performed and no parent package is known, then
+ :exc:`ImportError` will be raised. Previously, :exc:`SystemError` could be
+ raised. (Contributed by Brett Cannon in :issue:`18018`.)
+
+* Servers based on the :mod:`socketserver` module, including those
+ defined in :mod:`http.server`, :mod:`xmlrpc.server` and
+ :mod:`wsgiref.simple_server`, now only catch exceptions derived
+ from :exc:`Exception`. Therefore if a request handler raises
+ an exception like :exc:`SystemExit` or :exc:`KeyboardInterrupt`,
+ :meth:`~socketserver.BaseServer.handle_error` is no longer called, and
+ the exception will stop a single-threaded server. (Contributed by
+ Martin Panter in :issue:`23430`.)
+
+* :func:`spwd.getspnam` now raises a :exc:`PermissionError` instead of
+ :exc:`KeyError` if the user doesn't have privileges.
+
+* The :meth:`socket.socket.close` method now raises an exception if
+ an error (e.g. ``EBADF``) was reported by the underlying system call.
+ (Contributed by Martin Panter in :issue:`26685`.)
+
+* The *decode_data* argument for the :class:`smtpd.SMTPChannel` and
+ :class:`smtpd.SMTPServer` constructors is now ``False`` by default.
+ This means that the argument passed to
+ :meth:`~smtpd.SMTPServer.process_message` is now a bytes object by
+ default, and ``process_message()`` will be passed keyword arguments.
+ Code that has already been updated in accordance with the deprecation
+ warning generated by 3.5 will not be affected.
+
+* All optional arguments of the :func:`~json.dump`, :func:`~json.dumps`,
+ :func:`~json.load` and :func:`~json.loads` functions and
+ :class:`~json.JSONEncoder` and :class:`~json.JSONDecoder` class
+ constructors in the :mod:`json` module are now :ref:`keyword-only
+ <keyword-only_parameter>`.
+ (Contributed by Serhiy Storchaka in :issue:`18726`.)
+
+* Subclasses of :class:`type` which don't override ``type.__new__`` may no
+ longer use the one-argument form to get the type of an object.
+
+* As part of :pep:`487`, the handling of keyword arguments passed to
+ :class:`type` (other than the metaclass hint, ``metaclass``) is now
+ consistently delegated to :meth:`object.__init_subclass__`. This means that
+ :meth:`type.__new__` and :meth:`type.__init__` both now accept arbitrary
+ keyword arguments, but :meth:`object.__init_subclass__` (which is called from
+ :meth:`type.__new__`) will reject them by default. Custom metaclasses
+ accepting additional keyword arguments will need to adjust their calls to
+ :meth:`type.__new__` (whether direct or via :class:`super`) accordingly.
+
+* In :class:`distutils.command.sdist.sdist`, the ``default_format``
+ attribute has been removed and is no longer honored. Instead, the
+ gzipped tarfile format is the default on all platforms and no
+ platform-specific selection is made.
+ In environments where distributions are
+ built on Windows and zip distributions are required, configure
+ the project with a ``setup.cfg`` file containing the following::
+
+ [sdist]
+ formats=zip
+
+ This behavior has also been backported to earlier Python versions
+ by Setuptools 26.0.0.
+
+* In the :mod:`urllib.request` module and the
+ :meth:`http.client.HTTPConnection.request` method, if no Content-Length
+ header field has been specified and the request body is a file object,
+ it is now sent with HTTP 1.1 chunked encoding. If a file object has to
+ be sent to a HTTP 1.0 server, the Content-Length value now has to be
+ specified by the caller.
+ (Contributed by Demian Brecht and Rolf Krahl with tweaks from
+ Martin Panter in :issue:`12319`.)
+
+* The :class:`~csv.DictReader` now returns rows of type
+ :class:`~collections.OrderedDict`.
+ (Contributed by Steve Holden in :issue:`27842`.)
+
+* The :const:`crypt.METHOD_CRYPT` will no longer be added to ``crypt.methods``
+ if unsupported by the platform.
+ (Contributed by Victor Stinner in :issue:`25287`.)
+
+* The *verbose* and *rename* arguments for
+ :func:`~collections.namedtuple` are now keyword-only.
+ (Contributed by Raymond Hettinger in :issue:`25628`.)
+
+* On Linux, :func:`ctypes.util.find_library` now looks in
+ ``LD_LIBRARY_PATH`` for shared libraries.
+ (Contributed by Vinay Sajip in :issue:`9998`.)
+
+* The :class:`imaplib.IMAP4` class now handles flags containing the
+ ``']'`` character in messages sent from the server to improve
+ real-world compatibility.
+ (Contributed by Lita Cho in :issue:`21815`.)
+
+* The :func:`mmap.write() <mmap.write>` function now returns the number
+ of bytes written like other write methods.
+ (Contributed by Jakub Stasiak in :issue:`26335`.)
+
+* The :func:`pkgutil.iter_modules` and :func:`pkgutil.walk_packages`
+ functions now return :class:`~pkgutil.ModuleInfo` named tuples.
+ (Contributed by Ramchandra Apte in :issue:`17211`.)
+
+* :func:`re.sub` now raises an error for invalid numerical group
+ references in replacement templates even if the pattern is not
+ found in the string. The error message for invalid group references
+ now includes the group index and the position of the reference.
+ (Contributed by SilentGhost, Serhiy Storchaka in :issue:`25953`.)
+
+* :class:`zipfile.ZipFile` will now raise :exc:`NotImplementedError` for
+ unrecognized compression values. Previously a plain :exc:`RuntimeError`
+ was raised. Additionally, calling :class:`~zipfile.ZipFile` methods
+ on a closed ZipFile or calling the :meth:`~zipfile.ZipFile.write` method
+ on a ZipFile created with mode ``'r'`` will raise a :exc:`ValueError`.
+ Previously, a :exc:`RuntimeError` was raised in those scenarios.
+
+* when custom metaclasses are combined with zero-argument :func:`super` or
+ direct references from methods to the implicit ``__class__`` closure
+ variable, the implicit ``__classcell__`` namespace entry must now be passed
+ up to ``type.__new__`` for initialisation. Failing to do so will result in
+ a :exc:`DeprecationWarning` in 3.6 and a :exc:`RuntimeWarning` in the future.
+
+Changes in the C API
+--------------------
+
+* The :c:func:`PyMem_Malloc` allocator family now uses the :ref:`pymalloc allocator
+ <pymalloc>` rather than the system :c:func:`malloc`. Applications calling
+ :c:func:`PyMem_Malloc` without holding the GIL can now crash. Set the
+ :envvar:`PYTHONMALLOC` environment variable to ``debug`` to validate the
+ usage of memory allocators in your application. See :issue:`26249`.
+
+* :c:func:`Py_Exit` (and the main interpreter) now override the exit status
+ with 120 if flushing buffered data failed. See :issue:`5319`.
+
+
+CPython bytecode changes
+------------------------
+
+There have been several major changes to the :term:`bytecode` in Python 3.6.
+
+* The Python interpreter now uses a 16-bit wordcode instead of bytecode.
+ (Contributed by Demur Rumed with input and reviews from
+ Serhiy Storchaka and Victor Stinner in :issue:`26647` and :issue:`28050`.)
+
+* The new :opcode:`FORMAT_VALUE` and :opcode:`BUILD_STRING` opcodes as part
+ of the :ref:`formatted string literal <whatsnew36-pep498>` implementation.
+ (Contributed by Eric Smith in :issue:`25483` and
+ Serhiy Storchaka in :issue:`27078`.)
+
+* The new :opcode:`BUILD_CONST_KEY_MAP` opcode to optimize the creation
+ of dictionaries with constant keys.
+ (Contributed by Serhiy Storchaka in :issue:`27140`.)
+
+* The function call opcodes have been heavily reworked for better performance
+ and simpler implementation.
+ The :opcode:`MAKE_FUNCTION`, :opcode:`CALL_FUNCTION`,
+ :opcode:`CALL_FUNCTION_KW` and :opcode:`BUILD_MAP_UNPACK_WITH_CALL` opcodes
+ have been modified, the new :opcode:`CALL_FUNCTION_EX` and
+ :opcode:`BUILD_TUPLE_UNPACK_WITH_CALL` have been added, and
+ ``CALL_FUNCTION_VAR``, ``CALL_FUNCTION_VAR_KW`` and ``MAKE_CLOSURE`` opcodes
+ have been removed.
+ (Contributed by Demur Rumed in :issue:`27095`, and Serhiy Storchaka in
+ :issue:`27213`, :issue:`28257`.)
+
+* The new :opcode:`SETUP_ANNOTATIONS` and :opcode:`STORE_ANNOTATION` opcodes
+ have been added to support the new :term:`variable annotation` syntax.
+ (Contributed by Ivan Levkivskyi in :issue:`27985`.)
diff --git a/Doc/whatsnew/index.rst b/Doc/whatsnew/index.rst
index edb5502..7c92524 100644
--- a/Doc/whatsnew/index.rst
+++ b/Doc/whatsnew/index.rst
@@ -11,6 +11,7 @@ anyone wishing to stay up-to-date after a new release.
.. toctree::
:maxdepth: 2
+ 3.6.rst
3.5.rst
3.4.rst
3.3.rst
diff --git a/Grammar/Grammar b/Grammar/Grammar
index 4307523..b139e9f 100644
--- a/Grammar/Grammar
+++ b/Grammar/Grammar
@@ -27,25 +27,31 @@ async_funcdef: ASYNC funcdef
funcdef: 'def' NAME parameters ['->' test] ':' suite
parameters: '(' [typedargslist] ')'
-typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [','
- ['*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef]]
- | '*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef)
+typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
+ '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
+ | '**' tfpdef [',']]]
+ | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
+ | '**' tfpdef [','])
tfpdef: NAME [':' test]
-varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [','
- ['*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef]]
- | '*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef)
+varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
+ '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
+ | '**' vfpdef [',']]]
+ | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
+ | '**' vfpdef [',']
+)
vfpdef: NAME
stmt: simple_stmt | compound_stmt
simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
small_stmt: (expr_stmt | del_stmt | pass_stmt | flow_stmt |
import_stmt | global_stmt | nonlocal_stmt | assert_stmt)
-expr_stmt: testlist_star_expr (augassign (yield_expr|testlist) |
+expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
('=' (yield_expr|testlist_star_expr))*)
+annassign: ':' test ['=' test]
testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
'<<=' | '>>=' | '**=' | '//=')
-# For normal assignments, additional restrictions enforced by the interpreter
+# For normal and annotated assignments, additional restrictions enforced by the interpreter
del_stmt: 'del' exprlist
pass_stmt: 'pass'
flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
@@ -140,7 +146,7 @@ argument: ( test [comp_for] |
'*' test )
comp_iter: comp_for | comp_if
-comp_for: 'for' exprlist 'in' or_test [comp_iter]
+comp_for: [ASYNC] 'for' exprlist 'in' or_test [comp_iter]
comp_if: 'if' test_nocond [comp_iter]
# not used in grammar, but may appear in "node" passed from Parser to Compiler
diff --git a/Include/Python-ast.h b/Include/Python-ast.h
index 2d3eacb..70494b7 100644
--- a/Include/Python-ast.h
+++ b/Include/Python-ast.h
@@ -65,11 +65,12 @@ struct _mod {
enum _stmt_kind {FunctionDef_kind=1, AsyncFunctionDef_kind=2, ClassDef_kind=3,
Return_kind=4, Delete_kind=5, Assign_kind=6,
- AugAssign_kind=7, For_kind=8, AsyncFor_kind=9, While_kind=10,
- If_kind=11, With_kind=12, AsyncWith_kind=13, Raise_kind=14,
- Try_kind=15, Assert_kind=16, Import_kind=17,
- ImportFrom_kind=18, Global_kind=19, Nonlocal_kind=20,
- Expr_kind=21, Pass_kind=22, Break_kind=23, Continue_kind=24};
+ AugAssign_kind=7, AnnAssign_kind=8, For_kind=9,
+ AsyncFor_kind=10, While_kind=11, If_kind=12, With_kind=13,
+ AsyncWith_kind=14, Raise_kind=15, Try_kind=16,
+ Assert_kind=17, Import_kind=18, ImportFrom_kind=19,
+ Global_kind=20, Nonlocal_kind=21, Expr_kind=22, Pass_kind=23,
+ Break_kind=24, Continue_kind=25};
struct _stmt {
enum _stmt_kind kind;
union {
@@ -118,6 +119,13 @@ struct _stmt {
struct {
expr_ty target;
+ expr_ty annotation;
+ expr_ty value;
+ int simple;
+ } AnnAssign;
+
+ struct {
+ expr_ty target;
expr_ty iter;
asdl_seq *body;
asdl_seq *orelse;
@@ -201,9 +209,10 @@ enum _expr_kind {BoolOp_kind=1, BinOp_kind=2, UnaryOp_kind=3, Lambda_kind=4,
SetComp_kind=9, DictComp_kind=10, GeneratorExp_kind=11,
Await_kind=12, Yield_kind=13, YieldFrom_kind=14,
Compare_kind=15, Call_kind=16, Num_kind=17, Str_kind=18,
- Bytes_kind=19, NameConstant_kind=20, Ellipsis_kind=21,
- Attribute_kind=22, Subscript_kind=23, Starred_kind=24,
- Name_kind=25, List_kind=26, Tuple_kind=27};
+ FormattedValue_kind=19, JoinedStr_kind=20, Bytes_kind=21,
+ NameConstant_kind=22, Ellipsis_kind=23, Constant_kind=24,
+ Attribute_kind=25, Subscript_kind=26, Starred_kind=27,
+ Name_kind=28, List_kind=29, Tuple_kind=30};
struct _expr {
enum _expr_kind kind;
union {
@@ -297,6 +306,16 @@ struct _expr {
} Str;
struct {
+ expr_ty value;
+ int conversion;
+ expr_ty format_spec;
+ } FormattedValue;
+
+ struct {
+ asdl_seq *values;
+ } JoinedStr;
+
+ struct {
bytes s;
} Bytes;
@@ -305,6 +324,10 @@ struct _expr {
} NameConstant;
struct {
+ constant value;
+ } Constant;
+
+ struct {
expr_ty value;
identifier attr;
expr_context_ty ctx;
@@ -366,6 +389,7 @@ struct _comprehension {
expr_ty target;
expr_ty iter;
asdl_seq *ifs;
+ int is_async;
};
enum _excepthandler_kind {ExceptHandler_kind=1};
@@ -446,6 +470,9 @@ stmt_ty _Py_Assign(asdl_seq * targets, expr_ty value, int lineno, int
#define AugAssign(a0, a1, a2, a3, a4, a5) _Py_AugAssign(a0, a1, a2, a3, a4, a5)
stmt_ty _Py_AugAssign(expr_ty target, operator_ty op, expr_ty value, int
lineno, int col_offset, PyArena *arena);
+#define AnnAssign(a0, a1, a2, a3, a4, a5, a6) _Py_AnnAssign(a0, a1, a2, a3, a4, a5, a6)
+stmt_ty _Py_AnnAssign(expr_ty target, expr_ty annotation, expr_ty value, int
+ simple, int lineno, int col_offset, PyArena *arena);
#define For(a0, a1, a2, a3, a4, a5, a6) _Py_For(a0, a1, a2, a3, a4, a5, a6)
stmt_ty _Py_For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq *
orelse, int lineno, int col_offset, PyArena *arena);
@@ -543,6 +570,12 @@ expr_ty _Py_Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, int
expr_ty _Py_Num(object n, int lineno, int col_offset, PyArena *arena);
#define Str(a0, a1, a2, a3) _Py_Str(a0, a1, a2, a3)
expr_ty _Py_Str(string s, int lineno, int col_offset, PyArena *arena);
+#define FormattedValue(a0, a1, a2, a3, a4, a5) _Py_FormattedValue(a0, a1, a2, a3, a4, a5)
+expr_ty _Py_FormattedValue(expr_ty value, int conversion, expr_ty format_spec,
+ int lineno, int col_offset, PyArena *arena);
+#define JoinedStr(a0, a1, a2, a3) _Py_JoinedStr(a0, a1, a2, a3)
+expr_ty _Py_JoinedStr(asdl_seq * values, int lineno, int col_offset, PyArena
+ *arena);
#define Bytes(a0, a1, a2, a3) _Py_Bytes(a0, a1, a2, a3)
expr_ty _Py_Bytes(bytes s, int lineno, int col_offset, PyArena *arena);
#define NameConstant(a0, a1, a2, a3) _Py_NameConstant(a0, a1, a2, a3)
@@ -550,6 +583,9 @@ expr_ty _Py_NameConstant(singleton value, int lineno, int col_offset, PyArena
*arena);
#define Ellipsis(a0, a1, a2) _Py_Ellipsis(a0, a1, a2)
expr_ty _Py_Ellipsis(int lineno, int col_offset, PyArena *arena);
+#define Constant(a0, a1, a2, a3) _Py_Constant(a0, a1, a2, a3)
+expr_ty _Py_Constant(constant value, int lineno, int col_offset, PyArena
+ *arena);
#define Attribute(a0, a1, a2, a3, a4, a5) _Py_Attribute(a0, a1, a2, a3, a4, a5)
expr_ty _Py_Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int
lineno, int col_offset, PyArena *arena);
@@ -574,9 +610,9 @@ slice_ty _Py_Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena);
slice_ty _Py_ExtSlice(asdl_seq * dims, PyArena *arena);
#define Index(a0, a1) _Py_Index(a0, a1)
slice_ty _Py_Index(expr_ty value, PyArena *arena);
-#define comprehension(a0, a1, a2, a3) _Py_comprehension(a0, a1, a2, a3)
+#define comprehension(a0, a1, a2, a3, a4) _Py_comprehension(a0, a1, a2, a3, a4)
comprehension_ty _Py_comprehension(expr_ty target, expr_ty iter, asdl_seq *
- ifs, PyArena *arena);
+ ifs, int is_async, PyArena *arena);
#define ExceptHandler(a0, a1, a2, a3, a4, a5) _Py_ExceptHandler(a0, a1, a2, a3, a4, a5)
excepthandler_ty _Py_ExceptHandler(expr_ty type, identifier name, asdl_seq *
body, int lineno, int col_offset, PyArena
diff --git a/Include/Python.h b/Include/Python.h
index 858dbd1..4c7c9a4 100644
--- a/Include/Python.h
+++ b/Include/Python.h
@@ -116,6 +116,7 @@
#include "pylifecycle.h"
#include "ceval.h"
#include "sysmodule.h"
+#include "osmodule.h"
#include "intrcheck.h"
#include "import.h"
diff --git a/Include/abstract.h b/Include/abstract.h
index 7dbbb74..7d137a2 100644
--- a/Include/abstract.h
+++ b/Include/abstract.h
@@ -7,7 +7,9 @@ extern "C" {
#ifdef PY_SSIZE_T_CLEAN
#define PyObject_CallFunction _PyObject_CallFunction_SizeT
#define PyObject_CallMethod _PyObject_CallMethod_SizeT
+#ifndef Py_LIMITED_API
#define _PyObject_CallMethodId _PyObject_CallMethodId_SizeT
+#endif /* !Py_LIMITED_API */
#endif
/* Abstract Object Interface (many thanks to Jim Fulton) */
@@ -264,19 +266,97 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
*/
PyAPI_FUNC(PyObject *) PyObject_Call(PyObject *callable_object,
- PyObject *args, PyObject *kw);
+ PyObject *args, PyObject *kwargs);
/*
Call a callable Python object, callable_object, with
arguments and keywords arguments. The 'args' argument can not be
- NULL, but the 'kw' argument can be NULL.
+ NULL.
*/
#ifndef Py_LIMITED_API
+ PyAPI_FUNC(PyObject*) _PyStack_AsTuple(
+ PyObject **stack,
+ Py_ssize_t nargs);
+
+ /* Convert keyword arguments from the (stack, kwnames) format to a Python
+ dictionary.
+
+ kwnames must only contains str strings, no subclass, and all keys must
+ be unique. kwnames is not checked, usually these checks are done before or later
+ calling _PyStack_AsDict(). For example, _PyArg_ParseStack() raises an
+ error if a key is not a string. */
+ PyAPI_FUNC(PyObject *) _PyStack_AsDict(
+ PyObject **values,
+ PyObject *kwnames);
+
+ /* Convert (args, nargs, kwargs) into a (stack, nargs, kwnames).
+
+ Return a new stack which should be released by PyMem_Free(), or return
+ args unchanged if kwargs is NULL or an empty dictionary.
+
+ The stack uses borrowed references.
+
+ The type of keyword keys is not checked, these checks should be done
+ later (ex: _PyArg_ParseStack). */
+ PyAPI_FUNC(PyObject **) _PyStack_UnpackDict(
+ PyObject **args,
+ Py_ssize_t nargs,
+ PyObject *kwargs,
+ PyObject **kwnames,
+ PyObject *func);
+
+ /* Call the callable object func with the "fast call" calling convention:
+ args is a C array for positional arguments (nargs is the number of
+ positional arguments), kwargs is a dictionary for keyword arguments.
+
+ If nargs is equal to zero, args can be NULL. kwargs can be NULL.
+ nargs must be greater or equal to zero.
+
+ Return the result on success. Raise an exception on return NULL on
+ error. */
+ PyAPI_FUNC(PyObject *) _PyObject_FastCallDict(PyObject *func,
+ PyObject **args, Py_ssize_t nargs,
+ PyObject *kwargs);
+
+ /* Call the callable object func with the "fast call" calling convention:
+ args is a C array for positional arguments followed by values of
+ keyword arguments. Keys of keyword arguments are stored as a tuple
+ of strings in kwnames. nargs is the number of positional parameters at
+ the beginning of stack. The size of kwnames gives the number of keyword
+ values in the stack after positional arguments.
+
+ kwnames must only contains str strings, no subclass, and all keys must
+ be unique.
+
+ If nargs is equal to zero and there is no keyword argument (kwnames is
+ NULL or its size is zero), args can be NULL.
+
+ Return the result on success. Raise an exception and return NULL on
+ error. */
+ PyAPI_FUNC(PyObject *) _PyObject_FastCallKeywords
+ (PyObject *func,
+ PyObject **args,
+ Py_ssize_t nargs,
+ PyObject *kwnames);
+
+#define _PyObject_FastCall(func, args, nargs) \
+ _PyObject_FastCallDict((func), (args), (nargs), NULL)
+
+#define _PyObject_CallNoArg(func) \
+ _PyObject_FastCall((func), NULL, 0)
+
+#define _PyObject_CallArg1(func, arg) \
+ _PyObject_FastCall((func), &(arg), 1)
+
+ PyAPI_FUNC(PyObject *) _PyObject_Call_Prepend(PyObject *func,
+ PyObject *obj, PyObject *args,
+ PyObject *kwargs);
+
PyAPI_FUNC(PyObject *) _Py_CheckFunctionResult(PyObject *func,
PyObject *result,
const char *where);
-#endif
+#endif /* Py_LIMITED_API */
PyAPI_FUNC(PyObject *) PyObject_CallObject(PyObject *callable_object,
PyObject *args);
@@ -315,6 +395,7 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
Python expression: o.method(args).
*/
+#ifndef Py_LIMITED_API
PyAPI_FUNC(PyObject *) _PyObject_CallMethodId(PyObject *o,
_Py_Identifier *method,
const char *format, ...);
@@ -323,6 +404,7 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
Like PyObject_CallMethod, but expect a _Py_Identifier* as the
method name.
*/
+#endif /* !Py_LIMITED_API */
PyAPI_FUNC(PyObject *) _PyObject_CallFunction_SizeT(PyObject *callable,
const char *format,
@@ -331,10 +413,12 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
const char *name,
const char *format,
...);
+#ifndef Py_LIMITED_API
PyAPI_FUNC(PyObject *) _PyObject_CallMethodId_SizeT(PyObject *o,
_Py_Identifier *name,
const char *format,
...);
+#endif /* !Py_LIMITED_API */
PyAPI_FUNC(PyObject *) PyObject_CallFunctionObjArgs(PyObject *callable,
...);
@@ -350,9 +434,11 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
PyAPI_FUNC(PyObject *) PyObject_CallMethodObjArgs(PyObject *o,
PyObject *method, ...);
+#ifndef Py_LIMITED_API
PyAPI_FUNC(PyObject *) _PyObject_CallMethodIdObjArgs(PyObject *o,
struct _Py_Identifier *method,
...);
+#endif /* !Py_LIMITED_API */
/*
Call the method named m of object o with a variable number of
@@ -664,11 +750,13 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
o1*o2.
*/
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
PyAPI_FUNC(PyObject *) PyNumber_MatrixMultiply(PyObject *o1, PyObject *o2);
/*
This is the equivalent of the Python expression: o1 @ o2.
*/
+#endif
PyAPI_FUNC(PyObject *) PyNumber_FloorDivide(PyObject *o1, PyObject *o2);
@@ -844,11 +932,13 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
o1 *= o2.
*/
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
PyAPI_FUNC(PyObject *) PyNumber_InPlaceMatrixMultiply(PyObject *o1, PyObject *o2);
/*
This is the equivalent of the Python expression: o1 @= o2.
*/
+#endif
PyAPI_FUNC(PyObject *) PyNumber_InPlaceFloorDivide(PyObject *o1,
PyObject *o2);
@@ -1270,13 +1360,13 @@ PyAPI_FUNC(int) _PyObject_RealIsSubclass(PyObject *derived, PyObject *cls);
PyAPI_FUNC(char *const *) _PySequence_BytesToCharpArray(PyObject* self);
PyAPI_FUNC(void) _Py_FreeCharPArray(char *const array[]);
-#endif
/* For internal use by buffer API functions */
PyAPI_FUNC(void) _Py_add_one_to_index_F(int nd, Py_ssize_t *index,
const Py_ssize_t *shape);
PyAPI_FUNC(void) _Py_add_one_to_index_C(int nd, Py_ssize_t *index,
const Py_ssize_t *shape);
+#endif /* !Py_LIMITED_API */
#ifdef __cplusplus
diff --git a/Include/asdl.h b/Include/asdl.h
index 495153c..35e9fa1 100644
--- a/Include/asdl.h
+++ b/Include/asdl.h
@@ -6,6 +6,7 @@ typedef PyObject * string;
typedef PyObject * bytes;
typedef PyObject * object;
typedef PyObject * singleton;
+typedef PyObject * constant;
/* It would be nice if the code generated by asdl_c.py was completely
independent of Python, but it is a goal the requires too much work
diff --git a/Include/bytes_methods.h b/Include/bytes_methods.h
index 11d5f42..7fa7540 100644
--- a/Include/bytes_methods.h
+++ b/Include/bytes_methods.h
@@ -17,9 +17,18 @@ extern PyObject* _Py_bytes_istitle(const char *cptr, Py_ssize_t len);
/* These store their len sized answer in the given preallocated *result arg. */
extern void _Py_bytes_lower(char *result, const char *cptr, Py_ssize_t len);
extern void _Py_bytes_upper(char *result, const char *cptr, Py_ssize_t len);
-extern void _Py_bytes_title(char *result, char *s, Py_ssize_t len);
-extern void _Py_bytes_capitalize(char *result, char *s, Py_ssize_t len);
-extern void _Py_bytes_swapcase(char *result, char *s, Py_ssize_t len);
+extern void _Py_bytes_title(char *result, const char *s, Py_ssize_t len);
+extern void _Py_bytes_capitalize(char *result, const char *s, Py_ssize_t len);
+extern void _Py_bytes_swapcase(char *result, const char *s, Py_ssize_t len);
+
+extern PyObject *_Py_bytes_find(const char *str, Py_ssize_t len, PyObject *args);
+extern PyObject *_Py_bytes_index(const char *str, Py_ssize_t len, PyObject *args);
+extern PyObject *_Py_bytes_rfind(const char *str, Py_ssize_t len, PyObject *args);
+extern PyObject *_Py_bytes_rindex(const char *str, Py_ssize_t len, PyObject *args);
+extern PyObject *_Py_bytes_count(const char *str, Py_ssize_t len, PyObject *args);
+extern int _Py_bytes_contains(const char *str, Py_ssize_t len, PyObject *arg);
+extern PyObject *_Py_bytes_startswith(const char *str, Py_ssize_t len, PyObject *args);
+extern PyObject *_Py_bytes_endswith(const char *str, Py_ssize_t len, PyObject *args);
/* The maketrans() static method. */
extern PyObject* _Py_bytes_maketrans(Py_buffer *frm, Py_buffer *to);
@@ -37,7 +46,19 @@ extern const char _Py_upper__doc__[];
extern const char _Py_title__doc__[];
extern const char _Py_capitalize__doc__[];
extern const char _Py_swapcase__doc__[];
+extern const char _Py_count__doc__[];
+extern const char _Py_find__doc__[];
+extern const char _Py_index__doc__[];
+extern const char _Py_rfind__doc__[];
+extern const char _Py_rindex__doc__[];
+extern const char _Py_startswith__doc__[];
+extern const char _Py_endswith__doc__[];
extern const char _Py_maketrans__doc__[];
+extern const char _Py_expandtabs__doc__[];
+extern const char _Py_ljust__doc__[];
+extern const char _Py_rjust__doc__[];
+extern const char _Py_center__doc__[];
+extern const char _Py_zfill__doc__[];
/* this is needed because some docs are shared from the .o, not static */
#define PyDoc_STRVAR_shared(name,str) const char name[] = PyDoc_STR(str)
diff --git a/Include/bytesobject.h b/Include/bytesobject.h
index 6c1e0c3..0f0bf9f 100644
--- a/Include/bytesobject.h
+++ b/Include/bytesobject.h
@@ -62,11 +62,25 @@ PyAPI_FUNC(void) PyBytes_Concat(PyObject **, PyObject *);
PyAPI_FUNC(void) PyBytes_ConcatAndDel(PyObject **, PyObject *);
#ifndef Py_LIMITED_API
PyAPI_FUNC(int) _PyBytes_Resize(PyObject **, Py_ssize_t);
-PyAPI_FUNC(PyObject *) _PyBytes_Format(PyObject *, PyObject *);
+PyAPI_FUNC(PyObject*) _PyBytes_FormatEx(
+ const char *format,
+ Py_ssize_t format_len,
+ PyObject *args,
+ int use_bytearray);
+PyAPI_FUNC(PyObject*) _PyBytes_FromHex(
+ PyObject *string,
+ int use_bytearray);
#endif
PyAPI_FUNC(PyObject *) PyBytes_DecodeEscape(const char *, Py_ssize_t,
const char *, Py_ssize_t,
const char *);
+#ifndef Py_LIMITED_API
+/* Helper for PyBytes_DecodeEscape that detects invalid escape chars. */
+PyAPI_FUNC(PyObject *) _PyBytes_DecodeEscape(const char *, Py_ssize_t,
+ const char *, Py_ssize_t,
+ const char *,
+ const char **);
+#endif
/* Macro, trading safety for speed */
#ifndef Py_LIMITED_API
@@ -123,6 +137,87 @@ PyAPI_FUNC(Py_ssize_t) _PyBytes_InsertThousandsGrouping(char *buffer,
#define F_ALT (1<<3)
#define F_ZERO (1<<4)
+#ifndef Py_LIMITED_API
+/* The _PyBytesWriter structure is big: it contains an embedded "stack buffer".
+ A _PyBytesWriter variable must be declared at the end of variables in a
+ function to optimize the memory allocation on the stack. */
+typedef struct {
+ /* bytes, bytearray or NULL (when the small buffer is used) */
+ PyObject *buffer;
+
+ /* Number of allocated size. */
+ Py_ssize_t allocated;
+
+ /* Minimum number of allocated bytes,
+ incremented by _PyBytesWriter_Prepare() */
+ Py_ssize_t min_size;
+
+ /* If non-zero, use a bytearray instead of a bytes object for buffer. */
+ int use_bytearray;
+
+ /* If non-zero, overallocate the buffer (default: 0).
+ This flag must be zero if use_bytearray is non-zero. */
+ int overallocate;
+
+ /* Stack buffer */
+ int use_small_buffer;
+ char small_buffer[512];
+} _PyBytesWriter;
+
+/* Initialize a bytes writer
+
+ By default, the overallocation is disabled. Set the overallocate attribute
+ to control the allocation of the buffer. */
+PyAPI_FUNC(void) _PyBytesWriter_Init(_PyBytesWriter *writer);
+
+/* Get the buffer content and reset the writer.
+ Return a bytes object, or a bytearray object if use_bytearray is non-zero.
+ Raise an exception and return NULL on error. */
+PyAPI_FUNC(PyObject *) _PyBytesWriter_Finish(_PyBytesWriter *writer,
+ void *str);
+
+/* Deallocate memory of a writer (clear its internal buffer). */
+PyAPI_FUNC(void) _PyBytesWriter_Dealloc(_PyBytesWriter *writer);
+
+/* Allocate the buffer to write size bytes.
+ Return the pointer to the beginning of buffer data.
+ Raise an exception and return NULL on error. */
+PyAPI_FUNC(void*) _PyBytesWriter_Alloc(_PyBytesWriter *writer,
+ Py_ssize_t size);
+
+/* Ensure that the buffer is large enough to write *size* bytes.
+ Add size to the writer minimum size (min_size attribute).
+
+ str is the current pointer inside the buffer.
+ Return the updated current pointer inside the buffer.
+ Raise an exception and return NULL on error. */
+PyAPI_FUNC(void*) _PyBytesWriter_Prepare(_PyBytesWriter *writer,
+ void *str,
+ Py_ssize_t size);
+
+/* Resize the buffer to make it larger.
+ The new buffer may be larger than size bytes because of overallocation.
+ Return the updated current pointer inside the buffer.
+ Raise an exception and return NULL on error.
+
+ Note: size must be greater than the number of allocated bytes in the writer.
+
+ This function doesn't use the writer minimum size (min_size attribute).
+
+ See also _PyBytesWriter_Prepare().
+ */
+PyAPI_FUNC(void*) _PyBytesWriter_Resize(_PyBytesWriter *writer,
+ void *str,
+ Py_ssize_t size);
+
+/* Write bytes.
+ Raise an exception and return NULL on error. */
+PyAPI_FUNC(void*) _PyBytesWriter_WriteBytes(_PyBytesWriter *writer,
+ void *str,
+ const void *bytes,
+ Py_ssize_t size);
+#endif /* Py_LIMITED_API */
+
#ifdef __cplusplus
}
#endif
diff --git a/Include/ceval.h b/Include/ceval.h
index b5373a9..89c6062 100644
--- a/Include/ceval.h
+++ b/Include/ceval.h
@@ -8,7 +8,7 @@ extern "C" {
/* Interface to random parts in ceval.c */
PyAPI_FUNC(PyObject *) PyEval_CallObjectWithKeywords(
- PyObject *, PyObject *, PyObject *);
+ PyObject *func, PyObject *args, PyObject *kwargs);
/* Inline this */
#define PyEval_CallObject(func,arg) \
@@ -25,6 +25,10 @@ PyAPI_FUNC(void) PyEval_SetProfile(Py_tracefunc, PyObject *);
PyAPI_FUNC(void) PyEval_SetTrace(Py_tracefunc, PyObject *);
PyAPI_FUNC(void) _PyEval_SetCoroutineWrapper(PyObject *);
PyAPI_FUNC(PyObject *) _PyEval_GetCoroutineWrapper(void);
+PyAPI_FUNC(void) _PyEval_SetAsyncGenFirstiter(PyObject *);
+PyAPI_FUNC(PyObject *) _PyEval_GetAsyncGenFirstiter(void);
+PyAPI_FUNC(void) _PyEval_SetAsyncGenFinalizer(PyObject *);
+PyAPI_FUNC(PyObject *) _PyEval_GetAsyncGenFinalizer(void);
#endif
struct _frame; /* Avoid including frameobject.h */
@@ -119,6 +123,9 @@ PyAPI_FUNC(const char *) PyEval_GetFuncDesc(PyObject *);
PyAPI_FUNC(PyObject *) PyEval_GetCallStats(PyObject *);
PyAPI_FUNC(PyObject *) PyEval_EvalFrame(struct _frame *);
PyAPI_FUNC(PyObject *) PyEval_EvalFrameEx(struct _frame *f, int exc);
+#ifndef Py_LIMITED_API
+PyAPI_FUNC(PyObject *) _PyEval_EvalFrameDefault(struct _frame *f, int exc);
+#endif
/* Interface for threads.
@@ -172,7 +179,9 @@ PyAPI_FUNC(void) PyEval_RestoreThread(PyThreadState *);
PyAPI_FUNC(int) PyEval_ThreadsInitialized(void);
PyAPI_FUNC(void) PyEval_InitThreads(void);
+#ifndef Py_LIMITED_API
PyAPI_FUNC(void) _PyEval_FiniThreads(void);
+#endif /* !Py_LIMITED_API */
PyAPI_FUNC(void) PyEval_AcquireLock(void);
PyAPI_FUNC(void) PyEval_ReleaseLock(void);
PyAPI_FUNC(void) PyEval_AcquireThread(PyThreadState *tstate);
@@ -184,6 +193,10 @@ PyAPI_FUNC(void) _PyEval_SetSwitchInterval(unsigned long microseconds);
PyAPI_FUNC(unsigned long) _PyEval_GetSwitchInterval(void);
#endif
+#ifndef Py_LIMITED_API
+PyAPI_FUNC(Py_ssize_t) _PyEval_RequestCodeExtraIndex(freefunc);
+#endif
+
#define Py_BEGIN_ALLOW_THREADS { \
PyThreadState *_save; \
_save = PyEval_SaveThread();
@@ -206,6 +219,14 @@ PyAPI_FUNC(int) _PyEval_SliceIndex(PyObject *, Py_ssize_t *);
PyAPI_FUNC(void) _PyEval_SignalAsyncExc(void);
#endif
+/* Masks and values used by FORMAT_VALUE opcode. */
+#define FVC_MASK 0x3
+#define FVC_NONE 0x0
+#define FVC_STR 0x1
+#define FVC_REPR 0x2
+#define FVC_ASCII 0x3
+#define FVS_MASK 0x4
+#define FVS_HAVE_SPEC 0x4
#ifdef __cplusplus
}
diff --git a/Include/code.h b/Include/code.h
index 8ecf38a..c5fce3c 100644
--- a/Include/code.h
+++ b/Include/code.h
@@ -7,6 +7,16 @@
extern "C" {
#endif
+typedef uint16_t _Py_CODEUNIT;
+
+#ifdef WORDS_BIGENDIAN
+# define _Py_OPCODE(word) ((word) >> 8)
+# define _Py_OPARG(word) ((word) & 255)
+#else
+# define _Py_OPCODE(word) ((word) & 255)
+# define _Py_OPARG(word) ((word) >> 8)
+#endif
+
/* Bytecode object */
typedef struct {
PyObject_HEAD
@@ -15,26 +25,29 @@ typedef struct {
int co_nlocals; /* #local variables */
int co_stacksize; /* #entries needed for evaluation stack */
int co_flags; /* CO_..., see below */
+ int co_firstlineno; /* first source line number */
PyObject *co_code; /* instruction opcodes */
PyObject *co_consts; /* list (constants used) */
PyObject *co_names; /* list of strings (names used) */
PyObject *co_varnames; /* tuple of strings (local variable names) */
PyObject *co_freevars; /* tuple of strings (free variable names) */
PyObject *co_cellvars; /* tuple of strings (cell variable names) */
- /* The rest aren't used in either hash or comparisons, except for
- co_name (used in both) and co_firstlineno (used only in
- comparisons). This is done to preserve the name and line number
+ /* The rest aren't used in either hash or comparisons, except for co_name,
+ used in both. This is done to preserve the name and line number
for tracebacks and debuggers; otherwise, constant de-duplication
would collapse identical functions/lambdas defined on different lines.
*/
unsigned char *co_cell2arg; /* Maps cell vars which are arguments. */
PyObject *co_filename; /* unicode (where it was loaded from) */
PyObject *co_name; /* unicode (name, for reference) */
- int co_firstlineno; /* first source line number */
PyObject *co_lnotab; /* string (encoding addr<->lineno mapping) See
Objects/lnotab_notes.txt for details. */
void *co_zombieframe; /* for optimization only (see frameobject.c) */
PyObject *co_weakreflist; /* to support weakrefs to code objects */
+ /* Scratch space for extra data relating to the code object.
+ Type is a void* to keep the format private in codeobject.c to force
+ people to go through the proper APIs. */
+ void *co_extra;
} PyCodeObject;
/* Masks for co_flags above */
@@ -55,6 +68,7 @@ typedef struct {
``async def`` keywords) */
#define CO_COROUTINE 0x0080
#define CO_ITERABLE_COROUTINE 0x0100
+#define CO_ASYNC_GENERATOR 0x0200
/* These are no longer used. */
#if 0
@@ -126,7 +140,15 @@ PyAPI_FUNC(PyObject*) _PyCode_ConstantKey(PyObject *obj);
#endif
PyAPI_FUNC(PyObject*) PyCode_Optimize(PyObject *code, PyObject* consts,
- PyObject *names, PyObject *lineno_obj);
+ PyObject *names, PyObject *lnotab);
+
+
+#ifndef Py_LIMITED_API
+PyAPI_FUNC(int) _PyCode_GetExtra(PyObject *code, Py_ssize_t index,
+ void **extra);
+PyAPI_FUNC(int) _PyCode_SetExtra(PyObject *code, Py_ssize_t index,
+ void *extra);
+#endif
#ifdef __cplusplus
}
diff --git a/Include/codecs.h b/Include/codecs.h
index f8275a1..3ad0f2b 100644
--- a/Include/codecs.h
+++ b/Include/codecs.h
@@ -225,10 +225,14 @@ PyAPI_FUNC(PyObject *) PyCodec_XMLCharRefReplaceErrors(PyObject *exc);
/* replace the unicode encode error with backslash escapes (\x, \u and \U) */
PyAPI_FUNC(PyObject *) PyCodec_BackslashReplaceErrors(PyObject *exc);
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
/* replace the unicode encode error with backslash escapes (\N, \x, \u and \U) */
PyAPI_FUNC(PyObject *) PyCodec_NameReplaceErrors(PyObject *exc);
+#endif
+#ifndef Py_LIMITED_API
PyAPI_DATA(const char *) Py_hexdigits;
+#endif
#ifdef __cplusplus
}
diff --git a/Include/datetime.h b/Include/datetime.h
index 06cbc4a..3bf35cb 100644
--- a/Include/datetime.h
+++ b/Include/datetime.h
@@ -81,6 +81,7 @@ typedef struct
typedef struct
{
_PyDateTime_TIMEHEAD
+ unsigned char fold;
PyObject *tzinfo;
} PyDateTime_Time; /* hastzinfo true */
@@ -108,6 +109,7 @@ typedef struct
typedef struct
{
_PyDateTime_DATETIMEHEAD
+ unsigned char fold;
PyObject *tzinfo;
} PyDateTime_DateTime; /* hastzinfo true */
@@ -125,6 +127,7 @@ typedef struct
((((PyDateTime_DateTime*)o)->data[7] << 16) | \
(((PyDateTime_DateTime*)o)->data[8] << 8) | \
((PyDateTime_DateTime*)o)->data[9])
+#define PyDateTime_DATE_GET_FOLD(o) (((PyDateTime_DateTime*)o)->fold)
/* Apply for time instances. */
#define PyDateTime_TIME_GET_HOUR(o) (((PyDateTime_Time*)o)->data[0])
@@ -134,6 +137,7 @@ typedef struct
((((PyDateTime_Time*)o)->data[3] << 16) | \
(((PyDateTime_Time*)o)->data[4] << 8) | \
((PyDateTime_Time*)o)->data[5])
+#define PyDateTime_TIME_GET_FOLD(o) (((PyDateTime_Time*)o)->fold)
/* Apply for time delta instances */
#define PyDateTime_DELTA_GET_DAYS(o) (((PyDateTime_Delta*)o)->days)
@@ -162,6 +166,11 @@ typedef struct {
PyObject *(*DateTime_FromTimestamp)(PyObject*, PyObject*, PyObject*);
PyObject *(*Date_FromTimestamp)(PyObject*, PyObject*);
+ /* PEP 495 constructors */
+ PyObject *(*DateTime_FromDateAndTimeAndFold)(int, int, int, int, int, int, int,
+ PyObject*, int, PyTypeObject*);
+ PyObject *(*Time_FromTimeAndFold)(int, int, int, int, PyObject*, int, PyTypeObject*);
+
} PyDateTime_CAPI;
#define PyDateTime_CAPSULE_NAME "datetime.datetime_CAPI"
@@ -217,10 +226,18 @@ static PyDateTime_CAPI *PyDateTimeAPI = NULL;
PyDateTimeAPI->DateTime_FromDateAndTime(year, month, day, hour, \
min, sec, usec, Py_None, PyDateTimeAPI->DateTimeType)
+#define PyDateTime_FromDateAndTimeAndFold(year, month, day, hour, min, sec, usec, fold) \
+ PyDateTimeAPI->DateTime_FromDateAndTimeAndFold(year, month, day, hour, \
+ min, sec, usec, Py_None, fold, PyDateTimeAPI->DateTimeType)
+
#define PyTime_FromTime(hour, minute, second, usecond) \
PyDateTimeAPI->Time_FromTime(hour, minute, second, usecond, \
Py_None, PyDateTimeAPI->TimeType)
+#define PyTime_FromTimeAndFold(hour, minute, second, usecond, fold) \
+ PyDateTimeAPI->Time_FromTimeAndFold(hour, minute, second, usecond, \
+ Py_None, fold, PyDateTimeAPI->TimeType)
+
#define PyDelta_FromDSU(days, seconds, useconds) \
PyDateTimeAPI->Delta_FromDelta(days, seconds, useconds, 1, \
PyDateTimeAPI->DeltaType)
diff --git a/Include/descrobject.h b/Include/descrobject.h
index e2ba97f..8f3e84c 100644
--- a/Include/descrobject.h
+++ b/Include/descrobject.h
@@ -78,7 +78,9 @@ PyAPI_DATA(PyTypeObject) PyMemberDescr_Type;
PyAPI_DATA(PyTypeObject) PyMethodDescr_Type;
PyAPI_DATA(PyTypeObject) PyWrapperDescr_Type;
PyAPI_DATA(PyTypeObject) PyDictProxy_Type;
+#ifndef Py_LIMITED_API
PyAPI_DATA(PyTypeObject) _PyMethodWrapper_Type;
+#endif /* Py_LIMITED_API */
PyAPI_FUNC(PyObject *) PyDescr_NewMethod(PyTypeObject *, PyMethodDef *);
PyAPI_FUNC(PyObject *) PyDescr_NewClassMethod(PyTypeObject *, PyMethodDef *);
diff --git a/Include/dictobject.h b/Include/dictobject.h
index 17e12c0..c4f2e2f 100644
--- a/Include/dictobject.h
+++ b/Include/dictobject.h
@@ -22,8 +22,21 @@ typedef struct _dictkeysobject PyDictKeysObject;
*/
typedef struct {
PyObject_HEAD
+
+ /* Number of items in the dictionary */
Py_ssize_t ma_used;
+
+ /* Dictionary version: globally unique, value change each time
+ the dictionary is modified */
+ uint64_t ma_version_tag;
+
PyDictKeysObject *ma_keys;
+
+ /* If ma_values is NULL, the table is "combined": keys and values
+ are stored in ma_keys.
+
+ If ma_values is not NULL, the table is splitted:
+ keys are stored in ma_keys and values are stored in ma_values */
PyObject **ma_values;
} PyDictObject;
@@ -60,9 +73,9 @@ PyAPI_FUNC(PyObject *) _PyDict_GetItem_KnownHash(PyObject *mp, PyObject *key,
Py_hash_t hash);
#endif
PyAPI_FUNC(PyObject *) PyDict_GetItemWithError(PyObject *mp, PyObject *key);
+#ifndef Py_LIMITED_API
PyAPI_FUNC(PyObject *) _PyDict_GetItemIdWithError(PyObject *dp,
struct _Py_Identifier *key);
-#ifndef Py_LIMITED_API
PyAPI_FUNC(PyObject *) PyDict_SetDefault(
PyObject *mp, PyObject *key, PyObject *defaultobj);
#endif
@@ -101,8 +114,8 @@ PyAPI_FUNC(void) _PyDict_MaybeUntrack(PyObject *mp);
PyAPI_FUNC(int) _PyDict_HasOnlyStringKeys(PyObject *mp);
Py_ssize_t _PyDict_KeysSize(PyDictKeysObject *keys);
Py_ssize_t _PyDict_SizeOf(PyDictObject *);
-PyObject *_PyDict_Pop(PyDictObject *, PyObject *, PyObject *);
-PyObject *_PyDict_Pop_KnownHash(PyDictObject *, PyObject *, Py_hash_t, PyObject *);
+PyAPI_FUNC(PyObject *) _PyDict_Pop(PyObject *, PyObject *, PyObject *);
+PyObject *_PyDict_Pop_KnownHash(PyObject *, PyObject *, Py_hash_t, PyObject *);
PyObject *_PyDict_FromKeys(PyObject *, PyObject *, PyObject *);
#define _PyDict_HasSplitTable(d) ((d)->ma_values != NULL)
@@ -122,6 +135,12 @@ PyAPI_FUNC(int) PyDict_Merge(PyObject *mp,
int override);
#ifndef Py_LIMITED_API
+/* Like PyDict_Merge, but override can be 0, 1 or 2. If override is 0,
+ the first occurrence of a key wins, if override is 1, the last occurrence
+ of a key wins, if override is 2, a KeyError with conflicting key as
+ argument is raised.
+*/
+PyAPI_FUNC(int) _PyDict_MergeEx(PyObject *mp, PyObject *other, int override);
PyAPI_FUNC(PyObject *) _PyDictView_Intersect(PyObject* self, PyObject *other);
#endif
@@ -135,9 +154,13 @@ PyAPI_FUNC(int) PyDict_MergeFromSeq2(PyObject *d,
int override);
PyAPI_FUNC(PyObject *) PyDict_GetItemString(PyObject *dp, const char *key);
+#ifndef Py_LIMITED_API
PyAPI_FUNC(PyObject *) _PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key);
+#endif /* !Py_LIMITED_API */
PyAPI_FUNC(int) PyDict_SetItemString(PyObject *dp, const char *key, PyObject *item);
+#ifndef Py_LIMITED_API
PyAPI_FUNC(int) _PyDict_SetItemId(PyObject *dp, struct _Py_Identifier *key, PyObject *item);
+#endif /* !Py_LIMITED_API */
PyAPI_FUNC(int) PyDict_DelItemString(PyObject *dp, const char *key);
#ifndef Py_LIMITED_API
diff --git a/Include/fileobject.h b/Include/fileobject.h
index 03155d3..6120e51 100644
--- a/Include/fileobject.h
+++ b/Include/fileobject.h
@@ -23,6 +23,9 @@ PyAPI_FUNC(char *) Py_UniversalNewlineFgets(char *, int, FILE*, PyObject *);
If non-NULL, this is different than the default encoding for strings
*/
PyAPI_DATA(const char *) Py_FileSystemDefaultEncoding;
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03060000
+PyAPI_DATA(const char *) Py_FileSystemDefaultEncodeErrors;
+#endif
PyAPI_DATA(int) Py_HasFileSystemDefaultEncoding;
/* Internal API
diff --git a/Include/fileutils.h b/Include/fileutils.h
index b4a683c..b933e98 100644
--- a/Include/fileutils.h
+++ b/Include/fileutils.h
@@ -5,8 +5,7 @@
extern "C" {
#endif
-PyAPI_FUNC(PyObject *) _Py_device_encoding(int);
-
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
PyAPI_FUNC(wchar_t *) Py_DecodeLocale(
const char *arg,
size_t *size);
@@ -14,9 +13,12 @@ PyAPI_FUNC(wchar_t *) Py_DecodeLocale(
PyAPI_FUNC(char*) Py_EncodeLocale(
const wchar_t *text,
size_t *error_pos);
+#endif
#ifndef Py_LIMITED_API
+PyAPI_FUNC(PyObject *) _Py_device_encoding(int);
+
#ifdef MS_WINDOWS
struct _Py_stat_struct {
unsigned long st_dev;
@@ -46,13 +48,11 @@ PyAPI_FUNC(int) _Py_fstat(
PyAPI_FUNC(int) _Py_fstat_noraise(
int fd,
struct _Py_stat_struct *status);
-#endif /* Py_LIMITED_API */
PyAPI_FUNC(int) _Py_stat(
PyObject *path,
struct stat *status);
-#ifndef Py_LIMITED_API
PyAPI_FUNC(int) _Py_open(
const char *pathname,
int flags);
@@ -60,7 +60,6 @@ PyAPI_FUNC(int) _Py_open(
PyAPI_FUNC(int) _Py_open_noraise(
const char *pathname,
int flags);
-#endif
PyAPI_FUNC(FILE *) _Py_wfopen(
const wchar_t *path,
@@ -107,7 +106,6 @@ PyAPI_FUNC(wchar_t*) _Py_wgetcwd(
wchar_t *buf,
size_t size);
-#ifndef Py_LIMITED_API
PyAPI_FUNC(int) _Py_get_inheritable(int fd);
PyAPI_FUNC(int) _Py_set_inheritable(int fd, int inheritable,
@@ -121,18 +119,6 @@ PyAPI_FUNC(int) _Py_get_blocking(int fd);
PyAPI_FUNC(int) _Py_set_blocking(int fd, int blocking);
#endif /* !MS_WINDOWS */
-#if defined _MSC_VER && _MSC_VER >= 1400 && _MSC_VER < 1900
-/* A routine to check if a file descriptor is valid on Windows. Returns 0
- * and sets errno to EBADF if it isn't. This is to avoid Assertions
- * from various functions in the Windows CRT beginning with
- * Visual Studio 2005
- */
-int _PyVerify_fd(int fd);
-
-#else
-#define _PyVerify_fd(A) (1) /* dummy */
-#endif
-
#endif /* Py_LIMITED_API */
#ifdef __cplusplus
diff --git a/Include/floatobject.h b/Include/floatobject.h
index e240fdb..f1044d6 100644
--- a/Include/floatobject.h
+++ b/Include/floatobject.h
@@ -74,9 +74,9 @@ PyAPI_FUNC(double) PyFloat_AsDouble(PyObject *);
* happens in such cases is partly accidental (alas).
*/
-/* The pack routines write 4 or 8 bytes, starting at p. le is a bool
+/* The pack routines write 2, 4 or 8 bytes, starting at p. le is a bool
* argument, true if you want the string in little-endian format (exponent
- * last, at p+3 or p+7), false if you want big-endian format (exponent
+ * last, at p+1, p+3 or p+7), false if you want big-endian format (exponent
* first, at p).
* Return value: 0 if all is OK, -1 if error (and an exception is
* set, most likely OverflowError).
@@ -84,6 +84,7 @@ PyAPI_FUNC(double) PyFloat_AsDouble(PyObject *);
* 1): What this does is undefined if x is a NaN or infinity.
* 2): -0.0 and +0.0 produce the same string.
*/
+PyAPI_FUNC(int) _PyFloat_Pack2(double x, unsigned char *p, int le);
PyAPI_FUNC(int) _PyFloat_Pack4(double x, unsigned char *p, int le);
PyAPI_FUNC(int) _PyFloat_Pack8(double x, unsigned char *p, int le);
@@ -96,14 +97,15 @@ PyAPI_FUNC(int) _PyFloat_Repr(double x, char *p, size_t len);
PyAPI_FUNC(int) _PyFloat_Digits(char *buf, double v, int *signum);
PyAPI_FUNC(void) _PyFloat_DigitsInit(void);
-/* The unpack routines read 4 or 8 bytes, starting at p. le is a bool
+/* The unpack routines read 2, 4 or 8 bytes, starting at p. le is a bool
* argument, true if the string is in little-endian format (exponent
- * last, at p+3 or p+7), false if big-endian (exponent first, at p).
+ * last, at p+1, p+3 or p+7), false if big-endian (exponent first, at p).
* Return value: The unpacked double. On error, this is -1.0 and
* PyErr_Occurred() is true (and an exception is set, most likely
* OverflowError). Note that on a non-IEEE platform this will refuse
* to unpack a string that represents a NaN or infinity.
*/
+PyAPI_FUNC(double) _PyFloat_Unpack2(const unsigned char *p, int le);
PyAPI_FUNC(double) _PyFloat_Unpack4(const unsigned char *p, int le);
PyAPI_FUNC(double) _PyFloat_Unpack8(const unsigned char *p, int le);
diff --git a/Include/funcobject.h b/Include/funcobject.h
index cc1426c..77bb8c3 100644
--- a/Include/funcobject.h
+++ b/Include/funcobject.h
@@ -58,6 +58,20 @@ PyAPI_FUNC(int) PyFunction_SetClosure(PyObject *, PyObject *);
PyAPI_FUNC(PyObject *) PyFunction_GetAnnotations(PyObject *);
PyAPI_FUNC(int) PyFunction_SetAnnotations(PyObject *, PyObject *);
+#ifndef Py_LIMITED_API
+PyAPI_FUNC(PyObject *) _PyFunction_FastCallDict(
+ PyObject *func,
+ PyObject **args,
+ Py_ssize_t nargs,
+ PyObject *kwargs);
+
+PyAPI_FUNC(PyObject *) _PyFunction_FastCallKeywords(
+ PyObject *func,
+ PyObject **stack,
+ Py_ssize_t nargs,
+ PyObject *kwnames);
+#endif
+
/* Macros for direct access to these values. Type checks are *not*
done, so use with care. */
#define PyFunction_GET_CODE(func) \
diff --git a/Include/genobject.h b/Include/genobject.h
index 61e708a..8c1825f 100644
--- a/Include/genobject.h
+++ b/Include/genobject.h
@@ -43,7 +43,7 @@ PyAPI_FUNC(PyObject *) PyGen_NewWithQualName(struct _frame *,
PyAPI_FUNC(int) PyGen_NeedsFinalizing(PyGenObject *);
PyAPI_FUNC(int) _PyGen_SetStopIterationValue(PyObject *);
PyAPI_FUNC(int) _PyGen_FetchStopIterationValue(PyObject **);
-PyObject *_PyGen_Send(PyGenObject *, PyObject *);
+PyAPI_FUNC(PyObject *) _PyGen_Send(PyGenObject *, PyObject *);
PyObject *_PyGen_yf(PyGenObject *);
PyAPI_FUNC(void) _PyGen_Finalize(PyObject *self);
@@ -62,6 +62,37 @@ PyObject *_PyAIterWrapper_New(PyObject *aiter);
PyObject *_PyCoro_GetAwaitableIter(PyObject *o);
PyAPI_FUNC(PyObject *) PyCoro_New(struct _frame *,
PyObject *name, PyObject *qualname);
+
+/* Asynchronous Generators */
+
+typedef struct {
+ _PyGenObject_HEAD(ag)
+ PyObject *ag_finalizer;
+
+ /* Flag is set to 1 when hooks set up by sys.set_asyncgen_hooks
+ were called on the generator, to avoid calling them more
+ than once. */
+ int ag_hooks_inited;
+
+ /* Flag is set to 1 when aclose() is called for the first time, or
+ when a StopAsyncIteration exception is raised. */
+ int ag_closed;
+} PyAsyncGenObject;
+
+PyAPI_DATA(PyTypeObject) PyAsyncGen_Type;
+PyAPI_DATA(PyTypeObject) _PyAsyncGenASend_Type;
+PyAPI_DATA(PyTypeObject) _PyAsyncGenWrappedValue_Type;
+PyAPI_DATA(PyTypeObject) _PyAsyncGenAThrow_Type;
+
+PyAPI_FUNC(PyObject *) PyAsyncGen_New(struct _frame *,
+ PyObject *name, PyObject *qualname);
+
+#define PyAsyncGen_CheckExact(op) (Py_TYPE(op) == &PyAsyncGen_Type)
+
+PyObject *_PyAsyncGenValueWrapperNew(PyObject *);
+
+int PyAsyncGen_ClearFreeLists(void);
+
#endif
#undef _PyGenObject_HEAD
diff --git a/Include/graminit.h b/Include/graminit.h
index d030bc3..e9b4a93 100644
--- a/Include/graminit.h
+++ b/Include/graminit.h
@@ -17,71 +17,72 @@
#define simple_stmt 270
#define small_stmt 271
#define expr_stmt 272
-#define testlist_star_expr 273
-#define augassign 274
-#define del_stmt 275
-#define pass_stmt 276
-#define flow_stmt 277
-#define break_stmt 278
-#define continue_stmt 279
-#define return_stmt 280
-#define yield_stmt 281
-#define raise_stmt 282
-#define import_stmt 283
-#define import_name 284
-#define import_from 285
-#define import_as_name 286
-#define dotted_as_name 287
-#define import_as_names 288
-#define dotted_as_names 289
-#define dotted_name 290
-#define global_stmt 291
-#define nonlocal_stmt 292
-#define assert_stmt 293
-#define compound_stmt 294
-#define async_stmt 295
-#define if_stmt 296
-#define while_stmt 297
-#define for_stmt 298
-#define try_stmt 299
-#define with_stmt 300
-#define with_item 301
-#define except_clause 302
-#define suite 303
-#define test 304
-#define test_nocond 305
-#define lambdef 306
-#define lambdef_nocond 307
-#define or_test 308
-#define and_test 309
-#define not_test 310
-#define comparison 311
-#define comp_op 312
-#define star_expr 313
-#define expr 314
-#define xor_expr 315
-#define and_expr 316
-#define shift_expr 317
-#define arith_expr 318
-#define term 319
-#define factor 320
-#define power 321
-#define atom_expr 322
-#define atom 323
-#define testlist_comp 324
-#define trailer 325
-#define subscriptlist 326
-#define subscript 327
-#define sliceop 328
-#define exprlist 329
-#define testlist 330
-#define dictorsetmaker 331
-#define classdef 332
-#define arglist 333
-#define argument 334
-#define comp_iter 335
-#define comp_for 336
-#define comp_if 337
-#define encoding_decl 338
-#define yield_expr 339
-#define yield_arg 340
+#define annassign 273
+#define testlist_star_expr 274
+#define augassign 275
+#define del_stmt 276
+#define pass_stmt 277
+#define flow_stmt 278
+#define break_stmt 279
+#define continue_stmt 280
+#define return_stmt 281
+#define yield_stmt 282
+#define raise_stmt 283
+#define import_stmt 284
+#define import_name 285
+#define import_from 286
+#define import_as_name 287
+#define dotted_as_name 288
+#define import_as_names 289
+#define dotted_as_names 290
+#define dotted_name 291
+#define global_stmt 292
+#define nonlocal_stmt 293
+#define assert_stmt 294
+#define compound_stmt 295
+#define async_stmt 296
+#define if_stmt 297
+#define while_stmt 298
+#define for_stmt 299
+#define try_stmt 300
+#define with_stmt 301
+#define with_item 302
+#define except_clause 303
+#define suite 304
+#define test 305
+#define test_nocond 306
+#define lambdef 307
+#define lambdef_nocond 308
+#define or_test 309
+#define and_test 310
+#define not_test 311
+#define comparison 312
+#define comp_op 313
+#define star_expr 314
+#define expr 315
+#define xor_expr 316
+#define and_expr 317
+#define shift_expr 318
+#define arith_expr 319
+#define term 320
+#define factor 321
+#define power 322
+#define atom_expr 323
+#define atom 324
+#define testlist_comp 325
+#define trailer 326
+#define subscriptlist 327
+#define subscript 328
+#define sliceop 329
+#define exprlist 330
+#define testlist 331
+#define dictorsetmaker 332
+#define classdef 333
+#define arglist 334
+#define argument 335
+#define comp_iter 336
+#define comp_for 337
+#define comp_if 338
+#define encoding_decl 339
+#define yield_expr 340
+#define yield_arg 341
diff --git a/Include/grammar.h b/Include/grammar.h
index 85120b9..f775f96 100644
--- a/Include/grammar.h
+++ b/Include/grammar.h
@@ -69,6 +69,7 @@ typedef struct {
/* FUNCTIONS */
grammar *newgrammar(int start);
+void freegrammar(grammar *g);
dfa *adddfa(grammar *g, int type, const char *name);
int addstate(dfa *d);
void addarc(dfa *d, int from, int to, int lbl);
diff --git a/Include/import.h b/Include/import.h
index afdfac2..bb6beba6 100644
--- a/Include/import.h
+++ b/Include/import.h
@@ -7,9 +7,11 @@
extern "C" {
#endif
+#ifndef Py_LIMITED_API
PyAPI_FUNC(void) _PyImportZip_Init(void);
PyMODINIT_FUNC PyInit_imp(void);
+#endif /* !Py_LIMITED_API */
PyAPI_FUNC(long) PyImport_GetMagicNumber(void);
PyAPI_FUNC(const char *) PyImport_GetMagicTag(void);
PyAPI_FUNC(PyObject *) PyImport_ExecCodeModule(
@@ -27,16 +29,20 @@ PyAPI_FUNC(PyObject *) PyImport_ExecCodeModuleWithPathnames(
const char *pathname, /* decoded from the filesystem encoding */
const char *cpathname /* decoded from the filesystem encoding */
);
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
PyAPI_FUNC(PyObject *) PyImport_ExecCodeModuleObject(
PyObject *name,
PyObject *co,
PyObject *pathname,
PyObject *cpathname
);
+#endif
PyAPI_FUNC(PyObject *) PyImport_GetModuleDict(void);
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
PyAPI_FUNC(PyObject *) PyImport_AddModuleObject(
PyObject *name
);
+#endif
PyAPI_FUNC(PyObject *) PyImport_AddModule(
const char *name /* UTF-8 encoded string */
);
@@ -53,6 +59,7 @@ PyAPI_FUNC(PyObject *) PyImport_ImportModuleLevel(
PyObject *fromlist,
int level
);
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
PyAPI_FUNC(PyObject *) PyImport_ImportModuleLevelObject(
PyObject *name,
PyObject *globals,
@@ -60,6 +67,7 @@ PyAPI_FUNC(PyObject *) PyImport_ImportModuleLevelObject(
PyObject *fromlist,
int level
);
+#endif
#define PyImport_ImportModuleEx(n, g, l, f) \
PyImport_ImportModuleLevel(n, g, l, f, 0)
@@ -68,9 +76,11 @@ PyAPI_FUNC(PyObject *) PyImport_GetImporter(PyObject *path);
PyAPI_FUNC(PyObject *) PyImport_Import(PyObject *name);
PyAPI_FUNC(PyObject *) PyImport_ReloadModule(PyObject *m);
PyAPI_FUNC(void) PyImport_Cleanup(void);
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
PyAPI_FUNC(int) PyImport_ImportFrozenModuleObject(
PyObject *name
);
+#endif
PyAPI_FUNC(int) PyImport_ImportFrozenModule(
const char *name /* UTF-8 encoded string */
);
diff --git a/Include/intrcheck.h b/Include/intrcheck.h
index f53fee1..8fb96cf 100644
--- a/Include/intrcheck.h
+++ b/Include/intrcheck.h
@@ -8,12 +8,15 @@ extern "C" {
PyAPI_FUNC(int) PyOS_InterruptOccurred(void);
PyAPI_FUNC(void) PyOS_InitInterrupts(void);
PyAPI_FUNC(void) PyOS_AfterFork(void);
+
+#ifndef Py_LIMITED_API
PyAPI_FUNC(int) _PyOS_IsMainThread(void);
#ifdef MS_WINDOWS
/* windows.h is not included by Python.h so use void* instead of HANDLE */
PyAPI_FUNC(void*) _PyOS_SigintEvent(void);
#endif
+#endif /* !Py_LIMITED_API */
#ifdef __cplusplus
}
diff --git a/Include/longintrepr.h b/Include/longintrepr.h
index bbba4d8..a3b74b4 100644
--- a/Include/longintrepr.h
+++ b/Include/longintrepr.h
@@ -42,14 +42,10 @@ extern "C" {
*/
#if PYLONG_BITS_IN_DIGIT == 30
-#if !(defined HAVE_UINT64_T && defined HAVE_UINT32_T && \
- defined HAVE_INT64_T && defined HAVE_INT32_T)
-#error "30-bit long digits requested, but the necessary types are not available on this platform"
-#endif
-typedef PY_UINT32_T digit;
-typedef PY_INT32_T sdigit; /* signed variant of digit */
-typedef PY_UINT64_T twodigits;
-typedef PY_INT64_T stwodigits; /* signed variant of twodigits */
+typedef uint32_t digit;
+typedef int32_t sdigit; /* signed variant of digit */
+typedef uint64_t twodigits;
+typedef int64_t stwodigits; /* signed variant of twodigits */
#define PyLong_SHIFT 30
#define _PyLong_DECIMAL_SHIFT 9 /* max(e such that 10**e fits in a digit) */
#define _PyLong_DECIMAL_BASE ((digit)1000000000) /* 10 ** DECIMAL_SHIFT */
diff --git a/Include/longobject.h b/Include/longobject.h
index 2a2eecf..efd409c 100644
--- a/Include/longobject.h
+++ b/Include/longobject.h
@@ -65,7 +65,8 @@ PyAPI_FUNC(PyObject *) PyLong_GetInfo(void);
# error "void* different in size from int, long and long long"
#endif /* SIZEOF_VOID_P */
-/* Used by Python/mystrtoul.c. */
+/* Used by Python/mystrtoul.c, _PyBytes_FromHex(),
+ _PyBytes_DecodeEscapeRecode(), etc. */
#ifndef Py_LIMITED_API
PyAPI_DATA(unsigned char) _PyLong_DigitValue[256];
#endif
@@ -84,14 +85,12 @@ PyAPI_FUNC(double) PyLong_AsDouble(PyObject *);
PyAPI_FUNC(PyObject *) PyLong_FromVoidPtr(void *);
PyAPI_FUNC(void *) PyLong_AsVoidPtr(PyObject *);
-#ifdef HAVE_LONG_LONG
-PyAPI_FUNC(PyObject *) PyLong_FromLongLong(PY_LONG_LONG);
-PyAPI_FUNC(PyObject *) PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG);
-PyAPI_FUNC(PY_LONG_LONG) PyLong_AsLongLong(PyObject *);
-PyAPI_FUNC(unsigned PY_LONG_LONG) PyLong_AsUnsignedLongLong(PyObject *);
-PyAPI_FUNC(unsigned PY_LONG_LONG) PyLong_AsUnsignedLongLongMask(PyObject *);
-PyAPI_FUNC(PY_LONG_LONG) PyLong_AsLongLongAndOverflow(PyObject *, int *);
-#endif /* HAVE_LONG_LONG */
+PyAPI_FUNC(PyObject *) PyLong_FromLongLong(long long);
+PyAPI_FUNC(PyObject *) PyLong_FromUnsignedLongLong(unsigned long long);
+PyAPI_FUNC(long long) PyLong_AsLongLong(PyObject *);
+PyAPI_FUNC(unsigned long long) PyLong_AsUnsignedLongLong(PyObject *);
+PyAPI_FUNC(unsigned long long) PyLong_AsUnsignedLongLongMask(PyObject *);
+PyAPI_FUNC(long long) PyLong_AsLongLongAndOverflow(PyObject *, int *);
PyAPI_FUNC(PyObject *) PyLong_FromString(const char *, char **, int);
#ifndef Py_LIMITED_API
@@ -182,6 +181,13 @@ PyAPI_FUNC(int) _PyLong_FormatWriter(
int base,
int alternate);
+PyAPI_FUNC(char*) _PyLong_FormatBytesWriter(
+ _PyBytesWriter *writer,
+ char *str,
+ PyObject *obj,
+ int base,
+ int alternate);
+
/* Format the object based on the format_spec, as defined in PEP 3101
(Advanced String Formatting). */
PyAPI_FUNC(int) _PyLong_FormatAdvancedWriter(
@@ -198,8 +204,10 @@ PyAPI_FUNC(int) _PyLong_FormatAdvancedWriter(
PyAPI_FUNC(unsigned long) PyOS_strtoul(const char *, char **, int);
PyAPI_FUNC(long) PyOS_strtol(const char *, char **, int);
+#ifndef Py_LIMITED_API
/* For use by the gcd function in mathmodule.c */
PyAPI_FUNC(PyObject *) _PyLong_GCD(PyObject *, PyObject *);
+#endif /* !Py_LIMITED_API */
#ifdef __cplusplus
}
diff --git a/Include/memoryobject.h b/Include/memoryobject.h
index ab5ee09..990a716 100644
--- a/Include/memoryobject.h
+++ b/Include/memoryobject.h
@@ -21,8 +21,10 @@ PyAPI_DATA(PyTypeObject) PyMemoryView_Type;
#endif
PyAPI_FUNC(PyObject *) PyMemoryView_FromObject(PyObject *base);
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
PyAPI_FUNC(PyObject *) PyMemoryView_FromMemory(char *mem, Py_ssize_t size,
int flags);
+#endif
#ifndef Py_LIMITED_API
PyAPI_FUNC(PyObject *) PyMemoryView_FromBuffer(Py_buffer *info);
#endif
diff --git a/Include/methodobject.h b/Include/methodobject.h
index e2ad804..79fad82 100644
--- a/Include/methodobject.h
+++ b/Include/methodobject.h
@@ -16,6 +16,8 @@ PyAPI_DATA(PyTypeObject) PyCFunction_Type;
#define PyCFunction_Check(op) (Py_TYPE(op) == &PyCFunction_Type)
typedef PyObject *(*PyCFunction)(PyObject *, PyObject *);
+typedef PyObject *(*_PyCFunctionFast) (PyObject *self, PyObject **args,
+ Py_ssize_t nargs, PyObject *kwnames);
typedef PyObject *(*PyCFunctionWithKeywords)(PyObject *, PyObject *,
PyObject *);
typedef PyObject *(*PyNoArgsFunction)(PyObject *);
@@ -37,6 +39,18 @@ PyAPI_FUNC(int) PyCFunction_GetFlags(PyObject *);
#endif
PyAPI_FUNC(PyObject *) PyCFunction_Call(PyObject *, PyObject *, PyObject *);
+#ifndef Py_LIMITED_API
+PyAPI_FUNC(PyObject *) _PyCFunction_FastCallDict(PyObject *func,
+ PyObject **args,
+ Py_ssize_t nargs,
+ PyObject *kwargs);
+
+PyAPI_FUNC(PyObject *) _PyCFunction_FastCallKeywords(PyObject *func,
+ PyObject **stack,
+ Py_ssize_t nargs,
+ PyObject *kwnames);
+#endif
+
struct PyMethodDef {
const char *ml_name; /* The name of the built-in function/method */
PyCFunction ml_meth; /* The C function that implements it */
@@ -72,6 +86,8 @@ PyAPI_FUNC(PyObject *) PyCFunction_NewEx(PyMethodDef *, PyObject *,
#define METH_COEXIST 0x0040
#ifndef Py_LIMITED_API
+#define METH_FASTCALL 0x0080
+
typedef struct {
PyObject_HEAD
PyMethodDef *m_ml; /* Description of the C function to call */
diff --git a/Include/modsupport.h b/Include/modsupport.h
index 829aaf8..833e33d 100644
--- a/Include/modsupport.h
+++ b/Include/modsupport.h
@@ -15,12 +15,16 @@ extern "C" {
#define PyArg_Parse _PyArg_Parse_SizeT
#define PyArg_ParseTuple _PyArg_ParseTuple_SizeT
#define PyArg_ParseTupleAndKeywords _PyArg_ParseTupleAndKeywords_SizeT
+#ifndef Py_LIMITED_API
#define PyArg_VaParse _PyArg_VaParse_SizeT
#define PyArg_VaParseTupleAndKeywords _PyArg_VaParseTupleAndKeywords_SizeT
+#endif /* !Py_LIMITED_API */
#define Py_BuildValue _Py_BuildValue_SizeT
#define Py_VaBuildValue _Py_VaBuildValue_SizeT
#else
+#ifndef Py_LIMITED_API
PyAPI_FUNC(PyObject *) _Py_VaBuildValue_SizeT(const char *, va_list);
+#endif /* !Py_LIMITED_API */
#endif
/* Due to a glitch in 3.2, the _SizeT versions weren't exported from the DLL. */
@@ -44,6 +48,32 @@ PyAPI_FUNC(int) PyArg_VaParseTupleAndKeywords(PyObject *, PyObject *,
#endif
PyAPI_FUNC(PyObject *) Py_VaBuildValue(const char *, va_list);
+#ifndef Py_LIMITED_API
+typedef struct _PyArg_Parser {
+ const char *format;
+ const char * const *keywords;
+ const char *fname;
+ const char *custom_msg;
+ int pos; /* number of positional-only arguments */
+ int min; /* minimal number of arguments */
+ int max; /* maximal number of positional arguments */
+ PyObject *kwtuple; /* tuple of keyword parameter names */
+ struct _PyArg_Parser *next;
+} _PyArg_Parser;
+#ifdef PY_SSIZE_T_CLEAN
+#define _PyArg_ParseTupleAndKeywordsFast _PyArg_ParseTupleAndKeywordsFast_SizeT
+#define _PyArg_ParseStack _PyArg_ParseStack_SizeT
+#define _PyArg_VaParseTupleAndKeywordsFast _PyArg_VaParseTupleAndKeywordsFast_SizeT
+#endif
+PyAPI_FUNC(int) _PyArg_ParseTupleAndKeywordsFast(PyObject *, PyObject *,
+ struct _PyArg_Parser *, ...);
+PyAPI_FUNC(int) _PyArg_ParseStack(PyObject **args, Py_ssize_t nargs, PyObject *kwnames,
+ struct _PyArg_Parser *, ...);
+PyAPI_FUNC(int) _PyArg_VaParseTupleAndKeywordsFast(PyObject *, PyObject *,
+ struct _PyArg_Parser *, va_list);
+void _PyArg_Fini(void);
+#endif
+
PyAPI_FUNC(int) PyModule_AddObject(PyObject *, const char *, PyObject *);
PyAPI_FUNC(int) PyModule_AddIntConstant(PyObject *, const char *, long);
PyAPI_FUNC(int) PyModule_AddStringConstant(PyObject *, const char *, const char *);
diff --git a/Include/moduleobject.h b/Include/moduleobject.h
index b44fb9b..b6e4933 100644
--- a/Include/moduleobject.h
+++ b/Include/moduleobject.h
@@ -12,14 +12,18 @@ PyAPI_DATA(PyTypeObject) PyModule_Type;
#define PyModule_Check(op) PyObject_TypeCheck(op, &PyModule_Type)
#define PyModule_CheckExact(op) (Py_TYPE(op) == &PyModule_Type)
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
PyAPI_FUNC(PyObject *) PyModule_NewObject(
PyObject *name
);
+#endif
PyAPI_FUNC(PyObject *) PyModule_New(
const char *name /* UTF-8 encoded string */
);
PyAPI_FUNC(PyObject *) PyModule_GetDict(PyObject *);
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
PyAPI_FUNC(PyObject *) PyModule_GetNameObject(PyObject *);
+#endif
PyAPI_FUNC(const char *) PyModule_GetName(PyObject *);
PyAPI_FUNC(const char *) PyModule_GetFilename(PyObject *);
PyAPI_FUNC(PyObject *) PyModule_GetFilenameObject(PyObject *);
diff --git a/Include/namespaceobject.h b/Include/namespaceobject.h
index a412f05..0c8d95c 100644
--- a/Include/namespaceobject.h
+++ b/Include/namespaceobject.h
@@ -7,9 +7,11 @@
extern "C" {
#endif
+#ifndef Py_LIMITED_API
PyAPI_DATA(PyTypeObject) _PyNamespace_Type;
PyAPI_FUNC(PyObject *) _PyNamespace_New(PyObject *kwds);
+#endif /* !Py_LIMITED_API */
#ifdef __cplusplus
}
diff --git a/Include/object.h b/Include/object.h
index 50d9747..63e37b8 100644
--- a/Include/object.h
+++ b/Include/object.h
@@ -118,6 +118,7 @@ typedef struct {
#define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
#define Py_SIZE(ob) (((PyVarObject*)(ob))->ob_size)
+#ifndef Py_LIMITED_API
/********************* String Literals ****************************************/
/* This structure helps managing static strings. The basic usage goes like this:
Instead of doing
@@ -144,10 +145,12 @@ typedef struct _Py_Identifier {
PyObject *object;
} _Py_Identifier;
-#define _Py_static_string_init(value) { 0, value, 0 }
+#define _Py_static_string_init(value) { .next = NULL, .string = value, .object = NULL }
#define _Py_static_string(varname, value) static _Py_Identifier varname = _Py_static_string_init(value)
#define _Py_IDENTIFIER(varname) _Py_static_string(PyId_##varname, #varname)
+#endif /* !Py_LIMITED_API */
+
/*
Type objects contain a string containing the type name (to help somewhat
in debugging), the allocation parameters (see PyObject_New() and
@@ -512,8 +515,8 @@ PyAPI_FUNC(PyObject *) _PyType_GetTextSignatureFromInternalDoc(const char *, con
#endif
/* Generic operations on objects */
-struct _Py_Identifier;
#ifndef Py_LIMITED_API
+struct _Py_Identifier;
PyAPI_FUNC(int) PyObject_Print(PyObject *, FILE *, int);
PyAPI_FUNC(void) _Py_BreakPoint(void);
PyAPI_FUNC(void) _PyObject_Dump(PyObject *);
@@ -530,11 +533,11 @@ PyAPI_FUNC(int) PyObject_HasAttrString(PyObject *, const char *);
PyAPI_FUNC(PyObject *) PyObject_GetAttr(PyObject *, PyObject *);
PyAPI_FUNC(int) PyObject_SetAttr(PyObject *, PyObject *, PyObject *);
PyAPI_FUNC(int) PyObject_HasAttr(PyObject *, PyObject *);
+#ifndef Py_LIMITED_API
PyAPI_FUNC(int) _PyObject_IsAbstract(PyObject *);
PyAPI_FUNC(PyObject *) _PyObject_GetAttrId(PyObject *, struct _Py_Identifier *);
PyAPI_FUNC(int) _PyObject_SetAttrId(PyObject *, struct _Py_Identifier *, PyObject *);
PyAPI_FUNC(int) _PyObject_HasAttrId(PyObject *, struct _Py_Identifier *);
-#ifndef Py_LIMITED_API
PyAPI_FUNC(PyObject **) _PyObject_GetDictPtr(PyObject *);
#endif
PyAPI_FUNC(PyObject *) PyObject_SelfIter(PyObject *);
@@ -544,7 +547,9 @@ PyAPI_FUNC(PyObject *) _PyObject_NextNotImplemented(PyObject *);
PyAPI_FUNC(PyObject *) PyObject_GenericGetAttr(PyObject *, PyObject *);
PyAPI_FUNC(int) PyObject_GenericSetAttr(PyObject *,
PyObject *, PyObject *);
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
PyAPI_FUNC(int) PyObject_GenericSetDict(PyObject *, PyObject *, void *);
+#endif
PyAPI_FUNC(Py_hash_t) PyObject_Hash(PyObject *);
PyAPI_FUNC(Py_hash_t) PyObject_HashNotImplemented(PyObject *);
PyAPI_FUNC(int) PyObject_IsTrue(PyObject *);
@@ -557,6 +562,7 @@ PyAPI_FUNC(void) PyObject_CallFinalizer(PyObject *);
PyAPI_FUNC(int) PyObject_CallFinalizerFromDealloc(PyObject *);
#endif
+#ifndef Py_LIMITED_API
/* Same as PyObject_Generic{Get,Set}Attr, but passing the attributes
dict as the last parameter. */
PyAPI_FUNC(PyObject *)
@@ -564,6 +570,7 @@ _PyObject_GenericGetAttrWithDict(PyObject *, PyObject *, PyObject *);
PyAPI_FUNC(int)
_PyObject_GenericSetAttrWithDict(PyObject *, PyObject *,
PyObject *, PyObject *);
+#endif /* !Py_LIMITED_API */
/* Helper to look up a builtin object */
#ifndef Py_LIMITED_API
@@ -708,7 +715,6 @@ you can count such references to the type object.)
PyAPI_DATA(Py_ssize_t) _Py_RefTotal;
PyAPI_FUNC(void) _Py_NegativeRefcount(const char *fname,
int lineno, PyObject *op);
-PyAPI_FUNC(PyObject *) _PyDict_Dummy(void);
PyAPI_FUNC(Py_ssize_t) _Py_GetRefTotal(void);
#define _Py_INC_REFTOTAL _Py_RefTotal++
#define _Py_DEC_REFTOTAL _Py_RefTotal--
@@ -785,7 +791,7 @@ PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
--(_py_decref_tmp)->ob_refcnt != 0) \
_Py_CHECK_REFCNT(_py_decref_tmp) \
else \
- _Py_Dealloc(_py_decref_tmp); \
+ _Py_Dealloc(_py_decref_tmp); \
} while (0)
/* Safely decref `op` and set `op` to NULL, especially useful in tp_clear
@@ -889,8 +895,10 @@ they can have object code that is not dependent on Python compilation flags.
PyAPI_FUNC(void) Py_IncRef(PyObject *);
PyAPI_FUNC(void) Py_DecRef(PyObject *);
+#ifndef Py_LIMITED_API
PyAPI_DATA(PyTypeObject) _PyNone_Type;
PyAPI_DATA(PyTypeObject) _PyNotImplemented_Type;
+#endif /* !Py_LIMITED_API */
/*
_Py_NoneStruct is an object of undefined type which can be used in contexts
@@ -923,10 +931,12 @@ PyAPI_DATA(PyObject) _Py_NotImplementedStruct; /* Don't use this directly */
#define Py_GT 4
#define Py_GE 5
+#ifndef Py_LIMITED_API
/* Maps Py_LT to Py_GT, ..., Py_GE to Py_LE.
* Defined in object.c.
*/
PyAPI_DATA(int) _Py_SwappedOp[];
+#endif /* !Py_LIMITED_API */
/*
@@ -1023,12 +1033,14 @@ chain of N deallocations is broken into N / PyTrash_UNWIND_LEVEL pieces,
with the call stack never exceeding a depth of PyTrash_UNWIND_LEVEL.
*/
+#ifndef Py_LIMITED_API
/* This is the old private API, invoked by the macros before 3.2.4.
Kept for binary compatibility of extensions using the stable ABI. */
PyAPI_FUNC(void) _PyTrash_deposit_object(PyObject*);
PyAPI_FUNC(void) _PyTrash_destroy_chain(void);
PyAPI_DATA(int) _PyTrash_delete_nesting;
PyAPI_DATA(PyObject *) _PyTrash_delete_later;
+#endif /* !Py_LIMITED_API */
/* The new thread-safe private API, invoked by the macros below. */
PyAPI_FUNC(void) _PyTrash_thread_deposit_object(PyObject*);
diff --git a/Include/objimpl.h b/Include/objimpl.h
index 65b6d91..746f9c9 100644
--- a/Include/objimpl.h
+++ b/Include/objimpl.h
@@ -95,12 +95,16 @@ PyObject_{New, NewVar, Del}.
the raw memory.
*/
PyAPI_FUNC(void *) PyObject_Malloc(size_t size);
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
PyAPI_FUNC(void *) PyObject_Calloc(size_t nelem, size_t elsize);
+#endif
PyAPI_FUNC(void *) PyObject_Realloc(void *ptr, size_t new_size);
PyAPI_FUNC(void) PyObject_Free(void *ptr);
+#ifndef Py_LIMITED_API
/* This function returns the number of allocated memory blocks, regardless of size */
PyAPI_FUNC(Py_ssize_t) _Py_GetAllocatedBlocks(void);
+#endif /* !Py_LIMITED_API */
/* Macros */
#ifdef WITH_PYMALLOC
@@ -224,11 +228,12 @@ PyAPI_FUNC(void) PyObject_SetArenaAllocator(PyObjectArenaAllocator *allocator);
* ==========================
*/
-/* C equivalent of gc.collect(). */
+/* C equivalent of gc.collect() which ignores the state of gc.enabled. */
PyAPI_FUNC(Py_ssize_t) PyGC_Collect(void);
#ifndef Py_LIMITED_API
PyAPI_FUNC(Py_ssize_t) _PyGC_CollectNoFail(void);
+PyAPI_FUNC(Py_ssize_t) _PyGC_CollectIfEnabled(void);
#endif
/* Test if a type has a GC head */
@@ -322,8 +327,10 @@ extern PyGC_Head *_PyGC_generation0;
(!PyTuple_CheckExact(obj) || _PyObject_GC_IS_TRACKED(obj)))
#endif /* Py_LIMITED_API */
+#ifndef Py_LIMITED_API
PyAPI_FUNC(PyObject *) _PyObject_GC_Malloc(size_t size);
PyAPI_FUNC(PyObject *) _PyObject_GC_Calloc(size_t size);
+#endif /* !Py_LIMITED_API */
PyAPI_FUNC(PyObject *) _PyObject_GC_New(PyTypeObject *);
PyAPI_FUNC(PyVarObject *) _PyObject_GC_NewVar(PyTypeObject *, Py_ssize_t);
PyAPI_FUNC(void) PyObject_GC_Track(void *);
diff --git a/Include/odictobject.h b/Include/odictobject.h
index c1d9592..381de58 100644
--- a/Include/odictobject.h
+++ b/Include/odictobject.h
@@ -17,12 +17,13 @@ PyAPI_DATA(PyTypeObject) PyODictKeys_Type;
PyAPI_DATA(PyTypeObject) PyODictItems_Type;
PyAPI_DATA(PyTypeObject) PyODictValues_Type;
-#endif /* Py_LIMITED_API */
-
#define PyODict_Check(op) PyObject_TypeCheck(op, &PyODict_Type)
#define PyODict_CheckExact(op) (Py_TYPE(op) == &PyODict_Type)
#define PyODict_SIZE(op) ((PyDictObject *)op)->ma_used
-#define PyODict_HasKey(od, key) (PyMapping_HasKey(PyObject *)od, key)
+
+#endif /* Py_LIMITED_API */
+
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
PyAPI_FUNC(PyObject *) PyODict_New(void);
PyAPI_FUNC(int) PyODict_SetItem(PyObject *od, PyObject *key, PyObject *item);
@@ -37,6 +38,8 @@ PyAPI_FUNC(int) PyODict_DelItem(PyObject *od, PyObject *key);
#define PyODict_GetItemString(od, key) \
PyDict_GetItemString((PyObject *)od, key)
+#endif
+
#ifdef __cplusplus
}
#endif
diff --git a/Include/opcode.h b/Include/opcode.h
index 3f917fb..be360e1 100644
--- a/Include/opcode.h
+++ b/Include/opcode.h
@@ -60,6 +60,7 @@ extern "C" {
#define WITH_CLEANUP_FINISH 82
#define RETURN_VALUE 83
#define IMPORT_STAR 84
+#define SETUP_ANNOTATIONS 85
#define YIELD_VALUE 86
#define POP_BLOCK 87
#define END_FINALLY 88
@@ -98,18 +99,17 @@ extern "C" {
#define LOAD_FAST 124
#define STORE_FAST 125
#define DELETE_FAST 126
+#define STORE_ANNOTATION 127
#define RAISE_VARARGS 130
#define CALL_FUNCTION 131
#define MAKE_FUNCTION 132
#define BUILD_SLICE 133
-#define MAKE_CLOSURE 134
#define LOAD_CLOSURE 135
#define LOAD_DEREF 136
#define STORE_DEREF 137
#define DELETE_DEREF 138
-#define CALL_FUNCTION_VAR 140
#define CALL_FUNCTION_KW 141
-#define CALL_FUNCTION_VAR_KW 142
+#define CALL_FUNCTION_EX 142
#define SETUP_WITH 143
#define EXTENDED_ARG 144
#define LIST_APPEND 145
@@ -122,6 +122,10 @@ extern "C" {
#define BUILD_TUPLE_UNPACK 152
#define BUILD_SET_UNPACK 153
#define SETUP_ASYNC_WITH 154
+#define FORMAT_VALUE 155
+#define BUILD_CONST_KEY_MAP 156
+#define BUILD_STRING 157
+#define BUILD_TUPLE_UNPACK_WITH_CALL 158
/* EXCEPT_HANDLER is a special, implicit block type which is created when
entering an except handler. It is not an opcode but we define it here
diff --git a/Include/osmodule.h b/Include/osmodule.h
new file mode 100644
index 0000000..9095c2f
--- /dev/null
+++ b/Include/osmodule.h
@@ -0,0 +1,17 @@
+
+/* os module interface */
+
+#ifndef Py_OSMODULE_H
+#define Py_OSMODULE_H
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03060000
+PyAPI_FUNC(PyObject *) PyOS_FSPath(PyObject *path);
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+#endif /* !Py_OSMODULE_H */
diff --git a/Include/patchlevel.h b/Include/patchlevel.h
index 1ec9761..49930e8 100644
--- a/Include/patchlevel.h
+++ b/Include/patchlevel.h
@@ -17,13 +17,13 @@
/* Version parsed out into numeric values */
/*--start constants--*/
#define PY_MAJOR_VERSION 3
-#define PY_MINOR_VERSION 5
-#define PY_MICRO_VERSION 3
+#define PY_MINOR_VERSION 6
+#define PY_MICRO_VERSION 0
#define PY_RELEASE_LEVEL PY_RELEASE_LEVEL_FINAL
#define PY_RELEASE_SERIAL 0
/* Version as a string */
-#define PY_VERSION "3.5.3+"
+#define PY_VERSION "3.6.0+"
/*--end constants--*/
/* Version as a single 4-byte hex number, e.g. 0x010502B2 == 1.5.2b2.
diff --git a/Include/pgenheaders.h b/Include/pgenheaders.h
index 2049ae3..4843de6 100644
--- a/Include/pgenheaders.h
+++ b/Include/pgenheaders.h
@@ -23,6 +23,7 @@ PyAPI_FUNC(void) PySys_WriteStderr(const char *format, ...)
#define delbitset _Py_delbitset
#define dumptree _Py_dumptree
#define findlabel _Py_findlabel
+#define freegrammar _Py_freegrammar
#define mergebitset _Py_mergebitset
#define meta_grammar _Py_meta_grammar
#define newbitset _Py_newbitset
diff --git a/Include/py_curses.h b/Include/py_curses.h
index f2c08f6..3c21697 100644
--- a/Include/py_curses.h
+++ b/Include/py_curses.h
@@ -103,8 +103,8 @@ static void **PyCurses_API;
#endif
/* general error messages */
-static char *catchall_ERR = "curses function returned ERR";
-static char *catchall_NULL = "curses function returned NULL";
+static const char catchall_ERR[] = "curses function returned ERR";
+static const char catchall_NULL[] = "curses function returned NULL";
/* Function Prototype Macros - They are ugly but very, very useful. ;-)
diff --git a/Include/pyatomic.h b/Include/pyatomic.h
index 89028ef..893d30d 100644
--- a/Include/pyatomic.h
+++ b/Include/pyatomic.h
@@ -61,7 +61,7 @@ typedef enum _Py_memory_order {
} _Py_memory_order;
typedef struct _Py_atomic_address {
- Py_uintptr_t _value;
+ uintptr_t _value;
} _Py_atomic_address;
typedef struct _Py_atomic_int {
@@ -98,7 +98,7 @@ typedef enum _Py_memory_order {
} _Py_memory_order;
typedef struct _Py_atomic_address {
- Py_uintptr_t _value;
+ uintptr_t _value;
} _Py_atomic_address;
typedef struct _Py_atomic_int {
diff --git a/Include/pydebug.h b/Include/pydebug.h
index 19bec2b..6e23a89 100644
--- a/Include/pydebug.h
+++ b/Include/pydebug.h
@@ -24,6 +24,10 @@ PyAPI_DATA(int) Py_UnbufferedStdioFlag;
PyAPI_DATA(int) Py_HashRandomizationFlag;
PyAPI_DATA(int) Py_IsolatedFlag;
+#ifdef MS_WINDOWS
+PyAPI_DATA(int) Py_LegacyWindowsStdioFlag;
+#endif
+
/* this is a wrapper around getenv() that pays attention to
Py_IgnoreEnvironmentFlag. It should be used for getting variables like
PYTHONPATH and PYTHONHOME from the environment */
diff --git a/Include/pydtrace.d b/Include/pydtrace.d
new file mode 100644
index 0000000..8836055
--- /dev/null
+++ b/Include/pydtrace.d
@@ -0,0 +1,19 @@
+/* Python DTrace provider */
+
+provider python {
+ probe function__entry(const char *, const char *, int);
+ probe function__return(const char *, const char *, int);
+ probe instance__new__start(const char *, const char *);
+ probe instance__new__done(const char *, const char *);
+ probe instance__delete__start(const char *, const char *);
+ probe instance__delete__done(const char *, const char *);
+ probe line(const char *, const char *, int);
+ probe gc__start(int);
+ probe gc__done(long);
+};
+
+#pragma D attributes Evolving/Evolving/Common provider python provider
+#pragma D attributes Evolving/Evolving/Common provider python module
+#pragma D attributes Evolving/Evolving/Common provider python function
+#pragma D attributes Evolving/Evolving/Common provider python name
+#pragma D attributes Evolving/Evolving/Common provider python args
diff --git a/Include/pydtrace.h b/Include/pydtrace.h
new file mode 100644
index 0000000..c43a253
--- /dev/null
+++ b/Include/pydtrace.h
@@ -0,0 +1,53 @@
+/* Static DTrace probes interface */
+
+#ifndef Py_DTRACE_H
+#define Py_DTRACE_H
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef WITH_DTRACE
+
+#include "pydtrace_probes.h"
+
+/* pydtrace_probes.h, on systems with DTrace, is auto-generated to include
+ `PyDTrace_{PROBE}` and `PyDTrace_{PROBE}_ENABLED()` macros for every probe
+ defined in pydtrace_provider.d.
+
+ Calling these functions must be guarded by a `PyDTrace_{PROBE}_ENABLED()`
+ check to minimize performance impact when probing is off. For example:
+
+ if (PyDTrace_FUNCTION_ENTRY_ENABLED())
+ PyDTrace_FUNCTION_ENTRY(f);
+*/
+
+#else
+
+/* Without DTrace, compile to nothing. */
+
+static inline void PyDTrace_LINE(const char *arg0, const char *arg1, int arg2) {}
+static inline void PyDTrace_FUNCTION_ENTRY(const char *arg0, const char *arg1, int arg2) {}
+static inline void PyDTrace_FUNCTION_RETURN(const char *arg0, const char *arg1, int arg2) {}
+static inline void PyDTrace_GC_START(int arg0) {}
+static inline void PyDTrace_GC_DONE(int arg0) {}
+static inline void PyDTrace_INSTANCE_NEW_START(int arg0) {}
+static inline void PyDTrace_INSTANCE_NEW_DONE(int arg0) {}
+static inline void PyDTrace_INSTANCE_DELETE_START(int arg0) {}
+static inline void PyDTrace_INSTANCE_DELETE_DONE(int arg0) {}
+
+static inline int PyDTrace_LINE_ENABLED(void) { return 0; }
+static inline int PyDTrace_FUNCTION_ENTRY_ENABLED(void) { return 0; }
+static inline int PyDTrace_FUNCTION_RETURN_ENABLED(void) { return 0; }
+static inline int PyDTrace_GC_START_ENABLED(void) { return 0; }
+static inline int PyDTrace_GC_DONE_ENABLED(void) { return 0; }
+static inline int PyDTrace_INSTANCE_NEW_START_ENABLED(void) { return 0; }
+static inline int PyDTrace_INSTANCE_NEW_DONE_ENABLED(void) { return 0; }
+static inline int PyDTrace_INSTANCE_DELETE_START_ENABLED(void) { return 0; }
+static inline int PyDTrace_INSTANCE_DELETE_DONE_ENABLED(void) { return 0; }
+
+#endif /* !WITH_DTRACE */
+
+#ifdef __cplusplus
+}
+#endif
+#endif /* !Py_DTRACE_H */
diff --git a/Include/pyerrors.h b/Include/pyerrors.h
index 35aedb7..8c1dbc5 100644
--- a/Include/pyerrors.h
+++ b/Include/pyerrors.h
@@ -87,8 +87,10 @@ PyAPI_FUNC(PyObject *) PyErr_Occurred(void);
PyAPI_FUNC(void) PyErr_Clear(void);
PyAPI_FUNC(void) PyErr_Fetch(PyObject **, PyObject **, PyObject **);
PyAPI_FUNC(void) PyErr_Restore(PyObject *, PyObject *, PyObject *);
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
PyAPI_FUNC(void) PyErr_GetExcInfo(PyObject **, PyObject **, PyObject **);
PyAPI_FUNC(void) PyErr_SetExcInfo(PyObject *, PyObject *, PyObject *);
+#endif
#if defined(__clang__) || \
(defined(__GNUC_MAJOR__) && \
@@ -147,7 +149,9 @@ PyAPI_FUNC(void) _PyErr_ChainExceptions(PyObject *, PyObject *, PyObject *);
PyAPI_DATA(PyObject *) PyExc_BaseException;
PyAPI_DATA(PyObject *) PyExc_Exception;
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
PyAPI_DATA(PyObject *) PyExc_StopAsyncIteration;
+#endif
PyAPI_DATA(PyObject *) PyExc_StopIteration;
PyAPI_DATA(PyObject *) PyExc_GeneratorExit;
PyAPI_DATA(PyObject *) PyExc_ArithmeticError;
@@ -160,6 +164,9 @@ PyAPI_DATA(PyObject *) PyExc_EOFError;
PyAPI_DATA(PyObject *) PyExc_FloatingPointError;
PyAPI_DATA(PyObject *) PyExc_OSError;
PyAPI_DATA(PyObject *) PyExc_ImportError;
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03060000
+PyAPI_DATA(PyObject *) PyExc_ModuleNotFoundError;
+#endif
PyAPI_DATA(PyObject *) PyExc_IndexError;
PyAPI_DATA(PyObject *) PyExc_KeyError;
PyAPI_DATA(PyObject *) PyExc_KeyboardInterrupt;
@@ -167,7 +174,9 @@ PyAPI_DATA(PyObject *) PyExc_MemoryError;
PyAPI_DATA(PyObject *) PyExc_NameError;
PyAPI_DATA(PyObject *) PyExc_OverflowError;
PyAPI_DATA(PyObject *) PyExc_RuntimeError;
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
PyAPI_DATA(PyObject *) PyExc_RecursionError;
+#endif
PyAPI_DATA(PyObject *) PyExc_NotImplementedError;
PyAPI_DATA(PyObject *) PyExc_SyntaxError;
PyAPI_DATA(PyObject *) PyExc_IndentationError;
@@ -184,6 +193,7 @@ PyAPI_DATA(PyObject *) PyExc_UnicodeTranslateError;
PyAPI_DATA(PyObject *) PyExc_ValueError;
PyAPI_DATA(PyObject *) PyExc_ZeroDivisionError;
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
PyAPI_DATA(PyObject *) PyExc_BlockingIOError;
PyAPI_DATA(PyObject *) PyExc_BrokenPipeError;
PyAPI_DATA(PyObject *) PyExc_ChildProcessError;
@@ -199,6 +209,7 @@ PyAPI_DATA(PyObject *) PyExc_NotADirectoryError;
PyAPI_DATA(PyObject *) PyExc_PermissionError;
PyAPI_DATA(PyObject *) PyExc_ProcessLookupError;
PyAPI_DATA(PyObject *) PyExc_TimeoutError;
+#endif
/* Compatibility aliases */
@@ -231,8 +242,10 @@ PyAPI_FUNC(PyObject *) PyErr_NoMemory(void);
PyAPI_FUNC(PyObject *) PyErr_SetFromErrno(PyObject *);
PyAPI_FUNC(PyObject *) PyErr_SetFromErrnoWithFilenameObject(
PyObject *, PyObject *);
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03040000
PyAPI_FUNC(PyObject *) PyErr_SetFromErrnoWithFilenameObjects(
PyObject *, PyObject *, PyObject *);
+#endif
PyAPI_FUNC(PyObject *) PyErr_SetFromErrnoWithFilename(
PyObject *exc,
const char *filename /* decoded from the filesystem encoding */
@@ -254,6 +267,17 @@ PyAPI_FUNC(PyObject *) PyErr_FormatV(
va_list vargs);
#endif
+#ifndef Py_LIMITED_API
+/* Like PyErr_Format(), but saves current exception as __context__ and
+ __cause__.
+ */
+PyAPI_FUNC(PyObject *) _PyErr_FormatFromCause(
+ PyObject *exception,
+ const char *format, /* ASCII-encoded string */
+ ...
+ );
+#endif
+
#ifdef MS_WINDOWS
PyAPI_FUNC(PyObject *) PyErr_SetFromWindowsErrWithFilename(
int ierr,
@@ -267,8 +291,10 @@ PyAPI_FUNC(PyObject *) PyErr_SetFromWindowsErrWithUnicodeFilename(
PyAPI_FUNC(PyObject *) PyErr_SetFromWindowsErr(int);
PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErrWithFilenameObject(
PyObject *,int, PyObject *);
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03040000
PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErrWithFilenameObjects(
PyObject *,int, PyObject *, PyObject *);
+#endif
PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErrWithFilename(
PyObject *exc,
int ierr,
@@ -281,10 +307,14 @@ PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErrWithUnicodeFilename(
PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErr(PyObject *, int);
#endif /* MS_WINDOWS */
-PyAPI_FUNC(PyObject *) PyErr_SetExcWithArgsKwargs(PyObject *, PyObject *,
- PyObject *);
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03060000
+PyAPI_FUNC(PyObject *) PyErr_SetImportErrorSubclass(PyObject *, PyObject *,
+ PyObject *, PyObject *);
+#endif
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
PyAPI_FUNC(PyObject *) PyErr_SetImportError(PyObject *, PyObject *,
PyObject *);
+#endif
/* Export the old function so that the existing API remains available: */
PyAPI_FUNC(void) PyErr_BadInternalCall(void);
diff --git a/Include/pygetopt.h b/Include/pygetopt.h
index 425c7dd..962720c 100644
--- a/Include/pygetopt.h
+++ b/Include/pygetopt.h
@@ -11,9 +11,9 @@ PyAPI_DATA(int) _PyOS_optind;
PyAPI_DATA(wchar_t *) _PyOS_optarg;
PyAPI_FUNC(void) _PyOS_ResetGetOpt(void);
-#endif
PyAPI_FUNC(int) _PyOS_GetOpt(int argc, wchar_t **argv, wchar_t *optstring);
+#endif /* !Py_LIMITED_API */
#ifdef __cplusplus
}
diff --git a/Include/pyhash.h b/Include/pyhash.h
index a7ca937..a814af6 100644
--- a/Include/pyhash.h
+++ b/Include/pyhash.h
@@ -36,14 +36,14 @@ PyAPI_FUNC(Py_hash_t) _Py_HashBytes(const void*, Py_ssize_t);
* memory layout on 64 bit systems
* cccccccc cccccccc cccccccc uc -- unsigned char[24]
* pppppppp ssssssss ........ fnv -- two Py_hash_t
- * k0k0k0k0 k1k1k1k1 ........ siphash -- two PY_UINT64_T
+ * k0k0k0k0 k1k1k1k1 ........ siphash -- two uint64_t
* ........ ........ ssssssss djbx33a -- 16 bytes padding + one Py_hash_t
* ........ ........ eeeeeeee pyexpat XML hash salt
*
* memory layout on 32 bit systems
* cccccccc cccccccc cccccccc uc
* ppppssss ........ ........ fnv -- two Py_hash_t
- * k0k0k0k0 k1k1k1k1 ........ siphash -- two PY_UINT64_T (*)
+ * k0k0k0k0 k1k1k1k1 ........ siphash -- two uint64_t (*)
* ........ ........ ssss.... djbx33a -- 16 bytes padding + one Py_hash_t
* ........ ........ eeee.... pyexpat XML hash salt
*
@@ -59,13 +59,11 @@ typedef union {
Py_hash_t prefix;
Py_hash_t suffix;
} fnv;
-#ifdef PY_UINT64_T
/* two uint64 for SipHash24 */
struct {
- PY_UINT64_T k0;
- PY_UINT64_T k1;
+ uint64_t k0;
+ uint64_t k1;
} siphash;
-#endif
/* a different (!) Py_hash_t for small string optimization */
struct {
unsigned char padding[16];
@@ -121,8 +119,7 @@ PyAPI_FUNC(PyHash_FuncDef*) PyHash_GetFuncDef(void);
* configure script.
*
* - FNV is available on all platforms and architectures.
- * - SIPHASH24 only works on plaforms that provide PY_UINT64_T and doesn't
- * require aligned memory for integers.
+ * - SIPHASH24 only works on plaforms that don't require aligned memory for integers.
* - With EXTERNAL embedders can provide an alternative implementation with::
*
* PyHash_FuncDef PyHash_Func = {...};
@@ -134,8 +131,7 @@ PyAPI_FUNC(PyHash_FuncDef*) PyHash_GetFuncDef(void);
#define Py_HASH_FNV 2
#ifndef Py_HASH_ALGORITHM
-# if (defined(PY_UINT64_T) && defined(PY_UINT32_T) \
- && !defined(HAVE_ALIGNED_REQUIRED))
+# ifndef HAVE_ALIGNED_REQUIRED
# define Py_HASH_ALGORITHM Py_HASH_SIPHASH24
# else
# define Py_HASH_ALGORITHM Py_HASH_FNV
diff --git a/Include/pylifecycle.h b/Include/pylifecycle.h
index ccdebe2..5a67666 100644
--- a/Include/pylifecycle.h
+++ b/Include/pylifecycle.h
@@ -27,6 +27,7 @@ PyAPI_FUNC(void) Py_InitializeEx(int);
PyAPI_FUNC(void) _Py_InitializeEx_Private(int, int);
#endif
PyAPI_FUNC(void) Py_Finalize(void);
+PyAPI_FUNC(int) Py_FinalizeEx(void);
PyAPI_FUNC(int) Py_IsInitialized(void);
PyAPI_FUNC(PyThreadState *) Py_NewInterpreter(void);
PyAPI_FUNC(void) Py_EndInterpreter(PyThreadState *);
@@ -106,6 +107,7 @@ PyAPI_FUNC(void) _PyGC_Fini(void);
PyAPI_FUNC(void) PySlice_Fini(void);
PyAPI_FUNC(void) _PyType_Fini(void);
PyAPI_FUNC(void) _PyRandom_Fini(void);
+PyAPI_FUNC(void) PyAsyncGen_Fini(void);
PyAPI_DATA(PyThreadState *) _Py_Finalizing;
#endif
@@ -115,8 +117,11 @@ typedef void (*PyOS_sighandler_t)(int);
PyAPI_FUNC(PyOS_sighandler_t) PyOS_getsig(int);
PyAPI_FUNC(PyOS_sighandler_t) PyOS_setsig(int, PyOS_sighandler_t);
+#ifndef Py_LIMITED_API
/* Random */
-PyAPI_FUNC(int) _PyOS_URandom (void *buffer, Py_ssize_t size);
+PyAPI_FUNC(int) _PyOS_URandom(void *buffer, Py_ssize_t size);
+PyAPI_FUNC(int) _PyOS_URandomNonblock(void *buffer, Py_ssize_t size);
+#endif /* !Py_LIMITED_API */
#ifdef __cplusplus
}
diff --git a/Include/pymacro.h b/Include/pymacro.h
index 3f6f5dc..2a839ab 100644
--- a/Include/pymacro.h
+++ b/Include/pymacro.h
@@ -18,6 +18,9 @@
by "__LINE__". */
#define Py_STRINGIFY(x) _Py_XSTRINGIFY(x)
+/* Get the size of a structure member in bytes */
+#define Py_MEMBER_SIZE(type, member) sizeof(((type *)0)->member)
+
/* Argument must be a char or an int in [-128, 127] or [0, 255]. */
#define Py_CHARMASK(c) ((unsigned char)((c) & 0xff))
@@ -36,6 +39,10 @@
#define Py_BUILD_ASSERT_EXPR(cond) \
(sizeof(char [1 - 2*!(cond)]) - 1)
+#define Py_BUILD_ASSERT(cond) do { \
+ (void)Py_BUILD_ASSERT_EXPR(cond); \
+ } while(0)
+
/* Get the number of elements in a visible array
This does not work on pointers, or arrays declared as [], or function
@@ -75,12 +82,12 @@
#define _Py_SIZE_ROUND_UP(n, a) (((size_t)(n) + \
(size_t)((a) - 1)) & ~(size_t)((a) - 1))
/* Round pointer "p" down to the closest "a"-aligned address <= "p". */
-#define _Py_ALIGN_DOWN(p, a) ((void *)((Py_uintptr_t)(p) & ~(Py_uintptr_t)((a) - 1)))
+#define _Py_ALIGN_DOWN(p, a) ((void *)((uintptr_t)(p) & ~(uintptr_t)((a) - 1)))
/* Round pointer "p" up to the closest "a"-aligned address >= "p". */
-#define _Py_ALIGN_UP(p, a) ((void *)(((Py_uintptr_t)(p) + \
- (Py_uintptr_t)((a) - 1)) & ~(Py_uintptr_t)((a) - 1)))
+#define _Py_ALIGN_UP(p, a) ((void *)(((uintptr_t)(p) + \
+ (uintptr_t)((a) - 1)) & ~(uintptr_t)((a) - 1)))
/* Check if pointer "p" is aligned to "a"-bytes boundary. */
-#define _Py_IS_ALIGNED(p, a) (!((Py_uintptr_t)(p) & (Py_uintptr_t)((a) - 1)))
+#define _Py_IS_ALIGNED(p, a) (!((uintptr_t)(p) & (uintptr_t)((a) - 1)))
#ifdef __GNUC__
#define Py_UNUSED(name) _unused_ ## name __attribute__((unused))
diff --git a/Include/pymath.h b/Include/pymath.h
index 1c8d718..7216a09 100644
--- a/Include/pymath.h
+++ b/Include/pymath.h
@@ -55,6 +55,12 @@ extern double pow(double, double);
#define Py_MATH_E 2.7182818284590452354
#endif
+/* Tau (2pi) to 40 digits, taken from tauday.com/tau-digits. */
+#ifndef Py_MATH_TAU
+#define Py_MATH_TAU 6.2831853071795864769252867665590057683943L
+#endif
+
+
/* On x86, Py_FORCE_DOUBLE forces a floating-point number out of an x87 FPU
register and into a 64-bit memory location, rounding from extended
precision to double precision in the process. On other platforms it does
@@ -169,7 +175,7 @@ PyAPI_FUNC(void) _Py_set_387controlword(unsigned short);
#pragma float_control (pop)
#define Py_NAN __icc_nan()
#else /* ICC_NAN_RELAXED as default for Intel Compiler */
- static union { unsigned char buf[8]; double __icc_nan; } __nan_store = {0,0,0,0,0,0,0xf8,0x7f};
+ static const union { unsigned char buf[8]; double __icc_nan; } __nan_store = {0,0,0,0,0,0,0xf8,0x7f};
#define Py_NAN (__nan_store.__icc_nan)
#endif /* ICC_NAN_STRICT */
#endif /* __INTEL_COMPILER */
diff --git a/Include/pymem.h b/Include/pymem.h
index 043db64..a7eb4d2 100644
--- a/Include/pymem.h
+++ b/Include/pymem.h
@@ -16,8 +16,51 @@ PyAPI_FUNC(void *) PyMem_RawMalloc(size_t size);
PyAPI_FUNC(void *) PyMem_RawCalloc(size_t nelem, size_t elsize);
PyAPI_FUNC(void *) PyMem_RawRealloc(void *ptr, size_t new_size);
PyAPI_FUNC(void) PyMem_RawFree(void *ptr);
+
+/* Configure the Python memory allocators. Pass NULL to use default
+ allocators. */
+PyAPI_FUNC(int) _PyMem_SetupAllocators(const char *opt);
+
+#ifdef WITH_PYMALLOC
+PyAPI_FUNC(int) _PyMem_PymallocEnabled(void);
#endif
+/* Identifier of an address space (domain) in tracemalloc */
+typedef unsigned int _PyTraceMalloc_domain_t;
+
+/* Track an allocated memory block in the tracemalloc module.
+ Return 0 on success, return -1 on error (failed to allocate memory to store
+ the trace).
+
+ Return -2 if tracemalloc is disabled.
+
+ If memory block is already tracked, update the existing trace. */
+PyAPI_FUNC(int) _PyTraceMalloc_Track(
+ _PyTraceMalloc_domain_t domain,
+ uintptr_t ptr,
+ size_t size);
+
+/* Untrack an allocated memory block in the tracemalloc module.
+ Do nothing if the block was not tracked.
+
+ Return -2 if tracemalloc is disabled, otherwise return 0. */
+PyAPI_FUNC(int) _PyTraceMalloc_Untrack(
+ _PyTraceMalloc_domain_t domain,
+ uintptr_t ptr);
+
+/* Get the traceback where a memory block was allocated.
+
+ Return a tuple of (filename: str, lineno: int) tuples.
+
+ Return None if the tracemalloc module is disabled or if the memory block
+ is not tracked by tracemalloc.
+
+ Raise an exception and return NULL on error. */
+PyAPI_FUNC(PyObject*) _PyTraceMalloc_GetTraceback(
+ _PyTraceMalloc_domain_t domain,
+ uintptr_t ptr);
+#endif /* !Py_LIMITED_API */
+
/* BEWARE:
@@ -58,7 +101,9 @@ PyAPI_FUNC(void) PyMem_RawFree(void *ptr);
*/
PyAPI_FUNC(void *) PyMem_Malloc(size_t size);
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
PyAPI_FUNC(void *) PyMem_Calloc(size_t nelem, size_t elsize);
+#endif
PyAPI_FUNC(void *) PyMem_Realloc(void *ptr, size_t new_size);
PyAPI_FUNC(void) PyMem_Free(void *ptr);
diff --git a/Include/pyport.h b/Include/pyport.h
index e7e5178..52a91a0 100644
--- a/Include/pyport.h
+++ b/Include/pyport.h
@@ -3,15 +3,7 @@
#include "pyconfig.h" /* include for defines */
-/* Some versions of HP-UX & Solaris need inttypes.h for int32_t,
- INT32_MAX, etc. */
-#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
-#endif
-
-#ifdef HAVE_STDINT_H
-#include <stdint.h>
-#endif
/**************************************************************************
Symbols and macros to supply platform-independent interfaces to basic
@@ -31,14 +23,6 @@ Py_DEBUG
Meaning: Extra checks compiled in for debug mode.
Used in: Py_SAFE_DOWNCAST
-HAVE_UINTPTR_T
-Meaning: The C9X type uintptr_t is supported by the compiler
-Used in: Py_uintptr_t
-
-HAVE_LONG_LONG
-Meaning: The compiler supports the C type "long long"
-Used in: PY_LONG_LONG
-
**************************************************************************/
/* typedefs for some C9X-defined synonyms for integral types.
@@ -53,91 +37,31 @@ Used in: PY_LONG_LONG
* integral synonyms. Only define the ones we actually need.
*/
-#ifdef HAVE_LONG_LONG
+// long long is required. Ensure HAVE_LONG_LONG is defined for compatibility.
+#ifndef HAVE_LONG_LONG
+#define HAVE_LONG_LONG 1
+#endif
#ifndef PY_LONG_LONG
#define PY_LONG_LONG long long
-#if defined(LLONG_MAX)
/* If LLONG_MAX is defined in limits.h, use that. */
#define PY_LLONG_MIN LLONG_MIN
#define PY_LLONG_MAX LLONG_MAX
#define PY_ULLONG_MAX ULLONG_MAX
-#elif defined(__LONG_LONG_MAX__)
-/* Otherwise, if GCC has a builtin define, use that. (Definition of
- * PY_LLONG_MIN assumes two's complement with no trap representation.) */
-#define PY_LLONG_MAX __LONG_LONG_MAX__
-#define PY_LLONG_MIN (-PY_LLONG_MAX - 1)
-#define PY_ULLONG_MAX (PY_LLONG_MAX * Py_ULL(2) + 1)
-#elif defined(SIZEOF_LONG_LONG)
-/* Otherwise compute from SIZEOF_LONG_LONG, assuming two's complement, no
- padding bits, and no trap representation. Note: PY_ULLONG_MAX was
- previously #defined as (~0ULL) here; but that'll give the wrong value in a
- preprocessor expression on systems where long long != intmax_t. */
-#define PY_LLONG_MAX \
- (1 + 2 * ((Py_LL(1) << (CHAR_BIT * SIZEOF_LONG_LONG - 2)) - 1))
-#define PY_LLONG_MIN (-PY_LLONG_MAX - 1)
-#define PY_ULLONG_MAX (PY_LLONG_MAX * Py_ULL(2) + 1)
-#endif /* LLONG_MAX */
-#endif
-#endif /* HAVE_LONG_LONG */
-
-/* a build with 30-bit digits for Python integers needs an exact-width
- * 32-bit unsigned integer type to store those digits. (We could just use
- * type 'unsigned long', but that would be wasteful on a system where longs
- * are 64-bits.) On Unix systems, the autoconf macro AC_TYPE_UINT32_T defines
- * uint32_t to be such a type unless stdint.h or inttypes.h defines uint32_t.
- * However, it doesn't set HAVE_UINT32_T, so we do that here.
- */
-#ifdef uint32_t
-#define HAVE_UINT32_T 1
#endif
-#ifdef HAVE_UINT32_T
-#ifndef PY_UINT32_T
#define PY_UINT32_T uint32_t
-#endif
-#endif
-
-/* Macros for a 64-bit unsigned integer type; used for type 'twodigits' in the
- * integer implementation, when 30-bit digits are enabled.
- */
-#ifdef uint64_t
-#define HAVE_UINT64_T 1
-#endif
-
-#ifdef HAVE_UINT64_T
-#ifndef PY_UINT64_T
#define PY_UINT64_T uint64_t
-#endif
-#endif
/* Signed variants of the above */
-#ifdef int32_t
-#define HAVE_INT32_T 1
-#endif
-
-#ifdef HAVE_INT32_T
-#ifndef PY_INT32_T
#define PY_INT32_T int32_t
-#endif
-#endif
-
-#ifdef int64_t
-#define HAVE_INT64_T 1
-#endif
-
-#ifdef HAVE_INT64_T
-#ifndef PY_INT64_T
#define PY_INT64_T int64_t
-#endif
-#endif
/* If PYLONG_BITS_IN_DIGIT is not defined then we'll use 30-bit digits if all
the necessary integer types are available, and we're on a 64-bit platform
(as determined by SIZEOF_VOID_P); otherwise we use 15-bit digits. */
#ifndef PYLONG_BITS_IN_DIGIT
-#if (defined HAVE_UINT64_T && defined HAVE_INT64_T && \
- defined HAVE_UINT32_T && defined HAVE_INT32_T && SIZEOF_VOID_P >= 8)
+#if SIZEOF_VOID_P >= 8
#define PYLONG_BITS_IN_DIGIT 30
#else
#define PYLONG_BITS_IN_DIGIT 15
@@ -149,26 +73,9 @@ Used in: PY_LONG_LONG
* without loss of information. Similarly for intptr_t, wrt a signed
* integral type.
*/
-#ifdef HAVE_UINTPTR_T
typedef uintptr_t Py_uintptr_t;
typedef intptr_t Py_intptr_t;
-#elif SIZEOF_VOID_P <= SIZEOF_INT
-typedef unsigned int Py_uintptr_t;
-typedef int Py_intptr_t;
-
-#elif SIZEOF_VOID_P <= SIZEOF_LONG
-typedef unsigned long Py_uintptr_t;
-typedef long Py_intptr_t;
-
-#elif defined(HAVE_LONG_LONG) && (SIZEOF_VOID_P <= SIZEOF_LONG_LONG)
-typedef unsigned PY_LONG_LONG Py_uintptr_t;
-typedef PY_LONG_LONG Py_intptr_t;
-
-#else
-# error "Python needs a typedef for Py_uintptr_t in pyport.h."
-#endif /* HAVE_UINTPTR_T */
-
/* Py_ssize_t is a signed integral type such that sizeof(Py_ssize_t) ==
* sizeof(size_t). C99 doesn't define such a thing directly (size_t is an
* unsigned integral type). See PEP 353 for details.
@@ -195,16 +102,8 @@ typedef Py_ssize_t Py_ssize_clean_t;
typedef int Py_ssize_clean_t;
#endif
-/* Largest possible value of size_t.
- SIZE_MAX is part of C99, so it might be defined on some
- platforms. If it is not defined, (size_t)-1 is a portable
- definition for C89, due to the way signed->unsigned
- conversion is defined. */
-#ifdef SIZE_MAX
+/* Largest possible value of size_t. */
#define PY_SIZE_MAX SIZE_MAX
-#else
-#define PY_SIZE_MAX ((size_t)-1)
-#endif
/* Largest positive value of type Py_ssize_t. */
#define PY_SSIZE_T_MAX ((Py_ssize_t)(((size_t)-1)>>1))
@@ -247,22 +146,6 @@ typedef int Py_ssize_clean_t;
# endif
#endif
-/* PY_FORMAT_LONG_LONG is analogous to PY_FORMAT_SIZE_T above, but for
- * the long long type instead of the size_t type. It's only available
- * when HAVE_LONG_LONG is defined. The "high level" Python format
- * functions listed above will interpret "lld" or "llu" correctly on
- * all platforms.
- */
-#ifdef HAVE_LONG_LONG
-# ifndef PY_FORMAT_LONG_LONG
-# ifdef MS_WINDOWS
-# define PY_FORMAT_LONG_LONG "I64"
-# else
-# error "This platform's pyconfig.h needs to define PY_FORMAT_LONG_LONG"
-# endif
-# endif
-#endif
-
/* Py_LOCAL can be used instead of static to get the fastest possible calling
* convention for functions that are local to a given module.
*
@@ -298,26 +181,9 @@ typedef int Py_ssize_clean_t;
#define Py_LOCAL_INLINE(type) static type
#endif
-/* Py_MEMCPY can be used instead of memcpy in cases where the copied blocks
- * are often very short. While most platforms have highly optimized code for
- * large transfers, the setup costs for memcpy are often quite high. MEMCPY
- * solves this by doing short copies "in line".
- */
-
-#if defined(_MSC_VER)
-#define Py_MEMCPY(target, source, length) do { \
- size_t i_, n_ = (length); \
- char *t_ = (void*) (target); \
- const char *s_ = (void*) (source); \
- if (n_ >= 16) \
- memcpy(t_, s_, n_); \
- else \
- for (i_ = 0; i_ < n_; i_++) \
- t_[i_] = s_[i_]; \
- } while (0)
-#else
+/* Py_MEMCPY is kept for backwards compatibility,
+ * see https://bugs.python.org/issue28126 */
#define Py_MEMCPY memcpy
-#endif
#include <stdlib.h>
@@ -570,18 +436,18 @@ extern "C" {
#define HAVE_PY_SET_53BIT_PRECISION 1
#define _Py_SET_53BIT_PRECISION_HEADER \
unsigned int old_fpcr, new_fpcr
-#define _Py_SET_53BIT_PRECISION_START \
- do { \
- __asm__ ("fmove.l %%fpcr,%0" : "=g" (old_fpcr)); \
- /* Set double precision / round to nearest. */ \
- new_fpcr = (old_fpcr & ~0xf0) | 0x80; \
- if (new_fpcr != old_fpcr) \
- __asm__ volatile ("fmove.l %0,%%fpcr" : : "g" (new_fpcr)); \
+#define _Py_SET_53BIT_PRECISION_START \
+ do { \
+ __asm__ ("fmove.l %%fpcr,%0" : "=g" (old_fpcr)); \
+ /* Set double precision / round to nearest. */ \
+ new_fpcr = (old_fpcr & ~0xf0) | 0x80; \
+ if (new_fpcr != old_fpcr) \
+ __asm__ volatile ("fmove.l %0,%%fpcr" : : "g" (new_fpcr)); \
} while (0)
-#define _Py_SET_53BIT_PRECISION_END \
- do { \
- if (new_fpcr != old_fpcr) \
- __asm__ volatile ("fmove.l %0,%%fpcr" : : "g" (old_fpcr)); \
+#define _Py_SET_53BIT_PRECISION_END \
+ do { \
+ if (new_fpcr != old_fpcr) \
+ __asm__ volatile ("fmove.l %0,%%fpcr" : : "g" (old_fpcr)); \
} while (0)
#endif
@@ -850,10 +716,6 @@ extern pid_t forkpty(int *, char *, struct termios *, struct winsize *);
#pragma error_messages (off,E_END_OF_LOOP_CODE_NOT_REACHED)
#endif
-/*
- * Older Microsoft compilers don't support the C99 long long literal suffixes,
- * so these will be defined in PC/pyconfig.h for those compilers.
- */
#ifndef Py_LL
#define Py_LL(x) x##LL
#endif
@@ -862,15 +724,7 @@ extern pid_t forkpty(int *, char *, struct termios *, struct winsize *);
#define Py_ULL(x) Py_LL(x##U)
#endif
-#ifdef VA_LIST_IS_ARRAY
-#define Py_VA_COPY(x, y) Py_MEMCPY((x), (y), sizeof(va_list))
-#else
-#ifdef __va_copy
-#define Py_VA_COPY __va_copy
-#else
-#define Py_VA_COPY(x, y) (x) = (y)
-#endif
-#endif
+#define Py_VA_COPY va_copy
/*
* Convenient macros to deal with endianness of the platform. WORDS_BIGENDIAN is
@@ -906,4 +760,8 @@ extern _invalid_parameter_handler _Py_silent_invalid_parameter_handler;
#endif /* _MSC_VER >= 1900 */
#endif /* Py_BUILD_CORE */
+#ifdef __ANDROID__
+#include <android/api-level.h>
+#endif
+
#endif /* Py_PYPORT_H */
diff --git a/Include/pystate.h b/Include/pystate.h
index 0499a74..afc3c0c 100644
--- a/Include/pystate.h
+++ b/Include/pystate.h
@@ -8,14 +8,21 @@
extern "C" {
#endif
+/* This limitation is for performance and simplicity. If needed it can be
+removed (with effort). */
+#define MAX_CO_EXTRA_USERS 255
+
/* State shared between threads */
struct _ts; /* Forward */
struct _is; /* Forward */
+struct _frame; /* Forward declaration for PyFrameObject. */
#ifdef Py_LIMITED_API
typedef struct _is PyInterpreterState;
#else
+typedef PyObject* (*_PyFrameEvalFunction)(struct _frame *, int);
+
typedef struct _is {
struct _is *next;
@@ -36,19 +43,17 @@ typedef struct _is {
#ifdef HAVE_DLOPEN
int dlopenflags;
#endif
-#ifdef WITH_TSC
- int tscdump;
-#endif
PyObject *builtins_copy;
+ PyObject *import_func;
+ /* Initialized to PyEval_EvalFrameDefault(). */
+ _PyFrameEvalFunction eval_frame;
} PyInterpreterState;
#endif
/* State unique per thread */
-struct _frame; /* Avoid including frameobject.h */
-
#ifndef Py_LIMITED_API
/* Py_tracefunc return -1 when raising an exception, or 0 for success. */
typedef int (*Py_tracefunc)(PyObject *, struct _frame *, int, PyObject *);
@@ -137,6 +142,12 @@ typedef struct _ts {
PyObject *coroutine_wrapper;
int in_coroutine_wrapper;
+ Py_ssize_t co_extra_user_count;
+ freefunc co_extra_freefuncs[MAX_CO_EXTRA_USERS];
+
+ PyObject *async_gen_firstiter;
+ PyObject *async_gen_finalizer;
+
/* XXX signal handlers should also be here */
} PyThreadState;
@@ -146,7 +157,9 @@ typedef struct _ts {
PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_New(void);
PyAPI_FUNC(void) PyInterpreterState_Clear(PyInterpreterState *);
PyAPI_FUNC(void) PyInterpreterState_Delete(PyInterpreterState *);
+#ifndef Py_LIMITED_API
PyAPI_FUNC(int) _PyState_AddModule(PyObject*, struct PyModuleDef*);
+#endif /* !Py_LIMITED_API */
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
/* New in 3.3 */
PyAPI_FUNC(int) PyState_AddModule(PyObject*, struct PyModuleDef*);
@@ -158,14 +171,20 @@ PyAPI_FUNC(void) _PyState_ClearModules(void);
#endif
PyAPI_FUNC(PyThreadState *) PyThreadState_New(PyInterpreterState *);
+#ifndef Py_LIMITED_API
PyAPI_FUNC(PyThreadState *) _PyThreadState_Prealloc(PyInterpreterState *);
PyAPI_FUNC(void) _PyThreadState_Init(PyThreadState *);
+#endif /* !Py_LIMITED_API */
PyAPI_FUNC(void) PyThreadState_Clear(PyThreadState *);
PyAPI_FUNC(void) PyThreadState_Delete(PyThreadState *);
+#ifndef Py_LIMITED_API
PyAPI_FUNC(void) _PyThreadState_DeleteExcept(PyThreadState *tstate);
+#endif /* !Py_LIMITED_API */
#ifdef WITH_THREAD
PyAPI_FUNC(void) PyThreadState_DeleteCurrent(void);
+#ifndef Py_LIMITED_API
PyAPI_FUNC(void) _PyGILState_Reinit(void);
+#endif /* !Py_LIMITED_API */
#endif
/* Return the current thread state. The global interpreter lock must be held.
@@ -173,9 +192,11 @@ PyAPI_FUNC(void) _PyGILState_Reinit(void);
* the caller needn't check for NULL). */
PyAPI_FUNC(PyThreadState *) PyThreadState_Get(void);
+#ifndef Py_LIMITED_API
/* Similar to PyThreadState_Get(), but don't issue a fatal error
* if it is NULL. */
PyAPI_FUNC(PyThreadState *) _PyThreadState_UncheckedGet(void);
+#endif /* !Py_LIMITED_API */
PyAPI_FUNC(PyThreadState *) PyThreadState_Swap(PyThreadState *);
PyAPI_FUNC(PyObject *) PyThreadState_GetDict(void);
@@ -241,11 +262,23 @@ PyAPI_FUNC(void) PyGILState_Release(PyGILState_STATE);
*/
PyAPI_FUNC(PyThreadState *) PyGILState_GetThisThreadState(void);
-/* Helper/diagnostic function - return 1 if the current thread
- * currently holds the GIL, 0 otherwise
- */
#ifndef Py_LIMITED_API
+/* Issue #26558: Flag to disable PyGILState_Check().
+ If set to non-zero, PyGILState_Check() always return 1. */
+PyAPI_DATA(int) _PyGILState_check_enabled;
+
+/* Helper/diagnostic function - return 1 if the current thread
+ currently holds the GIL, 0 otherwise.
+
+ The function returns 1 if _PyGILState_check_enabled is non-zero. */
PyAPI_FUNC(int) PyGILState_Check(void);
+
+/* Unsafe function to get the single PyInterpreterState used by this process'
+ GILState implementation.
+
+ Return NULL before _PyGILState_Init() is called and after _PyGILState_Fini()
+ is called. */
+PyAPI_FUNC(PyInterpreterState *) _PyGILState_GetInterpreterStateUnsafe(void);
#endif
#endif /* #ifdef WITH_THREAD */
diff --git a/Include/pystrhex.h b/Include/pystrhex.h
index 1dc1255..66a30e2 100644
--- a/Include/pystrhex.h
+++ b/Include/pystrhex.h
@@ -5,10 +5,12 @@
extern "C" {
#endif
+#ifndef Py_LIMITED_API
/* Returns a str() containing the hex representation of argbuf. */
PyAPI_FUNC(PyObject*) _Py_strhex(const char* argbuf, const Py_ssize_t arglen);
/* Returns a bytes() containing the ASCII hex representation of argbuf. */
PyAPI_FUNC(PyObject*) _Py_strhex_bytes(const char* argbuf, const Py_ssize_t arglen);
+#endif /* !Py_LIMITED_API */
#ifdef __cplusplus
}
diff --git a/Include/pystrtod.h b/Include/pystrtod.h
index 23fd1c6..c1e84de 100644
--- a/Include/pystrtod.h
+++ b/Include/pystrtod.h
@@ -19,6 +19,10 @@ PyAPI_FUNC(char *) PyOS_double_to_string(double val,
int *type);
#ifndef Py_LIMITED_API
+PyAPI_FUNC(PyObject *) _Py_string_to_number_with_underscores(
+ const char *str, Py_ssize_t len, const char *what, PyObject *obj, void *arg,
+ PyObject *(*innerfunc)(const char *, Py_ssize_t, void *));
+
PyAPI_FUNC(double) _Py_parse_inf_or_nan(const char *p, char **endptr);
#endif
diff --git a/Include/pythonrun.h b/Include/pythonrun.h
index 9c2e813..efc613f 100644
--- a/Include/pythonrun.h
+++ b/Include/pythonrun.h
@@ -66,8 +66,8 @@ PyAPI_FUNC(struct _mod *) PyParser_ASTFromFile(
const char *filename, /* decoded from the filesystem encoding */
const char* enc,
int start,
- char *ps1,
- char *ps2,
+ const char *ps1,
+ const char *ps2,
PyCompilerFlags *flags,
int *errcode,
PyArena *arena);
@@ -76,8 +76,8 @@ PyAPI_FUNC(struct _mod *) PyParser_ASTFromFileObject(
PyObject *filename,
const char* enc,
int start,
- char *ps1,
- char *ps2,
+ const char *ps1,
+ const char *ps2,
PyCompilerFlags *flags,
int *errcode,
PyArena *arena);
@@ -91,9 +91,11 @@ PyAPI_FUNC(struct _mod *) PyParser_ASTFromFileObject(
#endif
PyAPI_FUNC(struct _node *) PyParser_SimpleParseStringFlags(const char *, int,
int);
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
PyAPI_FUNC(struct _node *) PyParser_SimpleParseStringFlagsFilename(const char *,
const char *,
int, int);
+#endif
PyAPI_FUNC(struct _node *) PyParser_SimpleParseFileFlags(FILE *, const char *,
int, int);
diff --git a/Include/pythread.h b/Include/pythread.h
index 6e9f303..88c4873 100644
--- a/Include/pythread.h
+++ b/Include/pythread.h
@@ -37,19 +37,14 @@ PyAPI_FUNC(int) PyThread_acquire_lock(PyThread_type_lock, int);
module exposes a higher-level API, with timeouts expressed in seconds
and floating-point numbers allowed.
*/
-#if defined(HAVE_LONG_LONG)
-#define PY_TIMEOUT_T PY_LONG_LONG
+#define PY_TIMEOUT_T long long
#define PY_TIMEOUT_MAX PY_LLONG_MAX
-#else
-#define PY_TIMEOUT_T long
-#define PY_TIMEOUT_MAX LONG_MAX
-#endif
/* In the NT API, the timeout is a DWORD and is expressed in milliseconds */
#if defined (NT_THREADS)
-#if (Py_LL(0xFFFFFFFF) * 1000 < PY_TIMEOUT_MAX)
+#if 0xFFFFFFFFLL * 1000 < PY_TIMEOUT_MAX
#undef PY_TIMEOUT_MAX
-#define PY_TIMEOUT_MAX (Py_LL(0xFFFFFFFF) * 1000)
+#define PY_TIMEOUT_MAX (0xFFFFFFFFLL * 1000)
#endif
#endif
@@ -74,7 +69,9 @@ PyAPI_FUNC(void) PyThread_release_lock(PyThread_type_lock);
PyAPI_FUNC(size_t) PyThread_get_stacksize(void);
PyAPI_FUNC(int) PyThread_set_stacksize(size_t);
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
PyAPI_FUNC(PyObject*) PyThread_GetInfo(void);
+#endif
/* Thread Local Storage (TLS) API */
PyAPI_FUNC(int) PyThread_create_key(void);
diff --git a/Include/pytime.h b/Include/pytime.h
index 494322c..87ac7fc 100644
--- a/Include/pytime.h
+++ b/Include/pytime.h
@@ -13,16 +13,12 @@ functions and constants
extern "C" {
#endif
-#ifdef PY_INT64_T
/* _PyTime_t: Python timestamp with subsecond precision. It can be used to
store a duration, and so indirectly a date (related to another date, like
UNIX epoch). */
-typedef PY_INT64_T _PyTime_t;
+typedef int64_t _PyTime_t;
#define _PyTime_MIN PY_LLONG_MIN
#define _PyTime_MAX PY_LLONG_MAX
-#else
-# error "_PyTime_t need signed 64-bit integer type"
-#endif
typedef enum {
/* Round towards minus infinity (-inf).
@@ -30,7 +26,10 @@ typedef enum {
_PyTime_ROUND_FLOOR=0,
/* Round towards infinity (+inf).
For example, used for timeout to wait "at least" N seconds. */
- _PyTime_ROUND_CEILING
+ _PyTime_ROUND_CEILING=1,
+ /* Round to nearest with ties going to nearest even integer.
+ For example, used to round from a Python float. */
+ _PyTime_ROUND_HALF_EVEN
} _PyTime_round_t;
/* Convert a time_t to a PyLong. */
@@ -75,7 +74,7 @@ PyAPI_FUNC(_PyTime_t) _PyTime_FromSeconds(int seconds);
((_PyTime_t)(seconds) * (1000 * 1000 * 1000))
/* Create a timestamp from a number of nanoseconds. */
-PyAPI_FUNC(_PyTime_t) _PyTime_FromNanoseconds(PY_LONG_LONG ns);
+PyAPI_FUNC(_PyTime_t) _PyTime_FromNanoseconds(long long ns);
/* Convert a number of seconds (Python float or int) to a timetamp.
Raise an exception and return -1 on error, return 0 on success. */
@@ -185,6 +184,14 @@ PyAPI_FUNC(int) _PyTime_GetMonotonicClockWithInfo(
Return 0 on success, raise an exception and return -1 on error. */
PyAPI_FUNC(int) _PyTime_Init(void);
+/* Converts a timestamp to the Gregorian time, using the local time zone.
+ Return 0 on success, raise an exception and return -1 on error. */
+PyAPI_FUNC(int) _PyTime_localtime(time_t t, struct tm *tm);
+
+/* Converts a timestamp to the Gregorian time, assuming UTC.
+ Return 0 on success, raise an exception and return -1 on error. */
+PyAPI_FUNC(int) _PyTime_gmtime(time_t t, struct tm *tm);
+
#ifdef __cplusplus
}
#endif
diff --git a/Include/setobject.h b/Include/setobject.h
index f17bc1b..87ec1c8 100644
--- a/Include/setobject.h
+++ b/Include/setobject.h
@@ -10,12 +10,13 @@ extern "C" {
/* There are three kinds of entries in the table:
-1. Unused: key == NULL
-2. Active: key != NULL and key != dummy
-3. Dummy: key == dummy
+1. Unused: key == NULL and hash == 0
+2. Dummy: key == dummy and hash == -1
+3. Active: key != NULL and key != dummy and hash != -1
-The hash field of Unused slots have no meaning.
-The hash field of Dummny slots are set to -1
+The hash field of Unused slots is always zero.
+
+The hash field of Dummy slots are set to -1
meaning that dummy entries can be detected by
either entry->key==dummy or by entry->hash==-1.
*/
diff --git a/Include/structmember.h b/Include/structmember.h
index 948f690..5da8a46 100644
--- a/Include/structmember.h
+++ b/Include/structmember.h
@@ -49,10 +49,8 @@ typedef struct PyMemberDef {
#define T_OBJECT_EX 16 /* Like T_OBJECT, but raises AttributeError
when the value is NULL, instead of
converting to None. */
-#ifdef HAVE_LONG_LONG
#define T_LONGLONG 17
#define T_ULONGLONG 18
-#endif /* HAVE_LONG_LONG */
#define T_PYSSIZET 19 /* Py_ssize_t */
#define T_NONE 20 /* Value is always None */
diff --git a/Include/symtable.h b/Include/symtable.h
index 1409cd9..86ae3c2 100644
--- a/Include/symtable.h
+++ b/Include/symtable.h
@@ -48,6 +48,7 @@ typedef struct _symtable_entry {
unsigned ste_child_free : 1; /* true if a child block has free vars,
including free refs to globals */
unsigned ste_generator : 1; /* true if namespace is a generator */
+ unsigned ste_coroutine : 1; /* true if namespace is a coroutine */
unsigned ste_varargs : 1; /* true if block has varargs */
unsigned ste_varkeywords : 1; /* true if block has varkeywords */
unsigned ste_returns_value : 1; /* true if namespace uses return with
@@ -91,6 +92,7 @@ PyAPI_FUNC(void) PySymtable_Free(struct symtable *);
#define DEF_FREE 2<<4 /* name used but not defined in nested block */
#define DEF_FREE_CLASS 2<<5 /* free variable from class's method */
#define DEF_IMPORT 2<<6 /* assignment occurred via import */
+#define DEF_ANNOT 2<<7 /* this name is annotated */
#define DEF_BOUND (DEF_LOCAL | DEF_PARAM | DEF_IMPORT)
diff --git a/Include/sysmodule.h b/Include/sysmodule.h
index cde10ac..c5547ff 100644
--- a/Include/sysmodule.h
+++ b/Include/sysmodule.h
@@ -8,11 +8,11 @@ extern "C" {
#endif
PyAPI_FUNC(PyObject *) PySys_GetObject(const char *);
+PyAPI_FUNC(int) PySys_SetObject(const char *, PyObject *);
#ifndef Py_LIMITED_API
PyAPI_FUNC(PyObject *) _PySys_GetObjectId(_Py_Identifier *key);
-#endif
-PyAPI_FUNC(int) PySys_SetObject(const char *, PyObject *);
PyAPI_FUNC(int) _PySys_SetObjectId(_Py_Identifier *key, PyObject *);
+#endif
PyAPI_FUNC(void) PySys_SetArgv(int, wchar_t **);
PyAPI_FUNC(void) PySys_SetArgvEx(int, wchar_t **, int);
diff --git a/Include/traceback.h b/Include/traceback.h
index c3ecbe2..b587410 100644
--- a/Include/traceback.h
+++ b/Include/traceback.h
@@ -31,6 +31,7 @@ PyAPI_FUNC(void) _PyTraceback_Add(const char *, const char *, int);
PyAPI_DATA(PyTypeObject) PyTraceBack_Type;
#define PyTraceBack_Check(v) (Py_TYPE(v) == &PyTraceBack_Type)
+#ifndef Py_LIMITED_API
/* Write the Python traceback into the file 'fd'. For example:
Traceback (most recent call first):
@@ -53,19 +54,64 @@ PyAPI_FUNC(void) _Py_DumpTraceback(
PyThreadState *tstate);
/* Write the traceback of all threads into the file 'fd'. current_thread can be
- NULL. Return NULL on success, or an error message on error.
+ NULL.
+
+ Return NULL on success, or an error message on error.
This function is written for debug purpose only. It calls
_Py_DumpTraceback() for each thread, and so has the same limitations. It
only write the traceback of the first 100 threads: write "..." if there are
more threads.
+ If current_tstate is NULL, the function tries to get the Python thread state
+ of the current thread. It is not an error if the function is unable to get
+ the current Python thread state.
+
+ If interp is NULL, the function tries to get the interpreter state from
+ the current Python thread state, or from
+ _PyGILState_GetInterpreterStateUnsafe() in last resort.
+
+ It is better to pass NULL to interp and current_tstate, the function tries
+ different options to retrieve these informations.
+
This function is signal safe. */
PyAPI_FUNC(const char*) _Py_DumpTracebackThreads(
- int fd, PyInterpreterState *interp,
- PyThreadState *current_thread);
+ int fd,
+ PyInterpreterState *interp,
+ PyThreadState *current_tstate);
+#endif /* !Py_LIMITED_API */
+
+#ifndef Py_LIMITED_API
+
+/* Write a Unicode object into the file descriptor fd. Encode the string to
+ ASCII using the backslashreplace error handler.
+
+ Do nothing if text is not a Unicode object. The function accepts Unicode
+ string which is not ready (PyUnicode_WCHAR_KIND).
+
+ This function is signal safe. */
+PyAPI_FUNC(void) _Py_DumpASCII(int fd, PyObject *text);
+
+/* Format an integer as decimal into the file descriptor fd.
+
+ This function is signal safe. */
+PyAPI_FUNC(void) _Py_DumpDecimal(
+ int fd,
+ unsigned long value);
+
+/* Format an integer as hexadecimal into the file descriptor fd with at least
+ width digits.
+
+ The maximum width is sizeof(unsigned long)*2 digits.
+
+ This function is signal safe. */
+PyAPI_FUNC(void) _Py_DumpHexadecimal(
+ int fd,
+ unsigned long value,
+ Py_ssize_t width);
+#endif /* !Py_LIMITED_API */
#ifdef __cplusplus
}
diff --git a/Include/unicodeobject.h b/Include/unicodeobject.h
index 9308a6a..587cf03 100644
--- a/Include/unicodeobject.h
+++ b/Include/unicodeobject.h
@@ -103,10 +103,6 @@ typedef wchar_t Py_UNICODE;
# endif
#endif
-#if defined(MS_WINDOWS)
-# define HAVE_MBCS
-#endif
-
#ifdef HAVE_WCHAR_H
/* Work around a cosmetic bug in BSDI 4.x wchar.h; thanks to Thomas Wouters */
# ifdef _HAVE_BSDI
@@ -117,21 +113,9 @@ typedef wchar_t Py_UNICODE;
/* Py_UCS4 and Py_UCS2 are typedefs for the respective
unicode representations. */
-#if SIZEOF_INT == 4
-typedef unsigned int Py_UCS4;
-#elif SIZEOF_LONG == 4
-typedef unsigned long Py_UCS4;
-#else
-#error "Could not find a proper typedef for Py_UCS4"
-#endif
-
-#if SIZEOF_SHORT == 2
-typedef unsigned short Py_UCS2;
-#else
-#error "Could not find a proper typedef for Py_UCS2"
-#endif
-
-typedef unsigned char Py_UCS1;
+typedef uint32_t Py_UCS4;
+typedef uint16_t Py_UCS2;
+typedef uint8_t Py_UCS1;
/* --- Internal Unicode Operations ---------------------------------------- */
@@ -172,7 +156,7 @@ typedef unsigned char Py_UCS1;
Py_UNICODE_ISNUMERIC(ch))
#define Py_UNICODE_COPY(target, source, length) \
- Py_MEMCPY((target), (source), (length)*sizeof(Py_UNICODE))
+ memcpy((target), (source), (length)*sizeof(Py_UNICODE))
#define Py_UNICODE_FILL(target, value, length) \
do {Py_ssize_t i_; Py_UNICODE *t_ = (target); Py_UNICODE v_ = (value);\
@@ -734,10 +718,12 @@ PyAPI_FUNC(PyObject*) _PyUnicode_FromASCII(
Py_ssize_t size);
#endif
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
PyAPI_FUNC(PyObject*) PyUnicode_Substring(
PyObject *str,
Py_ssize_t start,
Py_ssize_t end);
+#endif
#ifndef Py_LIMITED_API
/* Compute the maximum character of the substring unicode[start:end].
@@ -748,6 +734,7 @@ PyAPI_FUNC(Py_UCS4) _PyUnicode_FindMaxChar (
Py_ssize_t end);
#endif
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
/* Copy the string into a UCS4 buffer including the null character if copy_null
is set. Return NULL and raise an exception on error. Raise a SystemError if
the buffer is smaller than the string. Return buffer on success.
@@ -763,6 +750,7 @@ PyAPI_FUNC(Py_UCS4*) PyUnicode_AsUCS4(
* PyMem_Malloc; if this fails, NULL is returned with a memory error
exception set. */
PyAPI_FUNC(Py_UCS4*) PyUnicode_AsUCS4Copy(PyObject *unicode);
+#endif
/* Return a read-only pointer to the Unicode object's internal
Py_UNICODE buffer.
@@ -787,11 +775,13 @@ PyAPI_FUNC(Py_UNICODE *) PyUnicode_AsUnicodeAndSize(
);
#endif
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
/* Get the length of the Unicode object. */
PyAPI_FUNC(Py_ssize_t) PyUnicode_GetLength(
PyObject *unicode
);
+#endif
/* Get the number of Py_UNICODE units in the
string representation. */
@@ -800,6 +790,7 @@ PyAPI_FUNC(Py_ssize_t) PyUnicode_GetSize(
PyObject *unicode /* Unicode object */
);
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
/* Read a character from the string. */
PyAPI_FUNC(Py_UCS4) PyUnicode_ReadChar(
@@ -817,6 +808,7 @@ PyAPI_FUNC(int) PyUnicode_WriteChar(
Py_ssize_t index,
Py_UCS4 character
);
+#endif
#ifndef Py_LIMITED_API
/* Get the maximum ordinal for a Unicode character. */
@@ -900,7 +892,7 @@ typedef struct {
/* minimum character (default: 127, ASCII) */
Py_UCS4 min_char;
- /* If non-zero, overallocate the buffer by 25% (default: 0). */
+ /* If non-zero, overallocate the buffer (default: 0). */
unsigned char overallocate;
/* If readonly is 1, buffer is a shared string (cannot be modified)
@@ -934,6 +926,23 @@ PyAPI_FUNC(int)
_PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer,
Py_ssize_t length, Py_UCS4 maxchar);
+/* Prepare the buffer to have at least the kind KIND.
+ For example, kind=PyUnicode_2BYTE_KIND ensures that the writer will
+ support characters in range U+000-U+FFFF.
+
+ Return 0 on success, raise an exception and return -1 on error. */
+#define _PyUnicodeWriter_PrepareKind(WRITER, KIND) \
+ (assert((KIND) != PyUnicode_WCHAR_KIND), \
+ (KIND) <= (WRITER)->kind \
+ ? 0 \
+ : _PyUnicodeWriter_PrepareKindInternal((WRITER), (KIND)))
+
+/* Don't call this function directly, use the _PyUnicodeWriter_PrepareKind()
+ macro instead. */
+PyAPI_FUNC(int)
+_PyUnicodeWriter_PrepareKindInternal(_PyUnicodeWriter *writer,
+ enum PyUnicode_Kind kind);
+
/* Append a Unicode character.
Return 0 on success, raise an exception and return -1 on error. */
PyAPI_FUNC(int)
@@ -1170,22 +1179,30 @@ PyAPI_FUNC(PyObject*) PyUnicode_Decode(
);
/* Decode a Unicode object unicode and return the result as Python
- object. */
+ object.
+
+ This API is DEPRECATED. The only supported standard encoding is rot13.
+ Use PyCodec_Decode() to decode with rot13 and non-standard codecs
+ that decode from str. */
PyAPI_FUNC(PyObject*) PyUnicode_AsDecodedObject(
PyObject *unicode, /* Unicode object */
const char *encoding, /* encoding */
const char *errors /* error handling */
- );
+ ) Py_DEPRECATED(3.6);
/* Decode a Unicode object unicode and return the result as Unicode
- object. */
+ object.
+
+ This API is DEPRECATED. The only supported standard encoding is rot13.
+ Use PyCodec_Decode() to decode with rot13 and non-standard codecs
+ that decode from str to str. */
PyAPI_FUNC(PyObject*) PyUnicode_AsDecodedUnicode(
PyObject *unicode, /* Unicode object */
const char *encoding, /* encoding */
const char *errors /* error handling */
- );
+ ) Py_DEPRECATED(3.6);
/* Encodes a Py_UNICODE buffer of the given size and returns a
Python string object. */
@@ -1200,13 +1217,18 @@ PyAPI_FUNC(PyObject*) PyUnicode_Encode(
#endif
/* Encodes a Unicode object and returns the result as Python
- object. */
+ object.
+
+ This API is DEPRECATED. It is superceeded by PyUnicode_AsEncodedString()
+ since all standard encodings (except rot13) encode str to bytes.
+ Use PyCodec_Encode() for encoding with rot13 and non-standard codecs
+ that encode form str to non-bytes. */
PyAPI_FUNC(PyObject*) PyUnicode_AsEncodedObject(
PyObject *unicode, /* Unicode object */
const char *encoding, /* encoding */
const char *errors /* error handling */
- );
+ ) Py_DEPRECATED(3.6);
/* Encodes a Unicode object and returns the result as Python string
object. */
@@ -1218,13 +1240,17 @@ PyAPI_FUNC(PyObject*) PyUnicode_AsEncodedString(
);
/* Encodes a Unicode object and returns the result as Unicode
- object. */
+ object.
+
+ This API is DEPRECATED. The only supported standard encodings is rot13.
+ Use PyCodec_Encode() to encode with rot13 and non-standard codecs
+ that encode from str to str. */
PyAPI_FUNC(PyObject*) PyUnicode_AsEncodedUnicode(
PyObject *unicode, /* Unicode object */
const char *encoding, /* encoding */
const char *errors /* error handling */
- );
+ ) Py_DEPRECATED(3.6);
/* Build an encoding map. */
@@ -1468,6 +1494,19 @@ PyAPI_FUNC(PyObject*) PyUnicode_DecodeUnicodeEscape(
const char *errors /* error handling */
);
+#ifndef Py_LIMITED_API
+/* Helper for PyUnicode_DecodeUnicodeEscape that detects invalid escape
+ chars. */
+PyAPI_FUNC(PyObject*) _PyUnicode_DecodeUnicodeEscape(
+ const char *string, /* Unicode-Escape encoded string */
+ Py_ssize_t length, /* size of string */
+ const char *errors, /* error handling */
+ const char **first_invalid_escape /* on return, points to first
+ invalid escaped char in
+ string. */
+);
+#endif
+
PyAPI_FUNC(PyObject*) PyUnicode_AsUnicodeEscapeString(
PyObject *unicode /* Unicode object */
);
@@ -1640,13 +1679,13 @@ PyAPI_FUNC(PyObject *) PyUnicode_TranslateCharmap(
);
#endif
-#ifdef HAVE_MBCS
+#ifdef MS_WINDOWS
/* --- MBCS codecs for Windows -------------------------------------------- */
PyAPI_FUNC(PyObject*) PyUnicode_DecodeMBCS(
const char *string, /* MBCS encoded string */
- Py_ssize_t length, /* size of string */
+ Py_ssize_t length, /* size of string */
const char *errors /* error handling */
);
@@ -1657,6 +1696,7 @@ PyAPI_FUNC(PyObject*) PyUnicode_DecodeMBCSStateful(
Py_ssize_t *consumed /* bytes consumed */
);
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
PyAPI_FUNC(PyObject*) PyUnicode_DecodeCodePageStateful(
int code_page, /* code page number */
const char *string, /* encoded string */
@@ -1664,6 +1704,7 @@ PyAPI_FUNC(PyObject*) PyUnicode_DecodeCodePageStateful(
const char *errors, /* error handling */
Py_ssize_t *consumed /* bytes consumed */
);
+#endif
PyAPI_FUNC(PyObject*) PyUnicode_AsMBCSString(
PyObject *unicode /* Unicode object */
@@ -1677,13 +1718,15 @@ PyAPI_FUNC(PyObject*) PyUnicode_EncodeMBCS(
);
#endif
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
PyAPI_FUNC(PyObject*) PyUnicode_EncodeCodePage(
int code_page, /* code page number */
PyObject *unicode, /* Unicode object */
const char *errors /* error handling */
);
+#endif
-#endif /* HAVE_MBCS */
+#endif /* MS_WINDOWS */
/* --- Decimal Encoder ---------------------------------------------------- */
@@ -1744,6 +1787,7 @@ PyAPI_FUNC(PyObject*) _PyUnicode_TransformDecimalAndSpaceToASCII(
/* --- Locale encoding --------------------------------------------------- */
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
/* Decode a string from the current locale encoding. The decoder is strict if
*surrogateescape* is equal to zero, otherwise it uses the 'surrogateescape'
error handler (PEP 383) to escape undecodable bytes. If a byte sequence can
@@ -1773,6 +1817,7 @@ PyAPI_FUNC(PyObject*) PyUnicode_EncodeLocale(
PyObject *unicode,
const char *errors
);
+#endif
/* --- File system encoding ---------------------------------------------- */
@@ -1938,6 +1983,14 @@ PyAPI_FUNC(PyObject*) PyUnicode_Join(
PyObject *seq /* Sequence object */
);
+#ifndef Py_LIMITED_API
+PyAPI_FUNC(PyObject *) _PyUnicode_JoinArray(
+ PyObject *separator,
+ PyObject **items,
+ Py_ssize_t seqlen
+ );
+#endif /* Py_LIMITED_API */
+
/* Return 1 if substr matches str[start:end] at the given tail end, 0
otherwise. */
@@ -1961,6 +2014,7 @@ PyAPI_FUNC(Py_ssize_t) PyUnicode_Find(
int direction /* Find direction: +1 forward, -1 backward */
);
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
/* Like PyUnicode_Find, but search for single character only. */
PyAPI_FUNC(Py_ssize_t) PyUnicode_FindChar(
PyObject *str,
@@ -1969,6 +2023,7 @@ PyAPI_FUNC(Py_ssize_t) PyUnicode_FindChar(
Py_ssize_t end,
int direction
);
+#endif
/* Count the number of occurrences of substr in str[start:end]. */
@@ -2000,17 +2055,8 @@ PyAPI_FUNC(int) PyUnicode_Compare(
);
#ifndef Py_LIMITED_API
-/* Compare a string with an identifier and return -1, 0, 1 for less than,
- equal, and greater than, respectively.
- Raise an exception and return -1 on error. */
-
-PyAPI_FUNC(int) _PyUnicode_CompareWithId(
- PyObject *left, /* Left string */
- _Py_Identifier *right /* Right identifier */
- );
-
/* Test whether a unicode is equal to ASCII identifier. Return 1 if true,
- 0 otherwise. Return 0 if any argument contains non-ASCII characters.
+ 0 otherwise. The right argument must be ASCII identifier.
Any error occurs inside will be cleared before return. */
PyAPI_FUNC(int) _PyUnicode_EqualToASCIIId(
@@ -2032,7 +2078,7 @@ PyAPI_FUNC(int) PyUnicode_CompareWithASCIIString(
#ifndef Py_LIMITED_API
/* Test whether a unicode is equal to ASCII string. Return 1 if true,
- 0 otherwise. Return 0 if any argument contains non-ASCII characters.
+ 0 otherwise. The right argument must be ASCII-encoded string.
Any error occurs inside will be cleared before return. */
PyAPI_FUNC(int) _PyUnicode_EqualToASCIIString(
@@ -2274,11 +2320,17 @@ PyAPI_FUNC(int) _PyUnicode_CheckConsistency(
int check_content);
#endif
+#ifndef Py_LIMITED_API
/* Return an interned Unicode object for an Identifier; may fail if there is no memory.*/
PyAPI_FUNC(PyObject*) _PyUnicode_FromId(_Py_Identifier*);
/* Clear all static strings. */
PyAPI_FUNC(void) _PyUnicode_ClearStaticStrings(void);
+/* Fast equality check when the inputs are known to be exact unicode types
+ and where the hash values are equal (i.e. a very probable match) */
+PyAPI_FUNC(int) _PyUnicode_EQ(PyObject *, PyObject *);
+#endif /* !Py_LIMITED_API */
+
#ifdef __cplusplus
}
#endif
diff --git a/Include/warnings.h b/Include/warnings.h
index effb9fad..a3f83ff 100644
--- a/Include/warnings.h
+++ b/Include/warnings.h
@@ -17,6 +17,15 @@ PyAPI_FUNC(int) PyErr_WarnFormat(
Py_ssize_t stack_level,
const char *format, /* ASCII-encoded string */
...);
+
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03060000
+/* Emit a ResourceWarning warning */
+PyAPI_FUNC(int) PyErr_ResourceWarning(
+ PyObject *source,
+ Py_ssize_t stack_level,
+ const char *format, /* ASCII-encoded string */
+ ...);
+#endif
#ifndef Py_LIMITED_API
PyAPI_FUNC(int) PyErr_WarnExplicitObject(
PyObject *category,
diff --git a/Lib/_collections_abc.py b/Lib/_collections_abc.py
index 8bebd69..b172f3f 100644
--- a/Lib/_collections_abc.py
+++ b/Lib/_collections_abc.py
@@ -9,9 +9,10 @@ Unit tests are in test_collections.
from abc import ABCMeta, abstractmethod
import sys
-__all__ = ["Awaitable", "Coroutine", "AsyncIterable", "AsyncIterator",
- "Hashable", "Iterable", "Iterator", "Generator",
- "Sized", "Container", "Callable",
+__all__ = ["Awaitable", "Coroutine",
+ "AsyncIterable", "AsyncIterator", "AsyncGenerator",
+ "Hashable", "Iterable", "Iterator", "Generator", "Reversible",
+ "Sized", "Container", "Callable", "Collection",
"Set", "MutableSet",
"Mapping", "MutableMapping",
"MappingView", "KeysView", "ItemsView", "ValuesView",
@@ -59,10 +60,27 @@ _coro = _coro()
coroutine = type(_coro)
_coro.close() # Prevent ResourceWarning
del _coro
+## asynchronous generator ##
+async def _ag(): yield
+_ag = _ag()
+async_generator = type(_ag)
+del _ag
### ONE-TRICK PONIES ###
+def _check_methods(C, *methods):
+ mro = C.__mro__
+ for method in methods:
+ for B in mro:
+ if method in B.__dict__:
+ if B.__dict__[method] is None:
+ return NotImplemented
+ break
+ else:
+ return NotImplemented
+ return True
+
class Hashable(metaclass=ABCMeta):
__slots__ = ()
@@ -74,11 +92,7 @@ class Hashable(metaclass=ABCMeta):
@classmethod
def __subclasshook__(cls, C):
if cls is Hashable:
- for B in C.__mro__:
- if "__hash__" in B.__dict__:
- if B.__dict__["__hash__"]:
- return True
- break
+ return _check_methods(C, "__hash__")
return NotImplemented
@@ -93,11 +107,7 @@ class Awaitable(metaclass=ABCMeta):
@classmethod
def __subclasshook__(cls, C):
if cls is Awaitable:
- for B in C.__mro__:
- if "__await__" in B.__dict__:
- if B.__dict__["__await__"]:
- return True
- break
+ return _check_methods(C, "__await__")
return NotImplemented
@@ -138,14 +148,7 @@ class Coroutine(Awaitable):
@classmethod
def __subclasshook__(cls, C):
if cls is Coroutine:
- mro = C.__mro__
- for method in ('__await__', 'send', 'throw', 'close'):
- for base in mro:
- if method in base.__dict__:
- break
- else:
- return NotImplemented
- return True
+ return _check_methods(C, '__await__', 'send', 'throw', 'close')
return NotImplemented
@@ -163,8 +166,7 @@ class AsyncIterable(metaclass=ABCMeta):
@classmethod
def __subclasshook__(cls, C):
if cls is AsyncIterable:
- if any("__aiter__" in B.__dict__ for B in C.__mro__):
- return True
+ return _check_methods(C, "__aiter__")
return NotImplemented
@@ -183,12 +185,61 @@ class AsyncIterator(AsyncIterable):
@classmethod
def __subclasshook__(cls, C):
if cls is AsyncIterator:
- if (any("__anext__" in B.__dict__ for B in C.__mro__) and
- any("__aiter__" in B.__dict__ for B in C.__mro__)):
- return True
+ return _check_methods(C, "__anext__", "__aiter__")
return NotImplemented
+class AsyncGenerator(AsyncIterator):
+
+ __slots__ = ()
+
+ async def __anext__(self):
+ """Return the next item from the asynchronous generator.
+ When exhausted, raise StopAsyncIteration.
+ """
+ return await self.asend(None)
+
+ @abstractmethod
+ async def asend(self, value):
+ """Send a value into the asynchronous generator.
+ Return next yielded value or raise StopAsyncIteration.
+ """
+ raise StopAsyncIteration
+
+ @abstractmethod
+ async def athrow(self, typ, val=None, tb=None):
+ """Raise an exception in the asynchronous generator.
+ Return next yielded value or raise StopAsyncIteration.
+ """
+ if val is None:
+ if tb is None:
+ raise typ
+ val = typ()
+ if tb is not None:
+ val = val.with_traceback(tb)
+ raise val
+
+ async def aclose(self):
+ """Raise GeneratorExit inside coroutine.
+ """
+ try:
+ await self.athrow(GeneratorExit)
+ except (GeneratorExit, StopAsyncIteration):
+ pass
+ else:
+ raise RuntimeError("asynchronous generator ignored GeneratorExit")
+
+ @classmethod
+ def __subclasshook__(cls, C):
+ if cls is AsyncGenerator:
+ return _check_methods(C, '__aiter__', '__anext__',
+ 'asend', 'athrow', 'aclose')
+ return NotImplemented
+
+
+AsyncGenerator.register(async_generator)
+
+
class Iterable(metaclass=ABCMeta):
__slots__ = ()
@@ -201,8 +252,7 @@ class Iterable(metaclass=ABCMeta):
@classmethod
def __subclasshook__(cls, C):
if cls is Iterable:
- if any("__iter__" in B.__dict__ for B in C.__mro__):
- return True
+ return _check_methods(C, "__iter__")
return NotImplemented
@@ -221,9 +271,7 @@ class Iterator(Iterable):
@classmethod
def __subclasshook__(cls, C):
if cls is Iterator:
- if (any("__next__" in B.__dict__ for B in C.__mro__) and
- any("__iter__" in B.__dict__ for B in C.__mro__)):
- return True
+ return _check_methods(C, '__iter__', '__next__')
return NotImplemented
Iterator.register(bytes_iterator)
@@ -242,6 +290,22 @@ Iterator.register(tuple_iterator)
Iterator.register(zip_iterator)
+class Reversible(Iterable):
+
+ __slots__ = ()
+
+ @abstractmethod
+ def __reversed__(self):
+ while False:
+ yield None
+
+ @classmethod
+ def __subclasshook__(cls, C):
+ if cls is Reversible:
+ return _check_methods(C, "__reversed__", "__iter__")
+ return NotImplemented
+
+
class Generator(Iterator):
__slots__ = ()
@@ -285,17 +349,10 @@ class Generator(Iterator):
@classmethod
def __subclasshook__(cls, C):
if cls is Generator:
- mro = C.__mro__
- for method in ('__iter__', '__next__', 'send', 'throw', 'close'):
- for base in mro:
- if method in base.__dict__:
- break
- else:
- return NotImplemented
- return True
+ return _check_methods(C, '__iter__', '__next__',
+ 'send', 'throw', 'close')
return NotImplemented
-
Generator.register(generator)
@@ -310,8 +367,7 @@ class Sized(metaclass=ABCMeta):
@classmethod
def __subclasshook__(cls, C):
if cls is Sized:
- if any("__len__" in B.__dict__ for B in C.__mro__):
- return True
+ return _check_methods(C, "__len__")
return NotImplemented
@@ -326,10 +382,18 @@ class Container(metaclass=ABCMeta):
@classmethod
def __subclasshook__(cls, C):
if cls is Container:
- if any("__contains__" in B.__dict__ for B in C.__mro__):
- return True
+ return _check_methods(C, "__contains__")
return NotImplemented
+class Collection(Sized, Iterable, Container):
+
+ __slots__ = ()
+
+ @classmethod
+ def __subclasshook__(cls, C):
+ if cls is Collection:
+ return _check_methods(C, "__len__", "__iter__", "__contains__")
+ return NotImplemented
class Callable(metaclass=ABCMeta):
@@ -342,15 +406,14 @@ class Callable(metaclass=ABCMeta):
@classmethod
def __subclasshook__(cls, C):
if cls is Callable:
- if any("__call__" in B.__dict__ for B in C.__mro__):
- return True
+ return _check_methods(C, "__call__")
return NotImplemented
### SETS ###
-class Set(Sized, Iterable, Container):
+class Set(Collection):
"""A set is a finite, iterable container.
@@ -575,7 +638,7 @@ MutableSet.register(set)
### MAPPINGS ###
-class Mapping(Sized, Iterable, Container):
+class Mapping(Collection):
__slots__ = ()
@@ -623,6 +686,8 @@ class Mapping(Sized, Iterable, Container):
return NotImplemented
return dict(self.items()) == dict(other.items())
+ __reversed__ = None
+
Mapping.register(mappingproxy)
@@ -672,7 +737,7 @@ class ItemsView(MappingView, Set):
except KeyError:
return False
else:
- return v == value
+ return v is value or v == value
def __iter__(self):
for key in self._mapping:
@@ -687,7 +752,8 @@ class ValuesView(MappingView):
def __contains__(self, value):
for key in self._mapping:
- if value == self._mapping[key]:
+ v = self._mapping[key]
+ if v is value or v == value:
return True
return False
@@ -796,7 +862,7 @@ MutableMapping.register(dict)
### SEQUENCES ###
-class Sequence(Sized, Iterable, Container):
+class Sequence(Reversible, Collection):
"""All the operations on a read-only sequence.
@@ -822,7 +888,7 @@ class Sequence(Sized, Iterable, Container):
def __contains__(self, value):
for v in self:
- if v == value:
+ if v is value or v == value:
return True
return False
diff --git a/Lib/_compat_pickle.py b/Lib/_compat_pickle.py
index c0e0443..f68496a 100644
--- a/Lib/_compat_pickle.py
+++ b/Lib/_compat_pickle.py
@@ -242,3 +242,10 @@ PYTHON3_OSERROR_EXCEPTIONS = (
for excname in PYTHON3_OSERROR_EXCEPTIONS:
REVERSE_NAME_MAPPING[('builtins', excname)] = ('exceptions', 'OSError')
+
+PYTHON3_IMPORTERROR_EXCEPTIONS = (
+ 'ModuleNotFoundError',
+)
+
+for excname in PYTHON3_IMPORTERROR_EXCEPTIONS:
+ REVERSE_NAME_MAPPING[('builtins', excname)] = ('exceptions', 'ImportError')
diff --git a/Lib/_osx_support.py b/Lib/_osx_support.py
index 13fcd8b..eadf06f 100644
--- a/Lib/_osx_support.py
+++ b/Lib/_osx_support.py
@@ -210,7 +210,7 @@ def _remove_universal_flags(_config_vars):
# Do not alter a config var explicitly overridden by env var
if cv in _config_vars and cv not in os.environ:
flags = _config_vars[cv]
- flags = re.sub('-arch\s+\w+\s', ' ', flags, re.ASCII)
+ flags = re.sub(r'-arch\s+\w+\s', ' ', flags, re.ASCII)
flags = re.sub('-isysroot [^ \t]*', ' ', flags)
_save_modified_value(_config_vars, cv, flags)
@@ -232,7 +232,7 @@ def _remove_unsupported_archs(_config_vars):
if 'CC' in os.environ:
return _config_vars
- if re.search('-arch\s+ppc', _config_vars['CFLAGS']) is not None:
+ if re.search(r'-arch\s+ppc', _config_vars['CFLAGS']) is not None:
# NOTE: Cannot use subprocess here because of bootstrap
# issues when building Python itself
status = os.system(
@@ -251,7 +251,7 @@ def _remove_unsupported_archs(_config_vars):
for cv in _UNIVERSAL_CONFIG_VARS:
if cv in _config_vars and cv not in os.environ:
flags = _config_vars[cv]
- flags = re.sub('-arch\s+ppc\w*\s', ' ', flags)
+ flags = re.sub(r'-arch\s+ppc\w*\s', ' ', flags)
_save_modified_value(_config_vars, cv, flags)
return _config_vars
@@ -267,7 +267,7 @@ def _override_all_archs(_config_vars):
for cv in _UNIVERSAL_CONFIG_VARS:
if cv in _config_vars and '-arch' in _config_vars[cv]:
flags = _config_vars[cv]
- flags = re.sub('-arch\s+\w+\s', ' ', flags)
+ flags = re.sub(r'-arch\s+\w+\s', ' ', flags)
flags = flags + ' ' + arch
_save_modified_value(_config_vars, cv, flags)
@@ -465,7 +465,7 @@ def get_platform_osx(_config_vars, osname, release, machine):
machine = 'fat'
- archs = re.findall('-arch\s+(\S+)', cflags)
+ archs = re.findall(r'-arch\s+(\S+)', cflags)
archs = tuple(sorted(set(archs)))
if len(archs) == 1:
diff --git a/Lib/_pydecimal.py b/Lib/_pydecimal.py
index a5ea340..0b40928 100644
--- a/Lib/_pydecimal.py
+++ b/Lib/_pydecimal.py
@@ -148,7 +148,7 @@ __xname__ = __name__ # sys.modules lookup (--without-threads)
__name__ = 'decimal' # For pickling
__version__ = '1.70' # Highest version of the spec this complies with
# See http://speleotrove.com/decimal/
-__libmpdec_version__ = "2.4.1" # compatible libmpdec version
+__libmpdec_version__ = "2.4.2" # compatible libmpdec version
import math as _math
import numbers as _numbers
@@ -589,7 +589,7 @@ class Decimal(object):
# From a string
# REs insist on real strings, so we can too.
if isinstance(value, str):
- m = _parser(value.strip())
+ m = _parser(value.strip().replace("_", ""))
if m is None:
if context is None:
context = getcontext()
@@ -1010,6 +1010,56 @@ class Decimal(object):
"""
return DecimalTuple(self._sign, tuple(map(int, self._int)), self._exp)
+ def as_integer_ratio(self):
+ """Express a finite Decimal instance in the form n / d.
+
+ Returns a pair (n, d) of integers. When called on an infinity
+ or NaN, raises OverflowError or ValueError respectively.
+
+ >>> Decimal('3.14').as_integer_ratio()
+ (157, 50)
+ >>> Decimal('-123e5').as_integer_ratio()
+ (-12300000, 1)
+ >>> Decimal('0.00').as_integer_ratio()
+ (0, 1)
+
+ """
+ if self._is_special:
+ if self.is_nan():
+ raise ValueError("cannot convert NaN to integer ratio")
+ else:
+ raise OverflowError("cannot convert Infinity to integer ratio")
+
+ if not self:
+ return 0, 1
+
+ # Find n, d in lowest terms such that abs(self) == n / d;
+ # we'll deal with the sign later.
+ n = int(self._int)
+ if self._exp >= 0:
+ # self is an integer.
+ n, d = n * 10**self._exp, 1
+ else:
+ # Find d2, d5 such that abs(self) = n / (2**d2 * 5**d5).
+ d5 = -self._exp
+ while d5 > 0 and n % 5 == 0:
+ n //= 5
+ d5 -= 1
+
+ # (n & -n).bit_length() - 1 counts trailing zeros in binary
+ # representation of n (provided n is nonzero).
+ d2 = -self._exp
+ shift2 = min((n & -n).bit_length() - 1, d2)
+ if shift2:
+ n >>= shift2
+ d2 -= shift2
+
+ d = 5**d5 << d2
+
+ if self._sign:
+ n = -n
+ return n, d
+
def __repr__(self):
"""Represents the number as an instance of Decimal."""
# Invariant: eval(repr(d)) == d
@@ -4075,7 +4125,7 @@ class Context(object):
This will make it round up for that operation.
"""
rounding = self.rounding
- self.rounding= type
+ self.rounding = type
return rounding
def create_decimal(self, num='0'):
@@ -4084,10 +4134,10 @@ class Context(object):
This method implements the to-number operation of the
IBM Decimal specification."""
- if isinstance(num, str) and num != num.strip():
+ if isinstance(num, str) and (num != num.strip() or '_' in num):
return self._raise_error(ConversionSyntax,
- "no trailing or leading whitespace is "
- "permitted.")
+ "trailing or leading whitespace and "
+ "underscores are not permitted.")
d = Decimal(num, context=self)
if d._isnan() and len(d._int) > self.prec - self.clamp:
diff --git a/Lib/_pyio.py b/Lib/_pyio.py
index f2fe447..2ebfb05 100644
--- a/Lib/_pyio.py
+++ b/Lib/_pyio.py
@@ -6,7 +6,6 @@ import os
import abc
import codecs
import errno
-import array
import stat
import sys
# Import _thread instead of threading to reduce startup cost
@@ -161,6 +160,8 @@ def open(file, mode="r", buffering=-1, encoding=None, errors=None,
opened in a text mode, and for bytes a BytesIO can be used like a file
opened in a binary mode.
"""
+ if not isinstance(file, int):
+ file = os.fspath(file)
if not isinstance(file, (str, bytes, int)):
raise TypeError("invalid file: %r" % file)
if not isinstance(mode, str):
@@ -182,8 +183,8 @@ def open(file, mode="r", buffering=-1, encoding=None, errors=None,
text = "t" in modes
binary = "b" in modes
if "U" in modes:
- if creating or writing or appending:
- raise ValueError("can't use U and writing mode at once")
+ if creating or writing or appending or updating:
+ raise ValueError("mode U cannot be combined with 'x', 'w', 'a', or '+'")
import warnings
warnings.warn("'U' mode is deprecated",
DeprecationWarning, 2)
@@ -1516,7 +1517,7 @@ class FileIO(RawIOBase):
if self._fd >= 0 and self._closefd and not self.closed:
import warnings
warnings.warn('unclosed file %r' % (self,), ResourceWarning,
- stacklevel=2)
+ stacklevel=2, source=self)
self.close()
def __getstate__(self):
diff --git a/Lib/_strptime.py b/Lib/_strptime.py
index f84227b..fe94361 100644
--- a/Lib/_strptime.py
+++ b/Lib/_strptime.py
@@ -199,12 +199,15 @@ class TimeRE(dict):
'f': r"(?P<f>[0-9]{1,6})",
'H': r"(?P<H>2[0-3]|[0-1]\d|\d)",
'I': r"(?P<I>1[0-2]|0[1-9]|[1-9])",
+ 'G': r"(?P<G>\d\d\d\d)",
'j': r"(?P<j>36[0-6]|3[0-5]\d|[1-2]\d\d|0[1-9]\d|00[1-9]|[1-9]\d|0[1-9]|[1-9])",
'm': r"(?P<m>1[0-2]|0[1-9]|[1-9])",
'M': r"(?P<M>[0-5]\d|\d)",
'S': r"(?P<S>6[0-1]|[0-5]\d|\d)",
'U': r"(?P<U>5[0-3]|[0-4]\d|\d)",
'w': r"(?P<w>[0-6])",
+ 'u': r"(?P<u>[1-7])",
+ 'V': r"(?P<V>5[0-3]|0[1-9]|[1-4]\d|\d)",
# W is set below by using 'U'
'y': r"(?P<y>\d\d)",
#XXX: Does 'Y' need to worry about having less or more than
@@ -299,6 +302,22 @@ def _calc_julian_from_U_or_W(year, week_of_year, day_of_week, week_starts_Mon):
return 1 + days_to_week + day_of_week
+def _calc_julian_from_V(iso_year, iso_week, iso_weekday):
+ """Calculate the Julian day based on the ISO 8601 year, week, and weekday.
+ ISO weeks start on Mondays, with week 01 being the week containing 4 Jan.
+ ISO week days range from 1 (Monday) to 7 (Sunday).
+ """
+ correction = datetime_date(iso_year, 1, 4).isoweekday() + 3
+ ordinal = (iso_week * 7) + iso_weekday - correction
+ # ordinal may be negative or 0 now, which means the date is in the previous
+ # calendar year
+ if ordinal < 1:
+ ordinal += datetime_date(iso_year, 1, 1).toordinal()
+ iso_year -= 1
+ ordinal -= datetime_date(iso_year, 1, 1).toordinal()
+ return iso_year, ordinal
+
+
def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
"""Return a 2-tuple consisting of a time struct and an int containing
the number of microseconds based on the input string and the
@@ -345,15 +364,15 @@ def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
raise ValueError("unconverted data remains: %s" %
data_string[found.end():])
- year = None
+ iso_year = year = None
month = day = 1
hour = minute = second = fraction = 0
tz = -1
tzoffset = None
# Default to -1 to signify that values not known; not critical to have,
# though
- week_of_year = -1
- week_of_year_start = -1
+ iso_week = week_of_year = None
+ week_of_year_start = None
# weekday and julian defaulted to None so as to signal need to calculate
# values
weekday = julian = None
@@ -375,6 +394,8 @@ def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
year += 1900
elif group_key == 'Y':
year = int(found_dict['Y'])
+ elif group_key == 'G':
+ iso_year = int(found_dict['G'])
elif group_key == 'm':
month = int(found_dict['m'])
elif group_key == 'B':
@@ -420,6 +441,9 @@ def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
weekday = 6
else:
weekday -= 1
+ elif group_key == 'u':
+ weekday = int(found_dict['u'])
+ weekday -= 1
elif group_key == 'j':
julian = int(found_dict['j'])
elif group_key in ('U', 'W'):
@@ -430,6 +454,8 @@ def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
else:
# W starts week on Monday.
week_of_year_start = 0
+ elif group_key == 'V':
+ iso_week = int(found_dict['V'])
elif group_key == 'z':
z = found_dict['z']
tzoffset = int(z[1:3]) * 60 + int(z[3:5])
@@ -450,32 +476,61 @@ def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
else:
tz = value
break
+ # Deal with the cases where ambiguities arize
+ # don't assume default values for ISO week/year
+ if year is None and iso_year is not None:
+ if iso_week is None or weekday is None:
+ raise ValueError("ISO year directive '%G' must be used with "
+ "the ISO week directive '%V' and a weekday "
+ "directive ('%A', '%a', '%w', or '%u').")
+ if julian is not None:
+ raise ValueError("Day of the year directive '%j' is not "
+ "compatible with ISO year directive '%G'. "
+ "Use '%Y' instead.")
+ elif week_of_year is None and iso_week is not None:
+ if weekday is None:
+ raise ValueError("ISO week directive '%V' must be used with "
+ "the ISO year directive '%G' and a weekday "
+ "directive ('%A', '%a', '%w', or '%u').")
+ else:
+ raise ValueError("ISO week directive '%V' is incompatible with "
+ "the year directive '%Y'. Use the ISO year '%G' "
+ "instead.")
+
leap_year_fix = False
if year is None and month == 2 and day == 29:
year = 1904 # 1904 is first leap year of 20th century
leap_year_fix = True
elif year is None:
year = 1900
+
+
# If we know the week of the year and what day of that week, we can figure
# out the Julian day of the year.
- if julian is None and week_of_year != -1 and weekday is not None:
- week_starts_Mon = True if week_of_year_start == 0 else False
- julian = _calc_julian_from_U_or_W(year, week_of_year, weekday,
- week_starts_Mon)
- if julian <= 0:
+ if julian is None and weekday is not None:
+ if week_of_year is not None:
+ week_starts_Mon = True if week_of_year_start == 0 else False
+ julian = _calc_julian_from_U_or_W(year, week_of_year, weekday,
+ week_starts_Mon)
+ elif iso_year is not None and iso_week is not None:
+ year, julian = _calc_julian_from_V(iso_year, iso_week, weekday + 1)
+ if julian is not None and julian <= 0:
year -= 1
yday = 366 if calendar.isleap(year) else 365
julian += yday
- # Cannot pre-calculate datetime_date() since can change in Julian
- # calculation and thus could have different value for the day of the week
- # calculation.
+
if julian is None:
+ # Cannot pre-calculate datetime_date() since can change in Julian
+ # calculation and thus could have different value for the day of
+ # the week calculation.
# Need to add 1 to result since first day of the year is 1, not 0.
julian = datetime_date(year, month, day).toordinal() - \
datetime_date(year, 1, 1).toordinal() + 1
- else: # Assume that if they bothered to include Julian day it will
- # be accurate.
- datetime_result = datetime_date.fromordinal((julian - 1) + datetime_date(year, 1, 1).toordinal())
+ else: # Assume that if they bothered to include Julian day (or if it was
+ # calculated above with year/week/weekday) it will be accurate.
+ datetime_result = datetime_date.fromordinal(
+ (julian - 1) +
+ datetime_date(year, 1, 1).toordinal())
year = datetime_result.year
month = datetime_result.month
day = datetime_result.day
diff --git a/Lib/aifc.py b/Lib/aifc.py
index 7ebdbeb..692d0bf 100644
--- a/Lib/aifc.py
+++ b/Lib/aifc.py
@@ -257,6 +257,15 @@ from collections import namedtuple
_aifc_params = namedtuple('_aifc_params',
'nchannels sampwidth framerate nframes comptype compname')
+_aifc_params.nchannels.__doc__ = 'Number of audio channels (1 for mono, 2 for stereo)'
+_aifc_params.sampwidth.__doc__ = 'Sample width in bytes'
+_aifc_params.framerate.__doc__ = 'Sampling frequency'
+_aifc_params.nframes.__doc__ = 'Number of audio frames'
+_aifc_params.comptype.__doc__ = 'Compression type ("NONE" for AIFF files)'
+_aifc_params.compname.__doc__ = ("""\
+A human-readable version of the compression type
+('not compressed' for AIFF files)""")
+
class Aifc_read:
# Variables used in this class:
diff --git a/Lib/argparse.py b/Lib/argparse.py
index 9a06719..209b4e9 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -118,10 +118,16 @@ class _AttributeHolder(object):
def __repr__(self):
type_name = type(self).__name__
arg_strings = []
+ star_args = {}
for arg in self._get_args():
arg_strings.append(repr(arg))
for name, value in self._get_kwargs():
- arg_strings.append('%s=%r' % (name, value))
+ if name.isidentifier():
+ arg_strings.append('%s=%r' % (name, value))
+ else:
+ star_args[name] = value
+ if star_args:
+ arg_strings.append('**%s' % repr(star_args))
return '%s(%s)' % (type_name, ', '.join(arg_strings))
def _get_kwargs(self):
@@ -204,8 +210,6 @@ class HelpFormatter(object):
if self.parent is not None:
self.formatter._indent()
join = self.formatter._join_parts
- for func, args in self.items:
- func(*args)
item_help = join([func(*args) for func, args in self.items])
if self.parent is not None:
self.formatter._dedent()
diff --git a/Lib/ast.py b/Lib/ast.py
index 0170472..156a1f2 100644
--- a/Lib/ast.py
+++ b/Lib/ast.py
@@ -35,6 +35,8 @@ def parse(source, filename='<unknown>', mode='exec'):
return compile(source, filename, mode, PyCF_ONLY_AST)
+_NUM_TYPES = (int, float, complex)
+
def literal_eval(node_or_string):
"""
Safely evaluate an expression node or a string containing a Python
@@ -47,7 +49,9 @@ def literal_eval(node_or_string):
if isinstance(node_or_string, Expression):
node_or_string = node_or_string.body
def _convert(node):
- if isinstance(node, (Str, Bytes)):
+ if isinstance(node, Constant):
+ return node.value
+ elif isinstance(node, (Str, Bytes)):
return node.s
elif isinstance(node, Num):
return node.n
@@ -62,24 +66,21 @@ def literal_eval(node_or_string):
in zip(node.keys, node.values))
elif isinstance(node, NameConstant):
return node.value
- elif isinstance(node, UnaryOp) and \
- isinstance(node.op, (UAdd, USub)) and \
- isinstance(node.operand, (Num, UnaryOp, BinOp)):
+ elif isinstance(node, UnaryOp) and isinstance(node.op, (UAdd, USub)):
operand = _convert(node.operand)
- if isinstance(node.op, UAdd):
- return + operand
- else:
- return - operand
- elif isinstance(node, BinOp) and \
- isinstance(node.op, (Add, Sub)) and \
- isinstance(node.right, (Num, UnaryOp, BinOp)) and \
- isinstance(node.left, (Num, UnaryOp, BinOp)):
+ if isinstance(operand, _NUM_TYPES):
+ if isinstance(node.op, UAdd):
+ return + operand
+ else:
+ return - operand
+ elif isinstance(node, BinOp) and isinstance(node.op, (Add, Sub)):
left = _convert(node.left)
right = _convert(node.right)
- if isinstance(node.op, Add):
- return left + right
- else:
- return left - right
+ if isinstance(left, _NUM_TYPES) and isinstance(right, _NUM_TYPES):
+ if isinstance(node.op, Add):
+ return left + right
+ else:
+ return left - right
raise ValueError('malformed node or string: ' + repr(node))
return _convert(node_or_string)
@@ -196,12 +197,19 @@ def get_docstring(node, clean=True):
"""
if not isinstance(node, (AsyncFunctionDef, FunctionDef, ClassDef, Module)):
raise TypeError("%r can't have docstrings" % node.__class__.__name__)
- if node.body and isinstance(node.body[0], Expr) and \
- isinstance(node.body[0].value, Str):
- if clean:
- import inspect
- return inspect.cleandoc(node.body[0].value.s)
- return node.body[0].value.s
+ if not(node.body and isinstance(node.body[0], Expr)):
+ return
+ node = node.body[0].value
+ if isinstance(node, Str):
+ text = node.s
+ elif isinstance(node, Constant) and isinstance(node.value, str):
+ text = node.value
+ else:
+ return
+ if clean:
+ import inspect
+ text = inspect.cleandoc(text)
+ return text
def walk(node):
diff --git a/Lib/asynchat.py b/Lib/asynchat.py
index f728d1b..fc1146a 100644
--- a/Lib/asynchat.py
+++ b/Lib/asynchat.py
@@ -285,35 +285,6 @@ class simple_producer:
return result
-class fifo:
- def __init__(self, list=None):
- import warnings
- warnings.warn('fifo class will be removed in Python 3.6',
- DeprecationWarning, stacklevel=2)
- if not list:
- self.list = deque()
- else:
- self.list = deque(list)
-
- def __len__(self):
- return len(self.list)
-
- def is_empty(self):
- return not self.list
-
- def first(self):
- return self.list[0]
-
- def push(self, data):
- self.list.append(data)
-
- def pop(self):
- if self.list:
- return (1, self.list.popleft())
- else:
- return (0, None)
-
-
# Given 'haystack', see if any prefix of 'needle' is at its end. This
# assumes an exact match has already been checked. Return the number of
# characters matched.
diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py
index 50153f8..0df58c5 100644
--- a/Lib/asyncio/base_events.py
+++ b/Lib/asyncio/base_events.py
@@ -57,7 +57,7 @@ _FATAL_ERROR_IGNORE = (BrokenPipeError,
def _format_handle(handle):
cb = handle._callback
- if inspect.ismethod(cb) and isinstance(cb.__self__, tasks.Task):
+ if isinstance(getattr(cb, '__self__', None), tasks.Task):
# format the task
return repr(cb.__self__)
else:
@@ -505,7 +505,8 @@ class BaseEventLoop(events.AbstractEventLoop):
if compat.PY34:
def __del__(self):
if not self.is_closed():
- warnings.warn("unclosed event loop %r" % self, ResourceWarning)
+ warnings.warn("unclosed event loop %r" % self, ResourceWarning,
+ source=self)
if not self.is_running():
self.close()
diff --git a/Lib/asyncio/base_futures.py b/Lib/asyncio/base_futures.py
new file mode 100644
index 0000000..01259a0
--- /dev/null
+++ b/Lib/asyncio/base_futures.py
@@ -0,0 +1,71 @@
+__all__ = []
+
+import concurrent.futures._base
+import reprlib
+
+from . import events
+
+Error = concurrent.futures._base.Error
+CancelledError = concurrent.futures.CancelledError
+TimeoutError = concurrent.futures.TimeoutError
+
+
+class InvalidStateError(Error):
+ """The operation is not allowed in this state."""
+
+
+# States for Future.
+_PENDING = 'PENDING'
+_CANCELLED = 'CANCELLED'
+_FINISHED = 'FINISHED'
+
+
+def isfuture(obj):
+ """Check for a Future.
+
+ This returns True when obj is a Future instance or is advertising
+ itself as duck-type compatible by setting _asyncio_future_blocking.
+ See comment in Future for more details.
+ """
+ return (hasattr(obj.__class__, '_asyncio_future_blocking') and
+ obj._asyncio_future_blocking is not None)
+
+
+def _format_callbacks(cb):
+ """helper function for Future.__repr__"""
+ size = len(cb)
+ if not size:
+ cb = ''
+
+ def format_cb(callback):
+ return events._format_callback_source(callback, ())
+
+ if size == 1:
+ cb = format_cb(cb[0])
+ elif size == 2:
+ cb = '{}, {}'.format(format_cb(cb[0]), format_cb(cb[1]))
+ elif size > 2:
+ cb = '{}, <{} more>, {}'.format(format_cb(cb[0]),
+ size - 2,
+ format_cb(cb[-1]))
+ return 'cb=[%s]' % cb
+
+
+def _future_repr_info(future):
+ # (Future) -> str
+ """helper function for Future.__repr__"""
+ info = [future._state.lower()]
+ if future._state == _FINISHED:
+ if future._exception is not None:
+ info.append('exception={!r}'.format(future._exception))
+ else:
+ # use reprlib to limit the length of the output, especially
+ # for very long strings
+ result = reprlib.repr(future._result)
+ info.append('result={}'.format(result))
+ if future._callbacks:
+ info.append(_format_callbacks(future._callbacks))
+ if future._source_traceback:
+ frame = future._source_traceback[-1]
+ info.append('created at %s:%s' % (frame[0], frame[1]))
+ return info
diff --git a/Lib/asyncio/base_subprocess.py b/Lib/asyncio/base_subprocess.py
index 23742a1..a00d9d5 100644
--- a/Lib/asyncio/base_subprocess.py
+++ b/Lib/asyncio/base_subprocess.py
@@ -127,7 +127,8 @@ class BaseSubprocessTransport(transports.SubprocessTransport):
if compat.PY34:
def __del__(self):
if not self._closed:
- warnings.warn("unclosed transport %r" % self, ResourceWarning)
+ warnings.warn("unclosed transport %r" % self, ResourceWarning,
+ source=self)
self.close()
def get_pid(self):
diff --git a/Lib/asyncio/base_tasks.py b/Lib/asyncio/base_tasks.py
new file mode 100644
index 0000000..5f34434
--- /dev/null
+++ b/Lib/asyncio/base_tasks.py
@@ -0,0 +1,76 @@
+import linecache
+import traceback
+
+from . import base_futures
+from . import coroutines
+
+
+def _task_repr_info(task):
+ info = base_futures._future_repr_info(task)
+
+ if task._must_cancel:
+ # replace status
+ info[0] = 'cancelling'
+
+ coro = coroutines._format_coroutine(task._coro)
+ info.insert(1, 'coro=<%s>' % coro)
+
+ if task._fut_waiter is not None:
+ info.insert(2, 'wait_for=%r' % task._fut_waiter)
+ return info
+
+
+def _task_get_stack(task, limit):
+ frames = []
+ try:
+ # 'async def' coroutines
+ f = task._coro.cr_frame
+ except AttributeError:
+ f = task._coro.gi_frame
+ if f is not None:
+ while f is not None:
+ if limit is not None:
+ if limit <= 0:
+ break
+ limit -= 1
+ frames.append(f)
+ f = f.f_back
+ frames.reverse()
+ elif task._exception is not None:
+ tb = task._exception.__traceback__
+ while tb is not None:
+ if limit is not None:
+ if limit <= 0:
+ break
+ limit -= 1
+ frames.append(tb.tb_frame)
+ tb = tb.tb_next
+ return frames
+
+
+def _task_print_stack(task, limit, file):
+ extracted_list = []
+ checked = set()
+ for f in task.get_stack(limit=limit):
+ lineno = f.f_lineno
+ co = f.f_code
+ filename = co.co_filename
+ name = co.co_name
+ if filename not in checked:
+ checked.add(filename)
+ linecache.checkcache(filename)
+ line = linecache.getline(filename, lineno, f.f_globals)
+ extracted_list.append((filename, lineno, name, line))
+ exc = task._exception
+ if not extracted_list:
+ print('No stack for %r' % task, file=file)
+ elif exc is not None:
+ print('Traceback for %r (most recent call last):' % task,
+ file=file)
+ else:
+ print('Stack for %r (most recent call last):' % task,
+ file=file)
+ traceback.print_list(extracted_list, file=file)
+ if exc is not None:
+ for line in traceback.format_exception_only(exc.__class__, exc):
+ print(line, file=file, end='')
diff --git a/Lib/asyncio/coroutines.py b/Lib/asyncio/coroutines.py
index 5bdeceb..08e9441 100644
--- a/Lib/asyncio/coroutines.py
+++ b/Lib/asyncio/coroutines.py
@@ -11,7 +11,7 @@ import types
from . import compat
from . import events
-from . import futures
+from . import base_futures
from .log import logger
@@ -208,7 +208,7 @@ def coroutine(func):
@functools.wraps(func)
def coro(*args, **kw):
res = func(*args, **kw)
- if (futures.isfuture(res) or inspect.isgenerator(res) or
+ if (base_futures.isfuture(res) or inspect.isgenerator(res) or
isinstance(res, CoroWrapper)):
res = yield from res
elif _AwaitableABC is not None:
diff --git a/Lib/asyncio/futures.py b/Lib/asyncio/futures.py
index 9ca8d84..d11d289 100644
--- a/Lib/asyncio/futures.py
+++ b/Lib/asyncio/futures.py
@@ -1,33 +1,30 @@
"""A Future class similar to the one in PEP 3148."""
-__all__ = ['CancelledError', 'TimeoutError',
- 'InvalidStateError',
- 'Future', 'wrap_future', 'isfuture',
- ]
+__all__ = ['CancelledError', 'TimeoutError', 'InvalidStateError',
+ 'Future', 'wrap_future', 'isfuture']
-import concurrent.futures._base
+import concurrent.futures
import logging
-import reprlib
import sys
import traceback
+from . import base_futures
from . import compat
from . import events
-# States for Future.
-_PENDING = 'PENDING'
-_CANCELLED = 'CANCELLED'
-_FINISHED = 'FINISHED'
-Error = concurrent.futures._base.Error
-CancelledError = concurrent.futures.CancelledError
-TimeoutError = concurrent.futures.TimeoutError
+CancelledError = base_futures.CancelledError
+InvalidStateError = base_futures.InvalidStateError
+TimeoutError = base_futures.TimeoutError
+isfuture = base_futures.isfuture
-STACK_DEBUG = logging.DEBUG - 1 # heavy-duty debugging
+
+_PENDING = base_futures._PENDING
+_CANCELLED = base_futures._CANCELLED
+_FINISHED = base_futures._FINISHED
-class InvalidStateError(Error):
- """The operation is not allowed in this state."""
+STACK_DEBUG = logging.DEBUG - 1 # heavy-duty debugging
class _TracebackLogger:
@@ -110,17 +107,6 @@ class _TracebackLogger:
self.loop.call_exception_handler({'message': msg})
-def isfuture(obj):
- """Check for a Future.
-
- This returns True when obj is a Future instance or is advertising
- itself as duck-type compatible by setting _asyncio_future_blocking.
- See comment in Future for more details.
- """
- return (hasattr(obj.__class__, '_asyncio_future_blocking') and
- obj._asyncio_future_blocking is not None)
-
-
class Future:
"""This class is *almost* compatible with concurrent.futures.Future.
@@ -173,45 +159,10 @@ class Future:
if self._loop.get_debug():
self._source_traceback = traceback.extract_stack(sys._getframe(1))
- def __format_callbacks(self):
- cb = self._callbacks
- size = len(cb)
- if not size:
- cb = ''
-
- def format_cb(callback):
- return events._format_callback_source(callback, ())
-
- if size == 1:
- cb = format_cb(cb[0])
- elif size == 2:
- cb = '{}, {}'.format(format_cb(cb[0]), format_cb(cb[1]))
- elif size > 2:
- cb = '{}, <{} more>, {}'.format(format_cb(cb[0]),
- size-2,
- format_cb(cb[-1]))
- return 'cb=[%s]' % cb
-
- def _repr_info(self):
- info = [self._state.lower()]
- if self._state == _FINISHED:
- if self._exception is not None:
- info.append('exception={!r}'.format(self._exception))
- else:
- # use reprlib to limit the length of the output, especially
- # for very long strings
- result = reprlib.repr(self._result)
- info.append('result={}'.format(result))
- if self._callbacks:
- info.append(self.__format_callbacks())
- if self._source_traceback:
- frame = self._source_traceback[-1]
- info.append('created at %s:%s' % (frame[0], frame[1]))
- return info
+ _repr_info = base_futures._future_repr_info
def __repr__(self):
- info = self._repr_info()
- return '<%s %s>' % (self.__class__.__name__, ' '.join(info))
+ return '<%s %s>' % (self.__class__.__name__, ' '.join(self._repr_info()))
# On Python 3.3 and older, objects with a destructor part of a reference
# cycle are never destroyed. It's not more the case on Python 3.4 thanks
@@ -385,6 +336,10 @@ class Future:
__await__ = __iter__ # make compatible with 'await' expression
+# Needed for testing purposes.
+_PyFuture = Future
+
+
def _set_result_unless_cancelled(fut, result):
"""Helper setting the result only if the future was not cancelled."""
if fut.cancelled():
@@ -477,3 +432,12 @@ def wrap_future(future, *, loop=None):
new_future = loop.create_future()
_chain_future(future, new_future)
return new_future
+
+
+try:
+ import _asyncio
+except ImportError:
+ pass
+else:
+ # _CFuture is needed for tests.
+ Future = _CFuture = _asyncio.Future
diff --git a/Lib/asyncio/proactor_events.py b/Lib/asyncio/proactor_events.py
index fef3205..ff12877 100644
--- a/Lib/asyncio/proactor_events.py
+++ b/Lib/asyncio/proactor_events.py
@@ -92,7 +92,8 @@ class _ProactorBasePipeTransport(transports._FlowControlMixin,
if compat.PY34:
def __del__(self):
if self._sock is not None:
- warnings.warn("unclosed transport %r" % self, ResourceWarning)
+ warnings.warn("unclosed transport %r" % self, ResourceWarning,
+ source=self)
self.close()
def _fatal_error(self, exc, message='Fatal error on pipe transport'):
diff --git a/Lib/asyncio/selector_events.py b/Lib/asyncio/selector_events.py
index 12d357b..9dbe550 100644
--- a/Lib/asyncio/selector_events.py
+++ b/Lib/asyncio/selector_events.py
@@ -627,7 +627,8 @@ class _SelectorTransport(transports._FlowControlMixin,
if compat.PY34:
def __del__(self):
if self._sock is not None:
- warnings.warn("unclosed transport %r" % self, ResourceWarning)
+ warnings.warn("unclosed transport %r" % self, ResourceWarning,
+ source=self)
self._sock.close()
def _fatal_error(self, exc, message='Fatal error on transport'):
diff --git a/Lib/asyncio/sslproto.py b/Lib/asyncio/sslproto.py
index c2c4b95..7ad28d6 100644
--- a/Lib/asyncio/sslproto.py
+++ b/Lib/asyncio/sslproto.py
@@ -331,7 +331,8 @@ class _SSLProtocolTransport(transports._FlowControlMixin,
if compat.PY34:
def __del__(self):
if not self._closed:
- warnings.warn("unclosed transport %r" % self, ResourceWarning)
+ warnings.warn("unclosed transport %r" % self, ResourceWarning,
+ source=self)
self.close()
def pause_reading(self):
diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py
index 8852aa5..5a43ef2 100644
--- a/Lib/asyncio/tasks.py
+++ b/Lib/asyncio/tasks.py
@@ -9,11 +9,10 @@ __all__ = ['Task',
import concurrent.futures
import functools
import inspect
-import linecache
-import traceback
import warnings
import weakref
+from . import base_tasks
from . import compat
from . import coroutines
from . import events
@@ -93,18 +92,7 @@ class Task(futures.Future):
futures.Future.__del__(self)
def _repr_info(self):
- info = super()._repr_info()
-
- if self._must_cancel:
- # replace status
- info[0] = 'cancelling'
-
- coro = coroutines._format_coroutine(self._coro)
- info.insert(1, 'coro=<%s>' % coro)
-
- if self._fut_waiter is not None:
- info.insert(2, 'wait_for=%r' % self._fut_waiter)
- return info
+ return base_tasks._task_repr_info(self)
def get_stack(self, *, limit=None):
"""Return the list of stack frames for this task's coroutine.
@@ -127,31 +115,7 @@ class Task(futures.Future):
For reasons beyond our control, only one stack frame is
returned for a suspended coroutine.
"""
- frames = []
- try:
- # 'async def' coroutines
- f = self._coro.cr_frame
- except AttributeError:
- f = self._coro.gi_frame
- if f is not None:
- while f is not None:
- if limit is not None:
- if limit <= 0:
- break
- limit -= 1
- frames.append(f)
- f = f.f_back
- frames.reverse()
- elif self._exception is not None:
- tb = self._exception.__traceback__
- while tb is not None:
- if limit is not None:
- if limit <= 0:
- break
- limit -= 1
- frames.append(tb.tb_frame)
- tb = tb.tb_next
- return frames
+ return base_tasks._task_get_stack(self, limit)
def print_stack(self, *, limit=None, file=None):
"""Print the stack or traceback for this task's coroutine.
@@ -162,31 +126,7 @@ class Task(futures.Future):
to which the output is written; by default output is written
to sys.stderr.
"""
- extracted_list = []
- checked = set()
- for f in self.get_stack(limit=limit):
- lineno = f.f_lineno
- co = f.f_code
- filename = co.co_filename
- name = co.co_name
- if filename not in checked:
- checked.add(filename)
- linecache.checkcache(filename)
- line = linecache.getline(filename, lineno, f.f_globals)
- extracted_list.append((filename, lineno, name, line))
- exc = self._exception
- if not extracted_list:
- print('No stack for %r' % self, file=file)
- elif exc is not None:
- print('Traceback for %r (most recent call last):' % self,
- file=file)
- else:
- print('Stack for %r (most recent call last):' % self,
- file=file)
- traceback.print_list(extracted_list, file=file)
- if exc is not None:
- for line in traceback.format_exception_only(exc.__class__, exc):
- print(line, file=file, end='')
+ return base_tasks._task_print_stack(self, limit, file)
def cancel(self):
"""Request that this task cancel itself.
@@ -316,6 +256,18 @@ class Task(futures.Future):
self = None # Needed to break cycles when an exception occurs.
+_PyTask = Task
+
+
+try:
+ import _asyncio
+except ImportError:
+ pass
+else:
+ # _CTask is needed for tests.
+ Task = _CTask = _asyncio.Task
+
+
# wait() and as_completed() similar to those in PEP 3148.
FIRST_COMPLETED = concurrent.futures.FIRST_COMPLETED
diff --git a/Lib/asyncio/test_utils.py b/Lib/asyncio/test_utils.py
index 9d32822..99e3839 100644
--- a/Lib/asyncio/test_utils.py
+++ b/Lib/asyncio/test_utils.py
@@ -119,10 +119,10 @@ class SSLWSGIServerMixin:
'test', 'test_asyncio')
keyfile = os.path.join(here, 'ssl_key.pem')
certfile = os.path.join(here, 'ssl_cert.pem')
- ssock = ssl.wrap_socket(request,
- keyfile=keyfile,
- certfile=certfile,
- server_side=True)
+ context = ssl.SSLContext()
+ context.load_cert_chain(certfile, keyfile)
+
+ ssock = context.wrap_socket(request, server_side=True)
try:
self.RequestHandlerClass(ssock, client_address, self)
ssock.close()
diff --git a/Lib/asyncio/unix_events.py b/Lib/asyncio/unix_events.py
index 7790534..2806ea8 100644
--- a/Lib/asyncio/unix_events.py
+++ b/Lib/asyncio/unix_events.py
@@ -419,7 +419,8 @@ class _UnixReadPipeTransport(transports.ReadTransport):
if compat.PY34:
def __del__(self):
if self._pipe is not None:
- warnings.warn("unclosed transport %r" % self, ResourceWarning)
+ warnings.warn("unclosed transport %r" % self, ResourceWarning,
+ source=self)
self._pipe.close()
def _fatal_error(self, exc, message='Fatal error on pipe transport'):
@@ -619,7 +620,8 @@ class _UnixWritePipeTransport(transports._FlowControlMixin,
if compat.PY34:
def __del__(self):
if self._pipe is not None:
- warnings.warn("unclosed transport %r" % self, ResourceWarning)
+ warnings.warn("unclosed transport %r" % self, ResourceWarning,
+ source=self)
self._pipe.close()
def abort(self):
diff --git a/Lib/asyncio/windows_events.py b/Lib/asyncio/windows_events.py
index 668fe14..b777dd0 100644
--- a/Lib/asyncio/windows_events.py
+++ b/Lib/asyncio/windows_events.py
@@ -171,8 +171,13 @@ class _WaitCancelFuture(_BaseWaitHandleFuture):
def cancel(self):
raise RuntimeError("_WaitCancelFuture must not be cancelled")
- def _schedule_callbacks(self):
- super(_WaitCancelFuture, self)._schedule_callbacks()
+ def set_result(self, result):
+ super().set_result(result)
+ if self._done_callback is not None:
+ self._done_callback(self)
+
+ def set_exception(self, exception):
+ super().set_exception(exception)
if self._done_callback is not None:
self._done_callback(self)
diff --git a/Lib/asyncio/windows_utils.py b/Lib/asyncio/windows_utils.py
index 870cd13..7c63fb9 100644
--- a/Lib/asyncio/windows_utils.py
+++ b/Lib/asyncio/windows_utils.py
@@ -159,7 +159,8 @@ class PipeHandle:
def __del__(self):
if self._handle is not None:
- warnings.warn("unclosed %r" % self, ResourceWarning)
+ warnings.warn("unclosed %r" % self, ResourceWarning,
+ source=self)
self.close()
def __enter__(self):
diff --git a/Lib/asyncore.py b/Lib/asyncore.py
index 3b51f0f..705e406 100644
--- a/Lib/asyncore.py
+++ b/Lib/asyncore.py
@@ -333,7 +333,7 @@ class dispatcher:
self.connecting = True
err = self.socket.connect_ex(address)
if err in (EINPROGRESS, EALREADY, EWOULDBLOCK) \
- or err == EINVAL and os.name in ('nt', 'ce'):
+ or err == EINVAL and os.name == 'nt':
self.addr = address
return
if err in (0, EISCONN):
@@ -595,7 +595,8 @@ if os.name == 'posix':
def __del__(self):
if self.fd >= 0:
- warnings.warn("unclosed file %r" % self, ResourceWarning)
+ warnings.warn("unclosed file %r" % self, ResourceWarning,
+ source=self)
self.close()
def recv(self, *args):
diff --git a/Lib/base64.py b/Lib/base64.py
index adaec1d..58f6ad6 100755
--- a/Lib/base64.py
+++ b/Lib/base64.py
@@ -55,8 +55,7 @@ def b64encode(s, altchars=None):
alternative alphabet for the '+' and '/' characters. This allows an
application to e.g. generate url or filesystem safe Base64 strings.
"""
- # Strip off the trailing newline
- encoded = binascii.b2a_base64(s)[:-1]
+ encoded = binascii.b2a_base64(s, newline=False)
if altchars is not None:
assert len(altchars) == 2, repr(altchars)
return encoded.translate(bytes.maketrans(b'+/', altchars))
@@ -156,7 +155,7 @@ def b32encode(s):
leftover = len(s) % 5
# Pad the last quantum with zero bits if necessary
if leftover:
- s = s + bytes(5 - leftover) # Don't use += !
+ s = s + b'\0' * (5 - leftover) # Don't use += !
encoded = bytearray()
from_bytes = int.from_bytes
b32tab2 = _b32tab2
diff --git a/Lib/bz2.py b/Lib/bz2.py
index bc78c54..6f56328 100644
--- a/Lib/bz2.py
+++ b/Lib/bz2.py
@@ -11,6 +11,7 @@ __author__ = "Nadeem Vawda <nadeem.vawda@gmail.com>"
from builtins import open as _builtin_open
import io
+import os
import warnings
import _compression
@@ -42,9 +43,9 @@ class BZ2File(_compression.BaseStream):
def __init__(self, filename, mode="r", buffering=None, compresslevel=9):
"""Open a bzip2-compressed file.
- If filename is a str or bytes object, it gives the name
- of the file to be opened. Otherwise, it should be a file object,
- which will be used to read or write the compressed data.
+ If filename is a str, bytes, or PathLike object, it gives the
+ name of the file to be opened. Otherwise, it should be a file
+ object, which will be used to read or write the compressed data.
mode can be 'r' for reading (default), 'w' for (over)writing,
'x' for creating exclusively, or 'a' for appending. These can
@@ -91,7 +92,7 @@ class BZ2File(_compression.BaseStream):
else:
raise ValueError("Invalid mode: %r" % (mode,))
- if isinstance(filename, (str, bytes)):
+ if isinstance(filename, (str, bytes, os.PathLike)):
self._fp = _builtin_open(filename, mode)
self._closefp = True
self._mode = mode_code
@@ -99,7 +100,7 @@ class BZ2File(_compression.BaseStream):
self._fp = filename
self._mode = mode_code
else:
- raise TypeError("filename must be a str or bytes object, or a file")
+ raise TypeError("filename must be a str, bytes, file or PathLike object")
if self._mode == _MODE_READ:
raw = _compression.DecompressReader(self._fp,
@@ -289,8 +290,9 @@ def open(filename, mode="rb", compresslevel=9,
encoding=None, errors=None, newline=None):
"""Open a bzip2-compressed file in binary or text mode.
- The filename argument can be an actual filename (a str or bytes
- object), or an existing file object to read from or write to.
+ The filename argument can be an actual filename (a str, bytes, or
+ PathLike object), or an existing file object to read from or write
+ to.
The mode argument can be "r", "rb", "w", "wb", "x", "xb", "a" or
"ab" for binary mode, or "rt", "wt", "xt" or "at" for text mode.
diff --git a/Lib/calendar.py b/Lib/calendar.py
index 76cf8de..07594f3 100644
--- a/Lib/calendar.py
+++ b/Lib/calendar.py
@@ -13,7 +13,9 @@ from itertools import repeat
__all__ = ["IllegalMonthError", "IllegalWeekdayError", "setfirstweekday",
"firstweekday", "isleap", "leapdays", "weekday", "monthrange",
"monthcalendar", "prmonth", "month", "prcal", "calendar",
- "timegm", "month_name", "month_abbr", "day_name", "day_abbr"]
+ "timegm", "month_name", "month_abbr", "day_name", "day_abbr",
+ "Calendar", "TextCalendar", "HTMLCalendar", "LocaleTextCalendar",
+ "LocaleHTMLCalendar", "weekheader"]
# Exception raised for bad input (with string parameter for details)
error = ValueError
@@ -604,51 +606,63 @@ def timegm(tuple):
def main(args):
- import optparse
- parser = optparse.OptionParser(usage="usage: %prog [options] [year [month]]")
- parser.add_option(
+ import argparse
+ parser = argparse.ArgumentParser()
+ textgroup = parser.add_argument_group('text only arguments')
+ htmlgroup = parser.add_argument_group('html only arguments')
+ textgroup.add_argument(
"-w", "--width",
- dest="width", type="int", default=2,
- help="width of date column (default 2, text only)"
+ type=int, default=2,
+ help="width of date column (default 2)"
)
- parser.add_option(
+ textgroup.add_argument(
"-l", "--lines",
- dest="lines", type="int", default=1,
- help="number of lines for each week (default 1, text only)"
+ type=int, default=1,
+ help="number of lines for each week (default 1)"
)
- parser.add_option(
+ textgroup.add_argument(
"-s", "--spacing",
- dest="spacing", type="int", default=6,
- help="spacing between months (default 6, text only)"
+ type=int, default=6,
+ help="spacing between months (default 6)"
)
- parser.add_option(
+ textgroup.add_argument(
"-m", "--months",
- dest="months", type="int", default=3,
- help="months per row (default 3, text only)"
+ type=int, default=3,
+ help="months per row (default 3)"
)
- parser.add_option(
+ htmlgroup.add_argument(
"-c", "--css",
- dest="css", default="calendar.css",
- help="CSS to use for page (html only)"
+ default="calendar.css",
+ help="CSS to use for page"
)
- parser.add_option(
+ parser.add_argument(
"-L", "--locale",
- dest="locale", default=None,
+ default=None,
help="locale to be used from month and weekday names"
)
- parser.add_option(
+ parser.add_argument(
"-e", "--encoding",
- dest="encoding", default=None,
- help="Encoding to use for output."
+ default=None,
+ help="encoding to use for output"
)
- parser.add_option(
+ parser.add_argument(
"-t", "--type",
- dest="type", default="text",
+ default="text",
choices=("text", "html"),
help="output type (text or html)"
)
+ parser.add_argument(
+ "year",
+ nargs='?', type=int,
+ help="year number (1-9999)"
+ )
+ parser.add_argument(
+ "month",
+ nargs='?', type=int,
+ help="month number (1-12, text only)"
+ )
- (options, args) = parser.parse_args(args)
+ options = parser.parse_args(args[1:])
if options.locale and not options.encoding:
parser.error("if --locale is specified --encoding is required")
@@ -666,10 +680,10 @@ def main(args):
encoding = sys.getdefaultencoding()
optdict = dict(encoding=encoding, css=options.css)
write = sys.stdout.buffer.write
- if len(args) == 1:
+ if options.year is None:
write(cal.formatyearpage(datetime.date.today().year, **optdict))
- elif len(args) == 2:
- write(cal.formatyearpage(int(args[1]), **optdict))
+ elif options.month is None:
+ write(cal.formatyearpage(options.year, **optdict))
else:
parser.error("incorrect number of arguments")
sys.exit(1)
@@ -679,18 +693,15 @@ def main(args):
else:
cal = TextCalendar()
optdict = dict(w=options.width, l=options.lines)
- if len(args) != 3:
+ if options.month is None:
optdict["c"] = options.spacing
optdict["m"] = options.months
- if len(args) == 1:
+ if options.year is None:
result = cal.formatyear(datetime.date.today().year, **optdict)
- elif len(args) == 2:
- result = cal.formatyear(int(args[1]), **optdict)
- elif len(args) == 3:
- result = cal.formatmonth(int(args[1]), int(args[2]), **optdict)
+ elif options.month is None:
+ result = cal.formatyear(options.year, **optdict)
else:
- parser.error("incorrect number of arguments")
- sys.exit(1)
+ result = cal.formatmonth(options.year, options.month, **optdict)
write = sys.stdout.write
if options.encoding:
result = result.encode(options.encoding)
diff --git a/Lib/cgi.py b/Lib/cgi.py
index 189c6d5..233a496 100755
--- a/Lib/cgi.py
+++ b/Lib/cgi.py
@@ -45,7 +45,7 @@ import tempfile
__all__ = ["MiniFieldStorage", "FieldStorage",
"parse", "parse_qs", "parse_qsl", "parse_multipart",
- "parse_header", "print_exception", "print_environ",
+ "parse_header", "test", "print_exception", "print_environ",
"print_form", "print_directory", "print_arguments",
"print_environ_usage", "escape"]
diff --git a/Lib/code.py b/Lib/code.py
index 53244e3..23295f4 100644
--- a/Lib/code.py
+++ b/Lib/code.py
@@ -186,7 +186,7 @@ class InteractiveConsole(InteractiveInterpreter):
"""Reset the input buffer."""
self.buffer = []
- def interact(self, banner=None):
+ def interact(self, banner=None, exitmsg=None):
"""Closely emulate the interactive Python console.
The optional banner argument specifies the banner to print
@@ -196,6 +196,11 @@ class InteractiveConsole(InteractiveInterpreter):
to confuse this with the real interpreter -- since it's so
close!).
+ The optional exitmsg argument specifies the exit message
+ printed when exiting. Pass the empty string to suppress
+ printing an exit message. If exitmsg is not given or None,
+ a default message is printed.
+
"""
try:
sys.ps1
@@ -230,6 +235,10 @@ class InteractiveConsole(InteractiveInterpreter):
self.write("\nKeyboardInterrupt\n")
self.resetbuffer()
more = 0
+ if exitmsg is None:
+ self.write('now exiting %s...\n' % self.__class__.__name__)
+ elif exitmsg != '':
+ self.write('%s\n' % exitmsg)
def push(self, line):
"""Push a line to the interpreter.
@@ -267,7 +276,7 @@ class InteractiveConsole(InteractiveInterpreter):
-def interact(banner=None, readfunc=None, local=None):
+def interact(banner=None, readfunc=None, local=None, exitmsg=None):
"""Closely emulate the interactive Python interpreter.
This is a backwards compatible interface to the InteractiveConsole
@@ -279,6 +288,7 @@ def interact(banner=None, readfunc=None, local=None):
banner -- passed to InteractiveConsole.interact()
readfunc -- if not None, replaces InteractiveConsole.raw_input()
local -- passed to InteractiveInterpreter.__init__()
+ exitmsg -- passed to InteractiveConsole.interact()
"""
console = InteractiveConsole(local)
@@ -289,7 +299,7 @@ def interact(banner=None, readfunc=None, local=None):
import readline
except ImportError:
pass
- console.interact(banner)
+ console.interact(banner, exitmsg)
if __name__ == "__main__":
diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py
index bea811d..85b4c3c 100644
--- a/Lib/collections/__init__.py
+++ b/Lib/collections/__init__.py
@@ -356,7 +356,7 @@ _field_template = '''\
{name} = _property(_itemgetter({index:d}), doc='Alias for field number {index:d}')
'''
-def namedtuple(typename, field_names, verbose=False, rename=False):
+def namedtuple(typename, field_names, *, verbose=False, rename=False, module=None):
"""Returns a new subclass of tuple with named fields.
>>> Point = namedtuple('Point', ['x', 'y'])
@@ -396,7 +396,7 @@ def namedtuple(typename, field_names, verbose=False, rename=False):
field_names[index] = '_%d' % index
seen.add(name)
for name in [typename] + field_names:
- if type(name) != str:
+ if type(name) is not str:
raise TypeError('Type names and field names must be strings')
if not name.isidentifier():
raise ValueError('Type names and field names must be valid '
@@ -437,11 +437,15 @@ def namedtuple(typename, field_names, verbose=False, rename=False):
# For pickling to work, the __module__ variable needs to be set to the frame
# where the named tuple is created. Bypass this step in environments where
# sys._getframe is not defined (Jython for example) or sys._getframe is not
- # defined for arguments greater than 0 (IronPython).
- try:
- result.__module__ = _sys._getframe(1).f_globals.get('__name__', '__main__')
- except (AttributeError, ValueError):
- pass
+ # defined for arguments greater than 0 (IronPython), or where the user has
+ # specified a particular module.
+ if module is None:
+ try:
+ module = _sys._getframe(1).f_globals.get('__name__', '__main__')
+ except (AttributeError, ValueError):
+ pass
+ if module is not None:
+ result.__module__ = module
return result
@@ -845,7 +849,7 @@ class Counter(dict):
########################################################################
-### ChainMap (helper for configparser and string.Template)
+### ChainMap
########################################################################
class ChainMap(MutableMapping):
@@ -972,7 +976,7 @@ class UserDict(MutableMapping):
dict = kwargs.pop('dict')
import warnings
warnings.warn("Passing 'dict' as keyword argument is deprecated",
- PendingDeprecationWarning, stacklevel=2)
+ DeprecationWarning, stacklevel=2)
else:
dict = None
self.data = {}
diff --git a/Lib/collections/__main__.py b/Lib/collections/__main__.py
deleted file mode 100644
index 763e38e..0000000
--- a/Lib/collections/__main__.py
+++ /dev/null
@@ -1,38 +0,0 @@
-################################################################################
-### Simple tests
-################################################################################
-
-# verify that instances can be pickled
-from collections import namedtuple
-from pickle import loads, dumps
-Point = namedtuple('Point', 'x, y', True)
-p = Point(x=10, y=20)
-assert p == loads(dumps(p))
-
-# test and demonstrate ability to override methods
-class Point(namedtuple('Point', 'x y')):
- __slots__ = ()
- @property
- def hypot(self):
- return (self.x ** 2 + self.y ** 2) ** 0.5
- def __str__(self):
- return 'Point: x=%6.3f y=%6.3f hypot=%6.3f' % (self.x, self.y, self.hypot)
-
-for p in Point(3, 4), Point(14, 5/7.):
- print (p)
-
-class Point(namedtuple('Point', 'x y')):
- 'Point class with optimized _make() and _replace() without error-checking'
- __slots__ = ()
- _make = classmethod(tuple.__new__)
- def _replace(self, _map=map, **kwds):
- return self._make(_map(kwds.get, ('x', 'y'), self))
-
-print(Point(11, 22)._replace(x=100))
-
-Point3D = namedtuple('Point3D', Point._fields + ('z',))
-print(Point3D.__doc__)
-
-import doctest, collections
-TestResults = namedtuple('TestResults', 'failed attempted')
-print(TestResults(*doctest.testmod(collections)))
diff --git a/Lib/compileall.py b/Lib/compileall.py
index 2d4c523..1c9ceb6 100644
--- a/Lib/compileall.py
+++ b/Lib/compileall.py
@@ -25,6 +25,8 @@ from functools import partial
__all__ = ["compile_dir","compile_file","compile_path"]
def _walk_dir(dir, ddir=None, maxlevels=10, quiet=0):
+ if quiet < 2 and isinstance(dir, os.PathLike):
+ dir = os.fspath(dir)
if not quiet:
print('Listing {!r}...'.format(dir))
try:
@@ -71,7 +73,7 @@ def compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None,
files = _walk_dir(dir, quiet=quiet, maxlevels=maxlevels,
ddir=ddir)
- success = 1
+ success = True
if workers is not None and workers != 1 and ProcessPoolExecutor is not None:
workers = workers or None
with ProcessPoolExecutor(max_workers=workers) as executor:
@@ -81,12 +83,12 @@ def compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None,
legacy=legacy,
optimize=optimize),
files)
- success = min(results, default=1)
+ success = min(results, default=True)
else:
for file in files:
if not compile_file(file, ddir, force, rx, quiet,
legacy, optimize):
- success = 0
+ success = False
return success
def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0,
@@ -104,7 +106,9 @@ def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0,
legacy: if True, produce legacy pyc paths instead of PEP 3147 paths
optimize: optimization level or -1 for level of the interpreter
"""
- success = 1
+ success = True
+ if quiet < 2 and isinstance(fullname, os.PathLike):
+ fullname = os.fspath(fullname)
name = os.path.basename(fullname)
if ddir is not None:
dfile = os.path.join(ddir, name)
@@ -144,7 +148,7 @@ def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0,
ok = py_compile.compile(fullname, cfile, dfile, True,
optimize=optimize)
except py_compile.PyCompileError as err:
- success = 0
+ success = False
if quiet >= 2:
return success
elif quiet:
@@ -157,7 +161,7 @@ def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0,
msg = msg.decode(sys.stdout.encoding)
print(msg)
except (SyntaxError, UnicodeError, OSError) as e:
- success = 0
+ success = False
if quiet >= 2:
return success
elif quiet:
@@ -167,7 +171,7 @@ def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0,
print(e.__class__.__name__ + ':', e)
else:
if ok == 0:
- success = 0
+ success = False
return success
def compile_path(skip_curdir=1, maxlevels=0, force=False, quiet=0,
@@ -183,7 +187,7 @@ def compile_path(skip_curdir=1, maxlevels=0, force=False, quiet=0,
legacy: as for compile_dir() (default False)
optimize: as for compile_dir() (default -1)
"""
- success = 1
+ success = True
for dir in sys.path:
if (not dir or dir == os.curdir) and skip_curdir:
if quiet < 2:
diff --git a/Lib/concurrent/futures/thread.py b/Lib/concurrent/futures/thread.py
index 9c3aec9..03d276b 100644
--- a/Lib/concurrent/futures/thread.py
+++ b/Lib/concurrent/futures/thread.py
@@ -81,12 +81,13 @@ def _worker(executor_reference, work_queue):
_base.LOGGER.critical('Exception in worker', exc_info=True)
class ThreadPoolExecutor(_base.Executor):
- def __init__(self, max_workers=None):
+ def __init__(self, max_workers=None, thread_name_prefix=''):
"""Initializes a new ThreadPoolExecutor instance.
Args:
max_workers: The maximum number of threads that can be used to
execute the given calls.
+ thread_name_prefix: An optional name prefix to give our threads.
"""
if max_workers is None:
# Use this number because ThreadPoolExecutor is often
@@ -100,6 +101,7 @@ class ThreadPoolExecutor(_base.Executor):
self._threads = set()
self._shutdown = False
self._shutdown_lock = threading.Lock()
+ self._thread_name_prefix = thread_name_prefix
def submit(self, fn, *args, **kwargs):
with self._shutdown_lock:
@@ -121,8 +123,11 @@ class ThreadPoolExecutor(_base.Executor):
q.put(None)
# TODO(bquinlan): Should avoid creating new threads if there are more
# idle threads than items in the work queue.
- if len(self._threads) < self._max_workers:
- t = threading.Thread(target=_worker,
+ num_threads = len(self._threads)
+ if num_threads < self._max_workers:
+ thread_name = '%s_%d' % (self._thread_name_prefix or self,
+ num_threads)
+ t = threading.Thread(name=thread_name, target=_worker,
args=(weakref.ref(self, weakref_cb),
self._work_queue))
t.daemon = True
diff --git a/Lib/contextlib.py b/Lib/contextlib.py
index d44edd6..7d94a57 100644
--- a/Lib/contextlib.py
+++ b/Lib/contextlib.py
@@ -1,11 +1,34 @@
"""Utilities for with-statement contexts. See PEP 343."""
-
+import abc
import sys
from collections import deque
from functools import wraps
-__all__ = ["contextmanager", "closing", "ContextDecorator", "ExitStack",
- "redirect_stdout", "redirect_stderr", "suppress"]
+__all__ = ["contextmanager", "closing", "AbstractContextManager",
+ "ContextDecorator", "ExitStack", "redirect_stdout",
+ "redirect_stderr", "suppress"]
+
+
+class AbstractContextManager(abc.ABC):
+
+ """An abstract base class for context managers."""
+
+ def __enter__(self):
+ """Return `self` upon entering the runtime context."""
+ return self
+
+ @abc.abstractmethod
+ def __exit__(self, exc_type, exc_value, traceback):
+ """Raise any exception triggered within the runtime context."""
+ return None
+
+ @classmethod
+ def __subclasshook__(cls, C):
+ if cls is AbstractContextManager:
+ if (any("__enter__" in B.__dict__ for B in C.__mro__) and
+ any("__exit__" in B.__dict__ for B in C.__mro__)):
+ return True
+ return NotImplemented
class ContextDecorator(object):
@@ -31,7 +54,7 @@ class ContextDecorator(object):
return inner
-class _GeneratorContextManager(ContextDecorator):
+class _GeneratorContextManager(ContextDecorator, AbstractContextManager):
"""Helper for @contextmanager decorator."""
def __init__(self, func, args, kwds):
@@ -137,7 +160,7 @@ def contextmanager(func):
return helper
-class closing(object):
+class closing(AbstractContextManager):
"""Context to automatically close something at the end of a block.
Code like this:
@@ -162,7 +185,7 @@ class closing(object):
self.thing.close()
-class _RedirectStream:
+class _RedirectStream(AbstractContextManager):
_stream = None
@@ -202,7 +225,7 @@ class redirect_stderr(_RedirectStream):
_stream = "stderr"
-class suppress:
+class suppress(AbstractContextManager):
"""Context manager to suppress specified exceptions
After the exception is suppressed, execution proceeds with the next
@@ -233,7 +256,7 @@ class suppress:
# Inspired by discussions on http://bugs.python.org/issue13585
-class ExitStack(object):
+class ExitStack(AbstractContextManager):
"""Context manager for dynamic management of a stack of exit callbacks
For example:
@@ -312,9 +335,6 @@ class ExitStack(object):
"""Immediately unwind the context stack"""
self.__exit__(None, None, None)
- def __enter__(self):
- return self
-
def __exit__(self, *exc_details):
received_exc = exc_details[0] is not None
diff --git a/Lib/copy.py b/Lib/copy.py
index 972b94a..f86040a 100644
--- a/Lib/copy.py
+++ b/Lib/copy.py
@@ -51,7 +51,6 @@ __getstate__() and __setstate__(). See the documentation for module
import types
import weakref
from copyreg import dispatch_table
-import builtins
class Error(Exception):
pass
@@ -102,37 +101,33 @@ def copy(x):
else:
raise Error("un(shallow)copyable object of type %s" % cls)
- return _reconstruct(x, rv, 0)
+ if isinstance(rv, str):
+ return x
+ return _reconstruct(x, None, *rv)
_copy_dispatch = d = {}
def _copy_immutable(x):
return x
-for t in (type(None), int, float, bool, str, tuple,
- bytes, frozenset, type, range,
- types.BuiltinFunctionType, type(Ellipsis),
+for t in (type(None), int, float, bool, complex, str, tuple,
+ bytes, frozenset, type, range, slice,
+ types.BuiltinFunctionType, type(Ellipsis), type(NotImplemented),
types.FunctionType, weakref.ref):
d[t] = _copy_immutable
t = getattr(types, "CodeType", None)
if t is not None:
d[t] = _copy_immutable
-for name in ("complex", "unicode"):
- t = getattr(builtins, name, None)
- if t is not None:
- d[t] = _copy_immutable
-
-def _copy_with_constructor(x):
- return type(x)(x)
-for t in (list, dict, set):
- d[t] = _copy_with_constructor
-
-def _copy_with_copy_method(x):
- return x.copy()
+
+d[list] = list.copy
+d[dict] = dict.copy
+d[set] = set.copy
+d[bytearray] = bytearray.copy
+
if PyStringMap is not None:
- d[PyStringMap] = _copy_with_copy_method
+ d[PyStringMap] = PyStringMap.copy
-del d
+del d, t
def deepcopy(x, memo=None, _nil=[]):
"""Deep copy operation on arbitrary Python objects.
@@ -179,7 +174,10 @@ def deepcopy(x, memo=None, _nil=[]):
else:
raise Error(
"un(deep)copyable object of type %s" % cls)
- y = _reconstruct(x, rv, 1, memo)
+ if isinstance(rv, str):
+ y = x
+ else:
+ y = _reconstruct(x, memo, *rv)
# If is its own copy, don't memoize.
if y is not x:
@@ -193,13 +191,11 @@ def _deepcopy_atomic(x, memo):
return x
d[type(None)] = _deepcopy_atomic
d[type(Ellipsis)] = _deepcopy_atomic
+d[type(NotImplemented)] = _deepcopy_atomic
d[int] = _deepcopy_atomic
d[float] = _deepcopy_atomic
d[bool] = _deepcopy_atomic
-try:
- d[complex] = _deepcopy_atomic
-except NameError:
- pass
+d[complex] = _deepcopy_atomic
d[bytes] = _deepcopy_atomic
d[str] = _deepcopy_atomic
try:
@@ -211,15 +207,16 @@ d[types.BuiltinFunctionType] = _deepcopy_atomic
d[types.FunctionType] = _deepcopy_atomic
d[weakref.ref] = _deepcopy_atomic
-def _deepcopy_list(x, memo):
+def _deepcopy_list(x, memo, deepcopy=deepcopy):
y = []
memo[id(x)] = y
+ append = y.append
for a in x:
- y.append(deepcopy(a, memo))
+ append(deepcopy(a, memo))
return y
d[list] = _deepcopy_list
-def _deepcopy_tuple(x, memo):
+def _deepcopy_tuple(x, memo, deepcopy=deepcopy):
y = [deepcopy(a, memo) for a in x]
# We're not going to put the tuple in the memo, but it's still important we
# check for it, in case the tuple contains recursive mutable structures.
@@ -236,7 +233,7 @@ def _deepcopy_tuple(x, memo):
return y
d[tuple] = _deepcopy_tuple
-def _deepcopy_dict(x, memo):
+def _deepcopy_dict(x, memo, deepcopy=deepcopy):
y = {}
memo[id(x)] = y
for key, value in x.items():
@@ -248,7 +245,9 @@ if PyStringMap is not None:
def _deepcopy_method(x, memo): # Copy instance methods
return type(x)(x.__func__, deepcopy(x.__self__, memo))
-_deepcopy_dispatch[types.MethodType] = _deepcopy_method
+d[types.MethodType] = _deepcopy_method
+
+del d
def _keep_alive(x, memo):
"""Keeps a reference to the object x in the memo.
@@ -266,31 +265,15 @@ def _keep_alive(x, memo):
# aha, this is the first one :-)
memo[id(memo)]=[x]
-def _reconstruct(x, info, deep, memo=None):
- if isinstance(info, str):
- return x
- assert isinstance(info, tuple)
- if memo is None:
- memo = {}
- n = len(info)
- assert n in (2, 3, 4, 5)
- callable, args = info[:2]
- if n > 2:
- state = info[2]
- else:
- state = None
- if n > 3:
- listiter = info[3]
- else:
- listiter = None
- if n > 4:
- dictiter = info[4]
- else:
- dictiter = None
+def _reconstruct(x, memo, func, args,
+ state=None, listiter=None, dictiter=None,
+ deepcopy=deepcopy):
+ deep = memo is not None
+ if deep and args:
+ args = (deepcopy(arg, memo) for arg in args)
+ y = func(*args)
if deep:
- args = deepcopy(args, memo)
- y = callable(*args)
- memo[id(x)] = y
+ memo[id(x)] = y
if state is not None:
if deep:
@@ -309,22 +292,22 @@ def _reconstruct(x, info, deep, memo=None):
setattr(y, key, value)
if listiter is not None:
- for item in listiter:
- if deep:
+ if deep:
+ for item in listiter:
item = deepcopy(item, memo)
- y.append(item)
+ y.append(item)
+ else:
+ for item in listiter:
+ y.append(item)
if dictiter is not None:
- for key, value in dictiter:
- if deep:
+ if deep:
+ for key, value in dictiter:
key = deepcopy(key, memo)
value = deepcopy(value, memo)
- y[key] = value
+ y[key] = value
+ else:
+ for key, value in dictiter:
+ y[key] = value
return y
-del d
-
-del types
-
-# Helper for instance creation without calling __init__
-class _EmptyClass:
- pass
+del types, weakref, PyStringMap
diff --git a/Lib/crypt.py b/Lib/crypt.py
index 49ab96e..fbc5f4c 100644
--- a/Lib/crypt.py
+++ b/Lib/crypt.py
@@ -54,9 +54,8 @@ METHOD_SHA256 = _Method('SHA256', '5', 16, 63)
METHOD_SHA512 = _Method('SHA512', '6', 16, 106)
methods = []
-for _method in (METHOD_SHA512, METHOD_SHA256, METHOD_MD5):
+for _method in (METHOD_SHA512, METHOD_SHA256, METHOD_MD5, METHOD_CRYPT):
_result = crypt('', _method)
if _result and len(_result) == _method.total_size:
methods.append(_method)
-methods.append(METHOD_CRYPT)
del _result, _method
diff --git a/Lib/csv.py b/Lib/csv.py
index ca40e5e..0349e0b 100644
--- a/Lib/csv.py
+++ b/Lib/csv.py
@@ -11,13 +11,15 @@ from _csv import Error, __version__, writer, reader, register_dialect, \
__doc__
from _csv import Dialect as _Dialect
+from collections import OrderedDict
from io import StringIO
-__all__ = [ "QUOTE_MINIMAL", "QUOTE_ALL", "QUOTE_NONNUMERIC", "QUOTE_NONE",
- "Error", "Dialect", "__doc__", "excel", "excel_tab",
- "field_size_limit", "reader", "writer",
- "register_dialect", "get_dialect", "list_dialects", "Sniffer",
- "unregister_dialect", "__version__", "DictReader", "DictWriter" ]
+__all__ = ["QUOTE_MINIMAL", "QUOTE_ALL", "QUOTE_NONNUMERIC", "QUOTE_NONE",
+ "Error", "Dialect", "__doc__", "excel", "excel_tab",
+ "field_size_limit", "reader", "writer",
+ "register_dialect", "get_dialect", "list_dialects", "Sniffer",
+ "unregister_dialect", "__version__", "DictReader", "DictWriter",
+ "unix_dialect"]
class Dialect:
"""Describe a CSV dialect.
@@ -115,7 +117,7 @@ class DictReader:
# values
while row == []:
row = next(self.reader)
- d = dict(zip(self.fieldnames, row))
+ d = OrderedDict(zip(self.fieldnames, row))
lf = len(self.fieldnames)
lr = len(row)
if lf < lr:
@@ -143,7 +145,7 @@ class DictWriter:
def _dict_to_list(self, rowdict):
if self.extrasaction == "raise":
- wrong_fields = [k for k in rowdict if k not in self.fieldnames]
+ wrong_fields = rowdict.keys() - self.fieldnames
if wrong_fields:
raise ValueError("dict contains fields not in fieldnames: "
+ ", ".join([repr(x) for x in wrong_fields]))
@@ -213,10 +215,10 @@ class Sniffer:
"""
matches = []
- for restr in ('(?P<delim>[^\w\n"\'])(?P<space> ?)(?P<quote>["\']).*?(?P=quote)(?P=delim)', # ,".*?",
- '(?:^|\n)(?P<quote>["\']).*?(?P=quote)(?P<delim>[^\w\n"\'])(?P<space> ?)', # ".*?",
- '(?P<delim>>[^\w\n"\'])(?P<space> ?)(?P<quote>["\']).*?(?P=quote)(?:$|\n)', # ,".*?"
- '(?:^|\n)(?P<quote>["\']).*?(?P=quote)(?:$|\n)'): # ".*?" (no delim, no space)
+ for restr in (r'(?P<delim>[^\w\n"\'])(?P<space> ?)(?P<quote>["\']).*?(?P=quote)(?P=delim)', # ,".*?",
+ r'(?:^|\n)(?P<quote>["\']).*?(?P=quote)(?P<delim>[^\w\n"\'])(?P<space> ?)', # ".*?",
+ r'(?P<delim>>[^\w\n"\'])(?P<space> ?)(?P<quote>["\']).*?(?P=quote)(?:$|\n)', # ,".*?"
+ r'(?:^|\n)(?P<quote>["\']).*?(?P=quote)(?:$|\n)'): # ".*?" (no delim, no space)
regexp = re.compile(restr, re.DOTALL | re.MULTILINE)
matches = regexp.findall(data)
if matches:
diff --git a/Lib/ctypes/__init__.py b/Lib/ctypes/__init__.py
index 1469b8b..f870968 100644
--- a/Lib/ctypes/__init__.py
+++ b/Lib/ctypes/__init__.py
@@ -16,7 +16,7 @@ from struct import calcsize as _calcsize
if __version__ != _ctypes_version:
raise Exception("Version number mismatch", __version__, _ctypes_version)
-if _os.name in ("nt", "ce"):
+if _os.name == "nt":
from _ctypes import FormatError
DEFAULT_MODE = RTLD_LOCAL
@@ -103,12 +103,9 @@ def CFUNCTYPE(restype, *argtypes, **kw):
_c_functype_cache[(restype, argtypes, flags)] = CFunctionType
return CFunctionType
-if _os.name in ("nt", "ce"):
+if _os.name == "nt":
from _ctypes import LoadLibrary as _dlopen
from _ctypes import FUNCFLAG_STDCALL as _FUNCFLAG_STDCALL
- if _os.name == "ce":
- # 'ce' doesn't have the stdcall calling convention
- _FUNCFLAG_STDCALL = _FUNCFLAG_CDECL
_win_functype_cache = {}
def WINFUNCTYPE(restype, *argtypes, **kw):
@@ -262,7 +259,7 @@ class c_wchar(_SimpleCData):
def _reset_cache():
_pointer_type_cache.clear()
_c_functype_cache.clear()
- if _os.name in ("nt", "ce"):
+ if _os.name == "nt":
_win_functype_cache.clear()
# _SimpleCData.c_wchar_p_from_param
POINTER(c_wchar).from_param = c_wchar_p.from_param
@@ -378,7 +375,7 @@ class PyDLL(CDLL):
"""
_func_flags_ = _FUNCFLAG_CDECL | _FUNCFLAG_PYTHONAPI
-if _os.name in ("nt", "ce"):
+if _os.name == "nt":
class WinDLL(CDLL):
"""This class represents a dll exporting functions using the
@@ -431,7 +428,7 @@ class LibraryLoader(object):
cdll = LibraryLoader(CDLL)
pydll = LibraryLoader(PyDLL)
-if _os.name in ("nt", "ce"):
+if _os.name == "nt":
pythonapi = PyDLL("python dll", None, _sys.dllhandle)
elif _sys.platform == "cygwin":
pythonapi = PyDLL("libpython%d.%d.dll" % _sys.version_info[:2])
@@ -439,7 +436,7 @@ else:
pythonapi = PyDLL(None)
-if _os.name in ("nt", "ce"):
+if _os.name == "nt":
windll = LibraryLoader(WinDLL)
oledll = LibraryLoader(OleDLL)
@@ -507,7 +504,7 @@ else:
return _wstring_at(ptr, size)
-if _os.name in ("nt", "ce"): # COM stuff
+if _os.name == "nt": # COM stuff
def DllGetClassObject(rclsid, riid, ppv):
try:
ccom = __import__("comtypes.server.inprocserver", globals(), locals(), ['*'])
diff --git a/Lib/ctypes/test/test_bitfields.py b/Lib/ctypes/test/test_bitfields.py
index b39d82c..c71d71d 100644
--- a/Lib/ctypes/test/test_bitfields.py
+++ b/Lib/ctypes/test/test_bitfields.py
@@ -3,7 +3,6 @@ from ctypes.test import need_symbol
import unittest
import os
-import ctypes
import _ctypes_test
class BITS(Structure):
@@ -197,7 +196,7 @@ class BitFieldTest(unittest.TestCase):
class X(Structure):
_fields_ = [("a", c_byte, 4),
("b", c_int, 4)]
- if os.name in ("nt", "ce"):
+ if os.name == "nt":
self.assertEqual(sizeof(X), sizeof(c_int)*2)
else:
self.assertEqual(sizeof(X), sizeof(c_int))
@@ -225,7 +224,7 @@ class BitFieldTest(unittest.TestCase):
# MSVC does NOT combine c_short and c_int into one field, GCC
# does (unless GCC is run with '-mms-bitfields' which
# produces code compatible with MSVC).
- if os.name in ("nt", "ce"):
+ if os.name == "nt":
self.assertEqual(sizeof(X), sizeof(c_int) * 4)
else:
self.assertEqual(sizeof(X), sizeof(c_int) * 2)
diff --git a/Lib/ctypes/test/test_find.py b/Lib/ctypes/test/test_find.py
index 94b0b89..b99fdcb 100644
--- a/Lib/ctypes/test/test_find.py
+++ b/Lib/ctypes/test/test_find.py
@@ -69,5 +69,48 @@ class Test_OpenGL_libs(unittest.TestCase):
self.assertFalse(os.path.lexists(test.support.TESTFN))
self.assertIsNone(result)
+
+@unittest.skipUnless(sys.platform.startswith('linux'),
+ 'Test only valid for Linux')
+class LibPathFindTest(unittest.TestCase):
+ def test_find_on_libpath(self):
+ import subprocess
+ import tempfile
+
+ try:
+ p = subprocess.Popen(['gcc', '--version'], stdout=subprocess.PIPE,
+ stderr=subprocess.DEVNULL)
+ out, _ = p.communicate()
+ except OSError:
+ raise unittest.SkipTest('gcc, needed for test, not available')
+ with tempfile.TemporaryDirectory() as d:
+ # create an empty temporary file
+ srcname = os.path.join(d, 'dummy.c')
+ libname = 'py_ctypes_test_dummy'
+ dstname = os.path.join(d, 'lib%s.so' % libname)
+ with open(srcname, 'w') as f:
+ pass
+ self.assertTrue(os.path.exists(srcname))
+ # compile the file to a shared library
+ cmd = ['gcc', '-o', dstname, '--shared',
+ '-Wl,-soname,lib%s.so' % libname, srcname]
+ out = subprocess.check_output(cmd)
+ self.assertTrue(os.path.exists(dstname))
+ # now check that the .so can't be found (since not in
+ # LD_LIBRARY_PATH)
+ self.assertIsNone(find_library(libname))
+ # now add the location to LD_LIBRARY_PATH
+ with test.support.EnvironmentVarGuard() as env:
+ KEY = 'LD_LIBRARY_PATH'
+ if KEY not in env:
+ v = d
+ else:
+ v = '%s:%s' % (env[KEY], d)
+ env.set(KEY, v)
+ # now check that the .so can be found (since in
+ # LD_LIBRARY_PATH)
+ self.assertEqual(find_library(libname), 'lib%s.so' % libname)
+
+
if __name__ == "__main__":
unittest.main()
diff --git a/Lib/ctypes/test/test_funcptr.py b/Lib/ctypes/test/test_funcptr.py
index ff25c8f..f34734b 100644
--- a/Lib/ctypes/test/test_funcptr.py
+++ b/Lib/ctypes/test/test_funcptr.py
@@ -1,4 +1,4 @@
-import os, unittest
+import unittest
from ctypes import *
try:
@@ -39,7 +39,7 @@ class CFuncPtrTestCase(unittest.TestCase):
# possible, as in C, to call cdecl functions with more parameters.
#self.assertRaises(TypeError, c, 1, 2, 3)
self.assertEqual(c(1, 2, 3, 4, 5, 6), 3)
- if not WINFUNCTYPE is CFUNCTYPE and os.name != "ce":
+ if not WINFUNCTYPE is CFUNCTYPE:
self.assertRaises(TypeError, s, 1, 2, 3)
def test_structures(self):
diff --git a/Lib/ctypes/test/test_loading.py b/Lib/ctypes/test/test_loading.py
index 28468c1..45571f3 100644
--- a/Lib/ctypes/test/test_loading.py
+++ b/Lib/ctypes/test/test_loading.py
@@ -11,8 +11,6 @@ def setUpModule():
global libc_name
if os.name == "nt":
libc_name = find_library("c")
- elif os.name == "ce":
- libc_name = "coredll"
elif sys.platform == "cygwin":
libc_name = "cygwin1.dll"
else:
@@ -49,8 +47,8 @@ class LoaderTest(unittest.TestCase):
cdll.LoadLibrary(lib)
CDLL(lib)
- @unittest.skipUnless(os.name in ("nt", "ce"),
- 'test specific to Windows (NT/CE)')
+ @unittest.skipUnless(os.name == "nt",
+ 'test specific to Windows')
def test_load_library(self):
# CRT is no longer directly loadable. See issue23606 for the
# discussion about alternative approaches.
@@ -64,14 +62,9 @@ class LoaderTest(unittest.TestCase):
windll["kernel32"].GetModuleHandleW
windll.LoadLibrary("kernel32").GetModuleHandleW
WinDLL("kernel32").GetModuleHandleW
- elif os.name == "ce":
- windll.coredll.GetModuleHandleW
- windll["coredll"].GetModuleHandleW
- windll.LoadLibrary("coredll").GetModuleHandleW
- WinDLL("coredll").GetModuleHandleW
-
- @unittest.skipUnless(os.name in ("nt", "ce"),
- 'test specific to Windows (NT/CE)')
+
+ @unittest.skipUnless(os.name == "nt",
+ 'test specific to Windows')
def test_load_ordinal_functions(self):
import _ctypes_test
dll = WinDLL(_ctypes_test.__file__)
diff --git a/Lib/ctypes/test/test_objects.py b/Lib/ctypes/test/test_objects.py
index ef7b20b..19e3dc1 100644
--- a/Lib/ctypes/test/test_objects.py
+++ b/Lib/ctypes/test/test_objects.py
@@ -54,7 +54,7 @@ of 'x' ('_b_base_' is either None, or the root object owning the memory block):
'''
-import unittest, doctest, sys
+import unittest, doctest
import ctypes.test.test_objects
diff --git a/Lib/ctypes/test/test_parameters.py b/Lib/ctypes/test/test_parameters.py
index e56bccf..363f586 100644
--- a/Lib/ctypes/test/test_parameters.py
+++ b/Lib/ctypes/test/test_parameters.py
@@ -1,4 +1,4 @@
-import unittest, sys
+import unittest
from ctypes.test import need_symbol
class SimpleTypesTestCase(unittest.TestCase):
@@ -49,7 +49,7 @@ class SimpleTypesTestCase(unittest.TestCase):
# XXX Replace by c_char_p tests
def test_cstrings(self):
- from ctypes import c_char_p, byref
+ from ctypes import c_char_p
# c_char_p.from_param on a Python String packs the string
# into a cparam object
@@ -68,7 +68,7 @@ class SimpleTypesTestCase(unittest.TestCase):
@need_symbol('c_wchar_p')
def test_cw_strings(self):
- from ctypes import byref, c_wchar_p
+ from ctypes import c_wchar_p
c_wchar_p.from_param("123")
@@ -98,7 +98,7 @@ class SimpleTypesTestCase(unittest.TestCase):
def test_byref_pointer(self):
# The from_param class method of POINTER(typ) classes accepts what is
# returned by byref(obj), it type(obj) == typ
- from ctypes import c_short, c_uint, c_int, c_long, pointer, POINTER, byref
+ from ctypes import c_short, c_uint, c_int, c_long, POINTER, byref
LPINT = POINTER(c_int)
LPINT.from_param(byref(c_int(42)))
diff --git a/Lib/ctypes/test/test_pep3118.py b/Lib/ctypes/test/test_pep3118.py
index 32f802c..d68397e 100644
--- a/Lib/ctypes/test/test_pep3118.py
+++ b/Lib/ctypes/test/test_pep3118.py
@@ -1,6 +1,6 @@
import unittest
from ctypes import *
-import re, struct, sys
+import re, sys
if sys.byteorder == "little":
THIS_ENDIAN = "<"
diff --git a/Lib/ctypes/test/test_returnfuncptrs.py b/Lib/ctypes/test/test_returnfuncptrs.py
index 93eba6b..1974f40 100644
--- a/Lib/ctypes/test/test_returnfuncptrs.py
+++ b/Lib/ctypes/test/test_returnfuncptrs.py
@@ -1,6 +1,5 @@
import unittest
from ctypes import *
-import os
import _ctypes_test
diff --git a/Lib/ctypes/test/test_sizes.py b/Lib/ctypes/test/test_sizes.py
index f9b5e97..4ceacbc 100644
--- a/Lib/ctypes/test/test_sizes.py
+++ b/Lib/ctypes/test/test_sizes.py
@@ -2,7 +2,6 @@
from ctypes import *
-import sys
import unittest
diff --git a/Lib/ctypes/test/test_structures.py b/Lib/ctypes/test/test_structures.py
index c4a651c..8f6fe5f 100644
--- a/Lib/ctypes/test/test_structures.py
+++ b/Lib/ctypes/test/test_structures.py
@@ -326,11 +326,8 @@ class StructureTestCase(unittest.TestCase):
cls, msg = self.get_except(Person, b"Someone", (b"a", b"b", b"c"))
self.assertEqual(cls, RuntimeError)
- if issubclass(Exception, object):
- self.assertEqual(msg,
- "(Phone) <class 'TypeError'>: too many initializers")
- else:
- self.assertEqual(msg, "(Phone) TypeError: too many initializers")
+ self.assertEqual(msg,
+ "(Phone) <class 'TypeError'>: too many initializers")
def test_huge_field_name(self):
# issue12881: segfault with large structure field names
diff --git a/Lib/ctypes/test/test_values.py b/Lib/ctypes/test/test_values.py
index 5a3a47f..e71b480 100644
--- a/Lib/ctypes/test/test_values.py
+++ b/Lib/ctypes/test/test_values.py
@@ -79,9 +79,9 @@ class PythonValuesTestCase(unittest.TestCase):
continue
items.append((entry.name.decode("ascii"), entry.size))
- expected = [("__hello__", 161),
- ("__phello__", -161),
- ("__phello__.spam", 161),
+ expected = [("__hello__", 139),
+ ("__phello__", -139),
+ ("__phello__.spam", 139),
]
self.assertEqual(items, expected, "PyImport_FrozenModules example "
"in Doc/library/ctypes.rst may be out of date")
diff --git a/Lib/ctypes/util.py b/Lib/ctypes/util.py
index 7684eab..339ae8a 100644
--- a/Lib/ctypes/util.py
+++ b/Lib/ctypes/util.py
@@ -67,16 +67,6 @@ if os.name == "nt":
return fname
return None
-if os.name == "ce":
- # search path according to MSDN:
- # - absolute path specified by filename
- # - The .exe launch directory
- # - the Windows directory
- # - ROM dll files (where are they?)
- # - OEM specified search path: HKLM\Loader\SystemPath
- def find_library(name):
- return name
-
if os.name == "posix" and sys.platform == "darwin":
from ctypes.macholib.dyld import dyld_find as _dyld_find
def find_library(name):
@@ -271,8 +261,8 @@ elif os.name == "posix":
abi_type = mach_map.get(machine, 'libc6')
# XXX assuming GLIBC's ldconfig (with option -p)
- regex = os.fsencode(
- '\s+(lib%s\.[^\s]+)\s+\(%s' % (re.escape(name), abi_type))
+ regex = r'\s+(lib%s\.[^\s]+)\s+\(%s'
+ regex = os.fsencode(regex % (re.escape(name), abi_type))
try:
with subprocess.Popen(['/sbin/ldconfig', '-p'],
stdin=subprocess.DEVNULL,
@@ -285,8 +275,32 @@ elif os.name == "posix":
except OSError:
pass
+ def _findLib_ld(name):
+ # See issue #9998 for why this is needed
+ expr = r'[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name)
+ cmd = ['ld', '-t']
+ libpath = os.environ.get('LD_LIBRARY_PATH')
+ if libpath:
+ for d in libpath.split(':'):
+ cmd.extend(['-L', d])
+ cmd.extend(['-o', os.devnull, '-l%s' % name])
+ result = None
+ try:
+ p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE,
+ universal_newlines=True)
+ out, _ = p.communicate()
+ res = re.search(expr, os.fsdecode(out))
+ if res:
+ result = res.group(0)
+ except Exception as e:
+ pass # result will be None
+ return result
+
def find_library(name):
- return _findSoname_ldconfig(name) or _get_soname(_findLib_gcc(name))
+ # See issue #9998
+ return _findSoname_ldconfig(name) or \
+ _get_soname(_findLib_gcc(name) or _findLib_ld(name))
################################################################
# test code
diff --git a/Lib/datetime.py b/Lib/datetime.py
index a2178c7..5d5579c 100644
--- a/Lib/datetime.py
+++ b/Lib/datetime.py
@@ -152,12 +152,26 @@ def _build_struct_time(y, m, d, hh, mm, ss, dstflag):
dnum = _days_before_month(y, m) + d
return _time.struct_time((y, m, d, hh, mm, ss, wday, dnum, dstflag))
-def _format_time(hh, mm, ss, us):
- # Skip trailing microseconds when us==0.
- result = "%02d:%02d:%02d" % (hh, mm, ss)
- if us:
- result += ".%06d" % us
- return result
+def _format_time(hh, mm, ss, us, timespec='auto'):
+ specs = {
+ 'hours': '{:02d}',
+ 'minutes': '{:02d}:{:02d}',
+ 'seconds': '{:02d}:{:02d}:{:02d}',
+ 'milliseconds': '{:02d}:{:02d}:{:02d}.{:03d}',
+ 'microseconds': '{:02d}:{:02d}:{:02d}.{:06d}'
+ }
+
+ if timespec == 'auto':
+ # Skip trailing microseconds when us==0.
+ timespec = 'microseconds' if us else 'seconds'
+ elif timespec == 'milliseconds':
+ us //= 1000
+ try:
+ fmt = specs[timespec]
+ except KeyError:
+ raise ValueError('Unknown timespec value')
+ else:
+ return fmt.format(hh, mm, ss, us)
# Correctly substitute for %z and %Z escapes in strftime formats.
def _wrap_strftime(object, format, timetuple):
@@ -236,11 +250,11 @@ def _check_utc_offset(name, offset):
if not isinstance(offset, timedelta):
raise TypeError("tzinfo.%s() must return None "
"or timedelta, not '%s'" % (name, type(offset)))
- if offset % timedelta(minutes=1) or offset.microseconds:
+ if offset.microseconds:
raise ValueError("tzinfo.%s() must return a whole number "
- "of minutes, got %s" % (name, offset))
+ "of seconds, got %s" % (name, offset))
if not -timedelta(1) < offset < timedelta(1):
- raise ValueError("%s()=%s, must be must be strictly between "
+ raise ValueError("%s()=%s, must be strictly between "
"-timedelta(hours=24) and timedelta(hours=24)" %
(name, offset))
@@ -274,7 +288,7 @@ def _check_date_fields(year, month, day):
raise ValueError('day must be in 1..%d' % dim, day)
return year, month, day
-def _check_time_fields(hour, minute, second, microsecond):
+def _check_time_fields(hour, minute, second, microsecond, fold):
hour = _check_int_field(hour)
minute = _check_int_field(minute)
second = _check_int_field(second)
@@ -287,7 +301,9 @@ def _check_time_fields(hour, minute, second, microsecond):
raise ValueError('second must be in 0..59', second)
if not 0 <= microsecond <= 999999:
raise ValueError('microsecond must be in 0..999999', microsecond)
- return hour, minute, second, microsecond
+ if fold not in (0, 1):
+ raise ValueError('fold must be either 0 or 1', fold)
+ return hour, minute, second, microsecond, fold
def _check_tzinfo_arg(tz):
if tz is not None and not isinstance(tz, tzinfo):
@@ -316,6 +332,7 @@ def _divide_and_round(a, b):
return q
+
class timedelta:
"""Represent the difference between two datetime objects.
@@ -932,6 +949,7 @@ date.min = date(1, 1, 1)
date.max = date(9999, 12, 31)
date.resolution = timedelta(days=1)
+
class tzinfo:
"""Abstract base class for time zone info classes.
@@ -1023,11 +1041,11 @@ class time:
dst()
Properties (readonly):
- hour, minute, second, microsecond, tzinfo
+ hour, minute, second, microsecond, tzinfo, fold
"""
- __slots__ = '_hour', '_minute', '_second', '_microsecond', '_tzinfo', '_hashcode'
+ __slots__ = '_hour', '_minute', '_second', '_microsecond', '_tzinfo', '_hashcode', '_fold'
- def __new__(cls, hour=0, minute=0, second=0, microsecond=0, tzinfo=None):
+ def __new__(cls, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0):
"""Constructor.
Arguments:
@@ -1035,15 +1053,16 @@ class time:
hour, minute (required)
second, microsecond (default to zero)
tzinfo (default to None)
+ fold (keyword only, default to zero)
"""
- if isinstance(hour, bytes) and len(hour) == 6 and hour[0] < 24:
+ if isinstance(hour, bytes) and len(hour) == 6 and hour[0]&0x7F < 24:
# Pickle support
self = object.__new__(cls)
self.__setstate(hour, minute or None)
self._hashcode = -1
return self
- hour, minute, second, microsecond = _check_time_fields(
- hour, minute, second, microsecond)
+ hour, minute, second, microsecond, fold = _check_time_fields(
+ hour, minute, second, microsecond, fold)
_check_tzinfo_arg(tzinfo)
self = object.__new__(cls)
self._hour = hour
@@ -1052,6 +1071,7 @@ class time:
self._microsecond = microsecond
self._tzinfo = tzinfo
self._hashcode = -1
+ self._fold = fold
return self
# Read-only field accessors
@@ -1080,6 +1100,10 @@ class time:
"""timezone info object"""
return self._tzinfo
+ @property
+ def fold(self):
+ return self._fold
+
# Standard conversions, __hash__ (and helpers)
# Comparisons of time objects with other.
@@ -1145,9 +1169,13 @@ class time:
def __hash__(self):
"""Hash."""
if self._hashcode == -1:
- tzoff = self.utcoffset()
+ if self.fold:
+ t = self.replace(fold=0)
+ else:
+ t = self
+ tzoff = t.utcoffset()
if not tzoff: # zero or None
- self._hashcode = hash(self._getstate()[0])
+ self._hashcode = hash(t._getstate()[0])
else:
h, m = divmod(timedelta(hours=self.hour, minutes=self.minute) - tzoff,
timedelta(hours=1))
@@ -1171,10 +1199,11 @@ class time:
else:
sign = "+"
hh, mm = divmod(off, timedelta(hours=1))
- assert not mm % timedelta(minutes=1), "whole minute"
- mm //= timedelta(minutes=1)
+ mm, ss = divmod(mm, timedelta(minutes=1))
assert 0 <= hh < 24
off = "%s%02d%s%02d" % (sign, hh, sep, mm)
+ if ss:
+ off += ':%02d' % ss.seconds
return off
def __repr__(self):
@@ -1191,16 +1220,22 @@ class time:
if self._tzinfo is not None:
assert s[-1:] == ")"
s = s[:-1] + ", tzinfo=%r" % self._tzinfo + ")"
+ if self._fold:
+ assert s[-1:] == ")"
+ s = s[:-1] + ", fold=1)"
return s
- def isoformat(self):
+ def isoformat(self, timespec='auto'):
"""Return the time formatted according to ISO.
- This is 'HH:MM:SS.mmmmmm+zz:zz', or 'HH:MM:SS+zz:zz' if
- self.microsecond == 0.
+ The full format is 'HH:MM:SS.mmmmmm+zz:zz'. By default, the fractional
+ part is omitted if self.microsecond == 0.
+
+ The optional argument timespec specifies the number of additional
+ terms of the time to include.
"""
s = _format_time(self._hour, self._minute, self._second,
- self._microsecond)
+ self._microsecond, timespec)
tz = self._tzstr()
if tz:
s += tz
@@ -1266,7 +1301,7 @@ class time:
return offset
def replace(self, hour=None, minute=None, second=None, microsecond=None,
- tzinfo=True):
+ tzinfo=True, *, fold=None):
"""Return a new time with new values for the specified fields."""
if hour is None:
hour = self.hour
@@ -1278,14 +1313,19 @@ class time:
microsecond = self.microsecond
if tzinfo is True:
tzinfo = self.tzinfo
- return time(hour, minute, second, microsecond, tzinfo)
+ if fold is None:
+ fold = self._fold
+ return time(hour, minute, second, microsecond, tzinfo, fold=fold)
# Pickle support.
- def _getstate(self):
+ def _getstate(self, protocol=3):
us2, us3 = divmod(self._microsecond, 256)
us1, us2 = divmod(us2, 256)
- basestate = bytes([self._hour, self._minute, self._second,
+ h = self._hour
+ if self._fold and protocol > 3:
+ h += 128
+ basestate = bytes([h, self._minute, self._second,
us1, us2, us3])
if self._tzinfo is None:
return (basestate,)
@@ -1295,12 +1335,21 @@ class time:
def __setstate(self, string, tzinfo):
if tzinfo is not None and not isinstance(tzinfo, _tzinfo_class):
raise TypeError("bad tzinfo state arg")
- self._hour, self._minute, self._second, us1, us2, us3 = string
+ h, self._minute, self._second, us1, us2, us3 = string
+ if h > 127:
+ self._fold = 1
+ self._hour = h - 128
+ else:
+ self._fold = 0
+ self._hour = h
self._microsecond = (((us1 << 8) | us2) << 8) | us3
self._tzinfo = tzinfo
+ def __reduce_ex__(self, protocol):
+ return (time, self._getstate(protocol))
+
def __reduce__(self):
- return (time, self._getstate())
+ return self.__reduce_ex__(2)
_time_class = time # so functions w/ args named "time" can get at the class
@@ -1317,16 +1366,16 @@ class datetime(date):
__slots__ = date.__slots__ + time.__slots__
def __new__(cls, year, month=None, day=None, hour=0, minute=0, second=0,
- microsecond=0, tzinfo=None):
- if isinstance(year, bytes) and len(year) == 10 and 1 <= year[2] <= 12:
+ microsecond=0, tzinfo=None, *, fold=0):
+ if isinstance(year, bytes) and len(year) == 10 and 1 <= year[2]&0x7F <= 12:
# Pickle support
self = object.__new__(cls)
self.__setstate(year, month)
self._hashcode = -1
return self
year, month, day = _check_date_fields(year, month, day)
- hour, minute, second, microsecond = _check_time_fields(
- hour, minute, second, microsecond)
+ hour, minute, second, microsecond, fold = _check_time_fields(
+ hour, minute, second, microsecond, fold)
_check_tzinfo_arg(tzinfo)
self = object.__new__(cls)
self._year = year
@@ -1338,6 +1387,7 @@ class datetime(date):
self._microsecond = microsecond
self._tzinfo = tzinfo
self._hashcode = -1
+ self._fold = fold
return self
# Read-only field accessors
@@ -1366,6 +1416,10 @@ class datetime(date):
"""timezone info object"""
return self._tzinfo
+ @property
+ def fold(self):
+ return self._fold
+
@classmethod
def _fromtimestamp(cls, t, utc, tz):
"""Construct a datetime from a POSIX timestamp (like time.time()).
@@ -1384,7 +1438,23 @@ class datetime(date):
converter = _time.gmtime if utc else _time.localtime
y, m, d, hh, mm, ss, weekday, jday, dst = converter(t)
ss = min(ss, 59) # clamp out leap seconds if the platform has them
- return cls(y, m, d, hh, mm, ss, us, tz)
+ result = cls(y, m, d, hh, mm, ss, us, tz)
+ if tz is None:
+ # As of version 2015f max fold in IANA database is
+ # 23 hours at 1969-09-30 13:00:00 in Kwajalein.
+ # Let's probe 24 hours in the past to detect a transition:
+ max_fold_seconds = 24 * 3600
+ y, m, d, hh, mm, ss = converter(t - max_fold_seconds)[:6]
+ probe1 = cls(y, m, d, hh, mm, ss, us, tz)
+ trans = result - probe1 - timedelta(0, max_fold_seconds)
+ if trans.days < 0:
+ y, m, d, hh, mm, ss = converter(t + trans // timedelta(0, 1))[:6]
+ probe2 = cls(y, m, d, hh, mm, ss, us, tz)
+ if probe2 == result:
+ result._fold = 1
+ else:
+ result = tz.fromutc(result)
+ return result
@classmethod
def fromtimestamp(cls, t, tz=None):
@@ -1394,10 +1464,7 @@ class datetime(date):
"""
_check_tzinfo_arg(tz)
- result = cls._fromtimestamp(t, tz is not None, tz)
- if tz is not None:
- result = tz.fromutc(result)
- return result
+ return cls._fromtimestamp(t, tz is not None, tz)
@classmethod
def utcfromtimestamp(cls, t):
@@ -1417,15 +1484,17 @@ class datetime(date):
return cls.utcfromtimestamp(t)
@classmethod
- def combine(cls, date, time):
+ def combine(cls, date, time, tzinfo=True):
"Construct a datetime from a given date and a given time."
if not isinstance(date, _date_class):
raise TypeError("date argument must be a date instance")
if not isinstance(time, _time_class):
raise TypeError("time argument must be a time instance")
+ if tzinfo is True:
+ tzinfo = time.tzinfo
return cls(date.year, date.month, date.day,
time.hour, time.minute, time.second, time.microsecond,
- time.tzinfo)
+ tzinfo, fold=time.fold)
def timetuple(self):
"Return local time tuple compatible with time.localtime()."
@@ -1440,12 +1509,46 @@ class datetime(date):
self.hour, self.minute, self.second,
dst)
+ def _mktime(self):
+ """Return integer POSIX timestamp."""
+ epoch = datetime(1970, 1, 1)
+ max_fold_seconds = 24 * 3600
+ t = (self - epoch) // timedelta(0, 1)
+ def local(u):
+ y, m, d, hh, mm, ss = _time.localtime(u)[:6]
+ return (datetime(y, m, d, hh, mm, ss) - epoch) // timedelta(0, 1)
+
+ # Our goal is to solve t = local(u) for u.
+ a = local(t) - t
+ u1 = t - a
+ t1 = local(u1)
+ if t1 == t:
+ # We found one solution, but it may not be the one we need.
+ # Look for an earlier solution (if `fold` is 0), or a
+ # later one (if `fold` is 1).
+ u2 = u1 + (-max_fold_seconds, max_fold_seconds)[self.fold]
+ b = local(u2) - u2
+ if a == b:
+ return u1
+ else:
+ b = t1 - u1
+ assert a != b
+ u2 = t - b
+ t2 = local(u2)
+ if t2 == t:
+ return u2
+ if t1 == t:
+ return u1
+ # We have found both offsets a and b, but neither t - a nor t - b is
+ # a solution. This means t is in the gap.
+ return (max, min)[self.fold](u1, u2)
+
+
def timestamp(self):
"Return POSIX timestamp as float"
if self._tzinfo is None:
- return _time.mktime((self.year, self.month, self.day,
- self.hour, self.minute, self.second,
- -1, -1, -1)) + self.microsecond / 1e6
+ s = self._mktime()
+ return s + self.microsecond / 1e6
else:
return (self - _EPOCH).total_seconds()
@@ -1464,15 +1567,16 @@ class datetime(date):
def time(self):
"Return the time part, with tzinfo None."
- return time(self.hour, self.minute, self.second, self.microsecond)
+ return time(self.hour, self.minute, self.second, self.microsecond, fold=self.fold)
def timetz(self):
"Return the time part, with same tzinfo."
return time(self.hour, self.minute, self.second, self.microsecond,
- self._tzinfo)
+ self._tzinfo, fold=self.fold)
def replace(self, year=None, month=None, day=None, hour=None,
- minute=None, second=None, microsecond=None, tzinfo=True):
+ minute=None, second=None, microsecond=None, tzinfo=True,
+ *, fold=None):
"""Return a new datetime with new values for the specified fields."""
if year is None:
year = self.year
@@ -1490,46 +1594,45 @@ class datetime(date):
microsecond = self.microsecond
if tzinfo is True:
tzinfo = self.tzinfo
- return datetime(year, month, day, hour, minute, second, microsecond,
- tzinfo)
+ if fold is None:
+ fold = self.fold
+ return datetime(year, month, day, hour, minute, second,
+ microsecond, tzinfo, fold=fold)
+
+ def _local_timezone(self):
+ if self.tzinfo is None:
+ ts = self._mktime()
+ else:
+ ts = (self - _EPOCH) // timedelta(seconds=1)
+ localtm = _time.localtime(ts)
+ local = datetime(*localtm[:6])
+ try:
+ # Extract TZ data if available
+ gmtoff = localtm.tm_gmtoff
+ zone = localtm.tm_zone
+ except AttributeError:
+ delta = local - datetime(*_time.gmtime(ts)[:6])
+ zone = _time.strftime('%Z', localtm)
+ tz = timezone(delta, zone)
+ else:
+ tz = timezone(timedelta(seconds=gmtoff), zone)
+ return tz
def astimezone(self, tz=None):
if tz is None:
- if self.tzinfo is None:
- raise ValueError("astimezone() requires an aware datetime")
- ts = (self - _EPOCH) // timedelta(seconds=1)
- localtm = _time.localtime(ts)
- local = datetime(*localtm[:6])
- try:
- # Extract TZ data if available
- gmtoff = localtm.tm_gmtoff
- zone = localtm.tm_zone
- except AttributeError:
- # Compute UTC offset and compare with the value implied
- # by tm_isdst. If the values match, use the zone name
- # implied by tm_isdst.
- delta = local - datetime(*_time.gmtime(ts)[:6])
- dst = _time.daylight and localtm.tm_isdst > 0
- gmtoff = -(_time.altzone if dst else _time.timezone)
- if delta == timedelta(seconds=gmtoff):
- tz = timezone(delta, _time.tzname[dst])
- else:
- tz = timezone(delta)
- else:
- tz = timezone(timedelta(seconds=gmtoff), zone)
-
+ tz = self._local_timezone()
elif not isinstance(tz, tzinfo):
raise TypeError("tz argument must be an instance of tzinfo")
mytz = self.tzinfo
if mytz is None:
- raise ValueError("astimezone() requires an aware datetime")
+ mytz = self._local_timezone()
if tz is mytz:
return self
# Convert self to UTC, and attach the new time zone object.
- myoffset = self.utcoffset()
+ myoffset = mytz.utcoffset(self)
if myoffset is None:
raise ValueError("astimezone() requires an aware datetime")
utc = (self - myoffset).replace(tzinfo=tz)
@@ -1549,21 +1652,25 @@ class datetime(date):
self._hour, self._minute, self._second,
self._year)
- def isoformat(self, sep='T'):
+ def isoformat(self, sep='T', timespec='auto'):
"""Return the time formatted according to ISO.
- This is 'YYYY-MM-DD HH:MM:SS.mmmmmm', or 'YYYY-MM-DD HH:MM:SS' if
- self.microsecond == 0.
+ The full format looks like 'YYYY-MM-DD HH:MM:SS.mmmmmm'.
+ By default, the fractional part is omitted if self.microsecond == 0.
If self.tzinfo is not None, the UTC offset is also attached, giving
- 'YYYY-MM-DD HH:MM:SS.mmmmmm+HH:MM' or 'YYYY-MM-DD HH:MM:SS+HH:MM'.
+ giving a full format of 'YYYY-MM-DD HH:MM:SS.mmmmmm+HH:MM'.
Optional argument sep specifies the separator between date and
time, default 'T'.
+
+ The optional argument timespec specifies the number of additional
+ terms of the time to include.
"""
s = ("%04d-%02d-%02d%c" % (self._year, self._month, self._day, sep) +
_format_time(self._hour, self._minute, self._second,
- self._microsecond))
+ self._microsecond, timespec))
+
off = self.utcoffset()
if off is not None:
if off.days < 0:
@@ -1572,9 +1679,11 @@ class datetime(date):
else:
sign = "+"
hh, mm = divmod(off, timedelta(hours=1))
- assert not mm % timedelta(minutes=1), "whole minute"
- mm //= timedelta(minutes=1)
+ mm, ss = divmod(mm, timedelta(minutes=1))
s += "%s%02d:%02d" % (sign, hh, mm)
+ if ss:
+ assert not ss.microseconds
+ s += ":%02d" % ss.seconds
return s
def __repr__(self):
@@ -1591,6 +1700,9 @@ class datetime(date):
if self._tzinfo is not None:
assert s[-1:] == ")"
s = s[:-1] + ", tzinfo=%r" % self._tzinfo + ")"
+ if self._fold:
+ assert s[-1:] == ")"
+ s = s[:-1] + ", fold=1)"
return s
def __str__(self):
@@ -1693,6 +1805,12 @@ class datetime(date):
else:
myoff = self.utcoffset()
otoff = other.utcoffset()
+ # Assume that allow_mixed means that we are called from __eq__
+ if allow_mixed:
+ if myoff != self.replace(fold=not self.fold).utcoffset():
+ return 2
+ if otoff != other.replace(fold=not other.fold).utcoffset():
+ return 2
base_compare = myoff == otoff
if base_compare:
@@ -1760,9 +1878,13 @@ class datetime(date):
def __hash__(self):
if self._hashcode == -1:
- tzoff = self.utcoffset()
+ if self.fold:
+ t = self.replace(fold=0)
+ else:
+ t = self
+ tzoff = t.utcoffset()
if tzoff is None:
- self._hashcode = hash(self._getstate()[0])
+ self._hashcode = hash(t._getstate()[0])
else:
days = _ymd2ord(self.year, self.month, self.day)
seconds = self.hour * 3600 + self.minute * 60 + self.second
@@ -1771,11 +1893,14 @@ class datetime(date):
# Pickle support.
- def _getstate(self):
+ def _getstate(self, protocol=3):
yhi, ylo = divmod(self._year, 256)
us2, us3 = divmod(self._microsecond, 256)
us1, us2 = divmod(us2, 256)
- basestate = bytes([yhi, ylo, self._month, self._day,
+ m = self._month
+ if self._fold and protocol > 3:
+ m += 128
+ basestate = bytes([yhi, ylo, m, self._day,
self._hour, self._minute, self._second,
us1, us2, us3])
if self._tzinfo is None:
@@ -1786,14 +1911,23 @@ class datetime(date):
def __setstate(self, string, tzinfo):
if tzinfo is not None and not isinstance(tzinfo, _tzinfo_class):
raise TypeError("bad tzinfo state arg")
- (yhi, ylo, self._month, self._day, self._hour,
+ (yhi, ylo, m, self._day, self._hour,
self._minute, self._second, us1, us2, us3) = string
+ if m > 127:
+ self._fold = 1
+ self._month = m - 128
+ else:
+ self._fold = 0
+ self._month = m
self._year = yhi * 256 + ylo
self._microsecond = (((us1 << 8) | us2) << 8) | us3
self._tzinfo = tzinfo
+ def __reduce_ex__(self, protocol):
+ return (self.__class__, self._getstate(protocol))
+
def __reduce__(self):
- return (self.__class__, self._getstate())
+ return self.__reduce_ex__(2)
datetime.min = datetime(1, 1, 1)
@@ -1913,6 +2047,8 @@ class timezone(tzinfo):
@staticmethod
def _name_from_offset(delta):
+ if not delta:
+ return 'UTC'
if delta < timedelta(0):
sign = '-'
delta = -delta
diff --git a/Lib/dbm/dumb.py b/Lib/dbm/dumb.py
index e6d6505..2296dbf 100644
--- a/Lib/dbm/dumb.py
+++ b/Lib/dbm/dumb.py
@@ -81,6 +81,11 @@ class _Database(collections.MutableMapping):
try:
f = _io.open(self._datfile, 'r', encoding="Latin-1")
except OSError:
+ if flag not in ('c', 'n'):
+ import warnings
+ warnings.warn("The database file is missing, the "
+ "semantics of the 'c' flag will be used.",
+ DeprecationWarning, stacklevel=4)
with _io.open(self._datfile, 'w', encoding="Latin-1") as f:
self._chmod(self._datfile)
else:
@@ -180,6 +185,10 @@ class _Database(collections.MutableMapping):
f.write("%r, %r\n" % (key.decode("Latin-1"), pos_and_siz_pair))
def __setitem__(self, key, val):
+ if self._readonly:
+ import warnings
+ warnings.warn('The database is opened for reading only',
+ DeprecationWarning, stacklevel=2)
if isinstance(key, str):
key = key.encode('utf-8')
elif not isinstance(key, (bytes, bytearray)):
@@ -215,6 +224,10 @@ class _Database(collections.MutableMapping):
# (so that _commit() never gets called).
def __delitem__(self, key):
+ if self._readonly:
+ import warnings
+ warnings.warn('The database is opened for reading only',
+ DeprecationWarning, stacklevel=2)
if isinstance(key, str):
key = key.encode('utf-8')
self._verify_open()
@@ -304,4 +317,8 @@ def open(file, flag='c', mode=0o666):
else:
# Turn off any bits that are set in the umask
mode = mode & (~um)
+ if flag not in ('r', 'w', 'c', 'n'):
+ import warnings
+ warnings.warn("Flag must be one of 'r', 'w', 'c', or 'n'",
+ DeprecationWarning, stacklevel=2)
return _Database(file, mode, flag=flag)
diff --git a/Lib/difflib.py b/Lib/difflib.py
index 076bbac..2095a5e 100644
--- a/Lib/difflib.py
+++ b/Lib/difflib.py
@@ -1415,7 +1415,7 @@ def _mdiff(fromlines, tolines, context=None, linejunk=None,
import re
# regular expression for finding intraline change indices
- change_re = re.compile('(\++|\-+|\^+)')
+ change_re = re.compile(r'(\++|\-+|\^+)')
# create the difference iterator to generate the differences
diff_lines_iterator = ndiff(fromlines,tolines,linejunk,charjunk)
diff --git a/Lib/dis.py b/Lib/dis.py
index f7e3c7f..0794b7f 100644
--- a/Lib/dis.py
+++ b/Lib/dis.py
@@ -16,6 +16,8 @@ del _opcodes_all
_have_code = (types.MethodType, types.FunctionType, types.CodeType,
classmethod, staticmethod, type)
+FORMAT_VALUE = opmap['FORMAT_VALUE']
+
def _try_compile(source, name):
"""Attempts to compile the given source, first as an expression and
then as a statement if the first approach fails.
@@ -87,6 +89,7 @@ COMPILER_FLAG_NAMES = {
64: "NOFREE",
128: "COROUTINE",
256: "ITERABLE_COROUTINE",
+ 512: "ASYNC_GENERATOR",
}
def pretty_flags(flags):
@@ -163,6 +166,15 @@ def show_code(co, *, file=None):
_Instruction = collections.namedtuple("_Instruction",
"opname opcode arg argval argrepr offset starts_line is_jump_target")
+_Instruction.opname.__doc__ = "Human readable name for operation"
+_Instruction.opcode.__doc__ = "Numeric code for operation"
+_Instruction.arg.__doc__ = "Numeric argument to operation (if any), otherwise None"
+_Instruction.argval.__doc__ = "Resolved arg value (if known), otherwise same as arg"
+_Instruction.argrepr.__doc__ = "Human readable description of operation argument"
+_Instruction.offset.__doc__ = "Start index of operation within bytecode sequence"
+_Instruction.starts_line.__doc__ = "Line started by this opcode (if any), otherwise None"
+_Instruction.is_jump_target.__doc__ = "True if other code jumps to here, otherwise False"
+
class Instruction(_Instruction):
"""Details for a bytecode operation
@@ -276,7 +288,6 @@ def _get_instructions_bytes(code, varnames=None, names=None, constants=None,
"""
labels = findlabels(code)
starts_line = None
- free = None
for offset, op, arg in _unpack_opargs(code):
if linestarts is not None:
starts_line = linestarts.get(offset, None)
@@ -296,7 +307,7 @@ def _get_instructions_bytes(code, varnames=None, names=None, constants=None,
elif op in hasname:
argval, argrepr = _get_name_info(arg, names)
elif op in hasjrel:
- argval = offset + 3 + arg
+ argval = offset + 2 + arg
argrepr = "to " + repr(argval)
elif op in haslocal:
argval, argrepr = _get_name_info(arg, varnames)
@@ -305,8 +316,13 @@ def _get_instructions_bytes(code, varnames=None, names=None, constants=None,
argrepr = argval
elif op in hasfree:
argval, argrepr = _get_name_info(arg, cells)
- elif op in hasnargs:
- argrepr = "%d positional, %d keyword pair" % (arg%256, arg//256)
+ elif op == FORMAT_VALUE:
+ argval = ((None, str, repr, ascii)[arg & 0x3], bool(arg & 0x4))
+ argrepr = ('', 'str', 'repr', 'ascii')[arg & 0x3]
+ if argval[1]:
+ if argrepr:
+ argrepr += ', '
+ argrepr += 'with format'
yield Instruction(opname[op], op,
arg, argval, argrepr,
offset, starts_line, is_jump_target)
@@ -343,23 +359,15 @@ def _disassemble_str(source, *, file=None):
disco = disassemble # XXX For backwards compatibility
def _unpack_opargs(code):
- # enumerate() is not an option, since we sometimes process
- # multiple elements on a single pass through the loop
extended_arg = 0
- n = len(code)
- i = 0
- while i < n:
+ for i in range(0, len(code), 2):
op = code[i]
- offset = i
- i = i+1
- arg = None
if op >= HAVE_ARGUMENT:
- arg = code[i] + code[i+1]*256 + extended_arg
- extended_arg = 0
- i = i+2
- if op == EXTENDED_ARG:
- extended_arg = arg*65536
- yield (offset, op, arg)
+ arg = code[i+1] | extended_arg
+ extended_arg = (arg << 8) if op == EXTENDED_ARG else 0
+ else:
+ arg = None
+ yield (i, op, arg)
def findlabels(code):
"""Detect all offsets in a byte code which are jump targets.
@@ -370,14 +378,14 @@ def findlabels(code):
labels = []
for offset, op, arg in _unpack_opargs(code):
if arg is not None:
- label = -1
if op in hasjrel:
- label = offset + 3 + arg
+ label = offset + 2 + arg
elif op in hasjabs:
label = arg
- if label >= 0:
- if label not in labels:
- labels.append(label)
+ else:
+ continue
+ if label not in labels:
+ labels.append(label)
return labels
def findlinestarts(code):
@@ -386,8 +394,8 @@ def findlinestarts(code):
Generate pairs (offset, lineno) as described in Python/compile.c.
"""
- byte_increments = list(code.co_lnotab[0::2])
- line_increments = list(code.co_lnotab[1::2])
+ byte_increments = code.co_lnotab[0::2]
+ line_increments = code.co_lnotab[1::2]
lastlineno = None
lineno = code.co_firstlineno
@@ -398,6 +406,9 @@ def findlinestarts(code):
yield (addr, lineno)
lastlineno = lineno
addr += byte_incr
+ if line_incr >= 0x80:
+ # line_increments is an array of 8-bit signed integers
+ line_incr -= 0x100
lineno += line_incr
if lineno != lastlineno:
yield (addr, lineno)
diff --git a/Lib/distutils/archive_util.py b/Lib/distutils/archive_util.py
index bed1384..78ae575 100644
--- a/Lib/distutils/archive_util.py
+++ b/Lib/distutils/archive_util.py
@@ -171,7 +171,7 @@ def make_zipfile(base_name, base_dir, verbose=0, dry_run=0):
path = os.path.normpath(os.path.join(dirpath, name))
if os.path.isfile(path):
zip.write(path, path)
- log.info("adding '%s'" % path)
+ log.info("adding '%s'", path)
zip.close()
return zip_filename
diff --git a/Lib/distutils/cmd.py b/Lib/distutils/cmd.py
index c89d5ef..939f795 100644
--- a/Lib/distutils/cmd.py
+++ b/Lib/distutils/cmd.py
@@ -221,7 +221,7 @@ class Command:
self._ensure_stringlike(option, "string", default)
def ensure_string_list(self, option):
- """Ensure that 'option' is a list of strings. If 'option' is
+ r"""Ensure that 'option' is a list of strings. If 'option' is
currently a string, we split it either on /,\s*/ or /\s+/, so
"foo bar baz", "foo,bar,baz", and "foo, bar baz" all become
["foo", "bar", "baz"].
@@ -329,8 +329,7 @@ class Command:
# -- External world manipulation -----------------------------------
def warn(self, msg):
- log.warn("warning: %s: %s\n" %
- (self.get_command_name(), msg))
+ log.warn("warning: %s: %s\n", self.get_command_name(), msg)
def execute(self, func, args, msg=None, level=1):
util.execute(func, args, msg, dry_run=self.dry_run)
diff --git a/Lib/distutils/command/bdist_dumb.py b/Lib/distutils/command/bdist_dumb.py
index f1bfb24..e9274d9 100644
--- a/Lib/distutils/command/bdist_dumb.py
+++ b/Lib/distutils/command/bdist_dumb.py
@@ -85,7 +85,7 @@ class bdist_dumb(Command):
install.skip_build = self.skip_build
install.warn_dir = 0
- log.info("installing to %s" % self.bdist_dir)
+ log.info("installing to %s", self.bdist_dir)
self.run_command('install')
# And make an archive relative to the root of the
diff --git a/Lib/distutils/command/bdist_msi.py b/Lib/distutils/command/bdist_msi.py
index b3cfe9c..a4bd5a5 100644
--- a/Lib/distutils/command/bdist_msi.py
+++ b/Lib/distutils/command/bdist_msi.py
@@ -199,7 +199,7 @@ class bdist_msi(Command):
target_version = self.target_version
if not target_version:
assert self.skip_build, "Should have already checked this"
- target_version = sys.version[0:3]
+ target_version = '%d.%d' % sys.version_info[:2]
plat_specifier = ".%s-%s" % (self.plat_name, target_version)
build = self.get_finalized_command('build')
build.build_lib = os.path.join(build.build_base,
@@ -623,7 +623,7 @@ class bdist_msi(Command):
cost = PyDialog(db, "DiskCostDlg", x, y, w, h, modal, title,
"OK", "OK", "OK", bitmap=False)
cost.text("Title", 15, 6, 200, 15, 0x30003,
- "{\DlgFontBold8}Disk Space Requirements")
+ r"{\DlgFontBold8}Disk Space Requirements")
cost.text("Description", 20, 20, 280, 20, 0x30003,
"The disk space required for the installation of the selected features.")
cost.text("Text", 20, 53, 330, 60, 3,
@@ -670,7 +670,7 @@ class bdist_msi(Command):
progress = PyDialog(db, "ProgressDlg", x, y, w, h, modeless, title,
"Cancel", "Cancel", "Cancel", bitmap=False)
progress.text("Title", 20, 15, 200, 15, 0x30003,
- "{\DlgFontBold8}[Progress1] [ProductName]")
+ r"{\DlgFontBold8}[Progress1] [ProductName]")
progress.text("Text", 35, 65, 300, 30, 3,
"Please wait while the Installer [Progress2] [ProductName]. "
"This may take several minutes.")
diff --git a/Lib/distutils/command/bdist_wininst.py b/Lib/distutils/command/bdist_wininst.py
index 0c0e2c1..d3e1d3a 100644
--- a/Lib/distutils/command/bdist_wininst.py
+++ b/Lib/distutils/command/bdist_wininst.py
@@ -141,7 +141,7 @@ class bdist_wininst(Command):
target_version = self.target_version
if not target_version:
assert self.skip_build, "Should have already checked this"
- target_version = sys.version[0:3]
+ target_version = '%d.%d' % sys.version_info[:2]
plat_specifier = ".%s-%s" % (self.plat_name, target_version)
build = self.get_finalized_command('build')
build.build_lib = os.path.join(build.build_base,
diff --git a/Lib/distutils/command/build.py b/Lib/distutils/command/build.py
index 337dd0b..c6f52e6 100644
--- a/Lib/distutils/command/build.py
+++ b/Lib/distutils/command/build.py
@@ -81,7 +81,7 @@ class build(Command):
"--plat-name only supported on Windows (try "
"using './configure --help' on your platform)")
- plat_specifier = ".%s-%s" % (self.plat_name, sys.version[0:3])
+ plat_specifier = ".%s-%d.%d" % (self.plat_name, *sys.version_info[:2])
# Make it so Python 2.x and Python 2.x with --with-pydebug don't
# share the same build directories. Doing so confuses the build
@@ -114,7 +114,7 @@ class build(Command):
'temp' + plat_specifier)
if self.build_scripts is None:
self.build_scripts = os.path.join(self.build_base,
- 'scripts-' + sys.version[0:3])
+ 'scripts-%d.%d' % sys.version_info[:2])
if self.executable is None:
self.executable = os.path.normpath(sys.executable)
diff --git a/Lib/distutils/command/build_ext.py b/Lib/distutils/command/build_ext.py
index 7c278ef..74de782 100644
--- a/Lib/distutils/command/build_ext.py
+++ b/Lib/distutils/command/build_ext.py
@@ -364,9 +364,9 @@ class build_ext(Command):
ext_name, build_info = ext
- log.warn(("old-style (ext_name, build_info) tuple found in "
- "ext_modules for extension '%s'"
- "-- please convert to Extension instance" % ext_name))
+ log.warn("old-style (ext_name, build_info) tuple found in "
+ "ext_modules for extension '%s'"
+ "-- please convert to Extension instance", ext_name)
if not (isinstance(ext_name, str) and
extension_name_re.match(ext_name)):
diff --git a/Lib/distutils/command/build_scripts.py b/Lib/distutils/command/build_scripts.py
index 90a8380..ccc70e6 100644
--- a/Lib/distutils/command/build_scripts.py
+++ b/Lib/distutils/command/build_scripts.py
@@ -51,7 +51,7 @@ class build_scripts(Command):
def copy_scripts(self):
- """Copy each script listed in 'self.scripts'; if it's marked as a
+ r"""Copy each script listed in 'self.scripts'; if it's marked as a
Python script in the Unix way (first line matches 'first_line_re',
ie. starts with "\#!" and contains "python"), then adjust the first
line to refer to the current Python interpreter as we copy.
diff --git a/Lib/distutils/command/config.py b/Lib/distutils/command/config.py
index 847e858..4ae153d 100644
--- a/Lib/distutils/command/config.py
+++ b/Lib/distutils/command/config.py
@@ -9,7 +9,7 @@ configure-like tasks: "try to compile this C code", or "figure out where
this header file lives".
"""
-import sys, os, re
+import os, re
from distutils.core import Command
from distutils.errors import DistutilsExecError
@@ -337,7 +337,7 @@ def dump_file(filename, head=None):
If head is not None, will be dumped before the file content.
"""
if head is None:
- log.info('%s' % filename)
+ log.info('%s', filename)
else:
log.info(head)
file = open(filename)
diff --git a/Lib/distutils/command/install.py b/Lib/distutils/command/install.py
index 67db007..0258d3de 100644
--- a/Lib/distutils/command/install.py
+++ b/Lib/distutils/command/install.py
@@ -175,6 +175,7 @@ class install(Command):
self.compile = None
self.optimize = None
+ # Deprecated
# These two are for putting non-packagized distributions into their
# own directory and creating a .pth file if it makes sense.
# 'extra_path' comes from the setup file; 'install_path_file' can
@@ -290,8 +291,8 @@ class install(Command):
'dist_version': self.distribution.get_version(),
'dist_fullname': self.distribution.get_fullname(),
'py_version': py_version,
- 'py_version_short': py_version[0:3],
- 'py_version_nodot': py_version[0] + py_version[2],
+ 'py_version_short': '%d.%d' % sys.version_info[:2],
+ 'py_version_nodot': '%d%d' % sys.version_info[:2],
'sys_prefix': prefix,
'prefix': prefix,
'sys_exec_prefix': exec_prefix,
@@ -344,6 +345,7 @@ class install(Command):
'scripts', 'data', 'headers',
'userbase', 'usersite')
+ # Deprecated
# Well, we're not actually fully completely finalized yet: we still
# have to deal with 'extra_path', which is the hack for allowing
# non-packagized module distributions (hello, Numerical Python!) to
@@ -385,7 +387,7 @@ class install(Command):
else:
opt_name = opt_name.translate(longopt_xlate)
val = getattr(self, opt_name)
- log.debug(" %s: %s" % (opt_name, val))
+ log.debug(" %s: %s", opt_name, val)
def finalize_unix(self):
"""Finalizes options for posix platforms."""
@@ -490,6 +492,10 @@ class install(Command):
self.extra_path = self.distribution.extra_path
if self.extra_path is not None:
+ log.warn(
+ "Distribution option extra_path is deprecated. "
+ "See issue27919 for details."
+ )
if isinstance(self.extra_path, str):
self.extra_path = self.extra_path.split(',')
diff --git a/Lib/distutils/command/install_egg_info.py b/Lib/distutils/command/install_egg_info.py
index c2a7d64..0ddc736 100644
--- a/Lib/distutils/command/install_egg_info.py
+++ b/Lib/distutils/command/install_egg_info.py
@@ -21,10 +21,10 @@ class install_egg_info(Command):
def finalize_options(self):
self.set_undefined_options('install_lib',('install_dir','install_dir'))
- basename = "%s-%s-py%s.egg-info" % (
+ basename = "%s-%s-py%d.%d.egg-info" % (
to_filename(safe_name(self.distribution.get_name())),
to_filename(safe_version(self.distribution.get_version())),
- sys.version[:3]
+ *sys.version_info[:2]
)
self.target = os.path.join(self.install_dir, basename)
self.outputs = [self.target]
diff --git a/Lib/distutils/command/register.py b/Lib/distutils/command/register.py
index 86343c8..0fac94e 100644
--- a/Lib/distutils/command/register.py
+++ b/Lib/distutils/command/register.py
@@ -5,7 +5,7 @@ Implements the Distutils 'register' command (register with the repository).
# created 2002/10/21, Richard Jones
-import os, string, getpass
+import getpass
import io
import urllib.parse, urllib.request
from warnings import warn
@@ -94,7 +94,7 @@ class register(PyPIRCCommand):
'''
# send the info to the server and report the result
(code, result) = self.post_to_server(self.build_post_data('verify'))
- log.info('Server response (%s): %s' % (code, result))
+ log.info('Server response (%s): %s', code, result)
def send_metadata(self):
''' Send the metadata to the package index server.
@@ -205,7 +205,7 @@ Your selection [default 1]: ''', log.INFO)
data['email'] = input(' EMail: ')
code, result = self.post_to_server(data)
if code != 200:
- log.info('Server response (%s): %s' % (code, result))
+ log.info('Server response (%s): %s', code, result)
else:
log.info('You will receive an email shortly.')
log.info(('Follow the instructions in it to '
@@ -216,7 +216,7 @@ Your selection [default 1]: ''', log.INFO)
while not data['email']:
data['email'] = input('Your email address: ')
code, result = self.post_to_server(data)
- log.info('Server response (%s): %s' % (code, result))
+ log.info('Server response (%s): %s', code, result)
def build_post_data(self, action):
# figure the data to send - the metadata plus some additional
diff --git a/Lib/distutils/command/sdist.py b/Lib/distutils/command/sdist.py
index 7ea3d5f..4fd1d47 100644
--- a/Lib/distutils/command/sdist.py
+++ b/Lib/distutils/command/sdist.py
@@ -3,7 +3,6 @@
Implements the Distutils 'sdist' command (create a source distribution)."""
import os
-import string
import sys
from types import *
from glob import glob
@@ -92,9 +91,6 @@ class sdist(Command):
negative_opt = {'no-defaults': 'use-defaults',
'no-prune': 'prune' }
- default_format = {'posix': 'gztar',
- 'nt': 'zip' }
-
sub_commands = [('check', checking_metadata)]
def initialize_options(self):
@@ -111,7 +107,7 @@ class sdist(Command):
self.manifest_only = 0
self.force_manifest = 0
- self.formats = None
+ self.formats = ['gztar']
self.keep_temp = 0
self.dist_dir = None
@@ -127,13 +123,6 @@ class sdist(Command):
self.template = "MANIFEST.in"
self.ensure_string_list('formats')
- if self.formats is None:
- try:
- self.formats = [self.default_format[os.name]]
- except KeyError:
- raise DistutilsPlatformError(
- "don't know how to create source distributions "
- "on platform %s" % os.name)
bad_format = archive_util.check_archive_formats(self.formats)
if bad_format:
@@ -423,7 +412,7 @@ class sdist(Command):
log.info(msg)
for file in files:
if not os.path.isfile(file):
- log.warn("'%s' not a regular file -- skipping" % file)
+ log.warn("'%s' not a regular file -- skipping", file)
else:
dest = os.path.join(base_dir, file)
self.copy_file(file, dest, link=link)
diff --git a/Lib/distutils/core.py b/Lib/distutils/core.py
index f05b34b..d603d4a 100644
--- a/Lib/distutils/core.py
+++ b/Lib/distutils/core.py
@@ -204,16 +204,15 @@ def run_setup (script_name, script_args=None, stop_after="run"):
global _setup_stop_after, _setup_distribution
_setup_stop_after = stop_after
- save_argv = sys.argv
+ save_argv = sys.argv.copy()
g = {'__file__': script_name}
- l = {}
try:
try:
sys.argv[0] = script_name
if script_args is not None:
sys.argv[1:] = script_args
with open(script_name, 'rb') as f:
- exec(f.read(), g, l)
+ exec(f.read(), g)
finally:
sys.argv = save_argv
_setup_stop_after = None
diff --git a/Lib/distutils/cygwinccompiler.py b/Lib/distutils/cygwinccompiler.py
index c879646..1c36990 100644
--- a/Lib/distutils/cygwinccompiler.py
+++ b/Lib/distutils/cygwinccompiler.py
@@ -368,7 +368,7 @@ def check_config_h():
return (CONFIG_H_UNCERTAIN,
"couldn't read '%s': %s" % (fn, exc.strerror))
-RE_VERSION = re.compile(b'(\d+\.\d+(\.\d+)*)')
+RE_VERSION = re.compile(br'(\d+\.\d+(\.\d+)*)')
def _find_exe_version(cmd):
"""Find the version of an executable by running `cmd` in the shell.
diff --git a/Lib/distutils/dist.py b/Lib/distutils/dist.py
index ffb33ff6..62a2451 100644
--- a/Lib/distutils/dist.py
+++ b/Lib/distutils/dist.py
@@ -1018,8 +1018,7 @@ class DistributionMetadata:
"maintainer", "maintainer_email", "url",
"license", "description", "long_description",
"keywords", "platforms", "fullname", "contact",
- "contact_email", "license", "classifiers",
- "download_url",
+ "contact_email", "classifiers", "download_url",
# PEP 314
"provides", "requires", "obsoletes",
)
diff --git a/Lib/distutils/extension.py b/Lib/distutils/extension.py
index 7efbb74..c507da3 100644
--- a/Lib/distutils/extension.py
+++ b/Lib/distutils/extension.py
@@ -4,7 +4,6 @@ Provides the Extension class, used to describe C/C++ extension
modules in setup scripts."""
import os
-import sys
import warnings
# This class is really only used by the "build_ext" command, so it might
diff --git a/Lib/distutils/filelist.py b/Lib/distutils/filelist.py
index 6522e69..c92d5fd 100644
--- a/Lib/distutils/filelist.py
+++ b/Lib/distutils/filelist.py
@@ -302,21 +302,26 @@ def translate_pattern(pattern, anchor=1, prefix=None, is_regex=0):
else:
return pattern
+ # ditch start and end characters
+ start, _, end = glob_to_re('_').partition('_')
+
if pattern:
pattern_re = glob_to_re(pattern)
+ assert pattern_re.startswith(start) and pattern_re.endswith(end)
else:
pattern_re = ''
if prefix is not None:
- # ditch end of pattern character
- empty_pattern = glob_to_re('')
- prefix_re = glob_to_re(prefix)[:-len(empty_pattern)]
+ prefix_re = glob_to_re(prefix)
+ assert prefix_re.startswith(start) and prefix_re.endswith(end)
+ prefix_re = prefix_re[len(start): len(prefix_re) - len(end)]
sep = os.sep
if os.sep == '\\':
sep = r'\\'
- pattern_re = "^" + sep.join((prefix_re, ".*" + pattern_re))
+ pattern_re = pattern_re[len(start): len(pattern_re) - len(end)]
+ pattern_re = r'%s\A%s%s.*%s%s' % (start, prefix_re, sep, pattern_re, end)
else: # no prefix -- respect anchor flag
if anchor:
- pattern_re = "^" + pattern_re
+ pattern_re = r'%s\A%s' % (start, pattern_re[len(start):])
return re.compile(pattern_re)
diff --git a/Lib/distutils/msvc9compiler.py b/Lib/distutils/msvc9compiler.py
index 0b1fd19..2119127 100644
--- a/Lib/distutils/msvc9compiler.py
+++ b/Lib/distutils/msvc9compiler.py
@@ -716,7 +716,7 @@ class MSVCCompiler(CCompiler) :
r"""VC\d{2}\.CRT("|').*?(/>|</assemblyIdentity>)""",
re.DOTALL)
manifest_buf = re.sub(pattern, "", manifest_buf)
- pattern = "<dependentAssembly>\s*</dependentAssembly>"
+ pattern = r"<dependentAssembly>\s*</dependentAssembly>"
manifest_buf = re.sub(pattern, "", manifest_buf)
# Now see if any other assemblies are referenced - if not, we
# don't want a manifest embedded.
diff --git a/Lib/distutils/sysconfig.py b/Lib/distutils/sysconfig.py
index 573724d..8bf1a70 100644
--- a/Lib/distutils/sysconfig.py
+++ b/Lib/distutils/sysconfig.py
@@ -70,7 +70,7 @@ def get_python_version():
leaving off the patchlevel. Sample return values could be '1.5'
or '2.2'.
"""
- return sys.version[:3]
+ return '%d.%d' % sys.version_info[:2]
def get_python_inc(plat_specific=0, prefix=None):
@@ -242,6 +242,8 @@ def get_makefile_filename():
return os.path.join(_sys_home or project_base, "Makefile")
lib_dir = get_python_lib(plat_specific=0, standard_lib=1)
config_file = 'config-{}{}'.format(get_python_version(), build_flags)
+ if hasattr(sys.implementation, '_multiarch'):
+ config_file += '-%s' % sys.implementation._multiarch
return os.path.join(lib_dir, config_file, 'Makefile')
@@ -276,7 +278,7 @@ def parse_config_h(fp, g=None):
# Regexes needed for parsing Makefile (and similar syntaxes,
# like old-style Setup files).
-_variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
+_variable_rx = re.compile(r"([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
@@ -415,38 +417,18 @@ _config_vars = None
def _init_posix():
"""Initialize the module as appropriate for POSIX systems."""
- g = {}
- # load the installed Makefile:
- try:
- filename = get_makefile_filename()
- parse_makefile(filename, g)
- except OSError as msg:
- my_msg = "invalid Python installation: unable to open %s" % filename
- if hasattr(msg, "strerror"):
- my_msg = my_msg + " (%s)" % msg.strerror
-
- raise DistutilsPlatformError(my_msg)
-
- # load the installed pyconfig.h:
- try:
- filename = get_config_h_filename()
- with open(filename) as file:
- parse_config_h(file, g)
- except OSError as msg:
- my_msg = "invalid Python installation: unable to open %s" % filename
- if hasattr(msg, "strerror"):
- my_msg = my_msg + " (%s)" % msg.strerror
-
- raise DistutilsPlatformError(my_msg)
-
- # On AIX, there are wrong paths to the linker scripts in the Makefile
- # -- these paths are relative to the Python source, but when installed
- # the scripts are in another directory.
- if python_build:
- g['LDSHARED'] = g['BLDSHARED']
-
+ # _sysconfigdata is generated at build time, see the sysconfig module
+ name = os.environ.get('_PYTHON_SYSCONFIGDATA_NAME',
+ '_sysconfigdata_{abi}_{platform}_{multiarch}'.format(
+ abi=sys.abiflags,
+ platform=sys.platform,
+ multiarch=getattr(sys.implementation, '_multiarch', ''),
+ ))
+ _temp = __import__(name, globals(), locals(), ['build_time_vars'], 0)
+ build_time_vars = _temp.build_time_vars
global _config_vars
- _config_vars = g
+ _config_vars = {}
+ _config_vars.update(build_time_vars)
def _init_nt():
diff --git a/Lib/distutils/tests/test_bdist_rpm.py b/Lib/distutils/tests/test_bdist_rpm.py
index c5962dd..6453a02 100644
--- a/Lib/distutils/tests/test_bdist_rpm.py
+++ b/Lib/distutils/tests/test_bdist_rpm.py
@@ -9,8 +9,6 @@ from distutils.core import Distribution
from distutils.command.bdist_rpm import bdist_rpm
from distutils.tests import support
from distutils.spawn import find_executable
-from distutils import spawn
-from distutils.errors import DistutilsExecError
SETUP_PY = """\
from distutils.core import setup
diff --git a/Lib/distutils/tests/test_build.py b/Lib/distutils/tests/test_build.py
index 3391f36..b020a5b 100644
--- a/Lib/distutils/tests/test_build.py
+++ b/Lib/distutils/tests/test_build.py
@@ -27,7 +27,7 @@ class BuildTestCase(support.TempdirManager,
# build_platlib is 'build/lib.platform-x.x[-pydebug]'
# examples:
# build/lib.macosx-10.3-i386-2.7
- plat_spec = '.%s-%s' % (cmd.plat_name, sys.version[0:3])
+ plat_spec = '.%s-%d.%d' % (cmd.plat_name, *sys.version_info[:2])
if hasattr(sys, 'gettotalrefcount'):
self.assertTrue(cmd.build_platlib.endswith('-pydebug'))
plat_spec += '-pydebug'
@@ -42,7 +42,8 @@ class BuildTestCase(support.TempdirManager,
self.assertEqual(cmd.build_temp, wanted)
# build_scripts is build/scripts-x.x
- wanted = os.path.join(cmd.build_base, 'scripts-' + sys.version[0:3])
+ wanted = os.path.join(cmd.build_base,
+ 'scripts-%d.%d' % sys.version_info[:2])
self.assertEqual(cmd.build_scripts, wanted)
# executable is os.path.normpath(sys.executable)
diff --git a/Lib/distutils/tests/test_build_clib.py b/Lib/distutils/tests/test_build_clib.py
index acc99e7..85d0990 100644
--- a/Lib/distutils/tests/test_build_clib.py
+++ b/Lib/distutils/tests/test_build_clib.py
@@ -3,7 +3,7 @@ import unittest
import os
import sys
-from test.support import run_unittest
+from test.support import run_unittest, missing_compiler_executable
from distutils.command.build_clib import build_clib
from distutils.errors import DistutilsSetupError
@@ -116,19 +116,11 @@ class BuildCLibTestCase(support.TempdirManager,
cmd.build_temp = build_temp
cmd.build_clib = build_temp
- # before we run the command, we want to make sure
- # all commands are present on the system
- # by creating a compiler and checking its executables
- from distutils.ccompiler import new_compiler
- from distutils.sysconfig import customize_compiler
-
- compiler = new_compiler()
- customize_compiler(compiler)
- for ccmd in compiler.executables.values():
- if ccmd is None:
- continue
- if find_executable(ccmd[0]) is None:
- self.skipTest('The %r command is not found' % ccmd[0])
+ # Before we run the command, we want to make sure
+ # all commands are present on the system.
+ ccmd = missing_compiler_executable()
+ if ccmd is not None:
+ self.skipTest('The %r command is not found' % ccmd)
# this should work
cmd.run()
diff --git a/Lib/distutils/tests/test_build_ext.py b/Lib/distutils/tests/test_build_ext.py
index f3df564..be7f5f3 100644
--- a/Lib/distutils/tests/test_build_ext.py
+++ b/Lib/distutils/tests/test_build_ext.py
@@ -41,6 +41,9 @@ class BuildExtTestCase(TempdirManager,
return build_ext(*args, **kwargs)
def test_build_ext(self):
+ cmd = support.missing_compiler_executable()
+ if cmd is not None:
+ self.skipTest('The %r command is not found' % cmd)
global ALREADY_TESTED
copy_xxmodule_c(self.tmp_dir)
xx_c = os.path.join(self.tmp_dir, 'xxmodule.c')
@@ -166,7 +169,6 @@ class BuildExtTestCase(TempdirManager,
cmd = self.build_ext(dist)
cmd.finalize_options()
- from distutils import sysconfig
py_include = sysconfig.get_python_inc()
self.assertIn(py_include, cmd.include_dirs)
@@ -296,6 +298,9 @@ class BuildExtTestCase(TempdirManager,
self.assertEqual(cmd.compiler, 'unix')
def test_get_outputs(self):
+ cmd = support.missing_compiler_executable()
+ if cmd is not None:
+ self.skipTest('The %r command is not found' % cmd)
tmp_dir = self.mkdtemp()
c_file = os.path.join(tmp_dir, 'foo.c')
self.write_file(c_file, 'void PyInit_foo(void) {}\n')
diff --git a/Lib/distutils/tests/test_build_py.py b/Lib/distutils/tests/test_build_py.py
index 18283dc..0712e92 100644
--- a/Lib/distutils/tests/test_build_py.py
+++ b/Lib/distutils/tests/test_build_py.py
@@ -168,7 +168,8 @@ class BuildPyTestCase(support.TempdirManager,
finally:
sys.dont_write_bytecode = old_dont_write_bytecode
- self.assertIn('byte-compiling is disabled', self.logs[0][1])
+ self.assertIn('byte-compiling is disabled',
+ self.logs[0][1] % self.logs[0][2])
def test_suite():
diff --git a/Lib/distutils/tests/test_clean.py b/Lib/distutils/tests/test_clean.py
index b64f300..c605afd 100644
--- a/Lib/distutils/tests/test_clean.py
+++ b/Lib/distutils/tests/test_clean.py
@@ -1,8 +1,6 @@
"""Tests for distutils.command.clean."""
-import sys
import os
import unittest
-import getpass
from distutils.command.clean import clean
from distutils.tests import support
diff --git a/Lib/distutils/tests/test_config.py b/Lib/distutils/tests/test_config.py
index a384497..77ef788 100644
--- a/Lib/distutils/tests/test_config.py
+++ b/Lib/distutils/tests/test_config.py
@@ -1,8 +1,6 @@
"""Tests for distutils.pypirc.pypirc."""
-import sys
import os
import unittest
-import tempfile
from distutils.core import PyPIRCCommand
from distutils.core import Distribution
diff --git a/Lib/distutils/tests/test_config_cmd.py b/Lib/distutils/tests/test_config_cmd.py
index 0c8dbd8..6e566e7 100644
--- a/Lib/distutils/tests/test_config_cmd.py
+++ b/Lib/distutils/tests/test_config_cmd.py
@@ -2,7 +2,7 @@
import unittest
import os
import sys
-from test.support import run_unittest
+from test.support import run_unittest, missing_compiler_executable
from distutils.command.config import dump_file, config
from distutils.tests import support
@@ -39,6 +39,9 @@ class ConfigTestCase(support.LoggingSilencer,
@unittest.skipIf(sys.platform == 'win32', "can't test on Windows")
def test_search_cpp(self):
+ cmd = missing_compiler_executable(['preprocessor'])
+ if cmd is not None:
+ self.skipTest('The %r command is not found' % cmd)
pkg_dir, dist = self.create_dist()
cmd = config(dist)
diff --git a/Lib/distutils/tests/test_core.py b/Lib/distutils/tests/test_core.py
index 654227c..27ce732 100644
--- a/Lib/distutils/tests/test_core.py
+++ b/Lib/distutils/tests/test_core.py
@@ -29,6 +29,21 @@ from distutils.core import setup
setup()
"""
+setup_does_nothing = """\
+from distutils.core import setup
+setup()
+"""
+
+
+setup_defines_subclass = """\
+from distutils.core import setup
+from distutils.command.install import install as _install
+
+class install(_install):
+ sub_commands = _install.sub_commands + ['cmd']
+
+setup(cmdclass={'install': install})
+"""
class CoreTestCase(support.EnvironGuard, unittest.TestCase):
@@ -67,6 +82,21 @@ class CoreTestCase(support.EnvironGuard, unittest.TestCase):
distutils.core.run_setup(
self.write_setup(setup_using___file__))
+ def test_run_setup_preserves_sys_argv(self):
+ # Make sure run_setup does not clobber sys.argv
+ argv_copy = sys.argv.copy()
+ distutils.core.run_setup(
+ self.write_setup(setup_does_nothing))
+ self.assertEqual(sys.argv, argv_copy)
+
+ def test_run_setup_defines_subclass(self):
+ # Make sure the script can use __file__; if that's missing, the test
+ # setup.py script will raise NameError.
+ dist = distutils.core.run_setup(
+ self.write_setup(setup_defines_subclass))
+ install = dist.get_command_obj('install')
+ self.assertIn('cmd', install.sub_commands)
+
def test_run_setup_uses_current_dir(self):
# This tests that the setup script is run with the current directory
# as its own current directory; this was temporarily broken by a
diff --git a/Lib/distutils/tests/test_cygwinccompiler.py b/Lib/distutils/tests/test_cygwinccompiler.py
index 8569216..9dc869d 100644
--- a/Lib/distutils/tests/test_cygwinccompiler.py
+++ b/Lib/distutils/tests/test_cygwinccompiler.py
@@ -3,11 +3,10 @@ import unittest
import sys
import os
from io import BytesIO
-import subprocess
from test.support import run_unittest
from distutils import cygwinccompiler
-from distutils.cygwinccompiler import (CygwinCCompiler, check_config_h,
+from distutils.cygwinccompiler import (check_config_h,
CONFIG_H_OK, CONFIG_H_NOTOK,
CONFIG_H_UNCERTAIN, get_versions,
get_msvcr)
diff --git a/Lib/distutils/tests/test_dep_util.py b/Lib/distutils/tests/test_dep_util.py
index 3e1c366..c6fae39 100644
--- a/Lib/distutils/tests/test_dep_util.py
+++ b/Lib/distutils/tests/test_dep_util.py
@@ -1,7 +1,6 @@
"""Tests for distutils.dep_util."""
import unittest
import os
-import time
from distutils.dep_util import newer, newer_pairwise, newer_group
from distutils.errors import DistutilsFileError
diff --git a/Lib/distutils/tests/test_file_util.py b/Lib/distutils/tests/test_file_util.py
index a6d04f0..03040af 100644
--- a/Lib/distutils/tests/test_file_util.py
+++ b/Lib/distutils/tests/test_file_util.py
@@ -1,7 +1,6 @@
"""Tests for distutils.file_util."""
import unittest
import os
-import shutil
import errno
from unittest.mock import patch
diff --git a/Lib/distutils/tests/test_filelist.py b/Lib/distutils/tests/test_filelist.py
index 391af3c..c71342d 100644
--- a/Lib/distutils/tests/test_filelist.py
+++ b/Lib/distutils/tests/test_filelist.py
@@ -51,14 +51,14 @@ class FileListTestCase(support.LoggingSilencer,
for glob, regex in (
# simple cases
- ('foo*', r'foo[^%(sep)s]*\Z(?ms)'),
- ('foo?', r'foo[^%(sep)s]\Z(?ms)'),
- ('foo??', r'foo[^%(sep)s][^%(sep)s]\Z(?ms)'),
+ ('foo*', r'(?s:foo[^%(sep)s]*)\Z'),
+ ('foo?', r'(?s:foo[^%(sep)s])\Z'),
+ ('foo??', r'(?s:foo[^%(sep)s][^%(sep)s])\Z'),
# special cases
- (r'foo\\*', r'foo\\\\[^%(sep)s]*\Z(?ms)'),
- (r'foo\\\*', r'foo\\\\\\[^%(sep)s]*\Z(?ms)'),
- ('foo????', r'foo[^%(sep)s][^%(sep)s][^%(sep)s][^%(sep)s]\Z(?ms)'),
- (r'foo\\??', r'foo\\\\[^%(sep)s][^%(sep)s]\Z(?ms)')):
+ (r'foo\\*', r'(?s:foo\\\\[^%(sep)s]*)\Z'),
+ (r'foo\\\*', r'(?s:foo\\\\\\[^%(sep)s]*)\Z'),
+ ('foo????', r'(?s:foo[^%(sep)s][^%(sep)s][^%(sep)s][^%(sep)s])\Z'),
+ (r'foo\\??', r'(?s:foo\\\\[^%(sep)s][^%(sep)s])\Z')):
regex = regex % {'sep': sep}
self.assertEqual(glob_to_re(glob), regex)
diff --git a/Lib/distutils/tests/test_install.py b/Lib/distutils/tests/test_install.py
index 9313330..287ab19 100644
--- a/Lib/distutils/tests/test_install.py
+++ b/Lib/distutils/tests/test_install.py
@@ -17,6 +17,7 @@ from distutils.errors import DistutilsOptionError
from distutils.extension import Extension
from distutils.tests import support
+from test import support as test_support
def _make_ext_name(modname):
@@ -196,6 +197,9 @@ class InstallTestCase(support.TempdirManager,
self.assertEqual(found, expected)
def test_record_extensions(self):
+ cmd = test_support.missing_compiler_executable()
+ if cmd is not None:
+ self.skipTest('The %r command is not found' % cmd)
install_dir = self.mkdtemp()
project_dir, dist = self.create_dist(ext_modules=[
Extension('xx', ['xxmodule.c'])])
diff --git a/Lib/distutils/tests/test_install_data.py b/Lib/distutils/tests/test_install_data.py
index 4d8c00a..32ab296 100644
--- a/Lib/distutils/tests/test_install_data.py
+++ b/Lib/distutils/tests/test_install_data.py
@@ -1,8 +1,6 @@
"""Tests for distutils.command.install_data."""
-import sys
import os
import unittest
-import getpass
from distutils.command.install_data import install_data
from distutils.tests import support
diff --git a/Lib/distutils/tests/test_install_headers.py b/Lib/distutils/tests/test_install_headers.py
index d953157..2217b32 100644
--- a/Lib/distutils/tests/test_install_headers.py
+++ b/Lib/distutils/tests/test_install_headers.py
@@ -1,8 +1,6 @@
"""Tests for distutils.command.install_headers."""
-import sys
import os
import unittest
-import getpass
from distutils.command.install_headers import install_headers
from distutils.tests import support
diff --git a/Lib/distutils/tests/test_install_lib.py b/Lib/distutils/tests/test_install_lib.py
index 5378aa8..fda6315 100644
--- a/Lib/distutils/tests/test_install_lib.py
+++ b/Lib/distutils/tests/test_install_lib.py
@@ -104,7 +104,8 @@ class InstallLibTestCase(support.TempdirManager,
finally:
sys.dont_write_bytecode = old_dont_write_bytecode
- self.assertIn('byte-compiling is disabled', self.logs[0][1])
+ self.assertIn('byte-compiling is disabled',
+ self.logs[0][1] % self.logs[0][2])
def test_suite():
diff --git a/Lib/distutils/tests/test_spawn.py b/Lib/distutils/tests/test_spawn.py
index 6c7eb20..5edc24a 100644
--- a/Lib/distutils/tests/test_spawn.py
+++ b/Lib/distutils/tests/test_spawn.py
@@ -1,11 +1,11 @@
"""Tests for distutils.spawn."""
import unittest
+import sys
import os
-import time
-from test.support import captured_stdout, run_unittest
+from test.support import run_unittest, unix_shell
from distutils.spawn import _nt_quote_args
-from distutils.spawn import spawn, find_executable
+from distutils.spawn import spawn
from distutils.errors import DistutilsExecError
from distutils.tests import support
@@ -30,9 +30,9 @@ class SpawnTestCase(support.TempdirManager,
# creating something executable
# through the shell that returns 1
- if os.name == 'posix':
+ if sys.platform != 'win32':
exe = os.path.join(tmpdir, 'foo.sh')
- self.write_file(exe, '#!/bin/sh\nexit 1')
+ self.write_file(exe, '#!%s\nexit 1' % unix_shell)
else:
exe = os.path.join(tmpdir, 'foo.bat')
self.write_file(exe, 'exit 1')
@@ -41,9 +41,9 @@ class SpawnTestCase(support.TempdirManager,
self.assertRaises(DistutilsExecError, spawn, [exe])
# now something that works
- if os.name == 'posix':
+ if sys.platform != 'win32':
exe = os.path.join(tmpdir, 'foo.sh')
- self.write_file(exe, '#!/bin/sh\nexit 0')
+ self.write_file(exe, '#!%s\nexit 0' % unix_shell)
else:
exe = os.path.join(tmpdir, 'foo.bat')
self.write_file(exe, 'exit 0')
diff --git a/Lib/distutils/tests/test_sysconfig.py b/Lib/distutils/tests/test_sysconfig.py
index fc4d1de..fe4a299 100644
--- a/Lib/distutils/tests/test_sysconfig.py
+++ b/Lib/distutils/tests/test_sysconfig.py
@@ -39,15 +39,6 @@ class SysconfigTestCase(support.EnvironGuard, unittest.TestCase):
self.assertNotEqual(sysconfig.get_python_lib(),
sysconfig.get_python_lib(prefix=TESTFN))
- def test_get_python_inc(self):
- inc_dir = sysconfig.get_python_inc()
- # This is not much of a test. We make sure Python.h exists
- # in the directory returned by get_python_inc() but we don't know
- # it is the correct file.
- self.assertTrue(os.path.isdir(inc_dir), inc_dir)
- python_h = os.path.join(inc_dir, "Python.h")
- self.assertTrue(os.path.isfile(python_h), python_h)
-
def test_get_config_vars(self):
cvars = sysconfig.get_config_vars()
self.assertIsInstance(cvars, dict)
diff --git a/Lib/distutils/tests/test_unixccompiler.py b/Lib/distutils/tests/test_unixccompiler.py
index e171ee9..efba27e 100644
--- a/Lib/distutils/tests/test_unixccompiler.py
+++ b/Lib/distutils/tests/test_unixccompiler.py
@@ -1,5 +1,4 @@
"""Tests for distutils.unixccompiler."""
-import os
import sys
import unittest
from test.support import EnvironmentVarGuard, run_unittest
diff --git a/Lib/distutils/text_file.py b/Lib/distutils/text_file.py
index 478336f..93abad3 100644
--- a/Lib/distutils/text_file.py
+++ b/Lib/distutils/text_file.py
@@ -4,7 +4,7 @@ provides the TextFile class, which gives an interface to text files
that (optionally) takes care of stripping comments, ignoring blank
lines, and joining lines with backslashes."""
-import sys, os, io
+import sys, io
class TextFile:
diff --git a/Lib/distutils/util.py b/Lib/distutils/util.py
index e423325..fdcf6fa 100644
--- a/Lib/distutils/util.py
+++ b/Lib/distutils/util.py
@@ -7,8 +7,8 @@ one of the other *util.py modules.
import os
import re
import importlib.util
-import sys
import string
+import sys
from distutils.errors import DistutilsPlatformError
from distutils.dep_util import newer
from distutils.spawn import spawn
@@ -350,6 +350,11 @@ def byte_compile (py_files,
generated in indirect mode; unless you know what you're doing, leave
it set to None.
"""
+
+ # Late import to fix a bootstrap issue: _posixsubprocess is built by
+ # setup.py, but setup.py uses distutils.
+ import subprocess
+
# nothing is done if sys.dont_write_bytecode is True
if sys.dont_write_bytecode:
raise DistutilsByteCompileError('byte-compiling is disabled.')
@@ -412,11 +417,9 @@ byte_compile(files, optimize=%r, force=%r,
script.close()
- cmd = [sys.executable, script_name]
- if optimize == 1:
- cmd.insert(1, "-O")
- elif optimize == 2:
- cmd.insert(1, "-OO")
+ cmd = [sys.executable]
+ cmd.extend(subprocess._optim_args_from_interpreter_flags())
+ cmd.append(script_name)
spawn(cmd, dry_run=dry_run)
execute(os.remove, (script_name,), "removing %s" % script_name,
dry_run=dry_run)
diff --git a/Lib/distutils/versionpredicate.py b/Lib/distutils/versionpredicate.py
index b0dd9f4..062c98f 100644
--- a/Lib/distutils/versionpredicate.py
+++ b/Lib/distutils/versionpredicate.py
@@ -154,7 +154,7 @@ def split_provision(value):
global _provision_rx
if _provision_rx is None:
_provision_rx = re.compile(
- "([a-zA-Z_]\w*(?:\.[a-zA-Z_]\w*)*)(?:\s*\(\s*([^)\s]+)\s*\))?$",
+ r"([a-zA-Z_]\w*(?:\.[a-zA-Z_]\w*)*)(?:\s*\(\s*([^)\s]+)\s*\))?$",
re.ASCII)
value = value.strip()
m = _provision_rx.match(value)
diff --git a/Lib/doctest.py b/Lib/doctest.py
index 38fdd80..0b78544 100644
--- a/Lib/doctest.py
+++ b/Lib/doctest.py
@@ -381,12 +381,15 @@ class _OutputRedirectingPdb(pdb.Pdb):
sys.stdout = save_stdout
# [XX] Normalize with respect to os.path.pardir?
-def _module_relative_path(module, path):
+def _module_relative_path(module, test_path):
if not inspect.ismodule(module):
raise TypeError('Expected a module: %r' % module)
- if path.startswith('/'):
+ if test_path.startswith('/'):
raise ValueError('Module-relative files may not have absolute paths')
+ # Normalize the path. On Windows, replace "/" with "\".
+ test_path = os.path.join(*(test_path.split('/')))
+
# Find the base directory for the path.
if hasattr(module, '__file__'):
# A normal module/package
@@ -398,13 +401,19 @@ def _module_relative_path(module, path):
else:
basedir = os.curdir
else:
+ if hasattr(module, '__path__'):
+ for directory in module.__path__:
+ fullpath = os.path.join(directory, test_path)
+ if os.path.exists(fullpath):
+ return fullpath
+
# A module w/o __file__ (this includes builtins)
raise ValueError("Can't resolve paths relative to the module "
"%r (it has no __file__)"
% module.__name__)
- # Combine the base directory and the path.
- return os.path.join(basedir, *(path.split('/')))
+ # Combine the base directory and the test path.
+ return os.path.join(basedir, test_path)
######################################################################
## 2. Example & DocTest
@@ -756,7 +765,7 @@ class DocTestParser:
# This regular expression finds the indentation of every non-blank
# line in a string.
- _INDENT_RE = re.compile('^([ ]*)(?=\S)', re.MULTILINE)
+ _INDENT_RE = re.compile(r'^([ ]*)(?=\S)', re.MULTILINE)
def _min_indent(self, s):
"Return the minimum indentation of any non-blank line in `s`"
@@ -1097,7 +1106,7 @@ class DocTestFinder:
if lineno is not None:
if source_lines is None:
return lineno+1
- pat = re.compile('(^|.*:)\s*\w*("|\')')
+ pat = re.compile(r'(^|.*:)\s*\w*("|\')')
for lineno in range(lineno, len(source_lines)):
if pat.match(source_lines[lineno]):
return lineno
@@ -1599,11 +1608,11 @@ class OutputChecker:
# blank line, unless the DONT_ACCEPT_BLANKLINE flag is used.
if not (optionflags & DONT_ACCEPT_BLANKLINE):
# Replace <BLANKLINE> in want with a blank line.
- want = re.sub('(?m)^%s\s*?$' % re.escape(BLANKLINE_MARKER),
+ want = re.sub(r'(?m)^%s\s*?$' % re.escape(BLANKLINE_MARKER),
'', want)
# If a line in got contains only spaces, then remove the
# spaces.
- got = re.sub('(?m)^\s*?$', '', got)
+ got = re.sub(r'(?m)^\s*?$', '', got)
if got == want:
return True
diff --git a/Lib/email/_header_value_parser.py b/Lib/email/_header_value_parser.py
index 5df9511..57d01fb 100644
--- a/Lib/email/_header_value_parser.py
+++ b/Lib/email/_header_value_parser.py
@@ -652,8 +652,8 @@ class Comment(WhiteSpaceTokenList):
if value.token_type == 'comment':
return str(value)
return str(value).replace('\\', '\\\\').replace(
- '(', '\(').replace(
- ')', '\)')
+ '(', r'\(').replace(
+ ')', r'\)')
@property
def content(self):
@@ -1356,15 +1356,15 @@ RouteComponentMarker = ValueTerminal('@', 'route-component-marker')
_wsp_splitter = re.compile(r'([{}]+)'.format(''.join(WSP))).split
_non_atom_end_matcher = re.compile(r"[^{}]+".format(
- ''.join(ATOM_ENDS).replace('\\','\\\\').replace(']','\]'))).match
+ ''.join(ATOM_ENDS).replace('\\','\\\\').replace(']',r'\]'))).match
_non_printable_finder = re.compile(r"[\x00-\x20\x7F]").findall
_non_token_end_matcher = re.compile(r"[^{}]+".format(
- ''.join(TOKEN_ENDS).replace('\\','\\\\').replace(']','\]'))).match
+ ''.join(TOKEN_ENDS).replace('\\','\\\\').replace(']',r'\]'))).match
_non_attribute_end_matcher = re.compile(r"[^{}]+".format(
- ''.join(ATTRIBUTE_ENDS).replace('\\','\\\\').replace(']','\]'))).match
+ ''.join(ATTRIBUTE_ENDS).replace('\\','\\\\').replace(']',r'\]'))).match
_non_extended_attribute_end_matcher = re.compile(r"[^{}]+".format(
''.join(EXTENDED_ATTRIBUTE_ENDS).replace(
- '\\','\\\\').replace(']','\]'))).match
+ '\\','\\\\').replace(']',r'\]'))).match
def _validate_xtext(xtext):
"""If input token contains ASCII non-printables, register a defect."""
@@ -1517,7 +1517,7 @@ def get_unstructured(value):
return unstructured
def get_qp_ctext(value):
- """ctext = <printable ascii except \ ( )>
+ r"""ctext = <printable ascii except \ ( )>
This is not the RFC ctext, since we are handling nested comments in comment
and unquoting quoted-pairs here. We allow anything except the '()'
@@ -1878,7 +1878,7 @@ def get_obs_local_part(value):
return obs_local_part, value
def get_dtext(value):
- """ dtext = <printable ascii except \ [ ]> / obs-dtext
+ r""" dtext = <printable ascii except \ [ ]> / obs-dtext
obs-dtext = obs-NO-WS-CTL / quoted-pair
We allow anything except the excluded characters, but if we find any
diff --git a/Lib/email/_policybase.py b/Lib/email/_policybase.py
index c0d98a4..df46496 100644
--- a/Lib/email/_policybase.py
+++ b/Lib/email/_policybase.py
@@ -154,6 +154,9 @@ class Policy(_PolicyBase, metaclass=abc.ABCMeta):
them. This is used when the message is being
serialized by a generator. Default: True.
+ message_factory -- the class to use to create new message objects.
+ If the value is None, the default is Message.
+
"""
raise_on_defect = False
@@ -161,6 +164,7 @@ class Policy(_PolicyBase, metaclass=abc.ABCMeta):
cte_type = '8bit'
max_line_length = 78
mangle_from_ = False
+ message_factory = None
def handle_defect(self, obj, defect):
"""Based on policy, either raise defect or call register_defect.
diff --git a/Lib/email/contentmanager.py b/Lib/email/contentmanager.py
index b98ce27..b904ded 100644
--- a/Lib/email/contentmanager.py
+++ b/Lib/email/contentmanager.py
@@ -142,7 +142,7 @@ def _encode_base64(data, max_line_length):
def _encode_text(string, charset, cte, policy):
lines = string.encode(charset).splitlines()
linesep = policy.linesep.encode('ascii')
- def embeded_body(lines): return linesep.join(lines) + linesep
+ def embedded_body(lines): return linesep.join(lines) + linesep
def normal_body(lines): return b'\n'.join(lines) + b'\n'
if cte==None:
# Use heuristics to decide on the "best" encoding.
@@ -153,7 +153,7 @@ def _encode_text(string, charset, cte, policy):
if (policy.cte_type == '8bit' and
max(len(x) for x in lines) <= policy.max_line_length):
return '8bit', normal_body(lines).decode('ascii', 'surrogateescape')
- sniff = embeded_body(lines[:10])
+ sniff = embedded_body(lines[:10])
sniff_qp = quoprimime.body_encode(sniff.decode('latin-1'),
policy.max_line_length)
sniff_base64 = binascii.b2a_base64(sniff)
@@ -172,7 +172,7 @@ def _encode_text(string, charset, cte, policy):
data = quoprimime.body_encode(normal_body(lines).decode('latin-1'),
policy.max_line_length)
elif cte == 'base64':
- data = _encode_base64(embeded_body(lines), policy.max_line_length)
+ data = _encode_base64(embedded_body(lines), policy.max_line_length)
else:
raise ValueError("Unknown content transfer encoding {}".format(cte))
return cte, data
diff --git a/Lib/email/feedparser.py b/Lib/email/feedparser.py
index 0b312e5..7c07ca8 100644
--- a/Lib/email/feedparser.py
+++ b/Lib/email/feedparser.py
@@ -24,15 +24,14 @@ __all__ = ['FeedParser', 'BytesFeedParser']
import re
from email import errors
-from email import message
from email._policybase import compat32
from collections import deque
from io import StringIO
-NLCRE = re.compile('\r\n|\r|\n')
-NLCRE_bol = re.compile('(\r\n|\r|\n)')
-NLCRE_eol = re.compile('(\r\n|\r|\n)\Z')
-NLCRE_crack = re.compile('(\r\n|\r|\n)')
+NLCRE = re.compile(r'\r\n|\r|\n')
+NLCRE_bol = re.compile(r'(\r\n|\r|\n)')
+NLCRE_eol = re.compile(r'(\r\n|\r|\n)\Z')
+NLCRE_crack = re.compile(r'(\r\n|\r|\n)')
# RFC 2822 $3.6.8 Optional fields. ftext is %d33-57 / %d59-126, Any character
# except controls, SP, and ":".
headerRE = re.compile(r'^(From |[\041-\071\073-\176]*:|[\t ])')
@@ -148,13 +147,11 @@ class FeedParser:
self.policy = policy
self._old_style_factory = False
if _factory is None:
- # What this should be:
- #self._factory = policy.default_message_factory
- # but, because we are post 3.4 feature freeze, fix with temp hack:
- if self.policy is compat32:
- self._factory = message.Message
+ if policy.message_factory is None:
+ from email.message import Message
+ self._factory = Message
else:
- self._factory = message.EmailMessage
+ self._factory = policy.message_factory
else:
self._factory = _factory
try:
diff --git a/Lib/email/generator.py b/Lib/email/generator.py
index 256278d..ae670c2 100644
--- a/Lib/email/generator.py
+++ b/Lib/email/generator.py
@@ -448,7 +448,8 @@ class DecodedGenerator(Generator):
Like the Generator base class, except that non-text parts are substituted
with a format string representing the part.
"""
- def __init__(self, outfp, mangle_from_=None, maxheaderlen=78, fmt=None):
+ def __init__(self, outfp, mangle_from_=None, maxheaderlen=None, fmt=None, *,
+ policy=None):
"""Like Generator.__init__() except that an additional optional
argument is allowed.
@@ -470,7 +471,8 @@ class DecodedGenerator(Generator):
[Non-text (%(type)s) part of message omitted, filename %(filename)s]
"""
- Generator.__init__(self, outfp, mangle_from_, maxheaderlen)
+ Generator.__init__(self, outfp, mangle_from_, maxheaderlen,
+ policy=policy)
if fmt is None:
self._fmt = _FMT
else:
diff --git a/Lib/email/header.py b/Lib/email/header.py
index 6820ea1..c7b2dd9 100644
--- a/Lib/email/header.py
+++ b/Lib/email/header.py
@@ -49,7 +49,7 @@ fcre = re.compile(r'[\041-\176]+:$')
# Find a header embedded in a putative header value. Used to check for
# header injection attack.
-_embeded_header = re.compile(r'\n[^ \t]+:')
+_embedded_header = re.compile(r'\n[^ \t]+:')
@@ -385,7 +385,7 @@ class Header:
if self._chunks:
formatter.add_transition()
value = formatter._str(linesep)
- if _embeded_header.search(value):
+ if _embedded_header.search(value):
raise HeaderParseError("header value appears to contain "
"an embedded header: {!r}".format(value))
return value
diff --git a/Lib/email/message.py b/Lib/email/message.py
index 4b04283..b6512f2 100644
--- a/Lib/email/message.py
+++ b/Lib/email/message.py
@@ -4,18 +4,17 @@
"""Basic message object for the email package object model."""
-__all__ = ['Message']
+__all__ = ['Message', 'EmailMessage']
import re
import uu
import quopri
-import warnings
from io import BytesIO, StringIO
# Intrapackage imports
from email import utils
from email import errors
-from email._policybase import compat32
+from email._policybase import Policy, compat32
from email import charset as _charset
from email._encoded_words import decode_b
Charset = _charset.Charset
@@ -951,6 +950,26 @@ class MIMEPart(Message):
policy = default
Message.__init__(self, policy)
+
+ def as_string(self, unixfrom=False, maxheaderlen=None, policy=None):
+ """Return the entire formatted message as a string.
+
+ Optional 'unixfrom', when true, means include the Unix From_ envelope
+ header. maxheaderlen is retained for backward compatibility with the
+ base Message class, but defaults to None, meaning that the policy value
+ for max_line_length controls the header maximum length. 'policy' is
+ passed to the Generator instance used to serialize the mesasge; if it
+ is not specified the policy associated with the message instance is
+ used.
+ """
+ policy = self.policy if policy is None else policy
+ if maxheaderlen is None:
+ maxheaderlen = policy.max_line_length
+ return super().as_string(maxheaderlen=maxheaderlen, policy=policy)
+
+ def __str__(self):
+ return self.as_string(policy=self.policy.clone(utf8=True))
+
def is_attachment(self):
c_d = self.get('content-disposition')
return False if c_d is None else c_d.content_disposition == 'attachment'
diff --git a/Lib/email/mime/application.py b/Lib/email/mime/application.py
index f5c5905..6877e55 100644
--- a/Lib/email/mime/application.py
+++ b/Lib/email/mime/application.py
@@ -14,7 +14,7 @@ class MIMEApplication(MIMENonMultipart):
"""Class for generating application/* MIME documents."""
def __init__(self, _data, _subtype='octet-stream',
- _encoder=encoders.encode_base64, **_params):
+ _encoder=encoders.encode_base64, *, policy=None, **_params):
"""Create an application/* type MIME document.
_data is a string containing the raw application data.
@@ -31,6 +31,7 @@ class MIMEApplication(MIMENonMultipart):
"""
if _subtype is None:
raise TypeError('Invalid application MIME subtype')
- MIMENonMultipart.__init__(self, 'application', _subtype, **_params)
+ MIMENonMultipart.__init__(self, 'application', _subtype, policy=policy,
+ **_params)
self.set_payload(_data)
_encoder(self)
diff --git a/Lib/email/mime/audio.py b/Lib/email/mime/audio.py
index fbc1189..4bcd7b2 100644
--- a/Lib/email/mime/audio.py
+++ b/Lib/email/mime/audio.py
@@ -43,7 +43,7 @@ class MIMEAudio(MIMENonMultipart):
"""Class for generating audio/* MIME documents."""
def __init__(self, _audiodata, _subtype=None,
- _encoder=encoders.encode_base64, **_params):
+ _encoder=encoders.encode_base64, *, policy=None, **_params):
"""Create an audio/* type MIME document.
_audiodata is a string containing the raw audio data. If this data
@@ -68,6 +68,7 @@ class MIMEAudio(MIMENonMultipart):
_subtype = _whatsnd(_audiodata)
if _subtype is None:
raise TypeError('Could not find audio MIME subtype')
- MIMENonMultipart.__init__(self, 'audio', _subtype, **_params)
+ MIMENonMultipart.__init__(self, 'audio', _subtype, policy=policy,
+ **_params)
self.set_payload(_audiodata)
_encoder(self)
diff --git a/Lib/email/mime/base.py b/Lib/email/mime/base.py
index ac91925..1a3f9b5 100644
--- a/Lib/email/mime/base.py
+++ b/Lib/email/mime/base.py
@@ -6,6 +6,8 @@
__all__ = ['MIMEBase']
+import email.policy
+
from email import message
@@ -13,14 +15,16 @@ from email import message
class MIMEBase(message.Message):
"""Base class for MIME specializations."""
- def __init__(self, _maintype, _subtype, **_params):
+ def __init__(self, _maintype, _subtype, *, policy=None, **_params):
"""This constructor adds a Content-Type: and a MIME-Version: header.
The Content-Type: header is taken from the _maintype and _subtype
arguments. Additional parameters for this header are taken from the
keyword arguments.
"""
- message.Message.__init__(self)
+ if policy is None:
+ policy = email.policy.compat32
+ message.Message.__init__(self, policy=policy)
ctype = '%s/%s' % (_maintype, _subtype)
self.add_header('Content-Type', ctype, **_params)
self['MIME-Version'] = '1.0'
diff --git a/Lib/email/mime/image.py b/Lib/email/mime/image.py
index 5563823..9272464 100644
--- a/Lib/email/mime/image.py
+++ b/Lib/email/mime/image.py
@@ -17,7 +17,7 @@ class MIMEImage(MIMENonMultipart):
"""Class for generating image/* type MIME documents."""
def __init__(self, _imagedata, _subtype=None,
- _encoder=encoders.encode_base64, **_params):
+ _encoder=encoders.encode_base64, *, policy=None, **_params):
"""Create an image/* type MIME document.
_imagedata is a string containing the raw image data. If this data
@@ -41,6 +41,7 @@ class MIMEImage(MIMENonMultipart):
_subtype = imghdr.what(None, _imagedata)
if _subtype is None:
raise TypeError('Could not guess image MIME subtype')
- MIMENonMultipart.__init__(self, 'image', _subtype, **_params)
+ MIMENonMultipart.__init__(self, 'image', _subtype, policy=policy,
+ **_params)
self.set_payload(_imagedata)
_encoder(self)
diff --git a/Lib/email/mime/message.py b/Lib/email/mime/message.py
index 275dbfd..07e4f2d 100644
--- a/Lib/email/mime/message.py
+++ b/Lib/email/mime/message.py
@@ -14,7 +14,7 @@ from email.mime.nonmultipart import MIMENonMultipart
class MIMEMessage(MIMENonMultipart):
"""Class representing message/* MIME documents."""
- def __init__(self, _msg, _subtype='rfc822'):
+ def __init__(self, _msg, _subtype='rfc822', *, policy=None):
"""Create a message/* type MIME document.
_msg is a message object and must be an instance of Message, or a
@@ -24,7 +24,7 @@ class MIMEMessage(MIMENonMultipart):
default is "rfc822" (this is defined by the MIME standard, even though
the term "rfc822" is technically outdated by RFC 2822).
"""
- MIMENonMultipart.__init__(self, 'message', _subtype)
+ MIMENonMultipart.__init__(self, 'message', _subtype, policy=policy)
if not isinstance(_msg, message.Message):
raise TypeError('Argument is not an instance of Message')
# It's convenient to use this base class method. We need to do it
diff --git a/Lib/email/mime/multipart.py b/Lib/email/mime/multipart.py
index 9661865..2d3f288 100644
--- a/Lib/email/mime/multipart.py
+++ b/Lib/email/mime/multipart.py
@@ -14,6 +14,7 @@ class MIMEMultipart(MIMEBase):
"""Base class for MIME multipart/* type messages."""
def __init__(self, _subtype='mixed', boundary=None, _subparts=None,
+ *, policy=None,
**_params):
"""Creates a multipart/* type message.
@@ -33,7 +34,7 @@ class MIMEMultipart(MIMEBase):
Additional parameters for the Content-Type header are taken from the
keyword arguments (or passed into the _params argument).
"""
- MIMEBase.__init__(self, 'multipart', _subtype, **_params)
+ MIMEBase.__init__(self, 'multipart', _subtype, policy=policy, **_params)
# Initialise _payload to an empty list as the Message superclass's
# implementation of is_multipart assumes that _payload is a list for
diff --git a/Lib/email/mime/text.py b/Lib/email/mime/text.py
index da03086..35b4423 100644
--- a/Lib/email/mime/text.py
+++ b/Lib/email/mime/text.py
@@ -14,7 +14,7 @@ from email.mime.nonmultipart import MIMENonMultipart
class MIMEText(MIMENonMultipart):
"""Class for generating text/* type MIME documents."""
- def __init__(self, _text, _subtype='plain', _charset=None):
+ def __init__(self, _text, _subtype='plain', _charset=None, *, policy=None):
"""Create a text/* type MIME document.
_text is the string for this message object.
@@ -36,7 +36,7 @@ class MIMEText(MIMENonMultipart):
except UnicodeEncodeError:
_charset = 'utf-8'
- MIMENonMultipart.__init__(self, 'text', _subtype,
+ MIMENonMultipart.__init__(self, 'text', _subtype, policy=policy,
**{'charset': str(_charset)})
self.set_payload(_text, _charset)
diff --git a/Lib/email/policy.py b/Lib/email/policy.py
index 35d0e69..5131311ac 100644
--- a/Lib/email/policy.py
+++ b/Lib/email/policy.py
@@ -7,6 +7,7 @@ from email._policybase import Policy, Compat32, compat32, _extend_docstrings
from email.utils import _has_surrogates
from email.headerregistry import HeaderRegistry as HeaderRegistry
from email.contentmanager import raw_data_manager
+from email.message import EmailMessage
__all__ = [
'Compat32',
@@ -82,6 +83,7 @@ class EmailPolicy(Policy):
"""
+ message_factory = EmailMessage
utf8 = False
refold_source = 'long'
header_factory = HeaderRegistry()
diff --git a/Lib/encodings/__init__.py b/Lib/encodings/__init__.py
index 8dd7130..aa2fb7c 100644
--- a/Lib/encodings/__init__.py
+++ b/Lib/encodings/__init__.py
@@ -29,6 +29,7 @@ Written by Marc-Andre Lemburg (mal@lemburg.com).
"""#"
import codecs
+import sys
from . import aliases
_cache = {}
@@ -54,6 +55,7 @@ def normalize_encoding(encoding):
"""
if isinstance(encoding, bytes):
encoding = str(encoding, "ascii")
+
chars = []
punct = False
for c in encoding:
@@ -97,6 +99,8 @@ def search_function(encoding):
mod = __import__('encodings.' + modname, fromlist=_import_tail,
level=0)
except ImportError:
+ # ImportError may occur because 'encodings.(modname)' does not exist,
+ # or because it imports a name that does not exist (see mbcs and oem)
pass
else:
break
@@ -150,3 +154,16 @@ def search_function(encoding):
# Register the search_function in the Python codec registry
codecs.register(search_function)
+
+if sys.platform == 'win32':
+ def _alias_mbcs(encoding):
+ try:
+ import _bootlocale
+ if encoding == _bootlocale.getpreferredencoding(False):
+ import encodings.mbcs
+ return encodings.mbcs.getregentry()
+ except ImportError:
+ # Imports may fail while we are shutting down
+ pass
+
+ codecs.register(_alias_mbcs)
diff --git a/Lib/encodings/aliases.py b/Lib/encodings/aliases.py
index 67c828d..2e63c2f 100644
--- a/Lib/encodings/aliases.py
+++ b/Lib/encodings/aliases.py
@@ -458,6 +458,7 @@ aliases = {
'macturkish' : 'mac_turkish',
# mbcs codec
+ 'ansi' : 'mbcs',
'dbcs' : 'mbcs',
# ptcp154 codec
diff --git a/Lib/encodings/oem.py b/Lib/encodings/oem.py
new file mode 100644
index 0000000..2c3426b
--- /dev/null
+++ b/Lib/encodings/oem.py
@@ -0,0 +1,41 @@
+""" Python 'oem' Codec for Windows
+
+"""
+# Import them explicitly to cause an ImportError
+# on non-Windows systems
+from codecs import oem_encode, oem_decode
+# for IncrementalDecoder, IncrementalEncoder, ...
+import codecs
+
+### Codec APIs
+
+encode = oem_encode
+
+def decode(input, errors='strict'):
+ return oem_decode(input, errors, True)
+
+class IncrementalEncoder(codecs.IncrementalEncoder):
+ def encode(self, input, final=False):
+ return oem_encode(input, self.errors)[0]
+
+class IncrementalDecoder(codecs.BufferedIncrementalDecoder):
+ _buffer_decode = oem_decode
+
+class StreamWriter(codecs.StreamWriter):
+ encode = oem_encode
+
+class StreamReader(codecs.StreamReader):
+ decode = oem_decode
+
+### encodings module API
+
+def getregentry():
+ return codecs.CodecInfo(
+ name='oem',
+ encode=encode,
+ decode=decode,
+ incrementalencoder=IncrementalEncoder,
+ incrementaldecoder=IncrementalDecoder,
+ streamreader=StreamReader,
+ streamwriter=StreamWriter,
+ )
diff --git a/Lib/ensurepip/__init__.py b/Lib/ensurepip/__init__.py
index 25c5567..9f5d151 100644
--- a/Lib/ensurepip/__init__.py
+++ b/Lib/ensurepip/__init__.py
@@ -12,19 +12,6 @@ _SETUPTOOLS_VERSION = "28.8.0"
_PIP_VERSION = "9.0.1"
-# pip currently requires ssl support, so we try to provide a nicer
-# error message when that is missing (http://bugs.python.org/issue19744)
-_MISSING_SSL_MESSAGE = ("pip {} requires SSL/TLS".format(_PIP_VERSION))
-try:
- import ssl
-except ImportError:
- ssl = None
- def _require_ssl_for_pip():
- raise RuntimeError(_MISSING_SSL_MESSAGE)
-else:
- def _require_ssl_for_pip():
- pass
-
_PROJECTS = [
("setuptools", _SETUPTOOLS_VERSION),
("pip", _PIP_VERSION),
@@ -71,7 +58,6 @@ def bootstrap(*, root=None, upgrade=False, user=False,
if altinstall and default_pip:
raise ValueError("Cannot use altinstall and default_pip together")
- _require_ssl_for_pip()
_disable_pip_configuration_settings()
# By default, installing pip and setuptools installs all of the
@@ -133,7 +119,6 @@ def _uninstall_helper(*, verbosity=0):
print(msg.format(pip.__version__, _PIP_VERSION), file=sys.stderr)
return
- _require_ssl_for_pip()
_disable_pip_configuration_settings()
# Construct the arguments to be passed to the pip command
@@ -145,11 +130,6 @@ def _uninstall_helper(*, verbosity=0):
def _main(argv=None):
- if ssl is None:
- print("Ignoring ensurepip failure: {}".format(_MISSING_SSL_MESSAGE),
- file=sys.stderr)
- return
-
import argparse
parser = argparse.ArgumentParser(prog="python -m ensurepip")
parser.add_argument(
diff --git a/Lib/enum.py b/Lib/enum.py
index b8787d1..e79b038 100644
--- a/Lib/enum.py
+++ b/Lib/enum.py
@@ -1,8 +1,20 @@
import sys
-from collections import OrderedDict
from types import MappingProxyType, DynamicClassAttribute
+from functools import reduce
+from operator import or_ as _or_
-__all__ = ['Enum', 'IntEnum', 'unique']
+# try _collections first to reduce startup cost
+try:
+ from _collections import OrderedDict
+except ImportError:
+ from collections import OrderedDict
+
+
+__all__ = [
+ 'EnumMeta',
+ 'Enum', 'IntEnum', 'Flag', 'IntFlag',
+ 'auto', 'unique',
+ ]
def _is_descriptor(obj):
@@ -28,7 +40,6 @@ def _is_sunder(name):
name[-2:-1] != '_' and
len(name) > 2)
-
def _make_class_unpicklable(cls):
"""Make the given class un-picklable."""
def _break_on_call_reduce(self, proto):
@@ -36,6 +47,13 @@ def _make_class_unpicklable(cls):
cls.__reduce_ex__ = _break_on_call_reduce
cls.__module__ = '<unknown>'
+_auto_null = object()
+class auto:
+ """
+ Instances are replaced with an appropriate value in Enum class suites.
+ """
+ value = _auto_null
+
class _EnumDict(dict):
"""Track enum member order and ensure member names are not reused.
@@ -47,6 +65,7 @@ class _EnumDict(dict):
def __init__(self):
super().__init__()
self._member_names = []
+ self._last_values = []
def __setitem__(self, key, value):
"""Changes anything not dundered or not a descriptor.
@@ -58,21 +77,32 @@ class _EnumDict(dict):
"""
if _is_sunder(key):
- raise ValueError('_names_ are reserved for future Enum use')
+ if key not in (
+ '_order_', '_create_pseudo_member_',
+ '_generate_next_value_', '_missing_',
+ ):
+ raise ValueError('_names_ are reserved for future Enum use')
+ if key == '_generate_next_value_':
+ setattr(self, '_generate_next_value', value)
elif _is_dunder(key):
- pass
+ if key == '__order__':
+ key = '_order_'
elif key in self._member_names:
# descriptor overwriting an enum?
raise TypeError('Attempted to reuse key: %r' % key)
elif not _is_descriptor(value):
if key in self:
# enum overwriting a descriptor?
- raise TypeError('Key already defined as: %r' % self[key])
+ raise TypeError('%r already defined as: %r' % (key, self[key]))
+ if isinstance(value, auto):
+ if value.value == _auto_null:
+ value.value = self._generate_next_value(key, 1, len(self._member_names), self._last_values[:])
+ value = value.value
self._member_names.append(key)
+ self._last_values.append(value)
super().__setitem__(key, value)
-
# Dummy value for Enum as EnumMeta explicitly checks for it, but of course
# until EnumMeta finishes running the first time the Enum class doesn't exist.
# This is also why there are checks in EnumMeta like `if Enum is not None`
@@ -83,7 +113,13 @@ class EnumMeta(type):
"""Metaclass for Enum"""
@classmethod
def __prepare__(metacls, cls, bases):
- return _EnumDict()
+ # create the namespace dict
+ enum_dict = _EnumDict()
+ # inherit previous flags and _generate_next_value_ function
+ member_type, first_enum = metacls._get_mixins_(bases)
+ if first_enum is not None:
+ enum_dict['_generate_next_value_'] = getattr(first_enum, '_generate_next_value_', None)
+ return enum_dict
def __new__(metacls, cls, bases, classdict):
# an Enum class is final once enumeration items have been defined; it
@@ -96,12 +132,15 @@ class EnumMeta(type):
# save enum items into separate mapping so they don't get baked into
# the new class
- members = {k: classdict[k] for k in classdict._member_names}
+ enum_members = {k: classdict[k] for k in classdict._member_names}
for name in classdict._member_names:
del classdict[name]
+ # adjust the sunders
+ _order_ = classdict.pop('_order_', None)
+
# check for illegal enum names (any others?)
- invalid_names = set(members) & {'mro', }
+ invalid_names = set(enum_members) & {'mro', }
if invalid_names:
raise ValueError('Invalid enum member name: {0}'.format(
','.join(invalid_names)))
@@ -145,7 +184,7 @@ class EnumMeta(type):
# a custom __new__ is doing something funky with the values -- such as
# auto-numbering ;)
for member_name in classdict._member_names:
- value = members[member_name]
+ value = enum_members[member_name]
if not isinstance(value, tuple):
args = (value, )
else:
@@ -159,7 +198,10 @@ class EnumMeta(type):
else:
enum_member = __new__(enum_class, *args)
if not hasattr(enum_member, '_value_'):
- enum_member._value_ = member_type(*args)
+ if member_type is object:
+ enum_member._value_ = value
+ else:
+ enum_member._value_ = member_type(*args)
value = enum_member._value_
enum_member._name_ = member_name
enum_member.__objclass__ = enum_class
@@ -204,6 +246,14 @@ class EnumMeta(type):
if save_new:
enum_class.__new_member__ = __new__
enum_class.__new__ = Enum.__new__
+
+ # py3 support for definition order (helps keep py2/py3 code in sync)
+ if _order_ is not None:
+ if isinstance(_order_, str):
+ _order_ = _order_.replace(',', ' ').split()
+ if _order_ != enum_class._member_names_:
+ raise TypeError('member order does not match _order_')
+
return enum_class
def __bool__(self):
@@ -217,7 +267,7 @@ class EnumMeta(type):
This method is used both when an enum class is given a value to match
to an enumeration member (i.e. Color(3)) and for the functional API
- (i.e. Color = Enum('Color', names='red green blue')).
+ (i.e. Color = Enum('Color', names='RED GREEN BLUE')).
When used for the functional API:
@@ -325,13 +375,19 @@ class EnumMeta(type):
"""
metacls = cls.__class__
bases = (cls, ) if type is None else (type, cls)
+ _, first_enum = cls._get_mixins_(bases)
classdict = metacls.__prepare__(class_name, bases)
# special processing needed for names?
if isinstance(names, str):
names = names.replace(',', ' ').split()
if isinstance(names, (tuple, list)) and isinstance(names[0], str):
- names = [(e, i) for (i, e) in enumerate(names, start)]
+ original_names, names = names, []
+ last_values = []
+ for count, name in enumerate(original_names):
+ value = first_enum._generate_next_value_(name, start, count, last_values[:])
+ last_values.append(value)
+ names.append((name, value))
# Here, names is either an iterable of (name, value) or a mapping.
for item in names:
@@ -461,7 +517,7 @@ class Enum(metaclass=EnumMeta):
# without calling this method; this method is called by the metaclass'
# __call__ (i.e. Color(3) ), and by pickle
if type(value) is cls:
- # For lookups like Color(Color.red)
+ # For lookups like Color(Color.RED)
return value
# by-value search for a matching enum member
# see if it's in the reverse mapping (for hashable values)
@@ -473,6 +529,20 @@ class Enum(metaclass=EnumMeta):
for member in cls._member_map_.values():
if member._value_ == value:
return member
+ # still not found -- try _missing_ hook
+ return cls._missing_(value)
+
+ def _generate_next_value_(name, start, count, last_values):
+ for last_value in reversed(last_values):
+ try:
+ return last_value + 1
+ except TypeError:
+ pass
+ else:
+ return start
+
+ @classmethod
+ def _missing_(cls, value):
raise ValueError("%r is not a valid %s" % (value, cls.__name__))
def __repr__(self):
@@ -544,8 +614,21 @@ class Enum(metaclass=EnumMeta):
source = vars(source)
else:
source = module_globals
- members = {name: value for name, value in source.items()
- if filter(name)}
+ # We use an OrderedDict of sorted source keys so that the
+ # _value2member_map is populated in the same order every time
+ # for a consistent reverse mapping of number to name when there
+ # are multiple names for the same number rather than varying
+ # between runs due to hash randomization of the module dictionary.
+ members = [
+ (name, source[name])
+ for name in source.keys()
+ if filter(name)]
+ try:
+ # sort by value
+ members.sort(key=lambda t: (t[1], t[0]))
+ except TypeError:
+ # unless some values aren't comparable, in which case sort by name
+ members.sort(key=lambda t: t[0])
cls = cls(name, members, module=module)
cls.__reduce_ex__ = _reduce_ex_by_name
module_globals.update(cls.__members__)
@@ -560,6 +643,180 @@ class IntEnum(int, Enum):
def _reduce_ex_by_name(self, proto):
return self.name
+class Flag(Enum):
+ """Support for flags"""
+
+ def _generate_next_value_(name, start, count, last_values):
+ """
+ Generate the next value when not given.
+
+ name: the name of the member
+ start: the initital start value or None
+ count: the number of existing members
+ last_value: the last value assigned or None
+ """
+ if not count:
+ return start if start is not None else 1
+ for last_value in reversed(last_values):
+ try:
+ high_bit = _high_bit(last_value)
+ break
+ except Exception:
+ raise TypeError('Invalid Flag value: %r' % last_value) from None
+ return 2 ** (high_bit+1)
+
+ @classmethod
+ def _missing_(cls, value):
+ original_value = value
+ if value < 0:
+ value = ~value
+ possible_member = cls._create_pseudo_member_(value)
+ if original_value < 0:
+ possible_member = ~possible_member
+ return possible_member
+
+ @classmethod
+ def _create_pseudo_member_(cls, value):
+ """
+ Create a composite member iff value contains only members.
+ """
+ pseudo_member = cls._value2member_map_.get(value, None)
+ if pseudo_member is None:
+ # verify all bits are accounted for
+ _, extra_flags = _decompose(cls, value)
+ if extra_flags:
+ raise ValueError("%r is not a valid %s" % (value, cls.__name__))
+ # construct a singleton enum pseudo-member
+ pseudo_member = object.__new__(cls)
+ pseudo_member._name_ = None
+ pseudo_member._value_ = value
+ cls._value2member_map_[value] = pseudo_member
+ return pseudo_member
+
+ def __contains__(self, other):
+ if not isinstance(other, self.__class__):
+ return NotImplemented
+ return other._value_ & self._value_ == other._value_
+
+ def __repr__(self):
+ cls = self.__class__
+ if self._name_ is not None:
+ return '<%s.%s: %r>' % (cls.__name__, self._name_, self._value_)
+ members, uncovered = _decompose(cls, self._value_)
+ return '<%s.%s: %r>' % (
+ cls.__name__,
+ '|'.join([str(m._name_ or m._value_) for m in members]),
+ self._value_,
+ )
+
+ def __str__(self):
+ cls = self.__class__
+ if self._name_ is not None:
+ return '%s.%s' % (cls.__name__, self._name_)
+ members, uncovered = _decompose(cls, self._value_)
+ if len(members) == 1 and members[0]._name_ is None:
+ return '%s.%r' % (cls.__name__, members[0]._value_)
+ else:
+ return '%s.%s' % (
+ cls.__name__,
+ '|'.join([str(m._name_ or m._value_) for m in members]),
+ )
+
+ def __bool__(self):
+ return bool(self._value_)
+
+ def __or__(self, other):
+ if not isinstance(other, self.__class__):
+ return NotImplemented
+ return self.__class__(self._value_ | other._value_)
+
+ def __and__(self, other):
+ if not isinstance(other, self.__class__):
+ return NotImplemented
+ return self.__class__(self._value_ & other._value_)
+
+ def __xor__(self, other):
+ if not isinstance(other, self.__class__):
+ return NotImplemented
+ return self.__class__(self._value_ ^ other._value_)
+
+ def __invert__(self):
+ members, uncovered = _decompose(self.__class__, self._value_)
+ inverted_members = [
+ m for m in self.__class__
+ if m not in members and not m._value_ & self._value_
+ ]
+ inverted = reduce(_or_, inverted_members, self.__class__(0))
+ return self.__class__(inverted)
+
+
+class IntFlag(int, Flag):
+ """Support for integer-based Flags"""
+
+ @classmethod
+ def _missing_(cls, value):
+ if not isinstance(value, int):
+ raise ValueError("%r is not a valid %s" % (value, cls.__name__))
+ new_member = cls._create_pseudo_member_(value)
+ return new_member
+
+ @classmethod
+ def _create_pseudo_member_(cls, value):
+ pseudo_member = cls._value2member_map_.get(value, None)
+ if pseudo_member is None:
+ need_to_create = [value]
+ # get unaccounted for bits
+ _, extra_flags = _decompose(cls, value)
+ # timer = 10
+ while extra_flags:
+ # timer -= 1
+ bit = _high_bit(extra_flags)
+ flag_value = 2 ** bit
+ if (flag_value not in cls._value2member_map_ and
+ flag_value not in need_to_create
+ ):
+ need_to_create.append(flag_value)
+ if extra_flags == -flag_value:
+ extra_flags = 0
+ else:
+ extra_flags ^= flag_value
+ for value in reversed(need_to_create):
+ # construct singleton pseudo-members
+ pseudo_member = int.__new__(cls, value)
+ pseudo_member._name_ = None
+ pseudo_member._value_ = value
+ cls._value2member_map_[value] = pseudo_member
+ return pseudo_member
+
+ def __or__(self, other):
+ if not isinstance(other, (self.__class__, int)):
+ return NotImplemented
+ result = self.__class__(self._value_ | self.__class__(other)._value_)
+ return result
+
+ def __and__(self, other):
+ if not isinstance(other, (self.__class__, int)):
+ return NotImplemented
+ return self.__class__(self._value_ & self.__class__(other)._value_)
+
+ def __xor__(self, other):
+ if not isinstance(other, (self.__class__, int)):
+ return NotImplemented
+ return self.__class__(self._value_ ^ self.__class__(other)._value_)
+
+ __ror__ = __or__
+ __rand__ = __and__
+ __rxor__ = __xor__
+
+ def __invert__(self):
+ result = self.__class__(~self._value_)
+ return result
+
+
+def _high_bit(value):
+ """returns index of highest bit, or -1 if value is zero or negative"""
+ return value.bit_length() - 1
+
def unique(enumeration):
"""Class decorator for enumerations ensuring unique member values."""
duplicates = []
@@ -572,3 +829,40 @@ def unique(enumeration):
raise ValueError('duplicate values found in %r: %s' %
(enumeration, alias_details))
return enumeration
+
+def _decompose(flag, value):
+ """Extract all members from the value."""
+ # _decompose is only called if the value is not named
+ not_covered = value
+ negative = value < 0
+ if negative:
+ # only check for named flags
+ flags_to_check = [
+ (m, v)
+ for v, m in flag._value2member_map_.items()
+ if m.name is not None
+ ]
+ else:
+ # check for named flags and powers-of-two flags
+ flags_to_check = [
+ (m, v)
+ for v, m in flag._value2member_map_.items()
+ if m.name is not None or _power_of_two(v)
+ ]
+ members = []
+ for member, member_value in flags_to_check:
+ if member_value and member_value & value == member_value:
+ members.append(member)
+ not_covered &= ~member_value
+ if not members and value in flag._value2member_map_:
+ members.append(flag._value2member_map_[value])
+ members.sort(key=lambda m: m._value_, reverse=True)
+ if len(members) > 1 and members[0].value == value:
+ # we have the breakdown, don't need the value member itself
+ members.pop(0)
+ return members, not_covered
+
+def _power_of_two(value):
+ if value < 1:
+ return False
+ return value == 2 ** _high_bit(value)
diff --git a/Lib/fileinput.py b/Lib/fileinput.py
index d2b5206..721fe9c 100644
--- a/Lib/fileinput.py
+++ b/Lib/fileinput.py
@@ -75,13 +75,11 @@ XXX Possible additions:
import sys, os
__all__ = ["input", "close", "nextfile", "filename", "lineno", "filelineno",
- "isfirstline", "isstdin", "FileInput"]
+ "fileno", "isfirstline", "isstdin", "FileInput", "hook_compressed",
+ "hook_encoded"]
_state = None
-# No longer used
-DEFAULT_BUFSIZE = 8*1024
-
def input(files=None, inplace=False, backup="", bufsize=0,
mode="r", openhook=None):
"""Return an instance of the FileInput class, which can be iterated.
@@ -201,6 +199,10 @@ class FileInput:
self._files = files
self._inplace = inplace
self._backup = backup
+ if bufsize:
+ import warnings
+ warnings.warn('bufsize is deprecated and ignored',
+ DeprecationWarning, stacklevel=2)
self._savestdout = None
self._output = None
self._filename = None
@@ -398,9 +400,9 @@ def hook_compressed(filename, mode):
return open(filename, mode)
-def hook_encoded(encoding):
+def hook_encoded(encoding, errors=None):
def openhook(filename, mode):
- return open(filename, mode, encoding=encoding)
+ return open(filename, mode, encoding=encoding, errors=errors)
return openhook
diff --git a/Lib/fnmatch.py b/Lib/fnmatch.py
index 6330b0c..fd3b514 100644
--- a/Lib/fnmatch.py
+++ b/Lib/fnmatch.py
@@ -106,4 +106,4 @@ def translate(pat):
res = '%s[%s]' % (res, stuff)
else:
res = res + re.escape(c)
- return res + '\Z(?ms)'
+ return r'(?s:%s)\Z' % res
diff --git a/Lib/fractions.py b/Lib/fractions.py
index 9aabab3..8330202 100644
--- a/Lib/fractions.py
+++ b/Lib/fractions.py
@@ -81,7 +81,7 @@ class Fraction(numbers.Rational):
__slots__ = ('_numerator', '_denominator')
# We're immutable, so use __new__ not __init__
- def __new__(cls, numerator=0, denominator=None, _normalize=True):
+ def __new__(cls, numerator=0, denominator=None, *, _normalize=True):
"""Constructs a Rational.
Takes a string like '3/2' or '1.5', another Rational instance, a
@@ -125,17 +125,9 @@ class Fraction(numbers.Rational):
self._denominator = numerator.denominator
return self
- elif isinstance(numerator, float):
- # Exact conversion from float
- value = Fraction.from_float(numerator)
- self._numerator = value._numerator
- self._denominator = value._denominator
- return self
-
- elif isinstance(numerator, Decimal):
- value = Fraction.from_decimal(numerator)
- self._numerator = value._numerator
- self._denominator = value._denominator
+ elif isinstance(numerator, (float, Decimal)):
+ # Exact conversion
+ self._numerator, self._denominator = numerator.as_integer_ratio()
return self
elif isinstance(numerator, str):
@@ -210,10 +202,6 @@ class Fraction(numbers.Rational):
elif not isinstance(f, float):
raise TypeError("%s.from_float() only takes floats, not %r (%s)" %
(cls.__name__, f, type(f).__name__))
- if math.isnan(f):
- raise ValueError("Cannot convert %r to %s." % (f, cls.__name__))
- if math.isinf(f):
- raise OverflowError("Cannot convert %r to %s." % (f, cls.__name__))
return cls(*f.as_integer_ratio())
@classmethod
@@ -226,19 +214,7 @@ class Fraction(numbers.Rational):
raise TypeError(
"%s.from_decimal() only takes Decimals, not %r (%s)" %
(cls.__name__, dec, type(dec).__name__))
- if dec.is_infinite():
- raise OverflowError(
- "Cannot convert %s to %s." % (dec, cls.__name__))
- if dec.is_nan():
- raise ValueError("Cannot convert %s to %s." % (dec, cls.__name__))
- sign, digits, exp = dec.as_tuple()
- digits = int(''.join(map(str, digits)))
- if sign:
- digits = -digits
- if exp >= 0:
- return cls(digits * 10 ** exp)
- else:
- return cls(digits, 10 ** -exp)
+ return cls(*dec.as_integer_ratio())
def limit_denominator(self, max_denominator=1000000):
"""Closest Fraction to self with denominator at most max_denominator.
diff --git a/Lib/ftplib.py b/Lib/ftplib.py
index c416d85..8f36f53 100644
--- a/Lib/ftplib.py
+++ b/Lib/ftplib.py
@@ -36,13 +36,12 @@ python ftplib.py -d localhost -l -p -l
# Modified by Giampaolo Rodola' to add TLS support.
#
-import os
import sys
import socket
-import warnings
from socket import _GLOBAL_DEFAULT_TIMEOUT
-__all__ = ["FTP"]
+__all__ = ["FTP", "error_reply", "error_temp", "error_perm", "error_proto",
+ "all_errors"]
# Magic number from <socket.h>
MSG_OOB = 0x1 # Process data out of band
@@ -729,6 +728,10 @@ else:
if context is not None and certfile is not None:
raise ValueError("context and certfile arguments are mutually "
"exclusive")
+ if keyfile is not None or certfile is not None:
+ import warnings
+ warnings.warn("keyfile and certfile are deprecated, use a"
+ "custom context instead", DeprecationWarning, 2)
self.keyfile = keyfile
self.certfile = certfile
if context is None:
@@ -822,7 +825,7 @@ def parse150(resp):
if _150_re is None:
import re
_150_re = re.compile(
- "150 .* \((\d+) bytes\)", re.IGNORECASE | re.ASCII)
+ r"150 .* \((\d+) bytes\)", re.IGNORECASE | re.ASCII)
m = _150_re.match(resp)
if not m:
return None
diff --git a/Lib/functools.py b/Lib/functools.py
index 60cf3c4..89f2cf4 100644
--- a/Lib/functools.py
+++ b/Lib/functools.py
@@ -21,6 +21,7 @@ from abc import get_cache_token
from collections import namedtuple
from types import MappingProxyType
from weakref import WeakKeyDictionary
+from reprlib import recursive_repr
try:
from _thread import RLock
except ImportError:
@@ -237,26 +238,83 @@ except ImportError:
################################################################################
# Purely functional, no descriptor behaviour
-def partial(func, *args, **keywords):
+class partial:
"""New function with partial application of the given arguments
and keywords.
"""
- if hasattr(func, 'func'):
- args = func.args + args
- tmpkw = func.keywords.copy()
- tmpkw.update(keywords)
- keywords = tmpkw
- del tmpkw
- func = func.func
-
- def newfunc(*fargs, **fkeywords):
- newkeywords = keywords.copy()
- newkeywords.update(fkeywords)
- return func(*(args + fargs), **newkeywords)
- newfunc.func = func
- newfunc.args = args
- newfunc.keywords = keywords
- return newfunc
+
+ __slots__ = "func", "args", "keywords", "__dict__", "__weakref__"
+
+ def __new__(*args, **keywords):
+ if not args:
+ raise TypeError("descriptor '__new__' of partial needs an argument")
+ if len(args) < 2:
+ raise TypeError("type 'partial' takes at least one argument")
+ cls, func, *args = args
+ if not callable(func):
+ raise TypeError("the first argument must be callable")
+ args = tuple(args)
+
+ if hasattr(func, "func"):
+ args = func.args + args
+ tmpkw = func.keywords.copy()
+ tmpkw.update(keywords)
+ keywords = tmpkw
+ del tmpkw
+ func = func.func
+
+ self = super(partial, cls).__new__(cls)
+
+ self.func = func
+ self.args = args
+ self.keywords = keywords
+ return self
+
+ def __call__(*args, **keywords):
+ if not args:
+ raise TypeError("descriptor '__call__' of partial needs an argument")
+ self, *args = args
+ newkeywords = self.keywords.copy()
+ newkeywords.update(keywords)
+ return self.func(*self.args, *args, **newkeywords)
+
+ @recursive_repr()
+ def __repr__(self):
+ qualname = type(self).__qualname__
+ args = [repr(self.func)]
+ args.extend(repr(x) for x in self.args)
+ args.extend(f"{k}={v!r}" for (k, v) in self.keywords.items())
+ if type(self).__module__ == "functools":
+ return f"functools.{qualname}({', '.join(args)})"
+ return f"{qualname}({', '.join(args)})"
+
+ def __reduce__(self):
+ return type(self), (self.func,), (self.func, self.args,
+ self.keywords or None, self.__dict__ or None)
+
+ def __setstate__(self, state):
+ if not isinstance(state, tuple):
+ raise TypeError("argument to __setstate__ must be a tuple")
+ if len(state) != 4:
+ raise TypeError(f"expected 4 items in state, got {len(state)}")
+ func, args, kwds, namespace = state
+ if (not callable(func) or not isinstance(args, tuple) or
+ (kwds is not None and not isinstance(kwds, dict)) or
+ (namespace is not None and not isinstance(namespace, dict))):
+ raise TypeError("invalid partial state")
+
+ args = tuple(args) # just in case it's a subclass
+ if kwds is None:
+ kwds = {}
+ elif type(kwds) is not dict: # XXX does it need to be *exactly* dict?
+ kwds = dict(kwds)
+ if namespace is None:
+ namespace = {}
+
+ self.__dict__ = namespace
+ self.func = func
+ self.args = args
+ self.keywords = kwds
try:
from _functools import partial
@@ -363,7 +421,7 @@ class _HashedSeq(list):
def _make_key(args, kwds, typed,
kwd_mark = (object(),),
fasttypes = {int, str, frozenset, type(None)},
- sorted=sorted, tuple=tuple, type=type, len=len):
+ tuple=tuple, type=type, len=len):
"""Make a cache key from optionally typed positional and keyword arguments
The key is constructed in a way that is flat as possible rather than
@@ -376,14 +434,13 @@ def _make_key(args, kwds, typed,
"""
key = args
if kwds:
- sorted_items = sorted(kwds.items())
key += kwd_mark
- for item in sorted_items:
+ for item in kwds.items():
key += item
if typed:
key += tuple(type(v) for v in args)
if kwds:
- key += tuple(type(v) for k, v in sorted_items)
+ key += tuple(type(v) for v in kwds.values())
elif len(key) == 1 and type(key[0]) in fasttypes:
return key[0]
return _HashedSeq(key)
@@ -435,6 +492,7 @@ def _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo):
hits = misses = 0
full = False
cache_get = cache.get # bound method to lookup a key or return None
+ cache_len = cache.__len__ # get cache size without calling len()
lock = RLock() # because linkedlist updates aren't threadsafe
root = [] # root of the circular doubly linked list
root[:] = [root, root, None, None] # initialize by pointing to self
@@ -516,16 +574,16 @@ def _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo):
last = root[PREV]
link = [last, root, key, result]
last[NEXT] = root[PREV] = cache[key] = link
- # Use the __len__() method instead of the len() function
+ # Use the cache_len bound method instead of the len() function
# which could potentially be wrapped in an lru_cache itself.
- full = (cache.__len__() >= maxsize)
+ full = (cache_len() >= maxsize)
misses += 1
return result
def cache_info():
"""Report cache statistics"""
with lock:
- return _CacheInfo(hits, misses, maxsize, cache.__len__())
+ return _CacheInfo(hits, misses, maxsize, cache_len())
def cache_clear():
"""Clear the cache and cache statistics"""
diff --git a/Lib/genericpath.py b/Lib/genericpath.py
index 6714061..303b3b3 100644
--- a/Lib/genericpath.py
+++ b/Lib/genericpath.py
@@ -69,6 +69,12 @@ def getctime(filename):
def commonprefix(m):
"Given a list of pathnames, returns the longest common leading component"
if not m: return ''
+ # Some people pass in a list of pathname parts to operate in an OS-agnostic
+ # fashion; don't try to translate in that case as that's an abuse of the
+ # API and they are already doing what they need to be OS-agnostic and so
+ # they most likely won't be using an os.PathLike object in the sublists.
+ if not isinstance(m[0], (list, tuple)):
+ m = tuple(map(os.fspath, m))
s1 = min(m)
s2 = max(m)
for i, c in enumerate(s1):
diff --git a/Lib/glob.py b/Lib/glob.py
index 16330d8..002cd92 100644
--- a/Lib/glob.py
+++ b/Lib/glob.py
@@ -30,15 +30,16 @@ def iglob(pathname, *, recursive=False):
If recursive is true, the pattern '**' will match any files and
zero or more directories and subdirectories.
"""
- it = _iglob(pathname, recursive)
+ it = _iglob(pathname, recursive, False)
if recursive and _isrecursive(pathname):
s = next(it) # skip empty string
assert not s
return it
-def _iglob(pathname, recursive):
+def _iglob(pathname, recursive, dironly):
dirname, basename = os.path.split(pathname)
if not has_magic(pathname):
+ assert not dironly
if basename:
if os.path.lexists(pathname):
yield pathname
@@ -49,47 +50,39 @@ def _iglob(pathname, recursive):
return
if not dirname:
if recursive and _isrecursive(basename):
- yield from glob2(dirname, basename)
+ yield from _glob2(dirname, basename, dironly)
else:
- yield from glob1(dirname, basename)
+ yield from _glob1(dirname, basename, dironly)
return
# `os.path.split()` returns the argument itself as a dirname if it is a
# drive or UNC path. Prevent an infinite recursion if a drive or UNC path
# contains magic characters (i.e. r'\\?\C:').
if dirname != pathname and has_magic(dirname):
- dirs = _iglob(dirname, recursive)
+ dirs = _iglob(dirname, recursive, True)
else:
dirs = [dirname]
if has_magic(basename):
if recursive and _isrecursive(basename):
- glob_in_dir = glob2
+ glob_in_dir = _glob2
else:
- glob_in_dir = glob1
+ glob_in_dir = _glob1
else:
- glob_in_dir = glob0
+ glob_in_dir = _glob0
for dirname in dirs:
- for name in glob_in_dir(dirname, basename):
+ for name in glob_in_dir(dirname, basename, dironly):
yield os.path.join(dirname, name)
# These 2 helper functions non-recursively glob inside a literal directory.
-# They return a list of basenames. `glob1` accepts a pattern while `glob0`
+# They return a list of basenames. _glob1 accepts a pattern while _glob0
# takes a literal basename (so it only has to check for its existence).
-def glob1(dirname, pattern):
- if not dirname:
- if isinstance(pattern, bytes):
- dirname = bytes(os.curdir, 'ASCII')
- else:
- dirname = os.curdir
- try:
- names = os.listdir(dirname)
- except OSError:
- return []
+def _glob1(dirname, pattern, dironly):
+ names = list(_iterdir(dirname, dironly))
if not _ishidden(pattern):
- names = [x for x in names if not _ishidden(x)]
+ names = (x for x in names if not _ishidden(x))
return fnmatch.filter(names, pattern)
-def glob0(dirname, basename):
+def _glob0(dirname, basename, dironly):
if not basename:
# `os.path.split()` returns an empty basename for paths ending with a
# directory separator. 'q*x/' should match only directories.
@@ -100,30 +93,49 @@ def glob0(dirname, basename):
return [basename]
return []
+# Following functions are not public but can be used by third-party code.
+
+def glob0(dirname, pattern):
+ return _glob0(dirname, pattern, False)
+
+def glob1(dirname, pattern):
+ return _glob1(dirname, pattern, False)
+
# This helper function recursively yields relative pathnames inside a literal
# directory.
-def glob2(dirname, pattern):
+def _glob2(dirname, pattern, dironly):
assert _isrecursive(pattern)
yield pattern[:0]
- yield from _rlistdir(dirname)
+ yield from _rlistdir(dirname, dironly)
-# Recursively yields relative pathnames inside a literal directory.
-def _rlistdir(dirname):
+# If dironly is false, yields all file names inside a directory.
+# If dironly is true, yields only directory names.
+def _iterdir(dirname, dironly):
if not dirname:
if isinstance(dirname, bytes):
dirname = bytes(os.curdir, 'ASCII')
else:
dirname = os.curdir
try:
- names = os.listdir(dirname)
- except os.error:
+ with os.scandir(dirname) as it:
+ for entry in it:
+ try:
+ if not dironly or entry.is_dir():
+ yield entry.name
+ except OSError:
+ pass
+ except OSError:
return
+
+# Recursively yields relative pathnames inside a literal directory.
+def _rlistdir(dirname, dironly):
+ names = list(_iterdir(dirname, dironly))
for x in names:
if not _ishidden(x):
yield x
path = os.path.join(dirname, x) if dirname else x
- for y in _rlistdir(path):
+ for y in _rlistdir(path, dironly):
yield os.path.join(x, y)
diff --git a/Lib/gzip.py b/Lib/gzip.py
index da4479e..76ab497 100644
--- a/Lib/gzip.py
+++ b/Lib/gzip.py
@@ -49,7 +49,7 @@ def open(filename, mode="rb", compresslevel=9,
raise ValueError("Argument 'newline' not supported in binary mode")
gz_mode = mode.replace("t", "")
- if isinstance(filename, (str, bytes)):
+ if isinstance(filename, (str, bytes, os.PathLike)):
binary_file = GzipFile(filename, gz_mode, compresslevel)
elif hasattr(filename, "read") or hasattr(filename, "write"):
binary_file = GzipFile(None, gz_mode, compresslevel, filename)
@@ -165,6 +165,8 @@ class GzipFile(_compression.BaseStream):
filename = getattr(fileobj, 'name', '')
if not isinstance(filename, (str, bytes)):
filename = ''
+ else:
+ filename = os.fspath(filename)
if mode is None:
mode = getattr(fileobj, 'mode', 'rb')
@@ -357,10 +359,10 @@ class GzipFile(_compression.BaseStream):
if offset < self.offset:
raise OSError('Negative seek in write mode')
count = offset - self.offset
- chunk = bytes(1024)
+ chunk = b'\0' * 1024
for i in range(count // 1024):
self.write(chunk)
- self.write(bytes(count % 1024))
+ self.write(b'\0' * (count % 1024))
elif self.mode == READ:
self._check_not_closed()
return self._buffer.seek(offset, whence)
diff --git a/Lib/hashlib.py b/Lib/hashlib.py
index 316cece..053a7ad 100644
--- a/Lib/hashlib.py
+++ b/Lib/hashlib.py
@@ -4,14 +4,15 @@
__doc__ = """hashlib module - A common interface to many hash functions.
-new(name, data=b'') - returns a new hash object implementing the
- given hash function; initializing the hash
- using the given binary data.
+new(name, data=b'', **kwargs) - returns a new hash object implementing the
+ given hash function; initializing the hash
+ using the given binary data.
Named constructor functions are also available, these are faster
than using new(name):
-md5(), sha1(), sha224(), sha256(), sha384(), and sha512()
+md5(), sha1(), sha224(), sha256(), sha384(), sha512(), blake2b(), blake2s(),
+sha3_224, sha3_256, sha3_384, sha3_512, shake_128, and shake_256.
More algorithms may be available on your platform but the above are guaranteed
to exist. See the algorithms_guaranteed and algorithms_available attributes
@@ -54,7 +55,11 @@ More condensed:
# This tuple and __get_builtin_constructor() must be modified if a new
# always available algorithm is added.
-__always_supported = ('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512')
+__always_supported = ('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512',
+ 'blake2b', 'blake2s',
+ 'sha3_224', 'sha3_256', 'sha3_384', 'sha3_512',
+ 'shake_128', 'shake_256')
+
algorithms_guaranteed = set(__always_supported)
algorithms_available = set(__always_supported)
@@ -85,6 +90,19 @@ def __get_builtin_constructor(name):
import _sha512
cache['SHA384'] = cache['sha384'] = _sha512.sha384
cache['SHA512'] = cache['sha512'] = _sha512.sha512
+ elif name in ('blake2b', 'blake2s'):
+ import _blake2
+ cache['blake2b'] = _blake2.blake2b
+ cache['blake2s'] = _blake2.blake2s
+ elif name in {'sha3_224', 'sha3_256', 'sha3_384', 'sha3_512',
+ 'shake_128', 'shake_256'}:
+ import _sha3
+ cache['sha3_224'] = _sha3.sha3_224
+ cache['sha3_256'] = _sha3.sha3_256
+ cache['sha3_384'] = _sha3.sha3_384
+ cache['sha3_512'] = _sha3.sha3_512
+ cache['shake_128'] = _sha3.shake_128
+ cache['shake_256'] = _sha3.shake_256
except ImportError:
pass # no extension module, this hash is unsupported.
@@ -96,6 +114,9 @@ def __get_builtin_constructor(name):
def __get_openssl_constructor(name):
+ if name in {'blake2b', 'blake2s'}:
+ # Prefer our blake2 implementation.
+ return __get_builtin_constructor(name)
try:
f = getattr(_hashlib, 'openssl_' + name)
# Allow the C module to raise ValueError. The function will be
@@ -107,17 +128,23 @@ def __get_openssl_constructor(name):
return __get_builtin_constructor(name)
-def __py_new(name, data=b''):
- """new(name, data=b'') - Return a new hashing object using the named algorithm;
- optionally initialized with data (which must be bytes).
+def __py_new(name, data=b'', **kwargs):
+ """new(name, data=b'', **kwargs) - Return a new hashing object using the
+ named algorithm; optionally initialized with data (which must be bytes).
"""
- return __get_builtin_constructor(name)(data)
+ return __get_builtin_constructor(name)(data, **kwargs)
-def __hash_new(name, data=b''):
+def __hash_new(name, data=b'', **kwargs):
"""new(name, data=b'') - Return a new hashing object using the named algorithm;
optionally initialized with data (which must be bytes).
"""
+ if name in {'blake2b', 'blake2s'}:
+ # Prefer our blake2 implementation.
+ # OpenSSL 1.1.0 comes with a limited implementation of blake2b/s.
+ # It does neither support keyed blake2 nor advanced features like
+ # salt, personal, tree hashing or SSE.
+ return __get_builtin_constructor(name)(data, **kwargs)
try:
return _hashlib.new(name, data)
except ValueError:
@@ -202,6 +229,12 @@ except ImportError:
return dkey[:dklen]
+try:
+ # OpenSSL's scrypt requires OpenSSL 1.1+
+ from _hashlib import scrypt
+except ImportError:
+ pass
+
for __func_name in __always_supported:
# try them all, some may not work due to the OpenSSL
@@ -212,6 +245,7 @@ for __func_name in __always_supported:
import logging
logging.exception('code for hash %s was not found.', __func_name)
+
# Cleanup locals()
del __always_supported, __func_name, __get_hash
del __py_new, __hash_new, __get_openssl_constructor
diff --git a/Lib/hmac.py b/Lib/hmac.py
index 77785a2..121029a 100644
--- a/Lib/hmac.py
+++ b/Lib/hmac.py
@@ -77,7 +77,7 @@ class HMAC:
if len(key) > blocksize:
key = self.digest_cons(key).digest()
- key = key + bytes(blocksize - len(key))
+ key = key.ljust(blocksize, b'\0')
self.outer.update(key.translate(trans_5C))
self.inner.update(key.translate(trans_36))
if msg is not None:
diff --git a/Lib/html/parser.py b/Lib/html/parser.py
index b781c63..ef869bc 100644
--- a/Lib/html/parser.py
+++ b/Lib/html/parser.py
@@ -34,7 +34,7 @@ commentclose = re.compile(r'--\s*>')
# explode, so don't do it.
# see http://www.w3.org/TR/html5/tokenization.html#tag-open-state
# and http://www.w3.org/TR/html5/tokenization.html#tag-name-state
-tagfind_tolerant = re.compile('([a-zA-Z][^\t\n\r\f />\x00]*)(?:\s|/(?!>))*')
+tagfind_tolerant = re.compile(r'([a-zA-Z][^\t\n\r\f />\x00]*)(?:\s|/(?!>))*')
attrfind_tolerant = re.compile(
r'((?<=[\'"\s/])[^\s/>][^\s/=>]*)(\s*=+\s*'
r'(\'[^\']*\'|"[^"]*"|(?![\'"])[^>\s]*))?(?:\s|/(?!>))*')
@@ -56,7 +56,7 @@ locatestarttagend_tolerant = re.compile(r"""
endendtag = re.compile('>')
# the HTML 5 spec, section 8.1.2.2, doesn't allow spaces between
# </ and the tag name, so maybe this should be fixed
-endtagfind = re.compile('</\s*([a-zA-Z][-.a-zA-Z0-9:_]*)\s*>')
+endtagfind = re.compile(r'</\s*([a-zA-Z][-.a-zA-Z0-9:_]*)\s*>')
diff --git a/Lib/http/client.py b/Lib/http/client.py
index 352c101..a8e59b9 100644
--- a/Lib/http/client.py
+++ b/Lib/http/client.py
@@ -1,4 +1,4 @@
-"""HTTP/1.1 client library
+r"""HTTP/1.1 client library
<intro stuff goes here>
<other stuff, too>
@@ -420,6 +420,7 @@ class HTTPResponse(io.BufferedIOBase):
self.fp.flush()
def readable(self):
+ """Always returns True"""
return True
# End of "raw stream" methods
@@ -467,6 +468,10 @@ class HTTPResponse(io.BufferedIOBase):
return s
def readinto(self, b):
+ """Read up to len(b) bytes into bytearray b and return the number
+ of bytes read.
+ """
+
if self.fp is None:
return 0
@@ -706,6 +711,17 @@ class HTTPResponse(io.BufferedIOBase):
return self.fp.fileno()
def getheader(self, name, default=None):
+ '''Returns the value of the header matching *name*.
+
+ If there are multiple matching headers, the values are
+ combined into a single string separated by commas and spaces.
+
+ If no matching header is found, returns *default* or None if
+ the *default* is not specified.
+
+ If the headers are unknown, raises http.client.ResponseNotReady.
+
+ '''
if self.headers is None:
raise ResponseNotReady()
headers = self.headers.get_all(name) or default
@@ -728,12 +744,45 @@ class HTTPResponse(io.BufferedIOBase):
# For compatibility with old-style urllib responses.
def info(self):
+ '''Returns an instance of the class mimetools.Message containing
+ meta-information associated with the URL.
+
+ When the method is HTTP, these headers are those returned by
+ the server at the head of the retrieved HTML page (including
+ Content-Length and Content-Type).
+
+ When the method is FTP, a Content-Length header will be
+ present if (as is now usual) the server passed back a file
+ length in response to the FTP retrieval request. A
+ Content-Type header will be present if the MIME type can be
+ guessed.
+
+ When the method is local-file, returned headers will include
+ a Date representing the file's last-modified time, a
+ Content-Length giving file size, and a Content-Type
+ containing a guess at the file's type. See also the
+ description of the mimetools module.
+
+ '''
return self.headers
def geturl(self):
+ '''Return the real URL of the page.
+
+ In some cases, the HTTP server redirects a client to another
+ URL. The urlopen() function handles this transparently, but in
+ some cases the caller needs to know which URL the client was
+ redirected to. The geturl() method can be used to get at this
+ redirected URL.
+
+ '''
return self.url
def getcode(self):
+ '''Return the HTTP status code that was sent with the response,
+ or None if the URL is not an HTTP URL.
+
+ '''
return self.status
class HTTPConnection:
@@ -746,6 +795,44 @@ class HTTPConnection:
auto_open = 1
debuglevel = 0
+ @staticmethod
+ def _is_textIO(stream):
+ """Test whether a file-like object is a text or a binary stream.
+ """
+ return isinstance(stream, io.TextIOBase)
+
+ @staticmethod
+ def _get_content_length(body, method):
+ """Get the content-length based on the body.
+
+ If the body is None, we set Content-Length: 0 for methods that expect
+ a body (RFC 7230, Section 3.3.2). We also set the Content-Length for
+ any method if the body is a str or bytes-like object and not a file.
+ """
+ if body is None:
+ # do an explicit check for not None here to distinguish
+ # between unset and set but empty
+ if method.upper() in _METHODS_EXPECTING_BODY:
+ return 0
+ else:
+ return None
+
+ if hasattr(body, 'read'):
+ # file-like object.
+ return None
+
+ try:
+ # does it implement the buffer protocol (bytes, bytearray, array)?
+ mv = memoryview(body)
+ return mv.nbytes
+ except TypeError:
+ pass
+
+ if isinstance(body, str):
+ return len(body)
+
+ return None
+
def __init__(self, host, port=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
source_address=None):
self.timeout = timeout
@@ -884,18 +971,9 @@ class HTTPConnection:
if hasattr(data, "read") :
if self.debuglevel > 0:
print("sendIng a read()able")
- encode = False
- try:
- mode = data.mode
- except AttributeError:
- # io.BytesIO and other file-like objects don't have a `mode`
- # attribute.
- pass
- else:
- if "b" not in mode:
- encode = True
- if self.debuglevel > 0:
- print("encoding file using iso-8859-1")
+ encode = self._is_textIO(data)
+ if encode and self.debuglevel > 0:
+ print("encoding file using iso-8859-1")
while 1:
datablock = data.read(blocksize)
if not datablock:
@@ -921,7 +999,22 @@ class HTTPConnection:
"""
self._buffer.append(s)
- def _send_output(self, message_body=None):
+ def _read_readable(self, readable):
+ blocksize = 8192
+ if self.debuglevel > 0:
+ print("sendIng a read()able")
+ encode = self._is_textIO(readable)
+ if encode and self.debuglevel > 0:
+ print("encoding file using iso-8859-1")
+ while True:
+ datablock = readable.read(blocksize)
+ if not datablock:
+ break
+ if encode:
+ datablock = datablock.encode("iso-8859-1")
+ yield datablock
+
+ def _send_output(self, message_body=None, encode_chunked=False):
"""Send the currently buffered request and clear the buffer.
Appends an extra \\r\\n to the buffer.
@@ -930,10 +1023,50 @@ class HTTPConnection:
self._buffer.extend((b"", b""))
msg = b"\r\n".join(self._buffer)
del self._buffer[:]
-
self.send(msg)
+
if message_body is not None:
- self.send(message_body)
+
+ # create a consistent interface to message_body
+ if hasattr(message_body, 'read'):
+ # Let file-like take precedence over byte-like. This
+ # is needed to allow the current position of mmap'ed
+ # files to be taken into account.
+ chunks = self._read_readable(message_body)
+ else:
+ try:
+ # this is solely to check to see if message_body
+ # implements the buffer API. it /would/ be easier
+ # to capture if PyObject_CheckBuffer was exposed
+ # to Python.
+ memoryview(message_body)
+ except TypeError:
+ try:
+ chunks = iter(message_body)
+ except TypeError:
+ raise TypeError("message_body should be a bytes-like "
+ "object or an iterable, got %r"
+ % type(message_body))
+ else:
+ # the object implements the buffer interface and
+ # can be passed directly into socket methods
+ chunks = (message_body,)
+
+ for chunk in chunks:
+ if not chunk:
+ if self.debuglevel > 0:
+ print('Zero length chunk ignored')
+ continue
+
+ if encode_chunked and self._http_vsn == 11:
+ # chunked encoding
+ chunk = f'{len(chunk):X}\r\n'.encode('ascii') + chunk \
+ + b'\r\n'
+ self.send(chunk)
+
+ if encode_chunked and self._http_vsn == 11:
+ # end chunked transfer
+ self.send(b'0\r\n\r\n')
def putrequest(self, method, url, skip_host=False,
skip_accept_encoding=False):
@@ -1087,52 +1220,27 @@ class HTTPConnection:
header = header + b': ' + value
self._output(header)
- def endheaders(self, message_body=None):
+ def endheaders(self, message_body=None, *, encode_chunked=False):
"""Indicate that the last header line has been sent to the server.
This method sends the request to the server. The optional message_body
argument can be used to pass a message body associated with the
- request. The message body will be sent in the same packet as the
- message headers if it is a string, otherwise it is sent as a separate
- packet.
+ request.
"""
if self.__state == _CS_REQ_STARTED:
self.__state = _CS_REQ_SENT
else:
raise CannotSendHeader()
- self._send_output(message_body)
+ self._send_output(message_body, encode_chunked=encode_chunked)
- def request(self, method, url, body=None, headers={}):
+ def request(self, method, url, body=None, headers={}, *,
+ encode_chunked=False):
"""Send a complete request to the server."""
- self._send_request(method, url, body, headers)
-
- def _set_content_length(self, body, method):
- # Set the content-length based on the body. If the body is "empty", we
- # set Content-Length: 0 for methods that expect a body (RFC 7230,
- # Section 3.3.2). If the body is set for other methods, we set the
- # header provided we can figure out what the length is.
- thelen = None
- method_expects_body = method.upper() in _METHODS_EXPECTING_BODY
- if body is None and method_expects_body:
- thelen = '0'
- elif body is not None:
- try:
- thelen = str(len(body))
- except TypeError:
- # If this is a file-like object, try to
- # fstat its file descriptor
- try:
- thelen = str(os.fstat(body.fileno()).st_size)
- except (AttributeError, OSError):
- # Don't send a length if this failed
- if self.debuglevel > 0: print("Cannot stat!!")
-
- if thelen is not None:
- self.putheader('Content-Length', thelen)
+ self._send_request(method, url, body, headers, encode_chunked)
- def _send_request(self, method, url, body, headers):
+ def _send_request(self, method, url, body, headers, encode_chunked):
# Honor explicitly requested Host: and Accept-Encoding: headers.
- header_names = dict.fromkeys([k.lower() for k in headers])
+ header_names = frozenset(k.lower() for k in headers)
skips = {}
if 'host' in header_names:
skips['skip_host'] = 1
@@ -1141,15 +1249,40 @@ class HTTPConnection:
self.putrequest(method, url, **skips)
+ # chunked encoding will happen if HTTP/1.1 is used and either
+ # the caller passes encode_chunked=True or the following
+ # conditions hold:
+ # 1. content-length has not been explicitly set
+ # 2. the body is a file or iterable, but not a str or bytes-like
+ # 3. Transfer-Encoding has NOT been explicitly set by the caller
+
if 'content-length' not in header_names:
- self._set_content_length(body, method)
+ # only chunk body if not explicitly set for backwards
+ # compatibility, assuming the client code is already handling the
+ # chunking
+ if 'transfer-encoding' not in header_names:
+ # if content-length cannot be automatically determined, fall
+ # back to chunked encoding
+ encode_chunked = False
+ content_length = self._get_content_length(body, method)
+ if content_length is None:
+ if body is not None:
+ if self.debuglevel > 0:
+ print('Unable to determine size of %r' % body)
+ encode_chunked = True
+ self.putheader('Transfer-Encoding', 'chunked')
+ else:
+ self.putheader('Content-Length', str(content_length))
+ else:
+ encode_chunked = False
+
for hdr, value in headers.items():
self.putheader(hdr, value)
if isinstance(body, str):
# RFC 2616 Section 3.7.1 says that text default has a
# default charset of iso-8859-1.
body = _encode(body, 'body')
- self.endheaders(body)
+ self.endheaders(body, encode_chunked=encode_chunked)
def getresponse(self):
"""Get the response from the server.
@@ -1232,6 +1365,12 @@ else:
check_hostname=None):
super(HTTPSConnection, self).__init__(host, port, timeout,
source_address)
+ if (key_file is not None or cert_file is not None or
+ check_hostname is not None):
+ import warnings
+ warnings.warn("key_file, cert_file and check_hostname are "
+ "deprecated, use a custom context instead.",
+ DeprecationWarning, 2)
self.key_file = key_file
self.cert_file = cert_file
if context is None:
diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py
index 6d4572a..adf956d 100644
--- a/Lib/http/cookiejar.py
+++ b/Lib/http/cookiejar.py
@@ -200,7 +200,7 @@ def _str2time(day, mon, yr, hr, min, sec, tz):
STRICT_DATE_RE = re.compile(
r"^[SMTWF][a-z][a-z], (\d\d) ([JFMASOND][a-z][a-z]) "
- "(\d\d\d\d) (\d\d):(\d\d):(\d\d) GMT$", re.ASCII)
+ r"(\d\d\d\d) (\d\d):(\d\d):(\d\d) GMT$", re.ASCII)
WEEKDAY_RE = re.compile(
r"^(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)[a-z]*,?\s*", re.I | re.ASCII)
LOOSE_HTTP_DATE_RE = re.compile(
@@ -277,7 +277,7 @@ def http2time(text):
return _str2time(day, mon, yr, hr, min, sec, tz)
ISO_DATE_RE = re.compile(
- """^
+ r"""^
(\d{4}) # year
[-\/]?
(\d\d?) # numerical month
@@ -411,7 +411,7 @@ def split_header_words(header_values):
pairs = []
else:
# skip junk
- non_junk, nr_junk_chars = re.subn("^[=\s;]*", "", text)
+ non_junk, nr_junk_chars = re.subn(r"^[=\s;]*", "", text)
assert nr_junk_chars > 0, (
"split_header_words bug: '%s', '%s', %s" %
(orig_text, text, pairs))
diff --git a/Lib/http/cookies.py b/Lib/http/cookies.py
index a73fe38..be3b080 100644
--- a/Lib/http/cookies.py
+++ b/Lib/http/cookies.py
@@ -456,9 +456,8 @@ class Morsel(dict):
#
_LegalKeyChars = r"\w\d!#%&'~_`><@,:/\$\*\+\-\.\^\|\)\(\?\}\{\="
-_LegalValueChars = _LegalKeyChars + '\[\]'
+_LegalValueChars = _LegalKeyChars + r'\[\]'
_CookiePattern = re.compile(r"""
- (?x) # This is a verbose pattern
\s* # Optional whitespace at start of cookie
(?P<key> # Start of group 'key'
[""" + _LegalKeyChars + r"""]+? # Any word of at least one letter
@@ -475,7 +474,7 @@ _CookiePattern = re.compile(r"""
)? # End of optional value group
\s* # Any number of spaces.
(\s+|;|$) # Ending either at space, semicolon, or EOS.
- """, re.ASCII) # May be removed if safe.
+ """, re.ASCII | re.VERBOSE) # re.ASCII may be removed if safe.
# At long last, here is the cookie class. Using this class is almost just like
diff --git a/Lib/http/server.py b/Lib/http/server.py
index 00620d1..e12e45b 100644
--- a/Lib/http/server.py
+++ b/Lib/http/server.py
@@ -87,6 +87,7 @@ __all__ = [
"SimpleHTTPRequestHandler", "CGIHTTPRequestHandler",
]
+import email.utils
import html
import http.client
import io
@@ -126,9 +127,6 @@ DEFAULT_ERROR_MESSAGE = """\
DEFAULT_ERROR_CONTENT_TYPE = "text/html;charset=utf-8"
-def _quote_html(html):
- return html.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
-
class HTTPServer(socketserver.TCPServer):
allow_reuse_address = 1 # Seems to make sense in testing environment
@@ -136,7 +134,7 @@ class HTTPServer(socketserver.TCPServer):
def server_bind(self):
"""Override server_bind to store the server name."""
socketserver.TCPServer.server_bind(self)
- host, port = self.socket.getsockname()[:2]
+ host, port = self.server_address[:2]
self.server_name = socket.getfqdn(host)
self.server_port = port
@@ -282,12 +280,9 @@ class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):
words = requestline.split()
if len(words) == 3:
command, path, version = words
- if version[:5] != 'HTTP/':
- self.send_error(
- HTTPStatus.BAD_REQUEST,
- "Bad request version (%r)" % version)
- return False
try:
+ if version[:5] != 'HTTP/':
+ raise ValueError
base_version_number = version.split('/', 1)[1]
version_number = base_version_number.split(".")
# RFC 2145 section 3.1 says there can be only one "." and
@@ -309,7 +304,7 @@ class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):
if version_number >= (2, 0):
self.send_error(
HTTPStatus.HTTP_VERSION_NOT_SUPPORTED,
- "Invalid HTTP Version (%s)" % base_version_number)
+ "Invalid HTTP version (%s)" % base_version_number)
return False
elif len(words) == 2:
command, path = words
@@ -332,10 +327,11 @@ class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):
try:
self.headers = http.client.parse_headers(self.rfile,
_class=self.MessageClass)
- except http.client.LineTooLong:
+ except http.client.LineTooLong as err:
self.send_error(
- HTTPStatus.BAD_REQUEST,
- "Line too long")
+ HTTPStatus.REQUEST_HEADER_FIELDS_TOO_LARGE,
+ "Line too long",
+ str(err))
return False
except http.client.HTTPException as err:
self.send_error(
@@ -465,8 +461,8 @@ class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):
# (see bug #1100201)
content = (self.error_message_format % {
'code': code,
- 'message': _quote_html(message),
- 'explain': _quote_html(explain)
+ 'message': html.escape(message, quote=False),
+ 'explain': html.escape(explain, quote=False)
})
body = content.encode('UTF-8', 'replace')
self.send_header("Content-Type", self.error_content_type)
@@ -491,12 +487,12 @@ class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):
def send_response_only(self, code, message=None):
"""Send the response header only."""
- if message is None:
- if code in self.responses:
- message = self.responses[code][0]
- else:
- message = ''
if self.request_version != 'HTTP/0.9':
+ if message is None:
+ if code in self.responses:
+ message = self.responses[code][0]
+ else:
+ message = ''
if not hasattr(self, '_headers_buffer'):
self._headers_buffer = []
self._headers_buffer.append(("%s %d %s\r\n" %
@@ -583,12 +579,7 @@ class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):
"""Return the current date and time formatted for a message header."""
if timestamp is None:
timestamp = time.time()
- year, month, day, hh, mm, ss, wd, y, z = time.gmtime(timestamp)
- s = "%s, %02d %3s %4d %02d:%02d:%02d GMT" % (
- self.weekdayname[wd],
- day, self.monthname[month], year,
- hh, mm, ss)
- return s
+ return email.utils.formatdate(timestamp, usegmt=True)
def log_date_time_string(self):
"""Return the current time formatted for logging."""
@@ -726,7 +717,7 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
errors='surrogatepass')
except UnicodeDecodeError:
displaypath = urllib.parse.unquote(path)
- displaypath = html.escape(displaypath)
+ displaypath = html.escape(displaypath, quote=False)
enc = sys.getfilesystemencoding()
title = 'Directory listing for %s' % displaypath
r.append('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" '
@@ -750,7 +741,7 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
r.append('<li><a href="%s">%s</a></li>'
% (urllib.parse.quote(linkname,
errors='surrogatepass'),
- html.escape(displayname)))
+ html.escape(displayname, quote=False)))
r.append('</ul>\n<hr>\n</body>\n</html>\n')
encoded = '\n'.join(r).encode(enc, 'surrogateescape')
f = io.BytesIO()
@@ -1191,16 +1182,15 @@ def test(HandlerClass=BaseHTTPRequestHandler,
server_address = (bind, port)
HandlerClass.protocol_version = protocol
- httpd = ServerClass(server_address, HandlerClass)
-
- sa = httpd.socket.getsockname()
- print("Serving HTTP on", sa[0], "port", sa[1], "...")
- try:
- httpd.serve_forever()
- except KeyboardInterrupt:
- print("\nKeyboard interrupt received, exiting.")
- httpd.server_close()
- sys.exit(0)
+ with ServerClass(server_address, HandlerClass) as httpd:
+ sa = httpd.socket.getsockname()
+ serve_message = "Serving HTTP on {host} port {port} (http://{host}:{port}/) ..."
+ print(serve_message.format(host=sa[0], port=sa[1]))
+ try:
+ httpd.serve_forever()
+ except KeyboardInterrupt:
+ print("\nKeyboard interrupt received, exiting.")
+ sys.exit(0)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
diff --git a/Lib/idlelib/NEWS.txt b/Lib/idlelib/NEWS.txt
index 72905c1..a3fc501 100644
--- a/Lib/idlelib/NEWS.txt
+++ b/Lib/idlelib/NEWS.txt
@@ -1,45 +1,101 @@
-What's New in IDLE 3.5.3?
-=========================
-*Release date: 2017-01-01?*
+What's New in IDLE 3.6.0?
+===========================
+*Release date: 2016-12-16?*
- Issue #15308: Add 'interrupt execution' (^C) to Shell menu.
Patch by Roger Serwy, updated by Bayard Randel.
- Issue #27922: Stop IDLE tests from 'flashing' gui widgets on the screen.
+- Issue #27891: Consistently group and sort imports within idlelib modules.
+
+- Issue #17642: add larger font sizes for classroom projection.
+
- Add version to title of IDLE help window.
- Issue #25564: In section on IDLE -- console differences, mention that
using exec means that __builtins__ is defined for each statement.
+- Issue #27821: Fix 3.6.0a3 regression that prevented custom key sets
+ from being selected when no custom theme was defined.
+
- Issue #27714: text_textview and test_autocomplete now pass when re-run
in the same process. This occurs when test_idle fails when run with the
-w option but without -jn. Fix warning from test_config.
+- Issue #27621: Put query response validation error messages in the query
+ box itself instead of in a separate massagebox. Redo tests to match.
+ Add Mac OSX refinements. Original patch by Mark Roseman.
+
+- Issue #27620: Escape key now closes Query box as cancelled.
+
+- Issue #27609: IDLE: tab after initial whitespace should tab, not
+ autocomplete. This fixes problem with writing docstrings at least
+ twice indented.
+
+- Issue #27609: Explicitly return None when there are also non-None
+ returns. In a few cases, reverse a condition and eliminate a return.
+
- Issue #25507: IDLE no longer runs buggy code because of its tkinter imports.
Users must include the same imports required to run directly in Python.
+- Issue #27173: Add 'IDLE Modern Unix' to the built-in key sets.
+ Make the default key set depend on the platform.
+ Add tests for the changes to the config module.
+
+- Issue #27452: add line counter and crc to IDLE configHandler test dump.
+
+- Issue #27477: IDLE search dialogs now use ttk widgets.
+
+- Issue #27173: Add 'IDLE Modern Unix' to the built-in key sets.
+ Make the default key set depend on the platform.
+ Add tests for the changes to the config module.
+
+- Issue #27452: make command line "idle-test> python test_help.py" work.
+ __file__ is relative when python is started in the file's directory.
+
- Issue #27452: add line counter and crc to IDLE configHandler test dump.
+- Issue #27380: IDLE: add query.py with base Query dialog and ttk widgets.
+ Module had subclasses SectionName, ModuleName, and HelpSource, which are
+ used to get information from users by configdialog and file =>Load Module.
+ Each subclass has itw own validity checks. Using ModuleName allows users
+ to edit bad module names instead of starting over.
+ Add tests and delete the two files combined into the new one.
+
+- Issue #27372: Test_idle no longer changes the locale.
+
- Issue #27365: Allow non-ascii chars in IDLE NEWS.txt, for contributor names.
- Issue #27245: IDLE: Cleanly delete custom themes and key bindings.
Previously, when IDLE was started from a console or by import, a cascade
of warnings was emitted. Patch by Serhiy Storchaka.
+- Issue #24137: Run IDLE, test_idle, and htest with tkinter default root disabled.
+ Fix code and tests that fail with this restriction.
+ Fix htests to not create a second and redundant root and mainloop.
-What's New in IDLE 3.5.2?
-=========================
-*Release date: 2016-06-26*
+- Issue #27310: Fix IDLE.app failure to launch on OS X due to vestigial import.
- Issue #5124: Paste with text selected now replaces the selection on X11.
This matches how paste works on Windows, Mac, most modern Linux apps,
and ttk widgets. Original patch by Serhiy Storchaka.
+- Issue #24750: Switch all scrollbars in IDLE to ttk versions.
+ Where needed, minimal tests are added to cover changes.
+
+- Issue #24759: IDLE requires tk 8.5 and availability ttk widgets.
+ Delete now unneeded tk version tests and code for older versions.
+ Add test for IDLE syntax colorizer.
+
+- Issue #27239: idlelib.macosx.isXyzTk functions initialize as needed.
+
+- Issue #27262: move Aqua unbinding code, which enable context menus, to maxosx.
+
- Issue #24759: Make clear in idlelib.idle_test.__init__ that the directory
is a private implementation of test.test_idle and tool for maintainers.
-- Issue #27196: Stop 'ThemeChangef' warnings when running IDLE tests.
+- Issue #27196: Stop 'ThemeChanged' warnings when running IDLE tests.
These persisted after other warnings were suppressed in #20567.
Apply Serhiy Storchaka's update_idletasks solution to four test files.
Record this additional advice in idle_test/README.txt
@@ -47,9 +103,26 @@ What's New in IDLE 3.5.2?
- Issue #20567: Revise idle_test/README.txt with advice about avoiding
tk warning messages from tests. Apply advice to several IDLE tests.
+- Issue # 24225: Update idlelib/README.txt with new file names
+ and event handlers.
+
+- Issue #27156: Remove obsolete code not used by IDLE. Replacements:
+ 1. help.txt, replaced by help.html, is out-of-date and should not be used.
+ Its dedicated viewer has be replaced by the html viewer in help.py.
+ 2. 'import idlever; I = idlever.IDLE_VERSION' is the same as
+ 'import sys; I = version[:version.index(' ')]'
+ 3. After 'ob = stackviewer.VariablesTreeItem(*args)',
+ 'ob.keys()' == 'list(ob.object.keys).
+ 4. In macosc, runningAsOSXAPP == isAquaTk; idCarbonAquaTk == isCarbonTk
+
- Issue #27117: Make colorizer htest and turtledemo work with dark themes.
Move code for configuring text widget colors to a new function.
+- Issue #24225: Rename many idlelib/*.py and idle_test/test_*.py files.
+ Edit files to replace old names with new names when the old name
+ referred to the module rather than the class it contained.
+ See the issue and IDLE section in What's New in 3.6 for more.
+
- Issue #26673: When tk reports font size as 0, change to size 10.
Such fonts on Linux prevented the configuration dialog from opening.
@@ -62,8 +135,8 @@ What's New in IDLE 3.5.2?
- Issue #18410: Add test for IDLE's search dialog.
Original patch by Westley Martínez.
-- Issue #21703: Add test for undo delegator.
- Original patch by Saimadhav Heblikar .
+- Issue #21703: Add test for undo delegator. Patch mostly by
+ Saimadhav Heblikar .
- Issue #27044: Add ConfigDialog.remove_var_callbacks to stop memory leaks.
@@ -82,11 +155,6 @@ What's New in IDLE 3.5.2?
MARK in README.txt and open this and NEWS.txt with 'ascii'.
Re-encode CREDITS.txt to utf-8 and open it with 'utf-8'.
-
-What's New in IDLE 3.5.1?
-=========================
-*Release date: 2015-12-06*
-
- Issue 15348: Stop the debugger engine (normally in a user process)
before closing the debugger window (running in the IDLE process).
This prevents the RuntimeErrors that were being caught and ignored.
diff --git a/Lib/idlelib/README.txt b/Lib/idlelib/README.txt
index ff44504..51e8ef5 100644
--- a/Lib/idlelib/README.txt
+++ b/Lib/idlelib/README.txt
@@ -29,61 +29,61 @@ idle.pyw
Implementation
--------------
-AutoComplete.py # Complete attribute names or filenames.
-AutoCompleteWindow.py # Display completions.
-AutoExpand.py # Expand word with previous word in file.
-Bindings.py # Define most of IDLE menu.
-CallTipWindow.py # Display calltip.
-CallTips.py # Create calltip text.
-ClassBrowser.py # Create module browser window.
-CodeContext.py # Show compound statement headers otherwise not visible.
-ColorDelegator.py # Colorize text (nim).
-Debugger.py # Debug code run from editor; show window.
-Delegator.py # Define base class for delegators (nim).
-EditorWindow.py # Define most of editor and utility functions.
-FileList.py # Open files and manage list of open windows (nim).
-FormatParagraph.py# Re-wrap multiline strings and comments.
-GrepDialog.py # Find all occurrences of pattern in multiple files.
-HyperParser.py # Parse code around a given index.
-IOBinding.py # Open, read, and write files
-IdleHistory.py # Get previous or next user input in shell (nim)
-MultiCall.py # Wrap tk widget to allow multiple calls per event (nim).
-MultiStatusBar.py # Define status bar for windows (nim).
-ObjectBrowser.py # Define class used in StackViewer (nim).
-OutputWindow.py # Create window for grep output.
-ParenMatch.py # Match fenceposts: (), [], and {}.
-PathBrowser.py # Create path browser window.
-Percolator.py # Manage delegator stack (nim).
-PyParse.py # Give information on code indentation
-PyShell.py # Start IDLE, manage shell, complete editor window
-RemoteDebugger.py # Debug code run in remote process.
-RemoteObjectBrowser.py # Communicate objects between processes with rpc (nim).
-ReplaceDialog.py # Search and replace pattern in text.
-RstripExtension.py# Strip trailing whitespace
-ScriptBinding.py # Check and run user code.
-ScrolledList.py # Define ScrolledList widget for IDLE (nim).
-SearchDialog.py # Search for pattern in text.
-SearchDialogBase.py # Define base for search, replace, and grep dialogs.
-SearchEngine.py # Define engine for all 3 search dialogs.
-StackViewer.py # View stack after exception.
-TreeWidget.py # Define tree widger, used in browsers (nim).
-UndoDelegator.py # Manage undo stack.
-WidgetRedirector.py # Intercept widget subcommands (for percolator) (nim).
-WindowList.py # Manage window list and define listed top level.
-ZoomHeight.py # Zoom window to full height of screen.
-aboutDialog.py # Display About IDLE dialog.
-configDialog.py # Display user configuration dialogs.
-configHandler.py # Load, fetch, and save configuration (nim).
-configHelpSourceEdit.py # Specify help source.
-configSectionNameDialog.py # Spefify user config section name
-dynOptionMenuWidget.py # define mutable OptionMenu widget (nim).
+autocomplete.py # Complete attribute names or filenames.
+autocomplete_w.py # Display completions.
+autoexpand.py # Expand word with previous word in file.
+browser.py # Create module browser window.
+calltip_w.py # Display calltip.
+calltips.py # Create calltip text.
+codecontext.py # Show compound statement headers otherwise not visible.
+colorizer.py # Colorize text (nim)
+config.py # Load, fetch, and save configuration (nim).
+configdialog.py # Display user configuration dialogs.
+config_help.py # Specify help source in configdialog.
+config_key.py # Change keybindings.
+dynoption.py # Define mutable OptionMenu widget (nim).
+debugobj.py # Define class used in stackviewer.
+debugobj_r.py # Communicate objects between processes with rpc (nim).
+debugger.py # Debug code run from shell or editor; show window.
+debugger_r.py # Debug code run in remote process.
+delegator.py # Define base class for delegators (nim).
+editor.py # Define most of editor and utility functions.
+filelist.py # Open files and manage list of open windows (nim).
+grep.py # Find all occurrences of pattern in multiple files.
help.py # Display IDLE's html doc.
-keybindingDialog.py # Change keybindings.
-macosxSupport.py # Help IDLE run on Macs (nim).
+help_about.py # Display About IDLE dialog.
+history.py # Get previous or next user input in shell (nim)
+hyperparser.py # Parse code around a given index.
+iomenu.py # Open, read, and write files
+macosx.py # Help IDLE run on Macs (nim).
+mainmenu.py # Define most of IDLE menu.
+multicall.py # Wrap tk widget to allow multiple calls per event (nim).
+outwin.py # Create window for grep output.
+paragraph.py # Re-wrap multiline strings and comments.
+parenmatch.py # Match fenceposts: (), [], and {}.
+pathbrowser.py # Create path browser window.
+percolator.py # Manage delegator stack (nim).
+pyparse.py # Give information on code indentation
+pyshell.py # Start IDLE, manage shell, complete editor window
+query.py # Query user for information
+redirector.py # Intercept widget subcommands (for percolator) (nim).
+replace.py # Search and replace pattern in text.
rpc.py # Commuicate between idle and user processes (nim).
+rstrip.py # Strip trailing whitespace.
run.py # Manage user code execution subprocess.
+runscript.py # Check and run user code.
+scrolledlist.py # Define scrolledlist widget for IDLE (nim).
+search.py # Search for pattern in text.
+searchbase.py # Define base for search, replace, and grep dialogs.
+searchengine.py # Define engine for all 3 search dialogs.
+stackviewer.py # View stack after exception.
+statusbar.py # Define status bar for windows (nim).
tabbedpages.py # Define tabbed pages widget (nim).
-textView.py # Define read-only text widget (nim).
+textview.py # Define read-only text widget (nim).
+tree.py # Define tree widger, used in browsers (nim).
+undo.py # Manage undo stack.
+windows.py # Manage window list and define listed top level.
+zoomheight.py # Zoom window to full height of screen.
Configuration
-------------
@@ -104,127 +104,148 @@ help.html # copy of idle.html in docs, displayed by IDLE Help
Subdirectories
--------------
-Icons # small image files
-idle_test # files for human test and automated unit tests
+Icons # small image files
+idle_test # files for human test and automated unit tests
Unused and Deprecated files and objects (nim)
---------------------------------------------
-EditorWindow.py: Helpdialog and helpDialog
-ToolTip.py: unused.
-help.txt
-idlever.py
+tooltip.py # unused
+
IDLE MENUS
-Top level items and most submenu items are defined in Bindings.
+Top level items and most submenu items are defined in mainmenu.
Extenstions add submenu items when active. The names given are
found, quoted, in one of these modules, paired with a '<<pseudoevent>>'.
Each pseudoevent is bound to an event handler. Some event handlers
call another function that does the actual work. The annotations below
are intended to at least give the module where the actual work is done.
+'eEW' = editor.EditorWindow
-File # IOBindig except as noted
- New File
- Open... # IOBinding.open
- Open Module
+File
+ New File # eEW.new_callback
+ Open... # iomenu.open
+ Open Module # eEw.open_module
Recent Files
- Class Browser # Class Browser
- Path Browser # Path Browser
+ Class Browser # eEW.open_class_browser, browser.ClassBrowser
+ Path Browser # eEW.open_path_browser, pathbrowser
---
- Save # IDBinding.save
- Save As... # IOBinding.save_as
- Save Copy As... # IOBindling.save_a_copy
+ Save # iomenu.save
+ Save As... # iomenu.save_as
+ Save Copy As... # iomenu.save_a_copy
---
- Print Window # IOBinding.print_window
+ Print Window # iomenu.print_window
---
- Close
- Exit
+ Close # eEW.close_event
+ Exit # flist.close_all_callback (bound in eEW)
Edit
- Undo # undoDelegator
- Redo # undoDelegator
- ---
- Cut
- Copy
- Paste
- Select All
- --- # Next 5 items use SearchEngine; dialogs use SearchDialogBase
- Find # Search Dialog
- Find Again
- Find Selection
- Find in Files... # GrepDialog
- Replace... # ReplaceDialog
- Go to Line
- Show Completions # AutoComplete extension and AutoCompleteWidow (&HP)
- Expand Word # AutoExpand extension
- Show call tip # Calltips extension and CalltipWindow (& Hyperparser)
- Show surrounding parens # ParenMatch (& Hyperparser)
-
-Shell # PyShell
- View Last Restart # PyShell.PyShell.view_restart_mark
- Restart Shell # PyShell.PyShell.restart_shell
+ Undo # undodelegator
+ Redo # undodelegator
+ --- # eEW.right_menu_event
+ Cut # eEW.cut
+ Copy # eEW.copy
+ Paste # eEW.past
+ Select All # eEW.select_all (+ see eEW.remove_selection)
+ --- # Next 5 items use searchengine; dialogs use searchbase
+ Find # eEW.find_event, search.SearchDialog.find
+ Find Again # eEW.find_again_event, sSD.find_again
+ Find Selection # eEW.find_selection_event, sSD.find_selection
+ Find in Files... # eEW.find_in_files_event, grep
+ Replace... # eEW.replace_event, replace.ReplaceDialog.replace
+ Go to Line # eEW.goto_line_event
+ Show Completions # autocomplete extension and autocompleteWidow (&HP)
+ Expand Word # autoexpand extension
+ Show call tip # Calltips extension and CalltipWindow (& Hyperparser)
+ Show surrounding parens # parenmatch (& Hyperparser)
+
+Shell # pyshell
+ View Last Restart # pyshell.PyShell.view_restart_mark
+ Restart Shell # pyshell.PyShell.restart_shell
Interrupt Execution # pyshell.PyShell.cancel_callback
Debug (Shell only)
Go to File/Line
- Debugger # Debugger, RemoteDebugger, PyShell.toggle_debuger
- Stack Viewer # StackViewer, PyShell.open_stack_viewer
- Auto-open Stack Viewer # StackViewer
+ debugger # debugger, debugger_r, PyShell.toggle_debuger
+ Stack Viewer # stackviewer, PyShell.open_stack_viewer
+ Auto-open Stack Viewer # stackviewer
Format (Editor only)
- Indent Region
- Dedent Region
- Comment Out Region
- Uncomment Region
- Tabify Region
- Untabify Region
- Toggle Tabs
- New Indent Width
- Format Paragraph # FormatParagraph extension
+ Indent Region # eEW.indent_region_event
+ Dedent Region # eEW.dedent_region_event
+ Comment Out Reg. # eEW.comment_region_event
+ Uncomment Region # eEW.uncomment_region_event
+ Tabify Region # eEW.tabify_region_event
+ Untabify Region # eEW.untabify_region_event
+ Toggle Tabs # eEW.toggle_tabs_event
+ New Indent Width # eEW.change_indentwidth_event
+ Format Paragraph # paragraph extension
---
- Strip tailing whitespace # RstripExtension extension
+ Strip tailing whitespace # rstrip extension
Run (Editor only)
- Python Shell # PyShell
+ Python Shell # pyshell
---
- Check Module # ScriptBinding
- Run Module # ScriptBinding
+ Check Module # runscript
+ Run Module # runscript
Options
- Configure IDLE # configDialog
+ Configure IDLE # eEW.config_dialog, configdialog
(tabs in the dialog)
- Font tab # onfig-main.def
- Highlight tab # configSectionNameDialog, config-highlight.def
- Keys tab # keybindingDialog, configSectionNameDialog, onfig-keus.def
- General tab # configHelpSourceEdit, config-main.def
- Configure Extensions # configDialog
- Xyz tab # xyz.py, config-extensions.def
+ Font tab # config-main.def
+ Highlight tab # query, config-highlight.def
+ Keys tab # query, config_key, config_keys.def
+ General tab # config_help, config-main.def
+ Extensions tab # config-extensions.def, corresponding .py
---
- Code Context (editor only) # CodeContext extension
+ Code Context (ed)# codecontext extension
Window
- Zoomheight # ZoomHeight extension
+ Zoomheight # zoomheight extension
---
- <open windows> # WindowList
+ <open windows> # windows
Help
- About IDLE # aboutDialog
+ About IDLE # eEW.about_dialog, help_about.AboutDialog
---
- IDLE Help # help
- Python Doc
- Turtle Demo
+ IDLE Help # eEW.help_dialog, helpshow_idlehelp
+ Python Doc # eEW.python_docs
+ Turtle Demo # eEW.open_turtle_demo
---
<other help sources>
<Context Menu> (right click)
-Defined in EditorWindow, PyShell, Output
- Cut
- Copy
- Paste
- ---
- Go to file/line (shell and output only)
- Set Breakpoint (editor only)
- Clear Breakpoint (editor only)
- Defined in Debugger
- Go to source line
- Show stack frame
+ Defined in editor, PyShelpyshellut
+ Cut
+ Copy
+ Paste
+ ---
+ Go to file/line (shell and output only)
+ Set Breakpoint (editor only)
+ Clear Breakpoint (editor only)
+ Defined in debugger
+ Go to source line
+ Show stack frame
+
+<No menu>
+Center Insert # eEW.center_insert_event
+
+
+CODE STYLE -- Generally PEP 8.
+
+import
+------
+Put import at the top, unless there is a good reason otherwise.
+PEP 8 says to group stdlib, 3rd-party dependencies, and package imports.
+For idlelib, the groups are general stdlib, tkinter, and idlelib.
+Sort modules within each group, except that tkinter.ttk follows tkinter.
+Sort 'from idlelib import mod1' and 'from idlelib.mod2 import object'
+together by module, ignoring within module objects.
+Put 'import __main__' after other idlelib imports.
+
+Imports only needed for testing are put not at the top but in an
+htest function def or "if __name__ == '__main__'" clause.
+
+Within module imports like "from idlelib.mod import class" may cause
+circular imports to deadlock. Even without this, circular imports may
+require at least one of the imports to be delayed until a function call.
diff --git a/Lib/idlelib/__init__.py b/Lib/idlelib/__init__.py
index 711f61b..791ddea 100644
--- a/Lib/idlelib/__init__.py
+++ b/Lib/idlelib/__init__.py
@@ -1,8 +1,10 @@
"""The idlelib package implements the Idle application.
Idle includes an interactive shell and editor.
+Starting with Python 3.6, IDLE requires tcl/tk 8.5 or later.
Use the files named idle.* to start Idle.
The other files are private implementations. Their details are subject to
change. See PEP 434 for more. Import them at your own risk.
"""
+testing = False # Set True by test.test_idle.
diff --git a/Lib/idlelib/__main__.py b/Lib/idlelib/__main__.py
index 2edf5f7..6349ec7 100644
--- a/Lib/idlelib/__main__.py
+++ b/Lib/idlelib/__main__.py
@@ -3,6 +3,6 @@ IDLE main entry point
Run IDLE as python -m idlelib
"""
-import idlelib.PyShell
-idlelib.PyShell.main()
+import idlelib.pyshell
+idlelib.pyshell.main()
# This file does not work for 2.7; See issue 24212.
diff --git a/Lib/idlelib/AutoComplete.py b/Lib/idlelib/autocomplete.py
index ff085d5..1e44fa5 100644
--- a/Lib/idlelib/AutoComplete.py
+++ b/Lib/idlelib/autocomplete.py
@@ -1,29 +1,30 @@
-"""AutoComplete.py - An IDLE extension for automatically completing names.
+"""autocomplete.py - An IDLE extension for automatically completing names.
This extension can complete either attribute names or file names. It can pop
a window with all available names, for the user to select from.
"""
import os
-import sys
import string
+import sys
-from idlelib.configHandler import idleConf
-
-# This string includes all chars that may be in an identifier
-ID_CHARS = string.ascii_letters + string.digits + "_"
-
-# These constants represent the two different types of completions
+# These constants represent the two different types of completions.
+# They must be defined here so autocomple_w can import them.
COMPLETE_ATTRIBUTES, COMPLETE_FILES = range(1, 2+1)
-from idlelib import AutoCompleteWindow
-from idlelib.HyperParser import HyperParser
-
+from idlelib import autocomplete_w
+from idlelib.config import idleConf
+from idlelib.hyperparser import HyperParser
import __main__
+# This string includes all chars that may be in an identifier.
+# TODO Update this here and elsewhere.
+ID_CHARS = string.ascii_letters + string.digits + "_"
+
SEPS = os.sep
if os.altsep: # e.g. '/' on Windows...
SEPS += os.altsep
+
class AutoComplete:
menudefs = [
@@ -37,19 +38,17 @@ class AutoComplete:
def __init__(self, editwin=None):
self.editwin = editwin
- if editwin is None: # subprocess and test
- return
- self.text = editwin.text
- self.autocompletewindow = None
-
- # id of delayed call, and the index of the text insert when the delayed
- # call was issued. If _delayed_completion_id is None, there is no
- # delayed call.
- self._delayed_completion_id = None
- self._delayed_completion_index = None
+ if editwin is not None: # not in subprocess or test
+ self.text = editwin.text
+ self.autocompletewindow = None
+ # id of delayed call, and the index of the text insert when
+ # the delayed call was issued. If _delayed_completion_id is
+ # None, there is no delayed call.
+ self._delayed_completion_id = None
+ self._delayed_completion_index = None
def _make_autocomplete_window(self):
- return AutoCompleteWindow.AutoCompleteWindow(self.text)
+ return autocomplete_w.AutoCompleteWindow(self.text)
def _remove_autocomplete_window(self, event=None):
if self.autocompletewindow:
@@ -80,16 +79,17 @@ class AutoComplete:
open a completion list after that (if there is more than one
completion)
"""
- if hasattr(event, "mc_state") and event.mc_state:
- # A modifier was pressed along with the tab, continue as usual.
- return
+ if hasattr(event, "mc_state") and event.mc_state or\
+ not self.text.get("insert linestart", "insert").strip():
+ # A modifier was pressed along with the tab or
+ # there is only previous whitespace on this line, so tab.
+ return None
if self.autocompletewindow and self.autocompletewindow.is_active():
self.autocompletewindow.complete()
return "break"
else:
opened = self.open_completions(False, True, True)
- if opened:
- return "break"
+ return "break" if opened else None
def _open_completions_later(self, *args):
self._delayed_completion_index = self.text.index("insert")
@@ -101,9 +101,8 @@ class AutoComplete:
def _delayed_open_completions(self, *args):
self._delayed_completion_id = None
- if self.text.index("insert") != self._delayed_completion_index:
- return
- self.open_completions(*args)
+ if self.text.index("insert") == self._delayed_completion_index:
+ self.open_completions(*args)
def open_completions(self, evalfuncs, complete, userWantsWin, mode=None):
"""Find the completions and create the AutoCompleteWindow.
@@ -148,17 +147,17 @@ class AutoComplete:
comp_what = hp.get_expression()
if not comp_what or \
(not evalfuncs and comp_what.find('(') != -1):
- return
+ return None
else:
comp_what = ""
else:
- return
+ return None
if complete and not comp_what and not comp_start:
- return
+ return None
comp_lists = self.fetch_completions(comp_what, mode)
if not comp_lists[0]:
- return
+ return None
self.autocompletewindow = self._make_autocomplete_window()
return not self.autocompletewindow.show_window(
comp_lists, "insert-%dc" % len(comp_start),
diff --git a/Lib/idlelib/AutoCompleteWindow.py b/Lib/idlelib/autocomplete_w.py
index 2ee6878..3374c6e 100644
--- a/Lib/idlelib/AutoCompleteWindow.py
+++ b/Lib/idlelib/autocomplete_w.py
@@ -1,9 +1,11 @@
"""
-An auto-completion window for IDLE, used by the AutoComplete extension
+An auto-completion window for IDLE, used by the autocomplete extension
"""
from tkinter import *
-from idlelib.MultiCall import MC_SHIFT
-from idlelib.AutoComplete import COMPLETE_FILES, COMPLETE_ATTRIBUTES
+from tkinter.ttk import Scrollbar
+
+from idlelib.autocomplete import COMPLETE_FILES, COMPLETE_ATTRIBUTES
+from idlelib.multicall import MC_SHIFT
HIDE_VIRTUAL_EVENT_NAME = "<<autocompletewindow-hide>>"
HIDE_SEQUENCES = ("<FocusOut>", "<ButtonPress>")
@@ -34,8 +36,8 @@ class AutoCompleteWindow:
self.completions = None
# A list with more completions, or None
self.morecompletions = None
- # The completion mode. Either AutoComplete.COMPLETE_ATTRIBUTES or
- # AutoComplete.COMPLETE_FILES
+ # The completion mode. Either autocomplete.COMPLETE_ATTRIBUTES or
+ # autocomplete.COMPLETE_FILES
self.mode = None
# The current completion start, on the text box (a string)
self.start = None
@@ -215,6 +217,7 @@ class AutoCompleteWindow:
self.winconfigid = acw.bind(WINCONFIG_SEQUENCE, self.winconfig_event)
self.doubleclickid = listbox.bind(DOUBLECLICK_SEQUENCE,
self.doubleclick_event)
+ return None
def winconfig_event(self, event):
if not self.is_active():
@@ -238,16 +241,14 @@ class AutoCompleteWindow:
acw.wm_geometry("+%d+%d" % (new_x, new_y))
def hide_event(self, event):
- if not self.is_active():
- return
- self.hide_window()
+ if self.is_active():
+ self.hide_window()
def listselect_event(self, event):
- if not self.is_active():
- return
- self.userwantswindow = True
- cursel = int(self.listbox.curselection()[0])
- self._change_start(self.completions[cursel])
+ if self.is_active():
+ self.userwantswindow = True
+ cursel = int(self.listbox.curselection()[0])
+ self._change_start(self.completions[cursel])
def doubleclick_event(self, event):
# Put the selected completion in the text, and close the list
@@ -257,7 +258,7 @@ class AutoCompleteWindow:
def keypress_event(self, event):
if not self.is_active():
- return
+ return None
keysym = event.keysym
if hasattr(event, "mc_state"):
state = event.mc_state
@@ -282,7 +283,7 @@ class AutoCompleteWindow:
# keysym == "BackSpace"
if len(self.start) == 0:
self.hide_window()
- return
+ return None
self._change_start(self.start[:-1])
self.lasttypedstart = self.start
self.listbox.select_clear(0, int(self.listbox.curselection()[0]))
@@ -292,7 +293,7 @@ class AutoCompleteWindow:
elif keysym == "Return":
self.hide_window()
- return
+ return None
elif (self.mode == COMPLETE_ATTRIBUTES and keysym in
("period", "space", "parenleft", "parenright", "bracketleft",
@@ -308,7 +309,7 @@ class AutoCompleteWindow:
and (self.mode == COMPLETE_ATTRIBUTES or self.start):
self._change_start(self.completions[cursel])
self.hide_window()
- return
+ return None
elif keysym in ("Home", "End", "Prior", "Next", "Up", "Down") and \
not state:
@@ -349,12 +350,12 @@ class AutoCompleteWindow:
# first tab; let AutoComplete handle the completion
self.userwantswindow = True
self.lastkey_was_tab = True
- return
+ return None
elif any(s in keysym for s in ("Shift", "Control", "Alt",
"Meta", "Command", "Option")):
# A modifier key, so ignore
- return
+ return None
elif event.char and event.char >= ' ':
# Regular character with a non-length-1 keycode
@@ -368,7 +369,7 @@ class AutoCompleteWindow:
else:
# Unknown event, close the window and let it through.
self.hide_window()
- return
+ return None
def keyrelease_event(self, event):
if not self.is_active():
diff --git a/Lib/idlelib/AutoExpand.py b/Lib/idlelib/autoexpand.py
index 7059054..6b46bee 100644
--- a/Lib/idlelib/AutoExpand.py
+++ b/Lib/idlelib/autoexpand.py
@@ -12,8 +12,8 @@ its state.
This is an extension file and there is only one instance of AutoExpand.
'''
-import string
import re
+import string
###$ event <<expand-word>>
###$ win <Alt-slash>
@@ -31,6 +31,7 @@ class AutoExpand:
def __init__(self, editwin):
self.text = editwin.text
+ self.bell = self.text.bell
self.state = None
def expand_word_event(self, event):
@@ -46,14 +47,14 @@ class AutoExpand:
words = self.getwords()
index = 0
if not words:
- self.text.bell()
+ self.bell()
return "break"
word = self.getprevword()
self.text.delete("insert - %d chars" % len(word), "insert")
newword = words[index]
index = (index + 1) % len(words)
if index == 0:
- self.text.bell() # Warn we cycled around
+ self.bell() # Warn we cycled around
self.text.insert("insert", newword)
curinsert = self.text.index("insert")
curline = self.text.get("insert linestart", "insert lineend")
diff --git a/Lib/idlelib/ClassBrowser.py b/Lib/idlelib/browser.py
index d09c52f..ea05638 100644
--- a/Lib/idlelib/ClassBrowser.py
+++ b/Lib/idlelib/browser.py
@@ -11,16 +11,16 @@ XXX TO DO:
"""
import os
-import sys
import pyclbr
+import sys
-from idlelib import PyShell
-from idlelib.WindowList import ListedToplevel
-from idlelib.TreeWidget import TreeNode, TreeItem, ScrolledCanvas
-from idlelib.configHandler import idleConf
+from idlelib.config import idleConf
+from idlelib import pyshell
+from idlelib.tree import TreeNode, TreeItem, ScrolledCanvas
+from idlelib.windows import ListedToplevel
file_open = None # Method...Item and Class...Item use this.
-# Normally PyShell.flist.open, but there is no PyShell.flist for htest.
+# Normally pyshell.flist.open, but there is no pyshell.flist for htest.
class ClassBrowser:
@@ -32,7 +32,7 @@ class ClassBrowser:
"""
global file_open
if not _htest:
- file_open = PyShell.flist.open
+ file_open = pyshell.flist.open
self.name = name
self.file = os.path.join(path[0], self.name + ".py")
self._htest = _htest
@@ -95,7 +95,7 @@ class ModuleBrowserTreeItem(TreeItem):
return
if not os.path.exists(self.file):
return
- PyShell.flist.open(self.file)
+ pyshell.flist.open(self.file)
def IsExpandable(self):
return os.path.normcase(self.file[-3:]) == ".py"
@@ -226,7 +226,7 @@ def _class_browser(parent): #Wrapper for htest
file = sys.argv[0]
dir, file = os.path.split(file)
name = os.path.splitext(file)[0]
- flist = PyShell.PyShellFileList(parent)
+ flist = pyshell.PyShellFileList(parent)
global file_open
file_open = flist.open
ClassBrowser(flist, name, [dir], _htest=True)
diff --git a/Lib/idlelib/CallTipWindow.py b/Lib/idlelib/calltip_w.py
index 9eec175..c7361d1 100644
--- a/Lib/idlelib/CallTipWindow.py
+++ b/Lib/idlelib/calltip_w.py
@@ -1,7 +1,7 @@
"""A CallTip window class for Tkinter/IDLE.
-After ToolTip.py, which uses ideas gleaned from PySol
-Used by the CallTips IDLE extension.
+After tooltip.py, which uses ideas gleaned from PySol
+Used by the calltips IDLE extension.
"""
from tkinter import Toplevel, Label, LEFT, SOLID, TclError
@@ -138,8 +138,8 @@ def _calltip_window(parent): # htest #
top = Toplevel(parent)
top.title("Test calltips")
- top.geometry("200x100+%d+%d" % (parent.winfo_rootx() + 200,
- parent.winfo_rooty() + 150))
+ x, y = map(int, parent.geometry().split('+')[1:])
+ top.geometry("200x100+%d+%d" % (x + 250, y + 175))
text = Text(top)
text.pack(side=LEFT, fill=BOTH, expand=1)
text.insert("insert", "string.split")
diff --git a/Lib/idlelib/CallTips.py b/Lib/idlelib/calltips.py
index 81bd5f1..4c5aea2 100644
--- a/Lib/idlelib/CallTips.py
+++ b/Lib/idlelib/calltips.py
@@ -1,19 +1,19 @@
-"""CallTips.py - An IDLE Extension to Jog Your Memory
+"""calltips.py - An IDLE Extension to Jog Your Memory
Call Tips are floating windows which display function, class, and method
parameter and docstring information when you type an opening parenthesis, and
which disappear when you type a closing parenthesis.
"""
-import __main__
import inspect
import re
import sys
import textwrap
import types
-from idlelib import CallTipWindow
-from idlelib.HyperParser import HyperParser
+from idlelib import calltip_w
+from idlelib.hyperparser import HyperParser
+import __main__
class CallTips:
@@ -37,7 +37,7 @@ class CallTips:
def _make_tk_calltip_window(self):
# See __init__ for usage
- return CallTipWindow.CallTip(self.text)
+ return calltip_w.CallTip(self.text)
def _remove_calltip_window(self, event=None):
if self.active_calltip:
@@ -120,7 +120,7 @@ def get_entity(expression):
_MAX_COLS = 85
_MAX_LINES = 5 # enough for bytes
_INDENT = ' '*4 # for wrapped signatures
-_first_param = re.compile('(?<=\()\w*\,?\s*')
+_first_param = re.compile(r'(?<=\()\w*\,?\s*')
_default_callable_argspec = "See source or doc"
diff --git a/Lib/idlelib/CodeContext.py b/Lib/idlelib/codecontext.py
index 7d25ada..f25e1b3 100644
--- a/Lib/idlelib/CodeContext.py
+++ b/Lib/idlelib/codecontext.py
@@ -1,19 +1,21 @@
-"""CodeContext - Extension to display the block context above the edit window
+"""codecontext - Extension to display the block context above the edit window
Once code has scrolled off the top of a window, it can be difficult to
determine which block you are in. This extension implements a pane at the top
of each IDLE edit window which provides block structure hints. These hints are
the lines which contain the block opening keywords, e.g. 'if', for the
enclosing block. The number of hint lines is determined by the numlines
-variable in the CodeContext section of config-extensions.def. Lines which do
+variable in the codecontext section of config-extensions.def. Lines which do
not open blocks are not shown in the context hints pane.
"""
-import tkinter
-from tkinter.constants import TOP, LEFT, X, W, SUNKEN
import re
from sys import maxsize as INFINITY
-from idlelib.configHandler import idleConf
+
+import tkinter
+from tkinter.constants import TOP, LEFT, X, W, SUNKEN
+
+from idlelib.config import idleConf
BLOCKOPENERS = {"class", "def", "elif", "else", "except", "finally", "for",
"if", "try", "while", "with"}
diff --git a/Lib/idlelib/ColorDelegator.py b/Lib/idlelib/colorizer.py
index 02eac47..ff40845 100644
--- a/Lib/idlelib/ColorDelegator.py
+++ b/Lib/idlelib/colorizer.py
@@ -1,10 +1,10 @@
-import time
-import re
-import keyword
import builtins
-from tkinter import TkVersion
-from idlelib.Delegator import Delegator
-from idlelib.configHandler import idleConf
+import keyword
+import re
+import time
+
+from idlelib.config import idleConf
+from idlelib.delegator import Delegator
DEBUG = False
@@ -21,7 +21,7 @@ def make_pat():
# 1st 'file' colorized normal, 2nd as builtin, 3rd as string
builtin = r"([^.'\"\\#]\b|^)" + any("BUILTIN", builtinlist) + r"\b"
comment = any("COMMENT", [r"#[^\n]*"])
- stringprefix = r"(\br|u|ur|R|U|UR|Ur|uR|b|B|br|Br|bR|BR|rb|rB|Rb|RB)?"
+ stringprefix = r"(?i:\br|u|f|fr|rf|b|br|rb)?"
sqstring = stringprefix + r"'[^'\\\n]*(\\.[^'\\\n]*)*'?"
dqstring = stringprefix + r'"[^"\\\n]*(\\.[^"\\\n]*)*"?'
sq3string = stringprefix + r"'''[^'\\]*((\\.|'(?!''))[^'\\]*)*(''')?"
@@ -49,11 +49,8 @@ def color_config(text): # Called from htest, Editor, and Turtle Demo.
insertbackground=cursor_color,
selectforeground=select_colors['foreground'],
selectbackground=select_colors['background'],
- )
- if TkVersion >= 8.5:
- text.config(
- inactiveselectbackground=select_colors['background'])
-
+ inactiveselectbackground=select_colors['background'], # new in 8.5
+ )
class ColorDelegator(Delegator):
@@ -259,13 +256,20 @@ class ColorDelegator(Delegator):
def _color_delegator(parent): # htest #
from tkinter import Toplevel, Text
- from idlelib.Percolator import Percolator
+ from idlelib.percolator import Percolator
top = Toplevel(parent)
top.title("Test ColorDelegator")
- top.geometry("200x100+%d+%d" % (parent.winfo_rootx() + 200,
- parent.winfo_rooty() + 150))
- source = "if somename: x = 'abc' # comment\nprint\n"
+ x, y = map(int, parent.geometry().split('+')[1:])
+ top.geometry("700x250+%d+%d" % (x + 20, y + 175))
+ source = ("# Following has syntax errors\n"
+ "if True: then int 1\nelif False: print 0\nelse: float(None)\n"
+ "if iF + If + IF: 'keywork matching must respect case'\n"
+ "# All valid prefixes for unicode and byte strings should be colored\n"
+ "'x', '''x''', \"x\", \"\"\"x\"\"\"\n"
+ "r'x', u'x', R'x', U'x', f'x', F'x', ur'is invalid'\n"
+ "fr'x', Fr'x', fR'x', FR'x', rf'x', rF'x', Rf'x', RF'x'\n"
+ "b'x',B'x', br'x',Br'x',bR'x',BR'x', rb'x'.rB'x',Rb'x',RB'x'\n")
text = Text(top, background="white")
text.pack(expand=1, fill="both")
text.insert("insert", source)
@@ -277,5 +281,9 @@ def _color_delegator(parent): # htest #
p.insertfilter(d)
if __name__ == "__main__":
+ import unittest
+ unittest.main('idlelib.idle_test.test_colorizer',
+ verbosity=2, exit=False)
+
from idlelib.idle_test.htest import run
run(_color_delegator)
diff --git a/Lib/idlelib/config-keys.def b/Lib/idlelib/config-keys.def
index 3bfcb69..64788f9 100644
--- a/Lib/idlelib/config-keys.def
+++ b/Lib/idlelib/config-keys.def
@@ -109,6 +109,57 @@ change-indentwidth=<Alt-Key-u>
del-word-left=<Alt-Key-BackSpace>
del-word-right=<Alt-Key-d>
+[IDLE Modern Unix]
+copy = <Control-Shift-Key-C> <Control-Key-Insert>
+cut = <Control-Key-x> <Shift-Key-Delete>
+paste = <Control-Key-v> <Shift-Key-Insert>
+beginning-of-line = <Key-Home>
+center-insert = <Control-Key-l>
+close-all-windows = <Control-Key-q>
+close-window = <Control-Key-w> <Control-Shift-Key-W>
+do-nothing = <Control-Key-F12>
+end-of-file = <Control-Key-d>
+history-next = <Alt-Key-n> <Meta-Key-n>
+history-previous = <Alt-Key-p> <Meta-Key-p>
+interrupt-execution = <Control-Key-c>
+view-restart = <Key-F6>
+restart-shell = <Control-Key-F6>
+open-class-browser = <Control-Key-b>
+open-module = <Control-Key-m>
+open-new-window = <Control-Key-n>
+open-window-from-file = <Control-Key-o>
+plain-newline-and-indent = <Control-Key-j>
+print-window = <Control-Key-p>
+python-context-help = <Shift-Key-F1>
+python-docs = <Key-F1>
+redo = <Control-Shift-Key-Z>
+remove-selection = <Key-Escape>
+save-copy-of-window-as-file = <Alt-Shift-Key-S>
+save-window-as-file = <Control-Shift-Key-S>
+save-window = <Control-Key-s>
+select-all = <Control-Key-a>
+toggle-auto-coloring = <Control-Key-slash>
+undo = <Control-Key-z>
+find = <Control-Key-f>
+find-again = <Key-F3>
+find-in-files = <Control-Shift-Key-f>
+find-selection = <Control-Key-h>
+replace = <Control-Key-r>
+goto-line = <Control-Key-g>
+smart-backspace = <Key-BackSpace>
+newline-and-indent = <Key-Return> <Key-KP_Enter>
+smart-indent = <Key-Tab>
+indent-region = <Control-Key-bracketright>
+dedent-region = <Control-Key-bracketleft>
+comment-region = <Control-Key-d>
+uncomment-region = <Control-Shift-Key-D>
+tabify-region = <Alt-Key-5>
+untabify-region = <Alt-Key-6>
+toggle-tabs = <Control-Key-T>
+change-indentwidth = <Alt-Key-u>
+del-word-left = <Control-Key-BackSpace>
+del-word-right = <Control-Key-Delete>
+
[IDLE Classic Mac]
copy=<Command-Key-c>
cut=<Command-Key-x>
diff --git a/Lib/idlelib/config-main.def b/Lib/idlelib/config-main.def
index 8ebbc1b..16f4b09 100644
--- a/Lib/idlelib/config-main.def
+++ b/Lib/idlelib/config-main.def
@@ -4,44 +4,50 @@
# When IDLE starts, it will look in
# the following two sets of files, in order:
#
-# default configuration
-# ---------------------
-# config-main.def the default general config file
-# config-extensions.def the default extension config file
-# config-highlight.def the default highlighting config file
-# config-keys.def the default keybinding config file
+# default configuration files in idlelib
+# --------------------------------------
+# config-main.def default general config file
+# config-extensions.def default extension config file
+# config-highlight.def default highlighting config file
+# config-keys.def default keybinding config file
#
-# user configuration
-# -------------------
-# ~/.idlerc/config-main.cfg the user general config file
-# ~/.idlerc/config-extensions.cfg the user extension config file
-# ~/.idlerc/config-highlight.cfg the user highlighting config file
-# ~/.idlerc/config-keys.cfg the user keybinding config file
+# user configuration files in ~/.idlerc
+# -------------------------------------
+# config-main.cfg user general config file
+# config-extensions.cfg user extension config file
+# config-highlight.cfg user highlighting config file
+# config-keys.cfg user keybinding config file
#
-# On Windows2000 and Windows XP the .idlerc directory is at
-# Documents and Settings\<username>\.idlerc
-#
-# On Windows98 it is at c:\.idlerc
+# On Windows, the default location of the home directory ('~' above)
+# depends on the version. For Windows 10, it is C:\Users\<username>.
#
# Any options the user saves through the config dialog will be saved to
-# the relevant user config file. Reverting any general setting to the
-# default causes that entry to be wiped from the user file and re-read
-# from the default file. User highlighting themes or keybinding sets are
-# retained unless specifically deleted within the config dialog. Choosing
-# one of the default themes or keysets just applies the relevant settings
-# from the default file.
+# the relevant user config file. Reverting any general or extension
+# setting to the default causes that entry to be wiped from the user
+# file and re-read from the default file. This rule applies to each
+# item, except that the three editor font items are saved as a group.
+#
+# User highlighting themes and keybinding sets must have (section) names
+# distinct from the default names. All items are added and saved as a
+# group. They are retained unless specifically deleted within the config
+# dialog. Choosing one of the default themes or keysets just applies the
+# relevant settings from the default file.
+#
+# Additional help sources are listed in the [HelpFiles] section below
+# and should be viewable by a web browser (or the Windows Help viewer in
+# the case of .chm files). These sources will be listed on the Help
+# menu. The pattern, and two examples, are
#
-# Additional help sources are listed in the [HelpFiles] section and must be
-# viewable by a web browser (or the Windows Help viewer in the case of .chm
-# files). These sources will be listed on the Help menu. The pattern is
# <sequence_number = menu item;/path/to/help/source>
-# You can't use a semi-colon in a menu item or path. The path will be platform
-# specific because of path separators, drive specs etc.
+# 1 = IDLE;C:/Programs/Python36/Lib/idlelib/help.html
+# 2 = Pillow;https://pillow.readthedocs.io/en/latest/
#
-# It is best to use the Configuration GUI to set up additional help sources!
-# Example:
-#1 = My Extra Help Source;/usr/share/doc/foo/index.html
-#2 = Another Help Source;/path/to/another.pdf
+# You can't use a semi-colon in a menu item or path. The path will be
+# platform specific because of path separators, drive specs etc.
+#
+# The default files should not be edited except to add new sections to
+# config-extensions.def for added extensions . The user files should be
+# modified through the Settings dialog.
[General]
editor-on-startup= 0
@@ -54,6 +60,8 @@ delete-exitfunc= 1
width= 80
height= 40
font= TkFixedFont
+# For TkFixedFont, the actual size and boldness are obtained from tk
+# and override 10 and 0. See idlelib.config.IdleConf.GetFont
font-size= 10
font-bold= 0
encoding= none
@@ -70,7 +78,9 @@ name2=
[Keys]
default= 1
-name= IDLE Classic Windows
+name=
+name2=
+# name2 set in user config-main.cfg for keys added after 2016 July 1
[History]
cyclic=1
diff --git a/Lib/idlelib/configHandler.py b/Lib/idlelib/config.py
index 8954488..358bee4 100644
--- a/Lib/idlelib/configHandler.py
+++ b/Lib/idlelib/config.py
@@ -1,13 +1,20 @@
-"""Provides access to stored IDLE configuration information.
-
-Refer to the comments at the beginning of config-main.def for a description of
-the available configuration files and the design implemented to update user
-configuration information. In particular, user configuration choices which
-duplicate the defaults will be removed from the user's configuration files,
-and if a file becomes empty, it will be deleted.
-
-The contents of the user files may be altered using the Options/Configure IDLE
-menu to access the configuration GUI (configDialog.py), or manually.
+"""idlelib.config -- Manage IDLE configuration information.
+
+The comments at the beginning of config-main.def describe the
+configuration files and the design implemented to update user
+configuration information. In particular, user configuration choices
+which duplicate the defaults will be removed from the user's
+configuration files, and if a user file becomes empty, it will be
+deleted.
+
+The configuration database maps options to values. Comceptually, the
+database keys are tuples (config-type, section, item). As implemented,
+there are separate dicts for default and user values. Each has
+config-type keys 'main', 'extensions', 'highlight', and 'keys'. The
+value for each key is a ConfigParser instance that maps section and item
+to values. For 'main' and 'extenstons', user values override
+default values. For 'highlight' and 'keys', user sections augment the
+default sections (and must, therefore, have distinct names).
Throughout this module there is an emphasis on returning useable defaults
when a problem occurs in returning a requested configuration value back to
@@ -18,11 +25,10 @@ configuration problem notification and resolution.
"""
# TODOs added Oct 2014, tjr
+from configparser import ConfigParser
import os
import sys
-from configparser import ConfigParser
-from tkinter import TkVersion
from tkinter.font import Font, nametofont
class InvalidConfigType(Exception): pass
@@ -230,15 +236,12 @@ class IdleConf:
return self.userCfg[configType].Get(section, option,
type=type, raw=raw)
except ValueError:
- warning = ('\n Warning: configHandler.py - IdleConf.GetOption -\n'
+ warning = ('\n Warning: config.py - IdleConf.GetOption -\n'
' invalid %r value for configuration option %r\n'
' from section %r: %r' %
(type, option, section,
self.userCfg[configType].Get(section, option, raw=raw)))
- try:
- print(warning, file=sys.stderr)
- except OSError:
- pass
+ _warn(warning, configType, section, option)
try:
if self.defaultCfg[configType].has_option(section,option):
return self.defaultCfg[configType].Get(
@@ -247,15 +250,12 @@ class IdleConf:
pass
#returning default, print warning
if warn_on_default:
- warning = ('\n Warning: configHandler.py - IdleConf.GetOption -\n'
+ warning = ('\n Warning: config.py - IdleConf.GetOption -\n'
' problem retrieving configuration option %r\n'
' from section %r.\n'
' returning default value: %r' %
(option, section, default))
- try:
- print(warning, file=sys.stderr)
- except OSError:
- pass
+ _warn(warning, configType, section, option)
return default
def SetOption(self, configType, section, option, value):
@@ -358,52 +358,73 @@ class IdleConf:
for element in theme:
if not cfgParser.has_option(themeName, element):
# Print warning that will return a default color
- warning = ('\n Warning: configHandler.IdleConf.GetThemeDict'
+ warning = ('\n Warning: config.IdleConf.GetThemeDict'
' -\n problem retrieving theme element %r'
'\n from theme %r.\n'
' returning default color: %r' %
(element, themeName, theme[element]))
- try:
- print(warning, file=sys.stderr)
- except OSError:
- pass
+ _warn(warning, 'highlight', themeName, element)
theme[element] = cfgParser.Get(
themeName, element, default=theme[element])
return theme
def CurrentTheme(self):
- """Return the name of the currently active text color theme.
+ "Return the name of the currently active text color theme."
+ return self.current_colors_and_keys('Theme')
+
+ def CurrentKeys(self):
+ """Return the name of the currently active key set."""
+ return self.current_colors_and_keys('Keys')
+
+ def current_colors_and_keys(self, section):
+ """Return the currently active name for Theme or Keys section.
+
+ idlelib.config-main.def ('default') includes these sections
- idlelib.config-main.def includes this section
[Theme]
default= 1
name= IDLE Classic
name2=
- # name2 set in user config-main.cfg for themes added after 2015 Oct 1
- Item name2 is needed because setting name to a new builtin
- causes older IDLEs to display multiple error messages or quit.
+ [Keys]
+ default= 1
+ name=
+ name2=
+
+ Item 'name2', is used for built-in ('default') themes and keys
+ added after 2015 Oct 1 and 2016 July 1. This kludge is needed
+ because setting 'name' to a builtin not defined in older IDLEs
+ to display multiple error messages or quit.
See https://bugs.python.org/issue25313.
- When default = True, name2 takes precedence over name,
- while older IDLEs will just use name.
+ When default = True, 'name2' takes precedence over 'name',
+ while older IDLEs will just use name. When default = False,
+ 'name2' may still be set, but it is ignored.
"""
- default = self.GetOption('main', 'Theme', 'default',
+ cfgname = 'highlight' if section == 'Theme' else 'keys'
+ default = self.GetOption('main', section, 'default',
type='bool', default=True)
+ name = ''
if default:
- theme = self.GetOption('main', 'Theme', 'name2', default='')
- if default and not theme or not default:
- theme = self.GetOption('main', 'Theme', 'name', default='')
- source = self.defaultCfg if default else self.userCfg
- if source['highlight'].has_section(theme):
- return theme
+ name = self.GetOption('main', section, 'name2', default='')
+ if not name:
+ name = self.GetOption('main', section, 'name', default='')
+ if name:
+ source = self.defaultCfg if default else self.userCfg
+ if source[cfgname].has_section(name):
+ return name
+ return "IDLE Classic" if section == 'Theme' else self.default_keys()
+
+ @staticmethod
+ def default_keys():
+ if sys.platform[:3] == 'win':
+ return 'IDLE Classic Windows'
+ elif sys.platform == 'darwin':
+ return 'IDLE Classic OSX'
else:
- return "IDLE Classic"
+ return 'IDLE Modern Unix'
- def CurrentKeys(self):
- "Return the name of the currently active key set."
- return self.GetOption('main', 'Keys', 'name', default='')
-
- def GetExtensions(self, active_only=True, editor_only=False, shell_only=False):
+ def GetExtensions(self, active_only=True,
+ editor_only=False, shell_only=False):
"""Return extensions in default and user config-extensions files.
If active_only True, only return active (enabled) extensions
@@ -423,7 +444,7 @@ class IdleConf:
if self.GetOption('extensions', extn, 'enable', default=True,
type='bool'):
#the extension is enabled
- if editor_only or shell_only: # TODO if both, contradictory
+ if editor_only or shell_only: # TODO both True contradict
if editor_only:
option = "enable_editor"
else:
@@ -528,7 +549,8 @@ class IdleConf:
eventStr - virtual event, including brackets, as in '<<event>>'.
"""
eventName = eventStr[2:-2] #trim off the angle brackets
- binding = self.GetOption('keys', keySetName, eventName, default='').split()
+ binding = self.GetOption('keys', keySetName, eventName, default='',
+ warn_on_default=False).split()
return binding
def GetCurrentKeySet(self):
@@ -639,20 +661,28 @@ class IdleConf:
'<<del-word-right>>': ['<Control-Key-Delete>']
}
if keySetName:
- for event in keyBindings:
- binding = self.GetKeyBinding(keySetName, event)
- if binding:
- keyBindings[event] = binding
- else: #we are going to return a default, print warning
- warning=('\n Warning: configHandler.py - IdleConf.GetCoreKeys'
- ' -\n problem retrieving key binding for event %r'
- '\n from key set %r.\n'
- ' returning default value: %r' %
- (event, keySetName, keyBindings[event]))
- try:
- print(warning, file=sys.stderr)
- except OSError:
- pass
+ if not (self.userCfg['keys'].has_section(keySetName) or
+ self.defaultCfg['keys'].has_section(keySetName)):
+ warning = (
+ '\n Warning: config.py - IdleConf.GetCoreKeys -\n'
+ ' key set %r is not defined, using default bindings.' %
+ (keySetName,)
+ )
+ _warn(warning, 'keys', keySetName)
+ else:
+ for event in keyBindings:
+ binding = self.GetKeyBinding(keySetName, event)
+ if binding:
+ keyBindings[event] = binding
+ else: #we are going to return a default, print warning
+ warning = (
+ '\n Warning: config.py - IdleConf.GetCoreKeys -\n'
+ ' problem retrieving key binding for event %r\n'
+ ' from key set %r.\n'
+ ' returning default value: %r' %
+ (event, keySetName, keyBindings[event])
+ )
+ _warn(warning, 'keys', keySetName, event)
return keyBindings
def GetExtraHelpSourceList(self, configSet):
@@ -713,16 +743,13 @@ class IdleConf:
bold = self.GetOption(configType, section, 'font-bold', default=0,
type='bool')
if (family == 'TkFixedFont'):
- if TkVersion < 8.5:
- family = 'Courier'
- else:
- f = Font(name='TkFixedFont', exists=True, root=root)
- actualFont = Font.actual(f)
- family = actualFont['family']
- size = actualFont['size']
- if size <= 0:
- size = 10 # if font in pixels, ignore actual size
- bold = actualFont['weight']=='bold'
+ f = Font(name='TkFixedFont', exists=True, root=root)
+ actualFont = Font.actual(f)
+ family = actualFont['family']
+ size = actualFont['size']
+ if size <= 0:
+ size = 10 # if font in pixels, ignore actual size
+ bold = actualFont['weight'] == 'bold'
return (family, size, 'bold' if bold else 'normal')
def LoadCfgFiles(self):
@@ -739,6 +766,18 @@ class IdleConf:
idleConf = IdleConf()
+
+_warned = set()
+def _warn(msg, *key):
+ key = (msg,) + key
+ if key not in _warned:
+ try:
+ print(msg, file=sys.stderr)
+ except OSError:
+ pass
+ _warned.add(key)
+
+
# TODO Revise test output, write expanded unittest
#
if __name__ == '__main__':
diff --git a/Lib/idlelib/configHelpSourceEdit.py b/Lib/idlelib/configHelpSourceEdit.py
deleted file mode 100644
index cde8118..0000000
--- a/Lib/idlelib/configHelpSourceEdit.py
+++ /dev/null
@@ -1,170 +0,0 @@
-"Dialog to specify or edit the parameters for a user configured help source."
-
-import os
-import sys
-
-from tkinter import *
-import tkinter.messagebox as tkMessageBox
-import tkinter.filedialog as tkFileDialog
-
-class GetHelpSourceDialog(Toplevel):
- def __init__(self, parent, title, menuItem='', filePath='', _htest=False):
- """Get menu entry and url/ local file location for Additional Help
-
- User selects a name for the Help resource and provides a web url
- or a local file as its source. The user can enter a url or browse
- for the file.
-
- _htest - bool, change box location when running htest
- """
- Toplevel.__init__(self, parent)
- self.configure(borderwidth=5)
- self.resizable(height=FALSE, width=FALSE)
- self.title(title)
- self.transient(parent)
- self.grab_set()
- self.protocol("WM_DELETE_WINDOW", self.cancel)
- self.parent = parent
- self.result = None
- self.create_widgets()
- self.menu.set(menuItem)
- self.path.set(filePath)
- self.withdraw() #hide while setting geometry
- #needs to be done here so that the winfo_reqwidth is valid
- self.update_idletasks()
- #centre dialog over parent. below parent if running htest.
- self.geometry(
- "+%d+%d" % (
- parent.winfo_rootx() +
- (parent.winfo_width()/2 - self.winfo_reqwidth()/2),
- parent.winfo_rooty() +
- ((parent.winfo_height()/2 - self.winfo_reqheight()/2)
- if not _htest else 150)))
- self.deiconify() #geometry set, unhide
- self.bind('<Return>', self.ok)
- self.wait_window()
-
- def create_widgets(self):
- self.menu = StringVar(self)
- self.path = StringVar(self)
- self.fontSize = StringVar(self)
- self.frameMain = Frame(self, borderwidth=2, relief=GROOVE)
- self.frameMain.pack(side=TOP, expand=TRUE, fill=BOTH)
- labelMenu = Label(self.frameMain, anchor=W, justify=LEFT,
- text='Menu Item:')
- self.entryMenu = Entry(self.frameMain, textvariable=self.menu,
- width=30)
- self.entryMenu.focus_set()
- labelPath = Label(self.frameMain, anchor=W, justify=LEFT,
- text='Help File Path: Enter URL or browse for file')
- self.entryPath = Entry(self.frameMain, textvariable=self.path,
- width=40)
- self.entryMenu.focus_set()
- labelMenu.pack(anchor=W, padx=5, pady=3)
- self.entryMenu.pack(anchor=W, padx=5, pady=3)
- labelPath.pack(anchor=W, padx=5, pady=3)
- self.entryPath.pack(anchor=W, padx=5, pady=3)
- browseButton = Button(self.frameMain, text='Browse', width=8,
- command=self.browse_file)
- browseButton.pack(pady=3)
- frameButtons = Frame(self)
- frameButtons.pack(side=BOTTOM, fill=X)
- self.buttonOk = Button(frameButtons, text='OK',
- width=8, default=ACTIVE, command=self.ok)
- self.buttonOk.grid(row=0, column=0, padx=5,pady=5)
- self.buttonCancel = Button(frameButtons, text='Cancel',
- width=8, command=self.cancel)
- self.buttonCancel.grid(row=0, column=1, padx=5, pady=5)
-
- def browse_file(self):
- filetypes = [
- ("HTML Files", "*.htm *.html", "TEXT"),
- ("PDF Files", "*.pdf", "TEXT"),
- ("Windows Help Files", "*.chm"),
- ("Text Files", "*.txt", "TEXT"),
- ("All Files", "*")]
- path = self.path.get()
- if path:
- dir, base = os.path.split(path)
- else:
- base = None
- if sys.platform[:3] == 'win':
- dir = os.path.join(os.path.dirname(sys.executable), 'Doc')
- if not os.path.isdir(dir):
- dir = os.getcwd()
- else:
- dir = os.getcwd()
- opendialog = tkFileDialog.Open(parent=self, filetypes=filetypes)
- file = opendialog.show(initialdir=dir, initialfile=base)
- if file:
- self.path.set(file)
-
- def menu_ok(self):
- "Simple validity check for a sensible menu item name"
- menu_ok = True
- menu = self.menu.get()
- menu.strip()
- if not menu:
- tkMessageBox.showerror(title='Menu Item Error',
- message='No menu item specified',
- parent=self)
- self.entryMenu.focus_set()
- menu_ok = False
- elif len(menu) > 30:
- tkMessageBox.showerror(title='Menu Item Error',
- message='Menu item too long:'
- '\nLimit 30 characters.',
- parent=self)
- self.entryMenu.focus_set()
- menu_ok = False
- return menu_ok
-
- def path_ok(self):
- "Simple validity check for menu file path"
- path_ok = True
- path = self.path.get()
- path.strip()
- if not path: #no path specified
- tkMessageBox.showerror(title='File Path Error',
- message='No help file path specified.',
- parent=self)
- self.entryPath.focus_set()
- path_ok = False
- elif path.startswith(('www.', 'http')):
- pass
- else:
- if path[:5] == 'file:':
- path = path[5:]
- if not os.path.exists(path):
- tkMessageBox.showerror(title='File Path Error',
- message='Help file path does not exist.',
- parent=self)
- self.entryPath.focus_set()
- path_ok = False
- return path_ok
-
- def ok(self, event=None):
- if self.menu_ok() and self.path_ok():
- self.result = (self.menu.get().strip(),
- self.path.get().strip())
- if sys.platform == 'darwin':
- path = self.result[1]
- if path.startswith(('www', 'file:', 'http:', 'https:')):
- pass
- else:
- # Mac Safari insists on using the URI form for local files
- self.result = list(self.result)
- self.result[1] = "file://" + path
- self.destroy()
-
- def cancel(self, event=None):
- self.result = None
- self.destroy()
-
-if __name__ == '__main__':
- import unittest
- unittest.main('idlelib.idle_test.test_config_help',
- verbosity=2, exit=False)
-
- from idlelib.idle_test.htest import run
- run(GetHelpSourceDialog)
diff --git a/Lib/idlelib/configSectionNameDialog.py b/Lib/idlelib/configSectionNameDialog.py
deleted file mode 100644
index 5137836..0000000
--- a/Lib/idlelib/configSectionNameDialog.py
+++ /dev/null
@@ -1,98 +0,0 @@
-"""
-Dialog that allows user to specify a new config file section name.
-Used to get new highlight theme and keybinding set names.
-The 'return value' for the dialog, used two placed in configDialog.py,
-is the .result attribute set in the Ok and Cancel methods.
-"""
-from tkinter import *
-import tkinter.messagebox as tkMessageBox
-
-class GetCfgSectionNameDialog(Toplevel):
- def __init__(self, parent, title, message, used_names, _htest=False):
- """
- message - string, informational message to display
- used_names - string collection, names already in use for validity check
- _htest - bool, change box location when running htest
- """
- Toplevel.__init__(self, parent)
- self.configure(borderwidth=5)
- self.resizable(height=FALSE, width=FALSE)
- self.title(title)
- self.transient(parent)
- self.grab_set()
- self.protocol("WM_DELETE_WINDOW", self.Cancel)
- self.parent = parent
- self.message = message
- self.used_names = used_names
- self.create_widgets()
- self.withdraw() #hide while setting geometry
- self.update_idletasks()
- #needs to be done here so that the winfo_reqwidth is valid
- self.messageInfo.config(width=self.frameMain.winfo_reqwidth())
- self.geometry(
- "+%d+%d" % (
- parent.winfo_rootx() +
- (parent.winfo_width()/2 - self.winfo_reqwidth()/2),
- parent.winfo_rooty() +
- ((parent.winfo_height()/2 - self.winfo_reqheight()/2)
- if not _htest else 100)
- ) ) #centre dialog over parent (or below htest box)
- self.deiconify() #geometry set, unhide
- self.wait_window()
-
- def create_widgets(self):
- self.name = StringVar(self.parent)
- self.fontSize = StringVar(self.parent)
- self.frameMain = Frame(self, borderwidth=2, relief=SUNKEN)
- self.frameMain.pack(side=TOP, expand=TRUE, fill=BOTH)
- self.messageInfo = Message(self.frameMain, anchor=W, justify=LEFT,
- padx=5, pady=5, text=self.message) #,aspect=200)
- entryName = Entry(self.frameMain, textvariable=self.name, width=30)
- entryName.focus_set()
- self.messageInfo.pack(padx=5, pady=5) #, expand=TRUE, fill=BOTH)
- entryName.pack(padx=5, pady=5)
-
- frameButtons = Frame(self, pady=2)
- frameButtons.pack(side=BOTTOM)
- self.buttonOk = Button(frameButtons, text='Ok',
- width=8, command=self.Ok)
- self.buttonOk.pack(side=LEFT, padx=5)
- self.buttonCancel = Button(frameButtons, text='Cancel',
- width=8, command=self.Cancel)
- self.buttonCancel.pack(side=RIGHT, padx=5)
-
- def name_ok(self):
- ''' After stripping entered name, check that it is a sensible
- ConfigParser file section name. Return it if it is, '' if not.
- '''
- name = self.name.get().strip()
- if not name: #no name specified
- tkMessageBox.showerror(title='Name Error',
- message='No name specified.', parent=self)
- elif len(name)>30: #name too long
- tkMessageBox.showerror(title='Name Error',
- message='Name too long. It should be no more than '+
- '30 characters.', parent=self)
- name = ''
- elif name in self.used_names:
- tkMessageBox.showerror(title='Name Error',
- message='This name is already in use.', parent=self)
- name = ''
- return name
-
- def Ok(self, event=None):
- name = self.name_ok()
- if name:
- self.result = name
- self.destroy()
-
- def Cancel(self, event=None):
- self.result = ''
- self.destroy()
-
-if __name__ == '__main__':
- import unittest
- unittest.main('idlelib.idle_test.test_config_name', verbosity=2, exit=False)
-
- from idlelib.idle_test.htest import run
- run(GetCfgSectionNameDialog)
diff --git a/Lib/idlelib/keybindingDialog.py b/Lib/idlelib/config_key.py
index e6438bf..2602293 100644
--- a/Lib/idlelib/keybindingDialog.py
+++ b/Lib/idlelib/config_key.py
@@ -2,31 +2,35 @@
Dialog for building Tkinter accelerator key bindings
"""
from tkinter import *
+from tkinter.ttk import Scrollbar
import tkinter.messagebox as tkMessageBox
import string
import sys
class GetKeysDialog(Toplevel):
- def __init__(self,parent,title,action,currentKeySequences,_htest=False):
+ def __init__(self, parent, title, action, currentKeySequences,
+ _htest=False, _utest=False):
"""
action - string, the name of the virtual event these keys will be
mapped to
currentKeys - list, a list of all key sequence lists currently mapped
to virtual events, for overlap checking
+ _utest - bool, do not wait when running unittest
_htest - bool, change box location when running htest
"""
Toplevel.__init__(self, parent)
+ self.withdraw() #hide while setting geometry
self.configure(borderwidth=5)
- self.resizable(height=FALSE,width=FALSE)
+ self.resizable(height=FALSE, width=FALSE)
self.title(title)
self.transient(parent)
self.grab_set()
self.protocol("WM_DELETE_WINDOW", self.Cancel)
self.parent = parent
self.action=action
- self.currentKeySequences=currentKeySequences
- self.result=''
- self.keyString=StringVar(self)
+ self.currentKeySequences = currentKeySequences
+ self.result = ''
+ self.keyString = StringVar(self)
self.keyString.set('')
self.SetModifiersForPlatform() # set self.modifiers, self.modifier_label
self.modifier_vars = []
@@ -37,7 +41,6 @@ class GetKeysDialog(Toplevel):
self.advanced = False
self.CreateWidgets()
self.LoadFinalKeyList()
- self.withdraw() #hide while setting geometry
self.update_idletasks()
self.geometry(
"+%d+%d" % (
@@ -47,8 +50,9 @@ class GetKeysDialog(Toplevel):
((parent.winfo_height()/2 - self.winfo_reqheight()/2)
if not _htest else 150)
) ) #centre dialog over parent (or below htest box)
- self.deiconify() #geometry set, unhide
- self.wait_window()
+ if not _utest:
+ self.deiconify() #geometry set, unhide
+ self.wait_window()
def CreateWidgets(self):
frameMain = Frame(self,borderwidth=2,relief=SUNKEN)
@@ -261,6 +265,7 @@ class GetKeysDialog(Toplevel):
keysOK = True
return keysOK
+
if __name__ == '__main__':
from idlelib.idle_test.htest import run
run(GetKeysDialog)
diff --git a/Lib/idlelib/configDialog.py b/Lib/idlelib/configdialog.py
index 5f5bd36..8184582 100644
--- a/Lib/idlelib/configDialog.py
+++ b/Lib/idlelib/configdialog.py
@@ -10,18 +10,18 @@ Refer to comments in EditorWindow autoindent code for details.
"""
from tkinter import *
-import tkinter.messagebox as tkMessageBox
+from tkinter.ttk import Scrollbar
import tkinter.colorchooser as tkColorChooser
import tkinter.font as tkFont
+import tkinter.messagebox as tkMessageBox
-from idlelib.configHandler import idleConf
-from idlelib.dynOptionMenuWidget import DynOptionMenu
-from idlelib.keybindingDialog import GetKeysDialog
-from idlelib.configSectionNameDialog import GetCfgSectionNameDialog
-from idlelib.configHelpSourceEdit import GetHelpSourceDialog
+from idlelib.config import idleConf
+from idlelib.config_key import GetKeysDialog
+from idlelib.dynoption import DynOptionMenu
+from idlelib import macosx
+from idlelib.query import SectionName, HelpSource
from idlelib.tabbedpages import TabbedPageSet
-from idlelib.textView import view_text
-from idlelib import macosxSupport
+from idlelib.textview import view_text
class ConfigDialog(Toplevel):
@@ -91,7 +91,7 @@ class ConfigDialog(Toplevel):
self.create_action_buttons().pack(side=BOTTOM)
def create_action_buttons(self):
- if macosxSupport.isAquaTk():
+ if macosx.isAquaTk():
# Changing the default padding on OSX results in unreadable
# text in the buttons
paddingArgs = {}
@@ -341,6 +341,7 @@ class ConfigDialog(Toplevel):
buttonSaveCustomKeys = Button(
frames[1], text='Save as New Custom Key Set',
command=self.SaveAsNewKeySet)
+ self.new_custom_keys = Label(frames[0], bd=2)
##widget packing
#body
@@ -361,6 +362,7 @@ class ConfigDialog(Toplevel):
self.radioKeysCustom.grid(row=1, column=0, sticky=W+NS)
self.optMenuKeysBuiltin.grid(row=0, column=1, sticky=NSEW)
self.optMenuKeysCustom.grid(row=1, column=1, sticky=NSEW)
+ self.new_custom_keys.grid(row=0, column=2, sticky=NSEW, padx=5, pady=5)
self.buttonDeleteCustomKeys.pack(side=LEFT, fill=X, expand=True, padx=2)
buttonSaveCustomKeys.pack(side=LEFT, fill=X, expand=True, padx=2)
frames[0].pack(side=TOP, fill=BOTH, expand=True)
@@ -390,28 +392,28 @@ class ConfigDialog(Toplevel):
text=' Additional Help Sources ')
#frameRun
labelRunChoiceTitle = Label(frameRun, text='At Startup')
- radioStartupEdit = Radiobutton(
+ self.radioStartupEdit = Radiobutton(
frameRun, variable=self.startupEdit, value=1,
- command=self.SetKeysType, text="Open Edit Window")
- radioStartupShell = Radiobutton(
+ text="Open Edit Window")
+ self.radioStartupShell = Radiobutton(
frameRun, variable=self.startupEdit, value=0,
- command=self.SetKeysType, text='Open Shell Window')
+ text='Open Shell Window')
#frameSave
labelRunSaveTitle = Label(frameSave, text='At Start of Run (F5) ')
- radioSaveAsk = Radiobutton(
+ self.radioSaveAsk = Radiobutton(
frameSave, variable=self.autoSave, value=0,
- command=self.SetKeysType, text="Prompt to Save")
- radioSaveAuto = Radiobutton(
+ text="Prompt to Save")
+ self.radioSaveAuto = Radiobutton(
frameSave, variable=self.autoSave, value=1,
- command=self.SetKeysType, text='No Prompt')
+ text='No Prompt')
#frameWinSize
labelWinSizeTitle = Label(
frameWinSize, text='Initial Window Size (in characters)')
labelWinWidthTitle = Label(frameWinSize, text='Width')
- entryWinWidth = Entry(
+ self.entryWinWidth = Entry(
frameWinSize, textvariable=self.winWidth, width=3)
labelWinHeightTitle = Label(frameWinSize, text='Height')
- entryWinHeight = Entry(
+ self.entryWinHeight = Entry(
frameWinSize, textvariable=self.winHeight, width=3)
#frameHelp
frameHelpList = Frame(frameHelp)
@@ -441,17 +443,17 @@ class ConfigDialog(Toplevel):
frameHelp.pack(side=TOP, padx=5, pady=5, expand=TRUE, fill=BOTH)
#frameRun
labelRunChoiceTitle.pack(side=LEFT, anchor=W, padx=5, pady=5)
- radioStartupShell.pack(side=RIGHT, anchor=W, padx=5, pady=5)
- radioStartupEdit.pack(side=RIGHT, anchor=W, padx=5, pady=5)
+ self.radioStartupShell.pack(side=RIGHT, anchor=W, padx=5, pady=5)
+ self.radioStartupEdit.pack(side=RIGHT, anchor=W, padx=5, pady=5)
#frameSave
labelRunSaveTitle.pack(side=LEFT, anchor=W, padx=5, pady=5)
- radioSaveAuto.pack(side=RIGHT, anchor=W, padx=5, pady=5)
- radioSaveAsk.pack(side=RIGHT, anchor=W, padx=5, pady=5)
+ self.radioSaveAuto.pack(side=RIGHT, anchor=W, padx=5, pady=5)
+ self.radioSaveAsk.pack(side=RIGHT, anchor=W, padx=5, pady=5)
#frameWinSize
labelWinSizeTitle.pack(side=LEFT, anchor=W, padx=5, pady=5)
- entryWinHeight.pack(side=RIGHT, anchor=E, padx=10, pady=5)
+ self.entryWinHeight.pack(side=RIGHT, anchor=E, padx=10, pady=5)
labelWinHeightTitle.pack(side=RIGHT, anchor=E, pady=5)
- entryWinWidth.pack(side=RIGHT, anchor=E, padx=10, pady=5)
+ self.entryWinWidth.pack(side=RIGHT, anchor=E, padx=10, pady=5)
labelWinWidthTitle.pack(side=RIGHT, anchor=E, pady=5)
#frameHelp
frameHelpListButtons.pack(side=RIGHT, padx=5, pady=5, fill=Y)
@@ -464,24 +466,24 @@ class ConfigDialog(Toplevel):
return frame
def AttachVarCallbacks(self):
- self.fontSize.trace_variable('w', self.VarChanged_font)
- self.fontName.trace_variable('w', self.VarChanged_font)
- self.fontBold.trace_variable('w', self.VarChanged_font)
- self.spaceNum.trace_variable('w', self.VarChanged_spaceNum)
- self.colour.trace_variable('w', self.VarChanged_colour)
- self.builtinTheme.trace_variable('w', self.VarChanged_builtinTheme)
- self.customTheme.trace_variable('w', self.VarChanged_customTheme)
- self.themeIsBuiltin.trace_variable('w', self.VarChanged_themeIsBuiltin)
- self.highlightTarget.trace_variable('w', self.VarChanged_highlightTarget)
- self.keyBinding.trace_variable('w', self.VarChanged_keyBinding)
- self.builtinKeys.trace_variable('w', self.VarChanged_builtinKeys)
- self.customKeys.trace_variable('w', self.VarChanged_customKeys)
- self.keysAreBuiltin.trace_variable('w', self.VarChanged_keysAreBuiltin)
- self.winWidth.trace_variable('w', self.VarChanged_winWidth)
- self.winHeight.trace_variable('w', self.VarChanged_winHeight)
- self.startupEdit.trace_variable('w', self.VarChanged_startupEdit)
- self.autoSave.trace_variable('w', self.VarChanged_autoSave)
- self.encoding.trace_variable('w', self.VarChanged_encoding)
+ self.fontSize.trace_add('write', self.VarChanged_font)
+ self.fontName.trace_add('write', self.VarChanged_font)
+ self.fontBold.trace_add('write', self.VarChanged_font)
+ self.spaceNum.trace_add('write', self.VarChanged_spaceNum)
+ self.colour.trace_add('write', self.VarChanged_colour)
+ self.builtinTheme.trace_add('write', self.VarChanged_builtinTheme)
+ self.customTheme.trace_add('write', self.VarChanged_customTheme)
+ self.themeIsBuiltin.trace_add('write', self.VarChanged_themeIsBuiltin)
+ self.highlightTarget.trace_add('write', self.VarChanged_highlightTarget)
+ self.keyBinding.trace_add('write', self.VarChanged_keyBinding)
+ self.builtinKeys.trace_add('write', self.VarChanged_builtinKeys)
+ self.customKeys.trace_add('write', self.VarChanged_customKeys)
+ self.keysAreBuiltin.trace_add('write', self.VarChanged_keysAreBuiltin)
+ self.winWidth.trace_add('write', self.VarChanged_winWidth)
+ self.winHeight.trace_add('write', self.VarChanged_winHeight)
+ self.startupEdit.trace_add('write', self.VarChanged_startupEdit)
+ self.autoSave.trace_add('write', self.VarChanged_autoSave)
+ self.encoding.trace_add('write', self.VarChanged_encoding)
def remove_var_callbacks(self):
"Remove callbacks to prevent memory leaks."
@@ -492,7 +494,7 @@ class ConfigDialog(Toplevel):
self.keyBinding, self.builtinKeys, self.customKeys,
self.keysAreBuiltin, self.winWidth, self.winHeight,
self.startupEdit, self.autoSave, self.encoding,):
- var.trace_vdelete('w', var.trace_vinfo()[0][1])
+ var.trace_remove('write', var.trace_info()[0][1])
def VarChanged_font(self, *params):
'''When one font attribute changes, save them all, as they are
@@ -514,10 +516,11 @@ class ConfigDialog(Toplevel):
self.OnNewColourSet()
def VarChanged_builtinTheme(self, *params):
+ oldthemes = ('IDLE Classic', 'IDLE New')
value = self.builtinTheme.get()
- if value == 'IDLE Dark':
- if idleConf.GetOption('main', 'Theme', 'name') != 'IDLE New':
- self.AddChangedItem('main', 'Theme', 'name', 'IDLE Classic')
+ if value not in oldthemes:
+ if idleConf.GetOption('main', 'Theme', 'name') not in oldthemes:
+ self.AddChangedItem('main', 'Theme', 'name', oldthemes[0])
self.AddChangedItem('main', 'Theme', 'name2', value)
self.new_custom_theme.config(text='New theme, see Help',
fg='#500000')
@@ -557,8 +560,23 @@ class ConfigDialog(Toplevel):
self.AddChangedItem('extensions', extKeybindSection, event, value)
def VarChanged_builtinKeys(self, *params):
+ oldkeys = (
+ 'IDLE Classic Windows',
+ 'IDLE Classic Unix',
+ 'IDLE Classic Mac',
+ 'IDLE Classic OSX',
+ )
value = self.builtinKeys.get()
- self.AddChangedItem('main', 'Keys', 'name', value)
+ if value not in oldkeys:
+ if idleConf.GetOption('main', 'Keys', 'name') not in oldkeys:
+ self.AddChangedItem('main', 'Keys', 'name', oldkeys[0])
+ self.AddChangedItem('main', 'Keys', 'name2', value)
+ self.new_custom_keys.config(text='New key set, see Help',
+ fg='#500000')
+ else:
+ self.AddChangedItem('main', 'Keys', 'name', value)
+ self.AddChangedItem('main', 'Keys', 'name2', '')
+ self.new_custom_keys.config(text='', fg='black')
self.LoadKeysList(value)
def VarChanged_customKeys(self, *params):
@@ -683,7 +701,7 @@ class ConfigDialog(Toplevel):
def GetNewKeysName(self, message):
usedNames = (idleConf.GetSectionList('user', 'keys') +
idleConf.GetSectionList('default', 'keys'))
- newKeySet = GetCfgSectionNameDialog(
+ newKeySet = SectionName(
self, 'New Custom Key Set', message, usedNames).result
return newKeySet
@@ -767,8 +785,10 @@ class ConfigDialog(Toplevel):
else:
self.optMenuKeysCustom.SetMenu(itemList, itemList[0])
#revert to default key set
- self.keysAreBuiltin.set(idleConf.defaultCfg['main'].Get('Keys', 'default'))
- self.builtinKeys.set(idleConf.defaultCfg['main'].Get('Keys', 'name'))
+ self.keysAreBuiltin.set(idleConf.defaultCfg['main']
+ .Get('Keys', 'default'))
+ self.builtinKeys.set(idleConf.defaultCfg['main'].Get('Keys', 'name')
+ or idleConf.default_keys())
#user can't back out of these changes, they must be applied now
self.SaveAllChangedConfigs()
self.ActivateConfigChanges()
@@ -836,7 +856,7 @@ class ConfigDialog(Toplevel):
def GetNewThemeName(self, message):
usedNames = (idleConf.GetSectionList('user', 'highlight') +
idleConf.GetSectionList('default', 'highlight'))
- newTheme = GetCfgSectionNameDialog(
+ newTheme = SectionName(
self, 'New Custom Theme', message, usedNames).result
return newTheme
@@ -939,7 +959,8 @@ class ConfigDialog(Toplevel):
self.buttonHelpListRemove.config(state=DISABLED)
def HelpListItemAdd(self):
- helpSource = GetHelpSourceDialog(self, 'New Help Source').result
+ helpSource = HelpSource(self, 'New Help Source',
+ ).result
if helpSource:
self.userHelpList.append((helpSource[0], helpSource[1]))
self.listHelp.insert(END, helpSource[0])
@@ -949,16 +970,17 @@ class ConfigDialog(Toplevel):
def HelpListItemEdit(self):
itemIndex = self.listHelp.index(ANCHOR)
helpSource = self.userHelpList[itemIndex]
- newHelpSource = GetHelpSourceDialog(
- self, 'Edit Help Source', menuItem=helpSource[0],
- filePath=helpSource[1]).result
- if (not newHelpSource) or (newHelpSource == helpSource):
- return #no changes
- self.userHelpList[itemIndex] = newHelpSource
- self.listHelp.delete(itemIndex)
- self.listHelp.insert(itemIndex, newHelpSource[0])
- self.UpdateUserHelpChangedItems()
- self.SetHelpListButtonStates()
+ newHelpSource = HelpSource(
+ self, 'Edit Help Source',
+ menuitem=helpSource[0],
+ filepath=helpSource[1],
+ ).result
+ if newHelpSource and newHelpSource != helpSource:
+ self.userHelpList[itemIndex] = newHelpSource
+ self.listHelp.delete(itemIndex)
+ self.listHelp.insert(itemIndex, newHelpSource[0])
+ self.UpdateUserHelpChangedItems()
+ self.SetHelpListButtonStates()
def HelpListItemRemove(self):
itemIndex = self.listHelp.index(ANCHOR)
@@ -996,7 +1018,8 @@ class ConfigDialog(Toplevel):
pass
##font size dropdown
self.optMenuFontSize.SetMenu(('7', '8', '9', '10', '11', '12', '13',
- '14', '16', '18', '20', '22'), fontSize )
+ '14', '16', '18', '20', '22',
+ '25', '29', '34', '40'), fontSize )
##fontWeight
self.fontBold.set(fontBold)
##font sample
@@ -1065,7 +1088,7 @@ class ConfigDialog(Toplevel):
self.optMenuKeysCustom.SetMenu(itemList, currentOption)
itemList = idleConf.GetSectionList('default', 'keys')
itemList.sort()
- self.optMenuKeysBuiltin.SetMenu(itemList, itemList[0])
+ self.optMenuKeysBuiltin.SetMenu(itemList, idleConf.default_keys())
self.SetKeysType()
##load keyset element list
keySetName = idleConf.CurrentKeys()
@@ -1367,12 +1390,18 @@ machine. Some do not take affect until IDLE is restarted.
[Cancel] only cancels changes made since the last save.
'''
help_pages = {
- 'Highlighting':'''
+ 'Highlighting': '''
Highlighting:
The IDLE Dark color theme is new in October 2015. It can only
be used with older IDLE releases if it is saved as a custom
theme, with a different name.
-'''
+''',
+ 'Keys': '''
+Keys:
+The IDLE Modern Unix key set is new in June 2016. It can only
+be used with older IDLE releases if it is saved as a custom
+key set, with a different name.
+''',
}
diff --git a/Lib/idlelib/Debugger.py b/Lib/idlelib/debugger.py
index d5e217d..114d0d1 100644
--- a/Lib/idlelib/Debugger.py
+++ b/Lib/idlelib/debugger.py
@@ -1,9 +1,12 @@
-import os
import bdb
+import os
+
from tkinter import *
-from idlelib.WindowList import ListedToplevel
-from idlelib.ScrolledList import ScrolledList
-from idlelib import macosxSupport
+from tkinter.ttk import Scrollbar
+
+from idlelib import macosx
+from idlelib.scrolledlist import ScrolledList
+from idlelib.windows import ListedToplevel
class Idb(bdb.Bdb):
@@ -34,8 +37,10 @@ class Idb(bdb.Bdb):
return True
else:
prev_frame = frame.f_back
- if prev_frame.f_code.co_filename.count('Debugger.py'):
- # (that test will catch both Debugger.py and RemoteDebugger.py)
+ prev_name = prev_frame.f_code.co_filename
+ if 'idlelib' in prev_name and 'debugger' in prev_name:
+ # catch both idlelib/debugger.py and idlelib/debugger_r.py
+ # on both posix and windows
return False
return self.in_rpc_code(prev_frame)
@@ -370,7 +375,7 @@ class Debugger:
class StackViewer(ScrolledList):
def __init__(self, master, flist, gui):
- if macosxSupport.isAquaTk():
+ if macosx.isAquaTk():
# At least on with the stock AquaTk version on OSX 10.4 you'll
# get a shaking GUI that eventually kills IDLE if the width
# argument is specified.
@@ -502,7 +507,7 @@ class NamespaceViewer:
#
# There is also an obscure bug in sorted(dict) where the
# interpreter gets into a loop requesting non-existing dict[0],
- # dict[1], dict[2], etc from the RemoteDebugger.DictProxy.
+ # dict[1], dict[2], etc from the debugger_r.DictProxy.
###
keys_list = dict.keys()
names = sorted(keys_list)
diff --git a/Lib/idlelib/RemoteDebugger.py b/Lib/idlelib/debugger_r.py
index be2262f..bc97127 100644
--- a/Lib/idlelib/RemoteDebugger.py
+++ b/Lib/idlelib/debugger_r.py
@@ -21,7 +21,7 @@ barrier, in particular frame and traceback objects.
"""
import types
-from idlelib import Debugger
+from idlelib import debugger
debugging = 0
@@ -187,7 +187,7 @@ def start_debugger(rpchandler, gui_adap_oid):
"""
gui_proxy = GUIProxy(rpchandler, gui_adap_oid)
- idb = Debugger.Idb(gui_proxy)
+ idb = debugger.Idb(gui_proxy)
idb_adap = IdbAdapter(idb)
rpchandler.register(idb_adap_oid, idb_adap)
return idb_adap_oid
@@ -362,7 +362,7 @@ def start_remote_debugger(rpcclt, pyshell):
idb_adap_oid = rpcclt.remotecall("exec", "start_the_debugger",\
(gui_adap_oid,), {})
idb_proxy = IdbProxy(rpcclt, pyshell, idb_adap_oid)
- gui = Debugger.Debugger(pyshell, idb_proxy)
+ gui = debugger.Debugger(pyshell, idb_proxy)
gui_adap = GUIAdapter(rpcclt, gui)
rpcclt.register(gui_adap_oid, gui_adap)
return gui
@@ -373,7 +373,7 @@ def close_remote_debugger(rpcclt):
Request that the RPCServer shut down the subprocess debugger and link.
Unregister the GUIAdapter, which will cause a GC on the Idle process
debugger and RPC link objects. (The second reference to the debugger GUI
- is deleted in PyShell.close_remote_debugger().)
+ is deleted in pyshell.close_remote_debugger().)
"""
close_subprocess_debugger(rpcclt)
diff --git a/Lib/idlelib/ObjectBrowser.py b/Lib/idlelib/debugobj.py
index 7b57aa4..b70b13c 100644
--- a/Lib/idlelib/ObjectBrowser.py
+++ b/Lib/idlelib/debugobj.py
@@ -8,13 +8,10 @@
# XXX TO DO:
# - for classes/modules, add "open source" to object browser
-
-import re
-
-from idlelib.TreeWidget import TreeItem, TreeNode, ScrolledCanvas
-
from reprlib import Repr
+from idlelib.tree import TreeItem, TreeNode, ScrolledCanvas
+
myrepr = Repr()
myrepr.maxstring = 100
myrepr.maxother = 100
@@ -122,21 +119,20 @@ def make_objecttreeitem(labeltext, object, setfunction=None):
return c(labeltext, object, setfunction)
-def _object_browser(parent):
+def _object_browser(parent): # htest #
import sys
- from tkinter import Tk
- root = Tk()
- root.title("Test ObjectBrowser")
- width, height, x, y = list(map(int, re.split('[x+]', parent.geometry())))
- root.geometry("+%d+%d"%(x, y + 150))
- root.configure(bd=0, bg="yellow")
- root.focus_set()
- sc = ScrolledCanvas(root, bg="white", highlightthickness=0, takefocus=1)
+ from tkinter import Toplevel
+ top = Toplevel(parent)
+ top.title("Test debug object browser")
+ x, y = map(int, parent.geometry().split('+')[1:])
+ top.geometry("+%d+%d" % (x + 100, y + 175))
+ top.configure(bd=0, bg="yellow")
+ top.focus_set()
+ sc = ScrolledCanvas(top, bg="white", highlightthickness=0, takefocus=1)
sc.frame.pack(expand=1, fill="both")
item = make_objecttreeitem("sys", sys)
node = TreeNode(sc.canvas, None, item)
node.update()
- root.mainloop()
if __name__ == '__main__':
from idlelib.idle_test.htest import run
diff --git a/Lib/idlelib/RemoteObjectBrowser.py b/Lib/idlelib/debugobj_r.py
index 8031aae..8031aae 100644
--- a/Lib/idlelib/RemoteObjectBrowser.py
+++ b/Lib/idlelib/debugobj_r.py
diff --git a/Lib/idlelib/Delegator.py b/Lib/idlelib/delegator.py
index dc2a1aa..dc2a1aa 100644
--- a/Lib/idlelib/Delegator.py
+++ b/Lib/idlelib/delegator.py
diff --git a/Lib/idlelib/dynOptionMenuWidget.py b/Lib/idlelib/dynoption.py
index 515b4ba..9c6ffa4 100644
--- a/Lib/idlelib/dynOptionMenuWidget.py
+++ b/Lib/idlelib/dynoption.py
@@ -3,6 +3,7 @@ OptionMenu widget modified to allow dynamic menu reconfiguration
and setting of highlightthickness
"""
import copy
+
from tkinter import OptionMenu, _setit, StringVar, Button
class DynOptionMenu(OptionMenu):
@@ -34,12 +35,12 @@ class DynOptionMenu(OptionMenu):
self.variable.set(value)
def _dyn_option_menu(parent): # htest #
- from tkinter import Toplevel
+ from tkinter import Toplevel # + StringVar, Button
- top = Toplevel()
+ top = Toplevel(parent)
top.title("Tets dynamic option menu")
- top.geometry("200x100+%d+%d" % (parent.winfo_rootx() + 200,
- parent.winfo_rooty() + 150))
+ x, y = map(int, parent.geometry().split('+')[1:])
+ top.geometry("200x100+%d+%d" % (x + 250, y + 175))
top.focus_set()
var = StringVar(top)
diff --git a/Lib/idlelib/EditorWindow.py b/Lib/idlelib/editor.py
index 9944da3..ae475cb 100644
--- a/Lib/idlelib/EditorWindow.py
+++ b/Lib/idlelib/editor.py
@@ -6,28 +6,34 @@ import platform
import re
import string
import sys
+import tokenize
+import traceback
+import webbrowser
+
from tkinter import *
+from tkinter.ttk import Scrollbar
import tkinter.simpledialog as tkSimpleDialog
import tkinter.messagebox as tkMessageBox
-import traceback
-import webbrowser
-from idlelib.MultiCall import MultiCallCreator
-from idlelib import WindowList
-from idlelib import SearchDialog
-from idlelib import GrepDialog
-from idlelib import ReplaceDialog
-from idlelib import PyParse
-from idlelib.configHandler import idleConf
-from idlelib import aboutDialog, textView, configDialog
-from idlelib import macosxSupport
+from idlelib.config import idleConf
+from idlelib import configdialog
+from idlelib import grep
from idlelib import help
+from idlelib import help_about
+from idlelib import macosx
+from idlelib.multicall import MultiCallCreator
+from idlelib import pyparse
+from idlelib import query
+from idlelib import replace
+from idlelib import search
+from idlelib import textview
+from idlelib import windows
# The default tab setting for a Text widget, in average-width characters.
TK_TABWIDTH_DEFAULT = 8
-
_py_version = ' (%s)' % platform.python_version()
+
def _sphinx_version():
"Format sys.version_info to produce the Sphinx version string used to install the chm docs"
major, minor, micro, level, serial = sys.version_info
@@ -40,63 +46,16 @@ def _sphinx_version():
return release
-class HelpDialog(object):
-
- def __init__(self):
- self.parent = None # parent of help window
- self.dlg = None # the help window iteself
-
- def display(self, parent, near=None):
- """ Display the help dialog.
-
- parent - parent widget for the help window
-
- near - a Toplevel widget (e.g. EditorWindow or PyShell)
- to use as a reference for placing the help window
- """
- import warnings as w
- w.warn("EditorWindow.HelpDialog is no longer used by Idle.\n"
- "It will be removed in 3.6 or later.\n"
- "It has been replaced by private help.HelpWindow\n",
- DeprecationWarning, stacklevel=2)
- if self.dlg is None:
- self.show_dialog(parent)
- if near:
- self.nearwindow(near)
-
- def show_dialog(self, parent):
- self.parent = parent
- fn=os.path.join(os.path.abspath(os.path.dirname(__file__)),'help.txt')
- self.dlg = dlg = textView.view_file(parent,'Help',fn, modal=False)
- dlg.bind('<Destroy>', self.destroy, '+')
-
- def nearwindow(self, near):
- # Place the help dialog near the window specified by parent.
- # Note - this may not reposition the window in Metacity
- # if "/apps/metacity/general/disable_workarounds" is enabled
- dlg = self.dlg
- geom = (near.winfo_rootx() + 10, near.winfo_rooty() + 10)
- dlg.withdraw()
- dlg.geometry("=+%d+%d" % geom)
- dlg.deiconify()
- dlg.lift()
-
- def destroy(self, ev=None):
- self.dlg = None
- self.parent = None
-
-helpDialog = HelpDialog() # singleton instance, no longer used
-
-
class EditorWindow(object):
- from idlelib.Percolator import Percolator
- from idlelib.ColorDelegator import ColorDelegator, color_config
- from idlelib.UndoDelegator import UndoDelegator
- from idlelib.IOBinding import IOBinding, filesystemencoding, encoding
- from idlelib import Bindings
+ from idlelib.percolator import Percolator
+ from idlelib.colorizer import ColorDelegator, color_config
+ from idlelib.undo import UndoDelegator
+ from idlelib.iomenu import IOBinding, encoding
+ from idlelib import mainmenu
from tkinter import Toplevel
- from idlelib.MultiStatusBar import MultiStatusBar
+ from idlelib.statusbar import MultiStatusBar
+ filesystemencoding = sys.getfilesystemencoding() # for file names
help_url = None
def __init__(self, flist=None, filename=None, key=None, root=None):
@@ -136,11 +95,11 @@ class EditorWindow(object):
except AttributeError:
sys.ps1 = '>>> '
self.menubar = Menu(root)
- self.top = top = WindowList.ListedToplevel(root, menu=self.menubar)
+ self.top = top = windows.ListedToplevel(root, menu=self.menubar)
if flist:
self.tkinter_vars = flist.vars
#self.top.instance_dict makes flist.inversedict available to
- #configDialog.py so it can access all EditorWindow instances
+ #configdialog.py so it can access all EditorWindow instances
self.top.instance_dict = flist.inversedict
else:
self.tkinter_vars = {} # keys: Tkinter event names
@@ -158,13 +117,10 @@ class EditorWindow(object):
'wrap': 'none',
'highlightthickness': 0,
'width': self.width,
- 'height': idleConf.GetOption('main', 'EditorWindow',
- 'height', type='int')}
- if TkVersion >= 8.5:
- # Starting with tk 8.5 we have to set the new tabstyle option
- # to 'wordprocessor' to achieve the same display of tabs as in
- # older tk versions.
- text_options['tabstyle'] = 'wordprocessor'
+ 'tabstyle': 'wordprocessor', # new in 8.5
+ 'height': idleConf.GetOption(
+ 'main', 'EditorWindow', 'height', type='int'),
+ }
self.text = text = MultiCallCreator(Text)(text_frame, **text_options)
self.top.focused_widget = self.text
@@ -173,7 +129,7 @@ class EditorWindow(object):
self.top.protocol("WM_DELETE_WINDOW", self.close)
self.top.bind("<<close-window>>", self.close_event)
- if macosxSupport.isAquaTk():
+ if macosx.isAquaTk():
# Command-W on editorwindows doesn't work without this.
text.bind('<<close-window>>', self.close_event)
# Some OS X systems have only one mouse button, so use
@@ -309,7 +265,7 @@ class EditorWindow(object):
menu.add_separator()
end = end + 1
self.wmenu_end = end
- WindowList.register_callback(self.postwindowsmenu)
+ windows.register_callback(self.postwindowsmenu)
# Some abstractions so IDLE extensions are cross-IDE
self.askyesno = tkMessageBox.askyesno
@@ -418,7 +374,7 @@ class EditorWindow(object):
underline, label = prepstr(label)
menudict[name] = menu = Menu(mbar, name=name, tearoff=0)
mbar.add_cascade(label=label, menu=menu, underline=underline)
- if macosxSupport.isCarbonTk():
+ if macosx.isCarbonTk():
# Insert the application menu
menudict['application'] = menu = Menu(mbar, name='apple',
tearoff=0)
@@ -439,7 +395,7 @@ class EditorWindow(object):
end = -1
if end > self.wmenu_end:
menu.delete(self.wmenu_end+1, end)
- WindowList.add_windows_to_menu(menu)
+ windows.add_windows_to_menu(menu)
rmenu = None
@@ -507,17 +463,17 @@ class EditorWindow(object):
def about_dialog(self, event=None):
"Handle Help 'About IDLE' event."
- # Synchronize with macosxSupport.overrideRootMenu.about_dialog.
- aboutDialog.AboutDialog(self.top,'About IDLE')
+ # Synchronize with macosx.overrideRootMenu.about_dialog.
+ help_about.AboutDialog(self.top,'About IDLE')
def config_dialog(self, event=None):
"Handle Options 'Configure IDLE' event."
- # Synchronize with macosxSupport.overrideRootMenu.config_dialog.
- configDialog.ConfigDialog(self.top,'Settings')
+ # Synchronize with macosx.overrideRootMenu.config_dialog.
+ configdialog.ConfigDialog(self.top,'Settings')
def help_dialog(self, event=None):
"Handle Help 'IDLE Help' event."
- # Synchronize with macosxSupport.overrideRootMenu.help_dialog.
+ # Synchronize with macosx.overrideRootMenu.help_dialog.
if self.root:
parent = self.root
else:
@@ -590,23 +546,23 @@ class EditorWindow(object):
return "break"
def find_event(self, event):
- SearchDialog.find(self.text)
+ search.find(self.text)
return "break"
def find_again_event(self, event):
- SearchDialog.find_again(self.text)
+ search.find_again(self.text)
return "break"
def find_selection_event(self, event):
- SearchDialog.find_selection(self.text)
+ search.find_selection(self.text)
return "break"
def find_in_files_event(self, event):
- GrepDialog.grep(self.text, self.io, self.flist)
+ grep.grep(self.text, self.io, self.flist)
return "break"
def replace_event(self, event):
- ReplaceDialog.replace(self.text)
+ replace.replace(self.text)
return "break"
def goto_line_event(self, event):
@@ -622,46 +578,27 @@ class EditorWindow(object):
text.see("insert")
def open_module(self, event=None):
- # XXX Shouldn't this be in IOBinding?
+ """Get module name from user and open it.
+
+ Return module path or None for calls by open_class_browser
+ when latter is not invoked in named editor window.
+ """
+ # XXX This, open_class_browser, and open_path_browser
+ # would fit better in iomenu.IOBinding.
try:
- name = self.text.get("sel.first", "sel.last")
+ name = self.text.get("sel.first", "sel.last").strip()
except TclError:
- name = ""
- else:
- name = name.strip()
- name = tkSimpleDialog.askstring("Module",
- "Enter the name of a Python module\n"
- "to search on sys.path and open:",
- parent=self.text, initialvalue=name)
- if name:
- name = name.strip()
- if not name:
- return
- # XXX Ought to insert current file's directory in front of path
- try:
- spec = importlib.util.find_spec(name)
- except (ValueError, ImportError) as msg:
- tkMessageBox.showerror("Import error", str(msg), parent=self.text)
- return
- if spec is None:
- tkMessageBox.showerror("Import error", "module not found",
- parent=self.text)
- return
- if not isinstance(spec.loader, importlib.abc.SourceLoader):
- tkMessageBox.showerror("Import error", "not a source-based module",
- parent=self.text)
- return
- try:
- file_path = spec.loader.get_filename(name)
- except AttributeError:
- tkMessageBox.showerror("Import error",
- "loader does not support get_filename",
- parent=self.text)
- return
- if self.flist:
- self.flist.open(file_path)
- else:
- self.io.loadfile(file_path)
+ name = ''
+ file_path = query.ModuleName(
+ self.text, "Open Module",
+ "Enter the name of a Python module\n"
+ "to search on sys.path and open:",
+ name).result
+ if file_path is not None:
+ if self.flist:
+ self.flist.open(file_path)
+ else:
+ self.io.loadfile(file_path)
return file_path
def open_class_browser(self, event=None):
@@ -673,12 +610,12 @@ class EditorWindow(object):
return
head, tail = os.path.split(filename)
base, ext = os.path.splitext(tail)
- from idlelib import ClassBrowser
- ClassBrowser.ClassBrowser(self.flist, base, [head])
+ from idlelib import browser
+ browser.ClassBrowser(self.flist, base, [head])
def open_path_browser(self, event=None):
- from idlelib import PathBrowser
- PathBrowser.PathBrowser(self.flist)
+ from idlelib import pathbrowser
+ pathbrowser.PathBrowser(self.flist)
def open_turtle_demo(self, event = None):
import subprocess
@@ -739,7 +676,7 @@ class EditorWindow(object):
def ResetColorizer(self):
"Update the color theme"
- # Called from self.filename_change_hook and from configDialog.py
+ # Called from self.filename_change_hook and from configdialog.py
self._rmcolorizer()
self._addcolorizer()
EditorWindow.color_config(self.text)
@@ -759,14 +696,14 @@ class EditorWindow(object):
def ResetFont(self):
"Update the text widgets' font if it is changed"
- # Called from configDialog.py
+ # Called from configdialog.py
self.text['font'] = idleConf.GetFont(self.root, 'main','EditorWindow')
def RemoveKeybindings(self):
"Remove the keybindings before they are changed."
- # Called from configDialog.py
- self.Bindings.default_keydefs = keydefs = idleConf.GetCurrentKeySet()
+ # Called from configdialog.py
+ self.mainmenu.default_keydefs = keydefs = idleConf.GetCurrentKeySet()
for event, keylist in keydefs.items():
self.text.event_delete(event, *keylist)
for extensionName in self.get_standard_extension_names():
@@ -777,8 +714,8 @@ class EditorWindow(object):
def ApplyKeybindings(self):
"Update the keybindings after they are changed"
- # Called from configDialog.py
- self.Bindings.default_keydefs = keydefs = idleConf.GetCurrentKeySet()
+ # Called from configdialog.py
+ self.mainmenu.default_keydefs = keydefs = idleConf.GetCurrentKeySet()
self.apply_bindings()
for extensionName in self.get_standard_extension_names():
xkeydefs = idleConf.GetExtensionBindings(extensionName)
@@ -786,7 +723,7 @@ class EditorWindow(object):
self.apply_bindings(xkeydefs)
#update menu accelerators
menuEventDict = {}
- for menu in self.Bindings.menudefs:
+ for menu in self.mainmenu.menudefs:
menuEventDict[menu[0]] = {}
for item in menu[1]:
if item:
@@ -813,7 +750,7 @@ class EditorWindow(object):
def set_notabs_indentwidth(self):
"Update the indentwidth if changed and not using tabs in this window"
- # Called from configDialog.py
+ # Called from configdialog.py
if not self.usetabs:
self.indentwidth = idleConf.GetOption('main', 'Indent','num-spaces',
type='int')
@@ -993,7 +930,7 @@ class EditorWindow(object):
def _close(self):
if self.io.filename:
self.update_recent_files_list(new_file=self.io.filename)
- WindowList.unregister_callback(self.postwindowsmenu)
+ windows.unregister_callback(self.postwindowsmenu)
self.unload_extensions()
self.io.close()
self.io = None
@@ -1031,12 +968,25 @@ class EditorWindow(object):
def get_standard_extension_names(self):
return idleConf.GetExtensions(editor_only=True)
+ extfiles = { # map config-extension section names to new file names
+ 'AutoComplete': 'autocomplete',
+ 'AutoExpand': 'autoexpand',
+ 'CallTips': 'calltips',
+ 'CodeContext': 'codecontext',
+ 'FormatParagraph': 'paragraph',
+ 'ParenMatch': 'parenmatch',
+ 'RstripExtension': 'rstrip',
+ 'ScriptBinding': 'runscript',
+ 'ZoomHeight': 'zoomheight',
+ }
+
def load_extension(self, name):
+ fname = self.extfiles.get(name, name)
try:
try:
- mod = importlib.import_module('.' + name, package=__package__)
+ mod = importlib.import_module('.' + fname, package=__package__)
except (ImportError, TypeError):
- mod = importlib.import_module(name)
+ mod = importlib.import_module(fname)
except ImportError:
print("\nFailed to import extension: ", name)
raise
@@ -1060,7 +1010,7 @@ class EditorWindow(object):
def apply_bindings(self, keydefs=None):
if keydefs is None:
- keydefs = self.Bindings.default_keydefs
+ keydefs = self.mainmenu.default_keydefs
text = self.text
text.keydefs = keydefs
for event, keylist in keydefs.items():
@@ -1073,9 +1023,9 @@ class EditorWindow(object):
Menus that are absent or None in self.menudict are ignored.
"""
if menudefs is None:
- menudefs = self.Bindings.menudefs
+ menudefs = self.mainmenu.menudefs
if keydefs is None:
- keydefs = self.Bindings.default_keydefs
+ keydefs = self.mainmenu.default_keydefs
menudict = self.menudict
text = self.text
for mname, entrylist in menudefs:
@@ -1302,7 +1252,7 @@ class EditorWindow(object):
# adjust indentation for continuations and block
# open/close first need to find the last stmt
lno = index2line(text.index('insert'))
- y = PyParse.Parser(self.indentwidth, self.tabwidth)
+ y = pyparse.Parser(self.indentwidth, self.tabwidth)
if not self.context_use_ps1:
for context in self.num_context_lines:
startat = max(lno - context, 1)
@@ -1326,22 +1276,22 @@ class EditorWindow(object):
y.set_lo(0)
c = y.get_continuation_type()
- if c != PyParse.C_NONE:
+ if c != pyparse.C_NONE:
# The current stmt hasn't ended yet.
- if c == PyParse.C_STRING_FIRST_LINE:
+ if c == pyparse.C_STRING_FIRST_LINE:
# after the first line of a string; do not indent at all
pass
- elif c == PyParse.C_STRING_NEXT_LINES:
+ elif c == pyparse.C_STRING_NEXT_LINES:
# inside a string which started before this line;
# just mimic the current indent
text.insert("insert", indent)
- elif c == PyParse.C_BRACKET:
+ elif c == pyparse.C_BRACKET:
# line up with the first (if any) element of the
# last open bracket structure; else indent one
# level beyond the indent of the line with the
# last open bracket
self.reindent_to(y.compute_bracket_indent())
- elif c == PyParse.C_BACKSLASH:
+ elif c == pyparse.C_BACKSLASH:
# if more than one line in this stmt already, just
# mimic the current indent; else if initial line
# has a start on an assignment stmt, indent to
@@ -1569,9 +1519,6 @@ def classifyws(s, tabwidth):
break
return raw, effective
-import tokenize
-_tokenize = tokenize
-del tokenize
class IndentSearcher(object):
@@ -1596,8 +1543,8 @@ class IndentSearcher(object):
return self.text.get(mark, mark + " lineend+1c")
def tokeneater(self, type, token, start, end, line,
- INDENT=_tokenize.INDENT,
- NAME=_tokenize.NAME,
+ INDENT=tokenize.INDENT,
+ NAME=tokenize.NAME,
OPENERS=('class', 'def', 'for', 'if', 'try', 'while')):
if self.finished:
pass
@@ -1608,19 +1555,19 @@ class IndentSearcher(object):
self.finished = 1
def run(self):
- save_tabsize = _tokenize.tabsize
- _tokenize.tabsize = self.tabwidth
+ save_tabsize = tokenize.tabsize
+ tokenize.tabsize = self.tabwidth
try:
try:
- tokens = _tokenize.generate_tokens(self.readline)
+ tokens = tokenize.generate_tokens(self.readline)
for token in tokens:
self.tokeneater(*token)
- except (_tokenize.TokenError, SyntaxError):
+ except (tokenize.TokenError, SyntaxError):
# since we cut off the tokenizer early, we can trigger
# spurious errors
pass
finally:
- _tokenize.tabsize = save_tabsize
+ tokenize.tabsize = save_tabsize
return self.blkopenline, self.indentedline
### end autoindent code ###
@@ -1644,7 +1591,7 @@ def get_accelerator(keydefs, eventname):
keylist = keydefs.get(eventname)
# issue10940: temporary workaround to prevent hang with OS X Cocoa Tk 8.5
# if not keylist:
- if (not keylist) or (macosxSupport.isCocoaTk() and eventname in {
+ if (not keylist) or (macosx.isCocoaTk() and eventname in {
"<<open-module>>",
"<<goto-line>>",
"<<change-indentwidth>>"}):
@@ -1679,12 +1626,15 @@ def _editor_window(parent): # htest #
filename = sys.argv[1]
else:
filename = None
- macosxSupport.setupApp(root, None)
+ macosx.setupApp(root, None)
edit = EditorWindow(root=root, filename=filename)
edit.text.bind("<<close-all-windows>>", edit.close_event)
# Does not stop error, neither does following
# edit.text.bind("<<close-window>>", edit.close_event)
if __name__ == '__main__':
+ import unittest
+ unittest.main('idlelib.idle_test.test_editor', verbosity=2, exit=False)
+
from idlelib.idle_test.htest import run
run(_editor_window)
diff --git a/Lib/idlelib/FileList.py b/Lib/idlelib/filelist.py
index a9989a8..f46ad7c 100644
--- a/Lib/idlelib/FileList.py
+++ b/Lib/idlelib/filelist.py
@@ -1,4 +1,5 @@
import os
+
from tkinter import *
import tkinter.messagebox as tkMessageBox
@@ -6,7 +7,7 @@ import tkinter.messagebox as tkMessageBox
class FileList:
# N.B. this import overridden in PyShellFileList.
- from idlelib.EditorWindow import EditorWindow
+ from idlelib.editor import EditorWindow
def __init__(self, root):
self.root = root
@@ -111,7 +112,7 @@ class FileList:
def _test():
- from idlelib.EditorWindow import fixwordbreaks
+ from idlelib.editor import fixwordbreaks
import sys
root = Tk()
fixwordbreaks(root)
diff --git a/Lib/idlelib/GrepDialog.py b/Lib/idlelib/grep.py
index 721b231..64ba28d 100644
--- a/Lib/idlelib/GrepDialog.py
+++ b/Lib/idlelib/grep.py
@@ -1,17 +1,19 @@
-import os
import fnmatch
-import re # for htest
+import os
import sys
-from tkinter import StringVar, BooleanVar, Checkbutton # for GrepDialog
-from tkinter import Tk, Text, Button, SEL, END # for htest
-from idlelib import SearchEngine
-from idlelib.SearchDialogBase import SearchDialogBase
-# Importing OutputWindow fails due to import loop
+
+from tkinter import StringVar, BooleanVar
+from tkinter.ttk import Checkbutton
+
+from idlelib.searchbase import SearchDialogBase
+from idlelib import searchengine
+
+# Importing OutputWindow here fails due to import loop
# EditorWindow -> GrepDialop -> OutputWindow -> EditorWindow
def grep(text, io=None, flist=None):
root = text._root()
- engine = SearchEngine.get(root)
+ engine = searchengine.get(root)
if not hasattr(engine, "_grepdialog"):
engine._grepdialog = GrepDialog(root, engine, flist)
dialog = engine._grepdialog
@@ -47,13 +49,10 @@ class GrepDialog(SearchDialogBase):
self.globent = self.make_entry("In files:", self.globvar)[0]
def create_other_buttons(self):
- f = self.make_frame()[0]
-
- btn = Checkbutton(f, anchor="w",
- variable=self.recvar,
+ btn = Checkbutton(
+ self.make_frame()[0], variable=self.recvar,
text="Recurse down subdirectories")
btn.pack(side="top", fill="both")
- btn.select()
def create_command_buttons(self):
SearchDialogBase.create_command_buttons(self)
@@ -67,7 +66,7 @@ class GrepDialog(SearchDialogBase):
if not path:
self.top.bell()
return
- from idlelib.OutputWindow import OutputWindow # leave here!
+ from idlelib.outwin import OutputWindow # leave here!
save = sys.stdout
try:
sys.stdout = OutputWindow(self.flist)
@@ -131,14 +130,16 @@ class GrepDialog(SearchDialogBase):
def _grep_dialog(parent): # htest #
- from idlelib.PyShell import PyShellFileList
- root = Tk()
- root.title("Test GrepDialog")
- width, height, x, y = list(map(int, re.split('[x+]', parent.geometry())))
- root.geometry("+%d+%d"%(x, y + 150))
-
- flist = PyShellFileList(root)
- text = Text(root, height=5)
+ from tkinter import Toplevel, Text, SEL, END
+ from tkinter.ttk import Button
+ from idlelib.pyshell import PyShellFileList
+ top = Toplevel(parent)
+ top.title("Test GrepDialog")
+ x, y = map(int, parent.geometry().split('+')[1:])
+ top.geometry("+%d+%d" % (x, y + 175))
+
+ flist = PyShellFileList(top)
+ text = Text(top, height=5)
text.pack()
def show_grep_dialog():
@@ -146,9 +147,8 @@ def _grep_dialog(parent): # htest #
grep(text, flist=flist)
text.tag_remove(SEL, "1.0", END)
- button = Button(root, text="Show GrepDialog", command=show_grep_dialog)
+ button = Button(top, text="Show GrepDialog", command=show_grep_dialog)
button.pack()
- root.mainloop()
if __name__ == "__main__":
import unittest
diff --git a/Lib/idlelib/help.py b/Lib/idlelib/help.py
index a7008e9..77e01a3 100644
--- a/Lib/idlelib/help.py
+++ b/Lib/idlelib/help.py
@@ -4,7 +4,7 @@ Contents are subject to revision at any time, without notice.
Help => About IDLE: diplay About Idle dialog
-<to be moved here from aboutDialog.py>
+<to be moved here from help_about.py>
Help => IDLE Help: Display help.html with proper formatting.
@@ -25,15 +25,14 @@ copy_strip - Copy idle.html to help.html, rstripping each line.
show_idlehelp - Create HelpWindow. Called in EditorWindow.help_dialog.
"""
from html.parser import HTMLParser
-from os.path import abspath, dirname, isdir, isfile, join
+from os.path import abspath, dirname, isfile, join
from platform import python_version
-from tkinter import Tk, Toplevel, Frame, Text, Scrollbar, Menu, Menubutton
+
+from tkinter import Toplevel, Frame, Text, Menu
+from tkinter.ttk import Menubutton, Scrollbar
from tkinter import font as tkfont
-from idlelib.configHandler import idleConf
-use_ttk = False # until available to import
-if use_ttk:
- from tkinter.ttk import Menubutton
+from idlelib.config import idleConf
## About IDLE ##
@@ -197,15 +196,18 @@ class HelpFrame(Frame):
"Display html text, scrollbar, and toc."
def __init__(self, parent, filename):
Frame.__init__(self, parent)
- text = HelpText(self, filename)
+ # keep references to widgets for test access.
+ self.text = text = HelpText(self, filename)
self['background'] = text['background']
- scroll = Scrollbar(self, command=text.yview)
+ self.toc = toc = self.toc_menu(text)
+ self.scroll = scroll = Scrollbar(self, command=text.yview)
text['yscrollcommand'] = scroll.set
+
self.rowconfigure(0, weight=1)
self.columnconfigure(1, weight=1) # text
- self.toc_menu(text).grid(column=0, row=0, sticky='nw')
- text.grid(column=1, row=0, sticky='nsew')
- scroll.grid(column=2, row=0, sticky='ns')
+ toc.grid(row=0, column=0, sticky='nw')
+ text.grid(row=0, column=1, sticky='nsew')
+ scroll.grid(row=0, column=2, sticky='ns')
def toc_menu(self, text):
"Create table of contents as drop-down menu."
diff --git a/Lib/idlelib/help.txt b/Lib/idlelib/help.txt
deleted file mode 100644
index 89fbe0b..0000000
--- a/Lib/idlelib/help.txt
+++ /dev/null
@@ -1,372 +0,0 @@
-This file, idlelib/help.txt is out-of-date and no longer used by Idle.
-It is deprecated and will be removed in the future, possibly in 3.6
-----------------------------------------------------------------------
-
-[See the end of this file for ** TIPS ** on using IDLE !!]
-
-IDLE is the Python IDE built with the tkinter GUI toolkit.
-
-IDLE has the following features:
--coded in 100% pure Python, using the tkinter GUI toolkit
--cross-platform: works on Windows, Unix, and OS X
--multi-window text editor with multiple undo, Python colorizing, smart indent,
-call tips, and many other features
--Python shell window (a.k.a interactive interpreter)
--debugger (not complete, but you can set breakpoints, view and step)
-
-Menus:
-
-IDLE has two window types the Shell window and the Editor window. It is
-possible to have multiple editor windows simultaneously. IDLE's
-menus dynamically change based on which window is currently selected. Each menu
-documented below indicates which window type it is associated with.
-
-File Menu (Shell and Editor):
-
- New File -- Create a new file editing window
- Open... -- Open an existing file
- Open Module... -- Open an existing module (searches sys.path)
- Recent Files... -- Open a list of recent files
- Class Browser -- Show classes and methods in current file
- Path Browser -- Show sys.path directories, modules, classes,
- and methods
- ---
- Save -- Save current window to the associated file (unsaved
- windows have a * before and after the window title)
-
- Save As... -- Save current window to new file, which becomes
- the associated file
- Save Copy As... -- Save current window to different file
- without changing the associated file
- ---
- Print Window -- Print the current window
- ---
- Close -- Close current window (asks to save if unsaved)
- Exit -- Close all windows, quit (asks to save if unsaved)
-
-Edit Menu (Shell and Editor):
-
- Undo -- Undo last change to current window
- (a maximum of 1000 changes may be undone)
- Redo -- Redo last undone change to current window
- ---
- Cut -- Copy a selection into system-wide clipboard,
- then delete the selection
- Copy -- Copy selection into system-wide clipboard
- Paste -- Insert system-wide clipboard into window
- Select All -- Select the entire contents of the edit buffer
- ---
- Find... -- Open a search dialog box with many options
- Find Again -- Repeat last search
- Find Selection -- Search for the string in the selection
- Find in Files... -- Open a search dialog box for searching files
- Replace... -- Open a search-and-replace dialog box
- Go to Line -- Ask for a line number and show that line
- Expand Word -- Expand the word you have typed to match another
- word in the same buffer; repeat to get a
- different expansion
- Show Calltip -- After an unclosed parenthesis for a function, open
- a small window with function parameter hints
- Show Parens -- Highlight the surrounding parenthesis
- Show Completions -- Open a scroll window allowing selection keywords
- and attributes. (see '*TIPS*', below)
-
-Format Menu (Editor window only):
-
- Indent Region -- Shift selected lines right by the indent width
- (default 4 spaces)
- Dedent Region -- Shift selected lines left by the indent width
- (default 4 spaces)
- Comment Out Region -- Insert ## in front of selected lines
- Uncomment Region -- Remove leading # or ## from selected lines
- Tabify Region -- Turns *leading* stretches of spaces into tabs.
- (Note: We recommend using 4 space blocks to indent Python code.)
- Untabify Region -- Turn *all* tabs into the corrent number of spaces
- Toggle tabs -- Open a dialog to switch between indenting with
- spaces and tabs.
- New Indent Width... -- Open a dialog to change indent width. The
- accepted default by the Python community is 4
- spaces.
- Format Paragraph -- Reformat the current blank-line-separated
- paragraph. All lines in the paragraph will be
- formatted to less than 80 columns.
- ---
- Strip trailing whitespace -- Removed any space characters after the end
- of the last non-space character
-
-Run Menu (Editor window only):
-
- Python Shell -- Open or wake up the Python shell window
- ---
- Check Module -- Check the syntax of the module currently open in the
- Editor window. If the module has not been saved IDLE
- will prompt the user to save the code.
- Run Module -- Restart the shell to clean the environment, then
- execute the currently open module. If the module has
- not been saved IDLE will prompt the user to save the
- code.
-
-Shell Menu (Shell window only):
-
- View Last Restart -- Scroll the shell window to the last Shell restart
- Restart Shell -- Restart the shell to clean the environment
-
-Debug Menu (Shell window only):
-
- Go to File/Line -- Look around the insert point for a filename
- and line number, open the file, and show the line.
- Useful to view the source lines referenced in an
- exception traceback. Available in the context
- menu of the Shell window.
- Debugger (toggle) -- This feature is not complete and considered
- experimental. Run commands in the shell under the
- debugger.
- Stack Viewer -- Show the stack traceback of the last exception
- Auto-open Stack Viewer (toggle) -- Toggle automatically opening the
- stack viewer on unhandled
- exception
-
-Options Menu (Shell and Editor):
-
- Configure IDLE -- Open a configuration dialog. Fonts, indentation,
- keybindings, and color themes may be altered.
- Startup Preferences may be set, and additional Help
- sources can be specified. On OS X, open the
- configuration dialog by selecting Preferences
- in the application menu.
-
- ---
- Code Context (toggle) -- Open a pane at the top of the edit window
- which shows the block context of the section
- of code which is scrolling off the top or the
- window. This is not present in the Shell
- window only the Editor window.
-
-Window Menu (Shell and Editor):
-
- Zoom Height -- Toggles the window between normal size (40x80 initial
- setting) and maximum height. The initial size is in the Configure
- IDLE dialog under the general tab.
- ---
- The rest of this menu lists the names of all open windows;
- select one to bring it to the foreground (deiconifying it if
- necessary).
-
-Help Menu:
-
- About IDLE -- Version, copyright, license, credits
- ---
- IDLE Help -- Display this file which is a help file for IDLE
- detailing the menu options, basic editing and navigation,
- and other tips.
- Python Docs -- Access local Python documentation, if
- installed. Or will start a web browser and open
- docs.python.org showing the latest Python documentation.
- ---
- Additional help sources may be added here with the Configure IDLE
- dialog under the General tab.
-
-Editor context menu (Right-click / Control-click on OS X in Edit window):
-
- Cut -- Copy a selection into system-wide clipboard,
- then delete the selection
- Copy -- Copy selection into system-wide clipboard
- Paste -- Insert system-wide clipboard into window
- Set Breakpoint -- Sets a breakpoint. Breakpoints are only enabled
- when the debugger is open.
- Clear Breakpoint -- Clears the breakpoint on that line
-
-Shell context menu (Right-click / Control-click on OS X in Shell window):
-
- Cut -- Copy a selection into system-wide clipboard,
- then delete the selection
- Copy -- Copy selection into system-wide clipboard
- Paste -- Insert system-wide clipboard into window
- ---
- Go to file/line -- Same as in Debug menu
-
-
-** TIPS **
-==========
-
-Additional Help Sources:
-
- Windows users can Google on zopeshelf.chm to access Zope help files in
- the Windows help format. The Additional Help Sources feature of the
- configuration GUI supports .chm, along with any other filetypes
- supported by your browser. Supply a Menu Item title, and enter the
- location in the Help File Path slot of the New Help Source dialog. Use
- http:// and/or www. to identify external URLs, or download the file and
- browse for its path on your machine using the Browse button.
-
- All users can access the extensive sources of help, including
- tutorials, available at docs.python.org. Selected URLs can be added
- or removed from the Help menu at any time using Configure IDLE.
-
-Basic editing and navigation:
-
- Backspace deletes char to the left; DEL deletes char to the right.
- Control-backspace deletes word left, Control-DEL deletes word right.
- Arrow keys and Page Up/Down move around.
- Control-left/right Arrow moves by words in a strange but useful way.
- Home/End go to begin/end of line.
- Control-Home/End go to begin/end of file.
- Some useful Emacs bindings are inherited from Tcl/Tk:
- Control-a beginning of line
- Control-e end of line
- Control-k kill line (but doesn't put it in clipboard)
- Control-l center window around the insertion point
- Standard keybindings (like Control-c to copy and Control-v to
- paste) may work. Keybindings are selected in the Configure IDLE
- dialog.
-
-Automatic indentation:
-
- After a block-opening statement, the next line is indented by 4 spaces
- (in the Python Shell window by one tab). After certain keywords
- (break, return etc.) the next line is dedented. In leading
- indentation, Backspace deletes up to 4 spaces if they are there. Tab
- inserts spaces (in the Python Shell window one tab), number depends on
- Indent Width. Currently tabs are restricted to four spaces due
- to Tcl/Tk limitations.
-
- See also the indent/dedent region commands in the edit menu.
-
-Completions:
-
- Completions are supplied for functions, classes, and attributes of
- classes, both built-in and user-defined. Completions are also provided
- for filenames.
-
- The AutoCompleteWindow (ACW) will open after a predefined delay
- (default is two seconds) after a '.' or (in a string) an os.sep is
- typed. If after one of those characters (plus zero or more other
- characters) a tab is typed the ACW will open immediately if a possible
- continuation is found.
-
- If there is only one possible completion for the characters entered, a
- tab will supply that completion without opening the ACW.
-
- 'Show Completions' will force open a completions window, by default the
- Control-space keys will open a completions window. In an empty
- string, this will contain the files in the current directory. On a
- blank line, it will contain the built-in and user-defined functions and
- classes in the current name spaces, plus any modules imported. If some
- characters have been entered, the ACW will attempt to be more specific.
-
- If string of characters is typed, the ACW selection will jump to the
- entry most closely matching those characters. Entering a tab will cause
- the longest non-ambiguous match to be entered in the Edit window or
- Shell. Two tabs in a row will supply the current ACW selection, as
- will return or a double click. Cursor keys, Page Up/Down, mouse
- selection, and the scroll wheel all operate on the ACW.
-
- "Hidden" attributes can be accessed by typing the beginning of hidden
- name after a '.', e.g. '_'. This allows access to modules with
- '__all__' set, or to class-private attributes.
-
- Completions and the 'Expand Word' facility can save a lot of typing!
-
- Completions are currently limited to those in the namespaces. Names in
- an Editor window which are not via __main__ or sys.modules will not be
- found. Run the module once with your imports to correct this
- situation. Note that IDLE itself places quite a few modules in
- sys.modules, so much can be found by default, e.g. the re module.
-
- If you don't like the ACW popping up unbidden, simply make the delay
- longer or disable the extension. Or another option is the delay could
- be set to zero. Another alternative to preventing ACW popups is to
- disable the call tips extension.
-
-Python Shell window:
-
- Control-c interrupts executing command.
- Control-d sends end-of-file; closes window if typed at >>> prompt.
- Alt-/ expand word is also useful to reduce typing.
-
- Command history:
-
- Alt-p retrieves previous command matching what you have typed. On OS X
- use Control-p.
- Alt-n retrieves next. On OS X use Control-n.
- Return while cursor is on a previous command retrieves that command.
-
- Syntax colors:
-
- The coloring is applied in a background "thread", so you may
- occasionally see uncolorized text. To change the color
- scheme, use the Configure IDLE / Highlighting dialog.
-
- Python default syntax colors:
-
- Keywords orange
- Builtins royal purple
- Strings green
- Comments red
- Definitions blue
-
- Shell default colors:
-
- Console output brown
- stdout blue
- stderr red
- stdin black
-
-Other preferences:
-
- The font preferences, highlighting, keys, and general preferences can
- be changed via the Configure IDLE menu option. Be sure to note that
- keys can be user defined, IDLE ships with four built in key sets. In
- addition a user can create a custom key set in the Configure IDLE
- dialog under the keys tab.
-
-Command line usage:
-
- Enter idle -h at the command prompt to get a usage message.
-
- idle.py [-c command] [-d] [-e] [-s] [-t title] [arg] ...
-
- -c command run this command
- -d enable debugger
- -e edit mode; arguments are files to be edited
- -s run $IDLESTARTUP or $PYTHONSTARTUP first
- -t title set title of shell window
-
- If there are arguments:
- 1. If -e is used, arguments are files opened for editing and sys.argv
- reflects the arguments passed to IDLE itself.
- 2. Otherwise, if -c is used, all arguments are placed in
- sys.argv[1:...], with sys.argv[0] set to -c.
- 3. Otherwise, if neither -e nor -c is used, the first argument is a
- script which is executed with the remaining arguments in
- sys.argv[1:...] and sys.argv[0] set to the script name. If the
- script name is -, no script is executed but an interactive Python
- session is started; the arguments are still available in sys.argv.
-
-Running without a subprocess: (DEPRECATED in Python 3.4 see Issue 16123)
-
- If IDLE is started with the -n command line switch it will run in a
- single process and will not create the subprocess which runs the RPC
- Python execution server. This can be useful if Python cannot create
- the subprocess or the RPC socket interface on your platform. However,
- in this mode user code is not isolated from IDLE itself. Also, the
- environment is not restarted when Run/Run Module (F5) is selected. If
- your code has been modified, you must reload() the affected modules and
- re-import any specific items (e.g. from foo import baz) if the changes
- are to take effect. For these reasons, it is preferable to run IDLE
- with the default subprocess if at all possible.
-
-Extensions:
-
- IDLE contains an extension facility. See the beginning of
- config-extensions.def in the idlelib directory for further information.
- The default extensions are currently:
-
- FormatParagraph
- AutoExpand
- ZoomHeight
- ScriptBinding
- CallTips
- ParenMatch
- AutoComplete
- CodeContext
diff --git a/Lib/idlelib/aboutDialog.py b/Lib/idlelib/help_about.py
index a8f75d2..071bd3e 100644
--- a/Lib/idlelib/aboutDialog.py
+++ b/Lib/idlelib/help_about.py
@@ -1,11 +1,13 @@
"""About Dialog for IDLE
"""
-
import os
from sys import version
+
from tkinter import *
-from idlelib import textView
+
+from idlelib import textview
+
class AboutDialog(Toplevel):
"""Modal about dialog for idle
@@ -135,17 +137,18 @@ class AboutDialog(Toplevel):
def display_printer_text(self, title, printer):
printer._Printer__setup()
text = '\n'.join(printer._Printer__lines)
- textView.view_text(self, title, text)
+ textview.view_text(self, title, text)
def display_file_text(self, title, filename, encoding=None):
fn = os.path.join(os.path.abspath(os.path.dirname(__file__)), filename)
- textView.view_file(self, title, fn, encoding)
+ textview.view_file(self, title, fn, encoding)
def Ok(self, event=None):
self.destroy()
+
if __name__ == '__main__':
import unittest
- unittest.main('idlelib.idle_test.test_helpabout', verbosity=2, exit=False)
+ unittest.main('idlelib.idle_test.test_help_about', verbosity=2, exit=False)
from idlelib.idle_test.htest import run
run(AboutDialog)
diff --git a/Lib/idlelib/IdleHistory.py b/Lib/idlelib/history.py
index 078af29..56f53a0 100644
--- a/Lib/idlelib/IdleHistory.py
+++ b/Lib/idlelib/history.py
@@ -1,11 +1,12 @@
"Implement Idle Shell history mechanism with History class"
-from idlelib.configHandler import idleConf
+from idlelib.config import idleConf
+
class History:
''' Implement Idle Shell history mechanism.
- store - Store source statement (called from PyShell.resetoutput).
+ store - Store source statement (called from pyshell.resetoutput).
fetch - Fetch stored statement matching prefix already entered.
history_next - Bound to <<history-next>> event (default Alt-N).
history_prev - Bound to <<history-prev>> event (default Alt-P).
@@ -99,6 +100,7 @@ class History:
self.pointer = None
self.prefix = None
+
if __name__ == "__main__":
from unittest import main
- main('idlelib.idle_test.test_idlehistory', verbosity=2, exit=False)
+ main('idlelib.idle_test.test_history', verbosity=2, exit=False)
diff --git a/Lib/idlelib/HyperParser.py b/Lib/idlelib/hyperparser.py
index 77cb057..450a709 100644
--- a/Lib/idlelib/HyperParser.py
+++ b/Lib/idlelib/hyperparser.py
@@ -4,11 +4,10 @@ HyperParser uses PyParser. PyParser mostly gives information on the
proper indentation of code. HyperParser gives additional information on
the structure of code.
"""
-
-import string
from keyword import iskeyword
-from idlelib import PyParse
+import string
+from idlelib import pyparse
# all ASCII chars that may be in an identifier
_ASCII_ID_CHARS = frozenset(string.ascii_letters + string.digits + "_")
@@ -30,7 +29,7 @@ class HyperParser:
self.editwin = editwin
self.text = text = editwin.text
- parser = PyParse.Parser(editwin.indentwidth, editwin.tabwidth)
+ parser = pyparse.Parser(editwin.indentwidth, editwin.tabwidth)
def index2line(index):
return int(float(index))
diff --git a/Lib/idlelib/idle.py b/Lib/idlelib/idle.py
index 141534d..485d5a7 100644
--- a/Lib/idlelib/idle.py
+++ b/Lib/idlelib/idle.py
@@ -1,6 +1,7 @@
import os.path
import sys
+
# Enable running IDLE with idlelib in a non-standard location.
# This was once used to run development versions of IDLE.
# Because PEP 434 declared idle.py a public interface,
@@ -9,5 +10,5 @@ idlelib_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if idlelib_dir not in sys.path:
sys.path.insert(0, idlelib_dir)
-from idlelib.PyShell import main # This is subject to change
+from idlelib.pyshell import main # This is subject to change
main()
diff --git a/Lib/idlelib/idle.pyw b/Lib/idlelib/idle.pyw
index 142cb32..e73c049 100644
--- a/Lib/idlelib/idle.pyw
+++ b/Lib/idlelib/idle.pyw
@@ -1,10 +1,10 @@
try:
- import idlelib.PyShell
+ import idlelib.pyshell
except ImportError:
- # IDLE is not installed, but maybe PyShell is on sys.path:
- from . import PyShell
+ # IDLE is not installed, but maybe pyshell is on sys.path:
+ from . import pyshell
import os
- idledir = os.path.dirname(os.path.abspath(PyShell.__file__))
+ idledir = os.path.dirname(os.path.abspath(pyshell.__file__))
if idledir != os.getcwd():
# We're not in the IDLE directory, help the subprocess find run.py
pypath = os.environ.get('PYTHONPATH', '')
@@ -12,6 +12,6 @@ except ImportError:
os.environ['PYTHONPATH'] = pypath + ':' + idledir
else:
os.environ['PYTHONPATH'] = idledir
- PyShell.main()
+ pyshell.main()
else:
- idlelib.PyShell.main()
+ idlelib.pyshell.main()
diff --git a/Lib/idlelib/idle_test/__init__.py b/Lib/idlelib/idle_test/__init__.py
index 845c92d..ad067b4 100644
--- a/Lib/idlelib/idle_test/__init__.py
+++ b/Lib/idlelib/idle_test/__init__.py
@@ -1,6 +1,8 @@
'''idlelib.idle_test is a private implementation of test.test_idle,
which tests the IDLE application as part of the stdlib test suite.
Run IDLE tests alone with "python -m test.test_idle".
+Starting with Python 3.6, IDLE requires tcl/tk 8.5 or later.
+
This package and its contained modules are subject to change and
any direct use is at your own risk.
'''
diff --git a/Lib/idlelib/idle_test/htest.py b/Lib/idlelib/idle_test/htest.py
index 58e62cb..6f676ae 100644
--- a/Lib/idlelib/idle_test/htest.py
+++ b/Lib/idlelib/idle_test/htest.py
@@ -59,19 +59,20 @@ msg: master window hints about testing the widget.
Modules and classes not being tested at the moment:
-PyShell.PyShellEditorWindow
-Debugger.Debugger
-AutoCompleteWindow.AutoCompleteWindow
-OutputWindow.OutputWindow (indirectly being tested with grep test)
+pyshell.PyShellEditorWindow
+debugger.Debugger
+autocomplete_w.AutoCompleteWindow
+outwin.OutputWindow (indirectly being tested with grep test)
'''
from importlib import import_module
-from idlelib.macosxSupport import _initializeTkVariantTests
import tkinter as tk
+from tkinter.ttk import Scrollbar
+tk.NoDefaultRoot()
AboutDialog_spec = {
- 'file': 'aboutDialog',
- 'kwds': {'title': 'aboutDialog test',
+ 'file': 'help_about',
+ 'kwds': {'title': 'help_about test',
'_htest': True,
},
'msg': "Test every button. Ensure Python, TK and IDLE versions "
@@ -79,14 +80,14 @@ AboutDialog_spec = {
}
_calltip_window_spec = {
- 'file': 'CallTipWindow',
+ 'file': 'calltip_w',
'kwds': {},
'msg': "Typing '(' should display a calltip.\n"
"Typing ') should hide the calltip.\n"
}
_class_browser_spec = {
- 'file': 'ClassBrowser',
+ 'file': 'browser',
'kwds': {},
'msg': "Inspect names of module, class(with superclass if "
"applicable), methods and functions.\nToggle nested items.\n"
@@ -95,7 +96,7 @@ _class_browser_spec = {
}
_color_delegator_spec = {
- 'file': 'ColorDelegator',
+ 'file': 'colorizer',
'kwds': {},
'msg': "The text is sample Python code.\n"
"Ensure components like comments, keywords, builtins,\n"
@@ -104,7 +105,7 @@ _color_delegator_spec = {
}
ConfigDialog_spec = {
- 'file': 'configDialog',
+ 'file': 'configdialog',
'kwds': {'title': 'ConfigDialogTest',
'_htest': True,},
'msg': "IDLE preferences dialog.\n"
@@ -121,7 +122,7 @@ ConfigDialog_spec = {
# TODO Improve message
_dyn_option_menu_spec = {
- 'file': 'dynOptionMenuWidget',
+ 'file': 'dynoption',
'kwds': {},
'msg': "Select one of the many options in the 'old option set'.\n"
"Click the button to change the option set.\n"
@@ -130,39 +131,15 @@ _dyn_option_menu_spec = {
# TODO edit wrapper
_editor_window_spec = {
- 'file': 'EditorWindow',
+ 'file': 'editor',
'kwds': {},
'msg': "Test editor functions of interest.\n"
"Best to close editor first."
}
-GetCfgSectionNameDialog_spec = {
- 'file': 'configSectionNameDialog',
- 'kwds': {'title':'Get Name',
- 'message':'Enter something',
- 'used_names': {'abc'},
- '_htest': True},
- 'msg': "After the text entered with [Ok] is stripped, <nothing>, "
- "'abc', or more that 30 chars are errors.\n"
- "Close 'Get Name' with a valid entry (printed to Shell), "
- "[Cancel], or [X]",
- }
-
-GetHelpSourceDialog_spec = {
- 'file': 'configHelpSourceEdit',
- 'kwds': {'title': 'Get helpsource',
- '_htest': True},
- 'msg': "Enter menu item name and help file path\n "
- "<nothing> and more than 30 chars are invalid menu item names.\n"
- "<nothing>, file does not exist are invalid path items.\n"
- "Test for incomplete web address for help file path.\n"
- "A valid entry will be printed to shell with [0k].\n"
- "[Cancel] will print None to shell",
- }
-
# Update once issue21519 is resolved.
GetKeysDialog_spec = {
- 'file': 'keybindingDialog',
+ 'file': 'config_key',
'kwds': {'title': 'Test keybindings',
'action': 'find-again',
'currentKeySequences': [''] ,
@@ -177,7 +154,7 @@ GetKeysDialog_spec = {
}
_grep_dialog_spec = {
- 'file': 'GrepDialog',
+ 'file': 'grep',
'kwds': {},
'msg': "Click the 'Show GrepDialog' button.\n"
"Test the various 'Find-in-files' functions.\n"
@@ -186,8 +163,24 @@ _grep_dialog_spec = {
"should open that file \nin a new EditorWindow."
}
+HelpSource_spec = {
+ 'file': 'query',
+ 'kwds': {'title': 'Help name and source',
+ 'menuitem': 'test',
+ 'filepath': __file__,
+ 'used_names': {'abc'},
+ '_htest': True},
+ 'msg': "Enter menu item name and help file path\n"
+ "'', > than 30 chars, and 'abc' are invalid menu item names.\n"
+ "'' and file does not exist are invalid path items.\n"
+ "Any url ('www...', 'http...') is accepted.\n"
+ "Test Browse with and without path, as cannot unittest.\n"
+ "[Ok] or <Return> prints valid entry to shell\n"
+ "[Cancel] or <Escape> prints None to shell"
+ }
+
_io_binding_spec = {
- 'file': 'IOBinding',
+ 'file': 'iomenu',
'kwds': {},
'msg': "Test the following bindings.\n"
"<Control-o> to open file from dialog.\n"
@@ -200,7 +193,7 @@ _io_binding_spec = {
}
_multi_call_spec = {
- 'file': 'MultiCall',
+ 'file': 'multicall',
'kwds': {},
'msg': "The following actions should trigger a print to console or IDLE"
" Shell.\nEntering and leaving the text area, key entry, "
@@ -210,14 +203,14 @@ _multi_call_spec = {
}
_multistatus_bar_spec = {
- 'file': 'MultiStatusBar',
+ 'file': 'statusbar',
'kwds': {},
'msg': "Ensure presence of multi-status bar below text area.\n"
"Click 'Update Status' to change the multi-status text"
}
_object_browser_spec = {
- 'file': 'ObjectBrowser',
+ 'file': 'debugobj',
'kwds': {},
'msg': "Double click on items upto the lowest level.\n"
"Attributes of the objects and related information "
@@ -225,7 +218,7 @@ _object_browser_spec = {
}
_path_browser_spec = {
- 'file': 'PathBrowser',
+ 'file': 'pathbrowser',
'kwds': {},
'msg': "Test for correct display of all paths in sys.path.\n"
"Toggle nested items upto the lowest level.\n"
@@ -234,7 +227,7 @@ _path_browser_spec = {
}
_percolator_spec = {
- 'file': 'Percolator',
+ 'file': 'percolator',
'kwds': {},
'msg': "There are two tracers which can be toggled using a checkbox.\n"
"Toggling a tracer 'on' by checking it should print tracer"
@@ -244,8 +237,20 @@ _percolator_spec = {
"Test for actions like text entry, and removal."
}
+Query_spec = {
+ 'file': 'query',
+ 'kwds': {'title': 'Query',
+ 'message': 'Enter something',
+ 'text0': 'Go',
+ '_htest': True},
+ 'msg': "Enter with <Return> or [Ok]. Print valid entry to Shell\n"
+ "Blank line, after stripping, is ignored\n"
+ "Close dialog with valid entry, <Escape>, [Cancel], [X]"
+ }
+
+
_replace_dialog_spec = {
- 'file': 'ReplaceDialog',
+ 'file': 'replace',
'kwds': {},
'msg': "Click the 'Replace' button.\n"
"Test various replace options in the 'Replace dialog'.\n"
@@ -253,15 +258,22 @@ _replace_dialog_spec = {
}
_search_dialog_spec = {
- 'file': 'SearchDialog',
+ 'file': 'search',
'kwds': {},
'msg': "Click the 'Search' button.\n"
"Test various search options in the 'Search dialog'.\n"
"Click [Close] or [X] to close the 'Search Dialog'."
}
+_searchbase_spec = {
+ 'file': 'searchbase',
+ 'kwds': {},
+ 'msg': "Check the appearance of the base search dialog\n"
+ "Its only action is to close."
+ }
+
_scrolled_list_spec = {
- 'file': 'ScrolledList',
+ 'file': 'scrolledlist',
'kwds': {},
'msg': "You should see a scrollable list of items\n"
"Selecting (clicking) or double clicking an item "
@@ -277,7 +289,7 @@ show_idlehelp_spec = {
}
_stack_viewer_spec = {
- 'file': 'StackViewer',
+ 'file': 'stackviewer',
'kwds': {},
'msg': "A stacktrace for a NameError exception.\n"
"Expand 'idlelib ...' and '<locals>'.\n"
@@ -295,8 +307,8 @@ _tabbed_pages_spec = {
}
TextViewer_spec = {
- 'file': 'textView',
- 'kwds': {'title': 'Test textView',
+ 'file': 'textview',
+ 'kwds': {'title': 'Test textview',
'text':'The quick brown fox jumps over the lazy dog.\n'*35,
'_htest': True},
'msg': "Test for read-only property of text.\n"
@@ -304,21 +316,21 @@ TextViewer_spec = {
}
_tooltip_spec = {
- 'file': 'ToolTip',
+ 'file': 'tooltip',
'kwds': {},
'msg': "Place mouse cursor over both the buttons\n"
"A tooltip should appear with some text."
}
_tree_widget_spec = {
- 'file': 'TreeWidget',
+ 'file': 'tree',
'kwds': {},
'msg': "The canvas is scrollable.\n"
"Click on folders upto to the lowest level."
}
_undo_delegator_spec = {
- 'file': 'UndoDelegator',
+ 'file': 'undo',
'kwds': {},
'msg': "Click [Undo] to undo any action.\n"
"Click [Redo] to redo any action.\n"
@@ -327,7 +339,7 @@ _undo_delegator_spec = {
}
_widget_redirector_spec = {
- 'file': 'WidgetRedirector',
+ 'file': 'redirector',
'kwds': {},
'msg': "Every text insert should be printed to the console."
"or the IDLE shell."
@@ -337,14 +349,13 @@ def run(*tests):
root = tk.Tk()
root.title('IDLE htest')
root.resizable(0, 0)
- _initializeTkVariantTests(root)
# a scrollable Label like constant width text widget.
frameLabel = tk.Frame(root, padx=10)
frameLabel.pack()
text = tk.Text(frameLabel, wrap='word')
text.configure(bg=root.cget('bg'), relief='flat', height=4, width=70)
- scrollbar = tk.Scrollbar(frameLabel, command=text.yview)
+ scrollbar = Scrollbar(frameLabel, command=text.yview)
text.config(yscrollcommand=scrollbar.set)
scrollbar.pack(side='right', fill='y', expand=False)
text.pack(side='left', fill='both', expand=True)
@@ -365,11 +376,11 @@ def run(*tests):
test = getattr(mod, test_name)
test_list.append((test_spec, test))
- test_name = tk.StringVar('')
+ test_name = tk.StringVar(root)
callable_object = None
test_kwds = None
- def next():
+ def next_test():
nonlocal test_name, callable_object, test_kwds
if len(test_list) == 1:
@@ -384,20 +395,26 @@ def run(*tests):
text.insert("1.0",test_spec['msg'])
text.configure(state='disabled') # preserve read-only property
- def run_test():
+ def run_test(_=None):
widget = callable_object(**test_kwds)
try:
print(widget.result)
except AttributeError:
pass
- button = tk.Button(root, textvariable=test_name, command=run_test)
+ def close(_=None):
+ root.destroy()
+
+ button = tk.Button(root, textvariable=test_name,
+ default='active', command=run_test)
+ next_button = tk.Button(root, text="Next", command=next_test)
button.pack()
- next_button = tk.Button(root, text="Next", command=next)
next_button.pack()
+ next_button.focus_set()
+ root.bind('<Key-Return>', run_test)
+ root.bind('<Key-Escape>', close)
- next()
-
+ next_test()
root.mainloop()
if __name__ == '__main__':
diff --git a/Lib/idlelib/idle_test/mock_idle.py b/Lib/idlelib/idle_test/mock_idle.py
index 1672a34..c7b49ef 100644
--- a/Lib/idlelib/idle_test/mock_idle.py
+++ b/Lib/idlelib/idle_test/mock_idle.py
@@ -33,7 +33,7 @@ class Func:
class Editor:
- '''Minimally imitate EditorWindow.EditorWindow class.
+ '''Minimally imitate editor.EditorWindow class.
'''
def __init__(self, flist=None, filename=None, key=None, root=None):
self.text = Text()
@@ -46,7 +46,7 @@ class Editor:
class UndoDelegator:
- '''Minimally imitate UndoDelegator,UndoDelegator class.
+ '''Minimally imitate undo.UndoDelegator class.
'''
# A real undo block is only needed for user interaction.
def undo_block_start(*args):
diff --git a/Lib/idlelib/idle_test/test_autocomplete.py b/Lib/idlelib/idle_test/test_autocomplete.py
index 5fc899d..f3f2dea 100644
--- a/Lib/idlelib/idle_test/test_autocomplete.py
+++ b/Lib/idlelib/idle_test/test_autocomplete.py
@@ -1,9 +1,13 @@
+''' Test autocomplete and autocomple_w
+
+Coverage of autocomple: 56%
+'''
import unittest
from test.support import requires
from tkinter import Tk, Text
-import idlelib.AutoComplete as ac
-import idlelib.AutoCompleteWindow as acw
+import idlelib.autocomplete as ac
+import idlelib.autocomplete_w as acw
from idlelib.idle_test.mock_idle import Func
from idlelib.idle_test.mock_tk import Event
@@ -91,6 +95,11 @@ class AutoCompleteTest(unittest.TestCase):
self.assertIsNone(autocomplete.autocomplete_event(ev))
del ev.mc_state
+ # Test that tab after whitespace is ignored.
+ self.text.insert('1.0', ' """Docstring.\n ')
+ self.assertIsNone(autocomplete.autocomplete_event(ev))
+ self.text.delete('1.0', 'end')
+
# If autocomplete window is open, complete() method is called
self.text.insert('1.0', 're.')
# This must call autocomplete._make_autocomplete_window()
diff --git a/Lib/idlelib/idle_test/test_autoexpand.py b/Lib/idlelib/idle_test/test_autoexpand.py
index d2a3156..ae8186c 100644
--- a/Lib/idlelib/idle_test/test_autoexpand.py
+++ b/Lib/idlelib/idle_test/test_autoexpand.py
@@ -1,9 +1,9 @@
-"""Unit tests for idlelib.AutoExpand"""
+"""Unit tests for idlelib.autoexpand"""
import unittest
from test.support import requires
from tkinter import Text, Tk
#from idlelib.idle_test.mock_tk import Text
-from idlelib.AutoExpand import AutoExpand
+from idlelib.autoexpand import AutoExpand
class Dummy_Editwin:
@@ -22,6 +22,7 @@ class AutoExpandTest(unittest.TestCase):
else:
cls.text = Text()
cls.auto_expand = AutoExpand(Dummy_Editwin(cls.text))
+ cls.auto_expand.bell = lambda: None
@classmethod
def tearDownClass(cls):
@@ -137,5 +138,6 @@ class AutoExpandTest(unittest.TestCase):
new_state = self.auto_expand.state
self.assertNotEqual(initial_state, new_state)
+
if __name__ == '__main__':
unittest.main(verbosity=2)
diff --git a/Lib/idlelib/idle_test/test_calltips.py b/Lib/idlelib/idle_test/test_calltips.py
index b2a733c..0b11602 100644
--- a/Lib/idlelib/idle_test/test_calltips.py
+++ b/Lib/idlelib/idle_test/test_calltips.py
@@ -1,5 +1,5 @@
import unittest
-import idlelib.CallTips as ct
+import idlelib.calltips as ct
import textwrap
import types
diff --git a/Lib/idlelib/idle_test/test_colorizer.py b/Lib/idlelib/idle_test/test_colorizer.py
new file mode 100644
index 0000000..238bc3e
--- /dev/null
+++ b/Lib/idlelib/idle_test/test_colorizer.py
@@ -0,0 +1,56 @@
+'''Test idlelib/colorizer.py
+
+Perform minimal sanity checks that module imports and some things run.
+
+Coverage 22%.
+'''
+from idlelib import colorizer # always test import
+from test.support import requires
+from tkinter import Tk, Text
+import unittest
+
+
+class FunctionTest(unittest.TestCase):
+
+ def test_any(self):
+ self.assertTrue(colorizer.any('test', ('a', 'b')))
+
+ def test_make_pat(self):
+ self.assertTrue(colorizer.make_pat())
+
+
+class ColorConfigTest(unittest.TestCase):
+
+ @classmethod
+ def setUpClass(cls):
+ requires('gui')
+ cls.root = Tk()
+ cls.text = Text(cls.root)
+
+ @classmethod
+ def tearDownClass(cls):
+ del cls.text
+ cls.root.destroy()
+ del cls.root
+
+ def test_colorizer(self):
+ colorizer.color_config(self.text)
+
+class ColorDelegatorTest(unittest.TestCase):
+
+ @classmethod
+ def setUpClass(cls):
+ requires('gui')
+ cls.root = Tk()
+
+ @classmethod
+ def tearDownClass(cls):
+ cls.root.destroy()
+ del cls.root
+
+ def test_colorizer(self):
+ colorizer.ColorDelegator()
+
+
+if __name__ == '__main__':
+ unittest.main(verbosity=2)
diff --git a/Lib/idlelib/idle_test/test_config.py b/Lib/idlelib/idle_test/test_config.py
new file mode 100644
index 0000000..a3fa1a3
--- /dev/null
+++ b/Lib/idlelib/idle_test/test_config.py
@@ -0,0 +1,160 @@
+'''Test idlelib.config.
+
+Much is tested by opening config dialog live or in test_configdialog.
+Coverage: 27%
+'''
+from sys import modules
+from test.support import captured_stderr
+from tkinter import Tk
+import unittest
+from idlelib import config
+
+# Tests should not depend on fortuitous user configurations.
+# They must not affect actual user .cfg files.
+# Replace user parsers with empty parsers that cannot be saved.
+
+idleConf = config.idleConf
+usercfg = idleConf.userCfg
+testcfg = {}
+usermain = testcfg['main'] = config.IdleUserConfParser('') # filename
+userhigh = testcfg['highlight'] = config.IdleUserConfParser('')
+userkeys = testcfg['keys'] = config.IdleUserConfParser('')
+
+def setUpModule():
+ idleConf.userCfg = testcfg
+
+def tearDownModule():
+ idleConf.userCfg = usercfg
+
+
+class CurrentColorKeysTest(unittest.TestCase):
+ """ Test colorkeys function with user config [Theme] and [Keys] patterns.
+
+ colorkeys = config.IdleConf.current_colors_and_keys
+ Test all patterns written by IDLE and some errors
+ Item 'default' should really be 'builtin' (versus 'custom).
+ """
+ colorkeys = idleConf.current_colors_and_keys
+ default_theme = 'IDLE Classic'
+ default_keys = idleConf.default_keys()
+
+ def test_old_builtin_theme(self):
+ # On initial installation, user main is blank.
+ self.assertEqual(self.colorkeys('Theme'), self.default_theme)
+ # For old default, name2 must be blank.
+ usermain.read_string('''
+ [Theme]
+ default = True
+ ''')
+ # IDLE omits 'name' for default old builtin theme.
+ self.assertEqual(self.colorkeys('Theme'), self.default_theme)
+ # IDLE adds 'name' for non-default old builtin theme.
+ usermain['Theme']['name'] = 'IDLE New'
+ self.assertEqual(self.colorkeys('Theme'), 'IDLE New')
+ # Erroneous non-default old builtin reverts to default.
+ usermain['Theme']['name'] = 'non-existent'
+ self.assertEqual(self.colorkeys('Theme'), self.default_theme)
+ usermain.remove_section('Theme')
+
+ def test_new_builtin_theme(self):
+ # IDLE writes name2 for new builtins.
+ usermain.read_string('''
+ [Theme]
+ default = True
+ name2 = IDLE Dark
+ ''')
+ self.assertEqual(self.colorkeys('Theme'), 'IDLE Dark')
+ # Leftover 'name', not removed, is ignored.
+ usermain['Theme']['name'] = 'IDLE New'
+ self.assertEqual(self.colorkeys('Theme'), 'IDLE Dark')
+ # Erroneous non-default new builtin reverts to default.
+ usermain['Theme']['name2'] = 'non-existent'
+ self.assertEqual(self.colorkeys('Theme'), self.default_theme)
+ usermain.remove_section('Theme')
+
+ def test_user_override_theme(self):
+ # Erroneous custom name (no definition) reverts to default.
+ usermain.read_string('''
+ [Theme]
+ default = False
+ name = Custom Dark
+ ''')
+ self.assertEqual(self.colorkeys('Theme'), self.default_theme)
+ # Custom name is valid with matching Section name.
+ userhigh.read_string('[Custom Dark]\na=b')
+ self.assertEqual(self.colorkeys('Theme'), 'Custom Dark')
+ # Name2 is ignored.
+ usermain['Theme']['name2'] = 'non-existent'
+ self.assertEqual(self.colorkeys('Theme'), 'Custom Dark')
+ usermain.remove_section('Theme')
+ userhigh.remove_section('Custom Dark')
+
+ def test_old_builtin_keys(self):
+ # On initial installation, user main is blank.
+ self.assertEqual(self.colorkeys('Keys'), self.default_keys)
+ # For old default, name2 must be blank, name is always used.
+ usermain.read_string('''
+ [Keys]
+ default = True
+ name = IDLE Classic Unix
+ ''')
+ self.assertEqual(self.colorkeys('Keys'), 'IDLE Classic Unix')
+ # Erroneous non-default old builtin reverts to default.
+ usermain['Keys']['name'] = 'non-existent'
+ self.assertEqual(self.colorkeys('Keys'), self.default_keys)
+ usermain.remove_section('Keys')
+
+ def test_new_builtin_keys(self):
+ # IDLE writes name2 for new builtins.
+ usermain.read_string('''
+ [Keys]
+ default = True
+ name2 = IDLE Modern Unix
+ ''')
+ self.assertEqual(self.colorkeys('Keys'), 'IDLE Modern Unix')
+ # Leftover 'name', not removed, is ignored.
+ usermain['Keys']['name'] = 'IDLE Classic Unix'
+ self.assertEqual(self.colorkeys('Keys'), 'IDLE Modern Unix')
+ # Erroneous non-default new builtin reverts to default.
+ usermain['Keys']['name2'] = 'non-existent'
+ self.assertEqual(self.colorkeys('Keys'), self.default_keys)
+ usermain.remove_section('Keys')
+
+ def test_user_override_keys(self):
+ # Erroneous custom name (no definition) reverts to default.
+ usermain.read_string('''
+ [Keys]
+ default = False
+ name = Custom Keys
+ ''')
+ self.assertEqual(self.colorkeys('Keys'), self.default_keys)
+ # Custom name is valid with matching Section name.
+ userkeys.read_string('[Custom Keys]\na=b')
+ self.assertEqual(self.colorkeys('Keys'), 'Custom Keys')
+ # Name2 is ignored.
+ usermain['Keys']['name2'] = 'non-existent'
+ self.assertEqual(self.colorkeys('Keys'), 'Custom Keys')
+ usermain.remove_section('Keys')
+ userkeys.remove_section('Custom Keys')
+
+
+class WarningTest(unittest.TestCase):
+
+ def test_warn(self):
+ Equal = self.assertEqual
+ config._warned = set()
+ with captured_stderr() as stderr:
+ config._warn('warning', 'key')
+ Equal(config._warned, {('warning','key')})
+ Equal(stderr.getvalue(), 'warning'+'\n')
+ with captured_stderr() as stderr:
+ config._warn('warning', 'key')
+ Equal(stderr.getvalue(), '')
+ with captured_stderr() as stderr:
+ config._warn('warn2', 'yek')
+ Equal(config._warned, {('warning','key'), ('warn2','yek')})
+ Equal(stderr.getvalue(), 'warn2'+'\n')
+
+
+if __name__ == '__main__':
+ unittest.main(verbosity=2)
diff --git a/Lib/idlelib/idle_test/test_config_help.py b/Lib/idlelib/idle_test/test_config_help.py
deleted file mode 100644
index 664f8ed..0000000
--- a/Lib/idlelib/idle_test/test_config_help.py
+++ /dev/null
@@ -1,106 +0,0 @@
-"""Unittests for idlelib.configHelpSourceEdit"""
-import unittest
-from idlelib.idle_test.mock_tk import Var, Mbox, Entry
-from idlelib import configHelpSourceEdit as help_dialog_module
-
-help_dialog = help_dialog_module.GetHelpSourceDialog
-
-
-class Dummy_help_dialog:
- # Mock for testing the following methods of help_dialog
- menu_ok = help_dialog.menu_ok
- path_ok = help_dialog.path_ok
- ok = help_dialog.ok
- cancel = help_dialog.cancel
- # Attributes, constant or variable, needed for tests
- menu = Var()
- entryMenu = Entry()
- path = Var()
- entryPath = Entry()
- result = None
- destroyed = False
-
- def destroy(self):
- self.destroyed = True
-
-
-# menu_ok and path_ok call Mbox.showerror if menu and path are not ok.
-orig_mbox = help_dialog_module.tkMessageBox
-showerror = Mbox.showerror
-
-
-class ConfigHelpTest(unittest.TestCase):
- dialog = Dummy_help_dialog()
-
- @classmethod
- def setUpClass(cls):
- help_dialog_module.tkMessageBox = Mbox
-
- @classmethod
- def tearDownClass(cls):
- help_dialog_module.tkMessageBox = orig_mbox
-
- def test_blank_menu(self):
- self.dialog.menu.set('')
- self.assertFalse(self.dialog.menu_ok())
- self.assertEqual(showerror.title, 'Menu Item Error')
- self.assertIn('No', showerror.message)
-
- def test_long_menu(self):
- self.dialog.menu.set('hello' * 10)
- self.assertFalse(self.dialog.menu_ok())
- self.assertEqual(showerror.title, 'Menu Item Error')
- self.assertIn('long', showerror.message)
-
- def test_good_menu(self):
- self.dialog.menu.set('help')
- showerror.title = 'No Error' # should not be called
- self.assertTrue(self.dialog.menu_ok())
- self.assertEqual(showerror.title, 'No Error')
-
- def test_blank_path(self):
- self.dialog.path.set('')
- self.assertFalse(self.dialog.path_ok())
- self.assertEqual(showerror.title, 'File Path Error')
- self.assertIn('No', showerror.message)
-
- def test_invalid_file_path(self):
- self.dialog.path.set('foobar' * 100)
- self.assertFalse(self.dialog.path_ok())
- self.assertEqual(showerror.title, 'File Path Error')
- self.assertIn('not exist', showerror.message)
-
- def test_invalid_url_path(self):
- self.dialog.path.set('ww.foobar.com')
- self.assertFalse(self.dialog.path_ok())
- self.assertEqual(showerror.title, 'File Path Error')
- self.assertIn('not exist', showerror.message)
-
- self.dialog.path.set('htt.foobar.com')
- self.assertFalse(self.dialog.path_ok())
- self.assertEqual(showerror.title, 'File Path Error')
- self.assertIn('not exist', showerror.message)
-
- def test_good_path(self):
- self.dialog.path.set('https://docs.python.org')
- showerror.title = 'No Error' # should not be called
- self.assertTrue(self.dialog.path_ok())
- self.assertEqual(showerror.title, 'No Error')
-
- def test_ok(self):
- self.dialog.destroyed = False
- self.dialog.menu.set('help')
- self.dialog.path.set('https://docs.python.org')
- self.dialog.ok()
- self.assertEqual(self.dialog.result, ('help',
- 'https://docs.python.org'))
- self.assertTrue(self.dialog.destroyed)
-
- def test_cancel(self):
- self.dialog.destroyed = False
- self.dialog.cancel()
- self.assertEqual(self.dialog.result, None)
- self.assertTrue(self.dialog.destroyed)
-
-if __name__ == '__main__':
- unittest.main(verbosity=2, exit=False)
diff --git a/Lib/idlelib/idle_test/test_config_key.py b/Lib/idlelib/idle_test/test_config_key.py
new file mode 100644
index 0000000..ee3f2c8
--- /dev/null
+++ b/Lib/idlelib/idle_test/test_config_key.py
@@ -0,0 +1,33 @@
+''' Test idlelib.config_key.
+
+Coverage: 56% from creating and closing dialog.
+'''
+from idlelib import config_key
+from test.support import requires
+requires('gui')
+import unittest
+from tkinter import Tk, Text
+
+
+class GetKeysTest(unittest.TestCase):
+
+ @classmethod
+ def setUpClass(cls):
+ cls.root = Tk()
+ cls.root.withdraw()
+
+ @classmethod
+ def tearDownClass(cls):
+ cls.root.update() # Stop "can't run event command" warning.
+ cls.root.destroy()
+ del cls.root
+
+
+ def test_init(self):
+ dia = config_key.GetKeysDialog(
+ self.root, 'test', '<<Test>>', ['<Key-F12>'], _utest=True)
+ dia.Cancel()
+
+
+if __name__ == '__main__':
+ unittest.main(verbosity=2)
diff --git a/Lib/idlelib/idle_test/test_config_name.py b/Lib/idlelib/idle_test/test_config_name.py
deleted file mode 100644
index 40e72b9..0000000
--- a/Lib/idlelib/idle_test/test_config_name.py
+++ /dev/null
@@ -1,75 +0,0 @@
-"""Unit tests for idlelib.configSectionNameDialog"""
-import unittest
-from idlelib.idle_test.mock_tk import Var, Mbox
-from idlelib import configSectionNameDialog as name_dialog_module
-
-name_dialog = name_dialog_module.GetCfgSectionNameDialog
-
-class Dummy_name_dialog:
- # Mock for testing the following methods of name_dialog
- name_ok = name_dialog.name_ok
- Ok = name_dialog.Ok
- Cancel = name_dialog.Cancel
- # Attributes, constant or variable, needed for tests
- used_names = ['used']
- name = Var()
- result = None
- destroyed = False
- def destroy(self):
- self.destroyed = True
-
-# name_ok calls Mbox.showerror if name is not ok
-orig_mbox = name_dialog_module.tkMessageBox
-showerror = Mbox.showerror
-
-class ConfigNameTest(unittest.TestCase):
- dialog = Dummy_name_dialog()
-
- @classmethod
- def setUpClass(cls):
- name_dialog_module.tkMessageBox = Mbox
-
- @classmethod
- def tearDownClass(cls):
- name_dialog_module.tkMessageBox = orig_mbox
-
- def test_blank_name(self):
- self.dialog.name.set(' ')
- self.assertEqual(self.dialog.name_ok(), '')
- self.assertEqual(showerror.title, 'Name Error')
- self.assertIn('No', showerror.message)
-
- def test_used_name(self):
- self.dialog.name.set('used')
- self.assertEqual(self.dialog.name_ok(), '')
- self.assertEqual(showerror.title, 'Name Error')
- self.assertIn('use', showerror.message)
-
- def test_long_name(self):
- self.dialog.name.set('good'*8)
- self.assertEqual(self.dialog.name_ok(), '')
- self.assertEqual(showerror.title, 'Name Error')
- self.assertIn('too long', showerror.message)
-
- def test_good_name(self):
- self.dialog.name.set(' good ')
- showerror.title = 'No Error' # should not be called
- self.assertEqual(self.dialog.name_ok(), 'good')
- self.assertEqual(showerror.title, 'No Error')
-
- def test_ok(self):
- self.dialog.destroyed = False
- self.dialog.name.set('good')
- self.dialog.Ok()
- self.assertEqual(self.dialog.result, 'good')
- self.assertTrue(self.dialog.destroyed)
-
- def test_cancel(self):
- self.dialog.destroyed = False
- self.dialog.Cancel()
- self.assertEqual(self.dialog.result, '')
- self.assertTrue(self.dialog.destroyed)
-
-
-if __name__ == '__main__':
- unittest.main(verbosity=2, exit=False)
diff --git a/Lib/idlelib/idle_test/test_configdialog.py b/Lib/idlelib/idle_test/test_configdialog.py
index 5c09790..3f94493 100644
--- a/Lib/idlelib/idle_test/test_configdialog.py
+++ b/Lib/idlelib/idle_test/test_configdialog.py
@@ -1,32 +1,127 @@
-'''Test idlelib.configDialog.
+"""Test idlelib.configdialog.
-Coverage: 46% just by creating dialog.
-The other half is code for working with user customizations.
-'''
-from idlelib.configDialog import ConfigDialog # always test import
+Half the class creates dialog, half works with user customizations.
+Coverage: 46% just by creating dialog, 56% with current tests.
+"""
+from idlelib.configdialog import ConfigDialog, idleConf # test import
from test.support import requires
requires('gui')
from tkinter import Tk
import unittest
-from idlelib import macosxSupport as macosx
+import idlelib.config as config
-class ConfigDialogTest(unittest.TestCase):
+# Tests should not depend on fortuitous user configurations.
+# They must not affect actual user .cfg files.
+# Use solution from test_config: empty parsers with no filename.
+usercfg = idleConf.userCfg
+testcfg = {
+ 'main': config.IdleUserConfParser(''),
+ 'highlight': config.IdleUserConfParser(''),
+ 'keys': config.IdleUserConfParser(''),
+ 'extensions': config.IdleUserConfParser(''),
+}
- @classmethod
- def setUpClass(cls):
- cls.root = Tk()
- cls.root.withdraw()
- macosx._initializeTkVariantTests(cls.root)
+# ConfigDialog.changedItems is a 3-level hierarchical dictionary of
+# pending changes that mirrors the multilevel user config dict.
+# For testing, record args in a list for comparison with expected.
+changes = []
+class TestDialog(ConfigDialog):
+ def AddChangedItem(self, *args):
+ changes.append(args)
- @classmethod
- def tearDownClass(cls):
- cls.root.update_idletasks()
- cls.root.destroy()
- del cls.root
+def setUpModule():
+ global root, configure
+ idleConf.userCfg = testcfg
+ root = Tk()
+ root.withdraw()
+ configure = TestDialog(root, 'Test', _utest=True)
- def test_dialog(self):
- d = ConfigDialog(self.root, 'Test', _utest=True)
- d.remove_var_callbacks()
+
+def tearDownModule():
+ global root, configure
+ idleConf.userCfg = testcfg
+ configure.remove_var_callbacks()
+ del configure
+ root.update_idletasks()
+ root.destroy()
+ del root
+
+
+class FontTabTest(unittest.TestCase):
+
+
+ def setUp(self):
+ changes.clear()
+
+ def test_font(self):
+ # Set values guaranteed not to be defaults.
+ dfont = idleConf.GetFont(root, 'main', 'EditorWindow')
+ dsize = str(dfont[1])
+ dbold = dfont[2] == 'bold'
+ configure.fontName.set('Test Font')
+ expected = [
+ ('main', 'EditorWindow', 'font', 'Test Font'),
+ ('main', 'EditorWindow', 'font-size', dsize),
+ ('main', 'EditorWindow', 'font-bold', dbold)]
+ self.assertEqual(changes, expected)
+ changes.clear()
+ configure.fontSize.set(20)
+ expected = [
+ ('main', 'EditorWindow', 'font', 'Test Font'),
+ ('main', 'EditorWindow', 'font-size', '20'),
+ ('main', 'EditorWindow', 'font-bold', dbold)]
+ self.assertEqual(changes, expected)
+ changes.clear()
+ configure.fontBold.set(not dbold)
+ expected = [
+ ('main', 'EditorWindow', 'font', 'Test Font'),
+ ('main', 'EditorWindow', 'font-size', '20'),
+ ('main', 'EditorWindow', 'font-bold', not dbold)]
+ self.assertEqual(changes, expected)
+
+ #def test_sample(self): pass # TODO
+
+ def test_tabspace(self):
+ configure.spaceNum.set(6)
+ self.assertEqual(changes, [('main', 'Indent', 'num-spaces', 6)])
+
+
+class HighlightTest(unittest.TestCase):
+
+ def setUp(self):
+ changes.clear()
+
+ #def test_colorchoose(self): pass # TODO
+
+
+class KeysTest(unittest.TestCase):
+
+ def setUp(self):
+ changes.clear()
+
+
+class GeneralTest(unittest.TestCase):
+
+ def setUp(self):
+ changes.clear()
+
+ def test_startup(self):
+ configure.radioStartupEdit.invoke()
+ self.assertEqual(changes,
+ [('main', 'General', 'editor-on-startup', 1)])
+
+ def test_autosave(self):
+ configure.radioSaveAuto.invoke()
+ self.assertEqual(changes, [('main', 'General', 'autosave', 1)])
+
+ def test_editor_size(self):
+ configure.entryWinHeight.insert(0, '1')
+ self.assertEqual(changes, [('main', 'EditorWindow', 'height', '140')])
+ changes.clear()
+ configure.entryWinWidth.insert(0, '1')
+ self.assertEqual(changes, [('main', 'EditorWindow', 'width', '180')])
+
+ #def test_help_sources(self): pass # TODO
if __name__ == '__main__':
diff --git a/Lib/idlelib/idle_test/test_debugger.py b/Lib/idlelib/idle_test/test_debugger.py
new file mode 100644
index 0000000..bcba9a4
--- /dev/null
+++ b/Lib/idlelib/idle_test/test_debugger.py
@@ -0,0 +1,29 @@
+''' Test idlelib.debugger.
+
+Coverage: 19%
+'''
+from idlelib import debugger
+from test.support import requires
+requires('gui')
+import unittest
+from tkinter import Tk
+
+
+class NameSpaceTest(unittest.TestCase):
+
+ @classmethod
+ def setUpClass(cls):
+ cls.root = Tk()
+ cls.root.withdraw()
+
+ @classmethod
+ def tearDownClass(cls):
+ cls.root.destroy()
+ del cls.root
+
+ def test_init(self):
+ debugger.NamespaceViewer(self.root, 'Test')
+
+
+if __name__ == '__main__':
+ unittest.main(verbosity=2)
diff --git a/Lib/idlelib/idle_test/test_delegator.py b/Lib/idlelib/idle_test/test_delegator.py
index 1f0baa9..85624fb 100644
--- a/Lib/idlelib/idle_test/test_delegator.py
+++ b/Lib/idlelib/idle_test/test_delegator.py
@@ -1,5 +1,5 @@
import unittest
-from idlelib.Delegator import Delegator
+from idlelib.delegator import Delegator
class DelegatorTest(unittest.TestCase):
diff --git a/Lib/idlelib/idle_test/test_editmenu.py b/Lib/idlelib/idle_test/test_editmenu.py
index a258e29..17eb25c 100644
--- a/Lib/idlelib/idle_test/test_editmenu.py
+++ b/Lib/idlelib/idle_test/test_editmenu.py
@@ -5,8 +5,9 @@ Edit modules have their own test files files
from test.support import requires
requires('gui')
import tkinter as tk
+from tkinter import ttk
import unittest
-from idlelib import PyShell
+from idlelib import pyshell
class PasteTest(unittest.TestCase):
'''Test pasting into widgets that allow pasting.
@@ -16,17 +17,18 @@ class PasteTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.root = root = tk.Tk()
- root.withdraw()
- PyShell.fix_x11_paste(root)
+ cls.root.withdraw()
+ pyshell.fix_x11_paste(root)
cls.text = tk.Text(root)
cls.entry = tk.Entry(root)
+ cls.tentry = ttk.Entry(root)
cls.spin = tk.Spinbox(root)
root.clipboard_clear()
root.clipboard_append('two')
@classmethod
def tearDownClass(cls):
- del cls.text, cls.entry, cls.spin
+ del cls.text, cls.entry, cls.tentry
cls.root.clipboard_clear()
cls.root.update_idletasks()
cls.root.destroy()
@@ -44,16 +46,16 @@ class PasteTest(unittest.TestCase):
def test_paste_entry(self):
"Test pasting into an entry with and without a selection."
- # On 3.6, generated <<Paste>> fails without empty select range
- # for 'no selection'. Live widget works fine.
- entry = self.entry
- for end, ans in (0, 'onetwo'), ('end', 'two'):
- with self.subTest(entry=entry, end=end, ans=ans):
- entry.delete(0, 'end')
- entry.insert(0, 'one')
- entry.select_range(0, end) # see note
- entry.event_generate('<<Paste>>')
- self.assertEqual(entry.get(), ans)
+ # Generated <<Paste>> fails for tk entry without empty select
+ # range for 'no selection'. Live widget works fine.
+ for entry in self.entry, self.tentry:
+ for end, ans in (0, 'onetwo'), ('end', 'two'):
+ with self.subTest(entry=entry, end=end, ans=ans):
+ entry.delete(0, 'end')
+ entry.insert(0, 'one')
+ entry.select_range(0, end)
+ entry.event_generate('<<Paste>>')
+ self.assertEqual(entry.get(), ans)
def test_paste_spin(self):
"Test pasting into a spinbox with and without a selection."
diff --git a/Lib/idlelib/idle_test/test_editor.py b/Lib/idlelib/idle_test/test_editor.py
index a31d26d..e9d29d4 100644
--- a/Lib/idlelib/idle_test/test_editor.py
+++ b/Lib/idlelib/idle_test/test_editor.py
@@ -1,6 +1,6 @@
import unittest
from tkinter import Tk, Text
-from idlelib.EditorWindow import EditorWindow
+from idlelib.editor import EditorWindow
from test.support import requires
class Editor_func_test(unittest.TestCase):
diff --git a/Lib/idlelib/idle_test/test_grep.py b/Lib/idlelib/idle_test/test_grep.py
index 0d8ff0d..6b54c13 100644
--- a/Lib/idlelib/idle_test/test_grep.py
+++ b/Lib/idlelib/idle_test/test_grep.py
@@ -1,5 +1,5 @@
""" !Changing this line will break Test_findfile.test_found!
-Non-gui unit tests for idlelib.GrepDialog methods.
+Non-gui unit tests for grep.GrepDialog methods.
dummy_command calls grep_it calls findfiles.
An exception raised in one method will fail callers.
Otherwise, tests are mostly independent.
@@ -8,7 +8,7 @@ Otherwise, tests are mostly independent.
import unittest
from test.support import captured_stdout
from idlelib.idle_test.mock_tk import Var
-from idlelib.GrepDialog import GrepDialog
+from idlelib.grep import GrepDialog
import re
class Dummy_searchengine:
@@ -72,7 +72,7 @@ class Grep_itTest(unittest.TestCase):
self.assertTrue(lines[4].startswith('(Hint:'))
class Default_commandTest(unittest.TestCase):
- # To write this, mode OutputWindow import to top of GrepDialog
+ # To write this, move outwin import to top of GrepDialog
# so it can be replaced by captured_stdout in class setup/teardown.
pass
diff --git a/Lib/idlelib/idle_test/test_help.py b/Lib/idlelib/idle_test/test_help.py
new file mode 100644
index 0000000..2c68e23
--- /dev/null
+++ b/Lib/idlelib/idle_test/test_help.py
@@ -0,0 +1,34 @@
+'''Test idlelib.help.
+
+Coverage: 87%
+'''
+from idlelib import help
+from test.support import requires
+requires('gui')
+from os.path import abspath, dirname, join
+from tkinter import Tk
+import unittest
+
+class HelpFrameTest(unittest.TestCase):
+
+ @classmethod
+ def setUpClass(cls):
+ "By itself, this tests that file parsed without exception."
+ cls.root = root = Tk()
+ root.withdraw()
+ helpfile = join(dirname(dirname(abspath(__file__))), 'help.html')
+ cls.frame = help.HelpFrame(root, helpfile)
+
+ @classmethod
+ def tearDownClass(cls):
+ del cls.frame
+ cls.root.update_idletasks()
+ cls.root.destroy()
+ del cls.root
+
+ def test_line1(self):
+ text = self.frame.text
+ self.assertEqual(text.get('1.0', '1.end'), ' IDLE ')
+
+if __name__ == '__main__':
+ unittest.main(verbosity=2)
diff --git a/Lib/idlelib/idle_test/test_help_about.py b/Lib/idlelib/idle_test/test_help_about.py
index d0a0127..843efb9 100644
--- a/Lib/idlelib/idle_test/test_help_about.py
+++ b/Lib/idlelib/idle_test/test_help_about.py
@@ -2,10 +2,10 @@
Coverage:
'''
-from idlelib import aboutDialog as help_about
-from idlelib import textView as textview
+from idlelib import help_about
+from idlelib import textview
from idlelib.idle_test.mock_idle import Func
-from idlelib.idle_test.mock_tk import Mbox
+from idlelib.idle_test.mock_tk import Mbox_func
import unittest
About = help_about.AboutDialog
@@ -19,33 +19,33 @@ class Dummy_about_dialog():
class DisplayFileTest(unittest.TestCase):
- "Test that .txt files are found and properly decoded."
dialog = Dummy_about_dialog()
@classmethod
def setUpClass(cls):
- cls.orig_mbox = textview.tkMessageBox
+ cls.orig_error = textview.showerror
cls.orig_view = textview.view_text
- cls.mbox = Mbox()
+ cls.error = Mbox_func()
cls.view = Func()
- textview.tkMessageBox = cls.mbox
+ textview.showerror = cls.error
textview.view_text = cls.view
cls.About = Dummy_about_dialog()
@classmethod
def tearDownClass(cls):
- textview.tkMessageBox = cls.orig_mbox
+ textview.showerror = cls.orig_error
textview.view_text = cls.orig_view
def test_file_isplay(self):
for handler in (self.dialog.idle_credits,
self.dialog.idle_readme,
self.dialog.idle_news):
- self.mbox.showerror.message = ''
+ self.error.message = ''
self.view.called = False
- handler()
- self.assertEqual(self.mbox.showerror.message, '')
- self.assertEqual(self.view.called, True)
+ with self.subTest(handler=handler):
+ handler()
+ self.assertEqual(self.error.message, '')
+ self.assertEqual(self.view.called, True)
if __name__ == '__main__':
diff --git a/Lib/idlelib/idle_test/test_idlehistory.py b/Lib/idlelib/idle_test/test_history.py
index 6e7c6c3..b278010 100644
--- a/Lib/idlelib/idle_test/test_idlehistory.py
+++ b/Lib/idlelib/idle_test/test_history.py
@@ -4,8 +4,8 @@ from test.support import requires
import tkinter as tk
from tkinter import Text as tkText
from idlelib.idle_test.mock_tk import Text as mkText
-from idlelib.IdleHistory import History
-from idlelib.configHandler import idleConf
+from idlelib.history import History
+from idlelib.config import idleConf
line1 = 'a = 7'
line2 = 'b = a'
diff --git a/Lib/idlelib/idle_test/test_hyperparser.py b/Lib/idlelib/idle_test/test_hyperparser.py
index 9ce3f2c..73c8281 100644
--- a/Lib/idlelib/idle_test/test_hyperparser.py
+++ b/Lib/idlelib/idle_test/test_hyperparser.py
@@ -1,9 +1,9 @@
-"""Unittest for idlelib.HyperParser"""
+"""Unittest for idlelib.hyperparser.py."""
import unittest
from test.support import requires
from tkinter import Tk, Text
-from idlelib.EditorWindow import EditorWindow
-from idlelib.HyperParser import HyperParser
+from idlelib.editor import EditorWindow
+from idlelib.hyperparser import HyperParser
class DummyEditwin:
def __init__(self, text):
diff --git a/Lib/idlelib/idle_test/test_io.py b/Lib/idlelib/idle_test/test_iomenu.py
index e0e3b98..65bf593 100644
--- a/Lib/idlelib/idle_test/test_io.py
+++ b/Lib/idlelib/idle_test/test_iomenu.py
@@ -1,6 +1,7 @@
import unittest
import io
-from idlelib.PyShell import PseudoInputFile, PseudoOutputFile
+
+from idlelib.run import PseudoInputFile, PseudoOutputFile
class S(str):
@@ -230,4 +231,4 @@ class PseudeInputFilesTest(unittest.TestCase):
if __name__ == '__main__':
- unittest.main()
+ unittest.main(verbosity=2)
diff --git a/Lib/idlelib/idle_test/test_macosx.py b/Lib/idlelib/idle_test/test_macosx.py
new file mode 100644
index 0000000..fae75d8
--- /dev/null
+++ b/Lib/idlelib/idle_test/test_macosx.py
@@ -0,0 +1,103 @@
+'''Test idlelib.macosx.py.
+
+Coverage: 71% on Windows.
+'''
+from idlelib import macosx
+from test.support import requires
+import sys
+import tkinter as tk
+import unittest
+import unittest.mock as mock
+from idlelib.filelist import FileList
+
+mactypes = {'carbon', 'cocoa', 'xquartz'}
+nontypes = {'other'}
+alltypes = mactypes | nontypes
+
+
+class InitTktypeTest(unittest.TestCase):
+ "Test _init_tk_type."
+
+ @classmethod
+ def setUpClass(cls):
+ requires('gui')
+ cls.root = tk.Tk()
+ cls.root.withdraw()
+ cls.orig_platform = macosx.platform
+
+ @classmethod
+ def tearDownClass(cls):
+ cls.root.update_idletasks()
+ cls.root.destroy()
+ del cls.root
+ macosx.platform = cls.orig_platform
+
+ def test_init_sets_tktype(self):
+ "Test that _init_tk_type sets _tk_type according to platform."
+ for platform, types in ('darwin', alltypes), ('other', nontypes):
+ with self.subTest(platform=platform):
+ macosx.platform = platform
+ macosx._tk_type == None
+ macosx._init_tk_type()
+ self.assertIn(macosx._tk_type, types)
+
+
+class IsTypeTkTest(unittest.TestCase):
+ "Test each of the four isTypeTk predecates."
+ isfuncs = ((macosx.isAquaTk, ('carbon', 'cocoa')),
+ (macosx.isCarbonTk, ('carbon')),
+ (macosx.isCocoaTk, ('cocoa')),
+ (macosx.isXQuartz, ('xquartz')),
+ )
+
+ @mock.patch('idlelib.macosx._init_tk_type')
+ def test_is_calls_init(self, mockinit):
+ "Test that each isTypeTk calls _init_tk_type when _tk_type is None."
+ macosx._tk_type = None
+ for func, whentrue in self.isfuncs:
+ with self.subTest(func=func):
+ func()
+ self.assertTrue(mockinit.called)
+ mockinit.reset_mock()
+
+ def test_isfuncs(self):
+ "Test that each isTypeTk return correct bool."
+ for func, whentrue in self.isfuncs:
+ for tktype in alltypes:
+ with self.subTest(func=func, whentrue=whentrue, tktype=tktype):
+ macosx._tk_type = tktype
+ (self.assertTrue if tktype in whentrue else self.assertFalse)\
+ (func())
+
+
+class SetupTest(unittest.TestCase):
+ "Test setupApp."
+
+ @classmethod
+ def setUpClass(cls):
+ requires('gui')
+ cls.root = tk.Tk()
+ cls.root.withdraw()
+
+ @classmethod
+ def tearDownClass(cls):
+ cls.root.update_idletasks()
+ cls.root.destroy()
+ del cls.root
+
+ @mock.patch('idlelib.macosx.overrideRootMenu') #27312
+ def test_setupapp(self, overrideRootMenu):
+ "Call setupApp with each possible graphics type."
+ root = self.root
+ flist = FileList(root)
+ for tktype in alltypes:
+ with self.subTest(tktype=tktype):
+ macosx._tk_type = tktype
+ macosx.setupApp(root, flist)
+ if tktype in ('carbon', 'cocoa'):
+ self.assertTrue(overrideRootMenu.called)
+ overrideRootMenu.reset_mock()
+
+
+if __name__ == '__main__':
+ unittest.main(verbosity=2)
diff --git a/Lib/idlelib/idle_test/test_formatparagraph.py b/Lib/idlelib/idle_test/test_paragraph.py
index b6eb2f3..ba350c9 100644
--- a/Lib/idlelib/idle_test/test_formatparagraph.py
+++ b/Lib/idlelib/idle_test/test_paragraph.py
@@ -1,7 +1,7 @@
-# Test the functions and main class method of FormatParagraph.py
+# Test the functions and main class method of paragraph.py
import unittest
-from idlelib import FormatParagraph as fp
-from idlelib.EditorWindow import EditorWindow
+from idlelib import paragraph as fp
+from idlelib.editor import EditorWindow
from tkinter import Tk, Text
from test.support import requires
@@ -38,7 +38,7 @@ class Is_Get_Test(unittest.TestCase):
class FindTest(unittest.TestCase):
- """Test the find_paragraph function in FormatParagraph.
+ """Test the find_paragraph function in paragraph module.
Using the runcase() function, find_paragraph() is called with 'mark' set at
multiple indexes before and inside the test paragraph.
diff --git a/Lib/idlelib/idle_test/test_parenmatch.py b/Lib/idlelib/idle_test/test_parenmatch.py
index 95cc22c..051f7ea 100644
--- a/Lib/idlelib/idle_test/test_parenmatch.py
+++ b/Lib/idlelib/idle_test/test_parenmatch.py
@@ -1,4 +1,4 @@
-'''Test idlelib.ParenMatch.
+'''Test idlelib.parenmatch.
This must currently be a gui test because ParenMatch methods use
several text methods not defined on idlelib.idle_test.mock_tk.Text.
@@ -9,7 +9,7 @@ requires('gui')
import unittest
from unittest.mock import Mock
from tkinter import Tk, Text
-from idlelib.ParenMatch import ParenMatch
+from idlelib.parenmatch import ParenMatch
class DummyEditwin:
def __init__(self, text):
@@ -38,12 +38,17 @@ class ParenMatchTest(unittest.TestCase):
def tearDown(self):
self.text.delete('1.0', 'end')
+ def get_parenmatch(self):
+ pm = ParenMatch(self.editwin)
+ pm.bell = lambda: None
+ return pm
+
def test_paren_expression(self):
"""
Test ParenMatch with 'expression' style.
"""
text = self.text
- pm = ParenMatch(self.editwin)
+ pm = self.get_parenmatch()
pm.set_style('expression')
text.insert('insert', 'def foobar(a, b')
@@ -66,7 +71,7 @@ class ParenMatchTest(unittest.TestCase):
Test ParenMatch with 'default' style.
"""
text = self.text
- pm = ParenMatch(self.editwin)
+ pm = self.get_parenmatch()
pm.set_style('default')
text.insert('insert', 'def foobar(a, b')
@@ -86,7 +91,7 @@ class ParenMatchTest(unittest.TestCase):
These cases force conditional expression and alternate paths.
"""
text = self.text
- pm = ParenMatch(self.editwin)
+ pm = self.get_parenmatch()
text.insert('insert', '# this is a commen)')
self.assertIsNone(pm.paren_closed_event('event'))
@@ -99,7 +104,7 @@ class ParenMatchTest(unittest.TestCase):
self.assertIsNone(pm.paren_closed_event('event'))
def test_handle_restore_timer(self):
- pm = ParenMatch(self.editwin)
+ pm = self.get_parenmatch()
pm.restore_event = Mock()
pm.handle_restore_timer(0)
self.assertTrue(pm.restore_event.called)
diff --git a/Lib/idlelib/idle_test/test_pathbrowser.py b/Lib/idlelib/idle_test/test_pathbrowser.py
index afb886f..813cbcc 100644
--- a/Lib/idlelib/idle_test/test_pathbrowser.py
+++ b/Lib/idlelib/idle_test/test_pathbrowser.py
@@ -2,13 +2,13 @@ import unittest
import os
import sys
import idlelib
-from idlelib import PathBrowser
+from idlelib import pathbrowser
class PathBrowserTest(unittest.TestCase):
def test_DirBrowserTreeItem(self):
# Issue16226 - make sure that getting a sublist works
- d = PathBrowser.DirBrowserTreeItem('')
+ d = pathbrowser.DirBrowserTreeItem('')
d.GetSubList()
self.assertEqual('', d.GetText())
@@ -17,11 +17,11 @@ class PathBrowserTest(unittest.TestCase):
self.assertEqual(d.ispackagedir(dir + '/Icons'), False)
def test_PathBrowserTreeItem(self):
- p = PathBrowser.PathBrowserTreeItem()
+ p = pathbrowser.PathBrowserTreeItem()
self.assertEqual(p.GetText(), 'sys.path')
sub = p.GetSubList()
self.assertEqual(len(sub), len(sys.path))
- self.assertEqual(type(sub[0]), PathBrowser.DirBrowserTreeItem)
+ self.assertEqual(type(sub[0]), pathbrowser.DirBrowserTreeItem)
if __name__ == '__main__':
unittest.main(verbosity=2, exit=False)
diff --git a/Lib/idlelib/idle_test/test_percolator.py b/Lib/idlelib/idle_test/test_percolator.py
index 4c0a7ad..573b9a1 100644
--- a/Lib/idlelib/idle_test/test_percolator.py
+++ b/Lib/idlelib/idle_test/test_percolator.py
@@ -1,10 +1,10 @@
-'''Test Percolator'''
+'''Test percolator.py.'''
from test.support import requires
requires('gui')
import unittest
from tkinter import Text, Tk, END
-from idlelib.Percolator import Percolator, Delegator
+from idlelib.percolator import Percolator, Delegator
class MyFilter(Delegator):
diff --git a/Lib/idlelib/idle_test/test_query.py b/Lib/idlelib/idle_test/test_query.py
new file mode 100644
index 0000000..66af8eb
--- /dev/null
+++ b/Lib/idlelib/idle_test/test_query.py
@@ -0,0 +1,353 @@
+"""Test idlelib.query.
+
+Non-gui tests for Query, SectionName, ModuleName, and HelpSource use
+dummy versions that extract the non-gui methods and add other needed
+attributes. GUI tests create an instance of each class and simulate
+entries and button clicks. Subclass tests only target the new code in
+the subclass definition.
+
+The appearance of the widgets is checked by the Query and
+HelpSource htests. These are run by running query.py.
+
+Coverage: 94% (100% for Query and SectionName).
+6 of 8 missing are ModuleName exceptions I don't know how to trigger.
+"""
+from test.support import requires
+import sys
+from tkinter import Tk
+import unittest
+from unittest import mock
+from idlelib.idle_test.mock_tk import Var
+from idlelib import query
+
+
+# NON-GUI TESTS
+
+class QueryTest(unittest.TestCase):
+ "Test Query base class."
+
+ class Dummy_Query:
+ # Test the following Query methods.
+ entry_ok = query.Query.entry_ok
+ ok = query.Query.ok
+ cancel = query.Query.cancel
+ # Add attributes and initialization needed for tests.
+ entry = Var()
+ entry_error = {}
+ def __init__(self, dummy_entry):
+ self.entry.set(dummy_entry)
+ self.entry_error['text'] = ''
+ self.result = None
+ self.destroyed = False
+ def showerror(self, message):
+ self.entry_error['text'] = message
+ def destroy(self):
+ self.destroyed = True
+
+ def test_entry_ok_blank(self):
+ dialog = self.Dummy_Query(' ')
+ self.assertEqual(dialog.entry_ok(), None)
+ self.assertEqual((dialog.result, dialog.destroyed), (None, False))
+ self.assertIn('blank line', dialog.entry_error['text'])
+
+ def test_entry_ok_good(self):
+ dialog = self.Dummy_Query(' good ')
+ Equal = self.assertEqual
+ Equal(dialog.entry_ok(), 'good')
+ Equal((dialog.result, dialog.destroyed), (None, False))
+ Equal(dialog.entry_error['text'], '')
+
+ def test_ok_blank(self):
+ dialog = self.Dummy_Query('')
+ dialog.entry.focus_set = mock.Mock()
+ self.assertEqual(dialog.ok(), None)
+ self.assertTrue(dialog.entry.focus_set.called)
+ del dialog.entry.focus_set
+ self.assertEqual((dialog.result, dialog.destroyed), (None, False))
+
+ def test_ok_good(self):
+ dialog = self.Dummy_Query('good')
+ self.assertEqual(dialog.ok(), None)
+ self.assertEqual((dialog.result, dialog.destroyed), ('good', True))
+
+ def test_cancel(self):
+ dialog = self.Dummy_Query('does not matter')
+ self.assertEqual(dialog.cancel(), None)
+ self.assertEqual((dialog.result, dialog.destroyed), (None, True))
+
+
+class SectionNameTest(unittest.TestCase):
+ "Test SectionName subclass of Query."
+
+ class Dummy_SectionName:
+ entry_ok = query.SectionName.entry_ok # Function being tested.
+ used_names = ['used']
+ entry = Var()
+ entry_error = {}
+ def __init__(self, dummy_entry):
+ self.entry.set(dummy_entry)
+ self.entry_error['text'] = ''
+ def showerror(self, message):
+ self.entry_error['text'] = message
+
+ def test_blank_section_name(self):
+ dialog = self.Dummy_SectionName(' ')
+ self.assertEqual(dialog.entry_ok(), None)
+ self.assertIn('no name', dialog.entry_error['text'])
+
+ def test_used_section_name(self):
+ dialog = self.Dummy_SectionName('used')
+ self.assertEqual(dialog.entry_ok(), None)
+ self.assertIn('use', dialog.entry_error['text'])
+
+ def test_long_section_name(self):
+ dialog = self.Dummy_SectionName('good'*8)
+ self.assertEqual(dialog.entry_ok(), None)
+ self.assertIn('longer than 30', dialog.entry_error['text'])
+
+ def test_good_section_name(self):
+ dialog = self.Dummy_SectionName(' good ')
+ self.assertEqual(dialog.entry_ok(), 'good')
+ self.assertEqual(dialog.entry_error['text'], '')
+
+
+class ModuleNameTest(unittest.TestCase):
+ "Test ModuleName subclass of Query."
+
+ class Dummy_ModuleName:
+ entry_ok = query.ModuleName.entry_ok # Function being tested.
+ text0 = ''
+ entry = Var()
+ entry_error = {}
+ def __init__(self, dummy_entry):
+ self.entry.set(dummy_entry)
+ self.entry_error['text'] = ''
+ def showerror(self, message):
+ self.entry_error['text'] = message
+
+ def test_blank_module_name(self):
+ dialog = self.Dummy_ModuleName(' ')
+ self.assertEqual(dialog.entry_ok(), None)
+ self.assertIn('no name', dialog.entry_error['text'])
+
+ def test_bogus_module_name(self):
+ dialog = self.Dummy_ModuleName('__name_xyz123_should_not_exist__')
+ self.assertEqual(dialog.entry_ok(), None)
+ self.assertIn('not found', dialog.entry_error['text'])
+
+ def test_c_source_name(self):
+ dialog = self.Dummy_ModuleName('itertools')
+ self.assertEqual(dialog.entry_ok(), None)
+ self.assertIn('source-based', dialog.entry_error['text'])
+
+ def test_good_module_name(self):
+ dialog = self.Dummy_ModuleName('idlelib')
+ self.assertTrue(dialog.entry_ok().endswith('__init__.py'))
+ self.assertEqual(dialog.entry_error['text'], '')
+
+
+# 3 HelpSource test classes each test one function.
+
+orig_platform = query.platform
+
+class HelpsourceBrowsefileTest(unittest.TestCase):
+ "Test browse_file method of ModuleName subclass of Query."
+
+ class Dummy_HelpSource:
+ browse_file = query.HelpSource.browse_file
+ pathvar = Var()
+
+ def test_file_replaces_path(self):
+ dialog = self.Dummy_HelpSource()
+ # Path is widget entry, either '' or something.
+ # Func return is file dialog return, either '' or something.
+ # Func return should override widget entry.
+ # We need all 4 combination to test all (most) code paths.
+ for path, func, result in (
+ ('', lambda a,b,c:'', ''),
+ ('', lambda a,b,c: __file__, __file__),
+ ('htest', lambda a,b,c:'', 'htest'),
+ ('htest', lambda a,b,c: __file__, __file__)):
+ with self.subTest():
+ dialog.pathvar.set(path)
+ dialog.askfilename = func
+ dialog.browse_file()
+ self.assertEqual(dialog.pathvar.get(), result)
+
+
+class HelpsourcePathokTest(unittest.TestCase):
+ "Test path_ok method of HelpSource subclass of Query."
+
+ class Dummy_HelpSource:
+ path_ok = query.HelpSource.path_ok
+ path = Var()
+ path_error = {}
+ def __init__(self, dummy_path):
+ self.path.set(dummy_path)
+ self.path_error['text'] = ''
+ def showerror(self, message, widget=None):
+ self.path_error['text'] = message
+
+ @classmethod
+ def tearDownClass(cls):
+ query.platform = orig_platform
+
+ def test_path_ok_blank(self):
+ dialog = self.Dummy_HelpSource(' ')
+ self.assertEqual(dialog.path_ok(), None)
+ self.assertIn('no help file', dialog.path_error['text'])
+
+ def test_path_ok_bad(self):
+ dialog = self.Dummy_HelpSource(__file__ + 'bad-bad-bad')
+ self.assertEqual(dialog.path_ok(), None)
+ self.assertIn('not exist', dialog.path_error['text'])
+
+ def test_path_ok_web(self):
+ dialog = self.Dummy_HelpSource('')
+ Equal = self.assertEqual
+ for url in 'www.py.org', 'http://py.org':
+ with self.subTest():
+ dialog.path.set(url)
+ self.assertEqual(dialog.path_ok(), url)
+ self.assertEqual(dialog.path_error['text'], '')
+
+ def test_path_ok_file(self):
+ dialog = self.Dummy_HelpSource('')
+ for platform, prefix in ('darwin', 'file://'), ('other', ''):
+ with self.subTest():
+ query.platform = platform
+ dialog.path.set(__file__)
+ self.assertEqual(dialog.path_ok(), prefix + __file__)
+ self.assertEqual(dialog.path_error['text'], '')
+
+
+class HelpsourceEntryokTest(unittest.TestCase):
+ "Test entry_ok method of HelpSource subclass of Query."
+
+ class Dummy_HelpSource:
+ entry_ok = query.HelpSource.entry_ok
+ entry_error = {}
+ path_error = {}
+ def item_ok(self):
+ return self.name
+ def path_ok(self):
+ return self.path
+
+ def test_entry_ok_helpsource(self):
+ dialog = self.Dummy_HelpSource()
+ for name, path, result in ((None, None, None),
+ (None, 'doc.txt', None),
+ ('doc', None, None),
+ ('doc', 'doc.txt', ('doc', 'doc.txt'))):
+ with self.subTest():
+ dialog.name, dialog.path = name, path
+ self.assertEqual(dialog.entry_ok(), result)
+
+
+# GUI TESTS
+
+class QueryGuiTest(unittest.TestCase):
+
+ @classmethod
+ def setUpClass(cls):
+ requires('gui')
+ cls.root = root = Tk()
+ cls.root.withdraw()
+ cls.dialog = query.Query(root, 'TEST', 'test', _utest=True)
+ cls.dialog.destroy = mock.Mock()
+
+ @classmethod
+ def tearDownClass(cls):
+ del cls.dialog
+ cls.root.destroy()
+ del cls.root
+
+ def setUp(self):
+ self.dialog.entry.delete(0, 'end')
+ self.dialog.result = None
+ self.dialog.destroy.reset_mock()
+
+ def test_click_ok(self):
+ dialog = self.dialog
+ dialog.entry.insert(0, 'abc')
+ dialog.button_ok.invoke()
+ self.assertEqual(dialog.result, 'abc')
+ self.assertTrue(dialog.destroy.called)
+
+ def test_click_blank(self):
+ dialog = self.dialog
+ dialog.button_ok.invoke()
+ self.assertEqual(dialog.result, None)
+ self.assertFalse(dialog.destroy.called)
+
+ def test_click_cancel(self):
+ dialog = self.dialog
+ dialog.entry.insert(0, 'abc')
+ dialog.button_cancel.invoke()
+ self.assertEqual(dialog.result, None)
+ self.assertTrue(dialog.destroy.called)
+
+
+class SectionnameGuiTest(unittest.TestCase):
+
+ @classmethod
+ def setUpClass(cls):
+ requires('gui')
+
+ def test_click_section_name(self):
+ root = Tk()
+ root.withdraw()
+ dialog = query.SectionName(root, 'T', 't', {'abc'}, _utest=True)
+ Equal = self.assertEqual
+ self.assertEqual(dialog.used_names, {'abc'})
+ dialog.entry.insert(0, 'okay')
+ dialog.button_ok.invoke()
+ self.assertEqual(dialog.result, 'okay')
+ del dialog
+ root.destroy()
+ del root
+
+
+class ModulenameGuiTest(unittest.TestCase):
+
+ @classmethod
+ def setUpClass(cls):
+ requires('gui')
+
+ def test_click_module_name(self):
+ root = Tk()
+ root.withdraw()
+ dialog = query.ModuleName(root, 'T', 't', 'idlelib', _utest=True)
+ self.assertEqual(dialog.text0, 'idlelib')
+ self.assertEqual(dialog.entry.get(), 'idlelib')
+ dialog.button_ok.invoke()
+ self.assertTrue(dialog.result.endswith('__init__.py'))
+ del dialog
+ root.destroy()
+ del root
+
+
+class HelpsourceGuiTest(unittest.TestCase):
+
+ @classmethod
+ def setUpClass(cls):
+ requires('gui')
+
+ def test_click_help_source(self):
+ root = Tk()
+ root.withdraw()
+ dialog = query.HelpSource(root, 'T', menuitem='__test__',
+ filepath=__file__, _utest=True)
+ Equal = self.assertEqual
+ Equal(dialog.entry.get(), '__test__')
+ Equal(dialog.path.get(), __file__)
+ dialog.button_ok.invoke()
+ prefix = "file://" if sys.platform == 'darwin' else ''
+ Equal(dialog.result, ('__test__', prefix + __file__))
+ del dialog
+ root.destroy()
+ del root
+
+
+if __name__ == '__main__':
+ unittest.main(verbosity=2, exit=False)
diff --git a/Lib/idlelib/idle_test/test_widgetredir.py b/Lib/idlelib/idle_test/test_redirector.py
index baa975d..b0385fa 100644
--- a/Lib/idlelib/idle_test/test_widgetredir.py
+++ b/Lib/idlelib/idle_test/test_redirector.py
@@ -1,4 +1,4 @@
-'''Test idlelib.WidgetRedirector.
+'''Test idlelib.redirector.
100% coverage
'''
@@ -6,7 +6,7 @@ from test.support import requires
import unittest
from idlelib.idle_test.mock_idle import Func
from tkinter import Tk, Text, TclError
-from idlelib.WidgetRedirector import WidgetRedirector
+from idlelib.redirector import WidgetRedirector
class InitCloseTest(unittest.TestCase):
@@ -120,6 +120,5 @@ class WidgetRedirectorTest(unittest.TestCase):
self.assertEqual(self.root.call(self.text._w, 'insert', 'boo'), '')
-
if __name__ == '__main__':
unittest.main(verbosity=2)
diff --git a/Lib/idlelib/idle_test/test_replacedialog.py b/Lib/idlelib/idle_test/test_replace.py
index ff44820..9913ed2 100644
--- a/Lib/idlelib/idle_test/test_replacedialog.py
+++ b/Lib/idlelib/idle_test/test_replace.py
@@ -1,4 +1,4 @@
-"""Unittest for idlelib.ReplaceDialog"""
+"""Unittest for idlelib.replace.py"""
from test.support import requires
requires('gui')
@@ -6,8 +6,8 @@ import unittest
from unittest.mock import Mock
from tkinter import Tk, Text
from idlelib.idle_test.mock_tk import Mbox
-import idlelib.SearchEngine as se
-import idlelib.ReplaceDialog as rd
+import idlelib.searchengine as se
+from idlelib.replace import ReplaceDialog
orig_mbox = se.tkMessageBox
showerror = Mbox.showerror
@@ -21,7 +21,8 @@ class ReplaceDialogTest(unittest.TestCase):
cls.root.withdraw()
se.tkMessageBox = Mbox
cls.engine = se.SearchEngine(cls.root)
- cls.dialog = rd.ReplaceDialog(cls.root, cls.engine)
+ cls.dialog = ReplaceDialog(cls.root, cls.engine)
+ cls.dialog.bell = lambda: None
cls.dialog.ok = Mock()
cls.text = Text(cls.root)
cls.text.undo_block_start = Mock()
@@ -70,7 +71,6 @@ class ReplaceDialogTest(unittest.TestCase):
# text found and replaced
pv.set('a')
rv.set('asdf')
- self.dialog.open(self.text)
replace()
equal(text.get('1.8', '1.12'), 'asdf')
@@ -91,7 +91,7 @@ class ReplaceDialogTest(unittest.TestCase):
text.mark_set('insert', 'end')
text.insert('insert', '\nline42:')
before_text = text.get('1.0', 'end')
- pv.set('[a-z][\d]+')
+ pv.set(r'[a-z][\d]+')
replace()
after_text = text.get('1.0', 'end')
equal(before_text, after_text)
@@ -192,7 +192,7 @@ class ReplaceDialogTest(unittest.TestCase):
self.engine.revar.set(True)
before_text = text.get('1.0', 'end')
- pv.set('[a-z][\d]+')
+ pv.set(r'[a-z][\d]+')
rv.set('hello')
replace()
after_text = text.get('1.0', 'end')
@@ -207,7 +207,7 @@ class ReplaceDialogTest(unittest.TestCase):
self.assertIn('error', showerror.title)
self.assertIn('Empty', showerror.message)
- pv.set('[\d')
+ pv.set(r'[\d')
replace()
self.assertIn('error', showerror.title)
self.assertIn('Pattern', showerror.message)
diff --git a/Lib/idlelib/idle_test/test_rstrip.py b/Lib/idlelib/idle_test/test_rstrip.py
index 1c90b93..130e6be 100644
--- a/Lib/idlelib/idle_test/test_rstrip.py
+++ b/Lib/idlelib/idle_test/test_rstrip.py
@@ -1,5 +1,5 @@
import unittest
-import idlelib.RstripExtension as rs
+import idlelib.rstrip as rs
from idlelib.idle_test.mock_idle import Editor
class rstripTest(unittest.TestCase):
@@ -21,7 +21,7 @@ class rstripTest(unittest.TestCase):
def test_rstrip_multiple(self):
editor = Editor()
# Uncomment following to verify that test passes with real widgets.
-## from idlelib.EditorWindow import EditorWindow as Editor
+## from idlelib.editor import EditorWindow as Editor
## from tkinter import Tk
## editor = Editor(root=Tk())
text = editor.text
diff --git a/Lib/idlelib/idle_test/test_scrolledlist.py b/Lib/idlelib/idle_test/test_scrolledlist.py
new file mode 100644
index 0000000..56aabfe
--- /dev/null
+++ b/Lib/idlelib/idle_test/test_scrolledlist.py
@@ -0,0 +1,29 @@
+''' Test idlelib.scrolledlist.
+
+Coverage: 39%
+'''
+from idlelib import scrolledlist
+from test.support import requires
+requires('gui')
+import unittest
+from tkinter import Tk
+
+
+class ScrolledListTest(unittest.TestCase):
+
+ @classmethod
+ def setUpClass(cls):
+ cls.root = Tk()
+
+ @classmethod
+ def tearDownClass(cls):
+ cls.root.destroy()
+ del cls.root
+
+
+ def test_init(self):
+ scrolledlist.ScrolledList(self.root)
+
+
+if __name__ == '__main__':
+ unittest.main(verbosity=2)
diff --git a/Lib/idlelib/idle_test/test_searchdialog.py b/Lib/idlelib/idle_test/test_search.py
index 190c866..80fa93a 100644
--- a/Lib/idlelib/idle_test/test_searchdialog.py
+++ b/Lib/idlelib/idle_test/test_search.py
@@ -1,4 +1,4 @@
-"""Test SearchDialog class in SearchDialogue.py"""
+"""Test SearchDialog class in idlelib.search.py"""
# Does not currently test the event handler wrappers.
# A usage test should simulate clicks and check hilighting.
@@ -11,8 +11,8 @@ requires('gui')
import unittest
import tkinter as tk
from tkinter import BooleanVar
-import idlelib.SearchEngine as se
-import idlelib.SearchDialog as sd
+import idlelib.searchengine as se
+import idlelib.search as sd
class SearchDialogTest(unittest.TestCase):
@@ -29,6 +29,7 @@ class SearchDialogTest(unittest.TestCase):
def setUp(self):
self.engine = se.SearchEngine(self.root)
self.dialog = sd.SearchDialog(self.root, self.engine)
+ self.dialog.bell = lambda: None
self.text = tk.Text(self.root)
self.text.insert('1.0', 'Hello World!')
@@ -38,6 +39,7 @@ class SearchDialogTest(unittest.TestCase):
self.engine.setpat('')
self.assertFalse(self.dialog.find_again(text))
+ self.dialog.bell = lambda: None
self.engine.setpat('Hello')
self.assertTrue(self.dialog.find_again(text))
diff --git a/Lib/idlelib/idle_test/test_searchdialogbase.py b/Lib/idlelib/idle_test/test_searchbase.py
index 8036b91..d769fa2 100644
--- a/Lib/idlelib/idle_test/test_searchdialogbase.py
+++ b/Lib/idlelib/idle_test/test_searchbase.py
@@ -1,14 +1,13 @@
-'''Unittests for idlelib/SearchDialogBase.py
+'''tests idlelib.searchbase.
Coverage: 99%. The only thing not covered is inconsequential --
testing skipping of suite when self.needwrapbutton is false.
-
'''
import unittest
from test.support import requires
from tkinter import Tk, Toplevel, Frame ##, BooleanVar, StringVar
-from idlelib import SearchEngine as se
-from idlelib import SearchDialogBase as sdb
+from idlelib import searchengine as se
+from idlelib import searchbase as sdb
from idlelib.idle_test.mock_idle import Func
## from idlelib.idle_test.mock_tk import Var
@@ -74,7 +73,7 @@ class SearchDialogBaseTest(unittest.TestCase):
def test_make_entry(self):
equal = self.assertEqual
self.dialog.row = 0
- self.dialog.top = Toplevel(self.root)
+ self.dialog.top = self.root
entry, label = self.dialog.make_entry("Test:", 'hello')
equal(label['text'], 'Test:')
@@ -87,6 +86,7 @@ class SearchDialogBaseTest(unittest.TestCase):
equal(self.dialog.row, 1)
def test_create_entries(self):
+ self.dialog.top = self.root
self.dialog.row = 0
self.engine.setpat('hello')
self.dialog.create_entries()
@@ -94,7 +94,7 @@ class SearchDialogBaseTest(unittest.TestCase):
def test_make_frame(self):
self.dialog.row = 0
- self.dialog.top = Toplevel(self.root)
+ self.dialog.top = self.root
frame, label = self.dialog.make_frame()
self.assertEqual(label, '')
self.assertIsInstance(frame, Frame)
@@ -104,7 +104,7 @@ class SearchDialogBaseTest(unittest.TestCase):
self.assertIsInstance(frame, Frame)
def btn_test_setup(self, meth):
- self.dialog.top = Toplevel(self.root)
+ self.dialog.top = self.root
self.dialog.row = 0
return meth()
@@ -119,11 +119,6 @@ class SearchDialogBaseTest(unittest.TestCase):
var, label = spec
self.assertEqual(button['text'], label)
self.assertEqual(var.get(), state)
- if state == 1:
- button.deselect()
- else:
- button.select()
- self.assertEqual(var.get(), 1 - state)
def test_create_other_buttons(self):
for state in (False, True):
@@ -139,18 +134,15 @@ class SearchDialogBaseTest(unittest.TestCase):
# hit other button, then this one
# indexes depend on button order
self.assertEqual(var.get(), state)
- buttons[val].select()
- self.assertEqual(var.get(), 1 - state)
- buttons[1-val].select()
- self.assertEqual(var.get(), state)
def test_make_button(self):
- self.dialog.top = Toplevel(self.root)
+ self.dialog.top = self.root
self.dialog.buttonframe = Frame(self.dialog.top)
btn = self.dialog.make_button('Test', self.dialog.close)
self.assertEqual(btn['text'], 'Test')
def test_create_command_buttons(self):
+ self.dialog.top = self.root
self.dialog.create_command_buttons()
# Look for close button command in buttonframe
closebuttoncommand = ''
@@ -160,6 +152,5 @@ class SearchDialogBaseTest(unittest.TestCase):
self.assertIn('close', closebuttoncommand)
-
if __name__ == '__main__':
unittest.main(verbosity=2, exit=2)
diff --git a/Lib/idlelib/idle_test/test_searchengine.py b/Lib/idlelib/idle_test/test_searchengine.py
index edbd558..b3aa8eb 100644
--- a/Lib/idlelib/idle_test/test_searchengine.py
+++ b/Lib/idlelib/idle_test/test_searchengine.py
@@ -1,4 +1,4 @@
-'''Test functions and SearchEngine class in SearchEngine.py.'''
+'''Test functions and SearchEngine class in idlelib.searchengine.py.'''
# With mock replacements, the module does not use any gui widgets.
# The use of tk.Text is avoided (for now, until mock Text is improved)
@@ -10,7 +10,7 @@ import unittest
# from test.support import requires
from tkinter import BooleanVar, StringVar, TclError # ,Tk, Text
import tkinter.messagebox as tkMessageBox
-from idlelib import SearchEngine as se
+from idlelib import searchengine as se
from idlelib.idle_test.mock_tk import Var, Mbox
from idlelib.idle_test.mock_tk import Text as mockText
@@ -139,10 +139,10 @@ class SearchEngineTest(unittest.TestCase):
def test_setcookedpat(self):
engine = self.engine
- engine.setcookedpat('\s')
- self.assertEqual(engine.getpat(), '\s')
+ engine.setcookedpat(r'\s')
+ self.assertEqual(engine.getpat(), r'\s')
engine.revar.set(1)
- engine.setcookedpat('\s')
+ engine.setcookedpat(r'\s')
self.assertEqual(engine.getpat(), r'\\s')
def test_getcookedpat(self):
@@ -156,10 +156,10 @@ class SearchEngineTest(unittest.TestCase):
Equal(engine.getcookedpat(), r'\bhello\b')
engine.wordvar.set(False)
- engine.setpat('\s')
+ engine.setpat(r'\s')
Equal(engine.getcookedpat(), r'\\s')
engine.revar.set(True)
- Equal(engine.getcookedpat(), '\s')
+ Equal(engine.getcookedpat(), r'\s')
def test_getprog(self):
engine = self.engine
@@ -282,7 +282,7 @@ class ForwardBackwardTest(unittest.TestCase):
cls.pat = re.compile('target')
cls.res = (2, (10, 16)) # line, slice indexes of 'target'
cls.failpat = re.compile('xyz') # not in text
- cls.emptypat = re.compile('\w*') # empty match possible
+ cls.emptypat = re.compile(r'\w*') # empty match possible
def make_search(self, func):
def search(pat, line, col, wrap, ok=0):
diff --git a/Lib/idlelib/idle_test/test_text.py b/Lib/idlelib/idle_test/test_text.py
index 7e823df..a5ba7bb 100644
--- a/Lib/idlelib/idle_test/test_text.py
+++ b/Lib/idlelib/idle_test/test_text.py
@@ -1,17 +1,19 @@
-# Test mock_tk.Text class against tkinter.Text class by running same tests with both.
+''' Test mock_tk.Text class against tkinter.Text class
+
+Run same tests with both by creating a mixin class.
+'''
import unittest
from test.support import requires
-
from _tkinter import TclError
class TextTest(object):
+ "Define items common to both sets of tests."
- hw = 'hello\nworld' # usual initial insert after initialization
+ hw = 'hello\nworld' # Several tests insert this after after initialization.
hwn = hw+'\n' # \n present at initialization, before insert
- Text = None
- def setUp(self):
- self.text = self.Text()
+ # setUpClass defines cls.Text and maybe cls.root.
+ # setUp defines self.text from Text and maybe root.
def test_init(self):
self.assertEqual(self.text.get('1.0'), '\n')
@@ -196,6 +198,10 @@ class MockTextTest(TextTest, unittest.TestCase):
from idlelib.idle_test.mock_tk import Text
cls.Text = Text
+ def setUp(self):
+ self.text = self.Text()
+
+
def test_decode(self):
# test endflags (-1, 0) not tested by test_index (which uses +1)
decode = self.text._decode
@@ -222,6 +228,9 @@ class TkTextTest(TextTest, unittest.TestCase):
cls.root.destroy()
del cls.root
+ def setUp(self):
+ self.text = self.Text(self.root)
+
if __name__ == '__main__':
unittest.main(verbosity=2, exit=False)
diff --git a/Lib/idlelib/idle_test/test_textview.py b/Lib/idlelib/idle_test/test_textview.py
index 5d2e600..f018f5e 100644
--- a/Lib/idlelib/idle_test/test_textview.py
+++ b/Lib/idlelib/idle_test/test_textview.py
@@ -1,21 +1,21 @@
-'''Test idlelib.textView.
+'''Test idlelib.textview.
Since all methods and functions create (or destroy) a TextViewer, which
is a widget containing multiple widgets, all tests must be gui tests.
Using mock Text would not change this. Other mocks are used to retrieve
information about calls.
-The coverage is essentially 100%.
+Coverage: 94%.
'''
+from idlelib import textview as tv
from test.support import requires
requires('gui')
import unittest
import os
from tkinter import Tk
-from idlelib import textView as tv
from idlelib.idle_test.mock_idle import Func
-from idlelib.idle_test.mock_tk import Mbox
+from idlelib.idle_test.mock_tk import Mbox_func
def setUpModule():
global root
@@ -64,17 +64,17 @@ class TextViewTest(unittest.TestCase):
view.destroy()
-class textviewTest(unittest.TestCase):
+class ViewFunctionTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
- cls.orig_mbox = tv.tkMessageBox
- tv.tkMessageBox = Mbox
+ cls.orig_error = tv.showerror
+ tv.showerror = Mbox_func()
@classmethod
def tearDownClass(cls):
- tv.tkMessageBox = cls.orig_mbox
- del cls.orig_mbox
+ tv.showerror = cls.orig_error
+ del cls.orig_error
def test_view_text(self):
# If modal True, get tk error 'can't invoke "event" command'.
@@ -90,7 +90,7 @@ class textviewTest(unittest.TestCase):
self.assertIn('Test', view.textView.get('1.0', '1.end'))
view.Ok()
- # Mock messagebox will be used and view_file will not return anything
+ # Mock showerror will be used; view_file will return None.
testfile = os.path.join(test_dir, '../notthere.py')
view = tv.view_file(root, 'Title', testfile, modal=False)
self.assertIsNone(view)
diff --git a/Lib/idlelib/idle_test/test_tree.py b/Lib/idlelib/idle_test/test_tree.py
new file mode 100644
index 0000000..09ba964
--- /dev/null
+++ b/Lib/idlelib/idle_test/test_tree.py
@@ -0,0 +1,36 @@
+''' Test idlelib.tree.
+
+Coverage: 56%
+'''
+from idlelib import tree
+from test.support import requires
+requires('gui')
+import os
+import unittest
+from tkinter import Tk
+
+
+class TreeTest(unittest.TestCase):
+
+ @classmethod
+ def setUpClass(cls):
+ cls.root = Tk()
+ cls.root.withdraw()
+
+ @classmethod
+ def tearDownClass(cls):
+ cls.root.destroy()
+ del cls.root
+
+ def test_init(self):
+ # Start with code slightly adapted from htest.
+ sc = tree.ScrolledCanvas(
+ self.root, bg="white", highlightthickness=0, takefocus=1)
+ sc.frame.pack(expand=1, fill="both", side='left')
+ item = tree.FileTreeItem(tree.ICONDIR)
+ node = tree.TreeNode(sc.canvas, None, item)
+ node.expand()
+
+
+if __name__ == '__main__':
+ unittest.main(verbosity=2)
diff --git a/Lib/idlelib/idle_test/test_undodelegator.py b/Lib/idlelib/idle_test/test_undo.py
index 2b83c99..e872927 100644
--- a/Lib/idlelib/idle_test/test_undodelegator.py
+++ b/Lib/idlelib/idle_test/test_undo.py
@@ -1,4 +1,4 @@
-"""Unittest for UndoDelegator in idlelib.UndoDelegator.
+"""Unittest for UndoDelegator in idlelib.undo.py.
Coverage about 80% (retest).
"""
@@ -8,8 +8,8 @@ requires('gui')
import unittest
from unittest.mock import Mock
from tkinter import Text, Tk
-from idlelib.UndoDelegator import UndoDelegator
-from idlelib.Percolator import Percolator
+from idlelib.undo import UndoDelegator
+from idlelib.percolator import Percolator
class UndoDelegatorTest(unittest.TestCase):
@@ -29,8 +29,8 @@ class UndoDelegatorTest(unittest.TestCase):
def setUp(self):
self.delegator = UndoDelegator()
+ self.delegator.bell = Mock()
self.percolator.insertfilter(self.delegator)
- self.delegator.bell = Mock(wraps=self.delegator.bell)
def tearDown(self):
self.percolator.removefilter(self.delegator)
diff --git a/Lib/idlelib/idle_test/test_warning.py b/Lib/idlelib/idle_test/test_warning.py
index 18627dd..f3269f1 100644
--- a/Lib/idlelib/idle_test/test_warning.py
+++ b/Lib/idlelib/idle_test/test_warning.py
@@ -1,4 +1,4 @@
-'''Test warnings replacement in PyShell.py and run.py.
+'''Test warnings replacement in pyshell.py and run.py.
This file could be expanded to include traceback overrides
(in same two modules). If so, change name.
@@ -17,9 +17,9 @@ showwarning = warnings.showwarning
running_in_idle = 'idle' in showwarning.__name__
from idlelib import run
-from idlelib import PyShell as shell
+from idlelib import pyshell as shell
-# The following was generated from PyShell.idle_formatwarning
+# The following was generated from pyshell.idle_formatwarning
# and checked as matching expectation.
idlemsg = '''
Warning (from warnings module):
diff --git a/Lib/idlelib/idlever.py b/Lib/idlelib/idlever.py
deleted file mode 100644
index 3e9f69a..0000000
--- a/Lib/idlelib/idlever.py
+++ /dev/null
@@ -1,12 +0,0 @@
-"""
-The separate Idle version was eliminated years ago;
-idlelib.idlever is no longer used by Idle
-and will be removed in 3.6 or later. Use
- from sys import version
- IDLE_VERSION = version[:version.index(' ')]
-"""
-# Kept for now only for possible existing extension use
-import warnings as w
-w.warn(__doc__, DeprecationWarning, stacklevel=2)
-from sys import version
-IDLE_VERSION = version[:version.index(' ')]
diff --git a/Lib/idlelib/IOBinding.py b/Lib/idlelib/iomenu.py
index 84f39a2..3414c7b 100644
--- a/Lib/idlelib/IOBinding.py
+++ b/Lib/idlelib/iomenu.py
@@ -10,57 +10,60 @@ import tkinter.filedialog as tkFileDialog
import tkinter.messagebox as tkMessageBox
from tkinter.simpledialog import askstring
-from idlelib.configHandler import idleConf
+import idlelib
+from idlelib.config import idleConf
-
-# Try setting the locale, so that we can find out
-# what encoding to use
-try:
- import locale
- locale.setlocale(locale.LC_CTYPE, "")
-except (ImportError, locale.Error):
- pass
-
-# Encoding for file names
-filesystemencoding = sys.getfilesystemencoding() ### currently unused
-
-locale_encoding = 'ascii'
-if sys.platform == 'win32':
- # On Windows, we could use "mbcs". However, to give the user
- # a portable encoding name, we need to find the code page
- try:
- locale_encoding = locale.getdefaultlocale()[1]
- codecs.lookup(locale_encoding)
- except LookupError:
- pass
+if idlelib.testing: # Set True by test.test_idle to avoid setlocale.
+ encoding = 'utf-8'
else:
+ # Try setting the locale, so that we can find out
+ # what encoding to use
try:
- # Different things can fail here: the locale module may not be
- # loaded, it may not offer nl_langinfo, or CODESET, or the
- # resulting codeset may be unknown to Python. We ignore all
- # these problems, falling back to ASCII
- locale_encoding = locale.nl_langinfo(locale.CODESET)
- if locale_encoding is None or locale_encoding is '':
- # situation occurs on Mac OS X
- locale_encoding = 'ascii'
- codecs.lookup(locale_encoding)
- except (NameError, AttributeError, LookupError):
- # Try getdefaultlocale: it parses environment variables,
- # which may give a clue. Unfortunately, getdefaultlocale has
- # bugs that can cause ValueError.
+ import locale
+ locale.setlocale(locale.LC_CTYPE, "")
+ except (ImportError, locale.Error):
+ pass
+
+ locale_decode = 'ascii'
+ if sys.platform == 'win32':
+ # On Windows, we could use "mbcs". However, to give the user
+ # a portable encoding name, we need to find the code page
try:
locale_encoding = locale.getdefaultlocale()[1]
+ codecs.lookup(locale_encoding)
+ except LookupError:
+ pass
+ else:
+ try:
+ # Different things can fail here: the locale module may not be
+ # loaded, it may not offer nl_langinfo, or CODESET, or the
+ # resulting codeset may be unknown to Python. We ignore all
+ # these problems, falling back to ASCII
+ locale_encoding = locale.nl_langinfo(locale.CODESET)
if locale_encoding is None or locale_encoding is '':
# situation occurs on Mac OS X
locale_encoding = 'ascii'
codecs.lookup(locale_encoding)
- except (ValueError, LookupError):
- pass
+ except (NameError, AttributeError, LookupError):
+ # Try getdefaultlocale: it parses environment variables,
+ # which may give a clue. Unfortunately, getdefaultlocale has
+ # bugs that can cause ValueError.
+ try:
+ locale_encoding = locale.getdefaultlocale()[1]
+ if locale_encoding is None or locale_encoding is '':
+ # situation occurs on Mac OS X
+ locale_encoding = 'ascii'
+ codecs.lookup(locale_encoding)
+ except (ValueError, LookupError):
+ pass
-locale_encoding = locale_encoding.lower()
+ locale_encoding = locale_encoding.lower()
-encoding = locale_encoding ### KBK 07Sep07 This is used all over IDLE, check!
- ### 'encoding' is used below in encode(), check!
+ encoding = locale_encoding
+ # Encoding is used in multiple files; locale_encoding nowhere.
+ # The only use of 'encoding' below is in _decode as initial value
+ # of deprecated block asking user for encoding.
+ # Perhaps use elsewhere should be reviewed.
coding_re = re.compile(r'^[ \t\f]*#.*?coding[:=][ \t]*([-\w.]+)', re.ASCII)
blank_re = re.compile(r'^[ \t\f]*(?:[#\r\n]|$)', re.ASCII)
@@ -107,6 +110,9 @@ def coding_spec(data):
class IOBinding:
+# One instance per editor Window so methods know which to save, close.
+# Open returns focus to self.editwin if aborted.
+# EditorWindow.open_module, others, belong here.
def __init__(self, editwin):
self.editwin = editwin
@@ -301,7 +307,7 @@ class IOBinding:
"The file's encoding is invalid for Python 3.x.\n"
"IDLE will convert it to UTF-8.\n"
"What is the current encoding of the file?",
- initialvalue = locale_encoding,
+ initialvalue = encoding,
parent = self.editwin.text)
if enc:
@@ -529,8 +535,8 @@ def _io_binding(parent): # htest #
root = Toplevel(parent)
root.title("Test IOBinding")
- width, height, x, y = list(map(int, re.split('[x+]', parent.geometry())))
- root.geometry("+%d+%d"%(x, y + 150))
+ x, y = map(int, parent.geometry().split('+')[1:])
+ root.geometry("+%d+%d" % (x, y + 175))
class MyEditWin:
def __init__(self, text):
self.text = text
@@ -561,5 +567,8 @@ def _io_binding(parent): # htest #
IOBinding(editwin)
if __name__ == "__main__":
+ import unittest
+ unittest.main('idlelib.idle_test.test_iomenu', verbosity=2, exit=False)
+
from idlelib.idle_test.htest import run
run(_io_binding)
diff --git a/Lib/idlelib/macosxSupport.py b/Lib/idlelib/macosx.py
index b96bae1..c225dd9 100644
--- a/Lib/idlelib/macosxSupport.py
+++ b/Lib/idlelib/macosx.py
@@ -1,30 +1,25 @@
"""
A number of functions that enhance IDLE on Mac OSX.
"""
-import sys
-import tkinter
-from os import path
+from sys import platform # Used in _init_tk_type, changed by test.
import warnings
-def runningAsOSXApp():
- warnings.warn("runningAsOSXApp() is deprecated, use isAquaTk()",
- DeprecationWarning, stacklevel=2)
- return isAquaTk()
+import tkinter
+
-def isCarbonAquaTk(root):
- warnings.warn("isCarbonAquaTk(root) is deprecated, use isCarbonTk()",
- DeprecationWarning, stacklevel=2)
- return isCarbonTk()
+## Define functions that query the Mac graphics type.
+## _tk_type and its initializer are private to this section.
_tk_type = None
-def _initializeTkVariantTests(root):
+def _init_tk_type():
"""
Initializes OS X Tk variant values for
isAquaTk(), isCarbonTk(), isCocoaTk(), and isXQuartz().
"""
global _tk_type
- if sys.platform == 'darwin':
+ if platform == 'darwin':
+ root = tkinter.Tk()
ws = root.tk.call('tk', 'windowingsystem')
if 'x11' in ws:
_tk_type = "xquartz"
@@ -34,6 +29,7 @@ def _initializeTkVariantTests(root):
_tk_type = "cocoa"
else:
_tk_type = "carbon"
+ root.destroy()
else:
_tk_type = "other"
@@ -41,7 +37,8 @@ def isAquaTk():
"""
Returns True if IDLE is using a native OS X Tk (Cocoa or Carbon).
"""
- assert _tk_type is not None
+ if not _tk_type:
+ _init_tk_type()
return _tk_type == "cocoa" or _tk_type == "carbon"
def isCarbonTk():
@@ -49,23 +46,27 @@ def isCarbonTk():
Returns True if IDLE is using a Carbon Aqua Tk (instead of the
newer Cocoa Aqua Tk).
"""
- assert _tk_type is not None
+ if not _tk_type:
+ _init_tk_type()
return _tk_type == "carbon"
def isCocoaTk():
"""
Returns True if IDLE is using a Cocoa Aqua Tk.
"""
- assert _tk_type is not None
+ if not _tk_type:
+ _init_tk_type()
return _tk_type == "cocoa"
def isXQuartz():
"""
Returns True if IDLE is using an OS X X11 Tk.
"""
- assert _tk_type is not None
+ if not _tk_type:
+ _init_tk_type()
return _tk_type == "xquartz"
+
def tkVersionWarning(root):
"""
Returns a string warning message if the Tk version in use appears to
@@ -86,6 +87,9 @@ def tkVersionWarning(root):
else:
return False
+
+## Fix the menu and related functions.
+
def addOpenEventSupport(root, flist):
"""
This ensures that the application will respond to open AppleEvents, which
@@ -124,23 +128,23 @@ def overrideRootMenu(root, flist):
# Due to a (mis-)feature of TkAqua the user will also see an empty Help
# menu.
from tkinter import Menu
- from idlelib import Bindings
- from idlelib import WindowList
+ from idlelib import mainmenu
+ from idlelib import windows
- closeItem = Bindings.menudefs[0][1][-2]
+ closeItem = mainmenu.menudefs[0][1][-2]
# Remove the last 3 items of the file menu: a separator, close window and
# quit. Close window will be reinserted just above the save item, where
# it should be according to the HIG. Quit is in the application menu.
- del Bindings.menudefs[0][1][-3:]
- Bindings.menudefs[0][1].insert(6, closeItem)
+ del mainmenu.menudefs[0][1][-3:]
+ mainmenu.menudefs[0][1].insert(6, closeItem)
# Remove the 'About' entry from the help menu, it is in the application
# menu
- del Bindings.menudefs[-1][1][0:2]
+ del mainmenu.menudefs[-1][1][0:2]
# Remove the 'Configure Idle' entry from the options menu, it is in the
# application menu as 'Preferences'
- del Bindings.menudefs[-2][1][0]
+ del mainmenu.menudefs[-2][1][0]
menubar = Menu(root)
root.configure(menu=menubar)
menudict = {}
@@ -155,30 +159,30 @@ def overrideRootMenu(root, flist):
if end > 0:
menu.delete(0, end)
- WindowList.add_windows_to_menu(menu)
- WindowList.register_callback(postwindowsmenu)
+ windows.add_windows_to_menu(menu)
+ windows.register_callback(postwindowsmenu)
def about_dialog(event=None):
"Handle Help 'About IDLE' event."
- # Synchronize with EditorWindow.EditorWindow.about_dialog.
- from idlelib import aboutDialog
- aboutDialog.AboutDialog(root, 'About IDLE')
+ # Synchronize with editor.EditorWindow.about_dialog.
+ from idlelib import help_about
+ help_about.AboutDialog(root, 'About IDLE')
def config_dialog(event=None):
"Handle Options 'Configure IDLE' event."
- # Synchronize with EditorWindow.EditorWindow.config_dialog.
- from idlelib import configDialog
+ # Synchronize with editor.EditorWindow.config_dialog.
+ from idlelib import configdialog
# Ensure that the root object has an instance_dict attribute,
# mirrors code in EditorWindow (although that sets the attribute
# on an EditorWindow instance that is then passed as the first
# argument to ConfigDialog)
root.instance_dict = flist.inversedict
- configDialog.ConfigDialog(root, 'Settings')
+ configdialog.ConfigDialog(root, 'Settings')
def help_dialog(event=None):
"Handle Help 'IDLE Help' event."
- # Synchronize with EditorWindow.EditorWindow.help_dialog.
+ # Synchronize with editor.EditorWindow.help_dialog.
from idlelib import help
help.show_idlehelp(root)
@@ -198,29 +202,33 @@ def overrideRootMenu(root, flist):
menudict['application'] = menu = Menu(menubar, name='apple',
tearoff=0)
menubar.add_cascade(label='IDLE', menu=menu)
- Bindings.menudefs.insert(0,
+ mainmenu.menudefs.insert(0,
('application', [
('About IDLE', '<<about-idle>>'),
None,
]))
- tkversion = root.tk.eval('info patchlevel')
- if tuple(map(int, tkversion.split('.'))) < (8, 4, 14):
- # for earlier AquaTk versions, supply a Preferences menu item
- Bindings.menudefs[0][1].append(
- ('_Preferences....', '<<open-config-dialog>>'),
- )
if isCocoaTk():
# replace default About dialog with About IDLE one
root.createcommand('tkAboutDialog', about_dialog)
# replace default "Help" item in Help menu
root.createcommand('::tk::mac::ShowHelp', help_dialog)
# remove redundant "IDLE Help" from menu
- del Bindings.menudefs[-1][1][0]
+ del mainmenu.menudefs[-1][1][0]
+
+def fixb2context(root):
+ '''Removed bad AquaTk Button-2 (right) and Paste bindings.
+
+ They prevent context menu access and seem to be gone in AquaTk8.6.
+ See issue #24801.
+ '''
+ root.unbind_class('Text', '<B2>')
+ root.unbind_class('Text', '<B2-Motion>')
+ root.unbind_class('Text', '<<PasteSelection>>')
def setupApp(root, flist):
"""
Perform initial OS X customizations if needed.
- Called from PyShell.main() after initial calls to Tk()
+ Called from pyshell.main() after initial calls to Tk()
There are currently three major versions of Tk in use on OS X:
1. Aqua Cocoa Tk (native default since OS X 10.6)
@@ -233,8 +241,13 @@ def setupApp(root, flist):
isAquaTk(), isCarbonTk(), isCocoaTk(), isXQuartz() functions which
are initialized here as well.
"""
- _initializeTkVariantTests(root)
if isAquaTk():
hideTkConsole(root)
overrideRootMenu(root, flist)
addOpenEventSupport(root, flist)
+ fixb2context(root)
+
+
+if __name__ == '__main__':
+ from unittest import main
+ main('idlelib.idle_test.test_macosx', verbosity=2)
diff --git a/Lib/idlelib/Bindings.py b/Lib/idlelib/mainmenu.py
index e19a279..65345cd 100644
--- a/Lib/idlelib/Bindings.py
+++ b/Lib/idlelib/mainmenu.py
@@ -10,9 +10,9 @@ windows.
"""
from importlib.util import find_spec
-from idlelib.configHandler import idleConf
+from idlelib.config import idleConf
-# Warning: menudefs is altered in macosxSupport.overrideRootMenu()
+# Warning: menudefs is altered in macosx.overrideRootMenu()
# after it is determined that an OS X Aqua Tk is in use,
# which cannot be done until after Tk() is first called.
# Do not alter the 'file', 'options', or 'help' cascades here
diff --git a/Lib/idlelib/MultiCall.py b/Lib/idlelib/multicall.py
index 8462854..b74fed4 100644
--- a/Lib/idlelib/MultiCall.py
+++ b/Lib/idlelib/multicall.py
@@ -28,9 +28,9 @@ The order by which events are called is defined by these rules:
unless this conflicts with the first rule.
Each function will be called at most once for each event.
"""
-
-import sys
import re
+import sys
+
import tkinter
# the event type constants, which define the meaning of mc_type
@@ -414,12 +414,12 @@ def MultiCallCreator(widget):
return MultiCall
-def _multi_call(parent):
- root = tkinter.Tk()
- root.title("Test MultiCall")
- width, height, x, y = list(map(int, re.split('[x+]', parent.geometry())))
- root.geometry("+%d+%d"%(x, y + 150))
- text = MultiCallCreator(tkinter.Text)(root)
+def _multi_call(parent): # htest #
+ top = tkinter.Toplevel(parent)
+ top.title("Test MultiCall")
+ x, y = map(int, parent.geometry().split('+')[1:])
+ top.geometry("+%d+%d" % (x, y + 175))
+ text = MultiCallCreator(tkinter.Text)(top)
text.pack()
def bindseq(seq, n=[0]):
def handler(event):
@@ -439,7 +439,6 @@ def _multi_call(parent):
bindseq("<FocusOut>")
bindseq("<Enter>")
bindseq("<Leave>")
- root.mainloop()
if __name__ == "__main__":
from idlelib.idle_test.htest import run
diff --git a/Lib/idlelib/OutputWindow.py b/Lib/idlelib/outwin.py
index e614f9b..f6d2915 100644
--- a/Lib/idlelib/OutputWindow.py
+++ b/Lib/idlelib/outwin.py
@@ -1,8 +1,11 @@
-from tkinter import *
-from idlelib.EditorWindow import EditorWindow
import re
+
+from tkinter import *
import tkinter.messagebox as tkMessageBox
-from idlelib import IOBinding
+
+from idlelib.editor import EditorWindow
+from idlelib import iomenu
+
class OutputWindow(EditorWindow):
@@ -36,7 +39,7 @@ class OutputWindow(EditorWindow):
def write(self, s, tags=(), mark="insert"):
if isinstance(s, (bytes, bytes)):
- s = s.decode(IOBinding.encoding, "replace")
+ s = s.decode(iomenu.encoding, "replace")
self.text.insert(mark, s, tags)
self.text.see(mark)
self.text.update()
diff --git a/Lib/idlelib/FormatParagraph.py b/Lib/idlelib/paragraph.py
index 7a9d185..f11bdae 100644
--- a/Lib/idlelib/FormatParagraph.py
+++ b/Lib/idlelib/paragraph.py
@@ -14,9 +14,10 @@ Known problems with comment reformatting:
spaces, they will not be considered part of the same block.
* Fancy comments, like this bulleted list, aren't handled :-)
"""
-
import re
-from idlelib.configHandler import idleConf
+
+from idlelib.config import idleConf
+
class FormatParagraph:
@@ -129,7 +130,7 @@ def reformat_paragraph(data, limit):
partial = indent1
while i < n and not is_all_white(lines[i]):
# XXX Should take double space after period (etc.) into account
- words = re.split("(\s+)", lines[i])
+ words = re.split(r"(\s+)", lines[i])
for j in range(0, len(words), 2):
word = words[j]
if not word:
@@ -189,7 +190,8 @@ def get_comment_header(line):
if m is None: return ""
return m.group(1)
+
if __name__ == "__main__":
import unittest
- unittest.main('idlelib.idle_test.test_formatparagraph',
+ unittest.main('idlelib.idle_test.test_paragraph',
verbosity=2, exit=False)
diff --git a/Lib/idlelib/ParenMatch.py b/Lib/idlelib/parenmatch.py
index 47e10f3..ccec708 100644
--- a/Lib/idlelib/ParenMatch.py
+++ b/Lib/idlelib/parenmatch.py
@@ -4,9 +4,8 @@ When you hit a right paren, the cursor should move briefly to the left
paren. Paren here is used generically; the matching applies to
parentheses, square brackets, and curly braces.
"""
-
-from idlelib.HyperParser import HyperParser
-from idlelib.configHandler import idleConf
+from idlelib.hyperparser import HyperParser
+from idlelib.config import idleConf
_openers = {')':'(',']':'[','}':'{'}
CHECK_DELAY = 100 # milliseconds
@@ -64,6 +63,7 @@ class ParenMatch:
# and deactivate_restore (which calls event_delete).
editwin.text.bind(self.RESTORE_VIRTUAL_EVENT_NAME,
self.restore_event)
+ self.bell = self.text.bell if self.BELL else lambda: None
self.counter = 0
self.is_restore_active = 0
self.set_style(self.STYLE)
@@ -93,7 +93,7 @@ class ParenMatch:
indices = (HyperParser(self.editwin, "insert")
.get_surrounding_brackets())
if indices is None:
- self.warn_mismatched()
+ self.bell()
return
self.activate_restore()
self.create_tag(indices)
@@ -109,7 +109,7 @@ class ParenMatch:
return
indices = hp.get_surrounding_brackets(_openers[closer], True)
if indices is None:
- self.warn_mismatched()
+ self.bell()
return
self.activate_restore()
self.create_tag(indices)
@@ -124,10 +124,6 @@ class ParenMatch:
if timer_count == self.counter:
self.restore_event()
- def warn_mismatched(self):
- if self.BELL:
- self.text.bell()
-
# any one of the create_tag_XXX methods can be used depending on
# the style
diff --git a/Lib/idlelib/PathBrowser.py b/Lib/idlelib/pathbrowser.py
index 9ab7632..6c19508 100644
--- a/Lib/idlelib/PathBrowser.py
+++ b/Lib/idlelib/pathbrowser.py
@@ -1,10 +1,10 @@
+import importlib.machinery
import os
import sys
-import importlib.machinery
-from idlelib.TreeWidget import TreeItem
-from idlelib.ClassBrowser import ClassBrowser, ModuleBrowserTreeItem
-from idlelib.PyShell import PyShellFileList
+from idlelib.browser import ClassBrowser, ModuleBrowserTreeItem
+from idlelib.pyshell import PyShellFileList
+from idlelib.tree import TreeItem
class PathBrowser(ClassBrowser):
@@ -24,6 +24,7 @@ class PathBrowser(ClassBrowser):
def rootnode(self):
return PathBrowserTreeItem()
+
class PathBrowserTreeItem(TreeItem):
def GetText(self):
@@ -36,6 +37,7 @@ class PathBrowserTreeItem(TreeItem):
sublist.append(item)
return sublist
+
class DirBrowserTreeItem(TreeItem):
def __init__(self, dir, packages=[]):
@@ -95,6 +97,7 @@ class DirBrowserTreeItem(TreeItem):
sorted.sort()
return sorted
+
def _path_browser(parent): # htest #
flist = PyShellFileList(parent)
PathBrowser(flist, _htest=True)
diff --git a/Lib/idlelib/Percolator.py b/Lib/idlelib/percolator.py
index b8be2aa..d18daf0 100644
--- a/Lib/idlelib/Percolator.py
+++ b/Lib/idlelib/percolator.py
@@ -1,5 +1,5 @@
-from idlelib.WidgetRedirector import WidgetRedirector
-from idlelib.Delegator import Delegator
+from idlelib.delegator import Delegator
+from idlelib.redirector import WidgetRedirector
class Percolator:
@@ -57,7 +57,6 @@ class Percolator:
def _percolator(parent): # htest #
import tkinter as tk
- import re
class Tracer(Delegator):
def __init__(self, name):
@@ -74,8 +73,8 @@ def _percolator(parent): # htest #
box = tk.Toplevel(parent)
box.title("Test Percolator")
- width, height, x, y = list(map(int, re.split('[x+]', parent.geometry())))
- box.geometry("+%d+%d" % (x, y + 150))
+ x, y = map(int, parent.geometry().split('+')[1:])
+ box.geometry("+%d+%d" % (x, y + 175))
text = tk.Text(box)
p = Percolator(text)
pin = p.insertfilter
@@ -89,10 +88,10 @@ def _percolator(parent): # htest #
(pin if var2.get() else pout)(t2)
text.pack()
- var1 = tk.IntVar()
+ var1 = tk.IntVar(parent)
cb1 = tk.Checkbutton(box, text="Tracer1", command=toggle1, variable=var1)
cb1.pack()
- var2 = tk.IntVar()
+ var2 = tk.IntVar(parent)
cb2 = tk.Checkbutton(box, text="Tracer2", command=toggle2, variable=var2)
cb2.pack()
diff --git a/Lib/idlelib/PyParse.py b/Lib/idlelib/pyparse.py
index 9ccbb25..6739dfd 100644
--- a/Lib/idlelib/PyParse.py
+++ b/Lib/idlelib/pyparse.py
@@ -1,6 +1,6 @@
+from collections import Mapping
import re
import sys
-from collections import Mapping
# Reason last stmt is continued (or C_NONE if it's not).
(C_NONE, C_BACKSLASH, C_STRING_FIRST_LINE,
diff --git a/Lib/idlelib/PyShell.py b/Lib/idlelib/pyshell.py
index 5dec68e..f3ee391 100755
--- a/Lib/idlelib/PyShell.py
+++ b/Lib/idlelib/pyshell.py
@@ -1,8 +1,27 @@
#! /usr/bin/env python3
+try:
+ from tkinter import *
+except ImportError:
+ print("** IDLE can't import Tkinter.\n"
+ "Your Python may not be configured for Tk. **", file=sys.__stderr__)
+ raise SystemExit(1)
+import tkinter.messagebox as tkMessageBox
+if TkVersion < 8.5:
+ root = Tk() # otherwise create root in main
+ root.withdraw()
+ tkMessageBox.showerror("Idle Cannot Start",
+ "Idle requires tcl/tk 8.5+, not %s." % TkVersion,
+ parent=root)
+ raise SystemExit(1)
+
+from code import InteractiveInterpreter
import getopt
+import io
+import linecache
import os
import os.path
+from platform import python_version, system
import re
import socket
import subprocess
@@ -10,30 +29,20 @@ import sys
import threading
import time
import tokenize
-import io
-
-import linecache
-from code import InteractiveInterpreter
-from platform import python_version, system
-
-try:
- from tkinter import *
-except ImportError:
- print("** IDLE can't import Tkinter.\n"
- "Your Python may not be configured for Tk. **", file=sys.__stderr__)
- sys.exit(1)
-import tkinter.messagebox as tkMessageBox
+import warnings
-from idlelib.EditorWindow import EditorWindow, fixwordbreaks
-from idlelib.FileList import FileList
-from idlelib.ColorDelegator import ColorDelegator
-from idlelib.UndoDelegator import UndoDelegator
-from idlelib.OutputWindow import OutputWindow
-from idlelib.configHandler import idleConf
+from idlelib import testing # bool value
+from idlelib.colorizer import ColorDelegator
+from idlelib.config import idleConf
+from idlelib import debugger
+from idlelib import debugger_r
+from idlelib.editor import EditorWindow, fixwordbreaks
+from idlelib.filelist import FileList
+from idlelib import macosx
+from idlelib.outwin import OutputWindow
from idlelib import rpc
-from idlelib import Debugger
-from idlelib import RemoteDebugger
-from idlelib import macosxSupport
+from idlelib.run import idle_formatwarning, PseudoInputFile, PseudoOutputFile
+from idlelib.undo import UndoDelegator
HOST = '127.0.0.1' # python execution server on localhost loopback
PORT = 0 # someday pass in host, port for remote debug capability
@@ -43,20 +52,6 @@ PORT = 0 # someday pass in host, port for remote debug capability
# temporarily redirect the stream to the shell window to display warnings when
# checking user's code.
warning_stream = sys.__stderr__ # None, at least on Windows, if no console.
-import warnings
-
-def idle_formatwarning(message, category, filename, lineno, line=None):
- """Format warnings the IDLE way."""
-
- s = "\nWarning (from warnings module):\n"
- s += ' File \"%s\", line %s\n' % (filename, lineno)
- if line is None:
- line = linecache.getline(filename, lineno)
- line = line.strip()
- if line:
- s += " %s\n" % line
- s += "%s: %s\n" % (category.__name__, message)
- return s
def idle_showwarning(
message, category, filename, lineno, file=None, line=None):
@@ -410,7 +405,7 @@ class ModifiedInterpreter(InteractiveInterpreter):
# run from the IDLE source directory.
del_exitf = idleConf.GetOption('main', 'General', 'delete-exitfunc',
default=False, type='bool')
- if __name__ == 'idlelib.PyShell':
+ if __name__ == 'idlelib.pyshell':
command = "__import__('idlelib.run').run.main(%r)" % (del_exitf,)
else:
command = "__import__('run').main(%r)" % (del_exitf,)
@@ -468,7 +463,7 @@ class ModifiedInterpreter(InteractiveInterpreter):
if debug:
try:
# Only close subprocess debugger, don't unregister gui_adap!
- RemoteDebugger.close_subprocess_debugger(self.rpcclt)
+ debugger_r.close_subprocess_debugger(self.rpcclt)
except:
pass
# Kill subprocess, spawn a new one, accept connection.
@@ -497,7 +492,7 @@ class ModifiedInterpreter(InteractiveInterpreter):
# restart subprocess debugger
if debug:
# Restarted debugger connects to current instance of debug GUI
- RemoteDebugger.restart_subprocess_debugger(self.rpcclt)
+ debugger_r.restart_subprocess_debugger(self.rpcclt)
# reload remote debugger breakpoints for all PyShellEditWindows
debug.load_breakpoints()
self.compile.compiler.flags = self.original_compiler_flags
@@ -578,7 +573,7 @@ class ModifiedInterpreter(InteractiveInterpreter):
if self.tkconsole.getvar("<<toggle-jit-stack-viewer>>"):
self.remote_stack_viewer()
elif how == "ERROR":
- errmsg = "PyShell.ModifiedInterpreter: Subprocess ERROR:\n"
+ errmsg = "pyshell.ModifiedInterpreter: Subprocess ERROR:\n"
print(errmsg, what, file=sys.__stderr__)
print(errmsg, what, file=console)
# we received a response to the currently active seq number:
@@ -613,13 +608,13 @@ class ModifiedInterpreter(InteractiveInterpreter):
return
def remote_stack_viewer(self):
- from idlelib import RemoteObjectBrowser
+ from idlelib import debugobj_r
oid = self.rpcclt.remotequeue("exec", "stackviewer", ("flist",), {})
if oid is None:
self.tkconsole.root.bell()
return
- item = RemoteObjectBrowser.StubObjectTreeItem(self.rpcclt, oid)
- from idlelib.TreeWidget import ScrolledCanvas, TreeNode
+ item = debugobj_r.StubObjectTreeItem(self.rpcclt, oid)
+ from idlelib.tree import ScrolledCanvas, TreeNode
top = Toplevel(self.tkconsole.root)
theme = idleConf.CurrentTheme()
background = idleConf.GetHighlight(theme, 'normal')['background']
@@ -662,9 +657,9 @@ class ModifiedInterpreter(InteractiveInterpreter):
# at the moment, InteractiveInterpreter expects str
assert isinstance(source, str)
#if isinstance(source, str):
- # from idlelib import IOBinding
+ # from idlelib import iomenu
# try:
- # source = source.encode(IOBinding.encoding)
+ # source = source.encode(iomenu.encoding)
# except UnicodeError:
# self.tkconsole.resetoutput()
# self.write("Unsupported characters in input\n")
@@ -850,7 +845,7 @@ class PyShell(OutputWindow):
# New classes
- from idlelib.IdleHistory import History
+ from idlelib.history import History
def __init__(self, flist=None):
if use_subprocess:
@@ -888,11 +883,11 @@ class PyShell(OutputWindow):
self.save_stdout = sys.stdout
self.save_stderr = sys.stderr
self.save_stdin = sys.stdin
- from idlelib import IOBinding
- self.stdin = PseudoInputFile(self, "stdin", IOBinding.encoding)
- self.stdout = PseudoOutputFile(self, "stdout", IOBinding.encoding)
- self.stderr = PseudoOutputFile(self, "stderr", IOBinding.encoding)
- self.console = PseudoOutputFile(self, "console", IOBinding.encoding)
+ from idlelib import iomenu
+ self.stdin = PseudoInputFile(self, "stdin", iomenu.encoding)
+ self.stdout = PseudoOutputFile(self, "stdout", iomenu.encoding)
+ self.stderr = PseudoOutputFile(self, "stderr", iomenu.encoding)
+ self.console = PseudoOutputFile(self, "console", iomenu.encoding)
if not use_subprocess:
sys.stdout = self.stdout
sys.stderr = self.stderr
@@ -900,7 +895,7 @@ class PyShell(OutputWindow):
try:
# page help() text to shell.
import pydoc # import must be done here to capture i/o rebinding.
- # XXX KBK 27Dec07 use a textView someday, but must work w/o subproc
+ # XXX KBK 27Dec07 use TextViewer someday, but must work w/o subproc
pydoc.pager = pydoc.plainpager
except:
sys.stderr = sys.__stderr__
@@ -954,7 +949,7 @@ class PyShell(OutputWindow):
self.interp.setdebugger(None)
db.close()
if self.interp.rpcclt:
- RemoteDebugger.close_remote_debugger(self.interp.rpcclt)
+ debugger_r.close_remote_debugger(self.interp.rpcclt)
self.resetoutput()
self.console.write("[DEBUG OFF]\n")
sys.ps1 = ">>> "
@@ -963,10 +958,10 @@ class PyShell(OutputWindow):
def open_debugger(self):
if self.interp.rpcclt:
- dbg_gui = RemoteDebugger.start_remote_debugger(self.interp.rpcclt,
+ dbg_gui = debugger_r.start_remote_debugger(self.interp.rpcclt,
self)
else:
- dbg_gui = Debugger.Debugger(self)
+ dbg_gui = debugger.Debugger(self)
self.interp.setdebugger(dbg_gui)
dbg_gui.load_breakpoints()
sys.ps1 = "[DEBUG ON]\n>>> "
@@ -1241,7 +1236,7 @@ class PyShell(OutputWindow):
"(sys.last_traceback is not defined)",
parent=self.text)
return
- from idlelib.StackViewer import StackBrowser
+ from idlelib.stackviewer import StackBrowser
StackBrowser(self.root, self.flist)
def view_restart_mark(self, event=None):
@@ -1309,92 +1304,6 @@ class PyShell(OutputWindow):
return 'disabled'
return super().rmenu_check_paste()
-class PseudoFile(io.TextIOBase):
-
- def __init__(self, shell, tags, encoding=None):
- self.shell = shell
- self.tags = tags
- self._encoding = encoding
-
- @property
- def encoding(self):
- return self._encoding
-
- @property
- def name(self):
- return '<%s>' % self.tags
-
- def isatty(self):
- return True
-
-
-class PseudoOutputFile(PseudoFile):
-
- def writable(self):
- return True
-
- def write(self, s):
- if self.closed:
- raise ValueError("write to closed file")
- if type(s) is not str:
- if not isinstance(s, str):
- raise TypeError('must be str, not ' + type(s).__name__)
- # See issue #19481
- s = str.__str__(s)
- return self.shell.write(s, self.tags)
-
-
-class PseudoInputFile(PseudoFile):
-
- def __init__(self, shell, tags, encoding=None):
- PseudoFile.__init__(self, shell, tags, encoding)
- self._line_buffer = ''
-
- def readable(self):
- return True
-
- def read(self, size=-1):
- if self.closed:
- raise ValueError("read from closed file")
- if size is None:
- size = -1
- elif not isinstance(size, int):
- raise TypeError('must be int, not ' + type(size).__name__)
- result = self._line_buffer
- self._line_buffer = ''
- if size < 0:
- while True:
- line = self.shell.readline()
- if not line: break
- result += line
- else:
- while len(result) < size:
- line = self.shell.readline()
- if not line: break
- result += line
- self._line_buffer = result[size:]
- result = result[:size]
- return result
-
- def readline(self, size=-1):
- if self.closed:
- raise ValueError("read from closed file")
- if size is None:
- size = -1
- elif not isinstance(size, int):
- raise TypeError('must be int, not ' + type(size).__name__)
- line = self._line_buffer or self.shell.readline()
- if size < 0:
- size = len(line)
- eol = line.find('\n', 0, size)
- if eol >= 0:
- size = eol + 1
- self._line_buffer = line[size:]
- return line[:size]
-
- def close(self):
- self.shell.close()
-
def fix_x11_paste(root):
"Make paste replace selection on x11. See issue #5124."
@@ -1540,7 +1449,10 @@ def main():
enable_edit = enable_edit or edit_start
enable_shell = enable_shell or not enable_edit
- # start editor and/or shell windows:
+ # Setup root. Don't break user code run in IDLE process.
+ # Don't change environment when testing.
+ if use_subprocess and not testing:
+ NoDefaultRoot()
root = Tk(className="Idle")
root.withdraw()
@@ -1549,25 +1461,19 @@ def main():
if system() == 'Windows':
iconfile = os.path.join(icondir, 'idle.ico')
root.wm_iconbitmap(default=iconfile)
- elif TkVersion >= 8.5:
+ else:
ext = '.png' if TkVersion >= 8.6 else '.gif'
iconfiles = [os.path.join(icondir, 'idle_%d%s' % (size, ext))
for size in (16, 32, 48)]
- icons = [PhotoImage(file=iconfile) for iconfile in iconfiles]
+ icons = [PhotoImage(master=root, file=iconfile)
+ for iconfile in iconfiles]
root.wm_iconphoto(True, *icons)
+ # start editor and/or shell windows:
fixwordbreaks(root)
fix_x11_paste(root)
flist = PyShellFileList(root)
- macosxSupport.setupApp(root, flist)
-
- if macosxSupport.isAquaTk():
- # There are some screwed up <2> class bindings for text
- # widgets defined in Tk which we need to do away with.
- # See issue #24801.
- root.unbind_class('Text', '<B2>')
- root.unbind_class('Text', '<B2-Motion>')
- root.unbind_class('Text', '<<PasteSelection>>')
+ macosx.setupApp(root, flist)
if enable_edit:
if not (cmd or script):
@@ -1582,7 +1488,7 @@ def main():
shell = flist.open_shell()
if not shell:
return # couldn't open shell
- if macosxSupport.isAquaTk() and flist.dict:
+ if macosx.isAquaTk() and flist.dict:
# On OSX: when the user has double-clicked on a file that causes
# IDLE to be launched the shell window will open just in front of
# the file she wants to see. Lower the interpreter window when
@@ -1616,7 +1522,7 @@ def main():
# check for problematic OS X Tk versions and print a warning
# message in the IDLE shell window; this is less intrusive
# than always opening a separate window.
- tkversionwarning = macosxSupport.tkVersionWarning(root)
+ tkversionwarning = macosx.tkVersionWarning(root)
if tkversionwarning:
shell.interp.runcommand("print('%s')" % tkversionwarning)
@@ -1626,7 +1532,7 @@ def main():
capture_warnings(False)
if __name__ == "__main__":
- sys.modules['PyShell'] = sys.modules['__main__']
+ sys.modules['pyshell'] = sys.modules['__main__']
main()
capture_warnings(False) # Make sure turned off; see issue 18081
diff --git a/Lib/idlelib/query.py b/Lib/idlelib/query.py
new file mode 100644
index 0000000..3b1f1e2
--- /dev/null
+++ b/Lib/idlelib/query.py
@@ -0,0 +1,308 @@
+"""
+Dialogs that query users and verify the answer before accepting.
+Use ttk widgets, limiting use to tcl/tk 8.5+, as in IDLE 3.6+.
+
+Query is the generic base class for a popup dialog.
+The user must either enter a valid answer or close the dialog.
+Entries are validated when <Return> is entered or [Ok] is clicked.
+Entries are ignored when [Cancel] or [X] are clicked.
+The 'return value' is .result set to either a valid answer or None.
+
+Subclass SectionName gets a name for a new config file section.
+Configdialog uses it for new highlight theme and keybinding set names.
+Subclass ModuleName gets a name for File => Open Module.
+Subclass HelpSource gets menu item and path for additions to Help menu.
+"""
+# Query and Section name result from splitting GetCfgSectionNameDialog
+# of configSectionNameDialog.py (temporarily config_sec.py) into
+# generic and specific parts. 3.6 only, July 2016.
+# ModuleName.entry_ok came from editor.EditorWindow.load_module.
+# HelpSource was extracted from configHelpSourceEdit.py (temporarily
+# config_help.py), with darwin code moved from ok to path_ok.
+
+import importlib
+import os
+from sys import executable, platform # Platform is set for one test.
+
+from tkinter import Toplevel, StringVar, W, E, N, S
+from tkinter.ttk import Frame, Button, Entry, Label
+from tkinter import filedialog
+from tkinter.font import Font
+
+class Query(Toplevel):
+ """Base class for getting verified answer from a user.
+
+ For this base class, accept any non-blank string.
+ """
+ def __init__(self, parent, title, message, *, text0='', used_names={},
+ _htest=False, _utest=False):
+ """Create popup, do not return until tk widget destroyed.
+
+ Additional subclass init must be done before calling this
+ unless _utest=True is passed to suppress wait_window().
+
+ title - string, title of popup dialog
+ message - string, informational message to display
+ text0 - initial value for entry
+ used_names - names already in use
+ _htest - bool, change box location when running htest
+ _utest - bool, leave window hidden and not modal
+ """
+ Toplevel.__init__(self, parent)
+ self.withdraw() # Hide while configuring, especially geometry.
+ self.parent = parent
+ self.title(title)
+ self.message = message
+ self.text0 = text0
+ self.used_names = used_names
+ self.transient(parent)
+ self.grab_set()
+ windowingsystem = self.tk.call('tk', 'windowingsystem')
+ if windowingsystem == 'aqua':
+ try:
+ self.tk.call('::tk::unsupported::MacWindowStyle', 'style',
+ self._w, 'moveableModal', '')
+ except:
+ pass
+ self.bind("<Command-.>", self.cancel)
+ self.bind('<Key-Escape>', self.cancel)
+ self.protocol("WM_DELETE_WINDOW", self.cancel)
+ self.bind('<Key-Return>', self.ok)
+ self.bind("<KP_Enter>", self.ok)
+ self.resizable(height=False, width=False)
+ self.create_widgets()
+ self.update_idletasks() # Needed here for winfo_reqwidth below.
+ self.geometry( # Center dialog over parent (or below htest box).
+ "+%d+%d" % (
+ parent.winfo_rootx() +
+ (parent.winfo_width()/2 - self.winfo_reqwidth()/2),
+ parent.winfo_rooty() +
+ ((parent.winfo_height()/2 - self.winfo_reqheight()/2)
+ if not _htest else 150)
+ ) )
+ if not _utest:
+ self.deiconify() # Unhide now that geometry set.
+ self.wait_window()
+
+ def create_widgets(self): # Call from override, if any.
+ # Bind to self widgets needed for entry_ok or unittest.
+ self.frame = frame = Frame(self, padding=10)
+ frame.grid(column=0, row=0, sticky='news')
+ frame.grid_columnconfigure(0, weight=1)
+
+ entrylabel = Label(frame, anchor='w', justify='left',
+ text=self.message)
+ self.entryvar = StringVar(self, self.text0)
+ self.entry = Entry(frame, width=30, textvariable=self.entryvar)
+ self.entry.focus_set()
+ self.error_font = Font(name='TkCaptionFont',
+ exists=True, root=self.parent)
+ self.entry_error = Label(frame, text=' ', foreground='red',
+ font=self.error_font)
+ self.button_ok = Button(
+ frame, text='OK', default='active', command=self.ok)
+ self.button_cancel = Button(
+ frame, text='Cancel', command=self.cancel)
+
+ entrylabel.grid(column=0, row=0, columnspan=3, padx=5, sticky=W)
+ self.entry.grid(column=0, row=1, columnspan=3, padx=5, sticky=W+E,
+ pady=[10,0])
+ self.entry_error.grid(column=0, row=2, columnspan=3, padx=5,
+ sticky=W+E)
+ self.button_ok.grid(column=1, row=99, padx=5)
+ self.button_cancel.grid(column=2, row=99, padx=5)
+
+ def showerror(self, message, widget=None):
+ #self.bell(displayof=self)
+ (widget or self.entry_error)['text'] = 'ERROR: ' + message
+
+ def entry_ok(self): # Example: usually replace.
+ "Return non-blank entry or None."
+ self.entry_error['text'] = ''
+ entry = self.entry.get().strip()
+ if not entry:
+ self.showerror('blank line.')
+ return None
+ return entry
+
+ def ok(self, event=None): # Do not replace.
+ '''If entry is valid, bind it to 'result' and destroy tk widget.
+
+ Otherwise leave dialog open for user to correct entry or cancel.
+ '''
+ entry = self.entry_ok()
+ if entry is not None:
+ self.result = entry
+ self.destroy()
+ else:
+ # [Ok] moves focus. (<Return> does not.) Move it back.
+ self.entry.focus_set()
+
+ def cancel(self, event=None): # Do not replace.
+ "Set dialog result to None and destroy tk widget."
+ self.result = None
+ self.destroy()
+
+
+class SectionName(Query):
+ "Get a name for a config file section name."
+ # Used in ConfigDialog.GetNewKeysName, .GetNewThemeName (837)
+
+ def __init__(self, parent, title, message, used_names,
+ *, _htest=False, _utest=False):
+ super().__init__(parent, title, message, used_names=used_names,
+ _htest=_htest, _utest=_utest)
+
+ def entry_ok(self):
+ "Return sensible ConfigParser section name or None."
+ self.entry_error['text'] = ''
+ name = self.entry.get().strip()
+ if not name:
+ self.showerror('no name specified.')
+ return None
+ elif len(name)>30:
+ self.showerror('name is longer than 30 characters.')
+ return None
+ elif name in self.used_names:
+ self.showerror('name is already in use.')
+ return None
+ return name
+
+
+class ModuleName(Query):
+ "Get a module name for Open Module menu entry."
+ # Used in open_module (editor.EditorWindow until move to iobinding).
+
+ def __init__(self, parent, title, message, text0,
+ *, _htest=False, _utest=False):
+ super().__init__(parent, title, message, text0=text0,
+ _htest=_htest, _utest=_utest)
+
+ def entry_ok(self):
+ "Return entered module name as file path or None."
+ self.entry_error['text'] = ''
+ name = self.entry.get().strip()
+ if not name:
+ self.showerror('no name specified.')
+ return None
+ # XXX Ought to insert current file's directory in front of path.
+ try:
+ spec = importlib.util.find_spec(name)
+ except (ValueError, ImportError) as msg:
+ self.showerror(str(msg))
+ return None
+ if spec is None:
+ self.showerror("module not found")
+ return None
+ if not isinstance(spec.loader, importlib.abc.SourceLoader):
+ self.showerror("not a source-based module")
+ return None
+ try:
+ file_path = spec.loader.get_filename(name)
+ except AttributeError:
+ self.showerror("loader does not support get_filename",
+ parent=self)
+ return None
+ return file_path
+
+
+class HelpSource(Query):
+ "Get menu name and help source for Help menu."
+ # Used in ConfigDialog.HelpListItemAdd/Edit, (941/9)
+
+ def __init__(self, parent, title, *, menuitem='', filepath='',
+ used_names={}, _htest=False, _utest=False):
+ """Get menu entry and url/local file for Additional Help.
+
+ User enters a name for the Help resource and a web url or file
+ name. The user can browse for the file.
+ """
+ self.filepath = filepath
+ message = 'Name for item on Help menu:'
+ super().__init__(
+ parent, title, message, text0=menuitem,
+ used_names=used_names, _htest=_htest, _utest=_utest)
+
+ def create_widgets(self):
+ super().create_widgets()
+ frame = self.frame
+ pathlabel = Label(frame, anchor='w', justify='left',
+ text='Help File Path: Enter URL or browse for file')
+ self.pathvar = StringVar(self, self.filepath)
+ self.path = Entry(frame, textvariable=self.pathvar, width=40)
+ browse = Button(frame, text='Browse', width=8,
+ command=self.browse_file)
+ self.path_error = Label(frame, text=' ', foreground='red',
+ font=self.error_font)
+
+ pathlabel.grid(column=0, row=10, columnspan=3, padx=5, pady=[10,0],
+ sticky=W)
+ self.path.grid(column=0, row=11, columnspan=2, padx=5, sticky=W+E,
+ pady=[10,0])
+ browse.grid(column=2, row=11, padx=5, sticky=W+S)
+ self.path_error.grid(column=0, row=12, columnspan=3, padx=5,
+ sticky=W+E)
+
+ def askfilename(self, filetypes, initdir, initfile): # htest #
+ # Extracted from browse_file so can mock for unittests.
+ # Cannot unittest as cannot simulate button clicks.
+ # Test by running htest, such as by running this file.
+ return filedialog.Open(parent=self, filetypes=filetypes)\
+ .show(initialdir=initdir, initialfile=initfile)
+
+ def browse_file(self):
+ filetypes = [
+ ("HTML Files", "*.htm *.html", "TEXT"),
+ ("PDF Files", "*.pdf", "TEXT"),
+ ("Windows Help Files", "*.chm"),
+ ("Text Files", "*.txt", "TEXT"),
+ ("All Files", "*")]
+ path = self.pathvar.get()
+ if path:
+ dir, base = os.path.split(path)
+ else:
+ base = None
+ if platform[:3] == 'win':
+ dir = os.path.join(os.path.dirname(executable), 'Doc')
+ if not os.path.isdir(dir):
+ dir = os.getcwd()
+ else:
+ dir = os.getcwd()
+ file = self.askfilename(filetypes, dir, base)
+ if file:
+ self.pathvar.set(file)
+
+ item_ok = SectionName.entry_ok # localize for test override
+
+ def path_ok(self):
+ "Simple validity check for menu file path"
+ path = self.path.get().strip()
+ if not path: #no path specified
+ self.showerror('no help file path specified.', self.path_error)
+ return None
+ elif not path.startswith(('www.', 'http')):
+ if path[:5] == 'file:':
+ path = path[5:]
+ if not os.path.exists(path):
+ self.showerror('help file path does not exist.',
+ self.path_error)
+ return None
+ if platform == 'darwin': # for Mac Safari
+ path = "file://" + path
+ return path
+
+ def entry_ok(self):
+ "Return apparently valid (name, path) or None"
+ self.entry_error['text'] = ''
+ self.path_error['text'] = ''
+ name = self.item_ok()
+ path = self.path_ok()
+ return None if name is None or path is None else (name, path)
+
+
+if __name__ == '__main__':
+ import unittest
+ unittest.main('idlelib.idle_test.test_query', verbosity=2, exit=False)
+
+ from idlelib.idle_test.htest import run
+ run(Query, HelpSource)
diff --git a/Lib/idlelib/WidgetRedirector.py b/Lib/idlelib/redirector.py
index b66be9e..ec681de 100644
--- a/Lib/idlelib/WidgetRedirector.py
+++ b/Lib/idlelib/redirector.py
@@ -104,7 +104,7 @@ class WidgetRedirector:
Note that if a registered function is called, the operation is not
passed through to Tk. Apply the function returned by self.register()
- to *args to accomplish that. For an example, see ColorDelegator.py.
+ to *args to accomplish that. For an example, see colorizer.py.
'''
m = self._operations.get(operation)
@@ -151,14 +151,13 @@ class OriginalCommand:
def _widget_redirector(parent): # htest #
- from tkinter import Tk, Text
- import re
-
- root = Tk()
- root.title("Test WidgetRedirector")
- width, height, x, y = list(map(int, re.split('[x+]', parent.geometry())))
- root.geometry("+%d+%d"%(x, y + 150))
- text = Text(root)
+ from tkinter import Toplevel, Text
+
+ top = Toplevel(parent)
+ top.title("Test WidgetRedirector")
+ x, y = map(int, parent.geometry().split('+')[1:])
+ top.geometry("+%d+%d" % (x, y + 175))
+ text = Text(top)
text.pack()
text.focus_set()
redir = WidgetRedirector(text)
@@ -166,11 +165,11 @@ def _widget_redirector(parent): # htest #
print("insert", args)
original_insert(*args)
original_insert = redir.register("insert", my_insert)
- root.mainloop()
if __name__ == "__main__":
import unittest
- unittest.main('idlelib.idle_test.test_widgetredir',
+ unittest.main('idlelib.idle_test.test_redirector',
verbosity=2, exit=False)
+
from idlelib.idle_test.htest import run
run(_widget_redirector)
diff --git a/Lib/idlelib/ReplaceDialog.py b/Lib/idlelib/replace.py
index f2ea22e..abd9e59 100644
--- a/Lib/idlelib/ReplaceDialog.py
+++ b/Lib/idlelib/replace.py
@@ -3,18 +3,18 @@ Uses idlelib.SearchEngine for search capability.
Defines various replace related functions like replace, replace all,
replace+find.
"""
-from tkinter import *
-
-from idlelib import SearchEngine
-from idlelib.SearchDialogBase import SearchDialogBase
import re
+from tkinter import StringVar, TclError
+
+from idlelib.searchbase import SearchDialogBase
+from idlelib import searchengine
def replace(text):
"""Returns a singleton ReplaceDialog instance.The single dialog
saves user entries and preferences across instances."""
root = text._root()
- engine = SearchEngine.get(root)
+ engine = searchengine.get(root)
if not hasattr(engine, "_replacedialog"):
engine._replacedialog = ReplaceDialog(root, engine)
dialog = engine._replacedialog
@@ -95,7 +95,7 @@ class ReplaceDialog(SearchDialogBase):
text = self.text
res = self.engine.search_text(text, prog)
if not res:
- text.bell()
+ self.bell()
return
text.tag_remove("sel", "1.0", "end")
text.tag_remove("hit", "1.0", "end")
@@ -142,7 +142,7 @@ class ReplaceDialog(SearchDialogBase):
text = self.text
res = self.engine.search_text(text, None, ok)
if not res:
- text.bell()
+ self.bell()
return False
line, m = res
i, j = m.span()
@@ -164,7 +164,7 @@ class ReplaceDialog(SearchDialogBase):
pos = None
if not pos:
first = last = pos = text.index("insert")
- line, col = SearchEngine.get_line_col(pos)
+ line, col = searchengine.get_line_col(pos)
chars = text.get("%d.0" % line, "%d.0" % (line+1))
m = prog.match(chars, col)
if not prog:
@@ -204,11 +204,13 @@ class ReplaceDialog(SearchDialogBase):
def _replace_dialog(parent): # htest #
- """htest wrapper function"""
+ from tkinter import Toplevel, Text, END, SEL
+ from tkinter.ttk import Button
+
box = Toplevel(parent)
box.title("Test ReplaceDialog")
- width, height, x, y = list(map(int, re.split('[x+]', parent.geometry())))
- box.geometry("+%d+%d"%(x, y + 150))
+ x, y = map(int, parent.geometry().split('+')[1:])
+ box.geometry("+%d+%d" % (x, y + 175))
# mock undo delegator methods
def undo_block_start():
@@ -234,7 +236,7 @@ def _replace_dialog(parent): # htest #
if __name__ == '__main__':
import unittest
- unittest.main('idlelib.idle_test.test_replacedialog',
+ unittest.main('idlelib.idle_test.test_replace',
verbosity=2, exit=False)
from idlelib.idle_test.htest import run
diff --git a/Lib/idlelib/rpc.py b/Lib/idlelib/rpc.py
index 48105f2..8f57edb8 100644
--- a/Lib/idlelib/rpc.py
+++ b/Lib/idlelib/rpc.py
@@ -26,23 +26,21 @@ See the Idle run.main() docstring for further information on how this was
accomplished in Idle.
"""
-
-import sys
-import os
+import builtins
+import copyreg
import io
-import socket
+import marshal
+import os
+import pickle
+import queue
import select
+import socket
import socketserver
import struct
-import pickle
+import sys
import threading
-import queue
import traceback
-import copyreg
import types
-import marshal
-import builtins
-
def unpickle_code(ms):
co = marshal.loads(ms)
@@ -60,10 +58,12 @@ def dumps(obj, protocol=None):
p.dump(obj)
return f.getvalue()
+
class CodePickler(pickle.Pickler):
dispatch_table = {types.CodeType: pickle_code}
dispatch_table.update(copyreg.dispatch_table)
+
BUFSIZE = 8*1024
LOCALHOST = '127.0.0.1'
@@ -487,16 +487,19 @@ class RemoteObject(object):
# Token mix-in class
pass
+
def remoteref(obj):
oid = id(obj)
objecttable[oid] = obj
return RemoteProxy(oid)
+
class RemoteProxy(object):
def __init__(self, oid):
self.oid = oid
+
class RPCHandler(socketserver.BaseRequestHandler, SocketIO):
debugging = False
@@ -514,6 +517,7 @@ class RPCHandler(socketserver.BaseRequestHandler, SocketIO):
def get_remote_proxy(self, oid):
return RPCProxy(self, oid)
+
class RPCClient(SocketIO):
debugging = False
@@ -539,6 +543,7 @@ class RPCClient(SocketIO):
def get_remote_proxy(self, oid):
return RPCProxy(self, oid)
+
class RPCProxy(object):
__methods = None
@@ -587,6 +592,7 @@ def _getattributes(obj, attributes):
if not callable(attr):
attributes[name] = 1
+
class MethodProxy(object):
def __init__(self, sockio, oid, name):
diff --git a/Lib/idlelib/RstripExtension.py b/Lib/idlelib/rstrip.py
index 2ce3c7e..2ce3c7e 100644
--- a/Lib/idlelib/RstripExtension.py
+++ b/Lib/idlelib/rstrip.py
diff --git a/Lib/idlelib/run.py b/Lib/idlelib/run.py
index 28ce420..afa9744 100644
--- a/Lib/idlelib/run.py
+++ b/Lib/idlelib/run.py
@@ -1,33 +1,45 @@
-import sys
+import io
import linecache
+import queue
+import sys
import time
import traceback
import _thread as thread
import threading
-import queue
-import tkinter
-
-from idlelib import CallTips
-from idlelib import AutoComplete
+import warnings
-from idlelib import RemoteDebugger
-from idlelib import RemoteObjectBrowser
-from idlelib import StackViewer
-from idlelib import rpc
-from idlelib import PyShell
-from idlelib import IOBinding
+import tkinter # Tcl, deletions, messagebox if startup fails
+from idlelib import autocomplete # AutoComplete, fetch_encodings
+from idlelib import calltips # CallTips
+from idlelib import debugger_r # start_debugger
+from idlelib import debugobj_r # remote_object_tree_item
+from idlelib import iomenu # encoding
+from idlelib import rpc # multiple objects
+from idlelib import stackviewer # StackTreeItem
import __main__
for mod in ('simpledialog', 'messagebox', 'font',
'dialog', 'filedialog', 'commondialog',
- 'colorchooser'):
+ 'ttk'):
delattr(tkinter, mod)
del sys.modules['tkinter.' + mod]
LOCALHOST = '127.0.0.1'
-import warnings
+
+def idle_formatwarning(message, category, filename, lineno, line=None):
+ """Format warnings the IDLE way."""
+
+ s = "\nWarning (from warnings module):\n"
+ s += ' File \"%s\", line %s\n' % (filename, lineno)
+ if line is None:
+ line = linecache.getline(filename, lineno)
+ line = line.strip()
+ if line:
+ s += " %s\n" % line
+ s += "%s: %s\n" % (category.__name__, message)
+ return s
def idle_showwarning_subproc(
message, category, filename, lineno, file=None, line=None):
@@ -38,7 +50,7 @@ def idle_showwarning_subproc(
if file is None:
file = sys.stderr
try:
- file.write(PyShell.idle_formatwarning(
+ file.write(idle_formatwarning(
message, category, filename, lineno, line))
except IOError:
pass # the file (probably stderr) is invalid - this warning gets lost.
@@ -88,7 +100,7 @@ def main(del_exitfunc=False):
MyHandler object. That reference is saved as attribute rpchandler of the
Executive instance. The Executive methods have access to the reference and
can pass it on to entities that they command
- (e.g. RemoteDebugger.Debugger.start_debugger()). The latter, in turn, can
+ (e.g. debugger_r.Debugger.start_debugger()). The latter, in turn, can
call MyHandler(SocketIO) register/unregister methods via the reference to
register and unregister themselves.
@@ -210,7 +222,7 @@ def print_exception():
tbe = traceback.extract_tb(tb)
print('Traceback (most recent call last):', file=efile)
exclude = ("run.py", "rpc.py", "threading.py", "queue.py",
- "RemoteDebugger.py", "bdb.py")
+ "debugger_r.py", "bdb.py")
cleanup_traceback(tbe, exclude)
traceback.print_list(tbe, file=efile)
lines = traceback.format_exception_only(typ, exc)
@@ -267,6 +279,7 @@ def exit():
capture_warnings(False)
sys.exit(0)
+
class MyRPCServer(rpc.RPCServer):
def handle_error(self, request, client_address):
@@ -297,6 +310,96 @@ class MyRPCServer(rpc.RPCServer):
quitting = True
thread.interrupt_main()
+
+# Pseudofiles for shell-remote communication (also used in pyshell)
+
+class PseudoFile(io.TextIOBase):
+
+ def __init__(self, shell, tags, encoding=None):
+ self.shell = shell
+ self.tags = tags
+ self._encoding = encoding
+
+ @property
+ def encoding(self):
+ return self._encoding
+
+ @property
+ def name(self):
+ return '<%s>' % self.tags
+
+ def isatty(self):
+ return True
+
+
+class PseudoOutputFile(PseudoFile):
+
+ def writable(self):
+ return True
+
+ def write(self, s):
+ if self.closed:
+ raise ValueError("write to closed file")
+ if type(s) is not str:
+ if not isinstance(s, str):
+ raise TypeError('must be str, not ' + type(s).__name__)
+ # See issue #19481
+ s = str.__str__(s)
+ return self.shell.write(s, self.tags)
+
+
+class PseudoInputFile(PseudoFile):
+
+ def __init__(self, shell, tags, encoding=None):
+ PseudoFile.__init__(self, shell, tags, encoding)
+ self._line_buffer = ''
+
+ def readable(self):
+ return True
+
+ def read(self, size=-1):
+ if self.closed:
+ raise ValueError("read from closed file")
+ if size is None:
+ size = -1
+ elif not isinstance(size, int):
+ raise TypeError('must be int, not ' + type(size).__name__)
+ result = self._line_buffer
+ self._line_buffer = ''
+ if size < 0:
+ while True:
+ line = self.shell.readline()
+ if not line: break
+ result += line
+ else:
+ while len(result) < size:
+ line = self.shell.readline()
+ if not line: break
+ result += line
+ self._line_buffer = result[size:]
+ result = result[:size]
+ return result
+
+ def readline(self, size=-1):
+ if self.closed:
+ raise ValueError("read from closed file")
+ if size is None:
+ size = -1
+ elif not isinstance(size, int):
+ raise TypeError('must be int, not ' + type(size).__name__)
+ line = self._line_buffer or self.shell.readline()
+ if size < 0:
+ size = len(line)
+ eol = line.find('\n', 0, size)
+ if eol >= 0:
+ size = eol + 1
+ self._line_buffer = line[size:]
+ return line[:size]
+
+ def close(self):
+ self.shell.close()
+
+
class MyHandler(rpc.RPCHandler):
def handle(self):
@@ -304,12 +407,12 @@ class MyHandler(rpc.RPCHandler):
executive = Executive(self)
self.register("exec", executive)
self.console = self.get_remote_proxy("console")
- sys.stdin = PyShell.PseudoInputFile(self.console, "stdin",
- IOBinding.encoding)
- sys.stdout = PyShell.PseudoOutputFile(self.console, "stdout",
- IOBinding.encoding)
- sys.stderr = PyShell.PseudoOutputFile(self.console, "stderr",
- IOBinding.encoding)
+ sys.stdin = PseudoInputFile(self.console, "stdin",
+ iomenu.encoding)
+ sys.stdout = PseudoOutputFile(self.console, "stdout",
+ iomenu.encoding)
+ sys.stderr = PseudoOutputFile(self.console, "stderr",
+ iomenu.encoding)
sys.displayhook = rpc.displayhook
# page help() text to shell.
@@ -345,8 +448,8 @@ class Executive(object):
def __init__(self, rpchandler):
self.rpchandler = rpchandler
self.locals = __main__.__dict__
- self.calltip = CallTips.CallTips()
- self.autocomplete = AutoComplete.AutoComplete()
+ self.calltip = calltips.CallTips()
+ self.autocomplete = autocomplete.AutoComplete()
def runcode(self, code):
global interruptable
@@ -378,7 +481,7 @@ class Executive(object):
thread.interrupt_main()
def start_the_debugger(self, gui_adap_oid):
- return RemoteDebugger.start_debugger(self.rpchandler, gui_adap_oid)
+ return debugger_r.start_debugger(self.rpchandler, gui_adap_oid)
def stop_the_debugger(self, idb_adap_oid):
"Unregister the Idb Adapter. Link objects and Idb then subject to GC"
@@ -402,7 +505,7 @@ class Executive(object):
tb = tb.tb_next
sys.last_type = typ
sys.last_value = val
- item = StackViewer.StackTreeItem(flist, tb)
- return RemoteObjectBrowser.remote_object_tree_item(item)
+ item = stackviewer.StackTreeItem(flist, tb)
+ return debugobj_r.remote_object_tree_item(item)
capture_warnings(False) # Make sure turned off; see issue 18081
diff --git a/Lib/idlelib/ScriptBinding.py b/Lib/idlelib/runscript.py
index 5cb818d..79d86ad 100644
--- a/Lib/idlelib/ScriptBinding.py
+++ b/Lib/idlelib/runscript.py
@@ -20,11 +20,12 @@ XXX GvR Redesign this interface (yet again) as follows:
import os
import tabnanny
import tokenize
+
import tkinter.messagebox as tkMessageBox
-from idlelib import PyShell
-from idlelib.configHandler import idleConf
-from idlelib import macosxSupport
+from idlelib.config import idleConf
+from idlelib import macosx
+from idlelib import pyshell
indent_message = """Error: Inconsistent indentation detected!
@@ -46,12 +47,12 @@ class ScriptBinding:
def __init__(self, editwin):
self.editwin = editwin
- # Provide instance variables referenced by Debugger
+ # Provide instance variables referenced by debugger
# XXX This should be done differently
self.flist = self.editwin.flist
self.root = self.editwin.root
- if macosxSupport.isCocoaTk():
+ if macosx.isCocoaTk():
self.editwin.text_frame.bind('<<run-module-event-2>>', self._run_module_event)
def check_module_event(self, event):
@@ -112,7 +113,7 @@ class ScriptBinding:
shell.set_warning_stream(saved_stream)
def run_module_event(self, event):
- if macosxSupport.isCocoaTk():
+ if macosx.isCocoaTk():
# Tk-Cocoa in MacOSX is broken until at least
# Tk 8.5.9, and without this rather
# crude workaround IDLE would hang when a user
@@ -142,7 +143,7 @@ class ScriptBinding:
if not self.tabnanny(filename):
return 'break'
interp = self.shell.interp
- if PyShell.use_subprocess:
+ if pyshell.use_subprocess:
interp.restart_subprocess(with_cwd=False, filename=
self.editwin._filename_to_unicode(filename))
dirname = os.path.dirname(filename)
@@ -161,7 +162,7 @@ class ScriptBinding:
interp.prepend_syspath(filename)
# XXX KBK 03Jul04 When run w/o subprocess, runtime warnings still
# go to __stderr__. With subprocess, they go to the shell.
- # Need to change streams in PyShell.ModifiedInterpreter.
+ # Need to change streams in pyshell.ModifiedInterpreter.
interp.runcode(code)
return 'break'
diff --git a/Lib/idlelib/ScrolledList.py b/Lib/idlelib/scrolledlist.py
index 53576b5..cc08c26 100644
--- a/Lib/idlelib/ScrolledList.py
+++ b/Lib/idlelib/scrolledlist.py
@@ -1,5 +1,8 @@
from tkinter import *
-from idlelib import macosxSupport
+from tkinter.ttk import Scrollbar
+
+from idlelib import macosx
+
class ScrolledList:
@@ -23,7 +26,7 @@ class ScrolledList:
# Bind events to the list box
listbox.bind("<ButtonRelease-1>", self.click_event)
listbox.bind("<Double-ButtonRelease-1>", self.double_click_event)
- if macosxSupport.isAquaTk():
+ if macosx.isAquaTk():
listbox.bind("<ButtonPress-2>", self.popup_event)
listbox.bind("<Control-Button-1>", self.popup_event)
else:
@@ -124,22 +127,20 @@ class ScrolledList:
pass
-def _scrolled_list(parent):
- root = Tk()
- root.title("Test ScrolledList")
- width, height, x, y = list(map(int, re.split('[x+]', parent.geometry())))
- root.geometry("+%d+%d"%(x, y + 150))
+def _scrolled_list(parent): # htest #
+ top = Toplevel(parent)
+ x, y = map(int, parent.geometry().split('+')[1:])
+ top.geometry("+%d+%d" % (x+200, y + 175))
class MyScrolledList(ScrolledList):
def fill_menu(self): self.menu.add_command(label="right click")
def on_select(self, index): print("select", self.get(index))
def on_double(self, index): print("double", self.get(index))
- scrolled_list = MyScrolledList(root)
+ scrolled_list = MyScrolledList(top)
for i in range(30):
scrolled_list.append("Item %02d" % i)
- root.mainloop()
-
if __name__ == '__main__':
+ # At the moment, test_scrolledlist merely creates instance, like htest.
from idlelib.idle_test.htest import run
run(_scrolled_list)
diff --git a/Lib/idlelib/SearchDialog.py b/Lib/idlelib/search.py
index 765d53f..4b90659 100644
--- a/Lib/idlelib/SearchDialog.py
+++ b/Lib/idlelib/search.py
@@ -1,12 +1,12 @@
-from tkinter import *
+from tkinter import TclError
-from idlelib import SearchEngine
-from idlelib.SearchDialogBase import SearchDialogBase
+from idlelib import searchengine
+from idlelib.searchbase import SearchDialogBase
def _setup(text):
"Create or find the singleton SearchDialog instance."
root = text._root()
- engine = SearchEngine.get(root)
+ engine = searchengine.get(root)
if not hasattr(engine, "_searchdialog"):
engine._searchdialog = SearchDialog(root, engine)
return engine._searchdialog
@@ -24,6 +24,7 @@ def find_selection(text):
"Handle the editor edit menu item and corresponding event."
return _setup(text).find_selection(text)
+
class SearchDialog(SearchDialogBase):
def create_widgets(self):
@@ -51,7 +52,7 @@ class SearchDialog(SearchDialogBase):
selfirst = text.index("sel.first")
sellast = text.index("sel.last")
if selfirst == first and sellast == last:
- text.bell()
+ self.bell()
return False
except TclError:
pass
@@ -61,7 +62,7 @@ class SearchDialog(SearchDialogBase):
text.see("insert")
return True
else:
- text.bell()
+ self.bell()
return False
def find_selection(self, text):
@@ -72,26 +73,30 @@ class SearchDialog(SearchDialogBase):
def _search_dialog(parent): # htest #
- '''Display search test box.'''
+ "Display search test box."
+ from tkinter import Toplevel, Text
+ from tkinter.ttk import Button
+
box = Toplevel(parent)
box.title("Test SearchDialog")
- width, height, x, y = list(map(int, re.split('[x+]', parent.geometry())))
- box.geometry("+%d+%d"%(x, y + 150))
+ x, y = map(int, parent.geometry().split('+')[1:])
+ box.geometry("+%d+%d" % (x, y + 175))
text = Text(box, inactiveselectbackground='gray')
text.pack()
text.insert("insert","This is a sample string.\n"*5)
def show_find():
- text.tag_add(SEL, "1.0", END)
+ text.tag_add('sel', '1.0', 'end')
_setup(text).open(text)
- text.tag_remove(SEL, "1.0", END)
+ text.tag_remove('sel', '1.0', 'end')
button = Button(box, text="Search (selection ignored)", command=show_find)
button.pack()
if __name__ == '__main__':
import unittest
- unittest.main('idlelib.idle_test.test_searchdialog',
+ unittest.main('idlelib.idle_test.test_search',
verbosity=2, exit=False)
+
from idlelib.idle_test.htest import run
run(_search_dialog)
diff --git a/Lib/idlelib/SearchDialogBase.py b/Lib/idlelib/searchbase.py
index 5fa84e2..5f81785 100644
--- a/Lib/idlelib/SearchDialogBase.py
+++ b/Lib/idlelib/searchbase.py
@@ -1,7 +1,8 @@
'''Define SearchDialogBase used by Search, Replace, and Grep dialogs.'''
-from tkinter import (Toplevel, Frame, Entry, Label, Button,
- Checkbutton, Radiobutton)
+from tkinter import Toplevel, Frame
+from tkinter.ttk import Entry, Label, Button, Checkbutton, Radiobutton
+
class SearchDialogBase:
'''Create most of a 3 or 4 row, 3 column search dialog.
@@ -79,6 +80,7 @@ class SearchDialogBase:
top.wm_title(self.title)
top.wm_iconname(self.icon)
self.top = top
+ self.bell = top.bell
self.row = 0
self.top.grid_columnconfigure(0, pad=2, weight=0)
@@ -125,7 +127,7 @@ class SearchDialogBase:
def create_option_buttons(self):
'''Return (filled frame, options) for testing.
- Options is a list of SearchEngine booleanvar, label pairs.
+ Options is a list of searchengine booleanvar, label pairs.
A gridded frame from make_frame is filled with a Checkbutton
for each pair, bound to the var, with the corresponding label.
'''
@@ -137,10 +139,8 @@ class SearchDialogBase:
if self.needwrapbutton:
options.append((engine.wrapvar, "Wrap around"))
for var, label in options:
- btn = Checkbutton(frame, anchor="w", variable=var, text=label)
+ btn = Checkbutton(frame, variable=var, text=label)
btn.pack(side="left", fill="both")
- if var.get():
- btn.select()
return frame, options
def create_other_buttons(self):
@@ -153,11 +153,8 @@ class SearchDialogBase:
var = self.engine.backvar
others = [(1, 'Up'), (0, 'Down')]
for val, label in others:
- btn = Radiobutton(frame, anchor="w",
- variable=var, value=val, text=label)
+ btn = Radiobutton(frame, variable=var, value=val, text=label)
btn.pack(side="left", fill="both")
- if var.get() == val:
- btn.select()
return frame, others
def make_button(self, label, command, isdef=0):
@@ -178,7 +175,26 @@ class SearchDialogBase:
b = self.make_button("close", self.close)
b.lower()
+
+class _searchbase(SearchDialogBase): # htest #
+ "Create auto-opening dialog with no text connection."
+
+ def __init__(self, parent):
+ import re
+ from idlelib import searchengine
+
+ self.root = parent
+ self.engine = searchengine.get(parent)
+ self.create_widgets()
+ print(parent.geometry())
+ width,height, x,y = list(map(int, re.split('[x+]', parent.geometry())))
+ self.top.geometry("+%d+%d" % (x + 40, y + 175))
+
+ def default_command(self, dummy): pass
+
if __name__ == '__main__':
import unittest
- unittest.main(
- 'idlelib.idle_test.test_searchdialogbase', verbosity=2)
+ unittest.main('idlelib.idle_test.test_searchbase', verbosity=2, exit=False)
+
+ from idlelib.idle_test.htest import run
+ run(_searchbase)
diff --git a/Lib/idlelib/SearchEngine.py b/Lib/idlelib/searchengine.py
index 37883bf..253f1b0 100644
--- a/Lib/idlelib/SearchEngine.py
+++ b/Lib/idlelib/searchengine.py
@@ -1,5 +1,6 @@
'''Define SearchEngine for search dialogs.'''
import re
+
from tkinter import StringVar, BooleanVar, TclError
import tkinter.messagebox as tkMessageBox
@@ -14,6 +15,7 @@ def get(root):
# This creates a cycle that persists until root is deleted.
return root._searchengine
+
class SearchEngine:
"""Handles searching a text widget for Find, Replace, and Grep."""
@@ -57,7 +59,7 @@ class SearchEngine:
def setcookedpat(self, pat):
"Set pattern after escaping if re."
- # called only in SearchDialog.py: 66
+ # called only in search.py: 66
if self.isre():
pat = re.escape(pat)
self.setpat(pat)
@@ -186,6 +188,7 @@ class SearchEngine:
col = len(chars) - 1
return None
+
def search_reverse(prog, chars, col):
'''Search backwards and return an re match object or None.
diff --git a/Lib/idlelib/StackViewer.py b/Lib/idlelib/stackviewer.py
index ccc755c..0698def 100644
--- a/Lib/idlelib/StackViewer.py
+++ b/Lib/idlelib/stackviewer.py
@@ -1,12 +1,12 @@
-import os
-import sys
import linecache
+import os
import re
+import sys
+
import tkinter as tk
-from idlelib.TreeWidget import TreeNode, TreeItem, ScrolledCanvas
-from idlelib.ObjectBrowser import ObjectTreeItem, make_objecttreeitem
-from idlelib.PyShell import PyShellFileList
+from idlelib.debugobj import ObjectTreeItem, make_objecttreeitem
+from idlelib.tree import TreeNode, TreeItem, ScrolledCanvas
def StackBrowser(root, flist=None, tb=None, top=None):
if top is None:
@@ -17,6 +17,7 @@ def StackBrowser(root, flist=None, tb=None, top=None):
node = TreeNode(sc.canvas, None, item)
node.expand()
+
class StackTreeItem(TreeItem):
def __init__(self, flist=None, tb=None):
@@ -55,6 +56,7 @@ class StackTreeItem(TreeItem):
sublist.append(item)
return sublist
+
class FrameTreeItem(TreeItem):
def __init__(self, info, flist):
@@ -96,6 +98,7 @@ class FrameTreeItem(TreeItem):
if os.path.isfile(filename):
self.flist.gotofileline(filename, lineno)
+
class VariablesTreeItem(ObjectTreeItem):
def GetText(self):
@@ -120,15 +123,14 @@ class VariablesTreeItem(ObjectTreeItem):
sublist.append(item)
return sublist
- def keys(self): # unused, left for possible 3rd party use
- return list(self.object.keys())
-def _stack_viewer(parent):
- root = tk.Tk()
- root.title("Test StackViewer")
- width, height, x, y = list(map(int, re.split('[x+]', parent.geometry())))
- root.geometry("+%d+%d"%(x, y + 150))
- flist = PyShellFileList(root)
+def _stack_viewer(parent): # htest #
+ from idlelib.pyshell import PyShellFileList
+ top = tk.Toplevel(parent)
+ top.title("Test StackViewer")
+ x, y = map(int, parent.geometry().split('+')[1:])
+ top.geometry("+%d+%d" % (x + 50, y + 175))
+ flist = PyShellFileList(top)
try: # to obtain a traceback object
intentional_name_error
except NameError:
@@ -139,7 +141,7 @@ def _stack_viewer(parent):
sys.last_value = exc_value
sys.last_traceback = exc_tb
- StackBrowser(root, flist=flist, top=root, tb=exc_tb)
+ StackBrowser(top, flist=flist, top=top, tb=exc_tb)
# restore sys to original state
del sys.last_type
diff --git a/Lib/idlelib/MultiStatusBar.py b/Lib/idlelib/statusbar.py
index e82ba9a..8618528 100644
--- a/Lib/idlelib/MultiStatusBar.py
+++ b/Lib/idlelib/statusbar.py
@@ -1,16 +1,15 @@
-from tkinter import *
+from tkinter import Frame, Label
+
class MultiStatusBar(Frame):
- def __init__(self, master=None, **kw):
- if master is None:
- master = Tk()
+ def __init__(self, master, **kw):
Frame.__init__(self, master, **kw)
self.labels = {}
- def set_label(self, name, text='', side=LEFT, width=0):
+ def set_label(self, name, text='', side='left', width=0):
if name not in self.labels:
- label = Label(self, borderwidth=0, anchor=W)
+ label = Label(self, borderwidth=0, anchor='w')
label.pack(side=side, pady=0, padx=4)
self.labels[name] = label
else:
@@ -19,28 +18,28 @@ class MultiStatusBar(Frame):
label.config(width=width)
label.config(text=text)
-def _multistatus_bar(parent):
- root = Tk()
- width, height, x, y = list(map(int, re.split('[x+]', parent.geometry())))
- root.geometry("+%d+%d" %(x, y + 150))
- root.title("Test multistatus bar")
- frame = Frame(root)
- text = Text(frame)
+
+def _multistatus_bar(parent): # htest #
+ from tkinter import Toplevel, Frame, Text, Button
+ top = Toplevel(parent)
+ x, y = map(int, parent.geometry().split('+')[1:])
+ top.geometry("+%d+%d" %(x, y + 175))
+ top.title("Test multistatus bar")
+ frame = Frame(top)
+ text = Text(frame, height=5, width=40)
text.pack()
msb = MultiStatusBar(frame)
msb.set_label("one", "hello")
msb.set_label("two", "world")
- msb.pack(side=BOTTOM, fill=X)
+ msb.pack(side='bottom', fill='x')
def change():
msb.set_label("one", "foo")
msb.set_label("two", "bar")
- button = Button(root, text="Update status", command=change)
- button.pack(side=BOTTOM)
+ button = Button(top, text="Update status", command=change)
+ button.pack(side='bottom')
frame.pack()
- frame.mainloop()
- root.mainloop()
if __name__ == '__main__':
from idlelib.idle_test.htest import run
diff --git a/Lib/idlelib/tabbedpages.py b/Lib/idlelib/tabbedpages.py
index 965f9f8..4186fa2 100644
--- a/Lib/idlelib/tabbedpages.py
+++ b/Lib/idlelib/tabbedpages.py
@@ -285,6 +285,7 @@ class TabSet(Frame):
# placed hide it
self.tab_set.lower()
+
class TabbedPageSet(Frame):
"""A Tkinter tabbed-pane widget.
@@ -302,6 +303,7 @@ class TabbedPageSet(Frame):
remove_page() methods.
"""
+
class Page(object):
"""Abstract base class for TabbedPageSet's pages.
@@ -467,31 +469,29 @@ class TabbedPageSet(Frame):
self._tab_set.set_selected_tab(page_name)
-def _tabbed_pages(parent):
- # test dialog
- root=Tk()
- width, height, x, y = list(map(int, re.split('[x+]', parent.geometry())))
- root.geometry("+%d+%d"%(x, y + 175))
- root.title("Test tabbed pages")
- tabPage=TabbedPageSet(root, page_names=['Foobar','Baz'], n_rows=0,
+
+def _tabbed_pages(parent): # htest #
+ top=Toplevel(parent)
+ x, y = map(int, parent.geometry().split('+')[1:])
+ top.geometry("+%d+%d" % (x, y + 175))
+ top.title("Test tabbed pages")
+ tabPage=TabbedPageSet(top, page_names=['Foobar','Baz'], n_rows=0,
expand_tabs=False,
)
tabPage.pack(side=TOP, expand=TRUE, fill=BOTH)
Label(tabPage.pages['Foobar'].frame, text='Foo', pady=20).pack()
Label(tabPage.pages['Foobar'].frame, text='Bar', pady=20).pack()
Label(tabPage.pages['Baz'].frame, text='Baz').pack()
- entryPgName=Entry(root)
- buttonAdd=Button(root, text='Add Page',
+ entryPgName=Entry(top)
+ buttonAdd=Button(top, text='Add Page',
command=lambda:tabPage.add_page(entryPgName.get()))
- buttonRemove=Button(root, text='Remove Page',
+ buttonRemove=Button(top, text='Remove Page',
command=lambda:tabPage.remove_page(entryPgName.get()))
- labelPgName=Label(root, text='name of page to add/remove:')
+ labelPgName=Label(top, text='name of page to add/remove:')
buttonAdd.pack(padx=5, pady=5)
buttonRemove.pack(padx=5, pady=5)
labelPgName.pack(padx=5)
entryPgName.pack(padx=5)
- root.mainloop()
-
if __name__ == '__main__':
from idlelib.idle_test.htest import run
diff --git a/Lib/idlelib/textView.py b/Lib/idlelib/textview.py
index 12ac319..adee326 100644
--- a/Lib/idlelib/textView.py
+++ b/Lib/idlelib/textview.py
@@ -1,14 +1,14 @@
"""Simple text browser for IDLE
"""
-
from tkinter import *
-import tkinter.messagebox as tkMessageBox
+from tkinter.ttk import Scrollbar
+from tkinter.messagebox import showerror
+
class TextViewer(Toplevel):
- """A simple text viewer dialog for IDLE
+ "A simple text viewer dialog for IDLE."
- """
def __init__(self, parent, title, text, modal=True, _htest=False):
"""Show the given text in a scrollable window with a 'close' button
@@ -20,11 +20,11 @@ class TextViewer(Toplevel):
"""
Toplevel.__init__(self, parent)
self.configure(borderwidth=5)
- # place dialog below parent if running htest
+ # Place dialog below parent if running htest.
self.geometry("=%dx%d+%d+%d" % (750, 500,
parent.winfo_rootx() + 10,
parent.winfo_rooty() + (10 if not _htest else 100)))
- #elguavas - config placeholders til config stuff completed
+ # TODO: get fg/bg from theme.
self.bg = '#ffffff'
self.fg = '#000000'
@@ -33,9 +33,9 @@ class TextViewer(Toplevel):
self.protocol("WM_DELETE_WINDOW", self.Ok)
self.parent = parent
self.textView.focus_set()
- #key bindings for this dialog
- self.bind('<Return>',self.Ok) #dismiss dialog
- self.bind('<Escape>',self.Ok) #dismiss dialog
+ # Bind keys for closing this dialog.
+ self.bind('<Return>',self.Ok)
+ self.bind('<Escape>',self.Ok)
self.textView.insert(0.0, text)
self.textView.config(state=DISABLED)
@@ -50,7 +50,7 @@ class TextViewer(Toplevel):
self.buttonOk = Button(frameButtons, text='Close',
command=self.Ok, takefocus=FALSE)
self.scrollbarView = Scrollbar(frameText, orient=VERTICAL,
- takefocus=FALSE, highlightthickness=0)
+ takefocus=FALSE)
self.textView = Text(frameText, wrap=WORD, highlightthickness=0,
fg=self.fg, bg=self.bg)
self.scrollbarView.config(command=self.textView.yview)
@@ -72,14 +72,14 @@ def view_file(parent, title, filename, encoding=None, modal=True):
try:
with open(filename, 'r', encoding=encoding) as file:
contents = file.read()
- except IOError:
- tkMessageBox.showerror(title='File Load Error',
- message='Unable to load file %r .' % filename,
- parent=parent)
+ except OSError:
+ showerror(title='File Load Error',
+ message='Unable to load file %r .' % filename,
+ parent=parent)
except UnicodeDecodeError as err:
- tkMessageBox.showerror(title='Unicode Decode Error',
- message=str(err),
- parent=parent)
+ showerror(title='Unicode Decode Error',
+ message=str(err),
+ parent=parent)
else:
return view_text(parent, title, contents, modal)
diff --git a/Lib/idlelib/ToolTip.py b/Lib/idlelib/tooltip.py
index 964107e..843fb4a 100644
--- a/Lib/idlelib/ToolTip.py
+++ b/Lib/idlelib/tooltip.py
@@ -1,4 +1,4 @@
-# general purpose 'tooltip' routines - currently unused in idlefork
+# general purpose 'tooltip' routines - currently unused in idlelib
# (although the 'calltips' extension is partly based on this code)
# may be useful for some purposes in (or almost in ;) the current project scope
# Ideas gleaned from PySol
@@ -76,21 +76,20 @@ class ListboxToolTip(ToolTipBase):
for item in self.items:
listbox.insert(END, item)
-def _tooltip(parent):
- root = Tk()
- root.title("Test tooltip")
- width, height, x, y = list(map(int, re.split('[x+]', parent.geometry())))
- root.geometry("+%d+%d"%(x, y + 150))
- label = Label(root, text="Place your mouse over buttons")
+def _tooltip(parent): # htest #
+ top = Toplevel(parent)
+ top.title("Test tooltip")
+ x, y = map(int, parent.geometry().split('+')[1:])
+ top.geometry("+%d+%d" % (x, y + 150))
+ label = Label(top, text="Place your mouse over buttons")
label.pack()
- button1 = Button(root, text="Button 1")
- button2 = Button(root, text="Button 2")
+ button1 = Button(top, text="Button 1")
+ button2 = Button(top, text="Button 2")
button1.pack()
button2.pack()
ToolTip(button1, "This is tooltip text for button1.")
ListboxToolTip(button2, ["This is","multiple line",
"tooltip text","for button2"])
- root.mainloop()
if __name__ == '__main__':
from idlelib.idle_test.htest import run
diff --git a/Lib/idlelib/TreeWidget.py b/Lib/idlelib/tree.py
index a19578f..292ce36 100644
--- a/Lib/idlelib/TreeWidget.py
+++ b/Lib/idlelib/tree.py
@@ -15,10 +15,12 @@
# - optimize tree redraw after expand of subnode
import os
+
from tkinter import *
+from tkinter.ttk import Scrollbar
-from idlelib import ZoomHeight
-from idlelib.configHandler import idleConf
+from idlelib.config import idleConf
+from idlelib import zoomheight
ICONDIR = "Icons"
@@ -445,22 +447,21 @@ class ScrolledCanvas:
self.canvas.yview_scroll(1, "unit")
return "break"
def zoom_height(self, event):
- ZoomHeight.zoom_height(self.master)
+ zoomheight.zoom_height(self.master)
return "break"
-def _tree_widget(parent):
- root = Tk()
- root.title("Test TreeWidget")
- width, height, x, y = list(map(int, re.split('[x+]', parent.geometry())))
- root.geometry("+%d+%d"%(x, y + 150))
- sc = ScrolledCanvas(root, bg="white", highlightthickness=0, takefocus=1)
+def _tree_widget(parent): # htest #
+ top = Toplevel(parent)
+ x, y = map(int, parent.geometry().split('+')[1:])
+ top.geometry("+%d+%d" % (x+50, y+175))
+ sc = ScrolledCanvas(top, bg="white", highlightthickness=0, takefocus=1)
sc.frame.pack(expand=1, fill="both", side=LEFT)
- item = FileTreeItem(os.getcwd())
+ item = FileTreeItem(ICONDIR)
node = TreeNode(sc.canvas, None, item)
node.expand()
- root.mainloop()
if __name__ == '__main__':
+ # test_tree is currently a copy of this
from idlelib.idle_test.htest import run
run(_tree_widget)
diff --git a/Lib/idlelib/UndoDelegator.py b/Lib/idlelib/undo.py
index 1c2502d..4332f10 100644
--- a/Lib/idlelib/UndoDelegator.py
+++ b/Lib/idlelib/undo.py
@@ -1,7 +1,9 @@
import string
-from tkinter import *
-from idlelib.Delegator import Delegator
+from idlelib.delegator import Delegator
+
+# tkintter import not needed because module does not create widgets,
+# although many methods operate on text widget arguments.
#$ event <<redo>>
#$ win <Control-y>
@@ -158,7 +160,6 @@ class UndoDelegator(Delegator):
class Command:
-
# Base class for Undoable commands
tags = None
@@ -204,7 +205,6 @@ class Command:
class InsertCommand(Command):
-
# Undoable insert command
def __init__(self, index1, chars, tags=None):
@@ -262,7 +262,6 @@ class InsertCommand(Command):
class DeleteCommand(Command):
-
# Undoable delete command
def __init__(self, index1, index2=None):
@@ -297,8 +296,8 @@ class DeleteCommand(Command):
text.see('insert')
##sys.__stderr__.write("undo: %s\n" % self)
-class CommandSequence(Command):
+class CommandSequence(Command):
# Wrapper for a sequence of undoable cmds to be undone/redone
# as a unit
@@ -338,13 +337,12 @@ class CommandSequence(Command):
def _undo_delegator(parent): # htest #
- import re
- import tkinter as tk
- from idlelib.Percolator import Percolator
- undowin = tk.Toplevel()
+ from tkinter import Toplevel, Text, Button
+ from idlelib.percolator import Percolator
+ undowin = Toplevel(parent)
undowin.title("Test UndoDelegator")
- width, height, x, y = list(map(int, re.split('[x+]', parent.geometry())))
- undowin.geometry("+%d+%d"%(x, y + 150))
+ x, y = map(int, parent.geometry().split('+')[1:])
+ undowin.geometry("+%d+%d" % (x, y + 175))
text = Text(undowin, height=10)
text.pack()
@@ -362,7 +360,7 @@ def _undo_delegator(parent): # htest #
if __name__ == "__main__":
import unittest
- unittest.main('idlelib.idle_test.test_undodelegator', verbosity=2,
- exit=False)
+ unittest.main('idlelib.idle_test.test_undo', verbosity=2, exit=False)
+
from idlelib.idle_test.htest import run
run(_undo_delegator)
diff --git a/Lib/idlelib/WindowList.py b/Lib/idlelib/windows.py
index bc74348..a3f858a 100644
--- a/Lib/idlelib/WindowList.py
+++ b/Lib/idlelib/windows.py
@@ -1,5 +1,6 @@
from tkinter import *
+
class WindowList:
def __init__(self):
@@ -48,6 +49,7 @@ class WindowList:
t, v, tb = sys.exc_info()
print("warning: callback failed in WindowList", t, ":", v)
+
registry = WindowList()
add_windows_to_menu = registry.add_windows_to_menu
diff --git a/Lib/idlelib/ZoomHeight.py b/Lib/idlelib/zoomheight.py
index a5d679e..aa4a427 100644
--- a/Lib/idlelib/ZoomHeight.py
+++ b/Lib/idlelib/zoomheight.py
@@ -3,7 +3,8 @@
import re
import sys
-from idlelib import macosxSupport
+from idlelib import macosx
+
class ZoomHeight:
@@ -20,6 +21,7 @@ class ZoomHeight:
top = self.editwin.top
zoom_height(top)
+
def zoom_height(top):
geom = top.wm_geometry()
m = re.match(r"(\d+)x(\d+)\+(-?\d+)\+(-?\d+)", geom)
@@ -32,7 +34,7 @@ def zoom_height(top):
newy = 0
newheight = newheight - 72
- elif macosxSupport.isAquaTk():
+ elif macosx.isAquaTk():
# The '88' below is a magic number that avoids placing the bottom
# of the window below the panel on my machine. I don't know how
# to calculate the correct value for this with tkinter.
diff --git a/Lib/imaplib.py b/Lib/imaplib.py
index 2f7e933..2fa9012 100644
--- a/Lib/imaplib.py
+++ b/Lib/imaplib.py
@@ -111,7 +111,15 @@ InternalDate = re.compile(br'.*INTERNALDATE "'
# Literal is no longer used; kept for backward compatibility.
Literal = re.compile(br'.*{(?P<size>\d+)}$', re.ASCII)
MapCRLF = re.compile(br'\r\n|\r|\n')
-Response_code = re.compile(br'\[(?P<type>[A-Z-]+)( (?P<data>[^\]]*))?\]')
+# We no longer exclude the ']' character from the data portion of the response
+# code, even though it violates the RFC. Popular IMAP servers such as Gmail
+# allow flags with ']', and there are programs (including imaplib!) that can
+# produce them. The problem with this is if the 'text' portion of the response
+# includes a ']' we'll parse the response wrong (which is the point of the RFC
+# restriction). However, that seems less likely to be a problem in practice
+# than being unable to correctly parse flags that include ']' chars, which
+# was reported as a real-world problem in issue #21815.
+Response_code = re.compile(br'\[(?P<type>[A-Z-]+)( (?P<data>.*))?\]')
Untagged_response = re.compile(br'\* (?P<type>[A-Z-]+)( (?P<data>.*))?')
# Untagged_status is no longer used; kept for backward compatibility
Untagged_status = re.compile(
@@ -124,7 +132,7 @@ _Untagged_status = br'\* (?P<data>\d+) (?P<type>[A-Z-]+)( (?P<data2>.*))?'
class IMAP4:
- """IMAP4 client class.
+ r"""IMAP4 client class.
Instantiate with: IMAP4([host[, port]])
@@ -1259,7 +1267,10 @@ if HAVE_SSL:
if ssl_context is not None and certfile is not None:
raise ValueError("ssl_context and certfile arguments are mutually "
"exclusive")
-
+ if keyfile is not None or certfile is not None:
+ import warnings
+ warnings.warn("keyfile and certfile are deprecated, use a"
+ "custom ssl_context instead", DeprecationWarning, 2)
self.keyfile = keyfile
self.certfile = certfile
if ssl_context is None:
@@ -1527,7 +1538,7 @@ if __name__ == '__main__':
('select', ('/tmp/yyz 2',)),
('search', (None, 'SUBJECT', 'test')),
('fetch', ('1', '(FLAGS INTERNALDATE RFC822)')),
- ('store', ('1', 'FLAGS', '(\Deleted)')),
+ ('store', ('1', 'FLAGS', r'(\Deleted)')),
('namespace', ()),
('expunge', ()),
('recent', ()),
diff --git a/Lib/imghdr.py b/Lib/imghdr.py
index b267925..76e8abb 100644
--- a/Lib/imghdr.py
+++ b/Lib/imghdr.py
@@ -1,5 +1,7 @@
"""Recognize image file formats based on their first few bytes."""
+from os import PathLike
+
__all__ = ["what"]
#-------------------------#
@@ -10,7 +12,7 @@ def what(file, h=None):
f = None
try:
if h is None:
- if isinstance(file, str):
+ if isinstance(file, (str, PathLike)):
f = open(file, 'rb')
h = f.read(32)
else:
diff --git a/Lib/imp.py b/Lib/imp.py
index e264391..781ff23 100644
--- a/Lib/imp.py
+++ b/Lib/imp.py
@@ -30,7 +30,7 @@ import warnings
warnings.warn("the imp module is deprecated in favour of importlib; "
"see the module's documentation for alternative uses",
- PendingDeprecationWarning, stacklevel=2)
+ DeprecationWarning, stacklevel=2)
# DEPRECATED
SEARCH_ERROR = 0
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
index 9eecbfe..a531a03 100644
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -36,23 +36,6 @@ def _new_module(name):
return type(sys)(name)
-class _ManageReload:
-
- """Manages the possible clean-up of sys.modules for load_module()."""
-
- def __init__(self, name):
- self._name = name
-
- def __enter__(self):
- self._is_reload = self._name in sys.modules
-
- def __exit__(self, *args):
- if any(arg is not None for arg in args) and not self._is_reload:
- try:
- del sys.modules[self._name]
- except KeyError:
- pass
-
# Module-level locking ########################################################
# A dict mapping module names to weakrefs of _ModuleLock instances
@@ -270,7 +253,7 @@ def _load_module_shim(self, fullname):
# Module specifications #######################################################
def _module_repr(module):
- # The implementation of ModuleType__repr__().
+ # The implementation of ModuleType.__repr__().
loader = getattr(module, '__loader__', None)
if hasattr(loader, 'module_repr'):
# As soon as BuiltinImporter, FrozenImporter, and NamespaceLoader
@@ -576,9 +559,8 @@ def module_from_spec(spec):
# module creation should be used.
module = spec.loader.create_module(spec)
elif hasattr(spec.loader, 'exec_module'):
- _warnings.warn('starting in Python 3.6, loaders defining exec_module() '
- 'must also define create_module()',
- DeprecationWarning, stacklevel=2)
+ raise ImportError('loaders that define exec_module() '
+ 'must also define create_module()')
if module is None:
module = _new_module(spec.name)
_init_module_attrs(spec, module)
@@ -603,7 +585,7 @@ def _module_repr_from_spec(spec):
# Used by importlib.reload() and _load_module_shim().
def _exec(spec, module):
- """Execute the spec in an existing module's namespace."""
+ """Execute the spec's specified module in an existing module's namespace."""
name = spec.name
_imp.acquire_lock()
with _ModuleLockManager(name):
@@ -877,14 +859,21 @@ def _find_spec_legacy(finder, name, path):
def _find_spec(name, path, target=None):
- """Find a module's loader."""
- if sys.meta_path is not None and not sys.meta_path:
+ """Find a module's spec."""
+ meta_path = sys.meta_path
+ if meta_path is None:
+ # PyImport_Cleanup() is running or has been called.
+ raise ImportError("sys.meta_path is None, Python is likely "
+ "shutting down")
+
+ if not meta_path:
_warnings.warn('sys.meta_path is empty', ImportWarning)
+
# We check sys.modules here for the reload case. While a passed-in
# target will usually indicate a reload there is no guarantee, whereas
# sys.modules provides one.
is_reload = name in sys.modules
- for finder in sys.meta_path:
+ for finder in meta_path:
with _ImportLockContext():
try:
find_spec = finder.find_spec
@@ -925,6 +914,9 @@ def _sanity_check(name, package, level):
if level > 0:
if not isinstance(package, str):
raise TypeError('__package__ not set to a string')
+ elif not package:
+ raise ImportError('attempted relative import with no known parent '
+ 'package')
elif package not in sys.modules:
msg = ('Parent module {!r} not loaded, cannot perform relative '
'import')
@@ -950,10 +942,10 @@ def _find_and_load_unlocked(name, import_):
path = parent_module.__path__
except AttributeError:
msg = (_ERR_MSG + '; {!r} is not a package').format(name, parent)
- raise ImportError(msg, name=name) from None
+ raise ModuleNotFoundError(msg, name=name) from None
spec = _find_spec(name, path)
if spec is None:
- raise ImportError(_ERR_MSG.format(name), name=name)
+ raise ModuleNotFoundError(_ERR_MSG.format(name), name=name)
else:
module = _load_unlocked(spec)
if parent:
@@ -989,10 +981,11 @@ def _gcd_import(name, package=None, level=0):
_imp.release_lock()
message = ('import of {} halted; '
'None in sys.modules'.format(name))
- raise ImportError(message, name=name)
+ raise ModuleNotFoundError(message, name=name)
_lock_unlock_module(name)
return module
+
def _handle_fromlist(module, fromlist, import_):
"""Figure out what __import__ should return.
@@ -1014,13 +1007,12 @@ def _handle_fromlist(module, fromlist, import_):
from_name = '{}.{}'.format(module.__name__, x)
try:
_call_with_frames_removed(import_, from_name)
- except ImportError as exc:
+ except ModuleNotFoundError as exc:
# Backwards-compatibility dictates we ignore failed
# imports triggered by fromlist for modules that don't
# exist.
- if str(exc).startswith(_ERR_MSG_PREFIX):
- if exc.name == from_name:
- continue
+ if exc.name == from_name:
+ continue
raise
return module
@@ -1033,7 +1025,19 @@ def _calc___package__(globals):
"""
package = globals.get('__package__')
- if package is None:
+ spec = globals.get('__spec__')
+ if package is not None:
+ if spec is not None and package != spec.parent:
+ _warnings.warn("__package__ != __spec__.parent "
+ f"({package!r} != {spec.parent!r})",
+ ImportWarning, stacklevel=3)
+ return package
+ elif spec is not None:
+ return spec.parent
+ else:
+ _warnings.warn("can't resolve package from __spec__ or __package__, "
+ "falling back on __name__ and __path__",
+ ImportWarning, stacklevel=3)
package = globals['__name__']
if '__path__' not in globals:
package = package.rpartition('.')[0]
diff --git a/Lib/importlib/_bootstrap_external.py b/Lib/importlib/_bootstrap_external.py
index e54d691..9feec50 100644
--- a/Lib/importlib/_bootstrap_external.py
+++ b/Lib/importlib/_bootstrap_external.py
@@ -137,10 +137,6 @@ _code_type = type(_write_atomic.__code__)
# a .pyc file in text mode the magic number will be wrong; also, the
# Apple MPW compiler swaps their values, botching string constants.
#
-# The magic numbers must be spaced apart at least 2 values, as the
-# -U interpeter flag will cause MAGIC+1 being used. They have been
-# odd numbers for some time now.
-#
# There were a variety of old schemes for setting the magic number.
# The current working scheme is to increment the previous value by
# 10.
@@ -231,6 +227,19 @@ _code_type = type(_write_atomic.__code__)
# Python 3.5b2 3340 (fix dictionary display evaluation order #11205)
# Python 3.5b2 3350 (add GET_YIELD_FROM_ITER opcode #24400)
# Python 3.5.2 3351 (fix BUILD_MAP_UNPACK_WITH_CALL opcode #27286)
+# Python 3.6a0 3360 (add FORMAT_VALUE opcode #25483
+# Python 3.6a0 3361 (lineno delta of code.co_lnotab becomes signed)
+# Python 3.6a1 3370 (16 bit wordcode)
+# Python 3.6a1 3371 (add BUILD_CONST_KEY_MAP opcode #27140)
+# Python 3.6a1 3372 (MAKE_FUNCTION simplification, remove MAKE_CLOSURE
+# #27095)
+# Python 3.6b1 3373 (add BUILD_STRING opcode #27078)
+# Python 3.6b1 3375 (add SETUP_ANNOTATIONS and STORE_ANNOTATION opcodes
+# #27985)
+# Python 3.6b1 3376 (simplify CALL_FUNCTIONs & BUILD_MAP_UNPACK_WITH_CALL)
+# Python 3.6b1 3377 (set __class__ cell from type.__new__ #23722)
+# Python 3.6b2 3378 (add BUILD_TUPLE_UNPACK_WITH_CALL #28257)
+# Python 3.6rc1 3379 (more thorough __class__ validation #23722)
#
# MAGIC must change whenever the bytecode emitted by the compiler may no
# longer be understood by older implementations of the eval loop (usually
@@ -239,7 +248,7 @@ _code_type = type(_write_atomic.__code__)
# Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array
# in PC/launcher.c must also be updated.
-MAGIC_NUMBER = (3351).to_bytes(2, 'little') + b'\r\n'
+MAGIC_NUMBER = (3379).to_bytes(2, 'little') + b'\r\n'
_RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c
_PYCACHE = '__pycache__'
@@ -276,6 +285,7 @@ def cache_from_source(path, debug_override=None, *, optimization=None):
message = 'debug_override or optimization must be set to None'
raise TypeError(message)
optimization = '' if debug_override else 1
+ path = _os.fspath(path)
head, tail = _path_split(path)
base, sep, rest = tail.rpartition('.')
tag = sys.implementation.cache_tag
@@ -306,6 +316,7 @@ def source_from_cache(path):
"""
if sys.implementation.cache_tag is None:
raise NotImplementedError('sys.implementation.cache_tag is None')
+ path = _os.fspath(path)
head, pycache_filename = _path_split(path)
head, pycache = _path_split(head)
if pycache != _PYCACHE:
@@ -371,14 +382,6 @@ def _calc_mode(path):
return mode
-def _verbose_message(message, *args, verbosity=1):
- """Print the message to stderr if -v/PYTHONVERBOSE is turned on."""
- if sys.flags.verbose >= verbosity:
- if not message.startswith(('#', 'import ')):
- message = '# ' + message
- print(message.format(*args), file=sys.stderr)
-
-
def _check_name(method):
"""Decorator to verify that the module being requested matches the one the
loader can handle.
@@ -448,15 +451,15 @@ def _validate_bytecode_header(data, source_stats=None, name=None, path=None):
raw_size = data[8:12]
if magic != MAGIC_NUMBER:
message = 'bad magic number in {!r}: {!r}'.format(name, magic)
- _verbose_message('{}', message)
+ _bootstrap._verbose_message('{}', message)
raise ImportError(message, **exc_details)
elif len(raw_timestamp) != 4:
message = 'reached EOF while reading timestamp in {!r}'.format(name)
- _verbose_message('{}', message)
+ _bootstrap._verbose_message('{}', message)
raise EOFError(message)
elif len(raw_size) != 4:
message = 'reached EOF while reading size of source in {!r}'.format(name)
- _verbose_message('{}', message)
+ _bootstrap._verbose_message('{}', message)
raise EOFError(message)
if source_stats is not None:
try:
@@ -466,7 +469,7 @@ def _validate_bytecode_header(data, source_stats=None, name=None, path=None):
else:
if _r_long(raw_timestamp) != source_mtime:
message = 'bytecode is stale for {!r}'.format(name)
- _verbose_message('{}', message)
+ _bootstrap._verbose_message('{}', message)
raise ImportError(message, **exc_details)
try:
source_size = source_stats['size'] & 0xFFFFFFFF
@@ -483,7 +486,7 @@ def _compile_bytecode(data, name=None, bytecode_path=None, source_path=None):
"""Compile bytecode as returned by _validate_bytecode_header()."""
code = marshal.loads(data)
if isinstance(code, _code_type):
- _verbose_message('code object from {!r}', bytecode_path)
+ _bootstrap._verbose_message('code object from {!r}', bytecode_path)
if source_path is not None:
_imp._fix_co_filename(code, source_path)
return code
@@ -541,6 +544,8 @@ def spec_from_file_location(name, location=None, *, loader=None,
location = loader.get_filename(name)
except ImportError:
pass
+ else:
+ location = _os.fspath(location)
# If the location is on the filesystem, but doesn't actually exist,
# we could return None here, indicating that the location is not
@@ -610,7 +615,7 @@ class WindowsRegistryFinder:
else:
registry_key = cls.REGISTRY_KEY
key = registry_key.format(fullname=fullname,
- sys_version=sys.version[:3])
+ sys_version='%d.%d' % sys.version_info[:2])
try:
with cls._open_registry(key) as hkey:
filepath = _winreg.QueryValue(hkey, '')
@@ -673,6 +678,7 @@ class _LoaderBasics:
_bootstrap._call_with_frames_removed(exec, code, module.__dict__)
def load_module(self, fullname):
+ """This module is deprecated."""
return _bootstrap._load_module_shim(self, fullname)
@@ -766,21 +772,21 @@ class SourceLoader(_LoaderBasics):
except (ImportError, EOFError):
pass
else:
- _verbose_message('{} matches {}', bytecode_path,
- source_path)
+ _bootstrap._verbose_message('{} matches {}', bytecode_path,
+ source_path)
return _compile_bytecode(bytes_data, name=fullname,
bytecode_path=bytecode_path,
source_path=source_path)
source_bytes = self.get_data(source_path)
code_object = self.source_to_code(source_bytes, source_path)
- _verbose_message('code object from {}', source_path)
+ _bootstrap._verbose_message('code object from {}', source_path)
if (not sys.dont_write_bytecode and bytecode_path is not None and
source_mtime is not None):
data = _code_to_bytecode(code_object, source_mtime,
len(source_bytes))
try:
self._cache_bytecode(source_path, bytecode_path, data)
- _verbose_message('wrote {!r}', bytecode_path)
+ _bootstrap._verbose_message('wrote {!r}', bytecode_path)
except NotImplementedError:
pass
return code_object
@@ -860,14 +866,16 @@ class SourceFileLoader(FileLoader, SourceLoader):
except OSError as exc:
# Could be a permission error, read-only filesystem: just forget
# about writing the data.
- _verbose_message('could not create {!r}: {!r}', parent, exc)
+ _bootstrap._verbose_message('could not create {!r}: {!r}',
+ parent, exc)
return
try:
_write_atomic(path, data, _mode)
- _verbose_message('created {!r}', path)
+ _bootstrap._verbose_message('created {!r}', path)
except OSError as exc:
# Same as above: just don't write the bytecode.
- _verbose_message('could not create {!r}: {!r}', path, exc)
+ _bootstrap._verbose_message('could not create {!r}: {!r}', path,
+ exc)
class SourcelessFileLoader(FileLoader, _LoaderBasics):
@@ -912,14 +920,14 @@ class ExtensionFileLoader(FileLoader, _LoaderBasics):
"""Create an unitialized extension module"""
module = _bootstrap._call_with_frames_removed(
_imp.create_dynamic, spec)
- _verbose_message('extension module {!r} loaded from {!r}',
+ _bootstrap._verbose_message('extension module {!r} loaded from {!r}',
spec.name, self.path)
return module
def exec_module(self, module):
"""Initialize an extension module"""
_bootstrap._call_with_frames_removed(_imp.exec_dynamic, module)
- _verbose_message('extension module {!r} executed from {!r}',
+ _bootstrap._verbose_message('extension module {!r} executed from {!r}',
self.name, self.path)
def is_package(self, fullname):
@@ -985,6 +993,9 @@ class _NamespacePath:
def __iter__(self):
return iter(self._recalculate())
+ def __setitem__(self, index, path):
+ self._path[index] = path
+
def __len__(self):
return len(self._recalculate())
@@ -1034,7 +1045,8 @@ class _NamespaceLoader:
"""
# The import system never calls this method.
- _verbose_message('namespace module loaded with path {!r}', self._path)
+ _bootstrap._verbose_message('namespace module loaded with path {!r}',
+ self._path)
return _bootstrap._load_module_shim(self, fullname)
@@ -1054,11 +1066,7 @@ class PathFinder:
@classmethod
def _path_hooks(cls, path):
- """Search sequence of hooks for a finder for 'path'.
-
- If 'hooks' is false then use sys.path_hooks.
-
- """
+ """Search sys.path_hooks for a finder for 'path'."""
if sys.path_hooks is not None and not sys.path_hooks:
_warnings.warn('sys.path_hooks is empty', ImportWarning)
for hook in sys.path_hooks:
@@ -1140,8 +1148,10 @@ class PathFinder:
@classmethod
def find_spec(cls, fullname, path=None, target=None):
- """find the module on sys.path or 'path' based on sys.path_hooks and
- sys.path_importer_cache."""
+ """Try to find a spec for 'fullname' on sys.path or 'path'.
+
+ The search is based on sys.path_hooks and sys.path_importer_cache.
+ """
if path is None:
path = sys.path
spec = cls._get_spec(fullname, path, target)
@@ -1221,8 +1231,10 @@ class FileFinder:
submodule_search_locations=smsl)
def find_spec(self, fullname, target=None):
- """Try to find a spec for the specified module. Returns the
- matching spec, or None if not found."""
+ """Try to find a spec for the specified module.
+
+ Returns the matching spec, or None if not found.
+ """
is_namespace = False
tail_module = fullname.rpartition('.')[2]
try:
@@ -1254,12 +1266,13 @@ class FileFinder:
# Check for a file w/ a proper suffix exists.
for suffix, loader_class in self._loaders:
full_path = _path_join(self.path, tail_module + suffix)
- _verbose_message('trying {}'.format(full_path), verbosity=2)
+ _bootstrap._verbose_message('trying {}', full_path, verbosity=2)
if cache_module + suffix in cache:
if _path_isfile(full_path):
- return self._get_spec(loader_class, fullname, full_path, None, target)
+ return self._get_spec(loader_class, fullname, full_path,
+ None, target)
if is_namespace:
- _verbose_message('possible namespace for {}'.format(base_path))
+ _bootstrap._verbose_message('possible namespace for {}', base_path)
spec = _bootstrap.ModuleSpec(fullname, None)
spec.submodule_search_locations = [base_path]
return spec
@@ -1427,11 +1440,4 @@ def _install(_bootstrap_module):
_setup(_bootstrap_module)
supported_loaders = _get_supported_file_loaders()
sys.path_hooks.extend([FileFinder.path_hook(*supported_loaders)])
- if _os.__name__ == 'nt':
- sys.meta_path.append(WindowsRegistryFinder)
sys.meta_path.append(PathFinder)
-
- # XXX We expose a couple of classes in _bootstrap for the sake of
- # a setuptools bug (https://bitbucket.org/pypa/setuptools/issue/378).
- _bootstrap_module.FileFinder = FileFinder
- _bootstrap_module.SourceFileLoader = SourceFileLoader
diff --git a/Lib/importlib/util.py b/Lib/importlib/util.py
index e1fa07a..6bdf0d4 100644
--- a/Lib/importlib/util.py
+++ b/Lib/importlib/util.py
@@ -22,8 +22,8 @@ def resolve_name(name, package):
if not name.startswith('.'):
return name
elif not package:
- raise ValueError('{!r} is not a relative name '
- '(no leading dot)'.format(name))
+ raise ValueError(f'no package specified for {repr(name)} '
+ '(required for relative module names)')
level = 0
for character in name:
if character != '.':
@@ -204,11 +204,6 @@ def module_for_loader(fxn):
return module_for_loader_wrapper
-class _Module(types.ModuleType):
-
- """A subclass of the module type to allow __class__ manipulation."""
-
-
class _LazyModule(types.ModuleType):
"""A subclass of the module type which triggers loading upon attribute access."""
@@ -218,13 +213,14 @@ class _LazyModule(types.ModuleType):
# All module metadata must be garnered from __spec__ in order to avoid
# using mutated values.
# Stop triggering this method.
- self.__class__ = _Module
+ self.__class__ = types.ModuleType
# Get the original name to make sure no object substitution occurred
# in sys.modules.
original_name = self.__spec__.name
# Figure out exactly what attributes were mutated between the creation
# of the module and now.
- attrs_then = self.__spec__.loader_state
+ attrs_then = self.__spec__.loader_state['__dict__']
+ original_type = self.__spec__.loader_state['__class__']
attrs_now = self.__dict__
attrs_updated = {}
for key, value in attrs_now.items():
@@ -239,9 +235,9 @@ class _LazyModule(types.ModuleType):
# object was put into sys.modules.
if original_name in sys.modules:
if id(self) != id(sys.modules[original_name]):
- msg = ('module object for {!r} substituted in sys.modules '
- 'during a lazy load')
- raise ValueError(msg.format(original_name))
+ raise ValueError(f"module object for {original_name!r} "
+ "substituted in sys.modules during a lazy "
+ "load")
# Update after loading since that's what would happen in an eager
# loading situation.
self.__dict__.update(attrs_updated)
@@ -275,8 +271,7 @@ class LazyLoader(abc.Loader):
self.loader = loader
def create_module(self, spec):
- """Create a module which can have its __class__ manipulated."""
- return _Module(spec.name)
+ return self.loader.create_module(spec)
def exec_module(self, module):
"""Make the module load lazily."""
@@ -286,5 +281,8 @@ class LazyLoader(abc.Loader):
# on an object would have triggered the load,
# e.g. ``module.__spec__.loader = None`` would trigger a load from
# trying to access module.__spec__.
- module.__spec__.loader_state = module.__dict__.copy()
+ loader_state = {}
+ loader_state['__dict__'] = module.__dict__.copy()
+ loader_state['__class__'] = module.__class__
+ module.__spec__.loader_state = loader_state
module.__class__ = _LazyModule
diff --git a/Lib/inspect.py b/Lib/inspect.py
index 9f9fcfe..4d56ef5 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -16,7 +16,7 @@ Here are some of the useful functions provided by this module:
getmodule() - determine the module that an object came from
getclasstree() - arrange classes so as to represent their hierarchy
- getargspec(), getargvalues(), getcallargs() - get info about function arguments
+ getargvalues(), getcallargs() - get info about function arguments
getfullargspec() - same, with support for Python 3 features
formatargspec(), formatargvalues() - format an argument spec
getouterframes(), getinnerframes() - get info about frames
@@ -179,12 +179,24 @@ def isgeneratorfunction(object):
def iscoroutinefunction(object):
"""Return true if the object is a coroutine function.
- Coroutine functions are defined with "async def" syntax,
- or generators decorated with "types.coroutine".
+ Coroutine functions are defined with "async def" syntax.
"""
return bool((isfunction(object) or ismethod(object)) and
object.__code__.co_flags & CO_COROUTINE)
+def isasyncgenfunction(object):
+ """Return true if the object is an asynchronous generator function.
+
+ Asynchronous generator functions are defined with "async def"
+ syntax and have "yield" expressions in their body.
+ """
+ return bool((isfunction(object) or ismethod(object)) and
+ object.__code__.co_flags & CO_ASYNC_GENERATOR)
+
+def isasyncgen(object):
+ """Return true if the object is an asynchronous generator."""
+ return isinstance(object, types.AsyncGeneratorType)
+
def isgenerator(object):
"""Return true if the object is a generator.
@@ -623,23 +635,6 @@ def getfile(object):
raise TypeError('{!r} is not a module, class, method, '
'function, traceback, frame, or code object'.format(object))
-ModuleInfo = namedtuple('ModuleInfo', 'name suffix mode module_type')
-
-def getmoduleinfo(path):
- """Get the module name, suffix, mode, and module type for a given file."""
- warnings.warn('inspect.getmoduleinfo() is deprecated', DeprecationWarning,
- 2)
- with warnings.catch_warnings():
- warnings.simplefilter('ignore', PendingDeprecationWarning)
- import imp
- filename = os.path.basename(path)
- suffixes = [(-len(suffix), suffix, mode, mtype)
- for suffix, mode, mtype in imp.get_suffixes()]
- suffixes.sort() # try longest suffixes first, in case they overlap
- for neglen, suffix, mode, mtype in suffixes:
- if filename[neglen:] == suffix:
- return ModuleInfo(filename[:neglen], suffix, mode, mtype)
-
def getmodulename(path):
"""Return the module name for a given file, or None."""
fname = os.path.basename(path)
@@ -1024,24 +1019,30 @@ def _getfullargs(co):
ArgSpec = namedtuple('ArgSpec', 'args varargs keywords defaults')
def getargspec(func):
- """Get the names and default values of a function's arguments.
+ """Get the names and default values of a function's parameters.
A tuple of four things is returned: (args, varargs, keywords, defaults).
'args' is a list of the argument names, including keyword-only argument names.
- 'varargs' and 'keywords' are the names of the * and ** arguments or None.
- 'defaults' is an n-tuple of the default values of the last n arguments.
+ 'varargs' and 'keywords' are the names of the * and ** parameters or None.
+ 'defaults' is an n-tuple of the default values of the last n parameters.
- Use the getfullargspec() API for Python 3 code, as annotations
- and keyword arguments are supported. getargspec() will raise ValueError
- if the func has either annotations or keyword arguments.
+ This function is deprecated, as it does not support annotations or
+ keyword-only parameters and will raise ValueError if either is present
+ on the supplied callable.
+
+ For a more structured introspection API, use inspect.signature() instead.
+
+ Alternatively, use getfullargspec() for an API with a similar namedtuple
+ based interface, but full support for annotations and keyword-only
+ parameters.
"""
warnings.warn("inspect.getargspec() is deprecated, "
- "use inspect.signature() instead", DeprecationWarning,
- stacklevel=2)
+ "use inspect.signature() or inspect.getfullargspec()",
+ DeprecationWarning, stacklevel=2)
args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = \
getfullargspec(func)
if kwonlyargs or ann:
- raise ValueError("Function has keyword-only arguments or annotations"
+ raise ValueError("Function has keyword-only parameters or annotations"
", use getfullargspec() API which can support them")
return ArgSpec(args, varargs, varkw, defaults)
@@ -1049,20 +1050,20 @@ FullArgSpec = namedtuple('FullArgSpec',
'args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations')
def getfullargspec(func):
- """Get the names and default values of a callable object's arguments.
+ """Get the names and default values of a callable object's parameters.
A tuple of seven things is returned:
- (args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults annotations).
- 'args' is a list of the argument names.
- 'varargs' and 'varkw' are the names of the * and ** arguments or None.
- 'defaults' is an n-tuple of the default values of the last n arguments.
- 'kwonlyargs' is a list of keyword-only argument names.
+ (args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations).
+ 'args' is a list of the parameter names.
+ 'varargs' and 'varkw' are the names of the * and ** parameters or None.
+ 'defaults' is an n-tuple of the default values of the last n parameters.
+ 'kwonlyargs' is a list of keyword-only parameter names.
'kwonlydefaults' is a dictionary mapping names from kwonlyargs to defaults.
- 'annotations' is a dictionary mapping argument names to annotations.
-
- The first four items in the tuple correspond to getargspec().
+ 'annotations' is a dictionary mapping parameter names to annotations.
- This function is deprecated, use inspect.signature() instead.
+ Notable differences from inspect.signature():
+ - the "self" parameter is always reported, even for bound methods
+ - wrapper chains defined by __wrapped__ *not* unwrapped automatically
"""
try:
@@ -1174,8 +1175,7 @@ def formatargspec(args, varargs=None, varkw=None, defaults=None,
formatvalue=lambda value: '=' + repr(value),
formatreturns=lambda text: ' -> ' + text,
formatannotation=formatannotation):
- """Format an argument spec from the values returned by getargspec
- or getfullargspec.
+ """Format an argument spec from the values returned by getfullargspec.
The first seven arguments are (args, varargs, varkw, defaults,
kwonlyargs, kwonlydefaults, annotations). The other five arguments
@@ -2416,6 +2416,20 @@ class Parameter:
if not isinstance(name, str):
raise TypeError("name must be a str, not a {!r}".format(name))
+ if name[0] == '.' and name[1:].isdigit():
+ # These are implicit arguments generated by comprehensions. In
+ # order to provide a friendlier interface to users, we recast
+ # their name as "implicitN" and treat them as positional-only.
+ # See issue 19611.
+ if kind != _POSITIONAL_OR_KEYWORD:
+ raise ValueError(
+ 'implicit arguments must be passed in as {}'.format(
+ _POSITIONAL_OR_KEYWORD
+ )
+ )
+ self._kind = _POSITIONAL_ONLY
+ name = 'implicit{}'.format(name[1:])
+
if not name.isidentifier():
raise ValueError('{!r} is not a valid parameter name'.format(name))
diff --git a/Lib/io.py b/Lib/io.py
index e03db97..968ee50 100644
--- a/Lib/io.py
+++ b/Lib/io.py
@@ -90,3 +90,10 @@ for klass in (BytesIO, BufferedReader, BufferedWriter, BufferedRandom,
for klass in (StringIO, TextIOWrapper):
TextIOBase.register(klass)
del klass
+
+try:
+ from _io import _WindowsConsoleIO
+except ImportError:
+ pass
+else:
+ RawIOBase.register(_WindowsConsoleIO)
diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py
index 1f90058..20f33cb 100644
--- a/Lib/ipaddress.py
+++ b/Lib/ipaddress.py
@@ -636,12 +636,12 @@ class _BaseNetwork(_IPAddressBase):
broadcast = int(self.broadcast_address)
if n >= 0:
if network + n > broadcast:
- raise IndexError
+ raise IndexError('address out of range')
return self._address_class(network + n)
else:
n += 1
if broadcast + n < network:
- raise IndexError
+ raise IndexError('address out of range')
return self._address_class(broadcast + n)
def __lt__(self, other):
diff --git a/Lib/json/__init__.py b/Lib/json/__init__.py
index 1118b0e..b8d5e6c 100644
--- a/Lib/json/__init__.py
+++ b/Lib/json/__init__.py
@@ -105,6 +105,7 @@ __author__ = 'Bob Ippolito <bob@redivi.com>'
from .decoder import JSONDecoder, JSONDecodeError
from .encoder import JSONEncoder
+import codecs
_default_encoder = JSONEncoder(
skipkeys=False,
@@ -116,7 +117,7 @@ _default_encoder = JSONEncoder(
default=None,
)
-def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,
+def dump(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True,
allow_nan=True, cls=None, indent=None, separators=None,
default=None, sort_keys=False, **kw):
"""Serialize ``obj`` as a JSON formatted stream to ``fp`` (a
@@ -179,7 +180,7 @@ def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,
fp.write(chunk)
-def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True,
+def dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True,
allow_nan=True, cls=None, indent=None, separators=None,
default=None, sort_keys=False, **kw):
"""Serialize ``obj`` to a JSON formatted ``str``.
@@ -240,7 +241,37 @@ def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True,
_default_decoder = JSONDecoder(object_hook=None, object_pairs_hook=None)
-def load(fp, cls=None, object_hook=None, parse_float=None,
+def detect_encoding(b):
+ bstartswith = b.startswith
+ if bstartswith((codecs.BOM_UTF32_BE, codecs.BOM_UTF32_LE)):
+ return 'utf-32'
+ if bstartswith((codecs.BOM_UTF16_BE, codecs.BOM_UTF16_LE)):
+ return 'utf-16'
+ if bstartswith(codecs.BOM_UTF8):
+ return 'utf-8-sig'
+
+ if len(b) >= 4:
+ if not b[0]:
+ # 00 00 -- -- - utf-32-be
+ # 00 XX -- -- - utf-16-be
+ return 'utf-16-be' if b[1] else 'utf-32-be'
+ if not b[1]:
+ # XX 00 00 00 - utf-32-le
+ # XX 00 00 XX - utf-16-le
+ # XX 00 XX -- - utf-16-le
+ return 'utf-16-le' if b[2] or b[3] else 'utf-32-le'
+ elif len(b) == 2:
+ if not b[0]:
+ # 00 XX - utf-16-be
+ return 'utf-16-be'
+ if not b[1]:
+ # XX 00 - utf-16-le
+ return 'utf-16-le'
+ # default
+ return 'utf-8'
+
+
+def load(fp, *, cls=None, object_hook=None, parse_float=None,
parse_int=None, parse_constant=None, object_pairs_hook=None, **kw):
"""Deserialize ``fp`` (a ``.read()``-supporting file-like object containing
a JSON document) to a Python object.
@@ -268,10 +299,10 @@ def load(fp, cls=None, object_hook=None, parse_float=None,
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
-def loads(s, encoding=None, cls=None, object_hook=None, parse_float=None,
+def loads(s, *, encoding=None, cls=None, object_hook=None, parse_float=None,
parse_int=None, parse_constant=None, object_pairs_hook=None, **kw):
- """Deserialize ``s`` (a ``str`` instance containing a JSON
- document) to a Python object.
+ """Deserialize ``s`` (a ``str``, ``bytes`` or ``bytearray`` instance
+ containing a JSON document) to a Python object.
``object_hook`` is an optional function that will be called with the
result of any object literal decode (a ``dict``). The return value of
@@ -307,12 +338,16 @@ def loads(s, encoding=None, cls=None, object_hook=None, parse_float=None,
The ``encoding`` argument is ignored and deprecated.
"""
- if not isinstance(s, str):
- raise TypeError('the JSON object must be str, not {!r}'.format(
- s.__class__.__name__))
- if s.startswith(u'\ufeff'):
- raise JSONDecodeError("Unexpected UTF-8 BOM (decode using utf-8-sig)",
- s, 0)
+ if isinstance(s, str):
+ if s.startswith('\ufeff'):
+ raise JSONDecodeError("Unexpected UTF-8 BOM (decode using utf-8-sig)",
+ s, 0)
+ else:
+ if not isinstance(s, (bytes, bytearray)):
+ raise TypeError('the JSON object must be str, bytes or bytearray, '
+ 'not {!r}'.format(s.__class__.__name__))
+ s = s.decode(detect_encoding(s), 'surrogatepass')
+
if (cls is None and object_hook is None and
parse_int is None and parse_float is None and
parse_constant is None and object_pairs_hook is None and not kw):
diff --git a/Lib/json/decoder.py b/Lib/json/decoder.py
index 0f03f20..2422c6a 100644
--- a/Lib/json/decoder.py
+++ b/Lib/json/decoder.py
@@ -280,7 +280,7 @@ class JSONDecoder(object):
"""
- def __init__(self, object_hook=None, parse_float=None,
+ def __init__(self, *, object_hook=None, parse_float=None,
parse_int=None, parse_constant=None, strict=True,
object_pairs_hook=None):
"""``object_hook``, if specified, will be called with the result
diff --git a/Lib/json/encoder.py b/Lib/json/encoder.py
index d596489..41a497c 100644
--- a/Lib/json/encoder.py
+++ b/Lib/json/encoder.py
@@ -101,7 +101,7 @@ class JSONEncoder(object):
"""
item_separator = ', '
key_separator = ': '
- def __init__(self, skipkeys=False, ensure_ascii=True,
+ def __init__(self, *, skipkeys=False, ensure_ascii=True,
check_circular=True, allow_nan=True, sort_keys=False,
indent=None, separators=None, default=None):
"""Constructor for JSONEncoder, with sensible defaults.
@@ -176,7 +176,8 @@ class JSONEncoder(object):
return JSONEncoder.default(self, o)
"""
- raise TypeError(repr(o) + " is not JSON serializable")
+ raise TypeError("Object of type '%s' is not JSON serializable" %
+ o.__class__.__name__)
def encode(self, o):
"""Return a JSON string representation of a Python data structure.
diff --git a/Lib/lib2to3/Grammar.txt b/Lib/lib2to3/Grammar.txt
index 60503dd..2abd5ee 100644
--- a/Lib/lib2to3/Grammar.txt
+++ b/Lib/lib2to3/Grammar.txt
@@ -54,12 +54,13 @@ stmt: simple_stmt | compound_stmt
simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
small_stmt: (expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt |
import_stmt | global_stmt | exec_stmt | assert_stmt)
-expr_stmt: testlist_star_expr (augassign (yield_expr|testlist) |
+expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
('=' (yield_expr|testlist_star_expr))*)
+annassign: ':' test ['=' test]
testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
'<<=' | '>>=' | '**=' | '//=')
-# For normal assignments, additional restrictions enforced by the interpreter
+# For normal and annotated assignments, additional restrictions enforced by the interpreter
print_stmt: 'print' ( [ test (',' test)* [','] ] |
'>>' test [ (',' test)+ [','] ] )
del_stmt: 'del' exprlist
@@ -160,7 +161,7 @@ argument: ( test [comp_for] |
star_expr )
comp_iter: comp_for | comp_if
-comp_for: 'for' exprlist 'in' testlist_safe [comp_iter]
+comp_for: [ASYNC] 'for' exprlist 'in' testlist_safe [comp_iter]
comp_if: 'if' old_test [comp_iter]
testlist1: test (',' test)*
diff --git a/Lib/lib2to3/fixer_util.py b/Lib/lib2to3/fixer_util.py
index 44502bf..babe6cb 100644
--- a/Lib/lib2to3/fixer_util.py
+++ b/Lib/lib2to3/fixer_util.py
@@ -1,8 +1,6 @@
"""Utility functions, node construction macros, etc."""
# Author: Collin Winter
-from itertools import islice
-
# Local imports
from .pgen2 import token
from .pytree import Leaf, Node
diff --git a/Lib/lib2to3/fixes/fix_dict.py b/Lib/lib2to3/fixes/fix_dict.py
index 963f952..d3655c9 100644
--- a/Lib/lib2to3/fixes/fix_dict.py
+++ b/Lib/lib2to3/fixes/fix_dict.py
@@ -30,9 +30,8 @@ as an argument to a function that introspects the argument).
# Local imports
from .. import pytree
from .. import patcomp
-from ..pgen2 import token
from .. import fixer_base
-from ..fixer_util import Name, Call, LParen, RParen, ArgList, Dot
+from ..fixer_util import Name, Call, Dot
from .. import fixer_util
diff --git a/Lib/lib2to3/fixes/fix_exec.py b/Lib/lib2to3/fixes/fix_exec.py
index 2c9b72d..ab921ee 100644
--- a/Lib/lib2to3/fixes/fix_exec.py
+++ b/Lib/lib2to3/fixes/fix_exec.py
@@ -10,7 +10,6 @@ exec code in ns1, ns2 -> exec(code, ns1, ns2)
"""
# Local imports
-from .. import pytree
from .. import fixer_base
from ..fixer_util import Comma, Name, Call
diff --git a/Lib/lib2to3/fixes/fix_filter.py b/Lib/lib2to3/fixes/fix_filter.py
index 391889f..bb6718c 100644
--- a/Lib/lib2to3/fixes/fix_filter.py
+++ b/Lib/lib2to3/fixes/fix_filter.py
@@ -14,7 +14,6 @@ Python 2.6 figure it out.
"""
# Local imports
-from ..pgen2 import token
from .. import fixer_base
from ..fixer_util import Name, Call, ListComp, in_special_context
diff --git a/Lib/lib2to3/fixes/fix_has_key.py b/Lib/lib2to3/fixes/fix_has_key.py
index 18c560f..439708c 100644
--- a/Lib/lib2to3/fixes/fix_has_key.py
+++ b/Lib/lib2to3/fixes/fix_has_key.py
@@ -31,7 +31,6 @@ CAVEATS:
# Local imports
from .. import pytree
-from ..pgen2 import token
from .. import fixer_base
from ..fixer_util import Name, parenthesize
diff --git a/Lib/lib2to3/fixes/fix_metaclass.py b/Lib/lib2to3/fixes/fix_metaclass.py
index 46c7aaf..8e34463 100644
--- a/Lib/lib2to3/fixes/fix_metaclass.py
+++ b/Lib/lib2to3/fixes/fix_metaclass.py
@@ -20,7 +20,7 @@
# Local imports
from .. import fixer_base
from ..pygram import token
-from ..fixer_util import Name, syms, Node, Leaf
+from ..fixer_util import syms, Node, Leaf
def has_metaclass(parent):
diff --git a/Lib/lib2to3/fixes/fix_nonzero.py b/Lib/lib2to3/fixes/fix_nonzero.py
index ad91a29..c229596 100644
--- a/Lib/lib2to3/fixes/fix_nonzero.py
+++ b/Lib/lib2to3/fixes/fix_nonzero.py
@@ -3,7 +3,7 @@
# Local imports
from .. import fixer_base
-from ..fixer_util import Name, syms
+from ..fixer_util import Name
class FixNonzero(fixer_base.BaseFix):
BM_compatible = True
diff --git a/Lib/lib2to3/fixes/fix_print.py b/Lib/lib2to3/fixes/fix_print.py
index a1fe2f5..8780322 100644
--- a/Lib/lib2to3/fixes/fix_print.py
+++ b/Lib/lib2to3/fixes/fix_print.py
@@ -18,7 +18,7 @@ from .. import patcomp
from .. import pytree
from ..pgen2 import token
from .. import fixer_base
-from ..fixer_util import Name, Call, Comma, String, is_tuple
+from ..fixer_util import Name, Call, Comma, String
parend_expr = patcomp.compile_pattern(
diff --git a/Lib/lib2to3/fixes/fix_types.py b/Lib/lib2to3/fixes/fix_types.py
index db34104..67bf51f 100644
--- a/Lib/lib2to3/fixes/fix_types.py
+++ b/Lib/lib2to3/fixes/fix_types.py
@@ -20,7 +20,6 @@ There should be another fixer that handles at least the following constants:
"""
# Local imports
-from ..pgen2 import token
from .. import fixer_base
from ..fixer_util import Name
@@ -42,7 +41,7 @@ _TYPE_MAPPING = {
'NotImplementedType' : 'type(NotImplemented)',
'SliceType' : 'slice',
'StringType': 'bytes', # XXX ?
- 'StringTypes' : 'str', # XXX ?
+ 'StringTypes' : '(str,)', # XXX ?
'TupleType': 'tuple',
'TypeType' : 'type',
'UnicodeType': 'str',
diff --git a/Lib/lib2to3/fixes/fix_urllib.py b/Lib/lib2to3/fixes/fix_urllib.py
index 1481cd9..5a36049 100644
--- a/Lib/lib2to3/fixes/fix_urllib.py
+++ b/Lib/lib2to3/fixes/fix_urllib.py
@@ -6,7 +6,6 @@
# Local imports
from lib2to3.fixes.fix_imports import alternates, FixImports
-from lib2to3 import fixer_base
from lib2to3.fixer_util import (Name, Comma, FromImport, Newline,
find_indentation, Node, syms)
diff --git a/Lib/lib2to3/refactor.py b/Lib/lib2to3/refactor.py
index 0728083..c5a1aa2 100644
--- a/Lib/lib2to3/refactor.py
+++ b/Lib/lib2to3/refactor.py
@@ -8,8 +8,6 @@ recursively descend down directories. Imported as a module, this
provides infrastructure to write your own refactoring tool.
"""
-from __future__ import with_statement
-
__author__ = "Guido van Rossum <guido@python.org>"
@@ -26,7 +24,6 @@ from itertools import chain
from .pgen2 import driver, tokenize, token
from .fixer_util import find_root
from . import pytree, pygram
-from . import btm_utils as bu
from . import btm_matcher as bm
diff --git a/Lib/lib2to3/tests/data/py3_test_grammar.py b/Lib/lib2to3/tests/data/py3_test_grammar.py
index c0bf7f2..cf31a54 100644
--- a/Lib/lib2to3/tests/data/py3_test_grammar.py
+++ b/Lib/lib2to3/tests/data/py3_test_grammar.py
@@ -319,7 +319,7 @@ class GrammarTests(unittest.TestCase):
def f(x) -> list: pass
self.assertEquals(f.__annotations__, {'return': list})
- # test MAKE_CLOSURE with a variety of oparg's
+ # test closures with a variety of oparg's
closure = 1
def f(): return closure
def f(x=1): return closure
diff --git a/Lib/lib2to3/tests/support.py b/Lib/lib2to3/tests/support.py
index 0897177..ae7cfe8 100644
--- a/Lib/lib2to3/tests/support.py
+++ b/Lib/lib2to3/tests/support.py
@@ -3,10 +3,8 @@
# Python imports
import unittest
-import sys
import os
import os.path
-import re
from textwrap import dedent
# Local imports
diff --git a/Lib/lib2to3/tests/test_all_fixers.py b/Lib/lib2to3/tests/test_all_fixers.py
index 15079fe..c0507cf 100644
--- a/Lib/lib2to3/tests/test_all_fixers.py
+++ b/Lib/lib2to3/tests/test_all_fixers.py
@@ -10,7 +10,6 @@ import unittest
import test.support
# Local imports
-from lib2to3 import refactor
from . import support
diff --git a/Lib/lib2to3/tests/test_fixers.py b/Lib/lib2to3/tests/test_fixers.py
index 640dcef..b3f2680 100644
--- a/Lib/lib2to3/tests/test_fixers.py
+++ b/Lib/lib2to3/tests/test_fixers.py
@@ -2,12 +2,11 @@
# Python imports
import os
-import unittest
from itertools import chain
from operator import itemgetter
# Local imports
-from lib2to3 import pygram, pytree, refactor, fixer_util
+from lib2to3 import pygram, fixer_util
from lib2to3.tests import support
@@ -3234,6 +3233,10 @@ class Test_types(FixerTestCase):
a = """type(None)"""
self.check(b, a)
+ b = "types.StringTypes"
+ a = "(str,)"
+ self.check(b, a)
+
class Test_idioms(FixerTestCase):
fixer = "idioms"
diff --git a/Lib/lib2to3/tests/test_parser.py b/Lib/lib2to3/tests/test_parser.py
index e4a0194..9a969e8 100644
--- a/Lib/lib2to3/tests/test_parser.py
+++ b/Lib/lib2to3/tests/test_parser.py
@@ -8,7 +8,7 @@ test_grammar.py files from both Python 2 and Python 3.
# Testing imports
from . import support
-from .support import driver, test_dir
+from .support import driver
from test.support import verbose
# Python imports
@@ -134,6 +134,24 @@ class TestAsyncAwait(GrammarTest):
""")
self.validate("""async def foo():
+ [i async for i in b]
+ """)
+
+ self.validate("""async def foo():
+ {i for i in b
+ async for i in a if await i
+ for b in i}
+ """)
+
+ self.validate("""async def foo():
+ [await i for i in b if await c]
+ """)
+
+ self.validate("""async def foo():
+ [ i for i in b if c]
+ """)
+
+ self.validate("""async def foo():
def foo(): pass
@@ -272,6 +290,36 @@ class TestFunctionAnnotations(GrammarTest):
self.validate(s)
+# Adapted from Python 3's Lib/test/test_grammar.py:GrammarTests.test_var_annot
+class TestVarAnnotations(GrammarTest):
+ def test_1(self):
+ self.validate("var1: int = 5")
+
+ def test_2(self):
+ self.validate("var2: [int, str]")
+
+ def test_3(self):
+ self.validate("def f():\n"
+ " st: str = 'Hello'\n"
+ " a.b: int = (1, 2)\n"
+ " return st\n")
+
+ def test_4(self):
+ self.validate("def fbad():\n"
+ " x: int\n"
+ " print(x)\n")
+
+ def test_5(self):
+ self.validate("class C:\n"
+ " x: int\n"
+ " s: str = 'attr'\n"
+ " z = 2\n"
+ " def __init__(self, x):\n"
+ " self.x: int = x\n")
+
+ def test_6(self):
+ self.validate("lst: List[int] = []")
+
class TestExcept(GrammarTest):
def test_new(self):
s = """
diff --git a/Lib/lib2to3/tests/test_pytree.py b/Lib/lib2to3/tests/test_pytree.py
index 4d585a8..177126d 100644
--- a/Lib/lib2to3/tests/test_pytree.py
+++ b/Lib/lib2to3/tests/test_pytree.py
@@ -9,11 +9,6 @@ more helpful than printing of (the first line of) the docstring,
especially when debugging a test.
"""
-from __future__ import with_statement
-
-import sys
-import warnings
-
# Testing imports
from . import support
diff --git a/Lib/lib2to3/tests/test_refactor.py b/Lib/lib2to3/tests/test_refactor.py
index 8563001..e9bae5e 100644
--- a/Lib/lib2to3/tests/test_refactor.py
+++ b/Lib/lib2to3/tests/test_refactor.py
@@ -2,24 +2,18 @@
Unit tests for refactor.py.
"""
-from __future__ import with_statement
-
import sys
import os
import codecs
-import operator
import io
import re
import tempfile
import shutil
import unittest
-import warnings
from lib2to3 import refactor, pygram, fixer_base
from lib2to3.pgen2 import token
-from . import support
-
TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
FIXER_DIR = os.path.join(TEST_DATA_DIR, "fixers")
diff --git a/Lib/lib2to3/tests/test_util.py b/Lib/lib2to3/tests/test_util.py
index d2be82c..c6c6139 100644
--- a/Lib/lib2to3/tests/test_util.py
+++ b/Lib/lib2to3/tests/test_util.py
@@ -3,9 +3,6 @@
# Testing imports
from . import support
-# Python imports
-import os.path
-
# Local imports
from lib2to3.pytree import Node, Leaf
from lib2to3 import fixer_util
diff --git a/Lib/locale.py b/Lib/locale.py
index 6d59cd8..4de0090 100644
--- a/Lib/locale.py
+++ b/Lib/locale.py
@@ -302,12 +302,16 @@ def str(val):
def delocalize(string):
"Parses a string as a normalized number according to the locale settings."
+
+ conv = localeconv()
+
#First, get rid of the grouping
- ts = localeconv()['thousands_sep']
+ ts = conv['thousands_sep']
if ts:
string = string.replace(ts, '')
+
#next, replace the decimal point with a dot
- dd = localeconv()['decimal_point']
+ dd = conv['decimal_point']
if dd:
string = string.replace(dd, '.')
return string
diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py
index 6455f39..b44a3b2 100644
--- a/Lib/logging/__init__.py
+++ b/Lib/logging/__init__.py
@@ -33,8 +33,9 @@ __all__ = ['BASIC_FORMAT', 'BufferingFormatter', 'CRITICAL', 'DEBUG', 'ERROR',
'StreamHandler', 'WARN', 'WARNING', 'addLevelName', 'basicConfig',
'captureWarnings', 'critical', 'debug', 'disable', 'error',
'exception', 'fatal', 'getLevelName', 'getLogger', 'getLoggerClass',
- 'info', 'log', 'makeLogRecord', 'setLoggerClass', 'warn', 'warning',
- 'getLogRecordFactory', 'setLogRecordFactory', 'lastResort']
+ 'info', 'log', 'makeLogRecord', 'setLoggerClass', 'shutdown',
+ 'warn', 'warning', 'getLogRecordFactory', 'setLogRecordFactory',
+ 'lastResort', 'raiseExceptions']
try:
import threading
@@ -107,6 +108,7 @@ _levelToName = {
}
_nameToLevel = {
'CRITICAL': CRITICAL,
+ 'FATAL': FATAL,
'ERROR': ERROR,
'WARN': WARNING,
'WARNING': WARNING,
@@ -940,6 +942,10 @@ class Handler(Filterer):
finally:
del t, v, tb
+ def __repr__(self):
+ level = getLevelName(self.level)
+ return '<%s (%s)>' % (self.__class__.__name__, level)
+
class StreamHandler(Handler):
"""
A handler class which writes logging records, appropriately formatted,
@@ -991,6 +997,14 @@ class StreamHandler(Handler):
except Exception:
self.handleError(record)
+ def __repr__(self):
+ level = getLevelName(self.level)
+ name = getattr(self.stream, 'name', '')
+ if name:
+ name += ' '
+ return '<%s %s(%s)>' % (self.__class__.__name__, name, level)
+
+
class FileHandler(StreamHandler):
"""
A handler class which writes formatted logging records to disk files.
@@ -999,6 +1013,8 @@ class FileHandler(StreamHandler):
"""
Open the specified file and use it as the stream for logging.
"""
+ # Issue #27493: add support for Path objects to be passed in
+ filename = os.fspath(filename)
#keep the absolute path, otherwise derived classes which use this
#may come a cropper when the current directory changes
self.baseFilename = os.path.abspath(filename)
@@ -1053,6 +1069,11 @@ class FileHandler(StreamHandler):
self.stream = self._open()
StreamHandler.emit(self, record)
+ def __repr__(self):
+ level = getLevelName(self.level)
+ return '<%s %s (%s)>' % (self.__class__.__name__, self.baseFilename, level)
+
+
class _StderrHandler(StreamHandler):
"""
This class is like a StreamHandler using sys.stderr, but always uses
@@ -1545,6 +1566,11 @@ class Logger(Filterer):
suffix = '.'.join((self.name, suffix))
return self.manager.getLogger(suffix)
+ def __repr__(self):
+ level = getLevelName(self.getEffectiveLevel())
+ return '<%s %s (%s)>' % (self.__class__.__name__, self.name, level)
+
+
class RootLogger(Logger):
"""
A root logger is not that different to any other logger, except that
@@ -1671,6 +1697,11 @@ class LoggerAdapter(object):
"""
return self.logger.hasHandlers()
+ def __repr__(self):
+ logger = self.logger
+ level = getLevelName(logger.getEffectiveLevel())
+ return '<%s %s (%s)>' % (self.__class__.__name__, logger.name, level)
+
root = RootLogger(WARNING)
Logger.root = root
Logger.manager = Manager(Logger.root)
diff --git a/Lib/logging/handlers.py b/Lib/logging/handlers.py
index c39a56f..7d77973 100644
--- a/Lib/logging/handlers.py
+++ b/Lib/logging/handlers.py
@@ -246,6 +246,9 @@ class TimedRotatingFileHandler(BaseRotatingHandler):
self.extMatch = re.compile(self.extMatch, re.ASCII)
self.interval = self.interval * interval # multiply by units requested
+ # The following line added because the filename passed in could be a
+ # path object (see Issue #27493), but self.baseFilename will be a string
+ filename = self.baseFilename
if os.path.exists(filename):
t = os.stat(filename)[ST_MTIME]
else:
@@ -440,11 +443,11 @@ class WatchedFileHandler(logging.FileHandler):
sres = os.fstat(self.stream.fileno())
self.dev, self.ino = sres[ST_DEV], sres[ST_INO]
- def emit(self, record):
+ def reopenIfNeeded(self):
"""
- Emit a record.
+ Reopen log file if needed.
- First check if the underlying file has changed, and if it
+ Checks if the underlying file has changed, and if it
has, close the old stream and reopen the file to get the
current stream.
"""
@@ -467,6 +470,15 @@ class WatchedFileHandler(logging.FileHandler):
# open a new file handle and get new stat info from that fd
self.stream = self._open()
self._statstream()
+
+ def emit(self, record):
+ """
+ Emit a record.
+
+ If underlying file has changed, reopen the file before emitting the
+ record to it.
+ """
+ self.reopenIfNeeded()
logging.FileHandler.emit(self, record)
@@ -1229,17 +1241,25 @@ class MemoryHandler(BufferingHandler):
flushing them to a target handler. Flushing occurs whenever the buffer
is full, or when an event of a certain severity or greater is seen.
"""
- def __init__(self, capacity, flushLevel=logging.ERROR, target=None):
+ def __init__(self, capacity, flushLevel=logging.ERROR, target=None,
+ flushOnClose=True):
"""
Initialize the handler with the buffer size, the level at which
flushing should occur and an optional target.
Note that without a target being set either here or via setTarget(),
a MemoryHandler is no use to anyone!
+
+ The ``flushOnClose`` argument is ``True`` for backward compatibility
+ reasons - the old behaviour is that when the handler is closed, the
+ buffer is flushed, even if the flush level hasn't been exceeded nor the
+ capacity exceeded. To prevent this, set ``flushOnClose`` to ``False``.
"""
BufferingHandler.__init__(self, capacity)
self.flushLevel = flushLevel
self.target = target
+ # See Issue #26559 for why this has been added
+ self.flushOnClose = flushOnClose
def shouldFlush(self, record):
"""
@@ -1273,10 +1293,12 @@ class MemoryHandler(BufferingHandler):
def close(self):
"""
- Flush, set the target to None and lose the buffer.
+ Flush, if appropriately configured, set the target to None and lose the
+ buffer.
"""
try:
- self.flush()
+ if self.flushOnClose:
+ self.flush()
finally:
self.acquire()
try:
diff --git a/Lib/lzma.py b/Lib/lzma.py
index 7dff1c3..0817b87 100644
--- a/Lib/lzma.py
+++ b/Lib/lzma.py
@@ -23,6 +23,7 @@ __all__ = [
import builtins
import io
+import os
from _lzma import *
from _lzma import _encode_filter_properties, _decode_filter_properties
import _compression
@@ -49,9 +50,10 @@ class LZMAFile(_compression.BaseStream):
format=None, check=-1, preset=None, filters=None):
"""Open an LZMA-compressed file in binary mode.
- filename can be either an actual file name (given as a str or
- bytes object), in which case the named file is opened, or it can
- be an existing file object to read from or write to.
+ filename can be either an actual file name (given as a str,
+ bytes, or PathLike object), in which case the named file is
+ opened, or it can be an existing file object to read from or
+ write to.
mode can be "r" for reading (default), "w" for (over)writing,
"x" for creating exclusively, or "a" for appending. These can
@@ -112,7 +114,7 @@ class LZMAFile(_compression.BaseStream):
else:
raise ValueError("Invalid mode: {!r}".format(mode))
- if isinstance(filename, (str, bytes)):
+ if isinstance(filename, (str, bytes, os.PathLike)):
if "b" not in mode:
mode += "b"
self._fp = builtins.open(filename, mode)
@@ -122,7 +124,7 @@ class LZMAFile(_compression.BaseStream):
self._fp = filename
self._mode = mode_code
else:
- raise TypeError("filename must be a str or bytes object, or a file")
+ raise TypeError("filename must be a str, bytes, file or PathLike object")
if self._mode == _MODE_READ:
raw = _compression.DecompressReader(self._fp, LZMADecompressor,
@@ -263,9 +265,9 @@ def open(filename, mode="rb", *,
encoding=None, errors=None, newline=None):
"""Open an LZMA-compressed file in binary or text mode.
- filename can be either an actual file name (given as a str or bytes
- object), in which case the named file is opened, or it can be an
- existing file object to read from or write to.
+ filename can be either an actual file name (given as a str, bytes,
+ or PathLike object), in which case the named file is opened, or it
+ can be an existing file object to read from or write to.
The mode argument can be "r", "rb" (default), "w", "wb", "x", "xb",
"a", or "ab" for binary mode, or "rt", "wt", "xt", or "at" for text
diff --git a/Lib/mailbox.py b/Lib/mailbox.py
index 0270e25..39f24f9 100644
--- a/Lib/mailbox.py
+++ b/Lib/mailbox.py
@@ -23,9 +23,10 @@ try:
except ImportError:
fcntl = None
-__all__ = [ 'Mailbox', 'Maildir', 'mbox', 'MH', 'Babyl', 'MMDF',
- 'Message', 'MaildirMessage', 'mboxMessage', 'MHMessage',
- 'BabylMessage', 'MMDFMessage']
+__all__ = ['Mailbox', 'Maildir', 'mbox', 'MH', 'Babyl', 'MMDF',
+ 'Message', 'MaildirMessage', 'mboxMessage', 'MHMessage',
+ 'BabylMessage', 'MMDFMessage', 'Error', 'NoSuchMailboxError',
+ 'NotEmptyError', 'ExternalClashError', 'FormatError']
linesep = os.linesep.encode('ascii')
@@ -312,11 +313,12 @@ class Maildir(Mailbox):
# final position in order to prevent race conditions with changes
# from other programs
try:
- if hasattr(os, 'link'):
+ try:
os.link(tmp_file.name, dest)
- os.remove(tmp_file.name)
- else:
+ except (AttributeError, PermissionError):
os.rename(tmp_file.name, dest)
+ else:
+ os.remove(tmp_file.name)
except OSError as e:
os.remove(tmp_file.name)
if e.errno == errno.EEXIST:
@@ -1199,13 +1201,14 @@ class MH(Mailbox):
for key in self.iterkeys():
if key - 1 != prev:
changes.append((key, prev + 1))
- if hasattr(os, 'link'):
+ try:
os.link(os.path.join(self._path, str(key)),
os.path.join(self._path, str(prev + 1)))
- os.unlink(os.path.join(self._path, str(key)))
- else:
+ except (AttributeError, PermissionError):
os.rename(os.path.join(self._path, str(key)),
os.path.join(self._path, str(prev + 1)))
+ else:
+ os.unlink(os.path.join(self._path, str(key)))
prev += 1
self._next_key = prev + 1
if len(changes) == 0:
@@ -2075,13 +2078,14 @@ def _lock_file(f, dotlock=True):
else:
raise
try:
- if hasattr(os, 'link'):
+ try:
os.link(pre_lock.name, f.name + '.lock')
dotlock_done = True
- os.unlink(pre_lock.name)
- else:
+ except (AttributeError, PermissionError):
os.rename(pre_lock.name, f.name + '.lock')
dotlock_done = True
+ else:
+ os.unlink(pre_lock.name)
except FileExistsError:
os.remove(pre_lock.name)
raise ExternalClashError('dot lock unavailable: %s' %
diff --git a/Lib/mimetypes.py b/Lib/mimetypes.py
index 0be76ad..9a88680 100644
--- a/Lib/mimetypes.py
+++ b/Lib/mimetypes.py
@@ -33,8 +33,10 @@ except ImportError:
_winreg = None
__all__ = [
- "guess_type","guess_extension","guess_all_extensions",
- "add_type","read_mime_types","init"
+ "knownfiles", "inited", "MimeTypes",
+ "guess_type", "guess_all_extensions", "guess_extension",
+ "add_type", "init", "read_mime_types",
+ "suffix_map", "encodings_map", "types_map", "common_types"
]
knownfiles = [
diff --git a/Lib/modulefinder.py b/Lib/modulefinder.py
index 8103502..e220315 100644
--- a/Lib/modulefinder.py
+++ b/Lib/modulefinder.py
@@ -10,7 +10,7 @@ import types
import struct
import warnings
with warnings.catch_warnings():
- warnings.simplefilter('ignore', PendingDeprecationWarning)
+ warnings.simplefilter('ignore', DeprecationWarning)
import imp
LOAD_CONST = dis.opmap['LOAD_CONST']
@@ -336,8 +336,7 @@ class ModuleFinder:
fullname = name + "." + sub
self._add_badmodule(fullname, caller)
- def scan_opcodes_25(self, co,
- unpack = struct.unpack):
+ def scan_opcodes(self, co):
# Scan the code, and yield 'interesting' opcode combinations
code = co.co_code
names = co.co_names
@@ -360,7 +359,7 @@ class ModuleFinder:
def scan_code(self, co, m):
code = co.co_code
- scanner = self.scan_opcodes_25
+ scanner = self.scan_opcodes
for what, args in scanner(co):
if what == "store":
name, = args
diff --git a/Lib/msilib/__init__.py b/Lib/msilib/__init__.py
index 6d0a28f..f0370c2 100644
--- a/Lib/msilib/__init__.py
+++ b/Lib/msilib/__init__.py
@@ -1,7 +1,7 @@
# Copyright (C) 2005 Martin v. Löwis
# Licensed to PSF under a Contributor Agreement.
from _msi import *
-import glob
+import fnmatch
import os
import re
import string
@@ -289,7 +289,7 @@ class Directory:
def make_short(self, file):
oldfile = file
file = file.replace('+', '_')
- file = ''.join(c for c in file if not c in ' "/\[]:;=,')
+ file = ''.join(c for c in file if not c in r' "/\[]:;=,')
parts = file.split(".")
if len(parts) > 1:
prefix = "".join(parts[:-1]).upper()
@@ -379,7 +379,13 @@ class Directory:
def glob(self, pattern, exclude = None):
"""Add a list of files to the current component as specified in the
glob pattern. Individual files can be excluded in the exclude list."""
- files = glob.glob1(self.absolute, pattern)
+ try:
+ files = os.listdir(self.absolute)
+ except OSError:
+ return []
+ if pattern[:1] != '.':
+ files = (f for f in files if f[0] != '.')
+ files = fnmatch.filter(files, pattern)
for f in files:
if exclude and f in exclude: continue
self.add_file(f)
diff --git a/Lib/multiprocessing/connection.py b/Lib/multiprocessing/connection.py
index d0a1b86..d49e8f0 100644
--- a/Lib/multiprocessing/connection.py
+++ b/Lib/multiprocessing/connection.py
@@ -20,11 +20,11 @@ import itertools
import _multiprocessing
-from . import reduction
from . import util
from . import AuthenticationError, BufferTooShort
-from .reduction import ForkingPickler
+from .context import reduction
+_ForkingPickler = reduction.ForkingPickler
try:
import _winapi
@@ -203,7 +203,7 @@ class _ConnectionBase:
"""Send a (picklable) object"""
self._check_closed()
self._check_writable()
- self._send_bytes(ForkingPickler.dumps(obj))
+ self._send_bytes(_ForkingPickler.dumps(obj))
def recv_bytes(self, maxlength=None):
"""
@@ -248,7 +248,7 @@ class _ConnectionBase:
self._check_closed()
self._check_readable()
buf = self._recv_bytes()
- return ForkingPickler.loads(buf.getbuffer())
+ return _ForkingPickler.loads(buf.getbuffer())
def poll(self, timeout=0.0):
"""Whether there is any input available to be read"""
diff --git a/Lib/multiprocessing/context.py b/Lib/multiprocessing/context.py
index 115d4bf..623f6fb 100644
--- a/Lib/multiprocessing/context.py
+++ b/Lib/multiprocessing/context.py
@@ -3,6 +3,7 @@ import sys
import threading
from . import process
+from . import reduction
__all__ = [] # things are copied from here to __init__.py
@@ -198,6 +199,16 @@ class BaseContext(object):
def set_start_method(self, method, force=False):
raise ValueError('cannot set start method of concrete context')
+ @property
+ def reducer(self):
+ '''Controls how objects will be reduced to a form that can be
+ shared with other processes.'''
+ return globals().get('reduction')
+
+ @reducer.setter
+ def reducer(self, reduction):
+ globals()['reduction'] = reduction
+
def _check_available(self):
pass
@@ -245,7 +256,6 @@ class DefaultContext(BaseContext):
if sys.platform == 'win32':
return ['spawn']
else:
- from . import reduction
if reduction.HAVE_SEND_HANDLE:
return ['fork', 'spawn', 'forkserver']
else:
@@ -292,7 +302,6 @@ if sys.platform != 'win32':
_name = 'forkserver'
Process = ForkServerProcess
def _check_available(self):
- from . import reduction
if not reduction.HAVE_SEND_HANDLE:
raise ValueError('forkserver start method not available')
diff --git a/Lib/multiprocessing/forkserver.py b/Lib/multiprocessing/forkserver.py
index ad01ede..f2c179e 100644
--- a/Lib/multiprocessing/forkserver.py
+++ b/Lib/multiprocessing/forkserver.py
@@ -9,7 +9,7 @@ import threading
from . import connection
from . import process
-from . import reduction
+from .context import reduction
from . import semaphore_tracker
from . import spawn
from . import util
diff --git a/Lib/multiprocessing/heap.py b/Lib/multiprocessing/heap.py
index 44d9638..4433215 100644
--- a/Lib/multiprocessing/heap.py
+++ b/Lib/multiprocessing/heap.py
@@ -14,8 +14,7 @@ import sys
import tempfile
import threading
-from . import context
-from . import reduction
+from .context import reduction, assert_spawning
from . import util
__all__ = ['BufferWrapper']
@@ -48,7 +47,7 @@ if sys.platform == 'win32':
self._state = (self.size, self.name)
def __getstate__(self):
- context.assert_spawning(self)
+ assert_spawning(self)
return self._state
def __setstate__(self, state):
diff --git a/Lib/multiprocessing/managers.py b/Lib/multiprocessing/managers.py
index b175470..b9ce84b 100644
--- a/Lib/multiprocessing/managers.py
+++ b/Lib/multiprocessing/managers.py
@@ -23,10 +23,9 @@ from time import time as _time
from traceback import format_exc
from . import connection
-from . import context
+from .context import reduction, get_spawning_popen
from . import pool
from . import process
-from . import reduction
from . import util
from . import get_context
@@ -142,7 +141,8 @@ class Server(object):
self.id_to_obj = {'0': (None, ())}
self.id_to_refcount = {}
- self.mutex = threading.RLock()
+ self.id_to_local_proxy_obj = {}
+ self.mutex = threading.Lock()
def serve_forever(self):
'''
@@ -227,7 +227,14 @@ class Server(object):
methodname = obj = None
request = recv()
ident, methodname, args, kwds = request
- obj, exposed, gettypeid = id_to_obj[ident]
+ try:
+ obj, exposed, gettypeid = id_to_obj[ident]
+ except KeyError as ke:
+ try:
+ obj, exposed, gettypeid = \
+ self.id_to_local_proxy_obj[ident]
+ except KeyError as second_ke:
+ raise ke
if methodname not in exposed:
raise AttributeError(
@@ -308,7 +315,7 @@ class Server(object):
'''
with self.mutex:
result = []
- keys = list(self.id_to_obj.keys())
+ keys = list(self.id_to_refcount.keys())
keys.sort()
for ident in keys:
if ident != '0':
@@ -321,7 +328,8 @@ class Server(object):
'''
Number of shared objects
'''
- return len(self.id_to_obj) - 1 # don't count ident='0'
+ # Doesn't use (len(self.id_to_obj) - 1) as we shouldn't count ident='0'
+ return len(self.id_to_refcount)
def shutdown(self, c):
'''
@@ -363,13 +371,9 @@ class Server(object):
self.id_to_obj[ident] = (obj, set(exposed), method_to_typeid)
if ident not in self.id_to_refcount:
self.id_to_refcount[ident] = 0
- # increment the reference count immediately, to avoid
- # this object being garbage collected before a Proxy
- # object for it can be created. The caller of create()
- # is responsible for doing a decref once the Proxy object
- # has been created.
- self.incref(c, ident)
- return ident, tuple(exposed)
+
+ self.incref(c, ident)
+ return ident, tuple(exposed)
def get_methods(self, c, token):
'''
@@ -387,15 +391,45 @@ class Server(object):
def incref(self, c, ident):
with self.mutex:
- self.id_to_refcount[ident] += 1
+ try:
+ self.id_to_refcount[ident] += 1
+ except KeyError as ke:
+ # If no external references exist but an internal (to the
+ # manager) still does and a new external reference is created
+ # from it, restore the manager's tracking of it from the
+ # previously stashed internal ref.
+ if ident in self.id_to_local_proxy_obj:
+ self.id_to_refcount[ident] = 1
+ self.id_to_obj[ident] = \
+ self.id_to_local_proxy_obj[ident]
+ obj, exposed, gettypeid = self.id_to_obj[ident]
+ util.debug('Server re-enabled tracking & INCREF %r', ident)
+ else:
+ raise ke
def decref(self, c, ident):
+ if ident not in self.id_to_refcount and \
+ ident in self.id_to_local_proxy_obj:
+ util.debug('Server DECREF skipping %r', ident)
+ return
+
with self.mutex:
assert self.id_to_refcount[ident] >= 1
self.id_to_refcount[ident] -= 1
if self.id_to_refcount[ident] == 0:
- del self.id_to_obj[ident], self.id_to_refcount[ident]
- util.debug('disposing of obj with id %r', ident)
+ del self.id_to_refcount[ident]
+
+ if ident not in self.id_to_refcount:
+ # Two-step process in case the object turns out to contain other
+ # proxy objects (e.g. a managed list of managed lists).
+ # Otherwise, deleting self.id_to_obj[ident] would trigger the
+ # deleting of the stored value (another managed object) which would
+ # in turn attempt to acquire the mutex that is already held here.
+ self.id_to_obj[ident] = (None, (), None) # thread-safe
+ util.debug('disposing of obj with id %r', ident)
+ with self.mutex:
+ del self.id_to_obj[ident]
+
#
# Class to represent state of a manager
@@ -658,7 +692,7 @@ class BaseProxy(object):
_mutex = util.ForkAwareThreadLock()
def __init__(self, token, serializer, manager=None,
- authkey=None, exposed=None, incref=True):
+ authkey=None, exposed=None, incref=True, manager_owned=False):
with BaseProxy._mutex:
tls_idset = BaseProxy._address_to_local.get(token.address, None)
if tls_idset is None:
@@ -680,6 +714,12 @@ class BaseProxy(object):
self._serializer = serializer
self._Client = listener_client[serializer][1]
+ # Should be set to True only when a proxy object is being created
+ # on the manager server; primary use case: nested proxy objects.
+ # RebuildProxy detects when a proxy is being created on the manager
+ # and sets this value appropriately.
+ self._owned_by_manager = manager_owned
+
if authkey is not None:
self._authkey = process.AuthenticationString(authkey)
elif self._manager is not None:
@@ -738,6 +778,10 @@ class BaseProxy(object):
return self._callmethod('#GETVALUE')
def _incref(self):
+ if self._owned_by_manager:
+ util.debug('owned_by_manager skipped INCREF of %r', self._token.id)
+ return
+
conn = self._Client(self._token.address, authkey=self._authkey)
dispatch(conn, None, 'incref', (self._id,))
util.debug('INCREF %r', self._token.id)
@@ -788,7 +832,7 @@ class BaseProxy(object):
def __reduce__(self):
kwds = {}
- if context.get_spawning_popen() is not None:
+ if get_spawning_popen() is not None:
kwds['authkey'] = self._authkey
if getattr(self, '_isauto', False):
@@ -822,19 +866,19 @@ class BaseProxy(object):
def RebuildProxy(func, token, serializer, kwds):
'''
Function used for unpickling proxy objects.
-
- If possible the shared object is returned, or otherwise a proxy for it.
'''
server = getattr(process.current_process(), '_manager_server', None)
-
if server and server.address == token.address:
- return server.id_to_obj[token.id][0]
- else:
- incref = (
- kwds.pop('incref', True) and
- not getattr(process.current_process(), '_inheriting', False)
- )
- return func(token, serializer, incref=incref, **kwds)
+ util.debug('Rebuild a proxy owned by manager, token=%r', token)
+ kwds['manager_owned'] = True
+ if token.id not in server.id_to_local_proxy_obj:
+ server.id_to_local_proxy_obj[token.id] = \
+ server.id_to_obj[token.id]
+ incref = (
+ kwds.pop('incref', True) and
+ not getattr(process.current_process(), '_inheriting', False)
+ )
+ return func(token, serializer, incref=incref, **kwds)
#
# Functions to create proxies and proxy types
diff --git a/Lib/multiprocessing/pool.py b/Lib/multiprocessing/pool.py
index 6d25469..ffdf426 100644
--- a/Lib/multiprocessing/pool.py
+++ b/Lib/multiprocessing/pool.py
@@ -638,22 +638,26 @@ class MapResult(ApplyResult):
self._number_left = length//chunksize + bool(length % chunksize)
def _set(self, i, success_result):
+ self._number_left -= 1
success, result = success_result
- if success:
+ if success and self._success:
self._value[i*self._chunksize:(i+1)*self._chunksize] = result
- self._number_left -= 1
if self._number_left == 0:
if self._callback:
self._callback(self._value)
del self._cache[self._job]
self._event.set()
else:
- self._success = False
- self._value = result
- if self._error_callback:
- self._error_callback(self._value)
- del self._cache[self._job]
- self._event.set()
+ if not success and self._success:
+ # only store first exception
+ self._success = False
+ self._value = result
+ if self._number_left == 0:
+ # only consider the result ready once all jobs are done
+ if self._error_callback:
+ self._error_callback(self._value)
+ del self._cache[self._job]
+ self._event.set()
#
# Class whose instances are returned by `Pool.imap()`
diff --git a/Lib/multiprocessing/popen_forkserver.py b/Lib/multiprocessing/popen_forkserver.py
index e792194..222db2d 100644
--- a/Lib/multiprocessing/popen_forkserver.py
+++ b/Lib/multiprocessing/popen_forkserver.py
@@ -1,10 +1,9 @@
import io
import os
-from . import reduction
+from .context import reduction, set_spawning_popen
if not reduction.HAVE_SEND_HANDLE:
raise ImportError('No support for sending fds between processes')
-from . import context
from . import forkserver
from . import popen_fork
from . import spawn
@@ -42,12 +41,12 @@ class Popen(popen_fork.Popen):
def _launch(self, process_obj):
prep_data = spawn.get_preparation_data(process_obj._name)
buf = io.BytesIO()
- context.set_spawning_popen(self)
+ set_spawning_popen(self)
try:
reduction.dump(prep_data, buf)
reduction.dump(process_obj, buf)
finally:
- context.set_spawning_popen(None)
+ set_spawning_popen(None)
self.sentinel, w = forkserver.connect_to_new_process(self._fds)
util.Finalize(self, os.close, (self.sentinel,))
diff --git a/Lib/multiprocessing/popen_spawn_posix.py b/Lib/multiprocessing/popen_spawn_posix.py
index 6b0a8d6..98f8f0a 100644
--- a/Lib/multiprocessing/popen_spawn_posix.py
+++ b/Lib/multiprocessing/popen_spawn_posix.py
@@ -1,9 +1,8 @@
import io
import os
-from . import context
+from .context import reduction, set_spawning_popen
from . import popen_fork
-from . import reduction
from . import spawn
from . import util
@@ -42,12 +41,12 @@ class Popen(popen_fork.Popen):
self._fds.append(tracker_fd)
prep_data = spawn.get_preparation_data(process_obj._name)
fp = io.BytesIO()
- context.set_spawning_popen(self)
+ set_spawning_popen(self)
try:
reduction.dump(prep_data, fp)
reduction.dump(process_obj, fp)
finally:
- context.set_spawning_popen(None)
+ set_spawning_popen(None)
parent_r = child_w = child_r = parent_w = None
try:
diff --git a/Lib/multiprocessing/popen_spawn_win32.py b/Lib/multiprocessing/popen_spawn_win32.py
index 3b53068..6fd588f 100644
--- a/Lib/multiprocessing/popen_spawn_win32.py
+++ b/Lib/multiprocessing/popen_spawn_win32.py
@@ -4,9 +4,8 @@ import signal
import sys
import _winapi
-from . import context
+from .context import reduction, get_spawning_popen, set_spawning_popen
from . import spawn
-from . import reduction
from . import util
__all__ = ['Popen']
@@ -60,15 +59,15 @@ class Popen(object):
util.Finalize(self, _winapi.CloseHandle, (self.sentinel,))
# send information to child
- context.set_spawning_popen(self)
+ set_spawning_popen(self)
try:
reduction.dump(prep_data, to_child)
reduction.dump(process_obj, to_child)
finally:
- context.set_spawning_popen(None)
+ set_spawning_popen(None)
def duplicate_for_child(self, handle):
- assert self is context.get_spawning_popen()
+ assert self is get_spawning_popen()
return reduction.duplicate(handle, self.sentinel)
def wait(self, timeout=None):
diff --git a/Lib/multiprocessing/queues.py b/Lib/multiprocessing/queues.py
index 786a303..dda03dd 100644
--- a/Lib/multiprocessing/queues.py
+++ b/Lib/multiprocessing/queues.py
@@ -23,9 +23,9 @@ import _multiprocessing
from . import connection
from . import context
+_ForkingPickler = context.reduction.ForkingPickler
from .util import debug, info, Finalize, register_after_fork, is_exiting
-from .reduction import ForkingPickler
#
# Queue type using a pipe, buffer and thread
@@ -110,7 +110,7 @@ class Queue(object):
finally:
self._rlock.release()
# unserialize the data after having released the lock
- return ForkingPickler.loads(res)
+ return _ForkingPickler.loads(res)
def qsize(self):
# Raises NotImplementedError on Mac OSX because of broken sem_getvalue()
@@ -238,7 +238,7 @@ class Queue(object):
return
# serialize the data before acquiring the lock
- obj = ForkingPickler.dumps(obj)
+ obj = _ForkingPickler.dumps(obj)
if wacquire is None:
send_bytes(obj)
else:
@@ -342,11 +342,11 @@ class SimpleQueue(object):
with self._rlock:
res = self._reader.recv_bytes()
# unserialize the data after having released the lock
- return ForkingPickler.loads(res)
+ return _ForkingPickler.loads(res)
def put(self, obj):
# serialize the data before acquiring the lock
- obj = ForkingPickler.dumps(obj)
+ obj = _ForkingPickler.dumps(obj)
if self._wlock is None:
# writes to a message oriented win32 pipe are atomic
self._writer.send_bytes(obj)
diff --git a/Lib/multiprocessing/reduction.py b/Lib/multiprocessing/reduction.py
index 8f209b4..c043c9a 100644
--- a/Lib/multiprocessing/reduction.py
+++ b/Lib/multiprocessing/reduction.py
@@ -7,6 +7,7 @@
# Licensed to PSF under a Contributor Agreement.
#
+from abc import ABCMeta, abstractmethod
import copyreg
import functools
import io
@@ -238,3 +239,36 @@ else:
fd = df.detach()
return socket.socket(family, type, proto, fileno=fd)
register(socket.socket, _reduce_socket)
+
+
+class AbstractReducer(metaclass=ABCMeta):
+ '''Abstract base class for use in implementing a Reduction class
+ suitable for use in replacing the standard reduction mechanism
+ used in multiprocessing.'''
+ ForkingPickler = ForkingPickler
+ register = register
+ dump = dump
+ send_handle = send_handle
+ recv_handle = recv_handle
+
+ if sys.platform == 'win32':
+ steal_handle = steal_handle
+ duplicate = duplicate
+ DupHandle = DupHandle
+ else:
+ sendfds = sendfds
+ recvfds = recvfds
+ DupFd = DupFd
+
+ _reduce_method = _reduce_method
+ _reduce_method_descriptor = _reduce_method_descriptor
+ _rebuild_partial = _rebuild_partial
+ _reduce_socket = _reduce_socket
+ _rebuild_socket = _rebuild_socket
+
+ def __init__(self, *args):
+ register(type(_C().f), _reduce_method)
+ register(type(list.append), _reduce_method_descriptor)
+ register(type(int.__add__), _reduce_method_descriptor)
+ register(functools.partial, _reduce_partial)
+ register(socket.socket, _reduce_socket)
diff --git a/Lib/multiprocessing/resource_sharer.py b/Lib/multiprocessing/resource_sharer.py
index 5e46fc6..e44a728 100644
--- a/Lib/multiprocessing/resource_sharer.py
+++ b/Lib/multiprocessing/resource_sharer.py
@@ -15,7 +15,7 @@ import sys
import threading
from . import process
-from . import reduction
+from .context import reduction
from . import util
__all__ = ['stop']
diff --git a/Lib/multiprocessing/sharedctypes.py b/Lib/multiprocessing/sharedctypes.py
index 4258f59..25cbcf2 100644
--- a/Lib/multiprocessing/sharedctypes.py
+++ b/Lib/multiprocessing/sharedctypes.py
@@ -13,8 +13,8 @@ import weakref
from . import heap
from . import get_context
-from .context import assert_spawning
-from .reduction import ForkingPickler
+from .context import reduction, assert_spawning
+_ForkingPickler = reduction.ForkingPickler
__all__ = ['RawValue', 'RawArray', 'Value', 'Array', 'copy', 'synchronized']
@@ -134,7 +134,7 @@ def reduce_ctype(obj):
def rebuild_ctype(type_, wrapper, length):
if length is not None:
type_ = type_ * length
- ForkingPickler.register(type_, reduce_ctype)
+ _ForkingPickler.register(type_, reduce_ctype)
buf = wrapper.create_memoryview()
obj = type_.from_buffer(buf)
obj._wrapper = wrapper
diff --git a/Lib/multiprocessing/spawn.py b/Lib/multiprocessing/spawn.py
index 392c1599..4aba372 100644
--- a/Lib/multiprocessing/spawn.py
+++ b/Lib/multiprocessing/spawn.py
@@ -9,13 +9,13 @@
#
import os
-import pickle
import sys
import runpy
import types
from . import get_start_method, set_start_method
from . import process
+from .context import reduction
from . import util
__all__ = ['_main', 'freeze_support', 'set_executable', 'get_executable',
@@ -96,8 +96,7 @@ def spawn_main(pipe_handle, parent_pid=None, tracker_fd=None):
assert is_forking(sys.argv)
if sys.platform == 'win32':
import msvcrt
- from .reduction import steal_handle
- new_handle = steal_handle(parent_pid, pipe_handle)
+ new_handle = reduction.steal_handle(parent_pid, pipe_handle)
fd = msvcrt.open_osfhandle(new_handle, os.O_RDONLY)
else:
from . import semaphore_tracker
@@ -111,9 +110,9 @@ def _main(fd):
with os.fdopen(fd, 'rb', closefd=True) as from_parent:
process.current_process()._inheriting = True
try:
- preparation_data = pickle.load(from_parent)
+ preparation_data = reduction.pickle.load(from_parent)
prepare(preparation_data)
- self = pickle.load(from_parent)
+ self = reduction.pickle.load(from_parent)
finally:
del process.current_process()._inheriting
return self._bootstrap()
diff --git a/Lib/ntpath.py b/Lib/ntpath.py
index af6a709..a8f4b37 100644
--- a/Lib/ntpath.py
+++ b/Lib/ntpath.py
@@ -28,8 +28,6 @@ sep = '\\'
pathsep = ';'
altsep = '/'
defpath = '.;C:\\bin'
-if 'ce' in sys.builtin_module_names:
- defpath = '\\Windows'
devnull = 'nul'
def _get_bothseps(path):
@@ -46,6 +44,7 @@ def normcase(s):
"""Normalize case of pathname.
Makes all characters lowercase and all slashes into backslashes."""
+ s = os.fspath(s)
try:
if isinstance(s, bytes):
return s.replace(b'/', b'\\').lower()
@@ -66,12 +65,14 @@ def normcase(s):
def isabs(s):
"""Test whether a path is absolute"""
+ s = os.fspath(s)
s = splitdrive(s)[1]
return len(s) > 0 and s[0] in _get_bothseps(s)
# Join two (or more) paths.
def join(path, *paths):
+ path = os.fspath(path)
if isinstance(path, bytes):
sep = b'\\'
seps = b'\\/'
@@ -84,7 +85,7 @@ def join(path, *paths):
if not paths:
path[:0] + sep #23780: Ensure compatible data type even if p is null.
result_drive, result_path = splitdrive(path)
- for p in paths:
+ for p in map(os.fspath, paths):
p_drive, p_path = splitdrive(p)
if p_path and p_path[0] in seps:
# Second path is absolute
@@ -136,6 +137,7 @@ def splitdrive(p):
Paths cannot contain both a drive letter and a UNC path.
"""
+ p = os.fspath(p)
if len(p) >= 2:
if isinstance(p, bytes):
sep = b'\\'
@@ -199,7 +201,7 @@ def split(p):
Return tuple (head, tail) where tail is everything after the final slash.
Either part may be empty."""
-
+ p = os.fspath(p)
seps = _get_bothseps(p)
d, p = splitdrive(p)
# set i to index beyond p's last slash
@@ -218,6 +220,7 @@ def split(p):
# It is always true that root + ext == p.
def splitext(p):
+ p = os.fspath(p)
if isinstance(p, bytes):
return genericpath._splitext(p, b'\\', b'/', b'.')
else:
@@ -278,6 +281,7 @@ except ImportError:
def ismount(path):
"""Test whether a path is a mount point (a drive root, the root of a
share, or a mounted volume)"""
+ path = os.fspath(path)
seps = _get_bothseps(path)
path = abspath(path)
root, rest = splitdrive(path)
@@ -305,6 +309,7 @@ def expanduser(path):
"""Expand ~ and ~user constructs.
If user or $HOME is unknown, do nothing."""
+ path = os.fspath(path)
if isinstance(path, bytes):
tilde = b'~'
else:
@@ -354,6 +359,7 @@ def expandvars(path):
"""Expand shell variables of the forms $var, ${var} and %var%.
Unknown variables are left unchanged."""
+ path = os.fspath(path)
if isinstance(path, bytes):
if b'$' not in path and b'%' not in path:
return path
@@ -464,6 +470,7 @@ def expandvars(path):
def normpath(path):
"""Normalize path, eliminating double slashes, etc."""
+ path = os.fspath(path)
if isinstance(path, bytes):
sep = b'\\'
altsep = b'/'
@@ -518,6 +525,7 @@ try:
except ImportError: # not running on Windows - mock up something sensible
def abspath(path):
"""Return the absolute version of a path."""
+ path = os.fspath(path)
if not isabs(path):
if isinstance(path, bytes):
cwd = os.getcwdb()
@@ -531,6 +539,7 @@ else: # use native Windows method on Windows
"""Return the absolute version of a path."""
if path: # Empty path must return current working directory.
+ path = os.fspath(path)
try:
path = _getfullpathname(path)
except OSError:
@@ -549,6 +558,7 @@ supports_unicode_filenames = (hasattr(sys, "getwindowsversion") and
def relpath(path, start=None):
"""Return a relative version of a path"""
+ path = os.fspath(path)
if isinstance(path, bytes):
sep = b'\\'
curdir = b'.'
@@ -564,6 +574,7 @@ def relpath(path, start=None):
if not path:
raise ValueError("no path specified")
+ start = os.fspath(start)
try:
start_abs = abspath(normpath(start))
path_abs = abspath(normpath(path))
@@ -607,6 +618,7 @@ def commonpath(paths):
if not paths:
raise ValueError('commonpath() arg is an empty sequence')
+ paths = tuple(map(os.fspath, paths))
if isinstance(paths[0], bytes):
sep = b'\\'
altsep = b'/'
diff --git a/Lib/opcode.py b/Lib/opcode.py
index 4c826a7..b5916b6 100644
--- a/Lib/opcode.py
+++ b/Lib/opcode.py
@@ -31,12 +31,10 @@ hasjabs = []
haslocal = []
hascompare = []
hasfree = []
-hasnargs = []
+hasnargs = [] # unused
opmap = {}
-opname = [''] * 256
-for op in range(256): opname[op] = '<%r>' % (op,)
-del op
+opname = ['<%r>' % (op,) for op in range(256)]
def def_op(name, op):
opname[op] = name
@@ -121,7 +119,7 @@ def_op('WITH_CLEANUP_FINISH', 82)
def_op('RETURN_VALUE', 83)
def_op('IMPORT_STAR', 84)
-
+def_op('SETUP_ANNOTATIONS', 85)
def_op('YIELD_VALUE', 86)
def_op('POP_BLOCK', 87)
def_op('END_FINALLY', 88)
@@ -171,13 +169,12 @@ def_op('STORE_FAST', 125) # Local variable number
haslocal.append(125)
def_op('DELETE_FAST', 126) # Local variable number
haslocal.append(126)
+name_op('STORE_ANNOTATION', 127) # Index in name list
def_op('RAISE_VARARGS', 130) # Number of raise arguments (1, 2, or 3)
-def_op('CALL_FUNCTION', 131) # #args + (#kwargs << 8)
-hasnargs.append(131)
-def_op('MAKE_FUNCTION', 132) # Number of args with default values
+def_op('CALL_FUNCTION', 131) # #args
+def_op('MAKE_FUNCTION', 132) # Flags
def_op('BUILD_SLICE', 133) # Number of items
-def_op('MAKE_CLOSURE', 134)
def_op('LOAD_CLOSURE', 135)
hasfree.append(135)
def_op('LOAD_DEREF', 136)
@@ -187,12 +184,8 @@ hasfree.append(137)
def_op('DELETE_DEREF', 138)
hasfree.append(138)
-def_op('CALL_FUNCTION_VAR', 140) # #args + (#kwargs << 8)
-hasnargs.append(140)
-def_op('CALL_FUNCTION_KW', 141) # #args + (#kwargs << 8)
-hasnargs.append(141)
-def_op('CALL_FUNCTION_VAR_KW', 142) # #args + (#kwargs << 8)
-hasnargs.append(142)
+def_op('CALL_FUNCTION_KW', 141) # #args + #kwargs
+def_op('CALL_FUNCTION_EX', 142) # Flags
jrel_op('SETUP_WITH', 143)
@@ -203,8 +196,6 @@ def_op('MAP_ADD', 147)
def_op('LOAD_CLASSDEREF', 148)
hasfree.append(148)
-jrel_op('SETUP_ASYNC_WITH', 154)
-
def_op('EXTENDED_ARG', 144)
EXTENDED_ARG = 144
@@ -214,4 +205,11 @@ def_op('BUILD_MAP_UNPACK_WITH_CALL', 151)
def_op('BUILD_TUPLE_UNPACK', 152)
def_op('BUILD_SET_UNPACK', 153)
+jrel_op('SETUP_ASYNC_WITH', 154)
+
+def_op('FORMAT_VALUE', 155)
+def_op('BUILD_CONST_KEY_MAP', 156)
+def_op('BUILD_STRING', 157)
+def_op('BUILD_TUPLE_UNPACK_WITH_CALL', 158)
+
del def_op, name_op, jrel_op, jabs_op
diff --git a/Lib/optparse.py b/Lib/optparse.py
index 74b3b36..e8ac1e1 100644
--- a/Lib/optparse.py
+++ b/Lib/optparse.py
@@ -38,7 +38,8 @@ __all__ = ['Option',
'OptionError',
'OptionConflictError',
'OptionValueError',
- 'BadOptionError']
+ 'BadOptionError',
+ 'check_choice']
__copyright__ = """
Copyright (c) 2001-2006 Gregory P. Ward. All rights reserved.
diff --git a/Lib/os.py b/Lib/os.py
index 011285e..fa06f39 100644
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -1,12 +1,12 @@
r"""OS routines for NT or Posix depending on what system we're on.
This exports:
- - all functions from posix, nt or ce, e.g. unlink, stat, etc.
+ - all functions from posix or nt, e.g. unlink, stat, etc.
- os.path is either posixpath or ntpath
- - os.name is either 'posix', 'nt' or 'ce'.
- - os.curdir is a string representing the current directory ('.' or ':')
- - os.pardir is a string representing the parent directory ('..' or '::')
- - os.sep is the (or a most common) pathname separator ('/' or ':' or '\\')
+ - os.name is either 'posix' or 'nt'
+ - os.curdir is a string representing the current directory (always '.')
+ - os.pardir is a string representing the parent directory (always '..')
+ - os.sep is the (or a most common) pathname separator ('/' or '\\')
- os.extsep is the extension separator (always '.')
- os.altsep is the alternate pathname separator (None or '/')
- os.pathsep is the component separator used in $PATH etc
@@ -22,7 +22,7 @@ and opendir), and leave all pathname manipulation to os.path
"""
#'
-
+import abc
import sys, errno
import stat as st
@@ -85,27 +85,6 @@ elif 'nt' in _names:
except ImportError:
pass
-elif 'ce' in _names:
- name = 'ce'
- linesep = '\r\n'
- from ce import *
- try:
- from ce import _exit
- __all__.append('_exit')
- except ImportError:
- pass
- # We can use the standard Windows path.
- import ntpath as path
-
- import ce
- __all__.extend(_get_exports_list(ce))
- del ce
-
- try:
- from ce import _have_functions
- except ImportError:
- pass
-
else:
raise ImportError('no os specific module found')
@@ -353,9 +332,10 @@ def walk(top, topdown=True, onerror=None, followlinks=False):
dirs.remove('CVS') # don't visit CVS directories
"""
-
+ top = fspath(top)
dirs = []
nondirs = []
+ walk_dirs = []
# We may not have read permission for top, in which case we can't
# get a list of the files the directory contains. os.walk
@@ -363,48 +343,55 @@ def walk(top, topdown=True, onerror=None, followlinks=False):
# minor reason when (say) a thousand readable directories are still
# left to visit. That logic is copied here.
try:
- if name == 'nt' and isinstance(top, bytes):
- scandir_it = _dummy_scandir(top)
- else:
- # Note that scandir is global in this module due
- # to earlier import-*.
- scandir_it = scandir(top)
- entries = list(scandir_it)
+ # Note that scandir is global in this module due
+ # to earlier import-*.
+ scandir_it = scandir(top)
except OSError as error:
if onerror is not None:
onerror(error)
return
- for entry in entries:
- try:
- is_dir = entry.is_dir()
- except OSError:
- # If is_dir() raises an OSError, consider that the entry is not
- # a directory, same behaviour than os.path.isdir().
- is_dir = False
+ with scandir_it:
+ while True:
+ try:
+ try:
+ entry = next(scandir_it)
+ except StopIteration:
+ break
+ except OSError as error:
+ if onerror is not None:
+ onerror(error)
+ return
- if is_dir:
- dirs.append(entry.name)
- else:
- nondirs.append(entry.name)
+ try:
+ is_dir = entry.is_dir()
+ except OSError:
+ # If is_dir() raises an OSError, consider that the entry is not
+ # a directory, same behaviour than os.path.isdir().
+ is_dir = False
- if not topdown and is_dir:
- # Bottom-up: recurse into sub-directory, but exclude symlinks to
- # directories if followlinks is False
- if followlinks:
- walk_into = True
+ if is_dir:
+ dirs.append(entry.name)
else:
- try:
- is_symlink = entry.is_symlink()
- except OSError:
- # If is_symlink() raises an OSError, consider that the
- # entry is not a symbolic link, same behaviour than
- # os.path.islink().
- is_symlink = False
- walk_into = not is_symlink
+ nondirs.append(entry.name)
- if walk_into:
- yield from walk(entry.path, topdown, onerror, followlinks)
+ if not topdown and is_dir:
+ # Bottom-up: recurse into sub-directory, but exclude symlinks to
+ # directories if followlinks is False
+ if followlinks:
+ walk_into = True
+ else:
+ try:
+ is_symlink = entry.is_symlink()
+ except OSError:
+ # If is_symlink() raises an OSError, consider that the
+ # entry is not a symbolic link, same behaviour than
+ # os.path.islink().
+ is_symlink = False
+ walk_into = not is_symlink
+
+ if walk_into:
+ walk_dirs.append(entry.path)
# Yield before recursion if going top down
if topdown:
@@ -421,57 +408,12 @@ def walk(top, topdown=True, onerror=None, followlinks=False):
if followlinks or not islink(new_path):
yield from walk(new_path, topdown, onerror, followlinks)
else:
+ # Recurse into sub-directories
+ for new_path in walk_dirs:
+ yield from walk(new_path, topdown, onerror, followlinks)
# Yield after recursion if going bottom up
yield top, dirs, nondirs
-class _DummyDirEntry:
- """Dummy implementation of DirEntry
-
- Only used internally by os.walk(bytes). Since os.walk() doesn't need the
- follow_symlinks parameter: don't implement it, always follow symbolic
- links.
- """
-
- def __init__(self, dir, name):
- self.name = name
- self.path = path.join(dir, name)
- # Mimick FindFirstFile/FindNextFile: we should get file attributes
- # while iterating on a directory
- self._stat = None
- self._lstat = None
- try:
- self.stat(follow_symlinks=False)
- except OSError:
- pass
-
- def stat(self, *, follow_symlinks=True):
- if follow_symlinks:
- if self._stat is None:
- self._stat = stat(self.path)
- return self._stat
- else:
- if self._lstat is None:
- self._lstat = stat(self.path, follow_symlinks=False)
- return self._lstat
-
- def is_dir(self):
- if self._lstat is not None and not self.is_symlink():
- # use the cache lstat
- stat = self.stat(follow_symlinks=False)
- return st.S_ISDIR(stat.st_mode)
-
- stat = self.stat()
- return st.S_ISDIR(stat.st_mode)
-
- def is_symlink(self):
- stat = self.stat(follow_symlinks=False)
- return st.S_ISLNK(stat.st_mode)
-
-def _dummy_scandir(dir):
- # listdir-based implementation for bytes patches on Windows
- for name in listdir(dir):
- yield _DummyDirEntry(dir, name)
-
__all__.append("walk")
if {open, stat} <= supports_dir_fd and {listdir, stat} <= supports_fd:
@@ -509,6 +451,8 @@ if {open, stat} <= supports_dir_fd and {listdir, stat} <= supports_fd:
if 'CVS' in dirs:
dirs.remove('CVS') # don't visit CVS directories
"""
+ if not isinstance(top, int) or not hasattr(top, '__index__'):
+ top = fspath(top)
# Note: To guard against symlink races, we use the standard
# lstat()/open()/fstat() trick.
orig_st = stat(top, follow_symlinks=False, dir_fd=dir_fd)
@@ -843,36 +787,31 @@ if supports_bytes_environ:
def _fscodec():
encoding = sys.getfilesystemencoding()
- if encoding == 'mbcs':
- errors = 'strict'
- else:
- errors = 'surrogateescape'
+ errors = sys.getfilesystemencodeerrors()
def fsencode(filename):
+ """Encode filename (an os.PathLike, bytes, or str) to the filesystem
+ encoding with 'surrogateescape' error handler, return bytes unchanged.
+ On Windows, use 'strict' error handler if the file system encoding is
+ 'mbcs' (which is the default encoding).
"""
- Encode filename to the filesystem encoding with 'surrogateescape' error
- handler, return bytes unchanged. On Windows, use 'strict' error handler if
- the file system encoding is 'mbcs' (which is the default encoding).
- """
- if isinstance(filename, bytes):
- return filename
- elif isinstance(filename, str):
+ filename = fspath(filename) # Does type-checking of `filename`.
+ if isinstance(filename, str):
return filename.encode(encoding, errors)
else:
- raise TypeError("expect bytes or str, not %s" % type(filename).__name__)
+ return filename
def fsdecode(filename):
+ """Decode filename (an os.PathLike, bytes, or str) from the filesystem
+ encoding with 'surrogateescape' error handler, return str unchanged. On
+ Windows, use 'strict' error handler if the file system encoding is
+ 'mbcs' (which is the default encoding).
"""
- Decode filename from the filesystem encoding with 'surrogateescape' error
- handler, return str unchanged. On Windows, use 'strict' error handler if
- the file system encoding is 'mbcs' (which is the default encoding).
- """
- if isinstance(filename, str):
- return filename
- elif isinstance(filename, bytes):
+ filename = fspath(filename) # Does type-checking of `filename`.
+ if isinstance(filename, bytes):
return filename.decode(encoding, errors)
else:
- raise TypeError("expect bytes or str, not %s" % type(filename).__name__)
+ return filename
return fsencode, fsdecode
@@ -893,6 +832,10 @@ if _exists("fork") and not _exists("spawnv") and _exists("execv"):
def _spawnvef(mode, file, args, env, func):
# Internal helper; func is the exec*() function to use
+ if not isinstance(args, (tuple, list)):
+ raise TypeError('argv must be a tuple or a list')
+ if not args or not args[0]:
+ raise ValueError('argv first element cannot be empty')
pid = fork()
if not pid:
# Child
@@ -1070,3 +1013,55 @@ def fdopen(fd, *args, **kwargs):
raise TypeError("invalid fd type (%s, expected integer)" % type(fd))
import io
return io.open(fd, *args, **kwargs)
+
+
+# For testing purposes, make sure the function is available when the C
+# implementation exists.
+def _fspath(path):
+ """Return the path representation of a path-like object.
+
+ If str or bytes is passed in, it is returned unchanged. Otherwise the
+ os.PathLike interface is used to get the path representation. If the
+ path representation is not str or bytes, TypeError is raised. If the
+ provided path is not str, bytes, or os.PathLike, TypeError is raised.
+ """
+ if isinstance(path, (str, bytes)):
+ return path
+
+ # Work from the object's type to match method resolution of other magic
+ # methods.
+ path_type = type(path)
+ try:
+ path_repr = path_type.__fspath__(path)
+ except AttributeError:
+ if hasattr(path_type, '__fspath__'):
+ raise
+ else:
+ raise TypeError("expected str, bytes or os.PathLike object, "
+ "not " + path_type.__name__)
+ if isinstance(path_repr, (str, bytes)):
+ return path_repr
+ else:
+ raise TypeError("expected {}.__fspath__() to return str or bytes, "
+ "not {}".format(path_type.__name__,
+ type(path_repr).__name__))
+
+# If there is no C implementation, make the pure Python version the
+# implementation as transparently as possible.
+if not _exists('fspath'):
+ fspath = _fspath
+ fspath.__name__ = "fspath"
+
+
+class PathLike(abc.ABC):
+
+ """Abstract base class for implementing the file system path protocol."""
+
+ @abc.abstractmethod
+ def __fspath__(self):
+ """Return the file system path representation of the object."""
+ raise NotImplementedError
+
+ @classmethod
+ def __subclasshook__(cls, subclass):
+ return hasattr(subclass, '__fspath__')
diff --git a/Lib/pathlib.py b/Lib/pathlib.py
index 1480e2f..0484dac 100644
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -178,12 +178,28 @@ class _WindowsFlavour(_Flavour):
def casefold_parts(self, parts):
return [p.lower() for p in parts]
- def resolve(self, path):
+ def resolve(self, path, strict=False):
s = str(path)
if not s:
return os.getcwd()
+ previous_s = None
if _getfinalpathname is not None:
- return self._ext_to_normal(_getfinalpathname(s))
+ if strict:
+ return self._ext_to_normal(_getfinalpathname(s))
+ else:
+ while True:
+ try:
+ s = self._ext_to_normal(_getfinalpathname(s))
+ except FileNotFoundError:
+ previous_s = s
+ s = os.path.dirname(s)
+ if previous_s == s:
+ return path
+ else:
+ if previous_s is None:
+ return s
+ else:
+ return s + os.path.sep + os.path.basename(previous_s)
# Means fallback on absolute
return None
@@ -285,7 +301,7 @@ class _PosixFlavour(_Flavour):
def casefold_parts(self, parts):
return parts
- def resolve(self, path):
+ def resolve(self, path, strict=False):
sep = self.sep
accessor = path._accessor
seen = {}
@@ -315,7 +331,10 @@ class _PosixFlavour(_Flavour):
target = accessor.readlink(newpath)
except OSError as e:
if e.errno != EINVAL:
- raise
+ if strict:
+ raise
+ else:
+ return newpath
# Not a symlink
path = newpath
else:
@@ -385,6 +404,8 @@ class _NormalAccessor(_Accessor):
listdir = _wrap_strfunc(os.listdir)
+ scandir = _wrap_strfunc(os.scandir)
+
chmod = _wrap_strfunc(os.chmod)
if hasattr(os, "lchmod"):
@@ -429,25 +450,6 @@ _normal_accessor = _NormalAccessor()
# Globbing helpers
#
-@contextmanager
-def _cached(func):
- try:
- func.__cached__
- yield func
- except AttributeError:
- cache = {}
- def wrapper(*args):
- try:
- return cache[args]
- except KeyError:
- value = cache[args] = func(*args)
- return value
- wrapper.__cached__ = True
- try:
- yield wrapper
- finally:
- cache.clear()
-
def _make_selector(pattern_parts):
pat = pattern_parts[0]
child_parts = pattern_parts[1:]
@@ -473,8 +475,10 @@ class _Selector:
self.child_parts = child_parts
if child_parts:
self.successor = _make_selector(child_parts)
+ self.dironly = True
else:
self.successor = _TerminatingSelector()
+ self.dironly = False
def select_from(self, parent_path):
"""Iterate over all child paths of `parent_path` matched by this
@@ -482,13 +486,15 @@ class _Selector:
path_cls = type(parent_path)
is_dir = path_cls.is_dir
exists = path_cls.exists
- listdir = parent_path._accessor.listdir
- return self._select_from(parent_path, is_dir, exists, listdir)
+ scandir = parent_path._accessor.scandir
+ if not is_dir(parent_path):
+ return iter([])
+ return self._select_from(parent_path, is_dir, exists, scandir)
class _TerminatingSelector:
- def _select_from(self, parent_path, is_dir, exists, listdir):
+ def _select_from(self, parent_path, is_dir, exists, scandir):
yield parent_path
@@ -498,13 +504,11 @@ class _PreciseSelector(_Selector):
self.name = name
_Selector.__init__(self, child_parts)
- def _select_from(self, parent_path, is_dir, exists, listdir):
+ def _select_from(self, parent_path, is_dir, exists, scandir):
try:
- if not is_dir(parent_path):
- return
path = parent_path._make_child_relpath(self.name)
- if exists(path):
- for p in self.successor._select_from(path, is_dir, exists, listdir):
+ if (is_dir if self.dironly else exists)(path):
+ for p in self.successor._select_from(path, is_dir, exists, scandir):
yield p
except PermissionError:
return
@@ -516,17 +520,18 @@ class _WildcardSelector(_Selector):
self.pat = re.compile(fnmatch.translate(pat))
_Selector.__init__(self, child_parts)
- def _select_from(self, parent_path, is_dir, exists, listdir):
+ def _select_from(self, parent_path, is_dir, exists, scandir):
try:
- if not is_dir(parent_path):
- return
cf = parent_path._flavour.casefold
- for name in listdir(parent_path):
- casefolded = cf(name)
- if self.pat.match(casefolded):
- path = parent_path._make_child_relpath(name)
- for p in self.successor._select_from(path, is_dir, exists, listdir):
- yield p
+ entries = list(scandir(parent_path))
+ for entry in entries:
+ if not self.dironly or entry.is_dir():
+ name = entry.name
+ casefolded = cf(name)
+ if self.pat.match(casefolded):
+ path = parent_path._make_child_relpath(name)
+ for p in self.successor._select_from(path, is_dir, exists, scandir):
+ yield p
except PermissionError:
return
@@ -537,32 +542,30 @@ class _RecursiveWildcardSelector(_Selector):
def __init__(self, pat, child_parts):
_Selector.__init__(self, child_parts)
- def _iterate_directories(self, parent_path, is_dir, listdir):
+ def _iterate_directories(self, parent_path, is_dir, scandir):
yield parent_path
try:
- for name in listdir(parent_path):
- path = parent_path._make_child_relpath(name)
- if is_dir(path) and not path.is_symlink():
- for p in self._iterate_directories(path, is_dir, listdir):
+ entries = list(scandir(parent_path))
+ for entry in entries:
+ if entry.is_dir() and not entry.is_symlink():
+ path = parent_path._make_child_relpath(entry.name)
+ for p in self._iterate_directories(path, is_dir, scandir):
yield p
except PermissionError:
return
- def _select_from(self, parent_path, is_dir, exists, listdir):
+ def _select_from(self, parent_path, is_dir, exists, scandir):
try:
- if not is_dir(parent_path):
- return
- with _cached(listdir) as listdir:
- yielded = set()
- try:
- successor_select = self.successor._select_from
- for starting_point in self._iterate_directories(parent_path, is_dir, listdir):
- for p in successor_select(starting_point, is_dir, exists, listdir):
- if p not in yielded:
- yield p
- yielded.add(p)
- finally:
- yielded.clear()
+ yielded = set()
+ try:
+ successor_select = self.successor._select_from
+ for starting_point in self._iterate_directories(parent_path, is_dir, scandir):
+ for p in successor_select(starting_point, is_dir, exists, scandir):
+ if p not in yielded:
+ yield p
+ yielded.add(p)
+ finally:
+ yielded.clear()
except PermissionError:
return
@@ -634,13 +637,16 @@ class PurePath(object):
for a in args:
if isinstance(a, PurePath):
parts += a._parts
- elif isinstance(a, str):
- # Force-cast str subclasses to str (issue #21127)
- parts.append(str(a))
else:
- raise TypeError(
- "argument should be a path or str object, not %r"
- % type(a))
+ a = os.fspath(a)
+ if isinstance(a, str):
+ # Force-cast str subclasses to str (issue #21127)
+ parts.append(str(a))
+ else:
+ raise TypeError(
+ "argument should be a str object or an os.PathLike "
+ "object returning str, not %r"
+ % type(a))
return cls._flavour.parse_parts(parts)
@classmethod
@@ -693,6 +699,9 @@ class PurePath(object):
self._parts) or '.'
return self._str
+ def __fspath__(self):
+ return str(self)
+
def as_posix(self):
"""Return the string representation of the path with forward (/)
slashes."""
@@ -943,6 +952,10 @@ class PurePath(object):
return False
return True
+# Can't subclass os.PathLike from PurePath and keep the constructor
+# optimizations in PurePath._parse_args().
+os.PathLike.register(PurePath)
+
class PurePosixPath(PurePath):
_flavour = _posix_flavour
@@ -1098,7 +1111,7 @@ class Path(PurePath):
obj._init(template=self)
return obj
- def resolve(self):
+ def resolve(self, strict=False):
"""
Make the path absolute, resolving all symlinks on the way and also
normalizing it (for example turning slashes into backslashes under
@@ -1106,7 +1119,7 @@ class Path(PurePath):
"""
if self._closed:
self._raise_closed()
- s = self._flavour.resolve(self)
+ s = self._flavour.resolve(self, strict=strict)
if s is None:
# No symlink resolution => for consistency, raise an error if
# the path doesn't exist or is forbidden
diff --git a/Lib/pdb.py b/Lib/pdb.py
index 4797265..97618b0 100755
--- a/Lib/pdb.py
+++ b/Lib/pdb.py
@@ -52,7 +52,8 @@ If a file ".pdbrc" exists in your home directory or in the current
directory, it is read in and executed as if it had been typed at the
debugger prompt. This is particularly useful for aliases. If both
files exist, the one in the home directory is read first and aliases
-defined there can be overridden by the local file.
+defined there can be overridden by the local file. This behavior can be
+disabled by passing the "readrc=False" argument to the Pdb constructor.
Aside from aliases, the debugger is not directly programmable; but it
is implemented as a class from which you can derive your own debugger
@@ -137,7 +138,7 @@ class Pdb(bdb.Bdb, cmd.Cmd):
_previous_sigint_handler = None
def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None,
- nosigint=False):
+ nosigint=False, readrc=True):
bdb.Bdb.__init__(self, skip=skip)
cmd.Cmd.__init__(self, completekey, stdin, stdout)
if stdout:
@@ -160,18 +161,19 @@ class Pdb(bdb.Bdb, cmd.Cmd):
# Read $HOME/.pdbrc and ./.pdbrc
self.rcLines = []
- if 'HOME' in os.environ:
- envHome = os.environ['HOME']
+ if readrc:
+ if 'HOME' in os.environ:
+ envHome = os.environ['HOME']
+ try:
+ with open(os.path.join(envHome, ".pdbrc")) as rcFile:
+ self.rcLines.extend(rcFile)
+ except OSError:
+ pass
try:
- with open(os.path.join(envHome, ".pdbrc")) as rcFile:
+ with open(".pdbrc") as rcFile:
self.rcLines.extend(rcFile)
except OSError:
pass
- try:
- with open(".pdbrc") as rcFile:
- self.rcLines.extend(rcFile)
- except OSError:
- pass
self.commands = {} # associates a command list to breakpoint numbers
self.commands_doprompt = {} # for each bp num, tells if the prompt
diff --git a/Lib/pickle.py b/Lib/pickle.py
index 040ecb2..c8370c9 100644
--- a/Lib/pickle.py
+++ b/Lib/pickle.py
@@ -27,6 +27,7 @@ from types import FunctionType
from copyreg import dispatch_table
from copyreg import _extension_registry, _inverted_registry, _extension_cache
from itertools import islice
+from functools import partial
import sys
from sys import maxsize
from struct import pack, unpack
@@ -548,7 +549,7 @@ class _Pickler:
write = self.write
func_name = getattr(func, "__name__", "")
- if self.proto >= 4 and func_name == "__newobj_ex__":
+ if self.proto >= 2 and func_name == "__newobj_ex__":
cls, args, kwargs = args
if not hasattr(cls, "__new__"):
raise PicklingError("args[0] from {} args has no __new__"
@@ -556,10 +557,16 @@ class _Pickler:
if obj is not None and cls is not obj.__class__:
raise PicklingError("args[0] from {} args has the wrong class"
.format(func_name))
- save(cls)
- save(args)
- save(kwargs)
- write(NEWOBJ_EX)
+ if self.proto >= 4:
+ save(cls)
+ save(args)
+ save(kwargs)
+ write(NEWOBJ_EX)
+ else:
+ func = partial(cls.__new__, cls, *args, **kwargs)
+ save(func)
+ save(())
+ write(REDUCE)
elif self.proto >= 2 and func_name == "__newobj__":
# A __reduce__ implementation can direct protocol 2 or newer to
# use the more efficient NEWOBJ opcode, while still
@@ -1028,7 +1035,7 @@ class _Unpickler:
self._unframer = _Unframer(self._file_read, self._file_readline)
self.read = self._unframer.read
self.readline = self._unframer.readline
- self.mark = object() # any new unique object
+ self.metastack = []
self.stack = []
self.append = self.stack.append
self.proto = 0
@@ -1044,20 +1051,12 @@ class _Unpickler:
except _Stop as stopinst:
return stopinst.value
- # Return largest index k such that self.stack[k] is self.mark.
- # If the stack doesn't contain a mark, eventually raises IndexError.
- # This could be sped by maintaining another stack, of indices at which
- # the mark appears. For that matter, the latter stack would suffice,
- # and we wouldn't need to push mark objects on self.stack at all.
- # Doing so is probably a good thing, though, since if the pickle is
- # corrupt (or hostile) we may get a clue from finding self.mark embedded
- # in unpickled objects.
- def marker(self):
- stack = self.stack
- mark = self.mark
- k = len(stack)-1
- while stack[k] is not mark: k = k-1
- return k
+ # Return a list of items pushed in the stack after last MARK instruction.
+ def pop_mark(self):
+ items = self.stack
+ self.stack = self.metastack.pop()
+ self.append = self.stack.append
+ return items
def persistent_load(self, pid):
raise UnpicklingError("unsupported persistent id encountered")
@@ -1238,8 +1237,8 @@ class _Unpickler:
dispatch[SHORT_BINUNICODE[0]] = load_short_binunicode
def load_tuple(self):
- k = self.marker()
- self.stack[k:] = [tuple(self.stack[k+1:])]
+ items = self.pop_mark()
+ self.append(tuple(items))
dispatch[TUPLE[0]] = load_tuple
def load_empty_tuple(self):
@@ -1271,21 +1270,20 @@ class _Unpickler:
dispatch[EMPTY_SET[0]] = load_empty_set
def load_frozenset(self):
- k = self.marker()
- self.stack[k:] = [frozenset(self.stack[k+1:])]
+ items = self.pop_mark()
+ self.append(frozenset(items))
dispatch[FROZENSET[0]] = load_frozenset
def load_list(self):
- k = self.marker()
- self.stack[k:] = [self.stack[k+1:]]
+ items = self.pop_mark()
+ self.append(items)
dispatch[LIST[0]] = load_list
def load_dict(self):
- k = self.marker()
- items = self.stack[k+1:]
+ items = self.pop_mark()
d = {items[i]: items[i+1]
for i in range(0, len(items), 2)}
- self.stack[k:] = [d]
+ self.append(d)
dispatch[DICT[0]] = load_dict
# INST and OBJ differ only in how they get a class object. It's not
@@ -1293,9 +1291,7 @@ class _Unpickler:
# previously diverged and grew different bugs.
# klass is the class to instantiate, and k points to the topmost mark
# object, following which are the arguments for klass.__init__.
- def _instantiate(self, klass, k):
- args = tuple(self.stack[k+1:])
- del self.stack[k:]
+ def _instantiate(self, klass, args):
if (args or not isinstance(klass, type) or
hasattr(klass, "__getinitargs__")):
try:
@@ -1311,14 +1307,14 @@ class _Unpickler:
module = self.readline()[:-1].decode("ascii")
name = self.readline()[:-1].decode("ascii")
klass = self.find_class(module, name)
- self._instantiate(klass, self.marker())
+ self._instantiate(klass, self.pop_mark())
dispatch[INST[0]] = load_inst
def load_obj(self):
# Stack is ... markobject classobject arg1 arg2 ...
- k = self.marker()
- klass = self.stack.pop(k+1)
- self._instantiate(klass, k)
+ args = self.pop_mark()
+ cls = args.pop(0)
+ self._instantiate(cls, args)
dispatch[OBJ[0]] = load_obj
def load_newobj(self):
@@ -1403,12 +1399,14 @@ class _Unpickler:
dispatch[REDUCE[0]] = load_reduce
def load_pop(self):
- del self.stack[-1]
+ if self.stack:
+ del self.stack[-1]
+ else:
+ self.pop_mark()
dispatch[POP[0]] = load_pop
def load_pop_mark(self):
- k = self.marker()
- del self.stack[k:]
+ self.pop_mark()
dispatch[POP_MARK[0]] = load_pop_mark
def load_dup(self):
@@ -1464,17 +1462,14 @@ class _Unpickler:
dispatch[APPEND[0]] = load_append
def load_appends(self):
- stack = self.stack
- mark = self.marker()
- list_obj = stack[mark - 1]
- items = stack[mark + 1:]
+ items = self.pop_mark()
+ list_obj = self.stack[-1]
if isinstance(list_obj, list):
list_obj.extend(items)
else:
append = list_obj.append
for item in items:
append(item)
- del stack[mark:]
dispatch[APPENDS[0]] = load_appends
def load_setitem(self):
@@ -1486,27 +1481,21 @@ class _Unpickler:
dispatch[SETITEM[0]] = load_setitem
def load_setitems(self):
- stack = self.stack
- mark = self.marker()
- dict = stack[mark - 1]
- for i in range(mark + 1, len(stack), 2):
- dict[stack[i]] = stack[i + 1]
-
- del stack[mark:]
+ items = self.pop_mark()
+ dict = self.stack[-1]
+ for i in range(0, len(items), 2):
+ dict[items[i]] = items[i + 1]
dispatch[SETITEMS[0]] = load_setitems
def load_additems(self):
- stack = self.stack
- mark = self.marker()
- set_obj = stack[mark - 1]
- items = stack[mark + 1:]
+ items = self.pop_mark()
+ set_obj = self.stack[-1]
if isinstance(set_obj, set):
set_obj.update(items)
else:
add = set_obj.add
for item in items:
add(item)
- del stack[mark:]
dispatch[ADDITEMS[0]] = load_additems
def load_build(self):
@@ -1534,7 +1523,9 @@ class _Unpickler:
dispatch[BUILD[0]] = load_build
def load_mark(self):
- self.append(self.mark)
+ self.metastack.append(self.stack)
+ self.stack = []
+ self.append = self.stack.append
dispatch[MARK[0]] = load_mark
def load_stop(self):
diff --git a/Lib/pickletools.py b/Lib/pickletools.py
index 16ae7d5..5e129b5 100644
--- a/Lib/pickletools.py
+++ b/Lib/pickletools.py
@@ -707,7 +707,7 @@ def read_unicodestring8(f):
>>> enc = s.encode('utf-8')
>>> enc
b'abcd\xea\xaf\x8d'
- >>> n = bytes([len(enc)]) + bytes(7) # little-endian 8-byte length
+ >>> n = bytes([len(enc)]) + b'\0' * 7 # little-endian 8-byte length
>>> t = read_unicodestring8(io.BytesIO(n + enc + b'junk'))
>>> s == t
True
@@ -2440,6 +2440,7 @@ def dis(pickle, out=None, memo=None, indentlevel=4, annotate=0):
if opcode.name in ("PUT", "BINPUT", "LONG_BINPUT", "MEMOIZE"):
if opcode.name == "MEMOIZE":
memo_idx = len(memo)
+ markmsg = "(as %d)" % memo_idx
else:
assert arg is not None
memo_idx = arg
diff --git a/Lib/pkgutil.py b/Lib/pkgutil.py
index 81d418d..e37ad45 100644
--- a/Lib/pkgutil.py
+++ b/Lib/pkgutil.py
@@ -1,5 +1,6 @@
"""Utilities to support packages."""
+from collections import namedtuple
from functools import singledispatch as simplegeneric
import importlib
import importlib.util
@@ -14,9 +15,14 @@ __all__ = [
'get_importer', 'iter_importers', 'get_loader', 'find_loader',
'walk_packages', 'iter_modules', 'get_data',
'ImpImporter', 'ImpLoader', 'read_code', 'extend_path',
+ 'ModuleInfo',
]
+ModuleInfo = namedtuple('ModuleInfo', 'module_finder name ispkg')
+ModuleInfo.__doc__ = 'A namedtuple with minimal info about a module.'
+
+
def _get_spec(finder, name):
"""Return the finder-specific module spec."""
# Works with legacy finders.
@@ -45,7 +51,7 @@ def read_code(stream):
def walk_packages(path=None, prefix='', onerror=None):
- """Yields (module_finder, name, ispkg) for all modules recursively
+ """Yields ModuleInfo for all modules recursively
on path, or, if path is None, all accessible modules.
'path' should be either None or a list of paths to look for
@@ -78,31 +84,31 @@ def walk_packages(path=None, prefix='', onerror=None):
return True
m[p] = True
- for importer, name, ispkg in iter_modules(path, prefix):
- yield importer, name, ispkg
+ for info in iter_modules(path, prefix):
+ yield info
- if ispkg:
+ if info.ispkg:
try:
- __import__(name)
+ __import__(info.name)
except ImportError:
if onerror is not None:
- onerror(name)
+ onerror(info.name)
except Exception:
if onerror is not None:
- onerror(name)
+ onerror(info.name)
else:
raise
else:
- path = getattr(sys.modules[name], '__path__', None) or []
+ path = getattr(sys.modules[info.name], '__path__', None) or []
# don't traverse path items we've seen before
path = [p for p in path if not seen(p)]
- yield from walk_packages(path, name+'.', onerror)
+ yield from walk_packages(path, info.name+'.', onerror)
def iter_modules(path=None, prefix=''):
- """Yields (module_finder, name, ispkg) for all submodules on path,
+ """Yields ModuleInfo for all submodules on path,
or, if path is None, all top-level modules on sys.path.
'path' should be either None or a list of paths to look for
@@ -111,7 +117,6 @@ def iter_modules(path=None, prefix=''):
'prefix' is a string to output on the front of every module name
on output.
"""
-
if path is None:
importers = iter_importers()
else:
@@ -122,7 +127,7 @@ def iter_modules(path=None, prefix=''):
for name, ispkg in iter_importer_modules(i, prefix):
if name not in yielded:
yielded[name] = 1
- yield i, name, ispkg
+ yield ModuleInfo(i, name, ispkg)
@simplegeneric
@@ -180,7 +185,7 @@ iter_importer_modules.register(
def _import_imp():
global imp
with warnings.catch_warnings():
- warnings.simplefilter('ignore', PendingDeprecationWarning)
+ warnings.simplefilter('ignore', DeprecationWarning)
imp = importlib.import_module('imp')
class ImpImporter:
diff --git a/Lib/plat-aix4/IN.py b/Lib/plat-aix4/IN.py
deleted file mode 100644
index 43f8f23..0000000
--- a/Lib/plat-aix4/IN.py
+++ /dev/null
@@ -1,165 +0,0 @@
-# Generated by h2py from /usr/include/netinet/in.h
-
-# Included from net/nh.h
-
-# Included from sys/machine.h
-LITTLE_ENDIAN = 1234
-BIG_ENDIAN = 4321
-PDP_ENDIAN = 3412
-BYTE_ORDER = BIG_ENDIAN
-DEFAULT_GPR = 0xDEADBEEF
-MSR_EE = 0x8000
-MSR_PR = 0x4000
-MSR_FP = 0x2000
-MSR_ME = 0x1000
-MSR_FE = 0x0800
-MSR_FE0 = 0x0800
-MSR_SE = 0x0400
-MSR_BE = 0x0200
-MSR_IE = 0x0100
-MSR_FE1 = 0x0100
-MSR_AL = 0x0080
-MSR_IP = 0x0040
-MSR_IR = 0x0020
-MSR_DR = 0x0010
-MSR_PM = 0x0004
-DEFAULT_MSR = (MSR_EE | MSR_ME | MSR_AL | MSR_IR | MSR_DR)
-DEFAULT_USER_MSR = (DEFAULT_MSR | MSR_PR)
-CR_LT = 0x80000000
-CR_GT = 0x40000000
-CR_EQ = 0x20000000
-CR_SO = 0x10000000
-CR_FX = 0x08000000
-CR_FEX = 0x04000000
-CR_VX = 0x02000000
-CR_OX = 0x01000000
-XER_SO = 0x80000000
-XER_OV = 0x40000000
-XER_CA = 0x20000000
-def XER_COMP_BYTE(xer): return ((xer >> 8) & 0x000000FF)
-
-def XER_LENGTH(xer): return (xer & 0x0000007F)
-
-DSISR_IO = 0x80000000
-DSISR_PFT = 0x40000000
-DSISR_LOCK = 0x20000000
-DSISR_FPIO = 0x10000000
-DSISR_PROT = 0x08000000
-DSISR_LOOP = 0x04000000
-DSISR_DRST = 0x04000000
-DSISR_ST = 0x02000000
-DSISR_SEGB = 0x01000000
-DSISR_DABR = 0x00400000
-DSISR_EAR = 0x00100000
-SRR_IS_PFT = 0x40000000
-SRR_IS_ISPEC = 0x20000000
-SRR_IS_IIO = 0x10000000
-SRR_IS_GUARD = 0x10000000
-SRR_IS_PROT = 0x08000000
-SRR_IS_LOOP = 0x04000000
-SRR_PR_FPEN = 0x00100000
-SRR_PR_INVAL = 0x00080000
-SRR_PR_PRIV = 0x00040000
-SRR_PR_TRAP = 0x00020000
-SRR_PR_IMPRE = 0x00010000
-def BUID_7F_SRVAL(raddr): return (0x87F00000 | (((uint)(raddr)) >> 28))
-
-BT_256M = 0x1FFC
-BT_128M = 0x0FFC
-BT_64M = 0x07FC
-BT_32M = 0x03FC
-BT_16M = 0x01FC
-BT_8M = 0x00FC
-BT_4M = 0x007C
-BT_2M = 0x003C
-BT_1M = 0x001C
-BT_512K = 0x000C
-BT_256K = 0x0004
-BT_128K = 0x0000
-BT_NOACCESS = 0x0
-BT_RDONLY = 0x1
-BT_WRITE = 0x2
-BT_VS = 0x2
-BT_VP = 0x1
-def BAT_ESEG(dbatu): return (((uint)(dbatu) >> 28))
-
-MIN_BAT_SIZE = 0x00020000
-MAX_BAT_SIZE = 0x10000000
-def ntohl(x): return (x)
-
-def ntohs(x): return (x)
-
-def htonl(x): return (x)
-
-def htons(x): return (x)
-
-IPPROTO_IP = 0
-IPPROTO_ICMP = 1
-IPPROTO_IGMP = 2
-IPPROTO_GGP = 3
-IPPROTO_TCP = 6
-IPPROTO_EGP = 8
-IPPROTO_PUP = 12
-IPPROTO_UDP = 17
-IPPROTO_IDP = 22
-IPPROTO_TP = 29
-IPPROTO_LOCAL = 63
-IPPROTO_EON = 80
-IPPROTO_BIP = 0x53
-IPPROTO_RAW = 255
-IPPROTO_MAX = 256
-IPPORT_RESERVED = 1024
-IPPORT_USERRESERVED = 5000
-IPPORT_TIMESERVER = 37
-def IN_CLASSA(i): return (((int)(i) & 0x80000000) == 0)
-
-IN_CLASSA_NET = 0xff000000
-IN_CLASSA_NSHIFT = 24
-IN_CLASSA_HOST = 0x00ffffff
-IN_CLASSA_MAX = 128
-def IN_CLASSB(i): return (((int)(i) & 0xc0000000) == 0x80000000)
-
-IN_CLASSB_NET = 0xffff0000
-IN_CLASSB_NSHIFT = 16
-IN_CLASSB_HOST = 0x0000ffff
-IN_CLASSB_MAX = 65536
-def IN_CLASSC(i): return (((int)(i) & 0xe0000000) == 0xc0000000)
-
-IN_CLASSC_NET = 0xffffff00
-IN_CLASSC_NSHIFT = 8
-IN_CLASSC_HOST = 0x000000ff
-def IN_CLASSD(i): return (((int)(i) & 0xf0000000) == 0xe0000000)
-
-def IN_MULTICAST(i): return IN_CLASSD(i)
-
-IN_CLASSD_NET = 0xf0000000
-IN_CLASSD_NSHIFT = 28
-IN_CLASSD_HOST = 0x0fffffff
-INADDR_UNSPEC_GROUP = 0xe0000000
-INADDR_ALLHOSTS_GROUP = 0xe0000001
-INADDR_MAX_LOCAL_GROUP = 0xe00000ff
-def IN_EXPERIMENTAL(i): return (((int)(i) & 0xe0000000) == 0xe0000000)
-
-def IN_BADCLASS(i): return (((int)(i) & 0xf0000000) == 0xf0000000)
-
-INADDR_ANY = 0x00000000
-INADDR_BROADCAST = 0xffffffff
-INADDR_LOOPBACK = 0x7f000001
-INADDR_NONE = 0xffffffff
-IN_LOOPBACKNET = 127
-IP_OPTIONS = 1
-IP_HDRINCL = 2
-IP_TOS = 3
-IP_TTL = 4
-IP_RECVOPTS = 5
-IP_RECVRETOPTS = 6
-IP_RECVDSTADDR = 7
-IP_RETOPTS = 8
-IP_MULTICAST_IF = 9
-IP_MULTICAST_TTL = 10
-IP_MULTICAST_LOOP = 11
-IP_ADD_MEMBERSHIP = 12
-IP_DROP_MEMBERSHIP = 13
-IP_DEFAULT_MULTICAST_TTL = 1
-IP_DEFAULT_MULTICAST_LOOP = 1
-IP_MAX_MEMBERSHIPS = 20
diff --git a/Lib/plat-aix4/regen b/Lib/plat-aix4/regen
deleted file mode 100755
index 57a71c4..0000000
--- a/Lib/plat-aix4/regen
+++ /dev/null
@@ -1,8 +0,0 @@
-#! /bin/sh
-case `uname -sv` in
-'AIX 4'*) ;;
-*) echo Probably not on an AIX 4 system 1>&2
- exit 1;;
-esac
-set -v
-h2py.py -i '(u_long)' /usr/include/netinet/in.h
diff --git a/Lib/plat-darwin/IN.py b/Lib/plat-darwin/IN.py
deleted file mode 100644
index 6b6be33..0000000
--- a/Lib/plat-darwin/IN.py
+++ /dev/null
@@ -1,662 +0,0 @@
-# Generated by h2py from /usr/include/netinet/in.h
-
-# Included from sys/appleapiopts.h
-
-# Included from sys/_types.h
-
-# Included from sys/cdefs.h
-def __P(protos): return protos
-
-def __STRING(x): return #x
-
-def __P(protos): return ()
-
-def __STRING(x): return "x"
-
-def __attribute__(x): return
-
-def __COPYRIGHT(s): return __IDSTRING(copyright,s)
-
-def __RCSID(s): return __IDSTRING(rcsid,s)
-
-def __SCCSID(s): return __IDSTRING(sccsid,s)
-
-def __PROJECT_VERSION(s): return __IDSTRING(project_version,s)
-
-__DARWIN_UNIX03 = 1
-__DARWIN_UNIX03 = 0
-__DARWIN_UNIX03 = 0
-__DARWIN_UNIX03 = 1
-__DARWIN_64_BIT_INO_T = 1
-__DARWIN_64_BIT_INO_T = 0
-__DARWIN_64_BIT_INO_T = 0
-__DARWIN_NON_CANCELABLE = 0
-__DARWIN_VERS_1050 = 1
-__DARWIN_VERS_1050 = 0
-__DARWIN_SUF_UNIX03 = "$UNIX2003"
-__DARWIN_SUF_UNIX03_SET = 1
-__DARWIN_SUF_UNIX03_SET = 0
-__DARWIN_SUF_64_BIT_INO_T = "$INODE64"
-__DARWIN_SUF_NON_CANCELABLE = "$NOCANCEL"
-__DARWIN_SUF_1050 = "$1050"
-__DARWIN_SUF_UNIX03_SET = 0
-__DARWIN_SUF_EXTSN = "$DARWIN_EXTSN"
-__DARWIN_LONG_DOUBLE_IS_DOUBLE = 0
-def __DARWIN_LDBL_COMPAT(x): return
-
-def __DARWIN_LDBL_COMPAT2(x): return
-
-__DARWIN_LONG_DOUBLE_IS_DOUBLE = 1
-def __DARWIN_LDBL_COMPAT(x): return
-
-def __DARWIN_LDBL_COMPAT2(x): return
-
-__DARWIN_LONG_DOUBLE_IS_DOUBLE = 0
-_DARWIN_FEATURE_LONG_DOUBLE_IS_DOUBLE = 1
-_DARWIN_FEATURE_UNIX_CONFORMANCE = 3
-_DARWIN_FEATURE_64_BIT_INODE = 1
-
-# Included from machine/_types.h
-__PTHREAD_SIZE__ = 1168
-__PTHREAD_ATTR_SIZE__ = 56
-__PTHREAD_MUTEXATTR_SIZE__ = 8
-__PTHREAD_MUTEX_SIZE__ = 56
-__PTHREAD_CONDATTR_SIZE__ = 8
-__PTHREAD_COND_SIZE__ = 40
-__PTHREAD_ONCE_SIZE__ = 8
-__PTHREAD_RWLOCK_SIZE__ = 192
-__PTHREAD_RWLOCKATTR_SIZE__ = 16
-__PTHREAD_SIZE__ = 596
-__PTHREAD_ATTR_SIZE__ = 36
-__PTHREAD_MUTEXATTR_SIZE__ = 8
-__PTHREAD_MUTEX_SIZE__ = 40
-__PTHREAD_CONDATTR_SIZE__ = 4
-__PTHREAD_COND_SIZE__ = 24
-__PTHREAD_ONCE_SIZE__ = 4
-__PTHREAD_RWLOCK_SIZE__ = 124
-__PTHREAD_RWLOCKATTR_SIZE__ = 12
-__DARWIN_NULL = 0
-
-# Included from stdint.h
-__WORDSIZE = 64
-__WORDSIZE = 32
-INT8_MAX = 127
-INT16_MAX = 32767
-INT32_MAX = 2147483647
-INT8_MIN = -128
-INT16_MIN = -32768
-INT32_MIN = (-INT32_MAX-1)
-UINT8_MAX = 255
-UINT16_MAX = 65535
-INT_LEAST8_MIN = INT8_MIN
-INT_LEAST16_MIN = INT16_MIN
-INT_LEAST32_MIN = INT32_MIN
-INT_LEAST8_MAX = INT8_MAX
-INT_LEAST16_MAX = INT16_MAX
-INT_LEAST32_MAX = INT32_MAX
-UINT_LEAST8_MAX = UINT8_MAX
-UINT_LEAST16_MAX = UINT16_MAX
-INT_FAST8_MIN = INT8_MIN
-INT_FAST16_MIN = INT16_MIN
-INT_FAST32_MIN = INT32_MIN
-INT_FAST8_MAX = INT8_MAX
-INT_FAST16_MAX = INT16_MAX
-INT_FAST32_MAX = INT32_MAX
-UINT_FAST8_MAX = UINT8_MAX
-UINT_FAST16_MAX = UINT16_MAX
-INTPTR_MIN = INT32_MIN
-INTPTR_MAX = INT32_MAX
-PTRDIFF_MIN = INT32_MIN
-PTRDIFF_MAX = INT32_MAX
-WCHAR_MAX = 0x7fffffff
-WCHAR_MIN = 0
-WCHAR_MIN = (-WCHAR_MAX-1)
-WINT_MIN = INT32_MIN
-WINT_MAX = INT32_MAX
-SIG_ATOMIC_MIN = INT32_MIN
-SIG_ATOMIC_MAX = INT32_MAX
-def INT8_C(v): return (v)
-
-def INT16_C(v): return (v)
-
-def INT32_C(v): return (v)
-
-
-# Included from sys/socket.h
-
-# Included from machine/_param.h
-SOCK_STREAM = 1
-SOCK_DGRAM = 2
-SOCK_RAW = 3
-SOCK_RDM = 4
-SOCK_SEQPACKET = 5
-SO_DEBUG = 0x0001
-SO_ACCEPTCONN = 0x0002
-SO_REUSEADDR = 0x0004
-SO_KEEPALIVE = 0x0008
-SO_DONTROUTE = 0x0010
-SO_BROADCAST = 0x0020
-SO_USELOOPBACK = 0x0040
-SO_LINGER = 0x0080
-SO_LINGER = 0x1080
-SO_OOBINLINE = 0x0100
-SO_REUSEPORT = 0x0200
-SO_TIMESTAMP = 0x0400
-SO_ACCEPTFILTER = 0x1000
-SO_DONTTRUNC = 0x2000
-SO_WANTMORE = 0x4000
-SO_WANTOOBFLAG = 0x8000
-SO_SNDBUF = 0x1001
-SO_RCVBUF = 0x1002
-SO_SNDLOWAT = 0x1003
-SO_RCVLOWAT = 0x1004
-SO_SNDTIMEO = 0x1005
-SO_RCVTIMEO = 0x1006
-SO_ERROR = 0x1007
-SO_TYPE = 0x1008
-SO_NREAD = 0x1020
-SO_NKE = 0x1021
-SO_NOSIGPIPE = 0x1022
-SO_NOADDRERR = 0x1023
-SO_NWRITE = 0x1024
-SO_REUSESHAREUID = 0x1025
-SO_NOTIFYCONFLICT = 0x1026
-SO_LINGER_SEC = 0x1080
-SO_RESTRICTIONS = 0x1081
-SO_RESTRICT_DENYIN = 0x00000001
-SO_RESTRICT_DENYOUT = 0x00000002
-SO_RESTRICT_DENYSET = (-2147483648)
-SO_LABEL = 0x1010
-SO_PEERLABEL = 0x1011
-SOL_SOCKET = 0xffff
-AF_UNSPEC = 0
-AF_UNIX = 1
-AF_LOCAL = AF_UNIX
-AF_INET = 2
-AF_IMPLINK = 3
-AF_PUP = 4
-AF_CHAOS = 5
-AF_NS = 6
-AF_ISO = 7
-AF_OSI = AF_ISO
-AF_ECMA = 8
-AF_DATAKIT = 9
-AF_CCITT = 10
-AF_SNA = 11
-AF_DECnet = 12
-AF_DLI = 13
-AF_LAT = 14
-AF_HYLINK = 15
-AF_APPLETALK = 16
-AF_ROUTE = 17
-AF_LINK = 18
-pseudo_AF_XTP = 19
-AF_COIP = 20
-AF_CNT = 21
-pseudo_AF_RTIP = 22
-AF_IPX = 23
-AF_SIP = 24
-pseudo_AF_PIP = 25
-AF_NDRV = 27
-AF_ISDN = 28
-AF_E164 = AF_ISDN
-pseudo_AF_KEY = 29
-AF_INET6 = 30
-AF_NATM = 31
-AF_SYSTEM = 32
-AF_NETBIOS = 33
-AF_PPP = 34
-AF_ATM = 30
-pseudo_AF_HDRCMPLT = 35
-AF_RESERVED_36 = 36
-AF_NETGRAPH = 32
-AF_MAX = 37
-SOCK_MAXADDRLEN = 255
-_SS_MAXSIZE = 128
-PF_UNSPEC = AF_UNSPEC
-PF_LOCAL = AF_LOCAL
-PF_UNIX = PF_LOCAL
-PF_INET = AF_INET
-PF_IMPLINK = AF_IMPLINK
-PF_PUP = AF_PUP
-PF_CHAOS = AF_CHAOS
-PF_NS = AF_NS
-PF_ISO = AF_ISO
-PF_OSI = AF_ISO
-PF_ECMA = AF_ECMA
-PF_DATAKIT = AF_DATAKIT
-PF_CCITT = AF_CCITT
-PF_SNA = AF_SNA
-PF_DECnet = AF_DECnet
-PF_DLI = AF_DLI
-PF_LAT = AF_LAT
-PF_HYLINK = AF_HYLINK
-PF_APPLETALK = AF_APPLETALK
-PF_ROUTE = AF_ROUTE
-PF_LINK = AF_LINK
-PF_XTP = pseudo_AF_XTP
-PF_COIP = AF_COIP
-PF_CNT = AF_CNT
-PF_SIP = AF_SIP
-PF_IPX = AF_IPX
-PF_RTIP = pseudo_AF_RTIP
-PF_PIP = pseudo_AF_PIP
-PF_NDRV = AF_NDRV
-PF_ISDN = AF_ISDN
-PF_KEY = pseudo_AF_KEY
-PF_INET6 = AF_INET6
-PF_NATM = AF_NATM
-PF_SYSTEM = AF_SYSTEM
-PF_NETBIOS = AF_NETBIOS
-PF_PPP = AF_PPP
-PF_RESERVED_36 = AF_RESERVED_36
-PF_ATM = AF_ATM
-PF_NETGRAPH = AF_NETGRAPH
-PF_MAX = AF_MAX
-NET_MAXID = AF_MAX
-NET_RT_DUMP = 1
-NET_RT_FLAGS = 2
-NET_RT_IFLIST = 3
-NET_RT_STAT = 4
-NET_RT_TRASH = 5
-NET_RT_IFLIST2 = 6
-NET_RT_DUMP2 = 7
-NET_RT_MAXID = 8
-SOMAXCONN = 128
-MSG_OOB = 0x1
-MSG_PEEK = 0x2
-MSG_DONTROUTE = 0x4
-MSG_EOR = 0x8
-MSG_TRUNC = 0x10
-MSG_CTRUNC = 0x20
-MSG_WAITALL = 0x40
-MSG_DONTWAIT = 0x80
-MSG_EOF = 0x100
-MSG_WAITSTREAM = 0x200
-MSG_FLUSH = 0x400
-MSG_HOLD = 0x800
-MSG_SEND = 0x1000
-MSG_HAVEMORE = 0x2000
-MSG_RCVMORE = 0x4000
-MSG_NEEDSA = 0x10000
-CMGROUP_MAX = 16
-SCM_RIGHTS = 0x01
-SCM_TIMESTAMP = 0x02
-SCM_CREDS = 0x03
-SHUT_RD = 0
-SHUT_WR = 1
-SHUT_RDWR = 2
-
-# Included from machine/endian.h
-
-# Included from sys/_endian.h
-def ntohl(x): return (x)
-
-def ntohs(x): return (x)
-
-def htonl(x): return (x)
-
-def htons(x): return (x)
-
-def NTOHL(x): return (x)
-
-def NTOHS(x): return (x)
-
-def HTONL(x): return (x)
-
-def HTONS(x): return (x)
-
-
-# Included from libkern/_OSByteOrder.h
-def __DARWIN_OSSwapConstInt16(x): return \
-
-def __DARWIN_OSSwapConstInt32(x): return \
-
-def __DARWIN_OSSwapConstInt64(x): return \
-
-
-# Included from libkern/i386/_OSByteOrder.h
-def __DARWIN_OSSwapInt16(x): return \
-
-def __DARWIN_OSSwapInt32(x): return \
-
-def __DARWIN_OSSwapInt64(x): return \
-
-def __DARWIN_OSSwapInt16(x): return _OSSwapInt16(x)
-
-def __DARWIN_OSSwapInt32(x): return _OSSwapInt32(x)
-
-def __DARWIN_OSSwapInt64(x): return _OSSwapInt64(x)
-
-def ntohs(x): return __DARWIN_OSSwapInt16(x)
-
-def htons(x): return __DARWIN_OSSwapInt16(x)
-
-def ntohl(x): return __DARWIN_OSSwapInt32(x)
-
-def htonl(x): return __DARWIN_OSSwapInt32(x)
-
-IPPROTO_IP = 0
-IPPROTO_HOPOPTS = 0
-IPPROTO_ICMP = 1
-IPPROTO_IGMP = 2
-IPPROTO_GGP = 3
-IPPROTO_IPV4 = 4
-IPPROTO_IPIP = IPPROTO_IPV4
-IPPROTO_TCP = 6
-IPPROTO_ST = 7
-IPPROTO_EGP = 8
-IPPROTO_PIGP = 9
-IPPROTO_RCCMON = 10
-IPPROTO_NVPII = 11
-IPPROTO_PUP = 12
-IPPROTO_ARGUS = 13
-IPPROTO_EMCON = 14
-IPPROTO_XNET = 15
-IPPROTO_CHAOS = 16
-IPPROTO_UDP = 17
-IPPROTO_MUX = 18
-IPPROTO_MEAS = 19
-IPPROTO_HMP = 20
-IPPROTO_PRM = 21
-IPPROTO_IDP = 22
-IPPROTO_TRUNK1 = 23
-IPPROTO_TRUNK2 = 24
-IPPROTO_LEAF1 = 25
-IPPROTO_LEAF2 = 26
-IPPROTO_RDP = 27
-IPPROTO_IRTP = 28
-IPPROTO_TP = 29
-IPPROTO_BLT = 30
-IPPROTO_NSP = 31
-IPPROTO_INP = 32
-IPPROTO_SEP = 33
-IPPROTO_3PC = 34
-IPPROTO_IDPR = 35
-IPPROTO_XTP = 36
-IPPROTO_DDP = 37
-IPPROTO_CMTP = 38
-IPPROTO_TPXX = 39
-IPPROTO_IL = 40
-IPPROTO_IPV6 = 41
-IPPROTO_SDRP = 42
-IPPROTO_ROUTING = 43
-IPPROTO_FRAGMENT = 44
-IPPROTO_IDRP = 45
-IPPROTO_RSVP = 46
-IPPROTO_GRE = 47
-IPPROTO_MHRP = 48
-IPPROTO_BHA = 49
-IPPROTO_ESP = 50
-IPPROTO_AH = 51
-IPPROTO_INLSP = 52
-IPPROTO_SWIPE = 53
-IPPROTO_NHRP = 54
-IPPROTO_ICMPV6 = 58
-IPPROTO_NONE = 59
-IPPROTO_DSTOPTS = 60
-IPPROTO_AHIP = 61
-IPPROTO_CFTP = 62
-IPPROTO_HELLO = 63
-IPPROTO_SATEXPAK = 64
-IPPROTO_KRYPTOLAN = 65
-IPPROTO_RVD = 66
-IPPROTO_IPPC = 67
-IPPROTO_ADFS = 68
-IPPROTO_SATMON = 69
-IPPROTO_VISA = 70
-IPPROTO_IPCV = 71
-IPPROTO_CPNX = 72
-IPPROTO_CPHB = 73
-IPPROTO_WSN = 74
-IPPROTO_PVP = 75
-IPPROTO_BRSATMON = 76
-IPPROTO_ND = 77
-IPPROTO_WBMON = 78
-IPPROTO_WBEXPAK = 79
-IPPROTO_EON = 80
-IPPROTO_VMTP = 81
-IPPROTO_SVMTP = 82
-IPPROTO_VINES = 83
-IPPROTO_TTP = 84
-IPPROTO_IGP = 85
-IPPROTO_DGP = 86
-IPPROTO_TCF = 87
-IPPROTO_IGRP = 88
-IPPROTO_OSPFIGP = 89
-IPPROTO_SRPC = 90
-IPPROTO_LARP = 91
-IPPROTO_MTP = 92
-IPPROTO_AX25 = 93
-IPPROTO_IPEIP = 94
-IPPROTO_MICP = 95
-IPPROTO_SCCSP = 96
-IPPROTO_ETHERIP = 97
-IPPROTO_ENCAP = 98
-IPPROTO_APES = 99
-IPPROTO_GMTP = 100
-IPPROTO_IPCOMP = 108
-IPPROTO_PIM = 103
-IPPROTO_PGM = 113
-IPPROTO_DIVERT = 254
-IPPROTO_RAW = 255
-IPPROTO_MAX = 256
-IPPROTO_DONE = 257
-__DARWIN_IPPORT_RESERVED = 1024
-IPPORT_RESERVED = __DARWIN_IPPORT_RESERVED
-IPPORT_USERRESERVED = 5000
-IPPORT_HIFIRSTAUTO = 49152
-IPPORT_HILASTAUTO = 65535
-IPPORT_RESERVEDSTART = 600
-def IN_CLASSA(i): return (((u_int32_t)(i) & (-2147483648)) == 0)
-
-IN_CLASSA_NET = (-16777216)
-IN_CLASSA_NSHIFT = 24
-IN_CLASSA_HOST = 0x00ffffff
-IN_CLASSA_MAX = 128
-def IN_CLASSB(i): return (((u_int32_t)(i) & (-1073741824)) == (-2147483648))
-
-IN_CLASSB_NET = (-65536)
-IN_CLASSB_NSHIFT = 16
-IN_CLASSB_HOST = 0x0000ffff
-IN_CLASSB_MAX = 65536
-def IN_CLASSC(i): return (((u_int32_t)(i) & (-536870912)) == (-1073741824))
-
-IN_CLASSC_NET = (-256)
-IN_CLASSC_NSHIFT = 8
-IN_CLASSC_HOST = 0x000000ff
-def IN_CLASSD(i): return (((u_int32_t)(i) & (-268435456)) == (-536870912))
-
-IN_CLASSD_NET = (-268435456)
-IN_CLASSD_NSHIFT = 28
-IN_CLASSD_HOST = 0x0fffffff
-def IN_MULTICAST(i): return IN_CLASSD(i)
-
-def IN_EXPERIMENTAL(i): return (((u_int32_t)(i) & (-268435456)) == (-268435456))
-
-def IN_BADCLASS(i): return (((u_int32_t)(i) & (-268435456)) == (-268435456))
-
-INADDR_NONE = (-1)
-def IN_LINKLOCAL(i): return (((u_int32_t)(i) & IN_CLASSB_NET) == IN_LINKLOCALNETNUM)
-
-IN_LOOPBACKNET = 127
-INET_ADDRSTRLEN = 16
-IP_OPTIONS = 1
-IP_HDRINCL = 2
-IP_TOS = 3
-IP_TTL = 4
-IP_RECVOPTS = 5
-IP_RECVRETOPTS = 6
-IP_RECVDSTADDR = 7
-IP_RETOPTS = 8
-IP_MULTICAST_IF = 9
-IP_MULTICAST_TTL = 10
-IP_MULTICAST_LOOP = 11
-IP_ADD_MEMBERSHIP = 12
-IP_DROP_MEMBERSHIP = 13
-IP_MULTICAST_VIF = 14
-IP_RSVP_ON = 15
-IP_RSVP_OFF = 16
-IP_RSVP_VIF_ON = 17
-IP_RSVP_VIF_OFF = 18
-IP_PORTRANGE = 19
-IP_RECVIF = 20
-IP_IPSEC_POLICY = 21
-IP_FAITH = 22
-IP_STRIPHDR = 23
-IP_RECVTTL = 24
-IP_FW_ADD = 40
-IP_FW_DEL = 41
-IP_FW_FLUSH = 42
-IP_FW_ZERO = 43
-IP_FW_GET = 44
-IP_FW_RESETLOG = 45
-IP_OLD_FW_ADD = 50
-IP_OLD_FW_DEL = 51
-IP_OLD_FW_FLUSH = 52
-IP_OLD_FW_ZERO = 53
-IP_OLD_FW_GET = 54
-IP_NAT__XXX = 55
-IP_OLD_FW_RESETLOG = 56
-IP_DUMMYNET_CONFIGURE = 60
-IP_DUMMYNET_DEL = 61
-IP_DUMMYNET_FLUSH = 62
-IP_DUMMYNET_GET = 64
-IP_TRAFFIC_MGT_BACKGROUND = 65
-IP_FORCE_OUT_IFP = 69
-TRAFFIC_MGT_SO_BACKGROUND = 0x0001
-TRAFFIC_MGT_SO_BG_SUPPRESSED = 0x0002
-IP_DEFAULT_MULTICAST_TTL = 1
-IP_DEFAULT_MULTICAST_LOOP = 1
-IP_MAX_MEMBERSHIPS = 20
-IP_PORTRANGE_DEFAULT = 0
-IP_PORTRANGE_HIGH = 1
-IP_PORTRANGE_LOW = 2
-IPPROTO_MAXID = (IPPROTO_AH + 1)
-IPCTL_FORWARDING = 1
-IPCTL_SENDREDIRECTS = 2
-IPCTL_DEFTTL = 3
-IPCTL_DEFMTU = 4
-IPCTL_RTEXPIRE = 5
-IPCTL_RTMINEXPIRE = 6
-IPCTL_RTMAXCACHE = 7
-IPCTL_SOURCEROUTE = 8
-IPCTL_DIRECTEDBROADCAST = 9
-IPCTL_INTRQMAXLEN = 10
-IPCTL_INTRQDROPS = 11
-IPCTL_STATS = 12
-IPCTL_ACCEPTSOURCEROUTE = 13
-IPCTL_FASTFORWARDING = 14
-IPCTL_KEEPFAITH = 15
-IPCTL_GIF_TTL = 16
-IPCTL_MAXID = 17
-
-# Included from netinet6/in6.h
-__KAME_VERSION = "20010528/apple-darwin"
-IPV6PORT_RESERVED = 1024
-IPV6PORT_ANONMIN = 49152
-IPV6PORT_ANONMAX = 65535
-IPV6PORT_RESERVEDMIN = 600
-IPV6PORT_RESERVEDMAX = (IPV6PORT_RESERVED-1)
-INET6_ADDRSTRLEN = 46
-def IN6_IS_ADDR_UNSPECIFIED(a): return \
-
-def IN6_IS_ADDR_LOOPBACK(a): return \
-
-def IN6_IS_ADDR_V4COMPAT(a): return \
-
-def IN6_IS_ADDR_V4MAPPED(a): return \
-
-__IPV6_ADDR_SCOPE_NODELOCAL = 0x01
-__IPV6_ADDR_SCOPE_LINKLOCAL = 0x02
-__IPV6_ADDR_SCOPE_SITELOCAL = 0x05
-__IPV6_ADDR_SCOPE_ORGLOCAL = 0x08
-__IPV6_ADDR_SCOPE_GLOBAL = 0x0e
-def IN6_IS_ADDR_LINKLOCAL(a): return \
-
-def IN6_IS_ADDR_SITELOCAL(a): return \
-
-def IN6_IS_ADDR_MC_NODELOCAL(a): return \
-
-def IN6_IS_ADDR_MC_LINKLOCAL(a): return \
-
-def IN6_IS_ADDR_MC_SITELOCAL(a): return \
-
-def IN6_IS_ADDR_MC_ORGLOCAL(a): return \
-
-def IN6_IS_ADDR_MC_GLOBAL(a): return \
-
-IPV6_OPTIONS = 1
-IPV6_RECVOPTS = 5
-IPV6_RECVRETOPTS = 6
-IPV6_RECVDSTADDR = 7
-IPV6_RETOPTS = 8
-IPV6_SOCKOPT_RESERVED1 = 3
-IPV6_UNICAST_HOPS = 4
-IPV6_MULTICAST_IF = 9
-IPV6_MULTICAST_HOPS = 10
-IPV6_MULTICAST_LOOP = 11
-IPV6_JOIN_GROUP = 12
-IPV6_LEAVE_GROUP = 13
-IPV6_PORTRANGE = 14
-ICMP6_FILTER = 18
-IPV6_PKTINFO = 19
-IPV6_HOPLIMIT = 20
-IPV6_NEXTHOP = 21
-IPV6_HOPOPTS = 22
-IPV6_DSTOPTS = 23
-IPV6_RTHDR = 24
-IPV6_PKTOPTIONS = 25
-IPV6_CHECKSUM = 26
-IPV6_V6ONLY = 27
-IPV6_BINDV6ONLY = IPV6_V6ONLY
-IPV6_IPSEC_POLICY = 28
-IPV6_FAITH = 29
-IPV6_FW_ADD = 30
-IPV6_FW_DEL = 31
-IPV6_FW_FLUSH = 32
-IPV6_FW_ZERO = 33
-IPV6_FW_GET = 34
-IPV6_RTHDR_LOOSE = 0
-IPV6_RTHDR_STRICT = 1
-IPV6_RTHDR_TYPE_0 = 0
-IPV6_DEFAULT_MULTICAST_HOPS = 1
-IPV6_DEFAULT_MULTICAST_LOOP = 1
-IPV6_PORTRANGE_DEFAULT = 0
-IPV6_PORTRANGE_HIGH = 1
-IPV6_PORTRANGE_LOW = 2
-IPV6PROTO_MAXID = (IPPROTO_PIM + 1)
-IPV6CTL_FORWARDING = 1
-IPV6CTL_SENDREDIRECTS = 2
-IPV6CTL_DEFHLIM = 3
-IPV6CTL_DEFMTU = 4
-IPV6CTL_FORWSRCRT = 5
-IPV6CTL_STATS = 6
-IPV6CTL_MRTSTATS = 7
-IPV6CTL_MRTPROTO = 8
-IPV6CTL_MAXFRAGPACKETS = 9
-IPV6CTL_SOURCECHECK = 10
-IPV6CTL_SOURCECHECK_LOGINT = 11
-IPV6CTL_ACCEPT_RTADV = 12
-IPV6CTL_KEEPFAITH = 13
-IPV6CTL_LOG_INTERVAL = 14
-IPV6CTL_HDRNESTLIMIT = 15
-IPV6CTL_DAD_COUNT = 16
-IPV6CTL_AUTO_FLOWLABEL = 17
-IPV6CTL_DEFMCASTHLIM = 18
-IPV6CTL_GIF_HLIM = 19
-IPV6CTL_KAME_VERSION = 20
-IPV6CTL_USE_DEPRECATED = 21
-IPV6CTL_RR_PRUNE = 22
-IPV6CTL_MAPPED_ADDR = 23
-IPV6CTL_V6ONLY = 24
-IPV6CTL_RTEXPIRE = 25
-IPV6CTL_RTMINEXPIRE = 26
-IPV6CTL_RTMAXCACHE = 27
-IPV6CTL_USETEMPADDR = 32
-IPV6CTL_TEMPPLTIME = 33
-IPV6CTL_TEMPVLTIME = 34
-IPV6CTL_AUTO_LINKLOCAL = 35
-IPV6CTL_RIP6STATS = 36
-IPV6CTL_MAXFRAGS = 41
-IPV6CTL_MAXID = 42
diff --git a/Lib/plat-darwin/regen b/Lib/plat-darwin/regen
deleted file mode 100755
index a20cdc1..0000000
--- a/Lib/plat-darwin/regen
+++ /dev/null
@@ -1,3 +0,0 @@
-#! /bin/sh
-set -v
-python$EXE ../../Tools/scripts/h2py.py -i '(u_long)' /usr/include/netinet/in.h
diff --git a/Lib/plat-freebsd4/IN.py b/Lib/plat-freebsd4/IN.py
deleted file mode 100644
index bca2418..0000000
--- a/Lib/plat-freebsd4/IN.py
+++ /dev/null
@@ -1,355 +0,0 @@
-# Generated by h2py from /usr/include/netinet/in.h
-IPPROTO_IP = 0
-IPPROTO_HOPOPTS = 0
-IPPROTO_ICMP = 1
-IPPROTO_IGMP = 2
-IPPROTO_GGP = 3
-IPPROTO_IPV4 = 4
-IPPROTO_IPIP = IPPROTO_IPV4
-IPPROTO_TCP = 6
-IPPROTO_ST = 7
-IPPROTO_EGP = 8
-IPPROTO_PIGP = 9
-IPPROTO_RCCMON = 10
-IPPROTO_NVPII = 11
-IPPROTO_PUP = 12
-IPPROTO_ARGUS = 13
-IPPROTO_EMCON = 14
-IPPROTO_XNET = 15
-IPPROTO_CHAOS = 16
-IPPROTO_UDP = 17
-IPPROTO_MUX = 18
-IPPROTO_MEAS = 19
-IPPROTO_HMP = 20
-IPPROTO_PRM = 21
-IPPROTO_IDP = 22
-IPPROTO_TRUNK1 = 23
-IPPROTO_TRUNK2 = 24
-IPPROTO_LEAF1 = 25
-IPPROTO_LEAF2 = 26
-IPPROTO_RDP = 27
-IPPROTO_IRTP = 28
-IPPROTO_TP = 29
-IPPROTO_BLT = 30
-IPPROTO_NSP = 31
-IPPROTO_INP = 32
-IPPROTO_SEP = 33
-IPPROTO_3PC = 34
-IPPROTO_IDPR = 35
-IPPROTO_XTP = 36
-IPPROTO_DDP = 37
-IPPROTO_CMTP = 38
-IPPROTO_TPXX = 39
-IPPROTO_IL = 40
-IPPROTO_IPV6 = 41
-IPPROTO_SDRP = 42
-IPPROTO_ROUTING = 43
-IPPROTO_FRAGMENT = 44
-IPPROTO_IDRP = 45
-IPPROTO_RSVP = 46
-IPPROTO_GRE = 47
-IPPROTO_MHRP = 48
-IPPROTO_BHA = 49
-IPPROTO_ESP = 50
-IPPROTO_AH = 51
-IPPROTO_INLSP = 52
-IPPROTO_SWIPE = 53
-IPPROTO_NHRP = 54
-IPPROTO_ICMPV6 = 58
-IPPROTO_NONE = 59
-IPPROTO_DSTOPTS = 60
-IPPROTO_AHIP = 61
-IPPROTO_CFTP = 62
-IPPROTO_HELLO = 63
-IPPROTO_SATEXPAK = 64
-IPPROTO_KRYPTOLAN = 65
-IPPROTO_RVD = 66
-IPPROTO_IPPC = 67
-IPPROTO_ADFS = 68
-IPPROTO_SATMON = 69
-IPPROTO_VISA = 70
-IPPROTO_IPCV = 71
-IPPROTO_CPNX = 72
-IPPROTO_CPHB = 73
-IPPROTO_WSN = 74
-IPPROTO_PVP = 75
-IPPROTO_BRSATMON = 76
-IPPROTO_ND = 77
-IPPROTO_WBMON = 78
-IPPROTO_WBEXPAK = 79
-IPPROTO_EON = 80
-IPPROTO_VMTP = 81
-IPPROTO_SVMTP = 82
-IPPROTO_VINES = 83
-IPPROTO_TTP = 84
-IPPROTO_IGP = 85
-IPPROTO_DGP = 86
-IPPROTO_TCF = 87
-IPPROTO_IGRP = 88
-IPPROTO_OSPFIGP = 89
-IPPROTO_SRPC = 90
-IPPROTO_LARP = 91
-IPPROTO_MTP = 92
-IPPROTO_AX25 = 93
-IPPROTO_IPEIP = 94
-IPPROTO_MICP = 95
-IPPROTO_SCCSP = 96
-IPPROTO_ETHERIP = 97
-IPPROTO_ENCAP = 98
-IPPROTO_APES = 99
-IPPROTO_GMTP = 100
-IPPROTO_IPCOMP = 108
-IPPROTO_PIM = 103
-IPPROTO_PGM = 113
-IPPROTO_DIVERT = 254
-IPPROTO_RAW = 255
-IPPROTO_MAX = 256
-IPPROTO_DONE = 257
-IPPORT_RESERVED = 1024
-IPPORT_USERRESERVED = 5000
-IPPORT_HIFIRSTAUTO = 49152
-IPPORT_HILASTAUTO = 65535
-IPPORT_RESERVEDSTART = 600
-def IN_CLASSA(i): return (((u_int32_t)(i) & 0x80000000) == 0)
-
-IN_CLASSA_NET = 0xff000000
-IN_CLASSA_NSHIFT = 24
-IN_CLASSA_HOST = 0x00ffffff
-IN_CLASSA_MAX = 128
-def IN_CLASSB(i): return (((u_int32_t)(i) & 0xc0000000) == 0x80000000)
-
-IN_CLASSB_NET = 0xffff0000
-IN_CLASSB_NSHIFT = 16
-IN_CLASSB_HOST = 0x0000ffff
-IN_CLASSB_MAX = 65536
-def IN_CLASSC(i): return (((u_int32_t)(i) & 0xe0000000) == 0xc0000000)
-
-IN_CLASSC_NET = 0xffffff00
-IN_CLASSC_NSHIFT = 8
-IN_CLASSC_HOST = 0x000000ff
-def IN_CLASSD(i): return (((u_int32_t)(i) & 0xf0000000) == 0xe0000000)
-
-IN_CLASSD_NET = 0xf0000000
-IN_CLASSD_NSHIFT = 28
-IN_CLASSD_HOST = 0x0fffffff
-def IN_MULTICAST(i): return IN_CLASSD(i)
-
-def IN_EXPERIMENTAL(i): return (((u_int32_t)(i) & 0xf0000000) == 0xf0000000)
-
-def IN_BADCLASS(i): return (((u_int32_t)(i) & 0xf0000000) == 0xf0000000)
-
-INADDR_NONE = 0xffffffff
-IN_LOOPBACKNET = 127
-INET_ADDRSTRLEN = 16
-IP_OPTIONS = 1
-IP_HDRINCL = 2
-IP_TOS = 3
-IP_TTL = 4
-IP_RECVOPTS = 5
-IP_RECVRETOPTS = 6
-IP_RECVDSTADDR = 7
-IP_RETOPTS = 8
-IP_MULTICAST_IF = 9
-IP_MULTICAST_TTL = 10
-IP_MULTICAST_LOOP = 11
-IP_ADD_MEMBERSHIP = 12
-IP_DROP_MEMBERSHIP = 13
-IP_MULTICAST_VIF = 14
-IP_RSVP_ON = 15
-IP_RSVP_OFF = 16
-IP_RSVP_VIF_ON = 17
-IP_RSVP_VIF_OFF = 18
-IP_PORTRANGE = 19
-IP_RECVIF = 20
-IP_IPSEC_POLICY = 21
-IP_FAITH = 22
-IP_FW_ADD = 50
-IP_FW_DEL = 51
-IP_FW_FLUSH = 52
-IP_FW_ZERO = 53
-IP_FW_GET = 54
-IP_FW_RESETLOG = 55
-IP_DUMMYNET_CONFIGURE = 60
-IP_DUMMYNET_DEL = 61
-IP_DUMMYNET_FLUSH = 62
-IP_DUMMYNET_GET = 64
-IP_DEFAULT_MULTICAST_TTL = 1
-IP_DEFAULT_MULTICAST_LOOP = 1
-IP_MAX_MEMBERSHIPS = 20
-IP_PORTRANGE_DEFAULT = 0
-IP_PORTRANGE_HIGH = 1
-IP_PORTRANGE_LOW = 2
-IPPROTO_MAXID = (IPPROTO_AH + 1)
-IPCTL_FORWARDING = 1
-IPCTL_SENDREDIRECTS = 2
-IPCTL_DEFTTL = 3
-IPCTL_DEFMTU = 4
-IPCTL_RTEXPIRE = 5
-IPCTL_RTMINEXPIRE = 6
-IPCTL_RTMAXCACHE = 7
-IPCTL_SOURCEROUTE = 8
-IPCTL_DIRECTEDBROADCAST = 9
-IPCTL_INTRQMAXLEN = 10
-IPCTL_INTRQDROPS = 11
-IPCTL_STATS = 12
-IPCTL_ACCEPTSOURCEROUTE = 13
-IPCTL_FASTFORWARDING = 14
-IPCTL_KEEPFAITH = 15
-IPCTL_GIF_TTL = 16
-IPCTL_MAXID = 17
-
-# Included from netinet6/in6.h
-
-# Included from sys/queue.h
-def SLIST_HEAD_INITIALIZER(head): return \
-
-def SLIST_ENTRY(type): return \
-
-def STAILQ_HEAD_INITIALIZER(head): return \
-
-def STAILQ_ENTRY(type): return \
-
-def LIST_HEAD_INITIALIZER(head): return \
-
-def LIST_ENTRY(type): return \
-
-def TAILQ_HEAD_INITIALIZER(head): return \
-
-def TAILQ_ENTRY(type): return \
-
-def CIRCLEQ_ENTRY(type): return \
-
-__KAME_VERSION = "20000701/FreeBSD-current"
-IPV6PORT_RESERVED = 1024
-IPV6PORT_ANONMIN = 49152
-IPV6PORT_ANONMAX = 65535
-IPV6PORT_RESERVEDMIN = 600
-IPV6PORT_RESERVEDMAX = (IPV6PORT_RESERVED-1)
-INET6_ADDRSTRLEN = 46
-IPV6_ADDR_INT32_ONE = 1
-IPV6_ADDR_INT32_TWO = 2
-IPV6_ADDR_INT32_MNL = 0xff010000
-IPV6_ADDR_INT32_MLL = 0xff020000
-IPV6_ADDR_INT32_SMP = 0x0000ffff
-IPV6_ADDR_INT16_ULL = 0xfe80
-IPV6_ADDR_INT16_USL = 0xfec0
-IPV6_ADDR_INT16_MLL = 0xff02
-IPV6_ADDR_INT32_ONE = 0x01000000
-IPV6_ADDR_INT32_TWO = 0x02000000
-IPV6_ADDR_INT32_MNL = 0x000001ff
-IPV6_ADDR_INT32_MLL = 0x000002ff
-IPV6_ADDR_INT32_SMP = 0xffff0000
-IPV6_ADDR_INT16_ULL = 0x80fe
-IPV6_ADDR_INT16_USL = 0xc0fe
-IPV6_ADDR_INT16_MLL = 0x02ff
-def IN6_IS_ADDR_UNSPECIFIED(a): return \
-
-def IN6_IS_ADDR_LOOPBACK(a): return \
-
-def IN6_IS_ADDR_V4COMPAT(a): return \
-
-def IN6_IS_ADDR_V4MAPPED(a): return \
-
-IPV6_ADDR_SCOPE_NODELOCAL = 0x01
-IPV6_ADDR_SCOPE_LINKLOCAL = 0x02
-IPV6_ADDR_SCOPE_SITELOCAL = 0x05
-IPV6_ADDR_SCOPE_ORGLOCAL = 0x08
-IPV6_ADDR_SCOPE_GLOBAL = 0x0e
-__IPV6_ADDR_SCOPE_NODELOCAL = 0x01
-__IPV6_ADDR_SCOPE_LINKLOCAL = 0x02
-__IPV6_ADDR_SCOPE_SITELOCAL = 0x05
-__IPV6_ADDR_SCOPE_ORGLOCAL = 0x08
-__IPV6_ADDR_SCOPE_GLOBAL = 0x0e
-def IN6_IS_ADDR_LINKLOCAL(a): return \
-
-def IN6_IS_ADDR_SITELOCAL(a): return \
-
-def IN6_IS_ADDR_MC_NODELOCAL(a): return \
-
-def IN6_IS_ADDR_MC_LINKLOCAL(a): return \
-
-def IN6_IS_ADDR_MC_SITELOCAL(a): return \
-
-def IN6_IS_ADDR_MC_ORGLOCAL(a): return \
-
-def IN6_IS_ADDR_MC_GLOBAL(a): return \
-
-def IN6_IS_ADDR_MC_NODELOCAL(a): return \
-
-def IN6_IS_ADDR_MC_LINKLOCAL(a): return \
-
-def IN6_IS_ADDR_MC_SITELOCAL(a): return \
-
-def IN6_IS_ADDR_MC_ORGLOCAL(a): return \
-
-def IN6_IS_ADDR_MC_GLOBAL(a): return \
-
-def IN6_IS_SCOPE_LINKLOCAL(a): return \
-
-IPV6_OPTIONS = 1
-IPV6_RECVOPTS = 5
-IPV6_RECVRETOPTS = 6
-IPV6_RECVDSTADDR = 7
-IPV6_RETOPTS = 8
-IPV6_SOCKOPT_RESERVED1 = 3
-IPV6_UNICAST_HOPS = 4
-IPV6_MULTICAST_IF = 9
-IPV6_MULTICAST_HOPS = 10
-IPV6_MULTICAST_LOOP = 11
-IPV6_JOIN_GROUP = 12
-IPV6_LEAVE_GROUP = 13
-IPV6_PORTRANGE = 14
-ICMP6_FILTER = 18
-IPV6_PKTINFO = 19
-IPV6_HOPLIMIT = 20
-IPV6_NEXTHOP = 21
-IPV6_HOPOPTS = 22
-IPV6_DSTOPTS = 23
-IPV6_RTHDR = 24
-IPV6_PKTOPTIONS = 25
-IPV6_CHECKSUM = 26
-IPV6_BINDV6ONLY = 27
-IPV6_IPSEC_POLICY = 28
-IPV6_FAITH = 29
-IPV6_FW_ADD = 30
-IPV6_FW_DEL = 31
-IPV6_FW_FLUSH = 32
-IPV6_FW_ZERO = 33
-IPV6_FW_GET = 34
-IPV6_RTHDR_LOOSE = 0
-IPV6_RTHDR_STRICT = 1
-IPV6_RTHDR_TYPE_0 = 0
-IPV6_DEFAULT_MULTICAST_HOPS = 1
-IPV6_DEFAULT_MULTICAST_LOOP = 1
-IPV6_PORTRANGE_DEFAULT = 0
-IPV6_PORTRANGE_HIGH = 1
-IPV6_PORTRANGE_LOW = 2
-IPV6PROTO_MAXID = (IPPROTO_PIM + 1)
-IPV6CTL_FORWARDING = 1
-IPV6CTL_SENDREDIRECTS = 2
-IPV6CTL_DEFHLIM = 3
-IPV6CTL_DEFMTU = 4
-IPV6CTL_FORWSRCRT = 5
-IPV6CTL_STATS = 6
-IPV6CTL_MRTSTATS = 7
-IPV6CTL_MRTPROTO = 8
-IPV6CTL_MAXFRAGPACKETS = 9
-IPV6CTL_SOURCECHECK = 10
-IPV6CTL_SOURCECHECK_LOGINT = 11
-IPV6CTL_ACCEPT_RTADV = 12
-IPV6CTL_KEEPFAITH = 13
-IPV6CTL_LOG_INTERVAL = 14
-IPV6CTL_HDRNESTLIMIT = 15
-IPV6CTL_DAD_COUNT = 16
-IPV6CTL_AUTO_FLOWLABEL = 17
-IPV6CTL_DEFMCASTHLIM = 18
-IPV6CTL_GIF_HLIM = 19
-IPV6CTL_KAME_VERSION = 20
-IPV6CTL_USE_DEPRECATED = 21
-IPV6CTL_RR_PRUNE = 22
-IPV6CTL_MAPPED_ADDR = 23
-IPV6CTL_BINDV6ONLY = 24
-IPV6CTL_RTEXPIRE = 25
-IPV6CTL_RTMINEXPIRE = 26
-IPV6CTL_RTMAXCACHE = 27
-IPV6CTL_MAXID = 28
diff --git a/Lib/plat-freebsd4/regen b/Lib/plat-freebsd4/regen
deleted file mode 100755
index 8aa6898..0000000
--- a/Lib/plat-freebsd4/regen
+++ /dev/null
@@ -1,3 +0,0 @@
-#! /bin/sh
-set -v
-python ../../Tools/scripts/h2py.py -i '(u_long)' /usr/include/netinet/in.h
diff --git a/Lib/plat-freebsd5/IN.py b/Lib/plat-freebsd5/IN.py
deleted file mode 100644
index bca2418..0000000
--- a/Lib/plat-freebsd5/IN.py
+++ /dev/null
@@ -1,355 +0,0 @@
-# Generated by h2py from /usr/include/netinet/in.h
-IPPROTO_IP = 0
-IPPROTO_HOPOPTS = 0
-IPPROTO_ICMP = 1
-IPPROTO_IGMP = 2
-IPPROTO_GGP = 3
-IPPROTO_IPV4 = 4
-IPPROTO_IPIP = IPPROTO_IPV4
-IPPROTO_TCP = 6
-IPPROTO_ST = 7
-IPPROTO_EGP = 8
-IPPROTO_PIGP = 9
-IPPROTO_RCCMON = 10
-IPPROTO_NVPII = 11
-IPPROTO_PUP = 12
-IPPROTO_ARGUS = 13
-IPPROTO_EMCON = 14
-IPPROTO_XNET = 15
-IPPROTO_CHAOS = 16
-IPPROTO_UDP = 17
-IPPROTO_MUX = 18
-IPPROTO_MEAS = 19
-IPPROTO_HMP = 20
-IPPROTO_PRM = 21
-IPPROTO_IDP = 22
-IPPROTO_TRUNK1 = 23
-IPPROTO_TRUNK2 = 24
-IPPROTO_LEAF1 = 25
-IPPROTO_LEAF2 = 26
-IPPROTO_RDP = 27
-IPPROTO_IRTP = 28
-IPPROTO_TP = 29
-IPPROTO_BLT = 30
-IPPROTO_NSP = 31
-IPPROTO_INP = 32
-IPPROTO_SEP = 33
-IPPROTO_3PC = 34
-IPPROTO_IDPR = 35
-IPPROTO_XTP = 36
-IPPROTO_DDP = 37
-IPPROTO_CMTP = 38
-IPPROTO_TPXX = 39
-IPPROTO_IL = 40
-IPPROTO_IPV6 = 41
-IPPROTO_SDRP = 42
-IPPROTO_ROUTING = 43
-IPPROTO_FRAGMENT = 44
-IPPROTO_IDRP = 45
-IPPROTO_RSVP = 46
-IPPROTO_GRE = 47
-IPPROTO_MHRP = 48
-IPPROTO_BHA = 49
-IPPROTO_ESP = 50
-IPPROTO_AH = 51
-IPPROTO_INLSP = 52
-IPPROTO_SWIPE = 53
-IPPROTO_NHRP = 54
-IPPROTO_ICMPV6 = 58
-IPPROTO_NONE = 59
-IPPROTO_DSTOPTS = 60
-IPPROTO_AHIP = 61
-IPPROTO_CFTP = 62
-IPPROTO_HELLO = 63
-IPPROTO_SATEXPAK = 64
-IPPROTO_KRYPTOLAN = 65
-IPPROTO_RVD = 66
-IPPROTO_IPPC = 67
-IPPROTO_ADFS = 68
-IPPROTO_SATMON = 69
-IPPROTO_VISA = 70
-IPPROTO_IPCV = 71
-IPPROTO_CPNX = 72
-IPPROTO_CPHB = 73
-IPPROTO_WSN = 74
-IPPROTO_PVP = 75
-IPPROTO_BRSATMON = 76
-IPPROTO_ND = 77
-IPPROTO_WBMON = 78
-IPPROTO_WBEXPAK = 79
-IPPROTO_EON = 80
-IPPROTO_VMTP = 81
-IPPROTO_SVMTP = 82
-IPPROTO_VINES = 83
-IPPROTO_TTP = 84
-IPPROTO_IGP = 85
-IPPROTO_DGP = 86
-IPPROTO_TCF = 87
-IPPROTO_IGRP = 88
-IPPROTO_OSPFIGP = 89
-IPPROTO_SRPC = 90
-IPPROTO_LARP = 91
-IPPROTO_MTP = 92
-IPPROTO_AX25 = 93
-IPPROTO_IPEIP = 94
-IPPROTO_MICP = 95
-IPPROTO_SCCSP = 96
-IPPROTO_ETHERIP = 97
-IPPROTO_ENCAP = 98
-IPPROTO_APES = 99
-IPPROTO_GMTP = 100
-IPPROTO_IPCOMP = 108
-IPPROTO_PIM = 103
-IPPROTO_PGM = 113
-IPPROTO_DIVERT = 254
-IPPROTO_RAW = 255
-IPPROTO_MAX = 256
-IPPROTO_DONE = 257
-IPPORT_RESERVED = 1024
-IPPORT_USERRESERVED = 5000
-IPPORT_HIFIRSTAUTO = 49152
-IPPORT_HILASTAUTO = 65535
-IPPORT_RESERVEDSTART = 600
-def IN_CLASSA(i): return (((u_int32_t)(i) & 0x80000000) == 0)
-
-IN_CLASSA_NET = 0xff000000
-IN_CLASSA_NSHIFT = 24
-IN_CLASSA_HOST = 0x00ffffff
-IN_CLASSA_MAX = 128
-def IN_CLASSB(i): return (((u_int32_t)(i) & 0xc0000000) == 0x80000000)
-
-IN_CLASSB_NET = 0xffff0000
-IN_CLASSB_NSHIFT = 16
-IN_CLASSB_HOST = 0x0000ffff
-IN_CLASSB_MAX = 65536
-def IN_CLASSC(i): return (((u_int32_t)(i) & 0xe0000000) == 0xc0000000)
-
-IN_CLASSC_NET = 0xffffff00
-IN_CLASSC_NSHIFT = 8
-IN_CLASSC_HOST = 0x000000ff
-def IN_CLASSD(i): return (((u_int32_t)(i) & 0xf0000000) == 0xe0000000)
-
-IN_CLASSD_NET = 0xf0000000
-IN_CLASSD_NSHIFT = 28
-IN_CLASSD_HOST = 0x0fffffff
-def IN_MULTICAST(i): return IN_CLASSD(i)
-
-def IN_EXPERIMENTAL(i): return (((u_int32_t)(i) & 0xf0000000) == 0xf0000000)
-
-def IN_BADCLASS(i): return (((u_int32_t)(i) & 0xf0000000) == 0xf0000000)
-
-INADDR_NONE = 0xffffffff
-IN_LOOPBACKNET = 127
-INET_ADDRSTRLEN = 16
-IP_OPTIONS = 1
-IP_HDRINCL = 2
-IP_TOS = 3
-IP_TTL = 4
-IP_RECVOPTS = 5
-IP_RECVRETOPTS = 6
-IP_RECVDSTADDR = 7
-IP_RETOPTS = 8
-IP_MULTICAST_IF = 9
-IP_MULTICAST_TTL = 10
-IP_MULTICAST_LOOP = 11
-IP_ADD_MEMBERSHIP = 12
-IP_DROP_MEMBERSHIP = 13
-IP_MULTICAST_VIF = 14
-IP_RSVP_ON = 15
-IP_RSVP_OFF = 16
-IP_RSVP_VIF_ON = 17
-IP_RSVP_VIF_OFF = 18
-IP_PORTRANGE = 19
-IP_RECVIF = 20
-IP_IPSEC_POLICY = 21
-IP_FAITH = 22
-IP_FW_ADD = 50
-IP_FW_DEL = 51
-IP_FW_FLUSH = 52
-IP_FW_ZERO = 53
-IP_FW_GET = 54
-IP_FW_RESETLOG = 55
-IP_DUMMYNET_CONFIGURE = 60
-IP_DUMMYNET_DEL = 61
-IP_DUMMYNET_FLUSH = 62
-IP_DUMMYNET_GET = 64
-IP_DEFAULT_MULTICAST_TTL = 1
-IP_DEFAULT_MULTICAST_LOOP = 1
-IP_MAX_MEMBERSHIPS = 20
-IP_PORTRANGE_DEFAULT = 0
-IP_PORTRANGE_HIGH = 1
-IP_PORTRANGE_LOW = 2
-IPPROTO_MAXID = (IPPROTO_AH + 1)
-IPCTL_FORWARDING = 1
-IPCTL_SENDREDIRECTS = 2
-IPCTL_DEFTTL = 3
-IPCTL_DEFMTU = 4
-IPCTL_RTEXPIRE = 5
-IPCTL_RTMINEXPIRE = 6
-IPCTL_RTMAXCACHE = 7
-IPCTL_SOURCEROUTE = 8
-IPCTL_DIRECTEDBROADCAST = 9
-IPCTL_INTRQMAXLEN = 10
-IPCTL_INTRQDROPS = 11
-IPCTL_STATS = 12
-IPCTL_ACCEPTSOURCEROUTE = 13
-IPCTL_FASTFORWARDING = 14
-IPCTL_KEEPFAITH = 15
-IPCTL_GIF_TTL = 16
-IPCTL_MAXID = 17
-
-# Included from netinet6/in6.h
-
-# Included from sys/queue.h
-def SLIST_HEAD_INITIALIZER(head): return \
-
-def SLIST_ENTRY(type): return \
-
-def STAILQ_HEAD_INITIALIZER(head): return \
-
-def STAILQ_ENTRY(type): return \
-
-def LIST_HEAD_INITIALIZER(head): return \
-
-def LIST_ENTRY(type): return \
-
-def TAILQ_HEAD_INITIALIZER(head): return \
-
-def TAILQ_ENTRY(type): return \
-
-def CIRCLEQ_ENTRY(type): return \
-
-__KAME_VERSION = "20000701/FreeBSD-current"
-IPV6PORT_RESERVED = 1024
-IPV6PORT_ANONMIN = 49152
-IPV6PORT_ANONMAX = 65535
-IPV6PORT_RESERVEDMIN = 600
-IPV6PORT_RESERVEDMAX = (IPV6PORT_RESERVED-1)
-INET6_ADDRSTRLEN = 46
-IPV6_ADDR_INT32_ONE = 1
-IPV6_ADDR_INT32_TWO = 2
-IPV6_ADDR_INT32_MNL = 0xff010000
-IPV6_ADDR_INT32_MLL = 0xff020000
-IPV6_ADDR_INT32_SMP = 0x0000ffff
-IPV6_ADDR_INT16_ULL = 0xfe80
-IPV6_ADDR_INT16_USL = 0xfec0
-IPV6_ADDR_INT16_MLL = 0xff02
-IPV6_ADDR_INT32_ONE = 0x01000000
-IPV6_ADDR_INT32_TWO = 0x02000000
-IPV6_ADDR_INT32_MNL = 0x000001ff
-IPV6_ADDR_INT32_MLL = 0x000002ff
-IPV6_ADDR_INT32_SMP = 0xffff0000
-IPV6_ADDR_INT16_ULL = 0x80fe
-IPV6_ADDR_INT16_USL = 0xc0fe
-IPV6_ADDR_INT16_MLL = 0x02ff
-def IN6_IS_ADDR_UNSPECIFIED(a): return \
-
-def IN6_IS_ADDR_LOOPBACK(a): return \
-
-def IN6_IS_ADDR_V4COMPAT(a): return \
-
-def IN6_IS_ADDR_V4MAPPED(a): return \
-
-IPV6_ADDR_SCOPE_NODELOCAL = 0x01
-IPV6_ADDR_SCOPE_LINKLOCAL = 0x02
-IPV6_ADDR_SCOPE_SITELOCAL = 0x05
-IPV6_ADDR_SCOPE_ORGLOCAL = 0x08
-IPV6_ADDR_SCOPE_GLOBAL = 0x0e
-__IPV6_ADDR_SCOPE_NODELOCAL = 0x01
-__IPV6_ADDR_SCOPE_LINKLOCAL = 0x02
-__IPV6_ADDR_SCOPE_SITELOCAL = 0x05
-__IPV6_ADDR_SCOPE_ORGLOCAL = 0x08
-__IPV6_ADDR_SCOPE_GLOBAL = 0x0e
-def IN6_IS_ADDR_LINKLOCAL(a): return \
-
-def IN6_IS_ADDR_SITELOCAL(a): return \
-
-def IN6_IS_ADDR_MC_NODELOCAL(a): return \
-
-def IN6_IS_ADDR_MC_LINKLOCAL(a): return \
-
-def IN6_IS_ADDR_MC_SITELOCAL(a): return \
-
-def IN6_IS_ADDR_MC_ORGLOCAL(a): return \
-
-def IN6_IS_ADDR_MC_GLOBAL(a): return \
-
-def IN6_IS_ADDR_MC_NODELOCAL(a): return \
-
-def IN6_IS_ADDR_MC_LINKLOCAL(a): return \
-
-def IN6_IS_ADDR_MC_SITELOCAL(a): return \
-
-def IN6_IS_ADDR_MC_ORGLOCAL(a): return \
-
-def IN6_IS_ADDR_MC_GLOBAL(a): return \
-
-def IN6_IS_SCOPE_LINKLOCAL(a): return \
-
-IPV6_OPTIONS = 1
-IPV6_RECVOPTS = 5
-IPV6_RECVRETOPTS = 6
-IPV6_RECVDSTADDR = 7
-IPV6_RETOPTS = 8
-IPV6_SOCKOPT_RESERVED1 = 3
-IPV6_UNICAST_HOPS = 4
-IPV6_MULTICAST_IF = 9
-IPV6_MULTICAST_HOPS = 10
-IPV6_MULTICAST_LOOP = 11
-IPV6_JOIN_GROUP = 12
-IPV6_LEAVE_GROUP = 13
-IPV6_PORTRANGE = 14
-ICMP6_FILTER = 18
-IPV6_PKTINFO = 19
-IPV6_HOPLIMIT = 20
-IPV6_NEXTHOP = 21
-IPV6_HOPOPTS = 22
-IPV6_DSTOPTS = 23
-IPV6_RTHDR = 24
-IPV6_PKTOPTIONS = 25
-IPV6_CHECKSUM = 26
-IPV6_BINDV6ONLY = 27
-IPV6_IPSEC_POLICY = 28
-IPV6_FAITH = 29
-IPV6_FW_ADD = 30
-IPV6_FW_DEL = 31
-IPV6_FW_FLUSH = 32
-IPV6_FW_ZERO = 33
-IPV6_FW_GET = 34
-IPV6_RTHDR_LOOSE = 0
-IPV6_RTHDR_STRICT = 1
-IPV6_RTHDR_TYPE_0 = 0
-IPV6_DEFAULT_MULTICAST_HOPS = 1
-IPV6_DEFAULT_MULTICAST_LOOP = 1
-IPV6_PORTRANGE_DEFAULT = 0
-IPV6_PORTRANGE_HIGH = 1
-IPV6_PORTRANGE_LOW = 2
-IPV6PROTO_MAXID = (IPPROTO_PIM + 1)
-IPV6CTL_FORWARDING = 1
-IPV6CTL_SENDREDIRECTS = 2
-IPV6CTL_DEFHLIM = 3
-IPV6CTL_DEFMTU = 4
-IPV6CTL_FORWSRCRT = 5
-IPV6CTL_STATS = 6
-IPV6CTL_MRTSTATS = 7
-IPV6CTL_MRTPROTO = 8
-IPV6CTL_MAXFRAGPACKETS = 9
-IPV6CTL_SOURCECHECK = 10
-IPV6CTL_SOURCECHECK_LOGINT = 11
-IPV6CTL_ACCEPT_RTADV = 12
-IPV6CTL_KEEPFAITH = 13
-IPV6CTL_LOG_INTERVAL = 14
-IPV6CTL_HDRNESTLIMIT = 15
-IPV6CTL_DAD_COUNT = 16
-IPV6CTL_AUTO_FLOWLABEL = 17
-IPV6CTL_DEFMCASTHLIM = 18
-IPV6CTL_GIF_HLIM = 19
-IPV6CTL_KAME_VERSION = 20
-IPV6CTL_USE_DEPRECATED = 21
-IPV6CTL_RR_PRUNE = 22
-IPV6CTL_MAPPED_ADDR = 23
-IPV6CTL_BINDV6ONLY = 24
-IPV6CTL_RTEXPIRE = 25
-IPV6CTL_RTMINEXPIRE = 26
-IPV6CTL_RTMAXCACHE = 27
-IPV6CTL_MAXID = 28
diff --git a/Lib/plat-freebsd5/regen b/Lib/plat-freebsd5/regen
deleted file mode 100755
index 8aa6898..0000000
--- a/Lib/plat-freebsd5/regen
+++ /dev/null
@@ -1,3 +0,0 @@
-#! /bin/sh
-set -v
-python ../../Tools/scripts/h2py.py -i '(u_long)' /usr/include/netinet/in.h
diff --git a/Lib/plat-freebsd6/IN.py b/Lib/plat-freebsd6/IN.py
deleted file mode 100644
index 560bf84..0000000
--- a/Lib/plat-freebsd6/IN.py
+++ /dev/null
@@ -1,551 +0,0 @@
-# Generated by h2py from /usr/include/netinet/in.h
-
-# Included from sys/cdefs.h
-__GNUCLIKE_ASM = 3
-__GNUCLIKE_ASM = 2
-__GNUCLIKE___TYPEOF = 1
-__GNUCLIKE___OFFSETOF = 1
-__GNUCLIKE___SECTION = 1
-__GNUCLIKE_ATTRIBUTE_MODE_DI = 1
-__GNUCLIKE_CTOR_SECTION_HANDLING = 1
-__GNUCLIKE_BUILTIN_CONSTANT_P = 1
-__GNUCLIKE_BUILTIN_VARARGS = 1
-__GNUCLIKE_BUILTIN_STDARG = 1
-__GNUCLIKE_BUILTIN_VAALIST = 1
-__GNUC_VA_LIST_COMPATIBILITY = 1
-__GNUCLIKE_BUILTIN_NEXT_ARG = 1
-__GNUCLIKE_BUILTIN_MEMCPY = 1
-__CC_SUPPORTS_INLINE = 1
-__CC_SUPPORTS___INLINE = 1
-__CC_SUPPORTS___INLINE__ = 1
-__CC_SUPPORTS___FUNC__ = 1
-__CC_SUPPORTS_WARNING = 1
-__CC_SUPPORTS_VARADIC_XXX = 1
-__CC_SUPPORTS_DYNAMIC_ARRAY_INIT = 1
-__CC_INT_IS_32BIT = 1
-def __P(protos): return protos
-
-def __STRING(x): return #x
-
-def __XSTRING(x): return __STRING(x)
-
-def __P(protos): return ()
-
-def __STRING(x): return "x"
-
-def __aligned(x): return __attribute__((__aligned__(x)))
-
-def __section(x): return __attribute__((__section__(x)))
-
-def __aligned(x): return __attribute__((__aligned__(x)))
-
-def __section(x): return __attribute__((__section__(x)))
-
-def __nonnull(x): return __attribute__((__nonnull__(x)))
-
-def __predict_true(exp): return __builtin_expect((exp), 1)
-
-def __predict_false(exp): return __builtin_expect((exp), 0)
-
-def __predict_true(exp): return (exp)
-
-def __predict_false(exp): return (exp)
-
-def __format_arg(fmtarg): return __attribute__((__format_arg__ (fmtarg)))
-
-def __FBSDID(s): return __IDSTRING(__CONCAT(__rcsid_,__LINE__),s)
-
-def __RCSID(s): return __IDSTRING(__CONCAT(__rcsid_,__LINE__),s)
-
-def __RCSID_SOURCE(s): return __IDSTRING(__CONCAT(__rcsid_source_,__LINE__),s)
-
-def __SCCSID(s): return __IDSTRING(__CONCAT(__sccsid_,__LINE__),s)
-
-def __COPYRIGHT(s): return __IDSTRING(__CONCAT(__copyright_,__LINE__),s)
-
-_POSIX_C_SOURCE = 199009
-_POSIX_C_SOURCE = 199209
-__XSI_VISIBLE = 600
-_POSIX_C_SOURCE = 200112
-__XSI_VISIBLE = 500
-_POSIX_C_SOURCE = 199506
-_POSIX_C_SOURCE = 198808
-__POSIX_VISIBLE = 200112
-__ISO_C_VISIBLE = 1999
-__POSIX_VISIBLE = 199506
-__ISO_C_VISIBLE = 1990
-__POSIX_VISIBLE = 199309
-__ISO_C_VISIBLE = 1990
-__POSIX_VISIBLE = 199209
-__ISO_C_VISIBLE = 1990
-__POSIX_VISIBLE = 199009
-__ISO_C_VISIBLE = 1990
-__POSIX_VISIBLE = 198808
-__ISO_C_VISIBLE = 0
-__POSIX_VISIBLE = 0
-__XSI_VISIBLE = 0
-__BSD_VISIBLE = 0
-__ISO_C_VISIBLE = 1990
-__POSIX_VISIBLE = 0
-__XSI_VISIBLE = 0
-__BSD_VISIBLE = 0
-__ISO_C_VISIBLE = 1999
-__POSIX_VISIBLE = 200112
-__XSI_VISIBLE = 600
-__BSD_VISIBLE = 1
-__ISO_C_VISIBLE = 1999
-
-# Included from sys/_types.h
-
-# Included from machine/_types.h
-
-# Included from machine/endian.h
-_QUAD_HIGHWORD = 1
-_QUAD_LOWWORD = 0
-_LITTLE_ENDIAN = 1234
-_BIG_ENDIAN = 4321
-_PDP_ENDIAN = 3412
-_BYTE_ORDER = _LITTLE_ENDIAN
-LITTLE_ENDIAN = _LITTLE_ENDIAN
-BIG_ENDIAN = _BIG_ENDIAN
-PDP_ENDIAN = _PDP_ENDIAN
-BYTE_ORDER = _BYTE_ORDER
-def __word_swap_int_var(x): return \
-
-def __word_swap_int_const(x): return \
-
-def __word_swap_int(x): return __word_swap_int_var(x)
-
-def __byte_swap_int_var(x): return \
-
-def __byte_swap_int_const(x): return \
-
-def __byte_swap_int(x): return __byte_swap_int_var(x)
-
-def __byte_swap_long_var(x): return \
-
-def __byte_swap_long_const(x): return \
-
-def __byte_swap_long(x): return __byte_swap_long_var(x)
-
-def __byte_swap_word_var(x): return \
-
-def __byte_swap_word_const(x): return \
-
-def __byte_swap_word(x): return __byte_swap_word_var(x)
-
-def __htonl(x): return __bswap32(x)
-
-def __htons(x): return __bswap16(x)
-
-def __ntohl(x): return __bswap32(x)
-
-def __ntohs(x): return __bswap16(x)
-
-IPPROTO_IP = 0
-IPPROTO_ICMP = 1
-IPPROTO_TCP = 6
-IPPROTO_UDP = 17
-def htonl(x): return __htonl(x)
-
-def htons(x): return __htons(x)
-
-def ntohl(x): return __ntohl(x)
-
-def ntohs(x): return __ntohs(x)
-
-IPPROTO_RAW = 255
-INET_ADDRSTRLEN = 16
-IPPROTO_HOPOPTS = 0
-IPPROTO_IGMP = 2
-IPPROTO_GGP = 3
-IPPROTO_IPV4 = 4
-IPPROTO_IPIP = IPPROTO_IPV4
-IPPROTO_ST = 7
-IPPROTO_EGP = 8
-IPPROTO_PIGP = 9
-IPPROTO_RCCMON = 10
-IPPROTO_NVPII = 11
-IPPROTO_PUP = 12
-IPPROTO_ARGUS = 13
-IPPROTO_EMCON = 14
-IPPROTO_XNET = 15
-IPPROTO_CHAOS = 16
-IPPROTO_MUX = 18
-IPPROTO_MEAS = 19
-IPPROTO_HMP = 20
-IPPROTO_PRM = 21
-IPPROTO_IDP = 22
-IPPROTO_TRUNK1 = 23
-IPPROTO_TRUNK2 = 24
-IPPROTO_LEAF1 = 25
-IPPROTO_LEAF2 = 26
-IPPROTO_RDP = 27
-IPPROTO_IRTP = 28
-IPPROTO_TP = 29
-IPPROTO_BLT = 30
-IPPROTO_NSP = 31
-IPPROTO_INP = 32
-IPPROTO_SEP = 33
-IPPROTO_3PC = 34
-IPPROTO_IDPR = 35
-IPPROTO_XTP = 36
-IPPROTO_DDP = 37
-IPPROTO_CMTP = 38
-IPPROTO_TPXX = 39
-IPPROTO_IL = 40
-IPPROTO_IPV6 = 41
-IPPROTO_SDRP = 42
-IPPROTO_ROUTING = 43
-IPPROTO_FRAGMENT = 44
-IPPROTO_IDRP = 45
-IPPROTO_RSVP = 46
-IPPROTO_GRE = 47
-IPPROTO_MHRP = 48
-IPPROTO_BHA = 49
-IPPROTO_ESP = 50
-IPPROTO_AH = 51
-IPPROTO_INLSP = 52
-IPPROTO_SWIPE = 53
-IPPROTO_NHRP = 54
-IPPROTO_MOBILE = 55
-IPPROTO_TLSP = 56
-IPPROTO_SKIP = 57
-IPPROTO_ICMPV6 = 58
-IPPROTO_NONE = 59
-IPPROTO_DSTOPTS = 60
-IPPROTO_AHIP = 61
-IPPROTO_CFTP = 62
-IPPROTO_HELLO = 63
-IPPROTO_SATEXPAK = 64
-IPPROTO_KRYPTOLAN = 65
-IPPROTO_RVD = 66
-IPPROTO_IPPC = 67
-IPPROTO_ADFS = 68
-IPPROTO_SATMON = 69
-IPPROTO_VISA = 70
-IPPROTO_IPCV = 71
-IPPROTO_CPNX = 72
-IPPROTO_CPHB = 73
-IPPROTO_WSN = 74
-IPPROTO_PVP = 75
-IPPROTO_BRSATMON = 76
-IPPROTO_ND = 77
-IPPROTO_WBMON = 78
-IPPROTO_WBEXPAK = 79
-IPPROTO_EON = 80
-IPPROTO_VMTP = 81
-IPPROTO_SVMTP = 82
-IPPROTO_VINES = 83
-IPPROTO_TTP = 84
-IPPROTO_IGP = 85
-IPPROTO_DGP = 86
-IPPROTO_TCF = 87
-IPPROTO_IGRP = 88
-IPPROTO_OSPFIGP = 89
-IPPROTO_SRPC = 90
-IPPROTO_LARP = 91
-IPPROTO_MTP = 92
-IPPROTO_AX25 = 93
-IPPROTO_IPEIP = 94
-IPPROTO_MICP = 95
-IPPROTO_SCCSP = 96
-IPPROTO_ETHERIP = 97
-IPPROTO_ENCAP = 98
-IPPROTO_APES = 99
-IPPROTO_GMTP = 100
-IPPROTO_IPCOMP = 108
-IPPROTO_SCTP = 132
-IPPROTO_PIM = 103
-IPPROTO_CARP = 112
-IPPROTO_PGM = 113
-IPPROTO_PFSYNC = 240
-IPPROTO_OLD_DIVERT = 254
-IPPROTO_MAX = 256
-IPPROTO_DONE = 257
-IPPROTO_DIVERT = 258
-IPPROTO_SPACER = 32767
-IPPORT_RESERVED = 1024
-IPPORT_HIFIRSTAUTO = 49152
-IPPORT_HILASTAUTO = 65535
-IPPORT_RESERVEDSTART = 600
-IPPORT_MAX = 65535
-def IN_CLASSA(i): return (((u_int32_t)(i) & 0x80000000) == 0)
-
-IN_CLASSA_NET = 0xff000000
-IN_CLASSA_NSHIFT = 24
-IN_CLASSA_HOST = 0x00ffffff
-IN_CLASSA_MAX = 128
-def IN_CLASSB(i): return (((u_int32_t)(i) & 0xc0000000) == 0x80000000)
-
-IN_CLASSB_NET = 0xffff0000
-IN_CLASSB_NSHIFT = 16
-IN_CLASSB_HOST = 0x0000ffff
-IN_CLASSB_MAX = 65536
-def IN_CLASSC(i): return (((u_int32_t)(i) & 0xe0000000) == 0xc0000000)
-
-IN_CLASSC_NET = 0xffffff00
-IN_CLASSC_NSHIFT = 8
-IN_CLASSC_HOST = 0x000000ff
-def IN_CLASSD(i): return (((u_int32_t)(i) & 0xf0000000) == 0xe0000000)
-
-IN_CLASSD_NET = 0xf0000000
-IN_CLASSD_NSHIFT = 28
-IN_CLASSD_HOST = 0x0fffffff
-def IN_MULTICAST(i): return IN_CLASSD(i)
-
-def IN_EXPERIMENTAL(i): return (((u_int32_t)(i) & 0xf0000000) == 0xf0000000)
-
-def IN_BADCLASS(i): return (((u_int32_t)(i) & 0xf0000000) == 0xf0000000)
-
-INADDR_NONE = 0xffffffff
-IN_LOOPBACKNET = 127
-IP_OPTIONS = 1
-IP_HDRINCL = 2
-IP_TOS = 3
-IP_TTL = 4
-IP_RECVOPTS = 5
-IP_RECVRETOPTS = 6
-IP_RECVDSTADDR = 7
-IP_SENDSRCADDR = IP_RECVDSTADDR
-IP_RETOPTS = 8
-IP_MULTICAST_IF = 9
-IP_MULTICAST_TTL = 10
-IP_MULTICAST_LOOP = 11
-IP_ADD_MEMBERSHIP = 12
-IP_DROP_MEMBERSHIP = 13
-IP_MULTICAST_VIF = 14
-IP_RSVP_ON = 15
-IP_RSVP_OFF = 16
-IP_RSVP_VIF_ON = 17
-IP_RSVP_VIF_OFF = 18
-IP_PORTRANGE = 19
-IP_RECVIF = 20
-IP_IPSEC_POLICY = 21
-IP_FAITH = 22
-IP_ONESBCAST = 23
-IP_FW_TABLE_ADD = 40
-IP_FW_TABLE_DEL = 41
-IP_FW_TABLE_FLUSH = 42
-IP_FW_TABLE_GETSIZE = 43
-IP_FW_TABLE_LIST = 44
-IP_FW_ADD = 50
-IP_FW_DEL = 51
-IP_FW_FLUSH = 52
-IP_FW_ZERO = 53
-IP_FW_GET = 54
-IP_FW_RESETLOG = 55
-IP_DUMMYNET_CONFIGURE = 60
-IP_DUMMYNET_DEL = 61
-IP_DUMMYNET_FLUSH = 62
-IP_DUMMYNET_GET = 64
-IP_RECVTTL = 65
-IP_MINTTL = 66
-IP_DONTFRAG = 67
-IP_DEFAULT_MULTICAST_TTL = 1
-IP_DEFAULT_MULTICAST_LOOP = 1
-IP_MAX_MEMBERSHIPS = 20
-IP_PORTRANGE_DEFAULT = 0
-IP_PORTRANGE_HIGH = 1
-IP_PORTRANGE_LOW = 2
-IPPROTO_MAXID = (IPPROTO_AH + 1)
-IPCTL_FORWARDING = 1
-IPCTL_SENDREDIRECTS = 2
-IPCTL_DEFTTL = 3
-IPCTL_DEFMTU = 4
-IPCTL_RTEXPIRE = 5
-IPCTL_RTMINEXPIRE = 6
-IPCTL_RTMAXCACHE = 7
-IPCTL_SOURCEROUTE = 8
-IPCTL_DIRECTEDBROADCAST = 9
-IPCTL_INTRQMAXLEN = 10
-IPCTL_INTRQDROPS = 11
-IPCTL_STATS = 12
-IPCTL_ACCEPTSOURCEROUTE = 13
-IPCTL_FASTFORWARDING = 14
-IPCTL_KEEPFAITH = 15
-IPCTL_GIF_TTL = 16
-IPCTL_MAXID = 17
-def in_nullhost(x): return ((x).s_addr == INADDR_ANY)
-
-
-# Included from netinet6/in6.h
-__KAME_VERSION = "FreeBSD"
-IPV6PORT_RESERVED = 1024
-IPV6PORT_ANONMIN = 49152
-IPV6PORT_ANONMAX = 65535
-IPV6PORT_RESERVEDMIN = 600
-IPV6PORT_RESERVEDMAX = (IPV6PORT_RESERVED-1)
-INET6_ADDRSTRLEN = 46
-IPV6_ADDR_INT32_ONE = 1
-IPV6_ADDR_INT32_TWO = 2
-IPV6_ADDR_INT32_MNL = 0xff010000
-IPV6_ADDR_INT32_MLL = 0xff020000
-IPV6_ADDR_INT32_SMP = 0x0000ffff
-IPV6_ADDR_INT16_ULL = 0xfe80
-IPV6_ADDR_INT16_USL = 0xfec0
-IPV6_ADDR_INT16_MLL = 0xff02
-IPV6_ADDR_INT32_ONE = 0x01000000
-IPV6_ADDR_INT32_TWO = 0x02000000
-IPV6_ADDR_INT32_MNL = 0x000001ff
-IPV6_ADDR_INT32_MLL = 0x000002ff
-IPV6_ADDR_INT32_SMP = 0xffff0000
-IPV6_ADDR_INT16_ULL = 0x80fe
-IPV6_ADDR_INT16_USL = 0xc0fe
-IPV6_ADDR_INT16_MLL = 0x02ff
-def IN6_IS_ADDR_UNSPECIFIED(a): return \
-
-def IN6_IS_ADDR_LOOPBACK(a): return \
-
-def IN6_IS_ADDR_V4COMPAT(a): return \
-
-def IN6_IS_ADDR_V4MAPPED(a): return \
-
-IPV6_ADDR_SCOPE_NODELOCAL = 0x01
-IPV6_ADDR_SCOPE_INTFACELOCAL = 0x01
-IPV6_ADDR_SCOPE_LINKLOCAL = 0x02
-IPV6_ADDR_SCOPE_SITELOCAL = 0x05
-IPV6_ADDR_SCOPE_ORGLOCAL = 0x08
-IPV6_ADDR_SCOPE_GLOBAL = 0x0e
-__IPV6_ADDR_SCOPE_NODELOCAL = 0x01
-__IPV6_ADDR_SCOPE_INTFACELOCAL = 0x01
-__IPV6_ADDR_SCOPE_LINKLOCAL = 0x02
-__IPV6_ADDR_SCOPE_SITELOCAL = 0x05
-__IPV6_ADDR_SCOPE_ORGLOCAL = 0x08
-__IPV6_ADDR_SCOPE_GLOBAL = 0x0e
-def IN6_IS_ADDR_LINKLOCAL(a): return \
-
-def IN6_IS_ADDR_SITELOCAL(a): return \
-
-def IN6_IS_ADDR_MC_NODELOCAL(a): return \
-
-def IN6_IS_ADDR_MC_INTFACELOCAL(a): return \
-
-def IN6_IS_ADDR_MC_LINKLOCAL(a): return \
-
-def IN6_IS_ADDR_MC_SITELOCAL(a): return \
-
-def IN6_IS_ADDR_MC_ORGLOCAL(a): return \
-
-def IN6_IS_ADDR_MC_GLOBAL(a): return \
-
-def IN6_IS_ADDR_MC_NODELOCAL(a): return \
-
-def IN6_IS_ADDR_MC_LINKLOCAL(a): return \
-
-def IN6_IS_ADDR_MC_SITELOCAL(a): return \
-
-def IN6_IS_ADDR_MC_ORGLOCAL(a): return \
-
-def IN6_IS_ADDR_MC_GLOBAL(a): return \
-
-def IN6_IS_SCOPE_LINKLOCAL(a): return \
-
-def IFA6_IS_DEPRECATED(a): return \
-
-def IFA6_IS_INVALID(a): return \
-
-IPV6_OPTIONS = 1
-IPV6_RECVOPTS = 5
-IPV6_RECVRETOPTS = 6
-IPV6_RECVDSTADDR = 7
-IPV6_RETOPTS = 8
-IPV6_SOCKOPT_RESERVED1 = 3
-IPV6_UNICAST_HOPS = 4
-IPV6_MULTICAST_IF = 9
-IPV6_MULTICAST_HOPS = 10
-IPV6_MULTICAST_LOOP = 11
-IPV6_JOIN_GROUP = 12
-IPV6_LEAVE_GROUP = 13
-IPV6_PORTRANGE = 14
-ICMP6_FILTER = 18
-IPV6_2292PKTINFO = 19
-IPV6_2292HOPLIMIT = 20
-IPV6_2292NEXTHOP = 21
-IPV6_2292HOPOPTS = 22
-IPV6_2292DSTOPTS = 23
-IPV6_2292RTHDR = 24
-IPV6_2292PKTOPTIONS = 25
-IPV6_CHECKSUM = 26
-IPV6_V6ONLY = 27
-IPV6_BINDV6ONLY = IPV6_V6ONLY
-IPV6_IPSEC_POLICY = 28
-IPV6_FAITH = 29
-IPV6_FW_ADD = 30
-IPV6_FW_DEL = 31
-IPV6_FW_FLUSH = 32
-IPV6_FW_ZERO = 33
-IPV6_FW_GET = 34
-IPV6_RTHDRDSTOPTS = 35
-IPV6_RECVPKTINFO = 36
-IPV6_RECVHOPLIMIT = 37
-IPV6_RECVRTHDR = 38
-IPV6_RECVHOPOPTS = 39
-IPV6_RECVDSTOPTS = 40
-IPV6_RECVRTHDRDSTOPTS = 41
-IPV6_USE_MIN_MTU = 42
-IPV6_RECVPATHMTU = 43
-IPV6_PATHMTU = 44
-IPV6_REACHCONF = 45
-IPV6_PKTINFO = 46
-IPV6_HOPLIMIT = 47
-IPV6_NEXTHOP = 48
-IPV6_HOPOPTS = 49
-IPV6_DSTOPTS = 50
-IPV6_RTHDR = 51
-IPV6_PKTOPTIONS = 52
-IPV6_RECVTCLASS = 57
-IPV6_AUTOFLOWLABEL = 59
-IPV6_TCLASS = 61
-IPV6_DONTFRAG = 62
-IPV6_PREFER_TEMPADDR = 63
-IPV6_RTHDR_LOOSE = 0
-IPV6_RTHDR_STRICT = 1
-IPV6_RTHDR_TYPE_0 = 0
-IPV6_DEFAULT_MULTICAST_HOPS = 1
-IPV6_DEFAULT_MULTICAST_LOOP = 1
-IPV6_PORTRANGE_DEFAULT = 0
-IPV6_PORTRANGE_HIGH = 1
-IPV6_PORTRANGE_LOW = 2
-IPV6PROTO_MAXID = (IPPROTO_PIM + 1)
-IPV6CTL_FORWARDING = 1
-IPV6CTL_SENDREDIRECTS = 2
-IPV6CTL_DEFHLIM = 3
-IPV6CTL_DEFMTU = 4
-IPV6CTL_FORWSRCRT = 5
-IPV6CTL_STATS = 6
-IPV6CTL_MRTSTATS = 7
-IPV6CTL_MRTPROTO = 8
-IPV6CTL_MAXFRAGPACKETS = 9
-IPV6CTL_SOURCECHECK = 10
-IPV6CTL_SOURCECHECK_LOGINT = 11
-IPV6CTL_ACCEPT_RTADV = 12
-IPV6CTL_KEEPFAITH = 13
-IPV6CTL_LOG_INTERVAL = 14
-IPV6CTL_HDRNESTLIMIT = 15
-IPV6CTL_DAD_COUNT = 16
-IPV6CTL_AUTO_FLOWLABEL = 17
-IPV6CTL_DEFMCASTHLIM = 18
-IPV6CTL_GIF_HLIM = 19
-IPV6CTL_KAME_VERSION = 20
-IPV6CTL_USE_DEPRECATED = 21
-IPV6CTL_RR_PRUNE = 22
-IPV6CTL_MAPPED_ADDR = 23
-IPV6CTL_V6ONLY = 24
-IPV6CTL_RTEXPIRE = 25
-IPV6CTL_RTMINEXPIRE = 26
-IPV6CTL_RTMAXCACHE = 27
-IPV6CTL_USETEMPADDR = 32
-IPV6CTL_TEMPPLTIME = 33
-IPV6CTL_TEMPVLTIME = 34
-IPV6CTL_AUTO_LINKLOCAL = 35
-IPV6CTL_RIP6STATS = 36
-IPV6CTL_PREFER_TEMPADDR = 37
-IPV6CTL_ADDRCTLPOLICY = 38
-IPV6CTL_USE_DEFAULTZONE = 39
-IPV6CTL_MAXFRAGS = 41
-IPV6CTL_IFQ = 42
-IPV6CTL_ISATAPRTR = 43
-IPV6CTL_MCAST_PMTU = 44
-IPV6CTL_STEALTH = 45
-IPV6CTL_MAXID = 46
diff --git a/Lib/plat-freebsd6/regen b/Lib/plat-freebsd6/regen
deleted file mode 100755
index 8aa6898..0000000
--- a/Lib/plat-freebsd6/regen
+++ /dev/null
@@ -1,3 +0,0 @@
-#! /bin/sh
-set -v
-python ../../Tools/scripts/h2py.py -i '(u_long)' /usr/include/netinet/in.h
diff --git a/Lib/plat-freebsd7/IN.py b/Lib/plat-freebsd7/IN.py
deleted file mode 100644
index 4e3b3a2..0000000
--- a/Lib/plat-freebsd7/IN.py
+++ /dev/null
@@ -1,571 +0,0 @@
-# Generated by h2py from /usr/include/netinet/in.h
-
-# Included from sys/cdefs.h
-__GNUCLIKE_ASM = 3
-__GNUCLIKE_ASM = 2
-__GNUCLIKE___TYPEOF = 1
-__GNUCLIKE___OFFSETOF = 1
-__GNUCLIKE___SECTION = 1
-__GNUCLIKE_ATTRIBUTE_MODE_DI = 1
-__GNUCLIKE_CTOR_SECTION_HANDLING = 1
-__GNUCLIKE_BUILTIN_CONSTANT_P = 1
-__GNUCLIKE_BUILTIN_VARARGS = 1
-__GNUCLIKE_BUILTIN_STDARG = 1
-__GNUCLIKE_BUILTIN_VAALIST = 1
-__GNUC_VA_LIST_COMPATIBILITY = 1
-__GNUCLIKE_BUILTIN_NEXT_ARG = 1
-__GNUCLIKE_BUILTIN_MEMCPY = 1
-__CC_SUPPORTS_INLINE = 1
-__CC_SUPPORTS___INLINE = 1
-__CC_SUPPORTS___INLINE__ = 1
-__CC_SUPPORTS___FUNC__ = 1
-__CC_SUPPORTS_WARNING = 1
-__CC_SUPPORTS_VARADIC_XXX = 1
-__CC_SUPPORTS_DYNAMIC_ARRAY_INIT = 1
-__CC_INT_IS_32BIT = 1
-def __P(protos): return protos
-
-def __STRING(x): return #x
-
-def __XSTRING(x): return __STRING(x)
-
-def __P(protos): return ()
-
-def __STRING(x): return "x"
-
-def __aligned(x): return __attribute__((__aligned__(x)))
-
-def __section(x): return __attribute__((__section__(x)))
-
-def __aligned(x): return __attribute__((__aligned__(x)))
-
-def __section(x): return __attribute__((__section__(x)))
-
-def __nonnull(x): return __attribute__((__nonnull__(x)))
-
-def __predict_true(exp): return __builtin_expect((exp), 1)
-
-def __predict_false(exp): return __builtin_expect((exp), 0)
-
-def __predict_true(exp): return (exp)
-
-def __predict_false(exp): return (exp)
-
-def __format_arg(fmtarg): return __attribute__((__format_arg__ (fmtarg)))
-
-def __FBSDID(s): return __IDSTRING(__CONCAT(__rcsid_,__LINE__),s)
-
-def __RCSID(s): return __IDSTRING(__CONCAT(__rcsid_,__LINE__),s)
-
-def __RCSID_SOURCE(s): return __IDSTRING(__CONCAT(__rcsid_source_,__LINE__),s)
-
-def __SCCSID(s): return __IDSTRING(__CONCAT(__sccsid_,__LINE__),s)
-
-def __COPYRIGHT(s): return __IDSTRING(__CONCAT(__copyright_,__LINE__),s)
-
-_POSIX_C_SOURCE = 199009
-_POSIX_C_SOURCE = 199209
-__XSI_VISIBLE = 600
-_POSIX_C_SOURCE = 200112
-__XSI_VISIBLE = 500
-_POSIX_C_SOURCE = 199506
-_POSIX_C_SOURCE = 198808
-__POSIX_VISIBLE = 200112
-__ISO_C_VISIBLE = 1999
-__POSIX_VISIBLE = 199506
-__ISO_C_VISIBLE = 1990
-__POSIX_VISIBLE = 199309
-__ISO_C_VISIBLE = 1990
-__POSIX_VISIBLE = 199209
-__ISO_C_VISIBLE = 1990
-__POSIX_VISIBLE = 199009
-__ISO_C_VISIBLE = 1990
-__POSIX_VISIBLE = 198808
-__ISO_C_VISIBLE = 0
-__POSIX_VISIBLE = 0
-__XSI_VISIBLE = 0
-__BSD_VISIBLE = 0
-__ISO_C_VISIBLE = 1990
-__POSIX_VISIBLE = 0
-__XSI_VISIBLE = 0
-__BSD_VISIBLE = 0
-__ISO_C_VISIBLE = 1999
-__POSIX_VISIBLE = 200112
-__XSI_VISIBLE = 600
-__BSD_VISIBLE = 1
-__ISO_C_VISIBLE = 1999
-
-# Included from sys/_types.h
-
-# Included from machine/_types.h
-
-# Included from machine/endian.h
-_QUAD_HIGHWORD = 1
-_QUAD_LOWWORD = 0
-_LITTLE_ENDIAN = 1234
-_BIG_ENDIAN = 4321
-_PDP_ENDIAN = 3412
-_BYTE_ORDER = _LITTLE_ENDIAN
-LITTLE_ENDIAN = _LITTLE_ENDIAN
-BIG_ENDIAN = _BIG_ENDIAN
-PDP_ENDIAN = _PDP_ENDIAN
-BYTE_ORDER = _BYTE_ORDER
-def __word_swap_int_var(x): return \
-
-def __word_swap_int_const(x): return \
-
-def __word_swap_int(x): return __word_swap_int_var(x)
-
-def __byte_swap_int_var(x): return \
-
-def __byte_swap_int_const(x): return \
-
-def __byte_swap_int(x): return __byte_swap_int_var(x)
-
-def __byte_swap_word_var(x): return \
-
-def __byte_swap_word_const(x): return \
-
-def __byte_swap_word(x): return __byte_swap_word_var(x)
-
-def __htonl(x): return __bswap32(x)
-
-def __htons(x): return __bswap16(x)
-
-def __ntohl(x): return __bswap32(x)
-
-def __ntohs(x): return __bswap16(x)
-
-IPPROTO_IP = 0
-IPPROTO_ICMP = 1
-IPPROTO_TCP = 6
-IPPROTO_UDP = 17
-def htonl(x): return __htonl(x)
-
-def htons(x): return __htons(x)
-
-def ntohl(x): return __ntohl(x)
-
-def ntohs(x): return __ntohs(x)
-
-IPPROTO_RAW = 255
-INET_ADDRSTRLEN = 16
-IPPROTO_HOPOPTS = 0
-IPPROTO_IGMP = 2
-IPPROTO_GGP = 3
-IPPROTO_IPV4 = 4
-IPPROTO_IPIP = IPPROTO_IPV4
-IPPROTO_ST = 7
-IPPROTO_EGP = 8
-IPPROTO_PIGP = 9
-IPPROTO_RCCMON = 10
-IPPROTO_NVPII = 11
-IPPROTO_PUP = 12
-IPPROTO_ARGUS = 13
-IPPROTO_EMCON = 14
-IPPROTO_XNET = 15
-IPPROTO_CHAOS = 16
-IPPROTO_MUX = 18
-IPPROTO_MEAS = 19
-IPPROTO_HMP = 20
-IPPROTO_PRM = 21
-IPPROTO_IDP = 22
-IPPROTO_TRUNK1 = 23
-IPPROTO_TRUNK2 = 24
-IPPROTO_LEAF1 = 25
-IPPROTO_LEAF2 = 26
-IPPROTO_RDP = 27
-IPPROTO_IRTP = 28
-IPPROTO_TP = 29
-IPPROTO_BLT = 30
-IPPROTO_NSP = 31
-IPPROTO_INP = 32
-IPPROTO_SEP = 33
-IPPROTO_3PC = 34
-IPPROTO_IDPR = 35
-IPPROTO_XTP = 36
-IPPROTO_DDP = 37
-IPPROTO_CMTP = 38
-IPPROTO_TPXX = 39
-IPPROTO_IL = 40
-IPPROTO_IPV6 = 41
-IPPROTO_SDRP = 42
-IPPROTO_ROUTING = 43
-IPPROTO_FRAGMENT = 44
-IPPROTO_IDRP = 45
-IPPROTO_RSVP = 46
-IPPROTO_GRE = 47
-IPPROTO_MHRP = 48
-IPPROTO_BHA = 49
-IPPROTO_ESP = 50
-IPPROTO_AH = 51
-IPPROTO_INLSP = 52
-IPPROTO_SWIPE = 53
-IPPROTO_NHRP = 54
-IPPROTO_MOBILE = 55
-IPPROTO_TLSP = 56
-IPPROTO_SKIP = 57
-IPPROTO_ICMPV6 = 58
-IPPROTO_NONE = 59
-IPPROTO_DSTOPTS = 60
-IPPROTO_AHIP = 61
-IPPROTO_CFTP = 62
-IPPROTO_HELLO = 63
-IPPROTO_SATEXPAK = 64
-IPPROTO_KRYPTOLAN = 65
-IPPROTO_RVD = 66
-IPPROTO_IPPC = 67
-IPPROTO_ADFS = 68
-IPPROTO_SATMON = 69
-IPPROTO_VISA = 70
-IPPROTO_IPCV = 71
-IPPROTO_CPNX = 72
-IPPROTO_CPHB = 73
-IPPROTO_WSN = 74
-IPPROTO_PVP = 75
-IPPROTO_BRSATMON = 76
-IPPROTO_ND = 77
-IPPROTO_WBMON = 78
-IPPROTO_WBEXPAK = 79
-IPPROTO_EON = 80
-IPPROTO_VMTP = 81
-IPPROTO_SVMTP = 82
-IPPROTO_VINES = 83
-IPPROTO_TTP = 84
-IPPROTO_IGP = 85
-IPPROTO_DGP = 86
-IPPROTO_TCF = 87
-IPPROTO_IGRP = 88
-IPPROTO_OSPFIGP = 89
-IPPROTO_SRPC = 90
-IPPROTO_LARP = 91
-IPPROTO_MTP = 92
-IPPROTO_AX25 = 93
-IPPROTO_IPEIP = 94
-IPPROTO_MICP = 95
-IPPROTO_SCCSP = 96
-IPPROTO_ETHERIP = 97
-IPPROTO_ENCAP = 98
-IPPROTO_APES = 99
-IPPROTO_GMTP = 100
-IPPROTO_IPCOMP = 108
-IPPROTO_SCTP = 132
-IPPROTO_PIM = 103
-IPPROTO_CARP = 112
-IPPROTO_PGM = 113
-IPPROTO_PFSYNC = 240
-IPPROTO_OLD_DIVERT = 254
-IPPROTO_MAX = 256
-IPPROTO_DONE = 257
-IPPROTO_DIVERT = 258
-IPPROTO_SPACER = 32767
-IPPORT_RESERVED = 1024
-IPPORT_HIFIRSTAUTO = 49152
-IPPORT_HILASTAUTO = 65535
-IPPORT_RESERVEDSTART = 600
-IPPORT_MAX = 65535
-def IN_CLASSA(i): return (((u_int32_t)(i) & (-2147483648)) == 0)
-
-IN_CLASSA_NET = (-16777216)
-IN_CLASSA_NSHIFT = 24
-IN_CLASSA_HOST = 0x00ffffff
-IN_CLASSA_MAX = 128
-def IN_CLASSB(i): return (((u_int32_t)(i) & (-1073741824)) == (-2147483648))
-
-IN_CLASSB_NET = (-65536)
-IN_CLASSB_NSHIFT = 16
-IN_CLASSB_HOST = 0x0000ffff
-IN_CLASSB_MAX = 65536
-def IN_CLASSC(i): return (((u_int32_t)(i) & (-536870912)) == (-1073741824))
-
-IN_CLASSC_NET = (-256)
-IN_CLASSC_NSHIFT = 8
-IN_CLASSC_HOST = 0x000000ff
-def IN_CLASSD(i): return (((u_int32_t)(i) & (-268435456)) == (-536870912))
-
-IN_CLASSD_NET = (-268435456)
-IN_CLASSD_NSHIFT = 28
-IN_CLASSD_HOST = 0x0fffffff
-def IN_MULTICAST(i): return IN_CLASSD(i)
-
-def IN_EXPERIMENTAL(i): return (((u_int32_t)(i) & (-268435456)) == (-268435456))
-
-def IN_BADCLASS(i): return (((u_int32_t)(i) & (-268435456)) == (-268435456))
-
-def IN_LINKLOCAL(i): return (((u_int32_t)(i) & (-65536)) == (-1442971648))
-
-def IN_LOCAL_GROUP(i): return (((u_int32_t)(i) & (-256)) == (-536870912))
-
-INADDR_NONE = (-1)
-IN_LOOPBACKNET = 127
-IP_OPTIONS = 1
-IP_HDRINCL = 2
-IP_TOS = 3
-IP_TTL = 4
-IP_RECVOPTS = 5
-IP_RECVRETOPTS = 6
-IP_RECVDSTADDR = 7
-IP_SENDSRCADDR = IP_RECVDSTADDR
-IP_RETOPTS = 8
-IP_MULTICAST_IF = 9
-IP_MULTICAST_TTL = 10
-IP_MULTICAST_LOOP = 11
-IP_ADD_MEMBERSHIP = 12
-IP_DROP_MEMBERSHIP = 13
-IP_MULTICAST_VIF = 14
-IP_RSVP_ON = 15
-IP_RSVP_OFF = 16
-IP_RSVP_VIF_ON = 17
-IP_RSVP_VIF_OFF = 18
-IP_PORTRANGE = 19
-IP_RECVIF = 20
-IP_IPSEC_POLICY = 21
-IP_FAITH = 22
-IP_ONESBCAST = 23
-IP_FW_TABLE_ADD = 40
-IP_FW_TABLE_DEL = 41
-IP_FW_TABLE_FLUSH = 42
-IP_FW_TABLE_GETSIZE = 43
-IP_FW_TABLE_LIST = 44
-IP_FW_ADD = 50
-IP_FW_DEL = 51
-IP_FW_FLUSH = 52
-IP_FW_ZERO = 53
-IP_FW_GET = 54
-IP_FW_RESETLOG = 55
-IP_FW_NAT_CFG = 56
-IP_FW_NAT_DEL = 57
-IP_FW_NAT_GET_CONFIG = 58
-IP_FW_NAT_GET_LOG = 59
-IP_DUMMYNET_CONFIGURE = 60
-IP_DUMMYNET_DEL = 61
-IP_DUMMYNET_FLUSH = 62
-IP_DUMMYNET_GET = 64
-IP_RECVTTL = 65
-IP_MINTTL = 66
-IP_DONTFRAG = 67
-IP_ADD_SOURCE_MEMBERSHIP = 70
-IP_DROP_SOURCE_MEMBERSHIP = 71
-IP_BLOCK_SOURCE = 72
-IP_UNBLOCK_SOURCE = 73
-IP_MSFILTER = 74
-MCAST_JOIN_GROUP = 80
-MCAST_LEAVE_GROUP = 81
-MCAST_JOIN_SOURCE_GROUP = 82
-MCAST_LEAVE_SOURCE_GROUP = 83
-MCAST_BLOCK_SOURCE = 84
-MCAST_UNBLOCK_SOURCE = 85
-IP_DEFAULT_MULTICAST_TTL = 1
-IP_DEFAULT_MULTICAST_LOOP = 1
-IP_MIN_MEMBERSHIPS = 31
-IP_MAX_MEMBERSHIPS = 4095
-IP_MAX_SOURCE_FILTER = 1024
-MCAST_INCLUDE = 1
-MCAST_EXCLUDE = 2
-IP_PORTRANGE_DEFAULT = 0
-IP_PORTRANGE_HIGH = 1
-IP_PORTRANGE_LOW = 2
-IPPROTO_MAXID = (IPPROTO_AH + 1)
-IPCTL_FORWARDING = 1
-IPCTL_SENDREDIRECTS = 2
-IPCTL_DEFTTL = 3
-IPCTL_DEFMTU = 4
-IPCTL_RTEXPIRE = 5
-IPCTL_RTMINEXPIRE = 6
-IPCTL_RTMAXCACHE = 7
-IPCTL_SOURCEROUTE = 8
-IPCTL_DIRECTEDBROADCAST = 9
-IPCTL_INTRQMAXLEN = 10
-IPCTL_INTRQDROPS = 11
-IPCTL_STATS = 12
-IPCTL_ACCEPTSOURCEROUTE = 13
-IPCTL_FASTFORWARDING = 14
-IPCTL_KEEPFAITH = 15
-IPCTL_GIF_TTL = 16
-IPCTL_MAXID = 17
-def in_nullhost(x): return ((x).s_addr == INADDR_ANY)
-
-
-# Included from netinet6/in6.h
-__KAME_VERSION = "FreeBSD"
-IPV6PORT_RESERVED = 1024
-IPV6PORT_ANONMIN = 49152
-IPV6PORT_ANONMAX = 65535
-IPV6PORT_RESERVEDMIN = 600
-IPV6PORT_RESERVEDMAX = (IPV6PORT_RESERVED-1)
-INET6_ADDRSTRLEN = 46
-IPV6_ADDR_INT32_ONE = 1
-IPV6_ADDR_INT32_TWO = 2
-IPV6_ADDR_INT32_MNL = (-16711680)
-IPV6_ADDR_INT32_MLL = (-16646144)
-IPV6_ADDR_INT32_SMP = 0x0000ffff
-IPV6_ADDR_INT16_ULL = 0xfe80
-IPV6_ADDR_INT16_USL = 0xfec0
-IPV6_ADDR_INT16_MLL = 0xff02
-IPV6_ADDR_INT32_ONE = 0x01000000
-IPV6_ADDR_INT32_TWO = 0x02000000
-IPV6_ADDR_INT32_MNL = 0x000001ff
-IPV6_ADDR_INT32_MLL = 0x000002ff
-IPV6_ADDR_INT32_SMP = (-65536)
-IPV6_ADDR_INT16_ULL = 0x80fe
-IPV6_ADDR_INT16_USL = 0xc0fe
-IPV6_ADDR_INT16_MLL = 0x02ff
-def IN6_IS_ADDR_UNSPECIFIED(a): return \
-
-def IN6_IS_ADDR_LOOPBACK(a): return \
-
-def IN6_IS_ADDR_V4COMPAT(a): return \
-
-def IN6_IS_ADDR_V4MAPPED(a): return \
-
-IPV6_ADDR_SCOPE_NODELOCAL = 0x01
-IPV6_ADDR_SCOPE_INTFACELOCAL = 0x01
-IPV6_ADDR_SCOPE_LINKLOCAL = 0x02
-IPV6_ADDR_SCOPE_SITELOCAL = 0x05
-IPV6_ADDR_SCOPE_ORGLOCAL = 0x08
-IPV6_ADDR_SCOPE_GLOBAL = 0x0e
-__IPV6_ADDR_SCOPE_NODELOCAL = 0x01
-__IPV6_ADDR_SCOPE_INTFACELOCAL = 0x01
-__IPV6_ADDR_SCOPE_LINKLOCAL = 0x02
-__IPV6_ADDR_SCOPE_SITELOCAL = 0x05
-__IPV6_ADDR_SCOPE_ORGLOCAL = 0x08
-__IPV6_ADDR_SCOPE_GLOBAL = 0x0e
-def IN6_IS_ADDR_LINKLOCAL(a): return \
-
-def IN6_IS_ADDR_SITELOCAL(a): return \
-
-def IN6_IS_ADDR_MC_NODELOCAL(a): return \
-
-def IN6_IS_ADDR_MC_INTFACELOCAL(a): return \
-
-def IN6_IS_ADDR_MC_LINKLOCAL(a): return \
-
-def IN6_IS_ADDR_MC_SITELOCAL(a): return \
-
-def IN6_IS_ADDR_MC_ORGLOCAL(a): return \
-
-def IN6_IS_ADDR_MC_GLOBAL(a): return \
-
-def IN6_IS_ADDR_MC_NODELOCAL(a): return \
-
-def IN6_IS_ADDR_MC_LINKLOCAL(a): return \
-
-def IN6_IS_ADDR_MC_SITELOCAL(a): return \
-
-def IN6_IS_ADDR_MC_ORGLOCAL(a): return \
-
-def IN6_IS_ADDR_MC_GLOBAL(a): return \
-
-def IN6_IS_SCOPE_LINKLOCAL(a): return \
-
-def IN6_IS_SCOPE_EMBED(a): return \
-
-def IFA6_IS_DEPRECATED(a): return \
-
-def IFA6_IS_INVALID(a): return \
-
-IPV6_OPTIONS = 1
-IPV6_RECVOPTS = 5
-IPV6_RECVRETOPTS = 6
-IPV6_RECVDSTADDR = 7
-IPV6_RETOPTS = 8
-IPV6_SOCKOPT_RESERVED1 = 3
-IPV6_UNICAST_HOPS = 4
-IPV6_MULTICAST_IF = 9
-IPV6_MULTICAST_HOPS = 10
-IPV6_MULTICAST_LOOP = 11
-IPV6_JOIN_GROUP = 12
-IPV6_LEAVE_GROUP = 13
-IPV6_PORTRANGE = 14
-ICMP6_FILTER = 18
-IPV6_2292PKTINFO = 19
-IPV6_2292HOPLIMIT = 20
-IPV6_2292NEXTHOP = 21
-IPV6_2292HOPOPTS = 22
-IPV6_2292DSTOPTS = 23
-IPV6_2292RTHDR = 24
-IPV6_2292PKTOPTIONS = 25
-IPV6_CHECKSUM = 26
-IPV6_V6ONLY = 27
-IPV6_BINDV6ONLY = IPV6_V6ONLY
-IPV6_IPSEC_POLICY = 28
-IPV6_FAITH = 29
-IPV6_FW_ADD = 30
-IPV6_FW_DEL = 31
-IPV6_FW_FLUSH = 32
-IPV6_FW_ZERO = 33
-IPV6_FW_GET = 34
-IPV6_RTHDRDSTOPTS = 35
-IPV6_RECVPKTINFO = 36
-IPV6_RECVHOPLIMIT = 37
-IPV6_RECVRTHDR = 38
-IPV6_RECVHOPOPTS = 39
-IPV6_RECVDSTOPTS = 40
-IPV6_RECVRTHDRDSTOPTS = 41
-IPV6_USE_MIN_MTU = 42
-IPV6_RECVPATHMTU = 43
-IPV6_PATHMTU = 44
-IPV6_REACHCONF = 45
-IPV6_PKTINFO = 46
-IPV6_HOPLIMIT = 47
-IPV6_NEXTHOP = 48
-IPV6_HOPOPTS = 49
-IPV6_DSTOPTS = 50
-IPV6_RTHDR = 51
-IPV6_PKTOPTIONS = 52
-IPV6_RECVTCLASS = 57
-IPV6_AUTOFLOWLABEL = 59
-IPV6_TCLASS = 61
-IPV6_DONTFRAG = 62
-IPV6_PREFER_TEMPADDR = 63
-IPV6_MSFILTER = 74
-IPV6_RTHDR_LOOSE = 0
-IPV6_RTHDR_STRICT = 1
-IPV6_RTHDR_TYPE_0 = 0
-IPV6_DEFAULT_MULTICAST_HOPS = 1
-IPV6_DEFAULT_MULTICAST_LOOP = 1
-IPV6_PORTRANGE_DEFAULT = 0
-IPV6_PORTRANGE_HIGH = 1
-IPV6_PORTRANGE_LOW = 2
-IPV6PROTO_MAXID = (IPPROTO_PIM + 1)
-IPV6CTL_FORWARDING = 1
-IPV6CTL_SENDREDIRECTS = 2
-IPV6CTL_DEFHLIM = 3
-IPV6CTL_DEFMTU = 4
-IPV6CTL_FORWSRCRT = 5
-IPV6CTL_STATS = 6
-IPV6CTL_MRTSTATS = 7
-IPV6CTL_MRTPROTO = 8
-IPV6CTL_MAXFRAGPACKETS = 9
-IPV6CTL_SOURCECHECK = 10
-IPV6CTL_SOURCECHECK_LOGINT = 11
-IPV6CTL_ACCEPT_RTADV = 12
-IPV6CTL_KEEPFAITH = 13
-IPV6CTL_LOG_INTERVAL = 14
-IPV6CTL_HDRNESTLIMIT = 15
-IPV6CTL_DAD_COUNT = 16
-IPV6CTL_AUTO_FLOWLABEL = 17
-IPV6CTL_DEFMCASTHLIM = 18
-IPV6CTL_GIF_HLIM = 19
-IPV6CTL_KAME_VERSION = 20
-IPV6CTL_USE_DEPRECATED = 21
-IPV6CTL_RR_PRUNE = 22
-IPV6CTL_MAPPED_ADDR = 23
-IPV6CTL_V6ONLY = 24
-IPV6CTL_RTEXPIRE = 25
-IPV6CTL_RTMINEXPIRE = 26
-IPV6CTL_RTMAXCACHE = 27
-IPV6CTL_USETEMPADDR = 32
-IPV6CTL_TEMPPLTIME = 33
-IPV6CTL_TEMPVLTIME = 34
-IPV6CTL_AUTO_LINKLOCAL = 35
-IPV6CTL_RIP6STATS = 36
-IPV6CTL_PREFER_TEMPADDR = 37
-IPV6CTL_ADDRCTLPOLICY = 38
-IPV6CTL_USE_DEFAULTZONE = 39
-IPV6CTL_MAXFRAGS = 41
-IPV6CTL_IFQ = 42
-IPV6CTL_ISATAPRTR = 43
-IPV6CTL_MCAST_PMTU = 44
-IPV6CTL_STEALTH = 45
-IPV6CTL_MAXID = 46
diff --git a/Lib/plat-freebsd7/regen b/Lib/plat-freebsd7/regen
deleted file mode 100755
index 8aa6898..0000000
--- a/Lib/plat-freebsd7/regen
+++ /dev/null
@@ -1,3 +0,0 @@
-#! /bin/sh
-set -v
-python ../../Tools/scripts/h2py.py -i '(u_long)' /usr/include/netinet/in.h
diff --git a/Lib/plat-freebsd8/IN.py b/Lib/plat-freebsd8/IN.py
deleted file mode 100644
index 4e3b3a2..0000000
--- a/Lib/plat-freebsd8/IN.py
+++ /dev/null
@@ -1,571 +0,0 @@
-# Generated by h2py from /usr/include/netinet/in.h
-
-# Included from sys/cdefs.h
-__GNUCLIKE_ASM = 3
-__GNUCLIKE_ASM = 2
-__GNUCLIKE___TYPEOF = 1
-__GNUCLIKE___OFFSETOF = 1
-__GNUCLIKE___SECTION = 1
-__GNUCLIKE_ATTRIBUTE_MODE_DI = 1
-__GNUCLIKE_CTOR_SECTION_HANDLING = 1
-__GNUCLIKE_BUILTIN_CONSTANT_P = 1
-__GNUCLIKE_BUILTIN_VARARGS = 1
-__GNUCLIKE_BUILTIN_STDARG = 1
-__GNUCLIKE_BUILTIN_VAALIST = 1
-__GNUC_VA_LIST_COMPATIBILITY = 1
-__GNUCLIKE_BUILTIN_NEXT_ARG = 1
-__GNUCLIKE_BUILTIN_MEMCPY = 1
-__CC_SUPPORTS_INLINE = 1
-__CC_SUPPORTS___INLINE = 1
-__CC_SUPPORTS___INLINE__ = 1
-__CC_SUPPORTS___FUNC__ = 1
-__CC_SUPPORTS_WARNING = 1
-__CC_SUPPORTS_VARADIC_XXX = 1
-__CC_SUPPORTS_DYNAMIC_ARRAY_INIT = 1
-__CC_INT_IS_32BIT = 1
-def __P(protos): return protos
-
-def __STRING(x): return #x
-
-def __XSTRING(x): return __STRING(x)
-
-def __P(protos): return ()
-
-def __STRING(x): return "x"
-
-def __aligned(x): return __attribute__((__aligned__(x)))
-
-def __section(x): return __attribute__((__section__(x)))
-
-def __aligned(x): return __attribute__((__aligned__(x)))
-
-def __section(x): return __attribute__((__section__(x)))
-
-def __nonnull(x): return __attribute__((__nonnull__(x)))
-
-def __predict_true(exp): return __builtin_expect((exp), 1)
-
-def __predict_false(exp): return __builtin_expect((exp), 0)
-
-def __predict_true(exp): return (exp)
-
-def __predict_false(exp): return (exp)
-
-def __format_arg(fmtarg): return __attribute__((__format_arg__ (fmtarg)))
-
-def __FBSDID(s): return __IDSTRING(__CONCAT(__rcsid_,__LINE__),s)
-
-def __RCSID(s): return __IDSTRING(__CONCAT(__rcsid_,__LINE__),s)
-
-def __RCSID_SOURCE(s): return __IDSTRING(__CONCAT(__rcsid_source_,__LINE__),s)
-
-def __SCCSID(s): return __IDSTRING(__CONCAT(__sccsid_,__LINE__),s)
-
-def __COPYRIGHT(s): return __IDSTRING(__CONCAT(__copyright_,__LINE__),s)
-
-_POSIX_C_SOURCE = 199009
-_POSIX_C_SOURCE = 199209
-__XSI_VISIBLE = 600
-_POSIX_C_SOURCE = 200112
-__XSI_VISIBLE = 500
-_POSIX_C_SOURCE = 199506
-_POSIX_C_SOURCE = 198808
-__POSIX_VISIBLE = 200112
-__ISO_C_VISIBLE = 1999
-__POSIX_VISIBLE = 199506
-__ISO_C_VISIBLE = 1990
-__POSIX_VISIBLE = 199309
-__ISO_C_VISIBLE = 1990
-__POSIX_VISIBLE = 199209
-__ISO_C_VISIBLE = 1990
-__POSIX_VISIBLE = 199009
-__ISO_C_VISIBLE = 1990
-__POSIX_VISIBLE = 198808
-__ISO_C_VISIBLE = 0
-__POSIX_VISIBLE = 0
-__XSI_VISIBLE = 0
-__BSD_VISIBLE = 0
-__ISO_C_VISIBLE = 1990
-__POSIX_VISIBLE = 0
-__XSI_VISIBLE = 0
-__BSD_VISIBLE = 0
-__ISO_C_VISIBLE = 1999
-__POSIX_VISIBLE = 200112
-__XSI_VISIBLE = 600
-__BSD_VISIBLE = 1
-__ISO_C_VISIBLE = 1999
-
-# Included from sys/_types.h
-
-# Included from machine/_types.h
-
-# Included from machine/endian.h
-_QUAD_HIGHWORD = 1
-_QUAD_LOWWORD = 0
-_LITTLE_ENDIAN = 1234
-_BIG_ENDIAN = 4321
-_PDP_ENDIAN = 3412
-_BYTE_ORDER = _LITTLE_ENDIAN
-LITTLE_ENDIAN = _LITTLE_ENDIAN
-BIG_ENDIAN = _BIG_ENDIAN
-PDP_ENDIAN = _PDP_ENDIAN
-BYTE_ORDER = _BYTE_ORDER
-def __word_swap_int_var(x): return \
-
-def __word_swap_int_const(x): return \
-
-def __word_swap_int(x): return __word_swap_int_var(x)
-
-def __byte_swap_int_var(x): return \
-
-def __byte_swap_int_const(x): return \
-
-def __byte_swap_int(x): return __byte_swap_int_var(x)
-
-def __byte_swap_word_var(x): return \
-
-def __byte_swap_word_const(x): return \
-
-def __byte_swap_word(x): return __byte_swap_word_var(x)
-
-def __htonl(x): return __bswap32(x)
-
-def __htons(x): return __bswap16(x)
-
-def __ntohl(x): return __bswap32(x)
-
-def __ntohs(x): return __bswap16(x)
-
-IPPROTO_IP = 0
-IPPROTO_ICMP = 1
-IPPROTO_TCP = 6
-IPPROTO_UDP = 17
-def htonl(x): return __htonl(x)
-
-def htons(x): return __htons(x)
-
-def ntohl(x): return __ntohl(x)
-
-def ntohs(x): return __ntohs(x)
-
-IPPROTO_RAW = 255
-INET_ADDRSTRLEN = 16
-IPPROTO_HOPOPTS = 0
-IPPROTO_IGMP = 2
-IPPROTO_GGP = 3
-IPPROTO_IPV4 = 4
-IPPROTO_IPIP = IPPROTO_IPV4
-IPPROTO_ST = 7
-IPPROTO_EGP = 8
-IPPROTO_PIGP = 9
-IPPROTO_RCCMON = 10
-IPPROTO_NVPII = 11
-IPPROTO_PUP = 12
-IPPROTO_ARGUS = 13
-IPPROTO_EMCON = 14
-IPPROTO_XNET = 15
-IPPROTO_CHAOS = 16
-IPPROTO_MUX = 18
-IPPROTO_MEAS = 19
-IPPROTO_HMP = 20
-IPPROTO_PRM = 21
-IPPROTO_IDP = 22
-IPPROTO_TRUNK1 = 23
-IPPROTO_TRUNK2 = 24
-IPPROTO_LEAF1 = 25
-IPPROTO_LEAF2 = 26
-IPPROTO_RDP = 27
-IPPROTO_IRTP = 28
-IPPROTO_TP = 29
-IPPROTO_BLT = 30
-IPPROTO_NSP = 31
-IPPROTO_INP = 32
-IPPROTO_SEP = 33
-IPPROTO_3PC = 34
-IPPROTO_IDPR = 35
-IPPROTO_XTP = 36
-IPPROTO_DDP = 37
-IPPROTO_CMTP = 38
-IPPROTO_TPXX = 39
-IPPROTO_IL = 40
-IPPROTO_IPV6 = 41
-IPPROTO_SDRP = 42
-IPPROTO_ROUTING = 43
-IPPROTO_FRAGMENT = 44
-IPPROTO_IDRP = 45
-IPPROTO_RSVP = 46
-IPPROTO_GRE = 47
-IPPROTO_MHRP = 48
-IPPROTO_BHA = 49
-IPPROTO_ESP = 50
-IPPROTO_AH = 51
-IPPROTO_INLSP = 52
-IPPROTO_SWIPE = 53
-IPPROTO_NHRP = 54
-IPPROTO_MOBILE = 55
-IPPROTO_TLSP = 56
-IPPROTO_SKIP = 57
-IPPROTO_ICMPV6 = 58
-IPPROTO_NONE = 59
-IPPROTO_DSTOPTS = 60
-IPPROTO_AHIP = 61
-IPPROTO_CFTP = 62
-IPPROTO_HELLO = 63
-IPPROTO_SATEXPAK = 64
-IPPROTO_KRYPTOLAN = 65
-IPPROTO_RVD = 66
-IPPROTO_IPPC = 67
-IPPROTO_ADFS = 68
-IPPROTO_SATMON = 69
-IPPROTO_VISA = 70
-IPPROTO_IPCV = 71
-IPPROTO_CPNX = 72
-IPPROTO_CPHB = 73
-IPPROTO_WSN = 74
-IPPROTO_PVP = 75
-IPPROTO_BRSATMON = 76
-IPPROTO_ND = 77
-IPPROTO_WBMON = 78
-IPPROTO_WBEXPAK = 79
-IPPROTO_EON = 80
-IPPROTO_VMTP = 81
-IPPROTO_SVMTP = 82
-IPPROTO_VINES = 83
-IPPROTO_TTP = 84
-IPPROTO_IGP = 85
-IPPROTO_DGP = 86
-IPPROTO_TCF = 87
-IPPROTO_IGRP = 88
-IPPROTO_OSPFIGP = 89
-IPPROTO_SRPC = 90
-IPPROTO_LARP = 91
-IPPROTO_MTP = 92
-IPPROTO_AX25 = 93
-IPPROTO_IPEIP = 94
-IPPROTO_MICP = 95
-IPPROTO_SCCSP = 96
-IPPROTO_ETHERIP = 97
-IPPROTO_ENCAP = 98
-IPPROTO_APES = 99
-IPPROTO_GMTP = 100
-IPPROTO_IPCOMP = 108
-IPPROTO_SCTP = 132
-IPPROTO_PIM = 103
-IPPROTO_CARP = 112
-IPPROTO_PGM = 113
-IPPROTO_PFSYNC = 240
-IPPROTO_OLD_DIVERT = 254
-IPPROTO_MAX = 256
-IPPROTO_DONE = 257
-IPPROTO_DIVERT = 258
-IPPROTO_SPACER = 32767
-IPPORT_RESERVED = 1024
-IPPORT_HIFIRSTAUTO = 49152
-IPPORT_HILASTAUTO = 65535
-IPPORT_RESERVEDSTART = 600
-IPPORT_MAX = 65535
-def IN_CLASSA(i): return (((u_int32_t)(i) & (-2147483648)) == 0)
-
-IN_CLASSA_NET = (-16777216)
-IN_CLASSA_NSHIFT = 24
-IN_CLASSA_HOST = 0x00ffffff
-IN_CLASSA_MAX = 128
-def IN_CLASSB(i): return (((u_int32_t)(i) & (-1073741824)) == (-2147483648))
-
-IN_CLASSB_NET = (-65536)
-IN_CLASSB_NSHIFT = 16
-IN_CLASSB_HOST = 0x0000ffff
-IN_CLASSB_MAX = 65536
-def IN_CLASSC(i): return (((u_int32_t)(i) & (-536870912)) == (-1073741824))
-
-IN_CLASSC_NET = (-256)
-IN_CLASSC_NSHIFT = 8
-IN_CLASSC_HOST = 0x000000ff
-def IN_CLASSD(i): return (((u_int32_t)(i) & (-268435456)) == (-536870912))
-
-IN_CLASSD_NET = (-268435456)
-IN_CLASSD_NSHIFT = 28
-IN_CLASSD_HOST = 0x0fffffff
-def IN_MULTICAST(i): return IN_CLASSD(i)
-
-def IN_EXPERIMENTAL(i): return (((u_int32_t)(i) & (-268435456)) == (-268435456))
-
-def IN_BADCLASS(i): return (((u_int32_t)(i) & (-268435456)) == (-268435456))
-
-def IN_LINKLOCAL(i): return (((u_int32_t)(i) & (-65536)) == (-1442971648))
-
-def IN_LOCAL_GROUP(i): return (((u_int32_t)(i) & (-256)) == (-536870912))
-
-INADDR_NONE = (-1)
-IN_LOOPBACKNET = 127
-IP_OPTIONS = 1
-IP_HDRINCL = 2
-IP_TOS = 3
-IP_TTL = 4
-IP_RECVOPTS = 5
-IP_RECVRETOPTS = 6
-IP_RECVDSTADDR = 7
-IP_SENDSRCADDR = IP_RECVDSTADDR
-IP_RETOPTS = 8
-IP_MULTICAST_IF = 9
-IP_MULTICAST_TTL = 10
-IP_MULTICAST_LOOP = 11
-IP_ADD_MEMBERSHIP = 12
-IP_DROP_MEMBERSHIP = 13
-IP_MULTICAST_VIF = 14
-IP_RSVP_ON = 15
-IP_RSVP_OFF = 16
-IP_RSVP_VIF_ON = 17
-IP_RSVP_VIF_OFF = 18
-IP_PORTRANGE = 19
-IP_RECVIF = 20
-IP_IPSEC_POLICY = 21
-IP_FAITH = 22
-IP_ONESBCAST = 23
-IP_FW_TABLE_ADD = 40
-IP_FW_TABLE_DEL = 41
-IP_FW_TABLE_FLUSH = 42
-IP_FW_TABLE_GETSIZE = 43
-IP_FW_TABLE_LIST = 44
-IP_FW_ADD = 50
-IP_FW_DEL = 51
-IP_FW_FLUSH = 52
-IP_FW_ZERO = 53
-IP_FW_GET = 54
-IP_FW_RESETLOG = 55
-IP_FW_NAT_CFG = 56
-IP_FW_NAT_DEL = 57
-IP_FW_NAT_GET_CONFIG = 58
-IP_FW_NAT_GET_LOG = 59
-IP_DUMMYNET_CONFIGURE = 60
-IP_DUMMYNET_DEL = 61
-IP_DUMMYNET_FLUSH = 62
-IP_DUMMYNET_GET = 64
-IP_RECVTTL = 65
-IP_MINTTL = 66
-IP_DONTFRAG = 67
-IP_ADD_SOURCE_MEMBERSHIP = 70
-IP_DROP_SOURCE_MEMBERSHIP = 71
-IP_BLOCK_SOURCE = 72
-IP_UNBLOCK_SOURCE = 73
-IP_MSFILTER = 74
-MCAST_JOIN_GROUP = 80
-MCAST_LEAVE_GROUP = 81
-MCAST_JOIN_SOURCE_GROUP = 82
-MCAST_LEAVE_SOURCE_GROUP = 83
-MCAST_BLOCK_SOURCE = 84
-MCAST_UNBLOCK_SOURCE = 85
-IP_DEFAULT_MULTICAST_TTL = 1
-IP_DEFAULT_MULTICAST_LOOP = 1
-IP_MIN_MEMBERSHIPS = 31
-IP_MAX_MEMBERSHIPS = 4095
-IP_MAX_SOURCE_FILTER = 1024
-MCAST_INCLUDE = 1
-MCAST_EXCLUDE = 2
-IP_PORTRANGE_DEFAULT = 0
-IP_PORTRANGE_HIGH = 1
-IP_PORTRANGE_LOW = 2
-IPPROTO_MAXID = (IPPROTO_AH + 1)
-IPCTL_FORWARDING = 1
-IPCTL_SENDREDIRECTS = 2
-IPCTL_DEFTTL = 3
-IPCTL_DEFMTU = 4
-IPCTL_RTEXPIRE = 5
-IPCTL_RTMINEXPIRE = 6
-IPCTL_RTMAXCACHE = 7
-IPCTL_SOURCEROUTE = 8
-IPCTL_DIRECTEDBROADCAST = 9
-IPCTL_INTRQMAXLEN = 10
-IPCTL_INTRQDROPS = 11
-IPCTL_STATS = 12
-IPCTL_ACCEPTSOURCEROUTE = 13
-IPCTL_FASTFORWARDING = 14
-IPCTL_KEEPFAITH = 15
-IPCTL_GIF_TTL = 16
-IPCTL_MAXID = 17
-def in_nullhost(x): return ((x).s_addr == INADDR_ANY)
-
-
-# Included from netinet6/in6.h
-__KAME_VERSION = "FreeBSD"
-IPV6PORT_RESERVED = 1024
-IPV6PORT_ANONMIN = 49152
-IPV6PORT_ANONMAX = 65535
-IPV6PORT_RESERVEDMIN = 600
-IPV6PORT_RESERVEDMAX = (IPV6PORT_RESERVED-1)
-INET6_ADDRSTRLEN = 46
-IPV6_ADDR_INT32_ONE = 1
-IPV6_ADDR_INT32_TWO = 2
-IPV6_ADDR_INT32_MNL = (-16711680)
-IPV6_ADDR_INT32_MLL = (-16646144)
-IPV6_ADDR_INT32_SMP = 0x0000ffff
-IPV6_ADDR_INT16_ULL = 0xfe80
-IPV6_ADDR_INT16_USL = 0xfec0
-IPV6_ADDR_INT16_MLL = 0xff02
-IPV6_ADDR_INT32_ONE = 0x01000000
-IPV6_ADDR_INT32_TWO = 0x02000000
-IPV6_ADDR_INT32_MNL = 0x000001ff
-IPV6_ADDR_INT32_MLL = 0x000002ff
-IPV6_ADDR_INT32_SMP = (-65536)
-IPV6_ADDR_INT16_ULL = 0x80fe
-IPV6_ADDR_INT16_USL = 0xc0fe
-IPV6_ADDR_INT16_MLL = 0x02ff
-def IN6_IS_ADDR_UNSPECIFIED(a): return \
-
-def IN6_IS_ADDR_LOOPBACK(a): return \
-
-def IN6_IS_ADDR_V4COMPAT(a): return \
-
-def IN6_IS_ADDR_V4MAPPED(a): return \
-
-IPV6_ADDR_SCOPE_NODELOCAL = 0x01
-IPV6_ADDR_SCOPE_INTFACELOCAL = 0x01
-IPV6_ADDR_SCOPE_LINKLOCAL = 0x02
-IPV6_ADDR_SCOPE_SITELOCAL = 0x05
-IPV6_ADDR_SCOPE_ORGLOCAL = 0x08
-IPV6_ADDR_SCOPE_GLOBAL = 0x0e
-__IPV6_ADDR_SCOPE_NODELOCAL = 0x01
-__IPV6_ADDR_SCOPE_INTFACELOCAL = 0x01
-__IPV6_ADDR_SCOPE_LINKLOCAL = 0x02
-__IPV6_ADDR_SCOPE_SITELOCAL = 0x05
-__IPV6_ADDR_SCOPE_ORGLOCAL = 0x08
-__IPV6_ADDR_SCOPE_GLOBAL = 0x0e
-def IN6_IS_ADDR_LINKLOCAL(a): return \
-
-def IN6_IS_ADDR_SITELOCAL(a): return \
-
-def IN6_IS_ADDR_MC_NODELOCAL(a): return \
-
-def IN6_IS_ADDR_MC_INTFACELOCAL(a): return \
-
-def IN6_IS_ADDR_MC_LINKLOCAL(a): return \
-
-def IN6_IS_ADDR_MC_SITELOCAL(a): return \
-
-def IN6_IS_ADDR_MC_ORGLOCAL(a): return \
-
-def IN6_IS_ADDR_MC_GLOBAL(a): return \
-
-def IN6_IS_ADDR_MC_NODELOCAL(a): return \
-
-def IN6_IS_ADDR_MC_LINKLOCAL(a): return \
-
-def IN6_IS_ADDR_MC_SITELOCAL(a): return \
-
-def IN6_IS_ADDR_MC_ORGLOCAL(a): return \
-
-def IN6_IS_ADDR_MC_GLOBAL(a): return \
-
-def IN6_IS_SCOPE_LINKLOCAL(a): return \
-
-def IN6_IS_SCOPE_EMBED(a): return \
-
-def IFA6_IS_DEPRECATED(a): return \
-
-def IFA6_IS_INVALID(a): return \
-
-IPV6_OPTIONS = 1
-IPV6_RECVOPTS = 5
-IPV6_RECVRETOPTS = 6
-IPV6_RECVDSTADDR = 7
-IPV6_RETOPTS = 8
-IPV6_SOCKOPT_RESERVED1 = 3
-IPV6_UNICAST_HOPS = 4
-IPV6_MULTICAST_IF = 9
-IPV6_MULTICAST_HOPS = 10
-IPV6_MULTICAST_LOOP = 11
-IPV6_JOIN_GROUP = 12
-IPV6_LEAVE_GROUP = 13
-IPV6_PORTRANGE = 14
-ICMP6_FILTER = 18
-IPV6_2292PKTINFO = 19
-IPV6_2292HOPLIMIT = 20
-IPV6_2292NEXTHOP = 21
-IPV6_2292HOPOPTS = 22
-IPV6_2292DSTOPTS = 23
-IPV6_2292RTHDR = 24
-IPV6_2292PKTOPTIONS = 25
-IPV6_CHECKSUM = 26
-IPV6_V6ONLY = 27
-IPV6_BINDV6ONLY = IPV6_V6ONLY
-IPV6_IPSEC_POLICY = 28
-IPV6_FAITH = 29
-IPV6_FW_ADD = 30
-IPV6_FW_DEL = 31
-IPV6_FW_FLUSH = 32
-IPV6_FW_ZERO = 33
-IPV6_FW_GET = 34
-IPV6_RTHDRDSTOPTS = 35
-IPV6_RECVPKTINFO = 36
-IPV6_RECVHOPLIMIT = 37
-IPV6_RECVRTHDR = 38
-IPV6_RECVHOPOPTS = 39
-IPV6_RECVDSTOPTS = 40
-IPV6_RECVRTHDRDSTOPTS = 41
-IPV6_USE_MIN_MTU = 42
-IPV6_RECVPATHMTU = 43
-IPV6_PATHMTU = 44
-IPV6_REACHCONF = 45
-IPV6_PKTINFO = 46
-IPV6_HOPLIMIT = 47
-IPV6_NEXTHOP = 48
-IPV6_HOPOPTS = 49
-IPV6_DSTOPTS = 50
-IPV6_RTHDR = 51
-IPV6_PKTOPTIONS = 52
-IPV6_RECVTCLASS = 57
-IPV6_AUTOFLOWLABEL = 59
-IPV6_TCLASS = 61
-IPV6_DONTFRAG = 62
-IPV6_PREFER_TEMPADDR = 63
-IPV6_MSFILTER = 74
-IPV6_RTHDR_LOOSE = 0
-IPV6_RTHDR_STRICT = 1
-IPV6_RTHDR_TYPE_0 = 0
-IPV6_DEFAULT_MULTICAST_HOPS = 1
-IPV6_DEFAULT_MULTICAST_LOOP = 1
-IPV6_PORTRANGE_DEFAULT = 0
-IPV6_PORTRANGE_HIGH = 1
-IPV6_PORTRANGE_LOW = 2
-IPV6PROTO_MAXID = (IPPROTO_PIM + 1)
-IPV6CTL_FORWARDING = 1
-IPV6CTL_SENDREDIRECTS = 2
-IPV6CTL_DEFHLIM = 3
-IPV6CTL_DEFMTU = 4
-IPV6CTL_FORWSRCRT = 5
-IPV6CTL_STATS = 6
-IPV6CTL_MRTSTATS = 7
-IPV6CTL_MRTPROTO = 8
-IPV6CTL_MAXFRAGPACKETS = 9
-IPV6CTL_SOURCECHECK = 10
-IPV6CTL_SOURCECHECK_LOGINT = 11
-IPV6CTL_ACCEPT_RTADV = 12
-IPV6CTL_KEEPFAITH = 13
-IPV6CTL_LOG_INTERVAL = 14
-IPV6CTL_HDRNESTLIMIT = 15
-IPV6CTL_DAD_COUNT = 16
-IPV6CTL_AUTO_FLOWLABEL = 17
-IPV6CTL_DEFMCASTHLIM = 18
-IPV6CTL_GIF_HLIM = 19
-IPV6CTL_KAME_VERSION = 20
-IPV6CTL_USE_DEPRECATED = 21
-IPV6CTL_RR_PRUNE = 22
-IPV6CTL_MAPPED_ADDR = 23
-IPV6CTL_V6ONLY = 24
-IPV6CTL_RTEXPIRE = 25
-IPV6CTL_RTMINEXPIRE = 26
-IPV6CTL_RTMAXCACHE = 27
-IPV6CTL_USETEMPADDR = 32
-IPV6CTL_TEMPPLTIME = 33
-IPV6CTL_TEMPVLTIME = 34
-IPV6CTL_AUTO_LINKLOCAL = 35
-IPV6CTL_RIP6STATS = 36
-IPV6CTL_PREFER_TEMPADDR = 37
-IPV6CTL_ADDRCTLPOLICY = 38
-IPV6CTL_USE_DEFAULTZONE = 39
-IPV6CTL_MAXFRAGS = 41
-IPV6CTL_IFQ = 42
-IPV6CTL_ISATAPRTR = 43
-IPV6CTL_MCAST_PMTU = 44
-IPV6CTL_STEALTH = 45
-IPV6CTL_MAXID = 46
diff --git a/Lib/plat-freebsd8/regen b/Lib/plat-freebsd8/regen
deleted file mode 100755
index 8aa6898..0000000
--- a/Lib/plat-freebsd8/regen
+++ /dev/null
@@ -1,3 +0,0 @@
-#! /bin/sh
-set -v
-python ../../Tools/scripts/h2py.py -i '(u_long)' /usr/include/netinet/in.h
diff --git a/Lib/plat-generic/regen b/Lib/plat-generic/regen
deleted file mode 100755
index c96167d..0000000
--- a/Lib/plat-generic/regen
+++ /dev/null
@@ -1,3 +0,0 @@
-#! /bin/sh
-set -v
-eval $PYTHON_FOR_BUILD ../../Tools/scripts/h2py.py -i "'(u_long)'" /usr/include/netinet/in.h
diff --git a/Lib/plat-linux/CDROM.py b/Lib/plat-linux/CDROM.py
deleted file mode 100644
index 4340936..0000000
--- a/Lib/plat-linux/CDROM.py
+++ /dev/null
@@ -1,207 +0,0 @@
-# Generated by h2py from /usr/include/linux/cdrom.h
-
-CDROMPAUSE = 0x5301
-CDROMRESUME = 0x5302
-CDROMPLAYMSF = 0x5303
-CDROMPLAYTRKIND = 0x5304
-CDROMREADTOCHDR = 0x5305
-CDROMREADTOCENTRY = 0x5306
-CDROMSTOP = 0x5307
-CDROMSTART = 0x5308
-CDROMEJECT = 0x5309
-CDROMVOLCTRL = 0x530a
-CDROMSUBCHNL = 0x530b
-CDROMREADMODE2 = 0x530c
-CDROMREADMODE1 = 0x530d
-CDROMREADAUDIO = 0x530e
-CDROMEJECT_SW = 0x530f
-CDROMMULTISESSION = 0x5310
-CDROM_GET_MCN = 0x5311
-CDROM_GET_UPC = CDROM_GET_MCN
-CDROMRESET = 0x5312
-CDROMVOLREAD = 0x5313
-CDROMREADRAW = 0x5314
-CDROMREADCOOKED = 0x5315
-CDROMSEEK = 0x5316
-CDROMPLAYBLK = 0x5317
-CDROMREADALL = 0x5318
-CDROMGETSPINDOWN = 0x531d
-CDROMSETSPINDOWN = 0x531e
-CDROMCLOSETRAY = 0x5319
-CDROM_SET_OPTIONS = 0x5320
-CDROM_CLEAR_OPTIONS = 0x5321
-CDROM_SELECT_SPEED = 0x5322
-CDROM_SELECT_DISC = 0x5323
-CDROM_MEDIA_CHANGED = 0x5325
-CDROM_DRIVE_STATUS = 0x5326
-CDROM_DISC_STATUS = 0x5327
-CDROM_CHANGER_NSLOTS = 0x5328
-CDROM_LOCKDOOR = 0x5329
-CDROM_DEBUG = 0x5330
-CDROM_GET_CAPABILITY = 0x5331
-CDROMAUDIOBUFSIZ = 0x5382
-DVD_READ_STRUCT = 0x5390
-DVD_WRITE_STRUCT = 0x5391
-DVD_AUTH = 0x5392
-CDROM_SEND_PACKET = 0x5393
-CDROM_NEXT_WRITABLE = 0x5394
-CDROM_LAST_WRITTEN = 0x5395
-CDROM_PACKET_SIZE = 12
-CGC_DATA_UNKNOWN = 0
-CGC_DATA_WRITE = 1
-CGC_DATA_READ = 2
-CGC_DATA_NONE = 3
-CD_MINS = 74
-CD_SECS = 60
-CD_FRAMES = 75
-CD_SYNC_SIZE = 12
-CD_MSF_OFFSET = 150
-CD_CHUNK_SIZE = 24
-CD_NUM_OF_CHUNKS = 98
-CD_FRAMESIZE_SUB = 96
-CD_HEAD_SIZE = 4
-CD_SUBHEAD_SIZE = 8
-CD_EDC_SIZE = 4
-CD_ZERO_SIZE = 8
-CD_ECC_SIZE = 276
-CD_FRAMESIZE = 2048
-CD_FRAMESIZE_RAW = 2352
-CD_FRAMESIZE_RAWER = 2646
-CD_FRAMESIZE_RAW1 = (CD_FRAMESIZE_RAW-CD_SYNC_SIZE)
-CD_FRAMESIZE_RAW0 = (CD_FRAMESIZE_RAW-CD_SYNC_SIZE-CD_HEAD_SIZE)
-CD_XA_HEAD = (CD_HEAD_SIZE+CD_SUBHEAD_SIZE)
-CD_XA_TAIL = (CD_EDC_SIZE+CD_ECC_SIZE)
-CD_XA_SYNC_HEAD = (CD_SYNC_SIZE+CD_XA_HEAD)
-CDROM_LBA = 0x01
-CDROM_MSF = 0x02
-CDROM_DATA_TRACK = 0x04
-CDROM_LEADOUT = 0xAA
-CDROM_AUDIO_INVALID = 0x00
-CDROM_AUDIO_PLAY = 0x11
-CDROM_AUDIO_PAUSED = 0x12
-CDROM_AUDIO_COMPLETED = 0x13
-CDROM_AUDIO_ERROR = 0x14
-CDROM_AUDIO_NO_STATUS = 0x15
-CDC_CLOSE_TRAY = 0x1
-CDC_OPEN_TRAY = 0x2
-CDC_LOCK = 0x4
-CDC_SELECT_SPEED = 0x8
-CDC_SELECT_DISC = 0x10
-CDC_MULTI_SESSION = 0x20
-CDC_MCN = 0x40
-CDC_MEDIA_CHANGED = 0x80
-CDC_PLAY_AUDIO = 0x100
-CDC_RESET = 0x200
-CDC_IOCTLS = 0x400
-CDC_DRIVE_STATUS = 0x800
-CDC_GENERIC_PACKET = 0x1000
-CDC_CD_R = 0x2000
-CDC_CD_RW = 0x4000
-CDC_DVD = 0x8000
-CDC_DVD_R = 0x10000
-CDC_DVD_RAM = 0x20000
-CDS_NO_INFO = 0
-CDS_NO_DISC = 1
-CDS_TRAY_OPEN = 2
-CDS_DRIVE_NOT_READY = 3
-CDS_DISC_OK = 4
-CDS_AUDIO = 100
-CDS_DATA_1 = 101
-CDS_DATA_2 = 102
-CDS_XA_2_1 = 103
-CDS_XA_2_2 = 104
-CDS_MIXED = 105
-CDO_AUTO_CLOSE = 0x1
-CDO_AUTO_EJECT = 0x2
-CDO_USE_FFLAGS = 0x4
-CDO_LOCK = 0x8
-CDO_CHECK_TYPE = 0x10
-CD_PART_MAX = 64
-CD_PART_MASK = (CD_PART_MAX - 1)
-GPCMD_BLANK = 0xa1
-GPCMD_CLOSE_TRACK = 0x5b
-GPCMD_FLUSH_CACHE = 0x35
-GPCMD_FORMAT_UNIT = 0x04
-GPCMD_GET_CONFIGURATION = 0x46
-GPCMD_GET_EVENT_STATUS_NOTIFICATION = 0x4a
-GPCMD_GET_PERFORMANCE = 0xac
-GPCMD_INQUIRY = 0x12
-GPCMD_LOAD_UNLOAD = 0xa6
-GPCMD_MECHANISM_STATUS = 0xbd
-GPCMD_MODE_SELECT_10 = 0x55
-GPCMD_MODE_SENSE_10 = 0x5a
-GPCMD_PAUSE_RESUME = 0x4b
-GPCMD_PLAY_AUDIO_10 = 0x45
-GPCMD_PLAY_AUDIO_MSF = 0x47
-GPCMD_PLAY_AUDIO_TI = 0x48
-GPCMD_PLAY_CD = 0xbc
-GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL = 0x1e
-GPCMD_READ_10 = 0x28
-GPCMD_READ_12 = 0xa8
-GPCMD_READ_CDVD_CAPACITY = 0x25
-GPCMD_READ_CD = 0xbe
-GPCMD_READ_CD_MSF = 0xb9
-GPCMD_READ_DISC_INFO = 0x51
-GPCMD_READ_DVD_STRUCTURE = 0xad
-GPCMD_READ_FORMAT_CAPACITIES = 0x23
-GPCMD_READ_HEADER = 0x44
-GPCMD_READ_TRACK_RZONE_INFO = 0x52
-GPCMD_READ_SUBCHANNEL = 0x42
-GPCMD_READ_TOC_PMA_ATIP = 0x43
-GPCMD_REPAIR_RZONE_TRACK = 0x58
-GPCMD_REPORT_KEY = 0xa4
-GPCMD_REQUEST_SENSE = 0x03
-GPCMD_RESERVE_RZONE_TRACK = 0x53
-GPCMD_SCAN = 0xba
-GPCMD_SEEK = 0x2b
-GPCMD_SEND_DVD_STRUCTURE = 0xad
-GPCMD_SEND_EVENT = 0xa2
-GPCMD_SEND_KEY = 0xa3
-GPCMD_SEND_OPC = 0x54
-GPCMD_SET_READ_AHEAD = 0xa7
-GPCMD_SET_STREAMING = 0xb6
-GPCMD_START_STOP_UNIT = 0x1b
-GPCMD_STOP_PLAY_SCAN = 0x4e
-GPCMD_TEST_UNIT_READY = 0x00
-GPCMD_VERIFY_10 = 0x2f
-GPCMD_WRITE_10 = 0x2a
-GPCMD_WRITE_AND_VERIFY_10 = 0x2e
-GPCMD_SET_SPEED = 0xbb
-GPCMD_PLAYAUDIO_TI = 0x48
-GPCMD_GET_MEDIA_STATUS = 0xda
-GPMODE_R_W_ERROR_PAGE = 0x01
-GPMODE_WRITE_PARMS_PAGE = 0x05
-GPMODE_AUDIO_CTL_PAGE = 0x0e
-GPMODE_POWER_PAGE = 0x1a
-GPMODE_FAULT_FAIL_PAGE = 0x1c
-GPMODE_TO_PROTECT_PAGE = 0x1d
-GPMODE_CAPABILITIES_PAGE = 0x2a
-GPMODE_ALL_PAGES = 0x3f
-GPMODE_CDROM_PAGE = 0x0d
-DVD_STRUCT_PHYSICAL = 0x00
-DVD_STRUCT_COPYRIGHT = 0x01
-DVD_STRUCT_DISCKEY = 0x02
-DVD_STRUCT_BCA = 0x03
-DVD_STRUCT_MANUFACT = 0x04
-DVD_LAYERS = 4
-DVD_LU_SEND_AGID = 0
-DVD_HOST_SEND_CHALLENGE = 1
-DVD_LU_SEND_KEY1 = 2
-DVD_LU_SEND_CHALLENGE = 3
-DVD_HOST_SEND_KEY2 = 4
-DVD_AUTH_ESTABLISHED = 5
-DVD_AUTH_FAILURE = 6
-DVD_LU_SEND_TITLE_KEY = 7
-DVD_LU_SEND_ASF = 8
-DVD_INVALIDATE_AGID = 9
-DVD_LU_SEND_RPC_STATE = 10
-DVD_HOST_SEND_RPC_STATE = 11
-DVD_CPM_NO_COPYRIGHT = 0
-DVD_CPM_COPYRIGHTED = 1
-DVD_CP_SEC_NONE = 0
-DVD_CP_SEC_EXIST = 1
-DVD_CGMS_UNRESTRICTED = 0
-DVD_CGMS_SINGLE = 2
-DVD_CGMS_RESTRICTED = 3
-
-CDROM_MAX_SLOTS = 256
diff --git a/Lib/plat-linux/DLFCN.py b/Lib/plat-linux/DLFCN.py
deleted file mode 100644
index dd10ac4..0000000
--- a/Lib/plat-linux/DLFCN.py
+++ /dev/null
@@ -1,83 +0,0 @@
-# Generated by h2py from /usr/include/dlfcn.h
-_DLFCN_H = 1
-
-# Included from features.h
-_FEATURES_H = 1
-__USE_ANSI = 1
-__FAVOR_BSD = 1
-_ISOC99_SOURCE = 1
-_POSIX_SOURCE = 1
-_POSIX_C_SOURCE = 199506
-_XOPEN_SOURCE = 600
-_XOPEN_SOURCE_EXTENDED = 1
-_LARGEFILE64_SOURCE = 1
-_BSD_SOURCE = 1
-_SVID_SOURCE = 1
-_BSD_SOURCE = 1
-_SVID_SOURCE = 1
-__USE_ISOC99 = 1
-_POSIX_SOURCE = 1
-_POSIX_C_SOURCE = 2
-_POSIX_C_SOURCE = 199506
-__USE_POSIX = 1
-__USE_POSIX2 = 1
-__USE_POSIX199309 = 1
-__USE_POSIX199506 = 1
-__USE_XOPEN = 1
-__USE_XOPEN_EXTENDED = 1
-__USE_UNIX98 = 1
-_LARGEFILE_SOURCE = 1
-__USE_XOPEN2K = 1
-__USE_ISOC99 = 1
-__USE_XOPEN_EXTENDED = 1
-__USE_LARGEFILE = 1
-__USE_LARGEFILE64 = 1
-__USE_FILE_OFFSET64 = 1
-__USE_MISC = 1
-__USE_BSD = 1
-__USE_SVID = 1
-__USE_GNU = 1
-__USE_REENTRANT = 1
-__STDC_IEC_559__ = 1
-__STDC_IEC_559_COMPLEX__ = 1
-__STDC_ISO_10646__ = 200009
-__GNU_LIBRARY__ = 6
-__GLIBC__ = 2
-__GLIBC_MINOR__ = 2
-
-# Included from sys/cdefs.h
-_SYS_CDEFS_H = 1
-def __PMT(args): return args
-
-def __P(args): return args
-
-def __PMT(args): return args
-
-def __STRING(x): return #x
-
-__flexarr = []
-__flexarr = [0]
-__flexarr = []
-__flexarr = [1]
-def __ASMNAME(cname): return __ASMNAME2 (__USER_LABEL_PREFIX__, cname)
-
-def __attribute__(xyz): return
-
-def __attribute_format_arg__(x): return __attribute__ ((__format_arg__ (x)))
-
-def __attribute_format_arg__(x): return
-
-__USE_LARGEFILE = 1
-__USE_LARGEFILE64 = 1
-__USE_EXTERN_INLINES = 1
-
-# Included from gnu/stubs.h
-
-# Included from bits/dlfcn.h
-RTLD_LAZY = 0x00001
-RTLD_NOW = 0x00002
-RTLD_BINDING_MASK = 0x3
-RTLD_NOLOAD = 0x00004
-RTLD_GLOBAL = 0x00100
-RTLD_LOCAL = 0
-RTLD_NODELETE = 0x01000
diff --git a/Lib/plat-linux/IN.py b/Lib/plat-linux/IN.py
deleted file mode 100644
index d7d3002..0000000
--- a/Lib/plat-linux/IN.py
+++ /dev/null
@@ -1,615 +0,0 @@
-# Generated by h2py from /usr/include/netinet/in.h
-_NETINET_IN_H = 1
-
-# Included from features.h
-_FEATURES_H = 1
-__USE_ANSI = 1
-__FAVOR_BSD = 1
-_ISOC99_SOURCE = 1
-_POSIX_SOURCE = 1
-_POSIX_C_SOURCE = 199506
-_XOPEN_SOURCE = 600
-_XOPEN_SOURCE_EXTENDED = 1
-_LARGEFILE64_SOURCE = 1
-_BSD_SOURCE = 1
-_SVID_SOURCE = 1
-_BSD_SOURCE = 1
-_SVID_SOURCE = 1
-__USE_ISOC99 = 1
-_POSIX_SOURCE = 1
-_POSIX_C_SOURCE = 2
-_POSIX_C_SOURCE = 199506
-__USE_POSIX = 1
-__USE_POSIX2 = 1
-__USE_POSIX199309 = 1
-__USE_POSIX199506 = 1
-__USE_XOPEN = 1
-__USE_XOPEN_EXTENDED = 1
-__USE_UNIX98 = 1
-_LARGEFILE_SOURCE = 1
-__USE_XOPEN2K = 1
-__USE_ISOC99 = 1
-__USE_XOPEN_EXTENDED = 1
-__USE_LARGEFILE = 1
-__USE_LARGEFILE64 = 1
-__USE_FILE_OFFSET64 = 1
-__USE_MISC = 1
-__USE_BSD = 1
-__USE_SVID = 1
-__USE_GNU = 1
-__USE_REENTRANT = 1
-__STDC_IEC_559__ = 1
-__STDC_IEC_559_COMPLEX__ = 1
-__STDC_ISO_10646__ = 200009
-__GNU_LIBRARY__ = 6
-__GLIBC__ = 2
-__GLIBC_MINOR__ = 2
-
-# Included from sys/cdefs.h
-_SYS_CDEFS_H = 1
-def __PMT(args): return args
-
-def __P(args): return args
-
-def __PMT(args): return args
-
-def __STRING(x): return #x
-
-__flexarr = []
-__flexarr = [0]
-__flexarr = []
-__flexarr = [1]
-def __ASMNAME(cname): return __ASMNAME2 (__USER_LABEL_PREFIX__, cname)
-
-def __attribute__(xyz): return
-
-def __attribute_format_arg__(x): return __attribute__ ((__format_arg__ (x)))
-
-def __attribute_format_arg__(x): return
-
-__USE_LARGEFILE = 1
-__USE_LARGEFILE64 = 1
-__USE_EXTERN_INLINES = 1
-
-# Included from gnu/stubs.h
-
-# Included from stdint.h
-_STDINT_H = 1
-
-# Included from bits/wchar.h
-_BITS_WCHAR_H = 1
-__WCHAR_MIN = (-2147483647 - 1)
-__WCHAR_MAX = (2147483647)
-
-# Included from bits/wordsize.h
-__WORDSIZE = 32
-def __INT64_C(c): return c ## L
-
-def __UINT64_C(c): return c ## UL
-
-def __INT64_C(c): return c ## LL
-
-def __UINT64_C(c): return c ## ULL
-
-INT8_MIN = (-128)
-INT16_MIN = (-32767-1)
-INT32_MIN = (-2147483647-1)
-INT64_MIN = (-__INT64_C(9223372036854775807)-1)
-INT8_MAX = (127)
-INT16_MAX = (32767)
-INT32_MAX = (2147483647)
-INT64_MAX = (__INT64_C(9223372036854775807))
-UINT8_MAX = (255)
-UINT16_MAX = (65535)
-UINT64_MAX = (__UINT64_C(18446744073709551615))
-INT_LEAST8_MIN = (-128)
-INT_LEAST16_MIN = (-32767-1)
-INT_LEAST32_MIN = (-2147483647-1)
-INT_LEAST64_MIN = (-__INT64_C(9223372036854775807)-1)
-INT_LEAST8_MAX = (127)
-INT_LEAST16_MAX = (32767)
-INT_LEAST32_MAX = (2147483647)
-INT_LEAST64_MAX = (__INT64_C(9223372036854775807))
-UINT_LEAST8_MAX = (255)
-UINT_LEAST16_MAX = (65535)
-UINT_LEAST64_MAX = (__UINT64_C(18446744073709551615))
-INT_FAST8_MIN = (-128)
-INT_FAST16_MIN = (-9223372036854775807-1)
-INT_FAST32_MIN = (-9223372036854775807-1)
-INT_FAST16_MIN = (-2147483647-1)
-INT_FAST32_MIN = (-2147483647-1)
-INT_FAST64_MIN = (-__INT64_C(9223372036854775807)-1)
-INT_FAST8_MAX = (127)
-INT_FAST16_MAX = (9223372036854775807)
-INT_FAST32_MAX = (9223372036854775807)
-INT_FAST16_MAX = (2147483647)
-INT_FAST32_MAX = (2147483647)
-INT_FAST64_MAX = (__INT64_C(9223372036854775807))
-UINT_FAST8_MAX = (255)
-UINT_FAST64_MAX = (__UINT64_C(18446744073709551615))
-INTPTR_MIN = (-9223372036854775807-1)
-INTPTR_MAX = (9223372036854775807)
-INTPTR_MIN = (-2147483647-1)
-INTPTR_MAX = (2147483647)
-INTMAX_MIN = (-__INT64_C(9223372036854775807)-1)
-INTMAX_MAX = (__INT64_C(9223372036854775807))
-UINTMAX_MAX = (__UINT64_C(18446744073709551615))
-PTRDIFF_MIN = (-9223372036854775807-1)
-PTRDIFF_MAX = (9223372036854775807)
-PTRDIFF_MIN = (-2147483647-1)
-PTRDIFF_MAX = (2147483647)
-SIG_ATOMIC_MIN = (-2147483647-1)
-SIG_ATOMIC_MAX = (2147483647)
-WCHAR_MIN = __WCHAR_MIN
-WCHAR_MAX = __WCHAR_MAX
-def INT8_C(c): return c
-
-def INT16_C(c): return c
-
-def INT32_C(c): return c
-
-def INT64_C(c): return c ## L
-
-def INT64_C(c): return c ## LL
-
-def UINT8_C(c): return c ## U
-
-def UINT16_C(c): return c ## U
-
-def UINT32_C(c): return c ## U
-
-def UINT64_C(c): return c ## UL
-
-def UINT64_C(c): return c ## ULL
-
-def INTMAX_C(c): return c ## L
-
-def UINTMAX_C(c): return c ## UL
-
-def INTMAX_C(c): return c ## LL
-
-def UINTMAX_C(c): return c ## ULL
-
-
-# Included from bits/types.h
-_BITS_TYPES_H = 1
-__FD_SETSIZE = 1024
-
-# Included from bits/pthreadtypes.h
-_BITS_PTHREADTYPES_H = 1
-
-# Included from bits/sched.h
-SCHED_OTHER = 0
-SCHED_FIFO = 1
-SCHED_RR = 2
-CSIGNAL = 0x000000ff
-CLONE_VM = 0x00000100
-CLONE_FS = 0x00000200
-CLONE_FILES = 0x00000400
-CLONE_SIGHAND = 0x00000800
-CLONE_PID = 0x00001000
-CLONE_PTRACE = 0x00002000
-CLONE_VFORK = 0x00004000
-__defined_schedparam = 1
-def IN_CLASSA(a): return ((((in_addr_t)(a)) & (-2147483648)) == 0)
-
-IN_CLASSA_NET = (-16777216)
-IN_CLASSA_NSHIFT = 24
-IN_CLASSA_HOST = ((-1) & ~IN_CLASSA_NET)
-IN_CLASSA_MAX = 128
-def IN_CLASSB(a): return ((((in_addr_t)(a)) & (-1073741824)) == (-2147483648))
-
-IN_CLASSB_NET = (-65536)
-IN_CLASSB_NSHIFT = 16
-IN_CLASSB_HOST = ((-1) & ~IN_CLASSB_NET)
-IN_CLASSB_MAX = 65536
-def IN_CLASSC(a): return ((((in_addr_t)(a)) & (-536870912)) == (-1073741824))
-
-IN_CLASSC_NET = (-256)
-IN_CLASSC_NSHIFT = 8
-IN_CLASSC_HOST = ((-1) & ~IN_CLASSC_NET)
-def IN_CLASSD(a): return ((((in_addr_t)(a)) & (-268435456)) == (-536870912))
-
-def IN_MULTICAST(a): return IN_CLASSD(a)
-
-def IN_EXPERIMENTAL(a): return ((((in_addr_t)(a)) & (-536870912)) == (-536870912))
-
-def IN_BADCLASS(a): return ((((in_addr_t)(a)) & (-268435456)) == (-268435456))
-
-IN_LOOPBACKNET = 127
-INET_ADDRSTRLEN = 16
-INET6_ADDRSTRLEN = 46
-
-# Included from bits/socket.h
-
-# Included from limits.h
-_LIBC_LIMITS_H_ = 1
-MB_LEN_MAX = 16
-_LIMITS_H = 1
-CHAR_BIT = 8
-SCHAR_MIN = (-128)
-SCHAR_MAX = 127
-UCHAR_MAX = 255
-CHAR_MIN = 0
-CHAR_MAX = UCHAR_MAX
-CHAR_MIN = SCHAR_MIN
-CHAR_MAX = SCHAR_MAX
-SHRT_MIN = (-32768)
-SHRT_MAX = 32767
-USHRT_MAX = 65535
-INT_MAX = 2147483647
-LONG_MAX = 9223372036854775807
-LONG_MAX = 2147483647
-LONG_MIN = (-LONG_MAX - 1)
-
-# Included from bits/posix1_lim.h
-_BITS_POSIX1_LIM_H = 1
-_POSIX_AIO_LISTIO_MAX = 2
-_POSIX_AIO_MAX = 1
-_POSIX_ARG_MAX = 4096
-_POSIX_CHILD_MAX = 6
-_POSIX_DELAYTIMER_MAX = 32
-_POSIX_LINK_MAX = 8
-_POSIX_MAX_CANON = 255
-_POSIX_MAX_INPUT = 255
-_POSIX_MQ_OPEN_MAX = 8
-_POSIX_MQ_PRIO_MAX = 32
-_POSIX_NGROUPS_MAX = 0
-_POSIX_OPEN_MAX = 16
-_POSIX_FD_SETSIZE = _POSIX_OPEN_MAX
-_POSIX_NAME_MAX = 14
-_POSIX_PATH_MAX = 256
-_POSIX_PIPE_BUF = 512
-_POSIX_RTSIG_MAX = 8
-_POSIX_SEM_NSEMS_MAX = 256
-_POSIX_SEM_VALUE_MAX = 32767
-_POSIX_SIGQUEUE_MAX = 32
-_POSIX_SSIZE_MAX = 32767
-_POSIX_STREAM_MAX = 8
-_POSIX_TZNAME_MAX = 6
-_POSIX_QLIMIT = 1
-_POSIX_HIWAT = _POSIX_PIPE_BUF
-_POSIX_UIO_MAXIOV = 16
-_POSIX_TTY_NAME_MAX = 9
-_POSIX_TIMER_MAX = 32
-_POSIX_LOGIN_NAME_MAX = 9
-_POSIX_CLOCKRES_MIN = 20000000
-
-# Included from bits/local_lim.h
-
-# Included from linux/limits.h
-NR_OPEN = 1024
-NGROUPS_MAX = 32
-ARG_MAX = 131072
-CHILD_MAX = 999
-OPEN_MAX = 256
-LINK_MAX = 127
-MAX_CANON = 255
-MAX_INPUT = 255
-NAME_MAX = 255
-PATH_MAX = 4096
-PIPE_BUF = 4096
-RTSIG_MAX = 32
-_POSIX_THREAD_KEYS_MAX = 128
-PTHREAD_KEYS_MAX = 1024
-_POSIX_THREAD_DESTRUCTOR_ITERATIONS = 4
-PTHREAD_DESTRUCTOR_ITERATIONS = _POSIX_THREAD_DESTRUCTOR_ITERATIONS
-_POSIX_THREAD_THREADS_MAX = 64
-PTHREAD_THREADS_MAX = 1024
-AIO_PRIO_DELTA_MAX = 20
-PTHREAD_STACK_MIN = 16384
-TIMER_MAX = 256
-SSIZE_MAX = LONG_MAX
-NGROUPS_MAX = _POSIX_NGROUPS_MAX
-
-# Included from bits/posix2_lim.h
-_BITS_POSIX2_LIM_H = 1
-_POSIX2_BC_BASE_MAX = 99
-_POSIX2_BC_DIM_MAX = 2048
-_POSIX2_BC_SCALE_MAX = 99
-_POSIX2_BC_STRING_MAX = 1000
-_POSIX2_COLL_WEIGHTS_MAX = 2
-_POSIX2_EXPR_NEST_MAX = 32
-_POSIX2_LINE_MAX = 2048
-_POSIX2_RE_DUP_MAX = 255
-_POSIX2_CHARCLASS_NAME_MAX = 14
-BC_BASE_MAX = _POSIX2_BC_BASE_MAX
-BC_DIM_MAX = _POSIX2_BC_DIM_MAX
-BC_SCALE_MAX = _POSIX2_BC_SCALE_MAX
-BC_STRING_MAX = _POSIX2_BC_STRING_MAX
-COLL_WEIGHTS_MAX = 255
-EXPR_NEST_MAX = _POSIX2_EXPR_NEST_MAX
-LINE_MAX = _POSIX2_LINE_MAX
-CHARCLASS_NAME_MAX = 2048
-RE_DUP_MAX = (0x7fff)
-
-# Included from bits/xopen_lim.h
-_XOPEN_LIM_H = 1
-
-# Included from bits/stdio_lim.h
-L_tmpnam = 20
-TMP_MAX = 238328
-FILENAME_MAX = 4096
-L_ctermid = 9
-L_cuserid = 9
-FOPEN_MAX = 16
-IOV_MAX = 1024
-_XOPEN_IOV_MAX = _POSIX_UIO_MAXIOV
-NL_ARGMAX = _POSIX_ARG_MAX
-NL_LANGMAX = _POSIX2_LINE_MAX
-NL_MSGMAX = INT_MAX
-NL_NMAX = INT_MAX
-NL_SETMAX = INT_MAX
-NL_TEXTMAX = INT_MAX
-NZERO = 20
-WORD_BIT = 16
-WORD_BIT = 32
-WORD_BIT = 64
-WORD_BIT = 16
-WORD_BIT = 32
-WORD_BIT = 64
-WORD_BIT = 32
-LONG_BIT = 32
-LONG_BIT = 64
-LONG_BIT = 32
-LONG_BIT = 64
-LONG_BIT = 64
-LONG_BIT = 32
-from TYPES import *
-PF_UNSPEC = 0
-PF_LOCAL = 1
-PF_UNIX = PF_LOCAL
-PF_FILE = PF_LOCAL
-PF_INET = 2
-PF_AX25 = 3
-PF_IPX = 4
-PF_APPLETALK = 5
-PF_NETROM = 6
-PF_BRIDGE = 7
-PF_ATMPVC = 8
-PF_X25 = 9
-PF_INET6 = 10
-PF_ROSE = 11
-PF_DECnet = 12
-PF_NETBEUI = 13
-PF_SECURITY = 14
-PF_KEY = 15
-PF_NETLINK = 16
-PF_ROUTE = PF_NETLINK
-PF_PACKET = 17
-PF_ASH = 18
-PF_ECONET = 19
-PF_ATMSVC = 20
-PF_SNA = 22
-PF_IRDA = 23
-PF_PPPOX = 24
-PF_WANPIPE = 25
-PF_BLUETOOTH = 31
-PF_MAX = 32
-AF_UNSPEC = PF_UNSPEC
-AF_LOCAL = PF_LOCAL
-AF_UNIX = PF_UNIX
-AF_FILE = PF_FILE
-AF_INET = PF_INET
-AF_AX25 = PF_AX25
-AF_IPX = PF_IPX
-AF_APPLETALK = PF_APPLETALK
-AF_NETROM = PF_NETROM
-AF_BRIDGE = PF_BRIDGE
-AF_ATMPVC = PF_ATMPVC
-AF_X25 = PF_X25
-AF_INET6 = PF_INET6
-AF_ROSE = PF_ROSE
-AF_DECnet = PF_DECnet
-AF_NETBEUI = PF_NETBEUI
-AF_SECURITY = PF_SECURITY
-AF_KEY = PF_KEY
-AF_NETLINK = PF_NETLINK
-AF_ROUTE = PF_ROUTE
-AF_PACKET = PF_PACKET
-AF_ASH = PF_ASH
-AF_ECONET = PF_ECONET
-AF_ATMSVC = PF_ATMSVC
-AF_SNA = PF_SNA
-AF_IRDA = PF_IRDA
-AF_PPPOX = PF_PPPOX
-AF_WANPIPE = PF_WANPIPE
-AF_BLUETOOTH = PF_BLUETOOTH
-AF_MAX = PF_MAX
-SOL_RAW = 255
-SOL_DECNET = 261
-SOL_X25 = 262
-SOL_PACKET = 263
-SOL_ATM = 264
-SOL_AAL = 265
-SOL_IRDA = 266
-SOMAXCONN = 128
-
-# Included from bits/sockaddr.h
-_BITS_SOCKADDR_H = 1
-def __SOCKADDR_COMMON(sa_prefix): return \
-
-_SS_SIZE = 128
-def CMSG_FIRSTHDR(mhdr): return \
-
-
-# Included from asm/socket.h
-
-# Included from asm/sockios.h
-FIOSETOWN = 0x8901
-SIOCSPGRP = 0x8902
-FIOGETOWN = 0x8903
-SIOCGPGRP = 0x8904
-SIOCATMARK = 0x8905
-SIOCGSTAMP = 0x8906
-SOL_SOCKET = 1
-SO_DEBUG = 1
-SO_REUSEADDR = 2
-SO_TYPE = 3
-SO_ERROR = 4
-SO_DONTROUTE = 5
-SO_BROADCAST = 6
-SO_SNDBUF = 7
-SO_RCVBUF = 8
-SO_KEEPALIVE = 9
-SO_OOBINLINE = 10
-SO_NO_CHECK = 11
-SO_PRIORITY = 12
-SO_LINGER = 13
-SO_BSDCOMPAT = 14
-SO_PASSCRED = 16
-SO_PEERCRED = 17
-SO_RCVLOWAT = 18
-SO_SNDLOWAT = 19
-SO_RCVTIMEO = 20
-SO_SNDTIMEO = 21
-SO_SECURITY_AUTHENTICATION = 22
-SO_SECURITY_ENCRYPTION_TRANSPORT = 23
-SO_SECURITY_ENCRYPTION_NETWORK = 24
-SO_BINDTODEVICE = 25
-SO_ATTACH_FILTER = 26
-SO_DETACH_FILTER = 27
-SO_PEERNAME = 28
-SO_TIMESTAMP = 29
-SCM_TIMESTAMP = SO_TIMESTAMP
-SO_ACCEPTCONN = 30
-SOCK_STREAM = 1
-SOCK_DGRAM = 2
-SOCK_RAW = 3
-SOCK_RDM = 4
-SOCK_SEQPACKET = 5
-SOCK_PACKET = 10
-SOCK_MAX = (SOCK_PACKET+1)
-
-# Included from bits/in.h
-IP_TOS = 1
-IP_TTL = 2
-IP_HDRINCL = 3
-IP_OPTIONS = 4
-IP_ROUTER_ALERT = 5
-IP_RECVOPTS = 6
-IP_RETOPTS = 7
-IP_PKTINFO = 8
-IP_PKTOPTIONS = 9
-IP_PMTUDISC = 10
-IP_MTU_DISCOVER = 10
-IP_RECVERR = 11
-IP_RECVTTL = 12
-IP_RECVTOS = 13
-IP_MULTICAST_IF = 32
-IP_MULTICAST_TTL = 33
-IP_MULTICAST_LOOP = 34
-IP_ADD_MEMBERSHIP = 35
-IP_DROP_MEMBERSHIP = 36
-IP_RECVRETOPTS = IP_RETOPTS
-IP_PMTUDISC_DONT = 0
-IP_PMTUDISC_WANT = 1
-IP_PMTUDISC_DO = 2
-SOL_IP = 0
-IP_DEFAULT_MULTICAST_TTL = 1
-IP_DEFAULT_MULTICAST_LOOP = 1
-IP_MAX_MEMBERSHIPS = 20
-IPV6_ADDRFORM = 1
-IPV6_PKTINFO = 2
-IPV6_HOPOPTS = 3
-IPV6_DSTOPTS = 4
-IPV6_RTHDR = 5
-IPV6_PKTOPTIONS = 6
-IPV6_CHECKSUM = 7
-IPV6_HOPLIMIT = 8
-IPV6_NEXTHOP = 9
-IPV6_AUTHHDR = 10
-IPV6_UNICAST_HOPS = 16
-IPV6_MULTICAST_IF = 17
-IPV6_MULTICAST_HOPS = 18
-IPV6_MULTICAST_LOOP = 19
-IPV6_JOIN_GROUP = 20
-IPV6_LEAVE_GROUP = 21
-IPV6_ROUTER_ALERT = 22
-IPV6_MTU_DISCOVER = 23
-IPV6_MTU = 24
-IPV6_RECVERR = 25
-IPV6_RXHOPOPTS = IPV6_HOPOPTS
-IPV6_RXDSTOPTS = IPV6_DSTOPTS
-IPV6_ADD_MEMBERSHIP = IPV6_JOIN_GROUP
-IPV6_DROP_MEMBERSHIP = IPV6_LEAVE_GROUP
-IPV6_PMTUDISC_DONT = 0
-IPV6_PMTUDISC_WANT = 1
-IPV6_PMTUDISC_DO = 2
-SOL_IPV6 = 41
-SOL_ICMPV6 = 58
-IPV6_RTHDR_LOOSE = 0
-IPV6_RTHDR_STRICT = 1
-IPV6_RTHDR_TYPE_0 = 0
-
-# Included from endian.h
-_ENDIAN_H = 1
-__LITTLE_ENDIAN = 1234
-__BIG_ENDIAN = 4321
-__PDP_ENDIAN = 3412
-
-# Included from bits/endian.h
-__BYTE_ORDER = __LITTLE_ENDIAN
-__FLOAT_WORD_ORDER = __BYTE_ORDER
-LITTLE_ENDIAN = __LITTLE_ENDIAN
-BIG_ENDIAN = __BIG_ENDIAN
-PDP_ENDIAN = __PDP_ENDIAN
-BYTE_ORDER = __BYTE_ORDER
-
-# Included from bits/byteswap.h
-_BITS_BYTESWAP_H = 1
-def __bswap_constant_16(x): return \
-
-def __bswap_16(x): return \
-
-def __bswap_16(x): return __bswap_constant_16 (x)
-
-def __bswap_constant_32(x): return \
-
-def __bswap_32(x): return \
-
-def __bswap_32(x): return \
-
-def __bswap_32(x): return __bswap_constant_32 (x)
-
-def __bswap_constant_64(x): return \
-
-def __bswap_64(x): return \
-
-def ntohl(x): return (x)
-
-def ntohs(x): return (x)
-
-def htonl(x): return (x)
-
-def htons(x): return (x)
-
-def ntohl(x): return __bswap_32 (x)
-
-def ntohs(x): return __bswap_16 (x)
-
-def htonl(x): return __bswap_32 (x)
-
-def htons(x): return __bswap_16 (x)
-
-def IN6_IS_ADDR_UNSPECIFIED(a): return \
-
-def IN6_IS_ADDR_LOOPBACK(a): return \
-
-def IN6_IS_ADDR_LINKLOCAL(a): return \
-
-def IN6_IS_ADDR_SITELOCAL(a): return \
-
-def IN6_IS_ADDR_V4MAPPED(a): return \
-
-def IN6_IS_ADDR_V4COMPAT(a): return \
-
-def IN6_IS_ADDR_MC_NODELOCAL(a): return \
-
-def IN6_IS_ADDR_MC_LINKLOCAL(a): return \
-
-def IN6_IS_ADDR_MC_SITELOCAL(a): return \
-
-def IN6_IS_ADDR_MC_ORGLOCAL(a): return \
-
-def IN6_IS_ADDR_MC_GLOBAL(a): return
diff --git a/Lib/plat-linux/TYPES.py b/Lib/plat-linux/TYPES.py
deleted file mode 100644
index e7a324b..0000000
--- a/Lib/plat-linux/TYPES.py
+++ /dev/null
@@ -1,170 +0,0 @@
-# Generated by h2py from /usr/include/sys/types.h
-_SYS_TYPES_H = 1
-
-# Included from features.h
-_FEATURES_H = 1
-__USE_ANSI = 1
-__FAVOR_BSD = 1
-_ISOC99_SOURCE = 1
-_POSIX_SOURCE = 1
-_POSIX_C_SOURCE = 199506
-_XOPEN_SOURCE = 600
-_XOPEN_SOURCE_EXTENDED = 1
-_LARGEFILE64_SOURCE = 1
-_BSD_SOURCE = 1
-_SVID_SOURCE = 1
-_BSD_SOURCE = 1
-_SVID_SOURCE = 1
-__USE_ISOC99 = 1
-_POSIX_SOURCE = 1
-_POSIX_C_SOURCE = 2
-_POSIX_C_SOURCE = 199506
-__USE_POSIX = 1
-__USE_POSIX2 = 1
-__USE_POSIX199309 = 1
-__USE_POSIX199506 = 1
-__USE_XOPEN = 1
-__USE_XOPEN_EXTENDED = 1
-__USE_UNIX98 = 1
-_LARGEFILE_SOURCE = 1
-__USE_XOPEN2K = 1
-__USE_ISOC99 = 1
-__USE_XOPEN_EXTENDED = 1
-__USE_LARGEFILE = 1
-__USE_LARGEFILE64 = 1
-__USE_FILE_OFFSET64 = 1
-__USE_MISC = 1
-__USE_BSD = 1
-__USE_SVID = 1
-__USE_GNU = 1
-__USE_REENTRANT = 1
-__STDC_IEC_559__ = 1
-__STDC_IEC_559_COMPLEX__ = 1
-__STDC_ISO_10646__ = 200009
-__GNU_LIBRARY__ = 6
-__GLIBC__ = 2
-__GLIBC_MINOR__ = 2
-
-# Included from sys/cdefs.h
-_SYS_CDEFS_H = 1
-def __PMT(args): return args
-
-def __P(args): return args
-
-def __PMT(args): return args
-
-def __STRING(x): return #x
-
-__flexarr = []
-__flexarr = [0]
-__flexarr = []
-__flexarr = [1]
-def __ASMNAME(cname): return __ASMNAME2 (__USER_LABEL_PREFIX__, cname)
-
-def __attribute__(xyz): return
-
-def __attribute_format_arg__(x): return __attribute__ ((__format_arg__ (x)))
-
-def __attribute_format_arg__(x): return
-
-__USE_LARGEFILE = 1
-__USE_LARGEFILE64 = 1
-__USE_EXTERN_INLINES = 1
-
-# Included from gnu/stubs.h
-
-# Included from bits/types.h
-_BITS_TYPES_H = 1
-__FD_SETSIZE = 1024
-
-# Included from bits/pthreadtypes.h
-_BITS_PTHREADTYPES_H = 1
-
-# Included from bits/sched.h
-SCHED_OTHER = 0
-SCHED_FIFO = 1
-SCHED_RR = 2
-CSIGNAL = 0x000000ff
-CLONE_VM = 0x00000100
-CLONE_FS = 0x00000200
-CLONE_FILES = 0x00000400
-CLONE_SIGHAND = 0x00000800
-CLONE_PID = 0x00001000
-CLONE_PTRACE = 0x00002000
-CLONE_VFORK = 0x00004000
-__defined_schedparam = 1
-
-# Included from time.h
-_TIME_H = 1
-
-# Included from bits/time.h
-_BITS_TIME_H = 1
-CLOCKS_PER_SEC = 1000000
-CLOCK_REALTIME = 0
-CLOCK_PROCESS_CPUTIME_ID = 2
-CLOCK_THREAD_CPUTIME_ID = 3
-TIMER_ABSTIME = 1
-_STRUCT_TIMEVAL = 1
-CLK_TCK = CLOCKS_PER_SEC
-__clock_t_defined = 1
-__time_t_defined = 1
-__clockid_t_defined = 1
-__timer_t_defined = 1
-__timespec_defined = 1
-def __isleap(year): return \
-
-__BIT_TYPES_DEFINED__ = 1
-
-# Included from endian.h
-_ENDIAN_H = 1
-__LITTLE_ENDIAN = 1234
-__BIG_ENDIAN = 4321
-__PDP_ENDIAN = 3412
-
-# Included from bits/endian.h
-__BYTE_ORDER = __LITTLE_ENDIAN
-__FLOAT_WORD_ORDER = __BYTE_ORDER
-LITTLE_ENDIAN = __LITTLE_ENDIAN
-BIG_ENDIAN = __BIG_ENDIAN
-PDP_ENDIAN = __PDP_ENDIAN
-BYTE_ORDER = __BYTE_ORDER
-
-# Included from sys/select.h
-_SYS_SELECT_H = 1
-
-# Included from bits/select.h
-def __FD_ZERO(fdsp): return \
-
-def __FD_ZERO(set): return \
-
-
-# Included from bits/sigset.h
-_SIGSET_H_types = 1
-_SIGSET_H_fns = 1
-def __sigmask(sig): return \
-
-def __sigemptyset(set): return \
-
-def __sigfillset(set): return \
-
-def __sigisemptyset(set): return \
-
-def __FDELT(d): return ((d) / __NFDBITS)
-
-FD_SETSIZE = __FD_SETSIZE
-def FD_ZERO(fdsetp): return __FD_ZERO (fdsetp)
-
-
-# Included from sys/sysmacros.h
-_SYS_SYSMACROS_H = 1
-def major(dev): return ((int)(((dev) >> 8) & 0xff))
-
-def minor(dev): return ((int)((dev) & 0xff))
-
-def major(dev): return (((dev).__val[1] >> 8) & 0xff)
-
-def minor(dev): return ((dev).__val[1] & 0xff)
-
-def major(dev): return (((dev).__val[0] >> 8) & 0xff)
-
-def minor(dev): return ((dev).__val[0] & 0xff)
diff --git a/Lib/plat-linux/regen b/Lib/plat-linux/regen
deleted file mode 100755
index c76950e..0000000
--- a/Lib/plat-linux/regen
+++ /dev/null
@@ -1,8 +0,0 @@
-#! /bin/sh
-case `uname` in
-Linux*) ;;
-*) echo Probably not on a Linux system 1>&2
- exit 1;;
-esac
-set -v
-h2py -i '(u_long)' /usr/include/sys/types.h /usr/include/netinet/in.h /usr/include/dlfcn.h
diff --git a/Lib/plat-netbsd1/IN.py b/Lib/plat-netbsd1/IN.py
deleted file mode 100644
index 474c51e..0000000
--- a/Lib/plat-netbsd1/IN.py
+++ /dev/null
@@ -1,56 +0,0 @@
-# Generated by h2py from /usr/include/netinet/in.h
-IPPROTO_IP = 0
-IPPROTO_ICMP = 1
-IPPROTO_IGMP = 2
-IPPROTO_GGP = 3
-IPPROTO_IPIP = 4
-IPPROTO_TCP = 6
-IPPROTO_EGP = 8
-IPPROTO_PUP = 12
-IPPROTO_UDP = 17
-IPPROTO_IDP = 22
-IPPROTO_TP = 29
-IPPROTO_EON = 80
-IPPROTO_ENCAP = 98
-IPPROTO_RAW = 255
-IPPROTO_MAX = 256
-IPPORT_RESERVED = 1024
-IPPORT_USERRESERVED = 5000
-def __IPADDR(x): return ((u_int32_t)(x))
-
-IN_CLASSA_NSHIFT = 24
-IN_CLASSA_MAX = 128
-IN_CLASSB_NSHIFT = 16
-IN_CLASSB_MAX = 65536
-IN_CLASSC_NSHIFT = 8
-IN_CLASSD_NSHIFT = 28
-def IN_MULTICAST(i): return IN_CLASSD(i)
-
-IN_LOOPBACKNET = 127
-IP_OPTIONS = 1
-IP_HDRINCL = 2
-IP_TOS = 3
-IP_TTL = 4
-IP_RECVOPTS = 5
-IP_RECVRETOPTS = 6
-IP_RECVDSTADDR = 7
-IP_RETOPTS = 8
-IP_MULTICAST_IF = 9
-IP_MULTICAST_TTL = 10
-IP_MULTICAST_LOOP = 11
-IP_ADD_MEMBERSHIP = 12
-IP_DROP_MEMBERSHIP = 13
-IP_RECVIF = 20
-IP_DEFAULT_MULTICAST_TTL = 1
-IP_DEFAULT_MULTICAST_LOOP = 1
-IP_MAX_MEMBERSHIPS = 20
-IPPROTO_MAXID = (IPPROTO_IDP + 1)
-IPCTL_FORWARDING = 1
-IPCTL_SENDREDIRECTS = 2
-IPCTL_DEFTTL = 3
-IPCTL_DEFMTU = 4
-IPCTL_FORWSRCRT = 5
-IPCTL_DIRECTEDBCAST = 6
-IPCTL_ALLOWSRCRT = 7
-IPCTL_MAXID = 8
-def in_nullhost(x): return ((x).s_addr == INADDR_ANY)
diff --git a/Lib/plat-netbsd1/regen b/Lib/plat-netbsd1/regen
deleted file mode 100755
index 8aa6898..0000000
--- a/Lib/plat-netbsd1/regen
+++ /dev/null
@@ -1,3 +0,0 @@
-#! /bin/sh
-set -v
-python ../../Tools/scripts/h2py.py -i '(u_long)' /usr/include/netinet/in.h
diff --git a/Lib/plat-next3/regen b/Lib/plat-next3/regen
deleted file mode 100755
index 7a03613..0000000
--- a/Lib/plat-next3/regen
+++ /dev/null
@@ -1,6 +0,0 @@
-#! /bin/sh
-set -v
-INCLUDE="/NextDeveloper/Headers;/NextDeveloper/Headers/ansi;/NextDeveloper/Headers/bsd"
-export INCLUDE
-
-python ../../Tools/scripts/h2py.py -i '(u_long)' /usr/include/bsd/netinet/in.h
diff --git a/Lib/plat-sunos5/CDIO.py b/Lib/plat-sunos5/CDIO.py
deleted file mode 100644
index d766b50..0000000
--- a/Lib/plat-sunos5/CDIO.py
+++ /dev/null
@@ -1,73 +0,0 @@
-# Generated by h2py from /usr/include/sys/cdio.h
-CDROM_LBA = 0x01
-CDROM_MSF = 0x02
-CDROM_DATA_TRACK = 0x04
-CDROM_LEADOUT = 0xAA
-CDROM_AUDIO_INVALID = 0x00
-CDROM_AUDIO_PLAY = 0x11
-CDROM_AUDIO_PAUSED = 0x12
-CDROM_AUDIO_COMPLETED = 0x13
-CDROM_AUDIO_ERROR = 0x14
-CDROM_AUDIO_NO_STATUS = 0x15
-CDROM_DA_NO_SUBCODE = 0x00
-CDROM_DA_SUBQ = 0x01
-CDROM_DA_ALL_SUBCODE = 0x02
-CDROM_DA_SUBCODE_ONLY = 0x03
-CDROM_XA_DATA = 0x00
-CDROM_XA_SECTOR_DATA = 0x01
-CDROM_XA_DATA_W_ERROR = 0x02
-CDROM_BLK_512 = 512
-CDROM_BLK_1024 = 1024
-CDROM_BLK_2048 = 2048
-CDROM_BLK_2056 = 2056
-CDROM_BLK_2336 = 2336
-CDROM_BLK_2340 = 2340
-CDROM_BLK_2352 = 2352
-CDROM_BLK_2368 = 2368
-CDROM_BLK_2448 = 2448
-CDROM_BLK_2646 = 2646
-CDROM_BLK_2647 = 2647
-CDROM_BLK_SUBCODE = 96
-CDROM_NORMAL_SPEED = 0x00
-CDROM_DOUBLE_SPEED = 0x01
-CDROM_QUAD_SPEED = 0x03
-CDROM_TWELVE_SPEED = 0x0C
-CDROM_MAXIMUM_SPEED = 0xff
-CDIOC = (0x04 << 8)
-CDROMPAUSE = (CDIOC|151)
-CDROMRESUME = (CDIOC|152)
-CDROMPLAYMSF = (CDIOC|153)
-CDROMPLAYTRKIND = (CDIOC|154)
-CDROMREADTOCHDR = (CDIOC|155)
-CDROMREADTOCENTRY = (CDIOC|156)
-CDROMSTOP = (CDIOC|157)
-CDROMSTART = (CDIOC|158)
-CDROMEJECT = (CDIOC|159)
-CDROMVOLCTRL = (CDIOC|160)
-CDROMSUBCHNL = (CDIOC|161)
-CDROMREADMODE2 = (CDIOC|162)
-CDROMREADMODE1 = (CDIOC|163)
-CDROMREADOFFSET = (CDIOC|164)
-CDROMGBLKMODE = (CDIOC|165)
-CDROMSBLKMODE = (CDIOC|166)
-CDROMCDDA = (CDIOC|167)
-CDROMCDXA = (CDIOC|168)
-CDROMSUBCODE = (CDIOC|169)
-CDROMGDRVSPEED = (CDIOC|170)
-CDROMSDRVSPEED = (CDIOC|171)
-SCMD_READ_TOC = 0x43
-SCMD_PLAYAUDIO_MSF = 0x47
-SCMD_PLAYAUDIO_TI = 0x48
-SCMD_PAUSE_RESUME = 0x4B
-SCMD_READ_SUBCHANNEL = 0x42
-SCMD_PLAYAUDIO10 = 0x45
-SCMD_PLAYTRACK_REL10 = 0x49
-SCMD_READ_HEADER = 0x44
-SCMD_PLAYAUDIO12 = 0xA5
-SCMD_PLAYTRACK_REL12 = 0xA9
-SCMD_CD_PLAYBACK_CONTROL = 0xC9
-SCMD_CD_PLAYBACK_STATUS = 0xC4
-SCMD_READ_CDDA = 0xD8
-SCMD_READ_CDXA = 0xDB
-SCMD_READ_ALL_SUBCODES = 0xDF
-CDROM_MODE2_SIZE = 2336
diff --git a/Lib/plat-sunos5/DLFCN.py b/Lib/plat-sunos5/DLFCN.py
deleted file mode 100644
index f492350..0000000
--- a/Lib/plat-sunos5/DLFCN.py
+++ /dev/null
@@ -1,27 +0,0 @@
-# Generated by h2py from /usr/include/dlfcn.h
-from TYPES import *
-RTLD_LAZY = 0x00001
-RTLD_NOW = 0x00002
-RTLD_NOLOAD = 0x00004
-RTLD_GLOBAL = 0x00100
-RTLD_LOCAL = 0x00000
-RTLD_PARENT = 0x00200
-RTLD_GROUP = 0x00400
-RTLD_WORLD = 0x00800
-RTLD_NODELETE = 0x01000
-RTLD_CONFGEN = 0x10000
-RTLD_REL_RELATIVE = 0x00001
-RTLD_REL_EXEC = 0x00002
-RTLD_REL_DEPENDS = 0x00004
-RTLD_REL_PRELOAD = 0x00008
-RTLD_REL_SELF = 0x00010
-RTLD_REL_WEAK = 0x00020
-RTLD_REL_ALL = 0x00fff
-RTLD_MEMORY = 0x01000
-RTLD_STRIP = 0x02000
-RTLD_NOHEAP = 0x04000
-RTLD_CONFSET = 0x10000
-RTLD_DI_LMID = 1
-RTLD_DI_LINKMAP = 2
-RTLD_DI_CONFIGADDR = 3
-RTLD_DI_MAX = 3
diff --git a/Lib/plat-sunos5/IN.py b/Lib/plat-sunos5/IN.py
deleted file mode 100644
index 9572ead..0000000
--- a/Lib/plat-sunos5/IN.py
+++ /dev/null
@@ -1,1421 +0,0 @@
-# Generated by h2py from /usr/include/netinet/in.h
-
-# Included from sys/feature_tests.h
-
-# Included from sys/isa_defs.h
-_CHAR_ALIGNMENT = 1
-_SHORT_ALIGNMENT = 2
-_INT_ALIGNMENT = 4
-_LONG_ALIGNMENT = 8
-_LONG_LONG_ALIGNMENT = 8
-_DOUBLE_ALIGNMENT = 8
-_LONG_DOUBLE_ALIGNMENT = 16
-_POINTER_ALIGNMENT = 8
-_MAX_ALIGNMENT = 16
-_ALIGNMENT_REQUIRED = 1
-_CHAR_ALIGNMENT = 1
-_SHORT_ALIGNMENT = 2
-_INT_ALIGNMENT = 4
-_LONG_ALIGNMENT = 4
-_LONG_LONG_ALIGNMENT = 4
-_DOUBLE_ALIGNMENT = 4
-_LONG_DOUBLE_ALIGNMENT = 4
-_POINTER_ALIGNMENT = 4
-_MAX_ALIGNMENT = 4
-_ALIGNMENT_REQUIRED = 0
-_CHAR_ALIGNMENT = 1
-_SHORT_ALIGNMENT = 2
-_INT_ALIGNMENT = 4
-_LONG_LONG_ALIGNMENT = 8
-_DOUBLE_ALIGNMENT = 8
-_ALIGNMENT_REQUIRED = 1
-_LONG_ALIGNMENT = 4
-_LONG_DOUBLE_ALIGNMENT = 8
-_POINTER_ALIGNMENT = 4
-_MAX_ALIGNMENT = 8
-_LONG_ALIGNMENT = 8
-_LONG_DOUBLE_ALIGNMENT = 16
-_POINTER_ALIGNMENT = 8
-_MAX_ALIGNMENT = 16
-_POSIX_C_SOURCE = 1
-_LARGEFILE64_SOURCE = 1
-_LARGEFILE_SOURCE = 1
-_FILE_OFFSET_BITS = 64
-_FILE_OFFSET_BITS = 32
-_POSIX_C_SOURCE = 199506
-_POSIX_PTHREAD_SEMANTICS = 1
-_XOPEN_VERSION = 500
-_XOPEN_VERSION = 4
-_XOPEN_VERSION = 3
-from TYPES import *
-
-# Included from sys/stream.h
-
-# Included from sys/vnode.h
-from TYPES import *
-
-# Included from sys/t_lock.h
-
-# Included from sys/machlock.h
-from TYPES import *
-LOCK_HELD_VALUE = 0xff
-def SPIN_LOCK(pl): return ((pl) > ipltospl(LOCK_LEVEL))
-
-def LOCK_SAMPLE_INTERVAL(i): return (((i) & 0xff) == 0)
-
-CLOCK_LEVEL = 10
-LOCK_LEVEL = 10
-DISP_LEVEL = (LOCK_LEVEL + 1)
-PTR24_LSB = 5
-PTR24_MSB = (PTR24_LSB + 24)
-PTR24_ALIGN = 32
-PTR24_BASE = 0xe0000000
-
-# Included from sys/param.h
-from TYPES import *
-_POSIX_VDISABLE = 0
-MAX_INPUT = 512
-MAX_CANON = 256
-UID_NOBODY = 60001
-GID_NOBODY = UID_NOBODY
-UID_NOACCESS = 60002
-MAX_TASKID = 999999
-MAX_MAXPID = 999999
-DEFAULT_MAXPID = 999999
-DEFAULT_JUMPPID = 100000
-DEFAULT_MAXPID = 30000
-DEFAULT_JUMPPID = 0
-MAXUID = 2147483647
-MAXPROJID = MAXUID
-MAXLINK = 32767
-NMOUNT = 40
-CANBSIZ = 256
-NOFILE = 20
-NGROUPS_UMIN = 0
-NGROUPS_UMAX = 32
-NGROUPS_MAX_DEFAULT = 16
-NZERO = 20
-NULL = 0
-NULL = 0
-CMASK = 0o22
-CDLIMIT = (1<<11)
-NBPS = 0x20000
-NBPSCTR = 512
-UBSIZE = 512
-SCTRSHFT = 9
-SYSNAME = 9
-PREMOTE = 39
-MAXPATHLEN = 1024
-MAXSYMLINKS = 20
-MAXNAMELEN = 256
-NADDR = 13
-PIPE_BUF = 5120
-PIPE_MAX = 5120
-NBBY = 8
-MAXBSIZE = 8192
-DEV_BSIZE = 512
-DEV_BSHIFT = 9
-MAXFRAG = 8
-MAXOFF32_T = 0x7fffffff
-MAXOFF_T = 0x7fffffffffffffff
-MAXOFFSET_T = 0x7fffffffffffffff
-MAXOFF_T = 0x7fffffff
-MAXOFFSET_T = 0x7fffffff
-def btodb(bytes): return \
-
-def dbtob(db): return \
-
-def lbtodb(bytes): return \
-
-def ldbtob(db): return \
-
-NCARGS32 = 0x100000
-NCARGS64 = 0x200000
-NCARGS = NCARGS64
-NCARGS = NCARGS32
-FSHIFT = 8
-FSCALE = (1<<FSHIFT)
-def DELAY(n): return drv_usecwait(n)
-
-def mmu_ptob(x): return ((x) << MMU_PAGESHIFT)
-
-def mmu_btop(x): return (((x)) >> MMU_PAGESHIFT)
-
-def mmu_btopr(x): return ((((x) + MMU_PAGEOFFSET) >> MMU_PAGESHIFT))
-
-def mmu_ptod(x): return ((x) << (MMU_PAGESHIFT - DEV_BSHIFT))
-
-def ptod(x): return ((x) << (PAGESHIFT - DEV_BSHIFT))
-
-def ptob(x): return ((x) << PAGESHIFT)
-
-def btop(x): return (((x) >> PAGESHIFT))
-
-def btopr(x): return ((((x) + PAGEOFFSET) >> PAGESHIFT))
-
-def dtop(DD): return (((DD) + NDPP - 1) >> (PAGESHIFT - DEV_BSHIFT))
-
-def dtopt(DD): return ((DD) >> (PAGESHIFT - DEV_BSHIFT))
-
-_AIO_LISTIO_MAX = (4096)
-_AIO_MAX = (-1)
-_MQ_OPEN_MAX = (32)
-_MQ_PRIO_MAX = (32)
-_SEM_NSEMS_MAX = INT_MAX
-_SEM_VALUE_MAX = INT_MAX
-
-# Included from sys/unistd.h
-_CS_PATH = 65
-_CS_LFS_CFLAGS = 68
-_CS_LFS_LDFLAGS = 69
-_CS_LFS_LIBS = 70
-_CS_LFS_LINTFLAGS = 71
-_CS_LFS64_CFLAGS = 72
-_CS_LFS64_LDFLAGS = 73
-_CS_LFS64_LIBS = 74
-_CS_LFS64_LINTFLAGS = 75
-_CS_XBS5_ILP32_OFF32_CFLAGS = 700
-_CS_XBS5_ILP32_OFF32_LDFLAGS = 701
-_CS_XBS5_ILP32_OFF32_LIBS = 702
-_CS_XBS5_ILP32_OFF32_LINTFLAGS = 703
-_CS_XBS5_ILP32_OFFBIG_CFLAGS = 705
-_CS_XBS5_ILP32_OFFBIG_LDFLAGS = 706
-_CS_XBS5_ILP32_OFFBIG_LIBS = 707
-_CS_XBS5_ILP32_OFFBIG_LINTFLAGS = 708
-_CS_XBS5_LP64_OFF64_CFLAGS = 709
-_CS_XBS5_LP64_OFF64_LDFLAGS = 710
-_CS_XBS5_LP64_OFF64_LIBS = 711
-_CS_XBS5_LP64_OFF64_LINTFLAGS = 712
-_CS_XBS5_LPBIG_OFFBIG_CFLAGS = 713
-_CS_XBS5_LPBIG_OFFBIG_LDFLAGS = 714
-_CS_XBS5_LPBIG_OFFBIG_LIBS = 715
-_CS_XBS5_LPBIG_OFFBIG_LINTFLAGS = 716
-_SC_ARG_MAX = 1
-_SC_CHILD_MAX = 2
-_SC_CLK_TCK = 3
-_SC_NGROUPS_MAX = 4
-_SC_OPEN_MAX = 5
-_SC_JOB_CONTROL = 6
-_SC_SAVED_IDS = 7
-_SC_VERSION = 8
-_SC_PASS_MAX = 9
-_SC_LOGNAME_MAX = 10
-_SC_PAGESIZE = 11
-_SC_XOPEN_VERSION = 12
-_SC_NPROCESSORS_CONF = 14
-_SC_NPROCESSORS_ONLN = 15
-_SC_STREAM_MAX = 16
-_SC_TZNAME_MAX = 17
-_SC_AIO_LISTIO_MAX = 18
-_SC_AIO_MAX = 19
-_SC_AIO_PRIO_DELTA_MAX = 20
-_SC_ASYNCHRONOUS_IO = 21
-_SC_DELAYTIMER_MAX = 22
-_SC_FSYNC = 23
-_SC_MAPPED_FILES = 24
-_SC_MEMLOCK = 25
-_SC_MEMLOCK_RANGE = 26
-_SC_MEMORY_PROTECTION = 27
-_SC_MESSAGE_PASSING = 28
-_SC_MQ_OPEN_MAX = 29
-_SC_MQ_PRIO_MAX = 30
-_SC_PRIORITIZED_IO = 31
-_SC_PRIORITY_SCHEDULING = 32
-_SC_REALTIME_SIGNALS = 33
-_SC_RTSIG_MAX = 34
-_SC_SEMAPHORES = 35
-_SC_SEM_NSEMS_MAX = 36
-_SC_SEM_VALUE_MAX = 37
-_SC_SHARED_MEMORY_OBJECTS = 38
-_SC_SIGQUEUE_MAX = 39
-_SC_SIGRT_MIN = 40
-_SC_SIGRT_MAX = 41
-_SC_SYNCHRONIZED_IO = 42
-_SC_TIMERS = 43
-_SC_TIMER_MAX = 44
-_SC_2_C_BIND = 45
-_SC_2_C_DEV = 46
-_SC_2_C_VERSION = 47
-_SC_2_FORT_DEV = 48
-_SC_2_FORT_RUN = 49
-_SC_2_LOCALEDEF = 50
-_SC_2_SW_DEV = 51
-_SC_2_UPE = 52
-_SC_2_VERSION = 53
-_SC_BC_BASE_MAX = 54
-_SC_BC_DIM_MAX = 55
-_SC_BC_SCALE_MAX = 56
-_SC_BC_STRING_MAX = 57
-_SC_COLL_WEIGHTS_MAX = 58
-_SC_EXPR_NEST_MAX = 59
-_SC_LINE_MAX = 60
-_SC_RE_DUP_MAX = 61
-_SC_XOPEN_CRYPT = 62
-_SC_XOPEN_ENH_I18N = 63
-_SC_XOPEN_SHM = 64
-_SC_2_CHAR_TERM = 66
-_SC_XOPEN_XCU_VERSION = 67
-_SC_ATEXIT_MAX = 76
-_SC_IOV_MAX = 77
-_SC_XOPEN_UNIX = 78
-_SC_PAGE_SIZE = _SC_PAGESIZE
-_SC_T_IOV_MAX = 79
-_SC_PHYS_PAGES = 500
-_SC_AVPHYS_PAGES = 501
-_SC_COHER_BLKSZ = 503
-_SC_SPLIT_CACHE = 504
-_SC_ICACHE_SZ = 505
-_SC_DCACHE_SZ = 506
-_SC_ICACHE_LINESZ = 507
-_SC_DCACHE_LINESZ = 508
-_SC_ICACHE_BLKSZ = 509
-_SC_DCACHE_BLKSZ = 510
-_SC_DCACHE_TBLKSZ = 511
-_SC_ICACHE_ASSOC = 512
-_SC_DCACHE_ASSOC = 513
-_SC_MAXPID = 514
-_SC_STACK_PROT = 515
-_SC_THREAD_DESTRUCTOR_ITERATIONS = 568
-_SC_GETGR_R_SIZE_MAX = 569
-_SC_GETPW_R_SIZE_MAX = 570
-_SC_LOGIN_NAME_MAX = 571
-_SC_THREAD_KEYS_MAX = 572
-_SC_THREAD_STACK_MIN = 573
-_SC_THREAD_THREADS_MAX = 574
-_SC_TTY_NAME_MAX = 575
-_SC_THREADS = 576
-_SC_THREAD_ATTR_STACKADDR = 577
-_SC_THREAD_ATTR_STACKSIZE = 578
-_SC_THREAD_PRIORITY_SCHEDULING = 579
-_SC_THREAD_PRIO_INHERIT = 580
-_SC_THREAD_PRIO_PROTECT = 581
-_SC_THREAD_PROCESS_SHARED = 582
-_SC_THREAD_SAFE_FUNCTIONS = 583
-_SC_XOPEN_LEGACY = 717
-_SC_XOPEN_REALTIME = 718
-_SC_XOPEN_REALTIME_THREADS = 719
-_SC_XBS5_ILP32_OFF32 = 720
-_SC_XBS5_ILP32_OFFBIG = 721
-_SC_XBS5_LP64_OFF64 = 722
-_SC_XBS5_LPBIG_OFFBIG = 723
-_PC_LINK_MAX = 1
-_PC_MAX_CANON = 2
-_PC_MAX_INPUT = 3
-_PC_NAME_MAX = 4
-_PC_PATH_MAX = 5
-_PC_PIPE_BUF = 6
-_PC_NO_TRUNC = 7
-_PC_VDISABLE = 8
-_PC_CHOWN_RESTRICTED = 9
-_PC_ASYNC_IO = 10
-_PC_PRIO_IO = 11
-_PC_SYNC_IO = 12
-_PC_FILESIZEBITS = 67
-_PC_LAST = 67
-_POSIX_VERSION = 199506
-_POSIX2_VERSION = 199209
-_POSIX2_C_VERSION = 199209
-_XOPEN_XCU_VERSION = 4
-_XOPEN_REALTIME = 1
-_XOPEN_ENH_I18N = 1
-_XOPEN_SHM = 1
-_POSIX2_C_BIND = 1
-_POSIX2_CHAR_TERM = 1
-_POSIX2_LOCALEDEF = 1
-_POSIX2_C_DEV = 1
-_POSIX2_SW_DEV = 1
-_POSIX2_UPE = 1
-
-# Included from sys/mutex.h
-from TYPES import *
-def MUTEX_HELD(x): return (mutex_owned(x))
-
-
-# Included from sys/rwlock.h
-from TYPES import *
-def RW_READ_HELD(x): return (rw_read_held((x)))
-
-def RW_WRITE_HELD(x): return (rw_write_held((x)))
-
-def RW_LOCK_HELD(x): return (rw_lock_held((x)))
-
-def RW_ISWRITER(x): return (rw_iswriter(x))
-
-
-# Included from sys/semaphore.h
-
-# Included from sys/thread.h
-from TYPES import *
-
-# Included from sys/klwp.h
-from TYPES import *
-
-# Included from sys/condvar.h
-from TYPES import *
-
-# Included from sys/time.h
-
-# Included from sys/types32.h
-
-# Included from sys/int_types.h
-TIME32_MAX = INT32_MAX
-TIME32_MIN = INT32_MIN
-def TIMEVAL_OVERFLOW(tv): return \
-
-from TYPES import *
-DST_NONE = 0
-DST_USA = 1
-DST_AUST = 2
-DST_WET = 3
-DST_MET = 4
-DST_EET = 5
-DST_CAN = 6
-DST_GB = 7
-DST_RUM = 8
-DST_TUR = 9
-DST_AUSTALT = 10
-ITIMER_REAL = 0
-ITIMER_VIRTUAL = 1
-ITIMER_PROF = 2
-ITIMER_REALPROF = 3
-def ITIMERVAL_OVERFLOW(itv): return \
-
-SEC = 1
-MILLISEC = 1000
-MICROSEC = 1000000
-NANOSEC = 1000000000
-
-# Included from sys/time_impl.h
-def TIMESPEC_OVERFLOW(ts): return \
-
-def ITIMERSPEC_OVERFLOW(it): return \
-
-__CLOCK_REALTIME0 = 0
-CLOCK_VIRTUAL = 1
-CLOCK_PROF = 2
-__CLOCK_REALTIME3 = 3
-CLOCK_HIGHRES = 4
-CLOCK_MAX = 5
-CLOCK_REALTIME = __CLOCK_REALTIME3
-CLOCK_REALTIME = __CLOCK_REALTIME0
-TIMER_RELTIME = 0x0
-TIMER_ABSTIME = 0x1
-def TICK_TO_SEC(tick): return ((tick) / hz)
-
-def SEC_TO_TICK(sec): return ((sec) * hz)
-
-def TICK_TO_MSEC(tick): return \
-
-def MSEC_TO_TICK(msec): return \
-
-def MSEC_TO_TICK_ROUNDUP(msec): return \
-
-def TICK_TO_USEC(tick): return ((tick) * usec_per_tick)
-
-def USEC_TO_TICK(usec): return ((usec) / usec_per_tick)
-
-def USEC_TO_TICK_ROUNDUP(usec): return \
-
-def TICK_TO_NSEC(tick): return ((tick) * nsec_per_tick)
-
-def NSEC_TO_TICK(nsec): return ((nsec) / nsec_per_tick)
-
-def NSEC_TO_TICK_ROUNDUP(nsec): return \
-
-def TIMEVAL_TO_TICK(tvp): return \
-
-def TIMESTRUC_TO_TICK(tsp): return \
-
-
-# Included from time.h
-from TYPES import *
-
-# Included from iso/time_iso.h
-NULL = 0
-NULL = 0
-CLOCKS_PER_SEC = 1000000
-
-# Included from sys/select.h
-FD_SETSIZE = 65536
-FD_SETSIZE = 1024
-_NBBY = 8
-NBBY = _NBBY
-def FD_ZERO(p): return bzero((p), sizeof (*(p)))
-
-
-# Included from sys/signal.h
-
-# Included from sys/iso/signal_iso.h
-SIGHUP = 1
-SIGINT = 2
-SIGQUIT = 3
-SIGILL = 4
-SIGTRAP = 5
-SIGIOT = 6
-SIGABRT = 6
-SIGEMT = 7
-SIGFPE = 8
-SIGKILL = 9
-SIGBUS = 10
-SIGSEGV = 11
-SIGSYS = 12
-SIGPIPE = 13
-SIGALRM = 14
-SIGTERM = 15
-SIGUSR1 = 16
-SIGUSR2 = 17
-SIGCLD = 18
-SIGCHLD = 18
-SIGPWR = 19
-SIGWINCH = 20
-SIGURG = 21
-SIGPOLL = 22
-SIGIO = SIGPOLL
-SIGSTOP = 23
-SIGTSTP = 24
-SIGCONT = 25
-SIGTTIN = 26
-SIGTTOU = 27
-SIGVTALRM = 28
-SIGPROF = 29
-SIGXCPU = 30
-SIGXFSZ = 31
-SIGWAITING = 32
-SIGLWP = 33
-SIGFREEZE = 34
-SIGTHAW = 35
-SIGCANCEL = 36
-SIGLOST = 37
-_SIGRTMIN = 38
-_SIGRTMAX = 45
-SIG_BLOCK = 1
-SIG_UNBLOCK = 2
-SIG_SETMASK = 3
-SIGNO_MASK = 0xFF
-SIGDEFER = 0x100
-SIGHOLD = 0x200
-SIGRELSE = 0x400
-SIGIGNORE = 0x800
-SIGPAUSE = 0x1000
-
-# Included from sys/siginfo.h
-from TYPES import *
-SIGEV_NONE = 1
-SIGEV_SIGNAL = 2
-SIGEV_THREAD = 3
-SI_NOINFO = 32767
-SI_USER = 0
-SI_LWP = (-1)
-SI_QUEUE = (-2)
-SI_TIMER = (-3)
-SI_ASYNCIO = (-4)
-SI_MESGQ = (-5)
-
-# Included from sys/machsig.h
-ILL_ILLOPC = 1
-ILL_ILLOPN = 2
-ILL_ILLADR = 3
-ILL_ILLTRP = 4
-ILL_PRVOPC = 5
-ILL_PRVREG = 6
-ILL_COPROC = 7
-ILL_BADSTK = 8
-NSIGILL = 8
-EMT_TAGOVF = 1
-EMT_CPCOVF = 2
-NSIGEMT = 2
-FPE_INTDIV = 1
-FPE_INTOVF = 2
-FPE_FLTDIV = 3
-FPE_FLTOVF = 4
-FPE_FLTUND = 5
-FPE_FLTRES = 6
-FPE_FLTINV = 7
-FPE_FLTSUB = 8
-NSIGFPE = 8
-SEGV_MAPERR = 1
-SEGV_ACCERR = 2
-NSIGSEGV = 2
-BUS_ADRALN = 1
-BUS_ADRERR = 2
-BUS_OBJERR = 3
-NSIGBUS = 3
-TRAP_BRKPT = 1
-TRAP_TRACE = 2
-TRAP_RWATCH = 3
-TRAP_WWATCH = 4
-TRAP_XWATCH = 5
-NSIGTRAP = 5
-CLD_EXITED = 1
-CLD_KILLED = 2
-CLD_DUMPED = 3
-CLD_TRAPPED = 4
-CLD_STOPPED = 5
-CLD_CONTINUED = 6
-NSIGCLD = 6
-POLL_IN = 1
-POLL_OUT = 2
-POLL_MSG = 3
-POLL_ERR = 4
-POLL_PRI = 5
-POLL_HUP = 6
-NSIGPOLL = 6
-PROF_SIG = 1
-NSIGPROF = 1
-SI_MAXSZ = 256
-SI_MAXSZ = 128
-
-# Included from sys/time_std_impl.h
-from TYPES import *
-SI32_MAXSZ = 128
-def SI_CANQUEUE(c): return ((c) <= SI_QUEUE)
-
-SA_NOCLDSTOP = 0x00020000
-SA_ONSTACK = 0x00000001
-SA_RESETHAND = 0x00000002
-SA_RESTART = 0x00000004
-SA_SIGINFO = 0x00000008
-SA_NODEFER = 0x00000010
-SA_NOCLDWAIT = 0x00010000
-SA_WAITSIG = 0x00010000
-NSIG = 46
-MAXSIG = 45
-S_SIGNAL = 1
-S_SIGSET = 2
-S_SIGACTION = 3
-S_NONE = 4
-MINSIGSTKSZ = 2048
-SIGSTKSZ = 8192
-SS_ONSTACK = 0x00000001
-SS_DISABLE = 0x00000002
-SN_PROC = 1
-SN_CANCEL = 2
-SN_SEND = 3
-
-# Included from sys/ucontext.h
-from TYPES import *
-
-# Included from sys/regset.h
-REG_CCR = (0)
-REG_PSR = (0)
-REG_PSR = (0)
-REG_PC = (1)
-REG_nPC = (2)
-REG_Y = (3)
-REG_G1 = (4)
-REG_G2 = (5)
-REG_G3 = (6)
-REG_G4 = (7)
-REG_G5 = (8)
-REG_G6 = (9)
-REG_G7 = (10)
-REG_O0 = (11)
-REG_O1 = (12)
-REG_O2 = (13)
-REG_O3 = (14)
-REG_O4 = (15)
-REG_O5 = (16)
-REG_O6 = (17)
-REG_O7 = (18)
-REG_ASI = (19)
-REG_FPRS = (20)
-REG_PS = REG_PSR
-REG_SP = REG_O6
-REG_R0 = REG_O0
-REG_R1 = REG_O1
-_NGREG = 21
-_NGREG = 19
-NGREG = _NGREG
-_NGREG32 = 19
-_NGREG64 = 21
-SPARC_MAXREGWINDOW = 31
-MAXFPQ = 16
-XRS_ID = 0x78727300
-
-# Included from v7/sys/privregs.h
-
-# Included from v7/sys/psr.h
-PSR_CWP = 0x0000001F
-PSR_ET = 0x00000020
-PSR_PS = 0x00000040
-PSR_S = 0x00000080
-PSR_PIL = 0x00000F00
-PSR_EF = 0x00001000
-PSR_EC = 0x00002000
-PSR_RSV = 0x000FC000
-PSR_ICC = 0x00F00000
-PSR_C = 0x00100000
-PSR_V = 0x00200000
-PSR_Z = 0x00400000
-PSR_N = 0x00800000
-PSR_VER = 0x0F000000
-PSR_IMPL = 0xF0000000
-PSL_ALLCC = PSR_ICC
-PSL_USER = (PSR_S)
-PSL_USERMASK = (PSR_ICC)
-PSL_UBITS = (PSR_ICC|PSR_EF)
-def USERMODE(ps): return (((ps) & PSR_PS) == 0)
-
-
-# Included from sys/fsr.h
-FSR_CEXC = 0x0000001f
-FSR_AEXC = 0x000003e0
-FSR_FCC = 0x00000c00
-FSR_PR = 0x00001000
-FSR_QNE = 0x00002000
-FSR_FTT = 0x0001c000
-FSR_VER = 0x000e0000
-FSR_TEM = 0x0f800000
-FSR_RP = 0x30000000
-FSR_RD = 0xc0000000
-FSR_VER_SHIFT = 17
-FSR_FCC1 = 0x00000003
-FSR_FCC2 = 0x0000000C
-FSR_FCC3 = 0x00000030
-FSR_CEXC_NX = 0x00000001
-FSR_CEXC_DZ = 0x00000002
-FSR_CEXC_UF = 0x00000004
-FSR_CEXC_OF = 0x00000008
-FSR_CEXC_NV = 0x00000010
-FSR_AEXC_NX = (0x1 << 5)
-FSR_AEXC_DZ = (0x2 << 5)
-FSR_AEXC_UF = (0x4 << 5)
-FSR_AEXC_OF = (0x8 << 5)
-FSR_AEXC_NV = (0x10 << 5)
-FTT_NONE = 0
-FTT_IEEE = 1
-FTT_UNFIN = 2
-FTT_UNIMP = 3
-FTT_SEQ = 4
-FTT_ALIGN = 5
-FTT_DFAULT = 6
-FSR_FTT_SHIFT = 14
-FSR_FTT_IEEE = (FTT_IEEE << FSR_FTT_SHIFT)
-FSR_FTT_UNFIN = (FTT_UNFIN << FSR_FTT_SHIFT)
-FSR_FTT_UNIMP = (FTT_UNIMP << FSR_FTT_SHIFT)
-FSR_FTT_SEQ = (FTT_SEQ << FSR_FTT_SHIFT)
-FSR_FTT_ALIGN = (FTT_ALIGN << FSR_FTT_SHIFT)
-FSR_FTT_DFAULT = (FTT_DFAULT << FSR_FTT_SHIFT)
-FSR_TEM_NX = (0x1 << 23)
-FSR_TEM_DZ = (0x2 << 23)
-FSR_TEM_UF = (0x4 << 23)
-FSR_TEM_OF = (0x8 << 23)
-FSR_TEM_NV = (0x10 << 23)
-RP_DBLEXT = 0
-RP_SINGLE = 1
-RP_DOUBLE = 2
-RP_RESERVED = 3
-RD_NEAR = 0
-RD_ZER0 = 1
-RD_POSINF = 2
-RD_NEGINF = 3
-FPRS_DL = 0x1
-FPRS_DU = 0x2
-FPRS_FEF = 0x4
-PIL_MAX = 0xf
-def SAVE_GLOBALS(RP): return \
-
-def RESTORE_GLOBALS(RP): return \
-
-def SAVE_OUTS(RP): return \
-
-def RESTORE_OUTS(RP): return \
-
-def SAVE_WINDOW(SBP): return \
-
-def RESTORE_WINDOW(SBP): return \
-
-def STORE_FPREGS(FP): return \
-
-def LOAD_FPREGS(FP): return \
-
-_SPARC_MAXREGWINDOW = 31
-_XRS_ID = 0x78727300
-GETCONTEXT = 0
-SETCONTEXT = 1
-UC_SIGMASK = 0o01
-UC_STACK = 0o02
-UC_CPU = 0o04
-UC_MAU = 0o10
-UC_FPU = UC_MAU
-UC_INTR = 0o20
-UC_ASR = 0o40
-UC_MCONTEXT = (UC_CPU|UC_FPU|UC_ASR)
-UC_ALL = (UC_SIGMASK|UC_STACK|UC_MCONTEXT)
-_SIGQUEUE_MAX = 32
-_SIGNOTIFY_MAX = 32
-
-# Included from sys/pcb.h
-INSTR_VALID = 0x02
-NORMAL_STEP = 0x04
-WATCH_STEP = 0x08
-CPC_OVERFLOW = 0x10
-ASYNC_HWERR = 0x20
-STEP_NONE = 0
-STEP_REQUESTED = 1
-STEP_ACTIVE = 2
-STEP_WASACTIVE = 3
-
-# Included from sys/msacct.h
-LMS_USER = 0
-LMS_SYSTEM = 1
-LMS_TRAP = 2
-LMS_TFAULT = 3
-LMS_DFAULT = 4
-LMS_KFAULT = 5
-LMS_USER_LOCK = 6
-LMS_SLEEP = 7
-LMS_WAIT_CPU = 8
-LMS_STOPPED = 9
-NMSTATES = 10
-
-# Included from sys/lwp.h
-
-# Included from sys/synch.h
-from TYPES import *
-USYNC_THREAD = 0x00
-USYNC_PROCESS = 0x01
-LOCK_NORMAL = 0x00
-LOCK_ERRORCHECK = 0x02
-LOCK_RECURSIVE = 0x04
-USYNC_PROCESS_ROBUST = 0x08
-LOCK_PRIO_NONE = 0x00
-LOCK_PRIO_INHERIT = 0x10
-LOCK_PRIO_PROTECT = 0x20
-LOCK_STALL_NP = 0x00
-LOCK_ROBUST_NP = 0x40
-LOCK_OWNERDEAD = 0x1
-LOCK_NOTRECOVERABLE = 0x2
-LOCK_INITED = 0x4
-LOCK_UNMAPPED = 0x8
-LWP_DETACHED = 0x00000040
-LWP_SUSPENDED = 0x00000080
-__LWP_ASLWP = 0x00000100
-MAXSYSARGS = 8
-NORMALRETURN = 0
-JUSTRETURN = 1
-LWP_USER = 0x01
-LWP_SYS = 0x02
-TS_FREE = 0x00
-TS_SLEEP = 0x01
-TS_RUN = 0x02
-TS_ONPROC = 0x04
-TS_ZOMB = 0x08
-TS_STOPPED = 0x10
-T_INTR_THREAD = 0x0001
-T_WAKEABLE = 0x0002
-T_TOMASK = 0x0004
-T_TALLOCSTK = 0x0008
-T_WOULDBLOCK = 0x0020
-T_DONTBLOCK = 0x0040
-T_DONTPEND = 0x0080
-T_SYS_PROF = 0x0100
-T_WAITCVSEM = 0x0200
-T_WATCHPT = 0x0400
-T_PANIC = 0x0800
-TP_HOLDLWP = 0x0002
-TP_TWAIT = 0x0004
-TP_LWPEXIT = 0x0008
-TP_PRSTOP = 0x0010
-TP_CHKPT = 0x0020
-TP_EXITLWP = 0x0040
-TP_PRVSTOP = 0x0080
-TP_MSACCT = 0x0100
-TP_STOPPING = 0x0200
-TP_WATCHPT = 0x0400
-TP_PAUSE = 0x0800
-TP_CHANGEBIND = 0x1000
-TS_LOAD = 0x0001
-TS_DONT_SWAP = 0x0002
-TS_SWAPENQ = 0x0004
-TS_ON_SWAPQ = 0x0008
-TS_CSTART = 0x0100
-TS_UNPAUSE = 0x0200
-TS_XSTART = 0x0400
-TS_PSTART = 0x0800
-TS_RESUME = 0x1000
-TS_CREATE = 0x2000
-TS_ALLSTART = \
- (TS_CSTART|TS_UNPAUSE|TS_XSTART|TS_PSTART|TS_RESUME|TS_CREATE)
-def CPR_VSTOPPED(t): return \
-
-def THREAD_TRANSITION(tp): return thread_transition(tp);
-
-def THREAD_STOP(tp): return \
-
-def THREAD_ZOMB(tp): return THREAD_SET_STATE(tp, TS_ZOMB, NULL)
-
-def SEMA_HELD(x): return (sema_held((x)))
-
-NO_LOCKS_HELD = 1
-NO_COMPETING_THREADS = 1
-
-# Included from sys/cred.h
-
-# Included from sys/uio.h
-from TYPES import *
-
-# Included from sys/resource.h
-from TYPES import *
-PRIO_PROCESS = 0
-PRIO_PGRP = 1
-PRIO_USER = 2
-RLIMIT_CPU = 0
-RLIMIT_FSIZE = 1
-RLIMIT_DATA = 2
-RLIMIT_STACK = 3
-RLIMIT_CORE = 4
-RLIMIT_NOFILE = 5
-RLIMIT_VMEM = 6
-RLIMIT_AS = RLIMIT_VMEM
-RLIM_NLIMITS = 7
-RLIM_INFINITY = (-3)
-RLIM_SAVED_MAX = (-2)
-RLIM_SAVED_CUR = (-1)
-RLIM_INFINITY = 0x7fffffff
-RLIM_SAVED_MAX = 0x7ffffffe
-RLIM_SAVED_CUR = 0x7ffffffd
-RLIM32_INFINITY = 0x7fffffff
-RLIM32_SAVED_MAX = 0x7ffffffe
-RLIM32_SAVED_CUR = 0x7ffffffd
-
-# Included from sys/model.h
-
-# Included from sys/debug.h
-def ASSERT64(x): return ASSERT(x)
-
-def ASSERT32(x): return ASSERT(x)
-
-DATAMODEL_MASK = 0x0FF00000
-DATAMODEL_ILP32 = 0x00100000
-DATAMODEL_LP64 = 0x00200000
-DATAMODEL_NONE = 0
-DATAMODEL_NATIVE = DATAMODEL_LP64
-DATAMODEL_NATIVE = DATAMODEL_ILP32
-def STRUCT_SIZE(handle): return \
-
-def STRUCT_BUF(handle): return ((handle).ptr.m64)
-
-def SIZEOF_PTR(umodel): return \
-
-def STRUCT_SIZE(handle): return (sizeof (*(handle).ptr))
-
-def STRUCT_BUF(handle): return ((handle).ptr)
-
-def SIZEOF_PTR(umodel): return sizeof (caddr_t)
-
-def lwp_getdatamodel(t): return DATAMODEL_ILP32
-
-RUSAGE_SELF = 0
-RUSAGE_CHILDREN = -1
-
-# Included from vm/seg_enum.h
-
-# Included from sys/buf.h
-
-# Included from sys/kstat.h
-from TYPES import *
-KSTAT_STRLEN = 31
-def KSTAT_ENTER(k): return \
-
-def KSTAT_EXIT(k): return \
-
-KSTAT_TYPE_RAW = 0
-KSTAT_TYPE_NAMED = 1
-KSTAT_TYPE_INTR = 2
-KSTAT_TYPE_IO = 3
-KSTAT_TYPE_TIMER = 4
-KSTAT_NUM_TYPES = 5
-KSTAT_FLAG_VIRTUAL = 0x01
-KSTAT_FLAG_VAR_SIZE = 0x02
-KSTAT_FLAG_WRITABLE = 0x04
-KSTAT_FLAG_PERSISTENT = 0x08
-KSTAT_FLAG_DORMANT = 0x10
-KSTAT_FLAG_INVALID = 0x20
-KSTAT_READ = 0
-KSTAT_WRITE = 1
-KSTAT_DATA_CHAR = 0
-KSTAT_DATA_INT32 = 1
-KSTAT_DATA_UINT32 = 2
-KSTAT_DATA_INT64 = 3
-KSTAT_DATA_UINT64 = 4
-KSTAT_DATA_LONG = KSTAT_DATA_INT32
-KSTAT_DATA_ULONG = KSTAT_DATA_UINT32
-KSTAT_DATA_LONG = KSTAT_DATA_INT64
-KSTAT_DATA_ULONG = KSTAT_DATA_UINT64
-KSTAT_DATA_LONG = 7
-KSTAT_DATA_ULONG = 8
-KSTAT_DATA_LONGLONG = KSTAT_DATA_INT64
-KSTAT_DATA_ULONGLONG = KSTAT_DATA_UINT64
-KSTAT_DATA_FLOAT = 5
-KSTAT_DATA_DOUBLE = 6
-KSTAT_INTR_HARD = 0
-KSTAT_INTR_SOFT = 1
-KSTAT_INTR_WATCHDOG = 2
-KSTAT_INTR_SPURIOUS = 3
-KSTAT_INTR_MULTSVC = 4
-KSTAT_NUM_INTRS = 5
-B_BUSY = 0x0001
-B_DONE = 0x0002
-B_ERROR = 0x0004
-B_PAGEIO = 0x0010
-B_PHYS = 0x0020
-B_READ = 0x0040
-B_WRITE = 0x0100
-B_KERNBUF = 0x0008
-B_WANTED = 0x0080
-B_AGE = 0x000200
-B_ASYNC = 0x000400
-B_DELWRI = 0x000800
-B_STALE = 0x001000
-B_DONTNEED = 0x002000
-B_REMAPPED = 0x004000
-B_FREE = 0x008000
-B_INVAL = 0x010000
-B_FORCE = 0x020000
-B_HEAD = 0x040000
-B_NOCACHE = 0x080000
-B_TRUNC = 0x100000
-B_SHADOW = 0x200000
-B_RETRYWRI = 0x400000
-def notavail(bp): return \
-
-def BWRITE(bp): return \
-
-def BWRITE2(bp): return \
-
-VROOT = 0x01
-VNOCACHE = 0x02
-VNOMAP = 0x04
-VDUP = 0x08
-VNOSWAP = 0x10
-VNOMOUNT = 0x20
-VISSWAP = 0x40
-VSWAPLIKE = 0x80
-VVFSLOCK = 0x100
-VVFSWAIT = 0x200
-VVMLOCK = 0x400
-VDIROPEN = 0x800
-VVMEXEC = 0x1000
-VPXFS = 0x2000
-AT_TYPE = 0x0001
-AT_MODE = 0x0002
-AT_UID = 0x0004
-AT_GID = 0x0008
-AT_FSID = 0x0010
-AT_NODEID = 0x0020
-AT_NLINK = 0x0040
-AT_SIZE = 0x0080
-AT_ATIME = 0x0100
-AT_MTIME = 0x0200
-AT_CTIME = 0x0400
-AT_RDEV = 0x0800
-AT_BLKSIZE = 0x1000
-AT_NBLOCKS = 0x2000
-AT_VCODE = 0x4000
-AT_ALL = (AT_TYPE|AT_MODE|AT_UID|AT_GID|AT_FSID|AT_NODEID|\
- AT_NLINK|AT_SIZE|AT_ATIME|AT_MTIME|AT_CTIME|\
- AT_RDEV|AT_BLKSIZE|AT_NBLOCKS|AT_VCODE)
-AT_STAT = (AT_MODE|AT_UID|AT_GID|AT_FSID|AT_NODEID|AT_NLINK|\
- AT_SIZE|AT_ATIME|AT_MTIME|AT_CTIME|AT_RDEV)
-AT_TIMES = (AT_ATIME|AT_MTIME|AT_CTIME)
-AT_NOSET = (AT_NLINK|AT_RDEV|AT_FSID|AT_NODEID|AT_TYPE|\
- AT_BLKSIZE|AT_NBLOCKS|AT_VCODE)
-VSUID = 0o4000
-VSGID = 0o2000
-VSVTX = 0o1000
-VREAD = 0o0400
-VWRITE = 0o0200
-VEXEC = 0o0100
-MODEMASK = 0o7777
-PERMMASK = 0o0777
-def MANDMODE(mode): return (((mode) & (VSGID|(VEXEC>>3))) == VSGID)
-
-VSA_ACL = 0x0001
-VSA_ACLCNT = 0x0002
-VSA_DFACL = 0x0004
-VSA_DFACLCNT = 0x0008
-LOOKUP_DIR = 0x01
-DUMP_ALLOC = 0
-DUMP_FREE = 1
-DUMP_SCAN = 2
-ATTR_UTIME = 0x01
-ATTR_EXEC = 0x02
-ATTR_COMM = 0x04
-ATTR_HINT = 0x08
-ATTR_REAL = 0x10
-
-# Included from sys/poll.h
-POLLIN = 0x0001
-POLLPRI = 0x0002
-POLLOUT = 0x0004
-POLLRDNORM = 0x0040
-POLLWRNORM = POLLOUT
-POLLRDBAND = 0x0080
-POLLWRBAND = 0x0100
-POLLNORM = POLLRDNORM
-POLLERR = 0x0008
-POLLHUP = 0x0010
-POLLNVAL = 0x0020
-POLLREMOVE = 0x0800
-POLLRDDATA = 0x0200
-POLLNOERR = 0x0400
-POLLCLOSED = 0x8000
-
-# Included from sys/strmdep.h
-def str_aligned(X): return (((ulong_t)(X) & (sizeof (int) - 1)) == 0)
-
-
-# Included from sys/strft.h
-tdelta_t_sz = 12
-FTEV_MASK = 0x1FFF
-FTEV_ISWR = 0x8000
-FTEV_CS = 0x4000
-FTEV_PS = 0x2000
-FTEV_QMASK = 0x1F00
-FTEV_ALLOCMASK = 0x1FF8
-FTEV_ALLOCB = 0x0000
-FTEV_ESBALLOC = 0x0001
-FTEV_DESBALLOC = 0x0002
-FTEV_ESBALLOCA = 0x0003
-FTEV_DESBALLOCA = 0x0004
-FTEV_ALLOCBIG = 0x0005
-FTEV_ALLOCBW = 0x0006
-FTEV_FREEB = 0x0008
-FTEV_DUPB = 0x0009
-FTEV_COPYB = 0x000A
-FTEV_CALLER = 0x000F
-FTEV_PUT = 0x0100
-FTEV_FSYNCQ = 0x0103
-FTEV_DSYNCQ = 0x0104
-FTEV_PUTQ = 0x0105
-FTEV_GETQ = 0x0106
-FTEV_RMVQ = 0x0107
-FTEV_INSQ = 0x0108
-FTEV_PUTBQ = 0x0109
-FTEV_FLUSHQ = 0x010A
-FTEV_REPLYQ = 0x010B
-FTEV_PUTNEXT = 0x010D
-FTEV_RWNEXT = 0x010E
-FTEV_QWINNER = 0x010F
-FTEV_GEWRITE = 0x0101
-def FTFLW_HASH(h): return (((unsigned)(h))%ftflw_hash_sz)
-
-FTBLK_EVNTS = 0x9
-QENAB = 0x00000001
-QWANTR = 0x00000002
-QWANTW = 0x00000004
-QFULL = 0x00000008
-QREADR = 0x00000010
-QUSE = 0x00000020
-QNOENB = 0x00000040
-QBACK = 0x00000100
-QHLIST = 0x00000200
-QPAIR = 0x00000800
-QPERQ = 0x00001000
-QPERMOD = 0x00002000
-QMTSAFE = 0x00004000
-QMTOUTPERIM = 0x00008000
-QMT_TYPEMASK = (QPAIR|QPERQ|QPERMOD|QMTSAFE|QMTOUTPERIM)
-QINSERVICE = 0x00010000
-QWCLOSE = 0x00020000
-QEND = 0x00040000
-QWANTWSYNC = 0x00080000
-QSYNCSTR = 0x00100000
-QISDRV = 0x00200000
-QHOT = 0x00400000
-QNEXTHOT = 0x00800000
-_QINSERTING = 0x04000000
-_QREMOVING = 0x08000000
-Q_SQQUEUED = 0x01
-Q_SQDRAINING = 0x02
-QB_FULL = 0x01
-QB_WANTW = 0x02
-QB_BACK = 0x04
-NBAND = 256
-STRUIOT_NONE = -1
-STRUIOT_DONTCARE = 0
-STRUIOT_STANDARD = 1
-STRUIOT_IP = 2
-DBLK_REFMIN = 0x01
-STRUIO_SPEC = 0x01
-STRUIO_DONE = 0x02
-STRUIO_IP = 0x04
-STRUIO_ZC = 0x08
-STRUIO_ICK = 0x10
-MSGMARK = 0x01
-MSGNOLOOP = 0x02
-MSGDELIM = 0x04
-MSGNOGET = 0x08
-MSGMARKNEXT = 0x10
-MSGNOTMARKNEXT = 0x20
-M_DATA = 0x00
-M_PROTO = 0x01
-M_BREAK = 0x08
-M_PASSFP = 0x09
-M_EVENT = 0x0a
-M_SIG = 0x0b
-M_DELAY = 0x0c
-M_CTL = 0x0d
-M_IOCTL = 0x0e
-M_SETOPTS = 0x10
-M_RSE = 0x11
-M_IOCACK = 0x81
-M_IOCNAK = 0x82
-M_PCPROTO = 0x83
-M_PCSIG = 0x84
-M_READ = 0x85
-M_FLUSH = 0x86
-M_STOP = 0x87
-M_START = 0x88
-M_HANGUP = 0x89
-M_ERROR = 0x8a
-M_COPYIN = 0x8b
-M_COPYOUT = 0x8c
-M_IOCDATA = 0x8d
-M_PCRSE = 0x8e
-M_STOPI = 0x8f
-M_STARTI = 0x90
-M_PCEVENT = 0x91
-M_UNHANGUP = 0x92
-QNORM = 0x00
-QPCTL = 0x80
-IOC_MODELS = DATAMODEL_MASK
-IOC_ILP32 = DATAMODEL_ILP32
-IOC_LP64 = DATAMODEL_LP64
-IOC_NATIVE = DATAMODEL_NATIVE
-IOC_NONE = DATAMODEL_NONE
-STRCANON = 0x01
-RECOPY = 0x02
-SO_ALL = 0x003f
-SO_READOPT = 0x0001
-SO_WROFF = 0x0002
-SO_MINPSZ = 0x0004
-SO_MAXPSZ = 0x0008
-SO_HIWAT = 0x0010
-SO_LOWAT = 0x0020
-SO_MREADON = 0x0040
-SO_MREADOFF = 0x0080
-SO_NDELON = 0x0100
-SO_NDELOFF = 0x0200
-SO_ISTTY = 0x0400
-SO_ISNTTY = 0x0800
-SO_TOSTOP = 0x1000
-SO_TONSTOP = 0x2000
-SO_BAND = 0x4000
-SO_DELIM = 0x8000
-SO_NODELIM = 0x010000
-SO_STRHOLD = 0x020000
-SO_ERROPT = 0x040000
-SO_COPYOPT = 0x080000
-SO_MAXBLK = 0x100000
-DEF_IOV_MAX = 16
-INFOD_FIRSTBYTES = 0x02
-INFOD_BYTES = 0x04
-INFOD_COUNT = 0x08
-INFOD_COPYOUT = 0x10
-MODOPEN = 0x1
-CLONEOPEN = 0x2
-CONSOPEN = 0x4
-OPENFAIL = -1
-BPRI_LO = 1
-BPRI_MED = 2
-BPRI_HI = 3
-BPRI_FT = 4
-INFPSZ = -1
-FLUSHALL = 1
-FLUSHDATA = 0
-STRHIGH = 5120
-STRLOW = 1024
-MAXIOCBSZ = 1024
-PERIM_INNER = 1
-PERIM_OUTER = 2
-def datamsg(type): return \
-
-def straln(a): return (caddr_t)((intptr_t)(a) & ~(sizeof (int)-1))
-
-
-# Included from sys/byteorder.h
-def ntohl(x): return (x)
-
-def ntohs(x): return (x)
-
-def htonl(x): return (x)
-
-def htons(x): return (x)
-
-IPPROTO_IP = 0
-IPPROTO_HOPOPTS = 0
-IPPROTO_ICMP = 1
-IPPROTO_IGMP = 2
-IPPROTO_GGP = 3
-IPPROTO_ENCAP = 4
-IPPROTO_TCP = 6
-IPPROTO_EGP = 8
-IPPROTO_PUP = 12
-IPPROTO_UDP = 17
-IPPROTO_IDP = 22
-IPPROTO_IPV6 = 41
-IPPROTO_ROUTING = 43
-IPPROTO_FRAGMENT = 44
-IPPROTO_RSVP = 46
-IPPROTO_ESP = 50
-IPPROTO_AH = 51
-IPPROTO_ICMPV6 = 58
-IPPROTO_NONE = 59
-IPPROTO_DSTOPTS = 60
-IPPROTO_HELLO = 63
-IPPROTO_ND = 77
-IPPROTO_EON = 80
-IPPROTO_PIM = 103
-IPPROTO_RAW = 255
-IPPROTO_MAX = 256
-IPPORT_ECHO = 7
-IPPORT_DISCARD = 9
-IPPORT_SYSTAT = 11
-IPPORT_DAYTIME = 13
-IPPORT_NETSTAT = 15
-IPPORT_FTP = 21
-IPPORT_TELNET = 23
-IPPORT_SMTP = 25
-IPPORT_TIMESERVER = 37
-IPPORT_NAMESERVER = 42
-IPPORT_WHOIS = 43
-IPPORT_MTP = 57
-IPPORT_BOOTPS = 67
-IPPORT_BOOTPC = 68
-IPPORT_TFTP = 69
-IPPORT_RJE = 77
-IPPORT_FINGER = 79
-IPPORT_TTYLINK = 87
-IPPORT_SUPDUP = 95
-IPPORT_EXECSERVER = 512
-IPPORT_LOGINSERVER = 513
-IPPORT_CMDSERVER = 514
-IPPORT_EFSSERVER = 520
-IPPORT_BIFFUDP = 512
-IPPORT_WHOSERVER = 513
-IPPORT_ROUTESERVER = 520
-IPPORT_RESERVED = 1024
-IPPORT_USERRESERVED = 5000
-IMPLINK_IP = 155
-IMPLINK_LOWEXPER = 156
-IMPLINK_HIGHEXPER = 158
-IN_CLASSA_NSHIFT = 24
-IN_CLASSA_MAX = 128
-IN_CLASSB_NSHIFT = 16
-IN_CLASSB_MAX = 65536
-IN_CLASSC_NSHIFT = 8
-IN_CLASSD_NSHIFT = 28
-def IN_MULTICAST(i): return IN_CLASSD(i)
-
-IN_LOOPBACKNET = 127
-def IN_SET_LOOPBACK_ADDR(a): return \
-
-def IN6_IS_ADDR_UNSPECIFIED(addr): return \
-
-def IN6_IS_ADDR_LOOPBACK(addr): return \
-
-def IN6_IS_ADDR_LOOPBACK(addr): return \
-
-def IN6_IS_ADDR_MULTICAST(addr): return \
-
-def IN6_IS_ADDR_MULTICAST(addr): return \
-
-def IN6_IS_ADDR_LINKLOCAL(addr): return \
-
-def IN6_IS_ADDR_LINKLOCAL(addr): return \
-
-def IN6_IS_ADDR_SITELOCAL(addr): return \
-
-def IN6_IS_ADDR_SITELOCAL(addr): return \
-
-def IN6_IS_ADDR_V4MAPPED(addr): return \
-
-def IN6_IS_ADDR_V4MAPPED(addr): return \
-
-def IN6_IS_ADDR_V4MAPPED_ANY(addr): return \
-
-def IN6_IS_ADDR_V4MAPPED_ANY(addr): return \
-
-def IN6_IS_ADDR_V4COMPAT(addr): return \
-
-def IN6_IS_ADDR_V4COMPAT(addr): return \
-
-def IN6_IS_ADDR_MC_RESERVED(addr): return \
-
-def IN6_IS_ADDR_MC_RESERVED(addr): return \
-
-def IN6_IS_ADDR_MC_NODELOCAL(addr): return \
-
-def IN6_IS_ADDR_MC_NODELOCAL(addr): return \
-
-def IN6_IS_ADDR_MC_LINKLOCAL(addr): return \
-
-def IN6_IS_ADDR_MC_LINKLOCAL(addr): return \
-
-def IN6_IS_ADDR_MC_SITELOCAL(addr): return \
-
-def IN6_IS_ADDR_MC_SITELOCAL(addr): return \
-
-def IN6_IS_ADDR_MC_ORGLOCAL(addr): return \
-
-def IN6_IS_ADDR_MC_ORGLOCAL(addr): return \
-
-def IN6_IS_ADDR_MC_GLOBAL(addr): return \
-
-def IN6_IS_ADDR_MC_GLOBAL(addr): return \
-
-IP_OPTIONS = 1
-IP_HDRINCL = 2
-IP_TOS = 3
-IP_TTL = 4
-IP_RECVOPTS = 5
-IP_RECVRETOPTS = 6
-IP_RECVDSTADDR = 7
-IP_RETOPTS = 8
-IP_MULTICAST_IF = 0x10
-IP_MULTICAST_TTL = 0x11
-IP_MULTICAST_LOOP = 0x12
-IP_ADD_MEMBERSHIP = 0x13
-IP_DROP_MEMBERSHIP = 0x14
-IP_SEC_OPT = 0x22
-IPSEC_PREF_NEVER = 0x01
-IPSEC_PREF_REQUIRED = 0x02
-IPSEC_PREF_UNIQUE = 0x04
-IP_ADD_PROXY_ADDR = 0x40
-IP_BOUND_IF = 0x41
-IP_UNSPEC_SRC = 0x42
-IP_REUSEADDR = 0x104
-IP_DONTROUTE = 0x105
-IP_BROADCAST = 0x106
-IP_DEFAULT_MULTICAST_TTL = 1
-IP_DEFAULT_MULTICAST_LOOP = 1
-IPV6_RTHDR_TYPE_0 = 0
-IPV6_UNICAST_HOPS = 0x5
-IPV6_MULTICAST_IF = 0x6
-IPV6_MULTICAST_HOPS = 0x7
-IPV6_MULTICAST_LOOP = 0x8
-IPV6_JOIN_GROUP = 0x9
-IPV6_LEAVE_GROUP = 0xa
-IPV6_ADD_MEMBERSHIP = 0x9
-IPV6_DROP_MEMBERSHIP = 0xa
-IPV6_PKTINFO = 0xb
-IPV6_HOPLIMIT = 0xc
-IPV6_NEXTHOP = 0xd
-IPV6_HOPOPTS = 0xe
-IPV6_DSTOPTS = 0xf
-IPV6_RTHDR = 0x10
-IPV6_RTHDRDSTOPTS = 0x11
-IPV6_RECVPKTINFO = 0x12
-IPV6_RECVHOPLIMIT = 0x13
-IPV6_RECVHOPOPTS = 0x14
-IPV6_RECVDSTOPTS = 0x15
-IPV6_RECVRTHDR = 0x16
-IPV6_RECVRTHDRDSTOPTS = 0x17
-IPV6_CHECKSUM = 0x18
-IPV6_BOUND_IF = 0x41
-IPV6_UNSPEC_SRC = 0x42
-INET_ADDRSTRLEN = 16
-INET6_ADDRSTRLEN = 46
-IPV6_PAD1_OPT = 0
diff --git a/Lib/plat-sunos5/STROPTS.py b/Lib/plat-sunos5/STROPTS.py
deleted file mode 100644
index 8f735c4..0000000
--- a/Lib/plat-sunos5/STROPTS.py
+++ /dev/null
@@ -1,1813 +0,0 @@
-# Generated by h2py from /usr/include/sys/stropts.h
-
-# Included from sys/feature_tests.h
-
-# Included from sys/isa_defs.h
-_CHAR_ALIGNMENT = 1
-_SHORT_ALIGNMENT = 2
-_INT_ALIGNMENT = 4
-_LONG_ALIGNMENT = 8
-_LONG_LONG_ALIGNMENT = 8
-_DOUBLE_ALIGNMENT = 8
-_LONG_DOUBLE_ALIGNMENT = 16
-_POINTER_ALIGNMENT = 8
-_MAX_ALIGNMENT = 16
-_ALIGNMENT_REQUIRED = 1
-_CHAR_ALIGNMENT = 1
-_SHORT_ALIGNMENT = 2
-_INT_ALIGNMENT = 4
-_LONG_ALIGNMENT = 4
-_LONG_LONG_ALIGNMENT = 4
-_DOUBLE_ALIGNMENT = 4
-_LONG_DOUBLE_ALIGNMENT = 4
-_POINTER_ALIGNMENT = 4
-_MAX_ALIGNMENT = 4
-_ALIGNMENT_REQUIRED = 0
-_CHAR_ALIGNMENT = 1
-_SHORT_ALIGNMENT = 2
-_INT_ALIGNMENT = 4
-_LONG_LONG_ALIGNMENT = 8
-_DOUBLE_ALIGNMENT = 8
-_ALIGNMENT_REQUIRED = 1
-_LONG_ALIGNMENT = 4
-_LONG_DOUBLE_ALIGNMENT = 8
-_POINTER_ALIGNMENT = 4
-_MAX_ALIGNMENT = 8
-_LONG_ALIGNMENT = 8
-_LONG_DOUBLE_ALIGNMENT = 16
-_POINTER_ALIGNMENT = 8
-_MAX_ALIGNMENT = 16
-_POSIX_C_SOURCE = 1
-_LARGEFILE64_SOURCE = 1
-_LARGEFILE_SOURCE = 1
-_FILE_OFFSET_BITS = 64
-_FILE_OFFSET_BITS = 32
-_POSIX_C_SOURCE = 199506
-_POSIX_PTHREAD_SEMANTICS = 1
-_XOPEN_VERSION = 500
-_XOPEN_VERSION = 4
-_XOPEN_VERSION = 3
-from TYPES import *
-
-# Included from sys/conf.h
-
-# Included from sys/t_lock.h
-
-# Included from sys/machlock.h
-from TYPES import *
-LOCK_HELD_VALUE = 0xff
-def SPIN_LOCK(pl): return ((pl) > ipltospl(LOCK_LEVEL))
-
-def LOCK_SAMPLE_INTERVAL(i): return (((i) & 0xff) == 0)
-
-CLOCK_LEVEL = 10
-LOCK_LEVEL = 10
-DISP_LEVEL = (LOCK_LEVEL + 1)
-PTR24_LSB = 5
-PTR24_MSB = (PTR24_LSB + 24)
-PTR24_ALIGN = 32
-PTR24_BASE = 0xe0000000
-
-# Included from sys/param.h
-from TYPES import *
-_POSIX_VDISABLE = 0
-MAX_INPUT = 512
-MAX_CANON = 256
-UID_NOBODY = 60001
-GID_NOBODY = UID_NOBODY
-UID_NOACCESS = 60002
-MAX_TASKID = 999999
-MAX_MAXPID = 999999
-DEFAULT_MAXPID = 999999
-DEFAULT_JUMPPID = 100000
-DEFAULT_MAXPID = 30000
-DEFAULT_JUMPPID = 0
-MAXUID = 2147483647
-MAXPROJID = MAXUID
-MAXLINK = 32767
-NMOUNT = 40
-CANBSIZ = 256
-NOFILE = 20
-NGROUPS_UMIN = 0
-NGROUPS_UMAX = 32
-NGROUPS_MAX_DEFAULT = 16
-NZERO = 20
-NULL = 0
-NULL = 0
-CMASK = 0o22
-CDLIMIT = (1<<11)
-NBPS = 0x20000
-NBPSCTR = 512
-UBSIZE = 512
-SCTRSHFT = 9
-SYSNAME = 9
-PREMOTE = 39
-MAXPATHLEN = 1024
-MAXSYMLINKS = 20
-MAXNAMELEN = 256
-NADDR = 13
-PIPE_BUF = 5120
-PIPE_MAX = 5120
-NBBY = 8
-MAXBSIZE = 8192
-DEV_BSIZE = 512
-DEV_BSHIFT = 9
-MAXFRAG = 8
-MAXOFF32_T = 0x7fffffff
-MAXOFF_T = 0x7fffffffffffffff
-MAXOFFSET_T = 0x7fffffffffffffff
-MAXOFF_T = 0x7fffffff
-MAXOFFSET_T = 0x7fffffff
-def btodb(bytes): return \
-
-def dbtob(db): return \
-
-def lbtodb(bytes): return \
-
-def ldbtob(db): return \
-
-NCARGS32 = 0x100000
-NCARGS64 = 0x200000
-NCARGS = NCARGS64
-NCARGS = NCARGS32
-FSHIFT = 8
-FSCALE = (1<<FSHIFT)
-def DELAY(n): return drv_usecwait(n)
-
-def mmu_ptob(x): return ((x) << MMU_PAGESHIFT)
-
-def mmu_btop(x): return (((x)) >> MMU_PAGESHIFT)
-
-def mmu_btopr(x): return ((((x) + MMU_PAGEOFFSET) >> MMU_PAGESHIFT))
-
-def mmu_ptod(x): return ((x) << (MMU_PAGESHIFT - DEV_BSHIFT))
-
-def ptod(x): return ((x) << (PAGESHIFT - DEV_BSHIFT))
-
-def ptob(x): return ((x) << PAGESHIFT)
-
-def btop(x): return (((x) >> PAGESHIFT))
-
-def btopr(x): return ((((x) + PAGEOFFSET) >> PAGESHIFT))
-
-def dtop(DD): return (((DD) + NDPP - 1) >> (PAGESHIFT - DEV_BSHIFT))
-
-def dtopt(DD): return ((DD) >> (PAGESHIFT - DEV_BSHIFT))
-
-_AIO_LISTIO_MAX = (4096)
-_AIO_MAX = (-1)
-_MQ_OPEN_MAX = (32)
-_MQ_PRIO_MAX = (32)
-_SEM_NSEMS_MAX = INT_MAX
-_SEM_VALUE_MAX = INT_MAX
-
-# Included from sys/unistd.h
-_CS_PATH = 65
-_CS_LFS_CFLAGS = 68
-_CS_LFS_LDFLAGS = 69
-_CS_LFS_LIBS = 70
-_CS_LFS_LINTFLAGS = 71
-_CS_LFS64_CFLAGS = 72
-_CS_LFS64_LDFLAGS = 73
-_CS_LFS64_LIBS = 74
-_CS_LFS64_LINTFLAGS = 75
-_CS_XBS5_ILP32_OFF32_CFLAGS = 700
-_CS_XBS5_ILP32_OFF32_LDFLAGS = 701
-_CS_XBS5_ILP32_OFF32_LIBS = 702
-_CS_XBS5_ILP32_OFF32_LINTFLAGS = 703
-_CS_XBS5_ILP32_OFFBIG_CFLAGS = 705
-_CS_XBS5_ILP32_OFFBIG_LDFLAGS = 706
-_CS_XBS5_ILP32_OFFBIG_LIBS = 707
-_CS_XBS5_ILP32_OFFBIG_LINTFLAGS = 708
-_CS_XBS5_LP64_OFF64_CFLAGS = 709
-_CS_XBS5_LP64_OFF64_LDFLAGS = 710
-_CS_XBS5_LP64_OFF64_LIBS = 711
-_CS_XBS5_LP64_OFF64_LINTFLAGS = 712
-_CS_XBS5_LPBIG_OFFBIG_CFLAGS = 713
-_CS_XBS5_LPBIG_OFFBIG_LDFLAGS = 714
-_CS_XBS5_LPBIG_OFFBIG_LIBS = 715
-_CS_XBS5_LPBIG_OFFBIG_LINTFLAGS = 716
-_SC_ARG_MAX = 1
-_SC_CHILD_MAX = 2
-_SC_CLK_TCK = 3
-_SC_NGROUPS_MAX = 4
-_SC_OPEN_MAX = 5
-_SC_JOB_CONTROL = 6
-_SC_SAVED_IDS = 7
-_SC_VERSION = 8
-_SC_PASS_MAX = 9
-_SC_LOGNAME_MAX = 10
-_SC_PAGESIZE = 11
-_SC_XOPEN_VERSION = 12
-_SC_NPROCESSORS_CONF = 14
-_SC_NPROCESSORS_ONLN = 15
-_SC_STREAM_MAX = 16
-_SC_TZNAME_MAX = 17
-_SC_AIO_LISTIO_MAX = 18
-_SC_AIO_MAX = 19
-_SC_AIO_PRIO_DELTA_MAX = 20
-_SC_ASYNCHRONOUS_IO = 21
-_SC_DELAYTIMER_MAX = 22
-_SC_FSYNC = 23
-_SC_MAPPED_FILES = 24
-_SC_MEMLOCK = 25
-_SC_MEMLOCK_RANGE = 26
-_SC_MEMORY_PROTECTION = 27
-_SC_MESSAGE_PASSING = 28
-_SC_MQ_OPEN_MAX = 29
-_SC_MQ_PRIO_MAX = 30
-_SC_PRIORITIZED_IO = 31
-_SC_PRIORITY_SCHEDULING = 32
-_SC_REALTIME_SIGNALS = 33
-_SC_RTSIG_MAX = 34
-_SC_SEMAPHORES = 35
-_SC_SEM_NSEMS_MAX = 36
-_SC_SEM_VALUE_MAX = 37
-_SC_SHARED_MEMORY_OBJECTS = 38
-_SC_SIGQUEUE_MAX = 39
-_SC_SIGRT_MIN = 40
-_SC_SIGRT_MAX = 41
-_SC_SYNCHRONIZED_IO = 42
-_SC_TIMERS = 43
-_SC_TIMER_MAX = 44
-_SC_2_C_BIND = 45
-_SC_2_C_DEV = 46
-_SC_2_C_VERSION = 47
-_SC_2_FORT_DEV = 48
-_SC_2_FORT_RUN = 49
-_SC_2_LOCALEDEF = 50
-_SC_2_SW_DEV = 51
-_SC_2_UPE = 52
-_SC_2_VERSION = 53
-_SC_BC_BASE_MAX = 54
-_SC_BC_DIM_MAX = 55
-_SC_BC_SCALE_MAX = 56
-_SC_BC_STRING_MAX = 57
-_SC_COLL_WEIGHTS_MAX = 58
-_SC_EXPR_NEST_MAX = 59
-_SC_LINE_MAX = 60
-_SC_RE_DUP_MAX = 61
-_SC_XOPEN_CRYPT = 62
-_SC_XOPEN_ENH_I18N = 63
-_SC_XOPEN_SHM = 64
-_SC_2_CHAR_TERM = 66
-_SC_XOPEN_XCU_VERSION = 67
-_SC_ATEXIT_MAX = 76
-_SC_IOV_MAX = 77
-_SC_XOPEN_UNIX = 78
-_SC_PAGE_SIZE = _SC_PAGESIZE
-_SC_T_IOV_MAX = 79
-_SC_PHYS_PAGES = 500
-_SC_AVPHYS_PAGES = 501
-_SC_COHER_BLKSZ = 503
-_SC_SPLIT_CACHE = 504
-_SC_ICACHE_SZ = 505
-_SC_DCACHE_SZ = 506
-_SC_ICACHE_LINESZ = 507
-_SC_DCACHE_LINESZ = 508
-_SC_ICACHE_BLKSZ = 509
-_SC_DCACHE_BLKSZ = 510
-_SC_DCACHE_TBLKSZ = 511
-_SC_ICACHE_ASSOC = 512
-_SC_DCACHE_ASSOC = 513
-_SC_MAXPID = 514
-_SC_STACK_PROT = 515
-_SC_THREAD_DESTRUCTOR_ITERATIONS = 568
-_SC_GETGR_R_SIZE_MAX = 569
-_SC_GETPW_R_SIZE_MAX = 570
-_SC_LOGIN_NAME_MAX = 571
-_SC_THREAD_KEYS_MAX = 572
-_SC_THREAD_STACK_MIN = 573
-_SC_THREAD_THREADS_MAX = 574
-_SC_TTY_NAME_MAX = 575
-_SC_THREADS = 576
-_SC_THREAD_ATTR_STACKADDR = 577
-_SC_THREAD_ATTR_STACKSIZE = 578
-_SC_THREAD_PRIORITY_SCHEDULING = 579
-_SC_THREAD_PRIO_INHERIT = 580
-_SC_THREAD_PRIO_PROTECT = 581
-_SC_THREAD_PROCESS_SHARED = 582
-_SC_THREAD_SAFE_FUNCTIONS = 583
-_SC_XOPEN_LEGACY = 717
-_SC_XOPEN_REALTIME = 718
-_SC_XOPEN_REALTIME_THREADS = 719
-_SC_XBS5_ILP32_OFF32 = 720
-_SC_XBS5_ILP32_OFFBIG = 721
-_SC_XBS5_LP64_OFF64 = 722
-_SC_XBS5_LPBIG_OFFBIG = 723
-_PC_LINK_MAX = 1
-_PC_MAX_CANON = 2
-_PC_MAX_INPUT = 3
-_PC_NAME_MAX = 4
-_PC_PATH_MAX = 5
-_PC_PIPE_BUF = 6
-_PC_NO_TRUNC = 7
-_PC_VDISABLE = 8
-_PC_CHOWN_RESTRICTED = 9
-_PC_ASYNC_IO = 10
-_PC_PRIO_IO = 11
-_PC_SYNC_IO = 12
-_PC_FILESIZEBITS = 67
-_PC_LAST = 67
-_POSIX_VERSION = 199506
-_POSIX2_VERSION = 199209
-_POSIX2_C_VERSION = 199209
-_XOPEN_XCU_VERSION = 4
-_XOPEN_REALTIME = 1
-_XOPEN_ENH_I18N = 1
-_XOPEN_SHM = 1
-_POSIX2_C_BIND = 1
-_POSIX2_CHAR_TERM = 1
-_POSIX2_LOCALEDEF = 1
-_POSIX2_C_DEV = 1
-_POSIX2_SW_DEV = 1
-_POSIX2_UPE = 1
-
-# Included from sys/mutex.h
-from TYPES import *
-def MUTEX_HELD(x): return (mutex_owned(x))
-
-
-# Included from sys/rwlock.h
-from TYPES import *
-def RW_READ_HELD(x): return (rw_read_held((x)))
-
-def RW_WRITE_HELD(x): return (rw_write_held((x)))
-
-def RW_LOCK_HELD(x): return (rw_lock_held((x)))
-
-def RW_ISWRITER(x): return (rw_iswriter(x))
-
-
-# Included from sys/semaphore.h
-
-# Included from sys/thread.h
-from TYPES import *
-
-# Included from sys/klwp.h
-from TYPES import *
-
-# Included from sys/condvar.h
-from TYPES import *
-
-# Included from sys/time.h
-
-# Included from sys/types32.h
-
-# Included from sys/int_types.h
-TIME32_MAX = INT32_MAX
-TIME32_MIN = INT32_MIN
-def TIMEVAL_OVERFLOW(tv): return \
-
-from TYPES import *
-DST_NONE = 0
-DST_USA = 1
-DST_AUST = 2
-DST_WET = 3
-DST_MET = 4
-DST_EET = 5
-DST_CAN = 6
-DST_GB = 7
-DST_RUM = 8
-DST_TUR = 9
-DST_AUSTALT = 10
-ITIMER_REAL = 0
-ITIMER_VIRTUAL = 1
-ITIMER_PROF = 2
-ITIMER_REALPROF = 3
-def ITIMERVAL_OVERFLOW(itv): return \
-
-SEC = 1
-MILLISEC = 1000
-MICROSEC = 1000000
-NANOSEC = 1000000000
-
-# Included from sys/time_impl.h
-def TIMESPEC_OVERFLOW(ts): return \
-
-def ITIMERSPEC_OVERFLOW(it): return \
-
-__CLOCK_REALTIME0 = 0
-CLOCK_VIRTUAL = 1
-CLOCK_PROF = 2
-__CLOCK_REALTIME3 = 3
-CLOCK_HIGHRES = 4
-CLOCK_MAX = 5
-CLOCK_REALTIME = __CLOCK_REALTIME3
-CLOCK_REALTIME = __CLOCK_REALTIME0
-TIMER_RELTIME = 0x0
-TIMER_ABSTIME = 0x1
-def TICK_TO_SEC(tick): return ((tick) / hz)
-
-def SEC_TO_TICK(sec): return ((sec) * hz)
-
-def TICK_TO_MSEC(tick): return \
-
-def MSEC_TO_TICK(msec): return \
-
-def MSEC_TO_TICK_ROUNDUP(msec): return \
-
-def TICK_TO_USEC(tick): return ((tick) * usec_per_tick)
-
-def USEC_TO_TICK(usec): return ((usec) / usec_per_tick)
-
-def USEC_TO_TICK_ROUNDUP(usec): return \
-
-def TICK_TO_NSEC(tick): return ((tick) * nsec_per_tick)
-
-def NSEC_TO_TICK(nsec): return ((nsec) / nsec_per_tick)
-
-def NSEC_TO_TICK_ROUNDUP(nsec): return \
-
-def TIMEVAL_TO_TICK(tvp): return \
-
-def TIMESTRUC_TO_TICK(tsp): return \
-
-
-# Included from time.h
-from TYPES import *
-
-# Included from iso/time_iso.h
-NULL = 0
-NULL = 0
-CLOCKS_PER_SEC = 1000000
-
-# Included from sys/select.h
-FD_SETSIZE = 65536
-FD_SETSIZE = 1024
-_NBBY = 8
-NBBY = _NBBY
-def FD_ZERO(p): return bzero((p), sizeof (*(p)))
-
-
-# Included from sys/signal.h
-
-# Included from sys/iso/signal_iso.h
-SIGHUP = 1
-SIGINT = 2
-SIGQUIT = 3
-SIGILL = 4
-SIGTRAP = 5
-SIGIOT = 6
-SIGABRT = 6
-SIGEMT = 7
-SIGFPE = 8
-SIGKILL = 9
-SIGBUS = 10
-SIGSEGV = 11
-SIGSYS = 12
-SIGPIPE = 13
-SIGALRM = 14
-SIGTERM = 15
-SIGUSR1 = 16
-SIGUSR2 = 17
-SIGCLD = 18
-SIGCHLD = 18
-SIGPWR = 19
-SIGWINCH = 20
-SIGURG = 21
-SIGPOLL = 22
-SIGIO = SIGPOLL
-SIGSTOP = 23
-SIGTSTP = 24
-SIGCONT = 25
-SIGTTIN = 26
-SIGTTOU = 27
-SIGVTALRM = 28
-SIGPROF = 29
-SIGXCPU = 30
-SIGXFSZ = 31
-SIGWAITING = 32
-SIGLWP = 33
-SIGFREEZE = 34
-SIGTHAW = 35
-SIGCANCEL = 36
-SIGLOST = 37
-_SIGRTMIN = 38
-_SIGRTMAX = 45
-SIG_BLOCK = 1
-SIG_UNBLOCK = 2
-SIG_SETMASK = 3
-SIGNO_MASK = 0xFF
-SIGDEFER = 0x100
-SIGHOLD = 0x200
-SIGRELSE = 0x400
-SIGIGNORE = 0x800
-SIGPAUSE = 0x1000
-
-# Included from sys/siginfo.h
-from TYPES import *
-SIGEV_NONE = 1
-SIGEV_SIGNAL = 2
-SIGEV_THREAD = 3
-SI_NOINFO = 32767
-SI_USER = 0
-SI_LWP = (-1)
-SI_QUEUE = (-2)
-SI_TIMER = (-3)
-SI_ASYNCIO = (-4)
-SI_MESGQ = (-5)
-
-# Included from sys/machsig.h
-ILL_ILLOPC = 1
-ILL_ILLOPN = 2
-ILL_ILLADR = 3
-ILL_ILLTRP = 4
-ILL_PRVOPC = 5
-ILL_PRVREG = 6
-ILL_COPROC = 7
-ILL_BADSTK = 8
-NSIGILL = 8
-EMT_TAGOVF = 1
-EMT_CPCOVF = 2
-NSIGEMT = 2
-FPE_INTDIV = 1
-FPE_INTOVF = 2
-FPE_FLTDIV = 3
-FPE_FLTOVF = 4
-FPE_FLTUND = 5
-FPE_FLTRES = 6
-FPE_FLTINV = 7
-FPE_FLTSUB = 8
-NSIGFPE = 8
-SEGV_MAPERR = 1
-SEGV_ACCERR = 2
-NSIGSEGV = 2
-BUS_ADRALN = 1
-BUS_ADRERR = 2
-BUS_OBJERR = 3
-NSIGBUS = 3
-TRAP_BRKPT = 1
-TRAP_TRACE = 2
-TRAP_RWATCH = 3
-TRAP_WWATCH = 4
-TRAP_XWATCH = 5
-NSIGTRAP = 5
-CLD_EXITED = 1
-CLD_KILLED = 2
-CLD_DUMPED = 3
-CLD_TRAPPED = 4
-CLD_STOPPED = 5
-CLD_CONTINUED = 6
-NSIGCLD = 6
-POLL_IN = 1
-POLL_OUT = 2
-POLL_MSG = 3
-POLL_ERR = 4
-POLL_PRI = 5
-POLL_HUP = 6
-NSIGPOLL = 6
-PROF_SIG = 1
-NSIGPROF = 1
-SI_MAXSZ = 256
-SI_MAXSZ = 128
-
-# Included from sys/time_std_impl.h
-from TYPES import *
-SI32_MAXSZ = 128
-def SI_CANQUEUE(c): return ((c) <= SI_QUEUE)
-
-SA_NOCLDSTOP = 0x00020000
-SA_ONSTACK = 0x00000001
-SA_RESETHAND = 0x00000002
-SA_RESTART = 0x00000004
-SA_SIGINFO = 0x00000008
-SA_NODEFER = 0x00000010
-SA_NOCLDWAIT = 0x00010000
-SA_WAITSIG = 0x00010000
-NSIG = 46
-MAXSIG = 45
-S_SIGNAL = 1
-S_SIGSET = 2
-S_SIGACTION = 3
-S_NONE = 4
-MINSIGSTKSZ = 2048
-SIGSTKSZ = 8192
-SS_ONSTACK = 0x00000001
-SS_DISABLE = 0x00000002
-SN_PROC = 1
-SN_CANCEL = 2
-SN_SEND = 3
-
-# Included from sys/ucontext.h
-from TYPES import *
-
-# Included from sys/regset.h
-REG_CCR = (0)
-REG_PSR = (0)
-REG_PSR = (0)
-REG_PC = (1)
-REG_nPC = (2)
-REG_Y = (3)
-REG_G1 = (4)
-REG_G2 = (5)
-REG_G3 = (6)
-REG_G4 = (7)
-REG_G5 = (8)
-REG_G6 = (9)
-REG_G7 = (10)
-REG_O0 = (11)
-REG_O1 = (12)
-REG_O2 = (13)
-REG_O3 = (14)
-REG_O4 = (15)
-REG_O5 = (16)
-REG_O6 = (17)
-REG_O7 = (18)
-REG_ASI = (19)
-REG_FPRS = (20)
-REG_PS = REG_PSR
-REG_SP = REG_O6
-REG_R0 = REG_O0
-REG_R1 = REG_O1
-_NGREG = 21
-_NGREG = 19
-NGREG = _NGREG
-_NGREG32 = 19
-_NGREG64 = 21
-SPARC_MAXREGWINDOW = 31
-MAXFPQ = 16
-XRS_ID = 0x78727300
-
-# Included from v7/sys/privregs.h
-
-# Included from v7/sys/psr.h
-PSR_CWP = 0x0000001F
-PSR_ET = 0x00000020
-PSR_PS = 0x00000040
-PSR_S = 0x00000080
-PSR_PIL = 0x00000F00
-PSR_EF = 0x00001000
-PSR_EC = 0x00002000
-PSR_RSV = 0x000FC000
-PSR_ICC = 0x00F00000
-PSR_C = 0x00100000
-PSR_V = 0x00200000
-PSR_Z = 0x00400000
-PSR_N = 0x00800000
-PSR_VER = 0x0F000000
-PSR_IMPL = 0xF0000000
-PSL_ALLCC = PSR_ICC
-PSL_USER = (PSR_S)
-PSL_USERMASK = (PSR_ICC)
-PSL_UBITS = (PSR_ICC|PSR_EF)
-def USERMODE(ps): return (((ps) & PSR_PS) == 0)
-
-
-# Included from sys/fsr.h
-FSR_CEXC = 0x0000001f
-FSR_AEXC = 0x000003e0
-FSR_FCC = 0x00000c00
-FSR_PR = 0x00001000
-FSR_QNE = 0x00002000
-FSR_FTT = 0x0001c000
-FSR_VER = 0x000e0000
-FSR_TEM = 0x0f800000
-FSR_RP = 0x30000000
-FSR_RD = 0xc0000000
-FSR_VER_SHIFT = 17
-FSR_FCC1 = 0x00000003
-FSR_FCC2 = 0x0000000C
-FSR_FCC3 = 0x00000030
-FSR_CEXC_NX = 0x00000001
-FSR_CEXC_DZ = 0x00000002
-FSR_CEXC_UF = 0x00000004
-FSR_CEXC_OF = 0x00000008
-FSR_CEXC_NV = 0x00000010
-FSR_AEXC_NX = (0x1 << 5)
-FSR_AEXC_DZ = (0x2 << 5)
-FSR_AEXC_UF = (0x4 << 5)
-FSR_AEXC_OF = (0x8 << 5)
-FSR_AEXC_NV = (0x10 << 5)
-FTT_NONE = 0
-FTT_IEEE = 1
-FTT_UNFIN = 2
-FTT_UNIMP = 3
-FTT_SEQ = 4
-FTT_ALIGN = 5
-FTT_DFAULT = 6
-FSR_FTT_SHIFT = 14
-FSR_FTT_IEEE = (FTT_IEEE << FSR_FTT_SHIFT)
-FSR_FTT_UNFIN = (FTT_UNFIN << FSR_FTT_SHIFT)
-FSR_FTT_UNIMP = (FTT_UNIMP << FSR_FTT_SHIFT)
-FSR_FTT_SEQ = (FTT_SEQ << FSR_FTT_SHIFT)
-FSR_FTT_ALIGN = (FTT_ALIGN << FSR_FTT_SHIFT)
-FSR_FTT_DFAULT = (FTT_DFAULT << FSR_FTT_SHIFT)
-FSR_TEM_NX = (0x1 << 23)
-FSR_TEM_DZ = (0x2 << 23)
-FSR_TEM_UF = (0x4 << 23)
-FSR_TEM_OF = (0x8 << 23)
-FSR_TEM_NV = (0x10 << 23)
-RP_DBLEXT = 0
-RP_SINGLE = 1
-RP_DOUBLE = 2
-RP_RESERVED = 3
-RD_NEAR = 0
-RD_ZER0 = 1
-RD_POSINF = 2
-RD_NEGINF = 3
-FPRS_DL = 0x1
-FPRS_DU = 0x2
-FPRS_FEF = 0x4
-PIL_MAX = 0xf
-def SAVE_GLOBALS(RP): return \
-
-def RESTORE_GLOBALS(RP): return \
-
-def SAVE_OUTS(RP): return \
-
-def RESTORE_OUTS(RP): return \
-
-def SAVE_WINDOW(SBP): return \
-
-def RESTORE_WINDOW(SBP): return \
-
-def STORE_FPREGS(FP): return \
-
-def LOAD_FPREGS(FP): return \
-
-_SPARC_MAXREGWINDOW = 31
-_XRS_ID = 0x78727300
-GETCONTEXT = 0
-SETCONTEXT = 1
-UC_SIGMASK = 0o01
-UC_STACK = 0o02
-UC_CPU = 0o04
-UC_MAU = 0o10
-UC_FPU = UC_MAU
-UC_INTR = 0o20
-UC_ASR = 0o40
-UC_MCONTEXT = (UC_CPU|UC_FPU|UC_ASR)
-UC_ALL = (UC_SIGMASK|UC_STACK|UC_MCONTEXT)
-_SIGQUEUE_MAX = 32
-_SIGNOTIFY_MAX = 32
-
-# Included from sys/pcb.h
-INSTR_VALID = 0x02
-NORMAL_STEP = 0x04
-WATCH_STEP = 0x08
-CPC_OVERFLOW = 0x10
-ASYNC_HWERR = 0x20
-STEP_NONE = 0
-STEP_REQUESTED = 1
-STEP_ACTIVE = 2
-STEP_WASACTIVE = 3
-
-# Included from sys/msacct.h
-LMS_USER = 0
-LMS_SYSTEM = 1
-LMS_TRAP = 2
-LMS_TFAULT = 3
-LMS_DFAULT = 4
-LMS_KFAULT = 5
-LMS_USER_LOCK = 6
-LMS_SLEEP = 7
-LMS_WAIT_CPU = 8
-LMS_STOPPED = 9
-NMSTATES = 10
-
-# Included from sys/lwp.h
-
-# Included from sys/synch.h
-from TYPES import *
-USYNC_THREAD = 0x00
-USYNC_PROCESS = 0x01
-LOCK_NORMAL = 0x00
-LOCK_ERRORCHECK = 0x02
-LOCK_RECURSIVE = 0x04
-USYNC_PROCESS_ROBUST = 0x08
-LOCK_PRIO_NONE = 0x00
-LOCK_PRIO_INHERIT = 0x10
-LOCK_PRIO_PROTECT = 0x20
-LOCK_STALL_NP = 0x00
-LOCK_ROBUST_NP = 0x40
-LOCK_OWNERDEAD = 0x1
-LOCK_NOTRECOVERABLE = 0x2
-LOCK_INITED = 0x4
-LOCK_UNMAPPED = 0x8
-LWP_DETACHED = 0x00000040
-LWP_SUSPENDED = 0x00000080
-__LWP_ASLWP = 0x00000100
-MAXSYSARGS = 8
-NORMALRETURN = 0
-JUSTRETURN = 1
-LWP_USER = 0x01
-LWP_SYS = 0x02
-TS_FREE = 0x00
-TS_SLEEP = 0x01
-TS_RUN = 0x02
-TS_ONPROC = 0x04
-TS_ZOMB = 0x08
-TS_STOPPED = 0x10
-T_INTR_THREAD = 0x0001
-T_WAKEABLE = 0x0002
-T_TOMASK = 0x0004
-T_TALLOCSTK = 0x0008
-T_WOULDBLOCK = 0x0020
-T_DONTBLOCK = 0x0040
-T_DONTPEND = 0x0080
-T_SYS_PROF = 0x0100
-T_WAITCVSEM = 0x0200
-T_WATCHPT = 0x0400
-T_PANIC = 0x0800
-TP_HOLDLWP = 0x0002
-TP_TWAIT = 0x0004
-TP_LWPEXIT = 0x0008
-TP_PRSTOP = 0x0010
-TP_CHKPT = 0x0020
-TP_EXITLWP = 0x0040
-TP_PRVSTOP = 0x0080
-TP_MSACCT = 0x0100
-TP_STOPPING = 0x0200
-TP_WATCHPT = 0x0400
-TP_PAUSE = 0x0800
-TP_CHANGEBIND = 0x1000
-TS_LOAD = 0x0001
-TS_DONT_SWAP = 0x0002
-TS_SWAPENQ = 0x0004
-TS_ON_SWAPQ = 0x0008
-TS_CSTART = 0x0100
-TS_UNPAUSE = 0x0200
-TS_XSTART = 0x0400
-TS_PSTART = 0x0800
-TS_RESUME = 0x1000
-TS_CREATE = 0x2000
-TS_ALLSTART = \
- (TS_CSTART|TS_UNPAUSE|TS_XSTART|TS_PSTART|TS_RESUME|TS_CREATE)
-def CPR_VSTOPPED(t): return \
-
-def THREAD_TRANSITION(tp): return thread_transition(tp);
-
-def THREAD_STOP(tp): return \
-
-def THREAD_ZOMB(tp): return THREAD_SET_STATE(tp, TS_ZOMB, NULL)
-
-def SEMA_HELD(x): return (sema_held((x)))
-
-NO_LOCKS_HELD = 1
-NO_COMPETING_THREADS = 1
-FMNAMESZ = 8
-
-# Included from sys/systm.h
-from TYPES import *
-
-# Included from sys/proc.h
-
-# Included from sys/cred.h
-
-# Included from sys/user.h
-from TYPES import *
-
-# Included from sys/resource.h
-from TYPES import *
-PRIO_PROCESS = 0
-PRIO_PGRP = 1
-PRIO_USER = 2
-RLIMIT_CPU = 0
-RLIMIT_FSIZE = 1
-RLIMIT_DATA = 2
-RLIMIT_STACK = 3
-RLIMIT_CORE = 4
-RLIMIT_NOFILE = 5
-RLIMIT_VMEM = 6
-RLIMIT_AS = RLIMIT_VMEM
-RLIM_NLIMITS = 7
-RLIM_INFINITY = (-3)
-RLIM_SAVED_MAX = (-2)
-RLIM_SAVED_CUR = (-1)
-RLIM_INFINITY = 0x7fffffff
-RLIM_SAVED_MAX = 0x7ffffffe
-RLIM_SAVED_CUR = 0x7ffffffd
-RLIM32_INFINITY = 0x7fffffff
-RLIM32_SAVED_MAX = 0x7ffffffe
-RLIM32_SAVED_CUR = 0x7ffffffd
-
-# Included from sys/model.h
-
-# Included from sys/debug.h
-def ASSERT64(x): return ASSERT(x)
-
-def ASSERT32(x): return ASSERT(x)
-
-DATAMODEL_MASK = 0x0FF00000
-DATAMODEL_ILP32 = 0x00100000
-DATAMODEL_LP64 = 0x00200000
-DATAMODEL_NONE = 0
-DATAMODEL_NATIVE = DATAMODEL_LP64
-DATAMODEL_NATIVE = DATAMODEL_ILP32
-def STRUCT_SIZE(handle): return \
-
-def STRUCT_BUF(handle): return ((handle).ptr.m64)
-
-def SIZEOF_PTR(umodel): return \
-
-def STRUCT_SIZE(handle): return (sizeof (*(handle).ptr))
-
-def STRUCT_BUF(handle): return ((handle).ptr)
-
-def SIZEOF_PTR(umodel): return sizeof (caddr_t)
-
-def lwp_getdatamodel(t): return DATAMODEL_ILP32
-
-RUSAGE_SELF = 0
-RUSAGE_CHILDREN = -1
-
-# Included from sys/auxv.h
-AT_NULL = 0
-AT_IGNORE = 1
-AT_EXECFD = 2
-AT_PHDR = 3
-AT_PHENT = 4
-AT_PHNUM = 5
-AT_PAGESZ = 6
-AT_BASE = 7
-AT_FLAGS = 8
-AT_ENTRY = 9
-AT_DCACHEBSIZE = 10
-AT_ICACHEBSIZE = 11
-AT_UCACHEBSIZE = 12
-AT_SUN_UID = 2000
-AT_SUN_RUID = 2001
-AT_SUN_GID = 2002
-AT_SUN_RGID = 2003
-AT_SUN_LDELF = 2004
-AT_SUN_LDSHDR = 2005
-AT_SUN_LDNAME = 2006
-AT_SUN_LPAGESZ = 2007
-AT_SUN_PLATFORM = 2008
-AT_SUN_HWCAP = 2009
-AT_SUN_IFLUSH = 2010
-AT_SUN_CPU = 2011
-AT_SUN_EMUL_ENTRY = 2012
-AT_SUN_EMUL_EXECFD = 2013
-AT_SUN_EXECNAME = 2014
-AT_SUN_MMU = 2015
-
-# Included from sys/errno.h
-EPERM = 1
-ENOENT = 2
-ESRCH = 3
-EINTR = 4
-EIO = 5
-ENXIO = 6
-E2BIG = 7
-ENOEXEC = 8
-EBADF = 9
-ECHILD = 10
-EAGAIN = 11
-ENOMEM = 12
-EACCES = 13
-EFAULT = 14
-ENOTBLK = 15
-EBUSY = 16
-EEXIST = 17
-EXDEV = 18
-ENODEV = 19
-ENOTDIR = 20
-EISDIR = 21
-EINVAL = 22
-ENFILE = 23
-EMFILE = 24
-ENOTTY = 25
-ETXTBSY = 26
-EFBIG = 27
-ENOSPC = 28
-ESPIPE = 29
-EROFS = 30
-EMLINK = 31
-EPIPE = 32
-EDOM = 33
-ERANGE = 34
-ENOMSG = 35
-EIDRM = 36
-ECHRNG = 37
-EL2NSYNC = 38
-EL3HLT = 39
-EL3RST = 40
-ELNRNG = 41
-EUNATCH = 42
-ENOCSI = 43
-EL2HLT = 44
-EDEADLK = 45
-ENOLCK = 46
-ECANCELED = 47
-ENOTSUP = 48
-EDQUOT = 49
-EBADE = 50
-EBADR = 51
-EXFULL = 52
-ENOANO = 53
-EBADRQC = 54
-EBADSLT = 55
-EDEADLOCK = 56
-EBFONT = 57
-EOWNERDEAD = 58
-ENOTRECOVERABLE = 59
-ENOSTR = 60
-ENODATA = 61
-ETIME = 62
-ENOSR = 63
-ENONET = 64
-ENOPKG = 65
-EREMOTE = 66
-ENOLINK = 67
-EADV = 68
-ESRMNT = 69
-ECOMM = 70
-EPROTO = 71
-ELOCKUNMAPPED = 72
-ENOTACTIVE = 73
-EMULTIHOP = 74
-EBADMSG = 77
-ENAMETOOLONG = 78
-EOVERFLOW = 79
-ENOTUNIQ = 80
-EBADFD = 81
-EREMCHG = 82
-ELIBACC = 83
-ELIBBAD = 84
-ELIBSCN = 85
-ELIBMAX = 86
-ELIBEXEC = 87
-EILSEQ = 88
-ENOSYS = 89
-ELOOP = 90
-ERESTART = 91
-ESTRPIPE = 92
-ENOTEMPTY = 93
-EUSERS = 94
-ENOTSOCK = 95
-EDESTADDRREQ = 96
-EMSGSIZE = 97
-EPROTOTYPE = 98
-ENOPROTOOPT = 99
-EPROTONOSUPPORT = 120
-ESOCKTNOSUPPORT = 121
-EOPNOTSUPP = 122
-EPFNOSUPPORT = 123
-EAFNOSUPPORT = 124
-EADDRINUSE = 125
-EADDRNOTAVAIL = 126
-ENETDOWN = 127
-ENETUNREACH = 128
-ENETRESET = 129
-ECONNABORTED = 130
-ECONNRESET = 131
-ENOBUFS = 132
-EISCONN = 133
-ENOTCONN = 134
-ESHUTDOWN = 143
-ETOOMANYREFS = 144
-ETIMEDOUT = 145
-ECONNREFUSED = 146
-EHOSTDOWN = 147
-EHOSTUNREACH = 148
-EWOULDBLOCK = EAGAIN
-EALREADY = 149
-EINPROGRESS = 150
-ESTALE = 151
-PSARGSZ = 80
-PSCOMSIZ = 14
-MAXCOMLEN = 16
-__KERN_NAUXV_IMPL = 19
-__KERN_NAUXV_IMPL = 21
-__KERN_NAUXV_IMPL = 21
-PSARGSZ = 80
-
-# Included from sys/watchpoint.h
-from TYPES import *
-
-# Included from vm/seg_enum.h
-
-# Included from sys/copyops.h
-from TYPES import *
-
-# Included from sys/buf.h
-
-# Included from sys/kstat.h
-from TYPES import *
-KSTAT_STRLEN = 31
-def KSTAT_ENTER(k): return \
-
-def KSTAT_EXIT(k): return \
-
-KSTAT_TYPE_RAW = 0
-KSTAT_TYPE_NAMED = 1
-KSTAT_TYPE_INTR = 2
-KSTAT_TYPE_IO = 3
-KSTAT_TYPE_TIMER = 4
-KSTAT_NUM_TYPES = 5
-KSTAT_FLAG_VIRTUAL = 0x01
-KSTAT_FLAG_VAR_SIZE = 0x02
-KSTAT_FLAG_WRITABLE = 0x04
-KSTAT_FLAG_PERSISTENT = 0x08
-KSTAT_FLAG_DORMANT = 0x10
-KSTAT_FLAG_INVALID = 0x20
-KSTAT_READ = 0
-KSTAT_WRITE = 1
-KSTAT_DATA_CHAR = 0
-KSTAT_DATA_INT32 = 1
-KSTAT_DATA_UINT32 = 2
-KSTAT_DATA_INT64 = 3
-KSTAT_DATA_UINT64 = 4
-KSTAT_DATA_LONG = KSTAT_DATA_INT32
-KSTAT_DATA_ULONG = KSTAT_DATA_UINT32
-KSTAT_DATA_LONG = KSTAT_DATA_INT64
-KSTAT_DATA_ULONG = KSTAT_DATA_UINT64
-KSTAT_DATA_LONG = 7
-KSTAT_DATA_ULONG = 8
-KSTAT_DATA_LONGLONG = KSTAT_DATA_INT64
-KSTAT_DATA_ULONGLONG = KSTAT_DATA_UINT64
-KSTAT_DATA_FLOAT = 5
-KSTAT_DATA_DOUBLE = 6
-KSTAT_INTR_HARD = 0
-KSTAT_INTR_SOFT = 1
-KSTAT_INTR_WATCHDOG = 2
-KSTAT_INTR_SPURIOUS = 3
-KSTAT_INTR_MULTSVC = 4
-KSTAT_NUM_INTRS = 5
-B_BUSY = 0x0001
-B_DONE = 0x0002
-B_ERROR = 0x0004
-B_PAGEIO = 0x0010
-B_PHYS = 0x0020
-B_READ = 0x0040
-B_WRITE = 0x0100
-B_KERNBUF = 0x0008
-B_WANTED = 0x0080
-B_AGE = 0x000200
-B_ASYNC = 0x000400
-B_DELWRI = 0x000800
-B_STALE = 0x001000
-B_DONTNEED = 0x002000
-B_REMAPPED = 0x004000
-B_FREE = 0x008000
-B_INVAL = 0x010000
-B_FORCE = 0x020000
-B_HEAD = 0x040000
-B_NOCACHE = 0x080000
-B_TRUNC = 0x100000
-B_SHADOW = 0x200000
-B_RETRYWRI = 0x400000
-def notavail(bp): return \
-
-def BWRITE(bp): return \
-
-def BWRITE2(bp): return \
-
-
-# Included from sys/aio_req.h
-
-# Included from sys/uio.h
-from TYPES import *
-WP_NOWATCH = 0x01
-WP_SETPROT = 0x02
-
-# Included from sys/timer.h
-from TYPES import *
-_TIMER_MAX = 32
-ITLK_LOCKED = 0x01
-ITLK_WANTED = 0x02
-ITLK_REMOVE = 0x04
-IT_PERLWP = 0x01
-IT_SIGNAL = 0x02
-
-# Included from sys/utrap.h
-UT_INSTRUCTION_DISABLED = 1
-UT_INSTRUCTION_ERROR = 2
-UT_INSTRUCTION_PROTECTION = 3
-UT_ILLTRAP_INSTRUCTION = 4
-UT_ILLEGAL_INSTRUCTION = 5
-UT_PRIVILEGED_OPCODE = 6
-UT_FP_DISABLED = 7
-UT_FP_EXCEPTION_IEEE_754 = 8
-UT_FP_EXCEPTION_OTHER = 9
-UT_TAG_OVERFLOW = 10
-UT_DIVISION_BY_ZERO = 11
-UT_DATA_EXCEPTION = 12
-UT_DATA_ERROR = 13
-UT_DATA_PROTECTION = 14
-UT_MEM_ADDRESS_NOT_ALIGNED = 15
-UT_PRIVILEGED_ACTION = 16
-UT_ASYNC_DATA_ERROR = 17
-UT_TRAP_INSTRUCTION_16 = 18
-UT_TRAP_INSTRUCTION_17 = 19
-UT_TRAP_INSTRUCTION_18 = 20
-UT_TRAP_INSTRUCTION_19 = 21
-UT_TRAP_INSTRUCTION_20 = 22
-UT_TRAP_INSTRUCTION_21 = 23
-UT_TRAP_INSTRUCTION_22 = 24
-UT_TRAP_INSTRUCTION_23 = 25
-UT_TRAP_INSTRUCTION_24 = 26
-UT_TRAP_INSTRUCTION_25 = 27
-UT_TRAP_INSTRUCTION_26 = 28
-UT_TRAP_INSTRUCTION_27 = 29
-UT_TRAP_INSTRUCTION_28 = 30
-UT_TRAP_INSTRUCTION_29 = 31
-UT_TRAP_INSTRUCTION_30 = 32
-UT_TRAP_INSTRUCTION_31 = 33
-UTRAP_V8P_FP_DISABLED = UT_FP_DISABLED
-UTRAP_V8P_MEM_ADDRESS_NOT_ALIGNED = UT_MEM_ADDRESS_NOT_ALIGNED
-UT_PRECISE_MAXTRAPS = 33
-
-# Included from sys/refstr.h
-
-# Included from sys/task.h
-from TYPES import *
-TASK_NORMAL = 0x0
-TASK_FINAL = 0x1
-TASK_FINALITY = 0x1
-
-# Included from sys/id_space.h
-from TYPES import *
-
-# Included from sys/vmem.h
-from TYPES import *
-VM_SLEEP = 0x00000000
-VM_NOSLEEP = 0x00000001
-VM_PANIC = 0x00000002
-VM_KMFLAGS = 0x000000ff
-VM_BESTFIT = 0x00000100
-VMEM_ALLOC = 0x01
-VMEM_FREE = 0x02
-VMEM_SPAN = 0x10
-ISP_NORMAL = 0x0
-ISP_RESERVE = 0x1
-
-# Included from sys/exacct_impl.h
-from TYPES import *
-
-# Included from sys/kmem.h
-from TYPES import *
-KM_SLEEP = 0x0000
-KM_NOSLEEP = 0x0001
-KM_PANIC = 0x0002
-KM_VMFLAGS = 0x00ff
-KM_FLAGS = 0xffff
-KMC_NOTOUCH = 0x00010000
-KMC_NODEBUG = 0x00020000
-KMC_NOMAGAZINE = 0x00040000
-KMC_NOHASH = 0x00080000
-KMC_QCACHE = 0x00100000
-_ISA_IA32 = 0
-_ISA_IA64 = 1
-SSLEEP = 1
-SRUN = 2
-SZOMB = 3
-SSTOP = 4
-SIDL = 5
-SONPROC = 6
-CLDPEND = 0x0001
-CLDCONT = 0x0002
-SSYS = 0x00000001
-STRC = 0x00000002
-SLOAD = 0x00000008
-SLOCK = 0x00000010
-SPREXEC = 0x00000020
-SPROCTR = 0x00000040
-SPRFORK = 0x00000080
-SKILLED = 0x00000100
-SULOAD = 0x00000200
-SRUNLCL = 0x00000400
-SBPTADJ = 0x00000800
-SKILLCL = 0x00001000
-SOWEUPC = 0x00002000
-SEXECED = 0x00004000
-SPASYNC = 0x00008000
-SJCTL = 0x00010000
-SNOWAIT = 0x00020000
-SVFORK = 0x00040000
-SVFWAIT = 0x00080000
-EXITLWPS = 0x00100000
-HOLDFORK = 0x00200000
-SWAITSIG = 0x00400000
-HOLDFORK1 = 0x00800000
-COREDUMP = 0x01000000
-SMSACCT = 0x02000000
-ASLWP = 0x04000000
-SPRLOCK = 0x08000000
-NOCD = 0x10000000
-HOLDWATCH = 0x20000000
-SMSFORK = 0x40000000
-SDOCORE = 0x80000000
-FORREAL = 0
-JUSTLOOKING = 1
-SUSPEND_NORMAL = 0
-SUSPEND_PAUSE = 1
-NOCLASS = (-1)
-
-# Included from sys/dditypes.h
-DDI_DEVICE_ATTR_V0 = 0x0001
-DDI_NEVERSWAP_ACC = 0x00
-DDI_STRUCTURE_LE_ACC = 0x01
-DDI_STRUCTURE_BE_ACC = 0x02
-DDI_STRICTORDER_ACC = 0x00
-DDI_UNORDERED_OK_ACC = 0x01
-DDI_MERGING_OK_ACC = 0x02
-DDI_LOADCACHING_OK_ACC = 0x03
-DDI_STORECACHING_OK_ACC = 0x04
-DDI_DATA_SZ01_ACC = 1
-DDI_DATA_SZ02_ACC = 2
-DDI_DATA_SZ04_ACC = 4
-DDI_DATA_SZ08_ACC = 8
-VERS_ACCHDL = 0x0001
-DEVID_NONE = 0
-DEVID_SCSI3_WWN = 1
-DEVID_SCSI_SERIAL = 2
-DEVID_FAB = 3
-DEVID_ENCAP = 4
-DEVID_MAXTYPE = 4
-
-# Included from sys/varargs.h
-
-# Included from sys/va_list.h
-VA_ALIGN = 8
-def _ARGSIZEOF(t): return ((sizeof (t) + VA_ALIGN - 1) & ~(VA_ALIGN - 1))
-
-VA_ALIGN = 8
-def _ARGSIZEOF(t): return ((sizeof (t) + VA_ALIGN - 1) & ~(VA_ALIGN - 1))
-
-NSYSCALL = 256
-SE_32RVAL1 = 0x0
-SE_32RVAL2 = 0x1
-SE_64RVAL = 0x2
-SE_RVAL_MASK = 0x3
-SE_LOADABLE = 0x08
-SE_LOADED = 0x10
-SE_NOUNLOAD = 0x20
-SE_ARGC = 0x40
-
-# Included from sys/devops.h
-from TYPES import *
-
-# Included from sys/poll.h
-POLLIN = 0x0001
-POLLPRI = 0x0002
-POLLOUT = 0x0004
-POLLRDNORM = 0x0040
-POLLWRNORM = POLLOUT
-POLLRDBAND = 0x0080
-POLLWRBAND = 0x0100
-POLLNORM = POLLRDNORM
-POLLERR = 0x0008
-POLLHUP = 0x0010
-POLLNVAL = 0x0020
-POLLREMOVE = 0x0800
-POLLRDDATA = 0x0200
-POLLNOERR = 0x0400
-POLLCLOSED = 0x8000
-
-# Included from vm/as.h
-
-# Included from vm/seg.h
-
-# Included from sys/vnode.h
-from TYPES import *
-VROOT = 0x01
-VNOCACHE = 0x02
-VNOMAP = 0x04
-VDUP = 0x08
-VNOSWAP = 0x10
-VNOMOUNT = 0x20
-VISSWAP = 0x40
-VSWAPLIKE = 0x80
-VVFSLOCK = 0x100
-VVFSWAIT = 0x200
-VVMLOCK = 0x400
-VDIROPEN = 0x800
-VVMEXEC = 0x1000
-VPXFS = 0x2000
-AT_TYPE = 0x0001
-AT_MODE = 0x0002
-AT_UID = 0x0004
-AT_GID = 0x0008
-AT_FSID = 0x0010
-AT_NODEID = 0x0020
-AT_NLINK = 0x0040
-AT_SIZE = 0x0080
-AT_ATIME = 0x0100
-AT_MTIME = 0x0200
-AT_CTIME = 0x0400
-AT_RDEV = 0x0800
-AT_BLKSIZE = 0x1000
-AT_NBLOCKS = 0x2000
-AT_VCODE = 0x4000
-AT_ALL = (AT_TYPE|AT_MODE|AT_UID|AT_GID|AT_FSID|AT_NODEID|\
- AT_NLINK|AT_SIZE|AT_ATIME|AT_MTIME|AT_CTIME|\
- AT_RDEV|AT_BLKSIZE|AT_NBLOCKS|AT_VCODE)
-AT_STAT = (AT_MODE|AT_UID|AT_GID|AT_FSID|AT_NODEID|AT_NLINK|\
- AT_SIZE|AT_ATIME|AT_MTIME|AT_CTIME|AT_RDEV)
-AT_TIMES = (AT_ATIME|AT_MTIME|AT_CTIME)
-AT_NOSET = (AT_NLINK|AT_RDEV|AT_FSID|AT_NODEID|AT_TYPE|\
- AT_BLKSIZE|AT_NBLOCKS|AT_VCODE)
-VSUID = 0o4000
-VSGID = 0o2000
-VSVTX = 0o1000
-VREAD = 0o0400
-VWRITE = 0o0200
-VEXEC = 0o0100
-MODEMASK = 0o7777
-PERMMASK = 0o0777
-def MANDMODE(mode): return (((mode) & (VSGID|(VEXEC>>3))) == VSGID)
-
-VSA_ACL = 0x0001
-VSA_ACLCNT = 0x0002
-VSA_DFACL = 0x0004
-VSA_DFACLCNT = 0x0008
-LOOKUP_DIR = 0x01
-DUMP_ALLOC = 0
-DUMP_FREE = 1
-DUMP_SCAN = 2
-ATTR_UTIME = 0x01
-ATTR_EXEC = 0x02
-ATTR_COMM = 0x04
-ATTR_HINT = 0x08
-ATTR_REAL = 0x10
-
-# Included from vm/faultcode.h
-FC_HWERR = 0x1
-FC_ALIGN = 0x2
-FC_OBJERR = 0x3
-FC_PROT = 0x4
-FC_NOMAP = 0x5
-FC_NOSUPPORT = 0x6
-def FC_MAKE_ERR(e): return (((e) << 8) | FC_OBJERR)
-
-def FC_CODE(fc): return ((fc) & 0xff)
-
-def FC_ERRNO(fc): return ((unsigned)(fc) >> 8)
-
-
-# Included from vm/hat.h
-from TYPES import *
-
-# Included from vm/page.h
-PAGE_HASHAVELEN = 4
-PAGE_HASHVPSHIFT = 6
-PG_EXCL = 0x0001
-PG_WAIT = 0x0002
-PG_PHYSCONTIG = 0x0004
-PG_MATCH_COLOR = 0x0008
-PG_NORELOC = 0x0010
-PG_FREE_LIST = 1
-PG_CACHE_LIST = 2
-PG_LIST_TAIL = 0
-PG_LIST_HEAD = 1
-def page_next_raw(PP): return page_nextn_raw((PP), 1)
-
-PAGE_IO_INUSE = 0x1
-PAGE_IO_WANTED = 0x2
-PGREL_NOTREL = 0x1
-PGREL_CLEAN = 0x2
-PGREL_MOD = 0x3
-P_FREE = 0x80
-P_NORELOC = 0x40
-def PP_SETAGED(pp): return ASSERT(PP_ISAGED(pp))
-
-HAT_FLAGS_RESV = 0xFF000000
-HAT_LOAD = 0x00
-HAT_LOAD_LOCK = 0x01
-HAT_LOAD_ADV = 0x04
-HAT_LOAD_CONTIG = 0x10
-HAT_LOAD_NOCONSIST = 0x20
-HAT_LOAD_SHARE = 0x40
-HAT_LOAD_REMAP = 0x80
-HAT_RELOAD_SHARE = 0x100
-HAT_PLAT_ATTR_MASK = 0xF00000
-HAT_PROT_MASK = 0x0F
-HAT_NOFAULT = 0x10
-HAT_NOSYNC = 0x20
-HAT_STRICTORDER = 0x0000
-HAT_UNORDERED_OK = 0x0100
-HAT_MERGING_OK = 0x0200
-HAT_LOADCACHING_OK = 0x0300
-HAT_STORECACHING_OK = 0x0400
-HAT_ORDER_MASK = 0x0700
-HAT_NEVERSWAP = 0x0000
-HAT_STRUCTURE_BE = 0x1000
-HAT_STRUCTURE_LE = 0x2000
-HAT_ENDIAN_MASK = 0x3000
-HAT_COW = 0x0001
-HAT_UNLOAD = 0x00
-HAT_UNLOAD_NOSYNC = 0x02
-HAT_UNLOAD_UNLOCK = 0x04
-HAT_UNLOAD_OTHER = 0x08
-HAT_UNLOAD_UNMAP = 0x10
-HAT_SYNC_DONTZERO = 0x00
-HAT_SYNC_ZERORM = 0x01
-HAT_SYNC_STOPON_REF = 0x02
-HAT_SYNC_STOPON_MOD = 0x04
-HAT_SYNC_STOPON_RM = (HAT_SYNC_STOPON_REF | HAT_SYNC_STOPON_MOD)
-HAT_DUP_ALL = 1
-HAT_DUP_COW = 2
-HAT_MAP = 0x00
-HAT_ADV_PGUNLOAD = 0x00
-HAT_FORCE_PGUNLOAD = 0x01
-P_MOD = 0x1
-P_REF = 0x2
-P_RO = 0x4
-def hat_ismod(pp): return (hat_page_getattr(pp, P_MOD))
-
-def hat_isref(pp): return (hat_page_getattr(pp, P_REF))
-
-def hat_isro(pp): return (hat_page_getattr(pp, P_RO))
-
-def hat_setmod(pp): return (hat_page_setattr(pp, P_MOD))
-
-def hat_setref(pp): return (hat_page_setattr(pp, P_REF))
-
-def hat_setrefmod(pp): return (hat_page_setattr(pp, P_REF|P_MOD))
-
-def hat_clrmod(pp): return (hat_page_clrattr(pp, P_MOD))
-
-def hat_clrref(pp): return (hat_page_clrattr(pp, P_REF))
-
-def hat_clrrefmod(pp): return (hat_page_clrattr(pp, P_REF|P_MOD))
-
-def hat_page_is_mapped(pp): return (hat_page_getshare(pp))
-
-HAT_DONTALLOC = 0
-HAT_ALLOC = 1
-HRM_SHIFT = 4
-HRM_BYTES = (1 << HRM_SHIFT)
-HRM_PAGES = ((HRM_BYTES * NBBY) / 2)
-HRM_PGPERBYTE = (NBBY/2)
-HRM_PGBYTEMASK = (HRM_PGPERBYTE-1)
-HRM_HASHSIZE = 0x200
-HRM_HASHMASK = (HRM_HASHSIZE - 1)
-HRM_BLIST_INCR = 0x200
-HRM_SWSMONID = 1
-SSL_NLEVELS = 4
-SSL_BFACTOR = 4
-SSL_LOG2BF = 2
-SEGP_ASYNC_FLUSH = 0x1
-SEGP_FORCE_WIRED = 0x2
-SEGP_SUCCESS = 0
-SEGP_FAIL = 1
-def seg_pages(seg): return \
-
-IE_NOMEM = -1
-AS_PAGLCK = 0x80
-AS_CLAIMGAP = 0x40
-AS_UNMAPWAIT = 0x20
-def AS_TYPE_64BIT(as_): return \
-
-AS_LREP_LINKEDLIST = 0
-AS_LREP_SKIPLIST = 1
-AS_MUTATION_THRESH = 225
-AH_DIR = 0x1
-AH_LO = 0x0
-AH_HI = 0x1
-AH_CONTAIN = 0x2
-
-# Included from sys/ddidmareq.h
-DMA_UNIT_8 = 1
-DMA_UNIT_16 = 2
-DMA_UNIT_32 = 4
-DMALIM_VER0 = ((0x86000000) + 0)
-DDI_DMA_FORCE_PHYSICAL = 0x0100
-DMA_ATTR_V0 = 0
-DMA_ATTR_VERSION = DMA_ATTR_V0
-DDI_DMA_CALLBACK_RUNOUT = 0
-DDI_DMA_CALLBACK_DONE = 1
-DDI_DMA_WRITE = 0x0001
-DDI_DMA_READ = 0x0002
-DDI_DMA_RDWR = (DDI_DMA_READ | DDI_DMA_WRITE)
-DDI_DMA_REDZONE = 0x0004
-DDI_DMA_PARTIAL = 0x0008
-DDI_DMA_CONSISTENT = 0x0010
-DDI_DMA_EXCLUSIVE = 0x0020
-DDI_DMA_STREAMING = 0x0040
-DDI_DMA_SBUS_64BIT = 0x2000
-DDI_DMA_MAPPED = 0
-DDI_DMA_MAPOK = 0
-DDI_DMA_PARTIAL_MAP = 1
-DDI_DMA_DONE = 2
-DDI_DMA_NORESOURCES = -1
-DDI_DMA_NOMAPPING = -2
-DDI_DMA_TOOBIG = -3
-DDI_DMA_TOOSMALL = -4
-DDI_DMA_LOCKED = -5
-DDI_DMA_BADLIMITS = -6
-DDI_DMA_STALE = -7
-DDI_DMA_BADATTR = -8
-DDI_DMA_INUSE = -9
-DDI_DMA_SYNC_FORDEV = 0x0
-DDI_DMA_SYNC_FORCPU = 0x1
-DDI_DMA_SYNC_FORKERNEL = 0x2
-
-# Included from sys/ddimapreq.h
-
-# Included from sys/mman.h
-PROT_READ = 0x1
-PROT_WRITE = 0x2
-PROT_EXEC = 0x4
-PROT_USER = 0x8
-PROT_ZFOD = (PROT_READ | PROT_WRITE | PROT_EXEC | PROT_USER)
-PROT_ALL = (PROT_READ | PROT_WRITE | PROT_EXEC | PROT_USER)
-PROT_NONE = 0x0
-MAP_SHARED = 1
-MAP_PRIVATE = 2
-MAP_TYPE = 0xf
-MAP_FIXED = 0x10
-MAP_NORESERVE = 0x40
-MAP_ANON = 0x100
-MAP_ANONYMOUS = MAP_ANON
-MAP_RENAME = 0x20
-PROC_TEXT = (PROT_EXEC | PROT_READ)
-PROC_DATA = (PROT_READ | PROT_WRITE | PROT_EXEC)
-SHARED = 0x10
-PRIVATE = 0x20
-VALID_ATTR = (PROT_READ|PROT_WRITE|PROT_EXEC|SHARED|PRIVATE)
-PROT_EXCL = 0x20
-_MAP_LOW32 = 0x80
-_MAP_NEW = 0x80000000
-from TYPES import *
-MADV_NORMAL = 0
-MADV_RANDOM = 1
-MADV_SEQUENTIAL = 2
-MADV_WILLNEED = 3
-MADV_DONTNEED = 4
-MADV_FREE = 5
-MS_OLDSYNC = 0x0
-MS_SYNC = 0x4
-MS_ASYNC = 0x1
-MS_INVALIDATE = 0x2
-MC_SYNC = 1
-MC_LOCK = 2
-MC_UNLOCK = 3
-MC_ADVISE = 4
-MC_LOCKAS = 5
-MC_UNLOCKAS = 6
-MCL_CURRENT = 0x1
-MCL_FUTURE = 0x2
-DDI_MAP_VERSION = 0x0001
-DDI_MF_USER_MAPPING = 0x1
-DDI_MF_KERNEL_MAPPING = 0x2
-DDI_MF_DEVICE_MAPPING = 0x4
-DDI_ME_GENERIC = (-1)
-DDI_ME_UNIMPLEMENTED = (-2)
-DDI_ME_NORESOURCES = (-3)
-DDI_ME_UNSUPPORTED = (-4)
-DDI_ME_REGSPEC_RANGE = (-5)
-DDI_ME_RNUMBER_RANGE = (-6)
-DDI_ME_INVAL = (-7)
-
-# Included from sys/ddipropdefs.h
-def CELLS_1275_TO_BYTES(n): return ((n) * PROP_1275_CELL_SIZE)
-
-def BYTES_TO_1275_CELLS(n): return ((n) / PROP_1275_CELL_SIZE)
-
-PH_FROM_PROM = 0x01
-DDI_PROP_SUCCESS = 0
-DDI_PROP_NOT_FOUND = 1
-DDI_PROP_UNDEFINED = 2
-DDI_PROP_NO_MEMORY = 3
-DDI_PROP_INVAL_ARG = 4
-DDI_PROP_BUF_TOO_SMALL = 5
-DDI_PROP_CANNOT_DECODE = 6
-DDI_PROP_CANNOT_ENCODE = 7
-DDI_PROP_END_OF_DATA = 8
-DDI_PROP_FOUND_1275 = 255
-PROP_1275_INT_SIZE = 4
-DDI_PROP_DONTPASS = 0x0001
-DDI_PROP_CANSLEEP = 0x0002
-DDI_PROP_SYSTEM_DEF = 0x0004
-DDI_PROP_NOTPROM = 0x0008
-DDI_PROP_DONTSLEEP = 0x0010
-DDI_PROP_STACK_CREATE = 0x0020
-DDI_PROP_UNDEF_IT = 0x0040
-DDI_PROP_HW_DEF = 0x0080
-DDI_PROP_TYPE_INT = 0x0100
-DDI_PROP_TYPE_STRING = 0x0200
-DDI_PROP_TYPE_BYTE = 0x0400
-DDI_PROP_TYPE_COMPOSITE = 0x0800
-DDI_PROP_TYPE_ANY = (DDI_PROP_TYPE_INT | \
- DDI_PROP_TYPE_STRING | \
- DDI_PROP_TYPE_BYTE | \
- DDI_PROP_TYPE_COMPOSITE)
-DDI_PROP_TYPE_MASK = (DDI_PROP_TYPE_INT | \
- DDI_PROP_TYPE_STRING | \
- DDI_PROP_TYPE_BYTE | \
- DDI_PROP_TYPE_COMPOSITE)
-DDI_RELATIVE_ADDRESSING = "relative-addressing"
-DDI_GENERIC_ADDRESSING = "generic-addressing"
-
-# Included from sys/ddidevmap.h
-KMEM_PAGEABLE = 0x100
-KMEM_NON_PAGEABLE = 0x200
-UMEM_LOCKED = 0x400
-UMEM_TRASH = 0x800
-DEVMAP_OPS_REV = 1
-DEVMAP_DEFAULTS = 0x00
-DEVMAP_MAPPING_INVALID = 0x01
-DEVMAP_ALLOW_REMAP = 0x02
-DEVMAP_USE_PAGESIZE = 0x04
-DEVMAP_SETUP_FLAGS = \
- (DEVMAP_MAPPING_INVALID | DEVMAP_ALLOW_REMAP | DEVMAP_USE_PAGESIZE)
-DEVMAP_SETUP_DONE = 0x100
-DEVMAP_LOCK_INITED = 0x200
-DEVMAP_FAULTING = 0x400
-DEVMAP_LOCKED = 0x800
-DEVMAP_FLAG_LARGE = 0x1000
-DDI_UMEM_SLEEP = 0x0
-DDI_UMEM_NOSLEEP = 0x01
-DDI_UMEM_PAGEABLE = 0x02
-DDI_UMEM_TRASH = 0x04
-DDI_UMEMLOCK_READ = 0x01
-DDI_UMEMLOCK_WRITE = 0x02
-
-# Included from sys/nexusdefs.h
-
-# Included from sys/nexusintr.h
-BUSO_REV = 4
-BUSO_REV_3 = 3
-BUSO_REV_4 = 4
-DEVO_REV = 3
-CB_REV = 1
-DDI_IDENTIFIED = (0)
-DDI_NOT_IDENTIFIED = (-1)
-DDI_PROBE_FAILURE = ENXIO
-DDI_PROBE_DONTCARE = 0
-DDI_PROBE_PARTIAL = 1
-DDI_PROBE_SUCCESS = 2
-MAPDEV_REV = 1
-from TYPES import *
-D_NEW = 0x00
-_D_OLD = 0x01
-D_TAPE = 0x08
-D_MTSAFE = 0x0020
-_D_QNEXTLESS = 0x0040
-_D_MTOCSHARED = 0x0080
-D_MTOCEXCL = 0x0800
-D_MTPUTSHARED = 0x1000
-D_MTPERQ = 0x2000
-D_MTQPAIR = 0x4000
-D_MTPERMOD = 0x6000
-D_MTOUTPERIM = 0x8000
-_D_MTCBSHARED = 0x10000
-D_MTINNER_MOD = (D_MTPUTSHARED|_D_MTOCSHARED|_D_MTCBSHARED)
-D_MTOUTER_MOD = (D_MTOCEXCL)
-D_MP = D_MTSAFE
-D_64BIT = 0x200
-D_SYNCSTR = 0x400
-D_DEVMAP = 0x100
-D_HOTPLUG = 0x4
-SNDZERO = 0x001
-SNDPIPE = 0x002
-RNORM = 0x000
-RMSGD = 0x001
-RMSGN = 0x002
-RMODEMASK = 0x003
-RPROTDAT = 0x004
-RPROTDIS = 0x008
-RPROTNORM = 0x010
-RPROTMASK = 0x01c
-RFLUSHMASK = 0x020
-RFLUSHPCPROT = 0x020
-RERRNORM = 0x001
-RERRNONPERSIST = 0x002
-RERRMASK = (RERRNORM|RERRNONPERSIST)
-WERRNORM = 0x004
-WERRNONPERSIST = 0x008
-WERRMASK = (WERRNORM|WERRNONPERSIST)
-FLUSHR = 0x01
-FLUSHW = 0x02
-FLUSHRW = 0x03
-FLUSHBAND = 0x04
-MAPINOK = 0x01
-NOMAPIN = 0x02
-REMAPOK = 0x04
-NOREMAP = 0x08
-S_INPUT = 0x0001
-S_HIPRI = 0x0002
-S_OUTPUT = 0x0004
-S_MSG = 0x0008
-S_ERROR = 0x0010
-S_HANGUP = 0x0020
-S_RDNORM = 0x0040
-S_WRNORM = S_OUTPUT
-S_RDBAND = 0x0080
-S_WRBAND = 0x0100
-S_BANDURG = 0x0200
-RS_HIPRI = 0x01
-STRUIO_POSTPONE = 0x08
-STRUIO_MAPIN = 0x10
-MSG_HIPRI = 0x01
-MSG_ANY = 0x02
-MSG_BAND = 0x04
-MSG_XPG4 = 0x08
-MSG_IPEEK = 0x10
-MSG_DISCARDTAIL = 0x20
-MSG_HOLDSIG = 0x40
-MSG_IGNERROR = 0x80
-MSG_DELAYERROR = 0x100
-MSG_IGNFLOW = 0x200
-MSG_NOMARK = 0x400
-MORECTL = 1
-MOREDATA = 2
-MUXID_ALL = (-1)
-ANYMARK = 0x01
-LASTMARK = 0x02
-_INFTIM = -1
-INFTIM = _INFTIM
diff --git a/Lib/plat-sunos5/TYPES.py b/Lib/plat-sunos5/TYPES.py
deleted file mode 100644
index da4e6b1..0000000
--- a/Lib/plat-sunos5/TYPES.py
+++ /dev/null
@@ -1,313 +0,0 @@
-# Generated by h2py from /usr/include/sys/types.h
-
-# Included from sys/isa_defs.h
-_CHAR_ALIGNMENT = 1
-_SHORT_ALIGNMENT = 2
-_INT_ALIGNMENT = 4
-_LONG_ALIGNMENT = 8
-_LONG_LONG_ALIGNMENT = 8
-_DOUBLE_ALIGNMENT = 8
-_LONG_DOUBLE_ALIGNMENT = 16
-_POINTER_ALIGNMENT = 8
-_MAX_ALIGNMENT = 16
-_ALIGNMENT_REQUIRED = 1
-_CHAR_ALIGNMENT = 1
-_SHORT_ALIGNMENT = 2
-_INT_ALIGNMENT = 4
-_LONG_ALIGNMENT = 4
-_LONG_LONG_ALIGNMENT = 4
-_DOUBLE_ALIGNMENT = 4
-_LONG_DOUBLE_ALIGNMENT = 4
-_POINTER_ALIGNMENT = 4
-_MAX_ALIGNMENT = 4
-_ALIGNMENT_REQUIRED = 0
-_CHAR_ALIGNMENT = 1
-_SHORT_ALIGNMENT = 2
-_INT_ALIGNMENT = 4
-_LONG_LONG_ALIGNMENT = 8
-_DOUBLE_ALIGNMENT = 8
-_ALIGNMENT_REQUIRED = 1
-_LONG_ALIGNMENT = 4
-_LONG_DOUBLE_ALIGNMENT = 8
-_POINTER_ALIGNMENT = 4
-_MAX_ALIGNMENT = 8
-_LONG_ALIGNMENT = 8
-_LONG_DOUBLE_ALIGNMENT = 16
-_POINTER_ALIGNMENT = 8
-_MAX_ALIGNMENT = 16
-
-# Included from sys/feature_tests.h
-_POSIX_C_SOURCE = 1
-_LARGEFILE64_SOURCE = 1
-_LARGEFILE_SOURCE = 1
-_FILE_OFFSET_BITS = 64
-_FILE_OFFSET_BITS = 32
-_POSIX_C_SOURCE = 199506
-_POSIX_PTHREAD_SEMANTICS = 1
-_XOPEN_VERSION = 500
-_XOPEN_VERSION = 4
-_XOPEN_VERSION = 3
-
-# Included from sys/machtypes.h
-
-# Included from sys/inttypes.h
-
-# Included from sys/int_types.h
-
-# Included from sys/int_limits.h
-INT8_MAX = (127)
-INT16_MAX = (32767)
-INT32_MAX = (2147483647)
-INTMAX_MAX = INT32_MAX
-INT_LEAST8_MAX = INT8_MAX
-INT_LEAST16_MAX = INT16_MAX
-INT_LEAST32_MAX = INT32_MAX
-INT8_MIN = (-128)
-INT16_MIN = (-32767-1)
-INT32_MIN = (-2147483647-1)
-INTMAX_MIN = INT32_MIN
-INT_LEAST8_MIN = INT8_MIN
-INT_LEAST16_MIN = INT16_MIN
-INT_LEAST32_MIN = INT32_MIN
-
-# Included from sys/int_const.h
-def INT8_C(c): return (c)
-
-def INT16_C(c): return (c)
-
-def INT32_C(c): return (c)
-
-def INT64_C(c): return __CONCAT__(c,l)
-
-def INT64_C(c): return __CONCAT__(c,ll)
-
-def UINT8_C(c): return __CONCAT__(c,u)
-
-def UINT16_C(c): return __CONCAT__(c,u)
-
-def UINT32_C(c): return __CONCAT__(c,u)
-
-def UINT64_C(c): return __CONCAT__(c,ul)
-
-def UINT64_C(c): return __CONCAT__(c,ull)
-
-def INTMAX_C(c): return __CONCAT__(c,l)
-
-def UINTMAX_C(c): return __CONCAT__(c,ul)
-
-def INTMAX_C(c): return __CONCAT__(c,ll)
-
-def UINTMAX_C(c): return __CONCAT__(c,ull)
-
-def INTMAX_C(c): return (c)
-
-def UINTMAX_C(c): return (c)
-
-
-# Included from sys/int_fmtio.h
-PRId8 = "d"
-PRId16 = "d"
-PRId32 = "d"
-PRId64 = "ld"
-PRId64 = "lld"
-PRIdLEAST8 = "d"
-PRIdLEAST16 = "d"
-PRIdLEAST32 = "d"
-PRIdLEAST64 = "ld"
-PRIdLEAST64 = "lld"
-PRIi8 = "i"
-PRIi16 = "i"
-PRIi32 = "i"
-PRIi64 = "li"
-PRIi64 = "lli"
-PRIiLEAST8 = "i"
-PRIiLEAST16 = "i"
-PRIiLEAST32 = "i"
-PRIiLEAST64 = "li"
-PRIiLEAST64 = "lli"
-PRIo8 = "o"
-PRIo16 = "o"
-PRIo32 = "o"
-PRIo64 = "lo"
-PRIo64 = "llo"
-PRIoLEAST8 = "o"
-PRIoLEAST16 = "o"
-PRIoLEAST32 = "o"
-PRIoLEAST64 = "lo"
-PRIoLEAST64 = "llo"
-PRIx8 = "x"
-PRIx16 = "x"
-PRIx32 = "x"
-PRIx64 = "lx"
-PRIx64 = "llx"
-PRIxLEAST8 = "x"
-PRIxLEAST16 = "x"
-PRIxLEAST32 = "x"
-PRIxLEAST64 = "lx"
-PRIxLEAST64 = "llx"
-PRIX8 = "X"
-PRIX16 = "X"
-PRIX32 = "X"
-PRIX64 = "lX"
-PRIX64 = "llX"
-PRIXLEAST8 = "X"
-PRIXLEAST16 = "X"
-PRIXLEAST32 = "X"
-PRIXLEAST64 = "lX"
-PRIXLEAST64 = "llX"
-PRIu8 = "u"
-PRIu16 = "u"
-PRIu32 = "u"
-PRIu64 = "lu"
-PRIu64 = "llu"
-PRIuLEAST8 = "u"
-PRIuLEAST16 = "u"
-PRIuLEAST32 = "u"
-PRIuLEAST64 = "lu"
-PRIuLEAST64 = "llu"
-SCNd16 = "hd"
-SCNd32 = "d"
-SCNd64 = "ld"
-SCNd64 = "lld"
-SCNi16 = "hi"
-SCNi32 = "i"
-SCNi64 = "li"
-SCNi64 = "lli"
-SCNo16 = "ho"
-SCNo32 = "o"
-SCNo64 = "lo"
-SCNo64 = "llo"
-SCNu16 = "hu"
-SCNu32 = "u"
-SCNu64 = "lu"
-SCNu64 = "llu"
-SCNx16 = "hx"
-SCNx32 = "x"
-SCNx64 = "lx"
-SCNx64 = "llx"
-PRIdMAX = "ld"
-PRIoMAX = "lo"
-PRIxMAX = "lx"
-PRIuMAX = "lu"
-PRIdMAX = "lld"
-PRIoMAX = "llo"
-PRIxMAX = "llx"
-PRIuMAX = "llu"
-PRIdMAX = "d"
-PRIoMAX = "o"
-PRIxMAX = "x"
-PRIuMAX = "u"
-SCNiMAX = "li"
-SCNdMAX = "ld"
-SCNoMAX = "lo"
-SCNxMAX = "lx"
-SCNiMAX = "lli"
-SCNdMAX = "lld"
-SCNoMAX = "llo"
-SCNxMAX = "llx"
-SCNiMAX = "i"
-SCNdMAX = "d"
-SCNoMAX = "o"
-SCNxMAX = "x"
-
-# Included from sys/types32.h
-SHRT_MIN = (-32768)
-SHRT_MAX = 32767
-USHRT_MAX = 65535
-INT_MIN = (-2147483647-1)
-INT_MAX = 2147483647
-LONG_MIN = (-9223372036854775807-1)
-LONG_MAX = 9223372036854775807
-LONG_MIN = (-2147483647-1)
-LONG_MAX = 2147483647
-P_MYID = (-1)
-
-# Included from sys/select.h
-
-# Included from sys/time.h
-TIME32_MAX = INT32_MAX
-TIME32_MIN = INT32_MIN
-def TIMEVAL_OVERFLOW(tv): return \
-
-from TYPES import *
-DST_NONE = 0
-DST_USA = 1
-DST_AUST = 2
-DST_WET = 3
-DST_MET = 4
-DST_EET = 5
-DST_CAN = 6
-DST_GB = 7
-DST_RUM = 8
-DST_TUR = 9
-DST_AUSTALT = 10
-ITIMER_REAL = 0
-ITIMER_VIRTUAL = 1
-ITIMER_PROF = 2
-ITIMER_REALPROF = 3
-def ITIMERVAL_OVERFLOW(itv): return \
-
-SEC = 1
-MILLISEC = 1000
-MICROSEC = 1000000
-NANOSEC = 1000000000
-
-# Included from sys/time_impl.h
-def TIMESPEC_OVERFLOW(ts): return \
-
-def ITIMERSPEC_OVERFLOW(it): return \
-
-__CLOCK_REALTIME0 = 0
-CLOCK_VIRTUAL = 1
-CLOCK_PROF = 2
-__CLOCK_REALTIME3 = 3
-CLOCK_HIGHRES = 4
-CLOCK_MAX = 5
-CLOCK_REALTIME = __CLOCK_REALTIME3
-CLOCK_REALTIME = __CLOCK_REALTIME0
-TIMER_RELTIME = 0x0
-TIMER_ABSTIME = 0x1
-
-# Included from sys/mutex.h
-from TYPES import *
-def MUTEX_HELD(x): return (mutex_owned(x))
-
-def TICK_TO_SEC(tick): return ((tick) / hz)
-
-def SEC_TO_TICK(sec): return ((sec) * hz)
-
-def TICK_TO_MSEC(tick): return \
-
-def MSEC_TO_TICK(msec): return \
-
-def MSEC_TO_TICK_ROUNDUP(msec): return \
-
-def TICK_TO_USEC(tick): return ((tick) * usec_per_tick)
-
-def USEC_TO_TICK(usec): return ((usec) / usec_per_tick)
-
-def USEC_TO_TICK_ROUNDUP(usec): return \
-
-def TICK_TO_NSEC(tick): return ((tick) * nsec_per_tick)
-
-def NSEC_TO_TICK(nsec): return ((nsec) / nsec_per_tick)
-
-def NSEC_TO_TICK_ROUNDUP(nsec): return \
-
-def TIMEVAL_TO_TICK(tvp): return \
-
-def TIMESTRUC_TO_TICK(tsp): return \
-
-
-# Included from time.h
-from TYPES import *
-
-# Included from iso/time_iso.h
-NULL = 0
-NULL = 0
-CLOCKS_PER_SEC = 1000000
-FD_SETSIZE = 65536
-FD_SETSIZE = 1024
-_NBBY = 8
-NBBY = _NBBY
-def FD_ZERO(p): return bzero((p), sizeof (*(p)))
diff --git a/Lib/plat-sunos5/regen b/Lib/plat-sunos5/regen
deleted file mode 100755
index 78cb7de..0000000
--- a/Lib/plat-sunos5/regen
+++ /dev/null
@@ -1,9 +0,0 @@
-#! /bin/sh
-case `uname -sr` in
-'SunOS 5.'*) ;;
-*) echo Probably not on a Solaris 2 system 1>&2
- exit 1;;
-esac
-set -v
-h2py -i '(u_long)' /usr/include/sys/types.h /usr/include/netinet/in.h /usr/include/sys/stropts.h /usr/include/dlfcn.h
-
diff --git a/Lib/plat-unixware7/IN.py b/Lib/plat-unixware7/IN.py
deleted file mode 100644
index af023b4..0000000
--- a/Lib/plat-unixware7/IN.py
+++ /dev/null
@@ -1,836 +0,0 @@
-# Generated by h2py from /usr/include/netinet/in.h
-
-# Included from netinet/in_f.h
-def IN_CLASSA(i): return (((int)(i) & 0x80000000) == 0)
-
-IN_CLASSA_NET = 0xff000000
-IN_CLASSA_NSHIFT = 24
-IN_CLASSA_HOST = 0x00ffffff
-IN_CLASSA_MAX = 128
-def IN_CLASSB(i): return (((int)(i) & 0xc0000000) == 0x80000000)
-
-IN_CLASSB_NET = 0xffff0000
-IN_CLASSB_NSHIFT = 16
-IN_CLASSB_HOST = 0x0000ffff
-IN_CLASSB_MAX = 65536
-def IN_CLASSC(i): return (((int)(i) & 0xe0000000) == 0xc0000000)
-
-IN_CLASSC_NET = 0xffffff00
-IN_CLASSC_NSHIFT = 8
-IN_CLASSC_HOST = 0x000000ff
-def IN_CLASSD(i): return (((int)(i) & 0xf0000000) == 0xe0000000)
-
-IN_CLASSD_NET = 0xf0000000
-IN_CLASSD_NSHIFT = 28
-IN_CLASSD_HOST = 0x0fffffff
-def IN_MULTICAST(i): return IN_CLASSD(i)
-
-def IN_EXPERIMENTAL(i): return (((int)(i) & 0xe0000000) == 0xe0000000)
-
-def IN_BADCLASS(i): return (((int)(i) & 0xf0000000) == 0xf0000000)
-
-INADDR_ANY = 0x00000000
-INADDR_LOOPBACK = 0x7f000001
-INADDR_BROADCAST = 0xffffffff
-INADDR_NONE = 0xffffffff
-IN_LOOPBACKNET = 127
-INADDR_UNSPEC_GROUP = 0xe0000000
-INADDR_ALLHOSTS_GROUP = 0xe0000001
-INADDR_ALLRTRS_GROUP = 0xe0000002
-INADDR_MAX_LOCAL_GROUP = 0xe00000ff
-
-# Included from netinet/in6.h
-
-# Included from sys/types.h
-def quad_low(x): return x.val[0]
-
-ADT_EMASKSIZE = 8
-SHRT_MIN = -32768
-SHRT_MAX = 32767
-INT_MIN = (-2147483647-1)
-INT_MAX = 2147483647
-LONG_MIN = (-2147483647-1)
-LONG_MAX = 2147483647
-OFF32_MAX = LONG_MAX
-ISTAT_ASSERTED = 0
-ISTAT_ASSUMED = 1
-ISTAT_NONE = 2
-OFF_MAX = OFF32_MAX
-CLOCK_MAX = LONG_MAX
-P_MYID = (-1)
-P_MYHOSTID = (-1)
-
-# Included from sys/select.h
-FD_SETSIZE = 4096
-NBBY = 8
-NULL = 0
-
-# Included from sys/bitypes.h
-
-# Included from netinet/in6_f.h
-def IN6_IS_ADDR_UNSPECIFIED(a): return IN6_ADDR_EQUAL_L(a, 0, 0, 0, 0)
-
-def IN6_SET_ADDR_UNSPECIFIED(a): return IN6_ADDR_COPY_L(a, 0, 0, 0, 0)
-
-def IN6_IS_ADDR_ANY(a): return IN6_ADDR_EQUAL_L(a, 0, 0, 0, 0)
-
-def IN6_SET_ADDR_ANY(a): return IN6_ADDR_COPY_L(a, 0, 0, 0, 0)
-
-def IN6_IS_ADDR_LOOPBACK(a): return IN6_ADDR_EQUAL_L(a, 0, 0, 0, 0x01000000)
-
-def IN6_SET_ADDR_LOOPBACK(a): return IN6_ADDR_COPY_L(a, 0, 0, 0, 0x01000000)
-
-IN6_MC_FLAG_PERMANENT = 0x0
-IN6_MC_FLAG_TRANSIENT = 0x1
-IN6_MC_SCOPE_NODELOCAL = 0x1
-IN6_MC_SCOPE_LINKLOCAL = 0x2
-IN6_MC_SCOPE_SITELOCAL = 0x5
-IN6_MC_SCOPE_ORGLOCAL = 0x8
-IN6_MC_SCOPE_GLOBAL = 0xE
-def IN6_IS_ADDR_MC_NODELOCAL(a): return \
-
-def IN6_IS_ADDR_MC_LINKLOCAL(a): return \
-
-def IN6_IS_ADDR_MC_SITELOCAL(a): return \
-
-def IN6_IS_ADDR_MC_ORGLOCAL(a): return \
-
-def IN6_IS_ADDR_MC_GLOBAL(a): return \
-
-
-# Included from sys/convsa.h
-__NETLIB_UW211_SVR4 = 1
-__NETLIB_UW211_XPG4 = 2
-__NETLIB_GEMINI_SVR4 = 3
-__NETLIB_GEMINI_XPG4 = 4
-__NETLIB_FP1_SVR4 = 5
-__NETLIB_FP1_XPG4 = 6
-__NETLIB_BASE_VERSION__ = __NETLIB_UW211_SVR4
-__NETLIB_VERSION__ = __NETLIB_FP1_SVR4
-__NETLIB_VERSION__ = __NETLIB_FP1_XPG4
-__NETLIB_VERSION__ = __NETLIB_GEMINI_SVR4
-__NETLIB_VERSION__ = __NETLIB_GEMINI_XPG4
-__NETLIB_VERSION__ = __NETLIB_UW211_SVR4
-__NETLIB_VERSION__ = __NETLIB_UW211_XPG4
-__NETLIB_VERSION__ = __NETLIB_FP1_XPG4
-
-# Included from sys/byteorder.h
-LITTLE_ENDIAN = 1234
-BIG_ENDIAN = 4321
-PDP_ENDIAN = 3412
-
-# Included from sys/byteorder_f.h
-BYTE_ORDER = LITTLE_ENDIAN
-def htonl(hl): return __htonl(hl)
-
-def ntohl(nl): return __ntohl(nl)
-
-def htons(hs): return __htons(hs)
-
-def ntohs(ns): return __ntohs(ns)
-
-def ntohl(x): return (x)
-
-def ntohs(x): return (x)
-
-def htonl(x): return (x)
-
-def htons(x): return (x)
-
-def __NETLIB_VERSION_IS_XPG4(version): return (((version) % 2) == 0)
-
-def __NETLIB_VERSION_HAS_SALEN(version): return ((version) >= __NETLIB_GEMINI_SVR4)
-
-def __NETLIB_VERSION_IS_IKS(version): return ((version) >= __NETLIB_FP1_SVR4)
-
-def SA_FAMILY_GET(sa): return \
-
-INET6_ADDRSTRLEN = 46
-IPV6_UNICAST_HOPS = 3
-IPV6_ADDRFORM = 24
-IPV6_MULTICAST_HOPS = 25
-IPV6_MULTICAST_IF = 26
-IPV6_MULTICAST_LOOP = 27
-IPV6_ADD_MEMBERSHIP = 28
-IPV6_DROP_MEMBERSHIP = 29
-
-# Included from sys/insrem.h
-def LIST_INIT(head): return \
-
-def LIST_INIT(head): return \
-
-def remque(a): return REMQUE(a)
-
-
-# Included from sys/socket.h
-
-# Included from sys/uio.h
-SHUT_RD = 0
-SHUT_WR = 1
-SHUT_RDWR = 2
-
-# Included from sys/netconfig.h
-
-# Included from sys/cdefs.h
-def __P(protos): return protos
-
-def __STRING(x): return #x
-
-def __P(protos): return ()
-
-def __STRING(x): return "x"
-
-NETCONFIG = "/etc/netconfig"
-NETPATH = "NETPATH"
-NC_TPI_CLTS = 1
-NC_TPI_COTS = 2
-NC_TPI_COTS_ORD = 3
-NC_TPI_RAW = 4
-NC_NOFLAG = 00
-NC_VISIBLE = 0o1
-NC_BROADCAST = 0o2
-NC_NOPROTOFMLY = "-"
-NC_LOOPBACK = "loopback"
-NC_INET = "inet"
-NC_INET6 = "inet6"
-NC_IMPLINK = "implink"
-NC_PUP = "pup"
-NC_CHAOS = "chaos"
-NC_NS = "ns"
-NC_NBS = "nbs"
-NC_ECMA = "ecma"
-NC_DATAKIT = "datakit"
-NC_CCITT = "ccitt"
-NC_SNA = "sna"
-NC_DECNET = "decnet"
-NC_DLI = "dli"
-NC_LAT = "lat"
-NC_HYLINK = "hylink"
-NC_APPLETALK = "appletalk"
-NC_NIT = "nit"
-NC_IEEE802 = "ieee802"
-NC_OSI = "osi"
-NC_X25 = "x25"
-NC_OSINET = "osinet"
-NC_GOSIP = "gosip"
-NC_NETWARE = "netware"
-NC_NOPROTO = "-"
-NC_TCP = "tcp"
-NC_UDP = "udp"
-NC_ICMP = "icmp"
-NC_IPX = "ipx"
-NC_SPX = "spx"
-NC_TPI_CLTS = 1
-NC_TPI_COTS = 2
-NC_TPI_COTS_ORD = 3
-NC_TPI_RAW = 4
-SOCK_STREAM = 2
-SOCK_DGRAM = 1
-SOCK_RAW = 4
-SOCK_RDM = 5
-SOCK_SEQPACKET = 6
-SO_DEBUG = 0x0001
-SO_ACCEPTCONN = 0x0002
-SO_REUSEADDR = 0x0004
-SO_KEEPALIVE = 0x0008
-SO_DONTROUTE = 0x0010
-SO_BROADCAST = 0x0020
-SO_USELOOPBACK = 0x0040
-SO_LINGER = 0x0080
-SO_OOBINLINE = 0x0100
-SO_ORDREL = 0x0200
-SO_IMASOCKET = 0x0400
-SO_MGMT = 0x0800
-SO_REUSEPORT = 0x1000
-SO_LISTENING = 0x2000
-SO_RDWR = 0x4000
-SO_SEMA = 0x8000
-SO_DONTLINGER = (~SO_LINGER)
-SO_SNDBUF = 0x1001
-SO_RCVBUF = 0x1002
-SO_SNDLOWAT = 0x1003
-SO_RCVLOWAT = 0x1004
-SO_SNDTIMEO = 0x1005
-SO_RCVTIMEO = 0x1006
-SO_ERROR = 0x1007
-SO_TYPE = 0x1008
-SO_PROTOTYPE = 0x1009
-SO_ALLRAW = 0x100a
-SOL_SOCKET = 0xffff
-AF_UNSPEC = 0
-AF_UNIX = 1
-AF_LOCAL = AF_UNIX
-AF_INET = 2
-AF_IMPLINK = 3
-AF_PUP = 4
-AF_CHAOS = 5
-AF_NS = 6
-AF_NBS = 7
-AF_ECMA = 8
-AF_DATAKIT = 9
-AF_CCITT = 10
-AF_SNA = 11
-AF_DECnet = 12
-AF_DLI = 13
-AF_LAT = 14
-AF_HYLINK = 15
-AF_APPLETALK = 16
-AF_NIT = 17
-AF_802 = 18
-AF_OSI = 19
-AF_ISO = AF_OSI
-AF_X25 = 20
-AF_OSINET = 21
-AF_GOSIP = 22
-AF_YNET = 23
-AF_ROUTE = 24
-AF_LINK = 25
-pseudo_AF_XTP = 26
-AF_INET6 = 27
-AF_MAX = 27
-AF_INET_BSWAP = 0x0200
-PF_UNSPEC = AF_UNSPEC
-PF_UNIX = AF_UNIX
-PF_LOCAL = AF_LOCAL
-PF_INET = AF_INET
-PF_IMPLINK = AF_IMPLINK
-PF_PUP = AF_PUP
-PF_CHAOS = AF_CHAOS
-PF_NS = AF_NS
-PF_NBS = AF_NBS
-PF_ECMA = AF_ECMA
-PF_DATAKIT = AF_DATAKIT
-PF_CCITT = AF_CCITT
-PF_SNA = AF_SNA
-PF_DECnet = AF_DECnet
-PF_DLI = AF_DLI
-PF_LAT = AF_LAT
-PF_HYLINK = AF_HYLINK
-PF_APPLETALK = AF_APPLETALK
-PF_NIT = AF_NIT
-PF_802 = AF_802
-PF_OSI = AF_OSI
-PF_ISO = PF_OSI
-PF_X25 = AF_X25
-PF_OSINET = AF_OSINET
-PF_GOSIP = AF_GOSIP
-PF_YNET = AF_YNET
-PF_ROUTE = AF_ROUTE
-PF_LINK = AF_LINK
-pseudo_PF_XTP = pseudo_AF_XTP
-PF_INET6 = AF_INET6
-PF_MAX = AF_MAX
-SOMAXCONN = 5
-SCM_RIGHTS = 1
-MSG_OOB = 0x1
-MSG_PEEK = 0x2
-MSG_DONTROUTE = 0x4
-MSG_CTRUNC = 0x8
-MSG_TRUNC = 0x10
-MSG_EOR = 0x30
-MSG_WAITALL = 0x20
-MSG_MAXIOVLEN = 16
-def OPTLEN(x): return ((((x) + sizeof(int) - 1) / sizeof(int)) * sizeof(int))
-
-GIARG = 0x1
-CONTI = 0x2
-GITAB = 0x4
-SOCKETSYS = 88
-SOCKETSYS = 83
-SO_ACCEPT = 1
-SO_BIND = 2
-SO_CONNECT = 3
-SO_GETPEERNAME = 4
-SO_GETSOCKNAME = 5
-SO_GETSOCKOPT = 6
-SO_LISTEN = 7
-SO_RECV = 8
-SO_RECVFROM = 9
-SO_SEND = 10
-SO_SENDTO = 11
-SO_SETSOCKOPT = 12
-SO_SHUTDOWN = 13
-SO_SOCKET = 14
-SO_SOCKPOLL = 15
-SO_GETIPDOMAIN = 16
-SO_SETIPDOMAIN = 17
-SO_ADJTIME = 18
-
-# Included from sys/stream.h
-
-# Included from sys/cred.h
-
-# Included from sys/ksynch.h
-
-# Included from sys/dl.h
-SIGNBIT = 0x80000000
-
-# Included from sys/ipl.h
-
-# Included from sys/disp_p.h
-
-# Included from sys/trap.h
-DIVERR = 0
-SGLSTP = 1
-NMIFLT = 2
-BPTFLT = 3
-INTOFLT = 4
-BOUNDFLT = 5
-INVOPFLT = 6
-NOEXTFLT = 7
-DBLFLT = 8
-EXTOVRFLT = 9
-INVTSSFLT = 10
-SEGNPFLT = 11
-STKFLT = 12
-GPFLT = 13
-PGFLT = 14
-EXTERRFLT = 16
-ALIGNFLT = 17
-MCEFLT = 18
-USERFLT = 0x100
-TRP_PREEMPT = 0x200
-TRP_UNUSED = 0x201
-PF_ERR_MASK = 0x01
-PF_ERR_PAGE = 0
-PF_ERR_PROT = 1
-PF_ERR_WRITE = 2
-PF_ERR_USER = 4
-EVT_STRSCHED = 0x04
-EVT_GLOBCALLOUT = 0x08
-EVT_LCLCALLOUT = 0x10
-EVT_SOFTINTMASK = (EVT_STRSCHED|EVT_GLOBCALLOUT|EVT_LCLCALLOUT)
-PL0 = 0
-PL1 = 1
-PL2 = 2
-PL3 = 3
-PL4 = 4
-PL5 = 5
-PL6 = 6
-PLHI = 8
-PL7 = PLHI
-PLBASE = PL0
-PLTIMEOUT = PL1
-PLDISK = PL5
-PLSTR = PL6
-PLTTY = PLSTR
-PLMIN = PL0
-PLMIN = PL1
-MAX_INTR_LEVELS = 10
-MAX_INTR_NESTING = 50
-STRSCHED = EVT_STRSCHED
-GLOBALSOFTINT = EVT_GLOBCALLOUT
-LOCALSOFTINT = EVT_LCLCALLOUT
-
-# Included from sys/ksynch_p.h
-def GET_TIME(timep): return \
-
-LK_THRESHOLD = 500000
-
-# Included from sys/list.h
-
-# Included from sys/listasm.h
-def remque_null(e): return \
-
-def LS_ISEMPTY(listp): return \
-
-LK_BASIC = 0x1
-LK_SLEEP = 0x2
-LK_NOSTATS = 0x4
-def CYCLES_SINCE(c): return CYCLES_BETWEEN((c), CYCLES())
-
-LSB_NLKDS = 92
-EVT_RUNRUN = 0x01
-EVT_KPRUNRUN = 0x02
-SP_UNLOCKED = 0
-SP_LOCKED = 1
-KS_LOCKTEST = 0x01
-KS_MPSTATS = 0x02
-KS_DEINITED = 0x04
-KS_NVLTTRACE = 0x08
-RWS_READ = (ord('r'))
-RWS_WRITE = (ord('w'))
-RWS_UNLOCKED = (ord('u'))
-RWS_BUSY = (ord('b'))
-def SLEEP_LOCKOWNED(lkp): return \
-
-def SLEEP_DISOWN(lkp): return \
-
-KS_NOPRMPT = 0x00000001
-__KS_LOCKTEST = KS_LOCKTEST
-__KS_LOCKTEST = 0
-__KS_MPSTATS = KS_MPSTATS
-__KS_MPSTATS = 0
-__KS_NVLTTRACE = KS_NVLTTRACE
-__KS_NVLTTRACE = 0
-KSFLAGS = (__KS_LOCKTEST|__KS_MPSTATS|__KS_NVLTTRACE)
-KSVUNIPROC = 1
-KSVMPDEBUG = 2
-KSVMPNODEBUG = 3
-KSVFLAG = KSVUNIPROC
-KSVFLAG = KSVMPDEBUG
-KSVFLAG = KSVMPNODEBUG
-
-# Included from sys/ksinline.h
-_A_SP_LOCKED = 1
-_A_SP_UNLOCKED = 0
-_A_INVPL = -1
-def _ATOMIC_INT_INCR(atomic_intp): return \
-
-def _ATOMIC_INT_DECR(atomic_intp): return \
-
-def ATOMIC_INT_READ(atomic_intp): return _ATOMIC_INT_READ(atomic_intp)
-
-def ATOMIC_INT_INCR(atomic_intp): return _ATOMIC_INT_INCR(atomic_intp)
-
-def ATOMIC_INT_DECR(atomic_intp): return _ATOMIC_INT_DECR(atomic_intp)
-
-def FSPIN_INIT(lp): return
-
-def FSPIN_LOCK(l): return DISABLE()
-
-def FSPIN_TRYLOCK(l): return (DISABLE(), B_TRUE)
-
-def FSPIN_UNLOCK(l): return ENABLE()
-
-def LOCK_DEINIT(lp): return
-
-def LOCK_DEALLOC(lp): return
-
-def LOCK_OWNED(lp): return (B_TRUE)
-
-def RW_DEINIT(lp): return
-
-def RW_DEALLOC(lp): return
-
-def RW_OWNED(lp): return (B_TRUE)
-
-def IS_LOCKED(lockp): return B_FALSE
-
-def LOCK_PLMIN(lockp): return \
-
-def TRYLOCK_PLMIN(lockp): return LOCK_PLMIN(lockp)
-
-def LOCK_SH_PLMIN(lockp): return LOCK_PLMIN(lockp)
-
-def RW_RDLOCK_PLMIN(lockp): return LOCK_PLMIN(lockp)
-
-def RW_WRLOCK_PLMIN(lockp): return LOCK_PLMIN(lockp)
-
-def LOCK_DEINIT(l): return
-
-def LOCK_PLMIN(lockp): return LOCK((lockp), PLMIN)
-
-def TRYLOCK_PLMIN(lockp): return TRYLOCK((lockp), PLMIN)
-
-def LOCK_SH_PLMIN(lockp): return LOCK_SH((lockp), PLMIN)
-
-def RW_RDLOCK_PLMIN(lockp): return RW_RDLOCK((lockp), PLMIN)
-
-def RW_WRLOCK_PLMIN(lockp): return RW_WRLOCK((lockp), PLMIN)
-
-def FSPIN_IS_LOCKED(fsp): return B_FALSE
-
-def SPIN_IS_LOCKED(lockp): return B_FALSE
-
-def FSPIN_OWNED(l): return (B_TRUE)
-
-CR_MLDREAL = 0x00000001
-CR_RDUMP = 0x00000002
-def crhold(credp): return crholdn((credp), 1)
-
-def crfree(credp): return crfreen((credp), 1)
-
-
-# Included from sys/strmdep.h
-def str_aligned(X): return (((uint)(X) & (sizeof(int) - 1)) == 0)
-
-
-# Included from sys/engine.h
-
-# Included from sys/clock.h
-
-# Included from sys/time.h
-DST_NONE = 0
-DST_USA = 1
-DST_AUST = 2
-DST_WET = 3
-DST_MET = 4
-DST_EET = 5
-DST_CAN = 6
-DST_GB = 7
-DST_RUM = 8
-DST_TUR = 9
-DST_AUSTALT = 10
-ITIMER_REAL = 0
-ITIMER_VIRTUAL = 1
-ITIMER_PROF = 2
-FD_SETSIZE = 4096
-FD_NBBY = 8
-
-# Included from time.h
-NULL = 0
-CLOCKS_PER_SEC = 1000000
-
-# Included from sys/clock_p.h
-CGBITS = 4
-IDBITS = 28
-def toid_unpackcg(idval): return (((idval) >> IDBITS) & 0xf)
-
-def toid_unpackid(idval): return ((idval) & 0xfffffff)
-
-def toid_unpackcg(idval): return 0
-
-def toid_unpackid(idval): return (idval)
-
-NCALLOUT_HASH = 1024
-CALLOUT_MAXVAL = 0x7fffffff
-TO_PERIODIC = 0x80000000
-TO_IMMEDIATE = 0x80000000
-SEC = 1
-MILLISEC = 1000
-MICROSEC = 1000000
-NANOSEC = 1000000000
-SECHR = (60*60)
-SECDAY = (24*SECHR)
-SECYR = (365*SECDAY)
-def TIME_OWNED_R(cgnum): return (B_TRUE)
-
-LOOPSECONDS = 1800
-LOOPMICROSECONDS = (LOOPSECONDS * MICROSEC)
-def TICKS_SINCE(t): return TICKS_BETWEEN(t, TICKS())
-
-MAXRQS = 2
-E_OFFLINE = 0x01
-E_BAD = 0x02
-E_SHUTDOWN = 0x04
-E_DRIVER = 0x08
-E_DEFAULTKEEP = 0x100
-E_DRIVERBOUND = 0x200
-E_EXCLUSIVE = 0x400
-E_CGLEADER = 0x800
-E_NOWAY = (E_OFFLINE|E_BAD|E_SHUTDOWN)
-E_BOUND = 0x01
-E_GLOBAL = 0x00
-E_UNAVAIL = -1
-ENGINE_ONLINE = 1
-def PROCESSOR_UNMAP(e): return ((e) - engine)
-
-BOOTENG = 0
-QMOVED = 0x0001
-QWANTR = 0x0002
-QWANTW = 0x0004
-QFULL = 0x0008
-QREADR = 0x0010
-QUSE = 0x0020
-QNOENB = 0x0040
-QUP = 0x0080
-QBACK = 0x0100
-QINTER = 0x0200
-QPROCSON = 0x0400
-QTOENAB = 0x0800
-QFREEZE = 0x1000
-QBOUND = 0x2000
-QDEFCNT = 0x4000
-QENAB = 0x0001
-QSVCBUSY = 0x0002
-STRM_PUTCNT_TABLES = 31
-def STRM_MYENG_PUTCNT(sdp): return STRM_PUTCNT(l.eng_num, sdp)
-
-QB_FULL = 0x01
-QB_WANTW = 0x02
-QB_BACK = 0x04
-NBAND = 256
-DB_WASDUPED = 0x1
-DB_2PIECE = 0x2
-STRLEAKHASHSZ = 1021
-MSGMARK = 0x01
-MSGNOLOOP = 0x02
-MSGDELIM = 0x04
-MSGNOGET = 0x08
-MSGLOG = 0x10
-M_DATA = 0x00
-M_PROTO = 0x01
-M_BREAK = 0x08
-M_PASSFP = 0x09
-M_SIG = 0x0b
-M_DELAY = 0x0c
-M_CTL = 0x0d
-M_IOCTL = 0x0e
-M_SETOPTS = 0x10
-M_RSE = 0x11
-M_TRAIL = 0x12
-M_IOCACK = 0x81
-M_IOCNAK = 0x82
-M_PCPROTO = 0x83
-M_PCSIG = 0x84
-M_READ = 0x85
-M_FLUSH = 0x86
-M_STOP = 0x87
-M_START = 0x88
-M_HANGUP = 0x89
-M_ERROR = 0x8a
-M_COPYIN = 0x8b
-M_COPYOUT = 0x8c
-M_IOCDATA = 0x8d
-M_PCRSE = 0x8e
-M_STOPI = 0x8f
-M_STARTI = 0x90
-M_PCCTL = 0x91
-M_PCSETOPTS = 0x92
-QNORM = 0x00
-QPCTL = 0x80
-STRCANON = 0x01
-RECOPY = 0x02
-SO_ALL = 0x003f
-SO_READOPT = 0x0001
-SO_WROFF = 0x0002
-SO_MINPSZ = 0x0004
-SO_MAXPSZ = 0x0008
-SO_HIWAT = 0x0010
-SO_LOWAT = 0x0020
-SO_MREADON = 0x0040
-SO_MREADOFF = 0x0080
-SO_NDELON = 0x0100
-SO_NDELOFF = 0x0200
-SO_ISTTY = 0x0400
-SO_ISNTTY = 0x0800
-SO_TOSTOP = 0x1000
-SO_TONSTOP = 0x2000
-SO_BAND = 0x4000
-SO_DELIM = 0x8000
-SO_NODELIM = 0x010000
-SO_STRHOLD = 0x020000
-SO_LOOP = 0x040000
-DRVOPEN = 0x0
-MODOPEN = 0x1
-CLONEOPEN = 0x2
-OPENFAIL = -1
-BPRI_LO = 1
-BPRI_MED = 2
-BPRI_HI = 3
-INFPSZ = -1
-FLUSHALL = 1
-FLUSHDATA = 0
-STRHIGH = 5120
-STRLOW = 1024
-MAXIOCBSZ = 1024
-def straln(a): return (caddr_t)((int)(a) & ~(sizeof(int)-1))
-
-IPM_ID = 200
-ICMPM_ID = 201
-TCPM_ID = 202
-UDPM_ID = 203
-ARPM_ID = 204
-APPM_ID = 205
-RIPM_ID = 206
-PPPM_ID = 207
-AHDLCM_ID = 208
-MHDLCRIPM_ID = 209
-HDLCM_ID = 210
-PPCID_ID = 211
-IGMPM_ID = 212
-IPIPM_ID = 213
-IPPROTO_IP = 0
-IPPROTO_HOPOPTS = 0
-IPPROTO_ICMP = 1
-IPPROTO_IGMP = 2
-IPPROTO_GGP = 3
-IPPROTO_IPIP = 4
-IPPROTO_TCP = 6
-IPPROTO_EGP = 8
-IPPROTO_PUP = 12
-IPPROTO_UDP = 17
-IPPROTO_IDP = 22
-IPPROTO_TP = 29
-IPPROTO_IPV6 = 41
-IPPROTO_ROUTING = 43
-IPPROTO_FRAGMENT = 44
-IPPROTO_ESP = 50
-IPPROTO_AH = 51
-IPPROTO_ICMPV6 = 58
-IPPROTO_NONE = 59
-IPPROTO_DSTOPTS = 60
-IPPROTO_HELLO = 63
-IPPROTO_ND = 77
-IPPROTO_EON = 80
-IPPROTO_RAW = 255
-IPPROTO_MAX = 256
-IPPORT_ECHO = 7
-IPPORT_DISCARD = 9
-IPPORT_SYSTAT = 11
-IPPORT_DAYTIME = 13
-IPPORT_NETSTAT = 15
-IPPORT_FTP = 21
-IPPORT_TELNET = 23
-IPPORT_SMTP = 25
-IPPORT_TIMESERVER = 37
-IPPORT_NAMESERVER = 42
-IPPORT_WHOIS = 43
-IPPORT_MTP = 57
-IPPORT_TFTP = 69
-IPPORT_RJE = 77
-IPPORT_FINGER = 79
-IPPORT_TTYLINK = 87
-IPPORT_SUPDUP = 95
-IPPORT_EXECSERVER = 512
-IPPORT_LOGINSERVER = 513
-IPPORT_CMDSERVER = 514
-IPPORT_EFSSERVER = 520
-IPPORT_BIFFUDP = 512
-IPPORT_WHOSERVER = 513
-IPPORT_ROUTESERVER = 520
-IPPORT_RESERVED = 1024
-IPPORT_USERRESERVED = 65535
-IPPORT_RESERVED_LOW = 512
-IPPORT_RESERVED_HIGH = 1023
-IPPORT_USERRESERVED_LOW = 32768
-IPPORT_USERRESERVED_HIGH = 65535
-INET_ADDRSTRLEN = 16
-IP_OPTIONS = 1
-IP_TOS = 2
-IP_TTL = 3
-IP_HDRINCL = 4
-IP_RECVOPTS = 5
-IP_RECVRETOPTS = 6
-IP_RECVDSTADDR = 7
-IP_RETOPTS = 8
-IP_MULTICAST_IF = 9
-IP_MULTICAST_LOOP = 10
-IP_ADD_MEMBERSHIP = 11
-IP_DROP_MEMBERSHIP = 12
-IP_BROADCAST_IF = 14
-IP_RECVIFINDEX = 15
-IP_MULTICAST_TTL = 16
-MRT_INIT = 17
-MRT_DONE = 18
-MRT_ADD_VIF = 19
-MRT_DEL_VIF = 20
-MRT_ADD_MFC = 21
-MRT_DEL_MFC = 22
-MRT_VERSION = 23
-IP_DEFAULT_MULTICAST_TTL = 1
-IP_DEFAULT_MULTICAST_LOOP = 1
-IP_MAX_MEMBERSHIPS = 20
-INADDR_UNSPEC_GROUP = 0xe0000000
-INADDR_ALLHOSTS_GROUP = 0xe0000001
-INADDR_ALLRTRS_GROUP = 0xe0000002
-INADDR_MAX_LOCAL_GROUP = 0xe00000ff
-
-# Included from netinet/in_mp.h
-
-# Included from netinet/in_mp_ddi.h
-
-# Included from sys/inline.h
-IP_HIER_BASE = (20)
-def ASSERT_LOCK(x): return
-
-def ASSERT_WRLOCK(x): return
-
-def ASSERT_UNLOCK(x): return
-
-def CANPUT(q): return canput((q))
-
-def CANPUTNEXT(q): return canputnext((q))
-
-INET_DEBUG = 1
diff --git a/Lib/plat-unixware7/STROPTS.py b/Lib/plat-unixware7/STROPTS.py
deleted file mode 100644
index ef50a9c..0000000
--- a/Lib/plat-unixware7/STROPTS.py
+++ /dev/null
@@ -1,328 +0,0 @@
-# Generated by h2py from /usr/include/sys/stropts.h
-
-# Included from sys/types.h
-def quad_low(x): return x.val[0]
-
-ADT_EMASKSIZE = 8
-SHRT_MIN = -32768
-SHRT_MAX = 32767
-INT_MIN = (-2147483647-1)
-INT_MAX = 2147483647
-LONG_MIN = (-2147483647-1)
-LONG_MAX = 2147483647
-OFF32_MAX = LONG_MAX
-ISTAT_ASSERTED = 0
-ISTAT_ASSUMED = 1
-ISTAT_NONE = 2
-OFF_MAX = OFF32_MAX
-CLOCK_MAX = LONG_MAX
-P_MYID = (-1)
-P_MYHOSTID = (-1)
-
-# Included from sys/select.h
-FD_SETSIZE = 4096
-NBBY = 8
-NULL = 0
-
-# Included from sys/conf.h
-D_NEW = 0x00
-D_OLD = 0x01
-D_DMA = 0x02
-D_BLKOFF = 0x400
-D_LFS = 0x8000
-D_STR = 0x0800
-D_MOD = 0x1000
-D_PSEUDO = 0x2000
-D_RANDOM = 0x4000
-D_HOT = 0x10000
-D_SEEKNEG = 0x04
-D_TAPE = 0x08
-D_NOBRKUP = 0x10
-D_INITPUB = 0x20
-D_NOSPECMACDATA = 0x40
-D_RDWEQ = 0x80
-SECMASK = (D_INITPUB|D_NOSPECMACDATA|D_RDWEQ)
-DAF_REQDMA = 0x1
-DAF_PHYSREQ = 0x2
-DAF_PRE8 = 0x4
-DAF_STATIC = 0x8
-DAF_STR = 0x10
-D_MP = 0x100
-D_UPF = 0x200
-ROOTFS_NAMESZ = 7
-FMNAMESZ = 8
-MCD_VERSION = 1
-DI_BCBP = 0
-DI_MEDIA = 1
-
-# Included from sys/secsys.h
-ES_MACOPENLID = 1
-ES_MACSYSLID = 2
-ES_MACROOTLID = 3
-ES_PRVINFO = 4
-ES_PRVSETCNT = 5
-ES_PRVSETS = 6
-ES_MACADTLID = 7
-ES_PRVID = 8
-ES_TPGETMAJOR = 9
-SA_EXEC = 0o01
-SA_WRITE = 0o02
-SA_READ = 0o04
-SA_SUBSIZE = 0o10
-
-# Included from sys/stropts_f.h
-X_STR = (ord('S')<<8)
-X_I_BASE = (X_STR|0o200)
-X_I_NREAD = (X_STR|0o201)
-X_I_PUSH = (X_STR|0o202)
-X_I_POP = (X_STR|0o203)
-X_I_LOOK = (X_STR|0o204)
-X_I_FLUSH = (X_STR|0o205)
-X_I_SRDOPT = (X_STR|0o206)
-X_I_GRDOPT = (X_STR|0o207)
-X_I_STR = (X_STR|0o210)
-X_I_SETSIG = (X_STR|0o211)
-X_I_GETSIG = (X_STR|0o212)
-X_I_FIND = (X_STR|0o213)
-X_I_LINK = (X_STR|0o214)
-X_I_UNLINK = (X_STR|0o215)
-X_I_PEEK = (X_STR|0o217)
-X_I_FDINSERT = (X_STR|0o220)
-X_I_SENDFD = (X_STR|0o221)
-X_I_RECVFD = (X_STR|0o222)
-
-# Included from unistd.h
-
-# Included from sys/unistd.h
-R_OK = 0o04
-W_OK = 0o02
-X_OK = 0o01
-F_OK = 000
-EFF_ONLY_OK = 0o10
-EX_OK = 0o20
-SEEK_SET = 0
-SEEK_CUR = 1
-SEEK_END = 2
-_SC_ARG_MAX = 1
-_SC_CHILD_MAX = 2
-_SC_CLK_TCK = 3
-_SC_NGROUPS_MAX = 4
-_SC_OPEN_MAX = 5
-_SC_JOB_CONTROL = 6
-_SC_SAVED_IDS = 7
-_SC_VERSION = 8
-_SC_PASS_MAX = 9
-_SC_LOGNAME_MAX = 10
-_SC_PAGESIZE = 11
-_SC_PAGE_SIZE = _SC_PAGESIZE
-_SC_XOPEN_VERSION = 12
-_SC_NACLS_MAX = 13
-_SC_NPROCESSORS_CONF = 14
-_SC_NPROCESSORS_ONLN = 15
-_SC_NPROCESSES = 39
-_SC_TOTAL_MEMORY = 40
-_SC_USEABLE_MEMORY = 41
-_SC_GENERAL_MEMORY = 42
-_SC_DEDICATED_MEMORY = 43
-_SC_NCGS_CONF = 44
-_SC_NCGS_ONLN = 45
-_SC_MAX_CPUS_PER_CG = 46
-_SC_CG_SIMPLE_IMPL = 47
-_SC_CACHE_LINE = 48
-_SC_SYSTEM_ID = 49
-_SC_THREADS = 51
-_SC_THREAD_ATTR_STACKADDR = 52
-_SC_THREAD_ATTR_STACKSIZE = 53
-_SC_THREAD_DESTRUCTOR_ITERATIONS = 54
-_SC_THREAD_KEYS_MAX = 55
-_SC_THREAD_PRIORITY_SCHEDULING = 56
-_SC_THREAD_PRIO_INHERIT = 57
-_SC_THREAD_PRIO_PROTECT = 58
-_SC_THREAD_STACK_MIN = 59
-_SC_THREAD_PROCESS_SHARED = 60
-_SC_THREAD_SAFE_FUNCTIONS = 61
-_SC_THREAD_THREADS_MAX = 62
-_SC_KERNEL_VM = 63
-_SC_TZNAME_MAX = 320
-_SC_STREAM_MAX = 321
-_SC_XOPEN_CRYPT = 323
-_SC_XOPEN_ENH_I18N = 324
-_SC_XOPEN_SHM = 325
-_SC_XOPEN_XCU_VERSION = 327
-_SC_AES_OS_VERSION = 330
-_SC_ATEXIT_MAX = 331
-_SC_2_C_BIND = 350
-_SC_2_C_DEV = 351
-_SC_2_C_VERSION = 352
-_SC_2_CHAR_TERM = 353
-_SC_2_FORT_DEV = 354
-_SC_2_FORT_RUN = 355
-_SC_2_LOCALEDEF = 356
-_SC_2_SW_DEV = 357
-_SC_2_UPE = 358
-_SC_2_VERSION = 359
-_SC_BC_BASE_MAX = 370
-_SC_BC_DIM_MAX = 371
-_SC_BC_SCALE_MAX = 372
-_SC_BC_STRING_MAX = 373
-_SC_COLL_WEIGHTS_MAX = 380
-_SC_EXPR_NEST_MAX = 381
-_SC_LINE_MAX = 382
-_SC_RE_DUP_MAX = 383
-_SC_IOV_MAX = 390
-_SC_NPROC_CONF = 391
-_SC_NPROC_ONLN = 392
-_SC_XOPEN_UNIX = 400
-_SC_SEMAPHORES = 440
-_CS_PATH = 1
-__O_CS_HOSTNAME = 2
-_CS_RELEASE = 3
-_CS_VERSION = 4
-__O_CS_MACHINE = 5
-__O_CS_ARCHITECTURE = 6
-_CS_HW_SERIAL = 7
-__O_CS_HW_PROVIDER = 8
-_CS_SRPC_DOMAIN = 9
-_CS_INITTAB_NAME = 10
-__O_CS_SYSNAME = 11
-_CS_LFS_CFLAGS = 20
-_CS_LFS_LDFLAGS = 21
-_CS_LFS_LIBS = 22
-_CS_LFS_LINTFLAGS = 23
-_CS_LFS64_CFLAGS = 24
-_CS_LFS64_LDFLAGS = 25
-_CS_LFS64_LIBS = 26
-_CS_LFS64_LINTFLAGS = 27
-_CS_ARCHITECTURE = 100
-_CS_BUSTYPES = 101
-_CS_HOSTNAME = 102
-_CS_HW_PROVIDER = 103
-_CS_KERNEL_STAMP = 104
-_CS_MACHINE = 105
-_CS_OS_BASE = 106
-_CS_OS_PROVIDER = 107
-_CS_SYSNAME = 108
-_CS_USER_LIMIT = 109
-_PC_LINK_MAX = 1
-_PC_MAX_CANON = 2
-_PC_MAX_INPUT = 3
-_PC_NAME_MAX = 4
-_PC_PATH_MAX = 5
-_PC_PIPE_BUF = 6
-_PC_NO_TRUNC = 7
-_PC_VDISABLE = 8
-_PC_CHOWN_RESTRICTED = 9
-_PC_FILESIZEBITS = 10
-_POSIX_VERSION = 199009
-_XOPEN_VERSION = 4
-GF_PATH = "/etc/group"
-PF_PATH = "/etc/passwd"
-F_ULOCK = 0
-F_LOCK = 1
-F_TLOCK = 2
-F_TEST = 3
-_POSIX_JOB_CONTROL = 1
-_POSIX_SAVED_IDS = 1
-_POSIX_VDISABLE = 0
-NULL = 0
-STDIN_FILENO = 0
-STDOUT_FILENO = 1
-STDERR_FILENO = 2
-_XOPEN_UNIX = 1
-_XOPEN_ENH_I18N = 1
-_XOPEN_XPG4 = 1
-_POSIX2_C_VERSION = 199209
-_POSIX2_VERSION = 199209
-_XOPEN_XCU_VERSION = 4
-_POSIX_SEMAPHORES = 1
-_POSIX_THREADS = 1
-_POSIX_THREAD_ATTR_STACKADDR = 1
-_POSIX_THREAD_ATTR_STACKSIZE = 1
-_POSIX_THREAD_PRIORITY_SCHEDULING = 1
-_POSIX_THREAD_PROCESS_SHARED = 1
-_POSIX_THREAD_SAFE_FUNCTIONS = 1
-_POSIX2_C_BIND = 1
-_POSIX2_CHAR_TERM = 1
-_POSIX2_FORT_RUN = 1
-_POSIX2_LOCALEDEF = 1
-_POSIX2_UPE = 1
-_LFS_ASYNCHRONOUS_IO = 1
-_LFS_LARGEFILE = 1
-_LFS64_ASYNCHRONOUS_IO = 1
-_LFS64_LARGEFILE = 1
-_LFS64_STDIO = 1
-FMNAMESZ = 8
-SNDZERO = 0x001
-SNDPIPE = 0x002
-RNORM = 0x000
-RMSGD = 0x001
-RMSGN = 0x002
-RMODEMASK = 0x003
-RPROTDAT = 0x004
-RPROTDIS = 0x008
-RPROTNORM = 0x010
-RPROTMASK = 0x01c
-FLUSHR = 0x01
-FLUSHW = 0x02
-FLUSHRW = 0x03
-FLUSHBAND = 0x04
-S_INPUT = 0x0001
-S_HIPRI = 0x0002
-S_OUTPUT = 0x0004
-S_MSG = 0x0008
-S_ERROR = 0x0010
-S_HANGUP = 0x0020
-S_RDNORM = 0x0040
-S_WRNORM = S_OUTPUT
-S_RDBAND = 0x0080
-S_WRBAND = 0x0100
-S_BANDURG = 0x0200
-RS_HIPRI = 0x01
-MSG_HIPRI = 0x01
-MSG_ANY = 0x02
-MSG_BAND = 0x04
-MSG_DISCARD = 0x08
-MSG_PEEKIOCTL = 0x10
-MORECTL = 1
-MOREDATA = 2
-MUXID_ALL = (-1)
-ANYMARK = 0x01
-LASTMARK = 0x02
-STR = (ord('S')<<8)
-I_NREAD = (STR|0o1)
-I_PUSH = (STR|0o2)
-I_POP = (STR|0o3)
-I_LOOK = (STR|0o4)
-I_FLUSH = (STR|0o5)
-I_SRDOPT = (STR|0o6)
-I_GRDOPT = (STR|0o7)
-I_STR = (STR|0o10)
-I_SETSIG = (STR|0o11)
-I_GETSIG = (STR|0o12)
-I_FIND = (STR|0o13)
-I_LINK = (STR|0o14)
-I_UNLINK = (STR|0o15)
-I_PEEK = (STR|0o17)
-I_FDINSERT = (STR|0o20)
-I_SENDFD = (STR|0o21)
-I_RECVFD = (STR|0o22)
-I_E_RECVFD = (STR|0o16)
-I_RECVFD = (STR|0o16)
-I_RECVFD = (STR|0o22)
-I_SWROPT = (STR|0o23)
-I_GWROPT = (STR|0o24)
-I_LIST = (STR|0o25)
-I_PLINK = (STR|0o26)
-I_PUNLINK = (STR|0o27)
-I_FLUSHBAND = (STR|0o34)
-I_CKBAND = (STR|0o35)
-I_GETBAND = (STR|0o36)
-I_ATMARK = (STR|0o37)
-I_SETCLTIME = (STR|0o40)
-I_GETCLTIME = (STR|0o41)
-I_CANPUT = (STR|0o42)
-I_S_RECVFD = (STR|0o43)
-I_STATS = (STR|0o44)
-I_BIGPIPE = (STR|0o45)
-I_GETTP = (STR|0o46)
-INFTIM = -1
diff --git a/Lib/plat-unixware7/regen b/Lib/plat-unixware7/regen
deleted file mode 100755
index 68998a7..0000000
--- a/Lib/plat-unixware7/regen
+++ /dev/null
@@ -1,9 +0,0 @@
-#! /bin/sh
-case `uname -sr` in
-UnixWare*) ;;
-*) echo Probably not on a UnixWare system 1>&2
- exit 1;;
-esac
-set -v
-h2py -i '(u_long)' /usr/include/netinet/in.h
-h2py /usr/include/sys/stropts.h
diff --git a/Lib/platform.py b/Lib/platform.py
index 3e726a7..e48ad0b 100755
--- a/Lib/platform.py
+++ b/Lib/platform.py
@@ -13,7 +13,6 @@
# Python bug tracker (http://bugs.python.org) and assign them to "lemburg".
#
# Still needed:
-# * more support for WinCE
# * support for MS-DOS (PythonDX ?)
# * support for Amiga and other still unsupported platforms running Python
# * support for additional Linux distributions
@@ -252,13 +251,13 @@ def _dist_try_harder(distname, version, id):
_release_filename = re.compile(r'(\w+)[-_](release|version)', re.ASCII)
_lsb_release_version = re.compile(r'(.+)'
- ' release '
- '([\d.]+)'
- '[^(]*(?:\((.+)\))?', re.ASCII)
+ r' release '
+ r'([\d.]+)'
+ r'[^(]*(?:\((.+)\))?', re.ASCII)
_release_version = re.compile(r'([^0-9]+)'
- '(?: release )?'
- '([\d.]+)'
- '[^(]*(?:\((.+)\))?', re.ASCII)
+ r'(?: release )?'
+ r'([\d.]+)'
+ r'[^(]*(?:\((.+)\))?', re.ASCII)
# See also http://www.novell.com/coolsolutions/feature/11251.html
# and http://linuxmafia.com/faq/Admin/release-files.html
@@ -408,8 +407,8 @@ def _norm_version(version, build=''):
return version
_ver_output = re.compile(r'(?:([\w ]+) ([\w.]+) '
- '.*'
- '\[.* ([\d.]+)\])')
+ r'.*'
+ r'\[.* ([\d.]+)\])')
# Examples of VER command output:
#
@@ -509,7 +508,7 @@ def win32_ver(release='', version='', csd='', ptype=''):
from _winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE
winver = getwindowsversion()
- maj, min, build = winver._platform_version or winver[:3]
+ maj, min, build = winver.platform_version or winver[:3]
version = '{0}.{1}.{2}'.format(maj, min, build)
release = (_WIN32_CLIENT_RELEASES.get((maj, min)) or
@@ -1095,22 +1094,22 @@ _sys_version_parser = re.compile(
_ironpython_sys_version_parser = re.compile(
r'IronPython\s*'
- '([\d\.]+)'
- '(?: \(([\d\.]+)\))?'
- ' on (.NET [\d\.]+)', re.ASCII)
+ r'([\d\.]+)'
+ r'(?: \(([\d\.]+)\))?'
+ r' on (.NET [\d\.]+)', re.ASCII)
# IronPython covering 2.6 and 2.7
_ironpython26_sys_version_parser = re.compile(
r'([\d.]+)\s*'
- '\(IronPython\s*'
- '[\d.]+\s*'
- '\(([\d.]+)\) on ([\w.]+ [\d.]+(?: \(\d+-bit\))?)\)'
+ r'\(IronPython\s*'
+ r'[\d.]+\s*'
+ r'\(([\d.]+)\) on ([\w.]+ [\d.]+(?: \(\d+-bit\))?)\)'
)
_pypy_sys_version_parser = re.compile(
r'([\w.+]+)\s*'
- '\(#?([^,]+),\s*([\w ]+),\s*([\w :]+)\)\s*'
- '\[PyPy [^\]]+\]?')
+ r'\(#?([^,]+),\s*([\w ]+),\s*([\w :]+)\)\s*'
+ r'\[PyPy [^\]]+\]?')
_sys_version_cache = {}
@@ -1345,7 +1344,7 @@ def platform(aliased=0, terse=0):
# see issue #1322 for more information
warnings.filterwarnings(
'ignore',
- 'dist\(\) and linux_distribution\(\) '
+ r'dist\(\) and linux_distribution\(\) '
'functions are deprecated .*',
PendingDeprecationWarning,
)
diff --git a/Lib/plistlib.py b/Lib/plistlib.py
index 2502b39..09be5fd 100644
--- a/Lib/plistlib.py
+++ b/Lib/plistlib.py
@@ -47,7 +47,7 @@ Parse Plist example:
"""
__all__ = [
"readPlist", "writePlist", "readPlistFromBytes", "writePlistToBytes",
- "Plist", "Data", "Dict", "FMT_XML", "FMT_BINARY",
+ "Plist", "Data", "Dict", "InvalidFileException", "FMT_XML", "FMT_BINARY",
"load", "dump", "loads", "dumps"
]
diff --git a/Lib/poplib.py b/Lib/poplib.py
index f672390..cae6950 100644
--- a/Lib/poplib.py
+++ b/Lib/poplib.py
@@ -431,6 +431,10 @@ if HAVE_SSL:
if context is not None and certfile is not None:
raise ValueError("context and certfile arguments are mutually "
"exclusive")
+ if keyfile is not None or certfile is not None:
+ import warnings
+ warnings.warn("keyfile and certfile are deprecated, use a"
+ "custom context instead", DeprecationWarning, 2)
self.keyfile = keyfile
self.certfile = certfile
if context is None:
diff --git a/Lib/posixpath.py b/Lib/posixpath.py
index d9f3f99..6dbdab2 100644
--- a/Lib/posixpath.py
+++ b/Lib/posixpath.py
@@ -49,6 +49,7 @@ def _get_sep(path):
def normcase(s):
"""Normalize case of pathname. Has no effect under Posix"""
+ s = os.fspath(s)
if not isinstance(s, (bytes, str)):
raise TypeError("normcase() argument must be str or bytes, "
"not '{}'".format(s.__class__.__name__))
@@ -60,6 +61,7 @@ def normcase(s):
def isabs(s):
"""Test whether a path is absolute"""
+ s = os.fspath(s)
sep = _get_sep(s)
return s.startswith(sep)
@@ -73,12 +75,13 @@ def join(a, *p):
If any component is an absolute path, all previous path components
will be discarded. An empty last part will result in a path that
ends with a separator."""
+ a = os.fspath(a)
sep = _get_sep(a)
path = a
try:
if not p:
path[:0] + sep #23780: Ensure compatible data type even if p is null.
- for b in p:
+ for b in map(os.fspath, p):
if b.startswith(sep):
path = b
elif not path or path.endswith(sep):
@@ -99,6 +102,7 @@ def join(a, *p):
def split(p):
"""Split a pathname. Returns tuple "(head, tail)" where "tail" is
everything after the final slash. Either part may be empty."""
+ p = os.fspath(p)
sep = _get_sep(p)
i = p.rfind(sep) + 1
head, tail = p[:i], p[i:]
@@ -113,6 +117,7 @@ def split(p):
# It is always true that root + ext == p.
def splitext(p):
+ p = os.fspath(p)
if isinstance(p, bytes):
sep = b'/'
extsep = b'.'
@@ -128,6 +133,7 @@ splitext.__doc__ = genericpath._splitext.__doc__
def splitdrive(p):
"""Split a pathname into drive and path. On Posix, drive is always
empty."""
+ p = os.fspath(p)
return p[:0], p
@@ -135,6 +141,7 @@ def splitdrive(p):
def basename(p):
"""Returns the final component of a pathname"""
+ p = os.fspath(p)
sep = _get_sep(p)
i = p.rfind(sep) + 1
return p[i:]
@@ -144,6 +151,7 @@ def basename(p):
def dirname(p):
"""Returns the directory component of a pathname"""
+ p = os.fspath(p)
sep = _get_sep(p)
i = p.rfind(sep) + 1
head = p[:i]
@@ -222,6 +230,7 @@ def ismount(path):
def expanduser(path):
"""Expand ~ and ~user constructions. If user or $HOME is unknown,
do nothing."""
+ path = os.fspath(path)
if isinstance(path, bytes):
tilde = b'~'
else:
@@ -267,6 +276,7 @@ _varprogb = None
def expandvars(path):
"""Expand shell variables of form $var and ${var}. Unknown variables
are left unchanged."""
+ path = os.fspath(path)
global _varprog, _varprogb
if isinstance(path, bytes):
if b'$' not in path:
@@ -318,6 +328,7 @@ def expandvars(path):
def normpath(path):
"""Normalize path, eliminating double slashes, etc."""
+ path = os.fspath(path)
if isinstance(path, bytes):
sep = b'/'
empty = b''
@@ -355,6 +366,7 @@ def normpath(path):
def abspath(path):
"""Return an absolute path."""
+ path = os.fspath(path)
if not isabs(path):
if isinstance(path, bytes):
cwd = os.getcwdb()
@@ -370,6 +382,7 @@ def abspath(path):
def realpath(filename):
"""Return the canonical path of the specified filename, eliminating any
symbolic links encountered in the path."""
+ filename = os.fspath(filename)
path, ok = _joinrealpath(filename[:0], filename, {})
return abspath(path)
@@ -434,6 +447,7 @@ def relpath(path, start=None):
if not path:
raise ValueError("no path specified")
+ path = os.fspath(path)
if isinstance(path, bytes):
curdir = b'.'
sep = b'/'
@@ -445,6 +459,8 @@ def relpath(path, start=None):
if start is None:
start = curdir
+ else:
+ start = os.fspath(start)
try:
start_list = [x for x in abspath(start).split(sep) if x]
@@ -472,6 +488,7 @@ def commonpath(paths):
if not paths:
raise ValueError('commonpath() arg is an empty sequence')
+ paths = tuple(map(os.fspath, paths))
if isinstance(paths[0], bytes):
sep = b'/'
curdir = b'.'
diff --git a/Lib/pyclbr.py b/Lib/pyclbr.py
index 4d40b87..d7dba97 100644
--- a/Lib/pyclbr.py
+++ b/Lib/pyclbr.py
@@ -40,12 +40,10 @@ Instances of this class have the following instance variables:
"""
import io
-import os
import sys
import importlib.util
import tokenize
from token import NAME, DEDENT, OP
-from operator import itemgetter
__all__ = ["readmodule", "readmodule_ex", "Class", "Function"]
@@ -328,6 +326,7 @@ def _getname(g):
def _main():
# Main program for testing.
import os
+ from operator import itemgetter
mod = sys.argv[1]
if os.path.exists(mod):
path = [os.path.dirname(mod)]
diff --git a/Lib/pydoc.py b/Lib/pydoc.py
index 0d0d0ab..39db391 100755..100644
--- a/Lib/pydoc.py
+++ b/Lib/pydoc.py
@@ -209,6 +209,18 @@ def classify_class_attrs(object):
results.append((name, kind, cls, value))
return results
+def sort_attributes(attrs, object):
+ 'Sort the attrs list in-place by _fields and then alphabetically by name'
+ # This allows data descriptors to be ordered according
+ # to a _fields attribute if present.
+ fields = getattr(object, '_fields', [])
+ try:
+ field_order = {name : i-len(fields) for (i, name) in enumerate(fields)}
+ except TypeError:
+ field_order = {}
+ keyfunc = lambda attr: (field_order.get(attr[0], 0), attr[0])
+ attrs.sort(key=keyfunc)
+
# ----------------------------------------------------- module manipulation
def ispackage(path):
@@ -338,7 +350,7 @@ def safeimport(path, forceload=0, cache={}):
elif exc is SyntaxError:
# A SyntaxError occurred before we could execute the module.
raise ErrorDuringImport(value.filename, info)
- elif exc is ImportError and value.name == path:
+ elif issubclass(exc, ImportError) and value.name == path:
# No such module in the path.
return None
else:
@@ -868,8 +880,7 @@ class HTMLDoc(Doc):
object.__module__)
tag += ':<br>\n'
- # Sort attrs by name.
- attrs.sort(key=lambda t: t[0])
+ sort_attributes(attrs, object)
# Pump out the attrs, segregated by kind.
attrs = spill('Methods %s' % tag, attrs,
@@ -1287,8 +1298,8 @@ location listed above.
else:
tag = "inherited from %s" % classname(thisclass,
object.__module__)
- # Sort attrs by name.
- attrs.sort()
+
+ sort_attributes(attrs, object)
# Pump out the attrs, segregated by kind.
attrs = spill("Methods %s:\n" % tag, attrs,
@@ -1418,13 +1429,14 @@ def getpager():
return plainpager
if not sys.stdin.isatty() or not sys.stdout.isatty():
return plainpager
- if 'PAGER' in os.environ:
+ use_pager = os.environ.get('MANPAGER') or os.environ.get('PAGER')
+ if use_pager:
if sys.platform == 'win32': # pipes completely broken in Windows
- return lambda text: tempfilepager(plain(text), os.environ['PAGER'])
+ return lambda text: tempfilepager(plain(text), use_pager)
elif os.environ.get('TERM') in ('dumb', 'emacs'):
- return lambda text: pipepager(plain(text), os.environ['PAGER'])
+ return lambda text: pipepager(plain(text), use_pager)
else:
- return lambda text: pipepager(text, os.environ['PAGER'])
+ return lambda text: pipepager(text, use_pager)
if os.environ.get('TERM') in ('dumb', 'emacs'):
return plainpager
if sys.platform == 'win32':
@@ -1901,10 +1913,10 @@ has the same effect as typing a particular string at the help> prompt.
def intro(self):
self.output.write('''
-Welcome to Python %s's help utility!
+Welcome to Python {0}'s help utility!
If this is your first time using Python, you should definitely check out
-the tutorial on the Internet at http://docs.python.org/%s/tutorial/.
+the tutorial on the Internet at http://docs.python.org/{0}/tutorial/.
Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules. To quit this help utility and
@@ -1914,7 +1926,7 @@ To get a list of available modules, keywords, symbols, or topics, type
"modules", "keywords", "symbols", or "topics". Each module also comes
with a one-line summary of what it does; to list the modules whose name
or summary contain a given string such as "spam", type "modules spam".
-''' % tuple([sys.version[:3]]*2))
+'''.format('%d.%d' % sys.version_info[:2]))
def list(self, items, columns=4, width=80):
items = list(sorted(items))
diff --git a/Lib/pydoc_data/topics.py b/Lib/pydoc_data/topics.py
index 141a999..c7fac33 100644
--- a/Lib/pydoc_data/topics.py
+++ b/Lib/pydoc_data/topics.py
@@ -1,79 +1,12982 @@
# -*- coding: utf-8 -*-
-# Autogenerated by Sphinx on Sun Jan 1 22:05:59 2017
-topics = {'assert': u'\nThe "assert" statement\n**********************\n\nAssert statements are a convenient way to insert debugging assertions\ninto a program:\n\n assert_stmt ::= "assert" expression ["," expression]\n\nThe simple form, "assert expression", is equivalent to\n\n if __debug__:\n if not expression: raise AssertionError\n\nThe extended form, "assert expression1, expression2", is equivalent to\n\n if __debug__:\n if not expression1: raise AssertionError(expression2)\n\nThese equivalences assume that "__debug__" and "AssertionError" refer\nto the built-in variables with those names. In the current\nimplementation, the built-in variable "__debug__" is "True" under\nnormal circumstances, "False" when optimization is requested (command\nline option -O). The current code generator emits no code for an\nassert statement when optimization is requested at compile time. Note\nthat it is unnecessary to include the source code for the expression\nthat failed in the error message; it will be displayed as part of the\nstack trace.\n\nAssignments to "__debug__" are illegal. The value for the built-in\nvariable is determined when the interpreter starts.\n',
- 'assignment': u'\nAssignment statements\n*********************\n\nAssignment statements are used to (re)bind names to values and to\nmodify attributes or items of mutable objects:\n\n assignment_stmt ::= (target_list "=")+ (starred_expression | yield_expression)\n target_list ::= target ("," target)* [","]\n target ::= identifier\n | "(" target_list ")"\n | "[" [target_list] "]"\n | attributeref\n | subscription\n | slicing\n | "*" target\n\n(See section *Primaries* for the syntax definitions for\n*attributeref*, *subscription*, and *slicing*.)\n\nAn assignment statement evaluates the expression list (remember that\nthis can be a single expression or a comma-separated list, the latter\nyielding a tuple) and assigns the single resulting object to each of\nthe target lists, from left to right.\n\nAssignment is defined recursively depending on the form of the target\n(list). When a target is part of a mutable object (an attribute\nreference, subscription or slicing), the mutable object must\nultimately perform the assignment and decide about its validity, and\nmay raise an exception if the assignment is unacceptable. The rules\nobserved by various types and the exceptions raised are given with the\ndefinition of the object types (see section *The standard type\nhierarchy*).\n\nAssignment of an object to a target list, optionally enclosed in\nparentheses or square brackets, is recursively defined as follows.\n\n* If the target list is empty: The object must also be an empty\n iterable.\n\n* If the target list is a single target in parentheses: The object\n is assigned to that target.\n\n* If the target list is a comma-separated list of targets, or a\n single target in square brackets: The object must be an iterable\n with the same number of items as there are targets in the target\n list, and the items are assigned, from left to right, to the\n corresponding targets.\n\n * If the target list contains one target prefixed with an\n asterisk, called a "starred" target: The object must be an\n iterable with at least as many items as there are targets in the\n target list, minus one. The first items of the iterable are\n assigned, from left to right, to the targets before the starred\n target. The final items of the iterable are assigned to the\n targets after the starred target. A list of the remaining items\n in the iterable is then assigned to the starred target (the list\n can be empty).\n\n * Else: The object must be an iterable with the same number of\n items as there are targets in the target list, and the items are\n assigned, from left to right, to the corresponding targets.\n\nAssignment of an object to a single target is recursively defined as\nfollows.\n\n* If the target is an identifier (name):\n\n * If the name does not occur in a "global" or "nonlocal" statement\n in the current code block: the name is bound to the object in the\n current local namespace.\n\n * Otherwise: the name is bound to the object in the global\n namespace or the outer namespace determined by "nonlocal",\n respectively.\n\n The name is rebound if it was already bound. This may cause the\n reference count for the object previously bound to the name to reach\n zero, causing the object to be deallocated and its destructor (if it\n has one) to be called.\n\n* If the target is an attribute reference: The primary expression in\n the reference is evaluated. It should yield an object with\n assignable attributes; if this is not the case, "TypeError" is\n raised. That object is then asked to assign the assigned object to\n the given attribute; if it cannot perform the assignment, it raises\n an exception (usually but not necessarily "AttributeError").\n\n Note: If the object is a class instance and the attribute reference\n occurs on both sides of the assignment operator, the RHS expression,\n "a.x" can access either an instance attribute or (if no instance\n attribute exists) a class attribute. The LHS target "a.x" is always\n set as an instance attribute, creating it if necessary. Thus, the\n two occurrences of "a.x" do not necessarily refer to the same\n attribute: if the RHS expression refers to a class attribute, the\n LHS creates a new instance attribute as the target of the\n assignment:\n\n class Cls:\n x = 3 # class variable\n inst = Cls()\n inst.x = inst.x + 1 # writes inst.x as 4 leaving Cls.x as 3\n\n This description does not necessarily apply to descriptor\n attributes, such as properties created with "property()".\n\n* If the target is a subscription: The primary expression in the\n reference is evaluated. It should yield either a mutable sequence\n object (such as a list) or a mapping object (such as a dictionary).\n Next, the subscript expression is evaluated.\n\n If the primary is a mutable sequence object (such as a list), the\n subscript must yield an integer. If it is negative, the sequence\'s\n length is added to it. The resulting value must be a nonnegative\n integer less than the sequence\'s length, and the sequence is asked\n to assign the assigned object to its item with that index. If the\n index is out of range, "IndexError" is raised (assignment to a\n subscripted sequence cannot add new items to a list).\n\n If the primary is a mapping object (such as a dictionary), the\n subscript must have a type compatible with the mapping\'s key type,\n and the mapping is then asked to create a key/datum pair which maps\n the subscript to the assigned object. This can either replace an\n existing key/value pair with the same key value, or insert a new\n key/value pair (if no key with the same value existed).\n\n For user-defined objects, the "__setitem__()" method is called with\n appropriate arguments.\n\n* If the target is a slicing: The primary expression in the\n reference is evaluated. It should yield a mutable sequence object\n (such as a list). The assigned object should be a sequence object\n of the same type. Next, the lower and upper bound expressions are\n evaluated, insofar they are present; defaults are zero and the\n sequence\'s length. The bounds should evaluate to integers. If\n either bound is negative, the sequence\'s length is added to it. The\n resulting bounds are clipped to lie between zero and the sequence\'s\n length, inclusive. Finally, the sequence object is asked to replace\n the slice with the items of the assigned sequence. The length of\n the slice may be different from the length of the assigned sequence,\n thus changing the length of the target sequence, if the target\n sequence allows it.\n\n**CPython implementation detail:** In the current implementation, the\nsyntax for targets is taken to be the same as for expressions, and\ninvalid syntax is rejected during the code generation phase, causing\nless detailed error messages.\n\nAlthough the definition of assignment implies that overlaps between\nthe left-hand side and the right-hand side are \'simultaneous\' (for\nexample "a, b = b, a" swaps two variables), overlaps *within* the\ncollection of assigned-to variables occur left-to-right, sometimes\nresulting in confusion. For instance, the following program prints\n"[0, 2]":\n\n x = [0, 1]\n i = 0\n i, x[i] = 1, 2 # i is updated, then x[i] is updated\n print(x)\n\nSee also: **PEP 3132** - Extended Iterable Unpacking\n\n The specification for the "*target" feature.\n\n\nAugmented assignment statements\n===============================\n\nAugmented assignment is the combination, in a single statement, of a\nbinary operation and an assignment statement:\n\n augmented_assignment_stmt ::= augtarget augop (expression_list | yield_expression)\n augtarget ::= identifier | attributeref | subscription | slicing\n augop ::= "+=" | "-=" | "*=" | "@=" | "/=" | "//=" | "%=" | "**="\n | ">>=" | "<<=" | "&=" | "^=" | "|="\n\n(See section *Primaries* for the syntax definitions of the last three\nsymbols.)\n\nAn augmented assignment evaluates the target (which, unlike normal\nassignment statements, cannot be an unpacking) and the expression\nlist, performs the binary operation specific to the type of assignment\non the two operands, and assigns the result to the original target.\nThe target is only evaluated once.\n\nAn augmented assignment expression like "x += 1" can be rewritten as\n"x = x + 1" to achieve a similar, but not exactly equal effect. In the\naugmented version, "x" is only evaluated once. Also, when possible,\nthe actual operation is performed *in-place*, meaning that rather than\ncreating a new object and assigning that to the target, the old object\nis modified instead.\n\nUnlike normal assignments, augmented assignments evaluate the left-\nhand side *before* evaluating the right-hand side. For example, "a[i]\n+= f(x)" first looks-up "a[i]", then it evaluates "f(x)" and performs\nthe addition, and lastly, it writes the result back to "a[i]".\n\nWith the exception of assigning to tuples and multiple targets in a\nsingle statement, the assignment done by augmented assignment\nstatements is handled the same way as normal assignments. Similarly,\nwith the exception of the possible *in-place* behavior, the binary\noperation performed by augmented assignment is the same as the normal\nbinary operations.\n\nFor targets which are attribute references, the same *caveat about\nclass and instance attributes* applies as for regular assignments.\n',
- 'atom-identifiers': u'\nIdentifiers (Names)\n*******************\n\nAn identifier occurring as an atom is a name. See section\n*Identifiers and keywords* for lexical definition and section *Naming\nand binding* for documentation of naming and binding.\n\nWhen the name is bound to an object, evaluation of the atom yields\nthat object. When a name is not bound, an attempt to evaluate it\nraises a "NameError" exception.\n\n**Private name mangling:** When an identifier that textually occurs in\na class definition begins with two or more underscore characters and\ndoes not end in two or more underscores, it is considered a *private\nname* of that class. Private names are transformed to a longer form\nbefore code is generated for them. The transformation inserts the\nclass name, with leading underscores removed and a single underscore\ninserted, in front of the name. For example, the identifier "__spam"\noccurring in a class named "Ham" will be transformed to "_Ham__spam".\nThis transformation is independent of the syntactical context in which\nthe identifier is used. If the transformed name is extremely long\n(longer than 255 characters), implementation defined truncation may\nhappen. If the class name consists only of underscores, no\ntransformation is done.\n',
- 'atom-literals': u"\nLiterals\n********\n\nPython supports string and bytes literals and various numeric\nliterals:\n\n literal ::= stringliteral | bytesliteral\n | integer | floatnumber | imagnumber\n\nEvaluation of a literal yields an object of the given type (string,\nbytes, integer, floating point number, complex number) with the given\nvalue. The value may be approximated in the case of floating point\nand imaginary (complex) literals. See section *Literals* for details.\n\nAll literals correspond to immutable data types, and hence the\nobject's identity is less important than its value. Multiple\nevaluations of literals with the same value (either the same\noccurrence in the program text or a different occurrence) may obtain\nthe same object or a different object with the same value.\n",
- 'attribute-access': u'\nCustomizing attribute access\n****************************\n\nThe following methods can be defined to customize the meaning of\nattribute access (use of, assignment to, or deletion of "x.name") for\nclass instances.\n\nobject.__getattr__(self, name)\n\n Called when an attribute lookup has not found the attribute in the\n usual places (i.e. it is not an instance attribute nor is it found\n in the class tree for "self"). "name" is the attribute name. This\n method should return the (computed) attribute value or raise an\n "AttributeError" exception.\n\n Note that if the attribute is found through the normal mechanism,\n "__getattr__()" is not called. (This is an intentional asymmetry\n between "__getattr__()" and "__setattr__()".) This is done both for\n efficiency reasons and because otherwise "__getattr__()" would have\n no way to access other attributes of the instance. Note that at\n least for instance variables, you can fake total control by not\n inserting any values in the instance attribute dictionary (but\n instead inserting them in another object). See the\n "__getattribute__()" method below for a way to actually get total\n control over attribute access.\n\nobject.__getattribute__(self, name)\n\n Called unconditionally to implement attribute accesses for\n instances of the class. If the class also defines "__getattr__()",\n the latter will not be called unless "__getattribute__()" either\n calls it explicitly or raises an "AttributeError". This method\n should return the (computed) attribute value or raise an\n "AttributeError" exception. In order to avoid infinite recursion in\n this method, its implementation should always call the base class\n method with the same name to access any attributes it needs, for\n example, "object.__getattribute__(self, name)".\n\n Note: This method may still be bypassed when looking up special\n methods as the result of implicit invocation via language syntax\n or built-in functions. See *Special method lookup*.\n\nobject.__setattr__(self, name, value)\n\n Called when an attribute assignment is attempted. This is called\n instead of the normal mechanism (i.e. store the value in the\n instance dictionary). *name* is the attribute name, *value* is the\n value to be assigned to it.\n\n If "__setattr__()" wants to assign to an instance attribute, it\n should call the base class method with the same name, for example,\n "object.__setattr__(self, name, value)".\n\nobject.__delattr__(self, name)\n\n Like "__setattr__()" but for attribute deletion instead of\n assignment. This should only be implemented if "del obj.name" is\n meaningful for the object.\n\nobject.__dir__(self)\n\n Called when "dir()" is called on the object. A sequence must be\n returned. "dir()" converts the returned sequence to a list and\n sorts it.\n\n\nImplementing Descriptors\n========================\n\nThe following methods only apply when an instance of the class\ncontaining the method (a so-called *descriptor* class) appears in an\n*owner* class (the descriptor must be in either the owner\'s class\ndictionary or in the class dictionary for one of its parents). In the\nexamples below, "the attribute" refers to the attribute whose name is\nthe key of the property in the owner class\' "__dict__".\n\nobject.__get__(self, instance, owner)\n\n Called to get the attribute of the owner class (class attribute\n access) or of an instance of that class (instance attribute\n access). *owner* is always the owner class, while *instance* is the\n instance that the attribute was accessed through, or "None" when\n the attribute is accessed through the *owner*. This method should\n return the (computed) attribute value or raise an "AttributeError"\n exception.\n\nobject.__set__(self, instance, value)\n\n Called to set the attribute on an instance *instance* of the owner\n class to a new value, *value*.\n\nobject.__delete__(self, instance)\n\n Called to delete the attribute on an instance *instance* of the\n owner class.\n\nThe attribute "__objclass__" is interpreted by the "inspect" module as\nspecifying the class where this object was defined (setting this\nappropriately can assist in runtime introspection of dynamic class\nattributes). For callables, it may indicate that an instance of the\ngiven type (or a subclass) is expected or required as the first\npositional argument (for example, CPython sets this attribute for\nunbound methods that are implemented in C).\n\n\nInvoking Descriptors\n====================\n\nIn general, a descriptor is an object attribute with "binding\nbehavior", one whose attribute access has been overridden by methods\nin the descriptor protocol: "__get__()", "__set__()", and\n"__delete__()". If any of those methods are defined for an object, it\nis said to be a descriptor.\n\nThe default behavior for attribute access is to get, set, or delete\nthe attribute from an object\'s dictionary. For instance, "a.x" has a\nlookup chain starting with "a.__dict__[\'x\']", then\n"type(a).__dict__[\'x\']", and continuing through the base classes of\n"type(a)" excluding metaclasses.\n\nHowever, if the looked-up value is an object defining one of the\ndescriptor methods, then Python may override the default behavior and\ninvoke the descriptor method instead. Where this occurs in the\nprecedence chain depends on which descriptor methods were defined and\nhow they were called.\n\nThe starting point for descriptor invocation is a binding, "a.x". How\nthe arguments are assembled depends on "a":\n\nDirect Call\n The simplest and least common call is when user code directly\n invokes a descriptor method: "x.__get__(a)".\n\nInstance Binding\n If binding to an object instance, "a.x" is transformed into the\n call: "type(a).__dict__[\'x\'].__get__(a, type(a))".\n\nClass Binding\n If binding to a class, "A.x" is transformed into the call:\n "A.__dict__[\'x\'].__get__(None, A)".\n\nSuper Binding\n If "a" is an instance of "super", then the binding "super(B,\n obj).m()" searches "obj.__class__.__mro__" for the base class "A"\n immediately preceding "B" and then invokes the descriptor with the\n call: "A.__dict__[\'m\'].__get__(obj, obj.__class__)".\n\nFor instance bindings, the precedence of descriptor invocation depends\non the which descriptor methods are defined. A descriptor can define\nany combination of "__get__()", "__set__()" and "__delete__()". If it\ndoes not define "__get__()", then accessing the attribute will return\nthe descriptor object itself unless there is a value in the object\'s\ninstance dictionary. If the descriptor defines "__set__()" and/or\n"__delete__()", it is a data descriptor; if it defines neither, it is\na non-data descriptor. Normally, data descriptors define both\n"__get__()" and "__set__()", while non-data descriptors have just the\n"__get__()" method. Data descriptors with "__set__()" and "__get__()"\ndefined always override a redefinition in an instance dictionary. In\ncontrast, non-data descriptors can be overridden by instances.\n\nPython methods (including "staticmethod()" and "classmethod()") are\nimplemented as non-data descriptors. Accordingly, instances can\nredefine and override methods. This allows individual instances to\nacquire behaviors that differ from other instances of the same class.\n\nThe "property()" function is implemented as a data descriptor.\nAccordingly, instances cannot override the behavior of a property.\n\n\n__slots__\n=========\n\nBy default, instances of classes have a dictionary for attribute\nstorage. This wastes space for objects having very few instance\nvariables. The space consumption can become acute when creating large\nnumbers of instances.\n\nThe default can be overridden by defining *__slots__* in a class\ndefinition. The *__slots__* declaration takes a sequence of instance\nvariables and reserves just enough space in each instance to hold a\nvalue for each variable. Space is saved because *__dict__* is not\ncreated for each instance.\n\nobject.__slots__\n\n This class variable can be assigned a string, iterable, or sequence\n of strings with variable names used by instances. *__slots__*\n reserves space for the declared variables and prevents the\n automatic creation of *__dict__* and *__weakref__* for each\n instance.\n\n\nNotes on using *__slots__*\n--------------------------\n\n* When inheriting from a class without *__slots__*, the *__dict__*\n attribute of that class will always be accessible, so a *__slots__*\n definition in the subclass is meaningless.\n\n* Without a *__dict__* variable, instances cannot be assigned new\n variables not listed in the *__slots__* definition. Attempts to\n assign to an unlisted variable name raises "AttributeError". If\n dynamic assignment of new variables is desired, then add\n "\'__dict__\'" to the sequence of strings in the *__slots__*\n declaration.\n\n* Without a *__weakref__* variable for each instance, classes\n defining *__slots__* do not support weak references to its\n instances. If weak reference support is needed, then add\n "\'__weakref__\'" to the sequence of strings in the *__slots__*\n declaration.\n\n* *__slots__* are implemented at the class level by creating\n descriptors (*Implementing Descriptors*) for each variable name. As\n a result, class attributes cannot be used to set default values for\n instance variables defined by *__slots__*; otherwise, the class\n attribute would overwrite the descriptor assignment.\n\n* The action of a *__slots__* declaration is limited to the class\n where it is defined. As a result, subclasses will have a *__dict__*\n unless they also define *__slots__* (which must only contain names\n of any *additional* slots).\n\n* If a class defines a slot also defined in a base class, the\n instance variable defined by the base class slot is inaccessible\n (except by retrieving its descriptor directly from the base class).\n This renders the meaning of the program undefined. In the future, a\n check may be added to prevent this.\n\n* Nonempty *__slots__* does not work for classes derived from\n "variable-length" built-in types such as "int", "bytes" and "tuple".\n\n* Any non-string iterable may be assigned to *__slots__*. Mappings\n may also be used; however, in the future, special meaning may be\n assigned to the values corresponding to each key.\n\n* *__class__* assignment works only if both classes have the same\n *__slots__*.\n',
- 'attribute-references': u'\nAttribute references\n********************\n\nAn attribute reference is a primary followed by a period and a name:\n\n attributeref ::= primary "." identifier\n\nThe primary must evaluate to an object of a type that supports\nattribute references, which most objects do. This object is then\nasked to produce the attribute whose name is the identifier. This\nproduction can be customized by overriding the "__getattr__()" method.\nIf this attribute is not available, the exception "AttributeError" is\nraised. Otherwise, the type and value of the object produced is\ndetermined by the object. Multiple evaluations of the same attribute\nreference may yield different objects.\n',
- 'augassign': u'\nAugmented assignment statements\n*******************************\n\nAugmented assignment is the combination, in a single statement, of a\nbinary operation and an assignment statement:\n\n augmented_assignment_stmt ::= augtarget augop (expression_list | yield_expression)\n augtarget ::= identifier | attributeref | subscription | slicing\n augop ::= "+=" | "-=" | "*=" | "@=" | "/=" | "//=" | "%=" | "**="\n | ">>=" | "<<=" | "&=" | "^=" | "|="\n\n(See section *Primaries* for the syntax definitions of the last three\nsymbols.)\n\nAn augmented assignment evaluates the target (which, unlike normal\nassignment statements, cannot be an unpacking) and the expression\nlist, performs the binary operation specific to the type of assignment\non the two operands, and assigns the result to the original target.\nThe target is only evaluated once.\n\nAn augmented assignment expression like "x += 1" can be rewritten as\n"x = x + 1" to achieve a similar, but not exactly equal effect. In the\naugmented version, "x" is only evaluated once. Also, when possible,\nthe actual operation is performed *in-place*, meaning that rather than\ncreating a new object and assigning that to the target, the old object\nis modified instead.\n\nUnlike normal assignments, augmented assignments evaluate the left-\nhand side *before* evaluating the right-hand side. For example, "a[i]\n+= f(x)" first looks-up "a[i]", then it evaluates "f(x)" and performs\nthe addition, and lastly, it writes the result back to "a[i]".\n\nWith the exception of assigning to tuples and multiple targets in a\nsingle statement, the assignment done by augmented assignment\nstatements is handled the same way as normal assignments. Similarly,\nwith the exception of the possible *in-place* behavior, the binary\noperation performed by augmented assignment is the same as the normal\nbinary operations.\n\nFor targets which are attribute references, the same *caveat about\nclass and instance attributes* applies as for regular assignments.\n',
- 'binary': u'\nBinary arithmetic operations\n****************************\n\nThe binary arithmetic operations have the conventional priority\nlevels. Note that some of these operations also apply to certain non-\nnumeric types. Apart from the power operator, there are only two\nlevels, one for multiplicative operators and one for additive\noperators:\n\n m_expr ::= u_expr | m_expr "*" u_expr | m_expr "@" m_expr |\n m_expr "//" u_expr| m_expr "/" u_expr |\n m_expr "%" u_expr\n a_expr ::= m_expr | a_expr "+" m_expr | a_expr "-" m_expr\n\nThe "*" (multiplication) operator yields the product of its arguments.\nThe arguments must either both be numbers, or one argument must be an\ninteger and the other must be a sequence. In the former case, the\nnumbers are converted to a common type and then multiplied together.\nIn the latter case, sequence repetition is performed; a negative\nrepetition factor yields an empty sequence.\n\nThe "@" (at) operator is intended to be used for matrix\nmultiplication. No builtin Python types implement this operator.\n\nNew in version 3.5.\n\nThe "/" (division) and "//" (floor division) operators yield the\nquotient of their arguments. The numeric arguments are first\nconverted to a common type. Division of integers yields a float, while\nfloor division of integers results in an integer; the result is that\nof mathematical division with the \'floor\' function applied to the\nresult. Division by zero raises the "ZeroDivisionError" exception.\n\nThe "%" (modulo) operator yields the remainder from the division of\nthe first argument by the second. The numeric arguments are first\nconverted to a common type. A zero right argument raises the\n"ZeroDivisionError" exception. The arguments may be floating point\nnumbers, e.g., "3.14%0.7" equals "0.34" (since "3.14" equals "4*0.7 +\n0.34".) The modulo operator always yields a result with the same sign\nas its second operand (or zero); the absolute value of the result is\nstrictly smaller than the absolute value of the second operand [1].\n\nThe floor division and modulo operators are connected by the following\nidentity: "x == (x//y)*y + (x%y)". Floor division and modulo are also\nconnected with the built-in function "divmod()": "divmod(x, y) ==\n(x//y, x%y)". [2].\n\nIn addition to performing the modulo operation on numbers, the "%"\noperator is also overloaded by string objects to perform old-style\nstring formatting (also known as interpolation). The syntax for\nstring formatting is described in the Python Library Reference,\nsection *printf-style String Formatting*.\n\nThe floor division operator, the modulo operator, and the "divmod()"\nfunction are not defined for complex numbers. Instead, convert to a\nfloating point number using the "abs()" function if appropriate.\n\nThe "+" (addition) operator yields the sum of its arguments. The\narguments must either both be numbers or both be sequences of the same\ntype. In the former case, the numbers are converted to a common type\nand then added together. In the latter case, the sequences are\nconcatenated.\n\nThe "-" (subtraction) operator yields the difference of its arguments.\nThe numeric arguments are first converted to a common type.\n',
- 'bitwise': u'\nBinary bitwise operations\n*************************\n\nEach of the three bitwise operations has a different priority level:\n\n and_expr ::= shift_expr | and_expr "&" shift_expr\n xor_expr ::= and_expr | xor_expr "^" and_expr\n or_expr ::= xor_expr | or_expr "|" xor_expr\n\nThe "&" operator yields the bitwise AND of its arguments, which must\nbe integers.\n\nThe "^" operator yields the bitwise XOR (exclusive OR) of its\narguments, which must be integers.\n\nThe "|" operator yields the bitwise (inclusive) OR of its arguments,\nwhich must be integers.\n',
- 'bltin-code-objects': u'\nCode Objects\n************\n\nCode objects are used by the implementation to represent "pseudo-\ncompiled" executable Python code such as a function body. They differ\nfrom function objects because they don\'t contain a reference to their\nglobal execution environment. Code objects are returned by the built-\nin "compile()" function and can be extracted from function objects\nthrough their "__code__" attribute. See also the "code" module.\n\nA code object can be executed or evaluated by passing it (instead of a\nsource string) to the "exec()" or "eval()" built-in functions.\n\nSee *The standard type hierarchy* for more information.\n',
- 'bltin-ellipsis-object': u'\nThe Ellipsis Object\n*******************\n\nThis object is commonly used by slicing (see *Slicings*). It supports\nno special operations. There is exactly one ellipsis object, named\n"Ellipsis" (a built-in name). "type(Ellipsis)()" produces the\n"Ellipsis" singleton.\n\nIt is written as "Ellipsis" or "...".\n',
- 'bltin-null-object': u'\nThe Null Object\n***************\n\nThis object is returned by functions that don\'t explicitly return a\nvalue. It supports no special operations. There is exactly one null\nobject, named "None" (a built-in name). "type(None)()" produces the\nsame singleton.\n\nIt is written as "None".\n',
- 'bltin-type-objects': u'\nType Objects\n************\n\nType objects represent the various object types. An object\'s type is\naccessed by the built-in function "type()". There are no special\noperations on types. The standard module "types" defines names for\nall standard built-in types.\n\nTypes are written like this: "<class \'int\'>".\n',
- 'booleans': u'\nBoolean operations\n******************\n\n or_test ::= and_test | or_test "or" and_test\n and_test ::= not_test | and_test "and" not_test\n not_test ::= comparison | "not" not_test\n\nIn the context of Boolean operations, and also when expressions are\nused by control flow statements, the following values are interpreted\nas false: "False", "None", numeric zero of all types, and empty\nstrings and containers (including strings, tuples, lists,\ndictionaries, sets and frozensets). All other values are interpreted\nas true. User-defined objects can customize their truth value by\nproviding a "__bool__()" method.\n\nThe operator "not" yields "True" if its argument is false, "False"\notherwise.\n\nThe expression "x and y" first evaluates *x*; if *x* is false, its\nvalue is returned; otherwise, *y* is evaluated and the resulting value\nis returned.\n\nThe expression "x or y" first evaluates *x*; if *x* is true, its value\nis returned; otherwise, *y* is evaluated and the resulting value is\nreturned.\n\n(Note that neither "and" nor "or" restrict the value and type they\nreturn to "False" and "True", but rather return the last evaluated\nargument. This is sometimes useful, e.g., if "s" is a string that\nshould be replaced by a default value if it is empty, the expression\n"s or \'foo\'" yields the desired value. Because "not" has to create a\nnew value, it returns a boolean value regardless of the type of its\nargument (for example, "not \'foo\'" produces "False" rather than "\'\'".)\n',
- 'break': u'\nThe "break" statement\n*********************\n\n break_stmt ::= "break"\n\n"break" may only occur syntactically nested in a "for" or "while"\nloop, but not nested in a function or class definition within that\nloop.\n\nIt terminates the nearest enclosing loop, skipping the optional "else"\nclause if the loop has one.\n\nIf a "for" loop is terminated by "break", the loop control target\nkeeps its current value.\n\nWhen "break" passes control out of a "try" statement with a "finally"\nclause, that "finally" clause is executed before really leaving the\nloop.\n',
- 'callable-types': u'\nEmulating callable objects\n**************************\n\nobject.__call__(self[, args...])\n\n Called when the instance is "called" as a function; if this method\n is defined, "x(arg1, arg2, ...)" is a shorthand for\n "x.__call__(arg1, arg2, ...)".\n',
- 'calls': u'\nCalls\n*****\n\nA call calls a callable object (e.g., a *function*) with a possibly\nempty series of *arguments*:\n\n call ::= primary "(" [argument_list [","] | comprehension] ")"\n argument_list ::= positional_arguments ["," starred_and_keywords]\n ["," keywords_arguments]\n | starred_and_keywords ["," keywords_arguments]\n | keywords_arguments\n positional_arguments ::= ["*"] expression ("," ["*"] expression)*\n starred_and_keywords ::= ("*" expression | keyword_item)\n ("," "*" expression | "," keyword_item)*\n keywords_arguments ::= (keyword_item | "**" expression)\n ("," keyword_item | "," "**" expression)*\n keyword_item ::= identifier "=" expression\n\nAn optional trailing comma may be present after the positional and\nkeyword arguments but does not affect the semantics.\n\nThe primary must evaluate to a callable object (user-defined\nfunctions, built-in functions, methods of built-in objects, class\nobjects, methods of class instances, and all objects having a\n"__call__()" method are callable). All argument expressions are\nevaluated before the call is attempted. Please refer to section\n*Function definitions* for the syntax of formal *parameter* lists.\n\nIf keyword arguments are present, they are first converted to\npositional arguments, as follows. First, a list of unfilled slots is\ncreated for the formal parameters. If there are N positional\narguments, they are placed in the first N slots. Next, for each\nkeyword argument, the identifier is used to determine the\ncorresponding slot (if the identifier is the same as the first formal\nparameter name, the first slot is used, and so on). If the slot is\nalready filled, a "TypeError" exception is raised. Otherwise, the\nvalue of the argument is placed in the slot, filling it (even if the\nexpression is "None", it fills the slot). When all arguments have\nbeen processed, the slots that are still unfilled are filled with the\ncorresponding default value from the function definition. (Default\nvalues are calculated, once, when the function is defined; thus, a\nmutable object such as a list or dictionary used as default value will\nbe shared by all calls that don\'t specify an argument value for the\ncorresponding slot; this should usually be avoided.) If there are any\nunfilled slots for which no default value is specified, a "TypeError"\nexception is raised. Otherwise, the list of filled slots is used as\nthe argument list for the call.\n\n**CPython implementation detail:** An implementation may provide\nbuilt-in functions whose positional parameters do not have names, even\nif they are \'named\' for the purpose of documentation, and which\ntherefore cannot be supplied by keyword. In CPython, this is the case\nfor functions implemented in C that use "PyArg_ParseTuple()" to parse\ntheir arguments.\n\nIf there are more positional arguments than there are formal parameter\nslots, a "TypeError" exception is raised, unless a formal parameter\nusing the syntax "*identifier" is present; in this case, that formal\nparameter receives a tuple containing the excess positional arguments\n(or an empty tuple if there were no excess positional arguments).\n\nIf any keyword argument does not correspond to a formal parameter\nname, a "TypeError" exception is raised, unless a formal parameter\nusing the syntax "**identifier" is present; in this case, that formal\nparameter receives a dictionary containing the excess keyword\narguments (using the keywords as keys and the argument values as\ncorresponding values), or a (new) empty dictionary if there were no\nexcess keyword arguments.\n\nIf the syntax "*expression" appears in the function call, "expression"\nmust evaluate to an *iterable*. Elements from these iterables are\ntreated as if they were additional positional arguments. For the call\n"f(x1, x2, *y, x3, x4)", if *y* evaluates to a sequence *y1*, ...,\n*yM*, this is equivalent to a call with M+4 positional arguments *x1*,\n*x2*, *y1*, ..., *yM*, *x3*, *x4*.\n\nA consequence of this is that although the "*expression" syntax may\nappear *after* explicit keyword arguments, it is processed *before*\nthe keyword arguments (and any "**expression" arguments -- see below).\nSo:\n\n >>> def f(a, b):\n ... print(a, b)\n ...\n >>> f(b=1, *(2,))\n 2 1\n >>> f(a=1, *(2,))\n Traceback (most recent call last):\n File "<stdin>", line 1, in ?\n TypeError: f() got multiple values for keyword argument \'a\'\n >>> f(1, *(2,))\n 1 2\n\nIt is unusual for both keyword arguments and the "*expression" syntax\nto be used in the same call, so in practice this confusion does not\narise.\n\nIf the syntax "**expression" appears in the function call,\n"expression" must evaluate to a *mapping*, the contents of which are\ntreated as additional keyword arguments. If a keyword is already\npresent (as an explicit keyword argument, or from another unpacking),\na "TypeError" exception is raised.\n\nFormal parameters using the syntax "*identifier" or "**identifier"\ncannot be used as positional argument slots or as keyword argument\nnames.\n\nChanged in version 3.5: Function calls accept any number of "*" and\n"**" unpackings, positional arguments may follow iterable unpackings\n("*"), and keyword arguments may follow dictionary unpackings ("**").\nOriginally proposed by **PEP 448**.\n\nA call always returns some value, possibly "None", unless it raises an\nexception. How this value is computed depends on the type of the\ncallable object.\n\nIf it is---\n\na user-defined function:\n The code block for the function is executed, passing it the\n argument list. The first thing the code block will do is bind the\n formal parameters to the arguments; this is described in section\n *Function definitions*. When the code block executes a "return"\n statement, this specifies the return value of the function call.\n\na built-in function or method:\n The result is up to the interpreter; see *Built-in Functions* for\n the descriptions of built-in functions and methods.\n\na class object:\n A new instance of that class is returned.\n\na class instance method:\n The corresponding user-defined function is called, with an argument\n list that is one longer than the argument list of the call: the\n instance becomes the first argument.\n\na class instance:\n The class must define a "__call__()" method; the effect is then the\n same as if that method was called.\n',
- 'class': u'\nClass definitions\n*****************\n\nA class definition defines a class object (see section *The standard\ntype hierarchy*):\n\n classdef ::= [decorators] "class" classname [inheritance] ":" suite\n inheritance ::= "(" [argument_list] ")"\n classname ::= identifier\n\nA class definition is an executable statement. The inheritance list\nusually gives a list of base classes (see *Customizing class creation*\nfor more advanced uses), so each item in the list should evaluate to a\nclass object which allows subclassing. Classes without an inheritance\nlist inherit, by default, from the base class "object"; hence,\n\n class Foo:\n pass\n\nis equivalent to\n\n class Foo(object):\n pass\n\nThe class\'s suite is then executed in a new execution frame (see\n*Naming and binding*), using a newly created local namespace and the\noriginal global namespace. (Usually, the suite contains mostly\nfunction definitions.) When the class\'s suite finishes execution, its\nexecution frame is discarded but its local namespace is saved. [4] A\nclass object is then created using the inheritance list for the base\nclasses and the saved local namespace for the attribute dictionary.\nThe class name is bound to this class object in the original local\nnamespace.\n\nClass creation can be customized heavily using *metaclasses*.\n\nClasses can also be decorated: just like when decorating functions,\n\n @f1(arg)\n @f2\n class Foo: pass\n\nis roughly equivalent to\n\n class Foo: pass\n Foo = f1(arg)(f2(Foo))\n\nThe evaluation rules for the decorator expressions are the same as for\nfunction decorators. The result is then bound to the class name.\n\n**Programmer\'s note:** Variables defined in the class definition are\nclass attributes; they are shared by instances. Instance attributes\ncan be set in a method with "self.name = value". Both class and\ninstance attributes are accessible through the notation ""self.name"",\nand an instance attribute hides a class attribute with the same name\nwhen accessed in this way. Class attributes can be used as defaults\nfor instance attributes, but using mutable values there can lead to\nunexpected results. *Descriptors* can be used to create instance\nvariables with different implementation details.\n\nSee also: **PEP 3115** - Metaclasses in Python 3 **PEP 3129** -\n Class Decorators\n',
- 'comparisons': u'\nComparisons\n***********\n\nUnlike C, all comparison operations in Python have the same priority,\nwhich is lower than that of any arithmetic, shifting or bitwise\noperation. Also unlike C, expressions like "a < b < c" have the\ninterpretation that is conventional in mathematics:\n\n comparison ::= or_expr ( comp_operator or_expr )*\n comp_operator ::= "<" | ">" | "==" | ">=" | "<=" | "!="\n | "is" ["not"] | ["not"] "in"\n\nComparisons yield boolean values: "True" or "False".\n\nComparisons can be chained arbitrarily, e.g., "x < y <= z" is\nequivalent to "x < y and y <= z", except that "y" is evaluated only\nonce (but in both cases "z" is not evaluated at all when "x < y" is\nfound to be false).\n\nFormally, if *a*, *b*, *c*, ..., *y*, *z* are expressions and *op1*,\n*op2*, ..., *opN* are comparison operators, then "a op1 b op2 c ... y\nopN z" is equivalent to "a op1 b and b op2 c and ... y opN z", except\nthat each expression is evaluated at most once.\n\nNote that "a op1 b op2 c" doesn\'t imply any kind of comparison between\n*a* and *c*, so that, e.g., "x < y > z" is perfectly legal (though\nperhaps not pretty).\n\n\nValue comparisons\n=================\n\nThe operators "<", ">", "==", ">=", "<=", and "!=" compare the values\nof two objects. The objects do not need to have the same type.\n\nChapter *Objects, values and types* states that objects have a value\n(in addition to type and identity). The value of an object is a\nrather abstract notion in Python: For example, there is no canonical\naccess method for an object\'s value. Also, there is no requirement\nthat the value of an object should be constructed in a particular way,\ne.g. comprised of all its data attributes. Comparison operators\nimplement a particular notion of what the value of an object is. One\ncan think of them as defining the value of an object indirectly, by\nmeans of their comparison implementation.\n\nBecause all types are (direct or indirect) subtypes of "object", they\ninherit the default comparison behavior from "object". Types can\ncustomize their comparison behavior by implementing *rich comparison\nmethods* like "__lt__()", described in *Basic customization*.\n\nThe default behavior for equality comparison ("==" and "!=") is based\non the identity of the objects. Hence, equality comparison of\ninstances with the same identity results in equality, and equality\ncomparison of instances with different identities results in\ninequality. A motivation for this default behavior is the desire that\nall objects should be reflexive (i.e. "x is y" implies "x == y").\n\nA default order comparison ("<", ">", "<=", and ">=") is not provided;\nan attempt raises "TypeError". A motivation for this default behavior\nis the lack of a similar invariant as for equality.\n\nThe behavior of the default equality comparison, that instances with\ndifferent identities are always unequal, may be in contrast to what\ntypes will need that have a sensible definition of object value and\nvalue-based equality. Such types will need to customize their\ncomparison behavior, and in fact, a number of built-in types have done\nthat.\n\nThe following list describes the comparison behavior of the most\nimportant built-in types.\n\n* Numbers of built-in numeric types (*Numeric Types --- int, float,\n complex*) and of the standard library types "fractions.Fraction" and\n "decimal.Decimal" can be compared within and across their types,\n with the restriction that complex numbers do not support order\n comparison. Within the limits of the types involved, they compare\n mathematically (algorithmically) correct without loss of precision.\n\n The not-a-number values "float(\'NaN\')" and "Decimal(\'NaN\')" are\n special. They are identical to themselves ("x is x" is true) but\n are not equal to themselves ("x == x" is false). Additionally,\n comparing any number to a not-a-number value will return "False".\n For example, both "3 < float(\'NaN\')" and "float(\'NaN\') < 3" will\n return "False".\n\n* Binary sequences (instances of "bytes" or "bytearray") can be\n compared within and across their types. They compare\n lexicographically using the numeric values of their elements.\n\n* Strings (instances of "str") compare lexicographically using the\n numerical Unicode code points (the result of the built-in function\n "ord()") of their characters. [3]\n\n Strings and binary sequences cannot be directly compared.\n\n* Sequences (instances of "tuple", "list", or "range") can be\n compared only within each of their types, with the restriction that\n ranges do not support order comparison. Equality comparison across\n these types results in unequality, and ordering comparison across\n these types raises "TypeError".\n\n Sequences compare lexicographically using comparison of\n corresponding elements, whereby reflexivity of the elements is\n enforced.\n\n In enforcing reflexivity of elements, the comparison of collections\n assumes that for a collection element "x", "x == x" is always true.\n Based on that assumption, element identity is compared first, and\n element comparison is performed only for distinct elements. This\n approach yields the same result as a strict element comparison\n would, if the compared elements are reflexive. For non-reflexive\n elements, the result is different than for strict element\n comparison, and may be surprising: The non-reflexive not-a-number\n values for example result in the following comparison behavior when\n used in a list:\n\n >>> nan = float(\'NaN\')\n >>> nan is nan\n True\n >>> nan == nan\n False <-- the defined non-reflexive behavior of NaN\n >>> [nan] == [nan]\n True <-- list enforces reflexivity and tests identity first\n\n Lexicographical comparison between built-in collections works as\n follows:\n\n * For two collections to compare equal, they must be of the same\n type, have the same length, and each pair of corresponding\n elements must compare equal (for example, "[1,2] == (1,2)" is\n false because the type is not the same).\n\n * Collections that support order comparison are ordered the same\n as their first unequal elements (for example, "[1,2,x] <= [1,2,y]"\n has the same value as "x <= y"). If a corresponding element does\n not exist, the shorter collection is ordered first (for example,\n "[1,2] < [1,2,3]" is true).\n\n* Mappings (instances of "dict") compare equal if and only if they\n have equal *(key, value)* pairs. Equality comparison of the keys and\n elements enforces reflexivity.\n\n Order comparisons ("<", ">", "<=", and ">=") raise "TypeError".\n\n* Sets (instances of "set" or "frozenset") can be compared within\n and across their types.\n\n They define order comparison operators to mean subset and superset\n tests. Those relations do not define total orderings (for example,\n the two sets "{1,2}" and "{2,3}" are not equal, nor subsets of one\n another, nor supersets of one another). Accordingly, sets are not\n appropriate arguments for functions which depend on total ordering\n (for example, "min()", "max()", and "sorted()" produce undefined\n results given a list of sets as inputs).\n\n Comparison of sets enforces reflexivity of its elements.\n\n* Most other built-in types have no comparison methods implemented,\n so they inherit the default comparison behavior.\n\nUser-defined classes that customize their comparison behavior should\nfollow some consistency rules, if possible:\n\n* Equality comparison should be reflexive. In other words, identical\n objects should compare equal:\n\n "x is y" implies "x == y"\n\n* Comparison should be symmetric. In other words, the following\n expressions should have the same result:\n\n "x == y" and "y == x"\n\n "x != y" and "y != x"\n\n "x < y" and "y > x"\n\n "x <= y" and "y >= x"\n\n* Comparison should be transitive. The following (non-exhaustive)\n examples illustrate that:\n\n "x > y and y > z" implies "x > z"\n\n "x < y and y <= z" implies "x < z"\n\n* Inverse comparison should result in the boolean negation. In other\n words, the following expressions should have the same result:\n\n "x == y" and "not x != y"\n\n "x < y" and "not x >= y" (for total ordering)\n\n "x > y" and "not x <= y" (for total ordering)\n\n The last two expressions apply to totally ordered collections (e.g.\n to sequences, but not to sets or mappings). See also the\n "total_ordering()" decorator.\n\nPython does not enforce these consistency rules. In fact, the\nnot-a-number values are an example for not following these rules.\n\n\nMembership test operations\n==========================\n\nThe operators "in" and "not in" test for membership. "x in s"\nevaluates to true if *x* is a member of *s*, and false otherwise. "x\nnot in s" returns the negation of "x in s". All built-in sequences\nand set types support this as well as dictionary, for which "in" tests\nwhether the dictionary has a given key. For container types such as\nlist, tuple, set, frozenset, dict, or collections.deque, the\nexpression "x in y" is equivalent to "any(x is e or x == e for e in\ny)".\n\nFor the string and bytes types, "x in y" is true if and only if *x* is\na substring of *y*. An equivalent test is "y.find(x) != -1". Empty\nstrings are always considered to be a substring of any other string,\nso """ in "abc"" will return "True".\n\nFor user-defined classes which define the "__contains__()" method, "x\nin y" is true if and only if "y.__contains__(x)" is true.\n\nFor user-defined classes which do not define "__contains__()" but do\ndefine "__iter__()", "x in y" is true if some value "z" with "x == z"\nis produced while iterating over "y". If an exception is raised\nduring the iteration, it is as if "in" raised that exception.\n\nLastly, the old-style iteration protocol is tried: if a class defines\n"__getitem__()", "x in y" is true if and only if there is a non-\nnegative integer index *i* such that "x == y[i]", and all lower\ninteger indices do not raise "IndexError" exception. (If any other\nexception is raised, it is as if "in" raised that exception).\n\nThe operator "not in" is defined to have the inverse true value of\n"in".\n\n\nIdentity comparisons\n====================\n\nThe operators "is" and "is not" test for object identity: "x is y" is\ntrue if and only if *x* and *y* are the same object. Object identity\nis determined using the "id()" function. "x is not y" yields the\ninverse truth value. [4]\n',
- 'compound': u'\nCompound statements\n*******************\n\nCompound statements contain (groups of) other statements; they affect\nor control the execution of those other statements in some way. In\ngeneral, compound statements span multiple lines, although in simple\nincarnations a whole compound statement may be contained in one line.\n\nThe "if", "while" and "for" statements implement traditional control\nflow constructs. "try" specifies exception handlers and/or cleanup\ncode for a group of statements, while the "with" statement allows the\nexecution of initialization and finalization code around a block of\ncode. Function and class definitions are also syntactically compound\nstatements.\n\nA compound statement consists of one or more \'clauses.\' A clause\nconsists of a header and a \'suite.\' The clause headers of a\nparticular compound statement are all at the same indentation level.\nEach clause header begins with a uniquely identifying keyword and ends\nwith a colon. A suite is a group of statements controlled by a\nclause. A suite can be one or more semicolon-separated simple\nstatements on the same line as the header, following the header\'s\ncolon, or it can be one or more indented statements on subsequent\nlines. Only the latter form of a suite can contain nested compound\nstatements; the following is illegal, mostly because it wouldn\'t be\nclear to which "if" clause a following "else" clause would belong:\n\n if test1: if test2: print(x)\n\nAlso note that the semicolon binds tighter than the colon in this\ncontext, so that in the following example, either all or none of the\n"print()" calls are executed:\n\n if x < y < z: print(x); print(y); print(z)\n\nSummarizing:\n\n compound_stmt ::= if_stmt\n | while_stmt\n | for_stmt\n | try_stmt\n | with_stmt\n | funcdef\n | classdef\n | async_with_stmt\n | async_for_stmt\n | async_funcdef\n suite ::= stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT\n statement ::= stmt_list NEWLINE | compound_stmt\n stmt_list ::= simple_stmt (";" simple_stmt)* [";"]\n\nNote that statements always end in a "NEWLINE" possibly followed by a\n"DEDENT". Also note that optional continuation clauses always begin\nwith a keyword that cannot start a statement, thus there are no\nambiguities (the \'dangling "else"\' problem is solved in Python by\nrequiring nested "if" statements to be indented).\n\nThe formatting of the grammar rules in the following sections places\neach clause on a separate line for clarity.\n\n\nThe "if" statement\n==================\n\nThe "if" statement is used for conditional execution:\n\n if_stmt ::= "if" expression ":" suite\n ( "elif" expression ":" suite )*\n ["else" ":" suite]\n\nIt selects exactly one of the suites by evaluating the expressions one\nby one until one is found to be true (see section *Boolean operations*\nfor the definition of true and false); then that suite is executed\n(and no other part of the "if" statement is executed or evaluated).\nIf all expressions are false, the suite of the "else" clause, if\npresent, is executed.\n\n\nThe "while" statement\n=====================\n\nThe "while" statement is used for repeated execution as long as an\nexpression is true:\n\n while_stmt ::= "while" expression ":" suite\n ["else" ":" suite]\n\nThis repeatedly tests the expression and, if it is true, executes the\nfirst suite; if the expression is false (which may be the first time\nit is tested) the suite of the "else" clause, if present, is executed\nand the loop terminates.\n\nA "break" statement executed in the first suite terminates the loop\nwithout executing the "else" clause\'s suite. A "continue" statement\nexecuted in the first suite skips the rest of the suite and goes back\nto testing the expression.\n\n\nThe "for" statement\n===================\n\nThe "for" statement is used to iterate over the elements of a sequence\n(such as a string, tuple or list) or other iterable object:\n\n for_stmt ::= "for" target_list "in" expression_list ":" suite\n ["else" ":" suite]\n\nThe expression list is evaluated once; it should yield an iterable\nobject. An iterator is created for the result of the\n"expression_list". The suite is then executed once for each item\nprovided by the iterator, in the order returned by the iterator. Each\nitem in turn is assigned to the target list using the standard rules\nfor assignments (see *Assignment statements*), and then the suite is\nexecuted. When the items are exhausted (which is immediately when the\nsequence is empty or an iterator raises a "StopIteration" exception),\nthe suite in the "else" clause, if present, is executed, and the loop\nterminates.\n\nA "break" statement executed in the first suite terminates the loop\nwithout executing the "else" clause\'s suite. A "continue" statement\nexecuted in the first suite skips the rest of the suite and continues\nwith the next item, or with the "else" clause if there is no next\nitem.\n\nThe for-loop makes assignments to the variables(s) in the target list.\nThis overwrites all previous assignments to those variables including\nthose made in the suite of the for-loop:\n\n for i in range(10):\n print(i)\n i = 5 # this will not affect the for-loop\n # because i will be overwritten with the next\n # index in the range\n\nNames in the target list are not deleted when the loop is finished,\nbut if the sequence is empty, they will not have been assigned to at\nall by the loop. Hint: the built-in function "range()" returns an\niterator of integers suitable to emulate the effect of Pascal\'s "for i\n:= a to b do"; e.g., "list(range(3))" returns the list "[0, 1, 2]".\n\nNote: There is a subtlety when the sequence is being modified by the\n loop (this can only occur for mutable sequences, i.e. lists). An\n internal counter is used to keep track of which item is used next,\n and this is incremented on each iteration. When this counter has\n reached the length of the sequence the loop terminates. This means\n that if the suite deletes the current (or a previous) item from the\n sequence, the next item will be skipped (since it gets the index of\n the current item which has already been treated). Likewise, if the\n suite inserts an item in the sequence before the current item, the\n current item will be treated again the next time through the loop.\n This can lead to nasty bugs that can be avoided by making a\n temporary copy using a slice of the whole sequence, e.g.,\n\n for x in a[:]:\n if x < 0: a.remove(x)\n\n\nThe "try" statement\n===================\n\nThe "try" statement specifies exception handlers and/or cleanup code\nfor a group of statements:\n\n try_stmt ::= try1_stmt | try2_stmt\n try1_stmt ::= "try" ":" suite\n ("except" [expression ["as" identifier]] ":" suite)+\n ["else" ":" suite]\n ["finally" ":" suite]\n try2_stmt ::= "try" ":" suite\n "finally" ":" suite\n\nThe "except" clause(s) specify one or more exception handlers. When no\nexception occurs in the "try" clause, no exception handler is\nexecuted. When an exception occurs in the "try" suite, a search for an\nexception handler is started. This search inspects the except clauses\nin turn until one is found that matches the exception. An expression-\nless except clause, if present, must be last; it matches any\nexception. For an except clause with an expression, that expression\nis evaluated, and the clause matches the exception if the resulting\nobject is "compatible" with the exception. An object is compatible\nwith an exception if it is the class or a base class of the exception\nobject or a tuple containing an item compatible with the exception.\n\nIf no except clause matches the exception, the search for an exception\nhandler continues in the surrounding code and on the invocation stack.\n[1]\n\nIf the evaluation of an expression in the header of an except clause\nraises an exception, the original search for a handler is canceled and\na search starts for the new exception in the surrounding code and on\nthe call stack (it is treated as if the entire "try" statement raised\nthe exception).\n\nWhen a matching except clause is found, the exception is assigned to\nthe target specified after the "as" keyword in that except clause, if\npresent, and the except clause\'s suite is executed. All except\nclauses must have an executable block. When the end of this block is\nreached, execution continues normally after the entire try statement.\n(This means that if two nested handlers exist for the same exception,\nand the exception occurs in the try clause of the inner handler, the\nouter handler will not handle the exception.)\n\nWhen an exception has been assigned using "as target", it is cleared\nat the end of the except clause. This is as if\n\n except E as N:\n foo\n\nwas translated to\n\n except E as N:\n try:\n foo\n finally:\n del N\n\nThis means the exception must be assigned to a different name to be\nable to refer to it after the except clause. Exceptions are cleared\nbecause with the traceback attached to them, they form a reference\ncycle with the stack frame, keeping all locals in that frame alive\nuntil the next garbage collection occurs.\n\nBefore an except clause\'s suite is executed, details about the\nexception are stored in the "sys" module and can be accessed via\n"sys.exc_info()". "sys.exc_info()" returns a 3-tuple consisting of the\nexception class, the exception instance and a traceback object (see\nsection *The standard type hierarchy*) identifying the point in the\nprogram where the exception occurred. "sys.exc_info()" values are\nrestored to their previous values (before the call) when returning\nfrom a function that handled an exception.\n\nThe optional "else" clause is executed if and when control flows off\nthe end of the "try" clause. [2] Exceptions in the "else" clause are\nnot handled by the preceding "except" clauses.\n\nIf "finally" is present, it specifies a \'cleanup\' handler. The "try"\nclause is executed, including any "except" and "else" clauses. If an\nexception occurs in any of the clauses and is not handled, the\nexception is temporarily saved. The "finally" clause is executed. If\nthere is a saved exception it is re-raised at the end of the "finally"\nclause. If the "finally" clause raises another exception, the saved\nexception is set as the context of the new exception. If the "finally"\nclause executes a "return" or "break" statement, the saved exception\nis discarded:\n\n >>> def f():\n ... try:\n ... 1/0\n ... finally:\n ... return 42\n ...\n >>> f()\n 42\n\nThe exception information is not available to the program during\nexecution of the "finally" clause.\n\nWhen a "return", "break" or "continue" statement is executed in the\n"try" suite of a "try"..."finally" statement, the "finally" clause is\nalso executed \'on the way out.\' A "continue" statement is illegal in\nthe "finally" clause. (The reason is a problem with the current\nimplementation --- this restriction may be lifted in the future).\n\nThe return value of a function is determined by the last "return"\nstatement executed. Since the "finally" clause always executes, a\n"return" statement executed in the "finally" clause will always be the\nlast one executed:\n\n >>> def foo():\n ... try:\n ... return \'try\'\n ... finally:\n ... return \'finally\'\n ...\n >>> foo()\n \'finally\'\n\nAdditional information on exceptions can be found in section\n*Exceptions*, and information on using the "raise" statement to\ngenerate exceptions may be found in section *The raise statement*.\n\n\nThe "with" statement\n====================\n\nThe "with" statement is used to wrap the execution of a block with\nmethods defined by a context manager (see section *With Statement\nContext Managers*). This allows common "try"..."except"..."finally"\nusage patterns to be encapsulated for convenient reuse.\n\n with_stmt ::= "with" with_item ("," with_item)* ":" suite\n with_item ::= expression ["as" target]\n\nThe execution of the "with" statement with one "item" proceeds as\nfollows:\n\n1. The context expression (the expression given in the "with_item")\n is evaluated to obtain a context manager.\n\n2. The context manager\'s "__exit__()" is loaded for later use.\n\n3. The context manager\'s "__enter__()" method is invoked.\n\n4. If a target was included in the "with" statement, the return\n value from "__enter__()" is assigned to it.\n\n Note: The "with" statement guarantees that if the "__enter__()"\n method returns without an error, then "__exit__()" will always be\n called. Thus, if an error occurs during the assignment to the\n target list, it will be treated the same as an error occurring\n within the suite would be. See step 6 below.\n\n5. The suite is executed.\n\n6. The context manager\'s "__exit__()" method is invoked. If an\n exception caused the suite to be exited, its type, value, and\n traceback are passed as arguments to "__exit__()". Otherwise, three\n "None" arguments are supplied.\n\n If the suite was exited due to an exception, and the return value\n from the "__exit__()" method was false, the exception is reraised.\n If the return value was true, the exception is suppressed, and\n execution continues with the statement following the "with"\n statement.\n\n If the suite was exited for any reason other than an exception, the\n return value from "__exit__()" is ignored, and execution proceeds\n at the normal location for the kind of exit that was taken.\n\nWith more than one item, the context managers are processed as if\nmultiple "with" statements were nested:\n\n with A() as a, B() as b:\n suite\n\nis equivalent to\n\n with A() as a:\n with B() as b:\n suite\n\nChanged in version 3.1: Support for multiple context expressions.\n\nSee also: **PEP 343** - The "with" statement\n\n The specification, background, and examples for the Python "with"\n statement.\n\n\nFunction definitions\n====================\n\nA function definition defines a user-defined function object (see\nsection *The standard type hierarchy*):\n\n funcdef ::= [decorators] "def" funcname "(" [parameter_list] ")" ["->" expression] ":" suite\n decorators ::= decorator+\n decorator ::= "@" dotted_name ["(" [argument_list [","]] ")"] NEWLINE\n dotted_name ::= identifier ("." identifier)*\n parameter_list ::= (defparameter ",")*\n | "*" [parameter] ("," defparameter)* ["," "**" parameter]\n | "**" parameter\n | defparameter [","] )\n parameter ::= identifier [":" expression]\n defparameter ::= parameter ["=" expression]\n funcname ::= identifier\n\nA function definition is an executable statement. Its execution binds\nthe function name in the current local namespace to a function object\n(a wrapper around the executable code for the function). This\nfunction object contains a reference to the current global namespace\nas the global namespace to be used when the function is called.\n\nThe function definition does not execute the function body; this gets\nexecuted only when the function is called. [3]\n\nA function definition may be wrapped by one or more *decorator*\nexpressions. Decorator expressions are evaluated when the function is\ndefined, in the scope that contains the function definition. The\nresult must be a callable, which is invoked with the function object\nas the only argument. The returned value is bound to the function name\ninstead of the function object. Multiple decorators are applied in\nnested fashion. For example, the following code\n\n @f1(arg)\n @f2\n def func(): pass\n\nis roughly equivalent to\n\n def func(): pass\n func = f1(arg)(f2(func))\n\nexcept that the original function is not temporarily bound to the name\n"func".\n\nWhen one or more *parameters* have the form *parameter* "="\n*expression*, the function is said to have "default parameter values."\nFor a parameter with a default value, the corresponding *argument* may\nbe omitted from a call, in which case the parameter\'s default value is\nsubstituted. If a parameter has a default value, all following\nparameters up until the ""*"" must also have a default value --- this\nis a syntactic restriction that is not expressed by the grammar.\n\n**Default parameter values are evaluated from left to right when the\nfunction definition is executed.** This means that the expression is\nevaluated once, when the function is defined, and that the same "pre-\ncomputed" value is used for each call. This is especially important\nto understand when a default parameter is a mutable object, such as a\nlist or a dictionary: if the function modifies the object (e.g. by\nappending an item to a list), the default value is in effect modified.\nThis is generally not what was intended. A way around this is to use\n"None" as the default, and explicitly test for it in the body of the\nfunction, e.g.:\n\n def whats_on_the_telly(penguin=None):\n if penguin is None:\n penguin = []\n penguin.append("property of the zoo")\n return penguin\n\nFunction call semantics are described in more detail in section\n*Calls*. A function call always assigns values to all parameters\nmentioned in the parameter list, either from position arguments, from\nkeyword arguments, or from default values. If the form\n""*identifier"" is present, it is initialized to a tuple receiving any\nexcess positional parameters, defaulting to the empty tuple. If the\nform ""**identifier"" is present, it is initialized to a new\ndictionary receiving any excess keyword arguments, defaulting to a new\nempty dictionary. Parameters after ""*"" or ""*identifier"" are\nkeyword-only parameters and may only be passed used keyword arguments.\n\nParameters may have annotations of the form "": expression"" following\nthe parameter name. Any parameter may have an annotation even those\nof the form "*identifier" or "**identifier". Functions may have\n"return" annotation of the form ""-> expression"" after the parameter\nlist. These annotations can be any valid Python expression and are\nevaluated when the function definition is executed. Annotations may\nbe evaluated in a different order than they appear in the source code.\nThe presence of annotations does not change the semantics of a\nfunction. The annotation values are available as values of a\ndictionary keyed by the parameters\' names in the "__annotations__"\nattribute of the function object.\n\nIt is also possible to create anonymous functions (functions not bound\nto a name), for immediate use in expressions. This uses lambda\nexpressions, described in section *Lambdas*. Note that the lambda\nexpression is merely a shorthand for a simplified function definition;\na function defined in a ""def"" statement can be passed around or\nassigned to another name just like a function defined by a lambda\nexpression. The ""def"" form is actually more powerful since it\nallows the execution of multiple statements and annotations.\n\n**Programmer\'s note:** Functions are first-class objects. A ""def""\nstatement executed inside a function definition defines a local\nfunction that can be returned or passed around. Free variables used\nin the nested function can access the local variables of the function\ncontaining the def. See section *Naming and binding* for details.\n\nSee also: **PEP 3107** - Function Annotations\n\n The original specification for function annotations.\n\n\nClass definitions\n=================\n\nA class definition defines a class object (see section *The standard\ntype hierarchy*):\n\n classdef ::= [decorators] "class" classname [inheritance] ":" suite\n inheritance ::= "(" [argument_list] ")"\n classname ::= identifier\n\nA class definition is an executable statement. The inheritance list\nusually gives a list of base classes (see *Customizing class creation*\nfor more advanced uses), so each item in the list should evaluate to a\nclass object which allows subclassing. Classes without an inheritance\nlist inherit, by default, from the base class "object"; hence,\n\n class Foo:\n pass\n\nis equivalent to\n\n class Foo(object):\n pass\n\nThe class\'s suite is then executed in a new execution frame (see\n*Naming and binding*), using a newly created local namespace and the\noriginal global namespace. (Usually, the suite contains mostly\nfunction definitions.) When the class\'s suite finishes execution, its\nexecution frame is discarded but its local namespace is saved. [4] A\nclass object is then created using the inheritance list for the base\nclasses and the saved local namespace for the attribute dictionary.\nThe class name is bound to this class object in the original local\nnamespace.\n\nClass creation can be customized heavily using *metaclasses*.\n\nClasses can also be decorated: just like when decorating functions,\n\n @f1(arg)\n @f2\n class Foo: pass\n\nis roughly equivalent to\n\n class Foo: pass\n Foo = f1(arg)(f2(Foo))\n\nThe evaluation rules for the decorator expressions are the same as for\nfunction decorators. The result is then bound to the class name.\n\n**Programmer\'s note:** Variables defined in the class definition are\nclass attributes; they are shared by instances. Instance attributes\ncan be set in a method with "self.name = value". Both class and\ninstance attributes are accessible through the notation ""self.name"",\nand an instance attribute hides a class attribute with the same name\nwhen accessed in this way. Class attributes can be used as defaults\nfor instance attributes, but using mutable values there can lead to\nunexpected results. *Descriptors* can be used to create instance\nvariables with different implementation details.\n\nSee also: **PEP 3115** - Metaclasses in Python 3 **PEP 3129** -\n Class Decorators\n\n\nCoroutines\n==========\n\nNew in version 3.5.\n\n\nCoroutine function definition\n-----------------------------\n\n async_funcdef ::= [decorators] "async" "def" funcname "(" [parameter_list] ")" ["->" expression] ":" suite\n\nExecution of Python coroutines can be suspended and resumed at many\npoints (see *coroutine*). In the body of a coroutine, any "await" and\n"async" identifiers become reserved keywords; "await" expressions,\n"async for" and "async with" can only be used in coroutine bodies.\n\nFunctions defined with "async def" syntax are always coroutine\nfunctions, even if they do not contain "await" or "async" keywords.\n\nIt is a "SyntaxError" to use "yield" expressions in "async def"\ncoroutines.\n\nAn example of a coroutine function:\n\n async def func(param1, param2):\n do_stuff()\n await some_coroutine()\n\n\nThe "async for" statement\n-------------------------\n\n async_for_stmt ::= "async" for_stmt\n\nAn *asynchronous iterable* is able to call asynchronous code in its\n*iter* implementation, and *asynchronous iterator* can call\nasynchronous code in its *next* method.\n\nThe "async for" statement allows convenient iteration over\nasynchronous iterators.\n\nThe following code:\n\n async for TARGET in ITER:\n BLOCK\n else:\n BLOCK2\n\nIs semantically equivalent to:\n\n iter = (ITER)\n iter = type(iter).__aiter__(iter)\n running = True\n while running:\n try:\n TARGET = await type(iter).__anext__(iter)\n except StopAsyncIteration:\n running = False\n else:\n BLOCK\n else:\n BLOCK2\n\nSee also "__aiter__()" and "__anext__()" for details.\n\nIt is a "SyntaxError" to use "async for" statement outside of an\n"async def" function.\n\n\nThe "async with" statement\n--------------------------\n\n async_with_stmt ::= "async" with_stmt\n\nAn *asynchronous context manager* is a *context manager* that is able\nto suspend execution in its *enter* and *exit* methods.\n\nThe following code:\n\n async with EXPR as VAR:\n BLOCK\n\nIs semantically equivalent to:\n\n mgr = (EXPR)\n aexit = type(mgr).__aexit__\n aenter = type(mgr).__aenter__(mgr)\n exc = True\n\n VAR = await aenter\n try:\n BLOCK\n except:\n if not await aexit(mgr, *sys.exc_info()):\n raise\n else:\n await aexit(mgr, None, None, None)\n\nSee also "__aenter__()" and "__aexit__()" for details.\n\nIt is a "SyntaxError" to use "async with" statement outside of an\n"async def" function.\n\nSee also: **PEP 492** - Coroutines with async and await syntax\n\n-[ Footnotes ]-\n\n[1] The exception is propagated to the invocation stack unless\n there is a "finally" clause which happens to raise another\n exception. That new exception causes the old one to be lost.\n\n[2] Currently, control "flows off the end" except in the case of\n an exception or the execution of a "return", "continue", or\n "break" statement.\n\n[3] A string literal appearing as the first statement in the\n function body is transformed into the function\'s "__doc__"\n attribute and therefore the function\'s *docstring*.\n\n[4] A string literal appearing as the first statement in the class\n body is transformed into the namespace\'s "__doc__" item and\n therefore the class\'s *docstring*.\n',
- 'context-managers': u'\nWith Statement Context Managers\n*******************************\n\nA *context manager* is an object that defines the runtime context to\nbe established when executing a "with" statement. The context manager\nhandles the entry into, and the exit from, the desired runtime context\nfor the execution of the block of code. Context managers are normally\ninvoked using the "with" statement (described in section *The with\nstatement*), but can also be used by directly invoking their methods.\n\nTypical uses of context managers include saving and restoring various\nkinds of global state, locking and unlocking resources, closing opened\nfiles, etc.\n\nFor more information on context managers, see *Context Manager Types*.\n\nobject.__enter__(self)\n\n Enter the runtime context related to this object. The "with"\n statement will bind this method\'s return value to the target(s)\n specified in the "as" clause of the statement, if any.\n\nobject.__exit__(self, exc_type, exc_value, traceback)\n\n Exit the runtime context related to this object. The parameters\n describe the exception that caused the context to be exited. If the\n context was exited without an exception, all three arguments will\n be "None".\n\n If an exception is supplied, and the method wishes to suppress the\n exception (i.e., prevent it from being propagated), it should\n return a true value. Otherwise, the exception will be processed\n normally upon exit from this method.\n\n Note that "__exit__()" methods should not reraise the passed-in\n exception; this is the caller\'s responsibility.\n\nSee also: **PEP 343** - The "with" statement\n\n The specification, background, and examples for the Python "with"\n statement.\n',
- 'continue': u'\nThe "continue" statement\n************************\n\n continue_stmt ::= "continue"\n\n"continue" may only occur syntactically nested in a "for" or "while"\nloop, but not nested in a function or class definition or "finally"\nclause within that loop. It continues with the next cycle of the\nnearest enclosing loop.\n\nWhen "continue" passes control out of a "try" statement with a\n"finally" clause, that "finally" clause is executed before really\nstarting the next loop cycle.\n',
- 'conversions': u'\nArithmetic conversions\n**********************\n\nWhen a description of an arithmetic operator below uses the phrase\n"the numeric arguments are converted to a common type," this means\nthat the operator implementation for built-in types works as follows:\n\n* If either argument is a complex number, the other is converted to\n complex;\n\n* otherwise, if either argument is a floating point number, the\n other is converted to floating point;\n\n* otherwise, both must be integers and no conversion is necessary.\n\nSome additional rules apply for certain operators (e.g., a string as a\nleft argument to the \'%\' operator). Extensions must define their own\nconversion behavior.\n',
- 'customization': u'\nBasic customization\n*******************\n\nobject.__new__(cls[, ...])\n\n Called to create a new instance of class *cls*. "__new__()" is a\n static method (special-cased so you need not declare it as such)\n that takes the class of which an instance was requested as its\n first argument. The remaining arguments are those passed to the\n object constructor expression (the call to the class). The return\n value of "__new__()" should be the new object instance (usually an\n instance of *cls*).\n\n Typical implementations create a new instance of the class by\n invoking the superclass\'s "__new__()" method using\n "super(currentclass, cls).__new__(cls[, ...])" with appropriate\n arguments and then modifying the newly-created instance as\n necessary before returning it.\n\n If "__new__()" returns an instance of *cls*, then the new\n instance\'s "__init__()" method will be invoked like\n "__init__(self[, ...])", where *self* is the new instance and the\n remaining arguments are the same as were passed to "__new__()".\n\n If "__new__()" does not return an instance of *cls*, then the new\n instance\'s "__init__()" method will not be invoked.\n\n "__new__()" is intended mainly to allow subclasses of immutable\n types (like int, str, or tuple) to customize instance creation. It\n is also commonly overridden in custom metaclasses in order to\n customize class creation.\n\nobject.__init__(self[, ...])\n\n Called after the instance has been created (by "__new__()"), but\n before it is returned to the caller. The arguments are those\n passed to the class constructor expression. If a base class has an\n "__init__()" method, the derived class\'s "__init__()" method, if\n any, must explicitly call it to ensure proper initialization of the\n base class part of the instance; for example:\n "BaseClass.__init__(self, [args...])".\n\n Because "__new__()" and "__init__()" work together in constructing\n objects ("__new__()" to create it, and "__init__()" to customize\n it), no non-"None" value may be returned by "__init__()"; doing so\n will cause a "TypeError" to be raised at runtime.\n\nobject.__del__(self)\n\n Called when the instance is about to be destroyed. This is also\n called a destructor. If a base class has a "__del__()" method, the\n derived class\'s "__del__()" method, if any, must explicitly call it\n to ensure proper deletion of the base class part of the instance.\n Note that it is possible (though not recommended!) for the\n "__del__()" method to postpone destruction of the instance by\n creating a new reference to it. It may then be called at a later\n time when this new reference is deleted. It is not guaranteed that\n "__del__()" methods are called for objects that still exist when\n the interpreter exits.\n\n Note: "del x" doesn\'t directly call "x.__del__()" --- the former\n decrements the reference count for "x" by one, and the latter is\n only called when "x"\'s reference count reaches zero. Some common\n situations that may prevent the reference count of an object from\n going to zero include: circular references between objects (e.g.,\n a doubly-linked list or a tree data structure with parent and\n child pointers); a reference to the object on the stack frame of\n a function that caught an exception (the traceback stored in\n "sys.exc_info()[2]" keeps the stack frame alive); or a reference\n to the object on the stack frame that raised an unhandled\n exception in interactive mode (the traceback stored in\n "sys.last_traceback" keeps the stack frame alive). The first\n situation can only be remedied by explicitly breaking the cycles;\n the second can be resolved by freeing the reference to the\n traceback object when it is no longer useful, and the third can\n be resolved by storing "None" in "sys.last_traceback". Circular\n references which are garbage are detected and cleaned up when the\n cyclic garbage collector is enabled (it\'s on by default). Refer\n to the documentation for the "gc" module for more information\n about this topic.\n\n Warning: Due to the precarious circumstances under which\n "__del__()" methods are invoked, exceptions that occur during\n their execution are ignored, and a warning is printed to\n "sys.stderr" instead. Also, when "__del__()" is invoked in\n response to a module being deleted (e.g., when execution of the\n program is done), other globals referenced by the "__del__()"\n method may already have been deleted or in the process of being\n torn down (e.g. the import machinery shutting down). For this\n reason, "__del__()" methods should do the absolute minimum needed\n to maintain external invariants. Starting with version 1.5,\n Python guarantees that globals whose name begins with a single\n underscore are deleted from their module before other globals are\n deleted; if no other references to such globals exist, this may\n help in assuring that imported modules are still available at the\n time when the "__del__()" method is called.\n\nobject.__repr__(self)\n\n Called by the "repr()" built-in function to compute the "official"\n string representation of an object. If at all possible, this\n should look like a valid Python expression that could be used to\n recreate an object with the same value (given an appropriate\n environment). If this is not possible, a string of the form\n "<...some useful description...>" should be returned. The return\n value must be a string object. If a class defines "__repr__()" but\n not "__str__()", then "__repr__()" is also used when an "informal"\n string representation of instances of that class is required.\n\n This is typically used for debugging, so it is important that the\n representation is information-rich and unambiguous.\n\nobject.__str__(self)\n\n Called by "str(object)" and the built-in functions "format()" and\n "print()" to compute the "informal" or nicely printable string\n representation of an object. The return value must be a *string*\n object.\n\n This method differs from "object.__repr__()" in that there is no\n expectation that "__str__()" return a valid Python expression: a\n more convenient or concise representation can be used.\n\n The default implementation defined by the built-in type "object"\n calls "object.__repr__()".\n\nobject.__bytes__(self)\n\n Called by "bytes()" to compute a byte-string representation of an\n object. This should return a "bytes" object.\n\nobject.__format__(self, format_spec)\n\n Called by the "format()" built-in function (and by extension, the\n "str.format()" method of class "str") to produce a "formatted"\n string representation of an object. The "format_spec" argument is a\n string that contains a description of the formatting options\n desired. The interpretation of the "format_spec" argument is up to\n the type implementing "__format__()", however most classes will\n either delegate formatting to one of the built-in types, or use a\n similar formatting option syntax.\n\n See *Format Specification Mini-Language* for a description of the\n standard formatting syntax.\n\n The return value must be a string object.\n\n Changed in version 3.4: The __format__ method of "object" itself\n raises a "TypeError" if passed any non-empty string.\n\nobject.__lt__(self, other)\nobject.__le__(self, other)\nobject.__eq__(self, other)\nobject.__ne__(self, other)\nobject.__gt__(self, other)\nobject.__ge__(self, other)\n\n These are the so-called "rich comparison" methods. The\n correspondence between operator symbols and method names is as\n follows: "x<y" calls "x.__lt__(y)", "x<=y" calls "x.__le__(y)",\n "x==y" calls "x.__eq__(y)", "x!=y" calls "x.__ne__(y)", "x>y" calls\n "x.__gt__(y)", and "x>=y" calls "x.__ge__(y)".\n\n A rich comparison method may return the singleton "NotImplemented"\n if it does not implement the operation for a given pair of\n arguments. By convention, "False" and "True" are returned for a\n successful comparison. However, these methods can return any value,\n so if the comparison operator is used in a Boolean context (e.g.,\n in the condition of an "if" statement), Python will call "bool()"\n on the value to determine if the result is true or false.\n\n By default, "__ne__()" delegates to "__eq__()" and inverts the\n result unless it is "NotImplemented". There are no other implied\n relationships among the comparison operators, for example, the\n truth of "(x<y or x==y)" does not imply "x<=y". To automatically\n generate ordering operations from a single root operation, see\n "functools.total_ordering()".\n\n See the paragraph on "__hash__()" for some important notes on\n creating *hashable* objects which support custom comparison\n operations and are usable as dictionary keys.\n\n There are no swapped-argument versions of these methods (to be used\n when the left argument does not support the operation but the right\n argument does); rather, "__lt__()" and "__gt__()" are each other\'s\n reflection, "__le__()" and "__ge__()" are each other\'s reflection,\n and "__eq__()" and "__ne__()" are their own reflection. If the\n operands are of different types, and right operand\'s type is a\n direct or indirect subclass of the left operand\'s type, the\n reflected method of the right operand has priority, otherwise the\n left operand\'s method has priority. Virtual subclassing is not\n considered.\n\nobject.__hash__(self)\n\n Called by built-in function "hash()" and for operations on members\n of hashed collections including "set", "frozenset", and "dict".\n "__hash__()" should return an integer. The only required property\n is that objects which compare equal have the same hash value; it is\n advised to mix together the hash values of the components of the\n object that also play a part in comparison of objects by packing\n them into a tuple and hashing the tuple. Example:\n\n def __hash__(self):\n return hash((self.name, self.nick, self.color))\n\n Note: "hash()" truncates the value returned from an object\'s\n custom "__hash__()" method to the size of a "Py_ssize_t". This\n is typically 8 bytes on 64-bit builds and 4 bytes on 32-bit\n builds. If an object\'s "__hash__()" must interoperate on builds\n of different bit sizes, be sure to check the width on all\n supported builds. An easy way to do this is with "python -c\n "import sys; print(sys.hash_info.width)"".\n\n If a class does not define an "__eq__()" method it should not\n define a "__hash__()" operation either; if it defines "__eq__()"\n but not "__hash__()", its instances will not be usable as items in\n hashable collections. If a class defines mutable objects and\n implements an "__eq__()" method, it should not implement\n "__hash__()", since the implementation of hashable collections\n requires that a key\'s hash value is immutable (if the object\'s hash\n value changes, it will be in the wrong hash bucket).\n\n User-defined classes have "__eq__()" and "__hash__()" methods by\n default; with them, all objects compare unequal (except with\n themselves) and "x.__hash__()" returns an appropriate value such\n that "x == y" implies both that "x is y" and "hash(x) == hash(y)".\n\n A class that overrides "__eq__()" and does not define "__hash__()"\n will have its "__hash__()" implicitly set to "None". When the\n "__hash__()" method of a class is "None", instances of the class\n will raise an appropriate "TypeError" when a program attempts to\n retrieve their hash value, and will also be correctly identified as\n unhashable when checking "isinstance(obj, collections.Hashable)".\n\n If a class that overrides "__eq__()" needs to retain the\n implementation of "__hash__()" from a parent class, the interpreter\n must be told this explicitly by setting "__hash__ =\n <ParentClass>.__hash__".\n\n If a class that does not override "__eq__()" wishes to suppress\n hash support, it should include "__hash__ = None" in the class\n definition. A class which defines its own "__hash__()" that\n explicitly raises a "TypeError" would be incorrectly identified as\n hashable by an "isinstance(obj, collections.Hashable)" call.\n\n Note: By default, the "__hash__()" values of str, bytes and\n datetime objects are "salted" with an unpredictable random value.\n Although they remain constant within an individual Python\n process, they are not predictable between repeated invocations of\n Python.This is intended to provide protection against a denial-\n of-service caused by carefully-chosen inputs that exploit the\n worst case performance of a dict insertion, O(n^2) complexity.\n See http://www.ocert.org/advisories/ocert-2011-003.html for\n details.Changing hash values affects the iteration order of\n dicts, sets and other mappings. Python has never made guarantees\n about this ordering (and it typically varies between 32-bit and\n 64-bit builds).See also "PYTHONHASHSEED".\n\n Changed in version 3.3: Hash randomization is enabled by default.\n\nobject.__bool__(self)\n\n Called to implement truth value testing and the built-in operation\n "bool()"; should return "False" or "True". When this method is not\n defined, "__len__()" is called, if it is defined, and the object is\n considered true if its result is nonzero. If a class defines\n neither "__len__()" nor "__bool__()", all its instances are\n considered true.\n',
- 'debugger': u'\n"pdb" --- The Python Debugger\n*****************************\n\n**Source code:** Lib/pdb.py\n\n======================================================================\n\nThe module "pdb" defines an interactive source code debugger for\nPython programs. It supports setting (conditional) breakpoints and\nsingle stepping at the source line level, inspection of stack frames,\nsource code listing, and evaluation of arbitrary Python code in the\ncontext of any stack frame. It also supports post-mortem debugging\nand can be called under program control.\n\nThe debugger is extensible -- it is actually defined as the class\n"Pdb". This is currently undocumented but easily understood by reading\nthe source. The extension interface uses the modules "bdb" and "cmd".\n\nThe debugger\'s prompt is "(Pdb)". Typical usage to run a program under\ncontrol of the debugger is:\n\n >>> import pdb\n >>> import mymodule\n >>> pdb.run(\'mymodule.test()\')\n > <string>(0)?()\n (Pdb) continue\n > <string>(1)?()\n (Pdb) continue\n NameError: \'spam\'\n > <string>(1)?()\n (Pdb)\n\nChanged in version 3.3: Tab-completion via the "readline" module is\navailable for commands and command arguments, e.g. the current global\nand local names are offered as arguments of the "p" command.\n\n"pdb.py" can also be invoked as a script to debug other scripts. For\nexample:\n\n python3 -m pdb myscript.py\n\nWhen invoked as a script, pdb will automatically enter post-mortem\ndebugging if the program being debugged exits abnormally. After post-\nmortem debugging (or after normal exit of the program), pdb will\nrestart the program. Automatic restarting preserves pdb\'s state (such\nas breakpoints) and in most cases is more useful than quitting the\ndebugger upon program\'s exit.\n\nNew in version 3.2: "pdb.py" now accepts a "-c" option that executes\ncommands as if given in a ".pdbrc" file, see *Debugger Commands*.\n\nThe typical usage to break into the debugger from a running program is\nto insert\n\n import pdb; pdb.set_trace()\n\nat the location you want to break into the debugger. You can then\nstep through the code following this statement, and continue running\nwithout the debugger using the "continue" command.\n\nThe typical usage to inspect a crashed program is:\n\n >>> import pdb\n >>> import mymodule\n >>> mymodule.test()\n Traceback (most recent call last):\n File "<stdin>", line 1, in ?\n File "./mymodule.py", line 4, in test\n test2()\n File "./mymodule.py", line 3, in test2\n print(spam)\n NameError: spam\n >>> pdb.pm()\n > ./mymodule.py(3)test2()\n -> print(spam)\n (Pdb)\n\nThe module defines the following functions; each enters the debugger\nin a slightly different way:\n\npdb.run(statement, globals=None, locals=None)\n\n Execute the *statement* (given as a string or a code object) under\n debugger control. The debugger prompt appears before any code is\n executed; you can set breakpoints and type "continue", or you can\n step through the statement using "step" or "next" (all these\n commands are explained below). The optional *globals* and *locals*\n arguments specify the environment in which the code is executed; by\n default the dictionary of the module "__main__" is used. (See the\n explanation of the built-in "exec()" or "eval()" functions.)\n\npdb.runeval(expression, globals=None, locals=None)\n\n Evaluate the *expression* (given as a string or a code object)\n under debugger control. When "runeval()" returns, it returns the\n value of the expression. Otherwise this function is similar to\n "run()".\n\npdb.runcall(function, *args, **kwds)\n\n Call the *function* (a function or method object, not a string)\n with the given arguments. When "runcall()" returns, it returns\n whatever the function call returned. The debugger prompt appears\n as soon as the function is entered.\n\npdb.set_trace()\n\n Enter the debugger at the calling stack frame. This is useful to\n hard-code a breakpoint at a given point in a program, even if the\n code is not otherwise being debugged (e.g. when an assertion\n fails).\n\npdb.post_mortem(traceback=None)\n\n Enter post-mortem debugging of the given *traceback* object. If no\n *traceback* is given, it uses the one of the exception that is\n currently being handled (an exception must be being handled if the\n default is to be used).\n\npdb.pm()\n\n Enter post-mortem debugging of the traceback found in\n "sys.last_traceback".\n\nThe "run*" functions and "set_trace()" are aliases for instantiating\nthe "Pdb" class and calling the method of the same name. If you want\nto access further features, you have to do this yourself:\n\nclass class pdb.Pdb(completekey=\'tab\', stdin=None, stdout=None, skip=None, nosigint=False)\n\n "Pdb" is the debugger class.\n\n The *completekey*, *stdin* and *stdout* arguments are passed to the\n underlying "cmd.Cmd" class; see the description there.\n\n The *skip* argument, if given, must be an iterable of glob-style\n module name patterns. The debugger will not step into frames that\n originate in a module that matches one of these patterns. [1]\n\n By default, Pdb sets a handler for the SIGINT signal (which is sent\n when the user presses "Ctrl-C" on the console) when you give a\n "continue" command. This allows you to break into the debugger\n again by pressing "Ctrl-C". If you want Pdb not to touch the\n SIGINT handler, set *nosigint* to true.\n\n Example call to enable tracing with *skip*:\n\n import pdb; pdb.Pdb(skip=[\'django.*\']).set_trace()\n\n New in version 3.1: The *skip* argument.\n\n New in version 3.2: The *nosigint* argument. Previously, a SIGINT\n handler was never set by Pdb.\n\n run(statement, globals=None, locals=None)\n runeval(expression, globals=None, locals=None)\n runcall(function, *args, **kwds)\n set_trace()\n\n See the documentation for the functions explained above.\n\n\nDebugger Commands\n=================\n\nThe commands recognized by the debugger are listed below. Most\ncommands can be abbreviated to one or two letters as indicated; e.g.\n"h(elp)" means that either "h" or "help" can be used to enter the help\ncommand (but not "he" or "hel", nor "H" or "Help" or "HELP").\nArguments to commands must be separated by whitespace (spaces or\ntabs). Optional arguments are enclosed in square brackets ("[]") in\nthe command syntax; the square brackets must not be typed.\nAlternatives in the command syntax are separated by a vertical bar\n("|").\n\nEntering a blank line repeats the last command entered. Exception: if\nthe last command was a "list" command, the next 11 lines are listed.\n\nCommands that the debugger doesn\'t recognize are assumed to be Python\nstatements and are executed in the context of the program being\ndebugged. Python statements can also be prefixed with an exclamation\npoint ("!"). This is a powerful way to inspect the program being\ndebugged; it is even possible to change a variable or call a function.\nWhen an exception occurs in such a statement, the exception name is\nprinted but the debugger\'s state is not changed.\n\nThe debugger supports *aliases*. Aliases can have parameters which\nallows one a certain level of adaptability to the context under\nexamination.\n\nMultiple commands may be entered on a single line, separated by ";;".\n(A single ";" is not used as it is the separator for multiple commands\nin a line that is passed to the Python parser.) No intelligence is\napplied to separating the commands; the input is split at the first\n";;" pair, even if it is in the middle of a quoted string.\n\nIf a file ".pdbrc" exists in the user\'s home directory or in the\ncurrent directory, it is read in and executed as if it had been typed\nat the debugger prompt. This is particularly useful for aliases. If\nboth files exist, the one in the home directory is read first and\naliases defined there can be overridden by the local file.\n\nChanged in version 3.2: ".pdbrc" can now contain commands that\ncontinue debugging, such as "continue" or "next". Previously, these\ncommands had no effect.\n\nh(elp) [command]\n\n Without argument, print the list of available commands. With a\n *command* as argument, print help about that command. "help pdb"\n displays the full documentation (the docstring of the "pdb"\n module). Since the *command* argument must be an identifier, "help\n exec" must be entered to get help on the "!" command.\n\nw(here)\n\n Print a stack trace, with the most recent frame at the bottom. An\n arrow indicates the current frame, which determines the context of\n most commands.\n\nd(own) [count]\n\n Move the current frame *count* (default one) levels down in the\n stack trace (to a newer frame).\n\nu(p) [count]\n\n Move the current frame *count* (default one) levels up in the stack\n trace (to an older frame).\n\nb(reak) [([filename:]lineno | function) [, condition]]\n\n With a *lineno* argument, set a break there in the current file.\n With a *function* argument, set a break at the first executable\n statement within that function. The line number may be prefixed\n with a filename and a colon, to specify a breakpoint in another\n file (probably one that hasn\'t been loaded yet). The file is\n searched on "sys.path". Note that each breakpoint is assigned a\n number to which all the other breakpoint commands refer.\n\n If a second argument is present, it is an expression which must\n evaluate to true before the breakpoint is honored.\n\n Without argument, list all breaks, including for each breakpoint,\n the number of times that breakpoint has been hit, the current\n ignore count, and the associated condition if any.\n\ntbreak [([filename:]lineno | function) [, condition]]\n\n Temporary breakpoint, which is removed automatically when it is\n first hit. The arguments are the same as for "break".\n\ncl(ear) [filename:lineno | bpnumber [bpnumber ...]]\n\n With a *filename:lineno* argument, clear all the breakpoints at\n this line. With a space separated list of breakpoint numbers, clear\n those breakpoints. Without argument, clear all breaks (but first\n ask confirmation).\n\ndisable [bpnumber [bpnumber ...]]\n\n Disable the breakpoints given as a space separated list of\n breakpoint numbers. Disabling a breakpoint means it cannot cause\n the program to stop execution, but unlike clearing a breakpoint, it\n remains in the list of breakpoints and can be (re-)enabled.\n\nenable [bpnumber [bpnumber ...]]\n\n Enable the breakpoints specified.\n\nignore bpnumber [count]\n\n Set the ignore count for the given breakpoint number. If count is\n omitted, the ignore count is set to 0. A breakpoint becomes active\n when the ignore count is zero. When non-zero, the count is\n decremented each time the breakpoint is reached and the breakpoint\n is not disabled and any associated condition evaluates to true.\n\ncondition bpnumber [condition]\n\n Set a new *condition* for the breakpoint, an expression which must\n evaluate to true before the breakpoint is honored. If *condition*\n is absent, any existing condition is removed; i.e., the breakpoint\n is made unconditional.\n\ncommands [bpnumber]\n\n Specify a list of commands for breakpoint number *bpnumber*. The\n commands themselves appear on the following lines. Type a line\n containing just "end" to terminate the commands. An example:\n\n (Pdb) commands 1\n (com) p some_variable\n (com) end\n (Pdb)\n\n To remove all commands from a breakpoint, type commands and follow\n it immediately with "end"; that is, give no commands.\n\n With no *bpnumber* argument, commands refers to the last breakpoint\n set.\n\n You can use breakpoint commands to start your program up again.\n Simply use the continue command, or step, or any other command that\n resumes execution.\n\n Specifying any command resuming execution (currently continue,\n step, next, return, jump, quit and their abbreviations) terminates\n the command list (as if that command was immediately followed by\n end). This is because any time you resume execution (even with a\n simple next or step), you may encounter another breakpoint\u2014which\n could have its own command list, leading to ambiguities about which\n list to execute.\n\n If you use the \'silent\' command in the command list, the usual\n message about stopping at a breakpoint is not printed. This may be\n desirable for breakpoints that are to print a specific message and\n then continue. If none of the other commands print anything, you\n see no sign that the breakpoint was reached.\n\ns(tep)\n\n Execute the current line, stop at the first possible occasion\n (either in a function that is called or on the next line in the\n current function).\n\nn(ext)\n\n Continue execution until the next line in the current function is\n reached or it returns. (The difference between "next" and "step"\n is that "step" stops inside a called function, while "next"\n executes called functions at (nearly) full speed, only stopping at\n the next line in the current function.)\n\nunt(il) [lineno]\n\n Without argument, continue execution until the line with a number\n greater than the current one is reached.\n\n With a line number, continue execution until a line with a number\n greater or equal to that is reached. In both cases, also stop when\n the current frame returns.\n\n Changed in version 3.2: Allow giving an explicit line number.\n\nr(eturn)\n\n Continue execution until the current function returns.\n\nc(ont(inue))\n\n Continue execution, only stop when a breakpoint is encountered.\n\nj(ump) lineno\n\n Set the next line that will be executed. Only available in the\n bottom-most frame. This lets you jump back and execute code again,\n or jump forward to skip code that you don\'t want to run.\n\n It should be noted that not all jumps are allowed -- for instance\n it is not possible to jump into the middle of a "for" loop or out\n of a "finally" clause.\n\nl(ist) [first[, last]]\n\n List source code for the current file. Without arguments, list 11\n lines around the current line or continue the previous listing.\n With "." as argument, list 11 lines around the current line. With\n one argument, list 11 lines around at that line. With two\n arguments, list the given range; if the second argument is less\n than the first, it is interpreted as a count.\n\n The current line in the current frame is indicated by "->". If an\n exception is being debugged, the line where the exception was\n originally raised or propagated is indicated by ">>", if it differs\n from the current line.\n\n New in version 3.2: The ">>" marker.\n\nll | longlist\n\n List all source code for the current function or frame.\n Interesting lines are marked as for "list".\n\n New in version 3.2.\n\na(rgs)\n\n Print the argument list of the current function.\n\np expression\n\n Evaluate the *expression* in the current context and print its\n value.\n\n Note: "print()" can also be used, but is not a debugger command\n --- this executes the Python "print()" function.\n\npp expression\n\n Like the "p" command, except the value of the expression is pretty-\n printed using the "pprint" module.\n\nwhatis expression\n\n Print the type of the *expression*.\n\nsource expression\n\n Try to get source code for the given object and display it.\n\n New in version 3.2.\n\ndisplay [expression]\n\n Display the value of the expression if it changed, each time\n execution stops in the current frame.\n\n Without expression, list all display expressions for the current\n frame.\n\n New in version 3.2.\n\nundisplay [expression]\n\n Do not display the expression any more in the current frame.\n Without expression, clear all display expressions for the current\n frame.\n\n New in version 3.2.\n\ninteract\n\n Start an interactive interpreter (using the "code" module) whose\n global namespace contains all the (global and local) names found in\n the current scope.\n\n New in version 3.2.\n\nalias [name [command]]\n\n Create an alias called *name* that executes *command*. The command\n must *not* be enclosed in quotes. Replaceable parameters can be\n indicated by "%1", "%2", and so on, while "%*" is replaced by all\n the parameters. If no command is given, the current alias for\n *name* is shown. If no arguments are given, all aliases are listed.\n\n Aliases may be nested and can contain anything that can be legally\n typed at the pdb prompt. Note that internal pdb commands *can* be\n overridden by aliases. Such a command is then hidden until the\n alias is removed. Aliasing is recursively applied to the first\n word of the command line; all other words in the line are left\n alone.\n\n As an example, here are two useful aliases (especially when placed\n in the ".pdbrc" file):\n\n # Print instance variables (usage "pi classInst")\n alias pi for k in %1.__dict__.keys(): print("%1.",k,"=",%1.__dict__[k])\n # Print instance variables in self\n alias ps pi self\n\nunalias name\n\n Delete the specified alias.\n\n! statement\n\n Execute the (one-line) *statement* in the context of the current\n stack frame. The exclamation point can be omitted unless the first\n word of the statement resembles a debugger command. To set a\n global variable, you can prefix the assignment command with a\n "global" statement on the same line, e.g.:\n\n (Pdb) global list_options; list_options = [\'-l\']\n (Pdb)\n\nrun [args ...]\nrestart [args ...]\n\n Restart the debugged Python program. If an argument is supplied,\n it is split with "shlex" and the result is used as the new\n "sys.argv". History, breakpoints, actions and debugger options are\n preserved. "restart" is an alias for "run".\n\nq(uit)\n\n Quit from the debugger. The program being executed is aborted.\n\n-[ Footnotes ]-\n\n[1] Whether a frame is considered to originate in a certain module\n is determined by the "__name__" in the frame globals.\n',
- 'del': u'\nThe "del" statement\n*******************\n\n del_stmt ::= "del" target_list\n\nDeletion is recursively defined very similar to the way assignment is\ndefined. Rather than spelling it out in full details, here are some\nhints.\n\nDeletion of a target list recursively deletes each target, from left\nto right.\n\nDeletion of a name removes the binding of that name from the local or\nglobal namespace, depending on whether the name occurs in a "global"\nstatement in the same code block. If the name is unbound, a\n"NameError" exception will be raised.\n\nDeletion of attribute references, subscriptions and slicings is passed\nto the primary object involved; deletion of a slicing is in general\nequivalent to assignment of an empty slice of the right type (but even\nthis is determined by the sliced object).\n\nChanged in version 3.2: Previously it was illegal to delete a name\nfrom the local namespace if it occurs as a free variable in a nested\nblock.\n',
- 'dict': u'\nDictionary displays\n*******************\n\nA dictionary display is a possibly empty series of key/datum pairs\nenclosed in curly braces:\n\n dict_display ::= "{" [key_datum_list | dict_comprehension] "}"\n key_datum_list ::= key_datum ("," key_datum)* [","]\n key_datum ::= expression ":" expression | "**" or_expr\n dict_comprehension ::= expression ":" expression comp_for\n\nA dictionary display yields a new dictionary object.\n\nIf a comma-separated sequence of key/datum pairs is given, they are\nevaluated from left to right to define the entries of the dictionary:\neach key object is used as a key into the dictionary to store the\ncorresponding datum. This means that you can specify the same key\nmultiple times in the key/datum list, and the final dictionary\'s value\nfor that key will be the last one given.\n\nA double asterisk "**" denotes *dictionary unpacking*. Its operand\nmust be a *mapping*. Each mapping item is added to the new\ndictionary. Later values replace values already set by earlier\nkey/datum pairs and earlier dictionary unpackings.\n\nNew in version 3.5: Unpacking into dictionary displays, originally\nproposed by **PEP 448**.\n\nA dict comprehension, in contrast to list and set comprehensions,\nneeds two expressions separated with a colon followed by the usual\n"for" and "if" clauses. When the comprehension is run, the resulting\nkey and value elements are inserted in the new dictionary in the order\nthey are produced.\n\nRestrictions on the types of the key values are listed earlier in\nsection *The standard type hierarchy*. (To summarize, the key type\nshould be *hashable*, which excludes all mutable objects.) Clashes\nbetween duplicate keys are not detected; the last datum (textually\nrightmost in the display) stored for a given key value prevails.\n',
- 'dynamic-features': u'\nInteraction with dynamic features\n*********************************\n\nName resolution of free variables occurs at runtime, not at compile\ntime. This means that the following code will print 42:\n\n i = 10\n def f():\n print(i)\n i = 42\n f()\n\nThere are several cases where Python statements are illegal when used\nin conjunction with nested scopes that contain free variables.\n\nIf a variable is referenced in an enclosing scope, it is illegal to\ndelete the name. An error will be reported at compile time.\n\nThe "eval()" and "exec()" functions do not have access to the full\nenvironment for resolving names. Names may be resolved in the local\nand global namespaces of the caller. Free variables are not resolved\nin the nearest enclosing namespace, but in the global namespace. [1]\nThe "exec()" and "eval()" functions have optional arguments to\noverride the global and local namespace. If only one namespace is\nspecified, it is used for both.\n',
- 'else': u'\nThe "if" statement\n******************\n\nThe "if" statement is used for conditional execution:\n\n if_stmt ::= "if" expression ":" suite\n ( "elif" expression ":" suite )*\n ["else" ":" suite]\n\nIt selects exactly one of the suites by evaluating the expressions one\nby one until one is found to be true (see section *Boolean operations*\nfor the definition of true and false); then that suite is executed\n(and no other part of the "if" statement is executed or evaluated).\nIf all expressions are false, the suite of the "else" clause, if\npresent, is executed.\n',
- 'exceptions': u'\nExceptions\n**********\n\nExceptions are a means of breaking out of the normal flow of control\nof a code block in order to handle errors or other exceptional\nconditions. An exception is *raised* at the point where the error is\ndetected; it may be *handled* by the surrounding code block or by any\ncode block that directly or indirectly invoked the code block where\nthe error occurred.\n\nThe Python interpreter raises an exception when it detects a run-time\nerror (such as division by zero). A Python program can also\nexplicitly raise an exception with the "raise" statement. Exception\nhandlers are specified with the "try" ... "except" statement. The\n"finally" clause of such a statement can be used to specify cleanup\ncode which does not handle the exception, but is executed whether an\nexception occurred or not in the preceding code.\n\nPython uses the "termination" model of error handling: an exception\nhandler can find out what happened and continue execution at an outer\nlevel, but it cannot repair the cause of the error and retry the\nfailing operation (except by re-entering the offending piece of code\nfrom the top).\n\nWhen an exception is not handled at all, the interpreter terminates\nexecution of the program, or returns to its interactive main loop. In\neither case, it prints a stack backtrace, except when the exception is\n"SystemExit".\n\nExceptions are identified by class instances. The "except" clause is\nselected depending on the class of the instance: it must reference the\nclass of the instance or a base class thereof. The instance can be\nreceived by the handler and can carry additional information about the\nexceptional condition.\n\nNote: Exception messages are not part of the Python API. Their\n contents may change from one version of Python to the next without\n warning and should not be relied on by code which will run under\n multiple versions of the interpreter.\n\nSee also the description of the "try" statement in section *The try\nstatement* and "raise" statement in section *The raise statement*.\n\n-[ Footnotes ]-\n\n[1] This limitation occurs because the code that is executed by\n these operations is not available at the time the module is\n compiled.\n',
- 'execmodel': u'\nExecution model\n***************\n\n\nStructure of a program\n======================\n\nA Python program is constructed from code blocks. A *block* is a piece\nof Python program text that is executed as a unit. The following are\nblocks: a module, a function body, and a class definition. Each\ncommand typed interactively is a block. A script file (a file given\nas standard input to the interpreter or specified as a command line\nargument to the interpreter) is a code block. A script command (a\ncommand specified on the interpreter command line with the \'**-c**\'\noption) is a code block. The string argument passed to the built-in\nfunctions "eval()" and "exec()" is a code block.\n\nA code block is executed in an *execution frame*. A frame contains\nsome administrative information (used for debugging) and determines\nwhere and how execution continues after the code block\'s execution has\ncompleted.\n\n\nNaming and binding\n==================\n\n\nBinding of names\n----------------\n\n*Names* refer to objects. Names are introduced by name binding\noperations.\n\nThe following constructs bind names: formal parameters to functions,\n"import" statements, class and function definitions (these bind the\nclass or function name in the defining block), and targets that are\nidentifiers if occurring in an assignment, "for" loop header, or after\n"as" in a "with" statement or "except" clause. The "import" statement\nof the form "from ... import *" binds all names defined in the\nimported module, except those beginning with an underscore. This form\nmay only be used at the module level.\n\nA target occurring in a "del" statement is also considered bound for\nthis purpose (though the actual semantics are to unbind the name).\n\nEach assignment or import statement occurs within a block defined by a\nclass or function definition or at the module level (the top-level\ncode block).\n\nIf a name is bound in a block, it is a local variable of that block,\nunless declared as "nonlocal" or "global". If a name is bound at the\nmodule level, it is a global variable. (The variables of the module\ncode block are local and global.) If a variable is used in a code\nblock but not defined there, it is a *free variable*.\n\nEach occurrence of a name in the program text refers to the *binding*\nof that name established by the following name resolution rules.\n\n\nResolution of names\n-------------------\n\nA *scope* defines the visibility of a name within a block. If a local\nvariable is defined in a block, its scope includes that block. If the\ndefinition occurs in a function block, the scope extends to any blocks\ncontained within the defining one, unless a contained block introduces\na different binding for the name.\n\nWhen a name is used in a code block, it is resolved using the nearest\nenclosing scope. The set of all such scopes visible to a code block\nis called the block\'s *environment*.\n\nWhen a name is not found at all, a "NameError" exception is raised. If\nthe current scope is a function scope, and the name refers to a local\nvariable that has not yet been bound to a value at the point where the\nname is used, an "UnboundLocalError" exception is raised.\n"UnboundLocalError" is a subclass of "NameError".\n\nIf a name binding operation occurs anywhere within a code block, all\nuses of the name within the block are treated as references to the\ncurrent block. This can lead to errors when a name is used within a\nblock before it is bound. This rule is subtle. Python lacks\ndeclarations and allows name binding operations to occur anywhere\nwithin a code block. The local variables of a code block can be\ndetermined by scanning the entire text of the block for name binding\noperations.\n\nIf the "global" statement occurs within a block, all uses of the name\nspecified in the statement refer to the binding of that name in the\ntop-level namespace. Names are resolved in the top-level namespace by\nsearching the global namespace, i.e. the namespace of the module\ncontaining the code block, and the builtins namespace, the namespace\nof the module "builtins". The global namespace is searched first. If\nthe name is not found there, the builtins namespace is searched. The\n"global" statement must precede all uses of the name.\n\nThe "global" statement has the same scope as a name binding operation\nin the same block. If the nearest enclosing scope for a free variable\ncontains a global statement, the free variable is treated as a global.\n\nThe "nonlocal" statement causes corresponding names to refer to\npreviously bound variables in the nearest enclosing function scope.\n"SyntaxError" is raised at compile time if the given name does not\nexist in any enclosing function scope.\n\nThe namespace for a module is automatically created the first time a\nmodule is imported. The main module for a script is always called\n"__main__".\n\nClass definition blocks and arguments to "exec()" and "eval()" are\nspecial in the context of name resolution. A class definition is an\nexecutable statement that may use and define names. These references\nfollow the normal rules for name resolution with an exception that\nunbound local variables are looked up in the global namespace. The\nnamespace of the class definition becomes the attribute dictionary of\nthe class. The scope of names defined in a class block is limited to\nthe class block; it does not extend to the code blocks of methods --\nthis includes comprehensions and generator expressions since they are\nimplemented using a function scope. This means that the following\nwill fail:\n\n class A:\n a = 42\n b = list(a + i for i in range(10))\n\n\nBuiltins and restricted execution\n---------------------------------\n\nThe builtins namespace associated with the execution of a code block\nis actually found by looking up the name "__builtins__" in its global\nnamespace; this should be a dictionary or a module (in the latter case\nthe module\'s dictionary is used). By default, when in the "__main__"\nmodule, "__builtins__" is the built-in module "builtins"; when in any\nother module, "__builtins__" is an alias for the dictionary of the\n"builtins" module itself. "__builtins__" can be set to a user-created\ndictionary to create a weak form of restricted execution.\n\n**CPython implementation detail:** Users should not touch\n"__builtins__"; it is strictly an implementation detail. Users\nwanting to override values in the builtins namespace should "import"\nthe "builtins" module and modify its attributes appropriately.\n\n\nInteraction with dynamic features\n---------------------------------\n\nName resolution of free variables occurs at runtime, not at compile\ntime. This means that the following code will print 42:\n\n i = 10\n def f():\n print(i)\n i = 42\n f()\n\nThere are several cases where Python statements are illegal when used\nin conjunction with nested scopes that contain free variables.\n\nIf a variable is referenced in an enclosing scope, it is illegal to\ndelete the name. An error will be reported at compile time.\n\nThe "eval()" and "exec()" functions do not have access to the full\nenvironment for resolving names. Names may be resolved in the local\nand global namespaces of the caller. Free variables are not resolved\nin the nearest enclosing namespace, but in the global namespace. [1]\nThe "exec()" and "eval()" functions have optional arguments to\noverride the global and local namespace. If only one namespace is\nspecified, it is used for both.\n\n\nExceptions\n==========\n\nExceptions are a means of breaking out of the normal flow of control\nof a code block in order to handle errors or other exceptional\nconditions. An exception is *raised* at the point where the error is\ndetected; it may be *handled* by the surrounding code block or by any\ncode block that directly or indirectly invoked the code block where\nthe error occurred.\n\nThe Python interpreter raises an exception when it detects a run-time\nerror (such as division by zero). A Python program can also\nexplicitly raise an exception with the "raise" statement. Exception\nhandlers are specified with the "try" ... "except" statement. The\n"finally" clause of such a statement can be used to specify cleanup\ncode which does not handle the exception, but is executed whether an\nexception occurred or not in the preceding code.\n\nPython uses the "termination" model of error handling: an exception\nhandler can find out what happened and continue execution at an outer\nlevel, but it cannot repair the cause of the error and retry the\nfailing operation (except by re-entering the offending piece of code\nfrom the top).\n\nWhen an exception is not handled at all, the interpreter terminates\nexecution of the program, or returns to its interactive main loop. In\neither case, it prints a stack backtrace, except when the exception is\n"SystemExit".\n\nExceptions are identified by class instances. The "except" clause is\nselected depending on the class of the instance: it must reference the\nclass of the instance or a base class thereof. The instance can be\nreceived by the handler and can carry additional information about the\nexceptional condition.\n\nNote: Exception messages are not part of the Python API. Their\n contents may change from one version of Python to the next without\n warning and should not be relied on by code which will run under\n multiple versions of the interpreter.\n\nSee also the description of the "try" statement in section *The try\nstatement* and "raise" statement in section *The raise statement*.\n\n-[ Footnotes ]-\n\n[1] This limitation occurs because the code that is executed by\n these operations is not available at the time the module is\n compiled.\n',
- 'exprlists': u'\nExpression lists\n****************\n\n expression_list ::= expression ( "," expression )* [","]\n starred_list ::= starred_item ( "," starred_item )* [","]\n starred_expression ::= expression | ( starred_item "," )* [starred_item]\n starred_item ::= expression | "*" or_expr\n\nExcept when part of a list or set display, an expression list\ncontaining at least one comma yields a tuple. The length of the tuple\nis the number of expressions in the list. The expressions are\nevaluated from left to right.\n\nAn asterisk "*" denotes *iterable unpacking*. Its operand must be an\n*iterable*. The iterable is expanded into a sequence of items, which\nare included in the new tuple, list, or set, at the site of the\nunpacking.\n\nNew in version 3.5: Iterable unpacking in expression lists, originally\nproposed by **PEP 448**.\n\nThe trailing comma is required only to create a single tuple (a.k.a. a\n*singleton*); it is optional in all other cases. A single expression\nwithout a trailing comma doesn\'t create a tuple, but rather yields the\nvalue of that expression. (To create an empty tuple, use an empty pair\nof parentheses: "()".)\n',
- 'floating': u'\nFloating point literals\n***********************\n\nFloating point literals are described by the following lexical\ndefinitions:\n\n floatnumber ::= pointfloat | exponentfloat\n pointfloat ::= [intpart] fraction | intpart "."\n exponentfloat ::= (intpart | pointfloat) exponent\n intpart ::= digit+\n fraction ::= "." digit+\n exponent ::= ("e" | "E") ["+" | "-"] digit+\n\nNote that the integer and exponent parts are always interpreted using\nradix 10. For example, "077e010" is legal, and denotes the same number\nas "77e10". The allowed range of floating point literals is\nimplementation-dependent. Some examples of floating point literals:\n\n 3.14 10. .001 1e100 3.14e-10 0e0\n\nNote that numeric literals do not include a sign; a phrase like "-1"\nis actually an expression composed of the unary operator "-" and the\nliteral "1".\n',
- 'for': u'\nThe "for" statement\n*******************\n\nThe "for" statement is used to iterate over the elements of a sequence\n(such as a string, tuple or list) or other iterable object:\n\n for_stmt ::= "for" target_list "in" expression_list ":" suite\n ["else" ":" suite]\n\nThe expression list is evaluated once; it should yield an iterable\nobject. An iterator is created for the result of the\n"expression_list". The suite is then executed once for each item\nprovided by the iterator, in the order returned by the iterator. Each\nitem in turn is assigned to the target list using the standard rules\nfor assignments (see *Assignment statements*), and then the suite is\nexecuted. When the items are exhausted (which is immediately when the\nsequence is empty or an iterator raises a "StopIteration" exception),\nthe suite in the "else" clause, if present, is executed, and the loop\nterminates.\n\nA "break" statement executed in the first suite terminates the loop\nwithout executing the "else" clause\'s suite. A "continue" statement\nexecuted in the first suite skips the rest of the suite and continues\nwith the next item, or with the "else" clause if there is no next\nitem.\n\nThe for-loop makes assignments to the variables(s) in the target list.\nThis overwrites all previous assignments to those variables including\nthose made in the suite of the for-loop:\n\n for i in range(10):\n print(i)\n i = 5 # this will not affect the for-loop\n # because i will be overwritten with the next\n # index in the range\n\nNames in the target list are not deleted when the loop is finished,\nbut if the sequence is empty, they will not have been assigned to at\nall by the loop. Hint: the built-in function "range()" returns an\niterator of integers suitable to emulate the effect of Pascal\'s "for i\n:= a to b do"; e.g., "list(range(3))" returns the list "[0, 1, 2]".\n\nNote: There is a subtlety when the sequence is being modified by the\n loop (this can only occur for mutable sequences, i.e. lists). An\n internal counter is used to keep track of which item is used next,\n and this is incremented on each iteration. When this counter has\n reached the length of the sequence the loop terminates. This means\n that if the suite deletes the current (or a previous) item from the\n sequence, the next item will be skipped (since it gets the index of\n the current item which has already been treated). Likewise, if the\n suite inserts an item in the sequence before the current item, the\n current item will be treated again the next time through the loop.\n This can lead to nasty bugs that can be avoided by making a\n temporary copy using a slice of the whole sequence, e.g.,\n\n for x in a[:]:\n if x < 0: a.remove(x)\n',
- 'formatstrings': u'\nFormat String Syntax\n********************\n\nThe "str.format()" method and the "Formatter" class share the same\nsyntax for format strings (although in the case of "Formatter",\nsubclasses can define their own format string syntax).\n\nFormat strings contain "replacement fields" surrounded by curly braces\n"{}". Anything that is not contained in braces is considered literal\ntext, which is copied unchanged to the output. If you need to include\na brace character in the literal text, it can be escaped by doubling:\n"{{" and "}}".\n\nThe grammar for a replacement field is as follows:\n\n replacement_field ::= "{" [field_name] ["!" conversion] [":" format_spec] "}"\n field_name ::= arg_name ("." attribute_name | "[" element_index "]")*\n arg_name ::= [identifier | integer]\n attribute_name ::= identifier\n element_index ::= integer | index_string\n index_string ::= <any source character except "]"> +\n conversion ::= "r" | "s" | "a"\n format_spec ::= <described in the next section>\n\nIn less formal terms, the replacement field can start with a\n*field_name* that specifies the object whose value is to be formatted\nand inserted into the output instead of the replacement field. The\n*field_name* is optionally followed by a *conversion* field, which is\npreceded by an exclamation point "\'!\'", and a *format_spec*, which is\npreceded by a colon "\':\'". These specify a non-default format for the\nreplacement value.\n\nSee also the *Format Specification Mini-Language* section.\n\nThe *field_name* itself begins with an *arg_name* that is either a\nnumber or a keyword. If it\'s a number, it refers to a positional\nargument, and if it\'s a keyword, it refers to a named keyword\nargument. If the numerical arg_names in a format string are 0, 1, 2,\n... in sequence, they can all be omitted (not just some) and the\nnumbers 0, 1, 2, ... will be automatically inserted in that order.\nBecause *arg_name* is not quote-delimited, it is not possible to\nspecify arbitrary dictionary keys (e.g., the strings "\'10\'" or\n"\':-]\'") within a format string. The *arg_name* can be followed by any\nnumber of index or attribute expressions. An expression of the form\n"\'.name\'" selects the named attribute using "getattr()", while an\nexpression of the form "\'[index]\'" does an index lookup using\n"__getitem__()".\n\nChanged in version 3.1: The positional argument specifiers can be\nomitted, so "\'{} {}\'" is equivalent to "\'{0} {1}\'".\n\nSome simple format string examples:\n\n "First, thou shalt count to {0}" # References first positional argument\n "Bring me a {}" # Implicitly references the first positional argument\n "From {} to {}" # Same as "From {0} to {1}"\n "My quest is {name}" # References keyword argument \'name\'\n "Weight in tons {0.weight}" # \'weight\' attribute of first positional arg\n "Units destroyed: {players[0]}" # First element of keyword argument \'players\'.\n\nThe *conversion* field causes a type coercion before formatting.\nNormally, the job of formatting a value is done by the "__format__()"\nmethod of the value itself. However, in some cases it is desirable to\nforce a type to be formatted as a string, overriding its own\ndefinition of formatting. By converting the value to a string before\ncalling "__format__()", the normal formatting logic is bypassed.\n\nThree conversion flags are currently supported: "\'!s\'" which calls\n"str()" on the value, "\'!r\'" which calls "repr()" and "\'!a\'" which\ncalls "ascii()".\n\nSome examples:\n\n "Harold\'s a clever {0!s}" # Calls str() on the argument first\n "Bring out the holy {name!r}" # Calls repr() on the argument first\n "More {!a}" # Calls ascii() on the argument first\n\nThe *format_spec* field contains a specification of how the value\nshould be presented, including such details as field width, alignment,\npadding, decimal precision and so on. Each value type can define its\nown "formatting mini-language" or interpretation of the *format_spec*.\n\nMost built-in types support a common formatting mini-language, which\nis described in the next section.\n\nA *format_spec* field can also include nested replacement fields\nwithin it. These nested replacement fields may contain a field name,\nconversion flag and format specification, but deeper nesting is not\nallowed. The replacement fields within the format_spec are\nsubstituted before the *format_spec* string is interpreted. This\nallows the formatting of a value to be dynamically specified.\n\nSee the *Format examples* section for some examples.\n\n\nFormat Specification Mini-Language\n==================================\n\n"Format specifications" are used within replacement fields contained\nwithin a format string to define how individual values are presented\n(see *Format String Syntax*). They can also be passed directly to the\nbuilt-in "format()" function. Each formattable type may define how\nthe format specification is to be interpreted.\n\nMost built-in types implement the following options for format\nspecifications, although some of the formatting options are only\nsupported by the numeric types.\n\nA general convention is that an empty format string ("""") produces\nthe same result as if you had called "str()" on the value. A non-empty\nformat string typically modifies the result.\n\nThe general form of a *standard format specifier* is:\n\n format_spec ::= [[fill]align][sign][#][0][width][,][.precision][type]\n fill ::= <any character>\n align ::= "<" | ">" | "=" | "^"\n sign ::= "+" | "-" | " "\n width ::= integer\n precision ::= integer\n type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"\n\nIf a valid *align* value is specified, it can be preceded by a *fill*\ncharacter that can be any character and defaults to a space if\nomitted. It is not possible to use a literal curly brace (""{"" or\n""}"") as the *fill* character when using the "str.format()" method.\nHowever, it is possible to insert a curly brace with a nested\nreplacement field. This limitation doesn\'t affect the "format()"\nfunction.\n\nThe meaning of the various alignment options is as follows:\n\n +-----------+------------------------------------------------------------+\n | Option | Meaning |\n +===========+============================================================+\n | "\'<\'" | Forces the field to be left-aligned within the available |\n | | space (this is the default for most objects). |\n +-----------+------------------------------------------------------------+\n | "\'>\'" | Forces the field to be right-aligned within the available |\n | | space (this is the default for numbers). |\n +-----------+------------------------------------------------------------+\n | "\'=\'" | Forces the padding to be placed after the sign (if any) |\n | | but before the digits. This is used for printing fields |\n | | in the form \'+000000120\'. This alignment option is only |\n | | valid for numeric types. It becomes the default when \'0\' |\n | | immediately precedes the field width. |\n +-----------+------------------------------------------------------------+\n | "\'^\'" | Forces the field to be centered within the available |\n | | space. |\n +-----------+------------------------------------------------------------+\n\nNote that unless a minimum field width is defined, the field width\nwill always be the same size as the data to fill it, so that the\nalignment option has no meaning in this case.\n\nThe *sign* option is only valid for number types, and can be one of\nthe following:\n\n +-----------+------------------------------------------------------------+\n | Option | Meaning |\n +===========+============================================================+\n | "\'+\'" | indicates that a sign should be used for both positive as |\n | | well as negative numbers. |\n +-----------+------------------------------------------------------------+\n | "\'-\'" | indicates that a sign should be used only for negative |\n | | numbers (this is the default behavior). |\n +-----------+------------------------------------------------------------+\n | space | indicates that a leading space should be used on positive |\n | | numbers, and a minus sign on negative numbers. |\n +-----------+------------------------------------------------------------+\n\nThe "\'#\'" option causes the "alternate form" to be used for the\nconversion. The alternate form is defined differently for different\ntypes. This option is only valid for integer, float, complex and\nDecimal types. For integers, when binary, octal, or hexadecimal output\nis used, this option adds the prefix respective "\'0b\'", "\'0o\'", or\n"\'0x\'" to the output value. For floats, complex and Decimal the\nalternate form causes the result of the conversion to always contain a\ndecimal-point character, even if no digits follow it. Normally, a\ndecimal-point character appears in the result of these conversions\nonly if a digit follows it. In addition, for "\'g\'" and "\'G\'"\nconversions, trailing zeros are not removed from the result.\n\nThe "\',\'" option signals the use of a comma for a thousands separator.\nFor a locale aware separator, use the "\'n\'" integer presentation type\ninstead.\n\nChanged in version 3.1: Added the "\',\'" option (see also **PEP 378**).\n\n*width* is a decimal integer defining the minimum field width. If not\nspecified, then the field width will be determined by the content.\n\nWhen no explicit alignment is given, preceding the *width* field by a\nzero ("\'0\'") character enables sign-aware zero-padding for numeric\ntypes. This is equivalent to a *fill* character of "\'0\'" with an\n*alignment* type of "\'=\'".\n\nThe *precision* is a decimal number indicating how many digits should\nbe displayed after the decimal point for a floating point value\nformatted with "\'f\'" and "\'F\'", or before and after the decimal point\nfor a floating point value formatted with "\'g\'" or "\'G\'". For non-\nnumber types the field indicates the maximum field size - in other\nwords, how many characters will be used from the field content. The\n*precision* is not allowed for integer values.\n\nFinally, the *type* determines how the data should be presented.\n\nThe available string presentation types are:\n\n +-----------+------------------------------------------------------------+\n | Type | Meaning |\n +===========+============================================================+\n | "\'s\'" | String format. This is the default type for strings and |\n | | may be omitted. |\n +-----------+------------------------------------------------------------+\n | None | The same as "\'s\'". |\n +-----------+------------------------------------------------------------+\n\nThe available integer presentation types are:\n\n +-----------+------------------------------------------------------------+\n | Type | Meaning |\n +===========+============================================================+\n | "\'b\'" | Binary format. Outputs the number in base 2. |\n +-----------+------------------------------------------------------------+\n | "\'c\'" | Character. Converts the integer to the corresponding |\n | | unicode character before printing. |\n +-----------+------------------------------------------------------------+\n | "\'d\'" | Decimal Integer. Outputs the number in base 10. |\n +-----------+------------------------------------------------------------+\n | "\'o\'" | Octal format. Outputs the number in base 8. |\n +-----------+------------------------------------------------------------+\n | "\'x\'" | Hex format. Outputs the number in base 16, using lower- |\n | | case letters for the digits above 9. |\n +-----------+------------------------------------------------------------+\n | "\'X\'" | Hex format. Outputs the number in base 16, using upper- |\n | | case letters for the digits above 9. |\n +-----------+------------------------------------------------------------+\n | "\'n\'" | Number. This is the same as "\'d\'", except that it uses the |\n | | current locale setting to insert the appropriate number |\n | | separator characters. |\n +-----------+------------------------------------------------------------+\n | None | The same as "\'d\'". |\n +-----------+------------------------------------------------------------+\n\nIn addition to the above presentation types, integers can be formatted\nwith the floating point presentation types listed below (except "\'n\'"\nand "None"). When doing so, "float()" is used to convert the integer\nto a floating point number before formatting.\n\nThe available presentation types for floating point and decimal values\nare:\n\n +-----------+------------------------------------------------------------+\n | Type | Meaning |\n +===========+============================================================+\n | "\'e\'" | Exponent notation. Prints the number in scientific |\n | | notation using the letter \'e\' to indicate the exponent. |\n | | The default precision is "6". |\n +-----------+------------------------------------------------------------+\n | "\'E\'" | Exponent notation. Same as "\'e\'" except it uses an upper |\n | | case \'E\' as the separator character. |\n +-----------+------------------------------------------------------------+\n | "\'f\'" | Fixed point. Displays the number as a fixed-point number. |\n | | The default precision is "6". |\n +-----------+------------------------------------------------------------+\n | "\'F\'" | Fixed point. Same as "\'f\'", but converts "nan" to "NAN" |\n | | and "inf" to "INF". |\n +-----------+------------------------------------------------------------+\n | "\'g\'" | General format. For a given precision "p >= 1", this |\n | | rounds the number to "p" significant digits and then |\n | | formats the result in either fixed-point format or in |\n | | scientific notation, depending on its magnitude. The |\n | | precise rules are as follows: suppose that the result |\n | | formatted with presentation type "\'e\'" and precision "p-1" |\n | | would have exponent "exp". Then if "-4 <= exp < p", the |\n | | number is formatted with presentation type "\'f\'" and |\n | | precision "p-1-exp". Otherwise, the number is formatted |\n | | with presentation type "\'e\'" and precision "p-1". In both |\n | | cases insignificant trailing zeros are removed from the |\n | | significand, and the decimal point is also removed if |\n | | there are no remaining digits following it. Positive and |\n | | negative infinity, positive and negative zero, and nans, |\n | | are formatted as "inf", "-inf", "0", "-0" and "nan" |\n | | respectively, regardless of the precision. A precision of |\n | | "0" is treated as equivalent to a precision of "1". The |\n | | default precision is "6". |\n +-----------+------------------------------------------------------------+\n | "\'G\'" | General format. Same as "\'g\'" except switches to "\'E\'" if |\n | | the number gets too large. The representations of infinity |\n | | and NaN are uppercased, too. |\n +-----------+------------------------------------------------------------+\n | "\'n\'" | Number. This is the same as "\'g\'", except that it uses the |\n | | current locale setting to insert the appropriate number |\n | | separator characters. |\n +-----------+------------------------------------------------------------+\n | "\'%\'" | Percentage. Multiplies the number by 100 and displays in |\n | | fixed ("\'f\'") format, followed by a percent sign. |\n +-----------+------------------------------------------------------------+\n | None | Similar to "\'g\'", except that fixed-point notation, when |\n | | used, has at least one digit past the decimal point. The |\n | | default precision is as high as needed to represent the |\n | | particular value. The overall effect is to match the |\n | | output of "str()" as altered by the other format |\n | | modifiers. |\n +-----------+------------------------------------------------------------+\n\n\nFormat examples\n===============\n\nThis section contains examples of the "str.format()" syntax and\ncomparison with the old "%"-formatting.\n\nIn most of the cases the syntax is similar to the old "%"-formatting,\nwith the addition of the "{}" and with ":" used instead of "%". For\nexample, "\'%03.2f\'" can be translated to "\'{:03.2f}\'".\n\nThe new format syntax also supports new and different options, shown\nin the follow examples.\n\nAccessing arguments by position:\n\n >>> \'{0}, {1}, {2}\'.format(\'a\', \'b\', \'c\')\n \'a, b, c\'\n >>> \'{}, {}, {}\'.format(\'a\', \'b\', \'c\') # 3.1+ only\n \'a, b, c\'\n >>> \'{2}, {1}, {0}\'.format(\'a\', \'b\', \'c\')\n \'c, b, a\'\n >>> \'{2}, {1}, {0}\'.format(*\'abc\') # unpacking argument sequence\n \'c, b, a\'\n >>> \'{0}{1}{0}\'.format(\'abra\', \'cad\') # arguments\' indices can be repeated\n \'abracadabra\'\n\nAccessing arguments by name:\n\n >>> \'Coordinates: {latitude}, {longitude}\'.format(latitude=\'37.24N\', longitude=\'-115.81W\')\n \'Coordinates: 37.24N, -115.81W\'\n >>> coord = {\'latitude\': \'37.24N\', \'longitude\': \'-115.81W\'}\n >>> \'Coordinates: {latitude}, {longitude}\'.format(**coord)\n \'Coordinates: 37.24N, -115.81W\'\n\nAccessing arguments\' attributes:\n\n >>> c = 3-5j\n >>> (\'The complex number {0} is formed from the real part {0.real} \'\n ... \'and the imaginary part {0.imag}.\').format(c)\n \'The complex number (3-5j) is formed from the real part 3.0 and the imaginary part -5.0.\'\n >>> class Point:\n ... def __init__(self, x, y):\n ... self.x, self.y = x, y\n ... def __str__(self):\n ... return \'Point({self.x}, {self.y})\'.format(self=self)\n ...\n >>> str(Point(4, 2))\n \'Point(4, 2)\'\n\nAccessing arguments\' items:\n\n >>> coord = (3, 5)\n >>> \'X: {0[0]}; Y: {0[1]}\'.format(coord)\n \'X: 3; Y: 5\'\n\nReplacing "%s" and "%r":\n\n >>> "repr() shows quotes: {!r}; str() doesn\'t: {!s}".format(\'test1\', \'test2\')\n "repr() shows quotes: \'test1\'; str() doesn\'t: test2"\n\nAligning the text and specifying a width:\n\n >>> \'{:<30}\'.format(\'left aligned\')\n \'left aligned \'\n >>> \'{:>30}\'.format(\'right aligned\')\n \' right aligned\'\n >>> \'{:^30}\'.format(\'centered\')\n \' centered \'\n >>> \'{:*^30}\'.format(\'centered\') # use \'*\' as a fill char\n \'***********centered***********\'\n\nReplacing "%+f", "%-f", and "% f" and specifying a sign:\n\n >>> \'{:+f}; {:+f}\'.format(3.14, -3.14) # show it always\n \'+3.140000; -3.140000\'\n >>> \'{: f}; {: f}\'.format(3.14, -3.14) # show a space for positive numbers\n \' 3.140000; -3.140000\'\n >>> \'{:-f}; {:-f}\'.format(3.14, -3.14) # show only the minus -- same as \'{:f}; {:f}\'\n \'3.140000; -3.140000\'\n\nReplacing "%x" and "%o" and converting the value to different bases:\n\n >>> # format also supports binary numbers\n >>> "int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}".format(42)\n \'int: 42; hex: 2a; oct: 52; bin: 101010\'\n >>> # with 0x, 0o, or 0b as prefix:\n >>> "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}".format(42)\n \'int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010\'\n\nUsing the comma as a thousands separator:\n\n >>> \'{:,}\'.format(1234567890)\n \'1,234,567,890\'\n\nExpressing a percentage:\n\n >>> points = 19\n >>> total = 22\n >>> \'Correct answers: {:.2%}\'.format(points/total)\n \'Correct answers: 86.36%\'\n\nUsing type-specific formatting:\n\n >>> import datetime\n >>> d = datetime.datetime(2010, 7, 4, 12, 15, 58)\n >>> \'{:%Y-%m-%d %H:%M:%S}\'.format(d)\n \'2010-07-04 12:15:58\'\n\nNesting arguments and more complex examples:\n\n >>> for align, text in zip(\'<^>\', [\'left\', \'center\', \'right\']):\n ... \'{0:{fill}{align}16}\'.format(text, fill=align, align=align)\n ...\n \'left<<<<<<<<<<<<\'\n \'^^^^^center^^^^^\'\n \'>>>>>>>>>>>right\'\n >>>\n >>> octets = [192, 168, 0, 1]\n >>> \'{:02X}{:02X}{:02X}{:02X}\'.format(*octets)\n \'C0A80001\'\n >>> int(_, 16)\n 3232235521\n >>>\n >>> width = 5\n >>> for num in range(5,12): #doctest: +NORMALIZE_WHITESPACE\n ... for base in \'dXob\':\n ... print(\'{0:{width}{base}}\'.format(num, base=base, width=width), end=\' \')\n ... print()\n ...\n 5 5 5 101\n 6 6 6 110\n 7 7 7 111\n 8 8 10 1000\n 9 9 11 1001\n 10 A 12 1010\n 11 B 13 1011\n',
- 'function': u'\nFunction definitions\n********************\n\nA function definition defines a user-defined function object (see\nsection *The standard type hierarchy*):\n\n funcdef ::= [decorators] "def" funcname "(" [parameter_list] ")" ["->" expression] ":" suite\n decorators ::= decorator+\n decorator ::= "@" dotted_name ["(" [argument_list [","]] ")"] NEWLINE\n dotted_name ::= identifier ("." identifier)*\n parameter_list ::= (defparameter ",")*\n | "*" [parameter] ("," defparameter)* ["," "**" parameter]\n | "**" parameter\n | defparameter [","] )\n parameter ::= identifier [":" expression]\n defparameter ::= parameter ["=" expression]\n funcname ::= identifier\n\nA function definition is an executable statement. Its execution binds\nthe function name in the current local namespace to a function object\n(a wrapper around the executable code for the function). This\nfunction object contains a reference to the current global namespace\nas the global namespace to be used when the function is called.\n\nThe function definition does not execute the function body; this gets\nexecuted only when the function is called. [3]\n\nA function definition may be wrapped by one or more *decorator*\nexpressions. Decorator expressions are evaluated when the function is\ndefined, in the scope that contains the function definition. The\nresult must be a callable, which is invoked with the function object\nas the only argument. The returned value is bound to the function name\ninstead of the function object. Multiple decorators are applied in\nnested fashion. For example, the following code\n\n @f1(arg)\n @f2\n def func(): pass\n\nis roughly equivalent to\n\n def func(): pass\n func = f1(arg)(f2(func))\n\nexcept that the original function is not temporarily bound to the name\n"func".\n\nWhen one or more *parameters* have the form *parameter* "="\n*expression*, the function is said to have "default parameter values."\nFor a parameter with a default value, the corresponding *argument* may\nbe omitted from a call, in which case the parameter\'s default value is\nsubstituted. If a parameter has a default value, all following\nparameters up until the ""*"" must also have a default value --- this\nis a syntactic restriction that is not expressed by the grammar.\n\n**Default parameter values are evaluated from left to right when the\nfunction definition is executed.** This means that the expression is\nevaluated once, when the function is defined, and that the same "pre-\ncomputed" value is used for each call. This is especially important\nto understand when a default parameter is a mutable object, such as a\nlist or a dictionary: if the function modifies the object (e.g. by\nappending an item to a list), the default value is in effect modified.\nThis is generally not what was intended. A way around this is to use\n"None" as the default, and explicitly test for it in the body of the\nfunction, e.g.:\n\n def whats_on_the_telly(penguin=None):\n if penguin is None:\n penguin = []\n penguin.append("property of the zoo")\n return penguin\n\nFunction call semantics are described in more detail in section\n*Calls*. A function call always assigns values to all parameters\nmentioned in the parameter list, either from position arguments, from\nkeyword arguments, or from default values. If the form\n""*identifier"" is present, it is initialized to a tuple receiving any\nexcess positional parameters, defaulting to the empty tuple. If the\nform ""**identifier"" is present, it is initialized to a new\ndictionary receiving any excess keyword arguments, defaulting to a new\nempty dictionary. Parameters after ""*"" or ""*identifier"" are\nkeyword-only parameters and may only be passed used keyword arguments.\n\nParameters may have annotations of the form "": expression"" following\nthe parameter name. Any parameter may have an annotation even those\nof the form "*identifier" or "**identifier". Functions may have\n"return" annotation of the form ""-> expression"" after the parameter\nlist. These annotations can be any valid Python expression and are\nevaluated when the function definition is executed. Annotations may\nbe evaluated in a different order than they appear in the source code.\nThe presence of annotations does not change the semantics of a\nfunction. The annotation values are available as values of a\ndictionary keyed by the parameters\' names in the "__annotations__"\nattribute of the function object.\n\nIt is also possible to create anonymous functions (functions not bound\nto a name), for immediate use in expressions. This uses lambda\nexpressions, described in section *Lambdas*. Note that the lambda\nexpression is merely a shorthand for a simplified function definition;\na function defined in a ""def"" statement can be passed around or\nassigned to another name just like a function defined by a lambda\nexpression. The ""def"" form is actually more powerful since it\nallows the execution of multiple statements and annotations.\n\n**Programmer\'s note:** Functions are first-class objects. A ""def""\nstatement executed inside a function definition defines a local\nfunction that can be returned or passed around. Free variables used\nin the nested function can access the local variables of the function\ncontaining the def. See section *Naming and binding* for details.\n\nSee also: **PEP 3107** - Function Annotations\n\n The original specification for function annotations.\n',
- 'global': u'\nThe "global" statement\n**********************\n\n global_stmt ::= "global" identifier ("," identifier)*\n\nThe "global" statement is a declaration which holds for the entire\ncurrent code block. It means that the listed identifiers are to be\ninterpreted as globals. It would be impossible to assign to a global\nvariable without "global", although free variables may refer to\nglobals without being declared global.\n\nNames listed in a "global" statement must not be used in the same code\nblock textually preceding that "global" statement.\n\nNames listed in a "global" statement must not be defined as formal\nparameters or in a "for" loop control target, "class" definition,\nfunction definition, or "import" statement.\n\n**CPython implementation detail:** The current implementation does not\nenforce the two restrictions, but programs should not abuse this\nfreedom, as future implementations may enforce them or silently change\nthe meaning of the program.\n\n**Programmer\'s note:** the "global" is a directive to the parser. It\napplies only to code parsed at the same time as the "global"\nstatement. In particular, a "global" statement contained in a string\nor code object supplied to the built-in "exec()" function does not\naffect the code block *containing* the function call, and code\ncontained in such a string is unaffected by "global" statements in the\ncode containing the function call. The same applies to the "eval()"\nand "compile()" functions.\n',
- 'id-classes': u'\nReserved classes of identifiers\n*******************************\n\nCertain classes of identifiers (besides keywords) have special\nmeanings. These classes are identified by the patterns of leading and\ntrailing underscore characters:\n\n"_*"\n Not imported by "from module import *". The special identifier "_"\n is used in the interactive interpreter to store the result of the\n last evaluation; it is stored in the "builtins" module. When not\n in interactive mode, "_" has no special meaning and is not defined.\n See section *The import statement*.\n\n Note: The name "_" is often used in conjunction with\n internationalization; refer to the documentation for the\n "gettext" module for more information on this convention.\n\n"__*__"\n System-defined names. These names are defined by the interpreter\n and its implementation (including the standard library). Current\n system names are discussed in the *Special method names* section\n and elsewhere. More will likely be defined in future versions of\n Python. *Any* use of "__*__" names, in any context, that does not\n follow explicitly documented use, is subject to breakage without\n warning.\n\n"__*"\n Class-private names. Names in this category, when used within the\n context of a class definition, are re-written to use a mangled form\n to help avoid name clashes between "private" attributes of base and\n derived classes. See section *Identifiers (Names)*.\n',
- 'identifiers': u'\nIdentifiers and keywords\n************************\n\nIdentifiers (also referred to as *names*) are described by the\nfollowing lexical definitions.\n\nThe syntax of identifiers in Python is based on the Unicode standard\nannex UAX-31, with elaboration and changes as defined below; see also\n**PEP 3131** for further details.\n\nWithin the ASCII range (U+0001..U+007F), the valid characters for\nidentifiers are the same as in Python 2.x: the uppercase and lowercase\nletters "A" through "Z", the underscore "_" and, except for the first\ncharacter, the digits "0" through "9".\n\nPython 3.0 introduces additional characters from outside the ASCII\nrange (see **PEP 3131**). For these characters, the classification\nuses the version of the Unicode Character Database as included in the\n"unicodedata" module.\n\nIdentifiers are unlimited in length. Case is significant.\n\n identifier ::= xid_start xid_continue*\n id_start ::= <all characters in general categories Lu, Ll, Lt, Lm, Lo, Nl, the underscore, and characters with the Other_ID_Start property>\n id_continue ::= <all characters in id_start, plus characters in the categories Mn, Mc, Nd, Pc and others with the Other_ID_Continue property>\n xid_start ::= <all characters in id_start whose NFKC normalization is in "id_start xid_continue*">\n xid_continue ::= <all characters in id_continue whose NFKC normalization is in "id_continue*">\n\nThe Unicode category codes mentioned above stand for:\n\n* *Lu* - uppercase letters\n\n* *Ll* - lowercase letters\n\n* *Lt* - titlecase letters\n\n* *Lm* - modifier letters\n\n* *Lo* - other letters\n\n* *Nl* - letter numbers\n\n* *Mn* - nonspacing marks\n\n* *Mc* - spacing combining marks\n\n* *Nd* - decimal numbers\n\n* *Pc* - connector punctuations\n\n* *Other_ID_Start* - explicit list of characters in PropList.txt to\n support backwards compatibility\n\n* *Other_ID_Continue* - likewise\n\nAll identifiers are converted into the normal form NFKC while parsing;\ncomparison of identifiers is based on NFKC.\n\nA non-normative HTML file listing all valid identifier characters for\nUnicode 4.1 can be found at https://www.dcl.hpi.uni-\npotsdam.de/home/loewis/table-3131.html.\n\n\nKeywords\n========\n\nThe following identifiers are used as reserved words, or *keywords* of\nthe language, and cannot be used as ordinary identifiers. They must\nbe spelled exactly as written here:\n\n False class finally is return\n None continue for lambda try\n True def from nonlocal while\n and del global not with\n as elif if or yield\n assert else import pass\n break except in raise\n\n\nReserved classes of identifiers\n===============================\n\nCertain classes of identifiers (besides keywords) have special\nmeanings. These classes are identified by the patterns of leading and\ntrailing underscore characters:\n\n"_*"\n Not imported by "from module import *". The special identifier "_"\n is used in the interactive interpreter to store the result of the\n last evaluation; it is stored in the "builtins" module. When not\n in interactive mode, "_" has no special meaning and is not defined.\n See section *The import statement*.\n\n Note: The name "_" is often used in conjunction with\n internationalization; refer to the documentation for the\n "gettext" module for more information on this convention.\n\n"__*__"\n System-defined names. These names are defined by the interpreter\n and its implementation (including the standard library). Current\n system names are discussed in the *Special method names* section\n and elsewhere. More will likely be defined in future versions of\n Python. *Any* use of "__*__" names, in any context, that does not\n follow explicitly documented use, is subject to breakage without\n warning.\n\n"__*"\n Class-private names. Names in this category, when used within the\n context of a class definition, are re-written to use a mangled form\n to help avoid name clashes between "private" attributes of base and\n derived classes. See section *Identifiers (Names)*.\n',
- 'if': u'\nThe "if" statement\n******************\n\nThe "if" statement is used for conditional execution:\n\n if_stmt ::= "if" expression ":" suite\n ( "elif" expression ":" suite )*\n ["else" ":" suite]\n\nIt selects exactly one of the suites by evaluating the expressions one\nby one until one is found to be true (see section *Boolean operations*\nfor the definition of true and false); then that suite is executed\n(and no other part of the "if" statement is executed or evaluated).\nIf all expressions are false, the suite of the "else" clause, if\npresent, is executed.\n',
- 'imaginary': u'\nImaginary literals\n******************\n\nImaginary literals are described by the following lexical definitions:\n\n imagnumber ::= (floatnumber | intpart) ("j" | "J")\n\nAn imaginary literal yields a complex number with a real part of 0.0.\nComplex numbers are represented as a pair of floating point numbers\nand have the same restrictions on their range. To create a complex\nnumber with a nonzero real part, add a floating point number to it,\ne.g., "(3+4j)". Some examples of imaginary literals:\n\n 3.14j 10.j 10j .001j 1e100j 3.14e-10j\n',
- 'import': u'\nThe "import" statement\n**********************\n\n import_stmt ::= "import" module ["as" name] ( "," module ["as" name] )*\n | "from" relative_module "import" identifier ["as" name]\n ( "," identifier ["as" name] )*\n | "from" relative_module "import" "(" identifier ["as" name]\n ( "," identifier ["as" name] )* [","] ")"\n | "from" module "import" "*"\n module ::= (identifier ".")* identifier\n relative_module ::= "."* module | "."+\n name ::= identifier\n\nThe basic import statement (no "from" clause) is executed in two\nsteps:\n\n1. find a module, loading and initializing it if necessary\n\n2. define a name or names in the local namespace for the scope\n where the "import" statement occurs.\n\nWhen the statement contains multiple clauses (separated by commas) the\ntwo steps are carried out separately for each clause, just as though\nthe clauses had been separated out into individual import statements.\n\nThe details of the first step, finding and loading modules are\ndescribed in greater detail in the section on the *import system*,\nwhich also describes the various types of packages and modules that\ncan be imported, as well as all the hooks that can be used to\ncustomize the import system. Note that failures in this step may\nindicate either that the module could not be located, *or* that an\nerror occurred while initializing the module, which includes execution\nof the module\'s code.\n\nIf the requested module is retrieved successfully, it will be made\navailable in the local namespace in one of three ways:\n\n* If the module name is followed by "as", then the name following\n "as" is bound directly to the imported module.\n\n* If no other name is specified, and the module being imported is a\n top level module, the module\'s name is bound in the local namespace\n as a reference to the imported module\n\n* If the module being imported is *not* a top level module, then the\n name of the top level package that contains the module is bound in\n the local namespace as a reference to the top level package. The\n imported module must be accessed using its full qualified name\n rather than directly\n\nThe "from" form uses a slightly more complex process:\n\n1. find the module specified in the "from" clause, loading and\n initializing it if necessary;\n\n2. for each of the identifiers specified in the "import" clauses:\n\n 1. check if the imported module has an attribute by that name\n\n 2. if not, attempt to import a submodule with that name and then\n check the imported module again for that attribute\n\n 3. if the attribute is not found, "ImportError" is raised.\n\n 4. otherwise, a reference to that value is stored in the local\n namespace, using the name in the "as" clause if it is present,\n otherwise using the attribute name\n\nExamples:\n\n import foo # foo imported and bound locally\n import foo.bar.baz # foo.bar.baz imported, foo bound locally\n import foo.bar.baz as fbb # foo.bar.baz imported and bound as fbb\n from foo.bar import baz # foo.bar.baz imported and bound as baz\n from foo import attr # foo imported and foo.attr bound as attr\n\nIf the list of identifiers is replaced by a star ("\'*\'"), all public\nnames defined in the module are bound in the local namespace for the\nscope where the "import" statement occurs.\n\nThe *public names* defined by a module are determined by checking the\nmodule\'s namespace for a variable named "__all__"; if defined, it must\nbe a sequence of strings which are names defined or imported by that\nmodule. The names given in "__all__" are all considered public and\nare required to exist. If "__all__" is not defined, the set of public\nnames includes all names found in the module\'s namespace which do not\nbegin with an underscore character ("\'_\'"). "__all__" should contain\nthe entire public API. It is intended to avoid accidentally exporting\nitems that are not part of the API (such as library modules which were\nimported and used within the module).\n\nThe wild card form of import --- "from module import *" --- is only\nallowed at the module level. Attempting to use it in class or\nfunction definitions will raise a "SyntaxError".\n\nWhen specifying what module to import you do not have to specify the\nabsolute name of the module. When a module or package is contained\nwithin another package it is possible to make a relative import within\nthe same top package without having to mention the package name. By\nusing leading dots in the specified module or package after "from" you\ncan specify how high to traverse up the current package hierarchy\nwithout specifying exact names. One leading dot means the current\npackage where the module making the import exists. Two dots means up\none package level. Three dots is up two levels, etc. So if you execute\n"from . import mod" from a module in the "pkg" package then you will\nend up importing "pkg.mod". If you execute "from ..subpkg2 import mod"\nfrom within "pkg.subpkg1" you will import "pkg.subpkg2.mod". The\nspecification for relative imports is contained within **PEP 328**.\n\n"importlib.import_module()" is provided to support applications that\ndetermine dynamically the modules to be loaded.\n\n\nFuture statements\n=================\n\nA *future statement* is a directive to the compiler that a particular\nmodule should be compiled using syntax or semantics that will be\navailable in a specified future release of Python where the feature\nbecomes standard.\n\nThe future statement is intended to ease migration to future versions\nof Python that introduce incompatible changes to the language. It\nallows use of the new features on a per-module basis before the\nrelease in which the feature becomes standard.\n\n future_statement ::= "from" "__future__" "import" feature ["as" name]\n ("," feature ["as" name])*\n | "from" "__future__" "import" "(" feature ["as" name]\n ("," feature ["as" name])* [","] ")"\n feature ::= identifier\n name ::= identifier\n\nA future statement must appear near the top of the module. The only\nlines that can appear before a future statement are:\n\n* the module docstring (if any),\n\n* comments,\n\n* blank lines, and\n\n* other future statements.\n\nThe features recognized by Python 3.0 are "absolute_import",\n"division", "generators", "unicode_literals", "print_function",\n"nested_scopes" and "with_statement". They are all redundant because\nthey are always enabled, and only kept for backwards compatibility.\n\nA future statement is recognized and treated specially at compile\ntime: Changes to the semantics of core constructs are often\nimplemented by generating different code. It may even be the case\nthat a new feature introduces new incompatible syntax (such as a new\nreserved word), in which case the compiler may need to parse the\nmodule differently. Such decisions cannot be pushed off until\nruntime.\n\nFor any given release, the compiler knows which feature names have\nbeen defined, and raises a compile-time error if a future statement\ncontains a feature not known to it.\n\nThe direct runtime semantics are the same as for any import statement:\nthere is a standard module "__future__", described later, and it will\nbe imported in the usual way at the time the future statement is\nexecuted.\n\nThe interesting runtime semantics depend on the specific feature\nenabled by the future statement.\n\nNote that there is nothing special about the statement:\n\n import __future__ [as name]\n\nThat is not a future statement; it\'s an ordinary import statement with\nno special semantics or syntax restrictions.\n\nCode compiled by calls to the built-in functions "exec()" and\n"compile()" that occur in a module "M" containing a future statement\nwill, by default, use the new syntax or semantics associated with the\nfuture statement. This can be controlled by optional arguments to\n"compile()" --- see the documentation of that function for details.\n\nA future statement typed at an interactive interpreter prompt will\ntake effect for the rest of the interpreter session. If an\ninterpreter is started with the *-i* option, is passed a script name\nto execute, and the script includes a future statement, it will be in\neffect in the interactive session started after the script is\nexecuted.\n\nSee also: **PEP 236** - Back to the __future__\n\n The original proposal for the __future__ mechanism.\n',
- 'in': u'\nMembership test operations\n**************************\n\nThe operators "in" and "not in" test for membership. "x in s"\nevaluates to true if *x* is a member of *s*, and false otherwise. "x\nnot in s" returns the negation of "x in s". All built-in sequences\nand set types support this as well as dictionary, for which "in" tests\nwhether the dictionary has a given key. For container types such as\nlist, tuple, set, frozenset, dict, or collections.deque, the\nexpression "x in y" is equivalent to "any(x is e or x == e for e in\ny)".\n\nFor the string and bytes types, "x in y" is true if and only if *x* is\na substring of *y*. An equivalent test is "y.find(x) != -1". Empty\nstrings are always considered to be a substring of any other string,\nso """ in "abc"" will return "True".\n\nFor user-defined classes which define the "__contains__()" method, "x\nin y" is true if and only if "y.__contains__(x)" is true.\n\nFor user-defined classes which do not define "__contains__()" but do\ndefine "__iter__()", "x in y" is true if some value "z" with "x == z"\nis produced while iterating over "y". If an exception is raised\nduring the iteration, it is as if "in" raised that exception.\n\nLastly, the old-style iteration protocol is tried: if a class defines\n"__getitem__()", "x in y" is true if and only if there is a non-\nnegative integer index *i* such that "x == y[i]", and all lower\ninteger indices do not raise "IndexError" exception. (If any other\nexception is raised, it is as if "in" raised that exception).\n\nThe operator "not in" is defined to have the inverse true value of\n"in".\n',
- 'integers': u'\nInteger literals\n****************\n\nInteger literals are described by the following lexical definitions:\n\n integer ::= decimalinteger | octinteger | hexinteger | bininteger\n decimalinteger ::= nonzerodigit digit* | "0"+\n nonzerodigit ::= "1"..."9"\n digit ::= "0"..."9"\n octinteger ::= "0" ("o" | "O") octdigit+\n hexinteger ::= "0" ("x" | "X") hexdigit+\n bininteger ::= "0" ("b" | "B") bindigit+\n octdigit ::= "0"..."7"\n hexdigit ::= digit | "a"..."f" | "A"..."F"\n bindigit ::= "0" | "1"\n\nThere is no limit for the length of integer literals apart from what\ncan be stored in available memory.\n\nNote that leading zeros in a non-zero decimal number are not allowed.\nThis is for disambiguation with C-style octal literals, which Python\nused before version 3.0.\n\nSome examples of integer literals:\n\n 7 2147483647 0o177 0b100110111\n 3 79228162514264337593543950336 0o377 0xdeadbeef\n',
- 'lambda': u'\nLambdas\n*******\n\n lambda_expr ::= "lambda" [parameter_list]: expression\n lambda_expr_nocond ::= "lambda" [parameter_list]: expression_nocond\n\nLambda expressions (sometimes called lambda forms) are used to create\nanonymous functions. The expression "lambda arguments: expression"\nyields a function object. The unnamed object behaves like a function\nobject defined with:\n\n def <lambda>(arguments):\n return expression\n\nSee section *Function definitions* for the syntax of parameter lists.\nNote that functions created with lambda expressions cannot contain\nstatements or annotations.\n',
- 'lists': u'\nList displays\n*************\n\nA list display is a possibly empty series of expressions enclosed in\nsquare brackets:\n\n list_display ::= "[" [starred_list | comprehension] "]"\n\nA list display yields a new list object, the contents being specified\nby either a list of expressions or a comprehension. When a comma-\nseparated list of expressions is supplied, its elements are evaluated\nfrom left to right and placed into the list object in that order.\nWhen a comprehension is supplied, the list is constructed from the\nelements resulting from the comprehension.\n',
- 'naming': u'\nNaming and binding\n******************\n\n\nBinding of names\n================\n\n*Names* refer to objects. Names are introduced by name binding\noperations.\n\nThe following constructs bind names: formal parameters to functions,\n"import" statements, class and function definitions (these bind the\nclass or function name in the defining block), and targets that are\nidentifiers if occurring in an assignment, "for" loop header, or after\n"as" in a "with" statement or "except" clause. The "import" statement\nof the form "from ... import *" binds all names defined in the\nimported module, except those beginning with an underscore. This form\nmay only be used at the module level.\n\nA target occurring in a "del" statement is also considered bound for\nthis purpose (though the actual semantics are to unbind the name).\n\nEach assignment or import statement occurs within a block defined by a\nclass or function definition or at the module level (the top-level\ncode block).\n\nIf a name is bound in a block, it is a local variable of that block,\nunless declared as "nonlocal" or "global". If a name is bound at the\nmodule level, it is a global variable. (The variables of the module\ncode block are local and global.) If a variable is used in a code\nblock but not defined there, it is a *free variable*.\n\nEach occurrence of a name in the program text refers to the *binding*\nof that name established by the following name resolution rules.\n\n\nResolution of names\n===================\n\nA *scope* defines the visibility of a name within a block. If a local\nvariable is defined in a block, its scope includes that block. If the\ndefinition occurs in a function block, the scope extends to any blocks\ncontained within the defining one, unless a contained block introduces\na different binding for the name.\n\nWhen a name is used in a code block, it is resolved using the nearest\nenclosing scope. The set of all such scopes visible to a code block\nis called the block\'s *environment*.\n\nWhen a name is not found at all, a "NameError" exception is raised. If\nthe current scope is a function scope, and the name refers to a local\nvariable that has not yet been bound to a value at the point where the\nname is used, an "UnboundLocalError" exception is raised.\n"UnboundLocalError" is a subclass of "NameError".\n\nIf a name binding operation occurs anywhere within a code block, all\nuses of the name within the block are treated as references to the\ncurrent block. This can lead to errors when a name is used within a\nblock before it is bound. This rule is subtle. Python lacks\ndeclarations and allows name binding operations to occur anywhere\nwithin a code block. The local variables of a code block can be\ndetermined by scanning the entire text of the block for name binding\noperations.\n\nIf the "global" statement occurs within a block, all uses of the name\nspecified in the statement refer to the binding of that name in the\ntop-level namespace. Names are resolved in the top-level namespace by\nsearching the global namespace, i.e. the namespace of the module\ncontaining the code block, and the builtins namespace, the namespace\nof the module "builtins". The global namespace is searched first. If\nthe name is not found there, the builtins namespace is searched. The\n"global" statement must precede all uses of the name.\n\nThe "global" statement has the same scope as a name binding operation\nin the same block. If the nearest enclosing scope for a free variable\ncontains a global statement, the free variable is treated as a global.\n\nThe "nonlocal" statement causes corresponding names to refer to\npreviously bound variables in the nearest enclosing function scope.\n"SyntaxError" is raised at compile time if the given name does not\nexist in any enclosing function scope.\n\nThe namespace for a module is automatically created the first time a\nmodule is imported. The main module for a script is always called\n"__main__".\n\nClass definition blocks and arguments to "exec()" and "eval()" are\nspecial in the context of name resolution. A class definition is an\nexecutable statement that may use and define names. These references\nfollow the normal rules for name resolution with an exception that\nunbound local variables are looked up in the global namespace. The\nnamespace of the class definition becomes the attribute dictionary of\nthe class. The scope of names defined in a class block is limited to\nthe class block; it does not extend to the code blocks of methods --\nthis includes comprehensions and generator expressions since they are\nimplemented using a function scope. This means that the following\nwill fail:\n\n class A:\n a = 42\n b = list(a + i for i in range(10))\n\n\nBuiltins and restricted execution\n=================================\n\nThe builtins namespace associated with the execution of a code block\nis actually found by looking up the name "__builtins__" in its global\nnamespace; this should be a dictionary or a module (in the latter case\nthe module\'s dictionary is used). By default, when in the "__main__"\nmodule, "__builtins__" is the built-in module "builtins"; when in any\nother module, "__builtins__" is an alias for the dictionary of the\n"builtins" module itself. "__builtins__" can be set to a user-created\ndictionary to create a weak form of restricted execution.\n\n**CPython implementation detail:** Users should not touch\n"__builtins__"; it is strictly an implementation detail. Users\nwanting to override values in the builtins namespace should "import"\nthe "builtins" module and modify its attributes appropriately.\n\n\nInteraction with dynamic features\n=================================\n\nName resolution of free variables occurs at runtime, not at compile\ntime. This means that the following code will print 42:\n\n i = 10\n def f():\n print(i)\n i = 42\n f()\n\nThere are several cases where Python statements are illegal when used\nin conjunction with nested scopes that contain free variables.\n\nIf a variable is referenced in an enclosing scope, it is illegal to\ndelete the name. An error will be reported at compile time.\n\nThe "eval()" and "exec()" functions do not have access to the full\nenvironment for resolving names. Names may be resolved in the local\nand global namespaces of the caller. Free variables are not resolved\nin the nearest enclosing namespace, but in the global namespace. [1]\nThe "exec()" and "eval()" functions have optional arguments to\noverride the global and local namespace. If only one namespace is\nspecified, it is used for both.\n',
- 'nonlocal': u'\nThe "nonlocal" statement\n************************\n\n nonlocal_stmt ::= "nonlocal" identifier ("," identifier)*\n\nThe "nonlocal" statement causes the listed identifiers to refer to\npreviously bound variables in the nearest enclosing scope excluding\nglobals. This is important because the default behavior for binding is\nto search the local namespace first. The statement allows\nencapsulated code to rebind variables outside of the local scope\nbesides the global (module) scope.\n\nNames listed in a "nonlocal" statement, unlike those listed in a\n"global" statement, must refer to pre-existing bindings in an\nenclosing scope (the scope in which a new binding should be created\ncannot be determined unambiguously).\n\nNames listed in a "nonlocal" statement must not collide with pre-\nexisting bindings in the local scope.\n\nSee also: **PEP 3104** - Access to Names in Outer Scopes\n\n The specification for the "nonlocal" statement.\n',
- 'numbers': u'\nNumeric literals\n****************\n\nThere are three types of numeric literals: integers, floating point\nnumbers, and imaginary numbers. There are no complex literals\n(complex numbers can be formed by adding a real number and an\nimaginary number).\n\nNote that numeric literals do not include a sign; a phrase like "-1"\nis actually an expression composed of the unary operator \'"-"\' and the\nliteral "1".\n',
- 'numeric-types': u'\nEmulating numeric types\n***********************\n\nThe following methods can be defined to emulate numeric objects.\nMethods corresponding to operations that are not supported by the\nparticular kind of number implemented (e.g., bitwise operations for\nnon-integral numbers) should be left undefined.\n\nobject.__add__(self, other)\nobject.__sub__(self, other)\nobject.__mul__(self, other)\nobject.__matmul__(self, other)\nobject.__truediv__(self, other)\nobject.__floordiv__(self, other)\nobject.__mod__(self, other)\nobject.__divmod__(self, other)\nobject.__pow__(self, other[, modulo])\nobject.__lshift__(self, other)\nobject.__rshift__(self, other)\nobject.__and__(self, other)\nobject.__xor__(self, other)\nobject.__or__(self, other)\n\n These methods are called to implement the binary arithmetic\n operations ("+", "-", "*", "@", "/", "//", "%", "divmod()",\n "pow()", "**", "<<", ">>", "&", "^", "|"). For instance, to\n evaluate the expression "x + y", where *x* is an instance of a\n class that has an "__add__()" method, "x.__add__(y)" is called.\n The "__divmod__()" method should be the equivalent to using\n "__floordiv__()" and "__mod__()"; it should not be related to\n "__truediv__()". Note that "__pow__()" should be defined to accept\n an optional third argument if the ternary version of the built-in\n "pow()" function is to be supported.\n\n If one of those methods does not support the operation with the\n supplied arguments, it should return "NotImplemented".\n\nobject.__radd__(self, other)\nobject.__rsub__(self, other)\nobject.__rmul__(self, other)\nobject.__rmatmul__(self, other)\nobject.__rtruediv__(self, other)\nobject.__rfloordiv__(self, other)\nobject.__rmod__(self, other)\nobject.__rdivmod__(self, other)\nobject.__rpow__(self, other)\nobject.__rlshift__(self, other)\nobject.__rrshift__(self, other)\nobject.__rand__(self, other)\nobject.__rxor__(self, other)\nobject.__ror__(self, other)\n\n These methods are called to implement the binary arithmetic\n operations ("+", "-", "*", "@", "/", "//", "%", "divmod()",\n "pow()", "**", "<<", ">>", "&", "^", "|") with reflected (swapped)\n operands. These functions are only called if the left operand does\n not support the corresponding operation and the operands are of\n different types. [2] For instance, to evaluate the expression "x -\n y", where *y* is an instance of a class that has an "__rsub__()"\n method, "y.__rsub__(x)" is called if "x.__sub__(y)" returns\n *NotImplemented*.\n\n Note that ternary "pow()" will not try calling "__rpow__()" (the\n coercion rules would become too complicated).\n\n Note: If the right operand\'s type is a subclass of the left\n operand\'s type and that subclass provides the reflected method\n for the operation, this method will be called before the left\n operand\'s non-reflected method. This behavior allows subclasses\n to override their ancestors\' operations.\n\nobject.__iadd__(self, other)\nobject.__isub__(self, other)\nobject.__imul__(self, other)\nobject.__imatmul__(self, other)\nobject.__itruediv__(self, other)\nobject.__ifloordiv__(self, other)\nobject.__imod__(self, other)\nobject.__ipow__(self, other[, modulo])\nobject.__ilshift__(self, other)\nobject.__irshift__(self, other)\nobject.__iand__(self, other)\nobject.__ixor__(self, other)\nobject.__ior__(self, other)\n\n These methods are called to implement the augmented arithmetic\n assignments ("+=", "-=", "*=", "@=", "/=", "//=", "%=", "**=",\n "<<=", ">>=", "&=", "^=", "|="). These methods should attempt to\n do the operation in-place (modifying *self*) and return the result\n (which could be, but does not have to be, *self*). If a specific\n method is not defined, the augmented assignment falls back to the\n normal methods. For instance, if *x* is an instance of a class\n with an "__iadd__()" method, "x += y" is equivalent to "x =\n x.__iadd__(y)" . Otherwise, "x.__add__(y)" and "y.__radd__(x)" are\n considered, as with the evaluation of "x + y". In certain\n situations, augmented assignment can result in unexpected errors\n (see *Why does a_tuple[i] += [\'item\'] raise an exception when the\n addition works?*), but this behavior is in fact part of the data\n model.\n\nobject.__neg__(self)\nobject.__pos__(self)\nobject.__abs__(self)\nobject.__invert__(self)\n\n Called to implement the unary arithmetic operations ("-", "+",\n "abs()" and "~").\n\nobject.__complex__(self)\nobject.__int__(self)\nobject.__float__(self)\nobject.__round__(self[, n])\n\n Called to implement the built-in functions "complex()", "int()",\n "float()" and "round()". Should return a value of the appropriate\n type.\n\nobject.__index__(self)\n\n Called to implement "operator.index()", and whenever Python needs\n to losslessly convert the numeric object to an integer object (such\n as in slicing, or in the built-in "bin()", "hex()" and "oct()"\n functions). Presence of this method indicates that the numeric\n object is an integer type. Must return an integer.\n\n Note: In order to have a coherent integer type class, when\n "__index__()" is defined "__int__()" should also be defined, and\n both should return the same value.\n',
- 'objects': u'\nObjects, values and types\n*************************\n\n*Objects* are Python\'s abstraction for data. All data in a Python\nprogram is represented by objects or by relations between objects. (In\na sense, and in conformance to Von Neumann\'s model of a "stored\nprogram computer," code is also represented by objects.)\n\nEvery object has an identity, a type and a value. An object\'s\n*identity* never changes once it has been created; you may think of it\nas the object\'s address in memory. The \'"is"\' operator compares the\nidentity of two objects; the "id()" function returns an integer\nrepresenting its identity.\n\n**CPython implementation detail:** For CPython, "id(x)" is the memory\naddress where "x" is stored.\n\nAn object\'s type determines the operations that the object supports\n(e.g., "does it have a length?") and also defines the possible values\nfor objects of that type. The "type()" function returns an object\'s\ntype (which is an object itself). Like its identity, an object\'s\n*type* is also unchangeable. [1]\n\nThe *value* of some objects can change. Objects whose value can\nchange are said to be *mutable*; objects whose value is unchangeable\nonce they are created are called *immutable*. (The value of an\nimmutable container object that contains a reference to a mutable\nobject can change when the latter\'s value is changed; however the\ncontainer is still considered immutable, because the collection of\nobjects it contains cannot be changed. So, immutability is not\nstrictly the same as having an unchangeable value, it is more subtle.)\nAn object\'s mutability is determined by its type; for instance,\nnumbers, strings and tuples are immutable, while dictionaries and\nlists are mutable.\n\nObjects are never explicitly destroyed; however, when they become\nunreachable they may be garbage-collected. An implementation is\nallowed to postpone garbage collection or omit it altogether --- it is\na matter of implementation quality how garbage collection is\nimplemented, as long as no objects are collected that are still\nreachable.\n\n**CPython implementation detail:** CPython currently uses a reference-\ncounting scheme with (optional) delayed detection of cyclically linked\ngarbage, which collects most objects as soon as they become\nunreachable, but is not guaranteed to collect garbage containing\ncircular references. See the documentation of the "gc" module for\ninformation on controlling the collection of cyclic garbage. Other\nimplementations act differently and CPython may change. Do not depend\non immediate finalization of objects when they become unreachable (so\nyou should always close files explicitly).\n\nNote that the use of the implementation\'s tracing or debugging\nfacilities may keep objects alive that would normally be collectable.\nAlso note that catching an exception with a \'"try"..."except"\'\nstatement may keep objects alive.\n\nSome objects contain references to "external" resources such as open\nfiles or windows. It is understood that these resources are freed\nwhen the object is garbage-collected, but since garbage collection is\nnot guaranteed to happen, such objects also provide an explicit way to\nrelease the external resource, usually a "close()" method. Programs\nare strongly recommended to explicitly close such objects. The\n\'"try"..."finally"\' statement and the \'"with"\' statement provide\nconvenient ways to do this.\n\nSome objects contain references to other objects; these are called\n*containers*. Examples of containers are tuples, lists and\ndictionaries. The references are part of a container\'s value. In\nmost cases, when we talk about the value of a container, we imply the\nvalues, not the identities of the contained objects; however, when we\ntalk about the mutability of a container, only the identities of the\nimmediately contained objects are implied. So, if an immutable\ncontainer (like a tuple) contains a reference to a mutable object, its\nvalue changes if that mutable object is changed.\n\nTypes affect almost all aspects of object behavior. Even the\nimportance of object identity is affected in some sense: for immutable\ntypes, operations that compute new values may actually return a\nreference to any existing object with the same type and value, while\nfor mutable objects this is not allowed. E.g., after "a = 1; b = 1",\n"a" and "b" may or may not refer to the same object with the value\none, depending on the implementation, but after "c = []; d = []", "c"\nand "d" are guaranteed to refer to two different, unique, newly\ncreated empty lists. (Note that "c = d = []" assigns the same object\nto both "c" and "d".)\n',
- 'operator-summary': u'\nOperator precedence\n*******************\n\nThe following table summarizes the operator precedence in Python, from\nlowest precedence (least binding) to highest precedence (most\nbinding). Operators in the same box have the same precedence. Unless\nthe syntax is explicitly given, operators are binary. Operators in\nthe same box group left to right (except for exponentiation, which\ngroups from right to left).\n\nNote that comparisons, membership tests, and identity tests, all have\nthe same precedence and have a left-to-right chaining feature as\ndescribed in the *Comparisons* section.\n\n+-------------------------------------------------+---------------------------------------+\n| Operator | Description |\n+=================================================+=======================================+\n| "lambda" | Lambda expression |\n+-------------------------------------------------+---------------------------------------+\n| "if" -- "else" | Conditional expression |\n+-------------------------------------------------+---------------------------------------+\n| "or" | Boolean OR |\n+-------------------------------------------------+---------------------------------------+\n| "and" | Boolean AND |\n+-------------------------------------------------+---------------------------------------+\n| "not" "x" | Boolean NOT |\n+-------------------------------------------------+---------------------------------------+\n| "in", "not in", "is", "is not", "<", "<=", ">", | Comparisons, including membership |\n| ">=", "!=", "==" | tests and identity tests |\n+-------------------------------------------------+---------------------------------------+\n| "|" | Bitwise OR |\n+-------------------------------------------------+---------------------------------------+\n| "^" | Bitwise XOR |\n+-------------------------------------------------+---------------------------------------+\n| "&" | Bitwise AND |\n+-------------------------------------------------+---------------------------------------+\n| "<<", ">>" | Shifts |\n+-------------------------------------------------+---------------------------------------+\n| "+", "-" | Addition and subtraction |\n+-------------------------------------------------+---------------------------------------+\n| "*", "@", "/", "//", "%" | Multiplication, matrix multiplication |\n| | division, remainder [5] |\n+-------------------------------------------------+---------------------------------------+\n| "+x", "-x", "~x" | Positive, negative, bitwise NOT |\n+-------------------------------------------------+---------------------------------------+\n| "**" | Exponentiation [6] |\n+-------------------------------------------------+---------------------------------------+\n| "await" "x" | Await expression |\n+-------------------------------------------------+---------------------------------------+\n| "x[index]", "x[index:index]", | Subscription, slicing, call, |\n| "x(arguments...)", "x.attribute" | attribute reference |\n+-------------------------------------------------+---------------------------------------+\n| "(expressions...)", "[expressions...]", "{key: | Binding or tuple display, list |\n| value...}", "{expressions...}" | display, dictionary display, set |\n| | display |\n+-------------------------------------------------+---------------------------------------+\n\n-[ Footnotes ]-\n\n[1] While "abs(x%y) < abs(y)" is true mathematically, for floats\n it may not be true numerically due to roundoff. For example, and\n assuming a platform on which a Python float is an IEEE 754 double-\n precision number, in order that "-1e-100 % 1e100" have the same\n sign as "1e100", the computed result is "-1e-100 + 1e100", which\n is numerically exactly equal to "1e100". The function\n "math.fmod()" returns a result whose sign matches the sign of the\n first argument instead, and so returns "-1e-100" in this case.\n Which approach is more appropriate depends on the application.\n\n[2] If x is very close to an exact integer multiple of y, it\'s\n possible for "x//y" to be one larger than "(x-x%y)//y" due to\n rounding. In such cases, Python returns the latter result, in\n order to preserve that "divmod(x,y)[0] * y + x % y" be very close\n to "x".\n\n[3] The Unicode standard distinguishes between *code points* (e.g.\n U+0041) and *abstract characters* (e.g. "LATIN CAPITAL LETTER A").\n While most abstract characters in Unicode are only represented\n using one code point, there is a number of abstract characters\n that can in addition be represented using a sequence of more than\n one code point. For example, the abstract character "LATIN\n CAPITAL LETTER C WITH CEDILLA" can be represented as a single\n *precomposed character* at code position U+00C7, or as a sequence\n of a *base character* at code position U+0043 (LATIN CAPITAL\n LETTER C), followed by a *combining character* at code position\n U+0327 (COMBINING CEDILLA).\n\n The comparison operators on strings compare at the level of\n Unicode code points. This may be counter-intuitive to humans. For\n example, ""\\u00C7" == "\\u0043\\u0327"" is "False", even though both\n strings represent the same abstract character "LATIN CAPITAL\n LETTER C WITH CEDILLA".\n\n To compare strings at the level of abstract characters (that is,\n in a way intuitive to humans), use "unicodedata.normalize()".\n\n[4] Due to automatic garbage-collection, free lists, and the\n dynamic nature of descriptors, you may notice seemingly unusual\n behaviour in certain uses of the "is" operator, like those\n involving comparisons between instance methods, or constants.\n Check their documentation for more info.\n\n[5] The "%" operator is also used for string formatting; the same\n precedence applies.\n\n[6] The power operator "**" binds less tightly than an arithmetic\n or bitwise unary operator on its right, that is, "2**-1" is "0.5".\n',
- 'pass': u'\nThe "pass" statement\n********************\n\n pass_stmt ::= "pass"\n\n"pass" is a null operation --- when it is executed, nothing happens.\nIt is useful as a placeholder when a statement is required\nsyntactically, but no code needs to be executed, for example:\n\n def f(arg): pass # a function that does nothing (yet)\n\n class C: pass # a class with no methods (yet)\n',
- 'power': u'\nThe power operator\n******************\n\nThe power operator binds more tightly than unary operators on its\nleft; it binds less tightly than unary operators on its right. The\nsyntax is:\n\n power ::= ( await_expr | primary ) ["**" u_expr]\n\nThus, in an unparenthesized sequence of power and unary operators, the\noperators are evaluated from right to left (this does not constrain\nthe evaluation order for the operands): "-1**2" results in "-1".\n\nThe power operator has the same semantics as the built-in "pow()"\nfunction, when called with two arguments: it yields its left argument\nraised to the power of its right argument. The numeric arguments are\nfirst converted to a common type, and the result is of that type.\n\nFor int operands, the result has the same type as the operands unless\nthe second argument is negative; in that case, all arguments are\nconverted to float and a float result is delivered. For example,\n"10**2" returns "100", but "10**-2" returns "0.01".\n\nRaising "0.0" to a negative power results in a "ZeroDivisionError".\nRaising a negative number to a fractional power results in a "complex"\nnumber. (In earlier versions it raised a "ValueError".)\n',
- 'raise': u'\nThe "raise" statement\n*********************\n\n raise_stmt ::= "raise" [expression ["from" expression]]\n\nIf no expressions are present, "raise" re-raises the last exception\nthat was active in the current scope. If no exception is active in\nthe current scope, a "RuntimeError" exception is raised indicating\nthat this is an error.\n\nOtherwise, "raise" evaluates the first expression as the exception\nobject. It must be either a subclass or an instance of\n"BaseException". If it is a class, the exception instance will be\nobtained when needed by instantiating the class with no arguments.\n\nThe *type* of the exception is the exception instance\'s class, the\n*value* is the instance itself.\n\nA traceback object is normally created automatically when an exception\nis raised and attached to it as the "__traceback__" attribute, which\nis writable. You can create an exception and set your own traceback in\none step using the "with_traceback()" exception method (which returns\nthe same exception instance, with its traceback set to its argument),\nlike so:\n\n raise Exception("foo occurred").with_traceback(tracebackobj)\n\nThe "from" clause is used for exception chaining: if given, the second\n*expression* must be another exception class or instance, which will\nthen be attached to the raised exception as the "__cause__" attribute\n(which is writable). If the raised exception is not handled, both\nexceptions will be printed:\n\n >>> try:\n ... print(1 / 0)\n ... except Exception as exc:\n ... raise RuntimeError("Something bad happened") from exc\n ...\n Traceback (most recent call last):\n File "<stdin>", line 2, in <module>\n ZeroDivisionError: int division or modulo by zero\n\n The above exception was the direct cause of the following exception:\n\n Traceback (most recent call last):\n File "<stdin>", line 4, in <module>\n RuntimeError: Something bad happened\n\nA similar mechanism works implicitly if an exception is raised inside\nan exception handler or a "finally" clause: the previous exception is\nthen attached as the new exception\'s "__context__" attribute:\n\n >>> try:\n ... print(1 / 0)\n ... except:\n ... raise RuntimeError("Something bad happened")\n ...\n Traceback (most recent call last):\n File "<stdin>", line 2, in <module>\n ZeroDivisionError: int division or modulo by zero\n\n During handling of the above exception, another exception occurred:\n\n Traceback (most recent call last):\n File "<stdin>", line 4, in <module>\n RuntimeError: Something bad happened\n\nAdditional information on exceptions can be found in section\n*Exceptions*, and information about handling exceptions is in section\n*The try statement*.\n',
- 'return': u'\nThe "return" statement\n**********************\n\n return_stmt ::= "return" [expression_list]\n\n"return" may only occur syntactically nested in a function definition,\nnot within a nested class definition.\n\nIf an expression list is present, it is evaluated, else "None" is\nsubstituted.\n\n"return" leaves the current function call with the expression list (or\n"None") as return value.\n\nWhen "return" passes control out of a "try" statement with a "finally"\nclause, that "finally" clause is executed before really leaving the\nfunction.\n\nIn a generator function, the "return" statement indicates that the\ngenerator is done and will cause "StopIteration" to be raised. The\nreturned value (if any) is used as an argument to construct\n"StopIteration" and becomes the "StopIteration.value" attribute.\n',
- 'sequence-types': u'\nEmulating container types\n*************************\n\nThe following methods can be defined to implement container objects.\nContainers usually are sequences (such as lists or tuples) or mappings\n(like dictionaries), but can represent other containers as well. The\nfirst set of methods is used either to emulate a sequence or to\nemulate a mapping; the difference is that for a sequence, the\nallowable keys should be the integers *k* for which "0 <= k < N" where\n*N* is the length of the sequence, or slice objects, which define a\nrange of items. It is also recommended that mappings provide the\nmethods "keys()", "values()", "items()", "get()", "clear()",\n"setdefault()", "pop()", "popitem()", "copy()", and "update()"\nbehaving similar to those for Python\'s standard dictionary objects.\nThe "collections" module provides a "MutableMapping" abstract base\nclass to help create those methods from a base set of "__getitem__()",\n"__setitem__()", "__delitem__()", and "keys()". Mutable sequences\nshould provide methods "append()", "count()", "index()", "extend()",\n"insert()", "pop()", "remove()", "reverse()" and "sort()", like Python\nstandard list objects. Finally, sequence types should implement\naddition (meaning concatenation) and multiplication (meaning\nrepetition) by defining the methods "__add__()", "__radd__()",\n"__iadd__()", "__mul__()", "__rmul__()" and "__imul__()" described\nbelow; they should not define other numerical operators. It is\nrecommended that both mappings and sequences implement the\n"__contains__()" method to allow efficient use of the "in" operator;\nfor mappings, "in" should search the mapping\'s keys; for sequences, it\nshould search through the values. It is further recommended that both\nmappings and sequences implement the "__iter__()" method to allow\nefficient iteration through the container; for mappings, "__iter__()"\nshould be the same as "keys()"; for sequences, it should iterate\nthrough the values.\n\nobject.__len__(self)\n\n Called to implement the built-in function "len()". Should return\n the length of the object, an integer ">=" 0. Also, an object that\n doesn\'t define a "__bool__()" method and whose "__len__()" method\n returns zero is considered to be false in a Boolean context.\n\nobject.__length_hint__(self)\n\n Called to implement "operator.length_hint()". Should return an\n estimated length for the object (which may be greater or less than\n the actual length). The length must be an integer ">=" 0. This\n method is purely an optimization and is never required for\n correctness.\n\n New in version 3.4.\n\nNote: Slicing is done exclusively with the following three methods.\n A call like\n\n a[1:2] = b\n\n is translated to\n\n a[slice(1, 2, None)] = b\n\n and so forth. Missing slice items are always filled in with "None".\n\nobject.__getitem__(self, key)\n\n Called to implement evaluation of "self[key]". For sequence types,\n the accepted keys should be integers and slice objects. Note that\n the special interpretation of negative indexes (if the class wishes\n to emulate a sequence type) is up to the "__getitem__()" method. If\n *key* is of an inappropriate type, "TypeError" may be raised; if of\n a value outside the set of indexes for the sequence (after any\n special interpretation of negative values), "IndexError" should be\n raised. For mapping types, if *key* is missing (not in the\n container), "KeyError" should be raised.\n\n Note: "for" loops expect that an "IndexError" will be raised for\n illegal indexes to allow proper detection of the end of the\n sequence.\n\nobject.__missing__(self, key)\n\n Called by "dict"."__getitem__()" to implement "self[key]" for dict\n subclasses when key is not in the dictionary.\n\nobject.__setitem__(self, key, value)\n\n Called to implement assignment to "self[key]". Same note as for\n "__getitem__()". This should only be implemented for mappings if\n the objects support changes to the values for keys, or if new keys\n can be added, or for sequences if elements can be replaced. The\n same exceptions should be raised for improper *key* values as for\n the "__getitem__()" method.\n\nobject.__delitem__(self, key)\n\n Called to implement deletion of "self[key]". Same note as for\n "__getitem__()". This should only be implemented for mappings if\n the objects support removal of keys, or for sequences if elements\n can be removed from the sequence. The same exceptions should be\n raised for improper *key* values as for the "__getitem__()" method.\n\nobject.__iter__(self)\n\n This method is called when an iterator is required for a container.\n This method should return a new iterator object that can iterate\n over all the objects in the container. For mappings, it should\n iterate over the keys of the container.\n\n Iterator objects also need to implement this method; they are\n required to return themselves. For more information on iterator\n objects, see *Iterator Types*.\n\nobject.__reversed__(self)\n\n Called (if present) by the "reversed()" built-in to implement\n reverse iteration. It should return a new iterator object that\n iterates over all the objects in the container in reverse order.\n\n If the "__reversed__()" method is not provided, the "reversed()"\n built-in will fall back to using the sequence protocol ("__len__()"\n and "__getitem__()"). Objects that support the sequence protocol\n should only provide "__reversed__()" if they can provide an\n implementation that is more efficient than the one provided by\n "reversed()".\n\nThe membership test operators ("in" and "not in") are normally\nimplemented as an iteration through a sequence. However, container\nobjects can supply the following special method with a more efficient\nimplementation, which also does not require the object be a sequence.\n\nobject.__contains__(self, item)\n\n Called to implement membership test operators. Should return true\n if *item* is in *self*, false otherwise. For mapping objects, this\n should consider the keys of the mapping rather than the values or\n the key-item pairs.\n\n For objects that don\'t define "__contains__()", the membership test\n first tries iteration via "__iter__()", then the old sequence\n iteration protocol via "__getitem__()", see *this section in the\n language reference*.\n',
- 'shifting': u'\nShifting operations\n*******************\n\nThe shifting operations have lower priority than the arithmetic\noperations:\n\n shift_expr ::= a_expr | shift_expr ( "<<" | ">>" ) a_expr\n\nThese operators accept integers as arguments. They shift the first\nargument to the left or right by the number of bits given by the\nsecond argument.\n\nA right shift by *n* bits is defined as floor division by "pow(2,n)".\nA left shift by *n* bits is defined as multiplication with "pow(2,n)".\n\nNote: In the current implementation, the right-hand operand is\n required to be at most "sys.maxsize". If the right-hand operand is\n larger than "sys.maxsize" an "OverflowError" exception is raised.\n',
- 'slicings': u'\nSlicings\n********\n\nA slicing selects a range of items in a sequence object (e.g., a\nstring, tuple or list). Slicings may be used as expressions or as\ntargets in assignment or "del" statements. The syntax for a slicing:\n\n slicing ::= primary "[" slice_list "]"\n slice_list ::= slice_item ("," slice_item)* [","]\n slice_item ::= expression | proper_slice\n proper_slice ::= [lower_bound] ":" [upper_bound] [ ":" [stride] ]\n lower_bound ::= expression\n upper_bound ::= expression\n stride ::= expression\n\nThere is ambiguity in the formal syntax here: anything that looks like\nan expression list also looks like a slice list, so any subscription\ncan be interpreted as a slicing. Rather than further complicating the\nsyntax, this is disambiguated by defining that in this case the\ninterpretation as a subscription takes priority over the\ninterpretation as a slicing (this is the case if the slice list\ncontains no proper slice).\n\nThe semantics for a slicing are as follows. The primary is indexed\n(using the same "__getitem__()" method as normal subscription) with a\nkey that is constructed from the slice list, as follows. If the slice\nlist contains at least one comma, the key is a tuple containing the\nconversion of the slice items; otherwise, the conversion of the lone\nslice item is the key. The conversion of a slice item that is an\nexpression is that expression. The conversion of a proper slice is a\nslice object (see section *The standard type hierarchy*) whose\n"start", "stop" and "step" attributes are the values of the\nexpressions given as lower bound, upper bound and stride,\nrespectively, substituting "None" for missing expressions.\n',
- 'specialattrs': u'\nSpecial Attributes\n******************\n\nThe implementation adds a few special read-only attributes to several\nobject types, where they are relevant. Some of these are not reported\nby the "dir()" built-in function.\n\nobject.__dict__\n\n A dictionary or other mapping object used to store an object\'s\n (writable) attributes.\n\ninstance.__class__\n\n The class to which a class instance belongs.\n\nclass.__bases__\n\n The tuple of base classes of a class object.\n\ndefinition.__name__\n\n The name of the class, function, method, descriptor, or generator\n instance.\n\ndefinition.__qualname__\n\n The *qualified name* of the class, function, method, descriptor, or\n generator instance.\n\n New in version 3.3.\n\nclass.__mro__\n\n This attribute is a tuple of classes that are considered when\n looking for base classes during method resolution.\n\nclass.mro()\n\n This method can be overridden by a metaclass to customize the\n method resolution order for its instances. It is called at class\n instantiation, and its result is stored in "__mro__".\n\nclass.__subclasses__()\n\n Each class keeps a list of weak references to its immediate\n subclasses. This method returns a list of all those references\n still alive. Example:\n\n >>> int.__subclasses__()\n [<class \'bool\'>]\n\n-[ Footnotes ]-\n\n[1] Additional information on these special methods may be found\n in the Python Reference Manual (*Basic customization*).\n\n[2] As a consequence, the list "[1, 2]" is considered equal to\n "[1.0, 2.0]", and similarly for tuples.\n\n[3] They must have since the parser can\'t tell the type of the\n operands.\n\n[4] Cased characters are those with general category property\n being one of "Lu" (Letter, uppercase), "Ll" (Letter, lowercase),\n or "Lt" (Letter, titlecase).\n\n[5] To format only a tuple you should therefore provide a\n singleton tuple whose only element is the tuple to be formatted.\n',
- 'specialnames': u'\nSpecial method names\n********************\n\nA class can implement certain operations that are invoked by special\nsyntax (such as arithmetic operations or subscripting and slicing) by\ndefining methods with special names. This is Python\'s approach to\n*operator overloading*, allowing classes to define their own behavior\nwith respect to language operators. For instance, if a class defines\na method named "__getitem__()", and "x" is an instance of this class,\nthen "x[i]" is roughly equivalent to "type(x).__getitem__(x, i)".\nExcept where mentioned, attempts to execute an operation raise an\nexception when no appropriate method is defined (typically\n"AttributeError" or "TypeError").\n\nWhen implementing a class that emulates any built-in type, it is\nimportant that the emulation only be implemented to the degree that it\nmakes sense for the object being modelled. For example, some\nsequences may work well with retrieval of individual elements, but\nextracting a slice may not make sense. (One example of this is the\n"NodeList" interface in the W3C\'s Document Object Model.)\n\n\nBasic customization\n===================\n\nobject.__new__(cls[, ...])\n\n Called to create a new instance of class *cls*. "__new__()" is a\n static method (special-cased so you need not declare it as such)\n that takes the class of which an instance was requested as its\n first argument. The remaining arguments are those passed to the\n object constructor expression (the call to the class). The return\n value of "__new__()" should be the new object instance (usually an\n instance of *cls*).\n\n Typical implementations create a new instance of the class by\n invoking the superclass\'s "__new__()" method using\n "super(currentclass, cls).__new__(cls[, ...])" with appropriate\n arguments and then modifying the newly-created instance as\n necessary before returning it.\n\n If "__new__()" returns an instance of *cls*, then the new\n instance\'s "__init__()" method will be invoked like\n "__init__(self[, ...])", where *self* is the new instance and the\n remaining arguments are the same as were passed to "__new__()".\n\n If "__new__()" does not return an instance of *cls*, then the new\n instance\'s "__init__()" method will not be invoked.\n\n "__new__()" is intended mainly to allow subclasses of immutable\n types (like int, str, or tuple) to customize instance creation. It\n is also commonly overridden in custom metaclasses in order to\n customize class creation.\n\nobject.__init__(self[, ...])\n\n Called after the instance has been created (by "__new__()"), but\n before it is returned to the caller. The arguments are those\n passed to the class constructor expression. If a base class has an\n "__init__()" method, the derived class\'s "__init__()" method, if\n any, must explicitly call it to ensure proper initialization of the\n base class part of the instance; for example:\n "BaseClass.__init__(self, [args...])".\n\n Because "__new__()" and "__init__()" work together in constructing\n objects ("__new__()" to create it, and "__init__()" to customize\n it), no non-"None" value may be returned by "__init__()"; doing so\n will cause a "TypeError" to be raised at runtime.\n\nobject.__del__(self)\n\n Called when the instance is about to be destroyed. This is also\n called a destructor. If a base class has a "__del__()" method, the\n derived class\'s "__del__()" method, if any, must explicitly call it\n to ensure proper deletion of the base class part of the instance.\n Note that it is possible (though not recommended!) for the\n "__del__()" method to postpone destruction of the instance by\n creating a new reference to it. It may then be called at a later\n time when this new reference is deleted. It is not guaranteed that\n "__del__()" methods are called for objects that still exist when\n the interpreter exits.\n\n Note: "del x" doesn\'t directly call "x.__del__()" --- the former\n decrements the reference count for "x" by one, and the latter is\n only called when "x"\'s reference count reaches zero. Some common\n situations that may prevent the reference count of an object from\n going to zero include: circular references between objects (e.g.,\n a doubly-linked list or a tree data structure with parent and\n child pointers); a reference to the object on the stack frame of\n a function that caught an exception (the traceback stored in\n "sys.exc_info()[2]" keeps the stack frame alive); or a reference\n to the object on the stack frame that raised an unhandled\n exception in interactive mode (the traceback stored in\n "sys.last_traceback" keeps the stack frame alive). The first\n situation can only be remedied by explicitly breaking the cycles;\n the second can be resolved by freeing the reference to the\n traceback object when it is no longer useful, and the third can\n be resolved by storing "None" in "sys.last_traceback". Circular\n references which are garbage are detected and cleaned up when the\n cyclic garbage collector is enabled (it\'s on by default). Refer\n to the documentation for the "gc" module for more information\n about this topic.\n\n Warning: Due to the precarious circumstances under which\n "__del__()" methods are invoked, exceptions that occur during\n their execution are ignored, and a warning is printed to\n "sys.stderr" instead. Also, when "__del__()" is invoked in\n response to a module being deleted (e.g., when execution of the\n program is done), other globals referenced by the "__del__()"\n method may already have been deleted or in the process of being\n torn down (e.g. the import machinery shutting down). For this\n reason, "__del__()" methods should do the absolute minimum needed\n to maintain external invariants. Starting with version 1.5,\n Python guarantees that globals whose name begins with a single\n underscore are deleted from their module before other globals are\n deleted; if no other references to such globals exist, this may\n help in assuring that imported modules are still available at the\n time when the "__del__()" method is called.\n\nobject.__repr__(self)\n\n Called by the "repr()" built-in function to compute the "official"\n string representation of an object. If at all possible, this\n should look like a valid Python expression that could be used to\n recreate an object with the same value (given an appropriate\n environment). If this is not possible, a string of the form\n "<...some useful description...>" should be returned. The return\n value must be a string object. If a class defines "__repr__()" but\n not "__str__()", then "__repr__()" is also used when an "informal"\n string representation of instances of that class is required.\n\n This is typically used for debugging, so it is important that the\n representation is information-rich and unambiguous.\n\nobject.__str__(self)\n\n Called by "str(object)" and the built-in functions "format()" and\n "print()" to compute the "informal" or nicely printable string\n representation of an object. The return value must be a *string*\n object.\n\n This method differs from "object.__repr__()" in that there is no\n expectation that "__str__()" return a valid Python expression: a\n more convenient or concise representation can be used.\n\n The default implementation defined by the built-in type "object"\n calls "object.__repr__()".\n\nobject.__bytes__(self)\n\n Called by "bytes()" to compute a byte-string representation of an\n object. This should return a "bytes" object.\n\nobject.__format__(self, format_spec)\n\n Called by the "format()" built-in function (and by extension, the\n "str.format()" method of class "str") to produce a "formatted"\n string representation of an object. The "format_spec" argument is a\n string that contains a description of the formatting options\n desired. The interpretation of the "format_spec" argument is up to\n the type implementing "__format__()", however most classes will\n either delegate formatting to one of the built-in types, or use a\n similar formatting option syntax.\n\n See *Format Specification Mini-Language* for a description of the\n standard formatting syntax.\n\n The return value must be a string object.\n\n Changed in version 3.4: The __format__ method of "object" itself\n raises a "TypeError" if passed any non-empty string.\n\nobject.__lt__(self, other)\nobject.__le__(self, other)\nobject.__eq__(self, other)\nobject.__ne__(self, other)\nobject.__gt__(self, other)\nobject.__ge__(self, other)\n\n These are the so-called "rich comparison" methods. The\n correspondence between operator symbols and method names is as\n follows: "x<y" calls "x.__lt__(y)", "x<=y" calls "x.__le__(y)",\n "x==y" calls "x.__eq__(y)", "x!=y" calls "x.__ne__(y)", "x>y" calls\n "x.__gt__(y)", and "x>=y" calls "x.__ge__(y)".\n\n A rich comparison method may return the singleton "NotImplemented"\n if it does not implement the operation for a given pair of\n arguments. By convention, "False" and "True" are returned for a\n successful comparison. However, these methods can return any value,\n so if the comparison operator is used in a Boolean context (e.g.,\n in the condition of an "if" statement), Python will call "bool()"\n on the value to determine if the result is true or false.\n\n By default, "__ne__()" delegates to "__eq__()" and inverts the\n result unless it is "NotImplemented". There are no other implied\n relationships among the comparison operators, for example, the\n truth of "(x<y or x==y)" does not imply "x<=y". To automatically\n generate ordering operations from a single root operation, see\n "functools.total_ordering()".\n\n See the paragraph on "__hash__()" for some important notes on\n creating *hashable* objects which support custom comparison\n operations and are usable as dictionary keys.\n\n There are no swapped-argument versions of these methods (to be used\n when the left argument does not support the operation but the right\n argument does); rather, "__lt__()" and "__gt__()" are each other\'s\n reflection, "__le__()" and "__ge__()" are each other\'s reflection,\n and "__eq__()" and "__ne__()" are their own reflection. If the\n operands are of different types, and right operand\'s type is a\n direct or indirect subclass of the left operand\'s type, the\n reflected method of the right operand has priority, otherwise the\n left operand\'s method has priority. Virtual subclassing is not\n considered.\n\nobject.__hash__(self)\n\n Called by built-in function "hash()" and for operations on members\n of hashed collections including "set", "frozenset", and "dict".\n "__hash__()" should return an integer. The only required property\n is that objects which compare equal have the same hash value; it is\n advised to mix together the hash values of the components of the\n object that also play a part in comparison of objects by packing\n them into a tuple and hashing the tuple. Example:\n\n def __hash__(self):\n return hash((self.name, self.nick, self.color))\n\n Note: "hash()" truncates the value returned from an object\'s\n custom "__hash__()" method to the size of a "Py_ssize_t". This\n is typically 8 bytes on 64-bit builds and 4 bytes on 32-bit\n builds. If an object\'s "__hash__()" must interoperate on builds\n of different bit sizes, be sure to check the width on all\n supported builds. An easy way to do this is with "python -c\n "import sys; print(sys.hash_info.width)"".\n\n If a class does not define an "__eq__()" method it should not\n define a "__hash__()" operation either; if it defines "__eq__()"\n but not "__hash__()", its instances will not be usable as items in\n hashable collections. If a class defines mutable objects and\n implements an "__eq__()" method, it should not implement\n "__hash__()", since the implementation of hashable collections\n requires that a key\'s hash value is immutable (if the object\'s hash\n value changes, it will be in the wrong hash bucket).\n\n User-defined classes have "__eq__()" and "__hash__()" methods by\n default; with them, all objects compare unequal (except with\n themselves) and "x.__hash__()" returns an appropriate value such\n that "x == y" implies both that "x is y" and "hash(x) == hash(y)".\n\n A class that overrides "__eq__()" and does not define "__hash__()"\n will have its "__hash__()" implicitly set to "None". When the\n "__hash__()" method of a class is "None", instances of the class\n will raise an appropriate "TypeError" when a program attempts to\n retrieve their hash value, and will also be correctly identified as\n unhashable when checking "isinstance(obj, collections.Hashable)".\n\n If a class that overrides "__eq__()" needs to retain the\n implementation of "__hash__()" from a parent class, the interpreter\n must be told this explicitly by setting "__hash__ =\n <ParentClass>.__hash__".\n\n If a class that does not override "__eq__()" wishes to suppress\n hash support, it should include "__hash__ = None" in the class\n definition. A class which defines its own "__hash__()" that\n explicitly raises a "TypeError" would be incorrectly identified as\n hashable by an "isinstance(obj, collections.Hashable)" call.\n\n Note: By default, the "__hash__()" values of str, bytes and\n datetime objects are "salted" with an unpredictable random value.\n Although they remain constant within an individual Python\n process, they are not predictable between repeated invocations of\n Python.This is intended to provide protection against a denial-\n of-service caused by carefully-chosen inputs that exploit the\n worst case performance of a dict insertion, O(n^2) complexity.\n See http://www.ocert.org/advisories/ocert-2011-003.html for\n details.Changing hash values affects the iteration order of\n dicts, sets and other mappings. Python has never made guarantees\n about this ordering (and it typically varies between 32-bit and\n 64-bit builds).See also "PYTHONHASHSEED".\n\n Changed in version 3.3: Hash randomization is enabled by default.\n\nobject.__bool__(self)\n\n Called to implement truth value testing and the built-in operation\n "bool()"; should return "False" or "True". When this method is not\n defined, "__len__()" is called, if it is defined, and the object is\n considered true if its result is nonzero. If a class defines\n neither "__len__()" nor "__bool__()", all its instances are\n considered true.\n\n\nCustomizing attribute access\n============================\n\nThe following methods can be defined to customize the meaning of\nattribute access (use of, assignment to, or deletion of "x.name") for\nclass instances.\n\nobject.__getattr__(self, name)\n\n Called when an attribute lookup has not found the attribute in the\n usual places (i.e. it is not an instance attribute nor is it found\n in the class tree for "self"). "name" is the attribute name. This\n method should return the (computed) attribute value or raise an\n "AttributeError" exception.\n\n Note that if the attribute is found through the normal mechanism,\n "__getattr__()" is not called. (This is an intentional asymmetry\n between "__getattr__()" and "__setattr__()".) This is done both for\n efficiency reasons and because otherwise "__getattr__()" would have\n no way to access other attributes of the instance. Note that at\n least for instance variables, you can fake total control by not\n inserting any values in the instance attribute dictionary (but\n instead inserting them in another object). See the\n "__getattribute__()" method below for a way to actually get total\n control over attribute access.\n\nobject.__getattribute__(self, name)\n\n Called unconditionally to implement attribute accesses for\n instances of the class. If the class also defines "__getattr__()",\n the latter will not be called unless "__getattribute__()" either\n calls it explicitly or raises an "AttributeError". This method\n should return the (computed) attribute value or raise an\n "AttributeError" exception. In order to avoid infinite recursion in\n this method, its implementation should always call the base class\n method with the same name to access any attributes it needs, for\n example, "object.__getattribute__(self, name)".\n\n Note: This method may still be bypassed when looking up special\n methods as the result of implicit invocation via language syntax\n or built-in functions. See *Special method lookup*.\n\nobject.__setattr__(self, name, value)\n\n Called when an attribute assignment is attempted. This is called\n instead of the normal mechanism (i.e. store the value in the\n instance dictionary). *name* is the attribute name, *value* is the\n value to be assigned to it.\n\n If "__setattr__()" wants to assign to an instance attribute, it\n should call the base class method with the same name, for example,\n "object.__setattr__(self, name, value)".\n\nobject.__delattr__(self, name)\n\n Like "__setattr__()" but for attribute deletion instead of\n assignment. This should only be implemented if "del obj.name" is\n meaningful for the object.\n\nobject.__dir__(self)\n\n Called when "dir()" is called on the object. A sequence must be\n returned. "dir()" converts the returned sequence to a list and\n sorts it.\n\n\nImplementing Descriptors\n------------------------\n\nThe following methods only apply when an instance of the class\ncontaining the method (a so-called *descriptor* class) appears in an\n*owner* class (the descriptor must be in either the owner\'s class\ndictionary or in the class dictionary for one of its parents). In the\nexamples below, "the attribute" refers to the attribute whose name is\nthe key of the property in the owner class\' "__dict__".\n\nobject.__get__(self, instance, owner)\n\n Called to get the attribute of the owner class (class attribute\n access) or of an instance of that class (instance attribute\n access). *owner* is always the owner class, while *instance* is the\n instance that the attribute was accessed through, or "None" when\n the attribute is accessed through the *owner*. This method should\n return the (computed) attribute value or raise an "AttributeError"\n exception.\n\nobject.__set__(self, instance, value)\n\n Called to set the attribute on an instance *instance* of the owner\n class to a new value, *value*.\n\nobject.__delete__(self, instance)\n\n Called to delete the attribute on an instance *instance* of the\n owner class.\n\nThe attribute "__objclass__" is interpreted by the "inspect" module as\nspecifying the class where this object was defined (setting this\nappropriately can assist in runtime introspection of dynamic class\nattributes). For callables, it may indicate that an instance of the\ngiven type (or a subclass) is expected or required as the first\npositional argument (for example, CPython sets this attribute for\nunbound methods that are implemented in C).\n\n\nInvoking Descriptors\n--------------------\n\nIn general, a descriptor is an object attribute with "binding\nbehavior", one whose attribute access has been overridden by methods\nin the descriptor protocol: "__get__()", "__set__()", and\n"__delete__()". If any of those methods are defined for an object, it\nis said to be a descriptor.\n\nThe default behavior for attribute access is to get, set, or delete\nthe attribute from an object\'s dictionary. For instance, "a.x" has a\nlookup chain starting with "a.__dict__[\'x\']", then\n"type(a).__dict__[\'x\']", and continuing through the base classes of\n"type(a)" excluding metaclasses.\n\nHowever, if the looked-up value is an object defining one of the\ndescriptor methods, then Python may override the default behavior and\ninvoke the descriptor method instead. Where this occurs in the\nprecedence chain depends on which descriptor methods were defined and\nhow they were called.\n\nThe starting point for descriptor invocation is a binding, "a.x". How\nthe arguments are assembled depends on "a":\n\nDirect Call\n The simplest and least common call is when user code directly\n invokes a descriptor method: "x.__get__(a)".\n\nInstance Binding\n If binding to an object instance, "a.x" is transformed into the\n call: "type(a).__dict__[\'x\'].__get__(a, type(a))".\n\nClass Binding\n If binding to a class, "A.x" is transformed into the call:\n "A.__dict__[\'x\'].__get__(None, A)".\n\nSuper Binding\n If "a" is an instance of "super", then the binding "super(B,\n obj).m()" searches "obj.__class__.__mro__" for the base class "A"\n immediately preceding "B" and then invokes the descriptor with the\n call: "A.__dict__[\'m\'].__get__(obj, obj.__class__)".\n\nFor instance bindings, the precedence of descriptor invocation depends\non the which descriptor methods are defined. A descriptor can define\nany combination of "__get__()", "__set__()" and "__delete__()". If it\ndoes not define "__get__()", then accessing the attribute will return\nthe descriptor object itself unless there is a value in the object\'s\ninstance dictionary. If the descriptor defines "__set__()" and/or\n"__delete__()", it is a data descriptor; if it defines neither, it is\na non-data descriptor. Normally, data descriptors define both\n"__get__()" and "__set__()", while non-data descriptors have just the\n"__get__()" method. Data descriptors with "__set__()" and "__get__()"\ndefined always override a redefinition in an instance dictionary. In\ncontrast, non-data descriptors can be overridden by instances.\n\nPython methods (including "staticmethod()" and "classmethod()") are\nimplemented as non-data descriptors. Accordingly, instances can\nredefine and override methods. This allows individual instances to\nacquire behaviors that differ from other instances of the same class.\n\nThe "property()" function is implemented as a data descriptor.\nAccordingly, instances cannot override the behavior of a property.\n\n\n__slots__\n---------\n\nBy default, instances of classes have a dictionary for attribute\nstorage. This wastes space for objects having very few instance\nvariables. The space consumption can become acute when creating large\nnumbers of instances.\n\nThe default can be overridden by defining *__slots__* in a class\ndefinition. The *__slots__* declaration takes a sequence of instance\nvariables and reserves just enough space in each instance to hold a\nvalue for each variable. Space is saved because *__dict__* is not\ncreated for each instance.\n\nobject.__slots__\n\n This class variable can be assigned a string, iterable, or sequence\n of strings with variable names used by instances. *__slots__*\n reserves space for the declared variables and prevents the\n automatic creation of *__dict__* and *__weakref__* for each\n instance.\n\n\nNotes on using *__slots__*\n~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n* When inheriting from a class without *__slots__*, the *__dict__*\n attribute of that class will always be accessible, so a *__slots__*\n definition in the subclass is meaningless.\n\n* Without a *__dict__* variable, instances cannot be assigned new\n variables not listed in the *__slots__* definition. Attempts to\n assign to an unlisted variable name raises "AttributeError". If\n dynamic assignment of new variables is desired, then add\n "\'__dict__\'" to the sequence of strings in the *__slots__*\n declaration.\n\n* Without a *__weakref__* variable for each instance, classes\n defining *__slots__* do not support weak references to its\n instances. If weak reference support is needed, then add\n "\'__weakref__\'" to the sequence of strings in the *__slots__*\n declaration.\n\n* *__slots__* are implemented at the class level by creating\n descriptors (*Implementing Descriptors*) for each variable name. As\n a result, class attributes cannot be used to set default values for\n instance variables defined by *__slots__*; otherwise, the class\n attribute would overwrite the descriptor assignment.\n\n* The action of a *__slots__* declaration is limited to the class\n where it is defined. As a result, subclasses will have a *__dict__*\n unless they also define *__slots__* (which must only contain names\n of any *additional* slots).\n\n* If a class defines a slot also defined in a base class, the\n instance variable defined by the base class slot is inaccessible\n (except by retrieving its descriptor directly from the base class).\n This renders the meaning of the program undefined. In the future, a\n check may be added to prevent this.\n\n* Nonempty *__slots__* does not work for classes derived from\n "variable-length" built-in types such as "int", "bytes" and "tuple".\n\n* Any non-string iterable may be assigned to *__slots__*. Mappings\n may also be used; however, in the future, special meaning may be\n assigned to the values corresponding to each key.\n\n* *__class__* assignment works only if both classes have the same\n *__slots__*.\n\n\nCustomizing class creation\n==========================\n\nBy default, classes are constructed using "type()". The class body is\nexecuted in a new namespace and the class name is bound locally to the\nresult of "type(name, bases, namespace)".\n\nThe class creation process can be customized by passing the\n"metaclass" keyword argument in the class definition line, or by\ninheriting from an existing class that included such an argument. In\nthe following example, both "MyClass" and "MySubclass" are instances\nof "Meta":\n\n class Meta(type):\n pass\n\n class MyClass(metaclass=Meta):\n pass\n\n class MySubclass(MyClass):\n pass\n\nAny other keyword arguments that are specified in the class definition\nare passed through to all metaclass operations described below.\n\nWhen a class definition is executed, the following steps occur:\n\n* the appropriate metaclass is determined\n\n* the class namespace is prepared\n\n* the class body is executed\n\n* the class object is created\n\n\nDetermining the appropriate metaclass\n-------------------------------------\n\nThe appropriate metaclass for a class definition is determined as\nfollows:\n\n* if no bases and no explicit metaclass are given, then "type()" is\n used\n\n* if an explicit metaclass is given and it is *not* an instance of\n "type()", then it is used directly as the metaclass\n\n* if an instance of "type()" is given as the explicit metaclass, or\n bases are defined, then the most derived metaclass is used\n\nThe most derived metaclass is selected from the explicitly specified\nmetaclass (if any) and the metaclasses (i.e. "type(cls)") of all\nspecified base classes. The most derived metaclass is one which is a\nsubtype of *all* of these candidate metaclasses. If none of the\ncandidate metaclasses meets that criterion, then the class definition\nwill fail with "TypeError".\n\n\nPreparing the class namespace\n-----------------------------\n\nOnce the appropriate metaclass has been identified, then the class\nnamespace is prepared. If the metaclass has a "__prepare__" attribute,\nit is called as "namespace = metaclass.__prepare__(name, bases,\n**kwds)" (where the additional keyword arguments, if any, come from\nthe class definition).\n\nIf the metaclass has no "__prepare__" attribute, then the class\nnamespace is initialised as an empty "dict()" instance.\n\nSee also: **PEP 3115** - Metaclasses in Python 3000\n\n Introduced the "__prepare__" namespace hook\n\n\nExecuting the class body\n------------------------\n\nThe class body is executed (approximately) as "exec(body, globals(),\nnamespace)". The key difference from a normal call to "exec()" is that\nlexical scoping allows the class body (including any methods) to\nreference names from the current and outer scopes when the class\ndefinition occurs inside a function.\n\nHowever, even when the class definition occurs inside the function,\nmethods defined inside the class still cannot see names defined at the\nclass scope. Class variables must be accessed through the first\nparameter of instance or class methods, and cannot be accessed at all\nfrom static methods.\n\n\nCreating the class object\n-------------------------\n\nOnce the class namespace has been populated by executing the class\nbody, the class object is created by calling "metaclass(name, bases,\nnamespace, **kwds)" (the additional keywords passed here are the same\nas those passed to "__prepare__").\n\nThis class object is the one that will be referenced by the zero-\nargument form of "super()". "__class__" is an implicit closure\nreference created by the compiler if any methods in a class body refer\nto either "__class__" or "super". This allows the zero argument form\nof "super()" to correctly identify the class being defined based on\nlexical scoping, while the class or instance that was used to make the\ncurrent call is identified based on the first argument passed to the\nmethod.\n\nAfter the class object is created, it is passed to the class\ndecorators included in the class definition (if any) and the resulting\nobject is bound in the local namespace as the defined class.\n\nWhen a new class is created by "type.__new__", the object provided as\nthe namespace parameter is copied to a standard Python dictionary and\nthe original object is discarded. The new copy becomes the "__dict__"\nattribute of the class object.\n\nSee also: **PEP 3135** - New super\n\n Describes the implicit "__class__" closure reference\n\n\nMetaclass example\n-----------------\n\nThe potential uses for metaclasses are boundless. Some ideas that have\nbeen explored include logging, interface checking, automatic\ndelegation, automatic property creation, proxies, frameworks, and\nautomatic resource locking/synchronization.\n\nHere is an example of a metaclass that uses an\n"collections.OrderedDict" to remember the order that class variables\nare defined:\n\n class OrderedClass(type):\n\n @classmethod\n def __prepare__(metacls, name, bases, **kwds):\n return collections.OrderedDict()\n\n def __new__(cls, name, bases, namespace, **kwds):\n result = type.__new__(cls, name, bases, dict(namespace))\n result.members = tuple(namespace)\n return result\n\n class A(metaclass=OrderedClass):\n def one(self): pass\n def two(self): pass\n def three(self): pass\n def four(self): pass\n\n >>> A.members\n (\'__module__\', \'one\', \'two\', \'three\', \'four\')\n\nWhen the class definition for *A* gets executed, the process begins\nwith calling the metaclass\'s "__prepare__()" method which returns an\nempty "collections.OrderedDict". That mapping records the methods and\nattributes of *A* as they are defined within the body of the class\nstatement. Once those definitions are executed, the ordered dictionary\nis fully populated and the metaclass\'s "__new__()" method gets\ninvoked. That method builds the new type and it saves the ordered\ndictionary keys in an attribute called "members".\n\n\nCustomizing instance and subclass checks\n========================================\n\nThe following methods are used to override the default behavior of the\n"isinstance()" and "issubclass()" built-in functions.\n\nIn particular, the metaclass "abc.ABCMeta" implements these methods in\norder to allow the addition of Abstract Base Classes (ABCs) as\n"virtual base classes" to any class or type (including built-in\ntypes), including other ABCs.\n\nclass.__instancecheck__(self, instance)\n\n Return true if *instance* should be considered a (direct or\n indirect) instance of *class*. If defined, called to implement\n "isinstance(instance, class)".\n\nclass.__subclasscheck__(self, subclass)\n\n Return true if *subclass* should be considered a (direct or\n indirect) subclass of *class*. If defined, called to implement\n "issubclass(subclass, class)".\n\nNote that these methods are looked up on the type (metaclass) of a\nclass. They cannot be defined as class methods in the actual class.\nThis is consistent with the lookup of special methods that are called\non instances, only in this case the instance is itself a class.\n\nSee also: **PEP 3119** - Introducing Abstract Base Classes\n\n Includes the specification for customizing "isinstance()" and\n "issubclass()" behavior through "__instancecheck__()" and\n "__subclasscheck__()", with motivation for this functionality in\n the context of adding Abstract Base Classes (see the "abc"\n module) to the language.\n\n\nEmulating callable objects\n==========================\n\nobject.__call__(self[, args...])\n\n Called when the instance is "called" as a function; if this method\n is defined, "x(arg1, arg2, ...)" is a shorthand for\n "x.__call__(arg1, arg2, ...)".\n\n\nEmulating container types\n=========================\n\nThe following methods can be defined to implement container objects.\nContainers usually are sequences (such as lists or tuples) or mappings\n(like dictionaries), but can represent other containers as well. The\nfirst set of methods is used either to emulate a sequence or to\nemulate a mapping; the difference is that for a sequence, the\nallowable keys should be the integers *k* for which "0 <= k < N" where\n*N* is the length of the sequence, or slice objects, which define a\nrange of items. It is also recommended that mappings provide the\nmethods "keys()", "values()", "items()", "get()", "clear()",\n"setdefault()", "pop()", "popitem()", "copy()", and "update()"\nbehaving similar to those for Python\'s standard dictionary objects.\nThe "collections" module provides a "MutableMapping" abstract base\nclass to help create those methods from a base set of "__getitem__()",\n"__setitem__()", "__delitem__()", and "keys()". Mutable sequences\nshould provide methods "append()", "count()", "index()", "extend()",\n"insert()", "pop()", "remove()", "reverse()" and "sort()", like Python\nstandard list objects. Finally, sequence types should implement\naddition (meaning concatenation) and multiplication (meaning\nrepetition) by defining the methods "__add__()", "__radd__()",\n"__iadd__()", "__mul__()", "__rmul__()" and "__imul__()" described\nbelow; they should not define other numerical operators. It is\nrecommended that both mappings and sequences implement the\n"__contains__()" method to allow efficient use of the "in" operator;\nfor mappings, "in" should search the mapping\'s keys; for sequences, it\nshould search through the values. It is further recommended that both\nmappings and sequences implement the "__iter__()" method to allow\nefficient iteration through the container; for mappings, "__iter__()"\nshould be the same as "keys()"; for sequences, it should iterate\nthrough the values.\n\nobject.__len__(self)\n\n Called to implement the built-in function "len()". Should return\n the length of the object, an integer ">=" 0. Also, an object that\n doesn\'t define a "__bool__()" method and whose "__len__()" method\n returns zero is considered to be false in a Boolean context.\n\nobject.__length_hint__(self)\n\n Called to implement "operator.length_hint()". Should return an\n estimated length for the object (which may be greater or less than\n the actual length). The length must be an integer ">=" 0. This\n method is purely an optimization and is never required for\n correctness.\n\n New in version 3.4.\n\nNote: Slicing is done exclusively with the following three methods.\n A call like\n\n a[1:2] = b\n\n is translated to\n\n a[slice(1, 2, None)] = b\n\n and so forth. Missing slice items are always filled in with "None".\n\nobject.__getitem__(self, key)\n\n Called to implement evaluation of "self[key]". For sequence types,\n the accepted keys should be integers and slice objects. Note that\n the special interpretation of negative indexes (if the class wishes\n to emulate a sequence type) is up to the "__getitem__()" method. If\n *key* is of an inappropriate type, "TypeError" may be raised; if of\n a value outside the set of indexes for the sequence (after any\n special interpretation of negative values), "IndexError" should be\n raised. For mapping types, if *key* is missing (not in the\n container), "KeyError" should be raised.\n\n Note: "for" loops expect that an "IndexError" will be raised for\n illegal indexes to allow proper detection of the end of the\n sequence.\n\nobject.__missing__(self, key)\n\n Called by "dict"."__getitem__()" to implement "self[key]" for dict\n subclasses when key is not in the dictionary.\n\nobject.__setitem__(self, key, value)\n\n Called to implement assignment to "self[key]". Same note as for\n "__getitem__()". This should only be implemented for mappings if\n the objects support changes to the values for keys, or if new keys\n can be added, or for sequences if elements can be replaced. The\n same exceptions should be raised for improper *key* values as for\n the "__getitem__()" method.\n\nobject.__delitem__(self, key)\n\n Called to implement deletion of "self[key]". Same note as for\n "__getitem__()". This should only be implemented for mappings if\n the objects support removal of keys, or for sequences if elements\n can be removed from the sequence. The same exceptions should be\n raised for improper *key* values as for the "__getitem__()" method.\n\nobject.__iter__(self)\n\n This method is called when an iterator is required for a container.\n This method should return a new iterator object that can iterate\n over all the objects in the container. For mappings, it should\n iterate over the keys of the container.\n\n Iterator objects also need to implement this method; they are\n required to return themselves. For more information on iterator\n objects, see *Iterator Types*.\n\nobject.__reversed__(self)\n\n Called (if present) by the "reversed()" built-in to implement\n reverse iteration. It should return a new iterator object that\n iterates over all the objects in the container in reverse order.\n\n If the "__reversed__()" method is not provided, the "reversed()"\n built-in will fall back to using the sequence protocol ("__len__()"\n and "__getitem__()"). Objects that support the sequence protocol\n should only provide "__reversed__()" if they can provide an\n implementation that is more efficient than the one provided by\n "reversed()".\n\nThe membership test operators ("in" and "not in") are normally\nimplemented as an iteration through a sequence. However, container\nobjects can supply the following special method with a more efficient\nimplementation, which also does not require the object be a sequence.\n\nobject.__contains__(self, item)\n\n Called to implement membership test operators. Should return true\n if *item* is in *self*, false otherwise. For mapping objects, this\n should consider the keys of the mapping rather than the values or\n the key-item pairs.\n\n For objects that don\'t define "__contains__()", the membership test\n first tries iteration via "__iter__()", then the old sequence\n iteration protocol via "__getitem__()", see *this section in the\n language reference*.\n\n\nEmulating numeric types\n=======================\n\nThe following methods can be defined to emulate numeric objects.\nMethods corresponding to operations that are not supported by the\nparticular kind of number implemented (e.g., bitwise operations for\nnon-integral numbers) should be left undefined.\n\nobject.__add__(self, other)\nobject.__sub__(self, other)\nobject.__mul__(self, other)\nobject.__matmul__(self, other)\nobject.__truediv__(self, other)\nobject.__floordiv__(self, other)\nobject.__mod__(self, other)\nobject.__divmod__(self, other)\nobject.__pow__(self, other[, modulo])\nobject.__lshift__(self, other)\nobject.__rshift__(self, other)\nobject.__and__(self, other)\nobject.__xor__(self, other)\nobject.__or__(self, other)\n\n These methods are called to implement the binary arithmetic\n operations ("+", "-", "*", "@", "/", "//", "%", "divmod()",\n "pow()", "**", "<<", ">>", "&", "^", "|"). For instance, to\n evaluate the expression "x + y", where *x* is an instance of a\n class that has an "__add__()" method, "x.__add__(y)" is called.\n The "__divmod__()" method should be the equivalent to using\n "__floordiv__()" and "__mod__()"; it should not be related to\n "__truediv__()". Note that "__pow__()" should be defined to accept\n an optional third argument if the ternary version of the built-in\n "pow()" function is to be supported.\n\n If one of those methods does not support the operation with the\n supplied arguments, it should return "NotImplemented".\n\nobject.__radd__(self, other)\nobject.__rsub__(self, other)\nobject.__rmul__(self, other)\nobject.__rmatmul__(self, other)\nobject.__rtruediv__(self, other)\nobject.__rfloordiv__(self, other)\nobject.__rmod__(self, other)\nobject.__rdivmod__(self, other)\nobject.__rpow__(self, other)\nobject.__rlshift__(self, other)\nobject.__rrshift__(self, other)\nobject.__rand__(self, other)\nobject.__rxor__(self, other)\nobject.__ror__(self, other)\n\n These methods are called to implement the binary arithmetic\n operations ("+", "-", "*", "@", "/", "//", "%", "divmod()",\n "pow()", "**", "<<", ">>", "&", "^", "|") with reflected (swapped)\n operands. These functions are only called if the left operand does\n not support the corresponding operation and the operands are of\n different types. [2] For instance, to evaluate the expression "x -\n y", where *y* is an instance of a class that has an "__rsub__()"\n method, "y.__rsub__(x)" is called if "x.__sub__(y)" returns\n *NotImplemented*.\n\n Note that ternary "pow()" will not try calling "__rpow__()" (the\n coercion rules would become too complicated).\n\n Note: If the right operand\'s type is a subclass of the left\n operand\'s type and that subclass provides the reflected method\n for the operation, this method will be called before the left\n operand\'s non-reflected method. This behavior allows subclasses\n to override their ancestors\' operations.\n\nobject.__iadd__(self, other)\nobject.__isub__(self, other)\nobject.__imul__(self, other)\nobject.__imatmul__(self, other)\nobject.__itruediv__(self, other)\nobject.__ifloordiv__(self, other)\nobject.__imod__(self, other)\nobject.__ipow__(self, other[, modulo])\nobject.__ilshift__(self, other)\nobject.__irshift__(self, other)\nobject.__iand__(self, other)\nobject.__ixor__(self, other)\nobject.__ior__(self, other)\n\n These methods are called to implement the augmented arithmetic\n assignments ("+=", "-=", "*=", "@=", "/=", "//=", "%=", "**=",\n "<<=", ">>=", "&=", "^=", "|="). These methods should attempt to\n do the operation in-place (modifying *self*) and return the result\n (which could be, but does not have to be, *self*). If a specific\n method is not defined, the augmented assignment falls back to the\n normal methods. For instance, if *x* is an instance of a class\n with an "__iadd__()" method, "x += y" is equivalent to "x =\n x.__iadd__(y)" . Otherwise, "x.__add__(y)" and "y.__radd__(x)" are\n considered, as with the evaluation of "x + y". In certain\n situations, augmented assignment can result in unexpected errors\n (see *Why does a_tuple[i] += [\'item\'] raise an exception when the\n addition works?*), but this behavior is in fact part of the data\n model.\n\nobject.__neg__(self)\nobject.__pos__(self)\nobject.__abs__(self)\nobject.__invert__(self)\n\n Called to implement the unary arithmetic operations ("-", "+",\n "abs()" and "~").\n\nobject.__complex__(self)\nobject.__int__(self)\nobject.__float__(self)\nobject.__round__(self[, n])\n\n Called to implement the built-in functions "complex()", "int()",\n "float()" and "round()". Should return a value of the appropriate\n type.\n\nobject.__index__(self)\n\n Called to implement "operator.index()", and whenever Python needs\n to losslessly convert the numeric object to an integer object (such\n as in slicing, or in the built-in "bin()", "hex()" and "oct()"\n functions). Presence of this method indicates that the numeric\n object is an integer type. Must return an integer.\n\n Note: In order to have a coherent integer type class, when\n "__index__()" is defined "__int__()" should also be defined, and\n both should return the same value.\n\n\nWith Statement Context Managers\n===============================\n\nA *context manager* is an object that defines the runtime context to\nbe established when executing a "with" statement. The context manager\nhandles the entry into, and the exit from, the desired runtime context\nfor the execution of the block of code. Context managers are normally\ninvoked using the "with" statement (described in section *The with\nstatement*), but can also be used by directly invoking their methods.\n\nTypical uses of context managers include saving and restoring various\nkinds of global state, locking and unlocking resources, closing opened\nfiles, etc.\n\nFor more information on context managers, see *Context Manager Types*.\n\nobject.__enter__(self)\n\n Enter the runtime context related to this object. The "with"\n statement will bind this method\'s return value to the target(s)\n specified in the "as" clause of the statement, if any.\n\nobject.__exit__(self, exc_type, exc_value, traceback)\n\n Exit the runtime context related to this object. The parameters\n describe the exception that caused the context to be exited. If the\n context was exited without an exception, all three arguments will\n be "None".\n\n If an exception is supplied, and the method wishes to suppress the\n exception (i.e., prevent it from being propagated), it should\n return a true value. Otherwise, the exception will be processed\n normally upon exit from this method.\n\n Note that "__exit__()" methods should not reraise the passed-in\n exception; this is the caller\'s responsibility.\n\nSee also: **PEP 343** - The "with" statement\n\n The specification, background, and examples for the Python "with"\n statement.\n\n\nSpecial method lookup\n=====================\n\nFor custom classes, implicit invocations of special methods are only\nguaranteed to work correctly if defined on an object\'s type, not in\nthe object\'s instance dictionary. That behaviour is the reason why\nthe following code raises an exception:\n\n >>> class C:\n ... pass\n ...\n >>> c = C()\n >>> c.__len__ = lambda: 5\n >>> len(c)\n Traceback (most recent call last):\n File "<stdin>", line 1, in <module>\n TypeError: object of type \'C\' has no len()\n\nThe rationale behind this behaviour lies with a number of special\nmethods such as "__hash__()" and "__repr__()" that are implemented by\nall objects, including type objects. If the implicit lookup of these\nmethods used the conventional lookup process, they would fail when\ninvoked on the type object itself:\n\n >>> 1 .__hash__() == hash(1)\n True\n >>> int.__hash__() == hash(int)\n Traceback (most recent call last):\n File "<stdin>", line 1, in <module>\n TypeError: descriptor \'__hash__\' of \'int\' object needs an argument\n\nIncorrectly attempting to invoke an unbound method of a class in this\nway is sometimes referred to as \'metaclass confusion\', and is avoided\nby bypassing the instance when looking up special methods:\n\n >>> type(1).__hash__(1) == hash(1)\n True\n >>> type(int).__hash__(int) == hash(int)\n True\n\nIn addition to bypassing any instance attributes in the interest of\ncorrectness, implicit special method lookup generally also bypasses\nthe "__getattribute__()" method even of the object\'s metaclass:\n\n >>> class Meta(type):\n ... def __getattribute__(*args):\n ... print("Metaclass getattribute invoked")\n ... return type.__getattribute__(*args)\n ...\n >>> class C(object, metaclass=Meta):\n ... def __len__(self):\n ... return 10\n ... def __getattribute__(*args):\n ... print("Class getattribute invoked")\n ... return object.__getattribute__(*args)\n ...\n >>> c = C()\n >>> c.__len__() # Explicit lookup via instance\n Class getattribute invoked\n 10\n >>> type(c).__len__(c) # Explicit lookup via type\n Metaclass getattribute invoked\n 10\n >>> len(c) # Implicit lookup\n 10\n\nBypassing the "__getattribute__()" machinery in this fashion provides\nsignificant scope for speed optimisations within the interpreter, at\nthe cost of some flexibility in the handling of special methods (the\nspecial method *must* be set on the class object itself in order to be\nconsistently invoked by the interpreter).\n',
- 'string-methods': u'\nString Methods\n**************\n\nStrings implement all of the *common* sequence operations, along with\nthe additional methods described below.\n\nStrings also support two styles of string formatting, one providing a\nlarge degree of flexibility and customization (see "str.format()",\n*Format String Syntax* and *Custom String Formatting*) and the other\nbased on C "printf" style formatting that handles a narrower range of\ntypes and is slightly harder to use correctly, but is often faster for\nthe cases it can handle (*printf-style String Formatting*).\n\nThe *Text Processing Services* section of the standard library covers\na number of other modules that provide various text related utilities\n(including regular expression support in the "re" module).\n\nstr.capitalize()\n\n Return a copy of the string with its first character capitalized\n and the rest lowercased.\n\nstr.casefold()\n\n Return a casefolded copy of the string. Casefolded strings may be\n used for caseless matching.\n\n Casefolding is similar to lowercasing but more aggressive because\n it is intended to remove all case distinctions in a string. For\n example, the German lowercase letter "\'\xdf\'" is equivalent to ""ss"".\n Since it is already lowercase, "lower()" would do nothing to "\'\xdf\'";\n "casefold()" converts it to ""ss"".\n\n The casefolding algorithm is described in section 3.13 of the\n Unicode Standard.\n\n New in version 3.3.\n\nstr.center(width[, fillchar])\n\n Return centered in a string of length *width*. Padding is done\n using the specified *fillchar* (default is an ASCII space). The\n original string is returned if *width* is less than or equal to\n "len(s)".\n\nstr.count(sub[, start[, end]])\n\n Return the number of non-overlapping occurrences of substring *sub*\n in the range [*start*, *end*]. Optional arguments *start* and\n *end* are interpreted as in slice notation.\n\nstr.encode(encoding="utf-8", errors="strict")\n\n Return an encoded version of the string as a bytes object. Default\n encoding is "\'utf-8\'". *errors* may be given to set a different\n error handling scheme. The default for *errors* is "\'strict\'",\n meaning that encoding errors raise a "UnicodeError". Other possible\n values are "\'ignore\'", "\'replace\'", "\'xmlcharrefreplace\'",\n "\'backslashreplace\'" and any other name registered via\n "codecs.register_error()", see section *Error Handlers*. For a list\n of possible encodings, see section *Standard Encodings*.\n\n Changed in version 3.1: Support for keyword arguments added.\n\nstr.endswith(suffix[, start[, end]])\n\n Return "True" if the string ends with the specified *suffix*,\n otherwise return "False". *suffix* can also be a tuple of suffixes\n to look for. With optional *start*, test beginning at that\n position. With optional *end*, stop comparing at that position.\n\nstr.expandtabs(tabsize=8)\n\n Return a copy of the string where all tab characters are replaced\n by one or more spaces, depending on the current column and the\n given tab size. Tab positions occur every *tabsize* characters\n (default is 8, giving tab positions at columns 0, 8, 16 and so on).\n To expand the string, the current column is set to zero and the\n string is examined character by character. If the character is a\n tab ("\\t"), one or more space characters are inserted in the result\n until the current column is equal to the next tab position. (The\n tab character itself is not copied.) If the character is a newline\n ("\\n") or return ("\\r"), it is copied and the current column is\n reset to zero. Any other character is copied unchanged and the\n current column is incremented by one regardless of how the\n character is represented when printed.\n\n >>> \'01\\t012\\t0123\\t01234\'.expandtabs()\n \'01 012 0123 01234\'\n >>> \'01\\t012\\t0123\\t01234\'.expandtabs(4)\n \'01 012 0123 01234\'\n\nstr.find(sub[, start[, end]])\n\n Return the lowest index in the string where substring *sub* is\n found within the slice "s[start:end]". Optional arguments *start*\n and *end* are interpreted as in slice notation. Return "-1" if\n *sub* is not found.\n\n Note: The "find()" method should be used only if you need to know\n the position of *sub*. To check if *sub* is a substring or not,\n use the "in" operator:\n\n >>> \'Py\' in \'Python\'\n True\n\nstr.format(*args, **kwargs)\n\n Perform a string formatting operation. The string on which this\n method is called can contain literal text or replacement fields\n delimited by braces "{}". Each replacement field contains either\n the numeric index of a positional argument, or the name of a\n keyword argument. Returns a copy of the string where each\n replacement field is replaced with the string value of the\n corresponding argument.\n\n >>> "The sum of 1 + 2 is {0}".format(1+2)\n \'The sum of 1 + 2 is 3\'\n\n See *Format String Syntax* for a description of the various\n formatting options that can be specified in format strings.\n\nstr.format_map(mapping)\n\n Similar to "str.format(**mapping)", except that "mapping" is used\n directly and not copied to a "dict". This is useful if for example\n "mapping" is a dict subclass:\n\n >>> class Default(dict):\n ... def __missing__(self, key):\n ... return key\n ...\n >>> \'{name} was born in {country}\'.format_map(Default(name=\'Guido\'))\n \'Guido was born in country\'\n\n New in version 3.2.\n\nstr.index(sub[, start[, end]])\n\n Like "find()", but raise "ValueError" when the substring is not\n found.\n\nstr.isalnum()\n\n Return true if all characters in the string are alphanumeric and\n there is at least one character, false otherwise. A character "c"\n is alphanumeric if one of the following returns "True":\n "c.isalpha()", "c.isdecimal()", "c.isdigit()", or "c.isnumeric()".\n\nstr.isalpha()\n\n Return true if all characters in the string are alphabetic and\n there is at least one character, false otherwise. Alphabetic\n characters are those characters defined in the Unicode character\n database as "Letter", i.e., those with general category property\n being one of "Lm", "Lt", "Lu", "Ll", or "Lo". Note that this is\n different from the "Alphabetic" property defined in the Unicode\n Standard.\n\nstr.isdecimal()\n\n Return true if all characters in the string are decimal characters\n and there is at least one character, false otherwise. Decimal\n characters are those that can be used to form numbers in base 10,\n e.g. U+0660, ARABIC-INDIC DIGIT ZERO. Formally a decimal character\n is a character in the Unicode General Category "Nd".\n\nstr.isdigit()\n\n Return true if all characters in the string are digits and there is\n at least one character, false otherwise. Digits include decimal\n characters and digits that need special handling, such as the\n compatibility superscript digits. This covers digits which cannot\n be used to form numbers in base 10, like the Kharosthi numbers.\n Formally, a digit is a character that has the property value\n Numeric_Type=Digit or Numeric_Type=Decimal.\n\nstr.isidentifier()\n\n Return true if the string is a valid identifier according to the\n language definition, section *Identifiers and keywords*.\n\n Use "keyword.iskeyword()" to test for reserved identifiers such as\n "def" and "class".\n\nstr.islower()\n\n Return true if all cased characters [4] in the string are lowercase\n and there is at least one cased character, false otherwise.\n\nstr.isnumeric()\n\n Return true if all characters in the string are numeric characters,\n and there is at least one character, false otherwise. Numeric\n characters include digit characters, and all characters that have\n the Unicode numeric value property, e.g. U+2155, VULGAR FRACTION\n ONE FIFTH. Formally, numeric characters are those with the\n property value Numeric_Type=Digit, Numeric_Type=Decimal or\n Numeric_Type=Numeric.\n\nstr.isprintable()\n\n Return true if all characters in the string are printable or the\n string is empty, false otherwise. Nonprintable characters are\n those characters defined in the Unicode character database as\n "Other" or "Separator", excepting the ASCII space (0x20) which is\n considered printable. (Note that printable characters in this\n context are those which should not be escaped when "repr()" is\n invoked on a string. It has no bearing on the handling of strings\n written to "sys.stdout" or "sys.stderr".)\n\nstr.isspace()\n\n Return true if there are only whitespace characters in the string\n and there is at least one character, false otherwise. Whitespace\n characters are those characters defined in the Unicode character\n database as "Other" or "Separator" and those with bidirectional\n property being one of "WS", "B", or "S".\n\nstr.istitle()\n\n Return true if the string is a titlecased string and there is at\n least one character, for example uppercase characters may only\n follow uncased characters and lowercase characters only cased ones.\n Return false otherwise.\n\nstr.isupper()\n\n Return true if all cased characters [4] in the string are uppercase\n and there is at least one cased character, false otherwise.\n\nstr.join(iterable)\n\n Return a string which is the concatenation of the strings in the\n *iterable* *iterable*. A "TypeError" will be raised if there are\n any non-string values in *iterable*, including "bytes" objects.\n The separator between elements is the string providing this method.\n\nstr.ljust(width[, fillchar])\n\n Return the string left justified in a string of length *width*.\n Padding is done using the specified *fillchar* (default is an ASCII\n space). The original string is returned if *width* is less than or\n equal to "len(s)".\n\nstr.lower()\n\n Return a copy of the string with all the cased characters [4]\n converted to lowercase.\n\n The lowercasing algorithm used is described in section 3.13 of the\n Unicode Standard.\n\nstr.lstrip([chars])\n\n Return a copy of the string with leading characters removed. The\n *chars* argument is a string specifying the set of characters to be\n removed. If omitted or "None", the *chars* argument defaults to\n removing whitespace. The *chars* argument is not a prefix; rather,\n all combinations of its values are stripped:\n\n >>> \' spacious \'.lstrip()\n \'spacious \'\n >>> \'www.example.com\'.lstrip(\'cmowz.\')\n \'example.com\'\n\nstatic str.maketrans(x[, y[, z]])\n\n This static method returns a translation table usable for\n "str.translate()".\n\n If there is only one argument, it must be a dictionary mapping\n Unicode ordinals (integers) or characters (strings of length 1) to\n Unicode ordinals, strings (of arbitrary lengths) or "None".\n Character keys will then be converted to ordinals.\n\n If there are two arguments, they must be strings of equal length,\n and in the resulting dictionary, each character in x will be mapped\n to the character at the same position in y. If there is a third\n argument, it must be a string, whose characters will be mapped to\n "None" in the result.\n\nstr.partition(sep)\n\n Split the string at the first occurrence of *sep*, and return a\n 3-tuple containing the part before the separator, the separator\n itself, and the part after the separator. If the separator is not\n found, return a 3-tuple containing the string itself, followed by\n two empty strings.\n\nstr.replace(old, new[, count])\n\n Return a copy of the string with all occurrences of substring *old*\n replaced by *new*. If the optional argument *count* is given, only\n the first *count* occurrences are replaced.\n\nstr.rfind(sub[, start[, end]])\n\n Return the highest index in the string where substring *sub* is\n found, such that *sub* is contained within "s[start:end]".\n Optional arguments *start* and *end* are interpreted as in slice\n notation. Return "-1" on failure.\n\nstr.rindex(sub[, start[, end]])\n\n Like "rfind()" but raises "ValueError" when the substring *sub* is\n not found.\n\nstr.rjust(width[, fillchar])\n\n Return the string right justified in a string of length *width*.\n Padding is done using the specified *fillchar* (default is an ASCII\n space). The original string is returned if *width* is less than or\n equal to "len(s)".\n\nstr.rpartition(sep)\n\n Split the string at the last occurrence of *sep*, and return a\n 3-tuple containing the part before the separator, the separator\n itself, and the part after the separator. If the separator is not\n found, return a 3-tuple containing two empty strings, followed by\n the string itself.\n\nstr.rsplit(sep=None, maxsplit=-1)\n\n Return a list of the words in the string, using *sep* as the\n delimiter string. If *maxsplit* is given, at most *maxsplit* splits\n are done, the *rightmost* ones. If *sep* is not specified or\n "None", any whitespace string is a separator. Except for splitting\n from the right, "rsplit()" behaves like "split()" which is\n described in detail below.\n\nstr.rstrip([chars])\n\n Return a copy of the string with trailing characters removed. The\n *chars* argument is a string specifying the set of characters to be\n removed. If omitted or "None", the *chars* argument defaults to\n removing whitespace. The *chars* argument is not a suffix; rather,\n all combinations of its values are stripped:\n\n >>> \' spacious \'.rstrip()\n \' spacious\'\n >>> \'mississippi\'.rstrip(\'ipz\')\n \'mississ\'\n\nstr.split(sep=None, maxsplit=-1)\n\n Return a list of the words in the string, using *sep* as the\n delimiter string. If *maxsplit* is given, at most *maxsplit*\n splits are done (thus, the list will have at most "maxsplit+1"\n elements). If *maxsplit* is not specified or "-1", then there is\n no limit on the number of splits (all possible splits are made).\n\n If *sep* is given, consecutive delimiters are not grouped together\n and are deemed to delimit empty strings (for example,\n "\'1,,2\'.split(\',\')" returns "[\'1\', \'\', \'2\']"). The *sep* argument\n may consist of multiple characters (for example,\n "\'1<>2<>3\'.split(\'<>\')" returns "[\'1\', \'2\', \'3\']"). Splitting an\n empty string with a specified separator returns "[\'\']".\n\n For example:\n\n >>> \'1,2,3\'.split(\',\')\n [\'1\', \'2\', \'3\']\n >>> \'1,2,3\'.split(\',\', maxsplit=1)\n [\'1\', \'2,3\']\n >>> \'1,2,,3,\'.split(\',\')\n [\'1\', \'2\', \'\', \'3\', \'\']\n\n If *sep* is not specified or is "None", a different splitting\n algorithm is applied: runs of consecutive whitespace are regarded\n as a single separator, and the result will contain no empty strings\n at the start or end if the string has leading or trailing\n whitespace. Consequently, splitting an empty string or a string\n consisting of just whitespace with a "None" separator returns "[]".\n\n For example:\n\n >>> \'1 2 3\'.split()\n [\'1\', \'2\', \'3\']\n >>> \'1 2 3\'.split(maxsplit=1)\n [\'1\', \'2 3\']\n >>> \' 1 2 3 \'.split()\n [\'1\', \'2\', \'3\']\n\nstr.splitlines([keepends])\n\n Return a list of the lines in the string, breaking at line\n boundaries. Line breaks are not included in the resulting list\n unless *keepends* is given and true.\n\n This method splits on the following line boundaries. In\n particular, the boundaries are a superset of *universal newlines*.\n\n +-------------------------+-------------------------------+\n | Representation | Description |\n +=========================+===============================+\n | "\\n" | Line Feed |\n +-------------------------+-------------------------------+\n | "\\r" | Carriage Return |\n +-------------------------+-------------------------------+\n | "\\r\\n" | Carriage Return + Line Feed |\n +-------------------------+-------------------------------+\n | "\\v" or "\\x0b" | Line Tabulation |\n +-------------------------+-------------------------------+\n | "\\f" or "\\x0c" | Form Feed |\n +-------------------------+-------------------------------+\n | "\\x1c" | File Separator |\n +-------------------------+-------------------------------+\n | "\\x1d" | Group Separator |\n +-------------------------+-------------------------------+\n | "\\x1e" | Record Separator |\n +-------------------------+-------------------------------+\n | "\\x85" | Next Line (C1 Control Code) |\n +-------------------------+-------------------------------+\n | "\\u2028" | Line Separator |\n +-------------------------+-------------------------------+\n | "\\u2029" | Paragraph Separator |\n +-------------------------+-------------------------------+\n\n Changed in version 3.2: "\\v" and "\\f" added to list of line\n boundaries.\n\n For example:\n\n >>> \'ab c\\n\\nde fg\\rkl\\r\\n\'.splitlines()\n [\'ab c\', \'\', \'de fg\', \'kl\']\n >>> \'ab c\\n\\nde fg\\rkl\\r\\n\'.splitlines(keepends=True)\n [\'ab c\\n\', \'\\n\', \'de fg\\r\', \'kl\\r\\n\']\n\n Unlike "split()" when a delimiter string *sep* is given, this\n method returns an empty list for the empty string, and a terminal\n line break does not result in an extra line:\n\n >>> "".splitlines()\n []\n >>> "One line\\n".splitlines()\n [\'One line\']\n\n For comparison, "split(\'\\n\')" gives:\n\n >>> \'\'.split(\'\\n\')\n [\'\']\n >>> \'Two lines\\n\'.split(\'\\n\')\n [\'Two lines\', \'\']\n\nstr.startswith(prefix[, start[, end]])\n\n Return "True" if string starts with the *prefix*, otherwise return\n "False". *prefix* can also be a tuple of prefixes to look for.\n With optional *start*, test string beginning at that position.\n With optional *end*, stop comparing string at that position.\n\nstr.strip([chars])\n\n Return a copy of the string with the leading and trailing\n characters removed. The *chars* argument is a string specifying the\n set of characters to be removed. If omitted or "None", the *chars*\n argument defaults to removing whitespace. The *chars* argument is\n not a prefix or suffix; rather, all combinations of its values are\n stripped:\n\n >>> \' spacious \'.strip()\n \'spacious\'\n >>> \'www.example.com\'.strip(\'cmowz.\')\n \'example\'\n\n The outermost leading and trailing *chars* argument values are\n stripped from the string. Characters are removed from the leading\n end until reaching a string character that is not contained in the\n set of characters in *chars*. A similar action takes place on the\n trailing end. For example:\n\n >>> comment_string = \'#....... Section 3.2.1 Issue #32 .......\'\n >>> comment_string.strip(\'.#! \')\n \'Section 3.2.1 Issue #32\'\n\nstr.swapcase()\n\n Return a copy of the string with uppercase characters converted to\n lowercase and vice versa. Note that it is not necessarily true that\n "s.swapcase().swapcase() == s".\n\nstr.title()\n\n Return a titlecased version of the string where words start with an\n uppercase character and the remaining characters are lowercase.\n\n For example:\n\n >>> \'Hello world\'.title()\n \'Hello World\'\n\n The algorithm uses a simple language-independent definition of a\n word as groups of consecutive letters. The definition works in\n many contexts but it means that apostrophes in contractions and\n possessives form word boundaries, which may not be the desired\n result:\n\n >>> "they\'re bill\'s friends from the UK".title()\n "They\'Re Bill\'S Friends From The Uk"\n\n A workaround for apostrophes can be constructed using regular\n expressions:\n\n >>> import re\n >>> def titlecase(s):\n ... return re.sub(r"[A-Za-z]+(\'[A-Za-z]+)?",\n ... lambda mo: mo.group(0)[0].upper() +\n ... mo.group(0)[1:].lower(),\n ... s)\n ...\n >>> titlecase("they\'re bill\'s friends.")\n "They\'re Bill\'s Friends."\n\nstr.translate(table)\n\n Return a copy of the string in which each character has been mapped\n through the given translation table. The table must be an object\n that implements indexing via "__getitem__()", typically a *mapping*\n or *sequence*. When indexed by a Unicode ordinal (an integer), the\n table object can do any of the following: return a Unicode ordinal\n or a string, to map the character to one or more other characters;\n return "None", to delete the character from the return string; or\n raise a "LookupError" exception, to map the character to itself.\n\n You can use "str.maketrans()" to create a translation map from\n character-to-character mappings in different formats.\n\n See also the "codecs" module for a more flexible approach to custom\n character mappings.\n\nstr.upper()\n\n Return a copy of the string with all the cased characters [4]\n converted to uppercase. Note that "str.upper().isupper()" might be\n "False" if "s" contains uncased characters or if the Unicode\n category of the resulting character(s) is not "Lu" (Letter,\n uppercase), but e.g. "Lt" (Letter, titlecase).\n\n The uppercasing algorithm used is described in section 3.13 of the\n Unicode Standard.\n\nstr.zfill(width)\n\n Return a copy of the string left filled with ASCII "\'0\'" digits to\n make a string of length *width*. A leading sign prefix\n ("\'+\'"/"\'-\'") is handled by inserting the padding *after* the sign\n character rather than before. The original string is returned if\n *width* is less than or equal to "len(s)".\n\n For example:\n\n >>> "42".zfill(5)\n \'00042\'\n >>> "-42".zfill(5)\n \'-0042\'\n',
- 'strings': u'\nString and Bytes literals\n*************************\n\nString literals are described by the following lexical definitions:\n\n stringliteral ::= [stringprefix](shortstring | longstring)\n stringprefix ::= "r" | "u" | "R" | "U"\n shortstring ::= "\'" shortstringitem* "\'" | \'"\' shortstringitem* \'"\'\n longstring ::= "\'\'\'" longstringitem* "\'\'\'" | \'"""\' longstringitem* \'"""\'\n shortstringitem ::= shortstringchar | stringescapeseq\n longstringitem ::= longstringchar | stringescapeseq\n shortstringchar ::= <any source character except "\\" or newline or the quote>\n longstringchar ::= <any source character except "\\">\n stringescapeseq ::= "\\" <any source character>\n\n bytesliteral ::= bytesprefix(shortbytes | longbytes)\n bytesprefix ::= "b" | "B" | "br" | "Br" | "bR" | "BR" | "rb" | "rB" | "Rb" | "RB"\n shortbytes ::= "\'" shortbytesitem* "\'" | \'"\' shortbytesitem* \'"\'\n longbytes ::= "\'\'\'" longbytesitem* "\'\'\'" | \'"""\' longbytesitem* \'"""\'\n shortbytesitem ::= shortbyteschar | bytesescapeseq\n longbytesitem ::= longbyteschar | bytesescapeseq\n shortbyteschar ::= <any ASCII character except "\\" or newline or the quote>\n longbyteschar ::= <any ASCII character except "\\">\n bytesescapeseq ::= "\\" <any ASCII character>\n\nOne syntactic restriction not indicated by these productions is that\nwhitespace is not allowed between the "stringprefix" or "bytesprefix"\nand the rest of the literal. The source character set is defined by\nthe encoding declaration; it is UTF-8 if no encoding declaration is\ngiven in the source file; see section *Encoding declarations*.\n\nIn plain English: Both types of literals can be enclosed in matching\nsingle quotes ("\'") or double quotes ("""). They can also be enclosed\nin matching groups of three single or double quotes (these are\ngenerally referred to as *triple-quoted strings*). The backslash\n("\\") character is used to escape characters that otherwise have a\nspecial meaning, such as newline, backslash itself, or the quote\ncharacter.\n\nBytes literals are always prefixed with "\'b\'" or "\'B\'"; they produce\nan instance of the "bytes" type instead of the "str" type. They may\nonly contain ASCII characters; bytes with a numeric value of 128 or\ngreater must be expressed with escapes.\n\nAs of Python 3.3 it is possible again to prefix string literals with a\n"u" prefix to simplify maintenance of dual 2.x and 3.x codebases.\n\nBoth string and bytes literals may optionally be prefixed with a\nletter "\'r\'" or "\'R\'"; such strings are called *raw strings* and treat\nbackslashes as literal characters. As a result, in string literals,\n"\'\\U\'" and "\'\\u\'" escapes in raw strings are not treated specially.\nGiven that Python 2.x\'s raw unicode literals behave differently than\nPython 3.x\'s the "\'ur\'" syntax is not supported.\n\nNew in version 3.3: The "\'rb\'" prefix of raw bytes literals has been\nadded as a synonym of "\'br\'".\n\nNew in version 3.3: Support for the unicode legacy literal\n("u\'value\'") was reintroduced to simplify the maintenance of dual\nPython 2.x and 3.x codebases. See **PEP 414** for more information.\n\nIn triple-quoted literals, unescaped newlines and quotes are allowed\n(and are retained), except that three unescaped quotes in a row\nterminate the literal. (A "quote" is the character used to open the\nliteral, i.e. either "\'" or """.)\n\nUnless an "\'r\'" or "\'R\'" prefix is present, escape sequences in string\nand bytes literals are interpreted according to rules similar to those\nused by Standard C. The recognized escape sequences are:\n\n+-------------------+-----------------------------------+---------+\n| Escape Sequence | Meaning | Notes |\n+===================+===================================+=========+\n| "\\newline" | Backslash and newline ignored | |\n+-------------------+-----------------------------------+---------+\n| "\\\\" | Backslash ("\\") | |\n+-------------------+-----------------------------------+---------+\n| "\\\'" | Single quote ("\'") | |\n+-------------------+-----------------------------------+---------+\n| "\\"" | Double quote (""") | |\n+-------------------+-----------------------------------+---------+\n| "\\a" | ASCII Bell (BEL) | |\n+-------------------+-----------------------------------+---------+\n| "\\b" | ASCII Backspace (BS) | |\n+-------------------+-----------------------------------+---------+\n| "\\f" | ASCII Formfeed (FF) | |\n+-------------------+-----------------------------------+---------+\n| "\\n" | ASCII Linefeed (LF) | |\n+-------------------+-----------------------------------+---------+\n| "\\r" | ASCII Carriage Return (CR) | |\n+-------------------+-----------------------------------+---------+\n| "\\t" | ASCII Horizontal Tab (TAB) | |\n+-------------------+-----------------------------------+---------+\n| "\\v" | ASCII Vertical Tab (VT) | |\n+-------------------+-----------------------------------+---------+\n| "\\ooo" | Character with octal value *ooo* | (1,3) |\n+-------------------+-----------------------------------+---------+\n| "\\xhh" | Character with hex value *hh* | (2,3) |\n+-------------------+-----------------------------------+---------+\n\nEscape sequences only recognized in string literals are:\n\n+-------------------+-----------------------------------+---------+\n| Escape Sequence | Meaning | Notes |\n+===================+===================================+=========+\n| "\\N{name}" | Character named *name* in the | (4) |\n| | Unicode database | |\n+-------------------+-----------------------------------+---------+\n| "\\uxxxx" | Character with 16-bit hex value | (5) |\n| | *xxxx* | |\n+-------------------+-----------------------------------+---------+\n| "\\Uxxxxxxxx" | Character with 32-bit hex value | (6) |\n| | *xxxxxxxx* | |\n+-------------------+-----------------------------------+---------+\n\nNotes:\n\n1. As in Standard C, up to three octal digits are accepted.\n\n2. Unlike in Standard C, exactly two hex digits are required.\n\n3. In a bytes literal, hexadecimal and octal escapes denote the\n byte with the given value. In a string literal, these escapes\n denote a Unicode character with the given value.\n\n4. Changed in version 3.3: Support for name aliases [1] has been\n added.\n\n5. Exactly four hex digits are required.\n\n6. Any Unicode character can be encoded this way. Exactly eight\n hex digits are required.\n\nUnlike Standard C, all unrecognized escape sequences are left in the\nstring unchanged, i.e., *the backslash is left in the result*. (This\nbehavior is useful when debugging: if an escape sequence is mistyped,\nthe resulting output is more easily recognized as broken.) It is also\nimportant to note that the escape sequences only recognized in string\nliterals fall into the category of unrecognized escapes for bytes\nliterals.\n\nEven in a raw literal, quotes can be escaped with a backslash, but the\nbackslash remains in the result; for example, "r"\\""" is a valid\nstring literal consisting of two characters: a backslash and a double\nquote; "r"\\"" is not a valid string literal (even a raw string cannot\nend in an odd number of backslashes). Specifically, *a raw literal\ncannot end in a single backslash* (since the backslash would escape\nthe following quote character). Note also that a single backslash\nfollowed by a newline is interpreted as those two characters as part\nof the literal, *not* as a line continuation.\n',
- 'subscriptions': u'\nSubscriptions\n*************\n\nA subscription selects an item of a sequence (string, tuple or list)\nor mapping (dictionary) object:\n\n subscription ::= primary "[" expression_list "]"\n\nThe primary must evaluate to an object that supports subscription\n(lists or dictionaries for example). User-defined objects can support\nsubscription by defining a "__getitem__()" method.\n\nFor built-in objects, there are two types of objects that support\nsubscription:\n\nIf the primary is a mapping, the expression list must evaluate to an\nobject whose value is one of the keys of the mapping, and the\nsubscription selects the value in the mapping that corresponds to that\nkey. (The expression list is a tuple except if it has exactly one\nitem.)\n\nIf the primary is a sequence, the expression (list) must evaluate to\nan integer or a slice (as discussed in the following section).\n\nThe formal syntax makes no special provision for negative indices in\nsequences; however, built-in sequences all provide a "__getitem__()"\nmethod that interprets negative indices by adding the length of the\nsequence to the index (so that "x[-1]" selects the last item of "x").\nThe resulting value must be a nonnegative integer less than the number\nof items in the sequence, and the subscription selects the item whose\nindex is that value (counting from zero). Since the support for\nnegative indices and slicing occurs in the object\'s "__getitem__()"\nmethod, subclasses overriding this method will need to explicitly add\nthat support.\n\nA string\'s items are characters. A character is not a separate data\ntype but a string of exactly one character.\n',
- 'truth': u'\nTruth Value Testing\n*******************\n\nAny object can be tested for truth value, for use in an "if" or\n"while" condition or as operand of the Boolean operations below. The\nfollowing values are considered false:\n\n* "None"\n\n* "False"\n\n* zero of any numeric type, for example, "0", "0.0", "0j".\n\n* any empty sequence, for example, "\'\'", "()", "[]".\n\n* any empty mapping, for example, "{}".\n\n* instances of user-defined classes, if the class defines a\n "__bool__()" or "__len__()" method, when that method returns the\n integer zero or "bool" value "False". [1]\n\nAll other values are considered true --- so objects of many types are\nalways true.\n\nOperations and built-in functions that have a Boolean result always\nreturn "0" or "False" for false and "1" or "True" for true, unless\notherwise stated. (Important exception: the Boolean operations "or"\nand "and" always return one of their operands.)\n',
- 'try': u'\nThe "try" statement\n*******************\n\nThe "try" statement specifies exception handlers and/or cleanup code\nfor a group of statements:\n\n try_stmt ::= try1_stmt | try2_stmt\n try1_stmt ::= "try" ":" suite\n ("except" [expression ["as" identifier]] ":" suite)+\n ["else" ":" suite]\n ["finally" ":" suite]\n try2_stmt ::= "try" ":" suite\n "finally" ":" suite\n\nThe "except" clause(s) specify one or more exception handlers. When no\nexception occurs in the "try" clause, no exception handler is\nexecuted. When an exception occurs in the "try" suite, a search for an\nexception handler is started. This search inspects the except clauses\nin turn until one is found that matches the exception. An expression-\nless except clause, if present, must be last; it matches any\nexception. For an except clause with an expression, that expression\nis evaluated, and the clause matches the exception if the resulting\nobject is "compatible" with the exception. An object is compatible\nwith an exception if it is the class or a base class of the exception\nobject or a tuple containing an item compatible with the exception.\n\nIf no except clause matches the exception, the search for an exception\nhandler continues in the surrounding code and on the invocation stack.\n[1]\n\nIf the evaluation of an expression in the header of an except clause\nraises an exception, the original search for a handler is canceled and\na search starts for the new exception in the surrounding code and on\nthe call stack (it is treated as if the entire "try" statement raised\nthe exception).\n\nWhen a matching except clause is found, the exception is assigned to\nthe target specified after the "as" keyword in that except clause, if\npresent, and the except clause\'s suite is executed. All except\nclauses must have an executable block. When the end of this block is\nreached, execution continues normally after the entire try statement.\n(This means that if two nested handlers exist for the same exception,\nand the exception occurs in the try clause of the inner handler, the\nouter handler will not handle the exception.)\n\nWhen an exception has been assigned using "as target", it is cleared\nat the end of the except clause. This is as if\n\n except E as N:\n foo\n\nwas translated to\n\n except E as N:\n try:\n foo\n finally:\n del N\n\nThis means the exception must be assigned to a different name to be\nable to refer to it after the except clause. Exceptions are cleared\nbecause with the traceback attached to them, they form a reference\ncycle with the stack frame, keeping all locals in that frame alive\nuntil the next garbage collection occurs.\n\nBefore an except clause\'s suite is executed, details about the\nexception are stored in the "sys" module and can be accessed via\n"sys.exc_info()". "sys.exc_info()" returns a 3-tuple consisting of the\nexception class, the exception instance and a traceback object (see\nsection *The standard type hierarchy*) identifying the point in the\nprogram where the exception occurred. "sys.exc_info()" values are\nrestored to their previous values (before the call) when returning\nfrom a function that handled an exception.\n\nThe optional "else" clause is executed if and when control flows off\nthe end of the "try" clause. [2] Exceptions in the "else" clause are\nnot handled by the preceding "except" clauses.\n\nIf "finally" is present, it specifies a \'cleanup\' handler. The "try"\nclause is executed, including any "except" and "else" clauses. If an\nexception occurs in any of the clauses and is not handled, the\nexception is temporarily saved. The "finally" clause is executed. If\nthere is a saved exception it is re-raised at the end of the "finally"\nclause. If the "finally" clause raises another exception, the saved\nexception is set as the context of the new exception. If the "finally"\nclause executes a "return" or "break" statement, the saved exception\nis discarded:\n\n >>> def f():\n ... try:\n ... 1/0\n ... finally:\n ... return 42\n ...\n >>> f()\n 42\n\nThe exception information is not available to the program during\nexecution of the "finally" clause.\n\nWhen a "return", "break" or "continue" statement is executed in the\n"try" suite of a "try"..."finally" statement, the "finally" clause is\nalso executed \'on the way out.\' A "continue" statement is illegal in\nthe "finally" clause. (The reason is a problem with the current\nimplementation --- this restriction may be lifted in the future).\n\nThe return value of a function is determined by the last "return"\nstatement executed. Since the "finally" clause always executes, a\n"return" statement executed in the "finally" clause will always be the\nlast one executed:\n\n >>> def foo():\n ... try:\n ... return \'try\'\n ... finally:\n ... return \'finally\'\n ...\n >>> foo()\n \'finally\'\n\nAdditional information on exceptions can be found in section\n*Exceptions*, and information on using the "raise" statement to\ngenerate exceptions may be found in section *The raise statement*.\n',
- 'types': u'\nThe standard type hierarchy\n***************************\n\nBelow is a list of the types that are built into Python. Extension\nmodules (written in C, Java, or other languages, depending on the\nimplementation) can define additional types. Future versions of\nPython may add types to the type hierarchy (e.g., rational numbers,\nefficiently stored arrays of integers, etc.), although such additions\nwill often be provided via the standard library instead.\n\nSome of the type descriptions below contain a paragraph listing\n\'special attributes.\' These are attributes that provide access to the\nimplementation and are not intended for general use. Their definition\nmay change in the future.\n\nNone\n This type has a single value. There is a single object with this\n value. This object is accessed through the built-in name "None". It\n is used to signify the absence of a value in many situations, e.g.,\n it is returned from functions that don\'t explicitly return\n anything. Its truth value is false.\n\nNotImplemented\n This type has a single value. There is a single object with this\n value. This object is accessed through the built-in name\n "NotImplemented". Numeric methods and rich comparison methods\n should return this value if they do not implement the operation for\n the operands provided. (The interpreter will then try the\n reflected operation, or some other fallback, depending on the\n operator.) Its truth value is true.\n\n See *Implementing the arithmetic operations* for more details.\n\nEllipsis\n This type has a single value. There is a single object with this\n value. This object is accessed through the literal "..." or the\n built-in name "Ellipsis". Its truth value is true.\n\n"numbers.Number"\n These are created by numeric literals and returned as results by\n arithmetic operators and arithmetic built-in functions. Numeric\n objects are immutable; once created their value never changes.\n Python numbers are of course strongly related to mathematical\n numbers, but subject to the limitations of numerical representation\n in computers.\n\n Python distinguishes between integers, floating point numbers, and\n complex numbers:\n\n "numbers.Integral"\n These represent elements from the mathematical set of integers\n (positive and negative).\n\n There are two types of integers:\n\n Integers ("int")\n\n These represent numbers in an unlimited range, subject to\n available (virtual) memory only. For the purpose of shift\n and mask operations, a binary representation is assumed, and\n negative numbers are represented in a variant of 2\'s\n complement which gives the illusion of an infinite string of\n sign bits extending to the left.\n\n Booleans ("bool")\n These represent the truth values False and True. The two\n objects representing the values "False" and "True" are the\n only Boolean objects. The Boolean type is a subtype of the\n integer type, and Boolean values behave like the values 0 and\n 1, respectively, in almost all contexts, the exception being\n that when converted to a string, the strings ""False"" or\n ""True"" are returned, respectively.\n\n The rules for integer representation are intended to give the\n most meaningful interpretation of shift and mask operations\n involving negative integers.\n\n "numbers.Real" ("float")\n These represent machine-level double precision floating point\n numbers. You are at the mercy of the underlying machine\n architecture (and C or Java implementation) for the accepted\n range and handling of overflow. Python does not support single-\n precision floating point numbers; the savings in processor and\n memory usage that are usually the reason for using these are\n dwarfed by the overhead of using objects in Python, so there is\n no reason to complicate the language with two kinds of floating\n point numbers.\n\n "numbers.Complex" ("complex")\n These represent complex numbers as a pair of machine-level\n double precision floating point numbers. The same caveats apply\n as for floating point numbers. The real and imaginary parts of a\n complex number "z" can be retrieved through the read-only\n attributes "z.real" and "z.imag".\n\nSequences\n These represent finite ordered sets indexed by non-negative\n numbers. The built-in function "len()" returns the number of items\n of a sequence. When the length of a sequence is *n*, the index set\n contains the numbers 0, 1, ..., *n*-1. Item *i* of sequence *a* is\n selected by "a[i]".\n\n Sequences also support slicing: "a[i:j]" selects all items with\n index *k* such that *i* "<=" *k* "<" *j*. When used as an\n expression, a slice is a sequence of the same type. This implies\n that the index set is renumbered so that it starts at 0.\n\n Some sequences also support "extended slicing" with a third "step"\n parameter: "a[i:j:k]" selects all items of *a* with index *x* where\n "x = i + n*k", *n* ">=" "0" and *i* "<=" *x* "<" *j*.\n\n Sequences are distinguished according to their mutability:\n\n Immutable sequences\n An object of an immutable sequence type cannot change once it is\n created. (If the object contains references to other objects,\n these other objects may be mutable and may be changed; however,\n the collection of objects directly referenced by an immutable\n object cannot change.)\n\n The following types are immutable sequences:\n\n Strings\n A string is a sequence of values that represent Unicode code\n points. All the code points in the range "U+0000 - U+10FFFF"\n can be represented in a string. Python doesn\'t have a "char"\n type; instead, every code point in the string is represented\n as a string object with length "1". The built-in function\n "ord()" converts a code point from its string form to an\n integer in the range "0 - 10FFFF"; "chr()" converts an\n integer in the range "0 - 10FFFF" to the corresponding length\n "1" string object. "str.encode()" can be used to convert a\n "str" to "bytes" using the given text encoding, and\n "bytes.decode()" can be used to achieve the opposite.\n\n Tuples\n The items of a tuple are arbitrary Python objects. Tuples of\n two or more items are formed by comma-separated lists of\n expressions. A tuple of one item (a \'singleton\') can be\n formed by affixing a comma to an expression (an expression by\n itself does not create a tuple, since parentheses must be\n usable for grouping of expressions). An empty tuple can be\n formed by an empty pair of parentheses.\n\n Bytes\n A bytes object is an immutable array. The items are 8-bit\n bytes, represented by integers in the range 0 <= x < 256.\n Bytes literals (like "b\'abc\'") and the built-in function\n "bytes()" can be used to construct bytes objects. Also,\n bytes objects can be decoded to strings via the "decode()"\n method.\n\n Mutable sequences\n Mutable sequences can be changed after they are created. The\n subscription and slicing notations can be used as the target of\n assignment and "del" (delete) statements.\n\n There are currently two intrinsic mutable sequence types:\n\n Lists\n The items of a list are arbitrary Python objects. Lists are\n formed by placing a comma-separated list of expressions in\n square brackets. (Note that there are no special cases needed\n to form lists of length 0 or 1.)\n\n Byte Arrays\n A bytearray object is a mutable array. They are created by\n the built-in "bytearray()" constructor. Aside from being\n mutable (and hence unhashable), byte arrays otherwise provide\n the same interface and functionality as immutable bytes\n objects.\n\n The extension module "array" provides an additional example of a\n mutable sequence type, as does the "collections" module.\n\nSet types\n These represent unordered, finite sets of unique, immutable\n objects. As such, they cannot be indexed by any subscript. However,\n they can be iterated over, and the built-in function "len()"\n returns the number of items in a set. Common uses for sets are fast\n membership testing, removing duplicates from a sequence, and\n computing mathematical operations such as intersection, union,\n difference, and symmetric difference.\n\n For set elements, the same immutability rules apply as for\n dictionary keys. Note that numeric types obey the normal rules for\n numeric comparison: if two numbers compare equal (e.g., "1" and\n "1.0"), only one of them can be contained in a set.\n\n There are currently two intrinsic set types:\n\n Sets\n These represent a mutable set. They are created by the built-in\n "set()" constructor and can be modified afterwards by several\n methods, such as "add()".\n\n Frozen sets\n These represent an immutable set. They are created by the\n built-in "frozenset()" constructor. As a frozenset is immutable\n and *hashable*, it can be used again as an element of another\n set, or as a dictionary key.\n\nMappings\n These represent finite sets of objects indexed by arbitrary index\n sets. The subscript notation "a[k]" selects the item indexed by "k"\n from the mapping "a"; this can be used in expressions and as the\n target of assignments or "del" statements. The built-in function\n "len()" returns the number of items in a mapping.\n\n There is currently a single intrinsic mapping type:\n\n Dictionaries\n These represent finite sets of objects indexed by nearly\n arbitrary values. The only types of values not acceptable as\n keys are values containing lists or dictionaries or other\n mutable types that are compared by value rather than by object\n identity, the reason being that the efficient implementation of\n dictionaries requires a key\'s hash value to remain constant.\n Numeric types used for keys obey the normal rules for numeric\n comparison: if two numbers compare equal (e.g., "1" and "1.0")\n then they can be used interchangeably to index the same\n dictionary entry.\n\n Dictionaries are mutable; they can be created by the "{...}"\n notation (see section *Dictionary displays*).\n\n The extension modules "dbm.ndbm" and "dbm.gnu" provide\n additional examples of mapping types, as does the "collections"\n module.\n\nCallable types\n These are the types to which the function call operation (see\n section *Calls*) can be applied:\n\n User-defined functions\n A user-defined function object is created by a function\n definition (see section *Function definitions*). It should be\n called with an argument list containing the same number of items\n as the function\'s formal parameter list.\n\n Special attributes:\n\n +---------------------------+---------------------------------+-------------+\n | Attribute | Meaning | |\n +===========================+=================================+=============+\n | "__doc__" | The function\'s documentation | Writable |\n | | string, or "None" if | |\n | | unavailable; not inherited by | |\n | | subclasses | |\n +---------------------------+---------------------------------+-------------+\n | "__name__" | The function\'s name | Writable |\n +---------------------------+---------------------------------+-------------+\n | "__qualname__" | The function\'s *qualified name* | Writable |\n | | New in version 3.3. | |\n +---------------------------+---------------------------------+-------------+\n | "__module__" | The name of the module the | Writable |\n | | function was defined in, or | |\n | | "None" if unavailable. | |\n +---------------------------+---------------------------------+-------------+\n | "__defaults__" | A tuple containing default | Writable |\n | | argument values for those | |\n | | arguments that have defaults, | |\n | | or "None" if no arguments have | |\n | | a default value | |\n +---------------------------+---------------------------------+-------------+\n | "__code__" | The code object representing | Writable |\n | | the compiled function body. | |\n +---------------------------+---------------------------------+-------------+\n | "__globals__" | A reference to the dictionary | Read-only |\n | | that holds the function\'s | |\n | | global variables --- the global | |\n | | namespace of the module in | |\n | | which the function was defined. | |\n +---------------------------+---------------------------------+-------------+\n | "__dict__" | The namespace supporting | Writable |\n | | arbitrary function attributes. | |\n +---------------------------+---------------------------------+-------------+\n | "__closure__" | "None" or a tuple of cells that | Read-only |\n | | contain bindings for the | |\n | | function\'s free variables. | |\n +---------------------------+---------------------------------+-------------+\n | "__annotations__" | A dict containing annotations | Writable |\n | | of parameters. The keys of the | |\n | | dict are the parameter names, | |\n | | and "\'return\'" for the return | |\n | | annotation, if provided. | |\n +---------------------------+---------------------------------+-------------+\n | "__kwdefaults__" | A dict containing defaults for | Writable |\n | | keyword-only parameters. | |\n +---------------------------+---------------------------------+-------------+\n\n Most of the attributes labelled "Writable" check the type of the\n assigned value.\n\n Function objects also support getting and setting arbitrary\n attributes, which can be used, for example, to attach metadata\n to functions. Regular attribute dot-notation is used to get and\n set such attributes. *Note that the current implementation only\n supports function attributes on user-defined functions. Function\n attributes on built-in functions may be supported in the\n future.*\n\n Additional information about a function\'s definition can be\n retrieved from its code object; see the description of internal\n types below.\n\n Instance methods\n An instance method object combines a class, a class instance and\n any callable object (normally a user-defined function).\n\n Special read-only attributes: "__self__" is the class instance\n object, "__func__" is the function object; "__doc__" is the\n method\'s documentation (same as "__func__.__doc__"); "__name__"\n is the method name (same as "__func__.__name__"); "__module__"\n is the name of the module the method was defined in, or "None"\n if unavailable.\n\n Methods also support accessing (but not setting) the arbitrary\n function attributes on the underlying function object.\n\n User-defined method objects may be created when getting an\n attribute of a class (perhaps via an instance of that class), if\n that attribute is a user-defined function object or a class\n method object.\n\n When an instance method object is created by retrieving a user-\n defined function object from a class via one of its instances,\n its "__self__" attribute is the instance, and the method object\n is said to be bound. The new method\'s "__func__" attribute is\n the original function object.\n\n When a user-defined method object is created by retrieving\n another method object from a class or instance, the behaviour is\n the same as for a function object, except that the "__func__"\n attribute of the new instance is not the original method object\n but its "__func__" attribute.\n\n When an instance method object is created by retrieving a class\n method object from a class or instance, its "__self__" attribute\n is the class itself, and its "__func__" attribute is the\n function object underlying the class method.\n\n When an instance method object is called, the underlying\n function ("__func__") is called, inserting the class instance\n ("__self__") in front of the argument list. For instance, when\n "C" is a class which contains a definition for a function "f()",\n and "x" is an instance of "C", calling "x.f(1)" is equivalent to\n calling "C.f(x, 1)".\n\n When an instance method object is derived from a class method\n object, the "class instance" stored in "__self__" will actually\n be the class itself, so that calling either "x.f(1)" or "C.f(1)"\n is equivalent to calling "f(C,1)" where "f" is the underlying\n function.\n\n Note that the transformation from function object to instance\n method object happens each time the attribute is retrieved from\n the instance. In some cases, a fruitful optimization is to\n assign the attribute to a local variable and call that local\n variable. Also notice that this transformation only happens for\n user-defined functions; other callable objects (and all non-\n callable objects) are retrieved without transformation. It is\n also important to note that user-defined functions which are\n attributes of a class instance are not converted to bound\n methods; this *only* happens when the function is an attribute\n of the class.\n\n Generator functions\n A function or method which uses the "yield" statement (see\n section *The yield statement*) is called a *generator function*.\n Such a function, when called, always returns an iterator object\n which can be used to execute the body of the function: calling\n the iterator\'s "iterator.__next__()" method will cause the\n function to execute until it provides a value using the "yield"\n statement. When the function executes a "return" statement or\n falls off the end, a "StopIteration" exception is raised and the\n iterator will have reached the end of the set of values to be\n returned.\n\n Coroutine functions\n A function or method which is defined using "async def" is\n called a *coroutine function*. Such a function, when called,\n returns a *coroutine* object. It may contain "await"\n expressions, as well as "async with" and "async for" statements.\n See also the *Coroutine Objects* section.\n\n Built-in functions\n A built-in function object is a wrapper around a C function.\n Examples of built-in functions are "len()" and "math.sin()"\n ("math" is a standard built-in module). The number and type of\n the arguments are determined by the C function. Special read-\n only attributes: "__doc__" is the function\'s documentation\n string, or "None" if unavailable; "__name__" is the function\'s\n name; "__self__" is set to "None" (but see the next item);\n "__module__" is the name of the module the function was defined\n in or "None" if unavailable.\n\n Built-in methods\n This is really a different disguise of a built-in function, this\n time containing an object passed to the C function as an\n implicit extra argument. An example of a built-in method is\n "alist.append()", assuming *alist* is a list object. In this\n case, the special read-only attribute "__self__" is set to the\n object denoted by *alist*.\n\n Classes\n Classes are callable. These objects normally act as factories\n for new instances of themselves, but variations are possible for\n class types that override "__new__()". The arguments of the\n call are passed to "__new__()" and, in the typical case, to\n "__init__()" to initialize the new instance.\n\n Class Instances\n Instances of arbitrary classes can be made callable by defining\n a "__call__()" method in their class.\n\nModules\n Modules are a basic organizational unit of Python code, and are\n created by the *import system* as invoked either by the "import"\n statement (see "import"), or by calling functions such as\n "importlib.import_module()" and built-in "__import__()". A module\n object has a namespace implemented by a dictionary object (this is\n the dictionary referenced by the "__globals__" attribute of\n functions defined in the module). Attribute references are\n translated to lookups in this dictionary, e.g., "m.x" is equivalent\n to "m.__dict__["x"]". A module object does not contain the code\n object used to initialize the module (since it isn\'t needed once\n the initialization is done).\n\n Attribute assignment updates the module\'s namespace dictionary,\n e.g., "m.x = 1" is equivalent to "m.__dict__["x"] = 1".\n\n Special read-only attribute: "__dict__" is the module\'s namespace\n as a dictionary object.\n\n **CPython implementation detail:** Because of the way CPython\n clears module dictionaries, the module dictionary will be cleared\n when the module falls out of scope even if the dictionary still has\n live references. To avoid this, copy the dictionary or keep the\n module around while using its dictionary directly.\n\n Predefined (writable) attributes: "__name__" is the module\'s name;\n "__doc__" is the module\'s documentation string, or "None" if\n unavailable; "__file__" is the pathname of the file from which the\n module was loaded, if it was loaded from a file. The "__file__"\n attribute may be missing for certain types of modules, such as C\n modules that are statically linked into the interpreter; for\n extension modules loaded dynamically from a shared library, it is\n the pathname of the shared library file.\n\nCustom classes\n Custom class types are typically created by class definitions (see\n section *Class definitions*). A class has a namespace implemented\n by a dictionary object. Class attribute references are translated\n to lookups in this dictionary, e.g., "C.x" is translated to\n "C.__dict__["x"]" (although there are a number of hooks which allow\n for other means of locating attributes). When the attribute name is\n not found there, the attribute search continues in the base\n classes. This search of the base classes uses the C3 method\n resolution order which behaves correctly even in the presence of\n \'diamond\' inheritance structures where there are multiple\n inheritance paths leading back to a common ancestor. Additional\n details on the C3 MRO used by Python can be found in the\n documentation accompanying the 2.3 release at\n https://www.python.org/download/releases/2.3/mro/.\n\n When a class attribute reference (for class "C", say) would yield a\n class method object, it is transformed into an instance method\n object whose "__self__" attributes is "C". When it would yield a\n static method object, it is transformed into the object wrapped by\n the static method object. See section *Implementing Descriptors*\n for another way in which attributes retrieved from a class may\n differ from those actually contained in its "__dict__".\n\n Class attribute assignments update the class\'s dictionary, never\n the dictionary of a base class.\n\n A class object can be called (see above) to yield a class instance\n (see below).\n\n Special attributes: "__name__" is the class name; "__module__" is\n the module name in which the class was defined; "__dict__" is the\n dictionary containing the class\'s namespace; "__bases__" is a tuple\n (possibly a singleton) containing the base classes, in the order of\n their occurrence in the base class list; "__doc__" is the class\'s\n documentation string, or "None" if undefined.\n\nClass instances\n A class instance is created by calling a class object (see above).\n A class instance has a namespace implemented as a dictionary which\n is the first place in which attribute references are searched.\n When an attribute is not found there, and the instance\'s class has\n an attribute by that name, the search continues with the class\n attributes. If a class attribute is found that is a user-defined\n function object, it is transformed into an instance method object\n whose "__self__" attribute is the instance. Static method and\n class method objects are also transformed; see above under\n "Classes". See section *Implementing Descriptors* for another way\n in which attributes of a class retrieved via its instances may\n differ from the objects actually stored in the class\'s "__dict__".\n If no class attribute is found, and the object\'s class has a\n "__getattr__()" method, that is called to satisfy the lookup.\n\n Attribute assignments and deletions update the instance\'s\n dictionary, never a class\'s dictionary. If the class has a\n "__setattr__()" or "__delattr__()" method, this is called instead\n of updating the instance dictionary directly.\n\n Class instances can pretend to be numbers, sequences, or mappings\n if they have methods with certain special names. See section\n *Special method names*.\n\n Special attributes: "__dict__" is the attribute dictionary;\n "__class__" is the instance\'s class.\n\nI/O objects (also known as file objects)\n A *file object* represents an open file. Various shortcuts are\n available to create file objects: the "open()" built-in function,\n and also "os.popen()", "os.fdopen()", and the "makefile()" method\n of socket objects (and perhaps by other functions or methods\n provided by extension modules).\n\n The objects "sys.stdin", "sys.stdout" and "sys.stderr" are\n initialized to file objects corresponding to the interpreter\'s\n standard input, output and error streams; they are all open in text\n mode and therefore follow the interface defined by the\n "io.TextIOBase" abstract class.\n\nInternal types\n A few types used internally by the interpreter are exposed to the\n user. Their definitions may change with future versions of the\n interpreter, but they are mentioned here for completeness.\n\n Code objects\n Code objects represent *byte-compiled* executable Python code,\n or *bytecode*. The difference between a code object and a\n function object is that the function object contains an explicit\n reference to the function\'s globals (the module in which it was\n defined), while a code object contains no context; also the\n default argument values are stored in the function object, not\n in the code object (because they represent values calculated at\n run-time). Unlike function objects, code objects are immutable\n and contain no references (directly or indirectly) to mutable\n objects.\n\n Special read-only attributes: "co_name" gives the function name;\n "co_argcount" is the number of positional arguments (including\n arguments with default values); "co_nlocals" is the number of\n local variables used by the function (including arguments);\n "co_varnames" is a tuple containing the names of the local\n variables (starting with the argument names); "co_cellvars" is a\n tuple containing the names of local variables that are\n referenced by nested functions; "co_freevars" is a tuple\n containing the names of free variables; "co_code" is a string\n representing the sequence of bytecode instructions; "co_consts"\n is a tuple containing the literals used by the bytecode;\n "co_names" is a tuple containing the names used by the bytecode;\n "co_filename" is the filename from which the code was compiled;\n "co_firstlineno" is the first line number of the function;\n "co_lnotab" is a string encoding the mapping from bytecode\n offsets to line numbers (for details see the source code of the\n interpreter); "co_stacksize" is the required stack size\n (including local variables); "co_flags" is an integer encoding a\n number of flags for the interpreter.\n\n The following flag bits are defined for "co_flags": bit "0x04"\n is set if the function uses the "*arguments" syntax to accept an\n arbitrary number of positional arguments; bit "0x08" is set if\n the function uses the "**keywords" syntax to accept arbitrary\n keyword arguments; bit "0x20" is set if the function is a\n generator.\n\n Future feature declarations ("from __future__ import division")\n also use bits in "co_flags" to indicate whether a code object\n was compiled with a particular feature enabled: bit "0x2000" is\n set if the function was compiled with future division enabled;\n bits "0x10" and "0x1000" were used in earlier versions of\n Python.\n\n Other bits in "co_flags" are reserved for internal use.\n\n If a code object represents a function, the first item in\n "co_consts" is the documentation string of the function, or\n "None" if undefined.\n\n Frame objects\n Frame objects represent execution frames. They may occur in\n traceback objects (see below).\n\n Special read-only attributes: "f_back" is to the previous stack\n frame (towards the caller), or "None" if this is the bottom\n stack frame; "f_code" is the code object being executed in this\n frame; "f_locals" is the dictionary used to look up local\n variables; "f_globals" is used for global variables;\n "f_builtins" is used for built-in (intrinsic) names; "f_lasti"\n gives the precise instruction (this is an index into the\n bytecode string of the code object).\n\n Special writable attributes: "f_trace", if not "None", is a\n function called at the start of each source code line (this is\n used by the debugger); "f_lineno" is the current line number of\n the frame --- writing to this from within a trace function jumps\n to the given line (only for the bottom-most frame). A debugger\n can implement a Jump command (aka Set Next Statement) by writing\n to f_lineno.\n\n Frame objects support one method:\n\n frame.clear()\n\n This method clears all references to local variables held by\n the frame. Also, if the frame belonged to a generator, the\n generator is finalized. This helps break reference cycles\n involving frame objects (for example when catching an\n exception and storing its traceback for later use).\n\n "RuntimeError" is raised if the frame is currently executing.\n\n New in version 3.4.\n\n Traceback objects\n Traceback objects represent a stack trace of an exception. A\n traceback object is created when an exception occurs. When the\n search for an exception handler unwinds the execution stack, at\n each unwound level a traceback object is inserted in front of\n the current traceback. When an exception handler is entered,\n the stack trace is made available to the program. (See section\n *The try statement*.) It is accessible as the third item of the\n tuple returned by "sys.exc_info()". When the program contains no\n suitable handler, the stack trace is written (nicely formatted)\n to the standard error stream; if the interpreter is interactive,\n it is also made available to the user as "sys.last_traceback".\n\n Special read-only attributes: "tb_next" is the next level in the\n stack trace (towards the frame where the exception occurred), or\n "None" if there is no next level; "tb_frame" points to the\n execution frame of the current level; "tb_lineno" gives the line\n number where the exception occurred; "tb_lasti" indicates the\n precise instruction. The line number and last instruction in\n the traceback may differ from the line number of its frame\n object if the exception occurred in a "try" statement with no\n matching except clause or with a finally clause.\n\n Slice objects\n Slice objects are used to represent slices for "__getitem__()"\n methods. They are also created by the built-in "slice()"\n function.\n\n Special read-only attributes: "start" is the lower bound; "stop"\n is the upper bound; "step" is the step value; each is "None" if\n omitted. These attributes can have any type.\n\n Slice objects support one method:\n\n slice.indices(self, length)\n\n This method takes a single integer argument *length* and\n computes information about the slice that the slice object\n would describe if applied to a sequence of *length* items.\n It returns a tuple of three integers; respectively these are\n the *start* and *stop* indices and the *step* or stride\n length of the slice. Missing or out-of-bounds indices are\n handled in a manner consistent with regular slices.\n\n Static method objects\n Static method objects provide a way of defeating the\n transformation of function objects to method objects described\n above. A static method object is a wrapper around any other\n object, usually a user-defined method object. When a static\n method object is retrieved from a class or a class instance, the\n object actually returned is the wrapped object, which is not\n subject to any further transformation. Static method objects are\n not themselves callable, although the objects they wrap usually\n are. Static method objects are created by the built-in\n "staticmethod()" constructor.\n\n Class method objects\n A class method object, like a static method object, is a wrapper\n around another object that alters the way in which that object\n is retrieved from classes and class instances. The behaviour of\n class method objects upon such retrieval is described above,\n under "User-defined methods". Class method objects are created\n by the built-in "classmethod()" constructor.\n',
- 'typesfunctions': u'\nFunctions\n*********\n\nFunction objects are created by function definitions. The only\noperation on a function object is to call it: "func(argument-list)".\n\nThere are really two flavors of function objects: built-in functions\nand user-defined functions. Both support the same operation (to call\nthe function), but the implementation is different, hence the\ndifferent object types.\n\nSee *Function definitions* for more information.\n',
- 'typesmapping': u'\nMapping Types --- "dict"\n************************\n\nA *mapping* object maps *hashable* values to arbitrary objects.\nMappings are mutable objects. There is currently only one standard\nmapping type, the *dictionary*. (For other containers see the built-\nin "list", "set", and "tuple" classes, and the "collections" module.)\n\nA dictionary\'s keys are *almost* arbitrary values. Values that are\nnot *hashable*, that is, values containing lists, dictionaries or\nother mutable types (that are compared by value rather than by object\nidentity) may not be used as keys. Numeric types used for keys obey\nthe normal rules for numeric comparison: if two numbers compare equal\n(such as "1" and "1.0") then they can be used interchangeably to index\nthe same dictionary entry. (Note however, that since computers store\nfloating-point numbers as approximations it is usually unwise to use\nthem as dictionary keys.)\n\nDictionaries can be created by placing a comma-separated list of "key:\nvalue" pairs within braces, for example: "{\'jack\': 4098, \'sjoerd\':\n4127}" or "{4098: \'jack\', 4127: \'sjoerd\'}", or by the "dict"\nconstructor.\n\nclass class dict(**kwarg)\nclass class dict(mapping, **kwarg)\nclass class dict(iterable, **kwarg)\n\n Return a new dictionary initialized from an optional positional\n argument and a possibly empty set of keyword arguments.\n\n If no positional argument is given, an empty dictionary is created.\n If a positional argument is given and it is a mapping object, a\n dictionary is created with the same key-value pairs as the mapping\n object. Otherwise, the positional argument must be an *iterable*\n object. Each item in the iterable must itself be an iterable with\n exactly two objects. The first object of each item becomes a key\n in the new dictionary, and the second object the corresponding\n value. If a key occurs more than once, the last value for that key\n becomes the corresponding value in the new dictionary.\n\n If keyword arguments are given, the keyword arguments and their\n values are added to the dictionary created from the positional\n argument. If a key being added is already present, the value from\n the keyword argument replaces the value from the positional\n argument.\n\n To illustrate, the following examples all return a dictionary equal\n to "{"one": 1, "two": 2, "three": 3}":\n\n >>> a = dict(one=1, two=2, three=3)\n >>> b = {\'one\': 1, \'two\': 2, \'three\': 3}\n >>> c = dict(zip([\'one\', \'two\', \'three\'], [1, 2, 3]))\n >>> d = dict([(\'two\', 2), (\'one\', 1), (\'three\', 3)])\n >>> e = dict({\'three\': 3, \'one\': 1, \'two\': 2})\n >>> a == b == c == d == e\n True\n\n Providing keyword arguments as in the first example only works for\n keys that are valid Python identifiers. Otherwise, any valid keys\n can be used.\n\n These are the operations that dictionaries support (and therefore,\n custom mapping types should support too):\n\n len(d)\n\n Return the number of items in the dictionary *d*.\n\n d[key]\n\n Return the item of *d* with key *key*. Raises a "KeyError" if\n *key* is not in the map.\n\n If a subclass of dict defines a method "__missing__()" and *key*\n is not present, the "d[key]" operation calls that method with\n the key *key* as argument. The "d[key]" operation then returns\n or raises whatever is returned or raised by the\n "__missing__(key)" call. No other operations or methods invoke\n "__missing__()". If "__missing__()" is not defined, "KeyError"\n is raised. "__missing__()" must be a method; it cannot be an\n instance variable:\n\n >>> class Counter(dict):\n ... def __missing__(self, key):\n ... return 0\n >>> c = Counter()\n >>> c[\'red\']\n 0\n >>> c[\'red\'] += 1\n >>> c[\'red\']\n 1\n\n The example above shows part of the implementation of\n "collections.Counter". A different "__missing__" method is used\n by "collections.defaultdict".\n\n d[key] = value\n\n Set "d[key]" to *value*.\n\n del d[key]\n\n Remove "d[key]" from *d*. Raises a "KeyError" if *key* is not\n in the map.\n\n key in d\n\n Return "True" if *d* has a key *key*, else "False".\n\n key not in d\n\n Equivalent to "not key in d".\n\n iter(d)\n\n Return an iterator over the keys of the dictionary. This is a\n shortcut for "iter(d.keys())".\n\n clear()\n\n Remove all items from the dictionary.\n\n copy()\n\n Return a shallow copy of the dictionary.\n\n classmethod fromkeys(seq[, value])\n\n Create a new dictionary with keys from *seq* and values set to\n *value*.\n\n "fromkeys()" is a class method that returns a new dictionary.\n *value* defaults to "None".\n\n get(key[, default])\n\n Return the value for *key* if *key* is in the dictionary, else\n *default*. If *default* is not given, it defaults to "None", so\n that this method never raises a "KeyError".\n\n items()\n\n Return a new view of the dictionary\'s items ("(key, value)"\n pairs). See the *documentation of view objects*.\n\n keys()\n\n Return a new view of the dictionary\'s keys. See the\n *documentation of view objects*.\n\n pop(key[, default])\n\n If *key* is in the dictionary, remove it and return its value,\n else return *default*. If *default* is not given and *key* is\n not in the dictionary, a "KeyError" is raised.\n\n popitem()\n\n Remove and return an arbitrary "(key, value)" pair from the\n dictionary.\n\n "popitem()" is useful to destructively iterate over a\n dictionary, as often used in set algorithms. If the dictionary\n is empty, calling "popitem()" raises a "KeyError".\n\n setdefault(key[, default])\n\n If *key* is in the dictionary, return its value. If not, insert\n *key* with a value of *default* and return *default*. *default*\n defaults to "None".\n\n update([other])\n\n Update the dictionary with the key/value pairs from *other*,\n overwriting existing keys. Return "None".\n\n "update()" accepts either another dictionary object or an\n iterable of key/value pairs (as tuples or other iterables of\n length two). If keyword arguments are specified, the dictionary\n is then updated with those key/value pairs: "d.update(red=1,\n blue=2)".\n\n values()\n\n Return a new view of the dictionary\'s values. See the\n *documentation of view objects*.\n\n Dictionaries compare equal if and only if they have the same "(key,\n value)" pairs. Order comparisons (\'<\', \'<=\', \'>=\', \'>\') raise\n "TypeError".\n\nSee also: "types.MappingProxyType" can be used to create a read-only\n view of a "dict".\n\n\nDictionary view objects\n=======================\n\nThe objects returned by "dict.keys()", "dict.values()" and\n"dict.items()" are *view objects*. They provide a dynamic view on the\ndictionary\'s entries, which means that when the dictionary changes,\nthe view reflects these changes.\n\nDictionary views can be iterated over to yield their respective data,\nand support membership tests:\n\nlen(dictview)\n\n Return the number of entries in the dictionary.\n\niter(dictview)\n\n Return an iterator over the keys, values or items (represented as\n tuples of "(key, value)") in the dictionary.\n\n Keys and values are iterated over in an arbitrary order which is\n non-random, varies across Python implementations, and depends on\n the dictionary\'s history of insertions and deletions. If keys,\n values and items views are iterated over with no intervening\n modifications to the dictionary, the order of items will directly\n correspond. This allows the creation of "(value, key)" pairs using\n "zip()": "pairs = zip(d.values(), d.keys())". Another way to\n create the same list is "pairs = [(v, k) for (k, v) in d.items()]".\n\n Iterating views while adding or deleting entries in the dictionary\n may raise a "RuntimeError" or fail to iterate over all entries.\n\nx in dictview\n\n Return "True" if *x* is in the underlying dictionary\'s keys, values\n or items (in the latter case, *x* should be a "(key, value)"\n tuple).\n\nKeys views are set-like since their entries are unique and hashable.\nIf all values are hashable, so that "(key, value)" pairs are unique\nand hashable, then the items view is also set-like. (Values views are\nnot treated as set-like since the entries are generally not unique.)\nFor set-like views, all of the operations defined for the abstract\nbase class "collections.abc.Set" are available (for example, "==",\n"<", or "^").\n\nAn example of dictionary view usage:\n\n >>> dishes = {\'eggs\': 2, \'sausage\': 1, \'bacon\': 1, \'spam\': 500}\n >>> keys = dishes.keys()\n >>> values = dishes.values()\n\n >>> # iteration\n >>> n = 0\n >>> for val in values:\n ... n += val\n >>> print(n)\n 504\n\n >>> # keys and values are iterated over in the same order\n >>> list(keys)\n [\'eggs\', \'bacon\', \'sausage\', \'spam\']\n >>> list(values)\n [2, 1, 1, 500]\n\n >>> # view objects are dynamic and reflect dict changes\n >>> del dishes[\'eggs\']\n >>> del dishes[\'sausage\']\n >>> list(keys)\n [\'spam\', \'bacon\']\n\n >>> # set operations\n >>> keys & {\'eggs\', \'bacon\', \'salad\'}\n {\'bacon\'}\n >>> keys ^ {\'sausage\', \'juice\'}\n {\'juice\', \'sausage\', \'bacon\', \'spam\'}\n',
- 'typesmethods': u'\nMethods\n*******\n\nMethods are functions that are called using the attribute notation.\nThere are two flavors: built-in methods (such as "append()" on lists)\nand class instance methods. Built-in methods are described with the\ntypes that support them.\n\nIf you access a method (a function defined in a class namespace)\nthrough an instance, you get a special object: a *bound method* (also\ncalled *instance method*) object. When called, it will add the "self"\nargument to the argument list. Bound methods have two special read-\nonly attributes: "m.__self__" is the object on which the method\noperates, and "m.__func__" is the function implementing the method.\nCalling "m(arg-1, arg-2, ..., arg-n)" is completely equivalent to\ncalling "m.__func__(m.__self__, arg-1, arg-2, ..., arg-n)".\n\nLike function objects, bound method objects support getting arbitrary\nattributes. However, since method attributes are actually stored on\nthe underlying function object ("meth.__func__"), setting method\nattributes on bound methods is disallowed. Attempting to set an\nattribute on a method results in an "AttributeError" being raised. In\norder to set a method attribute, you need to explicitly set it on the\nunderlying function object:\n\n >>> class C:\n ... def method(self):\n ... pass\n ...\n >>> c = C()\n >>> c.method.whoami = \'my name is method\' # can\'t set on the method\n Traceback (most recent call last):\n File "<stdin>", line 1, in <module>\n AttributeError: \'method\' object has no attribute \'whoami\'\n >>> c.method.__func__.whoami = \'my name is method\'\n >>> c.method.whoami\n \'my name is method\'\n\nSee *The standard type hierarchy* for more information.\n',
- 'typesmodules': u'\nModules\n*******\n\nThe only special operation on a module is attribute access: "m.name",\nwhere *m* is a module and *name* accesses a name defined in *m*\'s\nsymbol table. Module attributes can be assigned to. (Note that the\n"import" statement is not, strictly speaking, an operation on a module\nobject; "import foo" does not require a module object named *foo* to\nexist, rather it requires an (external) *definition* for a module\nnamed *foo* somewhere.)\n\nA special attribute of every module is "__dict__". This is the\ndictionary containing the module\'s symbol table. Modifying this\ndictionary will actually change the module\'s symbol table, but direct\nassignment to the "__dict__" attribute is not possible (you can write\n"m.__dict__[\'a\'] = 1", which defines "m.a" to be "1", but you can\'t\nwrite "m.__dict__ = {}"). Modifying "__dict__" directly is not\nrecommended.\n\nModules built into the interpreter are written like this: "<module\n\'sys\' (built-in)>". If loaded from a file, they are written as\n"<module \'os\' from \'/usr/local/lib/pythonX.Y/os.pyc\'>".\n',
- 'typesseq': u'\nSequence Types --- "list", "tuple", "range"\n*******************************************\n\nThere are three basic sequence types: lists, tuples, and range\nobjects. Additional sequence types tailored for processing of *binary\ndata* and *text strings* are described in dedicated sections.\n\n\nCommon Sequence Operations\n==========================\n\nThe operations in the following table are supported by most sequence\ntypes, both mutable and immutable. The "collections.abc.Sequence" ABC\nis provided to make it easier to correctly implement these operations\non custom sequence types.\n\nThis table lists the sequence operations sorted in ascending priority.\nIn the table, *s* and *t* are sequences of the same type, *n*, *i*,\n*j* and *k* are integers and *x* is an arbitrary object that meets any\ntype and value restrictions imposed by *s*.\n\nThe "in" and "not in" operations have the same priorities as the\ncomparison operations. The "+" (concatenation) and "*" (repetition)\noperations have the same priority as the corresponding numeric\noperations.\n\n+----------------------------+----------------------------------+------------+\n| Operation | Result | Notes |\n+============================+==================================+============+\n| "x in s" | "True" if an item of *s* is | (1) |\n| | equal to *x*, else "False" | |\n+----------------------------+----------------------------------+------------+\n| "x not in s" | "False" if an item of *s* is | (1) |\n| | equal to *x*, else "True" | |\n+----------------------------+----------------------------------+------------+\n| "s + t" | the concatenation of *s* and *t* | (6)(7) |\n+----------------------------+----------------------------------+------------+\n| "s * n" or "n * s" | equivalent to adding *s* to | (2)(7) |\n| | itself *n* times | |\n+----------------------------+----------------------------------+------------+\n| "s[i]" | *i*th item of *s*, origin 0 | (3) |\n+----------------------------+----------------------------------+------------+\n| "s[i:j]" | slice of *s* from *i* to *j* | (3)(4) |\n+----------------------------+----------------------------------+------------+\n| "s[i:j:k]" | slice of *s* from *i* to *j* | (3)(5) |\n| | with step *k* | |\n+----------------------------+----------------------------------+------------+\n| "len(s)" | length of *s* | |\n+----------------------------+----------------------------------+------------+\n| "min(s)" | smallest item of *s* | |\n+----------------------------+----------------------------------+------------+\n| "max(s)" | largest item of *s* | |\n+----------------------------+----------------------------------+------------+\n| "s.index(x[, i[, j]])" | index of the first occurrence of | (8) |\n| | *x* in *s* (at or after index | |\n| | *i* and before index *j*) | |\n+----------------------------+----------------------------------+------------+\n| "s.count(x)" | total number of occurrences of | |\n| | *x* in *s* | |\n+----------------------------+----------------------------------+------------+\n\nSequences of the same type also support comparisons. In particular,\ntuples and lists are compared lexicographically by comparing\ncorresponding elements. This means that to compare equal, every\nelement must compare equal and the two sequences must be of the same\ntype and have the same length. (For full details see *Comparisons* in\nthe language reference.)\n\nNotes:\n\n1. While the "in" and "not in" operations are used only for simple\n containment testing in the general case, some specialised sequences\n (such as "str", "bytes" and "bytearray") also use them for\n subsequence testing:\n\n >>> "gg" in "eggs"\n True\n\n2. Values of *n* less than "0" are treated as "0" (which yields an\n empty sequence of the same type as *s*). Note that items in the\n sequence *s* are not copied; they are referenced multiple times.\n This often haunts new Python programmers; consider:\n\n >>> lists = [[]] * 3\n >>> lists\n [[], [], []]\n >>> lists[0].append(3)\n >>> lists\n [[3], [3], [3]]\n\n What has happened is that "[[]]" is a one-element list containing\n an empty list, so all three elements of "[[]] * 3" are references\n to this single empty list. Modifying any of the elements of\n "lists" modifies this single list. You can create a list of\n different lists this way:\n\n >>> lists = [[] for i in range(3)]\n >>> lists[0].append(3)\n >>> lists[1].append(5)\n >>> lists[2].append(7)\n >>> lists\n [[3], [5], [7]]\n\n Further explanation is available in the FAQ entry *How do I create\n a multidimensional list?*.\n\n3. If *i* or *j* is negative, the index is relative to the end of\n sequence *s*: "len(s) + i" or "len(s) + j" is substituted. But\n note that "-0" is still "0".\n\n4. The slice of *s* from *i* to *j* is defined as the sequence of\n items with index *k* such that "i <= k < j". If *i* or *j* is\n greater than "len(s)", use "len(s)". If *i* is omitted or "None",\n use "0". If *j* is omitted or "None", use "len(s)". If *i* is\n greater than or equal to *j*, the slice is empty.\n\n5. The slice of *s* from *i* to *j* with step *k* is defined as the\n sequence of items with index "x = i + n*k" such that "0 <= n <\n (j-i)/k". In other words, the indices are "i", "i+k", "i+2*k",\n "i+3*k" and so on, stopping when *j* is reached (but never\n including *j*). When *k* is positive, *i* and *j* are reduced to\n "len(s)" if they are greater. When *k* is negative, *i* and *j* are\n reduced to "len(s) - 1" if they are greater. If *i* or *j* are\n omitted or "None", they become "end" values (which end depends on\n the sign of *k*). Note, *k* cannot be zero. If *k* is "None", it\n is treated like "1".\n\n6. Concatenating immutable sequences always results in a new\n object. This means that building up a sequence by repeated\n concatenation will have a quadratic runtime cost in the total\n sequence length. To get a linear runtime cost, you must switch to\n one of the alternatives below:\n\n * if concatenating "str" objects, you can build a list and use\n "str.join()" at the end or else write to an "io.StringIO"\n instance and retrieve its value when complete\n\n * if concatenating "bytes" objects, you can similarly use\n "bytes.join()" or "io.BytesIO", or you can do in-place\n concatenation with a "bytearray" object. "bytearray" objects are\n mutable and have an efficient overallocation mechanism\n\n * if concatenating "tuple" objects, extend a "list" instead\n\n * for other types, investigate the relevant class documentation\n\n7. Some sequence types (such as "range") only support item\n sequences that follow specific patterns, and hence don\'t support\n sequence concatenation or repetition.\n\n8. "index" raises "ValueError" when *x* is not found in *s*. When\n supported, the additional arguments to the index method allow\n efficient searching of subsections of the sequence. Passing the\n extra arguments is roughly equivalent to using "s[i:j].index(x)",\n only without copying any data and with the returned index being\n relative to the start of the sequence rather than the start of the\n slice.\n\n\nImmutable Sequence Types\n========================\n\nThe only operation that immutable sequence types generally implement\nthat is not also implemented by mutable sequence types is support for\nthe "hash()" built-in.\n\nThis support allows immutable sequences, such as "tuple" instances, to\nbe used as "dict" keys and stored in "set" and "frozenset" instances.\n\nAttempting to hash an immutable sequence that contains unhashable\nvalues will result in "TypeError".\n\n\nMutable Sequence Types\n======================\n\nThe operations in the following table are defined on mutable sequence\ntypes. The "collections.abc.MutableSequence" ABC is provided to make\nit easier to correctly implement these operations on custom sequence\ntypes.\n\nIn the table *s* is an instance of a mutable sequence type, *t* is any\niterable object and *x* is an arbitrary object that meets any type and\nvalue restrictions imposed by *s* (for example, "bytearray" only\naccepts integers that meet the value restriction "0 <= x <= 255").\n\n+--------------------------------+----------------------------------+-----------------------+\n| Operation | Result | Notes |\n+================================+==================================+=======================+\n| "s[i] = x" | item *i* of *s* is replaced by | |\n| | *x* | |\n+--------------------------------+----------------------------------+-----------------------+\n| "s[i:j] = t" | slice of *s* from *i* to *j* is | |\n| | replaced by the contents of the | |\n| | iterable *t* | |\n+--------------------------------+----------------------------------+-----------------------+\n| "del s[i:j]" | same as "s[i:j] = []" | |\n+--------------------------------+----------------------------------+-----------------------+\n| "s[i:j:k] = t" | the elements of "s[i:j:k]" are | (1) |\n| | replaced by those of *t* | |\n+--------------------------------+----------------------------------+-----------------------+\n| "del s[i:j:k]" | removes the elements of | |\n| | "s[i:j:k]" from the list | |\n+--------------------------------+----------------------------------+-----------------------+\n| "s.append(x)" | appends *x* to the end of the | |\n| | sequence (same as | |\n| | "s[len(s):len(s)] = [x]") | |\n+--------------------------------+----------------------------------+-----------------------+\n| "s.clear()" | removes all items from "s" (same | (5) |\n| | as "del s[:]") | |\n+--------------------------------+----------------------------------+-----------------------+\n| "s.copy()" | creates a shallow copy of "s" | (5) |\n| | (same as "s[:]") | |\n+--------------------------------+----------------------------------+-----------------------+\n| "s.extend(t)" or "s += t" | extends *s* with the contents of | |\n| | *t* (for the most part the same | |\n| | as "s[len(s):len(s)] = t") | |\n+--------------------------------+----------------------------------+-----------------------+\n| "s *= n" | updates *s* with its contents | (6) |\n| | repeated *n* times | |\n+--------------------------------+----------------------------------+-----------------------+\n| "s.insert(i, x)" | inserts *x* into *s* at the | |\n| | index given by *i* (same as | |\n| | "s[i:i] = [x]") | |\n+--------------------------------+----------------------------------+-----------------------+\n| "s.pop([i])" | retrieves the item at *i* and | (2) |\n| | also removes it from *s* | |\n+--------------------------------+----------------------------------+-----------------------+\n| "s.remove(x)" | remove the first item from *s* | (3) |\n| | where "s[i] == x" | |\n+--------------------------------+----------------------------------+-----------------------+\n| "s.reverse()" | reverses the items of *s* in | (4) |\n| | place | |\n+--------------------------------+----------------------------------+-----------------------+\n\nNotes:\n\n1. *t* must have the same length as the slice it is replacing.\n\n2. The optional argument *i* defaults to "-1", so that by default\n the last item is removed and returned.\n\n3. "remove" raises "ValueError" when *x* is not found in *s*.\n\n4. The "reverse()" method modifies the sequence in place for\n economy of space when reversing a large sequence. To remind users\n that it operates by side effect, it does not return the reversed\n sequence.\n\n5. "clear()" and "copy()" are included for consistency with the\n interfaces of mutable containers that don\'t support slicing\n operations (such as "dict" and "set")\n\n New in version 3.3: "clear()" and "copy()" methods.\n\n6. The value *n* is an integer, or an object implementing\n "__index__()". Zero and negative values of *n* clear the sequence.\n Items in the sequence are not copied; they are referenced multiple\n times, as explained for "s * n" under *Common Sequence Operations*.\n\n\nLists\n=====\n\nLists are mutable sequences, typically used to store collections of\nhomogeneous items (where the precise degree of similarity will vary by\napplication).\n\nclass class list([iterable])\n\n Lists may be constructed in several ways:\n\n * Using a pair of square brackets to denote the empty list: "[]"\n\n * Using square brackets, separating items with commas: "[a]",\n "[a, b, c]"\n\n * Using a list comprehension: "[x for x in iterable]"\n\n * Using the type constructor: "list()" or "list(iterable)"\n\n The constructor builds a list whose items are the same and in the\n same order as *iterable*\'s items. *iterable* may be either a\n sequence, a container that supports iteration, or an iterator\n object. If *iterable* is already a list, a copy is made and\n returned, similar to "iterable[:]". For example, "list(\'abc\')"\n returns "[\'a\', \'b\', \'c\']" and "list( (1, 2, 3) )" returns "[1, 2,\n 3]". If no argument is given, the constructor creates a new empty\n list, "[]".\n\n Many other operations also produce lists, including the "sorted()"\n built-in.\n\n Lists implement all of the *common* and *mutable* sequence\n operations. Lists also provide the following additional method:\n\n sort(*, key=None, reverse=None)\n\n This method sorts the list in place, using only "<" comparisons\n between items. Exceptions are not suppressed - if any comparison\n operations fail, the entire sort operation will fail (and the\n list will likely be left in a partially modified state).\n\n "sort()" accepts two arguments that can only be passed by\n keyword (*keyword-only arguments*):\n\n *key* specifies a function of one argument that is used to\n extract a comparison key from each list element (for example,\n "key=str.lower"). The key corresponding to each item in the list\n is calculated once and then used for the entire sorting process.\n The default value of "None" means that list items are sorted\n directly without calculating a separate key value.\n\n The "functools.cmp_to_key()" utility is available to convert a\n 2.x style *cmp* function to a *key* function.\n\n *reverse* is a boolean value. If set to "True", then the list\n elements are sorted as if each comparison were reversed.\n\n This method modifies the sequence in place for economy of space\n when sorting a large sequence. To remind users that it operates\n by side effect, it does not return the sorted sequence (use\n "sorted()" to explicitly request a new sorted list instance).\n\n The "sort()" method is guaranteed to be stable. A sort is\n stable if it guarantees not to change the relative order of\n elements that compare equal --- this is helpful for sorting in\n multiple passes (for example, sort by department, then by salary\n grade).\n\n **CPython implementation detail:** While a list is being sorted,\n the effect of attempting to mutate, or even inspect, the list is\n undefined. The C implementation of Python makes the list appear\n empty for the duration, and raises "ValueError" if it can detect\n that the list has been mutated during a sort.\n\n\nTuples\n======\n\nTuples are immutable sequences, typically used to store collections of\nheterogeneous data (such as the 2-tuples produced by the "enumerate()"\nbuilt-in). Tuples are also used for cases where an immutable sequence\nof homogeneous data is needed (such as allowing storage in a "set" or\n"dict" instance).\n\nclass class tuple([iterable])\n\n Tuples may be constructed in a number of ways:\n\n * Using a pair of parentheses to denote the empty tuple: "()"\n\n * Using a trailing comma for a singleton tuple: "a," or "(a,)"\n\n * Separating items with commas: "a, b, c" or "(a, b, c)"\n\n * Using the "tuple()" built-in: "tuple()" or "tuple(iterable)"\n\n The constructor builds a tuple whose items are the same and in the\n same order as *iterable*\'s items. *iterable* may be either a\n sequence, a container that supports iteration, or an iterator\n object. If *iterable* is already a tuple, it is returned\n unchanged. For example, "tuple(\'abc\')" returns "(\'a\', \'b\', \'c\')"\n and "tuple( [1, 2, 3] )" returns "(1, 2, 3)". If no argument is\n given, the constructor creates a new empty tuple, "()".\n\n Note that it is actually the comma which makes a tuple, not the\n parentheses. The parentheses are optional, except in the empty\n tuple case, or when they are needed to avoid syntactic ambiguity.\n For example, "f(a, b, c)" is a function call with three arguments,\n while "f((a, b, c))" is a function call with a 3-tuple as the sole\n argument.\n\n Tuples implement all of the *common* sequence operations.\n\nFor heterogeneous collections of data where access by name is clearer\nthan access by index, "collections.namedtuple()" may be a more\nappropriate choice than a simple tuple object.\n\n\nRanges\n======\n\nThe "range" type represents an immutable sequence of numbers and is\ncommonly used for looping a specific number of times in "for" loops.\n\nclass class range(stop)\nclass class range(start, stop[, step])\n\n The arguments to the range constructor must be integers (either\n built-in "int" or any object that implements the "__index__"\n special method). If the *step* argument is omitted, it defaults to\n "1". If the *start* argument is omitted, it defaults to "0". If\n *step* is zero, "ValueError" is raised.\n\n For a positive *step*, the contents of a range "r" are determined\n by the formula "r[i] = start + step*i" where "i >= 0" and "r[i] <\n stop".\n\n For a negative *step*, the contents of the range are still\n determined by the formula "r[i] = start + step*i", but the\n constraints are "i >= 0" and "r[i] > stop".\n\n A range object will be empty if "r[0]" does not meet the value\n constraint. Ranges do support negative indices, but these are\n interpreted as indexing from the end of the sequence determined by\n the positive indices.\n\n Ranges containing absolute values larger than "sys.maxsize" are\n permitted but some features (such as "len()") may raise\n "OverflowError".\n\n Range examples:\n\n >>> list(range(10))\n [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n >>> list(range(1, 11))\n [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n >>> list(range(0, 30, 5))\n [0, 5, 10, 15, 20, 25]\n >>> list(range(0, 10, 3))\n [0, 3, 6, 9]\n >>> list(range(0, -10, -1))\n [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]\n >>> list(range(0))\n []\n >>> list(range(1, 0))\n []\n\n Ranges implement all of the *common* sequence operations except\n concatenation and repetition (due to the fact that range objects\n can only represent sequences that follow a strict pattern and\n repetition and concatenation will usually violate that pattern).\n\n start\n\n The value of the *start* parameter (or "0" if the parameter was\n not supplied)\n\n stop\n\n The value of the *stop* parameter\n\n step\n\n The value of the *step* parameter (or "1" if the parameter was\n not supplied)\n\nThe advantage of the "range" type over a regular "list" or "tuple" is\nthat a "range" object will always take the same (small) amount of\nmemory, no matter the size of the range it represents (as it only\nstores the "start", "stop" and "step" values, calculating individual\nitems and subranges as needed).\n\nRange objects implement the "collections.abc.Sequence" ABC, and\nprovide features such as containment tests, element index lookup,\nslicing and support for negative indices (see *Sequence Types ---\nlist, tuple, range*):\n\n>>> r = range(0, 20, 2)\n>>> r\nrange(0, 20, 2)\n>>> 11 in r\nFalse\n>>> 10 in r\nTrue\n>>> r.index(10)\n5\n>>> r[5]\n10\n>>> r[:5]\nrange(0, 10, 2)\n>>> r[-1]\n18\n\nTesting range objects for equality with "==" and "!=" compares them as\nsequences. That is, two range objects are considered equal if they\nrepresent the same sequence of values. (Note that two range objects\nthat compare equal might have different "start", "stop" and "step"\nattributes, for example "range(0) == range(2, 1, 3)" or "range(0, 3,\n2) == range(0, 4, 2)".)\n\nChanged in version 3.2: Implement the Sequence ABC. Support slicing\nand negative indices. Test "int" objects for membership in constant\ntime instead of iterating through all items.\n\nChanged in version 3.3: Define \'==\' and \'!=\' to compare range objects\nbased on the sequence of values they define (instead of comparing\nbased on object identity).\n\nNew in version 3.3: The "start", "stop" and "step" attributes.\n',
- 'typesseq-mutable': u'\nMutable Sequence Types\n**********************\n\nThe operations in the following table are defined on mutable sequence\ntypes. The "collections.abc.MutableSequence" ABC is provided to make\nit easier to correctly implement these operations on custom sequence\ntypes.\n\nIn the table *s* is an instance of a mutable sequence type, *t* is any\niterable object and *x* is an arbitrary object that meets any type and\nvalue restrictions imposed by *s* (for example, "bytearray" only\naccepts integers that meet the value restriction "0 <= x <= 255").\n\n+--------------------------------+----------------------------------+-----------------------+\n| Operation | Result | Notes |\n+================================+==================================+=======================+\n| "s[i] = x" | item *i* of *s* is replaced by | |\n| | *x* | |\n+--------------------------------+----------------------------------+-----------------------+\n| "s[i:j] = t" | slice of *s* from *i* to *j* is | |\n| | replaced by the contents of the | |\n| | iterable *t* | |\n+--------------------------------+----------------------------------+-----------------------+\n| "del s[i:j]" | same as "s[i:j] = []" | |\n+--------------------------------+----------------------------------+-----------------------+\n| "s[i:j:k] = t" | the elements of "s[i:j:k]" are | (1) |\n| | replaced by those of *t* | |\n+--------------------------------+----------------------------------+-----------------------+\n| "del s[i:j:k]" | removes the elements of | |\n| | "s[i:j:k]" from the list | |\n+--------------------------------+----------------------------------+-----------------------+\n| "s.append(x)" | appends *x* to the end of the | |\n| | sequence (same as | |\n| | "s[len(s):len(s)] = [x]") | |\n+--------------------------------+----------------------------------+-----------------------+\n| "s.clear()" | removes all items from "s" (same | (5) |\n| | as "del s[:]") | |\n+--------------------------------+----------------------------------+-----------------------+\n| "s.copy()" | creates a shallow copy of "s" | (5) |\n| | (same as "s[:]") | |\n+--------------------------------+----------------------------------+-----------------------+\n| "s.extend(t)" or "s += t" | extends *s* with the contents of | |\n| | *t* (for the most part the same | |\n| | as "s[len(s):len(s)] = t") | |\n+--------------------------------+----------------------------------+-----------------------+\n| "s *= n" | updates *s* with its contents | (6) |\n| | repeated *n* times | |\n+--------------------------------+----------------------------------+-----------------------+\n| "s.insert(i, x)" | inserts *x* into *s* at the | |\n| | index given by *i* (same as | |\n| | "s[i:i] = [x]") | |\n+--------------------------------+----------------------------------+-----------------------+\n| "s.pop([i])" | retrieves the item at *i* and | (2) |\n| | also removes it from *s* | |\n+--------------------------------+----------------------------------+-----------------------+\n| "s.remove(x)" | remove the first item from *s* | (3) |\n| | where "s[i] == x" | |\n+--------------------------------+----------------------------------+-----------------------+\n| "s.reverse()" | reverses the items of *s* in | (4) |\n| | place | |\n+--------------------------------+----------------------------------+-----------------------+\n\nNotes:\n\n1. *t* must have the same length as the slice it is replacing.\n\n2. The optional argument *i* defaults to "-1", so that by default\n the last item is removed and returned.\n\n3. "remove" raises "ValueError" when *x* is not found in *s*.\n\n4. The "reverse()" method modifies the sequence in place for\n economy of space when reversing a large sequence. To remind users\n that it operates by side effect, it does not return the reversed\n sequence.\n\n5. "clear()" and "copy()" are included for consistency with the\n interfaces of mutable containers that don\'t support slicing\n operations (such as "dict" and "set")\n\n New in version 3.3: "clear()" and "copy()" methods.\n\n6. The value *n* is an integer, or an object implementing\n "__index__()". Zero and negative values of *n* clear the sequence.\n Items in the sequence are not copied; they are referenced multiple\n times, as explained for "s * n" under *Common Sequence Operations*.\n',
- 'unary': u'\nUnary arithmetic and bitwise operations\n***************************************\n\nAll unary arithmetic and bitwise operations have the same priority:\n\n u_expr ::= power | "-" u_expr | "+" u_expr | "~" u_expr\n\nThe unary "-" (minus) operator yields the negation of its numeric\nargument.\n\nThe unary "+" (plus) operator yields its numeric argument unchanged.\n\nThe unary "~" (invert) operator yields the bitwise inversion of its\ninteger argument. The bitwise inversion of "x" is defined as\n"-(x+1)". It only applies to integral numbers.\n\nIn all three cases, if the argument does not have the proper type, a\n"TypeError" exception is raised.\n',
- 'while': u'\nThe "while" statement\n*********************\n\nThe "while" statement is used for repeated execution as long as an\nexpression is true:\n\n while_stmt ::= "while" expression ":" suite\n ["else" ":" suite]\n\nThis repeatedly tests the expression and, if it is true, executes the\nfirst suite; if the expression is false (which may be the first time\nit is tested) the suite of the "else" clause, if present, is executed\nand the loop terminates.\n\nA "break" statement executed in the first suite terminates the loop\nwithout executing the "else" clause\'s suite. A "continue" statement\nexecuted in the first suite skips the rest of the suite and goes back\nto testing the expression.\n',
- 'with': u'\nThe "with" statement\n********************\n\nThe "with" statement is used to wrap the execution of a block with\nmethods defined by a context manager (see section *With Statement\nContext Managers*). This allows common "try"..."except"..."finally"\nusage patterns to be encapsulated for convenient reuse.\n\n with_stmt ::= "with" with_item ("," with_item)* ":" suite\n with_item ::= expression ["as" target]\n\nThe execution of the "with" statement with one "item" proceeds as\nfollows:\n\n1. The context expression (the expression given in the "with_item")\n is evaluated to obtain a context manager.\n\n2. The context manager\'s "__exit__()" is loaded for later use.\n\n3. The context manager\'s "__enter__()" method is invoked.\n\n4. If a target was included in the "with" statement, the return\n value from "__enter__()" is assigned to it.\n\n Note: The "with" statement guarantees that if the "__enter__()"\n method returns without an error, then "__exit__()" will always be\n called. Thus, if an error occurs during the assignment to the\n target list, it will be treated the same as an error occurring\n within the suite would be. See step 6 below.\n\n5. The suite is executed.\n\n6. The context manager\'s "__exit__()" method is invoked. If an\n exception caused the suite to be exited, its type, value, and\n traceback are passed as arguments to "__exit__()". Otherwise, three\n "None" arguments are supplied.\n\n If the suite was exited due to an exception, and the return value\n from the "__exit__()" method was false, the exception is reraised.\n If the return value was true, the exception is suppressed, and\n execution continues with the statement following the "with"\n statement.\n\n If the suite was exited for any reason other than an exception, the\n return value from "__exit__()" is ignored, and execution proceeds\n at the normal location for the kind of exit that was taken.\n\nWith more than one item, the context managers are processed as if\nmultiple "with" statements were nested:\n\n with A() as a, B() as b:\n suite\n\nis equivalent to\n\n with A() as a:\n with B() as b:\n suite\n\nChanged in version 3.1: Support for multiple context expressions.\n\nSee also: **PEP 343** - The "with" statement\n\n The specification, background, and examples for the Python "with"\n statement.\n',
- 'yield': u'\nThe "yield" statement\n*********************\n\n yield_stmt ::= yield_expression\n\nA "yield" statement is semantically equivalent to a *yield\nexpression*. The yield statement can be used to omit the parentheses\nthat would otherwise be required in the equivalent yield expression\nstatement. For example, the yield statements\n\n yield <expr>\n yield from <expr>\n\nare equivalent to the yield expression statements\n\n (yield <expr>)\n (yield from <expr>)\n\nYield expressions and statements are only used when defining a\n*generator* function, and are only used in the body of the generator\nfunction. Using yield in a function definition is sufficient to cause\nthat definition to create a generator function instead of a normal\nfunction.\n\nFor full details of "yield" semantics, refer to the *Yield\nexpressions* section.\n'}
+# Autogenerated by Sphinx on Tue Dec 6 18:51:51 2016
+topics = {'assert': '\n'
+ 'The "assert" statement\n'
+ '**********************\n'
+ '\n'
+ 'Assert statements are a convenient way to insert debugging '
+ 'assertions\n'
+ 'into a program:\n'
+ '\n'
+ ' assert_stmt ::= "assert" expression ["," expression]\n'
+ '\n'
+ 'The simple form, "assert expression", is equivalent to\n'
+ '\n'
+ ' if __debug__:\n'
+ ' if not expression: raise AssertionError\n'
+ '\n'
+ 'The extended form, "assert expression1, expression2", is '
+ 'equivalent to\n'
+ '\n'
+ ' if __debug__:\n'
+ ' if not expression1: raise AssertionError(expression2)\n'
+ '\n'
+ 'These equivalences assume that "__debug__" and "AssertionError" '
+ 'refer\n'
+ 'to the built-in variables with those names. In the current\n'
+ 'implementation, the built-in variable "__debug__" is "True" under\n'
+ 'normal circumstances, "False" when optimization is requested '
+ '(command\n'
+ 'line option -O). The current code generator emits no code for an\n'
+ 'assert statement when optimization is requested at compile time. '
+ 'Note\n'
+ 'that it is unnecessary to include the source code for the '
+ 'expression\n'
+ 'that failed in the error message; it will be displayed as part of '
+ 'the\n'
+ 'stack trace.\n'
+ '\n'
+ 'Assignments to "__debug__" are illegal. The value for the '
+ 'built-in\n'
+ 'variable is determined when the interpreter starts.\n',
+ 'assignment': '\n'
+ 'Assignment statements\n'
+ '*********************\n'
+ '\n'
+ 'Assignment statements are used to (re)bind names to values and '
+ 'to\n'
+ 'modify attributes or items of mutable objects:\n'
+ '\n'
+ ' assignment_stmt ::= (target_list "=")+ (starred_expression '
+ '| yield_expression)\n'
+ ' target_list ::= target ("," target)* [","]\n'
+ ' target ::= identifier\n'
+ ' | "(" [target_list] ")"\n'
+ ' | "[" [target_list] "]"\n'
+ ' | attributeref\n'
+ ' | subscription\n'
+ ' | slicing\n'
+ ' | "*" target\n'
+ '\n'
+ '(See section Primaries for the syntax definitions for '
+ '*attributeref*,\n'
+ '*subscription*, and *slicing*.)\n'
+ '\n'
+ 'An assignment statement evaluates the expression list '
+ '(remember that\n'
+ 'this can be a single expression or a comma-separated list, the '
+ 'latter\n'
+ 'yielding a tuple) and assigns the single resulting object to '
+ 'each of\n'
+ 'the target lists, from left to right.\n'
+ '\n'
+ 'Assignment is defined recursively depending on the form of the '
+ 'target\n'
+ '(list). When a target is part of a mutable object (an '
+ 'attribute\n'
+ 'reference, subscription or slicing), the mutable object must\n'
+ 'ultimately perform the assignment and decide about its '
+ 'validity, and\n'
+ 'may raise an exception if the assignment is unacceptable. The '
+ 'rules\n'
+ 'observed by various types and the exceptions raised are given '
+ 'with the\n'
+ 'definition of the object types (see section The standard type\n'
+ 'hierarchy).\n'
+ '\n'
+ 'Assignment of an object to a target list, optionally enclosed '
+ 'in\n'
+ 'parentheses or square brackets, is recursively defined as '
+ 'follows.\n'
+ '\n'
+ '* If the target list is empty: The object must also be an '
+ 'empty\n'
+ ' iterable.\n'
+ '\n'
+ '* If the target list is a single target in parentheses: The '
+ 'object\n'
+ ' is assigned to that target.\n'
+ '\n'
+ '* If the target list is a comma-separated list of targets, or '
+ 'a\n'
+ ' single target in square brackets: The object must be an '
+ 'iterable\n'
+ ' with the same number of items as there are targets in the '
+ 'target\n'
+ ' list, and the items are assigned, from left to right, to '
+ 'the\n'
+ ' corresponding targets.\n'
+ '\n'
+ ' * If the target list contains one target prefixed with an\n'
+ ' asterisk, called a "starred" target: The object must be '
+ 'an\n'
+ ' iterable with at least as many items as there are targets '
+ 'in the\n'
+ ' target list, minus one. The first items of the iterable '
+ 'are\n'
+ ' assigned, from left to right, to the targets before the '
+ 'starred\n'
+ ' target. The final items of the iterable are assigned to '
+ 'the\n'
+ ' targets after the starred target. A list of the remaining '
+ 'items\n'
+ ' in the iterable is then assigned to the starred target '
+ '(the list\n'
+ ' can be empty).\n'
+ '\n'
+ ' * Else: The object must be an iterable with the same number '
+ 'of\n'
+ ' items as there are targets in the target list, and the '
+ 'items are\n'
+ ' assigned, from left to right, to the corresponding '
+ 'targets.\n'
+ '\n'
+ 'Assignment of an object to a single target is recursively '
+ 'defined as\n'
+ 'follows.\n'
+ '\n'
+ '* If the target is an identifier (name):\n'
+ '\n'
+ ' * If the name does not occur in a "global" or "nonlocal" '
+ 'statement\n'
+ ' in the current code block: the name is bound to the object '
+ 'in the\n'
+ ' current local namespace.\n'
+ '\n'
+ ' * Otherwise: the name is bound to the object in the global\n'
+ ' namespace or the outer namespace determined by '
+ '"nonlocal",\n'
+ ' respectively.\n'
+ '\n'
+ ' The name is rebound if it was already bound. This may cause '
+ 'the\n'
+ ' reference count for the object previously bound to the name '
+ 'to reach\n'
+ ' zero, causing the object to be deallocated and its '
+ 'destructor (if it\n'
+ ' has one) to be called.\n'
+ '\n'
+ '* If the target is an attribute reference: The primary '
+ 'expression in\n'
+ ' the reference is evaluated. It should yield an object with\n'
+ ' assignable attributes; if this is not the case, "TypeError" '
+ 'is\n'
+ ' raised. That object is then asked to assign the assigned '
+ 'object to\n'
+ ' the given attribute; if it cannot perform the assignment, it '
+ 'raises\n'
+ ' an exception (usually but not necessarily '
+ '"AttributeError").\n'
+ '\n'
+ ' Note: If the object is a class instance and the attribute '
+ 'reference\n'
+ ' occurs on both sides of the assignment operator, the RHS '
+ 'expression,\n'
+ ' "a.x" can access either an instance attribute or (if no '
+ 'instance\n'
+ ' attribute exists) a class attribute. The LHS target "a.x" '
+ 'is always\n'
+ ' set as an instance attribute, creating it if necessary. '
+ 'Thus, the\n'
+ ' two occurrences of "a.x" do not necessarily refer to the '
+ 'same\n'
+ ' attribute: if the RHS expression refers to a class '
+ 'attribute, the\n'
+ ' LHS creates a new instance attribute as the target of the\n'
+ ' assignment:\n'
+ '\n'
+ ' class Cls:\n'
+ ' x = 3 # class variable\n'
+ ' inst = Cls()\n'
+ ' inst.x = inst.x + 1 # writes inst.x as 4 leaving Cls.x '
+ 'as 3\n'
+ '\n'
+ ' This description does not necessarily apply to descriptor\n'
+ ' attributes, such as properties created with "property()".\n'
+ '\n'
+ '* If the target is a subscription: The primary expression in '
+ 'the\n'
+ ' reference is evaluated. It should yield either a mutable '
+ 'sequence\n'
+ ' object (such as a list) or a mapping object (such as a '
+ 'dictionary).\n'
+ ' Next, the subscript expression is evaluated.\n'
+ '\n'
+ ' If the primary is a mutable sequence object (such as a '
+ 'list), the\n'
+ ' subscript must yield an integer. If it is negative, the '
+ "sequence's\n"
+ ' length is added to it. The resulting value must be a '
+ 'nonnegative\n'
+ " integer less than the sequence's length, and the sequence is "
+ 'asked\n'
+ ' to assign the assigned object to its item with that index. '
+ 'If the\n'
+ ' index is out of range, "IndexError" is raised (assignment to '
+ 'a\n'
+ ' subscripted sequence cannot add new items to a list).\n'
+ '\n'
+ ' If the primary is a mapping object (such as a dictionary), '
+ 'the\n'
+ " subscript must have a type compatible with the mapping's key "
+ 'type,\n'
+ ' and the mapping is then asked to create a key/datum pair '
+ 'which maps\n'
+ ' the subscript to the assigned object. This can either '
+ 'replace an\n'
+ ' existing key/value pair with the same key value, or insert a '
+ 'new\n'
+ ' key/value pair (if no key with the same value existed).\n'
+ '\n'
+ ' For user-defined objects, the "__setitem__()" method is '
+ 'called with\n'
+ ' appropriate arguments.\n'
+ '\n'
+ '* If the target is a slicing: The primary expression in the\n'
+ ' reference is evaluated. It should yield a mutable sequence '
+ 'object\n'
+ ' (such as a list). The assigned object should be a sequence '
+ 'object\n'
+ ' of the same type. Next, the lower and upper bound '
+ 'expressions are\n'
+ ' evaluated, insofar they are present; defaults are zero and '
+ 'the\n'
+ " sequence's length. The bounds should evaluate to integers. "
+ 'If\n'
+ " either bound is negative, the sequence's length is added to "
+ 'it. The\n'
+ ' resulting bounds are clipped to lie between zero and the '
+ "sequence's\n"
+ ' length, inclusive. Finally, the sequence object is asked to '
+ 'replace\n'
+ ' the slice with the items of the assigned sequence. The '
+ 'length of\n'
+ ' the slice may be different from the length of the assigned '
+ 'sequence,\n'
+ ' thus changing the length of the target sequence, if the '
+ 'target\n'
+ ' sequence allows it.\n'
+ '\n'
+ '**CPython implementation detail:** In the current '
+ 'implementation, the\n'
+ 'syntax for targets is taken to be the same as for expressions, '
+ 'and\n'
+ 'invalid syntax is rejected during the code generation phase, '
+ 'causing\n'
+ 'less detailed error messages.\n'
+ '\n'
+ 'Although the definition of assignment implies that overlaps '
+ 'between\n'
+ "the left-hand side and the right-hand side are 'simultaneous' "
+ '(for\n'
+ 'example "a, b = b, a" swaps two variables), overlaps *within* '
+ 'the\n'
+ 'collection of assigned-to variables occur left-to-right, '
+ 'sometimes\n'
+ 'resulting in confusion. For instance, the following program '
+ 'prints\n'
+ '"[0, 2]":\n'
+ '\n'
+ ' x = [0, 1]\n'
+ ' i = 0\n'
+ ' i, x[i] = 1, 2 # i is updated, then x[i] is '
+ 'updated\n'
+ ' print(x)\n'
+ '\n'
+ 'See also:\n'
+ '\n'
+ ' **PEP 3132** - Extended Iterable Unpacking\n'
+ ' The specification for the "*target" feature.\n'
+ '\n'
+ '\n'
+ 'Augmented assignment statements\n'
+ '===============================\n'
+ '\n'
+ 'Augmented assignment is the combination, in a single '
+ 'statement, of a\n'
+ 'binary operation and an assignment statement:\n'
+ '\n'
+ ' augmented_assignment_stmt ::= augtarget augop '
+ '(expression_list | yield_expression)\n'
+ ' augtarget ::= identifier | attributeref | '
+ 'subscription | slicing\n'
+ ' augop ::= "+=" | "-=" | "*=" | "@=" | '
+ '"/=" | "//=" | "%=" | "**="\n'
+ ' | ">>=" | "<<=" | "&=" | "^=" | "|="\n'
+ '\n'
+ '(See section Primaries for the syntax definitions of the last '
+ 'three\n'
+ 'symbols.)\n'
+ '\n'
+ 'An augmented assignment evaluates the target (which, unlike '
+ 'normal\n'
+ 'assignment statements, cannot be an unpacking) and the '
+ 'expression\n'
+ 'list, performs the binary operation specific to the type of '
+ 'assignment\n'
+ 'on the two operands, and assigns the result to the original '
+ 'target.\n'
+ 'The target is only evaluated once.\n'
+ '\n'
+ 'An augmented assignment expression like "x += 1" can be '
+ 'rewritten as\n'
+ '"x = x + 1" to achieve a similar, but not exactly equal '
+ 'effect. In the\n'
+ 'augmented version, "x" is only evaluated once. Also, when '
+ 'possible,\n'
+ 'the actual operation is performed *in-place*, meaning that '
+ 'rather than\n'
+ 'creating a new object and assigning that to the target, the '
+ 'old object\n'
+ 'is modified instead.\n'
+ '\n'
+ 'Unlike normal assignments, augmented assignments evaluate the '
+ 'left-\n'
+ 'hand side *before* evaluating the right-hand side. For '
+ 'example, "a[i]\n'
+ '+= f(x)" first looks-up "a[i]", then it evaluates "f(x)" and '
+ 'performs\n'
+ 'the addition, and lastly, it writes the result back to '
+ '"a[i]".\n'
+ '\n'
+ 'With the exception of assigning to tuples and multiple targets '
+ 'in a\n'
+ 'single statement, the assignment done by augmented assignment\n'
+ 'statements is handled the same way as normal assignments. '
+ 'Similarly,\n'
+ 'with the exception of the possible *in-place* behavior, the '
+ 'binary\n'
+ 'operation performed by augmented assignment is the same as the '
+ 'normal\n'
+ 'binary operations.\n'
+ '\n'
+ 'For targets which are attribute references, the same caveat '
+ 'about\n'
+ 'class and instance attributes applies as for regular '
+ 'assignments.\n'
+ '\n'
+ '\n'
+ 'Annotated assignment statements\n'
+ '===============================\n'
+ '\n'
+ 'Annotation assignment is the combination, in a single '
+ 'statement, of a\n'
+ 'variable or attribute annotation and an optional assignment '
+ 'statement:\n'
+ '\n'
+ ' annotated_assignment_stmt ::= augtarget ":" expression ["=" '
+ 'expression]\n'
+ '\n'
+ 'The difference from normal Assignment statements is that only '
+ 'single\n'
+ 'target and only single right hand side value is allowed.\n'
+ '\n'
+ 'For simple names as assignment targets, if in class or module '
+ 'scope,\n'
+ 'the annotations are evaluated and stored in a special class or '
+ 'module\n'
+ 'attribute "__annotations__" that is a dictionary mapping from '
+ 'variable\n'
+ 'names (mangled if private) to evaluated annotations. This '
+ 'attribute is\n'
+ 'writable and is automatically created at the start of class or '
+ 'module\n'
+ 'body execution, if annotations are found statically.\n'
+ '\n'
+ 'For expressions as assignment targets, the annotations are '
+ 'evaluated\n'
+ 'if in class or module scope, but not stored.\n'
+ '\n'
+ 'If a name is annotated in a function scope, then this name is '
+ 'local\n'
+ 'for that scope. Annotations are never evaluated and stored in '
+ 'function\n'
+ 'scopes.\n'
+ '\n'
+ 'If the right hand side is present, an annotated assignment '
+ 'performs\n'
+ 'the actual assignment before evaluating annotations (where\n'
+ 'applicable). If the right hand side is not present for an '
+ 'expression\n'
+ 'target, then the interpreter evaluates the target except for '
+ 'the last\n'
+ '"__setitem__()" or "__setattr__()" call.\n'
+ '\n'
+ 'See also: **PEP 526** - Variable and attribute annotation '
+ 'syntax\n'
+ ' **PEP 484** - Type hints\n',
+ 'atom-identifiers': '\n'
+ 'Identifiers (Names)\n'
+ '*******************\n'
+ '\n'
+ 'An identifier occurring as an atom is a name. See '
+ 'section Identifiers\n'
+ 'and keywords for lexical definition and section Naming '
+ 'and binding for\n'
+ 'documentation of naming and binding.\n'
+ '\n'
+ 'When the name is bound to an object, evaluation of the '
+ 'atom yields\n'
+ 'that object. When a name is not bound, an attempt to '
+ 'evaluate it\n'
+ 'raises a "NameError" exception.\n'
+ '\n'
+ '**Private name mangling:** When an identifier that '
+ 'textually occurs in\n'
+ 'a class definition begins with two or more underscore '
+ 'characters and\n'
+ 'does not end in two or more underscores, it is '
+ 'considered a *private\n'
+ 'name* of that class. Private names are transformed to a '
+ 'longer form\n'
+ 'before code is generated for them. The transformation '
+ 'inserts the\n'
+ 'class name, with leading underscores removed and a '
+ 'single underscore\n'
+ 'inserted, in front of the name. For example, the '
+ 'identifier "__spam"\n'
+ 'occurring in a class named "Ham" will be transformed to '
+ '"_Ham__spam".\n'
+ 'This transformation is independent of the syntactical '
+ 'context in which\n'
+ 'the identifier is used. If the transformed name is '
+ 'extremely long\n'
+ '(longer than 255 characters), implementation defined '
+ 'truncation may\n'
+ 'happen. If the class name consists only of underscores, '
+ 'no\n'
+ 'transformation is done.\n',
+ 'atom-literals': '\n'
+ 'Literals\n'
+ '********\n'
+ '\n'
+ 'Python supports string and bytes literals and various '
+ 'numeric\n'
+ 'literals:\n'
+ '\n'
+ ' literal ::= stringliteral | bytesliteral\n'
+ ' | integer | floatnumber | imagnumber\n'
+ '\n'
+ 'Evaluation of a literal yields an object of the given type '
+ '(string,\n'
+ 'bytes, integer, floating point number, complex number) with '
+ 'the given\n'
+ 'value. The value may be approximated in the case of '
+ 'floating point\n'
+ 'and imaginary (complex) literals. See section Literals for '
+ 'details.\n'
+ '\n'
+ 'All literals correspond to immutable data types, and hence '
+ 'the\n'
+ "object's identity is less important than its value. "
+ 'Multiple\n'
+ 'evaluations of literals with the same value (either the '
+ 'same\n'
+ 'occurrence in the program text or a different occurrence) '
+ 'may obtain\n'
+ 'the same object or a different object with the same '
+ 'value.\n',
+ 'attribute-access': '\n'
+ 'Customizing attribute access\n'
+ '****************************\n'
+ '\n'
+ 'The following methods can be defined to customize the '
+ 'meaning of\n'
+ 'attribute access (use of, assignment to, or deletion of '
+ '"x.name") for\n'
+ 'class instances.\n'
+ '\n'
+ 'object.__getattr__(self, name)\n'
+ '\n'
+ ' Called when an attribute lookup has not found the '
+ 'attribute in the\n'
+ ' usual places (i.e. it is not an instance attribute '
+ 'nor is it found\n'
+ ' in the class tree for "self"). "name" is the '
+ 'attribute name. This\n'
+ ' method should return the (computed) attribute value '
+ 'or raise an\n'
+ ' "AttributeError" exception.\n'
+ '\n'
+ ' Note that if the attribute is found through the '
+ 'normal mechanism,\n'
+ ' "__getattr__()" is not called. (This is an '
+ 'intentional asymmetry\n'
+ ' between "__getattr__()" and "__setattr__()".) This is '
+ 'done both for\n'
+ ' efficiency reasons and because otherwise '
+ '"__getattr__()" would have\n'
+ ' no way to access other attributes of the instance. '
+ 'Note that at\n'
+ ' least for instance variables, you can fake total '
+ 'control by not\n'
+ ' inserting any values in the instance attribute '
+ 'dictionary (but\n'
+ ' instead inserting them in another object). See the\n'
+ ' "__getattribute__()" method below for a way to '
+ 'actually get total\n'
+ ' control over attribute access.\n'
+ '\n'
+ 'object.__getattribute__(self, name)\n'
+ '\n'
+ ' Called unconditionally to implement attribute '
+ 'accesses for\n'
+ ' instances of the class. If the class also defines '
+ '"__getattr__()",\n'
+ ' the latter will not be called unless '
+ '"__getattribute__()" either\n'
+ ' calls it explicitly or raises an "AttributeError". '
+ 'This method\n'
+ ' should return the (computed) attribute value or raise '
+ 'an\n'
+ ' "AttributeError" exception. In order to avoid '
+ 'infinite recursion in\n'
+ ' this method, its implementation should always call '
+ 'the base class\n'
+ ' method with the same name to access any attributes it '
+ 'needs, for\n'
+ ' example, "object.__getattribute__(self, name)".\n'
+ '\n'
+ ' Note: This method may still be bypassed when looking '
+ 'up special\n'
+ ' methods as the result of implicit invocation via '
+ 'language syntax\n'
+ ' or built-in functions. See Special method lookup.\n'
+ '\n'
+ 'object.__setattr__(self, name, value)\n'
+ '\n'
+ ' Called when an attribute assignment is attempted. '
+ 'This is called\n'
+ ' instead of the normal mechanism (i.e. store the value '
+ 'in the\n'
+ ' instance dictionary). *name* is the attribute name, '
+ '*value* is the\n'
+ ' value to be assigned to it.\n'
+ '\n'
+ ' If "__setattr__()" wants to assign to an instance '
+ 'attribute, it\n'
+ ' should call the base class method with the same name, '
+ 'for example,\n'
+ ' "object.__setattr__(self, name, value)".\n'
+ '\n'
+ 'object.__delattr__(self, name)\n'
+ '\n'
+ ' Like "__setattr__()" but for attribute deletion '
+ 'instead of\n'
+ ' assignment. This should only be implemented if "del '
+ 'obj.name" is\n'
+ ' meaningful for the object.\n'
+ '\n'
+ 'object.__dir__(self)\n'
+ '\n'
+ ' Called when "dir()" is called on the object. A '
+ 'sequence must be\n'
+ ' returned. "dir()" converts the returned sequence to a '
+ 'list and\n'
+ ' sorts it.\n'
+ '\n'
+ '\n'
+ 'Implementing Descriptors\n'
+ '========================\n'
+ '\n'
+ 'The following methods only apply when an instance of the '
+ 'class\n'
+ 'containing the method (a so-called *descriptor* class) '
+ 'appears in an\n'
+ '*owner* class (the descriptor must be in either the '
+ "owner's class\n"
+ 'dictionary or in the class dictionary for one of its '
+ 'parents). In the\n'
+ 'examples below, "the attribute" refers to the attribute '
+ 'whose name is\n'
+ "the key of the property in the owner class' "
+ '"__dict__".\n'
+ '\n'
+ 'object.__get__(self, instance, owner)\n'
+ '\n'
+ ' Called to get the attribute of the owner class (class '
+ 'attribute\n'
+ ' access) or of an instance of that class (instance '
+ 'attribute\n'
+ ' access). *owner* is always the owner class, while '
+ '*instance* is the\n'
+ ' instance that the attribute was accessed through, or '
+ '"None" when\n'
+ ' the attribute is accessed through the *owner*. This '
+ 'method should\n'
+ ' return the (computed) attribute value or raise an '
+ '"AttributeError"\n'
+ ' exception.\n'
+ '\n'
+ 'object.__set__(self, instance, value)\n'
+ '\n'
+ ' Called to set the attribute on an instance *instance* '
+ 'of the owner\n'
+ ' class to a new value, *value*.\n'
+ '\n'
+ 'object.__delete__(self, instance)\n'
+ '\n'
+ ' Called to delete the attribute on an instance '
+ '*instance* of the\n'
+ ' owner class.\n'
+ '\n'
+ 'object.__set_name__(self, owner, name)\n'
+ '\n'
+ ' Called at the time the owning class *owner* is '
+ 'created. The\n'
+ ' descriptor has been assigned to *name*.\n'
+ '\n'
+ ' New in version 3.6.\n'
+ '\n'
+ 'The attribute "__objclass__" is interpreted by the '
+ '"inspect" module as\n'
+ 'specifying the class where this object was defined '
+ '(setting this\n'
+ 'appropriately can assist in runtime introspection of '
+ 'dynamic class\n'
+ 'attributes). For callables, it may indicate that an '
+ 'instance of the\n'
+ 'given type (or a subclass) is expected or required as '
+ 'the first\n'
+ 'positional argument (for example, CPython sets this '
+ 'attribute for\n'
+ 'unbound methods that are implemented in C).\n'
+ '\n'
+ '\n'
+ 'Invoking Descriptors\n'
+ '====================\n'
+ '\n'
+ 'In general, a descriptor is an object attribute with '
+ '"binding\n'
+ 'behavior", one whose attribute access has been '
+ 'overridden by methods\n'
+ 'in the descriptor protocol: "__get__()", "__set__()", '
+ 'and\n'
+ '"__delete__()". If any of those methods are defined for '
+ 'an object, it\n'
+ 'is said to be a descriptor.\n'
+ '\n'
+ 'The default behavior for attribute access is to get, '
+ 'set, or delete\n'
+ "the attribute from an object's dictionary. For instance, "
+ '"a.x" has a\n'
+ 'lookup chain starting with "a.__dict__[\'x\']", then\n'
+ '"type(a).__dict__[\'x\']", and continuing through the '
+ 'base classes of\n'
+ '"type(a)" excluding metaclasses.\n'
+ '\n'
+ 'However, if the looked-up value is an object defining '
+ 'one of the\n'
+ 'descriptor methods, then Python may override the default '
+ 'behavior and\n'
+ 'invoke the descriptor method instead. Where this occurs '
+ 'in the\n'
+ 'precedence chain depends on which descriptor methods '
+ 'were defined and\n'
+ 'how they were called.\n'
+ '\n'
+ 'The starting point for descriptor invocation is a '
+ 'binding, "a.x". How\n'
+ 'the arguments are assembled depends on "a":\n'
+ '\n'
+ 'Direct Call\n'
+ ' The simplest and least common call is when user code '
+ 'directly\n'
+ ' invokes a descriptor method: "x.__get__(a)".\n'
+ '\n'
+ 'Instance Binding\n'
+ ' If binding to an object instance, "a.x" is '
+ 'transformed into the\n'
+ ' call: "type(a).__dict__[\'x\'].__get__(a, type(a))".\n'
+ '\n'
+ 'Class Binding\n'
+ ' If binding to a class, "A.x" is transformed into the '
+ 'call:\n'
+ ' "A.__dict__[\'x\'].__get__(None, A)".\n'
+ '\n'
+ 'Super Binding\n'
+ ' If "a" is an instance of "super", then the binding '
+ '"super(B,\n'
+ ' obj).m()" searches "obj.__class__.__mro__" for the '
+ 'base class "A"\n'
+ ' immediately preceding "B" and then invokes the '
+ 'descriptor with the\n'
+ ' call: "A.__dict__[\'m\'].__get__(obj, '
+ 'obj.__class__)".\n'
+ '\n'
+ 'For instance bindings, the precedence of descriptor '
+ 'invocation depends\n'
+ 'on the which descriptor methods are defined. A '
+ 'descriptor can define\n'
+ 'any combination of "__get__()", "__set__()" and '
+ '"__delete__()". If it\n'
+ 'does not define "__get__()", then accessing the '
+ 'attribute will return\n'
+ 'the descriptor object itself unless there is a value in '
+ "the object's\n"
+ 'instance dictionary. If the descriptor defines '
+ '"__set__()" and/or\n'
+ '"__delete__()", it is a data descriptor; if it defines '
+ 'neither, it is\n'
+ 'a non-data descriptor. Normally, data descriptors '
+ 'define both\n'
+ '"__get__()" and "__set__()", while non-data descriptors '
+ 'have just the\n'
+ '"__get__()" method. Data descriptors with "__set__()" '
+ 'and "__get__()"\n'
+ 'defined always override a redefinition in an instance '
+ 'dictionary. In\n'
+ 'contrast, non-data descriptors can be overridden by '
+ 'instances.\n'
+ '\n'
+ 'Python methods (including "staticmethod()" and '
+ '"classmethod()") are\n'
+ 'implemented as non-data descriptors. Accordingly, '
+ 'instances can\n'
+ 'redefine and override methods. This allows individual '
+ 'instances to\n'
+ 'acquire behaviors that differ from other instances of '
+ 'the same class.\n'
+ '\n'
+ 'The "property()" function is implemented as a data '
+ 'descriptor.\n'
+ 'Accordingly, instances cannot override the behavior of a '
+ 'property.\n'
+ '\n'
+ '\n'
+ '__slots__\n'
+ '=========\n'
+ '\n'
+ 'By default, instances of classes have a dictionary for '
+ 'attribute\n'
+ 'storage. This wastes space for objects having very few '
+ 'instance\n'
+ 'variables. The space consumption can become acute when '
+ 'creating large\n'
+ 'numbers of instances.\n'
+ '\n'
+ 'The default can be overridden by defining *__slots__* in '
+ 'a class\n'
+ 'definition. The *__slots__* declaration takes a sequence '
+ 'of instance\n'
+ 'variables and reserves just enough space in each '
+ 'instance to hold a\n'
+ 'value for each variable. Space is saved because '
+ '*__dict__* is not\n'
+ 'created for each instance.\n'
+ '\n'
+ 'object.__slots__\n'
+ '\n'
+ ' This class variable can be assigned a string, '
+ 'iterable, or sequence\n'
+ ' of strings with variable names used by instances. '
+ '*__slots__*\n'
+ ' reserves space for the declared variables and '
+ 'prevents the\n'
+ ' automatic creation of *__dict__* and *__weakref__* '
+ 'for each\n'
+ ' instance.\n'
+ '\n'
+ '\n'
+ 'Notes on using *__slots__*\n'
+ '--------------------------\n'
+ '\n'
+ '* When inheriting from a class without *__slots__*, the '
+ '*__dict__*\n'
+ ' attribute of that class will always be accessible, so '
+ 'a *__slots__*\n'
+ ' definition in the subclass is meaningless.\n'
+ '\n'
+ '* Without a *__dict__* variable, instances cannot be '
+ 'assigned new\n'
+ ' variables not listed in the *__slots__* definition. '
+ 'Attempts to\n'
+ ' assign to an unlisted variable name raises '
+ '"AttributeError". If\n'
+ ' dynamic assignment of new variables is desired, then '
+ 'add\n'
+ ' "\'__dict__\'" to the sequence of strings in the '
+ '*__slots__*\n'
+ ' declaration.\n'
+ '\n'
+ '* Without a *__weakref__* variable for each instance, '
+ 'classes\n'
+ ' defining *__slots__* do not support weak references to '
+ 'its\n'
+ ' instances. If weak reference support is needed, then '
+ 'add\n'
+ ' "\'__weakref__\'" to the sequence of strings in the '
+ '*__slots__*\n'
+ ' declaration.\n'
+ '\n'
+ '* *__slots__* are implemented at the class level by '
+ 'creating\n'
+ ' descriptors (Implementing Descriptors) for each '
+ 'variable name. As a\n'
+ ' result, class attributes cannot be used to set default '
+ 'values for\n'
+ ' instance variables defined by *__slots__*; otherwise, '
+ 'the class\n'
+ ' attribute would overwrite the descriptor assignment.\n'
+ '\n'
+ '* The action of a *__slots__* declaration is limited to '
+ 'the class\n'
+ ' where it is defined. As a result, subclasses will '
+ 'have a *__dict__*\n'
+ ' unless they also define *__slots__* (which must only '
+ 'contain names\n'
+ ' of any *additional* slots).\n'
+ '\n'
+ '* If a class defines a slot also defined in a base '
+ 'class, the\n'
+ ' instance variable defined by the base class slot is '
+ 'inaccessible\n'
+ ' (except by retrieving its descriptor directly from the '
+ 'base class).\n'
+ ' This renders the meaning of the program undefined. In '
+ 'the future, a\n'
+ ' check may be added to prevent this.\n'
+ '\n'
+ '* Nonempty *__slots__* does not work for classes derived '
+ 'from\n'
+ ' "variable-length" built-in types such as "int", '
+ '"bytes" and "tuple".\n'
+ '\n'
+ '* Any non-string iterable may be assigned to '
+ '*__slots__*. Mappings\n'
+ ' may also be used; however, in the future, special '
+ 'meaning may be\n'
+ ' assigned to the values corresponding to each key.\n'
+ '\n'
+ '* *__class__* assignment works only if both classes have '
+ 'the same\n'
+ ' *__slots__*.\n',
+ 'attribute-references': '\n'
+ 'Attribute references\n'
+ '********************\n'
+ '\n'
+ 'An attribute reference is a primary followed by a '
+ 'period and a name:\n'
+ '\n'
+ ' attributeref ::= primary "." identifier\n'
+ '\n'
+ 'The primary must evaluate to an object of a type '
+ 'that supports\n'
+ 'attribute references, which most objects do. This '
+ 'object is then\n'
+ 'asked to produce the attribute whose name is the '
+ 'identifier. This\n'
+ 'production can be customized by overriding the '
+ '"__getattr__()" method.\n'
+ 'If this attribute is not available, the exception '
+ '"AttributeError" is\n'
+ 'raised. Otherwise, the type and value of the object '
+ 'produced is\n'
+ 'determined by the object. Multiple evaluations of '
+ 'the same attribute\n'
+ 'reference may yield different objects.\n',
+ 'augassign': '\n'
+ 'Augmented assignment statements\n'
+ '*******************************\n'
+ '\n'
+ 'Augmented assignment is the combination, in a single statement, '
+ 'of a\n'
+ 'binary operation and an assignment statement:\n'
+ '\n'
+ ' augmented_assignment_stmt ::= augtarget augop '
+ '(expression_list | yield_expression)\n'
+ ' augtarget ::= identifier | attributeref | '
+ 'subscription | slicing\n'
+ ' augop ::= "+=" | "-=" | "*=" | "@=" | '
+ '"/=" | "//=" | "%=" | "**="\n'
+ ' | ">>=" | "<<=" | "&=" | "^=" | "|="\n'
+ '\n'
+ '(See section Primaries for the syntax definitions of the last '
+ 'three\n'
+ 'symbols.)\n'
+ '\n'
+ 'An augmented assignment evaluates the target (which, unlike '
+ 'normal\n'
+ 'assignment statements, cannot be an unpacking) and the '
+ 'expression\n'
+ 'list, performs the binary operation specific to the type of '
+ 'assignment\n'
+ 'on the two operands, and assigns the result to the original '
+ 'target.\n'
+ 'The target is only evaluated once.\n'
+ '\n'
+ 'An augmented assignment expression like "x += 1" can be '
+ 'rewritten as\n'
+ '"x = x + 1" to achieve a similar, but not exactly equal effect. '
+ 'In the\n'
+ 'augmented version, "x" is only evaluated once. Also, when '
+ 'possible,\n'
+ 'the actual operation is performed *in-place*, meaning that '
+ 'rather than\n'
+ 'creating a new object and assigning that to the target, the old '
+ 'object\n'
+ 'is modified instead.\n'
+ '\n'
+ 'Unlike normal assignments, augmented assignments evaluate the '
+ 'left-\n'
+ 'hand side *before* evaluating the right-hand side. For '
+ 'example, "a[i]\n'
+ '+= f(x)" first looks-up "a[i]", then it evaluates "f(x)" and '
+ 'performs\n'
+ 'the addition, and lastly, it writes the result back to "a[i]".\n'
+ '\n'
+ 'With the exception of assigning to tuples and multiple targets '
+ 'in a\n'
+ 'single statement, the assignment done by augmented assignment\n'
+ 'statements is handled the same way as normal assignments. '
+ 'Similarly,\n'
+ 'with the exception of the possible *in-place* behavior, the '
+ 'binary\n'
+ 'operation performed by augmented assignment is the same as the '
+ 'normal\n'
+ 'binary operations.\n'
+ '\n'
+ 'For targets which are attribute references, the same caveat '
+ 'about\n'
+ 'class and instance attributes applies as for regular '
+ 'assignments.\n',
+ 'binary': '\n'
+ 'Binary arithmetic operations\n'
+ '****************************\n'
+ '\n'
+ 'The binary arithmetic operations have the conventional priority\n'
+ 'levels. Note that some of these operations also apply to certain '
+ 'non-\n'
+ 'numeric types. Apart from the power operator, there are only two\n'
+ 'levels, one for multiplicative operators and one for additive\n'
+ 'operators:\n'
+ '\n'
+ ' m_expr ::= u_expr | m_expr "*" u_expr | m_expr "@" m_expr |\n'
+ ' m_expr "//" u_expr| m_expr "/" u_expr |\n'
+ ' m_expr "%" u_expr\n'
+ ' a_expr ::= m_expr | a_expr "+" m_expr | a_expr "-" m_expr\n'
+ '\n'
+ 'The "*" (multiplication) operator yields the product of its '
+ 'arguments.\n'
+ 'The arguments must either both be numbers, or one argument must be '
+ 'an\n'
+ 'integer and the other must be a sequence. In the former case, the\n'
+ 'numbers are converted to a common type and then multiplied '
+ 'together.\n'
+ 'In the latter case, sequence repetition is performed; a negative\n'
+ 'repetition factor yields an empty sequence.\n'
+ '\n'
+ 'The "@" (at) operator is intended to be used for matrix\n'
+ 'multiplication. No builtin Python types implement this operator.\n'
+ '\n'
+ 'New in version 3.5.\n'
+ '\n'
+ 'The "/" (division) and "//" (floor division) operators yield the\n'
+ 'quotient of their arguments. The numeric arguments are first\n'
+ 'converted to a common type. Division of integers yields a float, '
+ 'while\n'
+ 'floor division of integers results in an integer; the result is '
+ 'that\n'
+ "of mathematical division with the 'floor' function applied to the\n"
+ 'result. Division by zero raises the "ZeroDivisionError" '
+ 'exception.\n'
+ '\n'
+ 'The "%" (modulo) operator yields the remainder from the division '
+ 'of\n'
+ 'the first argument by the second. The numeric arguments are '
+ 'first\n'
+ 'converted to a common type. A zero right argument raises the\n'
+ '"ZeroDivisionError" exception. The arguments may be floating '
+ 'point\n'
+ 'numbers, e.g., "3.14%0.7" equals "0.34" (since "3.14" equals '
+ '"4*0.7 +\n'
+ '0.34".) The modulo operator always yields a result with the same '
+ 'sign\n'
+ 'as its second operand (or zero); the absolute value of the result '
+ 'is\n'
+ 'strictly smaller than the absolute value of the second operand '
+ '[1].\n'
+ '\n'
+ 'The floor division and modulo operators are connected by the '
+ 'following\n'
+ 'identity: "x == (x//y)*y + (x%y)". Floor division and modulo are '
+ 'also\n'
+ 'connected with the built-in function "divmod()": "divmod(x, y) ==\n'
+ '(x//y, x%y)". [2].\n'
+ '\n'
+ 'In addition to performing the modulo operation on numbers, the '
+ '"%"\n'
+ 'operator is also overloaded by string objects to perform '
+ 'old-style\n'
+ 'string formatting (also known as interpolation). The syntax for\n'
+ 'string formatting is described in the Python Library Reference,\n'
+ 'section printf-style String Formatting.\n'
+ '\n'
+ 'The floor division operator, the modulo operator, and the '
+ '"divmod()"\n'
+ 'function are not defined for complex numbers. Instead, convert to '
+ 'a\n'
+ 'floating point number using the "abs()" function if appropriate.\n'
+ '\n'
+ 'The "+" (addition) operator yields the sum of its arguments. The\n'
+ 'arguments must either both be numbers or both be sequences of the '
+ 'same\n'
+ 'type. In the former case, the numbers are converted to a common '
+ 'type\n'
+ 'and then added together. In the latter case, the sequences are\n'
+ 'concatenated.\n'
+ '\n'
+ 'The "-" (subtraction) operator yields the difference of its '
+ 'arguments.\n'
+ 'The numeric arguments are first converted to a common type.\n',
+ 'bitwise': '\n'
+ 'Binary bitwise operations\n'
+ '*************************\n'
+ '\n'
+ 'Each of the three bitwise operations has a different priority '
+ 'level:\n'
+ '\n'
+ ' and_expr ::= shift_expr | and_expr "&" shift_expr\n'
+ ' xor_expr ::= and_expr | xor_expr "^" and_expr\n'
+ ' or_expr ::= xor_expr | or_expr "|" xor_expr\n'
+ '\n'
+ 'The "&" operator yields the bitwise AND of its arguments, which '
+ 'must\n'
+ 'be integers.\n'
+ '\n'
+ 'The "^" operator yields the bitwise XOR (exclusive OR) of its\n'
+ 'arguments, which must be integers.\n'
+ '\n'
+ 'The "|" operator yields the bitwise (inclusive) OR of its '
+ 'arguments,\n'
+ 'which must be integers.\n',
+ 'bltin-code-objects': '\n'
+ 'Code Objects\n'
+ '************\n'
+ '\n'
+ 'Code objects are used by the implementation to '
+ 'represent "pseudo-\n'
+ 'compiled" executable Python code such as a function '
+ 'body. They differ\n'
+ "from function objects because they don't contain a "
+ 'reference to their\n'
+ 'global execution environment. Code objects are '
+ 'returned by the built-\n'
+ 'in "compile()" function and can be extracted from '
+ 'function objects\n'
+ 'through their "__code__" attribute. See also the '
+ '"code" module.\n'
+ '\n'
+ 'A code object can be executed or evaluated by passing '
+ 'it (instead of a\n'
+ 'source string) to the "exec()" or "eval()" built-in '
+ 'functions.\n'
+ '\n'
+ 'See The standard type hierarchy for more '
+ 'information.\n',
+ 'bltin-ellipsis-object': '\n'
+ 'The Ellipsis Object\n'
+ '*******************\n'
+ '\n'
+ 'This object is commonly used by slicing (see '
+ 'Slicings). It supports\n'
+ 'no special operations. There is exactly one '
+ 'ellipsis object, named\n'
+ '"Ellipsis" (a built-in name). "type(Ellipsis)()" '
+ 'produces the\n'
+ '"Ellipsis" singleton.\n'
+ '\n'
+ 'It is written as "Ellipsis" or "...".\n',
+ 'bltin-null-object': '\n'
+ 'The Null Object\n'
+ '***************\n'
+ '\n'
+ "This object is returned by functions that don't "
+ 'explicitly return a\n'
+ 'value. It supports no special operations. There is '
+ 'exactly one null\n'
+ 'object, named "None" (a built-in name). "type(None)()" '
+ 'produces the\n'
+ 'same singleton.\n'
+ '\n'
+ 'It is written as "None".\n',
+ 'bltin-type-objects': '\n'
+ 'Type Objects\n'
+ '************\n'
+ '\n'
+ 'Type objects represent the various object types. An '
+ "object's type is\n"
+ 'accessed by the built-in function "type()". There are '
+ 'no special\n'
+ 'operations on types. The standard module "types" '
+ 'defines names for\n'
+ 'all standard built-in types.\n'
+ '\n'
+ 'Types are written like this: "<class \'int\'>".\n',
+ 'booleans': '\n'
+ 'Boolean operations\n'
+ '******************\n'
+ '\n'
+ ' or_test ::= and_test | or_test "or" and_test\n'
+ ' and_test ::= not_test | and_test "and" not_test\n'
+ ' not_test ::= comparison | "not" not_test\n'
+ '\n'
+ 'In the context of Boolean operations, and also when expressions '
+ 'are\n'
+ 'used by control flow statements, the following values are '
+ 'interpreted\n'
+ 'as false: "False", "None", numeric zero of all types, and empty\n'
+ 'strings and containers (including strings, tuples, lists,\n'
+ 'dictionaries, sets and frozensets). All other values are '
+ 'interpreted\n'
+ 'as true. User-defined objects can customize their truth value '
+ 'by\n'
+ 'providing a "__bool__()" method.\n'
+ '\n'
+ 'The operator "not" yields "True" if its argument is false, '
+ '"False"\n'
+ 'otherwise.\n'
+ '\n'
+ 'The expression "x and y" first evaluates *x*; if *x* is false, '
+ 'its\n'
+ 'value is returned; otherwise, *y* is evaluated and the resulting '
+ 'value\n'
+ 'is returned.\n'
+ '\n'
+ 'The expression "x or y" first evaluates *x*; if *x* is true, its '
+ 'value\n'
+ 'is returned; otherwise, *y* is evaluated and the resulting value '
+ 'is\n'
+ 'returned.\n'
+ '\n'
+ '(Note that neither "and" nor "or" restrict the value and type '
+ 'they\n'
+ 'return to "False" and "True", but rather return the last '
+ 'evaluated\n'
+ 'argument. This is sometimes useful, e.g., if "s" is a string '
+ 'that\n'
+ 'should be replaced by a default value if it is empty, the '
+ 'expression\n'
+ '"s or \'foo\'" yields the desired value. Because "not" has to '
+ 'create a\n'
+ 'new value, it returns a boolean value regardless of the type of '
+ 'its\n'
+ 'argument (for example, "not \'foo\'" produces "False" rather '
+ 'than "\'\'".)\n',
+ 'break': '\n'
+ 'The "break" statement\n'
+ '*********************\n'
+ '\n'
+ ' break_stmt ::= "break"\n'
+ '\n'
+ '"break" may only occur syntactically nested in a "for" or "while"\n'
+ 'loop, but not nested in a function or class definition within that\n'
+ 'loop.\n'
+ '\n'
+ 'It terminates the nearest enclosing loop, skipping the optional '
+ '"else"\n'
+ 'clause if the loop has one.\n'
+ '\n'
+ 'If a "for" loop is terminated by "break", the loop control target\n'
+ 'keeps its current value.\n'
+ '\n'
+ 'When "break" passes control out of a "try" statement with a '
+ '"finally"\n'
+ 'clause, that "finally" clause is executed before really leaving '
+ 'the\n'
+ 'loop.\n',
+ 'callable-types': '\n'
+ 'Emulating callable objects\n'
+ '**************************\n'
+ '\n'
+ 'object.__call__(self[, args...])\n'
+ '\n'
+ ' Called when the instance is "called" as a function; if '
+ 'this method\n'
+ ' is defined, "x(arg1, arg2, ...)" is a shorthand for\n'
+ ' "x.__call__(arg1, arg2, ...)".\n',
+ 'calls': '\n'
+ 'Calls\n'
+ '*****\n'
+ '\n'
+ 'A call calls a callable object (e.g., a *function*) with a '
+ 'possibly\n'
+ 'empty series of *arguments*:\n'
+ '\n'
+ ' call ::= primary "(" [argument_list [","] | '
+ 'comprehension] ")"\n'
+ ' argument_list ::= positional_arguments ["," '
+ 'starred_and_keywords]\n'
+ ' ["," keywords_arguments]\n'
+ ' | starred_and_keywords ["," '
+ 'keywords_arguments]\n'
+ ' | keywords_arguments\n'
+ ' positional_arguments ::= ["*"] expression ("," ["*"] '
+ 'expression)*\n'
+ ' starred_and_keywords ::= ("*" expression | keyword_item)\n'
+ ' ("," "*" expression | "," '
+ 'keyword_item)*\n'
+ ' keywords_arguments ::= (keyword_item | "**" expression)\n'
+ ' ("," keyword_item | "**" expression)*\n'
+ ' keyword_item ::= identifier "=" expression\n'
+ '\n'
+ 'An optional trailing comma may be present after the positional and\n'
+ 'keyword arguments but does not affect the semantics.\n'
+ '\n'
+ 'The primary must evaluate to a callable object (user-defined\n'
+ 'functions, built-in functions, methods of built-in objects, class\n'
+ 'objects, methods of class instances, and all objects having a\n'
+ '"__call__()" method are callable). All argument expressions are\n'
+ 'evaluated before the call is attempted. Please refer to section\n'
+ 'Function definitions for the syntax of formal *parameter* lists.\n'
+ '\n'
+ 'If keyword arguments are present, they are first converted to\n'
+ 'positional arguments, as follows. First, a list of unfilled slots '
+ 'is\n'
+ 'created for the formal parameters. If there are N positional\n'
+ 'arguments, they are placed in the first N slots. Next, for each\n'
+ 'keyword argument, the identifier is used to determine the\n'
+ 'corresponding slot (if the identifier is the same as the first '
+ 'formal\n'
+ 'parameter name, the first slot is used, and so on). If the slot '
+ 'is\n'
+ 'already filled, a "TypeError" exception is raised. Otherwise, the\n'
+ 'value of the argument is placed in the slot, filling it (even if '
+ 'the\n'
+ 'expression is "None", it fills the slot). When all arguments have\n'
+ 'been processed, the slots that are still unfilled are filled with '
+ 'the\n'
+ 'corresponding default value from the function definition. '
+ '(Default\n'
+ 'values are calculated, once, when the function is defined; thus, a\n'
+ 'mutable object such as a list or dictionary used as default value '
+ 'will\n'
+ "be shared by all calls that don't specify an argument value for "
+ 'the\n'
+ 'corresponding slot; this should usually be avoided.) If there are '
+ 'any\n'
+ 'unfilled slots for which no default value is specified, a '
+ '"TypeError"\n'
+ 'exception is raised. Otherwise, the list of filled slots is used '
+ 'as\n'
+ 'the argument list for the call.\n'
+ '\n'
+ '**CPython implementation detail:** An implementation may provide\n'
+ 'built-in functions whose positional parameters do not have names, '
+ 'even\n'
+ "if they are 'named' for the purpose of documentation, and which\n"
+ 'therefore cannot be supplied by keyword. In CPython, this is the '
+ 'case\n'
+ 'for functions implemented in C that use "PyArg_ParseTuple()" to '
+ 'parse\n'
+ 'their arguments.\n'
+ '\n'
+ 'If there are more positional arguments than there are formal '
+ 'parameter\n'
+ 'slots, a "TypeError" exception is raised, unless a formal '
+ 'parameter\n'
+ 'using the syntax "*identifier" is present; in this case, that '
+ 'formal\n'
+ 'parameter receives a tuple containing the excess positional '
+ 'arguments\n'
+ '(or an empty tuple if there were no excess positional arguments).\n'
+ '\n'
+ 'If any keyword argument does not correspond to a formal parameter\n'
+ 'name, a "TypeError" exception is raised, unless a formal parameter\n'
+ 'using the syntax "**identifier" is present; in this case, that '
+ 'formal\n'
+ 'parameter receives a dictionary containing the excess keyword\n'
+ 'arguments (using the keywords as keys and the argument values as\n'
+ 'corresponding values), or a (new) empty dictionary if there were '
+ 'no\n'
+ 'excess keyword arguments.\n'
+ '\n'
+ 'If the syntax "*expression" appears in the function call, '
+ '"expression"\n'
+ 'must evaluate to an *iterable*. Elements from these iterables are\n'
+ 'treated as if they were additional positional arguments. For the '
+ 'call\n'
+ '"f(x1, x2, *y, x3, x4)", if *y* evaluates to a sequence *y1*, ...,\n'
+ '*yM*, this is equivalent to a call with M+4 positional arguments '
+ '*x1*,\n'
+ '*x2*, *y1*, ..., *yM*, *x3*, *x4*.\n'
+ '\n'
+ 'A consequence of this is that although the "*expression" syntax '
+ 'may\n'
+ 'appear *after* explicit keyword arguments, it is processed '
+ '*before*\n'
+ 'the keyword arguments (and any "**expression" arguments -- see '
+ 'below).\n'
+ 'So:\n'
+ '\n'
+ ' >>> def f(a, b):\n'
+ ' ... print(a, b)\n'
+ ' ...\n'
+ ' >>> f(b=1, *(2,))\n'
+ ' 2 1\n'
+ ' >>> f(a=1, *(2,))\n'
+ ' Traceback (most recent call last):\n'
+ ' File "<stdin>", line 1, in ?\n'
+ " TypeError: f() got multiple values for keyword argument 'a'\n"
+ ' >>> f(1, *(2,))\n'
+ ' 1 2\n'
+ '\n'
+ 'It is unusual for both keyword arguments and the "*expression" '
+ 'syntax\n'
+ 'to be used in the same call, so in practice this confusion does '
+ 'not\n'
+ 'arise.\n'
+ '\n'
+ 'If the syntax "**expression" appears in the function call,\n'
+ '"expression" must evaluate to a *mapping*, the contents of which '
+ 'are\n'
+ 'treated as additional keyword arguments. If a keyword is already\n'
+ 'present (as an explicit keyword argument, or from another '
+ 'unpacking),\n'
+ 'a "TypeError" exception is raised.\n'
+ '\n'
+ 'Formal parameters using the syntax "*identifier" or "**identifier"\n'
+ 'cannot be used as positional argument slots or as keyword argument\n'
+ 'names.\n'
+ '\n'
+ 'Changed in version 3.5: Function calls accept any number of "*" '
+ 'and\n'
+ '"**" unpackings, positional arguments may follow iterable '
+ 'unpackings\n'
+ '("*"), and keyword arguments may follow dictionary unpackings '
+ '("**").\n'
+ 'Originally proposed by **PEP 448**.\n'
+ '\n'
+ 'A call always returns some value, possibly "None", unless it raises '
+ 'an\n'
+ 'exception. How this value is computed depends on the type of the\n'
+ 'callable object.\n'
+ '\n'
+ 'If it is---\n'
+ '\n'
+ 'a user-defined function:\n'
+ ' The code block for the function is executed, passing it the\n'
+ ' argument list. The first thing the code block will do is bind '
+ 'the\n'
+ ' formal parameters to the arguments; this is described in '
+ 'section\n'
+ ' Function definitions. When the code block executes a "return"\n'
+ ' statement, this specifies the return value of the function '
+ 'call.\n'
+ '\n'
+ 'a built-in function or method:\n'
+ ' The result is up to the interpreter; see Built-in Functions for '
+ 'the\n'
+ ' descriptions of built-in functions and methods.\n'
+ '\n'
+ 'a class object:\n'
+ ' A new instance of that class is returned.\n'
+ '\n'
+ 'a class instance method:\n'
+ ' The corresponding user-defined function is called, with an '
+ 'argument\n'
+ ' list that is one longer than the argument list of the call: the\n'
+ ' instance becomes the first argument.\n'
+ '\n'
+ 'a class instance:\n'
+ ' The class must define a "__call__()" method; the effect is then '
+ 'the\n'
+ ' same as if that method was called.\n',
+ 'class': '\n'
+ 'Class definitions\n'
+ '*****************\n'
+ '\n'
+ 'A class definition defines a class object (see section The '
+ 'standard\n'
+ 'type hierarchy):\n'
+ '\n'
+ ' classdef ::= [decorators] "class" classname [inheritance] ":" '
+ 'suite\n'
+ ' inheritance ::= "(" [argument_list] ")"\n'
+ ' classname ::= identifier\n'
+ '\n'
+ 'A class definition is an executable statement. The inheritance '
+ 'list\n'
+ 'usually gives a list of base classes (see Metaclasses for more\n'
+ 'advanced uses), so each item in the list should evaluate to a '
+ 'class\n'
+ 'object which allows subclassing. Classes without an inheritance '
+ 'list\n'
+ 'inherit, by default, from the base class "object"; hence,\n'
+ '\n'
+ ' class Foo:\n'
+ ' pass\n'
+ '\n'
+ 'is equivalent to\n'
+ '\n'
+ ' class Foo(object):\n'
+ ' pass\n'
+ '\n'
+ "The class's suite is then executed in a new execution frame (see\n"
+ 'Naming and binding), using a newly created local namespace and the\n'
+ 'original global namespace. (Usually, the suite contains mostly\n'
+ "function definitions.) When the class's suite finishes execution, "
+ 'its\n'
+ 'execution frame is discarded but its local namespace is saved. [4] '
+ 'A\n'
+ 'class object is then created using the inheritance list for the '
+ 'base\n'
+ 'classes and the saved local namespace for the attribute '
+ 'dictionary.\n'
+ 'The class name is bound to this class object in the original local\n'
+ 'namespace.\n'
+ '\n'
+ 'The order in which attributes are defined in the class body is\n'
+ 'preserved in the new class\'s "__dict__". Note that this is '
+ 'reliable\n'
+ 'only right after the class is created and only for classes that '
+ 'were\n'
+ 'defined using the definition syntax.\n'
+ '\n'
+ 'Class creation can be customized heavily using metaclasses.\n'
+ '\n'
+ 'Classes can also be decorated: just like when decorating '
+ 'functions,\n'
+ '\n'
+ ' @f1(arg)\n'
+ ' @f2\n'
+ ' class Foo: pass\n'
+ '\n'
+ 'is roughly equivalent to\n'
+ '\n'
+ ' class Foo: pass\n'
+ ' Foo = f1(arg)(f2(Foo))\n'
+ '\n'
+ 'The evaluation rules for the decorator expressions are the same as '
+ 'for\n'
+ 'function decorators. The result is then bound to the class name.\n'
+ '\n'
+ "**Programmer's note:** Variables defined in the class definition "
+ 'are\n'
+ 'class attributes; they are shared by instances. Instance '
+ 'attributes\n'
+ 'can be set in a method with "self.name = value". Both class and\n'
+ 'instance attributes are accessible through the notation '
+ '""self.name"",\n'
+ 'and an instance attribute hides a class attribute with the same '
+ 'name\n'
+ 'when accessed in this way. Class attributes can be used as '
+ 'defaults\n'
+ 'for instance attributes, but using mutable values there can lead '
+ 'to\n'
+ 'unexpected results. Descriptors can be used to create instance\n'
+ 'variables with different implementation details.\n'
+ '\n'
+ 'See also: **PEP 3115** - Metaclasses in Python 3 **PEP 3129** -\n'
+ ' Class Decorators\n',
+ 'comparisons': '\n'
+ 'Comparisons\n'
+ '***********\n'
+ '\n'
+ 'Unlike C, all comparison operations in Python have the same '
+ 'priority,\n'
+ 'which is lower than that of any arithmetic, shifting or '
+ 'bitwise\n'
+ 'operation. Also unlike C, expressions like "a < b < c" have '
+ 'the\n'
+ 'interpretation that is conventional in mathematics:\n'
+ '\n'
+ ' comparison ::= or_expr ( comp_operator or_expr )*\n'
+ ' comp_operator ::= "<" | ">" | "==" | ">=" | "<=" | "!="\n'
+ ' | "is" ["not"] | ["not"] "in"\n'
+ '\n'
+ 'Comparisons yield boolean values: "True" or "False".\n'
+ '\n'
+ 'Comparisons can be chained arbitrarily, e.g., "x < y <= z" '
+ 'is\n'
+ 'equivalent to "x < y and y <= z", except that "y" is '
+ 'evaluated only\n'
+ 'once (but in both cases "z" is not evaluated at all when "x < '
+ 'y" is\n'
+ 'found to be false).\n'
+ '\n'
+ 'Formally, if *a*, *b*, *c*, ..., *y*, *z* are expressions and '
+ '*op1*,\n'
+ '*op2*, ..., *opN* are comparison operators, then "a op1 b op2 '
+ 'c ... y\n'
+ 'opN z" is equivalent to "a op1 b and b op2 c and ... y opN '
+ 'z", except\n'
+ 'that each expression is evaluated at most once.\n'
+ '\n'
+ 'Note that "a op1 b op2 c" doesn\'t imply any kind of '
+ 'comparison between\n'
+ '*a* and *c*, so that, e.g., "x < y > z" is perfectly legal '
+ '(though\n'
+ 'perhaps not pretty).\n'
+ '\n'
+ '\n'
+ 'Value comparisons\n'
+ '=================\n'
+ '\n'
+ 'The operators "<", ">", "==", ">=", "<=", and "!=" compare '
+ 'the values\n'
+ 'of two objects. The objects do not need to have the same '
+ 'type.\n'
+ '\n'
+ 'Chapter Objects, values and types states that objects have a '
+ 'value (in\n'
+ 'addition to type and identity). The value of an object is a '
+ 'rather\n'
+ 'abstract notion in Python: For example, there is no canonical '
+ 'access\n'
+ "method for an object's value. Also, there is no requirement "
+ 'that the\n'
+ 'value of an object should be constructed in a particular way, '
+ 'e.g.\n'
+ 'comprised of all its data attributes. Comparison operators '
+ 'implement a\n'
+ 'particular notion of what the value of an object is. One can '
+ 'think of\n'
+ 'them as defining the value of an object indirectly, by means '
+ 'of their\n'
+ 'comparison implementation.\n'
+ '\n'
+ 'Because all types are (direct or indirect) subtypes of '
+ '"object", they\n'
+ 'inherit the default comparison behavior from "object". Types '
+ 'can\n'
+ 'customize their comparison behavior by implementing *rich '
+ 'comparison\n'
+ 'methods* like "__lt__()", described in Basic customization.\n'
+ '\n'
+ 'The default behavior for equality comparison ("==" and "!=") '
+ 'is based\n'
+ 'on the identity of the objects. Hence, equality comparison '
+ 'of\n'
+ 'instances with the same identity results in equality, and '
+ 'equality\n'
+ 'comparison of instances with different identities results in\n'
+ 'inequality. A motivation for this default behavior is the '
+ 'desire that\n'
+ 'all objects should be reflexive (i.e. "x is y" implies "x == '
+ 'y").\n'
+ '\n'
+ 'A default order comparison ("<", ">", "<=", and ">=") is not '
+ 'provided;\n'
+ 'an attempt raises "TypeError". A motivation for this default '
+ 'behavior\n'
+ 'is the lack of a similar invariant as for equality.\n'
+ '\n'
+ 'The behavior of the default equality comparison, that '
+ 'instances with\n'
+ 'different identities are always unequal, may be in contrast '
+ 'to what\n'
+ 'types will need that have a sensible definition of object '
+ 'value and\n'
+ 'value-based equality. Such types will need to customize '
+ 'their\n'
+ 'comparison behavior, and in fact, a number of built-in types '
+ 'have done\n'
+ 'that.\n'
+ '\n'
+ 'The following list describes the comparison behavior of the '
+ 'most\n'
+ 'important built-in types.\n'
+ '\n'
+ '* Numbers of built-in numeric types (Numeric Types --- int, '
+ 'float,\n'
+ ' complex) and of the standard library types '
+ '"fractions.Fraction" and\n'
+ ' "decimal.Decimal" can be compared within and across their '
+ 'types,\n'
+ ' with the restriction that complex numbers do not support '
+ 'order\n'
+ ' comparison. Within the limits of the types involved, they '
+ 'compare\n'
+ ' mathematically (algorithmically) correct without loss of '
+ 'precision.\n'
+ '\n'
+ ' The not-a-number values "float(\'NaN\')" and '
+ '"Decimal(\'NaN\')" are\n'
+ ' special. They are identical to themselves ("x is x" is '
+ 'true) but\n'
+ ' are not equal to themselves ("x == x" is false). '
+ 'Additionally,\n'
+ ' comparing any number to a not-a-number value will return '
+ '"False".\n'
+ ' For example, both "3 < float(\'NaN\')" and "float(\'NaN\') '
+ '< 3" will\n'
+ ' return "False".\n'
+ '\n'
+ '* Binary sequences (instances of "bytes" or "bytearray") can '
+ 'be\n'
+ ' compared within and across their types. They compare\n'
+ ' lexicographically using the numeric values of their '
+ 'elements.\n'
+ '\n'
+ '* Strings (instances of "str") compare lexicographically '
+ 'using the\n'
+ ' numerical Unicode code points (the result of the built-in '
+ 'function\n'
+ ' "ord()") of their characters. [3]\n'
+ '\n'
+ ' Strings and binary sequences cannot be directly compared.\n'
+ '\n'
+ '* Sequences (instances of "tuple", "list", or "range") can '
+ 'be\n'
+ ' compared only within each of their types, with the '
+ 'restriction that\n'
+ ' ranges do not support order comparison. Equality '
+ 'comparison across\n'
+ ' these types results in unequality, and ordering comparison '
+ 'across\n'
+ ' these types raises "TypeError".\n'
+ '\n'
+ ' Sequences compare lexicographically using comparison of\n'
+ ' corresponding elements, whereby reflexivity of the elements '
+ 'is\n'
+ ' enforced.\n'
+ '\n'
+ ' In enforcing reflexivity of elements, the comparison of '
+ 'collections\n'
+ ' assumes that for a collection element "x", "x == x" is '
+ 'always true.\n'
+ ' Based on that assumption, element identity is compared '
+ 'first, and\n'
+ ' element comparison is performed only for distinct '
+ 'elements. This\n'
+ ' approach yields the same result as a strict element '
+ 'comparison\n'
+ ' would, if the compared elements are reflexive. For '
+ 'non-reflexive\n'
+ ' elements, the result is different than for strict element\n'
+ ' comparison, and may be surprising: The non-reflexive '
+ 'not-a-number\n'
+ ' values for example result in the following comparison '
+ 'behavior when\n'
+ ' used in a list:\n'
+ '\n'
+ " >>> nan = float('NaN')\n"
+ ' >>> nan is nan\n'
+ ' True\n'
+ ' >>> nan == nan\n'
+ ' False <-- the defined non-reflexive '
+ 'behavior of NaN\n'
+ ' >>> [nan] == [nan]\n'
+ ' True <-- list enforces reflexivity and '
+ 'tests identity first\n'
+ '\n'
+ ' Lexicographical comparison between built-in collections '
+ 'works as\n'
+ ' follows:\n'
+ '\n'
+ ' * For two collections to compare equal, they must be of the '
+ 'same\n'
+ ' type, have the same length, and each pair of '
+ 'corresponding\n'
+ ' elements must compare equal (for example, "[1,2] == '
+ '(1,2)" is\n'
+ ' false because the type is not the same).\n'
+ '\n'
+ ' * Collections that support order comparison are ordered the '
+ 'same\n'
+ ' as their first unequal elements (for example, "[1,2,x] <= '
+ '[1,2,y]"\n'
+ ' has the same value as "x <= y"). If a corresponding '
+ 'element does\n'
+ ' not exist, the shorter collection is ordered first (for '
+ 'example,\n'
+ ' "[1,2] < [1,2,3]" is true).\n'
+ '\n'
+ '* Mappings (instances of "dict") compare equal if and only if '
+ 'they\n'
+ ' have equal *(key, value)* pairs. Equality comparison of the '
+ 'keys and\n'
+ ' elements enforces reflexivity.\n'
+ '\n'
+ ' Order comparisons ("<", ">", "<=", and ">=") raise '
+ '"TypeError".\n'
+ '\n'
+ '* Sets (instances of "set" or "frozenset") can be compared '
+ 'within\n'
+ ' and across their types.\n'
+ '\n'
+ ' They define order comparison operators to mean subset and '
+ 'superset\n'
+ ' tests. Those relations do not define total orderings (for '
+ 'example,\n'
+ ' the two sets "{1,2}" and "{2,3}" are not equal, nor subsets '
+ 'of one\n'
+ ' another, nor supersets of one another). Accordingly, sets '
+ 'are not\n'
+ ' appropriate arguments for functions which depend on total '
+ 'ordering\n'
+ ' (for example, "min()", "max()", and "sorted()" produce '
+ 'undefined\n'
+ ' results given a list of sets as inputs).\n'
+ '\n'
+ ' Comparison of sets enforces reflexivity of its elements.\n'
+ '\n'
+ '* Most other built-in types have no comparison methods '
+ 'implemented,\n'
+ ' so they inherit the default comparison behavior.\n'
+ '\n'
+ 'User-defined classes that customize their comparison behavior '
+ 'should\n'
+ 'follow some consistency rules, if possible:\n'
+ '\n'
+ '* Equality comparison should be reflexive. In other words, '
+ 'identical\n'
+ ' objects should compare equal:\n'
+ '\n'
+ ' "x is y" implies "x == y"\n'
+ '\n'
+ '* Comparison should be symmetric. In other words, the '
+ 'following\n'
+ ' expressions should have the same result:\n'
+ '\n'
+ ' "x == y" and "y == x"\n'
+ '\n'
+ ' "x != y" and "y != x"\n'
+ '\n'
+ ' "x < y" and "y > x"\n'
+ '\n'
+ ' "x <= y" and "y >= x"\n'
+ '\n'
+ '* Comparison should be transitive. The following '
+ '(non-exhaustive)\n'
+ ' examples illustrate that:\n'
+ '\n'
+ ' "x > y and y > z" implies "x > z"\n'
+ '\n'
+ ' "x < y and y <= z" implies "x < z"\n'
+ '\n'
+ '* Inverse comparison should result in the boolean negation. '
+ 'In other\n'
+ ' words, the following expressions should have the same '
+ 'result:\n'
+ '\n'
+ ' "x == y" and "not x != y"\n'
+ '\n'
+ ' "x < y" and "not x >= y" (for total ordering)\n'
+ '\n'
+ ' "x > y" and "not x <= y" (for total ordering)\n'
+ '\n'
+ ' The last two expressions apply to totally ordered '
+ 'collections (e.g.\n'
+ ' to sequences, but not to sets or mappings). See also the\n'
+ ' "total_ordering()" decorator.\n'
+ '\n'
+ 'Python does not enforce these consistency rules. In fact, '
+ 'the\n'
+ 'not-a-number values are an example for not following these '
+ 'rules.\n'
+ '\n'
+ '\n'
+ 'Membership test operations\n'
+ '==========================\n'
+ '\n'
+ 'The operators "in" and "not in" test for membership. "x in '
+ 's"\n'
+ 'evaluates to true if *x* is a member of *s*, and false '
+ 'otherwise. "x\n'
+ 'not in s" returns the negation of "x in s". All built-in '
+ 'sequences\n'
+ 'and set types support this as well as dictionary, for which '
+ '"in" tests\n'
+ 'whether the dictionary has a given key. For container types '
+ 'such as\n'
+ 'list, tuple, set, frozenset, dict, or collections.deque, the\n'
+ 'expression "x in y" is equivalent to "any(x is e or x == e '
+ 'for e in\n'
+ 'y)".\n'
+ '\n'
+ 'For the string and bytes types, "x in y" is true if and only '
+ 'if *x* is\n'
+ 'a substring of *y*. An equivalent test is "y.find(x) != '
+ '-1". Empty\n'
+ 'strings are always considered to be a substring of any other '
+ 'string,\n'
+ 'so """ in "abc"" will return "True".\n'
+ '\n'
+ 'For user-defined classes which define the "__contains__()" '
+ 'method, "x\n'
+ 'in y" is true if and only if "y.__contains__(x)" is true.\n'
+ '\n'
+ 'For user-defined classes which do not define "__contains__()" '
+ 'but do\n'
+ 'define "__iter__()", "x in y" is true if some value "z" with '
+ '"x == z"\n'
+ 'is produced while iterating over "y". If an exception is '
+ 'raised\n'
+ 'during the iteration, it is as if "in" raised that '
+ 'exception.\n'
+ '\n'
+ 'Lastly, the old-style iteration protocol is tried: if a class '
+ 'defines\n'
+ '"__getitem__()", "x in y" is true if and only if there is a '
+ 'non-\n'
+ 'negative integer index *i* such that "x == y[i]", and all '
+ 'lower\n'
+ 'integer indices do not raise "IndexError" exception. (If any '
+ 'other\n'
+ 'exception is raised, it is as if "in" raised that '
+ 'exception).\n'
+ '\n'
+ 'The operator "not in" is defined to have the inverse true '
+ 'value of\n'
+ '"in".\n'
+ '\n'
+ '\n'
+ 'Identity comparisons\n'
+ '====================\n'
+ '\n'
+ 'The operators "is" and "is not" test for object identity: "x '
+ 'is y" is\n'
+ 'true if and only if *x* and *y* are the same object. Object '
+ 'identity\n'
+ 'is determined using the "id()" function. "x is not y" yields '
+ 'the\n'
+ 'inverse truth value. [4]\n',
+ 'compound': '\n'
+ 'Compound statements\n'
+ '*******************\n'
+ '\n'
+ 'Compound statements contain (groups of) other statements; they '
+ 'affect\n'
+ 'or control the execution of those other statements in some way. '
+ 'In\n'
+ 'general, compound statements span multiple lines, although in '
+ 'simple\n'
+ 'incarnations a whole compound statement may be contained in one '
+ 'line.\n'
+ '\n'
+ 'The "if", "while" and "for" statements implement traditional '
+ 'control\n'
+ 'flow constructs. "try" specifies exception handlers and/or '
+ 'cleanup\n'
+ 'code for a group of statements, while the "with" statement '
+ 'allows the\n'
+ 'execution of initialization and finalization code around a block '
+ 'of\n'
+ 'code. Function and class definitions are also syntactically '
+ 'compound\n'
+ 'statements.\n'
+ '\n'
+ "A compound statement consists of one or more 'clauses.' A "
+ 'clause\n'
+ "consists of a header and a 'suite.' The clause headers of a\n"
+ 'particular compound statement are all at the same indentation '
+ 'level.\n'
+ 'Each clause header begins with a uniquely identifying keyword '
+ 'and ends\n'
+ 'with a colon. A suite is a group of statements controlled by a\n'
+ 'clause. A suite can be one or more semicolon-separated simple\n'
+ 'statements on the same line as the header, following the '
+ "header's\n"
+ 'colon, or it can be one or more indented statements on '
+ 'subsequent\n'
+ 'lines. Only the latter form of a suite can contain nested '
+ 'compound\n'
+ "statements; the following is illegal, mostly because it wouldn't "
+ 'be\n'
+ 'clear to which "if" clause a following "else" clause would '
+ 'belong:\n'
+ '\n'
+ ' if test1: if test2: print(x)\n'
+ '\n'
+ 'Also note that the semicolon binds tighter than the colon in '
+ 'this\n'
+ 'context, so that in the following example, either all or none of '
+ 'the\n'
+ '"print()" calls are executed:\n'
+ '\n'
+ ' if x < y < z: print(x); print(y); print(z)\n'
+ '\n'
+ 'Summarizing:\n'
+ '\n'
+ ' compound_stmt ::= if_stmt\n'
+ ' | while_stmt\n'
+ ' | for_stmt\n'
+ ' | try_stmt\n'
+ ' | with_stmt\n'
+ ' | funcdef\n'
+ ' | classdef\n'
+ ' | async_with_stmt\n'
+ ' | async_for_stmt\n'
+ ' | async_funcdef\n'
+ ' suite ::= stmt_list NEWLINE | NEWLINE INDENT '
+ 'statement+ DEDENT\n'
+ ' statement ::= stmt_list NEWLINE | compound_stmt\n'
+ ' stmt_list ::= simple_stmt (";" simple_stmt)* [";"]\n'
+ '\n'
+ 'Note that statements always end in a "NEWLINE" possibly followed '
+ 'by a\n'
+ '"DEDENT". Also note that optional continuation clauses always '
+ 'begin\n'
+ 'with a keyword that cannot start a statement, thus there are no\n'
+ 'ambiguities (the \'dangling "else"\' problem is solved in Python '
+ 'by\n'
+ 'requiring nested "if" statements to be indented).\n'
+ '\n'
+ 'The formatting of the grammar rules in the following sections '
+ 'places\n'
+ 'each clause on a separate line for clarity.\n'
+ '\n'
+ '\n'
+ 'The "if" statement\n'
+ '==================\n'
+ '\n'
+ 'The "if" statement is used for conditional execution:\n'
+ '\n'
+ ' if_stmt ::= "if" expression ":" suite\n'
+ ' ( "elif" expression ":" suite )*\n'
+ ' ["else" ":" suite]\n'
+ '\n'
+ 'It selects exactly one of the suites by evaluating the '
+ 'expressions one\n'
+ 'by one until one is found to be true (see section Boolean '
+ 'operations\n'
+ 'for the definition of true and false); then that suite is '
+ 'executed\n'
+ '(and no other part of the "if" statement is executed or '
+ 'evaluated).\n'
+ 'If all expressions are false, the suite of the "else" clause, '
+ 'if\n'
+ 'present, is executed.\n'
+ '\n'
+ '\n'
+ 'The "while" statement\n'
+ '=====================\n'
+ '\n'
+ 'The "while" statement is used for repeated execution as long as '
+ 'an\n'
+ 'expression is true:\n'
+ '\n'
+ ' while_stmt ::= "while" expression ":" suite\n'
+ ' ["else" ":" suite]\n'
+ '\n'
+ 'This repeatedly tests the expression and, if it is true, '
+ 'executes the\n'
+ 'first suite; if the expression is false (which may be the first '
+ 'time\n'
+ 'it is tested) the suite of the "else" clause, if present, is '
+ 'executed\n'
+ 'and the loop terminates.\n'
+ '\n'
+ 'A "break" statement executed in the first suite terminates the '
+ 'loop\n'
+ 'without executing the "else" clause\'s suite. A "continue" '
+ 'statement\n'
+ 'executed in the first suite skips the rest of the suite and goes '
+ 'back\n'
+ 'to testing the expression.\n'
+ '\n'
+ '\n'
+ 'The "for" statement\n'
+ '===================\n'
+ '\n'
+ 'The "for" statement is used to iterate over the elements of a '
+ 'sequence\n'
+ '(such as a string, tuple or list) or other iterable object:\n'
+ '\n'
+ ' for_stmt ::= "for" target_list "in" expression_list ":" '
+ 'suite\n'
+ ' ["else" ":" suite]\n'
+ '\n'
+ 'The expression list is evaluated once; it should yield an '
+ 'iterable\n'
+ 'object. An iterator is created for the result of the\n'
+ '"expression_list". The suite is then executed once for each '
+ 'item\n'
+ 'provided by the iterator, in the order returned by the '
+ 'iterator. Each\n'
+ 'item in turn is assigned to the target list using the standard '
+ 'rules\n'
+ 'for assignments (see Assignment statements), and then the suite '
+ 'is\n'
+ 'executed. When the items are exhausted (which is immediately '
+ 'when the\n'
+ 'sequence is empty or an iterator raises a "StopIteration" '
+ 'exception),\n'
+ 'the suite in the "else" clause, if present, is executed, and the '
+ 'loop\n'
+ 'terminates.\n'
+ '\n'
+ 'A "break" statement executed in the first suite terminates the '
+ 'loop\n'
+ 'without executing the "else" clause\'s suite. A "continue" '
+ 'statement\n'
+ 'executed in the first suite skips the rest of the suite and '
+ 'continues\n'
+ 'with the next item, or with the "else" clause if there is no '
+ 'next\n'
+ 'item.\n'
+ '\n'
+ 'The for-loop makes assignments to the variables(s) in the target '
+ 'list.\n'
+ 'This overwrites all previous assignments to those variables '
+ 'including\n'
+ 'those made in the suite of the for-loop:\n'
+ '\n'
+ ' for i in range(10):\n'
+ ' print(i)\n'
+ ' i = 5 # this will not affect the for-loop\n'
+ ' # because i will be overwritten with '
+ 'the next\n'
+ ' # index in the range\n'
+ '\n'
+ 'Names in the target list are not deleted when the loop is '
+ 'finished,\n'
+ 'but if the sequence is empty, they will not have been assigned '
+ 'to at\n'
+ 'all by the loop. Hint: the built-in function "range()" returns '
+ 'an\n'
+ "iterator of integers suitable to emulate the effect of Pascal's "
+ '"for i\n'
+ ':= a to b do"; e.g., "list(range(3))" returns the list "[0, 1, '
+ '2]".\n'
+ '\n'
+ 'Note: There is a subtlety when the sequence is being modified by '
+ 'the\n'
+ ' loop (this can only occur for mutable sequences, i.e. lists). '
+ 'An\n'
+ ' internal counter is used to keep track of which item is used '
+ 'next,\n'
+ ' and this is incremented on each iteration. When this counter '
+ 'has\n'
+ ' reached the length of the sequence the loop terminates. This '
+ 'means\n'
+ ' that if the suite deletes the current (or a previous) item '
+ 'from the\n'
+ ' sequence, the next item will be skipped (since it gets the '
+ 'index of\n'
+ ' the current item which has already been treated). Likewise, '
+ 'if the\n'
+ ' suite inserts an item in the sequence before the current item, '
+ 'the\n'
+ ' current item will be treated again the next time through the '
+ 'loop.\n'
+ ' This can lead to nasty bugs that can be avoided by making a\n'
+ ' temporary copy using a slice of the whole sequence, e.g.,\n'
+ '\n'
+ ' for x in a[:]:\n'
+ ' if x < 0: a.remove(x)\n'
+ '\n'
+ '\n'
+ 'The "try" statement\n'
+ '===================\n'
+ '\n'
+ 'The "try" statement specifies exception handlers and/or cleanup '
+ 'code\n'
+ 'for a group of statements:\n'
+ '\n'
+ ' try_stmt ::= try1_stmt | try2_stmt\n'
+ ' try1_stmt ::= "try" ":" suite\n'
+ ' ("except" [expression ["as" identifier]] ":" '
+ 'suite)+\n'
+ ' ["else" ":" suite]\n'
+ ' ["finally" ":" suite]\n'
+ ' try2_stmt ::= "try" ":" suite\n'
+ ' "finally" ":" suite\n'
+ '\n'
+ 'The "except" clause(s) specify one or more exception handlers. '
+ 'When no\n'
+ 'exception occurs in the "try" clause, no exception handler is\n'
+ 'executed. When an exception occurs in the "try" suite, a search '
+ 'for an\n'
+ 'exception handler is started. This search inspects the except '
+ 'clauses\n'
+ 'in turn until one is found that matches the exception. An '
+ 'expression-\n'
+ 'less except clause, if present, must be last; it matches any\n'
+ 'exception. For an except clause with an expression, that '
+ 'expression\n'
+ 'is evaluated, and the clause matches the exception if the '
+ 'resulting\n'
+ 'object is "compatible" with the exception. An object is '
+ 'compatible\n'
+ 'with an exception if it is the class or a base class of the '
+ 'exception\n'
+ 'object or a tuple containing an item compatible with the '
+ 'exception.\n'
+ '\n'
+ 'If no except clause matches the exception, the search for an '
+ 'exception\n'
+ 'handler continues in the surrounding code and on the invocation '
+ 'stack.\n'
+ '[1]\n'
+ '\n'
+ 'If the evaluation of an expression in the header of an except '
+ 'clause\n'
+ 'raises an exception, the original search for a handler is '
+ 'canceled and\n'
+ 'a search starts for the new exception in the surrounding code '
+ 'and on\n'
+ 'the call stack (it is treated as if the entire "try" statement '
+ 'raised\n'
+ 'the exception).\n'
+ '\n'
+ 'When a matching except clause is found, the exception is '
+ 'assigned to\n'
+ 'the target specified after the "as" keyword in that except '
+ 'clause, if\n'
+ "present, and the except clause's suite is executed. All except\n"
+ 'clauses must have an executable block. When the end of this '
+ 'block is\n'
+ 'reached, execution continues normally after the entire try '
+ 'statement.\n'
+ '(This means that if two nested handlers exist for the same '
+ 'exception,\n'
+ 'and the exception occurs in the try clause of the inner handler, '
+ 'the\n'
+ 'outer handler will not handle the exception.)\n'
+ '\n'
+ 'When an exception has been assigned using "as target", it is '
+ 'cleared\n'
+ 'at the end of the except clause. This is as if\n'
+ '\n'
+ ' except E as N:\n'
+ ' foo\n'
+ '\n'
+ 'was translated to\n'
+ '\n'
+ ' except E as N:\n'
+ ' try:\n'
+ ' foo\n'
+ ' finally:\n'
+ ' del N\n'
+ '\n'
+ 'This means the exception must be assigned to a different name to '
+ 'be\n'
+ 'able to refer to it after the except clause. Exceptions are '
+ 'cleared\n'
+ 'because with the traceback attached to them, they form a '
+ 'reference\n'
+ 'cycle with the stack frame, keeping all locals in that frame '
+ 'alive\n'
+ 'until the next garbage collection occurs.\n'
+ '\n'
+ "Before an except clause's suite is executed, details about the\n"
+ 'exception are stored in the "sys" module and can be accessed '
+ 'via\n'
+ '"sys.exc_info()". "sys.exc_info()" returns a 3-tuple consisting '
+ 'of the\n'
+ 'exception class, the exception instance and a traceback object '
+ '(see\n'
+ 'section The standard type hierarchy) identifying the point in '
+ 'the\n'
+ 'program where the exception occurred. "sys.exc_info()" values '
+ 'are\n'
+ 'restored to their previous values (before the call) when '
+ 'returning\n'
+ 'from a function that handled an exception.\n'
+ '\n'
+ 'The optional "else" clause is executed if and when control flows '
+ 'off\n'
+ 'the end of the "try" clause. [2] Exceptions in the "else" clause '
+ 'are\n'
+ 'not handled by the preceding "except" clauses.\n'
+ '\n'
+ 'If "finally" is present, it specifies a \'cleanup\' handler. '
+ 'The "try"\n'
+ 'clause is executed, including any "except" and "else" clauses. '
+ 'If an\n'
+ 'exception occurs in any of the clauses and is not handled, the\n'
+ 'exception is temporarily saved. The "finally" clause is '
+ 'executed. If\n'
+ 'there is a saved exception it is re-raised at the end of the '
+ '"finally"\n'
+ 'clause. If the "finally" clause raises another exception, the '
+ 'saved\n'
+ 'exception is set as the context of the new exception. If the '
+ '"finally"\n'
+ 'clause executes a "return" or "break" statement, the saved '
+ 'exception\n'
+ 'is discarded:\n'
+ '\n'
+ ' >>> def f():\n'
+ ' ... try:\n'
+ ' ... 1/0\n'
+ ' ... finally:\n'
+ ' ... return 42\n'
+ ' ...\n'
+ ' >>> f()\n'
+ ' 42\n'
+ '\n'
+ 'The exception information is not available to the program '
+ 'during\n'
+ 'execution of the "finally" clause.\n'
+ '\n'
+ 'When a "return", "break" or "continue" statement is executed in '
+ 'the\n'
+ '"try" suite of a "try"..."finally" statement, the "finally" '
+ 'clause is\n'
+ 'also executed \'on the way out.\' A "continue" statement is '
+ 'illegal in\n'
+ 'the "finally" clause. (The reason is a problem with the current\n'
+ 'implementation --- this restriction may be lifted in the '
+ 'future).\n'
+ '\n'
+ 'The return value of a function is determined by the last '
+ '"return"\n'
+ 'statement executed. Since the "finally" clause always executes, '
+ 'a\n'
+ '"return" statement executed in the "finally" clause will always '
+ 'be the\n'
+ 'last one executed:\n'
+ '\n'
+ ' >>> def foo():\n'
+ ' ... try:\n'
+ " ... return 'try'\n"
+ ' ... finally:\n'
+ " ... return 'finally'\n"
+ ' ...\n'
+ ' >>> foo()\n'
+ " 'finally'\n"
+ '\n'
+ 'Additional information on exceptions can be found in section\n'
+ 'Exceptions, and information on using the "raise" statement to '
+ 'generate\n'
+ 'exceptions may be found in section The raise statement.\n'
+ '\n'
+ '\n'
+ 'The "with" statement\n'
+ '====================\n'
+ '\n'
+ 'The "with" statement is used to wrap the execution of a block '
+ 'with\n'
+ 'methods defined by a context manager (see section With '
+ 'Statement\n'
+ 'Context Managers). This allows common '
+ '"try"..."except"..."finally"\n'
+ 'usage patterns to be encapsulated for convenient reuse.\n'
+ '\n'
+ ' with_stmt ::= "with" with_item ("," with_item)* ":" suite\n'
+ ' with_item ::= expression ["as" target]\n'
+ '\n'
+ 'The execution of the "with" statement with one "item" proceeds '
+ 'as\n'
+ 'follows:\n'
+ '\n'
+ '1. The context expression (the expression given in the '
+ '"with_item")\n'
+ ' is evaluated to obtain a context manager.\n'
+ '\n'
+ '2. The context manager\'s "__exit__()" is loaded for later use.\n'
+ '\n'
+ '3. The context manager\'s "__enter__()" method is invoked.\n'
+ '\n'
+ '4. If a target was included in the "with" statement, the return\n'
+ ' value from "__enter__()" is assigned to it.\n'
+ '\n'
+ ' Note: The "with" statement guarantees that if the '
+ '"__enter__()"\n'
+ ' method returns without an error, then "__exit__()" will '
+ 'always be\n'
+ ' called. Thus, if an error occurs during the assignment to '
+ 'the\n'
+ ' target list, it will be treated the same as an error '
+ 'occurring\n'
+ ' within the suite would be. See step 6 below.\n'
+ '\n'
+ '5. The suite is executed.\n'
+ '\n'
+ '6. The context manager\'s "__exit__()" method is invoked. If '
+ 'an\n'
+ ' exception caused the suite to be exited, its type, value, '
+ 'and\n'
+ ' traceback are passed as arguments to "__exit__()". Otherwise, '
+ 'three\n'
+ ' "None" arguments are supplied.\n'
+ '\n'
+ ' If the suite was exited due to an exception, and the return '
+ 'value\n'
+ ' from the "__exit__()" method was false, the exception is '
+ 'reraised.\n'
+ ' If the return value was true, the exception is suppressed, '
+ 'and\n'
+ ' execution continues with the statement following the "with"\n'
+ ' statement.\n'
+ '\n'
+ ' If the suite was exited for any reason other than an '
+ 'exception, the\n'
+ ' return value from "__exit__()" is ignored, and execution '
+ 'proceeds\n'
+ ' at the normal location for the kind of exit that was taken.\n'
+ '\n'
+ 'With more than one item, the context managers are processed as '
+ 'if\n'
+ 'multiple "with" statements were nested:\n'
+ '\n'
+ ' with A() as a, B() as b:\n'
+ ' suite\n'
+ '\n'
+ 'is equivalent to\n'
+ '\n'
+ ' with A() as a:\n'
+ ' with B() as b:\n'
+ ' suite\n'
+ '\n'
+ 'Changed in version 3.1: Support for multiple context '
+ 'expressions.\n'
+ '\n'
+ 'See also:\n'
+ '\n'
+ ' **PEP 343** - The "with" statement\n'
+ ' The specification, background, and examples for the Python '
+ '"with"\n'
+ ' statement.\n'
+ '\n'
+ '\n'
+ 'Function definitions\n'
+ '====================\n'
+ '\n'
+ 'A function definition defines a user-defined function object '
+ '(see\n'
+ 'section The standard type hierarchy):\n'
+ '\n'
+ ' funcdef ::= [decorators] "def" funcname "(" '
+ '[parameter_list] ")" ["->" expression] ":" suite\n'
+ ' decorators ::= decorator+\n'
+ ' decorator ::= "@" dotted_name ["(" '
+ '[argument_list [","]] ")"] NEWLINE\n'
+ ' dotted_name ::= identifier ("." identifier)*\n'
+ ' parameter_list ::= defparameter ("," defparameter)* '
+ '["," [parameter_list_starargs]]\n'
+ ' | parameter_list_starargs\n'
+ ' parameter_list_starargs ::= "*" [parameter] ("," '
+ 'defparameter)* ["," ["**" parameter [","]]]\n'
+ ' | "**" parameter [","]\n'
+ ' parameter ::= identifier [":" expression]\n'
+ ' defparameter ::= parameter ["=" expression]\n'
+ ' funcname ::= identifier\n'
+ '\n'
+ 'A function definition is an executable statement. Its execution '
+ 'binds\n'
+ 'the function name in the current local namespace to a function '
+ 'object\n'
+ '(a wrapper around the executable code for the function). This\n'
+ 'function object contains a reference to the current global '
+ 'namespace\n'
+ 'as the global namespace to be used when the function is called.\n'
+ '\n'
+ 'The function definition does not execute the function body; this '
+ 'gets\n'
+ 'executed only when the function is called. [3]\n'
+ '\n'
+ 'A function definition may be wrapped by one or more *decorator*\n'
+ 'expressions. Decorator expressions are evaluated when the '
+ 'function is\n'
+ 'defined, in the scope that contains the function definition. '
+ 'The\n'
+ 'result must be a callable, which is invoked with the function '
+ 'object\n'
+ 'as the only argument. The returned value is bound to the '
+ 'function name\n'
+ 'instead of the function object. Multiple decorators are applied '
+ 'in\n'
+ 'nested fashion. For example, the following code\n'
+ '\n'
+ ' @f1(arg)\n'
+ ' @f2\n'
+ ' def func(): pass\n'
+ '\n'
+ 'is roughly equivalent to\n'
+ '\n'
+ ' def func(): pass\n'
+ ' func = f1(arg)(f2(func))\n'
+ '\n'
+ 'except that the original function is not temporarily bound to '
+ 'the name\n'
+ '"func".\n'
+ '\n'
+ 'When one or more *parameters* have the form *parameter* "="\n'
+ '*expression*, the function is said to have "default parameter '
+ 'values."\n'
+ 'For a parameter with a default value, the corresponding '
+ '*argument* may\n'
+ "be omitted from a call, in which case the parameter's default "
+ 'value is\n'
+ 'substituted. If a parameter has a default value, all following\n'
+ 'parameters up until the ""*"" must also have a default value --- '
+ 'this\n'
+ 'is a syntactic restriction that is not expressed by the '
+ 'grammar.\n'
+ '\n'
+ '**Default parameter values are evaluated from left to right when '
+ 'the\n'
+ 'function definition is executed.** This means that the '
+ 'expression is\n'
+ 'evaluated once, when the function is defined, and that the same '
+ '"pre-\n'
+ 'computed" value is used for each call. This is especially '
+ 'important\n'
+ 'to understand when a default parameter is a mutable object, such '
+ 'as a\n'
+ 'list or a dictionary: if the function modifies the object (e.g. '
+ 'by\n'
+ 'appending an item to a list), the default value is in effect '
+ 'modified.\n'
+ 'This is generally not what was intended. A way around this is '
+ 'to use\n'
+ '"None" as the default, and explicitly test for it in the body of '
+ 'the\n'
+ 'function, e.g.:\n'
+ '\n'
+ ' def whats_on_the_telly(penguin=None):\n'
+ ' if penguin is None:\n'
+ ' penguin = []\n'
+ ' penguin.append("property of the zoo")\n'
+ ' return penguin\n'
+ '\n'
+ 'Function call semantics are described in more detail in section '
+ 'Calls.\n'
+ 'A function call always assigns values to all parameters '
+ 'mentioned in\n'
+ 'the parameter list, either from position arguments, from '
+ 'keyword\n'
+ 'arguments, or from default values. If the form ""*identifier"" '
+ 'is\n'
+ 'present, it is initialized to a tuple receiving any excess '
+ 'positional\n'
+ 'parameters, defaulting to the empty tuple. If the form\n'
+ '""**identifier"" is present, it is initialized to a new ordered\n'
+ 'mapping receiving any excess keyword arguments, defaulting to a '
+ 'new\n'
+ 'empty mapping of the same type. Parameters after ""*"" or\n'
+ '""*identifier"" are keyword-only parameters and may only be '
+ 'passed\n'
+ 'used keyword arguments.\n'
+ '\n'
+ 'Parameters may have annotations of the form "": expression"" '
+ 'following\n'
+ 'the parameter name. Any parameter may have an annotation even '
+ 'those\n'
+ 'of the form "*identifier" or "**identifier". Functions may '
+ 'have\n'
+ '"return" annotation of the form ""-> expression"" after the '
+ 'parameter\n'
+ 'list. These annotations can be any valid Python expression and '
+ 'are\n'
+ 'evaluated when the function definition is executed. Annotations '
+ 'may\n'
+ 'be evaluated in a different order than they appear in the source '
+ 'code.\n'
+ 'The presence of annotations does not change the semantics of a\n'
+ 'function. The annotation values are available as values of a\n'
+ "dictionary keyed by the parameters' names in the "
+ '"__annotations__"\n'
+ 'attribute of the function object.\n'
+ '\n'
+ 'It is also possible to create anonymous functions (functions not '
+ 'bound\n'
+ 'to a name), for immediate use in expressions. This uses lambda\n'
+ 'expressions, described in section Lambdas. Note that the '
+ 'lambda\n'
+ 'expression is merely a shorthand for a simplified function '
+ 'definition;\n'
+ 'a function defined in a ""def"" statement can be passed around '
+ 'or\n'
+ 'assigned to another name just like a function defined by a '
+ 'lambda\n'
+ 'expression. The ""def"" form is actually more powerful since '
+ 'it\n'
+ 'allows the execution of multiple statements and annotations.\n'
+ '\n'
+ "**Programmer's note:** Functions are first-class objects. A "
+ '""def""\n'
+ 'statement executed inside a function definition defines a local\n'
+ 'function that can be returned or passed around. Free variables '
+ 'used\n'
+ 'in the nested function can access the local variables of the '
+ 'function\n'
+ 'containing the def. See section Naming and binding for '
+ 'details.\n'
+ '\n'
+ 'See also:\n'
+ '\n'
+ ' **PEP 3107** - Function Annotations\n'
+ ' The original specification for function annotations.\n'
+ '\n'
+ '\n'
+ 'Class definitions\n'
+ '=================\n'
+ '\n'
+ 'A class definition defines a class object (see section The '
+ 'standard\n'
+ 'type hierarchy):\n'
+ '\n'
+ ' classdef ::= [decorators] "class" classname [inheritance] '
+ '":" suite\n'
+ ' inheritance ::= "(" [argument_list] ")"\n'
+ ' classname ::= identifier\n'
+ '\n'
+ 'A class definition is an executable statement. The inheritance '
+ 'list\n'
+ 'usually gives a list of base classes (see Metaclasses for more\n'
+ 'advanced uses), so each item in the list should evaluate to a '
+ 'class\n'
+ 'object which allows subclassing. Classes without an inheritance '
+ 'list\n'
+ 'inherit, by default, from the base class "object"; hence,\n'
+ '\n'
+ ' class Foo:\n'
+ ' pass\n'
+ '\n'
+ 'is equivalent to\n'
+ '\n'
+ ' class Foo(object):\n'
+ ' pass\n'
+ '\n'
+ "The class's suite is then executed in a new execution frame "
+ '(see\n'
+ 'Naming and binding), using a newly created local namespace and '
+ 'the\n'
+ 'original global namespace. (Usually, the suite contains mostly\n'
+ "function definitions.) When the class's suite finishes "
+ 'execution, its\n'
+ 'execution frame is discarded but its local namespace is saved. '
+ '[4] A\n'
+ 'class object is then created using the inheritance list for the '
+ 'base\n'
+ 'classes and the saved local namespace for the attribute '
+ 'dictionary.\n'
+ 'The class name is bound to this class object in the original '
+ 'local\n'
+ 'namespace.\n'
+ '\n'
+ 'The order in which attributes are defined in the class body is\n'
+ 'preserved in the new class\'s "__dict__". Note that this is '
+ 'reliable\n'
+ 'only right after the class is created and only for classes that '
+ 'were\n'
+ 'defined using the definition syntax.\n'
+ '\n'
+ 'Class creation can be customized heavily using metaclasses.\n'
+ '\n'
+ 'Classes can also be decorated: just like when decorating '
+ 'functions,\n'
+ '\n'
+ ' @f1(arg)\n'
+ ' @f2\n'
+ ' class Foo: pass\n'
+ '\n'
+ 'is roughly equivalent to\n'
+ '\n'
+ ' class Foo: pass\n'
+ ' Foo = f1(arg)(f2(Foo))\n'
+ '\n'
+ 'The evaluation rules for the decorator expressions are the same '
+ 'as for\n'
+ 'function decorators. The result is then bound to the class '
+ 'name.\n'
+ '\n'
+ "**Programmer's note:** Variables defined in the class definition "
+ 'are\n'
+ 'class attributes; they are shared by instances. Instance '
+ 'attributes\n'
+ 'can be set in a method with "self.name = value". Both class '
+ 'and\n'
+ 'instance attributes are accessible through the notation '
+ '""self.name"",\n'
+ 'and an instance attribute hides a class attribute with the same '
+ 'name\n'
+ 'when accessed in this way. Class attributes can be used as '
+ 'defaults\n'
+ 'for instance attributes, but using mutable values there can lead '
+ 'to\n'
+ 'unexpected results. Descriptors can be used to create instance\n'
+ 'variables with different implementation details.\n'
+ '\n'
+ 'See also: **PEP 3115** - Metaclasses in Python 3 **PEP 3129** -\n'
+ ' Class Decorators\n'
+ '\n'
+ '\n'
+ 'Coroutines\n'
+ '==========\n'
+ '\n'
+ 'New in version 3.5.\n'
+ '\n'
+ '\n'
+ 'Coroutine function definition\n'
+ '-----------------------------\n'
+ '\n'
+ ' async_funcdef ::= [decorators] "async" "def" funcname "(" '
+ '[parameter_list] ")" ["->" expression] ":" suite\n'
+ '\n'
+ 'Execution of Python coroutines can be suspended and resumed at '
+ 'many\n'
+ 'points (see *coroutine*). In the body of a coroutine, any '
+ '"await" and\n'
+ '"async" identifiers become reserved keywords; "await" '
+ 'expressions,\n'
+ '"async for" and "async with" can only be used in coroutine '
+ 'bodies.\n'
+ '\n'
+ 'Functions defined with "async def" syntax are always coroutine\n'
+ 'functions, even if they do not contain "await" or "async" '
+ 'keywords.\n'
+ '\n'
+ 'It is a "SyntaxError" to use "yield" expressions in "async def"\n'
+ 'coroutines.\n'
+ '\n'
+ 'An example of a coroutine function:\n'
+ '\n'
+ ' async def func(param1, param2):\n'
+ ' do_stuff()\n'
+ ' await some_coroutine()\n'
+ '\n'
+ '\n'
+ 'The "async for" statement\n'
+ '-------------------------\n'
+ '\n'
+ ' async_for_stmt ::= "async" for_stmt\n'
+ '\n'
+ 'An *asynchronous iterable* is able to call asynchronous code in '
+ 'its\n'
+ '*iter* implementation, and *asynchronous iterator* can call\n'
+ 'asynchronous code in its *next* method.\n'
+ '\n'
+ 'The "async for" statement allows convenient iteration over\n'
+ 'asynchronous iterators.\n'
+ '\n'
+ 'The following code:\n'
+ '\n'
+ ' async for TARGET in ITER:\n'
+ ' BLOCK\n'
+ ' else:\n'
+ ' BLOCK2\n'
+ '\n'
+ 'Is semantically equivalent to:\n'
+ '\n'
+ ' iter = (ITER)\n'
+ ' iter = type(iter).__aiter__(iter)\n'
+ ' running = True\n'
+ ' while running:\n'
+ ' try:\n'
+ ' TARGET = await type(iter).__anext__(iter)\n'
+ ' except StopAsyncIteration:\n'
+ ' running = False\n'
+ ' else:\n'
+ ' BLOCK\n'
+ ' else:\n'
+ ' BLOCK2\n'
+ '\n'
+ 'See also "__aiter__()" and "__anext__()" for details.\n'
+ '\n'
+ 'It is a "SyntaxError" to use "async for" statement outside of '
+ 'an\n'
+ '"async def" function.\n'
+ '\n'
+ '\n'
+ 'The "async with" statement\n'
+ '--------------------------\n'
+ '\n'
+ ' async_with_stmt ::= "async" with_stmt\n'
+ '\n'
+ 'An *asynchronous context manager* is a *context manager* that is '
+ 'able\n'
+ 'to suspend execution in its *enter* and *exit* methods.\n'
+ '\n'
+ 'The following code:\n'
+ '\n'
+ ' async with EXPR as VAR:\n'
+ ' BLOCK\n'
+ '\n'
+ 'Is semantically equivalent to:\n'
+ '\n'
+ ' mgr = (EXPR)\n'
+ ' aexit = type(mgr).__aexit__\n'
+ ' aenter = type(mgr).__aenter__(mgr)\n'
+ ' exc = True\n'
+ '\n'
+ ' VAR = await aenter\n'
+ ' try:\n'
+ ' BLOCK\n'
+ ' except:\n'
+ ' if not await aexit(mgr, *sys.exc_info()):\n'
+ ' raise\n'
+ ' else:\n'
+ ' await aexit(mgr, None, None, None)\n'
+ '\n'
+ 'See also "__aenter__()" and "__aexit__()" for details.\n'
+ '\n'
+ 'It is a "SyntaxError" to use "async with" statement outside of '
+ 'an\n'
+ '"async def" function.\n'
+ '\n'
+ 'See also: **PEP 492** - Coroutines with async and await syntax\n'
+ '\n'
+ '-[ Footnotes ]-\n'
+ '\n'
+ '[1] The exception is propagated to the invocation stack unless\n'
+ ' there is a "finally" clause which happens to raise another\n'
+ ' exception. That new exception causes the old one to be '
+ 'lost.\n'
+ '\n'
+ '[2] Currently, control "flows off the end" except in the case '
+ 'of\n'
+ ' an exception or the execution of a "return", "continue", or\n'
+ ' "break" statement.\n'
+ '\n'
+ '[3] A string literal appearing as the first statement in the\n'
+ ' function body is transformed into the function\'s "__doc__"\n'
+ " attribute and therefore the function's *docstring*.\n"
+ '\n'
+ '[4] A string literal appearing as the first statement in the '
+ 'class\n'
+ ' body is transformed into the namespace\'s "__doc__" item '
+ 'and\n'
+ " therefore the class's *docstring*.\n",
+ 'context-managers': '\n'
+ 'With Statement Context Managers\n'
+ '*******************************\n'
+ '\n'
+ 'A *context manager* is an object that defines the '
+ 'runtime context to\n'
+ 'be established when executing a "with" statement. The '
+ 'context manager\n'
+ 'handles the entry into, and the exit from, the desired '
+ 'runtime context\n'
+ 'for the execution of the block of code. Context '
+ 'managers are normally\n'
+ 'invoked using the "with" statement (described in section '
+ 'The with\n'
+ 'statement), but can also be used by directly invoking '
+ 'their methods.\n'
+ '\n'
+ 'Typical uses of context managers include saving and '
+ 'restoring various\n'
+ 'kinds of global state, locking and unlocking resources, '
+ 'closing opened\n'
+ 'files, etc.\n'
+ '\n'
+ 'For more information on context managers, see Context '
+ 'Manager Types.\n'
+ '\n'
+ 'object.__enter__(self)\n'
+ '\n'
+ ' Enter the runtime context related to this object. The '
+ '"with"\n'
+ " statement will bind this method's return value to the "
+ 'target(s)\n'
+ ' specified in the "as" clause of the statement, if '
+ 'any.\n'
+ '\n'
+ 'object.__exit__(self, exc_type, exc_value, traceback)\n'
+ '\n'
+ ' Exit the runtime context related to this object. The '
+ 'parameters\n'
+ ' describe the exception that caused the context to be '
+ 'exited. If the\n'
+ ' context was exited without an exception, all three '
+ 'arguments will\n'
+ ' be "None".\n'
+ '\n'
+ ' If an exception is supplied, and the method wishes to '
+ 'suppress the\n'
+ ' exception (i.e., prevent it from being propagated), '
+ 'it should\n'
+ ' return a true value. Otherwise, the exception will be '
+ 'processed\n'
+ ' normally upon exit from this method.\n'
+ '\n'
+ ' Note that "__exit__()" methods should not reraise the '
+ 'passed-in\n'
+ " exception; this is the caller's responsibility.\n"
+ '\n'
+ 'See also:\n'
+ '\n'
+ ' **PEP 343** - The "with" statement\n'
+ ' The specification, background, and examples for the '
+ 'Python "with"\n'
+ ' statement.\n',
+ 'continue': '\n'
+ 'The "continue" statement\n'
+ '************************\n'
+ '\n'
+ ' continue_stmt ::= "continue"\n'
+ '\n'
+ '"continue" may only occur syntactically nested in a "for" or '
+ '"while"\n'
+ 'loop, but not nested in a function or class definition or '
+ '"finally"\n'
+ 'clause within that loop. It continues with the next cycle of '
+ 'the\n'
+ 'nearest enclosing loop.\n'
+ '\n'
+ 'When "continue" passes control out of a "try" statement with a\n'
+ '"finally" clause, that "finally" clause is executed before '
+ 'really\n'
+ 'starting the next loop cycle.\n',
+ 'conversions': '\n'
+ 'Arithmetic conversions\n'
+ '**********************\n'
+ '\n'
+ 'When a description of an arithmetic operator below uses the '
+ 'phrase\n'
+ '"the numeric arguments are converted to a common type," this '
+ 'means\n'
+ 'that the operator implementation for built-in types works as '
+ 'follows:\n'
+ '\n'
+ '* If either argument is a complex number, the other is '
+ 'converted to\n'
+ ' complex;\n'
+ '\n'
+ '* otherwise, if either argument is a floating point number, '
+ 'the\n'
+ ' other is converted to floating point;\n'
+ '\n'
+ '* otherwise, both must be integers and no conversion is '
+ 'necessary.\n'
+ '\n'
+ 'Some additional rules apply for certain operators (e.g., a '
+ 'string as a\n'
+ "left argument to the '%' operator). Extensions must define "
+ 'their own\n'
+ 'conversion behavior.\n',
+ 'customization': '\n'
+ 'Basic customization\n'
+ '*******************\n'
+ '\n'
+ 'object.__new__(cls[, ...])\n'
+ '\n'
+ ' Called to create a new instance of class *cls*. '
+ '"__new__()" is a\n'
+ ' static method (special-cased so you need not declare it '
+ 'as such)\n'
+ ' that takes the class of which an instance was requested '
+ 'as its\n'
+ ' first argument. The remaining arguments are those '
+ 'passed to the\n'
+ ' object constructor expression (the call to the class). '
+ 'The return\n'
+ ' value of "__new__()" should be the new object instance '
+ '(usually an\n'
+ ' instance of *cls*).\n'
+ '\n'
+ ' Typical implementations create a new instance of the '
+ 'class by\n'
+ ' invoking the superclass\'s "__new__()" method using\n'
+ ' "super(currentclass, cls).__new__(cls[, ...])" with '
+ 'appropriate\n'
+ ' arguments and then modifying the newly-created instance '
+ 'as\n'
+ ' necessary before returning it.\n'
+ '\n'
+ ' If "__new__()" returns an instance of *cls*, then the '
+ 'new\n'
+ ' instance\'s "__init__()" method will be invoked like\n'
+ ' "__init__(self[, ...])", where *self* is the new '
+ 'instance and the\n'
+ ' remaining arguments are the same as were passed to '
+ '"__new__()".\n'
+ '\n'
+ ' If "__new__()" does not return an instance of *cls*, '
+ 'then the new\n'
+ ' instance\'s "__init__()" method will not be invoked.\n'
+ '\n'
+ ' "__new__()" is intended mainly to allow subclasses of '
+ 'immutable\n'
+ ' types (like int, str, or tuple) to customize instance '
+ 'creation. It\n'
+ ' is also commonly overridden in custom metaclasses in '
+ 'order to\n'
+ ' customize class creation.\n'
+ '\n'
+ 'object.__init__(self[, ...])\n'
+ '\n'
+ ' Called after the instance has been created (by '
+ '"__new__()"), but\n'
+ ' before it is returned to the caller. The arguments are '
+ 'those\n'
+ ' passed to the class constructor expression. If a base '
+ 'class has an\n'
+ ' "__init__()" method, the derived class\'s "__init__()" '
+ 'method, if\n'
+ ' any, must explicitly call it to ensure proper '
+ 'initialization of the\n'
+ ' base class part of the instance; for example:\n'
+ ' "BaseClass.__init__(self, [args...])".\n'
+ '\n'
+ ' Because "__new__()" and "__init__()" work together in '
+ 'constructing\n'
+ ' objects ("__new__()" to create it, and "__init__()" to '
+ 'customize\n'
+ ' it), no non-"None" value may be returned by '
+ '"__init__()"; doing so\n'
+ ' will cause a "TypeError" to be raised at runtime.\n'
+ '\n'
+ 'object.__del__(self)\n'
+ '\n'
+ ' Called when the instance is about to be destroyed. This '
+ 'is also\n'
+ ' called a destructor. If a base class has a "__del__()" '
+ 'method, the\n'
+ ' derived class\'s "__del__()" method, if any, must '
+ 'explicitly call it\n'
+ ' to ensure proper deletion of the base class part of the '
+ 'instance.\n'
+ ' Note that it is possible (though not recommended!) for '
+ 'the\n'
+ ' "__del__()" method to postpone destruction of the '
+ 'instance by\n'
+ ' creating a new reference to it. It may then be called '
+ 'at a later\n'
+ ' time when this new reference is deleted. It is not '
+ 'guaranteed that\n'
+ ' "__del__()" methods are called for objects that still '
+ 'exist when\n'
+ ' the interpreter exits.\n'
+ '\n'
+ ' Note: "del x" doesn\'t directly call "x.__del__()" --- '
+ 'the former\n'
+ ' decrements the reference count for "x" by one, and the '
+ 'latter is\n'
+ ' only called when "x"\'s reference count reaches zero. '
+ 'Some common\n'
+ ' situations that may prevent the reference count of an '
+ 'object from\n'
+ ' going to zero include: circular references between '
+ 'objects (e.g.,\n'
+ ' a doubly-linked list or a tree data structure with '
+ 'parent and\n'
+ ' child pointers); a reference to the object on the '
+ 'stack frame of\n'
+ ' a function that caught an exception (the traceback '
+ 'stored in\n'
+ ' "sys.exc_info()[2]" keeps the stack frame alive); or a '
+ 'reference\n'
+ ' to the object on the stack frame that raised an '
+ 'unhandled\n'
+ ' exception in interactive mode (the traceback stored '
+ 'in\n'
+ ' "sys.last_traceback" keeps the stack frame alive). '
+ 'The first\n'
+ ' situation can only be remedied by explicitly breaking '
+ 'the cycles;\n'
+ ' the second can be resolved by freeing the reference to '
+ 'the\n'
+ ' traceback object when it is no longer useful, and the '
+ 'third can\n'
+ ' be resolved by storing "None" in "sys.last_traceback". '
+ 'Circular\n'
+ ' references which are garbage are detected and cleaned '
+ 'up when the\n'
+ " cyclic garbage collector is enabled (it's on by "
+ 'default). Refer\n'
+ ' to the documentation for the "gc" module for more '
+ 'information\n'
+ ' about this topic.\n'
+ '\n'
+ ' Warning: Due to the precarious circumstances under '
+ 'which\n'
+ ' "__del__()" methods are invoked, exceptions that occur '
+ 'during\n'
+ ' their execution are ignored, and a warning is printed '
+ 'to\n'
+ ' "sys.stderr" instead. Also, when "__del__()" is '
+ 'invoked in\n'
+ ' response to a module being deleted (e.g., when '
+ 'execution of the\n'
+ ' program is done), other globals referenced by the '
+ '"__del__()"\n'
+ ' method may already have been deleted or in the process '
+ 'of being\n'
+ ' torn down (e.g. the import machinery shutting down). '
+ 'For this\n'
+ ' reason, "__del__()" methods should do the absolute '
+ 'minimum needed\n'
+ ' to maintain external invariants. Starting with '
+ 'version 1.5,\n'
+ ' Python guarantees that globals whose name begins with '
+ 'a single\n'
+ ' underscore are deleted from their module before other '
+ 'globals are\n'
+ ' deleted; if no other references to such globals exist, '
+ 'this may\n'
+ ' help in assuring that imported modules are still '
+ 'available at the\n'
+ ' time when the "__del__()" method is called.\n'
+ '\n'
+ 'object.__repr__(self)\n'
+ '\n'
+ ' Called by the "repr()" built-in function to compute the '
+ '"official"\n'
+ ' string representation of an object. If at all possible, '
+ 'this\n'
+ ' should look like a valid Python expression that could be '
+ 'used to\n'
+ ' recreate an object with the same value (given an '
+ 'appropriate\n'
+ ' environment). If this is not possible, a string of the '
+ 'form\n'
+ ' "<...some useful description...>" should be returned. '
+ 'The return\n'
+ ' value must be a string object. If a class defines '
+ '"__repr__()" but\n'
+ ' not "__str__()", then "__repr__()" is also used when an '
+ '"informal"\n'
+ ' string representation of instances of that class is '
+ 'required.\n'
+ '\n'
+ ' This is typically used for debugging, so it is important '
+ 'that the\n'
+ ' representation is information-rich and unambiguous.\n'
+ '\n'
+ 'object.__str__(self)\n'
+ '\n'
+ ' Called by "str(object)" and the built-in functions '
+ '"format()" and\n'
+ ' "print()" to compute the "informal" or nicely printable '
+ 'string\n'
+ ' representation of an object. The return value must be a '
+ 'string\n'
+ ' object.\n'
+ '\n'
+ ' This method differs from "object.__repr__()" in that '
+ 'there is no\n'
+ ' expectation that "__str__()" return a valid Python '
+ 'expression: a\n'
+ ' more convenient or concise representation can be used.\n'
+ '\n'
+ ' The default implementation defined by the built-in type '
+ '"object"\n'
+ ' calls "object.__repr__()".\n'
+ '\n'
+ 'object.__bytes__(self)\n'
+ '\n'
+ ' Called by "bytes()" to compute a byte-string '
+ 'representation of an\n'
+ ' object. This should return a "bytes" object.\n'
+ '\n'
+ 'object.__format__(self, format_spec)\n'
+ '\n'
+ ' Called by the "format()" built-in function, and by '
+ 'extension,\n'
+ ' evaluation of formatted string literals and the '
+ '"str.format()"\n'
+ ' method, to produce a "formatted" string representation '
+ 'of an\n'
+ ' object. The "format_spec" argument is a string that '
+ 'contains a\n'
+ ' description of the formatting options desired. The '
+ 'interpretation\n'
+ ' of the "format_spec" argument is up to the type '
+ 'implementing\n'
+ ' "__format__()", however most classes will either '
+ 'delegate\n'
+ ' formatting to one of the built-in types, or use a '
+ 'similar\n'
+ ' formatting option syntax.\n'
+ '\n'
+ ' See Format Specification Mini-Language for a description '
+ 'of the\n'
+ ' standard formatting syntax.\n'
+ '\n'
+ ' The return value must be a string object.\n'
+ '\n'
+ ' Changed in version 3.4: The __format__ method of '
+ '"object" itself\n'
+ ' raises a "TypeError" if passed any non-empty string.\n'
+ '\n'
+ 'object.__lt__(self, other)\n'
+ 'object.__le__(self, other)\n'
+ 'object.__eq__(self, other)\n'
+ 'object.__ne__(self, other)\n'
+ 'object.__gt__(self, other)\n'
+ 'object.__ge__(self, other)\n'
+ '\n'
+ ' These are the so-called "rich comparison" methods. The\n'
+ ' correspondence between operator symbols and method names '
+ 'is as\n'
+ ' follows: "x<y" calls "x.__lt__(y)", "x<=y" calls '
+ '"x.__le__(y)",\n'
+ ' "x==y" calls "x.__eq__(y)", "x!=y" calls "x.__ne__(y)", '
+ '"x>y" calls\n'
+ ' "x.__gt__(y)", and "x>=y" calls "x.__ge__(y)".\n'
+ '\n'
+ ' A rich comparison method may return the singleton '
+ '"NotImplemented"\n'
+ ' if it does not implement the operation for a given pair '
+ 'of\n'
+ ' arguments. By convention, "False" and "True" are '
+ 'returned for a\n'
+ ' successful comparison. However, these methods can return '
+ 'any value,\n'
+ ' so if the comparison operator is used in a Boolean '
+ 'context (e.g.,\n'
+ ' in the condition of an "if" statement), Python will call '
+ '"bool()"\n'
+ ' on the value to determine if the result is true or '
+ 'false.\n'
+ '\n'
+ ' By default, "__ne__()" delegates to "__eq__()" and '
+ 'inverts the\n'
+ ' result unless it is "NotImplemented". There are no '
+ 'other implied\n'
+ ' relationships among the comparison operators, for '
+ 'example, the\n'
+ ' truth of "(x<y or x==y)" does not imply "x<=y". To '
+ 'automatically\n'
+ ' generate ordering operations from a single root '
+ 'operation, see\n'
+ ' "functools.total_ordering()".\n'
+ '\n'
+ ' See the paragraph on "__hash__()" for some important '
+ 'notes on\n'
+ ' creating *hashable* objects which support custom '
+ 'comparison\n'
+ ' operations and are usable as dictionary keys.\n'
+ '\n'
+ ' There are no swapped-argument versions of these methods '
+ '(to be used\n'
+ ' when the left argument does not support the operation '
+ 'but the right\n'
+ ' argument does); rather, "__lt__()" and "__gt__()" are '
+ "each other's\n"
+ ' reflection, "__le__()" and "__ge__()" are each other\'s '
+ 'reflection,\n'
+ ' and "__eq__()" and "__ne__()" are their own reflection. '
+ 'If the\n'
+ " operands are of different types, and right operand's "
+ 'type is a\n'
+ " direct or indirect subclass of the left operand's type, "
+ 'the\n'
+ ' reflected method of the right operand has priority, '
+ 'otherwise the\n'
+ " left operand's method has priority. Virtual subclassing "
+ 'is not\n'
+ ' considered.\n'
+ '\n'
+ 'object.__hash__(self)\n'
+ '\n'
+ ' Called by built-in function "hash()" and for operations '
+ 'on members\n'
+ ' of hashed collections including "set", "frozenset", and '
+ '"dict".\n'
+ ' "__hash__()" should return an integer. The only '
+ 'required property\n'
+ ' is that objects which compare equal have the same hash '
+ 'value; it is\n'
+ ' advised to somehow mix together (e.g. using exclusive '
+ 'or) the hash\n'
+ ' values for the components of the object that also play a '
+ 'part in\n'
+ ' comparison of objects.\n'
+ '\n'
+ ' Note: "hash()" truncates the value returned from an '
+ "object's\n"
+ ' custom "__hash__()" method to the size of a '
+ '"Py_ssize_t". This\n'
+ ' is typically 8 bytes on 64-bit builds and 4 bytes on '
+ '32-bit\n'
+ ' builds. If an object\'s "__hash__()" must '
+ 'interoperate on builds\n'
+ ' of different bit sizes, be sure to check the width on '
+ 'all\n'
+ ' supported builds. An easy way to do this is with '
+ '"python -c\n'
+ ' "import sys; print(sys.hash_info.width)"".\n'
+ '\n'
+ ' If a class does not define an "__eq__()" method it '
+ 'should not\n'
+ ' define a "__hash__()" operation either; if it defines '
+ '"__eq__()"\n'
+ ' but not "__hash__()", its instances will not be usable '
+ 'as items in\n'
+ ' hashable collections. If a class defines mutable '
+ 'objects and\n'
+ ' implements an "__eq__()" method, it should not '
+ 'implement\n'
+ ' "__hash__()", since the implementation of hashable '
+ 'collections\n'
+ " requires that a key's hash value is immutable (if the "
+ "object's hash\n"
+ ' value changes, it will be in the wrong hash bucket).\n'
+ '\n'
+ ' User-defined classes have "__eq__()" and "__hash__()" '
+ 'methods by\n'
+ ' default; with them, all objects compare unequal (except '
+ 'with\n'
+ ' themselves) and "x.__hash__()" returns an appropriate '
+ 'value such\n'
+ ' that "x == y" implies both that "x is y" and "hash(x) == '
+ 'hash(y)".\n'
+ '\n'
+ ' A class that overrides "__eq__()" and does not define '
+ '"__hash__()"\n'
+ ' will have its "__hash__()" implicitly set to "None". '
+ 'When the\n'
+ ' "__hash__()" method of a class is "None", instances of '
+ 'the class\n'
+ ' will raise an appropriate "TypeError" when a program '
+ 'attempts to\n'
+ ' retrieve their hash value, and will also be correctly '
+ 'identified as\n'
+ ' unhashable when checking "isinstance(obj, '
+ 'collections.Hashable)".\n'
+ '\n'
+ ' If a class that overrides "__eq__()" needs to retain '
+ 'the\n'
+ ' implementation of "__hash__()" from a parent class, the '
+ 'interpreter\n'
+ ' must be told this explicitly by setting "__hash__ =\n'
+ ' <ParentClass>.__hash__".\n'
+ '\n'
+ ' If a class that does not override "__eq__()" wishes to '
+ 'suppress\n'
+ ' hash support, it should include "__hash__ = None" in the '
+ 'class\n'
+ ' definition. A class which defines its own "__hash__()" '
+ 'that\n'
+ ' explicitly raises a "TypeError" would be incorrectly '
+ 'identified as\n'
+ ' hashable by an "isinstance(obj, collections.Hashable)" '
+ 'call.\n'
+ '\n'
+ ' Note: By default, the "__hash__()" values of str, bytes '
+ 'and\n'
+ ' datetime objects are "salted" with an unpredictable '
+ 'random value.\n'
+ ' Although they remain constant within an individual '
+ 'Python\n'
+ ' process, they are not predictable between repeated '
+ 'invocations of\n'
+ ' Python.This is intended to provide protection against '
+ 'a denial-\n'
+ ' of-service caused by carefully-chosen inputs that '
+ 'exploit the\n'
+ ' worst case performance of a dict insertion, O(n^2) '
+ 'complexity.\n'
+ ' See '
+ 'http://www.ocert.org/advisories/ocert-2011-003.html for\n'
+ ' details.Changing hash values affects the iteration '
+ 'order of\n'
+ ' dicts, sets and other mappings. Python has never made '
+ 'guarantees\n'
+ ' about this ordering (and it typically varies between '
+ '32-bit and\n'
+ ' 64-bit builds).See also "PYTHONHASHSEED".\n'
+ '\n'
+ ' Changed in version 3.3: Hash randomization is enabled by '
+ 'default.\n'
+ '\n'
+ 'object.__bool__(self)\n'
+ '\n'
+ ' Called to implement truth value testing and the built-in '
+ 'operation\n'
+ ' "bool()"; should return "False" or "True". When this '
+ 'method is not\n'
+ ' defined, "__len__()" is called, if it is defined, and '
+ 'the object is\n'
+ ' considered true if its result is nonzero. If a class '
+ 'defines\n'
+ ' neither "__len__()" nor "__bool__()", all its instances '
+ 'are\n'
+ ' considered true.\n',
+ 'debugger': '\n'
+ '"pdb" --- The Python Debugger\n'
+ '*****************************\n'
+ '\n'
+ '**Source code:** Lib/pdb.py\n'
+ '\n'
+ '======================================================================\n'
+ '\n'
+ 'The module "pdb" defines an interactive source code debugger '
+ 'for\n'
+ 'Python programs. It supports setting (conditional) breakpoints '
+ 'and\n'
+ 'single stepping at the source line level, inspection of stack '
+ 'frames,\n'
+ 'source code listing, and evaluation of arbitrary Python code in '
+ 'the\n'
+ 'context of any stack frame. It also supports post-mortem '
+ 'debugging\n'
+ 'and can be called under program control.\n'
+ '\n'
+ 'The debugger is extensible -- it is actually defined as the '
+ 'class\n'
+ '"Pdb". This is currently undocumented but easily understood by '
+ 'reading\n'
+ 'the source. The extension interface uses the modules "bdb" and '
+ '"cmd".\n'
+ '\n'
+ 'The debugger\'s prompt is "(Pdb)". Typical usage to run a '
+ 'program under\n'
+ 'control of the debugger is:\n'
+ '\n'
+ ' >>> import pdb\n'
+ ' >>> import mymodule\n'
+ " >>> pdb.run('mymodule.test()')\n"
+ ' > <string>(0)?()\n'
+ ' (Pdb) continue\n'
+ ' > <string>(1)?()\n'
+ ' (Pdb) continue\n'
+ " NameError: 'spam'\n"
+ ' > <string>(1)?()\n'
+ ' (Pdb)\n'
+ '\n'
+ 'Changed in version 3.3: Tab-completion via the "readline" module '
+ 'is\n'
+ 'available for commands and command arguments, e.g. the current '
+ 'global\n'
+ 'and local names are offered as arguments of the "p" command.\n'
+ '\n'
+ '"pdb.py" can also be invoked as a script to debug other '
+ 'scripts. For\n'
+ 'example:\n'
+ '\n'
+ ' python3 -m pdb myscript.py\n'
+ '\n'
+ 'When invoked as a script, pdb will automatically enter '
+ 'post-mortem\n'
+ 'debugging if the program being debugged exits abnormally. After '
+ 'post-\n'
+ 'mortem debugging (or after normal exit of the program), pdb '
+ 'will\n'
+ "restart the program. Automatic restarting preserves pdb's state "
+ '(such\n'
+ 'as breakpoints) and in most cases is more useful than quitting '
+ 'the\n'
+ "debugger upon program's exit.\n"
+ '\n'
+ 'New in version 3.2: "pdb.py" now accepts a "-c" option that '
+ 'executes\n'
+ 'commands as if given in a ".pdbrc" file, see Debugger Commands.\n'
+ '\n'
+ 'The typical usage to break into the debugger from a running '
+ 'program is\n'
+ 'to insert\n'
+ '\n'
+ ' import pdb; pdb.set_trace()\n'
+ '\n'
+ 'at the location you want to break into the debugger. You can '
+ 'then\n'
+ 'step through the code following this statement, and continue '
+ 'running\n'
+ 'without the debugger using the "continue" command.\n'
+ '\n'
+ 'The typical usage to inspect a crashed program is:\n'
+ '\n'
+ ' >>> import pdb\n'
+ ' >>> import mymodule\n'
+ ' >>> mymodule.test()\n'
+ ' Traceback (most recent call last):\n'
+ ' File "<stdin>", line 1, in ?\n'
+ ' File "./mymodule.py", line 4, in test\n'
+ ' test2()\n'
+ ' File "./mymodule.py", line 3, in test2\n'
+ ' print(spam)\n'
+ ' NameError: spam\n'
+ ' >>> pdb.pm()\n'
+ ' > ./mymodule.py(3)test2()\n'
+ ' -> print(spam)\n'
+ ' (Pdb)\n'
+ '\n'
+ 'The module defines the following functions; each enters the '
+ 'debugger\n'
+ 'in a slightly different way:\n'
+ '\n'
+ 'pdb.run(statement, globals=None, locals=None)\n'
+ '\n'
+ ' Execute the *statement* (given as a string or a code object) '
+ 'under\n'
+ ' debugger control. The debugger prompt appears before any '
+ 'code is\n'
+ ' executed; you can set breakpoints and type "continue", or you '
+ 'can\n'
+ ' step through the statement using "step" or "next" (all these\n'
+ ' commands are explained below). The optional *globals* and '
+ '*locals*\n'
+ ' arguments specify the environment in which the code is '
+ 'executed; by\n'
+ ' default the dictionary of the module "__main__" is used. '
+ '(See the\n'
+ ' explanation of the built-in "exec()" or "eval()" functions.)\n'
+ '\n'
+ 'pdb.runeval(expression, globals=None, locals=None)\n'
+ '\n'
+ ' Evaluate the *expression* (given as a string or a code '
+ 'object)\n'
+ ' under debugger control. When "runeval()" returns, it returns '
+ 'the\n'
+ ' value of the expression. Otherwise this function is similar '
+ 'to\n'
+ ' "run()".\n'
+ '\n'
+ 'pdb.runcall(function, *args, **kwds)\n'
+ '\n'
+ ' Call the *function* (a function or method object, not a '
+ 'string)\n'
+ ' with the given arguments. When "runcall()" returns, it '
+ 'returns\n'
+ ' whatever the function call returned. The debugger prompt '
+ 'appears\n'
+ ' as soon as the function is entered.\n'
+ '\n'
+ 'pdb.set_trace()\n'
+ '\n'
+ ' Enter the debugger at the calling stack frame. This is '
+ 'useful to\n'
+ ' hard-code a breakpoint at a given point in a program, even if '
+ 'the\n'
+ ' code is not otherwise being debugged (e.g. when an assertion\n'
+ ' fails).\n'
+ '\n'
+ 'pdb.post_mortem(traceback=None)\n'
+ '\n'
+ ' Enter post-mortem debugging of the given *traceback* object. '
+ 'If no\n'
+ ' *traceback* is given, it uses the one of the exception that '
+ 'is\n'
+ ' currently being handled (an exception must be being handled '
+ 'if the\n'
+ ' default is to be used).\n'
+ '\n'
+ 'pdb.pm()\n'
+ '\n'
+ ' Enter post-mortem debugging of the traceback found in\n'
+ ' "sys.last_traceback".\n'
+ '\n'
+ 'The "run*" functions and "set_trace()" are aliases for '
+ 'instantiating\n'
+ 'the "Pdb" class and calling the method of the same name. If you '
+ 'want\n'
+ 'to access further features, you have to do this yourself:\n'
+ '\n'
+ "class pdb.Pdb(completekey='tab', stdin=None, stdout=None, "
+ 'skip=None, nosigint=False, readrc=True)\n'
+ '\n'
+ ' "Pdb" is the debugger class.\n'
+ '\n'
+ ' The *completekey*, *stdin* and *stdout* arguments are passed '
+ 'to the\n'
+ ' underlying "cmd.Cmd" class; see the description there.\n'
+ '\n'
+ ' The *skip* argument, if given, must be an iterable of '
+ 'glob-style\n'
+ ' module name patterns. The debugger will not step into frames '
+ 'that\n'
+ ' originate in a module that matches one of these patterns. '
+ '[1]\n'
+ '\n'
+ ' By default, Pdb sets a handler for the SIGINT signal (which '
+ 'is sent\n'
+ ' when the user presses "Ctrl-C" on the console) when you give '
+ 'a\n'
+ ' "continue" command. This allows you to break into the '
+ 'debugger\n'
+ ' again by pressing "Ctrl-C". If you want Pdb not to touch '
+ 'the\n'
+ ' SIGINT handler, set *nosigint* to true.\n'
+ '\n'
+ ' The *readrc* argument defaults to true and controls whether '
+ 'Pdb\n'
+ ' will load .pdbrc files from the filesystem.\n'
+ '\n'
+ ' Example call to enable tracing with *skip*:\n'
+ '\n'
+ " import pdb; pdb.Pdb(skip=['django.*']).set_trace()\n"
+ '\n'
+ ' New in version 3.1: The *skip* argument.\n'
+ '\n'
+ ' New in version 3.2: The *nosigint* argument. Previously, a '
+ 'SIGINT\n'
+ ' handler was never set by Pdb.\n'
+ '\n'
+ ' Changed in version 3.6: The *readrc* argument.\n'
+ '\n'
+ ' run(statement, globals=None, locals=None)\n'
+ ' runeval(expression, globals=None, locals=None)\n'
+ ' runcall(function, *args, **kwds)\n'
+ ' set_trace()\n'
+ '\n'
+ ' See the documentation for the functions explained above.\n'
+ '\n'
+ '\n'
+ 'Debugger Commands\n'
+ '=================\n'
+ '\n'
+ 'The commands recognized by the debugger are listed below. Most\n'
+ 'commands can be abbreviated to one or two letters as indicated; '
+ 'e.g.\n'
+ '"h(elp)" means that either "h" or "help" can be used to enter '
+ 'the help\n'
+ 'command (but not "he" or "hel", nor "H" or "Help" or "HELP").\n'
+ 'Arguments to commands must be separated by whitespace (spaces '
+ 'or\n'
+ 'tabs). Optional arguments are enclosed in square brackets '
+ '("[]") in\n'
+ 'the command syntax; the square brackets must not be typed.\n'
+ 'Alternatives in the command syntax are separated by a vertical '
+ 'bar\n'
+ '("|").\n'
+ '\n'
+ 'Entering a blank line repeats the last command entered. '
+ 'Exception: if\n'
+ 'the last command was a "list" command, the next 11 lines are '
+ 'listed.\n'
+ '\n'
+ "Commands that the debugger doesn't recognize are assumed to be "
+ 'Python\n'
+ 'statements and are executed in the context of the program being\n'
+ 'debugged. Python statements can also be prefixed with an '
+ 'exclamation\n'
+ 'point ("!"). This is a powerful way to inspect the program '
+ 'being\n'
+ 'debugged; it is even possible to change a variable or call a '
+ 'function.\n'
+ 'When an exception occurs in such a statement, the exception name '
+ 'is\n'
+ "printed but the debugger's state is not changed.\n"
+ '\n'
+ 'The debugger supports aliases. Aliases can have parameters '
+ 'which\n'
+ 'allows one a certain level of adaptability to the context under\n'
+ 'examination.\n'
+ '\n'
+ 'Multiple commands may be entered on a single line, separated by '
+ '";;".\n'
+ '(A single ";" is not used as it is the separator for multiple '
+ 'commands\n'
+ 'in a line that is passed to the Python parser.) No intelligence '
+ 'is\n'
+ 'applied to separating the commands; the input is split at the '
+ 'first\n'
+ '";;" pair, even if it is in the middle of a quoted string.\n'
+ '\n'
+ 'If a file ".pdbrc" exists in the user\'s home directory or in '
+ 'the\n'
+ 'current directory, it is read in and executed as if it had been '
+ 'typed\n'
+ 'at the debugger prompt. This is particularly useful for '
+ 'aliases. If\n'
+ 'both files exist, the one in the home directory is read first '
+ 'and\n'
+ 'aliases defined there can be overridden by the local file.\n'
+ '\n'
+ 'Changed in version 3.2: ".pdbrc" can now contain commands that\n'
+ 'continue debugging, such as "continue" or "next". Previously, '
+ 'these\n'
+ 'commands had no effect.\n'
+ '\n'
+ 'h(elp) [command]\n'
+ '\n'
+ ' Without argument, print the list of available commands. With '
+ 'a\n'
+ ' *command* as argument, print help about that command. "help '
+ 'pdb"\n'
+ ' displays the full documentation (the docstring of the "pdb"\n'
+ ' module). Since the *command* argument must be an identifier, '
+ '"help\n'
+ ' exec" must be entered to get help on the "!" command.\n'
+ '\n'
+ 'w(here)\n'
+ '\n'
+ ' Print a stack trace, with the most recent frame at the '
+ 'bottom. An\n'
+ ' arrow indicates the current frame, which determines the '
+ 'context of\n'
+ ' most commands.\n'
+ '\n'
+ 'd(own) [count]\n'
+ '\n'
+ ' Move the current frame *count* (default one) levels down in '
+ 'the\n'
+ ' stack trace (to a newer frame).\n'
+ '\n'
+ 'u(p) [count]\n'
+ '\n'
+ ' Move the current frame *count* (default one) levels up in the '
+ 'stack\n'
+ ' trace (to an older frame).\n'
+ '\n'
+ 'b(reak) [([filename:]lineno | function) [, condition]]\n'
+ '\n'
+ ' With a *lineno* argument, set a break there in the current '
+ 'file.\n'
+ ' With a *function* argument, set a break at the first '
+ 'executable\n'
+ ' statement within that function. The line number may be '
+ 'prefixed\n'
+ ' with a filename and a colon, to specify a breakpoint in '
+ 'another\n'
+ " file (probably one that hasn't been loaded yet). The file "
+ 'is\n'
+ ' searched on "sys.path". Note that each breakpoint is '
+ 'assigned a\n'
+ ' number to which all the other breakpoint commands refer.\n'
+ '\n'
+ ' If a second argument is present, it is an expression which '
+ 'must\n'
+ ' evaluate to true before the breakpoint is honored.\n'
+ '\n'
+ ' Without argument, list all breaks, including for each '
+ 'breakpoint,\n'
+ ' the number of times that breakpoint has been hit, the '
+ 'current\n'
+ ' ignore count, and the associated condition if any.\n'
+ '\n'
+ 'tbreak [([filename:]lineno | function) [, condition]]\n'
+ '\n'
+ ' Temporary breakpoint, which is removed automatically when it '
+ 'is\n'
+ ' first hit. The arguments are the same as for "break".\n'
+ '\n'
+ 'cl(ear) [filename:lineno | bpnumber [bpnumber ...]]\n'
+ '\n'
+ ' With a *filename:lineno* argument, clear all the breakpoints '
+ 'at\n'
+ ' this line. With a space separated list of breakpoint numbers, '
+ 'clear\n'
+ ' those breakpoints. Without argument, clear all breaks (but '
+ 'first\n'
+ ' ask confirmation).\n'
+ '\n'
+ 'disable [bpnumber [bpnumber ...]]\n'
+ '\n'
+ ' Disable the breakpoints given as a space separated list of\n'
+ ' breakpoint numbers. Disabling a breakpoint means it cannot '
+ 'cause\n'
+ ' the program to stop execution, but unlike clearing a '
+ 'breakpoint, it\n'
+ ' remains in the list of breakpoints and can be (re-)enabled.\n'
+ '\n'
+ 'enable [bpnumber [bpnumber ...]]\n'
+ '\n'
+ ' Enable the breakpoints specified.\n'
+ '\n'
+ 'ignore bpnumber [count]\n'
+ '\n'
+ ' Set the ignore count for the given breakpoint number. If '
+ 'count is\n'
+ ' omitted, the ignore count is set to 0. A breakpoint becomes '
+ 'active\n'
+ ' when the ignore count is zero. When non-zero, the count is\n'
+ ' decremented each time the breakpoint is reached and the '
+ 'breakpoint\n'
+ ' is not disabled and any associated condition evaluates to '
+ 'true.\n'
+ '\n'
+ 'condition bpnumber [condition]\n'
+ '\n'
+ ' Set a new *condition* for the breakpoint, an expression which '
+ 'must\n'
+ ' evaluate to true before the breakpoint is honored. If '
+ '*condition*\n'
+ ' is absent, any existing condition is removed; i.e., the '
+ 'breakpoint\n'
+ ' is made unconditional.\n'
+ '\n'
+ 'commands [bpnumber]\n'
+ '\n'
+ ' Specify a list of commands for breakpoint number *bpnumber*. '
+ 'The\n'
+ ' commands themselves appear on the following lines. Type a '
+ 'line\n'
+ ' containing just "end" to terminate the commands. An example:\n'
+ '\n'
+ ' (Pdb) commands 1\n'
+ ' (com) p some_variable\n'
+ ' (com) end\n'
+ ' (Pdb)\n'
+ '\n'
+ ' To remove all commands from a breakpoint, type commands and '
+ 'follow\n'
+ ' it immediately with "end"; that is, give no commands.\n'
+ '\n'
+ ' With no *bpnumber* argument, commands refers to the last '
+ 'breakpoint\n'
+ ' set.\n'
+ '\n'
+ ' You can use breakpoint commands to start your program up '
+ 'again.\n'
+ ' Simply use the continue command, or step, or any other '
+ 'command that\n'
+ ' resumes execution.\n'
+ '\n'
+ ' Specifying any command resuming execution (currently '
+ 'continue,\n'
+ ' step, next, return, jump, quit and their abbreviations) '
+ 'terminates\n'
+ ' the command list (as if that command was immediately followed '
+ 'by\n'
+ ' end). This is because any time you resume execution (even '
+ 'with a\n'
+ ' simple next or step), you may encounter another '
+ 'breakpoint—which\n'
+ ' could have its own command list, leading to ambiguities about '
+ 'which\n'
+ ' list to execute.\n'
+ '\n'
+ " If you use the 'silent' command in the command list, the "
+ 'usual\n'
+ ' message about stopping at a breakpoint is not printed. This '
+ 'may be\n'
+ ' desirable for breakpoints that are to print a specific '
+ 'message and\n'
+ ' then continue. If none of the other commands print anything, '
+ 'you\n'
+ ' see no sign that the breakpoint was reached.\n'
+ '\n'
+ 's(tep)\n'
+ '\n'
+ ' Execute the current line, stop at the first possible '
+ 'occasion\n'
+ ' (either in a function that is called or on the next line in '
+ 'the\n'
+ ' current function).\n'
+ '\n'
+ 'n(ext)\n'
+ '\n'
+ ' Continue execution until the next line in the current '
+ 'function is\n'
+ ' reached or it returns. (The difference between "next" and '
+ '"step"\n'
+ ' is that "step" stops inside a called function, while "next"\n'
+ ' executes called functions at (nearly) full speed, only '
+ 'stopping at\n'
+ ' the next line in the current function.)\n'
+ '\n'
+ 'unt(il) [lineno]\n'
+ '\n'
+ ' Without argument, continue execution until the line with a '
+ 'number\n'
+ ' greater than the current one is reached.\n'
+ '\n'
+ ' With a line number, continue execution until a line with a '
+ 'number\n'
+ ' greater or equal to that is reached. In both cases, also '
+ 'stop when\n'
+ ' the current frame returns.\n'
+ '\n'
+ ' Changed in version 3.2: Allow giving an explicit line '
+ 'number.\n'
+ '\n'
+ 'r(eturn)\n'
+ '\n'
+ ' Continue execution until the current function returns.\n'
+ '\n'
+ 'c(ont(inue))\n'
+ '\n'
+ ' Continue execution, only stop when a breakpoint is '
+ 'encountered.\n'
+ '\n'
+ 'j(ump) lineno\n'
+ '\n'
+ ' Set the next line that will be executed. Only available in '
+ 'the\n'
+ ' bottom-most frame. This lets you jump back and execute code '
+ 'again,\n'
+ " or jump forward to skip code that you don't want to run.\n"
+ '\n'
+ ' It should be noted that not all jumps are allowed -- for '
+ 'instance\n'
+ ' it is not possible to jump into the middle of a "for" loop or '
+ 'out\n'
+ ' of a "finally" clause.\n'
+ '\n'
+ 'l(ist) [first[, last]]\n'
+ '\n'
+ ' List source code for the current file. Without arguments, '
+ 'list 11\n'
+ ' lines around the current line or continue the previous '
+ 'listing.\n'
+ ' With "." as argument, list 11 lines around the current line. '
+ 'With\n'
+ ' one argument, list 11 lines around at that line. With two\n'
+ ' arguments, list the given range; if the second argument is '
+ 'less\n'
+ ' than the first, it is interpreted as a count.\n'
+ '\n'
+ ' The current line in the current frame is indicated by "->". '
+ 'If an\n'
+ ' exception is being debugged, the line where the exception '
+ 'was\n'
+ ' originally raised or propagated is indicated by ">>", if it '
+ 'differs\n'
+ ' from the current line.\n'
+ '\n'
+ ' New in version 3.2: The ">>" marker.\n'
+ '\n'
+ 'll | longlist\n'
+ '\n'
+ ' List all source code for the current function or frame.\n'
+ ' Interesting lines are marked as for "list".\n'
+ '\n'
+ ' New in version 3.2.\n'
+ '\n'
+ 'a(rgs)\n'
+ '\n'
+ ' Print the argument list of the current function.\n'
+ '\n'
+ 'p expression\n'
+ '\n'
+ ' Evaluate the *expression* in the current context and print '
+ 'its\n'
+ ' value.\n'
+ '\n'
+ ' Note: "print()" can also be used, but is not a debugger '
+ 'command\n'
+ ' --- this executes the Python "print()" function.\n'
+ '\n'
+ 'pp expression\n'
+ '\n'
+ ' Like the "p" command, except the value of the expression is '
+ 'pretty-\n'
+ ' printed using the "pprint" module.\n'
+ '\n'
+ 'whatis expression\n'
+ '\n'
+ ' Print the type of the *expression*.\n'
+ '\n'
+ 'source expression\n'
+ '\n'
+ ' Try to get source code for the given object and display it.\n'
+ '\n'
+ ' New in version 3.2.\n'
+ '\n'
+ 'display [expression]\n'
+ '\n'
+ ' Display the value of the expression if it changed, each time\n'
+ ' execution stops in the current frame.\n'
+ '\n'
+ ' Without expression, list all display expressions for the '
+ 'current\n'
+ ' frame.\n'
+ '\n'
+ ' New in version 3.2.\n'
+ '\n'
+ 'undisplay [expression]\n'
+ '\n'
+ ' Do not display the expression any more in the current frame.\n'
+ ' Without expression, clear all display expressions for the '
+ 'current\n'
+ ' frame.\n'
+ '\n'
+ ' New in version 3.2.\n'
+ '\n'
+ 'interact\n'
+ '\n'
+ ' Start an interactive interpreter (using the "code" module) '
+ 'whose\n'
+ ' global namespace contains all the (global and local) names '
+ 'found in\n'
+ ' the current scope.\n'
+ '\n'
+ ' New in version 3.2.\n'
+ '\n'
+ 'alias [name [command]]\n'
+ '\n'
+ ' Create an alias called *name* that executes *command*. The '
+ 'command\n'
+ ' must *not* be enclosed in quotes. Replaceable parameters can '
+ 'be\n'
+ ' indicated by "%1", "%2", and so on, while "%*" is replaced by '
+ 'all\n'
+ ' the parameters. If no command is given, the current alias '
+ 'for\n'
+ ' *name* is shown. If no arguments are given, all aliases are '
+ 'listed.\n'
+ '\n'
+ ' Aliases may be nested and can contain anything that can be '
+ 'legally\n'
+ ' typed at the pdb prompt. Note that internal pdb commands '
+ '*can* be\n'
+ ' overridden by aliases. Such a command is then hidden until '
+ 'the\n'
+ ' alias is removed. Aliasing is recursively applied to the '
+ 'first\n'
+ ' word of the command line; all other words in the line are '
+ 'left\n'
+ ' alone.\n'
+ '\n'
+ ' As an example, here are two useful aliases (especially when '
+ 'placed\n'
+ ' in the ".pdbrc" file):\n'
+ '\n'
+ ' # Print instance variables (usage "pi classInst")\n'
+ ' alias pi for k in %1.__dict__.keys(): '
+ 'print("%1.",k,"=",%1.__dict__[k])\n'
+ ' # Print instance variables in self\n'
+ ' alias ps pi self\n'
+ '\n'
+ 'unalias name\n'
+ '\n'
+ ' Delete the specified alias.\n'
+ '\n'
+ '! statement\n'
+ '\n'
+ ' Execute the (one-line) *statement* in the context of the '
+ 'current\n'
+ ' stack frame. The exclamation point can be omitted unless the '
+ 'first\n'
+ ' word of the statement resembles a debugger command. To set '
+ 'a\n'
+ ' global variable, you can prefix the assignment command with '
+ 'a\n'
+ ' "global" statement on the same line, e.g.:\n'
+ '\n'
+ " (Pdb) global list_options; list_options = ['-l']\n"
+ ' (Pdb)\n'
+ '\n'
+ 'run [args ...]\n'
+ 'restart [args ...]\n'
+ '\n'
+ ' Restart the debugged Python program. If an argument is '
+ 'supplied,\n'
+ ' it is split with "shlex" and the result is used as the new\n'
+ ' "sys.argv". History, breakpoints, actions and debugger '
+ 'options are\n'
+ ' preserved. "restart" is an alias for "run".\n'
+ '\n'
+ 'q(uit)\n'
+ '\n'
+ ' Quit from the debugger. The program being executed is '
+ 'aborted.\n'
+ '\n'
+ '-[ Footnotes ]-\n'
+ '\n'
+ '[1] Whether a frame is considered to originate in a certain '
+ 'module\n'
+ ' is determined by the "__name__" in the frame globals.\n',
+ 'del': '\n'
+ 'The "del" statement\n'
+ '*******************\n'
+ '\n'
+ ' del_stmt ::= "del" target_list\n'
+ '\n'
+ 'Deletion is recursively defined very similar to the way assignment '
+ 'is\n'
+ 'defined. Rather than spelling it out in full details, here are some\n'
+ 'hints.\n'
+ '\n'
+ 'Deletion of a target list recursively deletes each target, from left\n'
+ 'to right.\n'
+ '\n'
+ 'Deletion of a name removes the binding of that name from the local '
+ 'or\n'
+ 'global namespace, depending on whether the name occurs in a "global"\n'
+ 'statement in the same code block. If the name is unbound, a\n'
+ '"NameError" exception will be raised.\n'
+ '\n'
+ 'Deletion of attribute references, subscriptions and slicings is '
+ 'passed\n'
+ 'to the primary object involved; deletion of a slicing is in general\n'
+ 'equivalent to assignment of an empty slice of the right type (but '
+ 'even\n'
+ 'this is determined by the sliced object).\n'
+ '\n'
+ 'Changed in version 3.2: Previously it was illegal to delete a name\n'
+ 'from the local namespace if it occurs as a free variable in a nested\n'
+ 'block.\n',
+ 'dict': '\n'
+ 'Dictionary displays\n'
+ '*******************\n'
+ '\n'
+ 'A dictionary display is a possibly empty series of key/datum pairs\n'
+ 'enclosed in curly braces:\n'
+ '\n'
+ ' dict_display ::= "{" [key_datum_list | dict_comprehension] '
+ '"}"\n'
+ ' key_datum_list ::= key_datum ("," key_datum)* [","]\n'
+ ' key_datum ::= expression ":" expression | "**" or_expr\n'
+ ' dict_comprehension ::= expression ":" expression comp_for\n'
+ '\n'
+ 'A dictionary display yields a new dictionary object.\n'
+ '\n'
+ 'If a comma-separated sequence of key/datum pairs is given, they are\n'
+ 'evaluated from left to right to define the entries of the '
+ 'dictionary:\n'
+ 'each key object is used as a key into the dictionary to store the\n'
+ 'corresponding datum. This means that you can specify the same key\n'
+ "multiple times in the key/datum list, and the final dictionary's "
+ 'value\n'
+ 'for that key will be the last one given.\n'
+ '\n'
+ 'A double asterisk "**" denotes *dictionary unpacking*. Its operand\n'
+ 'must be a *mapping*. Each mapping item is added to the new\n'
+ 'dictionary. Later values replace values already set by earlier\n'
+ 'key/datum pairs and earlier dictionary unpackings.\n'
+ '\n'
+ 'New in version 3.5: Unpacking into dictionary displays, originally\n'
+ 'proposed by **PEP 448**.\n'
+ '\n'
+ 'A dict comprehension, in contrast to list and set comprehensions,\n'
+ 'needs two expressions separated with a colon followed by the usual\n'
+ '"for" and "if" clauses. When the comprehension is run, the '
+ 'resulting\n'
+ 'key and value elements are inserted in the new dictionary in the '
+ 'order\n'
+ 'they are produced.\n'
+ '\n'
+ 'Restrictions on the types of the key values are listed earlier in\n'
+ 'section The standard type hierarchy. (To summarize, the key type\n'
+ 'should be *hashable*, which excludes all mutable objects.) Clashes\n'
+ 'between duplicate keys are not detected; the last datum (textually\n'
+ 'rightmost in the display) stored for a given key value prevails.\n',
+ 'dynamic-features': '\n'
+ 'Interaction with dynamic features\n'
+ '*********************************\n'
+ '\n'
+ 'Name resolution of free variables occurs at runtime, not '
+ 'at compile\n'
+ 'time. This means that the following code will print 42:\n'
+ '\n'
+ ' i = 10\n'
+ ' def f():\n'
+ ' print(i)\n'
+ ' i = 42\n'
+ ' f()\n'
+ '\n'
+ 'There are several cases where Python statements are '
+ 'illegal when used\n'
+ 'in conjunction with nested scopes that contain free '
+ 'variables.\n'
+ '\n'
+ 'If a variable is referenced in an enclosing scope, it is '
+ 'illegal to\n'
+ 'delete the name. An error will be reported at compile '
+ 'time.\n'
+ '\n'
+ 'The "eval()" and "exec()" functions do not have access '
+ 'to the full\n'
+ 'environment for resolving names. Names may be resolved '
+ 'in the local\n'
+ 'and global namespaces of the caller. Free variables are '
+ 'not resolved\n'
+ 'in the nearest enclosing namespace, but in the global '
+ 'namespace. [1]\n'
+ 'The "exec()" and "eval()" functions have optional '
+ 'arguments to\n'
+ 'override the global and local namespace. If only one '
+ 'namespace is\n'
+ 'specified, it is used for both.\n',
+ 'else': '\n'
+ 'The "if" statement\n'
+ '******************\n'
+ '\n'
+ 'The "if" statement is used for conditional execution:\n'
+ '\n'
+ ' if_stmt ::= "if" expression ":" suite\n'
+ ' ( "elif" expression ":" suite )*\n'
+ ' ["else" ":" suite]\n'
+ '\n'
+ 'It selects exactly one of the suites by evaluating the expressions '
+ 'one\n'
+ 'by one until one is found to be true (see section Boolean '
+ 'operations\n'
+ 'for the definition of true and false); then that suite is executed\n'
+ '(and no other part of the "if" statement is executed or evaluated).\n'
+ 'If all expressions are false, the suite of the "else" clause, if\n'
+ 'present, is executed.\n',
+ 'exceptions': '\n'
+ 'Exceptions\n'
+ '**********\n'
+ '\n'
+ 'Exceptions are a means of breaking out of the normal flow of '
+ 'control\n'
+ 'of a code block in order to handle errors or other '
+ 'exceptional\n'
+ 'conditions. An exception is *raised* at the point where the '
+ 'error is\n'
+ 'detected; it may be *handled* by the surrounding code block or '
+ 'by any\n'
+ 'code block that directly or indirectly invoked the code block '
+ 'where\n'
+ 'the error occurred.\n'
+ '\n'
+ 'The Python interpreter raises an exception when it detects a '
+ 'run-time\n'
+ 'error (such as division by zero). A Python program can also\n'
+ 'explicitly raise an exception with the "raise" statement. '
+ 'Exception\n'
+ 'handlers are specified with the "try" ... "except" statement. '
+ 'The\n'
+ '"finally" clause of such a statement can be used to specify '
+ 'cleanup\n'
+ 'code which does not handle the exception, but is executed '
+ 'whether an\n'
+ 'exception occurred or not in the preceding code.\n'
+ '\n'
+ 'Python uses the "termination" model of error handling: an '
+ 'exception\n'
+ 'handler can find out what happened and continue execution at '
+ 'an outer\n'
+ 'level, but it cannot repair the cause of the error and retry '
+ 'the\n'
+ 'failing operation (except by re-entering the offending piece '
+ 'of code\n'
+ 'from the top).\n'
+ '\n'
+ 'When an exception is not handled at all, the interpreter '
+ 'terminates\n'
+ 'execution of the program, or returns to its interactive main '
+ 'loop. In\n'
+ 'either case, it prints a stack backtrace, except when the '
+ 'exception is\n'
+ '"SystemExit".\n'
+ '\n'
+ 'Exceptions are identified by class instances. The "except" '
+ 'clause is\n'
+ 'selected depending on the class of the instance: it must '
+ 'reference the\n'
+ 'class of the instance or a base class thereof. The instance '
+ 'can be\n'
+ 'received by the handler and can carry additional information '
+ 'about the\n'
+ 'exceptional condition.\n'
+ '\n'
+ 'Note: Exception messages are not part of the Python API. '
+ 'Their\n'
+ ' contents may change from one version of Python to the next '
+ 'without\n'
+ ' warning and should not be relied on by code which will run '
+ 'under\n'
+ ' multiple versions of the interpreter.\n'
+ '\n'
+ 'See also the description of the "try" statement in section The '
+ 'try\n'
+ 'statement and "raise" statement in section The raise '
+ 'statement.\n'
+ '\n'
+ '-[ Footnotes ]-\n'
+ '\n'
+ '[1] This limitation occurs because the code that is executed '
+ 'by\n'
+ ' these operations is not available at the time the module '
+ 'is\n'
+ ' compiled.\n',
+ 'execmodel': '\n'
+ 'Execution model\n'
+ '***************\n'
+ '\n'
+ '\n'
+ 'Structure of a program\n'
+ '======================\n'
+ '\n'
+ 'A Python program is constructed from code blocks. A *block* is '
+ 'a piece\n'
+ 'of Python program text that is executed as a unit. The '
+ 'following are\n'
+ 'blocks: a module, a function body, and a class definition. '
+ 'Each\n'
+ 'command typed interactively is a block. A script file (a file '
+ 'given\n'
+ 'as standard input to the interpreter or specified as a command '
+ 'line\n'
+ 'argument to the interpreter) is a code block. A script command '
+ '(a\n'
+ 'command specified on the interpreter command line with the '
+ "'**-c**'\n"
+ 'option) is a code block. The string argument passed to the '
+ 'built-in\n'
+ 'functions "eval()" and "exec()" is a code block.\n'
+ '\n'
+ 'A code block is executed in an *execution frame*. A frame '
+ 'contains\n'
+ 'some administrative information (used for debugging) and '
+ 'determines\n'
+ "where and how execution continues after the code block's "
+ 'execution has\n'
+ 'completed.\n'
+ '\n'
+ '\n'
+ 'Naming and binding\n'
+ '==================\n'
+ '\n'
+ '\n'
+ 'Binding of names\n'
+ '----------------\n'
+ '\n'
+ '*Names* refer to objects. Names are introduced by name '
+ 'binding\n'
+ 'operations.\n'
+ '\n'
+ 'The following constructs bind names: formal parameters to '
+ 'functions,\n'
+ '"import" statements, class and function definitions (these bind '
+ 'the\n'
+ 'class or function name in the defining block), and targets that '
+ 'are\n'
+ 'identifiers if occurring in an assignment, "for" loop header, '
+ 'or after\n'
+ '"as" in a "with" statement or "except" clause. The "import" '
+ 'statement\n'
+ 'of the form "from ... import *" binds all names defined in the\n'
+ 'imported module, except those beginning with an underscore. '
+ 'This form\n'
+ 'may only be used at the module level.\n'
+ '\n'
+ 'A target occurring in a "del" statement is also considered '
+ 'bound for\n'
+ 'this purpose (though the actual semantics are to unbind the '
+ 'name).\n'
+ '\n'
+ 'Each assignment or import statement occurs within a block '
+ 'defined by a\n'
+ 'class or function definition or at the module level (the '
+ 'top-level\n'
+ 'code block).\n'
+ '\n'
+ 'If a name is bound in a block, it is a local variable of that '
+ 'block,\n'
+ 'unless declared as "nonlocal" or "global". If a name is bound '
+ 'at the\n'
+ 'module level, it is a global variable. (The variables of the '
+ 'module\n'
+ 'code block are local and global.) If a variable is used in a '
+ 'code\n'
+ 'block but not defined there, it is a *free variable*.\n'
+ '\n'
+ 'Each occurrence of a name in the program text refers to the '
+ '*binding*\n'
+ 'of that name established by the following name resolution '
+ 'rules.\n'
+ '\n'
+ '\n'
+ 'Resolution of names\n'
+ '-------------------\n'
+ '\n'
+ 'A *scope* defines the visibility of a name within a block. If '
+ 'a local\n'
+ 'variable is defined in a block, its scope includes that block. '
+ 'If the\n'
+ 'definition occurs in a function block, the scope extends to any '
+ 'blocks\n'
+ 'contained within the defining one, unless a contained block '
+ 'introduces\n'
+ 'a different binding for the name.\n'
+ '\n'
+ 'When a name is used in a code block, it is resolved using the '
+ 'nearest\n'
+ 'enclosing scope. The set of all such scopes visible to a code '
+ 'block\n'
+ "is called the block's *environment*.\n"
+ '\n'
+ 'When a name is not found at all, a "NameError" exception is '
+ 'raised. If\n'
+ 'the current scope is a function scope, and the name refers to a '
+ 'local\n'
+ 'variable that has not yet been bound to a value at the point '
+ 'where the\n'
+ 'name is used, an "UnboundLocalError" exception is raised.\n'
+ '"UnboundLocalError" is a subclass of "NameError".\n'
+ '\n'
+ 'If a name binding operation occurs anywhere within a code '
+ 'block, all\n'
+ 'uses of the name within the block are treated as references to '
+ 'the\n'
+ 'current block. This can lead to errors when a name is used '
+ 'within a\n'
+ 'block before it is bound. This rule is subtle. Python lacks\n'
+ 'declarations and allows name binding operations to occur '
+ 'anywhere\n'
+ 'within a code block. The local variables of a code block can '
+ 'be\n'
+ 'determined by scanning the entire text of the block for name '
+ 'binding\n'
+ 'operations.\n'
+ '\n'
+ 'If the "global" statement occurs within a block, all uses of '
+ 'the name\n'
+ 'specified in the statement refer to the binding of that name in '
+ 'the\n'
+ 'top-level namespace. Names are resolved in the top-level '
+ 'namespace by\n'
+ 'searching the global namespace, i.e. the namespace of the '
+ 'module\n'
+ 'containing the code block, and the builtins namespace, the '
+ 'namespace\n'
+ 'of the module "builtins". The global namespace is searched '
+ 'first. If\n'
+ 'the name is not found there, the builtins namespace is '
+ 'searched. The\n'
+ '"global" statement must precede all uses of the name.\n'
+ '\n'
+ 'The "global" statement has the same scope as a name binding '
+ 'operation\n'
+ 'in the same block. If the nearest enclosing scope for a free '
+ 'variable\n'
+ 'contains a global statement, the free variable is treated as a '
+ 'global.\n'
+ '\n'
+ 'The "nonlocal" statement causes corresponding names to refer '
+ 'to\n'
+ 'previously bound variables in the nearest enclosing function '
+ 'scope.\n'
+ '"SyntaxError" is raised at compile time if the given name does '
+ 'not\n'
+ 'exist in any enclosing function scope.\n'
+ '\n'
+ 'The namespace for a module is automatically created the first '
+ 'time a\n'
+ 'module is imported. The main module for a script is always '
+ 'called\n'
+ '"__main__".\n'
+ '\n'
+ 'Class definition blocks and arguments to "exec()" and "eval()" '
+ 'are\n'
+ 'special in the context of name resolution. A class definition '
+ 'is an\n'
+ 'executable statement that may use and define names. These '
+ 'references\n'
+ 'follow the normal rules for name resolution with an exception '
+ 'that\n'
+ 'unbound local variables are looked up in the global namespace. '
+ 'The\n'
+ 'namespace of the class definition becomes the attribute '
+ 'dictionary of\n'
+ 'the class. The scope of names defined in a class block is '
+ 'limited to\n'
+ 'the class block; it does not extend to the code blocks of '
+ 'methods --\n'
+ 'this includes comprehensions and generator expressions since '
+ 'they are\n'
+ 'implemented using a function scope. This means that the '
+ 'following\n'
+ 'will fail:\n'
+ '\n'
+ ' class A:\n'
+ ' a = 42\n'
+ ' b = list(a + i for i in range(10))\n'
+ '\n'
+ '\n'
+ 'Builtins and restricted execution\n'
+ '---------------------------------\n'
+ '\n'
+ 'The builtins namespace associated with the execution of a code '
+ 'block\n'
+ 'is actually found by looking up the name "__builtins__" in its '
+ 'global\n'
+ 'namespace; this should be a dictionary or a module (in the '
+ 'latter case\n'
+ "the module's dictionary is used). By default, when in the "
+ '"__main__"\n'
+ 'module, "__builtins__" is the built-in module "builtins"; when '
+ 'in any\n'
+ 'other module, "__builtins__" is an alias for the dictionary of '
+ 'the\n'
+ '"builtins" module itself. "__builtins__" can be set to a '
+ 'user-created\n'
+ 'dictionary to create a weak form of restricted execution.\n'
+ '\n'
+ '**CPython implementation detail:** Users should not touch\n'
+ '"__builtins__"; it is strictly an implementation detail. '
+ 'Users\n'
+ 'wanting to override values in the builtins namespace should '
+ '"import"\n'
+ 'the "builtins" module and modify its attributes appropriately.\n'
+ '\n'
+ '\n'
+ 'Interaction with dynamic features\n'
+ '---------------------------------\n'
+ '\n'
+ 'Name resolution of free variables occurs at runtime, not at '
+ 'compile\n'
+ 'time. This means that the following code will print 42:\n'
+ '\n'
+ ' i = 10\n'
+ ' def f():\n'
+ ' print(i)\n'
+ ' i = 42\n'
+ ' f()\n'
+ '\n'
+ 'There are several cases where Python statements are illegal '
+ 'when used\n'
+ 'in conjunction with nested scopes that contain free variables.\n'
+ '\n'
+ 'If a variable is referenced in an enclosing scope, it is '
+ 'illegal to\n'
+ 'delete the name. An error will be reported at compile time.\n'
+ '\n'
+ 'The "eval()" and "exec()" functions do not have access to the '
+ 'full\n'
+ 'environment for resolving names. Names may be resolved in the '
+ 'local\n'
+ 'and global namespaces of the caller. Free variables are not '
+ 'resolved\n'
+ 'in the nearest enclosing namespace, but in the global '
+ 'namespace. [1]\n'
+ 'The "exec()" and "eval()" functions have optional arguments to\n'
+ 'override the global and local namespace. If only one namespace '
+ 'is\n'
+ 'specified, it is used for both.\n'
+ '\n'
+ '\n'
+ 'Exceptions\n'
+ '==========\n'
+ '\n'
+ 'Exceptions are a means of breaking out of the normal flow of '
+ 'control\n'
+ 'of a code block in order to handle errors or other exceptional\n'
+ 'conditions. An exception is *raised* at the point where the '
+ 'error is\n'
+ 'detected; it may be *handled* by the surrounding code block or '
+ 'by any\n'
+ 'code block that directly or indirectly invoked the code block '
+ 'where\n'
+ 'the error occurred.\n'
+ '\n'
+ 'The Python interpreter raises an exception when it detects a '
+ 'run-time\n'
+ 'error (such as division by zero). A Python program can also\n'
+ 'explicitly raise an exception with the "raise" statement. '
+ 'Exception\n'
+ 'handlers are specified with the "try" ... "except" statement. '
+ 'The\n'
+ '"finally" clause of such a statement can be used to specify '
+ 'cleanup\n'
+ 'code which does not handle the exception, but is executed '
+ 'whether an\n'
+ 'exception occurred or not in the preceding code.\n'
+ '\n'
+ 'Python uses the "termination" model of error handling: an '
+ 'exception\n'
+ 'handler can find out what happened and continue execution at an '
+ 'outer\n'
+ 'level, but it cannot repair the cause of the error and retry '
+ 'the\n'
+ 'failing operation (except by re-entering the offending piece of '
+ 'code\n'
+ 'from the top).\n'
+ '\n'
+ 'When an exception is not handled at all, the interpreter '
+ 'terminates\n'
+ 'execution of the program, or returns to its interactive main '
+ 'loop. In\n'
+ 'either case, it prints a stack backtrace, except when the '
+ 'exception is\n'
+ '"SystemExit".\n'
+ '\n'
+ 'Exceptions are identified by class instances. The "except" '
+ 'clause is\n'
+ 'selected depending on the class of the instance: it must '
+ 'reference the\n'
+ 'class of the instance or a base class thereof. The instance '
+ 'can be\n'
+ 'received by the handler and can carry additional information '
+ 'about the\n'
+ 'exceptional condition.\n'
+ '\n'
+ 'Note: Exception messages are not part of the Python API. '
+ 'Their\n'
+ ' contents may change from one version of Python to the next '
+ 'without\n'
+ ' warning and should not be relied on by code which will run '
+ 'under\n'
+ ' multiple versions of the interpreter.\n'
+ '\n'
+ 'See also the description of the "try" statement in section The '
+ 'try\n'
+ 'statement and "raise" statement in section The raise '
+ 'statement.\n'
+ '\n'
+ '-[ Footnotes ]-\n'
+ '\n'
+ '[1] This limitation occurs because the code that is executed '
+ 'by\n'
+ ' these operations is not available at the time the module '
+ 'is\n'
+ ' compiled.\n',
+ 'exprlists': '\n'
+ 'Expression lists\n'
+ '****************\n'
+ '\n'
+ ' expression_list ::= expression ( "," expression )* [","]\n'
+ ' starred_list ::= starred_item ( "," starred_item )* '
+ '[","]\n'
+ ' starred_expression ::= expression | ( starred_item "," )* '
+ '[starred_item]\n'
+ ' starred_item ::= expression | "*" or_expr\n'
+ '\n'
+ 'Except when part of a list or set display, an expression list\n'
+ 'containing at least one comma yields a tuple. The length of '
+ 'the tuple\n'
+ 'is the number of expressions in the list. The expressions are\n'
+ 'evaluated from left to right.\n'
+ '\n'
+ 'An asterisk "*" denotes *iterable unpacking*. Its operand must '
+ 'be an\n'
+ '*iterable*. The iterable is expanded into a sequence of items, '
+ 'which\n'
+ 'are included in the new tuple, list, or set, at the site of '
+ 'the\n'
+ 'unpacking.\n'
+ '\n'
+ 'New in version 3.5: Iterable unpacking in expression lists, '
+ 'originally\n'
+ 'proposed by **PEP 448**.\n'
+ '\n'
+ 'The trailing comma is required only to create a single tuple '
+ '(a.k.a. a\n'
+ '*singleton*); it is optional in all other cases. A single '
+ 'expression\n'
+ "without a trailing comma doesn't create a tuple, but rather "
+ 'yields the\n'
+ 'value of that expression. (To create an empty tuple, use an '
+ 'empty pair\n'
+ 'of parentheses: "()".)\n',
+ 'floating': '\n'
+ 'Floating point literals\n'
+ '***********************\n'
+ '\n'
+ 'Floating point literals are described by the following lexical\n'
+ 'definitions:\n'
+ '\n'
+ ' floatnumber ::= pointfloat | exponentfloat\n'
+ ' pointfloat ::= [digitpart] fraction | digitpart "."\n'
+ ' exponentfloat ::= (digitpart | pointfloat) exponent\n'
+ ' digitpart ::= digit (["_"] digit)*\n'
+ ' fraction ::= "." digitpart\n'
+ ' exponent ::= ("e" | "E") ["+" | "-"] digitpart\n'
+ '\n'
+ 'Note that the integer and exponent parts are always interpreted '
+ 'using\n'
+ 'radix 10. For example, "077e010" is legal, and denotes the same '
+ 'number\n'
+ 'as "77e10". The allowed range of floating point literals is\n'
+ 'implementation-dependent. As in integer literals, underscores '
+ 'are\n'
+ 'supported for digit grouping.\n'
+ '\n'
+ 'Some examples of floating point literals:\n'
+ '\n'
+ ' 3.14 10. .001 1e100 3.14e-10 0e0 '
+ '3.14_15_93\n'
+ '\n'
+ 'Note that numeric literals do not include a sign; a phrase like '
+ '"-1"\n'
+ 'is actually an expression composed of the unary operator "-" and '
+ 'the\n'
+ 'literal "1".\n'
+ '\n'
+ 'Changed in version 3.6: Underscores are now allowed for '
+ 'grouping\n'
+ 'purposes in literals.\n',
+ 'for': '\n'
+ 'The "for" statement\n'
+ '*******************\n'
+ '\n'
+ 'The "for" statement is used to iterate over the elements of a '
+ 'sequence\n'
+ '(such as a string, tuple or list) or other iterable object:\n'
+ '\n'
+ ' for_stmt ::= "for" target_list "in" expression_list ":" suite\n'
+ ' ["else" ":" suite]\n'
+ '\n'
+ 'The expression list is evaluated once; it should yield an iterable\n'
+ 'object. An iterator is created for the result of the\n'
+ '"expression_list". The suite is then executed once for each item\n'
+ 'provided by the iterator, in the order returned by the iterator. '
+ 'Each\n'
+ 'item in turn is assigned to the target list using the standard rules\n'
+ 'for assignments (see Assignment statements), and then the suite is\n'
+ 'executed. When the items are exhausted (which is immediately when '
+ 'the\n'
+ 'sequence is empty or an iterator raises a "StopIteration" '
+ 'exception),\n'
+ 'the suite in the "else" clause, if present, is executed, and the '
+ 'loop\n'
+ 'terminates.\n'
+ '\n'
+ 'A "break" statement executed in the first suite terminates the loop\n'
+ 'without executing the "else" clause\'s suite. A "continue" '
+ 'statement\n'
+ 'executed in the first suite skips the rest of the suite and '
+ 'continues\n'
+ 'with the next item, or with the "else" clause if there is no next\n'
+ 'item.\n'
+ '\n'
+ 'The for-loop makes assignments to the variables(s) in the target '
+ 'list.\n'
+ 'This overwrites all previous assignments to those variables '
+ 'including\n'
+ 'those made in the suite of the for-loop:\n'
+ '\n'
+ ' for i in range(10):\n'
+ ' print(i)\n'
+ ' i = 5 # this will not affect the for-loop\n'
+ ' # because i will be overwritten with the '
+ 'next\n'
+ ' # index in the range\n'
+ '\n'
+ 'Names in the target list are not deleted when the loop is finished,\n'
+ 'but if the sequence is empty, they will not have been assigned to at\n'
+ 'all by the loop. Hint: the built-in function "range()" returns an\n'
+ 'iterator of integers suitable to emulate the effect of Pascal\'s "for '
+ 'i\n'
+ ':= a to b do"; e.g., "list(range(3))" returns the list "[0, 1, 2]".\n'
+ '\n'
+ 'Note: There is a subtlety when the sequence is being modified by the\n'
+ ' loop (this can only occur for mutable sequences, i.e. lists). An\n'
+ ' internal counter is used to keep track of which item is used next,\n'
+ ' and this is incremented on each iteration. When this counter has\n'
+ ' reached the length of the sequence the loop terminates. This '
+ 'means\n'
+ ' that if the suite deletes the current (or a previous) item from '
+ 'the\n'
+ ' sequence, the next item will be skipped (since it gets the index '
+ 'of\n'
+ ' the current item which has already been treated). Likewise, if '
+ 'the\n'
+ ' suite inserts an item in the sequence before the current item, the\n'
+ ' current item will be treated again the next time through the loop.\n'
+ ' This can lead to nasty bugs that can be avoided by making a\n'
+ ' temporary copy using a slice of the whole sequence, e.g.,\n'
+ '\n'
+ ' for x in a[:]:\n'
+ ' if x < 0: a.remove(x)\n',
+ 'formatstrings': '\n'
+ 'Format String Syntax\n'
+ '********************\n'
+ '\n'
+ 'The "str.format()" method and the "Formatter" class share '
+ 'the same\n'
+ 'syntax for format strings (although in the case of '
+ '"Formatter",\n'
+ 'subclasses can define their own format string syntax). The '
+ 'syntax is\n'
+ 'related to that of formatted string literals, but there '
+ 'are\n'
+ 'differences.\n'
+ '\n'
+ 'Format strings contain "replacement fields" surrounded by '
+ 'curly braces\n'
+ '"{}". Anything that is not contained in braces is '
+ 'considered literal\n'
+ 'text, which is copied unchanged to the output. If you need '
+ 'to include\n'
+ 'a brace character in the literal text, it can be escaped by '
+ 'doubling:\n'
+ '"{{" and "}}".\n'
+ '\n'
+ 'The grammar for a replacement field is as follows:\n'
+ '\n'
+ ' replacement_field ::= "{" [field_name] ["!" '
+ 'conversion] [":" format_spec] "}"\n'
+ ' field_name ::= arg_name ("." attribute_name | '
+ '"[" element_index "]")*\n'
+ ' arg_name ::= [identifier | integer]\n'
+ ' attribute_name ::= identifier\n'
+ ' element_index ::= integer | index_string\n'
+ ' index_string ::= <any source character except '
+ '"]"> +\n'
+ ' conversion ::= "r" | "s" | "a"\n'
+ ' format_spec ::= <described in the next '
+ 'section>\n'
+ '\n'
+ 'In less formal terms, the replacement field can start with '
+ 'a\n'
+ '*field_name* that specifies the object whose value is to be '
+ 'formatted\n'
+ 'and inserted into the output instead of the replacement '
+ 'field. The\n'
+ '*field_name* is optionally followed by a *conversion* '
+ 'field, which is\n'
+ 'preceded by an exclamation point "\'!\'", and a '
+ '*format_spec*, which is\n'
+ 'preceded by a colon "\':\'". These specify a non-default '
+ 'format for the\n'
+ 'replacement value.\n'
+ '\n'
+ 'See also the Format Specification Mini-Language section.\n'
+ '\n'
+ 'The *field_name* itself begins with an *arg_name* that is '
+ 'either a\n'
+ "number or a keyword. If it's a number, it refers to a "
+ 'positional\n'
+ "argument, and if it's a keyword, it refers to a named "
+ 'keyword\n'
+ 'argument. If the numerical arg_names in a format string '
+ 'are 0, 1, 2,\n'
+ '... in sequence, they can all be omitted (not just some) '
+ 'and the\n'
+ 'numbers 0, 1, 2, ... will be automatically inserted in that '
+ 'order.\n'
+ 'Because *arg_name* is not quote-delimited, it is not '
+ 'possible to\n'
+ 'specify arbitrary dictionary keys (e.g., the strings '
+ '"\'10\'" or\n'
+ '"\':-]\'") within a format string. The *arg_name* can be '
+ 'followed by any\n'
+ 'number of index or attribute expressions. An expression of '
+ 'the form\n'
+ '"\'.name\'" selects the named attribute using "getattr()", '
+ 'while an\n'
+ 'expression of the form "\'[index]\'" does an index lookup '
+ 'using\n'
+ '"__getitem__()".\n'
+ '\n'
+ 'Changed in version 3.1: The positional argument specifiers '
+ 'can be\n'
+ 'omitted, so "\'{} {}\'" is equivalent to "\'{0} {1}\'".\n'
+ '\n'
+ 'Some simple format string examples:\n'
+ '\n'
+ ' "First, thou shalt count to {0}" # References first '
+ 'positional argument\n'
+ ' "Bring me a {}" # Implicitly '
+ 'references the first positional argument\n'
+ ' "From {} to {}" # Same as "From {0} to '
+ '{1}"\n'
+ ' "My quest is {name}" # References keyword '
+ "argument 'name'\n"
+ ' "Weight in tons {0.weight}" # \'weight\' attribute '
+ 'of first positional arg\n'
+ ' "Units destroyed: {players[0]}" # First element of '
+ "keyword argument 'players'.\n"
+ '\n'
+ 'The *conversion* field causes a type coercion before '
+ 'formatting.\n'
+ 'Normally, the job of formatting a value is done by the '
+ '"__format__()"\n'
+ 'method of the value itself. However, in some cases it is '
+ 'desirable to\n'
+ 'force a type to be formatted as a string, overriding its '
+ 'own\n'
+ 'definition of formatting. By converting the value to a '
+ 'string before\n'
+ 'calling "__format__()", the normal formatting logic is '
+ 'bypassed.\n'
+ '\n'
+ 'Three conversion flags are currently supported: "\'!s\'" '
+ 'which calls\n'
+ '"str()" on the value, "\'!r\'" which calls "repr()" and '
+ '"\'!a\'" which\n'
+ 'calls "ascii()".\n'
+ '\n'
+ 'Some examples:\n'
+ '\n'
+ ' "Harold\'s a clever {0!s}" # Calls str() on the '
+ 'argument first\n'
+ ' "Bring out the holy {name!r}" # Calls repr() on the '
+ 'argument first\n'
+ ' "More {!a}" # Calls ascii() on the '
+ 'argument first\n'
+ '\n'
+ 'The *format_spec* field contains a specification of how the '
+ 'value\n'
+ 'should be presented, including such details as field width, '
+ 'alignment,\n'
+ 'padding, decimal precision and so on. Each value type can '
+ 'define its\n'
+ 'own "formatting mini-language" or interpretation of the '
+ '*format_spec*.\n'
+ '\n'
+ 'Most built-in types support a common formatting '
+ 'mini-language, which\n'
+ 'is described in the next section.\n'
+ '\n'
+ 'A *format_spec* field can also include nested replacement '
+ 'fields\n'
+ 'within it. These nested replacement fields may contain a '
+ 'field name,\n'
+ 'conversion flag and format specification, but deeper '
+ 'nesting is not\n'
+ 'allowed. The replacement fields within the format_spec '
+ 'are\n'
+ 'substituted before the *format_spec* string is interpreted. '
+ 'This\n'
+ 'allows the formatting of a value to be dynamically '
+ 'specified.\n'
+ '\n'
+ 'See the Format examples section for some examples.\n'
+ '\n'
+ '\n'
+ 'Format Specification Mini-Language\n'
+ '==================================\n'
+ '\n'
+ '"Format specifications" are used within replacement fields '
+ 'contained\n'
+ 'within a format string to define how individual values are '
+ 'presented\n'
+ '(see Format String Syntax and Formatted string literals). '
+ 'They can\n'
+ 'also be passed directly to the built-in "format()" '
+ 'function. Each\n'
+ 'formattable type may define how the format specification is '
+ 'to be\n'
+ 'interpreted.\n'
+ '\n'
+ 'Most built-in types implement the following options for '
+ 'format\n'
+ 'specifications, although some of the formatting options are '
+ 'only\n'
+ 'supported by the numeric types.\n'
+ '\n'
+ 'A general convention is that an empty format string ("""") '
+ 'produces\n'
+ 'the same result as if you had called "str()" on the value. '
+ 'A non-empty\n'
+ 'format string typically modifies the result.\n'
+ '\n'
+ 'The general form of a *standard format specifier* is:\n'
+ '\n'
+ ' format_spec ::= '
+ '[[fill]align][sign][#][0][width][grouping_option][.precision][type]\n'
+ ' fill ::= <any character>\n'
+ ' align ::= "<" | ">" | "=" | "^"\n'
+ ' sign ::= "+" | "-" | " "\n'
+ ' width ::= integer\n'
+ ' grouping_option ::= "_" | ","\n'
+ ' precision ::= integer\n'
+ ' type ::= "b" | "c" | "d" | "e" | "E" | "f" | '
+ '"F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"\n'
+ '\n'
+ 'If a valid *align* value is specified, it can be preceded '
+ 'by a *fill*\n'
+ 'character that can be any character and defaults to a space '
+ 'if\n'
+ 'omitted. It is not possible to use a literal curly brace '
+ '(""{"" or\n'
+ '""}"") as the *fill* character in a formatted string '
+ 'literal or when\n'
+ 'using the "str.format()" method. However, it is possible '
+ 'to insert a\n'
+ 'curly brace with a nested replacement field. This '
+ "limitation doesn't\n"
+ 'affect the "format()" function.\n'
+ '\n'
+ 'The meaning of the various alignment options is as '
+ 'follows:\n'
+ '\n'
+ ' '
+ '+-----------+------------------------------------------------------------+\n'
+ ' | Option | '
+ 'Meaning '
+ '|\n'
+ ' '
+ '+===========+============================================================+\n'
+ ' | "\'<\'" | Forces the field to be left-aligned '
+ 'within the available |\n'
+ ' | | space (this is the default for most '
+ 'objects). |\n'
+ ' '
+ '+-----------+------------------------------------------------------------+\n'
+ ' | "\'>\'" | Forces the field to be right-aligned '
+ 'within the available |\n'
+ ' | | space (this is the default for '
+ 'numbers). |\n'
+ ' '
+ '+-----------+------------------------------------------------------------+\n'
+ ' | "\'=\'" | Forces the padding to be placed after '
+ 'the sign (if any) |\n'
+ ' | | but before the digits. This is used for '
+ 'printing fields |\n'
+ " | | in the form '+000000120'. This alignment "
+ 'option is only |\n'
+ ' | | valid for numeric types. It becomes the '
+ "default when '0' |\n"
+ ' | | immediately precedes the field '
+ 'width. |\n'
+ ' '
+ '+-----------+------------------------------------------------------------+\n'
+ ' | "\'^\'" | Forces the field to be centered within '
+ 'the available |\n'
+ ' | | '
+ 'space. '
+ '|\n'
+ ' '
+ '+-----------+------------------------------------------------------------+\n'
+ '\n'
+ 'Note that unless a minimum field width is defined, the '
+ 'field width\n'
+ 'will always be the same size as the data to fill it, so '
+ 'that the\n'
+ 'alignment option has no meaning in this case.\n'
+ '\n'
+ 'The *sign* option is only valid for number types, and can '
+ 'be one of\n'
+ 'the following:\n'
+ '\n'
+ ' '
+ '+-----------+------------------------------------------------------------+\n'
+ ' | Option | '
+ 'Meaning '
+ '|\n'
+ ' '
+ '+===========+============================================================+\n'
+ ' | "\'+\'" | indicates that a sign should be used for '
+ 'both positive as |\n'
+ ' | | well as negative '
+ 'numbers. |\n'
+ ' '
+ '+-----------+------------------------------------------------------------+\n'
+ ' | "\'-\'" | indicates that a sign should be used '
+ 'only for negative |\n'
+ ' | | numbers (this is the default '
+ 'behavior). |\n'
+ ' '
+ '+-----------+------------------------------------------------------------+\n'
+ ' | space | indicates that a leading space should be '
+ 'used on positive |\n'
+ ' | | numbers, and a minus sign on negative '
+ 'numbers. |\n'
+ ' '
+ '+-----------+------------------------------------------------------------+\n'
+ '\n'
+ 'The "\'#\'" option causes the "alternate form" to be used '
+ 'for the\n'
+ 'conversion. The alternate form is defined differently for '
+ 'different\n'
+ 'types. This option is only valid for integer, float, '
+ 'complex and\n'
+ 'Decimal types. For integers, when binary, octal, or '
+ 'hexadecimal output\n'
+ 'is used, this option adds the prefix respective "\'0b\'", '
+ '"\'0o\'", or\n'
+ '"\'0x\'" to the output value. For floats, complex and '
+ 'Decimal the\n'
+ 'alternate form causes the result of the conversion to '
+ 'always contain a\n'
+ 'decimal-point character, even if no digits follow it. '
+ 'Normally, a\n'
+ 'decimal-point character appears in the result of these '
+ 'conversions\n'
+ 'only if a digit follows it. In addition, for "\'g\'" and '
+ '"\'G\'"\n'
+ 'conversions, trailing zeros are not removed from the '
+ 'result.\n'
+ '\n'
+ 'The "\',\'" option signals the use of a comma for a '
+ 'thousands separator.\n'
+ 'For a locale aware separator, use the "\'n\'" integer '
+ 'presentation type\n'
+ 'instead.\n'
+ '\n'
+ 'Changed in version 3.1: Added the "\',\'" option (see also '
+ '**PEP 378**).\n'
+ '\n'
+ 'The "\'_\'" option signals the use of an underscore for a '
+ 'thousands\n'
+ 'separator for floating point presentation types and for '
+ 'integer\n'
+ 'presentation type "\'d\'". For integer presentation types '
+ '"\'b\'", "\'o\'",\n'
+ '"\'x\'", and "\'X\'", underscores will be inserted every 4 '
+ 'digits. For\n'
+ 'other presentation types, specifying this option is an '
+ 'error.\n'
+ '\n'
+ 'Changed in version 3.6: Added the "\'_\'" option (see also '
+ '**PEP 515**).\n'
+ '\n'
+ '*width* is a decimal integer defining the minimum field '
+ 'width. If not\n'
+ 'specified, then the field width will be determined by the '
+ 'content.\n'
+ '\n'
+ 'When no explicit alignment is given, preceding the *width* '
+ 'field by a\n'
+ 'zero ("\'0\'") character enables sign-aware zero-padding '
+ 'for numeric\n'
+ 'types. This is equivalent to a *fill* character of "\'0\'" '
+ 'with an\n'
+ '*alignment* type of "\'=\'".\n'
+ '\n'
+ 'The *precision* is a decimal number indicating how many '
+ 'digits should\n'
+ 'be displayed after the decimal point for a floating point '
+ 'value\n'
+ 'formatted with "\'f\'" and "\'F\'", or before and after the '
+ 'decimal point\n'
+ 'for a floating point value formatted with "\'g\'" or '
+ '"\'G\'". For non-\n'
+ 'number types the field indicates the maximum field size - '
+ 'in other\n'
+ 'words, how many characters will be used from the field '
+ 'content. The\n'
+ '*precision* is not allowed for integer values.\n'
+ '\n'
+ 'Finally, the *type* determines how the data should be '
+ 'presented.\n'
+ '\n'
+ 'The available string presentation types are:\n'
+ '\n'
+ ' '
+ '+-----------+------------------------------------------------------------+\n'
+ ' | Type | '
+ 'Meaning '
+ '|\n'
+ ' '
+ '+===========+============================================================+\n'
+ ' | "\'s\'" | String format. This is the default type '
+ 'for strings and |\n'
+ ' | | may be '
+ 'omitted. |\n'
+ ' '
+ '+-----------+------------------------------------------------------------+\n'
+ ' | None | The same as '
+ '"\'s\'". |\n'
+ ' '
+ '+-----------+------------------------------------------------------------+\n'
+ '\n'
+ 'The available integer presentation types are:\n'
+ '\n'
+ ' '
+ '+-----------+------------------------------------------------------------+\n'
+ ' | Type | '
+ 'Meaning '
+ '|\n'
+ ' '
+ '+===========+============================================================+\n'
+ ' | "\'b\'" | Binary format. Outputs the number in '
+ 'base 2. |\n'
+ ' '
+ '+-----------+------------------------------------------------------------+\n'
+ ' | "\'c\'" | Character. Converts the integer to the '
+ 'corresponding |\n'
+ ' | | unicode character before '
+ 'printing. |\n'
+ ' '
+ '+-----------+------------------------------------------------------------+\n'
+ ' | "\'d\'" | Decimal Integer. Outputs the number in '
+ 'base 10. |\n'
+ ' '
+ '+-----------+------------------------------------------------------------+\n'
+ ' | "\'o\'" | Octal format. Outputs the number in base '
+ '8. |\n'
+ ' '
+ '+-----------+------------------------------------------------------------+\n'
+ ' | "\'x\'" | Hex format. Outputs the number in base '
+ '16, using lower- |\n'
+ ' | | case letters for the digits above '
+ '9. |\n'
+ ' '
+ '+-----------+------------------------------------------------------------+\n'
+ ' | "\'X\'" | Hex format. Outputs the number in base '
+ '16, using upper- |\n'
+ ' | | case letters for the digits above '
+ '9. |\n'
+ ' '
+ '+-----------+------------------------------------------------------------+\n'
+ ' | "\'n\'" | Number. This is the same as "\'d\'", '
+ 'except that it uses the |\n'
+ ' | | current locale setting to insert the '
+ 'appropriate number |\n'
+ ' | | separator '
+ 'characters. |\n'
+ ' '
+ '+-----------+------------------------------------------------------------+\n'
+ ' | None | The same as '
+ '"\'d\'". |\n'
+ ' '
+ '+-----------+------------------------------------------------------------+\n'
+ '\n'
+ 'In addition to the above presentation types, integers can '
+ 'be formatted\n'
+ 'with the floating point presentation types listed below '
+ '(except "\'n\'"\n'
+ 'and "None"). When doing so, "float()" is used to convert '
+ 'the integer\n'
+ 'to a floating point number before formatting.\n'
+ '\n'
+ 'The available presentation types for floating point and '
+ 'decimal values\n'
+ 'are:\n'
+ '\n'
+ ' '
+ '+-----------+------------------------------------------------------------+\n'
+ ' | Type | '
+ 'Meaning '
+ '|\n'
+ ' '
+ '+===========+============================================================+\n'
+ ' | "\'e\'" | Exponent notation. Prints the number in '
+ 'scientific |\n'
+ " | | notation using the letter 'e' to indicate "
+ 'the exponent. |\n'
+ ' | | The default precision is '
+ '"6". |\n'
+ ' '
+ '+-----------+------------------------------------------------------------+\n'
+ ' | "\'E\'" | Exponent notation. Same as "\'e\'" '
+ 'except it uses an upper |\n'
+ " | | case 'E' as the separator "
+ 'character. |\n'
+ ' '
+ '+-----------+------------------------------------------------------------+\n'
+ ' | "\'f\'" | Fixed point. Displays the number as a '
+ 'fixed-point number. |\n'
+ ' | | The default precision is '
+ '"6". |\n'
+ ' '
+ '+-----------+------------------------------------------------------------+\n'
+ ' | "\'F\'" | Fixed point. Same as "\'f\'", but '
+ 'converts "nan" to "NAN" |\n'
+ ' | | and "inf" to '
+ '"INF". |\n'
+ ' '
+ '+-----------+------------------------------------------------------------+\n'
+ ' | "\'g\'" | General format. For a given precision '
+ '"p >= 1", this |\n'
+ ' | | rounds the number to "p" significant '
+ 'digits and then |\n'
+ ' | | formats the result in either fixed-point '
+ 'format or in |\n'
+ ' | | scientific notation, depending on its '
+ 'magnitude. The |\n'
+ ' | | precise rules are as follows: suppose that '
+ 'the result |\n'
+ ' | | formatted with presentation type "\'e\'" '
+ 'and precision "p-1" |\n'
+ ' | | would have exponent "exp". Then if "-4 <= '
+ 'exp < p", the |\n'
+ ' | | number is formatted with presentation type '
+ '"\'f\'" and |\n'
+ ' | | precision "p-1-exp". Otherwise, the '
+ 'number is formatted |\n'
+ ' | | with presentation type "\'e\'" and '
+ 'precision "p-1". In both |\n'
+ ' | | cases insignificant trailing zeros are '
+ 'removed from the |\n'
+ ' | | significand, and the decimal point is also '
+ 'removed if |\n'
+ ' | | there are no remaining digits following '
+ 'it. Positive and |\n'
+ ' | | negative infinity, positive and negative '
+ 'zero, and nans, |\n'
+ ' | | are formatted as "inf", "-inf", "0", "-0" '
+ 'and "nan" |\n'
+ ' | | respectively, regardless of the '
+ 'precision. A precision of |\n'
+ ' | | "0" is treated as equivalent to a '
+ 'precision of "1". The |\n'
+ ' | | default precision is '
+ '"6". |\n'
+ ' '
+ '+-----------+------------------------------------------------------------+\n'
+ ' | "\'G\'" | General format. Same as "\'g\'" except '
+ 'switches to "\'E\'" if |\n'
+ ' | | the number gets too large. The '
+ 'representations of infinity |\n'
+ ' | | and NaN are uppercased, '
+ 'too. |\n'
+ ' '
+ '+-----------+------------------------------------------------------------+\n'
+ ' | "\'n\'" | Number. This is the same as "\'g\'", '
+ 'except that it uses the |\n'
+ ' | | current locale setting to insert the '
+ 'appropriate number |\n'
+ ' | | separator '
+ 'characters. |\n'
+ ' '
+ '+-----------+------------------------------------------------------------+\n'
+ ' | "\'%\'" | Percentage. Multiplies the number by 100 '
+ 'and displays in |\n'
+ ' | | fixed ("\'f\'") format, followed by a '
+ 'percent sign. |\n'
+ ' '
+ '+-----------+------------------------------------------------------------+\n'
+ ' | None | Similar to "\'g\'", except that '
+ 'fixed-point notation, when |\n'
+ ' | | used, has at least one digit past the '
+ 'decimal point. The |\n'
+ ' | | default precision is as high as needed to '
+ 'represent the |\n'
+ ' | | particular value. The overall effect is to '
+ 'match the |\n'
+ ' | | output of "str()" as altered by the other '
+ 'format |\n'
+ ' | | '
+ 'modifiers. '
+ '|\n'
+ ' '
+ '+-----------+------------------------------------------------------------+\n'
+ '\n'
+ '\n'
+ 'Format examples\n'
+ '===============\n'
+ '\n'
+ 'This section contains examples of the "str.format()" syntax '
+ 'and\n'
+ 'comparison with the old "%"-formatting.\n'
+ '\n'
+ 'In most of the cases the syntax is similar to the old '
+ '"%"-formatting,\n'
+ 'with the addition of the "{}" and with ":" used instead of '
+ '"%". For\n'
+ 'example, "\'%03.2f\'" can be translated to "\'{:03.2f}\'".\n'
+ '\n'
+ 'The new format syntax also supports new and different '
+ 'options, shown\n'
+ 'in the follow examples.\n'
+ '\n'
+ 'Accessing arguments by position:\n'
+ '\n'
+ " >>> '{0}, {1}, {2}'.format('a', 'b', 'c')\n"
+ " 'a, b, c'\n"
+ " >>> '{}, {}, {}'.format('a', 'b', 'c') # 3.1+ only\n"
+ " 'a, b, c'\n"
+ " >>> '{2}, {1}, {0}'.format('a', 'b', 'c')\n"
+ " 'c, b, a'\n"
+ " >>> '{2}, {1}, {0}'.format(*'abc') # unpacking "
+ 'argument sequence\n'
+ " 'c, b, a'\n"
+ " >>> '{0}{1}{0}'.format('abra', 'cad') # arguments' "
+ 'indices can be repeated\n'
+ " 'abracadabra'\n"
+ '\n'
+ 'Accessing arguments by name:\n'
+ '\n'
+ " >>> 'Coordinates: {latitude}, "
+ "{longitude}'.format(latitude='37.24N', "
+ "longitude='-115.81W')\n"
+ " 'Coordinates: 37.24N, -115.81W'\n"
+ " >>> coord = {'latitude': '37.24N', 'longitude': "
+ "'-115.81W'}\n"
+ " >>> 'Coordinates: {latitude}, "
+ "{longitude}'.format(**coord)\n"
+ " 'Coordinates: 37.24N, -115.81W'\n"
+ '\n'
+ "Accessing arguments' attributes:\n"
+ '\n'
+ ' >>> c = 3-5j\n'
+ " >>> ('The complex number {0} is formed from the real "
+ "part {0.real} '\n"
+ " ... 'and the imaginary part {0.imag}.').format(c)\n"
+ " 'The complex number (3-5j) is formed from the real part "
+ "3.0 and the imaginary part -5.0.'\n"
+ ' >>> class Point:\n'
+ ' ... def __init__(self, x, y):\n'
+ ' ... self.x, self.y = x, y\n'
+ ' ... def __str__(self):\n'
+ " ... return 'Point({self.x}, "
+ "{self.y})'.format(self=self)\n"
+ ' ...\n'
+ ' >>> str(Point(4, 2))\n'
+ " 'Point(4, 2)'\n"
+ '\n'
+ "Accessing arguments' items:\n"
+ '\n'
+ ' >>> coord = (3, 5)\n'
+ " >>> 'X: {0[0]}; Y: {0[1]}'.format(coord)\n"
+ " 'X: 3; Y: 5'\n"
+ '\n'
+ 'Replacing "%s" and "%r":\n'
+ '\n'
+ ' >>> "repr() shows quotes: {!r}; str() doesn\'t: '
+ '{!s}".format(\'test1\', \'test2\')\n'
+ ' "repr() shows quotes: \'test1\'; str() doesn\'t: test2"\n'
+ '\n'
+ 'Aligning the text and specifying a width:\n'
+ '\n'
+ " >>> '{:<30}'.format('left aligned')\n"
+ " 'left aligned '\n"
+ " >>> '{:>30}'.format('right aligned')\n"
+ " ' right aligned'\n"
+ " >>> '{:^30}'.format('centered')\n"
+ " ' centered '\n"
+ " >>> '{:*^30}'.format('centered') # use '*' as a fill "
+ 'char\n'
+ " '***********centered***********'\n"
+ '\n'
+ 'Replacing "%+f", "%-f", and "% f" and specifying a sign:\n'
+ '\n'
+ " >>> '{:+f}; {:+f}'.format(3.14, -3.14) # show it "
+ 'always\n'
+ " '+3.140000; -3.140000'\n"
+ " >>> '{: f}; {: f}'.format(3.14, -3.14) # show a space "
+ 'for positive numbers\n'
+ " ' 3.140000; -3.140000'\n"
+ " >>> '{:-f}; {:-f}'.format(3.14, -3.14) # show only the "
+ "minus -- same as '{:f}; {:f}'\n"
+ " '3.140000; -3.140000'\n"
+ '\n'
+ 'Replacing "%x" and "%o" and converting the value to '
+ 'different bases:\n'
+ '\n'
+ ' >>> # format also supports binary numbers\n'
+ ' >>> "int: {0:d}; hex: {0:x}; oct: {0:o}; bin: '
+ '{0:b}".format(42)\n'
+ " 'int: 42; hex: 2a; oct: 52; bin: 101010'\n"
+ ' >>> # with 0x, 0o, or 0b as prefix:\n'
+ ' >>> "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: '
+ '{0:#b}".format(42)\n'
+ " 'int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010'\n"
+ '\n'
+ 'Using the comma as a thousands separator:\n'
+ '\n'
+ " >>> '{:,}'.format(1234567890)\n"
+ " '1,234,567,890'\n"
+ '\n'
+ 'Expressing a percentage:\n'
+ '\n'
+ ' >>> points = 19\n'
+ ' >>> total = 22\n'
+ " >>> 'Correct answers: {:.2%}'.format(points/total)\n"
+ " 'Correct answers: 86.36%'\n"
+ '\n'
+ 'Using type-specific formatting:\n'
+ '\n'
+ ' >>> import datetime\n'
+ ' >>> d = datetime.datetime(2010, 7, 4, 12, 15, 58)\n'
+ " >>> '{:%Y-%m-%d %H:%M:%S}'.format(d)\n"
+ " '2010-07-04 12:15:58'\n"
+ '\n'
+ 'Nesting arguments and more complex examples:\n'
+ '\n'
+ " >>> for align, text in zip('<^>', ['left', 'center', "
+ "'right']):\n"
+ " ... '{0:{fill}{align}16}'.format(text, fill=align, "
+ 'align=align)\n'
+ ' ...\n'
+ " 'left<<<<<<<<<<<<'\n"
+ " '^^^^^center^^^^^'\n"
+ " '>>>>>>>>>>>right'\n"
+ ' >>>\n'
+ ' >>> octets = [192, 168, 0, 1]\n'
+ " >>> '{:02X}{:02X}{:02X}{:02X}'.format(*octets)\n"
+ " 'C0A80001'\n"
+ ' >>> int(_, 16)\n'
+ ' 3232235521\n'
+ ' >>>\n'
+ ' >>> width = 5\n'
+ ' >>> for num in range(5,12): #doctest: '
+ '+NORMALIZE_WHITESPACE\n'
+ " ... for base in 'dXob':\n"
+ " ... print('{0:{width}{base}}'.format(num, "
+ "base=base, width=width), end=' ')\n"
+ ' ... print()\n'
+ ' ...\n'
+ ' 5 5 5 101\n'
+ ' 6 6 6 110\n'
+ ' 7 7 7 111\n'
+ ' 8 8 10 1000\n'
+ ' 9 9 11 1001\n'
+ ' 10 A 12 1010\n'
+ ' 11 B 13 1011\n',
+ 'function': '\n'
+ 'Function definitions\n'
+ '********************\n'
+ '\n'
+ 'A function definition defines a user-defined function object '
+ '(see\n'
+ 'section The standard type hierarchy):\n'
+ '\n'
+ ' funcdef ::= [decorators] "def" funcname "(" '
+ '[parameter_list] ")" ["->" expression] ":" suite\n'
+ ' decorators ::= decorator+\n'
+ ' decorator ::= "@" dotted_name ["(" '
+ '[argument_list [","]] ")"] NEWLINE\n'
+ ' dotted_name ::= identifier ("." identifier)*\n'
+ ' parameter_list ::= defparameter ("," defparameter)* '
+ '["," [parameter_list_starargs]]\n'
+ ' | parameter_list_starargs\n'
+ ' parameter_list_starargs ::= "*" [parameter] ("," '
+ 'defparameter)* ["," ["**" parameter [","]]]\n'
+ ' | "**" parameter [","]\n'
+ ' parameter ::= identifier [":" expression]\n'
+ ' defparameter ::= parameter ["=" expression]\n'
+ ' funcname ::= identifier\n'
+ '\n'
+ 'A function definition is an executable statement. Its execution '
+ 'binds\n'
+ 'the function name in the current local namespace to a function '
+ 'object\n'
+ '(a wrapper around the executable code for the function). This\n'
+ 'function object contains a reference to the current global '
+ 'namespace\n'
+ 'as the global namespace to be used when the function is called.\n'
+ '\n'
+ 'The function definition does not execute the function body; this '
+ 'gets\n'
+ 'executed only when the function is called. [3]\n'
+ '\n'
+ 'A function definition may be wrapped by one or more *decorator*\n'
+ 'expressions. Decorator expressions are evaluated when the '
+ 'function is\n'
+ 'defined, in the scope that contains the function definition. '
+ 'The\n'
+ 'result must be a callable, which is invoked with the function '
+ 'object\n'
+ 'as the only argument. The returned value is bound to the '
+ 'function name\n'
+ 'instead of the function object. Multiple decorators are applied '
+ 'in\n'
+ 'nested fashion. For example, the following code\n'
+ '\n'
+ ' @f1(arg)\n'
+ ' @f2\n'
+ ' def func(): pass\n'
+ '\n'
+ 'is roughly equivalent to\n'
+ '\n'
+ ' def func(): pass\n'
+ ' func = f1(arg)(f2(func))\n'
+ '\n'
+ 'except that the original function is not temporarily bound to '
+ 'the name\n'
+ '"func".\n'
+ '\n'
+ 'When one or more *parameters* have the form *parameter* "="\n'
+ '*expression*, the function is said to have "default parameter '
+ 'values."\n'
+ 'For a parameter with a default value, the corresponding '
+ '*argument* may\n'
+ "be omitted from a call, in which case the parameter's default "
+ 'value is\n'
+ 'substituted. If a parameter has a default value, all following\n'
+ 'parameters up until the ""*"" must also have a default value --- '
+ 'this\n'
+ 'is a syntactic restriction that is not expressed by the '
+ 'grammar.\n'
+ '\n'
+ '**Default parameter values are evaluated from left to right when '
+ 'the\n'
+ 'function definition is executed.** This means that the '
+ 'expression is\n'
+ 'evaluated once, when the function is defined, and that the same '
+ '"pre-\n'
+ 'computed" value is used for each call. This is especially '
+ 'important\n'
+ 'to understand when a default parameter is a mutable object, such '
+ 'as a\n'
+ 'list or a dictionary: if the function modifies the object (e.g. '
+ 'by\n'
+ 'appending an item to a list), the default value is in effect '
+ 'modified.\n'
+ 'This is generally not what was intended. A way around this is '
+ 'to use\n'
+ '"None" as the default, and explicitly test for it in the body of '
+ 'the\n'
+ 'function, e.g.:\n'
+ '\n'
+ ' def whats_on_the_telly(penguin=None):\n'
+ ' if penguin is None:\n'
+ ' penguin = []\n'
+ ' penguin.append("property of the zoo")\n'
+ ' return penguin\n'
+ '\n'
+ 'Function call semantics are described in more detail in section '
+ 'Calls.\n'
+ 'A function call always assigns values to all parameters '
+ 'mentioned in\n'
+ 'the parameter list, either from position arguments, from '
+ 'keyword\n'
+ 'arguments, or from default values. If the form ""*identifier"" '
+ 'is\n'
+ 'present, it is initialized to a tuple receiving any excess '
+ 'positional\n'
+ 'parameters, defaulting to the empty tuple. If the form\n'
+ '""**identifier"" is present, it is initialized to a new ordered\n'
+ 'mapping receiving any excess keyword arguments, defaulting to a '
+ 'new\n'
+ 'empty mapping of the same type. Parameters after ""*"" or\n'
+ '""*identifier"" are keyword-only parameters and may only be '
+ 'passed\n'
+ 'used keyword arguments.\n'
+ '\n'
+ 'Parameters may have annotations of the form "": expression"" '
+ 'following\n'
+ 'the parameter name. Any parameter may have an annotation even '
+ 'those\n'
+ 'of the form "*identifier" or "**identifier". Functions may '
+ 'have\n'
+ '"return" annotation of the form ""-> expression"" after the '
+ 'parameter\n'
+ 'list. These annotations can be any valid Python expression and '
+ 'are\n'
+ 'evaluated when the function definition is executed. Annotations '
+ 'may\n'
+ 'be evaluated in a different order than they appear in the source '
+ 'code.\n'
+ 'The presence of annotations does not change the semantics of a\n'
+ 'function. The annotation values are available as values of a\n'
+ "dictionary keyed by the parameters' names in the "
+ '"__annotations__"\n'
+ 'attribute of the function object.\n'
+ '\n'
+ 'It is also possible to create anonymous functions (functions not '
+ 'bound\n'
+ 'to a name), for immediate use in expressions. This uses lambda\n'
+ 'expressions, described in section Lambdas. Note that the '
+ 'lambda\n'
+ 'expression is merely a shorthand for a simplified function '
+ 'definition;\n'
+ 'a function defined in a ""def"" statement can be passed around '
+ 'or\n'
+ 'assigned to another name just like a function defined by a '
+ 'lambda\n'
+ 'expression. The ""def"" form is actually more powerful since '
+ 'it\n'
+ 'allows the execution of multiple statements and annotations.\n'
+ '\n'
+ "**Programmer's note:** Functions are first-class objects. A "
+ '""def""\n'
+ 'statement executed inside a function definition defines a local\n'
+ 'function that can be returned or passed around. Free variables '
+ 'used\n'
+ 'in the nested function can access the local variables of the '
+ 'function\n'
+ 'containing the def. See section Naming and binding for '
+ 'details.\n'
+ '\n'
+ 'See also:\n'
+ '\n'
+ ' **PEP 3107** - Function Annotations\n'
+ ' The original specification for function annotations.\n',
+ 'global': '\n'
+ 'The "global" statement\n'
+ '**********************\n'
+ '\n'
+ ' global_stmt ::= "global" identifier ("," identifier)*\n'
+ '\n'
+ 'The "global" statement is a declaration which holds for the '
+ 'entire\n'
+ 'current code block. It means that the listed identifiers are to '
+ 'be\n'
+ 'interpreted as globals. It would be impossible to assign to a '
+ 'global\n'
+ 'variable without "global", although free variables may refer to\n'
+ 'globals without being declared global.\n'
+ '\n'
+ 'Names listed in a "global" statement must not be used in the same '
+ 'code\n'
+ 'block textually preceding that "global" statement.\n'
+ '\n'
+ 'Names listed in a "global" statement must not be defined as '
+ 'formal\n'
+ 'parameters or in a "for" loop control target, "class" definition,\n'
+ 'function definition, "import" statement, or variable annotation.\n'
+ '\n'
+ '**CPython implementation detail:** The current implementation does '
+ 'not\n'
+ 'enforce some of these restriction, but programs should not abuse '
+ 'this\n'
+ 'freedom, as future implementations may enforce them or silently '
+ 'change\n'
+ 'the meaning of the program.\n'
+ '\n'
+ '**Programmer\'s note:** the "global" is a directive to the '
+ 'parser. It\n'
+ 'applies only to code parsed at the same time as the "global"\n'
+ 'statement. In particular, a "global" statement contained in a '
+ 'string\n'
+ 'or code object supplied to the built-in "exec()" function does '
+ 'not\n'
+ 'affect the code block *containing* the function call, and code\n'
+ 'contained in such a string is unaffected by "global" statements in '
+ 'the\n'
+ 'code containing the function call. The same applies to the '
+ '"eval()"\n'
+ 'and "compile()" functions.\n',
+ 'id-classes': '\n'
+ 'Reserved classes of identifiers\n'
+ '*******************************\n'
+ '\n'
+ 'Certain classes of identifiers (besides keywords) have '
+ 'special\n'
+ 'meanings. These classes are identified by the patterns of '
+ 'leading and\n'
+ 'trailing underscore characters:\n'
+ '\n'
+ '"_*"\n'
+ ' Not imported by "from module import *". The special '
+ 'identifier "_"\n'
+ ' is used in the interactive interpreter to store the result '
+ 'of the\n'
+ ' last evaluation; it is stored in the "builtins" module. '
+ 'When not\n'
+ ' in interactive mode, "_" has no special meaning and is not '
+ 'defined.\n'
+ ' See section The import statement.\n'
+ '\n'
+ ' Note: The name "_" is often used in conjunction with\n'
+ ' internationalization; refer to the documentation for the\n'
+ ' "gettext" module for more information on this '
+ 'convention.\n'
+ '\n'
+ '"__*__"\n'
+ ' System-defined names. These names are defined by the '
+ 'interpreter\n'
+ ' and its implementation (including the standard library). '
+ 'Current\n'
+ ' system names are discussed in the Special method names '
+ 'section and\n'
+ ' elsewhere. More will likely be defined in future versions '
+ 'of\n'
+ ' Python. *Any* use of "__*__" names, in any context, that '
+ 'does not\n'
+ ' follow explicitly documented use, is subject to breakage '
+ 'without\n'
+ ' warning.\n'
+ '\n'
+ '"__*"\n'
+ ' Class-private names. Names in this category, when used '
+ 'within the\n'
+ ' context of a class definition, are re-written to use a '
+ 'mangled form\n'
+ ' to help avoid name clashes between "private" attributes of '
+ 'base and\n'
+ ' derived classes. See section Identifiers (Names).\n',
+ 'identifiers': '\n'
+ 'Identifiers and keywords\n'
+ '************************\n'
+ '\n'
+ 'Identifiers (also referred to as *names*) are described by '
+ 'the\n'
+ 'following lexical definitions.\n'
+ '\n'
+ 'The syntax of identifiers in Python is based on the Unicode '
+ 'standard\n'
+ 'annex UAX-31, with elaboration and changes as defined below; '
+ 'see also\n'
+ '**PEP 3131** for further details.\n'
+ '\n'
+ 'Within the ASCII range (U+0001..U+007F), the valid characters '
+ 'for\n'
+ 'identifiers are the same as in Python 2.x: the uppercase and '
+ 'lowercase\n'
+ 'letters "A" through "Z", the underscore "_" and, except for '
+ 'the first\n'
+ 'character, the digits "0" through "9".\n'
+ '\n'
+ 'Python 3.0 introduces additional characters from outside the '
+ 'ASCII\n'
+ 'range (see **PEP 3131**). For these characters, the '
+ 'classification\n'
+ 'uses the version of the Unicode Character Database as '
+ 'included in the\n'
+ '"unicodedata" module.\n'
+ '\n'
+ 'Identifiers are unlimited in length. Case is significant.\n'
+ '\n'
+ ' identifier ::= xid_start xid_continue*\n'
+ ' id_start ::= <all characters in general categories Lu, '
+ 'Ll, Lt, Lm, Lo, Nl, the underscore, and characters with the '
+ 'Other_ID_Start property>\n'
+ ' id_continue ::= <all characters in id_start, plus '
+ 'characters in the categories Mn, Mc, Nd, Pc and others with '
+ 'the Other_ID_Continue property>\n'
+ ' xid_start ::= <all characters in id_start whose NFKC '
+ 'normalization is in "id_start xid_continue*">\n'
+ ' xid_continue ::= <all characters in id_continue whose NFKC '
+ 'normalization is in "id_continue*">\n'
+ '\n'
+ 'The Unicode category codes mentioned above stand for:\n'
+ '\n'
+ '* *Lu* - uppercase letters\n'
+ '\n'
+ '* *Ll* - lowercase letters\n'
+ '\n'
+ '* *Lt* - titlecase letters\n'
+ '\n'
+ '* *Lm* - modifier letters\n'
+ '\n'
+ '* *Lo* - other letters\n'
+ '\n'
+ '* *Nl* - letter numbers\n'
+ '\n'
+ '* *Mn* - nonspacing marks\n'
+ '\n'
+ '* *Mc* - spacing combining marks\n'
+ '\n'
+ '* *Nd* - decimal numbers\n'
+ '\n'
+ '* *Pc* - connector punctuations\n'
+ '\n'
+ '* *Other_ID_Start* - explicit list of characters in '
+ 'PropList.txt to\n'
+ ' support backwards compatibility\n'
+ '\n'
+ '* *Other_ID_Continue* - likewise\n'
+ '\n'
+ 'All identifiers are converted into the normal form NFKC while '
+ 'parsing;\n'
+ 'comparison of identifiers is based on NFKC.\n'
+ '\n'
+ 'A non-normative HTML file listing all valid identifier '
+ 'characters for\n'
+ 'Unicode 4.1 can be found at https://www.dcl.hpi.uni-\n'
+ 'potsdam.de/home/loewis/table-3131.html.\n'
+ '\n'
+ '\n'
+ 'Keywords\n'
+ '========\n'
+ '\n'
+ 'The following identifiers are used as reserved words, or '
+ '*keywords* of\n'
+ 'the language, and cannot be used as ordinary identifiers. '
+ 'They must\n'
+ 'be spelled exactly as written here:\n'
+ '\n'
+ ' False class finally is return\n'
+ ' None continue for lambda try\n'
+ ' True def from nonlocal while\n'
+ ' and del global not with\n'
+ ' as elif if or yield\n'
+ ' assert else import pass\n'
+ ' break except in raise\n'
+ '\n'
+ '\n'
+ 'Reserved classes of identifiers\n'
+ '===============================\n'
+ '\n'
+ 'Certain classes of identifiers (besides keywords) have '
+ 'special\n'
+ 'meanings. These classes are identified by the patterns of '
+ 'leading and\n'
+ 'trailing underscore characters:\n'
+ '\n'
+ '"_*"\n'
+ ' Not imported by "from module import *". The special '
+ 'identifier "_"\n'
+ ' is used in the interactive interpreter to store the result '
+ 'of the\n'
+ ' last evaluation; it is stored in the "builtins" module. '
+ 'When not\n'
+ ' in interactive mode, "_" has no special meaning and is not '
+ 'defined.\n'
+ ' See section The import statement.\n'
+ '\n'
+ ' Note: The name "_" is often used in conjunction with\n'
+ ' internationalization; refer to the documentation for '
+ 'the\n'
+ ' "gettext" module for more information on this '
+ 'convention.\n'
+ '\n'
+ '"__*__"\n'
+ ' System-defined names. These names are defined by the '
+ 'interpreter\n'
+ ' and its implementation (including the standard library). '
+ 'Current\n'
+ ' system names are discussed in the Special method names '
+ 'section and\n'
+ ' elsewhere. More will likely be defined in future versions '
+ 'of\n'
+ ' Python. *Any* use of "__*__" names, in any context, that '
+ 'does not\n'
+ ' follow explicitly documented use, is subject to breakage '
+ 'without\n'
+ ' warning.\n'
+ '\n'
+ '"__*"\n'
+ ' Class-private names. Names in this category, when used '
+ 'within the\n'
+ ' context of a class definition, are re-written to use a '
+ 'mangled form\n'
+ ' to help avoid name clashes between "private" attributes of '
+ 'base and\n'
+ ' derived classes. See section Identifiers (Names).\n',
+ 'if': '\n'
+ 'The "if" statement\n'
+ '******************\n'
+ '\n'
+ 'The "if" statement is used for conditional execution:\n'
+ '\n'
+ ' if_stmt ::= "if" expression ":" suite\n'
+ ' ( "elif" expression ":" suite )*\n'
+ ' ["else" ":" suite]\n'
+ '\n'
+ 'It selects exactly one of the suites by evaluating the expressions '
+ 'one\n'
+ 'by one until one is found to be true (see section Boolean operations\n'
+ 'for the definition of true and false); then that suite is executed\n'
+ '(and no other part of the "if" statement is executed or evaluated).\n'
+ 'If all expressions are false, the suite of the "else" clause, if\n'
+ 'present, is executed.\n',
+ 'imaginary': '\n'
+ 'Imaginary literals\n'
+ '******************\n'
+ '\n'
+ 'Imaginary literals are described by the following lexical '
+ 'definitions:\n'
+ '\n'
+ ' imagnumber ::= (floatnumber | digitpart) ("j" | "J")\n'
+ '\n'
+ 'An imaginary literal yields a complex number with a real part '
+ 'of 0.0.\n'
+ 'Complex numbers are represented as a pair of floating point '
+ 'numbers\n'
+ 'and have the same restrictions on their range. To create a '
+ 'complex\n'
+ 'number with a nonzero real part, add a floating point number to '
+ 'it,\n'
+ 'e.g., "(3+4j)". Some examples of imaginary literals:\n'
+ '\n'
+ ' 3.14j 10.j 10j .001j 1e100j 3.14e-10j '
+ '3.14_15_93j\n',
+ 'import': '\n'
+ 'The "import" statement\n'
+ '**********************\n'
+ '\n'
+ ' import_stmt ::= "import" module ["as" name] ( "," module '
+ '["as" name] )*\n'
+ ' | "from" relative_module "import" identifier '
+ '["as" name]\n'
+ ' ( "," identifier ["as" name] )*\n'
+ ' | "from" relative_module "import" "(" '
+ 'identifier ["as" name]\n'
+ ' ( "," identifier ["as" name] )* [","] ")"\n'
+ ' | "from" module "import" "*"\n'
+ ' module ::= (identifier ".")* identifier\n'
+ ' relative_module ::= "."* module | "."+\n'
+ ' name ::= identifier\n'
+ '\n'
+ 'The basic import statement (no "from" clause) is executed in two\n'
+ 'steps:\n'
+ '\n'
+ '1. find a module, loading and initializing it if necessary\n'
+ '\n'
+ '2. define a name or names in the local namespace for the scope\n'
+ ' where the "import" statement occurs.\n'
+ '\n'
+ 'When the statement contains multiple clauses (separated by commas) '
+ 'the\n'
+ 'two steps are carried out separately for each clause, just as '
+ 'though\n'
+ 'the clauses had been separated out into individual import '
+ 'statements.\n'
+ '\n'
+ 'The details of the first step, finding and loading modules are\n'
+ 'described in greater detail in the section on the import system, '
+ 'which\n'
+ 'also describes the various types of packages and modules that can '
+ 'be\n'
+ 'imported, as well as all the hooks that can be used to customize '
+ 'the\n'
+ 'import system. Note that failures in this step may indicate '
+ 'either\n'
+ 'that the module could not be located, *or* that an error occurred\n'
+ 'while initializing the module, which includes execution of the\n'
+ "module's code.\n"
+ '\n'
+ 'If the requested module is retrieved successfully, it will be '
+ 'made\n'
+ 'available in the local namespace in one of three ways:\n'
+ '\n'
+ '* If the module name is followed by "as", then the name following\n'
+ ' "as" is bound directly to the imported module.\n'
+ '\n'
+ '* If no other name is specified, and the module being imported is '
+ 'a\n'
+ " top level module, the module's name is bound in the local "
+ 'namespace\n'
+ ' as a reference to the imported module\n'
+ '\n'
+ '* If the module being imported is *not* a top level module, then '
+ 'the\n'
+ ' name of the top level package that contains the module is bound '
+ 'in\n'
+ ' the local namespace as a reference to the top level package. '
+ 'The\n'
+ ' imported module must be accessed using its full qualified name\n'
+ ' rather than directly\n'
+ '\n'
+ 'The "from" form uses a slightly more complex process:\n'
+ '\n'
+ '1. find the module specified in the "from" clause, loading and\n'
+ ' initializing it if necessary;\n'
+ '\n'
+ '2. for each of the identifiers specified in the "import" clauses:\n'
+ '\n'
+ ' 1. check if the imported module has an attribute by that name\n'
+ '\n'
+ ' 2. if not, attempt to import a submodule with that name and '
+ 'then\n'
+ ' check the imported module again for that attribute\n'
+ '\n'
+ ' 3. if the attribute is not found, "ImportError" is raised.\n'
+ '\n'
+ ' 4. otherwise, a reference to that value is stored in the local\n'
+ ' namespace, using the name in the "as" clause if it is '
+ 'present,\n'
+ ' otherwise using the attribute name\n'
+ '\n'
+ 'Examples:\n'
+ '\n'
+ ' import foo # foo imported and bound locally\n'
+ ' import foo.bar.baz # foo.bar.baz imported, foo bound '
+ 'locally\n'
+ ' import foo.bar.baz as fbb # foo.bar.baz imported and bound as '
+ 'fbb\n'
+ ' from foo.bar import baz # foo.bar.baz imported and bound as '
+ 'baz\n'
+ ' from foo import attr # foo imported and foo.attr bound as '
+ 'attr\n'
+ '\n'
+ 'If the list of identifiers is replaced by a star ("\'*\'"), all '
+ 'public\n'
+ 'names defined in the module are bound in the local namespace for '
+ 'the\n'
+ 'scope where the "import" statement occurs.\n'
+ '\n'
+ 'The *public names* defined by a module are determined by checking '
+ 'the\n'
+ 'module\'s namespace for a variable named "__all__"; if defined, it '
+ 'must\n'
+ 'be a sequence of strings which are names defined or imported by '
+ 'that\n'
+ 'module. The names given in "__all__" are all considered public '
+ 'and\n'
+ 'are required to exist. If "__all__" is not defined, the set of '
+ 'public\n'
+ "names includes all names found in the module's namespace which do "
+ 'not\n'
+ 'begin with an underscore character ("\'_\'"). "__all__" should '
+ 'contain\n'
+ 'the entire public API. It is intended to avoid accidentally '
+ 'exporting\n'
+ 'items that are not part of the API (such as library modules which '
+ 'were\n'
+ 'imported and used within the module).\n'
+ '\n'
+ 'The wild card form of import --- "from module import *" --- is '
+ 'only\n'
+ 'allowed at the module level. Attempting to use it in class or\n'
+ 'function definitions will raise a "SyntaxError".\n'
+ '\n'
+ 'When specifying what module to import you do not have to specify '
+ 'the\n'
+ 'absolute name of the module. When a module or package is '
+ 'contained\n'
+ 'within another package it is possible to make a relative import '
+ 'within\n'
+ 'the same top package without having to mention the package name. '
+ 'By\n'
+ 'using leading dots in the specified module or package after "from" '
+ 'you\n'
+ 'can specify how high to traverse up the current package hierarchy\n'
+ 'without specifying exact names. One leading dot means the current\n'
+ 'package where the module making the import exists. Two dots means '
+ 'up\n'
+ 'one package level. Three dots is up two levels, etc. So if you '
+ 'execute\n'
+ '"from . import mod" from a module in the "pkg" package then you '
+ 'will\n'
+ 'end up importing "pkg.mod". If you execute "from ..subpkg2 import '
+ 'mod"\n'
+ 'from within "pkg.subpkg1" you will import "pkg.subpkg2.mod". The\n'
+ 'specification for relative imports is contained within **PEP '
+ '328**.\n'
+ '\n'
+ '"importlib.import_module()" is provided to support applications '
+ 'that\n'
+ 'determine dynamically the modules to be loaded.\n'
+ '\n'
+ '\n'
+ 'Future statements\n'
+ '=================\n'
+ '\n'
+ 'A *future statement* is a directive to the compiler that a '
+ 'particular\n'
+ 'module should be compiled using syntax or semantics that will be\n'
+ 'available in a specified future release of Python where the '
+ 'feature\n'
+ 'becomes standard.\n'
+ '\n'
+ 'The future statement is intended to ease migration to future '
+ 'versions\n'
+ 'of Python that introduce incompatible changes to the language. '
+ 'It\n'
+ 'allows use of the new features on a per-module basis before the\n'
+ 'release in which the feature becomes standard.\n'
+ '\n'
+ ' future_statement ::= "from" "__future__" "import" feature ["as" '
+ 'name]\n'
+ ' ("," feature ["as" name])*\n'
+ ' | "from" "__future__" "import" "(" feature '
+ '["as" name]\n'
+ ' ("," feature ["as" name])* [","] ")"\n'
+ ' feature ::= identifier\n'
+ ' name ::= identifier\n'
+ '\n'
+ 'A future statement must appear near the top of the module. The '
+ 'only\n'
+ 'lines that can appear before a future statement are:\n'
+ '\n'
+ '* the module docstring (if any),\n'
+ '\n'
+ '* comments,\n'
+ '\n'
+ '* blank lines, and\n'
+ '\n'
+ '* other future statements.\n'
+ '\n'
+ 'The features recognized by Python 3.0 are "absolute_import",\n'
+ '"division", "generators", "unicode_literals", "print_function",\n'
+ '"nested_scopes" and "with_statement". They are all redundant '
+ 'because\n'
+ 'they are always enabled, and only kept for backwards '
+ 'compatibility.\n'
+ '\n'
+ 'A future statement is recognized and treated specially at compile\n'
+ 'time: Changes to the semantics of core constructs are often\n'
+ 'implemented by generating different code. It may even be the '
+ 'case\n'
+ 'that a new feature introduces new incompatible syntax (such as a '
+ 'new\n'
+ 'reserved word), in which case the compiler may need to parse the\n'
+ 'module differently. Such decisions cannot be pushed off until\n'
+ 'runtime.\n'
+ '\n'
+ 'For any given release, the compiler knows which feature names '
+ 'have\n'
+ 'been defined, and raises a compile-time error if a future '
+ 'statement\n'
+ 'contains a feature not known to it.\n'
+ '\n'
+ 'The direct runtime semantics are the same as for any import '
+ 'statement:\n'
+ 'there is a standard module "__future__", described later, and it '
+ 'will\n'
+ 'be imported in the usual way at the time the future statement is\n'
+ 'executed.\n'
+ '\n'
+ 'The interesting runtime semantics depend on the specific feature\n'
+ 'enabled by the future statement.\n'
+ '\n'
+ 'Note that there is nothing special about the statement:\n'
+ '\n'
+ ' import __future__ [as name]\n'
+ '\n'
+ "That is not a future statement; it's an ordinary import statement "
+ 'with\n'
+ 'no special semantics or syntax restrictions.\n'
+ '\n'
+ 'Code compiled by calls to the built-in functions "exec()" and\n'
+ '"compile()" that occur in a module "M" containing a future '
+ 'statement\n'
+ 'will, by default, use the new syntax or semantics associated with '
+ 'the\n'
+ 'future statement. This can be controlled by optional arguments '
+ 'to\n'
+ '"compile()" --- see the documentation of that function for '
+ 'details.\n'
+ '\n'
+ 'A future statement typed at an interactive interpreter prompt '
+ 'will\n'
+ 'take effect for the rest of the interpreter session. If an\n'
+ 'interpreter is started with the "-i" option, is passed a script '
+ 'name\n'
+ 'to execute, and the script includes a future statement, it will be '
+ 'in\n'
+ 'effect in the interactive session started after the script is\n'
+ 'executed.\n'
+ '\n'
+ 'See also:\n'
+ '\n'
+ ' **PEP 236** - Back to the __future__\n'
+ ' The original proposal for the __future__ mechanism.\n',
+ 'in': '\n'
+ 'Membership test operations\n'
+ '**************************\n'
+ '\n'
+ 'The operators "in" and "not in" test for membership. "x in s"\n'
+ 'evaluates to true if *x* is a member of *s*, and false otherwise. "x\n'
+ 'not in s" returns the negation of "x in s". All built-in sequences\n'
+ 'and set types support this as well as dictionary, for which "in" '
+ 'tests\n'
+ 'whether the dictionary has a given key. For container types such as\n'
+ 'list, tuple, set, frozenset, dict, or collections.deque, the\n'
+ 'expression "x in y" is equivalent to "any(x is e or x == e for e in\n'
+ 'y)".\n'
+ '\n'
+ 'For the string and bytes types, "x in y" is true if and only if *x* '
+ 'is\n'
+ 'a substring of *y*. An equivalent test is "y.find(x) != -1". Empty\n'
+ 'strings are always considered to be a substring of any other string,\n'
+ 'so """ in "abc"" will return "True".\n'
+ '\n'
+ 'For user-defined classes which define the "__contains__()" method, "x\n'
+ 'in y" is true if and only if "y.__contains__(x)" is true.\n'
+ '\n'
+ 'For user-defined classes which do not define "__contains__()" but do\n'
+ 'define "__iter__()", "x in y" is true if some value "z" with "x == z"\n'
+ 'is produced while iterating over "y". If an exception is raised\n'
+ 'during the iteration, it is as if "in" raised that exception.\n'
+ '\n'
+ 'Lastly, the old-style iteration protocol is tried: if a class defines\n'
+ '"__getitem__()", "x in y" is true if and only if there is a non-\n'
+ 'negative integer index *i* such that "x == y[i]", and all lower\n'
+ 'integer indices do not raise "IndexError" exception. (If any other\n'
+ 'exception is raised, it is as if "in" raised that exception).\n'
+ '\n'
+ 'The operator "not in" is defined to have the inverse true value of\n'
+ '"in".\n',
+ 'integers': '\n'
+ 'Integer literals\n'
+ '****************\n'
+ '\n'
+ 'Integer literals are described by the following lexical '
+ 'definitions:\n'
+ '\n'
+ ' integer ::= decinteger | bininteger | octinteger | '
+ 'hexinteger\n'
+ ' decinteger ::= nonzerodigit (["_"] digit)* | "0"+ (["_"] '
+ '"0")*\n'
+ ' bininteger ::= "0" ("b" | "B") (["_"] bindigit)+\n'
+ ' octinteger ::= "0" ("o" | "O") (["_"] octdigit)+\n'
+ ' hexinteger ::= "0" ("x" | "X") (["_"] hexdigit)+\n'
+ ' nonzerodigit ::= "1"..."9"\n'
+ ' digit ::= "0"..."9"\n'
+ ' bindigit ::= "0" | "1"\n'
+ ' octdigit ::= "0"..."7"\n'
+ ' hexdigit ::= digit | "a"..."f" | "A"..."F"\n'
+ '\n'
+ 'There is no limit for the length of integer literals apart from '
+ 'what\n'
+ 'can be stored in available memory.\n'
+ '\n'
+ 'Underscores are ignored for determining the numeric value of '
+ 'the\n'
+ 'literal. They can be used to group digits for enhanced '
+ 'readability.\n'
+ 'One underscore can occur between digits, and after base '
+ 'specifiers\n'
+ 'like "0x".\n'
+ '\n'
+ 'Note that leading zeros in a non-zero decimal number are not '
+ 'allowed.\n'
+ 'This is for disambiguation with C-style octal literals, which '
+ 'Python\n'
+ 'used before version 3.0.\n'
+ '\n'
+ 'Some examples of integer literals:\n'
+ '\n'
+ ' 7 2147483647 0o177 0b100110111\n'
+ ' 3 79228162514264337593543950336 0o377 0xdeadbeef\n'
+ ' 100_000_000_000 0b_1110_0101\n'
+ '\n'
+ 'Changed in version 3.6: Underscores are now allowed for '
+ 'grouping\n'
+ 'purposes in literals.\n',
+ 'lambda': '\n'
+ 'Lambdas\n'
+ '*******\n'
+ '\n'
+ ' lambda_expr ::= "lambda" [parameter_list]: expression\n'
+ ' lambda_expr_nocond ::= "lambda" [parameter_list]: '
+ 'expression_nocond\n'
+ '\n'
+ 'Lambda expressions (sometimes called lambda forms) are used to '
+ 'create\n'
+ 'anonymous functions. The expression "lambda arguments: '
+ 'expression"\n'
+ 'yields a function object. The unnamed object behaves like a '
+ 'function\n'
+ 'object defined with:\n'
+ '\n'
+ ' def <lambda>(arguments):\n'
+ ' return expression\n'
+ '\n'
+ 'See section Function definitions for the syntax of parameter '
+ 'lists.\n'
+ 'Note that functions created with lambda expressions cannot '
+ 'contain\n'
+ 'statements or annotations.\n',
+ 'lists': '\n'
+ 'List displays\n'
+ '*************\n'
+ '\n'
+ 'A list display is a possibly empty series of expressions enclosed '
+ 'in\n'
+ 'square brackets:\n'
+ '\n'
+ ' list_display ::= "[" [starred_list | comprehension] "]"\n'
+ '\n'
+ 'A list display yields a new list object, the contents being '
+ 'specified\n'
+ 'by either a list of expressions or a comprehension. When a comma-\n'
+ 'separated list of expressions is supplied, its elements are '
+ 'evaluated\n'
+ 'from left to right and placed into the list object in that order.\n'
+ 'When a comprehension is supplied, the list is constructed from the\n'
+ 'elements resulting from the comprehension.\n',
+ 'naming': '\n'
+ 'Naming and binding\n'
+ '******************\n'
+ '\n'
+ '\n'
+ 'Binding of names\n'
+ '================\n'
+ '\n'
+ '*Names* refer to objects. Names are introduced by name binding\n'
+ 'operations.\n'
+ '\n'
+ 'The following constructs bind names: formal parameters to '
+ 'functions,\n'
+ '"import" statements, class and function definitions (these bind '
+ 'the\n'
+ 'class or function name in the defining block), and targets that '
+ 'are\n'
+ 'identifiers if occurring in an assignment, "for" loop header, or '
+ 'after\n'
+ '"as" in a "with" statement or "except" clause. The "import" '
+ 'statement\n'
+ 'of the form "from ... import *" binds all names defined in the\n'
+ 'imported module, except those beginning with an underscore. This '
+ 'form\n'
+ 'may only be used at the module level.\n'
+ '\n'
+ 'A target occurring in a "del" statement is also considered bound '
+ 'for\n'
+ 'this purpose (though the actual semantics are to unbind the '
+ 'name).\n'
+ '\n'
+ 'Each assignment or import statement occurs within a block defined '
+ 'by a\n'
+ 'class or function definition or at the module level (the '
+ 'top-level\n'
+ 'code block).\n'
+ '\n'
+ 'If a name is bound in a block, it is a local variable of that '
+ 'block,\n'
+ 'unless declared as "nonlocal" or "global". If a name is bound at '
+ 'the\n'
+ 'module level, it is a global variable. (The variables of the '
+ 'module\n'
+ 'code block are local and global.) If a variable is used in a '
+ 'code\n'
+ 'block but not defined there, it is a *free variable*.\n'
+ '\n'
+ 'Each occurrence of a name in the program text refers to the '
+ '*binding*\n'
+ 'of that name established by the following name resolution rules.\n'
+ '\n'
+ '\n'
+ 'Resolution of names\n'
+ '===================\n'
+ '\n'
+ 'A *scope* defines the visibility of a name within a block. If a '
+ 'local\n'
+ 'variable is defined in a block, its scope includes that block. If '
+ 'the\n'
+ 'definition occurs in a function block, the scope extends to any '
+ 'blocks\n'
+ 'contained within the defining one, unless a contained block '
+ 'introduces\n'
+ 'a different binding for the name.\n'
+ '\n'
+ 'When a name is used in a code block, it is resolved using the '
+ 'nearest\n'
+ 'enclosing scope. The set of all such scopes visible to a code '
+ 'block\n'
+ "is called the block's *environment*.\n"
+ '\n'
+ 'When a name is not found at all, a "NameError" exception is '
+ 'raised. If\n'
+ 'the current scope is a function scope, and the name refers to a '
+ 'local\n'
+ 'variable that has not yet been bound to a value at the point where '
+ 'the\n'
+ 'name is used, an "UnboundLocalError" exception is raised.\n'
+ '"UnboundLocalError" is a subclass of "NameError".\n'
+ '\n'
+ 'If a name binding operation occurs anywhere within a code block, '
+ 'all\n'
+ 'uses of the name within the block are treated as references to '
+ 'the\n'
+ 'current block. This can lead to errors when a name is used within '
+ 'a\n'
+ 'block before it is bound. This rule is subtle. Python lacks\n'
+ 'declarations and allows name binding operations to occur anywhere\n'
+ 'within a code block. The local variables of a code block can be\n'
+ 'determined by scanning the entire text of the block for name '
+ 'binding\n'
+ 'operations.\n'
+ '\n'
+ 'If the "global" statement occurs within a block, all uses of the '
+ 'name\n'
+ 'specified in the statement refer to the binding of that name in '
+ 'the\n'
+ 'top-level namespace. Names are resolved in the top-level '
+ 'namespace by\n'
+ 'searching the global namespace, i.e. the namespace of the module\n'
+ 'containing the code block, and the builtins namespace, the '
+ 'namespace\n'
+ 'of the module "builtins". The global namespace is searched '
+ 'first. If\n'
+ 'the name is not found there, the builtins namespace is searched. '
+ 'The\n'
+ '"global" statement must precede all uses of the name.\n'
+ '\n'
+ 'The "global" statement has the same scope as a name binding '
+ 'operation\n'
+ 'in the same block. If the nearest enclosing scope for a free '
+ 'variable\n'
+ 'contains a global statement, the free variable is treated as a '
+ 'global.\n'
+ '\n'
+ 'The "nonlocal" statement causes corresponding names to refer to\n'
+ 'previously bound variables in the nearest enclosing function '
+ 'scope.\n'
+ '"SyntaxError" is raised at compile time if the given name does '
+ 'not\n'
+ 'exist in any enclosing function scope.\n'
+ '\n'
+ 'The namespace for a module is automatically created the first time '
+ 'a\n'
+ 'module is imported. The main module for a script is always '
+ 'called\n'
+ '"__main__".\n'
+ '\n'
+ 'Class definition blocks and arguments to "exec()" and "eval()" '
+ 'are\n'
+ 'special in the context of name resolution. A class definition is '
+ 'an\n'
+ 'executable statement that may use and define names. These '
+ 'references\n'
+ 'follow the normal rules for name resolution with an exception '
+ 'that\n'
+ 'unbound local variables are looked up in the global namespace. '
+ 'The\n'
+ 'namespace of the class definition becomes the attribute dictionary '
+ 'of\n'
+ 'the class. The scope of names defined in a class block is limited '
+ 'to\n'
+ 'the class block; it does not extend to the code blocks of methods '
+ '--\n'
+ 'this includes comprehensions and generator expressions since they '
+ 'are\n'
+ 'implemented using a function scope. This means that the '
+ 'following\n'
+ 'will fail:\n'
+ '\n'
+ ' class A:\n'
+ ' a = 42\n'
+ ' b = list(a + i for i in range(10))\n'
+ '\n'
+ '\n'
+ 'Builtins and restricted execution\n'
+ '=================================\n'
+ '\n'
+ 'The builtins namespace associated with the execution of a code '
+ 'block\n'
+ 'is actually found by looking up the name "__builtins__" in its '
+ 'global\n'
+ 'namespace; this should be a dictionary or a module (in the latter '
+ 'case\n'
+ "the module's dictionary is used). By default, when in the "
+ '"__main__"\n'
+ 'module, "__builtins__" is the built-in module "builtins"; when in '
+ 'any\n'
+ 'other module, "__builtins__" is an alias for the dictionary of '
+ 'the\n'
+ '"builtins" module itself. "__builtins__" can be set to a '
+ 'user-created\n'
+ 'dictionary to create a weak form of restricted execution.\n'
+ '\n'
+ '**CPython implementation detail:** Users should not touch\n'
+ '"__builtins__"; it is strictly an implementation detail. Users\n'
+ 'wanting to override values in the builtins namespace should '
+ '"import"\n'
+ 'the "builtins" module and modify its attributes appropriately.\n'
+ '\n'
+ '\n'
+ 'Interaction with dynamic features\n'
+ '=================================\n'
+ '\n'
+ 'Name resolution of free variables occurs at runtime, not at '
+ 'compile\n'
+ 'time. This means that the following code will print 42:\n'
+ '\n'
+ ' i = 10\n'
+ ' def f():\n'
+ ' print(i)\n'
+ ' i = 42\n'
+ ' f()\n'
+ '\n'
+ 'There are several cases where Python statements are illegal when '
+ 'used\n'
+ 'in conjunction with nested scopes that contain free variables.\n'
+ '\n'
+ 'If a variable is referenced in an enclosing scope, it is illegal '
+ 'to\n'
+ 'delete the name. An error will be reported at compile time.\n'
+ '\n'
+ 'The "eval()" and "exec()" functions do not have access to the '
+ 'full\n'
+ 'environment for resolving names. Names may be resolved in the '
+ 'local\n'
+ 'and global namespaces of the caller. Free variables are not '
+ 'resolved\n'
+ 'in the nearest enclosing namespace, but in the global namespace. '
+ '[1]\n'
+ 'The "exec()" and "eval()" functions have optional arguments to\n'
+ 'override the global and local namespace. If only one namespace '
+ 'is\n'
+ 'specified, it is used for both.\n',
+ 'nonlocal': '\n'
+ 'The "nonlocal" statement\n'
+ '************************\n'
+ '\n'
+ ' nonlocal_stmt ::= "nonlocal" identifier ("," identifier)*\n'
+ '\n'
+ 'The "nonlocal" statement causes the listed identifiers to refer '
+ 'to\n'
+ 'previously bound variables in the nearest enclosing scope '
+ 'excluding\n'
+ 'globals. This is important because the default behavior for '
+ 'binding is\n'
+ 'to search the local namespace first. The statement allows\n'
+ 'encapsulated code to rebind variables outside of the local '
+ 'scope\n'
+ 'besides the global (module) scope.\n'
+ '\n'
+ 'Names listed in a "nonlocal" statement, unlike those listed in '
+ 'a\n'
+ '"global" statement, must refer to pre-existing bindings in an\n'
+ 'enclosing scope (the scope in which a new binding should be '
+ 'created\n'
+ 'cannot be determined unambiguously).\n'
+ '\n'
+ 'Names listed in a "nonlocal" statement must not collide with '
+ 'pre-\n'
+ 'existing bindings in the local scope.\n'
+ '\n'
+ 'See also:\n'
+ '\n'
+ ' **PEP 3104** - Access to Names in Outer Scopes\n'
+ ' The specification for the "nonlocal" statement.\n',
+ 'numbers': '\n'
+ 'Numeric literals\n'
+ '****************\n'
+ '\n'
+ 'There are three types of numeric literals: integers, floating '
+ 'point\n'
+ 'numbers, and imaginary numbers. There are no complex literals\n'
+ '(complex numbers can be formed by adding a real number and an\n'
+ 'imaginary number).\n'
+ '\n'
+ 'Note that numeric literals do not include a sign; a phrase like '
+ '"-1"\n'
+ 'is actually an expression composed of the unary operator \'"-"\' '
+ 'and the\n'
+ 'literal "1".\n',
+ 'numeric-types': '\n'
+ 'Emulating numeric types\n'
+ '***********************\n'
+ '\n'
+ 'The following methods can be defined to emulate numeric '
+ 'objects.\n'
+ 'Methods corresponding to operations that are not supported '
+ 'by the\n'
+ 'particular kind of number implemented (e.g., bitwise '
+ 'operations for\n'
+ 'non-integral numbers) should be left undefined.\n'
+ '\n'
+ 'object.__add__(self, other)\n'
+ 'object.__sub__(self, other)\n'
+ 'object.__mul__(self, other)\n'
+ 'object.__matmul__(self, other)\n'
+ 'object.__truediv__(self, other)\n'
+ 'object.__floordiv__(self, other)\n'
+ 'object.__mod__(self, other)\n'
+ 'object.__divmod__(self, other)\n'
+ 'object.__pow__(self, other[, modulo])\n'
+ 'object.__lshift__(self, other)\n'
+ 'object.__rshift__(self, other)\n'
+ 'object.__and__(self, other)\n'
+ 'object.__xor__(self, other)\n'
+ 'object.__or__(self, other)\n'
+ '\n'
+ ' These methods are called to implement the binary '
+ 'arithmetic\n'
+ ' operations ("+", "-", "*", "@", "/", "//", "%", '
+ '"divmod()",\n'
+ ' "pow()", "**", "<<", ">>", "&", "^", "|"). For '
+ 'instance, to\n'
+ ' evaluate the expression "x + y", where *x* is an '
+ 'instance of a\n'
+ ' class that has an "__add__()" method, "x.__add__(y)" is '
+ 'called.\n'
+ ' The "__divmod__()" method should be the equivalent to '
+ 'using\n'
+ ' "__floordiv__()" and "__mod__()"; it should not be '
+ 'related to\n'
+ ' "__truediv__()". Note that "__pow__()" should be '
+ 'defined to accept\n'
+ ' an optional third argument if the ternary version of the '
+ 'built-in\n'
+ ' "pow()" function is to be supported.\n'
+ '\n'
+ ' If one of those methods does not support the operation '
+ 'with the\n'
+ ' supplied arguments, it should return "NotImplemented".\n'
+ '\n'
+ 'object.__radd__(self, other)\n'
+ 'object.__rsub__(self, other)\n'
+ 'object.__rmul__(self, other)\n'
+ 'object.__rmatmul__(self, other)\n'
+ 'object.__rtruediv__(self, other)\n'
+ 'object.__rfloordiv__(self, other)\n'
+ 'object.__rmod__(self, other)\n'
+ 'object.__rdivmod__(self, other)\n'
+ 'object.__rpow__(self, other)\n'
+ 'object.__rlshift__(self, other)\n'
+ 'object.__rrshift__(self, other)\n'
+ 'object.__rand__(self, other)\n'
+ 'object.__rxor__(self, other)\n'
+ 'object.__ror__(self, other)\n'
+ '\n'
+ ' These methods are called to implement the binary '
+ 'arithmetic\n'
+ ' operations ("+", "-", "*", "@", "/", "//", "%", '
+ '"divmod()",\n'
+ ' "pow()", "**", "<<", ">>", "&", "^", "|") with reflected '
+ '(swapped)\n'
+ ' operands. These functions are only called if the left '
+ 'operand does\n'
+ ' not support the corresponding operation [3] and the '
+ 'operands are of\n'
+ ' different types. [4] For instance, to evaluate the '
+ 'expression "x -\n'
+ ' y", where *y* is an instance of a class that has an '
+ '"__rsub__()"\n'
+ ' method, "y.__rsub__(x)" is called if "x.__sub__(y)" '
+ 'returns\n'
+ ' *NotImplemented*.\n'
+ '\n'
+ ' Note that ternary "pow()" will not try calling '
+ '"__rpow__()" (the\n'
+ ' coercion rules would become too complicated).\n'
+ '\n'
+ " Note: If the right operand's type is a subclass of the "
+ 'left\n'
+ " operand's type and that subclass provides the "
+ 'reflected method\n'
+ ' for the operation, this method will be called before '
+ 'the left\n'
+ " operand's non-reflected method. This behavior allows "
+ 'subclasses\n'
+ " to override their ancestors' operations.\n"
+ '\n'
+ 'object.__iadd__(self, other)\n'
+ 'object.__isub__(self, other)\n'
+ 'object.__imul__(self, other)\n'
+ 'object.__imatmul__(self, other)\n'
+ 'object.__itruediv__(self, other)\n'
+ 'object.__ifloordiv__(self, other)\n'
+ 'object.__imod__(self, other)\n'
+ 'object.__ipow__(self, other[, modulo])\n'
+ 'object.__ilshift__(self, other)\n'
+ 'object.__irshift__(self, other)\n'
+ 'object.__iand__(self, other)\n'
+ 'object.__ixor__(self, other)\n'
+ 'object.__ior__(self, other)\n'
+ '\n'
+ ' These methods are called to implement the augmented '
+ 'arithmetic\n'
+ ' assignments ("+=", "-=", "*=", "@=", "/=", "//=", "%=", '
+ '"**=",\n'
+ ' "<<=", ">>=", "&=", "^=", "|="). These methods should '
+ 'attempt to\n'
+ ' do the operation in-place (modifying *self*) and return '
+ 'the result\n'
+ ' (which could be, but does not have to be, *self*). If a '
+ 'specific\n'
+ ' method is not defined, the augmented assignment falls '
+ 'back to the\n'
+ ' normal methods. For instance, if *x* is an instance of '
+ 'a class\n'
+ ' with an "__iadd__()" method, "x += y" is equivalent to '
+ '"x =\n'
+ ' x.__iadd__(y)" . Otherwise, "x.__add__(y)" and '
+ '"y.__radd__(x)" are\n'
+ ' considered, as with the evaluation of "x + y". In '
+ 'certain\n'
+ ' situations, augmented assignment can result in '
+ 'unexpected errors\n'
+ " (see Why does a_tuple[i] += ['item'] raise an exception "
+ 'when the\n'
+ ' addition works?), but this behavior is in fact part of '
+ 'the data\n'
+ ' model.\n'
+ '\n'
+ 'object.__neg__(self)\n'
+ 'object.__pos__(self)\n'
+ 'object.__abs__(self)\n'
+ 'object.__invert__(self)\n'
+ '\n'
+ ' Called to implement the unary arithmetic operations '
+ '("-", "+",\n'
+ ' "abs()" and "~").\n'
+ '\n'
+ 'object.__complex__(self)\n'
+ 'object.__int__(self)\n'
+ 'object.__float__(self)\n'
+ 'object.__round__(self[, n])\n'
+ '\n'
+ ' Called to implement the built-in functions "complex()", '
+ '"int()",\n'
+ ' "float()" and "round()". Should return a value of the '
+ 'appropriate\n'
+ ' type.\n'
+ '\n'
+ 'object.__index__(self)\n'
+ '\n'
+ ' Called to implement "operator.index()", and whenever '
+ 'Python needs\n'
+ ' to losslessly convert the numeric object to an integer '
+ 'object (such\n'
+ ' as in slicing, or in the built-in "bin()", "hex()" and '
+ '"oct()"\n'
+ ' functions). Presence of this method indicates that the '
+ 'numeric\n'
+ ' object is an integer type. Must return an integer.\n'
+ '\n'
+ ' Note: In order to have a coherent integer type class, '
+ 'when\n'
+ ' "__index__()" is defined "__int__()" should also be '
+ 'defined, and\n'
+ ' both should return the same value.\n',
+ 'objects': '\n'
+ 'Objects, values and types\n'
+ '*************************\n'
+ '\n'
+ "*Objects* are Python's abstraction for data. All data in a "
+ 'Python\n'
+ 'program is represented by objects or by relations between '
+ 'objects. (In\n'
+ 'a sense, and in conformance to Von Neumann\'s model of a "stored\n'
+ 'program computer," code is also represented by objects.)\n'
+ '\n'
+ "Every object has an identity, a type and a value. An object's\n"
+ '*identity* never changes once it has been created; you may think '
+ 'of it\n'
+ 'as the object\'s address in memory. The \'"is"\' operator '
+ 'compares the\n'
+ 'identity of two objects; the "id()" function returns an integer\n'
+ 'representing its identity.\n'
+ '\n'
+ '**CPython implementation detail:** For CPython, "id(x)" is the '
+ 'memory\n'
+ 'address where "x" is stored.\n'
+ '\n'
+ "An object's type determines the operations that the object "
+ 'supports\n'
+ '(e.g., "does it have a length?") and also defines the possible '
+ 'values\n'
+ 'for objects of that type. The "type()" function returns an '
+ "object's\n"
+ 'type (which is an object itself). Like its identity, an '
+ "object's\n"
+ '*type* is also unchangeable. [1]\n'
+ '\n'
+ 'The *value* of some objects can change. Objects whose value can\n'
+ 'change are said to be *mutable*; objects whose value is '
+ 'unchangeable\n'
+ 'once they are created are called *immutable*. (The value of an\n'
+ 'immutable container object that contains a reference to a '
+ 'mutable\n'
+ "object can change when the latter's value is changed; however "
+ 'the\n'
+ 'container is still considered immutable, because the collection '
+ 'of\n'
+ 'objects it contains cannot be changed. So, immutability is not\n'
+ 'strictly the same as having an unchangeable value, it is more '
+ 'subtle.)\n'
+ "An object's mutability is determined by its type; for instance,\n"
+ 'numbers, strings and tuples are immutable, while dictionaries '
+ 'and\n'
+ 'lists are mutable.\n'
+ '\n'
+ 'Objects are never explicitly destroyed; however, when they '
+ 'become\n'
+ 'unreachable they may be garbage-collected. An implementation is\n'
+ 'allowed to postpone garbage collection or omit it altogether --- '
+ 'it is\n'
+ 'a matter of implementation quality how garbage collection is\n'
+ 'implemented, as long as no objects are collected that are still\n'
+ 'reachable.\n'
+ '\n'
+ '**CPython implementation detail:** CPython currently uses a '
+ 'reference-\n'
+ 'counting scheme with (optional) delayed detection of cyclically '
+ 'linked\n'
+ 'garbage, which collects most objects as soon as they become\n'
+ 'unreachable, but is not guaranteed to collect garbage containing\n'
+ 'circular references. See the documentation of the "gc" module '
+ 'for\n'
+ 'information on controlling the collection of cyclic garbage. '
+ 'Other\n'
+ 'implementations act differently and CPython may change. Do not '
+ 'depend\n'
+ 'on immediate finalization of objects when they become unreachable '
+ '(so\n'
+ 'you should always close files explicitly).\n'
+ '\n'
+ "Note that the use of the implementation's tracing or debugging\n"
+ 'facilities may keep objects alive that would normally be '
+ 'collectable.\n'
+ 'Also note that catching an exception with a \'"try"..."except"\'\n'
+ 'statement may keep objects alive.\n'
+ '\n'
+ 'Some objects contain references to "external" resources such as '
+ 'open\n'
+ 'files or windows. It is understood that these resources are '
+ 'freed\n'
+ 'when the object is garbage-collected, but since garbage '
+ 'collection is\n'
+ 'not guaranteed to happen, such objects also provide an explicit '
+ 'way to\n'
+ 'release the external resource, usually a "close()" method. '
+ 'Programs\n'
+ 'are strongly recommended to explicitly close such objects. The\n'
+ '\'"try"..."finally"\' statement and the \'"with"\' statement '
+ 'provide\n'
+ 'convenient ways to do this.\n'
+ '\n'
+ 'Some objects contain references to other objects; these are '
+ 'called\n'
+ '*containers*. Examples of containers are tuples, lists and\n'
+ "dictionaries. The references are part of a container's value. "
+ 'In\n'
+ 'most cases, when we talk about the value of a container, we imply '
+ 'the\n'
+ 'values, not the identities of the contained objects; however, '
+ 'when we\n'
+ 'talk about the mutability of a container, only the identities of '
+ 'the\n'
+ 'immediately contained objects are implied. So, if an immutable\n'
+ 'container (like a tuple) contains a reference to a mutable '
+ 'object, its\n'
+ 'value changes if that mutable object is changed.\n'
+ '\n'
+ 'Types affect almost all aspects of object behavior. Even the\n'
+ 'importance of object identity is affected in some sense: for '
+ 'immutable\n'
+ 'types, operations that compute new values may actually return a\n'
+ 'reference to any existing object with the same type and value, '
+ 'while\n'
+ 'for mutable objects this is not allowed. E.g., after "a = 1; b = '
+ '1",\n'
+ '"a" and "b" may or may not refer to the same object with the '
+ 'value\n'
+ 'one, depending on the implementation, but after "c = []; d = []", '
+ '"c"\n'
+ 'and "d" are guaranteed to refer to two different, unique, newly\n'
+ 'created empty lists. (Note that "c = d = []" assigns the same '
+ 'object\n'
+ 'to both "c" and "d".)\n',
+ 'operator-summary': '\n'
+ 'Operator precedence\n'
+ '*******************\n'
+ '\n'
+ 'The following table summarizes the operator precedence '
+ 'in Python, from\n'
+ 'lowest precedence (least binding) to highest precedence '
+ '(most\n'
+ 'binding). Operators in the same box have the same '
+ 'precedence. Unless\n'
+ 'the syntax is explicitly given, operators are binary. '
+ 'Operators in\n'
+ 'the same box group left to right (except for '
+ 'exponentiation, which\n'
+ 'groups from right to left).\n'
+ '\n'
+ 'Note that comparisons, membership tests, and identity '
+ 'tests, all have\n'
+ 'the same precedence and have a left-to-right chaining '
+ 'feature as\n'
+ 'described in the Comparisons section.\n'
+ '\n'
+ '+-------------------------------------------------+---------------------------------------+\n'
+ '| Operator | '
+ 'Description |\n'
+ '+=================================================+=======================================+\n'
+ '| "lambda" | '
+ 'Lambda expression |\n'
+ '+-------------------------------------------------+---------------------------------------+\n'
+ '| "if" -- "else" | '
+ 'Conditional expression |\n'
+ '+-------------------------------------------------+---------------------------------------+\n'
+ '| "or" | '
+ 'Boolean OR |\n'
+ '+-------------------------------------------------+---------------------------------------+\n'
+ '| "and" | '
+ 'Boolean AND |\n'
+ '+-------------------------------------------------+---------------------------------------+\n'
+ '| "not" "x" | '
+ 'Boolean NOT |\n'
+ '+-------------------------------------------------+---------------------------------------+\n'
+ '| "in", "not in", "is", "is not", "<", "<=", ">", | '
+ 'Comparisons, including membership |\n'
+ '| ">=", "!=", "==" | '
+ 'tests and identity tests |\n'
+ '+-------------------------------------------------+---------------------------------------+\n'
+ '| "|" | '
+ 'Bitwise OR |\n'
+ '+-------------------------------------------------+---------------------------------------+\n'
+ '| "^" | '
+ 'Bitwise XOR |\n'
+ '+-------------------------------------------------+---------------------------------------+\n'
+ '| "&" | '
+ 'Bitwise AND |\n'
+ '+-------------------------------------------------+---------------------------------------+\n'
+ '| "<<", ">>" | '
+ 'Shifts |\n'
+ '+-------------------------------------------------+---------------------------------------+\n'
+ '| "+", "-" | '
+ 'Addition and subtraction |\n'
+ '+-------------------------------------------------+---------------------------------------+\n'
+ '| "*", "@", "/", "//", "%" | '
+ 'Multiplication, matrix multiplication |\n'
+ '| | '
+ 'division, remainder [5] |\n'
+ '+-------------------------------------------------+---------------------------------------+\n'
+ '| "+x", "-x", "~x" | '
+ 'Positive, negative, bitwise NOT |\n'
+ '+-------------------------------------------------+---------------------------------------+\n'
+ '| "**" | '
+ 'Exponentiation [6] |\n'
+ '+-------------------------------------------------+---------------------------------------+\n'
+ '| "await" "x" | '
+ 'Await expression |\n'
+ '+-------------------------------------------------+---------------------------------------+\n'
+ '| "x[index]", "x[index:index]", | '
+ 'Subscription, slicing, call, |\n'
+ '| "x(arguments...)", "x.attribute" | '
+ 'attribute reference |\n'
+ '+-------------------------------------------------+---------------------------------------+\n'
+ '| "(expressions...)", "[expressions...]", "{key: | '
+ 'Binding or tuple display, list |\n'
+ '| value...}", "{expressions...}" | '
+ 'display, dictionary display, set |\n'
+ '| | '
+ 'display |\n'
+ '+-------------------------------------------------+---------------------------------------+\n'
+ '\n'
+ '-[ Footnotes ]-\n'
+ '\n'
+ '[1] While "abs(x%y) < abs(y)" is true mathematically, '
+ 'for floats\n'
+ ' it may not be true numerically due to roundoff. For '
+ 'example, and\n'
+ ' assuming a platform on which a Python float is an '
+ 'IEEE 754 double-\n'
+ ' precision number, in order that "-1e-100 % 1e100" '
+ 'have the same\n'
+ ' sign as "1e100", the computed result is "-1e-100 + '
+ '1e100", which\n'
+ ' is numerically exactly equal to "1e100". The '
+ 'function\n'
+ ' "math.fmod()" returns a result whose sign matches '
+ 'the sign of the\n'
+ ' first argument instead, and so returns "-1e-100" in '
+ 'this case.\n'
+ ' Which approach is more appropriate depends on the '
+ 'application.\n'
+ '\n'
+ '[2] If x is very close to an exact integer multiple of '
+ "y, it's\n"
+ ' possible for "x//y" to be one larger than '
+ '"(x-x%y)//y" due to\n'
+ ' rounding. In such cases, Python returns the latter '
+ 'result, in\n'
+ ' order to preserve that "divmod(x,y)[0] * y + x % y" '
+ 'be very close\n'
+ ' to "x".\n'
+ '\n'
+ '[3] The Unicode standard distinguishes between *code '
+ 'points* (e.g.\n'
+ ' U+0041) and *abstract characters* (e.g. "LATIN '
+ 'CAPITAL LETTER A").\n'
+ ' While most abstract characters in Unicode are only '
+ 'represented\n'
+ ' using one code point, there is a number of abstract '
+ 'characters\n'
+ ' that can in addition be represented using a sequence '
+ 'of more than\n'
+ ' one code point. For example, the abstract character '
+ '"LATIN\n'
+ ' CAPITAL LETTER C WITH CEDILLA" can be represented as '
+ 'a single\n'
+ ' *precomposed character* at code position U+00C7, or '
+ 'as a sequence\n'
+ ' of a *base character* at code position U+0043 (LATIN '
+ 'CAPITAL\n'
+ ' LETTER C), followed by a *combining character* at '
+ 'code position\n'
+ ' U+0327 (COMBINING CEDILLA).\n'
+ '\n'
+ ' The comparison operators on strings compare at the '
+ 'level of\n'
+ ' Unicode code points. This may be counter-intuitive '
+ 'to humans. For\n'
+ ' example, ""\\u00C7" == "\\u0043\\u0327"" is "False", '
+ 'even though both\n'
+ ' strings represent the same abstract character "LATIN '
+ 'CAPITAL\n'
+ ' LETTER C WITH CEDILLA".\n'
+ '\n'
+ ' To compare strings at the level of abstract '
+ 'characters (that is,\n'
+ ' in a way intuitive to humans), use '
+ '"unicodedata.normalize()".\n'
+ '\n'
+ '[4] Due to automatic garbage-collection, free lists, and '
+ 'the\n'
+ ' dynamic nature of descriptors, you may notice '
+ 'seemingly unusual\n'
+ ' behaviour in certain uses of the "is" operator, like '
+ 'those\n'
+ ' involving comparisons between instance methods, or '
+ 'constants.\n'
+ ' Check their documentation for more info.\n'
+ '\n'
+ '[5] The "%" operator is also used for string formatting; '
+ 'the same\n'
+ ' precedence applies.\n'
+ '\n'
+ '[6] The power operator "**" binds less tightly than an '
+ 'arithmetic\n'
+ ' or bitwise unary operator on its right, that is, '
+ '"2**-1" is "0.5".\n',
+ 'pass': '\n'
+ 'The "pass" statement\n'
+ '********************\n'
+ '\n'
+ ' pass_stmt ::= "pass"\n'
+ '\n'
+ '"pass" is a null operation --- when it is executed, nothing '
+ 'happens.\n'
+ 'It is useful as a placeholder when a statement is required\n'
+ 'syntactically, but no code needs to be executed, for example:\n'
+ '\n'
+ ' def f(arg): pass # a function that does nothing (yet)\n'
+ '\n'
+ ' class C: pass # a class with no methods (yet)\n',
+ 'power': '\n'
+ 'The power operator\n'
+ '******************\n'
+ '\n'
+ 'The power operator binds more tightly than unary operators on its\n'
+ 'left; it binds less tightly than unary operators on its right. '
+ 'The\n'
+ 'syntax is:\n'
+ '\n'
+ ' power ::= ( await_expr | primary ) ["**" u_expr]\n'
+ '\n'
+ 'Thus, in an unparenthesized sequence of power and unary operators, '
+ 'the\n'
+ 'operators are evaluated from right to left (this does not '
+ 'constrain\n'
+ 'the evaluation order for the operands): "-1**2" results in "-1".\n'
+ '\n'
+ 'The power operator has the same semantics as the built-in "pow()"\n'
+ 'function, when called with two arguments: it yields its left '
+ 'argument\n'
+ 'raised to the power of its right argument. The numeric arguments '
+ 'are\n'
+ 'first converted to a common type, and the result is of that type.\n'
+ '\n'
+ 'For int operands, the result has the same type as the operands '
+ 'unless\n'
+ 'the second argument is negative; in that case, all arguments are\n'
+ 'converted to float and a float result is delivered. For example,\n'
+ '"10**2" returns "100", but "10**-2" returns "0.01".\n'
+ '\n'
+ 'Raising "0.0" to a negative power results in a '
+ '"ZeroDivisionError".\n'
+ 'Raising a negative number to a fractional power results in a '
+ '"complex"\n'
+ 'number. (In earlier versions it raised a "ValueError".)\n',
+ 'raise': '\n'
+ 'The "raise" statement\n'
+ '*********************\n'
+ '\n'
+ ' raise_stmt ::= "raise" [expression ["from" expression]]\n'
+ '\n'
+ 'If no expressions are present, "raise" re-raises the last '
+ 'exception\n'
+ 'that was active in the current scope. If no exception is active '
+ 'in\n'
+ 'the current scope, a "RuntimeError" exception is raised indicating\n'
+ 'that this is an error.\n'
+ '\n'
+ 'Otherwise, "raise" evaluates the first expression as the exception\n'
+ 'object. It must be either a subclass or an instance of\n'
+ '"BaseException". If it is a class, the exception instance will be\n'
+ 'obtained when needed by instantiating the class with no arguments.\n'
+ '\n'
+ "The *type* of the exception is the exception instance's class, the\n"
+ '*value* is the instance itself.\n'
+ '\n'
+ 'A traceback object is normally created automatically when an '
+ 'exception\n'
+ 'is raised and attached to it as the "__traceback__" attribute, '
+ 'which\n'
+ 'is writable. You can create an exception and set your own traceback '
+ 'in\n'
+ 'one step using the "with_traceback()" exception method (which '
+ 'returns\n'
+ 'the same exception instance, with its traceback set to its '
+ 'argument),\n'
+ 'like so:\n'
+ '\n'
+ ' raise Exception("foo occurred").with_traceback(tracebackobj)\n'
+ '\n'
+ 'The "from" clause is used for exception chaining: if given, the '
+ 'second\n'
+ '*expression* must be another exception class or instance, which '
+ 'will\n'
+ 'then be attached to the raised exception as the "__cause__" '
+ 'attribute\n'
+ '(which is writable). If the raised exception is not handled, both\n'
+ 'exceptions will be printed:\n'
+ '\n'
+ ' >>> try:\n'
+ ' ... print(1 / 0)\n'
+ ' ... except Exception as exc:\n'
+ ' ... raise RuntimeError("Something bad happened") from exc\n'
+ ' ...\n'
+ ' Traceback (most recent call last):\n'
+ ' File "<stdin>", line 2, in <module>\n'
+ ' ZeroDivisionError: int division or modulo by zero\n'
+ '\n'
+ ' The above exception was the direct cause of the following '
+ 'exception:\n'
+ '\n'
+ ' Traceback (most recent call last):\n'
+ ' File "<stdin>", line 4, in <module>\n'
+ ' RuntimeError: Something bad happened\n'
+ '\n'
+ 'A similar mechanism works implicitly if an exception is raised '
+ 'inside\n'
+ 'an exception handler or a "finally" clause: the previous exception '
+ 'is\n'
+ 'then attached as the new exception\'s "__context__" attribute:\n'
+ '\n'
+ ' >>> try:\n'
+ ' ... print(1 / 0)\n'
+ ' ... except:\n'
+ ' ... raise RuntimeError("Something bad happened")\n'
+ ' ...\n'
+ ' Traceback (most recent call last):\n'
+ ' File "<stdin>", line 2, in <module>\n'
+ ' ZeroDivisionError: int division or modulo by zero\n'
+ '\n'
+ ' During handling of the above exception, another exception '
+ 'occurred:\n'
+ '\n'
+ ' Traceback (most recent call last):\n'
+ ' File "<stdin>", line 4, in <module>\n'
+ ' RuntimeError: Something bad happened\n'
+ '\n'
+ 'Additional information on exceptions can be found in section\n'
+ 'Exceptions, and information about handling exceptions is in '
+ 'section\n'
+ 'The try statement.\n',
+ 'return': '\n'
+ 'The "return" statement\n'
+ '**********************\n'
+ '\n'
+ ' return_stmt ::= "return" [expression_list]\n'
+ '\n'
+ '"return" may only occur syntactically nested in a function '
+ 'definition,\n'
+ 'not within a nested class definition.\n'
+ '\n'
+ 'If an expression list is present, it is evaluated, else "None" is\n'
+ 'substituted.\n'
+ '\n'
+ '"return" leaves the current function call with the expression list '
+ '(or\n'
+ '"None") as return value.\n'
+ '\n'
+ 'When "return" passes control out of a "try" statement with a '
+ '"finally"\n'
+ 'clause, that "finally" clause is executed before really leaving '
+ 'the\n'
+ 'function.\n'
+ '\n'
+ 'In a generator function, the "return" statement indicates that '
+ 'the\n'
+ 'generator is done and will cause "StopIteration" to be raised. '
+ 'The\n'
+ 'returned value (if any) is used as an argument to construct\n'
+ '"StopIteration" and becomes the "StopIteration.value" attribute.\n',
+ 'sequence-types': '\n'
+ 'Emulating container types\n'
+ '*************************\n'
+ '\n'
+ 'The following methods can be defined to implement '
+ 'container objects.\n'
+ 'Containers usually are sequences (such as lists or tuples) '
+ 'or mappings\n'
+ '(like dictionaries), but can represent other containers as '
+ 'well. The\n'
+ 'first set of methods is used either to emulate a sequence '
+ 'or to\n'
+ 'emulate a mapping; the difference is that for a sequence, '
+ 'the\n'
+ 'allowable keys should be the integers *k* for which "0 <= '
+ 'k < N" where\n'
+ '*N* is the length of the sequence, or slice objects, which '
+ 'define a\n'
+ 'range of items. It is also recommended that mappings '
+ 'provide the\n'
+ 'methods "keys()", "values()", "items()", "get()", '
+ '"clear()",\n'
+ '"setdefault()", "pop()", "popitem()", "copy()", and '
+ '"update()"\n'
+ "behaving similar to those for Python's standard dictionary "
+ 'objects.\n'
+ 'The "collections" module provides a "MutableMapping" '
+ 'abstract base\n'
+ 'class to help create those methods from a base set of '
+ '"__getitem__()",\n'
+ '"__setitem__()", "__delitem__()", and "keys()". Mutable '
+ 'sequences\n'
+ 'should provide methods "append()", "count()", "index()", '
+ '"extend()",\n'
+ '"insert()", "pop()", "remove()", "reverse()" and "sort()", '
+ 'like Python\n'
+ 'standard list objects. Finally, sequence types should '
+ 'implement\n'
+ 'addition (meaning concatenation) and multiplication '
+ '(meaning\n'
+ 'repetition) by defining the methods "__add__()", '
+ '"__radd__()",\n'
+ '"__iadd__()", "__mul__()", "__rmul__()" and "__imul__()" '
+ 'described\n'
+ 'below; they should not define other numerical operators. '
+ 'It is\n'
+ 'recommended that both mappings and sequences implement '
+ 'the\n'
+ '"__contains__()" method to allow efficient use of the "in" '
+ 'operator;\n'
+ 'for mappings, "in" should search the mapping\'s keys; for '
+ 'sequences, it\n'
+ 'should search through the values. It is further '
+ 'recommended that both\n'
+ 'mappings and sequences implement the "__iter__()" method '
+ 'to allow\n'
+ 'efficient iteration through the container; for mappings, '
+ '"__iter__()"\n'
+ 'should be the same as "keys()"; for sequences, it should '
+ 'iterate\n'
+ 'through the values.\n'
+ '\n'
+ 'object.__len__(self)\n'
+ '\n'
+ ' Called to implement the built-in function "len()". '
+ 'Should return\n'
+ ' the length of the object, an integer ">=" 0. Also, an '
+ 'object that\n'
+ ' doesn\'t define a "__bool__()" method and whose '
+ '"__len__()" method\n'
+ ' returns zero is considered to be false in a Boolean '
+ 'context.\n'
+ '\n'
+ 'object.__length_hint__(self)\n'
+ '\n'
+ ' Called to implement "operator.length_hint()". Should '
+ 'return an\n'
+ ' estimated length for the object (which may be greater '
+ 'or less than\n'
+ ' the actual length). The length must be an integer ">=" '
+ '0. This\n'
+ ' method is purely an optimization and is never required '
+ 'for\n'
+ ' correctness.\n'
+ '\n'
+ ' New in version 3.4.\n'
+ '\n'
+ 'Note: Slicing is done exclusively with the following three '
+ 'methods.\n'
+ ' A call like\n'
+ '\n'
+ ' a[1:2] = b\n'
+ '\n'
+ ' is translated to\n'
+ '\n'
+ ' a[slice(1, 2, None)] = b\n'
+ '\n'
+ ' and so forth. Missing slice items are always filled in '
+ 'with "None".\n'
+ '\n'
+ 'object.__getitem__(self, key)\n'
+ '\n'
+ ' Called to implement evaluation of "self[key]". For '
+ 'sequence types,\n'
+ ' the accepted keys should be integers and slice '
+ 'objects. Note that\n'
+ ' the special interpretation of negative indexes (if the '
+ 'class wishes\n'
+ ' to emulate a sequence type) is up to the '
+ '"__getitem__()" method. If\n'
+ ' *key* is of an inappropriate type, "TypeError" may be '
+ 'raised; if of\n'
+ ' a value outside the set of indexes for the sequence '
+ '(after any\n'
+ ' special interpretation of negative values), '
+ '"IndexError" should be\n'
+ ' raised. For mapping types, if *key* is missing (not in '
+ 'the\n'
+ ' container), "KeyError" should be raised.\n'
+ '\n'
+ ' Note: "for" loops expect that an "IndexError" will be '
+ 'raised for\n'
+ ' illegal indexes to allow proper detection of the end '
+ 'of the\n'
+ ' sequence.\n'
+ '\n'
+ 'object.__missing__(self, key)\n'
+ '\n'
+ ' Called by "dict"."__getitem__()" to implement '
+ '"self[key]" for dict\n'
+ ' subclasses when key is not in the dictionary.\n'
+ '\n'
+ 'object.__setitem__(self, key, value)\n'
+ '\n'
+ ' Called to implement assignment to "self[key]". Same '
+ 'note as for\n'
+ ' "__getitem__()". This should only be implemented for '
+ 'mappings if\n'
+ ' the objects support changes to the values for keys, or '
+ 'if new keys\n'
+ ' can be added, or for sequences if elements can be '
+ 'replaced. The\n'
+ ' same exceptions should be raised for improper *key* '
+ 'values as for\n'
+ ' the "__getitem__()" method.\n'
+ '\n'
+ 'object.__delitem__(self, key)\n'
+ '\n'
+ ' Called to implement deletion of "self[key]". Same note '
+ 'as for\n'
+ ' "__getitem__()". This should only be implemented for '
+ 'mappings if\n'
+ ' the objects support removal of keys, or for sequences '
+ 'if elements\n'
+ ' can be removed from the sequence. The same exceptions '
+ 'should be\n'
+ ' raised for improper *key* values as for the '
+ '"__getitem__()" method.\n'
+ '\n'
+ 'object.__iter__(self)\n'
+ '\n'
+ ' This method is called when an iterator is required for '
+ 'a container.\n'
+ ' This method should return a new iterator object that '
+ 'can iterate\n'
+ ' over all the objects in the container. For mappings, '
+ 'it should\n'
+ ' iterate over the keys of the container.\n'
+ '\n'
+ ' Iterator objects also need to implement this method; '
+ 'they are\n'
+ ' required to return themselves. For more information on '
+ 'iterator\n'
+ ' objects, see Iterator Types.\n'
+ '\n'
+ 'object.__reversed__(self)\n'
+ '\n'
+ ' Called (if present) by the "reversed()" built-in to '
+ 'implement\n'
+ ' reverse iteration. It should return a new iterator '
+ 'object that\n'
+ ' iterates over all the objects in the container in '
+ 'reverse order.\n'
+ '\n'
+ ' If the "__reversed__()" method is not provided, the '
+ '"reversed()"\n'
+ ' built-in will fall back to using the sequence protocol '
+ '("__len__()"\n'
+ ' and "__getitem__()"). Objects that support the '
+ 'sequence protocol\n'
+ ' should only provide "__reversed__()" if they can '
+ 'provide an\n'
+ ' implementation that is more efficient than the one '
+ 'provided by\n'
+ ' "reversed()".\n'
+ '\n'
+ 'The membership test operators ("in" and "not in") are '
+ 'normally\n'
+ 'implemented as an iteration through a sequence. However, '
+ 'container\n'
+ 'objects can supply the following special method with a '
+ 'more efficient\n'
+ 'implementation, which also does not require the object be '
+ 'a sequence.\n'
+ '\n'
+ 'object.__contains__(self, item)\n'
+ '\n'
+ ' Called to implement membership test operators. Should '
+ 'return true\n'
+ ' if *item* is in *self*, false otherwise. For mapping '
+ 'objects, this\n'
+ ' should consider the keys of the mapping rather than the '
+ 'values or\n'
+ ' the key-item pairs.\n'
+ '\n'
+ ' For objects that don\'t define "__contains__()", the '
+ 'membership test\n'
+ ' first tries iteration via "__iter__()", then the old '
+ 'sequence\n'
+ ' iteration protocol via "__getitem__()", see this '
+ 'section in the\n'
+ ' language reference.\n',
+ 'shifting': '\n'
+ 'Shifting operations\n'
+ '*******************\n'
+ '\n'
+ 'The shifting operations have lower priority than the arithmetic\n'
+ 'operations:\n'
+ '\n'
+ ' shift_expr ::= a_expr | shift_expr ( "<<" | ">>" ) a_expr\n'
+ '\n'
+ 'These operators accept integers as arguments. They shift the '
+ 'first\n'
+ 'argument to the left or right by the number of bits given by '
+ 'the\n'
+ 'second argument.\n'
+ '\n'
+ 'A right shift by *n* bits is defined as floor division by '
+ '"pow(2,n)".\n'
+ 'A left shift by *n* bits is defined as multiplication with '
+ '"pow(2,n)".\n'
+ '\n'
+ 'Note: In the current implementation, the right-hand operand is\n'
+ ' required to be at most "sys.maxsize". If the right-hand '
+ 'operand is\n'
+ ' larger than "sys.maxsize" an "OverflowError" exception is '
+ 'raised.\n',
+ 'slicings': '\n'
+ 'Slicings\n'
+ '********\n'
+ '\n'
+ 'A slicing selects a range of items in a sequence object (e.g., '
+ 'a\n'
+ 'string, tuple or list). Slicings may be used as expressions or '
+ 'as\n'
+ 'targets in assignment or "del" statements. The syntax for a '
+ 'slicing:\n'
+ '\n'
+ ' slicing ::= primary "[" slice_list "]"\n'
+ ' slice_list ::= slice_item ("," slice_item)* [","]\n'
+ ' slice_item ::= expression | proper_slice\n'
+ ' proper_slice ::= [lower_bound] ":" [upper_bound] [ ":" '
+ '[stride] ]\n'
+ ' lower_bound ::= expression\n'
+ ' upper_bound ::= expression\n'
+ ' stride ::= expression\n'
+ '\n'
+ 'There is ambiguity in the formal syntax here: anything that '
+ 'looks like\n'
+ 'an expression list also looks like a slice list, so any '
+ 'subscription\n'
+ 'can be interpreted as a slicing. Rather than further '
+ 'complicating the\n'
+ 'syntax, this is disambiguated by defining that in this case the\n'
+ 'interpretation as a subscription takes priority over the\n'
+ 'interpretation as a slicing (this is the case if the slice list\n'
+ 'contains no proper slice).\n'
+ '\n'
+ 'The semantics for a slicing are as follows. The primary is '
+ 'indexed\n'
+ '(using the same "__getitem__()" method as normal subscription) '
+ 'with a\n'
+ 'key that is constructed from the slice list, as follows. If the '
+ 'slice\n'
+ 'list contains at least one comma, the key is a tuple containing '
+ 'the\n'
+ 'conversion of the slice items; otherwise, the conversion of the '
+ 'lone\n'
+ 'slice item is the key. The conversion of a slice item that is '
+ 'an\n'
+ 'expression is that expression. The conversion of a proper slice '
+ 'is a\n'
+ 'slice object (see section The standard type hierarchy) whose '
+ '"start",\n'
+ '"stop" and "step" attributes are the values of the expressions '
+ 'given\n'
+ 'as lower bound, upper bound and stride, respectively, '
+ 'substituting\n'
+ '"None" for missing expressions.\n',
+ 'specialattrs': '\n'
+ 'Special Attributes\n'
+ '******************\n'
+ '\n'
+ 'The implementation adds a few special read-only attributes '
+ 'to several\n'
+ 'object types, where they are relevant. Some of these are '
+ 'not reported\n'
+ 'by the "dir()" built-in function.\n'
+ '\n'
+ 'object.__dict__\n'
+ '\n'
+ ' A dictionary or other mapping object used to store an '
+ "object's\n"
+ ' (writable) attributes.\n'
+ '\n'
+ 'instance.__class__\n'
+ '\n'
+ ' The class to which a class instance belongs.\n'
+ '\n'
+ 'class.__bases__\n'
+ '\n'
+ ' The tuple of base classes of a class object.\n'
+ '\n'
+ 'definition.__name__\n'
+ '\n'
+ ' The name of the class, function, method, descriptor, or '
+ 'generator\n'
+ ' instance.\n'
+ '\n'
+ 'definition.__qualname__\n'
+ '\n'
+ ' The *qualified name* of the class, function, method, '
+ 'descriptor, or\n'
+ ' generator instance.\n'
+ '\n'
+ ' New in version 3.3.\n'
+ '\n'
+ 'class.__mro__\n'
+ '\n'
+ ' This attribute is a tuple of classes that are considered '
+ 'when\n'
+ ' looking for base classes during method resolution.\n'
+ '\n'
+ 'class.mro()\n'
+ '\n'
+ ' This method can be overridden by a metaclass to customize '
+ 'the\n'
+ ' method resolution order for its instances. It is called '
+ 'at class\n'
+ ' instantiation, and its result is stored in "__mro__".\n'
+ '\n'
+ 'class.__subclasses__()\n'
+ '\n'
+ ' Each class keeps a list of weak references to its '
+ 'immediate\n'
+ ' subclasses. This method returns a list of all those '
+ 'references\n'
+ ' still alive. Example:\n'
+ '\n'
+ ' >>> int.__subclasses__()\n'
+ " [<class 'bool'>]\n"
+ '\n'
+ '-[ Footnotes ]-\n'
+ '\n'
+ '[1] Additional information on these special methods may be '
+ 'found\n'
+ ' in the Python Reference Manual (Basic customization).\n'
+ '\n'
+ '[2] As a consequence, the list "[1, 2]" is considered equal '
+ 'to\n'
+ ' "[1.0, 2.0]", and similarly for tuples.\n'
+ '\n'
+ "[3] They must have since the parser can't tell the type of "
+ 'the\n'
+ ' operands.\n'
+ '\n'
+ '[4] Cased characters are those with general category '
+ 'property\n'
+ ' being one of "Lu" (Letter, uppercase), "Ll" (Letter, '
+ 'lowercase),\n'
+ ' or "Lt" (Letter, titlecase).\n'
+ '\n'
+ '[5] To format only a tuple you should therefore provide a\n'
+ ' singleton tuple whose only element is the tuple to be '
+ 'formatted.\n',
+ 'specialnames': '\n'
+ 'Special method names\n'
+ '********************\n'
+ '\n'
+ 'A class can implement certain operations that are invoked by '
+ 'special\n'
+ 'syntax (such as arithmetic operations or subscripting and '
+ 'slicing) by\n'
+ "defining methods with special names. This is Python's "
+ 'approach to\n'
+ '*operator overloading*, allowing classes to define their own '
+ 'behavior\n'
+ 'with respect to language operators. For instance, if a '
+ 'class defines\n'
+ 'a method named "__getitem__()", and "x" is an instance of '
+ 'this class,\n'
+ 'then "x[i]" is roughly equivalent to "type(x).__getitem__(x, '
+ 'i)".\n'
+ 'Except where mentioned, attempts to execute an operation '
+ 'raise an\n'
+ 'exception when no appropriate method is defined (typically\n'
+ '"AttributeError" or "TypeError").\n'
+ '\n'
+ 'Setting a special method to "None" indicates that the '
+ 'corresponding\n'
+ 'operation is not available. For example, if a class sets '
+ '"__iter__()"\n'
+ 'to "None", the class is not iterable, so calling "iter()" on '
+ 'its\n'
+ 'instances will raise a "TypeError" (without falling back to\n'
+ '"__getitem__()"). [2]\n'
+ '\n'
+ 'When implementing a class that emulates any built-in type, '
+ 'it is\n'
+ 'important that the emulation only be implemented to the '
+ 'degree that it\n'
+ 'makes sense for the object being modelled. For example, '
+ 'some\n'
+ 'sequences may work well with retrieval of individual '
+ 'elements, but\n'
+ 'extracting a slice may not make sense. (One example of this '
+ 'is the\n'
+ '"NodeList" interface in the W3C\'s Document Object Model.)\n'
+ '\n'
+ '\n'
+ 'Basic customization\n'
+ '===================\n'
+ '\n'
+ 'object.__new__(cls[, ...])\n'
+ '\n'
+ ' Called to create a new instance of class *cls*. '
+ '"__new__()" is a\n'
+ ' static method (special-cased so you need not declare it '
+ 'as such)\n'
+ ' that takes the class of which an instance was requested '
+ 'as its\n'
+ ' first argument. The remaining arguments are those passed '
+ 'to the\n'
+ ' object constructor expression (the call to the class). '
+ 'The return\n'
+ ' value of "__new__()" should be the new object instance '
+ '(usually an\n'
+ ' instance of *cls*).\n'
+ '\n'
+ ' Typical implementations create a new instance of the '
+ 'class by\n'
+ ' invoking the superclass\'s "__new__()" method using\n'
+ ' "super(currentclass, cls).__new__(cls[, ...])" with '
+ 'appropriate\n'
+ ' arguments and then modifying the newly-created instance '
+ 'as\n'
+ ' necessary before returning it.\n'
+ '\n'
+ ' If "__new__()" returns an instance of *cls*, then the '
+ 'new\n'
+ ' instance\'s "__init__()" method will be invoked like\n'
+ ' "__init__(self[, ...])", where *self* is the new instance '
+ 'and the\n'
+ ' remaining arguments are the same as were passed to '
+ '"__new__()".\n'
+ '\n'
+ ' If "__new__()" does not return an instance of *cls*, then '
+ 'the new\n'
+ ' instance\'s "__init__()" method will not be invoked.\n'
+ '\n'
+ ' "__new__()" is intended mainly to allow subclasses of '
+ 'immutable\n'
+ ' types (like int, str, or tuple) to customize instance '
+ 'creation. It\n'
+ ' is also commonly overridden in custom metaclasses in '
+ 'order to\n'
+ ' customize class creation.\n'
+ '\n'
+ 'object.__init__(self[, ...])\n'
+ '\n'
+ ' Called after the instance has been created (by '
+ '"__new__()"), but\n'
+ ' before it is returned to the caller. The arguments are '
+ 'those\n'
+ ' passed to the class constructor expression. If a base '
+ 'class has an\n'
+ ' "__init__()" method, the derived class\'s "__init__()" '
+ 'method, if\n'
+ ' any, must explicitly call it to ensure proper '
+ 'initialization of the\n'
+ ' base class part of the instance; for example:\n'
+ ' "BaseClass.__init__(self, [args...])".\n'
+ '\n'
+ ' Because "__new__()" and "__init__()" work together in '
+ 'constructing\n'
+ ' objects ("__new__()" to create it, and "__init__()" to '
+ 'customize\n'
+ ' it), no non-"None" value may be returned by "__init__()"; '
+ 'doing so\n'
+ ' will cause a "TypeError" to be raised at runtime.\n'
+ '\n'
+ 'object.__del__(self)\n'
+ '\n'
+ ' Called when the instance is about to be destroyed. This '
+ 'is also\n'
+ ' called a destructor. If a base class has a "__del__()" '
+ 'method, the\n'
+ ' derived class\'s "__del__()" method, if any, must '
+ 'explicitly call it\n'
+ ' to ensure proper deletion of the base class part of the '
+ 'instance.\n'
+ ' Note that it is possible (though not recommended!) for '
+ 'the\n'
+ ' "__del__()" method to postpone destruction of the '
+ 'instance by\n'
+ ' creating a new reference to it. It may then be called at '
+ 'a later\n'
+ ' time when this new reference is deleted. It is not '
+ 'guaranteed that\n'
+ ' "__del__()" methods are called for objects that still '
+ 'exist when\n'
+ ' the interpreter exits.\n'
+ '\n'
+ ' Note: "del x" doesn\'t directly call "x.__del__()" --- '
+ 'the former\n'
+ ' decrements the reference count for "x" by one, and the '
+ 'latter is\n'
+ ' only called when "x"\'s reference count reaches zero. '
+ 'Some common\n'
+ ' situations that may prevent the reference count of an '
+ 'object from\n'
+ ' going to zero include: circular references between '
+ 'objects (e.g.,\n'
+ ' a doubly-linked list or a tree data structure with '
+ 'parent and\n'
+ ' child pointers); a reference to the object on the stack '
+ 'frame of\n'
+ ' a function that caught an exception (the traceback '
+ 'stored in\n'
+ ' "sys.exc_info()[2]" keeps the stack frame alive); or a '
+ 'reference\n'
+ ' to the object on the stack frame that raised an '
+ 'unhandled\n'
+ ' exception in interactive mode (the traceback stored in\n'
+ ' "sys.last_traceback" keeps the stack frame alive). The '
+ 'first\n'
+ ' situation can only be remedied by explicitly breaking '
+ 'the cycles;\n'
+ ' the second can be resolved by freeing the reference to '
+ 'the\n'
+ ' traceback object when it is no longer useful, and the '
+ 'third can\n'
+ ' be resolved by storing "None" in "sys.last_traceback". '
+ 'Circular\n'
+ ' references which are garbage are detected and cleaned '
+ 'up when the\n'
+ " cyclic garbage collector is enabled (it's on by "
+ 'default). Refer\n'
+ ' to the documentation for the "gc" module for more '
+ 'information\n'
+ ' about this topic.\n'
+ '\n'
+ ' Warning: Due to the precarious circumstances under which\n'
+ ' "__del__()" methods are invoked, exceptions that occur '
+ 'during\n'
+ ' their execution are ignored, and a warning is printed '
+ 'to\n'
+ ' "sys.stderr" instead. Also, when "__del__()" is invoked '
+ 'in\n'
+ ' response to a module being deleted (e.g., when '
+ 'execution of the\n'
+ ' program is done), other globals referenced by the '
+ '"__del__()"\n'
+ ' method may already have been deleted or in the process '
+ 'of being\n'
+ ' torn down (e.g. the import machinery shutting down). '
+ 'For this\n'
+ ' reason, "__del__()" methods should do the absolute '
+ 'minimum needed\n'
+ ' to maintain external invariants. Starting with version '
+ '1.5,\n'
+ ' Python guarantees that globals whose name begins with a '
+ 'single\n'
+ ' underscore are deleted from their module before other '
+ 'globals are\n'
+ ' deleted; if no other references to such globals exist, '
+ 'this may\n'
+ ' help in assuring that imported modules are still '
+ 'available at the\n'
+ ' time when the "__del__()" method is called.\n'
+ '\n'
+ 'object.__repr__(self)\n'
+ '\n'
+ ' Called by the "repr()" built-in function to compute the '
+ '"official"\n'
+ ' string representation of an object. If at all possible, '
+ 'this\n'
+ ' should look like a valid Python expression that could be '
+ 'used to\n'
+ ' recreate an object with the same value (given an '
+ 'appropriate\n'
+ ' environment). If this is not possible, a string of the '
+ 'form\n'
+ ' "<...some useful description...>" should be returned. The '
+ 'return\n'
+ ' value must be a string object. If a class defines '
+ '"__repr__()" but\n'
+ ' not "__str__()", then "__repr__()" is also used when an '
+ '"informal"\n'
+ ' string representation of instances of that class is '
+ 'required.\n'
+ '\n'
+ ' This is typically used for debugging, so it is important '
+ 'that the\n'
+ ' representation is information-rich and unambiguous.\n'
+ '\n'
+ 'object.__str__(self)\n'
+ '\n'
+ ' Called by "str(object)" and the built-in functions '
+ '"format()" and\n'
+ ' "print()" to compute the "informal" or nicely printable '
+ 'string\n'
+ ' representation of an object. The return value must be a '
+ 'string\n'
+ ' object.\n'
+ '\n'
+ ' This method differs from "object.__repr__()" in that '
+ 'there is no\n'
+ ' expectation that "__str__()" return a valid Python '
+ 'expression: a\n'
+ ' more convenient or concise representation can be used.\n'
+ '\n'
+ ' The default implementation defined by the built-in type '
+ '"object"\n'
+ ' calls "object.__repr__()".\n'
+ '\n'
+ 'object.__bytes__(self)\n'
+ '\n'
+ ' Called by "bytes()" to compute a byte-string '
+ 'representation of an\n'
+ ' object. This should return a "bytes" object.\n'
+ '\n'
+ 'object.__format__(self, format_spec)\n'
+ '\n'
+ ' Called by the "format()" built-in function, and by '
+ 'extension,\n'
+ ' evaluation of formatted string literals and the '
+ '"str.format()"\n'
+ ' method, to produce a "formatted" string representation of '
+ 'an\n'
+ ' object. The "format_spec" argument is a string that '
+ 'contains a\n'
+ ' description of the formatting options desired. The '
+ 'interpretation\n'
+ ' of the "format_spec" argument is up to the type '
+ 'implementing\n'
+ ' "__format__()", however most classes will either '
+ 'delegate\n'
+ ' formatting to one of the built-in types, or use a '
+ 'similar\n'
+ ' formatting option syntax.\n'
+ '\n'
+ ' See Format Specification Mini-Language for a description '
+ 'of the\n'
+ ' standard formatting syntax.\n'
+ '\n'
+ ' The return value must be a string object.\n'
+ '\n'
+ ' Changed in version 3.4: The __format__ method of "object" '
+ 'itself\n'
+ ' raises a "TypeError" if passed any non-empty string.\n'
+ '\n'
+ 'object.__lt__(self, other)\n'
+ 'object.__le__(self, other)\n'
+ 'object.__eq__(self, other)\n'
+ 'object.__ne__(self, other)\n'
+ 'object.__gt__(self, other)\n'
+ 'object.__ge__(self, other)\n'
+ '\n'
+ ' These are the so-called "rich comparison" methods. The\n'
+ ' correspondence between operator symbols and method names '
+ 'is as\n'
+ ' follows: "x<y" calls "x.__lt__(y)", "x<=y" calls '
+ '"x.__le__(y)",\n'
+ ' "x==y" calls "x.__eq__(y)", "x!=y" calls "x.__ne__(y)", '
+ '"x>y" calls\n'
+ ' "x.__gt__(y)", and "x>=y" calls "x.__ge__(y)".\n'
+ '\n'
+ ' A rich comparison method may return the singleton '
+ '"NotImplemented"\n'
+ ' if it does not implement the operation for a given pair '
+ 'of\n'
+ ' arguments. By convention, "False" and "True" are returned '
+ 'for a\n'
+ ' successful comparison. However, these methods can return '
+ 'any value,\n'
+ ' so if the comparison operator is used in a Boolean '
+ 'context (e.g.,\n'
+ ' in the condition of an "if" statement), Python will call '
+ '"bool()"\n'
+ ' on the value to determine if the result is true or '
+ 'false.\n'
+ '\n'
+ ' By default, "__ne__()" delegates to "__eq__()" and '
+ 'inverts the\n'
+ ' result unless it is "NotImplemented". There are no other '
+ 'implied\n'
+ ' relationships among the comparison operators, for '
+ 'example, the\n'
+ ' truth of "(x<y or x==y)" does not imply "x<=y". To '
+ 'automatically\n'
+ ' generate ordering operations from a single root '
+ 'operation, see\n'
+ ' "functools.total_ordering()".\n'
+ '\n'
+ ' See the paragraph on "__hash__()" for some important '
+ 'notes on\n'
+ ' creating *hashable* objects which support custom '
+ 'comparison\n'
+ ' operations and are usable as dictionary keys.\n'
+ '\n'
+ ' There are no swapped-argument versions of these methods '
+ '(to be used\n'
+ ' when the left argument does not support the operation but '
+ 'the right\n'
+ ' argument does); rather, "__lt__()" and "__gt__()" are '
+ "each other's\n"
+ ' reflection, "__le__()" and "__ge__()" are each other\'s '
+ 'reflection,\n'
+ ' and "__eq__()" and "__ne__()" are their own reflection. '
+ 'If the\n'
+ " operands are of different types, and right operand's type "
+ 'is a\n'
+ " direct or indirect subclass of the left operand's type, "
+ 'the\n'
+ ' reflected method of the right operand has priority, '
+ 'otherwise the\n'
+ " left operand's method has priority. Virtual subclassing "
+ 'is not\n'
+ ' considered.\n'
+ '\n'
+ 'object.__hash__(self)\n'
+ '\n'
+ ' Called by built-in function "hash()" and for operations '
+ 'on members\n'
+ ' of hashed collections including "set", "frozenset", and '
+ '"dict".\n'
+ ' "__hash__()" should return an integer. The only required '
+ 'property\n'
+ ' is that objects which compare equal have the same hash '
+ 'value; it is\n'
+ ' advised to somehow mix together (e.g. using exclusive or) '
+ 'the hash\n'
+ ' values for the components of the object that also play a '
+ 'part in\n'
+ ' comparison of objects.\n'
+ '\n'
+ ' Note: "hash()" truncates the value returned from an '
+ "object's\n"
+ ' custom "__hash__()" method to the size of a '
+ '"Py_ssize_t". This\n'
+ ' is typically 8 bytes on 64-bit builds and 4 bytes on '
+ '32-bit\n'
+ ' builds. If an object\'s "__hash__()" must '
+ 'interoperate on builds\n'
+ ' of different bit sizes, be sure to check the width on '
+ 'all\n'
+ ' supported builds. An easy way to do this is with '
+ '"python -c\n'
+ ' "import sys; print(sys.hash_info.width)"".\n'
+ '\n'
+ ' If a class does not define an "__eq__()" method it should '
+ 'not\n'
+ ' define a "__hash__()" operation either; if it defines '
+ '"__eq__()"\n'
+ ' but not "__hash__()", its instances will not be usable as '
+ 'items in\n'
+ ' hashable collections. If a class defines mutable objects '
+ 'and\n'
+ ' implements an "__eq__()" method, it should not implement\n'
+ ' "__hash__()", since the implementation of hashable '
+ 'collections\n'
+ " requires that a key's hash value is immutable (if the "
+ "object's hash\n"
+ ' value changes, it will be in the wrong hash bucket).\n'
+ '\n'
+ ' User-defined classes have "__eq__()" and "__hash__()" '
+ 'methods by\n'
+ ' default; with them, all objects compare unequal (except '
+ 'with\n'
+ ' themselves) and "x.__hash__()" returns an appropriate '
+ 'value such\n'
+ ' that "x == y" implies both that "x is y" and "hash(x) == '
+ 'hash(y)".\n'
+ '\n'
+ ' A class that overrides "__eq__()" and does not define '
+ '"__hash__()"\n'
+ ' will have its "__hash__()" implicitly set to "None". '
+ 'When the\n'
+ ' "__hash__()" method of a class is "None", instances of '
+ 'the class\n'
+ ' will raise an appropriate "TypeError" when a program '
+ 'attempts to\n'
+ ' retrieve their hash value, and will also be correctly '
+ 'identified as\n'
+ ' unhashable when checking "isinstance(obj, '
+ 'collections.Hashable)".\n'
+ '\n'
+ ' If a class that overrides "__eq__()" needs to retain the\n'
+ ' implementation of "__hash__()" from a parent class, the '
+ 'interpreter\n'
+ ' must be told this explicitly by setting "__hash__ =\n'
+ ' <ParentClass>.__hash__".\n'
+ '\n'
+ ' If a class that does not override "__eq__()" wishes to '
+ 'suppress\n'
+ ' hash support, it should include "__hash__ = None" in the '
+ 'class\n'
+ ' definition. A class which defines its own "__hash__()" '
+ 'that\n'
+ ' explicitly raises a "TypeError" would be incorrectly '
+ 'identified as\n'
+ ' hashable by an "isinstance(obj, collections.Hashable)" '
+ 'call.\n'
+ '\n'
+ ' Note: By default, the "__hash__()" values of str, bytes '
+ 'and\n'
+ ' datetime objects are "salted" with an unpredictable '
+ 'random value.\n'
+ ' Although they remain constant within an individual '
+ 'Python\n'
+ ' process, they are not predictable between repeated '
+ 'invocations of\n'
+ ' Python.This is intended to provide protection against a '
+ 'denial-\n'
+ ' of-service caused by carefully-chosen inputs that '
+ 'exploit the\n'
+ ' worst case performance of a dict insertion, O(n^2) '
+ 'complexity.\n'
+ ' See http://www.ocert.org/advisories/ocert-2011-003.html '
+ 'for\n'
+ ' details.Changing hash values affects the iteration '
+ 'order of\n'
+ ' dicts, sets and other mappings. Python has never made '
+ 'guarantees\n'
+ ' about this ordering (and it typically varies between '
+ '32-bit and\n'
+ ' 64-bit builds).See also "PYTHONHASHSEED".\n'
+ '\n'
+ ' Changed in version 3.3: Hash randomization is enabled by '
+ 'default.\n'
+ '\n'
+ 'object.__bool__(self)\n'
+ '\n'
+ ' Called to implement truth value testing and the built-in '
+ 'operation\n'
+ ' "bool()"; should return "False" or "True". When this '
+ 'method is not\n'
+ ' defined, "__len__()" is called, if it is defined, and the '
+ 'object is\n'
+ ' considered true if its result is nonzero. If a class '
+ 'defines\n'
+ ' neither "__len__()" nor "__bool__()", all its instances '
+ 'are\n'
+ ' considered true.\n'
+ '\n'
+ '\n'
+ 'Customizing attribute access\n'
+ '============================\n'
+ '\n'
+ 'The following methods can be defined to customize the '
+ 'meaning of\n'
+ 'attribute access (use of, assignment to, or deletion of '
+ '"x.name") for\n'
+ 'class instances.\n'
+ '\n'
+ 'object.__getattr__(self, name)\n'
+ '\n'
+ ' Called when an attribute lookup has not found the '
+ 'attribute in the\n'
+ ' usual places (i.e. it is not an instance attribute nor is '
+ 'it found\n'
+ ' in the class tree for "self"). "name" is the attribute '
+ 'name. This\n'
+ ' method should return the (computed) attribute value or '
+ 'raise an\n'
+ ' "AttributeError" exception.\n'
+ '\n'
+ ' Note that if the attribute is found through the normal '
+ 'mechanism,\n'
+ ' "__getattr__()" is not called. (This is an intentional '
+ 'asymmetry\n'
+ ' between "__getattr__()" and "__setattr__()".) This is '
+ 'done both for\n'
+ ' efficiency reasons and because otherwise "__getattr__()" '
+ 'would have\n'
+ ' no way to access other attributes of the instance. Note '
+ 'that at\n'
+ ' least for instance variables, you can fake total control '
+ 'by not\n'
+ ' inserting any values in the instance attribute dictionary '
+ '(but\n'
+ ' instead inserting them in another object). See the\n'
+ ' "__getattribute__()" method below for a way to actually '
+ 'get total\n'
+ ' control over attribute access.\n'
+ '\n'
+ 'object.__getattribute__(self, name)\n'
+ '\n'
+ ' Called unconditionally to implement attribute accesses '
+ 'for\n'
+ ' instances of the class. If the class also defines '
+ '"__getattr__()",\n'
+ ' the latter will not be called unless "__getattribute__()" '
+ 'either\n'
+ ' calls it explicitly or raises an "AttributeError". This '
+ 'method\n'
+ ' should return the (computed) attribute value or raise an\n'
+ ' "AttributeError" exception. In order to avoid infinite '
+ 'recursion in\n'
+ ' this method, its implementation should always call the '
+ 'base class\n'
+ ' method with the same name to access any attributes it '
+ 'needs, for\n'
+ ' example, "object.__getattribute__(self, name)".\n'
+ '\n'
+ ' Note: This method may still be bypassed when looking up '
+ 'special\n'
+ ' methods as the result of implicit invocation via '
+ 'language syntax\n'
+ ' or built-in functions. See Special method lookup.\n'
+ '\n'
+ 'object.__setattr__(self, name, value)\n'
+ '\n'
+ ' Called when an attribute assignment is attempted. This '
+ 'is called\n'
+ ' instead of the normal mechanism (i.e. store the value in '
+ 'the\n'
+ ' instance dictionary). *name* is the attribute name, '
+ '*value* is the\n'
+ ' value to be assigned to it.\n'
+ '\n'
+ ' If "__setattr__()" wants to assign to an instance '
+ 'attribute, it\n'
+ ' should call the base class method with the same name, for '
+ 'example,\n'
+ ' "object.__setattr__(self, name, value)".\n'
+ '\n'
+ 'object.__delattr__(self, name)\n'
+ '\n'
+ ' Like "__setattr__()" but for attribute deletion instead '
+ 'of\n'
+ ' assignment. This should only be implemented if "del '
+ 'obj.name" is\n'
+ ' meaningful for the object.\n'
+ '\n'
+ 'object.__dir__(self)\n'
+ '\n'
+ ' Called when "dir()" is called on the object. A sequence '
+ 'must be\n'
+ ' returned. "dir()" converts the returned sequence to a '
+ 'list and\n'
+ ' sorts it.\n'
+ '\n'
+ '\n'
+ 'Implementing Descriptors\n'
+ '------------------------\n'
+ '\n'
+ 'The following methods only apply when an instance of the '
+ 'class\n'
+ 'containing the method (a so-called *descriptor* class) '
+ 'appears in an\n'
+ "*owner* class (the descriptor must be in either the owner's "
+ 'class\n'
+ 'dictionary or in the class dictionary for one of its '
+ 'parents). In the\n'
+ 'examples below, "the attribute" refers to the attribute '
+ 'whose name is\n'
+ 'the key of the property in the owner class\' "__dict__".\n'
+ '\n'
+ 'object.__get__(self, instance, owner)\n'
+ '\n'
+ ' Called to get the attribute of the owner class (class '
+ 'attribute\n'
+ ' access) or of an instance of that class (instance '
+ 'attribute\n'
+ ' access). *owner* is always the owner class, while '
+ '*instance* is the\n'
+ ' instance that the attribute was accessed through, or '
+ '"None" when\n'
+ ' the attribute is accessed through the *owner*. This '
+ 'method should\n'
+ ' return the (computed) attribute value or raise an '
+ '"AttributeError"\n'
+ ' exception.\n'
+ '\n'
+ 'object.__set__(self, instance, value)\n'
+ '\n'
+ ' Called to set the attribute on an instance *instance* of '
+ 'the owner\n'
+ ' class to a new value, *value*.\n'
+ '\n'
+ 'object.__delete__(self, instance)\n'
+ '\n'
+ ' Called to delete the attribute on an instance *instance* '
+ 'of the\n'
+ ' owner class.\n'
+ '\n'
+ 'object.__set_name__(self, owner, name)\n'
+ '\n'
+ ' Called at the time the owning class *owner* is created. '
+ 'The\n'
+ ' descriptor has been assigned to *name*.\n'
+ '\n'
+ ' New in version 3.6.\n'
+ '\n'
+ 'The attribute "__objclass__" is interpreted by the "inspect" '
+ 'module as\n'
+ 'specifying the class where this object was defined (setting '
+ 'this\n'
+ 'appropriately can assist in runtime introspection of dynamic '
+ 'class\n'
+ 'attributes). For callables, it may indicate that an instance '
+ 'of the\n'
+ 'given type (or a subclass) is expected or required as the '
+ 'first\n'
+ 'positional argument (for example, CPython sets this '
+ 'attribute for\n'
+ 'unbound methods that are implemented in C).\n'
+ '\n'
+ '\n'
+ 'Invoking Descriptors\n'
+ '--------------------\n'
+ '\n'
+ 'In general, a descriptor is an object attribute with '
+ '"binding\n'
+ 'behavior", one whose attribute access has been overridden by '
+ 'methods\n'
+ 'in the descriptor protocol: "__get__()", "__set__()", and\n'
+ '"__delete__()". If any of those methods are defined for an '
+ 'object, it\n'
+ 'is said to be a descriptor.\n'
+ '\n'
+ 'The default behavior for attribute access is to get, set, or '
+ 'delete\n'
+ "the attribute from an object's dictionary. For instance, "
+ '"a.x" has a\n'
+ 'lookup chain starting with "a.__dict__[\'x\']", then\n'
+ '"type(a).__dict__[\'x\']", and continuing through the base '
+ 'classes of\n'
+ '"type(a)" excluding metaclasses.\n'
+ '\n'
+ 'However, if the looked-up value is an object defining one of '
+ 'the\n'
+ 'descriptor methods, then Python may override the default '
+ 'behavior and\n'
+ 'invoke the descriptor method instead. Where this occurs in '
+ 'the\n'
+ 'precedence chain depends on which descriptor methods were '
+ 'defined and\n'
+ 'how they were called.\n'
+ '\n'
+ 'The starting point for descriptor invocation is a binding, '
+ '"a.x". How\n'
+ 'the arguments are assembled depends on "a":\n'
+ '\n'
+ 'Direct Call\n'
+ ' The simplest and least common call is when user code '
+ 'directly\n'
+ ' invokes a descriptor method: "x.__get__(a)".\n'
+ '\n'
+ 'Instance Binding\n'
+ ' If binding to an object instance, "a.x" is transformed '
+ 'into the\n'
+ ' call: "type(a).__dict__[\'x\'].__get__(a, type(a))".\n'
+ '\n'
+ 'Class Binding\n'
+ ' If binding to a class, "A.x" is transformed into the '
+ 'call:\n'
+ ' "A.__dict__[\'x\'].__get__(None, A)".\n'
+ '\n'
+ 'Super Binding\n'
+ ' If "a" is an instance of "super", then the binding '
+ '"super(B,\n'
+ ' obj).m()" searches "obj.__class__.__mro__" for the base '
+ 'class "A"\n'
+ ' immediately preceding "B" and then invokes the descriptor '
+ 'with the\n'
+ ' call: "A.__dict__[\'m\'].__get__(obj, obj.__class__)".\n'
+ '\n'
+ 'For instance bindings, the precedence of descriptor '
+ 'invocation depends\n'
+ 'on the which descriptor methods are defined. A descriptor '
+ 'can define\n'
+ 'any combination of "__get__()", "__set__()" and '
+ '"__delete__()". If it\n'
+ 'does not define "__get__()", then accessing the attribute '
+ 'will return\n'
+ 'the descriptor object itself unless there is a value in the '
+ "object's\n"
+ 'instance dictionary. If the descriptor defines "__set__()" '
+ 'and/or\n'
+ '"__delete__()", it is a data descriptor; if it defines '
+ 'neither, it is\n'
+ 'a non-data descriptor. Normally, data descriptors define '
+ 'both\n'
+ '"__get__()" and "__set__()", while non-data descriptors have '
+ 'just the\n'
+ '"__get__()" method. Data descriptors with "__set__()" and '
+ '"__get__()"\n'
+ 'defined always override a redefinition in an instance '
+ 'dictionary. In\n'
+ 'contrast, non-data descriptors can be overridden by '
+ 'instances.\n'
+ '\n'
+ 'Python methods (including "staticmethod()" and '
+ '"classmethod()") are\n'
+ 'implemented as non-data descriptors. Accordingly, instances '
+ 'can\n'
+ 'redefine and override methods. This allows individual '
+ 'instances to\n'
+ 'acquire behaviors that differ from other instances of the '
+ 'same class.\n'
+ '\n'
+ 'The "property()" function is implemented as a data '
+ 'descriptor.\n'
+ 'Accordingly, instances cannot override the behavior of a '
+ 'property.\n'
+ '\n'
+ '\n'
+ '__slots__\n'
+ '---------\n'
+ '\n'
+ 'By default, instances of classes have a dictionary for '
+ 'attribute\n'
+ 'storage. This wastes space for objects having very few '
+ 'instance\n'
+ 'variables. The space consumption can become acute when '
+ 'creating large\n'
+ 'numbers of instances.\n'
+ '\n'
+ 'The default can be overridden by defining *__slots__* in a '
+ 'class\n'
+ 'definition. The *__slots__* declaration takes a sequence of '
+ 'instance\n'
+ 'variables and reserves just enough space in each instance to '
+ 'hold a\n'
+ 'value for each variable. Space is saved because *__dict__* '
+ 'is not\n'
+ 'created for each instance.\n'
+ '\n'
+ 'object.__slots__\n'
+ '\n'
+ ' This class variable can be assigned a string, iterable, '
+ 'or sequence\n'
+ ' of strings with variable names used by instances. '
+ '*__slots__*\n'
+ ' reserves space for the declared variables and prevents '
+ 'the\n'
+ ' automatic creation of *__dict__* and *__weakref__* for '
+ 'each\n'
+ ' instance.\n'
+ '\n'
+ '\n'
+ 'Notes on using *__slots__*\n'
+ '~~~~~~~~~~~~~~~~~~~~~~~~~~\n'
+ '\n'
+ '* When inheriting from a class without *__slots__*, the '
+ '*__dict__*\n'
+ ' attribute of that class will always be accessible, so a '
+ '*__slots__*\n'
+ ' definition in the subclass is meaningless.\n'
+ '\n'
+ '* Without a *__dict__* variable, instances cannot be '
+ 'assigned new\n'
+ ' variables not listed in the *__slots__* definition. '
+ 'Attempts to\n'
+ ' assign to an unlisted variable name raises '
+ '"AttributeError". If\n'
+ ' dynamic assignment of new variables is desired, then add\n'
+ ' "\'__dict__\'" to the sequence of strings in the '
+ '*__slots__*\n'
+ ' declaration.\n'
+ '\n'
+ '* Without a *__weakref__* variable for each instance, '
+ 'classes\n'
+ ' defining *__slots__* do not support weak references to '
+ 'its\n'
+ ' instances. If weak reference support is needed, then add\n'
+ ' "\'__weakref__\'" to the sequence of strings in the '
+ '*__slots__*\n'
+ ' declaration.\n'
+ '\n'
+ '* *__slots__* are implemented at the class level by '
+ 'creating\n'
+ ' descriptors (Implementing Descriptors) for each variable '
+ 'name. As a\n'
+ ' result, class attributes cannot be used to set default '
+ 'values for\n'
+ ' instance variables defined by *__slots__*; otherwise, the '
+ 'class\n'
+ ' attribute would overwrite the descriptor assignment.\n'
+ '\n'
+ '* The action of a *__slots__* declaration is limited to the '
+ 'class\n'
+ ' where it is defined. As a result, subclasses will have a '
+ '*__dict__*\n'
+ ' unless they also define *__slots__* (which must only '
+ 'contain names\n'
+ ' of any *additional* slots).\n'
+ '\n'
+ '* If a class defines a slot also defined in a base class, '
+ 'the\n'
+ ' instance variable defined by the base class slot is '
+ 'inaccessible\n'
+ ' (except by retrieving its descriptor directly from the '
+ 'base class).\n'
+ ' This renders the meaning of the program undefined. In the '
+ 'future, a\n'
+ ' check may be added to prevent this.\n'
+ '\n'
+ '* Nonempty *__slots__* does not work for classes derived '
+ 'from\n'
+ ' "variable-length" built-in types such as "int", "bytes" '
+ 'and "tuple".\n'
+ '\n'
+ '* Any non-string iterable may be assigned to *__slots__*. '
+ 'Mappings\n'
+ ' may also be used; however, in the future, special meaning '
+ 'may be\n'
+ ' assigned to the values corresponding to each key.\n'
+ '\n'
+ '* *__class__* assignment works only if both classes have the '
+ 'same\n'
+ ' *__slots__*.\n'
+ '\n'
+ '\n'
+ 'Customizing class creation\n'
+ '==========================\n'
+ '\n'
+ 'Whenever a class inherits from another class, '
+ '*__init_subclass__* is\n'
+ 'called on that class. This way, it is possible to write '
+ 'classes which\n'
+ 'change the behavior of subclasses. This is closely related '
+ 'to class\n'
+ 'decorators, but where class decorators only affect the '
+ 'specific class\n'
+ 'they\'re applied to, "__init_subclass__" solely applies to '
+ 'future\n'
+ 'subclasses of the class defining the method.\n'
+ '\n'
+ 'classmethod object.__init_subclass__(cls)\n'
+ '\n'
+ ' This method is called whenever the containing class is '
+ 'subclassed.\n'
+ ' *cls* is then the new subclass. If defined as a normal '
+ 'instance\n'
+ ' method, this method is implicitly converted to a class '
+ 'method.\n'
+ '\n'
+ ' Keyword arguments which are given to a new class are '
+ 'passed to the\n'
+ ' parent\'s class "__init_subclass__". For compatibility '
+ 'with other\n'
+ ' classes using "__init_subclass__", one should take out '
+ 'the needed\n'
+ ' keyword arguments and pass the others over to the base '
+ 'class, as\n'
+ ' in:\n'
+ '\n'
+ ' class Philosopher:\n'
+ ' def __init_subclass__(cls, default_name, '
+ '**kwargs):\n'
+ ' super().__init_subclass__(**kwargs)\n'
+ ' cls.default_name = default_name\n'
+ '\n'
+ ' class AustralianPhilosopher(Philosopher, '
+ 'default_name="Bruce"):\n'
+ ' pass\n'
+ '\n'
+ ' The default implementation "object.__init_subclass__" '
+ 'does nothing,\n'
+ ' but raises an error if it is called with any arguments.\n'
+ '\n'
+ ' Note: The metaclass hint "metaclass" is consumed by the '
+ 'rest of\n'
+ ' the type machinery, and is never passed to '
+ '"__init_subclass__"\n'
+ ' implementations. The actual metaclass (rather than the '
+ 'explicit\n'
+ ' hint) can be accessed as "type(cls)".\n'
+ '\n'
+ ' New in version 3.6.\n'
+ '\n'
+ '\n'
+ 'Metaclasses\n'
+ '-----------\n'
+ '\n'
+ 'By default, classes are constructed using "type()". The '
+ 'class body is\n'
+ 'executed in a new namespace and the class name is bound '
+ 'locally to the\n'
+ 'result of "type(name, bases, namespace)".\n'
+ '\n'
+ 'The class creation process can be customized by passing the\n'
+ '"metaclass" keyword argument in the class definition line, '
+ 'or by\n'
+ 'inheriting from an existing class that included such an '
+ 'argument. In\n'
+ 'the following example, both "MyClass" and "MySubclass" are '
+ 'instances\n'
+ 'of "Meta":\n'
+ '\n'
+ ' class Meta(type):\n'
+ ' pass\n'
+ '\n'
+ ' class MyClass(metaclass=Meta):\n'
+ ' pass\n'
+ '\n'
+ ' class MySubclass(MyClass):\n'
+ ' pass\n'
+ '\n'
+ 'Any other keyword arguments that are specified in the class '
+ 'definition\n'
+ 'are passed through to all metaclass operations described '
+ 'below.\n'
+ '\n'
+ 'When a class definition is executed, the following steps '
+ 'occur:\n'
+ '\n'
+ '* the appropriate metaclass is determined\n'
+ '\n'
+ '* the class namespace is prepared\n'
+ '\n'
+ '* the class body is executed\n'
+ '\n'
+ '* the class object is created\n'
+ '\n'
+ '\n'
+ 'Determining the appropriate metaclass\n'
+ '-------------------------------------\n'
+ '\n'
+ 'The appropriate metaclass for a class definition is '
+ 'determined as\n'
+ 'follows:\n'
+ '\n'
+ '* if no bases and no explicit metaclass are given, then '
+ '"type()" is\n'
+ ' used\n'
+ '\n'
+ '* if an explicit metaclass is given and it is *not* an '
+ 'instance of\n'
+ ' "type()", then it is used directly as the metaclass\n'
+ '\n'
+ '* if an instance of "type()" is given as the explicit '
+ 'metaclass, or\n'
+ ' bases are defined, then the most derived metaclass is '
+ 'used\n'
+ '\n'
+ 'The most derived metaclass is selected from the explicitly '
+ 'specified\n'
+ 'metaclass (if any) and the metaclasses (i.e. "type(cls)") of '
+ 'all\n'
+ 'specified base classes. The most derived metaclass is one '
+ 'which is a\n'
+ 'subtype of *all* of these candidate metaclasses. If none of '
+ 'the\n'
+ 'candidate metaclasses meets that criterion, then the class '
+ 'definition\n'
+ 'will fail with "TypeError".\n'
+ '\n'
+ '\n'
+ 'Preparing the class namespace\n'
+ '-----------------------------\n'
+ '\n'
+ 'Once the appropriate metaclass has been identified, then the '
+ 'class\n'
+ 'namespace is prepared. If the metaclass has a "__prepare__" '
+ 'attribute,\n'
+ 'it is called as "namespace = metaclass.__prepare__(name, '
+ 'bases,\n'
+ '**kwds)" (where the additional keyword arguments, if any, '
+ 'come from\n'
+ 'the class definition).\n'
+ '\n'
+ 'If the metaclass has no "__prepare__" attribute, then the '
+ 'class\n'
+ 'namespace is initialised as an empty ordered mapping.\n'
+ '\n'
+ 'See also:\n'
+ '\n'
+ ' **PEP 3115** - Metaclasses in Python 3000\n'
+ ' Introduced the "__prepare__" namespace hook\n'
+ '\n'
+ '\n'
+ 'Executing the class body\n'
+ '------------------------\n'
+ '\n'
+ 'The class body is executed (approximately) as "exec(body, '
+ 'globals(),\n'
+ 'namespace)". The key difference from a normal call to '
+ '"exec()" is that\n'
+ 'lexical scoping allows the class body (including any '
+ 'methods) to\n'
+ 'reference names from the current and outer scopes when the '
+ 'class\n'
+ 'definition occurs inside a function.\n'
+ '\n'
+ 'However, even when the class definition occurs inside the '
+ 'function,\n'
+ 'methods defined inside the class still cannot see names '
+ 'defined at the\n'
+ 'class scope. Class variables must be accessed through the '
+ 'first\n'
+ 'parameter of instance or class methods, or through the '
+ 'implicit\n'
+ 'lexically scoped "__class__" reference described in the next '
+ 'section.\n'
+ '\n'
+ '\n'
+ 'Creating the class object\n'
+ '-------------------------\n'
+ '\n'
+ 'Once the class namespace has been populated by executing the '
+ 'class\n'
+ 'body, the class object is created by calling '
+ '"metaclass(name, bases,\n'
+ 'namespace, **kwds)" (the additional keywords passed here are '
+ 'the same\n'
+ 'as those passed to "__prepare__").\n'
+ '\n'
+ 'This class object is the one that will be referenced by the '
+ 'zero-\n'
+ 'argument form of "super()". "__class__" is an implicit '
+ 'closure\n'
+ 'reference created by the compiler if any methods in a class '
+ 'body refer\n'
+ 'to either "__class__" or "super". This allows the zero '
+ 'argument form\n'
+ 'of "super()" to correctly identify the class being defined '
+ 'based on\n'
+ 'lexical scoping, while the class or instance that was used '
+ 'to make the\n'
+ 'current call is identified based on the first argument '
+ 'passed to the\n'
+ 'method.\n'
+ '\n'
+ '**CPython implementation detail:** In CPython 3.6 and later, '
+ 'the\n'
+ '"__class__" cell is passed to the metaclass as a '
+ '"__classcell__" entry\n'
+ 'in the class namespace. If present, this must be propagated '
+ 'up to the\n'
+ '"type.__new__" call in order for the class to be '
+ 'initialised\n'
+ 'correctly. Failing to do so will result in a '
+ '"DeprecationWarning" in\n'
+ 'Python 3.6, and a "RuntimeWarning" in the future.\n'
+ '\n'
+ 'When using the default metaclass "type", or any metaclass '
+ 'that\n'
+ 'ultimately calls "type.__new__", the following additional\n'
+ 'customisation steps are invoked after creating the class '
+ 'object:\n'
+ '\n'
+ '* first, "type.__new__" collects all of the descriptors in '
+ 'the class\n'
+ ' namespace that define a "__set_name__()" method;\n'
+ '\n'
+ '* second, all of these "__set_name__" methods are called '
+ 'with the\n'
+ ' class being defined and the assigned name of that '
+ 'particular\n'
+ ' descriptor; and\n'
+ '\n'
+ '* finally, the "__init_subclass__()" hook is called on the '
+ 'immediate\n'
+ ' parent of the new class in its method resolution order.\n'
+ '\n'
+ 'After the class object is created, it is passed to the '
+ 'class\n'
+ 'decorators included in the class definition (if any) and the '
+ 'resulting\n'
+ 'object is bound in the local namespace as the defined '
+ 'class.\n'
+ '\n'
+ 'When a new class is created by "type.__new__", the object '
+ 'provided as\n'
+ 'the namespace parameter is copied to a new ordered mapping '
+ 'and the\n'
+ 'original object is discarded. The new copy is wrapped in a '
+ 'read-only\n'
+ 'proxy, which becomes the "__dict__" attribute of the class '
+ 'object.\n'
+ '\n'
+ 'See also:\n'
+ '\n'
+ ' **PEP 3135** - New super\n'
+ ' Describes the implicit "__class__" closure reference\n'
+ '\n'
+ '\n'
+ 'Metaclass example\n'
+ '-----------------\n'
+ '\n'
+ 'The potential uses for metaclasses are boundless. Some ideas '
+ 'that have\n'
+ 'been explored include logging, interface checking, '
+ 'automatic\n'
+ 'delegation, automatic property creation, proxies, '
+ 'frameworks, and\n'
+ 'automatic resource locking/synchronization.\n'
+ '\n'
+ 'Here is an example of a metaclass that uses an\n'
+ '"collections.OrderedDict" to remember the order that class '
+ 'variables\n'
+ 'are defined:\n'
+ '\n'
+ ' class OrderedClass(type):\n'
+ '\n'
+ ' @classmethod\n'
+ ' def __prepare__(metacls, name, bases, **kwds):\n'
+ ' return collections.OrderedDict()\n'
+ '\n'
+ ' def __new__(cls, name, bases, namespace, **kwds):\n'
+ ' result = type.__new__(cls, name, bases, '
+ 'dict(namespace))\n'
+ ' result.members = tuple(namespace)\n'
+ ' return result\n'
+ '\n'
+ ' class A(metaclass=OrderedClass):\n'
+ ' def one(self): pass\n'
+ ' def two(self): pass\n'
+ ' def three(self): pass\n'
+ ' def four(self): pass\n'
+ '\n'
+ ' >>> A.members\n'
+ " ('__module__', 'one', 'two', 'three', 'four')\n"
+ '\n'
+ 'When the class definition for *A* gets executed, the process '
+ 'begins\n'
+ 'with calling the metaclass\'s "__prepare__()" method which '
+ 'returns an\n'
+ 'empty "collections.OrderedDict". That mapping records the '
+ 'methods and\n'
+ 'attributes of *A* as they are defined within the body of the '
+ 'class\n'
+ 'statement. Once those definitions are executed, the ordered '
+ 'dictionary\n'
+ 'is fully populated and the metaclass\'s "__new__()" method '
+ 'gets\n'
+ 'invoked. That method builds the new type and it saves the '
+ 'ordered\n'
+ 'dictionary keys in an attribute called "members".\n'
+ '\n'
+ '\n'
+ 'Customizing instance and subclass checks\n'
+ '========================================\n'
+ '\n'
+ 'The following methods are used to override the default '
+ 'behavior of the\n'
+ '"isinstance()" and "issubclass()" built-in functions.\n'
+ '\n'
+ 'In particular, the metaclass "abc.ABCMeta" implements these '
+ 'methods in\n'
+ 'order to allow the addition of Abstract Base Classes (ABCs) '
+ 'as\n'
+ '"virtual base classes" to any class or type (including '
+ 'built-in\n'
+ 'types), including other ABCs.\n'
+ '\n'
+ 'class.__instancecheck__(self, instance)\n'
+ '\n'
+ ' Return true if *instance* should be considered a (direct '
+ 'or\n'
+ ' indirect) instance of *class*. If defined, called to '
+ 'implement\n'
+ ' "isinstance(instance, class)".\n'
+ '\n'
+ 'class.__subclasscheck__(self, subclass)\n'
+ '\n'
+ ' Return true if *subclass* should be considered a (direct '
+ 'or\n'
+ ' indirect) subclass of *class*. If defined, called to '
+ 'implement\n'
+ ' "issubclass(subclass, class)".\n'
+ '\n'
+ 'Note that these methods are looked up on the type '
+ '(metaclass) of a\n'
+ 'class. They cannot be defined as class methods in the '
+ 'actual class.\n'
+ 'This is consistent with the lookup of special methods that '
+ 'are called\n'
+ 'on instances, only in this case the instance is itself a '
+ 'class.\n'
+ '\n'
+ 'See also:\n'
+ '\n'
+ ' **PEP 3119** - Introducing Abstract Base Classes\n'
+ ' Includes the specification for customizing '
+ '"isinstance()" and\n'
+ ' "issubclass()" behavior through "__instancecheck__()" '
+ 'and\n'
+ ' "__subclasscheck__()", with motivation for this '
+ 'functionality in\n'
+ ' the context of adding Abstract Base Classes (see the '
+ '"abc"\n'
+ ' module) to the language.\n'
+ '\n'
+ '\n'
+ 'Emulating callable objects\n'
+ '==========================\n'
+ '\n'
+ 'object.__call__(self[, args...])\n'
+ '\n'
+ ' Called when the instance is "called" as a function; if '
+ 'this method\n'
+ ' is defined, "x(arg1, arg2, ...)" is a shorthand for\n'
+ ' "x.__call__(arg1, arg2, ...)".\n'
+ '\n'
+ '\n'
+ 'Emulating container types\n'
+ '=========================\n'
+ '\n'
+ 'The following methods can be defined to implement container '
+ 'objects.\n'
+ 'Containers usually are sequences (such as lists or tuples) '
+ 'or mappings\n'
+ '(like dictionaries), but can represent other containers as '
+ 'well. The\n'
+ 'first set of methods is used either to emulate a sequence or '
+ 'to\n'
+ 'emulate a mapping; the difference is that for a sequence, '
+ 'the\n'
+ 'allowable keys should be the integers *k* for which "0 <= k '
+ '< N" where\n'
+ '*N* is the length of the sequence, or slice objects, which '
+ 'define a\n'
+ 'range of items. It is also recommended that mappings '
+ 'provide the\n'
+ 'methods "keys()", "values()", "items()", "get()", '
+ '"clear()",\n'
+ '"setdefault()", "pop()", "popitem()", "copy()", and '
+ '"update()"\n'
+ "behaving similar to those for Python's standard dictionary "
+ 'objects.\n'
+ 'The "collections" module provides a "MutableMapping" '
+ 'abstract base\n'
+ 'class to help create those methods from a base set of '
+ '"__getitem__()",\n'
+ '"__setitem__()", "__delitem__()", and "keys()". Mutable '
+ 'sequences\n'
+ 'should provide methods "append()", "count()", "index()", '
+ '"extend()",\n'
+ '"insert()", "pop()", "remove()", "reverse()" and "sort()", '
+ 'like Python\n'
+ 'standard list objects. Finally, sequence types should '
+ 'implement\n'
+ 'addition (meaning concatenation) and multiplication '
+ '(meaning\n'
+ 'repetition) by defining the methods "__add__()", '
+ '"__radd__()",\n'
+ '"__iadd__()", "__mul__()", "__rmul__()" and "__imul__()" '
+ 'described\n'
+ 'below; they should not define other numerical operators. It '
+ 'is\n'
+ 'recommended that both mappings and sequences implement the\n'
+ '"__contains__()" method to allow efficient use of the "in" '
+ 'operator;\n'
+ 'for mappings, "in" should search the mapping\'s keys; for '
+ 'sequences, it\n'
+ 'should search through the values. It is further recommended '
+ 'that both\n'
+ 'mappings and sequences implement the "__iter__()" method to '
+ 'allow\n'
+ 'efficient iteration through the container; for mappings, '
+ '"__iter__()"\n'
+ 'should be the same as "keys()"; for sequences, it should '
+ 'iterate\n'
+ 'through the values.\n'
+ '\n'
+ 'object.__len__(self)\n'
+ '\n'
+ ' Called to implement the built-in function "len()". '
+ 'Should return\n'
+ ' the length of the object, an integer ">=" 0. Also, an '
+ 'object that\n'
+ ' doesn\'t define a "__bool__()" method and whose '
+ '"__len__()" method\n'
+ ' returns zero is considered to be false in a Boolean '
+ 'context.\n'
+ '\n'
+ 'object.__length_hint__(self)\n'
+ '\n'
+ ' Called to implement "operator.length_hint()". Should '
+ 'return an\n'
+ ' estimated length for the object (which may be greater or '
+ 'less than\n'
+ ' the actual length). The length must be an integer ">=" 0. '
+ 'This\n'
+ ' method is purely an optimization and is never required '
+ 'for\n'
+ ' correctness.\n'
+ '\n'
+ ' New in version 3.4.\n'
+ '\n'
+ 'Note: Slicing is done exclusively with the following three '
+ 'methods.\n'
+ ' A call like\n'
+ '\n'
+ ' a[1:2] = b\n'
+ '\n'
+ ' is translated to\n'
+ '\n'
+ ' a[slice(1, 2, None)] = b\n'
+ '\n'
+ ' and so forth. Missing slice items are always filled in '
+ 'with "None".\n'
+ '\n'
+ 'object.__getitem__(self, key)\n'
+ '\n'
+ ' Called to implement evaluation of "self[key]". For '
+ 'sequence types,\n'
+ ' the accepted keys should be integers and slice objects. '
+ 'Note that\n'
+ ' the special interpretation of negative indexes (if the '
+ 'class wishes\n'
+ ' to emulate a sequence type) is up to the "__getitem__()" '
+ 'method. If\n'
+ ' *key* is of an inappropriate type, "TypeError" may be '
+ 'raised; if of\n'
+ ' a value outside the set of indexes for the sequence '
+ '(after any\n'
+ ' special interpretation of negative values), "IndexError" '
+ 'should be\n'
+ ' raised. For mapping types, if *key* is missing (not in '
+ 'the\n'
+ ' container), "KeyError" should be raised.\n'
+ '\n'
+ ' Note: "for" loops expect that an "IndexError" will be '
+ 'raised for\n'
+ ' illegal indexes to allow proper detection of the end of '
+ 'the\n'
+ ' sequence.\n'
+ '\n'
+ 'object.__missing__(self, key)\n'
+ '\n'
+ ' Called by "dict"."__getitem__()" to implement "self[key]" '
+ 'for dict\n'
+ ' subclasses when key is not in the dictionary.\n'
+ '\n'
+ 'object.__setitem__(self, key, value)\n'
+ '\n'
+ ' Called to implement assignment to "self[key]". Same note '
+ 'as for\n'
+ ' "__getitem__()". This should only be implemented for '
+ 'mappings if\n'
+ ' the objects support changes to the values for keys, or if '
+ 'new keys\n'
+ ' can be added, or for sequences if elements can be '
+ 'replaced. The\n'
+ ' same exceptions should be raised for improper *key* '
+ 'values as for\n'
+ ' the "__getitem__()" method.\n'
+ '\n'
+ 'object.__delitem__(self, key)\n'
+ '\n'
+ ' Called to implement deletion of "self[key]". Same note '
+ 'as for\n'
+ ' "__getitem__()". This should only be implemented for '
+ 'mappings if\n'
+ ' the objects support removal of keys, or for sequences if '
+ 'elements\n'
+ ' can be removed from the sequence. The same exceptions '
+ 'should be\n'
+ ' raised for improper *key* values as for the '
+ '"__getitem__()" method.\n'
+ '\n'
+ 'object.__iter__(self)\n'
+ '\n'
+ ' This method is called when an iterator is required for a '
+ 'container.\n'
+ ' This method should return a new iterator object that can '
+ 'iterate\n'
+ ' over all the objects in the container. For mappings, it '
+ 'should\n'
+ ' iterate over the keys of the container.\n'
+ '\n'
+ ' Iterator objects also need to implement this method; they '
+ 'are\n'
+ ' required to return themselves. For more information on '
+ 'iterator\n'
+ ' objects, see Iterator Types.\n'
+ '\n'
+ 'object.__reversed__(self)\n'
+ '\n'
+ ' Called (if present) by the "reversed()" built-in to '
+ 'implement\n'
+ ' reverse iteration. It should return a new iterator '
+ 'object that\n'
+ ' iterates over all the objects in the container in reverse '
+ 'order.\n'
+ '\n'
+ ' If the "__reversed__()" method is not provided, the '
+ '"reversed()"\n'
+ ' built-in will fall back to using the sequence protocol '
+ '("__len__()"\n'
+ ' and "__getitem__()"). Objects that support the sequence '
+ 'protocol\n'
+ ' should only provide "__reversed__()" if they can provide '
+ 'an\n'
+ ' implementation that is more efficient than the one '
+ 'provided by\n'
+ ' "reversed()".\n'
+ '\n'
+ 'The membership test operators ("in" and "not in") are '
+ 'normally\n'
+ 'implemented as an iteration through a sequence. However, '
+ 'container\n'
+ 'objects can supply the following special method with a more '
+ 'efficient\n'
+ 'implementation, which also does not require the object be a '
+ 'sequence.\n'
+ '\n'
+ 'object.__contains__(self, item)\n'
+ '\n'
+ ' Called to implement membership test operators. Should '
+ 'return true\n'
+ ' if *item* is in *self*, false otherwise. For mapping '
+ 'objects, this\n'
+ ' should consider the keys of the mapping rather than the '
+ 'values or\n'
+ ' the key-item pairs.\n'
+ '\n'
+ ' For objects that don\'t define "__contains__()", the '
+ 'membership test\n'
+ ' first tries iteration via "__iter__()", then the old '
+ 'sequence\n'
+ ' iteration protocol via "__getitem__()", see this section '
+ 'in the\n'
+ ' language reference.\n'
+ '\n'
+ '\n'
+ 'Emulating numeric types\n'
+ '=======================\n'
+ '\n'
+ 'The following methods can be defined to emulate numeric '
+ 'objects.\n'
+ 'Methods corresponding to operations that are not supported '
+ 'by the\n'
+ 'particular kind of number implemented (e.g., bitwise '
+ 'operations for\n'
+ 'non-integral numbers) should be left undefined.\n'
+ '\n'
+ 'object.__add__(self, other)\n'
+ 'object.__sub__(self, other)\n'
+ 'object.__mul__(self, other)\n'
+ 'object.__matmul__(self, other)\n'
+ 'object.__truediv__(self, other)\n'
+ 'object.__floordiv__(self, other)\n'
+ 'object.__mod__(self, other)\n'
+ 'object.__divmod__(self, other)\n'
+ 'object.__pow__(self, other[, modulo])\n'
+ 'object.__lshift__(self, other)\n'
+ 'object.__rshift__(self, other)\n'
+ 'object.__and__(self, other)\n'
+ 'object.__xor__(self, other)\n'
+ 'object.__or__(self, other)\n'
+ '\n'
+ ' These methods are called to implement the binary '
+ 'arithmetic\n'
+ ' operations ("+", "-", "*", "@", "/", "//", "%", '
+ '"divmod()",\n'
+ ' "pow()", "**", "<<", ">>", "&", "^", "|"). For instance, '
+ 'to\n'
+ ' evaluate the expression "x + y", where *x* is an instance '
+ 'of a\n'
+ ' class that has an "__add__()" method, "x.__add__(y)" is '
+ 'called.\n'
+ ' The "__divmod__()" method should be the equivalent to '
+ 'using\n'
+ ' "__floordiv__()" and "__mod__()"; it should not be '
+ 'related to\n'
+ ' "__truediv__()". Note that "__pow__()" should be defined '
+ 'to accept\n'
+ ' an optional third argument if the ternary version of the '
+ 'built-in\n'
+ ' "pow()" function is to be supported.\n'
+ '\n'
+ ' If one of those methods does not support the operation '
+ 'with the\n'
+ ' supplied arguments, it should return "NotImplemented".\n'
+ '\n'
+ 'object.__radd__(self, other)\n'
+ 'object.__rsub__(self, other)\n'
+ 'object.__rmul__(self, other)\n'
+ 'object.__rmatmul__(self, other)\n'
+ 'object.__rtruediv__(self, other)\n'
+ 'object.__rfloordiv__(self, other)\n'
+ 'object.__rmod__(self, other)\n'
+ 'object.__rdivmod__(self, other)\n'
+ 'object.__rpow__(self, other)\n'
+ 'object.__rlshift__(self, other)\n'
+ 'object.__rrshift__(self, other)\n'
+ 'object.__rand__(self, other)\n'
+ 'object.__rxor__(self, other)\n'
+ 'object.__ror__(self, other)\n'
+ '\n'
+ ' These methods are called to implement the binary '
+ 'arithmetic\n'
+ ' operations ("+", "-", "*", "@", "/", "//", "%", '
+ '"divmod()",\n'
+ ' "pow()", "**", "<<", ">>", "&", "^", "|") with reflected '
+ '(swapped)\n'
+ ' operands. These functions are only called if the left '
+ 'operand does\n'
+ ' not support the corresponding operation [3] and the '
+ 'operands are of\n'
+ ' different types. [4] For instance, to evaluate the '
+ 'expression "x -\n'
+ ' y", where *y* is an instance of a class that has an '
+ '"__rsub__()"\n'
+ ' method, "y.__rsub__(x)" is called if "x.__sub__(y)" '
+ 'returns\n'
+ ' *NotImplemented*.\n'
+ '\n'
+ ' Note that ternary "pow()" will not try calling '
+ '"__rpow__()" (the\n'
+ ' coercion rules would become too complicated).\n'
+ '\n'
+ " Note: If the right operand's type is a subclass of the "
+ 'left\n'
+ " operand's type and that subclass provides the reflected "
+ 'method\n'
+ ' for the operation, this method will be called before '
+ 'the left\n'
+ " operand's non-reflected method. This behavior allows "
+ 'subclasses\n'
+ " to override their ancestors' operations.\n"
+ '\n'
+ 'object.__iadd__(self, other)\n'
+ 'object.__isub__(self, other)\n'
+ 'object.__imul__(self, other)\n'
+ 'object.__imatmul__(self, other)\n'
+ 'object.__itruediv__(self, other)\n'
+ 'object.__ifloordiv__(self, other)\n'
+ 'object.__imod__(self, other)\n'
+ 'object.__ipow__(self, other[, modulo])\n'
+ 'object.__ilshift__(self, other)\n'
+ 'object.__irshift__(self, other)\n'
+ 'object.__iand__(self, other)\n'
+ 'object.__ixor__(self, other)\n'
+ 'object.__ior__(self, other)\n'
+ '\n'
+ ' These methods are called to implement the augmented '
+ 'arithmetic\n'
+ ' assignments ("+=", "-=", "*=", "@=", "/=", "//=", "%=", '
+ '"**=",\n'
+ ' "<<=", ">>=", "&=", "^=", "|="). These methods should '
+ 'attempt to\n'
+ ' do the operation in-place (modifying *self*) and return '
+ 'the result\n'
+ ' (which could be, but does not have to be, *self*). If a '
+ 'specific\n'
+ ' method is not defined, the augmented assignment falls '
+ 'back to the\n'
+ ' normal methods. For instance, if *x* is an instance of a '
+ 'class\n'
+ ' with an "__iadd__()" method, "x += y" is equivalent to "x '
+ '=\n'
+ ' x.__iadd__(y)" . Otherwise, "x.__add__(y)" and '
+ '"y.__radd__(x)" are\n'
+ ' considered, as with the evaluation of "x + y". In '
+ 'certain\n'
+ ' situations, augmented assignment can result in unexpected '
+ 'errors\n'
+ " (see Why does a_tuple[i] += ['item'] raise an exception "
+ 'when the\n'
+ ' addition works?), but this behavior is in fact part of '
+ 'the data\n'
+ ' model.\n'
+ '\n'
+ 'object.__neg__(self)\n'
+ 'object.__pos__(self)\n'
+ 'object.__abs__(self)\n'
+ 'object.__invert__(self)\n'
+ '\n'
+ ' Called to implement the unary arithmetic operations ("-", '
+ '"+",\n'
+ ' "abs()" and "~").\n'
+ '\n'
+ 'object.__complex__(self)\n'
+ 'object.__int__(self)\n'
+ 'object.__float__(self)\n'
+ 'object.__round__(self[, n])\n'
+ '\n'
+ ' Called to implement the built-in functions "complex()", '
+ '"int()",\n'
+ ' "float()" and "round()". Should return a value of the '
+ 'appropriate\n'
+ ' type.\n'
+ '\n'
+ 'object.__index__(self)\n'
+ '\n'
+ ' Called to implement "operator.index()", and whenever '
+ 'Python needs\n'
+ ' to losslessly convert the numeric object to an integer '
+ 'object (such\n'
+ ' as in slicing, or in the built-in "bin()", "hex()" and '
+ '"oct()"\n'
+ ' functions). Presence of this method indicates that the '
+ 'numeric\n'
+ ' object is an integer type. Must return an integer.\n'
+ '\n'
+ ' Note: In order to have a coherent integer type class, '
+ 'when\n'
+ ' "__index__()" is defined "__int__()" should also be '
+ 'defined, and\n'
+ ' both should return the same value.\n'
+ '\n'
+ '\n'
+ 'With Statement Context Managers\n'
+ '===============================\n'
+ '\n'
+ 'A *context manager* is an object that defines the runtime '
+ 'context to\n'
+ 'be established when executing a "with" statement. The '
+ 'context manager\n'
+ 'handles the entry into, and the exit from, the desired '
+ 'runtime context\n'
+ 'for the execution of the block of code. Context managers '
+ 'are normally\n'
+ 'invoked using the "with" statement (described in section The '
+ 'with\n'
+ 'statement), but can also be used by directly invoking their '
+ 'methods.\n'
+ '\n'
+ 'Typical uses of context managers include saving and '
+ 'restoring various\n'
+ 'kinds of global state, locking and unlocking resources, '
+ 'closing opened\n'
+ 'files, etc.\n'
+ '\n'
+ 'For more information on context managers, see Context '
+ 'Manager Types.\n'
+ '\n'
+ 'object.__enter__(self)\n'
+ '\n'
+ ' Enter the runtime context related to this object. The '
+ '"with"\n'
+ " statement will bind this method's return value to the "
+ 'target(s)\n'
+ ' specified in the "as" clause of the statement, if any.\n'
+ '\n'
+ 'object.__exit__(self, exc_type, exc_value, traceback)\n'
+ '\n'
+ ' Exit the runtime context related to this object. The '
+ 'parameters\n'
+ ' describe the exception that caused the context to be '
+ 'exited. If the\n'
+ ' context was exited without an exception, all three '
+ 'arguments will\n'
+ ' be "None".\n'
+ '\n'
+ ' If an exception is supplied, and the method wishes to '
+ 'suppress the\n'
+ ' exception (i.e., prevent it from being propagated), it '
+ 'should\n'
+ ' return a true value. Otherwise, the exception will be '
+ 'processed\n'
+ ' normally upon exit from this method.\n'
+ '\n'
+ ' Note that "__exit__()" methods should not reraise the '
+ 'passed-in\n'
+ " exception; this is the caller's responsibility.\n"
+ '\n'
+ 'See also:\n'
+ '\n'
+ ' **PEP 343** - The "with" statement\n'
+ ' The specification, background, and examples for the '
+ 'Python "with"\n'
+ ' statement.\n'
+ '\n'
+ '\n'
+ 'Special method lookup\n'
+ '=====================\n'
+ '\n'
+ 'For custom classes, implicit invocations of special methods '
+ 'are only\n'
+ "guaranteed to work correctly if defined on an object's type, "
+ 'not in\n'
+ "the object's instance dictionary. That behaviour is the "
+ 'reason why\n'
+ 'the following code raises an exception:\n'
+ '\n'
+ ' >>> class C:\n'
+ ' ... pass\n'
+ ' ...\n'
+ ' >>> c = C()\n'
+ ' >>> c.__len__ = lambda: 5\n'
+ ' >>> len(c)\n'
+ ' Traceback (most recent call last):\n'
+ ' File "<stdin>", line 1, in <module>\n'
+ " TypeError: object of type 'C' has no len()\n"
+ '\n'
+ 'The rationale behind this behaviour lies with a number of '
+ 'special\n'
+ 'methods such as "__hash__()" and "__repr__()" that are '
+ 'implemented by\n'
+ 'all objects, including type objects. If the implicit lookup '
+ 'of these\n'
+ 'methods used the conventional lookup process, they would '
+ 'fail when\n'
+ 'invoked on the type object itself:\n'
+ '\n'
+ ' >>> 1 .__hash__() == hash(1)\n'
+ ' True\n'
+ ' >>> int.__hash__() == hash(int)\n'
+ ' Traceback (most recent call last):\n'
+ ' File "<stdin>", line 1, in <module>\n'
+ " TypeError: descriptor '__hash__' of 'int' object needs an "
+ 'argument\n'
+ '\n'
+ 'Incorrectly attempting to invoke an unbound method of a '
+ 'class in this\n'
+ "way is sometimes referred to as 'metaclass confusion', and "
+ 'is avoided\n'
+ 'by bypassing the instance when looking up special methods:\n'
+ '\n'
+ ' >>> type(1).__hash__(1) == hash(1)\n'
+ ' True\n'
+ ' >>> type(int).__hash__(int) == hash(int)\n'
+ ' True\n'
+ '\n'
+ 'In addition to bypassing any instance attributes in the '
+ 'interest of\n'
+ 'correctness, implicit special method lookup generally also '
+ 'bypasses\n'
+ 'the "__getattribute__()" method even of the object\'s '
+ 'metaclass:\n'
+ '\n'
+ ' >>> class Meta(type):\n'
+ ' ... def __getattribute__(*args):\n'
+ ' ... print("Metaclass getattribute invoked")\n'
+ ' ... return type.__getattribute__(*args)\n'
+ ' ...\n'
+ ' >>> class C(object, metaclass=Meta):\n'
+ ' ... def __len__(self):\n'
+ ' ... return 10\n'
+ ' ... def __getattribute__(*args):\n'
+ ' ... print("Class getattribute invoked")\n'
+ ' ... return object.__getattribute__(*args)\n'
+ ' ...\n'
+ ' >>> c = C()\n'
+ ' >>> c.__len__() # Explicit lookup via '
+ 'instance\n'
+ ' Class getattribute invoked\n'
+ ' 10\n'
+ ' >>> type(c).__len__(c) # Explicit lookup via '
+ 'type\n'
+ ' Metaclass getattribute invoked\n'
+ ' 10\n'
+ ' >>> len(c) # Implicit lookup\n'
+ ' 10\n'
+ '\n'
+ 'Bypassing the "__getattribute__()" machinery in this fashion '
+ 'provides\n'
+ 'significant scope for speed optimisations within the '
+ 'interpreter, at\n'
+ 'the cost of some flexibility in the handling of special '
+ 'methods (the\n'
+ 'special method *must* be set on the class object itself in '
+ 'order to be\n'
+ 'consistently invoked by the interpreter).\n',
+ 'string-methods': '\n'
+ 'String Methods\n'
+ '**************\n'
+ '\n'
+ 'Strings implement all of the common sequence operations, '
+ 'along with\n'
+ 'the additional methods described below.\n'
+ '\n'
+ 'Strings also support two styles of string formatting, one '
+ 'providing a\n'
+ 'large degree of flexibility and customization (see '
+ '"str.format()",\n'
+ 'Format String Syntax and Custom String Formatting) and the '
+ 'other based\n'
+ 'on C "printf" style formatting that handles a narrower '
+ 'range of types\n'
+ 'and is slightly harder to use correctly, but is often '
+ 'faster for the\n'
+ 'cases it can handle (printf-style String Formatting).\n'
+ '\n'
+ 'The Text Processing Services section of the standard '
+ 'library covers a\n'
+ 'number of other modules that provide various text related '
+ 'utilities\n'
+ '(including regular expression support in the "re" '
+ 'module).\n'
+ '\n'
+ 'str.capitalize()\n'
+ '\n'
+ ' Return a copy of the string with its first character '
+ 'capitalized\n'
+ ' and the rest lowercased.\n'
+ '\n'
+ 'str.casefold()\n'
+ '\n'
+ ' Return a casefolded copy of the string. Casefolded '
+ 'strings may be\n'
+ ' used for caseless matching.\n'
+ '\n'
+ ' Casefolding is similar to lowercasing but more '
+ 'aggressive because\n'
+ ' it is intended to remove all case distinctions in a '
+ 'string. For\n'
+ ' example, the German lowercase letter "\'ß\'" is '
+ 'equivalent to ""ss"".\n'
+ ' Since it is already lowercase, "lower()" would do '
+ 'nothing to "\'ß\'";\n'
+ ' "casefold()" converts it to ""ss"".\n'
+ '\n'
+ ' The casefolding algorithm is described in section 3.13 '
+ 'of the\n'
+ ' Unicode Standard.\n'
+ '\n'
+ ' New in version 3.3.\n'
+ '\n'
+ 'str.center(width[, fillchar])\n'
+ '\n'
+ ' Return centered in a string of length *width*. Padding '
+ 'is done\n'
+ ' using the specified *fillchar* (default is an ASCII '
+ 'space). The\n'
+ ' original string is returned if *width* is less than or '
+ 'equal to\n'
+ ' "len(s)".\n'
+ '\n'
+ 'str.count(sub[, start[, end]])\n'
+ '\n'
+ ' Return the number of non-overlapping occurrences of '
+ 'substring *sub*\n'
+ ' in the range [*start*, *end*]. Optional arguments '
+ '*start* and\n'
+ ' *end* are interpreted as in slice notation.\n'
+ '\n'
+ 'str.encode(encoding="utf-8", errors="strict")\n'
+ '\n'
+ ' Return an encoded version of the string as a bytes '
+ 'object. Default\n'
+ ' encoding is "\'utf-8\'". *errors* may be given to set a '
+ 'different\n'
+ ' error handling scheme. The default for *errors* is '
+ '"\'strict\'",\n'
+ ' meaning that encoding errors raise a "UnicodeError". '
+ 'Other possible\n'
+ ' values are "\'ignore\'", "\'replace\'", '
+ '"\'xmlcharrefreplace\'",\n'
+ ' "\'backslashreplace\'" and any other name registered '
+ 'via\n'
+ ' "codecs.register_error()", see section Error Handlers. '
+ 'For a list\n'
+ ' of possible encodings, see section Standard Encodings.\n'
+ '\n'
+ ' Changed in version 3.1: Support for keyword arguments '
+ 'added.\n'
+ '\n'
+ 'str.endswith(suffix[, start[, end]])\n'
+ '\n'
+ ' Return "True" if the string ends with the specified '
+ '*suffix*,\n'
+ ' otherwise return "False". *suffix* can also be a tuple '
+ 'of suffixes\n'
+ ' to look for. With optional *start*, test beginning at '
+ 'that\n'
+ ' position. With optional *end*, stop comparing at that '
+ 'position.\n'
+ '\n'
+ 'str.expandtabs(tabsize=8)\n'
+ '\n'
+ ' Return a copy of the string where all tab characters '
+ 'are replaced\n'
+ ' by one or more spaces, depending on the current column '
+ 'and the\n'
+ ' given tab size. Tab positions occur every *tabsize* '
+ 'characters\n'
+ ' (default is 8, giving tab positions at columns 0, 8, 16 '
+ 'and so on).\n'
+ ' To expand the string, the current column is set to zero '
+ 'and the\n'
+ ' string is examined character by character. If the '
+ 'character is a\n'
+ ' tab ("\\t"), one or more space characters are inserted '
+ 'in the result\n'
+ ' until the current column is equal to the next tab '
+ 'position. (The\n'
+ ' tab character itself is not copied.) If the character '
+ 'is a newline\n'
+ ' ("\\n") or return ("\\r"), it is copied and the current '
+ 'column is\n'
+ ' reset to zero. Any other character is copied unchanged '
+ 'and the\n'
+ ' current column is incremented by one regardless of how '
+ 'the\n'
+ ' character is represented when printed.\n'
+ '\n'
+ " >>> '01\\t012\\t0123\\t01234'.expandtabs()\n"
+ " '01 012 0123 01234'\n"
+ " >>> '01\\t012\\t0123\\t01234'.expandtabs(4)\n"
+ " '01 012 0123 01234'\n"
+ '\n'
+ 'str.find(sub[, start[, end]])\n'
+ '\n'
+ ' Return the lowest index in the string where substring '
+ '*sub* is\n'
+ ' found within the slice "s[start:end]". Optional '
+ 'arguments *start*\n'
+ ' and *end* are interpreted as in slice notation. Return '
+ '"-1" if\n'
+ ' *sub* is not found.\n'
+ '\n'
+ ' Note: The "find()" method should be used only if you '
+ 'need to know\n'
+ ' the position of *sub*. To check if *sub* is a '
+ 'substring or not,\n'
+ ' use the "in" operator:\n'
+ '\n'
+ " >>> 'Py' in 'Python'\n"
+ ' True\n'
+ '\n'
+ 'str.format(*args, **kwargs)\n'
+ '\n'
+ ' Perform a string formatting operation. The string on '
+ 'which this\n'
+ ' method is called can contain literal text or '
+ 'replacement fields\n'
+ ' delimited by braces "{}". Each replacement field '
+ 'contains either\n'
+ ' the numeric index of a positional argument, or the name '
+ 'of a\n'
+ ' keyword argument. Returns a copy of the string where '
+ 'each\n'
+ ' replacement field is replaced with the string value of '
+ 'the\n'
+ ' corresponding argument.\n'
+ '\n'
+ ' >>> "The sum of 1 + 2 is {0}".format(1+2)\n'
+ " 'The sum of 1 + 2 is 3'\n"
+ '\n'
+ ' See Format String Syntax for a description of the '
+ 'various\n'
+ ' formatting options that can be specified in format '
+ 'strings.\n'
+ '\n'
+ 'str.format_map(mapping)\n'
+ '\n'
+ ' Similar to "str.format(**mapping)", except that '
+ '"mapping" is used\n'
+ ' directly and not copied to a "dict". This is useful if '
+ 'for example\n'
+ ' "mapping" is a dict subclass:\n'
+ '\n'
+ ' >>> class Default(dict):\n'
+ ' ... def __missing__(self, key):\n'
+ ' ... return key\n'
+ ' ...\n'
+ " >>> '{name} was born in "
+ "{country}'.format_map(Default(name='Guido'))\n"
+ " 'Guido was born in country'\n"
+ '\n'
+ ' New in version 3.2.\n'
+ '\n'
+ 'str.index(sub[, start[, end]])\n'
+ '\n'
+ ' Like "find()", but raise "ValueError" when the '
+ 'substring is not\n'
+ ' found.\n'
+ '\n'
+ 'str.isalnum()\n'
+ '\n'
+ ' Return true if all characters in the string are '
+ 'alphanumeric and\n'
+ ' there is at least one character, false otherwise. A '
+ 'character "c"\n'
+ ' is alphanumeric if one of the following returns '
+ '"True":\n'
+ ' "c.isalpha()", "c.isdecimal()", "c.isdigit()", or '
+ '"c.isnumeric()".\n'
+ '\n'
+ 'str.isalpha()\n'
+ '\n'
+ ' Return true if all characters in the string are '
+ 'alphabetic and\n'
+ ' there is at least one character, false otherwise. '
+ 'Alphabetic\n'
+ ' characters are those characters defined in the Unicode '
+ 'character\n'
+ ' database as "Letter", i.e., those with general category '
+ 'property\n'
+ ' being one of "Lm", "Lt", "Lu", "Ll", or "Lo". Note '
+ 'that this is\n'
+ ' different from the "Alphabetic" property defined in the '
+ 'Unicode\n'
+ ' Standard.\n'
+ '\n'
+ 'str.isdecimal()\n'
+ '\n'
+ ' Return true if all characters in the string are decimal '
+ 'characters\n'
+ ' and there is at least one character, false otherwise. '
+ 'Decimal\n'
+ ' characters are those from general category "Nd". This '
+ 'category\n'
+ ' includes digit characters, and all characters that can '
+ 'be used to\n'
+ ' form decimal-radix numbers, e.g. U+0660, ARABIC-INDIC '
+ 'DIGIT ZERO.\n'
+ '\n'
+ 'str.isdigit()\n'
+ '\n'
+ ' Return true if all characters in the string are digits '
+ 'and there is\n'
+ ' at least one character, false otherwise. Digits '
+ 'include decimal\n'
+ ' characters and digits that need special handling, such '
+ 'as the\n'
+ ' compatibility superscript digits. Formally, a digit is '
+ 'a character\n'
+ ' that has the property value Numeric_Type=Digit or\n'
+ ' Numeric_Type=Decimal.\n'
+ '\n'
+ 'str.isidentifier()\n'
+ '\n'
+ ' Return true if the string is a valid identifier '
+ 'according to the\n'
+ ' language definition, section Identifiers and keywords.\n'
+ '\n'
+ ' Use "keyword.iskeyword()" to test for reserved '
+ 'identifiers such as\n'
+ ' "def" and "class".\n'
+ '\n'
+ 'str.islower()\n'
+ '\n'
+ ' Return true if all cased characters [4] in the string '
+ 'are lowercase\n'
+ ' and there is at least one cased character, false '
+ 'otherwise.\n'
+ '\n'
+ 'str.isnumeric()\n'
+ '\n'
+ ' Return true if all characters in the string are numeric '
+ 'characters,\n'
+ ' and there is at least one character, false otherwise. '
+ 'Numeric\n'
+ ' characters include digit characters, and all characters '
+ 'that have\n'
+ ' the Unicode numeric value property, e.g. U+2155, VULGAR '
+ 'FRACTION\n'
+ ' ONE FIFTH. Formally, numeric characters are those with '
+ 'the\n'
+ ' property value Numeric_Type=Digit, Numeric_Type=Decimal '
+ 'or\n'
+ ' Numeric_Type=Numeric.\n'
+ '\n'
+ 'str.isprintable()\n'
+ '\n'
+ ' Return true if all characters in the string are '
+ 'printable or the\n'
+ ' string is empty, false otherwise. Nonprintable '
+ 'characters are\n'
+ ' those characters defined in the Unicode character '
+ 'database as\n'
+ ' "Other" or "Separator", excepting the ASCII space '
+ '(0x20) which is\n'
+ ' considered printable. (Note that printable characters '
+ 'in this\n'
+ ' context are those which should not be escaped when '
+ '"repr()" is\n'
+ ' invoked on a string. It has no bearing on the handling '
+ 'of strings\n'
+ ' written to "sys.stdout" or "sys.stderr".)\n'
+ '\n'
+ 'str.isspace()\n'
+ '\n'
+ ' Return true if there are only whitespace characters in '
+ 'the string\n'
+ ' and there is at least one character, false otherwise. '
+ 'Whitespace\n'
+ ' characters are those characters defined in the Unicode '
+ 'character\n'
+ ' database as "Other" or "Separator" and those with '
+ 'bidirectional\n'
+ ' property being one of "WS", "B", or "S".\n'
+ '\n'
+ 'str.istitle()\n'
+ '\n'
+ ' Return true if the string is a titlecased string and '
+ 'there is at\n'
+ ' least one character, for example uppercase characters '
+ 'may only\n'
+ ' follow uncased characters and lowercase characters only '
+ 'cased ones.\n'
+ ' Return false otherwise.\n'
+ '\n'
+ 'str.isupper()\n'
+ '\n'
+ ' Return true if all cased characters [4] in the string '
+ 'are uppercase\n'
+ ' and there is at least one cased character, false '
+ 'otherwise.\n'
+ '\n'
+ 'str.join(iterable)\n'
+ '\n'
+ ' Return a string which is the concatenation of the '
+ 'strings in the\n'
+ ' *iterable* *iterable*. A "TypeError" will be raised if '
+ 'there are\n'
+ ' any non-string values in *iterable*, including "bytes" '
+ 'objects.\n'
+ ' The separator between elements is the string providing '
+ 'this method.\n'
+ '\n'
+ 'str.ljust(width[, fillchar])\n'
+ '\n'
+ ' Return the string left justified in a string of length '
+ '*width*.\n'
+ ' Padding is done using the specified *fillchar* (default '
+ 'is an ASCII\n'
+ ' space). The original string is returned if *width* is '
+ 'less than or\n'
+ ' equal to "len(s)".\n'
+ '\n'
+ 'str.lower()\n'
+ '\n'
+ ' Return a copy of the string with all the cased '
+ 'characters [4]\n'
+ ' converted to lowercase.\n'
+ '\n'
+ ' The lowercasing algorithm used is described in section '
+ '3.13 of the\n'
+ ' Unicode Standard.\n'
+ '\n'
+ 'str.lstrip([chars])\n'
+ '\n'
+ ' Return a copy of the string with leading characters '
+ 'removed. The\n'
+ ' *chars* argument is a string specifying the set of '
+ 'characters to be\n'
+ ' removed. If omitted or "None", the *chars* argument '
+ 'defaults to\n'
+ ' removing whitespace. The *chars* argument is not a '
+ 'prefix; rather,\n'
+ ' all combinations of its values are stripped:\n'
+ '\n'
+ " >>> ' spacious '.lstrip()\n"
+ " 'spacious '\n"
+ " >>> 'www.example.com'.lstrip('cmowz.')\n"
+ " 'example.com'\n"
+ '\n'
+ 'static str.maketrans(x[, y[, z]])\n'
+ '\n'
+ ' This static method returns a translation table usable '
+ 'for\n'
+ ' "str.translate()".\n'
+ '\n'
+ ' If there is only one argument, it must be a dictionary '
+ 'mapping\n'
+ ' Unicode ordinals (integers) or characters (strings of '
+ 'length 1) to\n'
+ ' Unicode ordinals, strings (of arbitrary lengths) or '
+ '"None".\n'
+ ' Character keys will then be converted to ordinals.\n'
+ '\n'
+ ' If there are two arguments, they must be strings of '
+ 'equal length,\n'
+ ' and in the resulting dictionary, each character in x '
+ 'will be mapped\n'
+ ' to the character at the same position in y. If there '
+ 'is a third\n'
+ ' argument, it must be a string, whose characters will be '
+ 'mapped to\n'
+ ' "None" in the result.\n'
+ '\n'
+ 'str.partition(sep)\n'
+ '\n'
+ ' Split the string at the first occurrence of *sep*, and '
+ 'return a\n'
+ ' 3-tuple containing the part before the separator, the '
+ 'separator\n'
+ ' itself, and the part after the separator. If the '
+ 'separator is not\n'
+ ' found, return a 3-tuple containing the string itself, '
+ 'followed by\n'
+ ' two empty strings.\n'
+ '\n'
+ 'str.replace(old, new[, count])\n'
+ '\n'
+ ' Return a copy of the string with all occurrences of '
+ 'substring *old*\n'
+ ' replaced by *new*. If the optional argument *count* is '
+ 'given, only\n'
+ ' the first *count* occurrences are replaced.\n'
+ '\n'
+ 'str.rfind(sub[, start[, end]])\n'
+ '\n'
+ ' Return the highest index in the string where substring '
+ '*sub* is\n'
+ ' found, such that *sub* is contained within '
+ '"s[start:end]".\n'
+ ' Optional arguments *start* and *end* are interpreted as '
+ 'in slice\n'
+ ' notation. Return "-1" on failure.\n'
+ '\n'
+ 'str.rindex(sub[, start[, end]])\n'
+ '\n'
+ ' Like "rfind()" but raises "ValueError" when the '
+ 'substring *sub* is\n'
+ ' not found.\n'
+ '\n'
+ 'str.rjust(width[, fillchar])\n'
+ '\n'
+ ' Return the string right justified in a string of length '
+ '*width*.\n'
+ ' Padding is done using the specified *fillchar* (default '
+ 'is an ASCII\n'
+ ' space). The original string is returned if *width* is '
+ 'less than or\n'
+ ' equal to "len(s)".\n'
+ '\n'
+ 'str.rpartition(sep)\n'
+ '\n'
+ ' Split the string at the last occurrence of *sep*, and '
+ 'return a\n'
+ ' 3-tuple containing the part before the separator, the '
+ 'separator\n'
+ ' itself, and the part after the separator. If the '
+ 'separator is not\n'
+ ' found, return a 3-tuple containing two empty strings, '
+ 'followed by\n'
+ ' the string itself.\n'
+ '\n'
+ 'str.rsplit(sep=None, maxsplit=-1)\n'
+ '\n'
+ ' Return a list of the words in the string, using *sep* '
+ 'as the\n'
+ ' delimiter string. If *maxsplit* is given, at most '
+ '*maxsplit* splits\n'
+ ' are done, the *rightmost* ones. If *sep* is not '
+ 'specified or\n'
+ ' "None", any whitespace string is a separator. Except '
+ 'for splitting\n'
+ ' from the right, "rsplit()" behaves like "split()" which '
+ 'is\n'
+ ' described in detail below.\n'
+ '\n'
+ 'str.rstrip([chars])\n'
+ '\n'
+ ' Return a copy of the string with trailing characters '
+ 'removed. The\n'
+ ' *chars* argument is a string specifying the set of '
+ 'characters to be\n'
+ ' removed. If omitted or "None", the *chars* argument '
+ 'defaults to\n'
+ ' removing whitespace. The *chars* argument is not a '
+ 'suffix; rather,\n'
+ ' all combinations of its values are stripped:\n'
+ '\n'
+ " >>> ' spacious '.rstrip()\n"
+ " ' spacious'\n"
+ " >>> 'mississippi'.rstrip('ipz')\n"
+ " 'mississ'\n"
+ '\n'
+ 'str.split(sep=None, maxsplit=-1)\n'
+ '\n'
+ ' Return a list of the words in the string, using *sep* '
+ 'as the\n'
+ ' delimiter string. If *maxsplit* is given, at most '
+ '*maxsplit*\n'
+ ' splits are done (thus, the list will have at most '
+ '"maxsplit+1"\n'
+ ' elements). If *maxsplit* is not specified or "-1", '
+ 'then there is\n'
+ ' no limit on the number of splits (all possible splits '
+ 'are made).\n'
+ '\n'
+ ' If *sep* is given, consecutive delimiters are not '
+ 'grouped together\n'
+ ' and are deemed to delimit empty strings (for example,\n'
+ ' "\'1,,2\'.split(\',\')" returns "[\'1\', \'\', '
+ '\'2\']"). The *sep* argument\n'
+ ' may consist of multiple characters (for example,\n'
+ ' "\'1<>2<>3\'.split(\'<>\')" returns "[\'1\', \'2\', '
+ '\'3\']"). Splitting an\n'
+ ' empty string with a specified separator returns '
+ '"[\'\']".\n'
+ '\n'
+ ' For example:\n'
+ '\n'
+ " >>> '1,2,3'.split(',')\n"
+ " ['1', '2', '3']\n"
+ " >>> '1,2,3'.split(',', maxsplit=1)\n"
+ " ['1', '2,3']\n"
+ " >>> '1,2,,3,'.split(',')\n"
+ " ['1', '2', '', '3', '']\n"
+ '\n'
+ ' If *sep* is not specified or is "None", a different '
+ 'splitting\n'
+ ' algorithm is applied: runs of consecutive whitespace '
+ 'are regarded\n'
+ ' as a single separator, and the result will contain no '
+ 'empty strings\n'
+ ' at the start or end if the string has leading or '
+ 'trailing\n'
+ ' whitespace. Consequently, splitting an empty string or '
+ 'a string\n'
+ ' consisting of just whitespace with a "None" separator '
+ 'returns "[]".\n'
+ '\n'
+ ' For example:\n'
+ '\n'
+ " >>> '1 2 3'.split()\n"
+ " ['1', '2', '3']\n"
+ " >>> '1 2 3'.split(maxsplit=1)\n"
+ " ['1', '2 3']\n"
+ " >>> ' 1 2 3 '.split()\n"
+ " ['1', '2', '3']\n"
+ '\n'
+ 'str.splitlines([keepends])\n'
+ '\n'
+ ' Return a list of the lines in the string, breaking at '
+ 'line\n'
+ ' boundaries. Line breaks are not included in the '
+ 'resulting list\n'
+ ' unless *keepends* is given and true.\n'
+ '\n'
+ ' This method splits on the following line boundaries. '
+ 'In\n'
+ ' particular, the boundaries are a superset of *universal '
+ 'newlines*.\n'
+ '\n'
+ ' '
+ '+-------------------------+-------------------------------+\n'
+ ' | Representation | '
+ 'Description |\n'
+ ' '
+ '+=========================+===============================+\n'
+ ' | "\\n" | Line '
+ 'Feed |\n'
+ ' '
+ '+-------------------------+-------------------------------+\n'
+ ' | "\\r" | Carriage '
+ 'Return |\n'
+ ' '
+ '+-------------------------+-------------------------------+\n'
+ ' | "\\r\\n" | Carriage Return + Line '
+ 'Feed |\n'
+ ' '
+ '+-------------------------+-------------------------------+\n'
+ ' | "\\v" or "\\x0b" | Line '
+ 'Tabulation |\n'
+ ' '
+ '+-------------------------+-------------------------------+\n'
+ ' | "\\f" or "\\x0c" | Form '
+ 'Feed |\n'
+ ' '
+ '+-------------------------+-------------------------------+\n'
+ ' | "\\x1c" | File '
+ 'Separator |\n'
+ ' '
+ '+-------------------------+-------------------------------+\n'
+ ' | "\\x1d" | Group '
+ 'Separator |\n'
+ ' '
+ '+-------------------------+-------------------------------+\n'
+ ' | "\\x1e" | Record '
+ 'Separator |\n'
+ ' '
+ '+-------------------------+-------------------------------+\n'
+ ' | "\\x85" | Next Line (C1 Control '
+ 'Code) |\n'
+ ' '
+ '+-------------------------+-------------------------------+\n'
+ ' | "\\u2028" | Line '
+ 'Separator |\n'
+ ' '
+ '+-------------------------+-------------------------------+\n'
+ ' | "\\u2029" | Paragraph '
+ 'Separator |\n'
+ ' '
+ '+-------------------------+-------------------------------+\n'
+ '\n'
+ ' Changed in version 3.2: "\\v" and "\\f" added to list '
+ 'of line\n'
+ ' boundaries.\n'
+ '\n'
+ ' For example:\n'
+ '\n'
+ " >>> 'ab c\\n\\nde fg\\rkl\\r\\n'.splitlines()\n"
+ " ['ab c', '', 'de fg', 'kl']\n"
+ " >>> 'ab c\\n\\nde "
+ "fg\\rkl\\r\\n'.splitlines(keepends=True)\n"
+ " ['ab c\\n', '\\n', 'de fg\\r', 'kl\\r\\n']\n"
+ '\n'
+ ' Unlike "split()" when a delimiter string *sep* is '
+ 'given, this\n'
+ ' method returns an empty list for the empty string, and '
+ 'a terminal\n'
+ ' line break does not result in an extra line:\n'
+ '\n'
+ ' >>> "".splitlines()\n'
+ ' []\n'
+ ' >>> "One line\\n".splitlines()\n'
+ " ['One line']\n"
+ '\n'
+ ' For comparison, "split(\'\\n\')" gives:\n'
+ '\n'
+ " >>> ''.split('\\n')\n"
+ " ['']\n"
+ " >>> 'Two lines\\n'.split('\\n')\n"
+ " ['Two lines', '']\n"
+ '\n'
+ 'str.startswith(prefix[, start[, end]])\n'
+ '\n'
+ ' Return "True" if string starts with the *prefix*, '
+ 'otherwise return\n'
+ ' "False". *prefix* can also be a tuple of prefixes to '
+ 'look for.\n'
+ ' With optional *start*, test string beginning at that '
+ 'position.\n'
+ ' With optional *end*, stop comparing string at that '
+ 'position.\n'
+ '\n'
+ 'str.strip([chars])\n'
+ '\n'
+ ' Return a copy of the string with the leading and '
+ 'trailing\n'
+ ' characters removed. The *chars* argument is a string '
+ 'specifying the\n'
+ ' set of characters to be removed. If omitted or "None", '
+ 'the *chars*\n'
+ ' argument defaults to removing whitespace. The *chars* '
+ 'argument is\n'
+ ' not a prefix or suffix; rather, all combinations of its '
+ 'values are\n'
+ ' stripped:\n'
+ '\n'
+ " >>> ' spacious '.strip()\n"
+ " 'spacious'\n"
+ " >>> 'www.example.com'.strip('cmowz.')\n"
+ " 'example'\n"
+ '\n'
+ ' The outermost leading and trailing *chars* argument '
+ 'values are\n'
+ ' stripped from the string. Characters are removed from '
+ 'the leading\n'
+ ' end until reaching a string character that is not '
+ 'contained in the\n'
+ ' set of characters in *chars*. A similar action takes '
+ 'place on the\n'
+ ' trailing end. For example:\n'
+ '\n'
+ " >>> comment_string = '#....... Section 3.2.1 Issue "
+ "#32 .......'\n"
+ " >>> comment_string.strip('.#! ')\n"
+ " 'Section 3.2.1 Issue #32'\n"
+ '\n'
+ 'str.swapcase()\n'
+ '\n'
+ ' Return a copy of the string with uppercase characters '
+ 'converted to\n'
+ ' lowercase and vice versa. Note that it is not '
+ 'necessarily true that\n'
+ ' "s.swapcase().swapcase() == s".\n'
+ '\n'
+ 'str.title()\n'
+ '\n'
+ ' Return a titlecased version of the string where words '
+ 'start with an\n'
+ ' uppercase character and the remaining characters are '
+ 'lowercase.\n'
+ '\n'
+ ' For example:\n'
+ '\n'
+ " >>> 'Hello world'.title()\n"
+ " 'Hello World'\n"
+ '\n'
+ ' The algorithm uses a simple language-independent '
+ 'definition of a\n'
+ ' word as groups of consecutive letters. The definition '
+ 'works in\n'
+ ' many contexts but it means that apostrophes in '
+ 'contractions and\n'
+ ' possessives form word boundaries, which may not be the '
+ 'desired\n'
+ ' result:\n'
+ '\n'
+ ' >>> "they\'re bill\'s friends from the UK".title()\n'
+ ' "They\'Re Bill\'S Friends From The Uk"\n'
+ '\n'
+ ' A workaround for apostrophes can be constructed using '
+ 'regular\n'
+ ' expressions:\n'
+ '\n'
+ ' >>> import re\n'
+ ' >>> def titlecase(s):\n'
+ ' ... return re.sub(r"[A-Za-z]+(\'[A-Za-z]+)?",\n'
+ ' ... lambda mo: '
+ 'mo.group(0)[0].upper() +\n'
+ ' ... '
+ 'mo.group(0)[1:].lower(),\n'
+ ' ... s)\n'
+ ' ...\n'
+ ' >>> titlecase("they\'re bill\'s friends.")\n'
+ ' "They\'re Bill\'s Friends."\n'
+ '\n'
+ 'str.translate(table)\n'
+ '\n'
+ ' Return a copy of the string in which each character has '
+ 'been mapped\n'
+ ' through the given translation table. The table must be '
+ 'an object\n'
+ ' that implements indexing via "__getitem__()", typically '
+ 'a *mapping*\n'
+ ' or *sequence*. When indexed by a Unicode ordinal (an '
+ 'integer), the\n'
+ ' table object can do any of the following: return a '
+ 'Unicode ordinal\n'
+ ' or a string, to map the character to one or more other '
+ 'characters;\n'
+ ' return "None", to delete the character from the return '
+ 'string; or\n'
+ ' raise a "LookupError" exception, to map the character '
+ 'to itself.\n'
+ '\n'
+ ' You can use "str.maketrans()" to create a translation '
+ 'map from\n'
+ ' character-to-character mappings in different formats.\n'
+ '\n'
+ ' See also the "codecs" module for a more flexible '
+ 'approach to custom\n'
+ ' character mappings.\n'
+ '\n'
+ 'str.upper()\n'
+ '\n'
+ ' Return a copy of the string with all the cased '
+ 'characters [4]\n'
+ ' converted to uppercase. Note that '
+ '"str.upper().isupper()" might be\n'
+ ' "False" if "s" contains uncased characters or if the '
+ 'Unicode\n'
+ ' category of the resulting character(s) is not "Lu" '
+ '(Letter,\n'
+ ' uppercase), but e.g. "Lt" (Letter, titlecase).\n'
+ '\n'
+ ' The uppercasing algorithm used is described in section '
+ '3.13 of the\n'
+ ' Unicode Standard.\n'
+ '\n'
+ 'str.zfill(width)\n'
+ '\n'
+ ' Return a copy of the string left filled with ASCII '
+ '"\'0\'" digits to\n'
+ ' make a string of length *width*. A leading sign prefix\n'
+ ' ("\'+\'"/"\'-\'") is handled by inserting the padding '
+ '*after* the sign\n'
+ ' character rather than before. The original string is '
+ 'returned if\n'
+ ' *width* is less than or equal to "len(s)".\n'
+ '\n'
+ ' For example:\n'
+ '\n'
+ ' >>> "42".zfill(5)\n'
+ " '00042'\n"
+ ' >>> "-42".zfill(5)\n'
+ " '-0042'\n",
+ 'strings': '\n'
+ 'String and Bytes literals\n'
+ '*************************\n'
+ '\n'
+ 'String literals are described by the following lexical '
+ 'definitions:\n'
+ '\n'
+ ' stringliteral ::= [stringprefix](shortstring | longstring)\n'
+ ' stringprefix ::= "r" | "u" | "R" | "U" | "f" | "F"\n'
+ ' | "fr" | "Fr" | "fR" | "FR" | "rf" | "rF" | '
+ '"Rf" | "RF"\n'
+ ' shortstring ::= "\'" shortstringitem* "\'" | \'"\' '
+ 'shortstringitem* \'"\'\n'
+ ' longstring ::= "\'\'\'" longstringitem* "\'\'\'" | '
+ '\'"""\' longstringitem* \'"""\'\n'
+ ' shortstringitem ::= shortstringchar | stringescapeseq\n'
+ ' longstringitem ::= longstringchar | stringescapeseq\n'
+ ' shortstringchar ::= <any source character except "\\" or '
+ 'newline or the quote>\n'
+ ' longstringchar ::= <any source character except "\\">\n'
+ ' stringescapeseq ::= "\\" <any source character>\n'
+ '\n'
+ ' bytesliteral ::= bytesprefix(shortbytes | longbytes)\n'
+ ' bytesprefix ::= "b" | "B" | "br" | "Br" | "bR" | "BR" | '
+ '"rb" | "rB" | "Rb" | "RB"\n'
+ ' shortbytes ::= "\'" shortbytesitem* "\'" | \'"\' '
+ 'shortbytesitem* \'"\'\n'
+ ' longbytes ::= "\'\'\'" longbytesitem* "\'\'\'" | \'"""\' '
+ 'longbytesitem* \'"""\'\n'
+ ' shortbytesitem ::= shortbyteschar | bytesescapeseq\n'
+ ' longbytesitem ::= longbyteschar | bytesescapeseq\n'
+ ' shortbyteschar ::= <any ASCII character except "\\" or newline '
+ 'or the quote>\n'
+ ' longbyteschar ::= <any ASCII character except "\\">\n'
+ ' bytesescapeseq ::= "\\" <any ASCII character>\n'
+ '\n'
+ 'One syntactic restriction not indicated by these productions is '
+ 'that\n'
+ 'whitespace is not allowed between the "stringprefix" or '
+ '"bytesprefix"\n'
+ 'and the rest of the literal. The source character set is defined '
+ 'by\n'
+ 'the encoding declaration; it is UTF-8 if no encoding declaration '
+ 'is\n'
+ 'given in the source file; see section Encoding declarations.\n'
+ '\n'
+ 'In plain English: Both types of literals can be enclosed in '
+ 'matching\n'
+ 'single quotes ("\'") or double quotes ("""). They can also be '
+ 'enclosed\n'
+ 'in matching groups of three single or double quotes (these are\n'
+ 'generally referred to as *triple-quoted strings*). The '
+ 'backslash\n'
+ '("\\") character is used to escape characters that otherwise have '
+ 'a\n'
+ 'special meaning, such as newline, backslash itself, or the quote\n'
+ 'character.\n'
+ '\n'
+ 'Bytes literals are always prefixed with "\'b\'" or "\'B\'"; they '
+ 'produce\n'
+ 'an instance of the "bytes" type instead of the "str" type. They '
+ 'may\n'
+ 'only contain ASCII characters; bytes with a numeric value of 128 '
+ 'or\n'
+ 'greater must be expressed with escapes.\n'
+ '\n'
+ 'As of Python 3.3 it is possible again to prefix string literals '
+ 'with a\n'
+ '"u" prefix to simplify maintenance of dual 2.x and 3.x '
+ 'codebases.\n'
+ '\n'
+ 'Both string and bytes literals may optionally be prefixed with a\n'
+ 'letter "\'r\'" or "\'R\'"; such strings are called *raw strings* '
+ 'and treat\n'
+ 'backslashes as literal characters. As a result, in string '
+ 'literals,\n'
+ '"\'\\U\'" and "\'\\u\'" escapes in raw strings are not treated '
+ 'specially.\n'
+ "Given that Python 2.x's raw unicode literals behave differently "
+ 'than\n'
+ 'Python 3.x\'s the "\'ur\'" syntax is not supported.\n'
+ '\n'
+ 'New in version 3.3: The "\'rb\'" prefix of raw bytes literals has '
+ 'been\n'
+ 'added as a synonym of "\'br\'".\n'
+ '\n'
+ 'New in version 3.3: Support for the unicode legacy literal\n'
+ '("u\'value\'") was reintroduced to simplify the maintenance of '
+ 'dual\n'
+ 'Python 2.x and 3.x codebases. See **PEP 414** for more '
+ 'information.\n'
+ '\n'
+ 'A string literal with "\'f\'" or "\'F\'" in its prefix is a '
+ '*formatted\n'
+ 'string literal*; see Formatted string literals. The "\'f\'" may '
+ 'be\n'
+ 'combined with "\'r\'", but not with "\'b\'" or "\'u\'", therefore '
+ 'raw\n'
+ 'formatted strings are possible, but formatted bytes literals are '
+ 'not.\n'
+ '\n'
+ 'In triple-quoted literals, unescaped newlines and quotes are '
+ 'allowed\n'
+ '(and are retained), except that three unescaped quotes in a row\n'
+ 'terminate the literal. (A "quote" is the character used to open '
+ 'the\n'
+ 'literal, i.e. either "\'" or """.)\n'
+ '\n'
+ 'Unless an "\'r\'" or "\'R\'" prefix is present, escape sequences '
+ 'in string\n'
+ 'and bytes literals are interpreted according to rules similar to '
+ 'those\n'
+ 'used by Standard C. The recognized escape sequences are:\n'
+ '\n'
+ '+-------------------+-----------------------------------+---------+\n'
+ '| Escape Sequence | Meaning | Notes '
+ '|\n'
+ '+===================+===================================+=========+\n'
+ '| "\\newline" | Backslash and newline ignored '
+ '| |\n'
+ '+-------------------+-----------------------------------+---------+\n'
+ '| "\\\\" | Backslash ("\\") '
+ '| |\n'
+ '+-------------------+-----------------------------------+---------+\n'
+ '| "\\\'" | Single quote ("\'") '
+ '| |\n'
+ '+-------------------+-----------------------------------+---------+\n'
+ '| "\\"" | Double quote (""") '
+ '| |\n'
+ '+-------------------+-----------------------------------+---------+\n'
+ '| "\\a" | ASCII Bell (BEL) '
+ '| |\n'
+ '+-------------------+-----------------------------------+---------+\n'
+ '| "\\b" | ASCII Backspace (BS) '
+ '| |\n'
+ '+-------------------+-----------------------------------+---------+\n'
+ '| "\\f" | ASCII Formfeed (FF) '
+ '| |\n'
+ '+-------------------+-----------------------------------+---------+\n'
+ '| "\\n" | ASCII Linefeed (LF) '
+ '| |\n'
+ '+-------------------+-----------------------------------+---------+\n'
+ '| "\\r" | ASCII Carriage Return (CR) '
+ '| |\n'
+ '+-------------------+-----------------------------------+---------+\n'
+ '| "\\t" | ASCII Horizontal Tab (TAB) '
+ '| |\n'
+ '+-------------------+-----------------------------------+---------+\n'
+ '| "\\v" | ASCII Vertical Tab (VT) '
+ '| |\n'
+ '+-------------------+-----------------------------------+---------+\n'
+ '| "\\ooo" | Character with octal value *ooo* | '
+ '(1,3) |\n'
+ '+-------------------+-----------------------------------+---------+\n'
+ '| "\\xhh" | Character with hex value *hh* | '
+ '(2,3) |\n'
+ '+-------------------+-----------------------------------+---------+\n'
+ '\n'
+ 'Escape sequences only recognized in string literals are:\n'
+ '\n'
+ '+-------------------+-----------------------------------+---------+\n'
+ '| Escape Sequence | Meaning | Notes '
+ '|\n'
+ '+===================+===================================+=========+\n'
+ '| "\\N{name}" | Character named *name* in the | '
+ '(4) |\n'
+ '| | Unicode database | '
+ '|\n'
+ '+-------------------+-----------------------------------+---------+\n'
+ '| "\\uxxxx" | Character with 16-bit hex value | '
+ '(5) |\n'
+ '| | *xxxx* | '
+ '|\n'
+ '+-------------------+-----------------------------------+---------+\n'
+ '| "\\Uxxxxxxxx" | Character with 32-bit hex value | '
+ '(6) |\n'
+ '| | *xxxxxxxx* | '
+ '|\n'
+ '+-------------------+-----------------------------------+---------+\n'
+ '\n'
+ 'Notes:\n'
+ '\n'
+ '1. As in Standard C, up to three octal digits are accepted.\n'
+ '\n'
+ '2. Unlike in Standard C, exactly two hex digits are required.\n'
+ '\n'
+ '3. In a bytes literal, hexadecimal and octal escapes denote the\n'
+ ' byte with the given value. In a string literal, these escapes\n'
+ ' denote a Unicode character with the given value.\n'
+ '\n'
+ '4. Changed in version 3.3: Support for name aliases [1] has been\n'
+ ' added.\n'
+ '\n'
+ '5. Exactly four hex digits are required.\n'
+ '\n'
+ '6. Any Unicode character can be encoded this way. Exactly eight\n'
+ ' hex digits are required.\n'
+ '\n'
+ 'Unlike Standard C, all unrecognized escape sequences are left in '
+ 'the\n'
+ 'string unchanged, i.e., *the backslash is left in the result*. '
+ '(This\n'
+ 'behavior is useful when debugging: if an escape sequence is '
+ 'mistyped,\n'
+ 'the resulting output is more easily recognized as broken.) It is '
+ 'also\n'
+ 'important to note that the escape sequences only recognized in '
+ 'string\n'
+ 'literals fall into the category of unrecognized escapes for '
+ 'bytes\n'
+ 'literals.\n'
+ '\n'
+ ' Changed in version 3.6: Unrecognized escape sequences produce '
+ 'a\n'
+ ' DeprecationWarning. In some future version of Python they '
+ 'will be\n'
+ ' a SyntaxError.\n'
+ '\n'
+ 'Even in a raw literal, quotes can be escaped with a backslash, '
+ 'but the\n'
+ 'backslash remains in the result; for example, "r"\\""" is a '
+ 'valid\n'
+ 'string literal consisting of two characters: a backslash and a '
+ 'double\n'
+ 'quote; "r"\\"" is not a valid string literal (even a raw string '
+ 'cannot\n'
+ 'end in an odd number of backslashes). Specifically, *a raw '
+ 'literal\n'
+ 'cannot end in a single backslash* (since the backslash would '
+ 'escape\n'
+ 'the following quote character). Note also that a single '
+ 'backslash\n'
+ 'followed by a newline is interpreted as those two characters as '
+ 'part\n'
+ 'of the literal, *not* as a line continuation.\n',
+ 'subscriptions': '\n'
+ 'Subscriptions\n'
+ '*************\n'
+ '\n'
+ 'A subscription selects an item of a sequence (string, tuple '
+ 'or list)\n'
+ 'or mapping (dictionary) object:\n'
+ '\n'
+ ' subscription ::= primary "[" expression_list "]"\n'
+ '\n'
+ 'The primary must evaluate to an object that supports '
+ 'subscription\n'
+ '(lists or dictionaries for example). User-defined objects '
+ 'can support\n'
+ 'subscription by defining a "__getitem__()" method.\n'
+ '\n'
+ 'For built-in objects, there are two types of objects that '
+ 'support\n'
+ 'subscription:\n'
+ '\n'
+ 'If the primary is a mapping, the expression list must '
+ 'evaluate to an\n'
+ 'object whose value is one of the keys of the mapping, and '
+ 'the\n'
+ 'subscription selects the value in the mapping that '
+ 'corresponds to that\n'
+ 'key. (The expression list is a tuple except if it has '
+ 'exactly one\n'
+ 'item.)\n'
+ '\n'
+ 'If the primary is a sequence, the expression (list) must '
+ 'evaluate to\n'
+ 'an integer or a slice (as discussed in the following '
+ 'section).\n'
+ '\n'
+ 'The formal syntax makes no special provision for negative '
+ 'indices in\n'
+ 'sequences; however, built-in sequences all provide a '
+ '"__getitem__()"\n'
+ 'method that interprets negative indices by adding the '
+ 'length of the\n'
+ 'sequence to the index (so that "x[-1]" selects the last '
+ 'item of "x").\n'
+ 'The resulting value must be a nonnegative integer less than '
+ 'the number\n'
+ 'of items in the sequence, and the subscription selects the '
+ 'item whose\n'
+ 'index is that value (counting from zero). Since the support '
+ 'for\n'
+ "negative indices and slicing occurs in the object's "
+ '"__getitem__()"\n'
+ 'method, subclasses overriding this method will need to '
+ 'explicitly add\n'
+ 'that support.\n'
+ '\n'
+ "A string's items are characters. A character is not a "
+ 'separate data\n'
+ 'type but a string of exactly one character.\n',
+ 'truth': '\n'
+ 'Truth Value Testing\n'
+ '*******************\n'
+ '\n'
+ 'Any object can be tested for truth value, for use in an "if" or\n'
+ '"while" condition or as operand of the Boolean operations below. '
+ 'The\n'
+ 'following values are considered false:\n'
+ '\n'
+ '* "None"\n'
+ '\n'
+ '* "False"\n'
+ '\n'
+ '* zero of any numeric type, for example, "0", "0.0", "0j".\n'
+ '\n'
+ '* any empty sequence, for example, "\'\'", "()", "[]".\n'
+ '\n'
+ '* any empty mapping, for example, "{}".\n'
+ '\n'
+ '* instances of user-defined classes, if the class defines a\n'
+ ' "__bool__()" or "__len__()" method, when that method returns the\n'
+ ' integer zero or "bool" value "False". [1]\n'
+ '\n'
+ 'All other values are considered true --- so objects of many types '
+ 'are\n'
+ 'always true.\n'
+ '\n'
+ 'Operations and built-in functions that have a Boolean result '
+ 'always\n'
+ 'return "0" or "False" for false and "1" or "True" for true, unless\n'
+ 'otherwise stated. (Important exception: the Boolean operations '
+ '"or"\n'
+ 'and "and" always return one of their operands.)\n',
+ 'try': '\n'
+ 'The "try" statement\n'
+ '*******************\n'
+ '\n'
+ 'The "try" statement specifies exception handlers and/or cleanup code\n'
+ 'for a group of statements:\n'
+ '\n'
+ ' try_stmt ::= try1_stmt | try2_stmt\n'
+ ' try1_stmt ::= "try" ":" suite\n'
+ ' ("except" [expression ["as" identifier]] ":" '
+ 'suite)+\n'
+ ' ["else" ":" suite]\n'
+ ' ["finally" ":" suite]\n'
+ ' try2_stmt ::= "try" ":" suite\n'
+ ' "finally" ":" suite\n'
+ '\n'
+ 'The "except" clause(s) specify one or more exception handlers. When '
+ 'no\n'
+ 'exception occurs in the "try" clause, no exception handler is\n'
+ 'executed. When an exception occurs in the "try" suite, a search for '
+ 'an\n'
+ 'exception handler is started. This search inspects the except '
+ 'clauses\n'
+ 'in turn until one is found that matches the exception. An '
+ 'expression-\n'
+ 'less except clause, if present, must be last; it matches any\n'
+ 'exception. For an except clause with an expression, that expression\n'
+ 'is evaluated, and the clause matches the exception if the resulting\n'
+ 'object is "compatible" with the exception. An object is compatible\n'
+ 'with an exception if it is the class or a base class of the '
+ 'exception\n'
+ 'object or a tuple containing an item compatible with the exception.\n'
+ '\n'
+ 'If no except clause matches the exception, the search for an '
+ 'exception\n'
+ 'handler continues in the surrounding code and on the invocation '
+ 'stack.\n'
+ '[1]\n'
+ '\n'
+ 'If the evaluation of an expression in the header of an except clause\n'
+ 'raises an exception, the original search for a handler is canceled '
+ 'and\n'
+ 'a search starts for the new exception in the surrounding code and on\n'
+ 'the call stack (it is treated as if the entire "try" statement '
+ 'raised\n'
+ 'the exception).\n'
+ '\n'
+ 'When a matching except clause is found, the exception is assigned to\n'
+ 'the target specified after the "as" keyword in that except clause, '
+ 'if\n'
+ "present, and the except clause's suite is executed. All except\n"
+ 'clauses must have an executable block. When the end of this block '
+ 'is\n'
+ 'reached, execution continues normally after the entire try '
+ 'statement.\n'
+ '(This means that if two nested handlers exist for the same '
+ 'exception,\n'
+ 'and the exception occurs in the try clause of the inner handler, the\n'
+ 'outer handler will not handle the exception.)\n'
+ '\n'
+ 'When an exception has been assigned using "as target", it is cleared\n'
+ 'at the end of the except clause. This is as if\n'
+ '\n'
+ ' except E as N:\n'
+ ' foo\n'
+ '\n'
+ 'was translated to\n'
+ '\n'
+ ' except E as N:\n'
+ ' try:\n'
+ ' foo\n'
+ ' finally:\n'
+ ' del N\n'
+ '\n'
+ 'This means the exception must be assigned to a different name to be\n'
+ 'able to refer to it after the except clause. Exceptions are cleared\n'
+ 'because with the traceback attached to them, they form a reference\n'
+ 'cycle with the stack frame, keeping all locals in that frame alive\n'
+ 'until the next garbage collection occurs.\n'
+ '\n'
+ "Before an except clause's suite is executed, details about the\n"
+ 'exception are stored in the "sys" module and can be accessed via\n'
+ '"sys.exc_info()". "sys.exc_info()" returns a 3-tuple consisting of '
+ 'the\n'
+ 'exception class, the exception instance and a traceback object (see\n'
+ 'section The standard type hierarchy) identifying the point in the\n'
+ 'program where the exception occurred. "sys.exc_info()" values are\n'
+ 'restored to their previous values (before the call) when returning\n'
+ 'from a function that handled an exception.\n'
+ '\n'
+ 'The optional "else" clause is executed if and when control flows off\n'
+ 'the end of the "try" clause. [2] Exceptions in the "else" clause are\n'
+ 'not handled by the preceding "except" clauses.\n'
+ '\n'
+ 'If "finally" is present, it specifies a \'cleanup\' handler. The '
+ '"try"\n'
+ 'clause is executed, including any "except" and "else" clauses. If '
+ 'an\n'
+ 'exception occurs in any of the clauses and is not handled, the\n'
+ 'exception is temporarily saved. The "finally" clause is executed. '
+ 'If\n'
+ 'there is a saved exception it is re-raised at the end of the '
+ '"finally"\n'
+ 'clause. If the "finally" clause raises another exception, the saved\n'
+ 'exception is set as the context of the new exception. If the '
+ '"finally"\n'
+ 'clause executes a "return" or "break" statement, the saved exception\n'
+ 'is discarded:\n'
+ '\n'
+ ' >>> def f():\n'
+ ' ... try:\n'
+ ' ... 1/0\n'
+ ' ... finally:\n'
+ ' ... return 42\n'
+ ' ...\n'
+ ' >>> f()\n'
+ ' 42\n'
+ '\n'
+ 'The exception information is not available to the program during\n'
+ 'execution of the "finally" clause.\n'
+ '\n'
+ 'When a "return", "break" or "continue" statement is executed in the\n'
+ '"try" suite of a "try"..."finally" statement, the "finally" clause '
+ 'is\n'
+ 'also executed \'on the way out.\' A "continue" statement is illegal '
+ 'in\n'
+ 'the "finally" clause. (The reason is a problem with the current\n'
+ 'implementation --- this restriction may be lifted in the future).\n'
+ '\n'
+ 'The return value of a function is determined by the last "return"\n'
+ 'statement executed. Since the "finally" clause always executes, a\n'
+ '"return" statement executed in the "finally" clause will always be '
+ 'the\n'
+ 'last one executed:\n'
+ '\n'
+ ' >>> def foo():\n'
+ ' ... try:\n'
+ " ... return 'try'\n"
+ ' ... finally:\n'
+ " ... return 'finally'\n"
+ ' ...\n'
+ ' >>> foo()\n'
+ " 'finally'\n"
+ '\n'
+ 'Additional information on exceptions can be found in section\n'
+ 'Exceptions, and information on using the "raise" statement to '
+ 'generate\n'
+ 'exceptions may be found in section The raise statement.\n',
+ 'types': '\n'
+ 'The standard type hierarchy\n'
+ '***************************\n'
+ '\n'
+ 'Below is a list of the types that are built into Python. '
+ 'Extension\n'
+ 'modules (written in C, Java, or other languages, depending on the\n'
+ 'implementation) can define additional types. Future versions of\n'
+ 'Python may add types to the type hierarchy (e.g., rational '
+ 'numbers,\n'
+ 'efficiently stored arrays of integers, etc.), although such '
+ 'additions\n'
+ 'will often be provided via the standard library instead.\n'
+ '\n'
+ 'Some of the type descriptions below contain a paragraph listing\n'
+ "'special attributes.' These are attributes that provide access to "
+ 'the\n'
+ 'implementation and are not intended for general use. Their '
+ 'definition\n'
+ 'may change in the future.\n'
+ '\n'
+ 'None\n'
+ ' This type has a single value. There is a single object with '
+ 'this\n'
+ ' value. This object is accessed through the built-in name "None". '
+ 'It\n'
+ ' is used to signify the absence of a value in many situations, '
+ 'e.g.,\n'
+ " it is returned from functions that don't explicitly return\n"
+ ' anything. Its truth value is false.\n'
+ '\n'
+ 'NotImplemented\n'
+ ' This type has a single value. There is a single object with '
+ 'this\n'
+ ' value. This object is accessed through the built-in name\n'
+ ' "NotImplemented". Numeric methods and rich comparison methods\n'
+ ' should return this value if they do not implement the operation '
+ 'for\n'
+ ' the operands provided. (The interpreter will then try the\n'
+ ' reflected operation, or some other fallback, depending on the\n'
+ ' operator.) Its truth value is true.\n'
+ '\n'
+ ' See Implementing the arithmetic operations for more details.\n'
+ '\n'
+ 'Ellipsis\n'
+ ' This type has a single value. There is a single object with '
+ 'this\n'
+ ' value. This object is accessed through the literal "..." or the\n'
+ ' built-in name "Ellipsis". Its truth value is true.\n'
+ '\n'
+ '"numbers.Number"\n'
+ ' These are created by numeric literals and returned as results '
+ 'by\n'
+ ' arithmetic operators and arithmetic built-in functions. '
+ 'Numeric\n'
+ ' objects are immutable; once created their value never changes.\n'
+ ' Python numbers are of course strongly related to mathematical\n'
+ ' numbers, but subject to the limitations of numerical '
+ 'representation\n'
+ ' in computers.\n'
+ '\n'
+ ' Python distinguishes between integers, floating point numbers, '
+ 'and\n'
+ ' complex numbers:\n'
+ '\n'
+ ' "numbers.Integral"\n'
+ ' These represent elements from the mathematical set of '
+ 'integers\n'
+ ' (positive and negative).\n'
+ '\n'
+ ' There are two types of integers:\n'
+ '\n'
+ ' Integers ("int")\n'
+ '\n'
+ ' These represent numbers in an unlimited range, subject to\n'
+ ' available (virtual) memory only. For the purpose of '
+ 'shift\n'
+ ' and mask operations, a binary representation is assumed, '
+ 'and\n'
+ " negative numbers are represented in a variant of 2's\n"
+ ' complement which gives the illusion of an infinite string '
+ 'of\n'
+ ' sign bits extending to the left.\n'
+ '\n'
+ ' Booleans ("bool")\n'
+ ' These represent the truth values False and True. The two\n'
+ ' objects representing the values "False" and "True" are '
+ 'the\n'
+ ' only Boolean objects. The Boolean type is a subtype of '
+ 'the\n'
+ ' integer type, and Boolean values behave like the values 0 '
+ 'and\n'
+ ' 1, respectively, in almost all contexts, the exception '
+ 'being\n'
+ ' that when converted to a string, the strings ""False"" or\n'
+ ' ""True"" are returned, respectively.\n'
+ '\n'
+ ' The rules for integer representation are intended to give '
+ 'the\n'
+ ' most meaningful interpretation of shift and mask operations\n'
+ ' involving negative integers.\n'
+ '\n'
+ ' "numbers.Real" ("float")\n'
+ ' These represent machine-level double precision floating '
+ 'point\n'
+ ' numbers. You are at the mercy of the underlying machine\n'
+ ' architecture (and C or Java implementation) for the accepted\n'
+ ' range and handling of overflow. Python does not support '
+ 'single-\n'
+ ' precision floating point numbers; the savings in processor '
+ 'and\n'
+ ' memory usage that are usually the reason for using these are\n'
+ ' dwarfed by the overhead of using objects in Python, so there '
+ 'is\n'
+ ' no reason to complicate the language with two kinds of '
+ 'floating\n'
+ ' point numbers.\n'
+ '\n'
+ ' "numbers.Complex" ("complex")\n'
+ ' These represent complex numbers as a pair of machine-level\n'
+ ' double precision floating point numbers. The same caveats '
+ 'apply\n'
+ ' as for floating point numbers. The real and imaginary parts '
+ 'of a\n'
+ ' complex number "z" can be retrieved through the read-only\n'
+ ' attributes "z.real" and "z.imag".\n'
+ '\n'
+ 'Sequences\n'
+ ' These represent finite ordered sets indexed by non-negative\n'
+ ' numbers. The built-in function "len()" returns the number of '
+ 'items\n'
+ ' of a sequence. When the length of a sequence is *n*, the index '
+ 'set\n'
+ ' contains the numbers 0, 1, ..., *n*-1. Item *i* of sequence *a* '
+ 'is\n'
+ ' selected by "a[i]".\n'
+ '\n'
+ ' Sequences also support slicing: "a[i:j]" selects all items with\n'
+ ' index *k* such that *i* "<=" *k* "<" *j*. When used as an\n'
+ ' expression, a slice is a sequence of the same type. This '
+ 'implies\n'
+ ' that the index set is renumbered so that it starts at 0.\n'
+ '\n'
+ ' Some sequences also support "extended slicing" with a third '
+ '"step"\n'
+ ' parameter: "a[i:j:k]" selects all items of *a* with index *x* '
+ 'where\n'
+ ' "x = i + n*k", *n* ">=" "0" and *i* "<=" *x* "<" *j*.\n'
+ '\n'
+ ' Sequences are distinguished according to their mutability:\n'
+ '\n'
+ ' Immutable sequences\n'
+ ' An object of an immutable sequence type cannot change once it '
+ 'is\n'
+ ' created. (If the object contains references to other '
+ 'objects,\n'
+ ' these other objects may be mutable and may be changed; '
+ 'however,\n'
+ ' the collection of objects directly referenced by an '
+ 'immutable\n'
+ ' object cannot change.)\n'
+ '\n'
+ ' The following types are immutable sequences:\n'
+ '\n'
+ ' Strings\n'
+ ' A string is a sequence of values that represent Unicode '
+ 'code\n'
+ ' points. All the code points in the range "U+0000 - '
+ 'U+10FFFF"\n'
+ " can be represented in a string. Python doesn't have a "
+ '"char"\n'
+ ' type; instead, every code point in the string is '
+ 'represented\n'
+ ' as a string object with length "1". The built-in '
+ 'function\n'
+ ' "ord()" converts a code point from its string form to an\n'
+ ' integer in the range "0 - 10FFFF"; "chr()" converts an\n'
+ ' integer in the range "0 - 10FFFF" to the corresponding '
+ 'length\n'
+ ' "1" string object. "str.encode()" can be used to convert '
+ 'a\n'
+ ' "str" to "bytes" using the given text encoding, and\n'
+ ' "bytes.decode()" can be used to achieve the opposite.\n'
+ '\n'
+ ' Tuples\n'
+ ' The items of a tuple are arbitrary Python objects. Tuples '
+ 'of\n'
+ ' two or more items are formed by comma-separated lists of\n'
+ " expressions. A tuple of one item (a 'singleton') can be\n"
+ ' formed by affixing a comma to an expression (an expression '
+ 'by\n'
+ ' itself does not create a tuple, since parentheses must be\n'
+ ' usable for grouping of expressions). An empty tuple can '
+ 'be\n'
+ ' formed by an empty pair of parentheses.\n'
+ '\n'
+ ' Bytes\n'
+ ' A bytes object is an immutable array. The items are '
+ '8-bit\n'
+ ' bytes, represented by integers in the range 0 <= x < 256.\n'
+ ' Bytes literals (like "b\'abc\'") and the built-in '
+ 'function\n'
+ ' "bytes()" can be used to construct bytes objects. Also,\n'
+ ' bytes objects can be decoded to strings via the '
+ '"decode()"\n'
+ ' method.\n'
+ '\n'
+ ' Mutable sequences\n'
+ ' Mutable sequences can be changed after they are created. '
+ 'The\n'
+ ' subscription and slicing notations can be used as the target '
+ 'of\n'
+ ' assignment and "del" (delete) statements.\n'
+ '\n'
+ ' There are currently two intrinsic mutable sequence types:\n'
+ '\n'
+ ' Lists\n'
+ ' The items of a list are arbitrary Python objects. Lists '
+ 'are\n'
+ ' formed by placing a comma-separated list of expressions '
+ 'in\n'
+ ' square brackets. (Note that there are no special cases '
+ 'needed\n'
+ ' to form lists of length 0 or 1.)\n'
+ '\n'
+ ' Byte Arrays\n'
+ ' A bytearray object is a mutable array. They are created '
+ 'by\n'
+ ' the built-in "bytearray()" constructor. Aside from being\n'
+ ' mutable (and hence unhashable), byte arrays otherwise '
+ 'provide\n'
+ ' the same interface and functionality as immutable bytes\n'
+ ' objects.\n'
+ '\n'
+ ' The extension module "array" provides an additional example '
+ 'of a\n'
+ ' mutable sequence type, as does the "collections" module.\n'
+ '\n'
+ 'Set types\n'
+ ' These represent unordered, finite sets of unique, immutable\n'
+ ' objects. As such, they cannot be indexed by any subscript. '
+ 'However,\n'
+ ' they can be iterated over, and the built-in function "len()"\n'
+ ' returns the number of items in a set. Common uses for sets are '
+ 'fast\n'
+ ' membership testing, removing duplicates from a sequence, and\n'
+ ' computing mathematical operations such as intersection, union,\n'
+ ' difference, and symmetric difference.\n'
+ '\n'
+ ' For set elements, the same immutability rules apply as for\n'
+ ' dictionary keys. Note that numeric types obey the normal rules '
+ 'for\n'
+ ' numeric comparison: if two numbers compare equal (e.g., "1" and\n'
+ ' "1.0"), only one of them can be contained in a set.\n'
+ '\n'
+ ' There are currently two intrinsic set types:\n'
+ '\n'
+ ' Sets\n'
+ ' These represent a mutable set. They are created by the '
+ 'built-in\n'
+ ' "set()" constructor and can be modified afterwards by '
+ 'several\n'
+ ' methods, such as "add()".\n'
+ '\n'
+ ' Frozen sets\n'
+ ' These represent an immutable set. They are created by the\n'
+ ' built-in "frozenset()" constructor. As a frozenset is '
+ 'immutable\n'
+ ' and *hashable*, it can be used again as an element of '
+ 'another\n'
+ ' set, or as a dictionary key.\n'
+ '\n'
+ 'Mappings\n'
+ ' These represent finite sets of objects indexed by arbitrary '
+ 'index\n'
+ ' sets. The subscript notation "a[k]" selects the item indexed by '
+ '"k"\n'
+ ' from the mapping "a"; this can be used in expressions and as '
+ 'the\n'
+ ' target of assignments or "del" statements. The built-in '
+ 'function\n'
+ ' "len()" returns the number of items in a mapping.\n'
+ '\n'
+ ' There is currently a single intrinsic mapping type:\n'
+ '\n'
+ ' Dictionaries\n'
+ ' These represent finite sets of objects indexed by nearly\n'
+ ' arbitrary values. The only types of values not acceptable '
+ 'as\n'
+ ' keys are values containing lists or dictionaries or other\n'
+ ' mutable types that are compared by value rather than by '
+ 'object\n'
+ ' identity, the reason being that the efficient implementation '
+ 'of\n'
+ " dictionaries requires a key's hash value to remain constant.\n"
+ ' Numeric types used for keys obey the normal rules for '
+ 'numeric\n'
+ ' comparison: if two numbers compare equal (e.g., "1" and '
+ '"1.0")\n'
+ ' then they can be used interchangeably to index the same\n'
+ ' dictionary entry.\n'
+ '\n'
+ ' Dictionaries are mutable; they can be created by the "{...}"\n'
+ ' notation (see section Dictionary displays).\n'
+ '\n'
+ ' The extension modules "dbm.ndbm" and "dbm.gnu" provide\n'
+ ' additional examples of mapping types, as does the '
+ '"collections"\n'
+ ' module.\n'
+ '\n'
+ 'Callable types\n'
+ ' These are the types to which the function call operation (see\n'
+ ' section Calls) can be applied:\n'
+ '\n'
+ ' User-defined functions\n'
+ ' A user-defined function object is created by a function\n'
+ ' definition (see section Function definitions). It should be\n'
+ ' called with an argument list containing the same number of '
+ 'items\n'
+ " as the function's formal parameter list.\n"
+ '\n'
+ ' Special attributes:\n'
+ '\n'
+ ' '
+ '+---------------------------+---------------------------------+-------------+\n'
+ ' | Attribute | Meaning '
+ '| |\n'
+ ' '
+ '+===========================+=================================+=============+\n'
+ ' | "__doc__" | The function\'s '
+ 'documentation | Writable |\n'
+ ' | | string, or "None" if '
+ '| |\n'
+ ' | | unavailable; not inherited by '
+ '| |\n'
+ ' | | subclasses '
+ '| |\n'
+ ' '
+ '+---------------------------+---------------------------------+-------------+\n'
+ ' | "__name__" | The function\'s '
+ 'name | Writable |\n'
+ ' '
+ '+---------------------------+---------------------------------+-------------+\n'
+ ' | "__qualname__" | The function\'s *qualified '
+ 'name* | Writable |\n'
+ ' | | New in version 3.3. '
+ '| |\n'
+ ' '
+ '+---------------------------+---------------------------------+-------------+\n'
+ ' | "__module__" | The name of the module the '
+ '| Writable |\n'
+ ' | | function was defined in, or '
+ '| |\n'
+ ' | | "None" if unavailable. '
+ '| |\n'
+ ' '
+ '+---------------------------+---------------------------------+-------------+\n'
+ ' | "__defaults__" | A tuple containing default '
+ '| Writable |\n'
+ ' | | argument values for those '
+ '| |\n'
+ ' | | arguments that have defaults, '
+ '| |\n'
+ ' | | or "None" if no arguments have '
+ '| |\n'
+ ' | | a default value '
+ '| |\n'
+ ' '
+ '+---------------------------+---------------------------------+-------------+\n'
+ ' | "__code__" | The code object representing '
+ '| Writable |\n'
+ ' | | the compiled function body. '
+ '| |\n'
+ ' '
+ '+---------------------------+---------------------------------+-------------+\n'
+ ' | "__globals__" | A reference to the dictionary '
+ '| Read-only |\n'
+ " | | that holds the function's "
+ '| |\n'
+ ' | | global variables --- the global '
+ '| |\n'
+ ' | | namespace of the module in '
+ '| |\n'
+ ' | | which the function was defined. '
+ '| |\n'
+ ' '
+ '+---------------------------+---------------------------------+-------------+\n'
+ ' | "__dict__" | The namespace supporting '
+ '| Writable |\n'
+ ' | | arbitrary function attributes. '
+ '| |\n'
+ ' '
+ '+---------------------------+---------------------------------+-------------+\n'
+ ' | "__closure__" | "None" or a tuple of cells that '
+ '| Read-only |\n'
+ ' | | contain bindings for the '
+ '| |\n'
+ " | | function's free variables. "
+ '| |\n'
+ ' '
+ '+---------------------------+---------------------------------+-------------+\n'
+ ' | "__annotations__" | A dict containing annotations '
+ '| Writable |\n'
+ ' | | of parameters. The keys of the '
+ '| |\n'
+ ' | | dict are the parameter names, '
+ '| |\n'
+ ' | | and "\'return\'" for the '
+ 'return | |\n'
+ ' | | annotation, if provided. '
+ '| |\n'
+ ' '
+ '+---------------------------+---------------------------------+-------------+\n'
+ ' | "__kwdefaults__" | A dict containing defaults for '
+ '| Writable |\n'
+ ' | | keyword-only parameters. '
+ '| |\n'
+ ' '
+ '+---------------------------+---------------------------------+-------------+\n'
+ '\n'
+ ' Most of the attributes labelled "Writable" check the type of '
+ 'the\n'
+ ' assigned value.\n'
+ '\n'
+ ' Function objects also support getting and setting arbitrary\n'
+ ' attributes, which can be used, for example, to attach '
+ 'metadata\n'
+ ' to functions. Regular attribute dot-notation is used to get '
+ 'and\n'
+ ' set such attributes. *Note that the current implementation '
+ 'only\n'
+ ' supports function attributes on user-defined functions. '
+ 'Function\n'
+ ' attributes on built-in functions may be supported in the\n'
+ ' future.*\n'
+ '\n'
+ " Additional information about a function's definition can be\n"
+ ' retrieved from its code object; see the description of '
+ 'internal\n'
+ ' types below.\n'
+ '\n'
+ ' Instance methods\n'
+ ' An instance method object combines a class, a class instance '
+ 'and\n'
+ ' any callable object (normally a user-defined function).\n'
+ '\n'
+ ' Special read-only attributes: "__self__" is the class '
+ 'instance\n'
+ ' object, "__func__" is the function object; "__doc__" is the\n'
+ ' method\'s documentation (same as "__func__.__doc__"); '
+ '"__name__"\n'
+ ' is the method name (same as "__func__.__name__"); '
+ '"__module__"\n'
+ ' is the name of the module the method was defined in, or '
+ '"None"\n'
+ ' if unavailable.\n'
+ '\n'
+ ' Methods also support accessing (but not setting) the '
+ 'arbitrary\n'
+ ' function attributes on the underlying function object.\n'
+ '\n'
+ ' User-defined method objects may be created when getting an\n'
+ ' attribute of a class (perhaps via an instance of that class), '
+ 'if\n'
+ ' that attribute is a user-defined function object or a class\n'
+ ' method object.\n'
+ '\n'
+ ' When an instance method object is created by retrieving a '
+ 'user-\n'
+ ' defined function object from a class via one of its '
+ 'instances,\n'
+ ' its "__self__" attribute is the instance, and the method '
+ 'object\n'
+ ' is said to be bound. The new method\'s "__func__" attribute '
+ 'is\n'
+ ' the original function object.\n'
+ '\n'
+ ' When a user-defined method object is created by retrieving\n'
+ ' another method object from a class or instance, the behaviour '
+ 'is\n'
+ ' the same as for a function object, except that the '
+ '"__func__"\n'
+ ' attribute of the new instance is not the original method '
+ 'object\n'
+ ' but its "__func__" attribute.\n'
+ '\n'
+ ' When an instance method object is created by retrieving a '
+ 'class\n'
+ ' method object from a class or instance, its "__self__" '
+ 'attribute\n'
+ ' is the class itself, and its "__func__" attribute is the\n'
+ ' function object underlying the class method.\n'
+ '\n'
+ ' When an instance method object is called, the underlying\n'
+ ' function ("__func__") is called, inserting the class '
+ 'instance\n'
+ ' ("__self__") in front of the argument list. For instance, '
+ 'when\n'
+ ' "C" is a class which contains a definition for a function '
+ '"f()",\n'
+ ' and "x" is an instance of "C", calling "x.f(1)" is equivalent '
+ 'to\n'
+ ' calling "C.f(x, 1)".\n'
+ '\n'
+ ' When an instance method object is derived from a class '
+ 'method\n'
+ ' object, the "class instance" stored in "__self__" will '
+ 'actually\n'
+ ' be the class itself, so that calling either "x.f(1)" or '
+ '"C.f(1)"\n'
+ ' is equivalent to calling "f(C,1)" where "f" is the '
+ 'underlying\n'
+ ' function.\n'
+ '\n'
+ ' Note that the transformation from function object to '
+ 'instance\n'
+ ' method object happens each time the attribute is retrieved '
+ 'from\n'
+ ' the instance. In some cases, a fruitful optimization is to\n'
+ ' assign the attribute to a local variable and call that local\n'
+ ' variable. Also notice that this transformation only happens '
+ 'for\n'
+ ' user-defined functions; other callable objects (and all non-\n'
+ ' callable objects) are retrieved without transformation. It '
+ 'is\n'
+ ' also important to note that user-defined functions which are\n'
+ ' attributes of a class instance are not converted to bound\n'
+ ' methods; this *only* happens when the function is an '
+ 'attribute\n'
+ ' of the class.\n'
+ '\n'
+ ' Generator functions\n'
+ ' A function or method which uses the "yield" statement (see\n'
+ ' section The yield statement) is called a *generator '
+ 'function*.\n'
+ ' Such a function, when called, always returns an iterator '
+ 'object\n'
+ ' which can be used to execute the body of the function: '
+ 'calling\n'
+ ' the iterator\'s "iterator.__next__()" method will cause the\n'
+ ' function to execute until it provides a value using the '
+ '"yield"\n'
+ ' statement. When the function executes a "return" statement '
+ 'or\n'
+ ' falls off the end, a "StopIteration" exception is raised and '
+ 'the\n'
+ ' iterator will have reached the end of the set of values to '
+ 'be\n'
+ ' returned.\n'
+ '\n'
+ ' Coroutine functions\n'
+ ' A function or method which is defined using "async def" is\n'
+ ' called a *coroutine function*. Such a function, when '
+ 'called,\n'
+ ' returns a *coroutine* object. It may contain "await"\n'
+ ' expressions, as well as "async with" and "async for" '
+ 'statements.\n'
+ ' See also the Coroutine Objects section.\n'
+ '\n'
+ ' Built-in functions\n'
+ ' A built-in function object is a wrapper around a C function.\n'
+ ' Examples of built-in functions are "len()" and "math.sin()"\n'
+ ' ("math" is a standard built-in module). The number and type '
+ 'of\n'
+ ' the arguments are determined by the C function. Special '
+ 'read-\n'
+ ' only attributes: "__doc__" is the function\'s documentation\n'
+ ' string, or "None" if unavailable; "__name__" is the '
+ "function's\n"
+ ' name; "__self__" is set to "None" (but see the next item);\n'
+ ' "__module__" is the name of the module the function was '
+ 'defined\n'
+ ' in or "None" if unavailable.\n'
+ '\n'
+ ' Built-in methods\n'
+ ' This is really a different disguise of a built-in function, '
+ 'this\n'
+ ' time containing an object passed to the C function as an\n'
+ ' implicit extra argument. An example of a built-in method is\n'
+ ' "alist.append()", assuming *alist* is a list object. In this\n'
+ ' case, the special read-only attribute "__self__" is set to '
+ 'the\n'
+ ' object denoted by *alist*.\n'
+ '\n'
+ ' Classes\n'
+ ' Classes are callable. These objects normally act as '
+ 'factories\n'
+ ' for new instances of themselves, but variations are possible '
+ 'for\n'
+ ' class types that override "__new__()". The arguments of the\n'
+ ' call are passed to "__new__()" and, in the typical case, to\n'
+ ' "__init__()" to initialize the new instance.\n'
+ '\n'
+ ' Class Instances\n'
+ ' Instances of arbitrary classes can be made callable by '
+ 'defining\n'
+ ' a "__call__()" method in their class.\n'
+ '\n'
+ 'Modules\n'
+ ' Modules are a basic organizational unit of Python code, and are\n'
+ ' created by the import system as invoked either by the "import"\n'
+ ' statement (see "import"), or by calling functions such as\n'
+ ' "importlib.import_module()" and built-in "__import__()". A '
+ 'module\n'
+ ' object has a namespace implemented by a dictionary object (this '
+ 'is\n'
+ ' the dictionary referenced by the "__globals__" attribute of\n'
+ ' functions defined in the module). Attribute references are\n'
+ ' translated to lookups in this dictionary, e.g., "m.x" is '
+ 'equivalent\n'
+ ' to "m.__dict__["x"]". A module object does not contain the code\n'
+ " object used to initialize the module (since it isn't needed "
+ 'once\n'
+ ' the initialization is done).\n'
+ '\n'
+ " Attribute assignment updates the module's namespace dictionary,\n"
+ ' e.g., "m.x = 1" is equivalent to "m.__dict__["x"] = 1".\n'
+ '\n'
+ ' Predefined (writable) attributes: "__name__" is the module\'s '
+ 'name;\n'
+ ' "__doc__" is the module\'s documentation string, or "None" if\n'
+ ' unavailable; "__annotations__" (optional) is a dictionary\n'
+ ' containing *variable annotations* collected during module body\n'
+ ' execution; "__file__" is the pathname of the file from which '
+ 'the\n'
+ ' module was loaded, if it was loaded from a file. The "__file__"\n'
+ ' attribute may be missing for certain types of modules, such as '
+ 'C\n'
+ ' modules that are statically linked into the interpreter; for\n'
+ ' extension modules loaded dynamically from a shared library, it '
+ 'is\n'
+ ' the pathname of the shared library file.\n'
+ '\n'
+ ' Special read-only attribute: "__dict__" is the module\'s '
+ 'namespace\n'
+ ' as a dictionary object.\n'
+ '\n'
+ ' **CPython implementation detail:** Because of the way CPython\n'
+ ' clears module dictionaries, the module dictionary will be '
+ 'cleared\n'
+ ' when the module falls out of scope even if the dictionary still '
+ 'has\n'
+ ' live references. To avoid this, copy the dictionary or keep '
+ 'the\n'
+ ' module around while using its dictionary directly.\n'
+ '\n'
+ 'Custom classes\n'
+ ' Custom class types are typically created by class definitions '
+ '(see\n'
+ ' section Class definitions). A class has a namespace implemented '
+ 'by\n'
+ ' a dictionary object. Class attribute references are translated '
+ 'to\n'
+ ' lookups in this dictionary, e.g., "C.x" is translated to\n'
+ ' "C.__dict__["x"]" (although there are a number of hooks which '
+ 'allow\n'
+ ' for other means of locating attributes). When the attribute name '
+ 'is\n'
+ ' not found there, the attribute search continues in the base\n'
+ ' classes. This search of the base classes uses the C3 method\n'
+ ' resolution order which behaves correctly even in the presence '
+ 'of\n'
+ " 'diamond' inheritance structures where there are multiple\n"
+ ' inheritance paths leading back to a common ancestor. Additional\n'
+ ' details on the C3 MRO used by Python can be found in the\n'
+ ' documentation accompanying the 2.3 release at\n'
+ ' https://www.python.org/download/releases/2.3/mro/.\n'
+ '\n'
+ ' When a class attribute reference (for class "C", say) would '
+ 'yield a\n'
+ ' class method object, it is transformed into an instance method\n'
+ ' object whose "__self__" attributes is "C". When it would yield '
+ 'a\n'
+ ' static method object, it is transformed into the object wrapped '
+ 'by\n'
+ ' the static method object. See section Implementing Descriptors '
+ 'for\n'
+ ' another way in which attributes retrieved from a class may '
+ 'differ\n'
+ ' from those actually contained in its "__dict__".\n'
+ '\n'
+ " Class attribute assignments update the class's dictionary, "
+ 'never\n'
+ ' the dictionary of a base class.\n'
+ '\n'
+ ' A class object can be called (see above) to yield a class '
+ 'instance\n'
+ ' (see below).\n'
+ '\n'
+ ' Special attributes: "__name__" is the class name; "__module__" '
+ 'is\n'
+ ' the module name in which the class was defined; "__dict__" is '
+ 'the\n'
+ ' dictionary containing the class\'s namespace; "__bases__" is a '
+ 'tuple\n'
+ ' (possibly empty or a singleton) containing the base classes, in '
+ 'the\n'
+ ' order of their occurrence in the base class list; "__doc__" is '
+ 'the\n'
+ ' class\'s documentation string, or "None" if undefined;\n'
+ ' "__annotations__" (optional) is a dictionary containing '
+ '*variable\n'
+ ' annotations* collected during class body execution.\n'
+ '\n'
+ 'Class instances\n'
+ ' A class instance is created by calling a class object (see '
+ 'above).\n'
+ ' A class instance has a namespace implemented as a dictionary '
+ 'which\n'
+ ' is the first place in which attribute references are searched.\n'
+ " When an attribute is not found there, and the instance's class "
+ 'has\n'
+ ' an attribute by that name, the search continues with the class\n'
+ ' attributes. If a class attribute is found that is a '
+ 'user-defined\n'
+ ' function object, it is transformed into an instance method '
+ 'object\n'
+ ' whose "__self__" attribute is the instance. Static method and\n'
+ ' class method objects are also transformed; see above under\n'
+ ' "Classes". See section Implementing Descriptors for another way '
+ 'in\n'
+ ' which attributes of a class retrieved via its instances may '
+ 'differ\n'
+ ' from the objects actually stored in the class\'s "__dict__". If '
+ 'no\n'
+ " class attribute is found, and the object's class has a\n"
+ ' "__getattr__()" method, that is called to satisfy the lookup.\n'
+ '\n'
+ " Attribute assignments and deletions update the instance's\n"
+ " dictionary, never a class's dictionary. If the class has a\n"
+ ' "__setattr__()" or "__delattr__()" method, this is called '
+ 'instead\n'
+ ' of updating the instance dictionary directly.\n'
+ '\n'
+ ' Class instances can pretend to be numbers, sequences, or '
+ 'mappings\n'
+ ' if they have methods with certain special names. See section\n'
+ ' Special method names.\n'
+ '\n'
+ ' Special attributes: "__dict__" is the attribute dictionary;\n'
+ ' "__class__" is the instance\'s class.\n'
+ '\n'
+ 'I/O objects (also known as file objects)\n'
+ ' A *file object* represents an open file. Various shortcuts are\n'
+ ' available to create file objects: the "open()" built-in '
+ 'function,\n'
+ ' and also "os.popen()", "os.fdopen()", and the "makefile()" '
+ 'method\n'
+ ' of socket objects (and perhaps by other functions or methods\n'
+ ' provided by extension modules).\n'
+ '\n'
+ ' The objects "sys.stdin", "sys.stdout" and "sys.stderr" are\n'
+ " initialized to file objects corresponding to the interpreter's\n"
+ ' standard input, output and error streams; they are all open in '
+ 'text\n'
+ ' mode and therefore follow the interface defined by the\n'
+ ' "io.TextIOBase" abstract class.\n'
+ '\n'
+ 'Internal types\n'
+ ' A few types used internally by the interpreter are exposed to '
+ 'the\n'
+ ' user. Their definitions may change with future versions of the\n'
+ ' interpreter, but they are mentioned here for completeness.\n'
+ '\n'
+ ' Code objects\n'
+ ' Code objects represent *byte-compiled* executable Python '
+ 'code,\n'
+ ' or *bytecode*. The difference between a code object and a\n'
+ ' function object is that the function object contains an '
+ 'explicit\n'
+ " reference to the function's globals (the module in which it "
+ 'was\n'
+ ' defined), while a code object contains no context; also the\n'
+ ' default argument values are stored in the function object, '
+ 'not\n'
+ ' in the code object (because they represent values calculated '
+ 'at\n'
+ ' run-time). Unlike function objects, code objects are '
+ 'immutable\n'
+ ' and contain no references (directly or indirectly) to '
+ 'mutable\n'
+ ' objects.\n'
+ '\n'
+ ' Special read-only attributes: "co_name" gives the function '
+ 'name;\n'
+ ' "co_argcount" is the number of positional arguments '
+ '(including\n'
+ ' arguments with default values); "co_nlocals" is the number '
+ 'of\n'
+ ' local variables used by the function (including arguments);\n'
+ ' "co_varnames" is a tuple containing the names of the local\n'
+ ' variables (starting with the argument names); "co_cellvars" '
+ 'is a\n'
+ ' tuple containing the names of local variables that are\n'
+ ' referenced by nested functions; "co_freevars" is a tuple\n'
+ ' containing the names of free variables; "co_code" is a '
+ 'string\n'
+ ' representing the sequence of bytecode instructions; '
+ '"co_consts"\n'
+ ' is a tuple containing the literals used by the bytecode;\n'
+ ' "co_names" is a tuple containing the names used by the '
+ 'bytecode;\n'
+ ' "co_filename" is the filename from which the code was '
+ 'compiled;\n'
+ ' "co_firstlineno" is the first line number of the function;\n'
+ ' "co_lnotab" is a string encoding the mapping from bytecode\n'
+ ' offsets to line numbers (for details see the source code of '
+ 'the\n'
+ ' interpreter); "co_stacksize" is the required stack size\n'
+ ' (including local variables); "co_flags" is an integer '
+ 'encoding a\n'
+ ' number of flags for the interpreter.\n'
+ '\n'
+ ' The following flag bits are defined for "co_flags": bit '
+ '"0x04"\n'
+ ' is set if the function uses the "*arguments" syntax to accept '
+ 'an\n'
+ ' arbitrary number of positional arguments; bit "0x08" is set '
+ 'if\n'
+ ' the function uses the "**keywords" syntax to accept '
+ 'arbitrary\n'
+ ' keyword arguments; bit "0x20" is set if the function is a\n'
+ ' generator.\n'
+ '\n'
+ ' Future feature declarations ("from __future__ import '
+ 'division")\n'
+ ' also use bits in "co_flags" to indicate whether a code '
+ 'object\n'
+ ' was compiled with a particular feature enabled: bit "0x2000" '
+ 'is\n'
+ ' set if the function was compiled with future division '
+ 'enabled;\n'
+ ' bits "0x10" and "0x1000" were used in earlier versions of\n'
+ ' Python.\n'
+ '\n'
+ ' Other bits in "co_flags" are reserved for internal use.\n'
+ '\n'
+ ' If a code object represents a function, the first item in\n'
+ ' "co_consts" is the documentation string of the function, or\n'
+ ' "None" if undefined.\n'
+ '\n'
+ ' Frame objects\n'
+ ' Frame objects represent execution frames. They may occur in\n'
+ ' traceback objects (see below).\n'
+ '\n'
+ ' Special read-only attributes: "f_back" is to the previous '
+ 'stack\n'
+ ' frame (towards the caller), or "None" if this is the bottom\n'
+ ' stack frame; "f_code" is the code object being executed in '
+ 'this\n'
+ ' frame; "f_locals" is the dictionary used to look up local\n'
+ ' variables; "f_globals" is used for global variables;\n'
+ ' "f_builtins" is used for built-in (intrinsic) names; '
+ '"f_lasti"\n'
+ ' gives the precise instruction (this is an index into the\n'
+ ' bytecode string of the code object).\n'
+ '\n'
+ ' Special writable attributes: "f_trace", if not "None", is a\n'
+ ' function called at the start of each source code line (this '
+ 'is\n'
+ ' used by the debugger); "f_lineno" is the current line number '
+ 'of\n'
+ ' the frame --- writing to this from within a trace function '
+ 'jumps\n'
+ ' to the given line (only for the bottom-most frame). A '
+ 'debugger\n'
+ ' can implement a Jump command (aka Set Next Statement) by '
+ 'writing\n'
+ ' to f_lineno.\n'
+ '\n'
+ ' Frame objects support one method:\n'
+ '\n'
+ ' frame.clear()\n'
+ '\n'
+ ' This method clears all references to local variables held '
+ 'by\n'
+ ' the frame. Also, if the frame belonged to a generator, '
+ 'the\n'
+ ' generator is finalized. This helps break reference '
+ 'cycles\n'
+ ' involving frame objects (for example when catching an\n'
+ ' exception and storing its traceback for later use).\n'
+ '\n'
+ ' "RuntimeError" is raised if the frame is currently '
+ 'executing.\n'
+ '\n'
+ ' New in version 3.4.\n'
+ '\n'
+ ' Traceback objects\n'
+ ' Traceback objects represent a stack trace of an exception. '
+ 'A\n'
+ ' traceback object is created when an exception occurs. When '
+ 'the\n'
+ ' search for an exception handler unwinds the execution stack, '
+ 'at\n'
+ ' each unwound level a traceback object is inserted in front '
+ 'of\n'
+ ' the current traceback. When an exception handler is '
+ 'entered,\n'
+ ' the stack trace is made available to the program. (See '
+ 'section\n'
+ ' The try statement.) It is accessible as the third item of '
+ 'the\n'
+ ' tuple returned by "sys.exc_info()". When the program contains '
+ 'no\n'
+ ' suitable handler, the stack trace is written (nicely '
+ 'formatted)\n'
+ ' to the standard error stream; if the interpreter is '
+ 'interactive,\n'
+ ' it is also made available to the user as '
+ '"sys.last_traceback".\n'
+ '\n'
+ ' Special read-only attributes: "tb_next" is the next level in '
+ 'the\n'
+ ' stack trace (towards the frame where the exception occurred), '
+ 'or\n'
+ ' "None" if there is no next level; "tb_frame" points to the\n'
+ ' execution frame of the current level; "tb_lineno" gives the '
+ 'line\n'
+ ' number where the exception occurred; "tb_lasti" indicates '
+ 'the\n'
+ ' precise instruction. The line number and last instruction '
+ 'in\n'
+ ' the traceback may differ from the line number of its frame\n'
+ ' object if the exception occurred in a "try" statement with '
+ 'no\n'
+ ' matching except clause or with a finally clause.\n'
+ '\n'
+ ' Slice objects\n'
+ ' Slice objects are used to represent slices for '
+ '"__getitem__()"\n'
+ ' methods. They are also created by the built-in "slice()"\n'
+ ' function.\n'
+ '\n'
+ ' Special read-only attributes: "start" is the lower bound; '
+ '"stop"\n'
+ ' is the upper bound; "step" is the step value; each is "None" '
+ 'if\n'
+ ' omitted. These attributes can have any type.\n'
+ '\n'
+ ' Slice objects support one method:\n'
+ '\n'
+ ' slice.indices(self, length)\n'
+ '\n'
+ ' This method takes a single integer argument *length* and\n'
+ ' computes information about the slice that the slice '
+ 'object\n'
+ ' would describe if applied to a sequence of *length* '
+ 'items.\n'
+ ' It returns a tuple of three integers; respectively these '
+ 'are\n'
+ ' the *start* and *stop* indices and the *step* or stride\n'
+ ' length of the slice. Missing or out-of-bounds indices are\n'
+ ' handled in a manner consistent with regular slices.\n'
+ '\n'
+ ' Static method objects\n'
+ ' Static method objects provide a way of defeating the\n'
+ ' transformation of function objects to method objects '
+ 'described\n'
+ ' above. A static method object is a wrapper around any other\n'
+ ' object, usually a user-defined method object. When a static\n'
+ ' method object is retrieved from a class or a class instance, '
+ 'the\n'
+ ' object actually returned is the wrapped object, which is not\n'
+ ' subject to any further transformation. Static method objects '
+ 'are\n'
+ ' not themselves callable, although the objects they wrap '
+ 'usually\n'
+ ' are. Static method objects are created by the built-in\n'
+ ' "staticmethod()" constructor.\n'
+ '\n'
+ ' Class method objects\n'
+ ' A class method object, like a static method object, is a '
+ 'wrapper\n'
+ ' around another object that alters the way in which that '
+ 'object\n'
+ ' is retrieved from classes and class instances. The behaviour '
+ 'of\n'
+ ' class method objects upon such retrieval is described above,\n'
+ ' under "User-defined methods". Class method objects are '
+ 'created\n'
+ ' by the built-in "classmethod()" constructor.\n',
+ 'typesfunctions': '\n'
+ 'Functions\n'
+ '*********\n'
+ '\n'
+ 'Function objects are created by function definitions. The '
+ 'only\n'
+ 'operation on a function object is to call it: '
+ '"func(argument-list)".\n'
+ '\n'
+ 'There are really two flavors of function objects: built-in '
+ 'functions\n'
+ 'and user-defined functions. Both support the same '
+ 'operation (to call\n'
+ 'the function), but the implementation is different, hence '
+ 'the\n'
+ 'different object types.\n'
+ '\n'
+ 'See Function definitions for more information.\n',
+ 'typesmapping': '\n'
+ 'Mapping Types --- "dict"\n'
+ '************************\n'
+ '\n'
+ 'A *mapping* object maps *hashable* values to arbitrary '
+ 'objects.\n'
+ 'Mappings are mutable objects. There is currently only one '
+ 'standard\n'
+ 'mapping type, the *dictionary*. (For other containers see '
+ 'the built-\n'
+ 'in "list", "set", and "tuple" classes, and the "collections" '
+ 'module.)\n'
+ '\n'
+ "A dictionary's keys are *almost* arbitrary values. Values "
+ 'that are\n'
+ 'not *hashable*, that is, values containing lists, '
+ 'dictionaries or\n'
+ 'other mutable types (that are compared by value rather than '
+ 'by object\n'
+ 'identity) may not be used as keys. Numeric types used for '
+ 'keys obey\n'
+ 'the normal rules for numeric comparison: if two numbers '
+ 'compare equal\n'
+ '(such as "1" and "1.0") then they can be used '
+ 'interchangeably to index\n'
+ 'the same dictionary entry. (Note however, that since '
+ 'computers store\n'
+ 'floating-point numbers as approximations it is usually '
+ 'unwise to use\n'
+ 'them as dictionary keys.)\n'
+ '\n'
+ 'Dictionaries can be created by placing a comma-separated '
+ 'list of "key:\n'
+ 'value" pairs within braces, for example: "{\'jack\': 4098, '
+ "'sjoerd':\n"
+ '4127}" or "{4098: \'jack\', 4127: \'sjoerd\'}", or by the '
+ '"dict"\n'
+ 'constructor.\n'
+ '\n'
+ 'class dict(**kwarg)\n'
+ 'class dict(mapping, **kwarg)\n'
+ 'class dict(iterable, **kwarg)\n'
+ '\n'
+ ' Return a new dictionary initialized from an optional '
+ 'positional\n'
+ ' argument and a possibly empty set of keyword arguments.\n'
+ '\n'
+ ' If no positional argument is given, an empty dictionary '
+ 'is created.\n'
+ ' If a positional argument is given and it is a mapping '
+ 'object, a\n'
+ ' dictionary is created with the same key-value pairs as '
+ 'the mapping\n'
+ ' object. Otherwise, the positional argument must be an '
+ '*iterable*\n'
+ ' object. Each item in the iterable must itself be an '
+ 'iterable with\n'
+ ' exactly two objects. The first object of each item '
+ 'becomes a key\n'
+ ' in the new dictionary, and the second object the '
+ 'corresponding\n'
+ ' value. If a key occurs more than once, the last value '
+ 'for that key\n'
+ ' becomes the corresponding value in the new dictionary.\n'
+ '\n'
+ ' If keyword arguments are given, the keyword arguments and '
+ 'their\n'
+ ' values are added to the dictionary created from the '
+ 'positional\n'
+ ' argument. If a key being added is already present, the '
+ 'value from\n'
+ ' the keyword argument replaces the value from the '
+ 'positional\n'
+ ' argument.\n'
+ '\n'
+ ' To illustrate, the following examples all return a '
+ 'dictionary equal\n'
+ ' to "{"one": 1, "two": 2, "three": 3}":\n'
+ '\n'
+ ' >>> a = dict(one=1, two=2, three=3)\n'
+ " >>> b = {'one': 1, 'two': 2, 'three': 3}\n"
+ " >>> c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))\n"
+ " >>> d = dict([('two', 2), ('one', 1), ('three', 3)])\n"
+ " >>> e = dict({'three': 3, 'one': 1, 'two': 2})\n"
+ ' >>> a == b == c == d == e\n'
+ ' True\n'
+ '\n'
+ ' Providing keyword arguments as in the first example only '
+ 'works for\n'
+ ' keys that are valid Python identifiers. Otherwise, any '
+ 'valid keys\n'
+ ' can be used.\n'
+ '\n'
+ ' These are the operations that dictionaries support (and '
+ 'therefore,\n'
+ ' custom mapping types should support too):\n'
+ '\n'
+ ' len(d)\n'
+ '\n'
+ ' Return the number of items in the dictionary *d*.\n'
+ '\n'
+ ' d[key]\n'
+ '\n'
+ ' Return the item of *d* with key *key*. Raises a '
+ '"KeyError" if\n'
+ ' *key* is not in the map.\n'
+ '\n'
+ ' If a subclass of dict defines a method "__missing__()" '
+ 'and *key*\n'
+ ' is not present, the "d[key]" operation calls that '
+ 'method with\n'
+ ' the key *key* as argument. The "d[key]" operation '
+ 'then returns\n'
+ ' or raises whatever is returned or raised by the\n'
+ ' "__missing__(key)" call. No other operations or '
+ 'methods invoke\n'
+ ' "__missing__()". If "__missing__()" is not defined, '
+ '"KeyError"\n'
+ ' is raised. "__missing__()" must be a method; it cannot '
+ 'be an\n'
+ ' instance variable:\n'
+ '\n'
+ ' >>> class Counter(dict):\n'
+ ' ... def __missing__(self, key):\n'
+ ' ... return 0\n'
+ ' >>> c = Counter()\n'
+ " >>> c['red']\n"
+ ' 0\n'
+ " >>> c['red'] += 1\n"
+ " >>> c['red']\n"
+ ' 1\n'
+ '\n'
+ ' The example above shows part of the implementation of\n'
+ ' "collections.Counter". A different "__missing__" '
+ 'method is used\n'
+ ' by "collections.defaultdict".\n'
+ '\n'
+ ' d[key] = value\n'
+ '\n'
+ ' Set "d[key]" to *value*.\n'
+ '\n'
+ ' del d[key]\n'
+ '\n'
+ ' Remove "d[key]" from *d*. Raises a "KeyError" if '
+ '*key* is not\n'
+ ' in the map.\n'
+ '\n'
+ ' key in d\n'
+ '\n'
+ ' Return "True" if *d* has a key *key*, else "False".\n'
+ '\n'
+ ' key not in d\n'
+ '\n'
+ ' Equivalent to "not key in d".\n'
+ '\n'
+ ' iter(d)\n'
+ '\n'
+ ' Return an iterator over the keys of the dictionary. '
+ 'This is a\n'
+ ' shortcut for "iter(d.keys())".\n'
+ '\n'
+ ' clear()\n'
+ '\n'
+ ' Remove all items from the dictionary.\n'
+ '\n'
+ ' copy()\n'
+ '\n'
+ ' Return a shallow copy of the dictionary.\n'
+ '\n'
+ ' classmethod fromkeys(seq[, value])\n'
+ '\n'
+ ' Create a new dictionary with keys from *seq* and '
+ 'values set to\n'
+ ' *value*.\n'
+ '\n'
+ ' "fromkeys()" is a class method that returns a new '
+ 'dictionary.\n'
+ ' *value* defaults to "None".\n'
+ '\n'
+ ' get(key[, default])\n'
+ '\n'
+ ' Return the value for *key* if *key* is in the '
+ 'dictionary, else\n'
+ ' *default*. If *default* is not given, it defaults to '
+ '"None", so\n'
+ ' that this method never raises a "KeyError".\n'
+ '\n'
+ ' items()\n'
+ '\n'
+ ' Return a new view of the dictionary\'s items ("(key, '
+ 'value)"\n'
+ ' pairs). See the documentation of view objects.\n'
+ '\n'
+ ' keys()\n'
+ '\n'
+ " Return a new view of the dictionary's keys. See the\n"
+ ' documentation of view objects.\n'
+ '\n'
+ ' pop(key[, default])\n'
+ '\n'
+ ' If *key* is in the dictionary, remove it and return '
+ 'its value,\n'
+ ' else return *default*. If *default* is not given and '
+ '*key* is\n'
+ ' not in the dictionary, a "KeyError" is raised.\n'
+ '\n'
+ ' popitem()\n'
+ '\n'
+ ' Remove and return an arbitrary "(key, value)" pair '
+ 'from the\n'
+ ' dictionary.\n'
+ '\n'
+ ' "popitem()" is useful to destructively iterate over a\n'
+ ' dictionary, as often used in set algorithms. If the '
+ 'dictionary\n'
+ ' is empty, calling "popitem()" raises a "KeyError".\n'
+ '\n'
+ ' setdefault(key[, default])\n'
+ '\n'
+ ' If *key* is in the dictionary, return its value. If '
+ 'not, insert\n'
+ ' *key* with a value of *default* and return *default*. '
+ '*default*\n'
+ ' defaults to "None".\n'
+ '\n'
+ ' update([other])\n'
+ '\n'
+ ' Update the dictionary with the key/value pairs from '
+ '*other*,\n'
+ ' overwriting existing keys. Return "None".\n'
+ '\n'
+ ' "update()" accepts either another dictionary object or '
+ 'an\n'
+ ' iterable of key/value pairs (as tuples or other '
+ 'iterables of\n'
+ ' length two). If keyword arguments are specified, the '
+ 'dictionary\n'
+ ' is then updated with those key/value pairs: '
+ '"d.update(red=1,\n'
+ ' blue=2)".\n'
+ '\n'
+ ' values()\n'
+ '\n'
+ " Return a new view of the dictionary's values. See "
+ 'the\n'
+ ' documentation of view objects.\n'
+ '\n'
+ ' Dictionaries compare equal if and only if they have the '
+ 'same "(key,\n'
+ ' value)" pairs. Order comparisons (\'<\', \'<=\', \'>=\', '
+ "'>') raise\n"
+ ' "TypeError".\n'
+ '\n'
+ 'See also: "types.MappingProxyType" can be used to create a '
+ 'read-only\n'
+ ' view of a "dict".\n'
+ '\n'
+ '\n'
+ 'Dictionary view objects\n'
+ '=======================\n'
+ '\n'
+ 'The objects returned by "dict.keys()", "dict.values()" and\n'
+ '"dict.items()" are *view objects*. They provide a dynamic '
+ 'view on the\n'
+ "dictionary's entries, which means that when the dictionary "
+ 'changes,\n'
+ 'the view reflects these changes.\n'
+ '\n'
+ 'Dictionary views can be iterated over to yield their '
+ 'respective data,\n'
+ 'and support membership tests:\n'
+ '\n'
+ 'len(dictview)\n'
+ '\n'
+ ' Return the number of entries in the dictionary.\n'
+ '\n'
+ 'iter(dictview)\n'
+ '\n'
+ ' Return an iterator over the keys, values or items '
+ '(represented as\n'
+ ' tuples of "(key, value)") in the dictionary.\n'
+ '\n'
+ ' Keys and values are iterated over in an arbitrary order '
+ 'which is\n'
+ ' non-random, varies across Python implementations, and '
+ 'depends on\n'
+ " the dictionary's history of insertions and deletions. If "
+ 'keys,\n'
+ ' values and items views are iterated over with no '
+ 'intervening\n'
+ ' modifications to the dictionary, the order of items will '
+ 'directly\n'
+ ' correspond. This allows the creation of "(value, key)" '
+ 'pairs using\n'
+ ' "zip()": "pairs = zip(d.values(), d.keys())". Another '
+ 'way to\n'
+ ' create the same list is "pairs = [(v, k) for (k, v) in '
+ 'd.items()]".\n'
+ '\n'
+ ' Iterating views while adding or deleting entries in the '
+ 'dictionary\n'
+ ' may raise a "RuntimeError" or fail to iterate over all '
+ 'entries.\n'
+ '\n'
+ 'x in dictview\n'
+ '\n'
+ ' Return "True" if *x* is in the underlying dictionary\'s '
+ 'keys, values\n'
+ ' or items (in the latter case, *x* should be a "(key, '
+ 'value)"\n'
+ ' tuple).\n'
+ '\n'
+ 'Keys views are set-like since their entries are unique and '
+ 'hashable.\n'
+ 'If all values are hashable, so that "(key, value)" pairs are '
+ 'unique\n'
+ 'and hashable, then the items view is also set-like. (Values '
+ 'views are\n'
+ 'not treated as set-like since the entries are generally not '
+ 'unique.)\n'
+ 'For set-like views, all of the operations defined for the '
+ 'abstract\n'
+ 'base class "collections.abc.Set" are available (for example, '
+ '"==",\n'
+ '"<", or "^").\n'
+ '\n'
+ 'An example of dictionary view usage:\n'
+ '\n'
+ " >>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, "
+ "'spam': 500}\n"
+ ' >>> keys = dishes.keys()\n'
+ ' >>> values = dishes.values()\n'
+ '\n'
+ ' >>> # iteration\n'
+ ' >>> n = 0\n'
+ ' >>> for val in values:\n'
+ ' ... n += val\n'
+ ' >>> print(n)\n'
+ ' 504\n'
+ '\n'
+ ' >>> # keys and values are iterated over in the same '
+ 'order\n'
+ ' >>> list(keys)\n'
+ " ['eggs', 'bacon', 'sausage', 'spam']\n"
+ ' >>> list(values)\n'
+ ' [2, 1, 1, 500]\n'
+ '\n'
+ ' >>> # view objects are dynamic and reflect dict changes\n'
+ " >>> del dishes['eggs']\n"
+ " >>> del dishes['sausage']\n"
+ ' >>> list(keys)\n'
+ " ['spam', 'bacon']\n"
+ '\n'
+ ' >>> # set operations\n'
+ " >>> keys & {'eggs', 'bacon', 'salad'}\n"
+ " {'bacon'}\n"
+ " >>> keys ^ {'sausage', 'juice'}\n"
+ " {'juice', 'sausage', 'bacon', 'spam'}\n",
+ 'typesmethods': '\n'
+ 'Methods\n'
+ '*******\n'
+ '\n'
+ 'Methods are functions that are called using the attribute '
+ 'notation.\n'
+ 'There are two flavors: built-in methods (such as "append()" '
+ 'on lists)\n'
+ 'and class instance methods. Built-in methods are described '
+ 'with the\n'
+ 'types that support them.\n'
+ '\n'
+ 'If you access a method (a function defined in a class '
+ 'namespace)\n'
+ 'through an instance, you get a special object: a *bound '
+ 'method* (also\n'
+ 'called *instance method*) object. When called, it will add '
+ 'the "self"\n'
+ 'argument to the argument list. Bound methods have two '
+ 'special read-\n'
+ 'only attributes: "m.__self__" is the object on which the '
+ 'method\n'
+ 'operates, and "m.__func__" is the function implementing the '
+ 'method.\n'
+ 'Calling "m(arg-1, arg-2, ..., arg-n)" is completely '
+ 'equivalent to\n'
+ 'calling "m.__func__(m.__self__, arg-1, arg-2, ..., arg-n)".\n'
+ '\n'
+ 'Like function objects, bound method objects support getting '
+ 'arbitrary\n'
+ 'attributes. However, since method attributes are actually '
+ 'stored on\n'
+ 'the underlying function object ("meth.__func__"), setting '
+ 'method\n'
+ 'attributes on bound methods is disallowed. Attempting to '
+ 'set an\n'
+ 'attribute on a method results in an "AttributeError" being '
+ 'raised. In\n'
+ 'order to set a method attribute, you need to explicitly set '
+ 'it on the\n'
+ 'underlying function object:\n'
+ '\n'
+ ' >>> class C:\n'
+ ' ... def method(self):\n'
+ ' ... pass\n'
+ ' ...\n'
+ ' >>> c = C()\n'
+ " >>> c.method.whoami = 'my name is method' # can't set on "
+ 'the method\n'
+ ' Traceback (most recent call last):\n'
+ ' File "<stdin>", line 1, in <module>\n'
+ " AttributeError: 'method' object has no attribute "
+ "'whoami'\n"
+ " >>> c.method.__func__.whoami = 'my name is method'\n"
+ ' >>> c.method.whoami\n'
+ " 'my name is method'\n"
+ '\n'
+ 'See The standard type hierarchy for more information.\n',
+ 'typesmodules': '\n'
+ 'Modules\n'
+ '*******\n'
+ '\n'
+ 'The only special operation on a module is attribute access: '
+ '"m.name",\n'
+ 'where *m* is a module and *name* accesses a name defined in '
+ "*m*'s\n"
+ 'symbol table. Module attributes can be assigned to. (Note '
+ 'that the\n'
+ '"import" statement is not, strictly speaking, an operation '
+ 'on a module\n'
+ 'object; "import foo" does not require a module object named '
+ '*foo* to\n'
+ 'exist, rather it requires an (external) *definition* for a '
+ 'module\n'
+ 'named *foo* somewhere.)\n'
+ '\n'
+ 'A special attribute of every module is "__dict__". This is '
+ 'the\n'
+ "dictionary containing the module's symbol table. Modifying "
+ 'this\n'
+ "dictionary will actually change the module's symbol table, "
+ 'but direct\n'
+ 'assignment to the "__dict__" attribute is not possible (you '
+ 'can write\n'
+ '"m.__dict__[\'a\'] = 1", which defines "m.a" to be "1", but '
+ "you can't\n"
+ 'write "m.__dict__ = {}"). Modifying "__dict__" directly is '
+ 'not\n'
+ 'recommended.\n'
+ '\n'
+ 'Modules built into the interpreter are written like this: '
+ '"<module\n'
+ '\'sys\' (built-in)>". If loaded from a file, they are '
+ 'written as\n'
+ '"<module \'os\' from '
+ '\'/usr/local/lib/pythonX.Y/os.pyc\'>".\n',
+ 'typesseq': '\n'
+ 'Sequence Types --- "list", "tuple", "range"\n'
+ '*******************************************\n'
+ '\n'
+ 'There are three basic sequence types: lists, tuples, and range\n'
+ 'objects. Additional sequence types tailored for processing of '
+ 'binary\n'
+ 'data and text strings are described in dedicated sections.\n'
+ '\n'
+ '\n'
+ 'Common Sequence Operations\n'
+ '==========================\n'
+ '\n'
+ 'The operations in the following table are supported by most '
+ 'sequence\n'
+ 'types, both mutable and immutable. The '
+ '"collections.abc.Sequence" ABC\n'
+ 'is provided to make it easier to correctly implement these '
+ 'operations\n'
+ 'on custom sequence types.\n'
+ '\n'
+ 'This table lists the sequence operations sorted in ascending '
+ 'priority.\n'
+ 'In the table, *s* and *t* are sequences of the same type, *n*, '
+ '*i*,\n'
+ '*j* and *k* are integers and *x* is an arbitrary object that '
+ 'meets any\n'
+ 'type and value restrictions imposed by *s*.\n'
+ '\n'
+ 'The "in" and "not in" operations have the same priorities as '
+ 'the\n'
+ 'comparison operations. The "+" (concatenation) and "*" '
+ '(repetition)\n'
+ 'operations have the same priority as the corresponding numeric\n'
+ 'operations.\n'
+ '\n'
+ '+----------------------------+----------------------------------+------------+\n'
+ '| Operation | Result '
+ '| Notes |\n'
+ '+============================+==================================+============+\n'
+ '| "x in s" | "True" if an item of *s* is '
+ '| (1) |\n'
+ '| | equal to *x*, else "False" '
+ '| |\n'
+ '+----------------------------+----------------------------------+------------+\n'
+ '| "x not in s" | "False" if an item of *s* is '
+ '| (1) |\n'
+ '| | equal to *x*, else "True" '
+ '| |\n'
+ '+----------------------------+----------------------------------+------------+\n'
+ '| "s + t" | the concatenation of *s* and *t* '
+ '| (6)(7) |\n'
+ '+----------------------------+----------------------------------+------------+\n'
+ '| "s * n" or "n * s" | equivalent to adding *s* to '
+ '| (2)(7) |\n'
+ '| | itself *n* times '
+ '| |\n'
+ '+----------------------------+----------------------------------+------------+\n'
+ '| "s[i]" | *i*th item of *s*, origin 0 '
+ '| (3) |\n'
+ '+----------------------------+----------------------------------+------------+\n'
+ '| "s[i:j]" | slice of *s* from *i* to *j* '
+ '| (3)(4) |\n'
+ '+----------------------------+----------------------------------+------------+\n'
+ '| "s[i:j:k]" | slice of *s* from *i* to *j* '
+ '| (3)(5) |\n'
+ '| | with step *k* '
+ '| |\n'
+ '+----------------------------+----------------------------------+------------+\n'
+ '| "len(s)" | length of *s* '
+ '| |\n'
+ '+----------------------------+----------------------------------+------------+\n'
+ '| "min(s)" | smallest item of *s* '
+ '| |\n'
+ '+----------------------------+----------------------------------+------------+\n'
+ '| "max(s)" | largest item of *s* '
+ '| |\n'
+ '+----------------------------+----------------------------------+------------+\n'
+ '| "s.index(x[, i[, j]])" | index of the first occurrence of '
+ '| (8) |\n'
+ '| | *x* in *s* (at or after index '
+ '| |\n'
+ '| | *i* and before index *j*) '
+ '| |\n'
+ '+----------------------------+----------------------------------+------------+\n'
+ '| "s.count(x)" | total number of occurrences of '
+ '| |\n'
+ '| | *x* in *s* '
+ '| |\n'
+ '+----------------------------+----------------------------------+------------+\n'
+ '\n'
+ 'Sequences of the same type also support comparisons. In '
+ 'particular,\n'
+ 'tuples and lists are compared lexicographically by comparing\n'
+ 'corresponding elements. This means that to compare equal, every\n'
+ 'element must compare equal and the two sequences must be of the '
+ 'same\n'
+ 'type and have the same length. (For full details see '
+ 'Comparisons in\n'
+ 'the language reference.)\n'
+ '\n'
+ 'Notes:\n'
+ '\n'
+ '1. While the "in" and "not in" operations are used only for '
+ 'simple\n'
+ ' containment testing in the general case, some specialised '
+ 'sequences\n'
+ ' (such as "str", "bytes" and "bytearray") also use them for\n'
+ ' subsequence testing:\n'
+ '\n'
+ ' >>> "gg" in "eggs"\n'
+ ' True\n'
+ '\n'
+ '2. Values of *n* less than "0" are treated as "0" (which yields '
+ 'an\n'
+ ' empty sequence of the same type as *s*). Note that items in '
+ 'the\n'
+ ' sequence *s* are not copied; they are referenced multiple '
+ 'times.\n'
+ ' This often haunts new Python programmers; consider:\n'
+ '\n'
+ ' >>> lists = [[]] * 3\n'
+ ' >>> lists\n'
+ ' [[], [], []]\n'
+ ' >>> lists[0].append(3)\n'
+ ' >>> lists\n'
+ ' [[3], [3], [3]]\n'
+ '\n'
+ ' What has happened is that "[[]]" is a one-element list '
+ 'containing\n'
+ ' an empty list, so all three elements of "[[]] * 3" are '
+ 'references\n'
+ ' to this single empty list. Modifying any of the elements of\n'
+ ' "lists" modifies this single list. You can create a list of\n'
+ ' different lists this way:\n'
+ '\n'
+ ' >>> lists = [[] for i in range(3)]\n'
+ ' >>> lists[0].append(3)\n'
+ ' >>> lists[1].append(5)\n'
+ ' >>> lists[2].append(7)\n'
+ ' >>> lists\n'
+ ' [[3], [5], [7]]\n'
+ '\n'
+ ' Further explanation is available in the FAQ entry How do I '
+ 'create a\n'
+ ' multidimensional list?.\n'
+ '\n'
+ '3. If *i* or *j* is negative, the index is relative to the end '
+ 'of\n'
+ ' the string: "len(s) + i" or "len(s) + j" is substituted. But '
+ 'note\n'
+ ' that "-0" is still "0".\n'
+ '\n'
+ '4. The slice of *s* from *i* to *j* is defined as the sequence '
+ 'of\n'
+ ' items with index *k* such that "i <= k < j". If *i* or *j* '
+ 'is\n'
+ ' greater than "len(s)", use "len(s)". If *i* is omitted or '
+ '"None",\n'
+ ' use "0". If *j* is omitted or "None", use "len(s)". If *i* '
+ 'is\n'
+ ' greater than or equal to *j*, the slice is empty.\n'
+ '\n'
+ '5. The slice of *s* from *i* to *j* with step *k* is defined as '
+ 'the\n'
+ ' sequence of items with index "x = i + n*k" such that "0 <= n '
+ '<\n'
+ ' (j-i)/k". In other words, the indices are "i", "i+k", '
+ '"i+2*k",\n'
+ ' "i+3*k" and so on, stopping when *j* is reached (but never\n'
+ ' including *j*). If *i* or *j* is greater than "len(s)", use\n'
+ ' "len(s)". If *i* or *j* are omitted or "None", they become '
+ '"end"\n'
+ ' values (which end depends on the sign of *k*). Note, *k* '
+ 'cannot be\n'
+ ' zero. If *k* is "None", it is treated like "1".\n'
+ '\n'
+ '6. Concatenating immutable sequences always results in a new\n'
+ ' object. This means that building up a sequence by repeated\n'
+ ' concatenation will have a quadratic runtime cost in the '
+ 'total\n'
+ ' sequence length. To get a linear runtime cost, you must '
+ 'switch to\n'
+ ' one of the alternatives below:\n'
+ '\n'
+ ' * if concatenating "str" objects, you can build a list and '
+ 'use\n'
+ ' "str.join()" at the end or else write to an "io.StringIO"\n'
+ ' instance and retrieve its value when complete\n'
+ '\n'
+ ' * if concatenating "bytes" objects, you can similarly use\n'
+ ' "bytes.join()" or "io.BytesIO", or you can do in-place\n'
+ ' concatenation with a "bytearray" object. "bytearray" '
+ 'objects are\n'
+ ' mutable and have an efficient overallocation mechanism\n'
+ '\n'
+ ' * if concatenating "tuple" objects, extend a "list" instead\n'
+ '\n'
+ ' * for other types, investigate the relevant class '
+ 'documentation\n'
+ '\n'
+ '7. Some sequence types (such as "range") only support item\n'
+ " sequences that follow specific patterns, and hence don't "
+ 'support\n'
+ ' sequence concatenation or repetition.\n'
+ '\n'
+ '8. "index" raises "ValueError" when *x* is not found in *s*. '
+ 'When\n'
+ ' supported, the additional arguments to the index method '
+ 'allow\n'
+ ' efficient searching of subsections of the sequence. Passing '
+ 'the\n'
+ ' extra arguments is roughly equivalent to using '
+ '"s[i:j].index(x)",\n'
+ ' only without copying any data and with the returned index '
+ 'being\n'
+ ' relative to the start of the sequence rather than the start '
+ 'of the\n'
+ ' slice.\n'
+ '\n'
+ '\n'
+ 'Immutable Sequence Types\n'
+ '========================\n'
+ '\n'
+ 'The only operation that immutable sequence types generally '
+ 'implement\n'
+ 'that is not also implemented by mutable sequence types is '
+ 'support for\n'
+ 'the "hash()" built-in.\n'
+ '\n'
+ 'This support allows immutable sequences, such as "tuple" '
+ 'instances, to\n'
+ 'be used as "dict" keys and stored in "set" and "frozenset" '
+ 'instances.\n'
+ '\n'
+ 'Attempting to hash an immutable sequence that contains '
+ 'unhashable\n'
+ 'values will result in "TypeError".\n'
+ '\n'
+ '\n'
+ 'Mutable Sequence Types\n'
+ '======================\n'
+ '\n'
+ 'The operations in the following table are defined on mutable '
+ 'sequence\n'
+ 'types. The "collections.abc.MutableSequence" ABC is provided to '
+ 'make\n'
+ 'it easier to correctly implement these operations on custom '
+ 'sequence\n'
+ 'types.\n'
+ '\n'
+ 'In the table *s* is an instance of a mutable sequence type, *t* '
+ 'is any\n'
+ 'iterable object and *x* is an arbitrary object that meets any '
+ 'type and\n'
+ 'value restrictions imposed by *s* (for example, "bytearray" '
+ 'only\n'
+ 'accepts integers that meet the value restriction "0 <= x <= '
+ '255").\n'
+ '\n'
+ '+--------------------------------+----------------------------------+-----------------------+\n'
+ '| Operation | '
+ 'Result | Notes |\n'
+ '+================================+==================================+=======================+\n'
+ '| "s[i] = x" | item *i* of *s* is replaced '
+ 'by | |\n'
+ '| | '
+ '*x* | |\n'
+ '+--------------------------------+----------------------------------+-----------------------+\n'
+ '| "s[i:j] = t" | slice of *s* from *i* to *j* '
+ 'is | |\n'
+ '| | replaced by the contents of '
+ 'the | |\n'
+ '| | iterable '
+ '*t* | |\n'
+ '+--------------------------------+----------------------------------+-----------------------+\n'
+ '| "del s[i:j]" | same as "s[i:j] = '
+ '[]" | |\n'
+ '+--------------------------------+----------------------------------+-----------------------+\n'
+ '| "s[i:j:k] = t" | the elements of "s[i:j:k]" '
+ 'are | (1) |\n'
+ '| | replaced by those of '
+ '*t* | |\n'
+ '+--------------------------------+----------------------------------+-----------------------+\n'
+ '| "del s[i:j:k]" | removes the elements '
+ 'of | |\n'
+ '| | "s[i:j:k]" from the '
+ 'list | |\n'
+ '+--------------------------------+----------------------------------+-----------------------+\n'
+ '| "s.append(x)" | appends *x* to the end of '
+ 'the | |\n'
+ '| | sequence (same '
+ 'as | |\n'
+ '| | "s[len(s):len(s)] = '
+ '[x]") | |\n'
+ '+--------------------------------+----------------------------------+-----------------------+\n'
+ '| "s.clear()" | removes all items from "s" '
+ '(same | (5) |\n'
+ '| | as "del '
+ 's[:]") | |\n'
+ '+--------------------------------+----------------------------------+-----------------------+\n'
+ '| "s.copy()" | creates a shallow copy of '
+ '"s" | (5) |\n'
+ '| | (same as '
+ '"s[:]") | |\n'
+ '+--------------------------------+----------------------------------+-----------------------+\n'
+ '| "s.extend(t)" or "s += t" | extends *s* with the contents '
+ 'of | |\n'
+ '| | *t* (for the most part the '
+ 'same | |\n'
+ '| | as "s[len(s):len(s)] = '
+ 't") | |\n'
+ '+--------------------------------+----------------------------------+-----------------------+\n'
+ '| "s *= n" | updates *s* with its '
+ 'contents | (6) |\n'
+ '| | repeated *n* '
+ 'times | |\n'
+ '+--------------------------------+----------------------------------+-----------------------+\n'
+ '| "s.insert(i, x)" | inserts *x* into *s* at '
+ 'the | |\n'
+ '| | index given by *i* (same '
+ 'as | |\n'
+ '| | "s[i:i] = '
+ '[x]") | |\n'
+ '+--------------------------------+----------------------------------+-----------------------+\n'
+ '| "s.pop([i])" | retrieves the item at *i* '
+ 'and | (2) |\n'
+ '| | also removes it from '
+ '*s* | |\n'
+ '+--------------------------------+----------------------------------+-----------------------+\n'
+ '| "s.remove(x)" | remove the first item from '
+ '*s* | (3) |\n'
+ '| | where "s[i] == '
+ 'x" | |\n'
+ '+--------------------------------+----------------------------------+-----------------------+\n'
+ '| "s.reverse()" | reverses the items of *s* '
+ 'in | (4) |\n'
+ '| | '
+ 'place | |\n'
+ '+--------------------------------+----------------------------------+-----------------------+\n'
+ '\n'
+ 'Notes:\n'
+ '\n'
+ '1. *t* must have the same length as the slice it is replacing.\n'
+ '\n'
+ '2. The optional argument *i* defaults to "-1", so that by '
+ 'default\n'
+ ' the last item is removed and returned.\n'
+ '\n'
+ '3. "remove" raises "ValueError" when *x* is not found in *s*.\n'
+ '\n'
+ '4. The "reverse()" method modifies the sequence in place for\n'
+ ' economy of space when reversing a large sequence. To remind '
+ 'users\n'
+ ' that it operates by side effect, it does not return the '
+ 'reversed\n'
+ ' sequence.\n'
+ '\n'
+ '5. "clear()" and "copy()" are included for consistency with the\n'
+ " interfaces of mutable containers that don't support slicing\n"
+ ' operations (such as "dict" and "set")\n'
+ '\n'
+ ' New in version 3.3: "clear()" and "copy()" methods.\n'
+ '\n'
+ '6. The value *n* is an integer, or an object implementing\n'
+ ' "__index__()". Zero and negative values of *n* clear the '
+ 'sequence.\n'
+ ' Items in the sequence are not copied; they are referenced '
+ 'multiple\n'
+ ' times, as explained for "s * n" under Common Sequence '
+ 'Operations.\n'
+ '\n'
+ '\n'
+ 'Lists\n'
+ '=====\n'
+ '\n'
+ 'Lists are mutable sequences, typically used to store collections '
+ 'of\n'
+ 'homogeneous items (where the precise degree of similarity will '
+ 'vary by\n'
+ 'application).\n'
+ '\n'
+ 'class list([iterable])\n'
+ '\n'
+ ' Lists may be constructed in several ways:\n'
+ '\n'
+ ' * Using a pair of square brackets to denote the empty list: '
+ '"[]"\n'
+ '\n'
+ ' * Using square brackets, separating items with commas: '
+ '"[a]",\n'
+ ' "[a, b, c]"\n'
+ '\n'
+ ' * Using a list comprehension: "[x for x in iterable]"\n'
+ '\n'
+ ' * Using the type constructor: "list()" or "list(iterable)"\n'
+ '\n'
+ ' The constructor builds a list whose items are the same and in '
+ 'the\n'
+ " same order as *iterable*'s items. *iterable* may be either "
+ 'a\n'
+ ' sequence, a container that supports iteration, or an '
+ 'iterator\n'
+ ' object. If *iterable* is already a list, a copy is made and\n'
+ ' returned, similar to "iterable[:]". For example, '
+ '"list(\'abc\')"\n'
+ ' returns "[\'a\', \'b\', \'c\']" and "list( (1, 2, 3) )" '
+ 'returns "[1, 2,\n'
+ ' 3]". If no argument is given, the constructor creates a new '
+ 'empty\n'
+ ' list, "[]".\n'
+ '\n'
+ ' Many other operations also produce lists, including the '
+ '"sorted()"\n'
+ ' built-in.\n'
+ '\n'
+ ' Lists implement all of the common and mutable sequence '
+ 'operations.\n'
+ ' Lists also provide the following additional method:\n'
+ '\n'
+ ' sort(*, key=None, reverse=None)\n'
+ '\n'
+ ' This method sorts the list in place, using only "<" '
+ 'comparisons\n'
+ ' between items. Exceptions are not suppressed - if any '
+ 'comparison\n'
+ ' operations fail, the entire sort operation will fail (and '
+ 'the\n'
+ ' list will likely be left in a partially modified state).\n'
+ '\n'
+ ' "sort()" accepts two arguments that can only be passed by\n'
+ ' keyword (keyword-only arguments):\n'
+ '\n'
+ ' *key* specifies a function of one argument that is used '
+ 'to\n'
+ ' extract a comparison key from each list element (for '
+ 'example,\n'
+ ' "key=str.lower"). The key corresponding to each item in '
+ 'the list\n'
+ ' is calculated once and then used for the entire sorting '
+ 'process.\n'
+ ' The default value of "None" means that list items are '
+ 'sorted\n'
+ ' directly without calculating a separate key value.\n'
+ '\n'
+ ' The "functools.cmp_to_key()" utility is available to '
+ 'convert a\n'
+ ' 2.x style *cmp* function to a *key* function.\n'
+ '\n'
+ ' *reverse* is a boolean value. If set to "True", then the '
+ 'list\n'
+ ' elements are sorted as if each comparison were reversed.\n'
+ '\n'
+ ' This method modifies the sequence in place for economy of '
+ 'space\n'
+ ' when sorting a large sequence. To remind users that it '
+ 'operates\n'
+ ' by side effect, it does not return the sorted sequence '
+ '(use\n'
+ ' "sorted()" to explicitly request a new sorted list '
+ 'instance).\n'
+ '\n'
+ ' The "sort()" method is guaranteed to be stable. A sort '
+ 'is\n'
+ ' stable if it guarantees not to change the relative order '
+ 'of\n'
+ ' elements that compare equal --- this is helpful for '
+ 'sorting in\n'
+ ' multiple passes (for example, sort by department, then by '
+ 'salary\n'
+ ' grade).\n'
+ '\n'
+ ' **CPython implementation detail:** While a list is being '
+ 'sorted,\n'
+ ' the effect of attempting to mutate, or even inspect, the '
+ 'list is\n'
+ ' undefined. The C implementation of Python makes the list '
+ 'appear\n'
+ ' empty for the duration, and raises "ValueError" if it can '
+ 'detect\n'
+ ' that the list has been mutated during a sort.\n'
+ '\n'
+ '\n'
+ 'Tuples\n'
+ '======\n'
+ '\n'
+ 'Tuples are immutable sequences, typically used to store '
+ 'collections of\n'
+ 'heterogeneous data (such as the 2-tuples produced by the '
+ '"enumerate()"\n'
+ 'built-in). Tuples are also used for cases where an immutable '
+ 'sequence\n'
+ 'of homogeneous data is needed (such as allowing storage in a '
+ '"set" or\n'
+ '"dict" instance).\n'
+ '\n'
+ 'class tuple([iterable])\n'
+ '\n'
+ ' Tuples may be constructed in a number of ways:\n'
+ '\n'
+ ' * Using a pair of parentheses to denote the empty tuple: '
+ '"()"\n'
+ '\n'
+ ' * Using a trailing comma for a singleton tuple: "a," or '
+ '"(a,)"\n'
+ '\n'
+ ' * Separating items with commas: "a, b, c" or "(a, b, c)"\n'
+ '\n'
+ ' * Using the "tuple()" built-in: "tuple()" or '
+ '"tuple(iterable)"\n'
+ '\n'
+ ' The constructor builds a tuple whose items are the same and '
+ 'in the\n'
+ " same order as *iterable*'s items. *iterable* may be either "
+ 'a\n'
+ ' sequence, a container that supports iteration, or an '
+ 'iterator\n'
+ ' object. If *iterable* is already a tuple, it is returned\n'
+ ' unchanged. For example, "tuple(\'abc\')" returns "(\'a\', '
+ '\'b\', \'c\')"\n'
+ ' and "tuple( [1, 2, 3] )" returns "(1, 2, 3)". If no argument '
+ 'is\n'
+ ' given, the constructor creates a new empty tuple, "()".\n'
+ '\n'
+ ' Note that it is actually the comma which makes a tuple, not '
+ 'the\n'
+ ' parentheses. The parentheses are optional, except in the '
+ 'empty\n'
+ ' tuple case, or when they are needed to avoid syntactic '
+ 'ambiguity.\n'
+ ' For example, "f(a, b, c)" is a function call with three '
+ 'arguments,\n'
+ ' while "f((a, b, c))" is a function call with a 3-tuple as the '
+ 'sole\n'
+ ' argument.\n'
+ '\n'
+ ' Tuples implement all of the common sequence operations.\n'
+ '\n'
+ 'For heterogeneous collections of data where access by name is '
+ 'clearer\n'
+ 'than access by index, "collections.namedtuple()" may be a more\n'
+ 'appropriate choice than a simple tuple object.\n'
+ '\n'
+ '\n'
+ 'Ranges\n'
+ '======\n'
+ '\n'
+ 'The "range" type represents an immutable sequence of numbers and '
+ 'is\n'
+ 'commonly used for looping a specific number of times in "for" '
+ 'loops.\n'
+ '\n'
+ 'class range(stop)\n'
+ 'class range(start, stop[, step])\n'
+ '\n'
+ ' The arguments to the range constructor must be integers '
+ '(either\n'
+ ' built-in "int" or any object that implements the "__index__"\n'
+ ' special method). If the *step* argument is omitted, it '
+ 'defaults to\n'
+ ' "1". If the *start* argument is omitted, it defaults to "0". '
+ 'If\n'
+ ' *step* is zero, "ValueError" is raised.\n'
+ '\n'
+ ' For a positive *step*, the contents of a range "r" are '
+ 'determined\n'
+ ' by the formula "r[i] = start + step*i" where "i >= 0" and '
+ '"r[i] <\n'
+ ' stop".\n'
+ '\n'
+ ' For a negative *step*, the contents of the range are still\n'
+ ' determined by the formula "r[i] = start + step*i", but the\n'
+ ' constraints are "i >= 0" and "r[i] > stop".\n'
+ '\n'
+ ' A range object will be empty if "r[0]" does not meet the '
+ 'value\n'
+ ' constraint. Ranges do support negative indices, but these '
+ 'are\n'
+ ' interpreted as indexing from the end of the sequence '
+ 'determined by\n'
+ ' the positive indices.\n'
+ '\n'
+ ' Ranges containing absolute values larger than "sys.maxsize" '
+ 'are\n'
+ ' permitted but some features (such as "len()") may raise\n'
+ ' "OverflowError".\n'
+ '\n'
+ ' Range examples:\n'
+ '\n'
+ ' >>> list(range(10))\n'
+ ' [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n'
+ ' >>> list(range(1, 11))\n'
+ ' [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n'
+ ' >>> list(range(0, 30, 5))\n'
+ ' [0, 5, 10, 15, 20, 25]\n'
+ ' >>> list(range(0, 10, 3))\n'
+ ' [0, 3, 6, 9]\n'
+ ' >>> list(range(0, -10, -1))\n'
+ ' [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]\n'
+ ' >>> list(range(0))\n'
+ ' []\n'
+ ' >>> list(range(1, 0))\n'
+ ' []\n'
+ '\n'
+ ' Ranges implement all of the common sequence operations '
+ 'except\n'
+ ' concatenation and repetition (due to the fact that range '
+ 'objects\n'
+ ' can only represent sequences that follow a strict pattern '
+ 'and\n'
+ ' repetition and concatenation will usually violate that '
+ 'pattern).\n'
+ '\n'
+ ' start\n'
+ '\n'
+ ' The value of the *start* parameter (or "0" if the '
+ 'parameter was\n'
+ ' not supplied)\n'
+ '\n'
+ ' stop\n'
+ '\n'
+ ' The value of the *stop* parameter\n'
+ '\n'
+ ' step\n'
+ '\n'
+ ' The value of the *step* parameter (or "1" if the parameter '
+ 'was\n'
+ ' not supplied)\n'
+ '\n'
+ 'The advantage of the "range" type over a regular "list" or '
+ '"tuple" is\n'
+ 'that a "range" object will always take the same (small) amount '
+ 'of\n'
+ 'memory, no matter the size of the range it represents (as it '
+ 'only\n'
+ 'stores the "start", "stop" and "step" values, calculating '
+ 'individual\n'
+ 'items and subranges as needed).\n'
+ '\n'
+ 'Range objects implement the "collections.abc.Sequence" ABC, and\n'
+ 'provide features such as containment tests, element index '
+ 'lookup,\n'
+ 'slicing and support for negative indices (see Sequence Types --- '
+ 'list,\n'
+ 'tuple, range):\n'
+ '\n'
+ '>>> r = range(0, 20, 2)\n'
+ '>>> r\n'
+ 'range(0, 20, 2)\n'
+ '>>> 11 in r\n'
+ 'False\n'
+ '>>> 10 in r\n'
+ 'True\n'
+ '>>> r.index(10)\n'
+ '5\n'
+ '>>> r[5]\n'
+ '10\n'
+ '>>> r[:5]\n'
+ 'range(0, 10, 2)\n'
+ '>>> r[-1]\n'
+ '18\n'
+ '\n'
+ 'Testing range objects for equality with "==" and "!=" compares '
+ 'them as\n'
+ 'sequences. That is, two range objects are considered equal if '
+ 'they\n'
+ 'represent the same sequence of values. (Note that two range '
+ 'objects\n'
+ 'that compare equal might have different "start", "stop" and '
+ '"step"\n'
+ 'attributes, for example "range(0) == range(2, 1, 3)" or '
+ '"range(0, 3,\n'
+ '2) == range(0, 4, 2)".)\n'
+ '\n'
+ 'Changed in version 3.2: Implement the Sequence ABC. Support '
+ 'slicing\n'
+ 'and negative indices. Test "int" objects for membership in '
+ 'constant\n'
+ 'time instead of iterating through all items.\n'
+ '\n'
+ "Changed in version 3.3: Define '==' and '!=' to compare range "
+ 'objects\n'
+ 'based on the sequence of values they define (instead of '
+ 'comparing\n'
+ 'based on object identity).\n'
+ '\n'
+ 'New in version 3.3: The "start", "stop" and "step" attributes.\n'
+ '\n'
+ 'See also:\n'
+ '\n'
+ ' * The linspace recipe shows how to implement a lazy version '
+ 'of\n'
+ ' range that suitable for floating point applications.\n',
+ 'typesseq-mutable': '\n'
+ 'Mutable Sequence Types\n'
+ '**********************\n'
+ '\n'
+ 'The operations in the following table are defined on '
+ 'mutable sequence\n'
+ 'types. The "collections.abc.MutableSequence" ABC is '
+ 'provided to make\n'
+ 'it easier to correctly implement these operations on '
+ 'custom sequence\n'
+ 'types.\n'
+ '\n'
+ 'In the table *s* is an instance of a mutable sequence '
+ 'type, *t* is any\n'
+ 'iterable object and *x* is an arbitrary object that '
+ 'meets any type and\n'
+ 'value restrictions imposed by *s* (for example, '
+ '"bytearray" only\n'
+ 'accepts integers that meet the value restriction "0 <= x '
+ '<= 255").\n'
+ '\n'
+ '+--------------------------------+----------------------------------+-----------------------+\n'
+ '| Operation | '
+ 'Result | Notes '
+ '|\n'
+ '+================================+==================================+=======================+\n'
+ '| "s[i] = x" | item *i* of *s* is '
+ 'replaced by | |\n'
+ '| | '
+ '*x* | '
+ '|\n'
+ '+--------------------------------+----------------------------------+-----------------------+\n'
+ '| "s[i:j] = t" | slice of *s* from *i* '
+ 'to *j* is | |\n'
+ '| | replaced by the '
+ 'contents of the | |\n'
+ '| | iterable '
+ '*t* | |\n'
+ '+--------------------------------+----------------------------------+-----------------------+\n'
+ '| "del s[i:j]" | same as "s[i:j] = '
+ '[]" | |\n'
+ '+--------------------------------+----------------------------------+-----------------------+\n'
+ '| "s[i:j:k] = t" | the elements of '
+ '"s[i:j:k]" are | (1) |\n'
+ '| | replaced by those of '
+ '*t* | |\n'
+ '+--------------------------------+----------------------------------+-----------------------+\n'
+ '| "del s[i:j:k]" | removes the elements '
+ 'of | |\n'
+ '| | "s[i:j:k]" from the '
+ 'list | |\n'
+ '+--------------------------------+----------------------------------+-----------------------+\n'
+ '| "s.append(x)" | appends *x* to the '
+ 'end of the | |\n'
+ '| | sequence (same '
+ 'as | |\n'
+ '| | "s[len(s):len(s)] = '
+ '[x]") | |\n'
+ '+--------------------------------+----------------------------------+-----------------------+\n'
+ '| "s.clear()" | removes all items '
+ 'from "s" (same | (5) |\n'
+ '| | as "del '
+ 's[:]") | |\n'
+ '+--------------------------------+----------------------------------+-----------------------+\n'
+ '| "s.copy()" | creates a shallow '
+ 'copy of "s" | (5) |\n'
+ '| | (same as '
+ '"s[:]") | |\n'
+ '+--------------------------------+----------------------------------+-----------------------+\n'
+ '| "s.extend(t)" or "s += t" | extends *s* with the '
+ 'contents of | |\n'
+ '| | *t* (for the most '
+ 'part the same | |\n'
+ '| | as "s[len(s):len(s)] '
+ '= t") | |\n'
+ '+--------------------------------+----------------------------------+-----------------------+\n'
+ '| "s *= n" | updates *s* with its '
+ 'contents | (6) |\n'
+ '| | repeated *n* '
+ 'times | |\n'
+ '+--------------------------------+----------------------------------+-----------------------+\n'
+ '| "s.insert(i, x)" | inserts *x* into *s* '
+ 'at the | |\n'
+ '| | index given by *i* '
+ '(same as | |\n'
+ '| | "s[i:i] = '
+ '[x]") | |\n'
+ '+--------------------------------+----------------------------------+-----------------------+\n'
+ '| "s.pop([i])" | retrieves the item at '
+ '*i* and | (2) |\n'
+ '| | also removes it from '
+ '*s* | |\n'
+ '+--------------------------------+----------------------------------+-----------------------+\n'
+ '| "s.remove(x)" | remove the first item '
+ 'from *s* | (3) |\n'
+ '| | where "s[i] == '
+ 'x" | |\n'
+ '+--------------------------------+----------------------------------+-----------------------+\n'
+ '| "s.reverse()" | reverses the items of '
+ '*s* in | (4) |\n'
+ '| | '
+ 'place | '
+ '|\n'
+ '+--------------------------------+----------------------------------+-----------------------+\n'
+ '\n'
+ 'Notes:\n'
+ '\n'
+ '1. *t* must have the same length as the slice it is '
+ 'replacing.\n'
+ '\n'
+ '2. The optional argument *i* defaults to "-1", so that '
+ 'by default\n'
+ ' the last item is removed and returned.\n'
+ '\n'
+ '3. "remove" raises "ValueError" when *x* is not found in '
+ '*s*.\n'
+ '\n'
+ '4. The "reverse()" method modifies the sequence in place '
+ 'for\n'
+ ' economy of space when reversing a large sequence. To '
+ 'remind users\n'
+ ' that it operates by side effect, it does not return '
+ 'the reversed\n'
+ ' sequence.\n'
+ '\n'
+ '5. "clear()" and "copy()" are included for consistency '
+ 'with the\n'
+ " interfaces of mutable containers that don't support "
+ 'slicing\n'
+ ' operations (such as "dict" and "set")\n'
+ '\n'
+ ' New in version 3.3: "clear()" and "copy()" methods.\n'
+ '\n'
+ '6. The value *n* is an integer, or an object '
+ 'implementing\n'
+ ' "__index__()". Zero and negative values of *n* clear '
+ 'the sequence.\n'
+ ' Items in the sequence are not copied; they are '
+ 'referenced multiple\n'
+ ' times, as explained for "s * n" under Common Sequence '
+ 'Operations.\n',
+ 'unary': '\n'
+ 'Unary arithmetic and bitwise operations\n'
+ '***************************************\n'
+ '\n'
+ 'All unary arithmetic and bitwise operations have the same '
+ 'priority:\n'
+ '\n'
+ ' u_expr ::= power | "-" u_expr | "+" u_expr | "~" u_expr\n'
+ '\n'
+ 'The unary "-" (minus) operator yields the negation of its numeric\n'
+ 'argument.\n'
+ '\n'
+ 'The unary "+" (plus) operator yields its numeric argument '
+ 'unchanged.\n'
+ '\n'
+ 'The unary "~" (invert) operator yields the bitwise inversion of '
+ 'its\n'
+ 'integer argument. The bitwise inversion of "x" is defined as\n'
+ '"-(x+1)". It only applies to integral numbers.\n'
+ '\n'
+ 'In all three cases, if the argument does not have the proper type, '
+ 'a\n'
+ '"TypeError" exception is raised.\n',
+ 'while': '\n'
+ 'The "while" statement\n'
+ '*********************\n'
+ '\n'
+ 'The "while" statement is used for repeated execution as long as an\n'
+ 'expression is true:\n'
+ '\n'
+ ' while_stmt ::= "while" expression ":" suite\n'
+ ' ["else" ":" suite]\n'
+ '\n'
+ 'This repeatedly tests the expression and, if it is true, executes '
+ 'the\n'
+ 'first suite; if the expression is false (which may be the first '
+ 'time\n'
+ 'it is tested) the suite of the "else" clause, if present, is '
+ 'executed\n'
+ 'and the loop terminates.\n'
+ '\n'
+ 'A "break" statement executed in the first suite terminates the '
+ 'loop\n'
+ 'without executing the "else" clause\'s suite. A "continue" '
+ 'statement\n'
+ 'executed in the first suite skips the rest of the suite and goes '
+ 'back\n'
+ 'to testing the expression.\n',
+ 'with': '\n'
+ 'The "with" statement\n'
+ '********************\n'
+ '\n'
+ 'The "with" statement is used to wrap the execution of a block with\n'
+ 'methods defined by a context manager (see section With Statement\n'
+ 'Context Managers). This allows common "try"..."except"..."finally"\n'
+ 'usage patterns to be encapsulated for convenient reuse.\n'
+ '\n'
+ ' with_stmt ::= "with" with_item ("," with_item)* ":" suite\n'
+ ' with_item ::= expression ["as" target]\n'
+ '\n'
+ 'The execution of the "with" statement with one "item" proceeds as\n'
+ 'follows:\n'
+ '\n'
+ '1. The context expression (the expression given in the "with_item")\n'
+ ' is evaluated to obtain a context manager.\n'
+ '\n'
+ '2. The context manager\'s "__exit__()" is loaded for later use.\n'
+ '\n'
+ '3. The context manager\'s "__enter__()" method is invoked.\n'
+ '\n'
+ '4. If a target was included in the "with" statement, the return\n'
+ ' value from "__enter__()" is assigned to it.\n'
+ '\n'
+ ' Note: The "with" statement guarantees that if the "__enter__()"\n'
+ ' method returns without an error, then "__exit__()" will always '
+ 'be\n'
+ ' called. Thus, if an error occurs during the assignment to the\n'
+ ' target list, it will be treated the same as an error occurring\n'
+ ' within the suite would be. See step 6 below.\n'
+ '\n'
+ '5. The suite is executed.\n'
+ '\n'
+ '6. The context manager\'s "__exit__()" method is invoked. If an\n'
+ ' exception caused the suite to be exited, its type, value, and\n'
+ ' traceback are passed as arguments to "__exit__()". Otherwise, '
+ 'three\n'
+ ' "None" arguments are supplied.\n'
+ '\n'
+ ' If the suite was exited due to an exception, and the return '
+ 'value\n'
+ ' from the "__exit__()" method was false, the exception is '
+ 'reraised.\n'
+ ' If the return value was true, the exception is suppressed, and\n'
+ ' execution continues with the statement following the "with"\n'
+ ' statement.\n'
+ '\n'
+ ' If the suite was exited for any reason other than an exception, '
+ 'the\n'
+ ' return value from "__exit__()" is ignored, and execution '
+ 'proceeds\n'
+ ' at the normal location for the kind of exit that was taken.\n'
+ '\n'
+ 'With more than one item, the context managers are processed as if\n'
+ 'multiple "with" statements were nested:\n'
+ '\n'
+ ' with A() as a, B() as b:\n'
+ ' suite\n'
+ '\n'
+ 'is equivalent to\n'
+ '\n'
+ ' with A() as a:\n'
+ ' with B() as b:\n'
+ ' suite\n'
+ '\n'
+ 'Changed in version 3.1: Support for multiple context expressions.\n'
+ '\n'
+ 'See also:\n'
+ '\n'
+ ' **PEP 343** - The "with" statement\n'
+ ' The specification, background, and examples for the Python '
+ '"with"\n'
+ ' statement.\n',
+ 'yield': '\n'
+ 'The "yield" statement\n'
+ '*********************\n'
+ '\n'
+ ' yield_stmt ::= yield_expression\n'
+ '\n'
+ 'A "yield" statement is semantically equivalent to a yield '
+ 'expression.\n'
+ 'The yield statement can be used to omit the parentheses that would\n'
+ 'otherwise be required in the equivalent yield expression '
+ 'statement.\n'
+ 'For example, the yield statements\n'
+ '\n'
+ ' yield <expr>\n'
+ ' yield from <expr>\n'
+ '\n'
+ 'are equivalent to the yield expression statements\n'
+ '\n'
+ ' (yield <expr>)\n'
+ ' (yield from <expr>)\n'
+ '\n'
+ 'Yield expressions and statements are only used when defining a\n'
+ '*generator* function, and are only used in the body of the '
+ 'generator\n'
+ 'function. Using yield in a function definition is sufficient to '
+ 'cause\n'
+ 'that definition to create a generator function instead of a normal\n'
+ 'function.\n'
+ '\n'
+ 'For full details of "yield" semantics, refer to the Yield '
+ 'expressions\n'
+ 'section.\n'}
diff --git a/Lib/random.py b/Lib/random.py
index 4efbb0a..ad1c916 100644
--- a/Lib/random.py
+++ b/Lib/random.py
@@ -8,6 +8,7 @@
---------
pick random element
pick random sample
+ pick weighted random sample
generate random permutation
distributions on the real line:
@@ -43,12 +44,14 @@ from math import sqrt as _sqrt, acos as _acos, cos as _cos, sin as _sin
from os import urandom as _urandom
from _collections_abc import Set as _Set, Sequence as _Sequence
from hashlib import sha512 as _sha512
+import itertools as _itertools
+import bisect as _bisect
__all__ = ["Random","seed","random","uniform","randint","choice","sample",
"randrange","shuffle","normalvariate","lognormvariate",
"expovariate","vonmisesvariate","gammavariate","triangular",
"gauss","betavariate","paretovariate","weibullvariate",
- "getstate","setstate", "getrandbits",
+ "getstate","setstate", "getrandbits", "choices",
"SystemRandom"]
NV_MAGICCONST = 4 * _exp(-0.5)/_sqrt(2.0)
@@ -105,15 +108,6 @@ class Random(_random.Random):
"""
- if a is None:
- try:
- # Seed with enough bytes to span the 19937 bit
- # state space for the Mersenne Twister
- a = int.from_bytes(_urandom(2500), 'big')
- except NotImplementedError:
- import time
- a = int(time.time() * 256) # use fractional seconds
-
if version == 1 and isinstance(a, (str, bytes)):
x = ord(a[0]) << 7 if a else 0
for c in a:
@@ -121,12 +115,11 @@ class Random(_random.Random):
x ^= len(a)
a = -2 if x == -1 else x
- if version == 2:
- if isinstance(a, (str, bytes, bytearray)):
- if isinstance(a, str):
- a = a.encode()
- a += _sha512(a).digest()
- a = int.from_bytes(a, 'big')
+ if version == 2 and isinstance(a, (str, bytes, bytearray)):
+ if isinstance(a, str):
+ a = a.encode()
+ a += _sha512(a).digest()
+ a = int.from_bytes(a, 'big')
super().seed(a)
self.gauss_next = None
@@ -261,7 +254,7 @@ class Random(_random.Random):
try:
i = self._randbelow(len(seq))
except ValueError:
- raise IndexError('Cannot choose from an empty sequence')
+ raise IndexError('Cannot choose from an empty sequence') from None
return seq[i]
def shuffle(self, x, random=None):
@@ -321,7 +314,7 @@ class Random(_random.Random):
randbelow = self._randbelow
n = len(population)
if not 0 <= k <= n:
- raise ValueError("Sample larger than population")
+ raise ValueError("Sample larger than population or is negative")
result = [None] * k
setsize = 21 # size of a small set minus size of an empty list
if k > 5:
@@ -344,6 +337,28 @@ class Random(_random.Random):
result[i] = population[j]
return result
+ def choices(self, population, weights=None, *, cum_weights=None, k=1):
+ """Return a k sized list of population elements chosen with replacement.
+
+ If the relative weights or cumulative weights are not specified,
+ the selections are made with equal probability.
+
+ """
+ random = self.random
+ if cum_weights is None:
+ if weights is None:
+ _int = int
+ total = len(population)
+ return [population[_int(random() * total)] for i in range(k)]
+ cum_weights = list(_itertools.accumulate(weights))
+ elif weights is not None:
+ raise TypeError('Cannot specify both weights and cumulative weights')
+ if len(cum_weights) != len(population):
+ raise ValueError('The number of weights does not match the population')
+ bisect = _bisect.bisect
+ total = cum_weights[-1]
+ return [population[bisect(cum_weights, random() * total)] for i in range(k)]
+
## -------------------- real-valued distributions -------------------
## -------------------- uniform distribution -------------------
@@ -615,11 +630,11 @@ class Random(_random.Random):
# This version due to Janne Sinkkonen, and matches all the std
# texts (e.g., Knuth Vol 2 Ed 3 pg 134 "the beta distribution").
- y = self.gammavariate(alpha, 1.)
+ y = self.gammavariate(alpha, 1.0)
if y == 0:
return 0.0
else:
- return y / (y + self.gammavariate(beta, 1.))
+ return y / (y + self.gammavariate(beta, 1.0))
## -------------------- Pareto --------------------
@@ -734,6 +749,7 @@ choice = _inst.choice
randrange = _inst.randrange
sample = _inst.sample
shuffle = _inst.shuffle
+choices = _inst.choices
normalvariate = _inst.normalvariate
lognormvariate = _inst.lognormvariate
expovariate = _inst.expovariate
diff --git a/Lib/re.py b/Lib/re.py
index dde8901..d321cff 100644
--- a/Lib/re.py
+++ b/Lib/re.py
@@ -119,9 +119,10 @@ This module also defines an exception 'error'.
"""
-import sys
+import enum
import sre_compile
import sre_parse
+import functools
try:
import _locale
except ImportError:
@@ -138,18 +139,26 @@ __all__ = [
__version__ = "2.2.1"
-# flags
-A = ASCII = sre_compile.SRE_FLAG_ASCII # assume ascii "locale"
-I = IGNORECASE = sre_compile.SRE_FLAG_IGNORECASE # ignore case
-L = LOCALE = sre_compile.SRE_FLAG_LOCALE # assume current 8-bit locale
-U = UNICODE = sre_compile.SRE_FLAG_UNICODE # assume unicode "locale"
-M = MULTILINE = sre_compile.SRE_FLAG_MULTILINE # make anchors look for newline
-S = DOTALL = sre_compile.SRE_FLAG_DOTALL # make dot match newline
-X = VERBOSE = sre_compile.SRE_FLAG_VERBOSE # ignore whitespace and comments
-
-# sre extensions (experimental, don't rely on these)
-T = TEMPLATE = sre_compile.SRE_FLAG_TEMPLATE # disable backtracking
-DEBUG = sre_compile.SRE_FLAG_DEBUG # dump pattern after compilation
+class RegexFlag(enum.IntFlag):
+ ASCII = sre_compile.SRE_FLAG_ASCII # assume ascii "locale"
+ IGNORECASE = sre_compile.SRE_FLAG_IGNORECASE # ignore case
+ LOCALE = sre_compile.SRE_FLAG_LOCALE # assume current 8-bit locale
+ UNICODE = sre_compile.SRE_FLAG_UNICODE # assume unicode "locale"
+ MULTILINE = sre_compile.SRE_FLAG_MULTILINE # make anchors look for newline
+ DOTALL = sre_compile.SRE_FLAG_DOTALL # make dot match newline
+ VERBOSE = sre_compile.SRE_FLAG_VERBOSE # ignore whitespace and comments
+ A = ASCII
+ I = IGNORECASE
+ L = LOCALE
+ U = UNICODE
+ M = MULTILINE
+ S = DOTALL
+ X = VERBOSE
+ # sre extensions (experimental, don't rely on these)
+ TEMPLATE = sre_compile.SRE_FLAG_TEMPLATE # disable backtracking
+ T = TEMPLATE
+ DEBUG = sre_compile.SRE_FLAG_DEBUG # dump pattern after compilation
+globals().update(RegexFlag.__members__)
# sre exception
error = sre_compile.error
@@ -226,7 +235,7 @@ def compile(pattern, flags=0):
def purge():
"Clear the regular expression caches"
_cache.clear()
- _cache_repl.clear()
+ _compile_repl.cache_clear()
def template(pattern, flags=0):
"Compile a template pattern, returning a pattern object"
@@ -270,7 +279,6 @@ def escape(pattern):
# internals
_cache = {}
-_cache_repl = {}
_pattern_type = type(sre_compile.compile("", 0))
@@ -303,17 +311,10 @@ def _compile(pattern, flags):
_cache[type(pattern), pattern, flags] = p, loc
return p
+@functools.lru_cache(_MAXCACHE)
def _compile_repl(repl, pattern):
# internal: compile replacement pattern
- try:
- return _cache_repl[repl, pattern]
- except KeyError:
- pass
- p = sre_parse.parse_template(repl, pattern)
- if len(_cache_repl) >= _MAXCACHE:
- _cache_repl.clear()
- _cache_repl[repl, pattern] = p
- return p
+ return sre_parse.parse_template(repl, pattern)
def _expand(pattern, match, template):
# internal: match.expand implementation hook
@@ -353,7 +354,7 @@ class Scanner:
for phrase, action in lexicon:
gid = s.opengroup()
p.append(sre_parse.SubPattern(s, [
- (SUBPATTERN, (gid, sre_parse.parse(phrase, flags))),
+ (SUBPATTERN, (gid, 0, 0, sre_parse.parse(phrase, flags))),
]))
s.closegroup(gid, p[-1])
p = sre_parse.SubPattern(s, [(BRANCH, (None, p))])
diff --git a/Lib/rlcompleter.py b/Lib/rlcompleter.py
index 401a626..bca4a7b 100644
--- a/Lib/rlcompleter.py
+++ b/Lib/rlcompleter.py
@@ -113,6 +113,12 @@ class Completer:
for word in keyword.kwlist:
if word[:n] == text:
seen.add(word)
+ if word in {'finally', 'try'}:
+ word = word + ':'
+ elif word not in {'False', 'None', 'True',
+ 'break', 'continue', 'pass',
+ 'else'}:
+ word = word + ' '
matches.append(word)
for nspace in [self.namespace, builtins.__dict__]:
for word, val in nspace.items():
@@ -152,14 +158,30 @@ class Completer:
words.update(get_class_members(thisobject.__class__))
matches = []
n = len(attr)
- for word in words:
- if word[:n] == attr:
- try:
- val = getattr(thisobject, word)
- except Exception:
- continue # Exclude properties that are not set
- word = self._callable_postfix(val, "%s.%s" % (expr, word))
- matches.append(word)
+ if attr == '':
+ noprefix = '_'
+ elif attr == '_':
+ noprefix = '__'
+ else:
+ noprefix = None
+ while True:
+ for word in words:
+ if (word[:n] == attr and
+ not (noprefix and word[:n+1] == noprefix)):
+ match = "%s.%s" % (expr, word)
+ try:
+ val = getattr(thisobject, word)
+ except Exception:
+ pass # Include even if attribute not set
+ else:
+ match = self._callable_postfix(val, match)
+ matches.append(match)
+ if matches or not noprefix:
+ break
+ if noprefix == '_':
+ noprefix = '__'
+ else:
+ noprefix = None
matches.sort()
return matches
diff --git a/Lib/sched.py b/Lib/sched.py
index b47648d..3d8c011 100644
--- a/Lib/sched.py
+++ b/Lib/sched.py
@@ -23,11 +23,6 @@ The action function may be an instance method so it
has another way to reference private data (besides global variables).
"""
-# XXX The timefunc and delayfunc should have been defined as methods
-# XXX so you can define new kinds of schedulers using subclassing
-# XXX instead of having to define a module or class just to hold
-# XXX the global state of your particular time and delay functions.
-
import time
import heapq
from collections import namedtuple
@@ -40,12 +35,24 @@ from time import monotonic as _time
__all__ = ["scheduler"]
class Event(namedtuple('Event', 'time, priority, action, argument, kwargs')):
+ __slots__ = []
def __eq__(s, o): return (s.time, s.priority) == (o.time, o.priority)
def __lt__(s, o): return (s.time, s.priority) < (o.time, o.priority)
def __le__(s, o): return (s.time, s.priority) <= (o.time, o.priority)
def __gt__(s, o): return (s.time, s.priority) > (o.time, o.priority)
def __ge__(s, o): return (s.time, s.priority) >= (o.time, o.priority)
+Event.time.__doc__ = ('''Numeric type compatible with the return value of the
+timefunc function passed to the constructor.''')
+Event.priority.__doc__ = ('''Events scheduled for the same time will be executed
+in the order of their priority.''')
+Event.action.__doc__ = ('''Executing the event means executing
+action(*argument, **kwargs)''')
+Event.argument.__doc__ = ('''argument is a sequence holding the positional
+arguments for the action.''')
+Event.kwargs.__doc__ = ('''kwargs is a dictionary holding the keyword
+arguments for the action.''')
+
_sentinel = object()
class scheduler:
diff --git a/Lib/secrets.py b/Lib/secrets.py
new file mode 100644
index 0000000..1304342
--- /dev/null
+++ b/Lib/secrets.py
@@ -0,0 +1,73 @@
+"""Generate cryptographically strong pseudo-random numbers suitable for
+managing secrets such as account authentication, tokens, and similar.
+
+See PEP 506 for more information.
+https://www.python.org/dev/peps/pep-0506/
+
+"""
+
+__all__ = ['choice', 'randbelow', 'randbits', 'SystemRandom',
+ 'token_bytes', 'token_hex', 'token_urlsafe',
+ 'compare_digest',
+ ]
+
+
+import base64
+import binascii
+import os
+
+from hmac import compare_digest
+from random import SystemRandom
+
+_sysrand = SystemRandom()
+
+randbits = _sysrand.getrandbits
+choice = _sysrand.choice
+
+def randbelow(exclusive_upper_bound):
+ """Return a random int in the range [0, n)."""
+ if exclusive_upper_bound <= 0:
+ raise ValueError("Upper bound must be positive.")
+ return _sysrand._randbelow(exclusive_upper_bound)
+
+DEFAULT_ENTROPY = 32 # number of bytes to return by default
+
+def token_bytes(nbytes=None):
+ """Return a random byte string containing *nbytes* bytes.
+
+ If *nbytes* is ``None`` or not supplied, a reasonable
+ default is used.
+
+ >>> token_bytes(16) #doctest:+SKIP
+ b'\\xebr\\x17D*t\\xae\\xd4\\xe3S\\xb6\\xe2\\xebP1\\x8b'
+
+ """
+ if nbytes is None:
+ nbytes = DEFAULT_ENTROPY
+ return os.urandom(nbytes)
+
+def token_hex(nbytes=None):
+ """Return a random text string, in hexadecimal.
+
+ The string has *nbytes* random bytes, each byte converted to two
+ hex digits. If *nbytes* is ``None`` or not supplied, a reasonable
+ default is used.
+
+ >>> token_hex(16) #doctest:+SKIP
+ 'f9bf78b9a18ce6d46a0cd2b0b86df9da'
+
+ """
+ return binascii.hexlify(token_bytes(nbytes)).decode('ascii')
+
+def token_urlsafe(nbytes=None):
+ """Return a random URL-safe text string, in Base64 encoding.
+
+ The string has *nbytes* random bytes. If *nbytes* is ``None``
+ or not supplied, a reasonable default is used.
+
+ >>> token_urlsafe(16) #doctest:+SKIP
+ 'Drmhze6EPcv0fN_81Bj-nA'
+
+ """
+ tok = token_bytes(nbytes)
+ return base64.urlsafe_b64encode(tok).rstrip(b'=').decode('ascii')
diff --git a/Lib/shlex.py b/Lib/shlex.py
index ecd2efd..2c9786c 100644
--- a/Lib/shlex.py
+++ b/Lib/shlex.py
@@ -5,6 +5,7 @@
# push_source() and pop_source() made explicit by ESR, January 2001.
# Posix compliance, split(), string arguments, and
# iterator interface by Gustavo Niemeyer, April 2003.
+# changes to tokenize more like Posix shells by Vinay Sajip, July 2016.
import os
import re
@@ -17,7 +18,8 @@ __all__ = ["shlex", "split", "quote"]
class shlex:
"A lexical analyzer class for simple shell-like syntaxes."
- def __init__(self, instream=None, infile=None, posix=False):
+ def __init__(self, instream=None, infile=None, posix=False,
+ punctuation_chars=False):
if isinstance(instream, str):
instream = StringIO(instream)
if instream is not None:
@@ -49,6 +51,19 @@ class shlex:
self.token = ''
self.filestack = deque()
self.source = None
+ if not punctuation_chars:
+ punctuation_chars = ''
+ elif punctuation_chars is True:
+ punctuation_chars = '();<>|&'
+ self.punctuation_chars = punctuation_chars
+ if punctuation_chars:
+ # _pushback_chars is a push back queue used by lookahead logic
+ self._pushback_chars = deque()
+ # these chars added because allowed in file names, args, wildcards
+ self.wordchars += '~-./*?='
+ #remove any punctuation chars from wordchars
+ t = self.wordchars.maketrans(dict.fromkeys(punctuation_chars))
+ self.wordchars = self.wordchars.translate(t)
def push_token(self, tok):
"Push a token onto the stack popped by the get_token method"
@@ -115,12 +130,15 @@ class shlex:
quoted = False
escapedstate = ' '
while True:
- nextchar = self.instream.read(1)
+ if self.punctuation_chars and self._pushback_chars:
+ nextchar = self._pushback_chars.pop()
+ else:
+ nextchar = self.instream.read(1)
if nextchar == '\n':
- self.lineno = self.lineno + 1
+ self.lineno += 1
if self.debug >= 3:
- print("shlex: in state", repr(self.state), \
- "I see character:", repr(nextchar))
+ print("shlex: in state %r I see character: %r" % (self.state,
+ nextchar))
if self.state is None:
self.token = '' # past end of file
break
@@ -137,13 +155,16 @@ class shlex:
continue
elif nextchar in self.commenters:
self.instream.readline()
- self.lineno = self.lineno + 1
+ self.lineno += 1
elif self.posix and nextchar in self.escape:
escapedstate = 'a'
self.state = nextchar
elif nextchar in self.wordchars:
self.token = nextchar
self.state = 'a'
+ elif nextchar in self.punctuation_chars:
+ self.token = nextchar
+ self.state = 'c'
elif nextchar in self.quotes:
if not self.posix:
self.token = nextchar
@@ -166,17 +187,17 @@ class shlex:
raise ValueError("No closing quotation")
if nextchar == self.state:
if not self.posix:
- self.token = self.token + nextchar
+ self.token += nextchar
self.state = ' '
break
else:
self.state = 'a'
- elif self.posix and nextchar in self.escape and \
- self.state in self.escapedquotes:
+ elif (self.posix and nextchar in self.escape and self.state
+ in self.escapedquotes):
escapedstate = self.state
self.state = nextchar
else:
- self.token = self.token + nextchar
+ self.token += nextchar
elif self.state in self.escape:
if not nextchar: # end of file
if self.debug >= 2:
@@ -185,12 +206,12 @@ class shlex:
raise ValueError("No escaped character")
# In posix shells, only the quote itself or the escape
# character may be escaped within quotes.
- if escapedstate in self.quotes and \
- nextchar != self.state and nextchar != escapedstate:
- self.token = self.token + self.state
- self.token = self.token + nextchar
+ if (escapedstate in self.quotes and
+ nextchar != self.state and nextchar != escapedstate):
+ self.token += self.state
+ self.token += nextchar
self.state = escapedstate
- elif self.state == 'a':
+ elif self.state in ('a', 'c'):
if not nextchar:
self.state = None # end of file
break
@@ -204,23 +225,34 @@ class shlex:
continue
elif nextchar in self.commenters:
self.instream.readline()
- self.lineno = self.lineno + 1
+ self.lineno += 1
if self.posix:
self.state = ' '
if self.token or (self.posix and quoted):
break # emit current token
else:
continue
+ elif self.state == 'c':
+ if nextchar in self.punctuation_chars:
+ self.token += nextchar
+ else:
+ if nextchar not in self.whitespace:
+ self._pushback_chars.append(nextchar)
+ self.state = ' '
+ break
elif self.posix and nextchar in self.quotes:
self.state = nextchar
elif self.posix and nextchar in self.escape:
escapedstate = 'a'
self.state = nextchar
- elif nextchar in self.wordchars or nextchar in self.quotes \
- or self.whitespace_split:
- self.token = self.token + nextchar
+ elif (nextchar in self.wordchars or nextchar in self.quotes
+ or self.whitespace_split):
+ self.token += nextchar
else:
- self.pushback.appendleft(nextchar)
+ if self.punctuation_chars:
+ self._pushback_chars.append(nextchar)
+ else:
+ self.pushback.appendleft(nextchar)
if self.debug >= 2:
print("shlex: I see punctuation in word state")
self.state = ' '
diff --git a/Lib/shutil.py b/Lib/shutil.py
index 16ca80b..bd4760f 100644
--- a/Lib/shutil.py
+++ b/Lib/shutil.py
@@ -981,6 +981,9 @@ if hasattr(os, 'statvfs'):
__all__.append('disk_usage')
_ntuple_diskusage = collections.namedtuple('usage', 'total used free')
+ _ntuple_diskusage.total.__doc__ = 'Total space in bytes'
+ _ntuple_diskusage.used.__doc__ = 'Used space in bytes'
+ _ntuple_diskusage.free.__doc__ = 'Free space in bytes'
def disk_usage(path):
"""Return disk usage statistics about the given path.
diff --git a/Lib/site-packages/README.txt b/Lib/site-packages/README.txt
new file mode 100644
index 0000000..273f625
--- /dev/null
+++ b/Lib/site-packages/README.txt
@@ -0,0 +1,2 @@
+This directory exists so that 3rd party packages can be installed
+here. Read the source for site.py for more details.
diff --git a/Lib/site.py b/Lib/site.py
index 3f78ef5..0fc9200 100644
--- a/Lib/site.py
+++ b/Lib/site.py
@@ -60,7 +60,8 @@ omitted because it is not mentioned in either path configuration file.
The readline module is also automatically configured to enable
completion for systems that support it. This can be overridden in
-sitecustomize, usercustomize or PYTHONSTARTUP.
+sitecustomize, usercustomize or PYTHONSTARTUP. Starting Python in
+isolated mode (-I) disables automatic readline configuration.
After these operations, an attempt is made to import a module
named sitecustomize, which can perform arbitrary additional
@@ -131,13 +132,13 @@ def removeduppaths():
def _init_pathinfo():
- """Return a set containing all existing directory entries from sys.path"""
+ """Return a set containing all existing file system items from sys.path."""
d = set()
- for dir in sys.path:
+ for item in sys.path:
try:
- if os.path.isdir(dir):
- dir, dircase = makepath(dir)
- d.add(dircase)
+ if os.path.exists(item):
+ _, itemcase = makepath(item)
+ d.add(itemcase)
except TypeError:
continue
return d
@@ -150,9 +151,9 @@ def addpackage(sitedir, name, known_paths):
"""
if known_paths is None:
known_paths = _init_pathinfo()
- reset = 1
+ reset = True
else:
- reset = 0
+ reset = False
fullname = os.path.join(sitedir, name)
try:
f = open(fullname, "r")
@@ -190,9 +191,9 @@ def addsitedir(sitedir, known_paths=None):
'sitedir'"""
if known_paths is None:
known_paths = _init_pathinfo()
- reset = 1
+ reset = True
else:
- reset = 0
+ reset = False
sitedir, sitedircase = makepath(sitedir)
if not sitedircase in known_paths:
sys.path.append(sitedir) # Add path component
@@ -304,7 +305,7 @@ def getsitepackages(prefixes=None):
if os.sep == '/':
sitepackages.append(os.path.join(prefix, "lib",
- "python" + sys.version[:3],
+ "python%d.%d" % sys.version_info[:2],
"site-packages"))
else:
sitepackages.append(prefix)
@@ -317,7 +318,7 @@ def getsitepackages(prefixes=None):
if framework:
sitepackages.append(
os.path.join("/Library", framework,
- sys.version[:3], "site-packages"))
+ '%d.%d' % sys.version_info[:2], "site-packages"))
return sitepackages
def addsitepackages(known_paths, prefixes=None):
@@ -335,9 +336,7 @@ def setquit():
The repr of each object contains a hint at how it works.
"""
- if os.sep == ':':
- eof = 'Cmd-Q'
- elif os.sep == '\\':
+ if os.sep == '\\':
eof = 'Ctrl-Z plus Return'
else:
eof = 'Ctrl-D (i.e. EOF)'
@@ -423,23 +422,6 @@ def enablerlcompleter():
sys.__interactivehook__ = register_readline
-def aliasmbcs():
- """On Windows, some default encodings are not provided by Python,
- while they are always available as "mbcs" in each locale. Make
- them usable by aliasing to "mbcs" in such a case."""
- if sys.platform == 'win32':
- import _bootlocale, codecs
- enc = _bootlocale.getpreferredencoding(False)
- if enc.startswith('cp'): # "cp***" ?
- try:
- codecs.lookup(enc)
- except LookupError:
- import encodings
- encodings._cache[enc] = encodings._unknown
- encodings.aliases.aliases[enc] = 'mbcs'
-
-CONFIG_LINE = r'^(?P<key>(\w|[-_])+)\s*=\s*(?P<value>.*)\s*$'
-
def venv(known_paths):
global PREFIXES, ENABLE_USER_SITE
@@ -461,29 +443,20 @@ def venv(known_paths):
]
if candidate_confs:
- import re
- config_line = re.compile(CONFIG_LINE)
virtual_conf = candidate_confs[0]
system_site = "true"
# Issue 25185: Use UTF-8, as that's what the venv module uses when
# writing the file.
with open(virtual_conf, encoding='utf-8') as f:
for line in f:
- line = line.strip()
- m = config_line.match(line)
- if m:
- d = m.groupdict()
- key, value = d['key'].lower(), d['value']
+ if '=' in line:
+ key, _, value = line.partition('=')
+ key = key.strip().lower()
+ value = value.strip()
if key == 'include-system-site-packages':
system_site = value.lower()
elif key == 'home':
sys._home = value
- elif key == 'applocal' and value.lower() == 'true':
- # App-local installs use the exe_dir as prefix,
- # not one level higher, and do not use system
- # site packages.
- site_prefix = exe_dir
- system_site = 'false'
sys.prefix = sys.exec_prefix = site_prefix
@@ -504,11 +477,15 @@ def venv(known_paths):
def execsitecustomize():
"""Run custom site specific code, if available."""
try:
- import sitecustomize
- except ImportError:
- pass
+ try:
+ import sitecustomize
+ except ImportError as exc:
+ if exc.name == 'sitecustomize':
+ pass
+ else:
+ raise
except Exception as err:
- if os.environ.get("PYTHONVERBOSE"):
+ if sys.flags.verbose:
sys.excepthook(*sys.exc_info())
else:
sys.stderr.write(
@@ -520,11 +497,15 @@ def execsitecustomize():
def execusercustomize():
"""Run custom user specific code, if available."""
try:
- import usercustomize
- except ImportError:
- pass
+ try:
+ import usercustomize
+ except ImportError as exc:
+ if exc.name == 'usercustomize':
+ pass
+ else:
+ raise
except Exception as err:
- if os.environ.get("PYTHONVERBOSE"):
+ if sys.flags.verbose:
sys.excepthook(*sys.exc_info())
else:
sys.stderr.write(
@@ -551,13 +532,13 @@ def main():
setquit()
setcopyright()
sethelper()
- enablerlcompleter()
- aliasmbcs()
+ if not sys.flags.isolated:
+ enablerlcompleter()
execsitecustomize()
if ENABLE_USER_SITE:
execusercustomize()
-# Prevent edition of sys.path when python was started with -S and
+# Prevent extending of sys.path when python was started with -S and
# site is imported later.
if not sys.flags.no_site:
main()
diff --git a/Lib/smtpd.py b/Lib/smtpd.py
index 732066e..8103ca9 100755
--- a/Lib/smtpd.py
+++ b/Lib/smtpd.py
@@ -89,7 +89,10 @@ import collections
from warnings import warn
from email._header_value_parser import get_addr_spec, get_angle_addr
-__all__ = ["SMTPServer","DebuggingServer","PureProxy","MailmanProxy"]
+__all__ = [
+ "SMTPChannel", "SMTPServer", "DebuggingServer", "PureProxy",
+ "MailmanProxy",
+]
program = sys.argv[0]
__version__ = 'Python SMTP proxy version 0.3'
@@ -128,24 +131,17 @@ class SMTPChannel(asynchat.async_chat):
return self.command_size_limit
def __init__(self, server, conn, addr, data_size_limit=DATA_SIZE_DEFAULT,
- map=None, enable_SMTPUTF8=False, decode_data=None):
+ map=None, enable_SMTPUTF8=False, decode_data=False):
asynchat.async_chat.__init__(self, conn, map=map)
self.smtp_server = server
self.conn = conn
self.addr = addr
self.data_size_limit = data_size_limit
self.enable_SMTPUTF8 = enable_SMTPUTF8
- if enable_SMTPUTF8:
- if decode_data:
- raise ValueError("decode_data and enable_SMTPUTF8 cannot"
- " be set to True at the same time")
- decode_data = False
- if decode_data is None:
- warn("The decode_data default of True will change to False in 3.6;"
- " specify an explicit value for this keyword",
- DeprecationWarning, 2)
- decode_data = True
self._decode_data = decode_data
+ if enable_SMTPUTF8 and decode_data:
+ raise ValueError("decode_data and enable_SMTPUTF8 cannot"
+ " be set to True at the same time")
if decode_data:
self._emptystring = ''
self._linesep = '\r\n'
@@ -635,23 +631,15 @@ class SMTPServer(asyncore.dispatcher):
def __init__(self, localaddr, remoteaddr,
data_size_limit=DATA_SIZE_DEFAULT, map=None,
- enable_SMTPUTF8=False, decode_data=None):
+ enable_SMTPUTF8=False, decode_data=False):
self._localaddr = localaddr
self._remoteaddr = remoteaddr
self.data_size_limit = data_size_limit
self.enable_SMTPUTF8 = enable_SMTPUTF8
- if enable_SMTPUTF8:
- if decode_data:
- raise ValueError("The decode_data and enable_SMTPUTF8"
- " parameters cannot be set to True at the"
- " same time.")
- decode_data = False
- if decode_data is None:
- warn("The decode_data default of True will change to False in 3.6;"
- " specify an explicit value for this keyword",
- DeprecationWarning, 2)
- decode_data = True
self._decode_data = decode_data
+ if enable_SMTPUTF8 and decode_data:
+ raise ValueError("decode_data and enable_SMTPUTF8 cannot"
+ " be set to True at the same time")
asyncore.dispatcher.__init__(self, map=map)
try:
gai_results = socket.getaddrinfo(*localaddr,
@@ -698,9 +686,9 @@ class SMTPServer(asyncore.dispatcher):
containing a `.' followed by other text has had the leading dot
removed.
- kwargs is a dictionary containing additional information. It is empty
- unless decode_data=False or enable_SMTPUTF8=True was given as init
- parameter, in which case ut will contain the following keys:
+ kwargs is a dictionary containing additional information. It is
+ empty if decode_data=True was given as init parameter, otherwise
+ it will contain the following keys:
'mail_options': list of parameters to the mail command. All
elements are uppercase strings. Example:
['BODY=8BITMIME', 'SMTPUTF8'].
diff --git a/Lib/smtplib.py b/Lib/smtplib.py
index 5b9e665..f7c2c77 100755
--- a/Lib/smtplib.py
+++ b/Lib/smtplib.py
@@ -759,6 +759,10 @@ class SMTP:
if context is not None and certfile is not None:
raise ValueError("context and certfile arguments are mutually "
"exclusive")
+ if keyfile is not None or certfile is not None:
+ import warnings
+ warnings.warn("keyfile and certfile are deprecated, use a"
+ "custom context instead", DeprecationWarning, 2)
if context is None:
context = ssl._create_stdlib_context(certfile=certfile,
keyfile=keyfile)
@@ -1011,6 +1015,10 @@ if _have_ssl:
if context is not None and certfile is not None:
raise ValueError("context and certfile arguments are mutually "
"exclusive")
+ if keyfile is not None or certfile is not None:
+ import warnings
+ warnings.warn("keyfile and certfile are deprecated, use a"
+ "custom context instead", DeprecationWarning, 2)
self.keyfile = keyfile
self.certfile = certfile
if context is None:
diff --git a/Lib/sndhdr.py b/Lib/sndhdr.py
index e5901ec..7ecafb4 100644
--- a/Lib/sndhdr.py
+++ b/Lib/sndhdr.py
@@ -37,6 +37,18 @@ from collections import namedtuple
SndHeaders = namedtuple('SndHeaders',
'filetype framerate nchannels nframes sampwidth')
+SndHeaders.filetype.__doc__ = ("""The value for type indicates the data type
+and will be one of the strings 'aifc', 'aiff', 'au','hcom',
+'sndr', 'sndt', 'voc', 'wav', '8svx', 'sb', 'ub', or 'ul'.""")
+SndHeaders.framerate.__doc__ = ("""The sampling_rate will be either the actual
+value or 0 if unknown or difficult to decode.""")
+SndHeaders.nchannels.__doc__ = ("""The number of channels or 0 if it cannot be
+determined or if the value is difficult to decode.""")
+SndHeaders.nframes.__doc__ = ("""The value for frames will be either the number
+of frames or -1.""")
+SndHeaders.sampwidth.__doc__ = ("""Either the sample size in bits or
+'A' for A-LAW or 'U' for u-LAW.""")
+
def what(filename):
"""Guess the type of a sound file."""
res = whathdr(filename)
diff --git a/Lib/socket.py b/Lib/socket.py
index a9cab32..740e717 100644
--- a/Lib/socket.py
+++ b/Lib/socket.py
@@ -50,7 +50,7 @@ import _socket
from _socket import *
import os, sys, io, selectors
-from enum import IntEnum
+from enum import IntEnum, IntFlag
try:
import errno
@@ -80,6 +80,16 @@ IntEnum._convert(
__name__,
lambda C: C.isupper() and C.startswith('SOCK_'))
+IntFlag._convert(
+ 'MsgFlag',
+ __name__,
+ lambda C: C.isupper() and C.startswith('MSG_'))
+
+IntFlag._convert(
+ 'AddressInfo',
+ __name__,
+ lambda C: C.isupper() and C.startswith('AI_'))
+
_LOCALHOST = '127.0.0.1'
_LOCALHOST_V6 = '::1'
diff --git a/Lib/socketserver.py b/Lib/socketserver.py
index 8808813..41a3766 100644
--- a/Lib/socketserver.py
+++ b/Lib/socketserver.py
@@ -127,16 +127,20 @@ import socket
import selectors
import os
import errno
+import sys
try:
import threading
except ImportError:
import dummy_threading as threading
+from io import BufferedIOBase
from time import monotonic as time
-__all__ = ["BaseServer", "TCPServer", "UDPServer", "ForkingUDPServer",
- "ForkingTCPServer", "ThreadingUDPServer", "ThreadingTCPServer",
+__all__ = ["BaseServer", "TCPServer", "UDPServer",
+ "ThreadingUDPServer", "ThreadingTCPServer",
"BaseRequestHandler", "StreamRequestHandler",
- "DatagramRequestHandler", "ThreadingMixIn", "ForkingMixIn"]
+ "DatagramRequestHandler", "ThreadingMixIn"]
+if hasattr(os, "fork"):
+ __all__.extend(["ForkingUDPServer","ForkingTCPServer", "ForkingMixIn"])
if hasattr(socket, "AF_UNIX"):
__all__.extend(["UnixStreamServer","UnixDatagramServer",
"ThreadingUnixStreamServer",
@@ -311,9 +315,12 @@ class BaseServer:
if self.verify_request(request, client_address):
try:
self.process_request(request, client_address)
- except:
+ except Exception:
self.handle_error(request, client_address)
self.shutdown_request(request)
+ except:
+ self.shutdown_request(request)
+ raise
else:
self.shutdown_request(request)
@@ -367,12 +374,18 @@ class BaseServer:
The default is to print a traceback and continue.
"""
- print('-'*40)
- print('Exception happened during processing of request from', end=' ')
- print(client_address)
+ print('-'*40, file=sys.stderr)
+ print('Exception happened during processing of request from',
+ client_address, file=sys.stderr)
import traceback
- traceback.print_exc() # XXX But this goes to stderr!
- print('-'*40)
+ traceback.print_exc()
+ print('-'*40, file=sys.stderr)
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *args):
+ self.server_close()
class TCPServer(BaseServer):
@@ -527,85 +540,86 @@ class UDPServer(TCPServer):
# No need to close anything.
pass
-class ForkingMixIn:
-
- """Mix-in class to handle each request in a new process."""
-
- timeout = 300
- active_children = None
- max_children = 40
-
- def collect_children(self):
- """Internal routine to wait for children that have exited."""
- if self.active_children is None:
- return
-
- # If we're above the max number of children, wait and reap them until
- # we go back below threshold. Note that we use waitpid(-1) below to be
- # able to collect children in size(<defunct children>) syscalls instead
- # of size(<children>): the downside is that this might reap children
- # which we didn't spawn, which is why we only resort to this when we're
- # above max_children.
- while len(self.active_children) >= self.max_children:
- try:
- pid, _ = os.waitpid(-1, 0)
- self.active_children.discard(pid)
- except ChildProcessError:
- # we don't have any children, we're done
- self.active_children.clear()
- except OSError:
- break
-
- # Now reap all defunct children.
- for pid in self.active_children.copy():
- try:
- pid, _ = os.waitpid(pid, os.WNOHANG)
- # if the child hasn't exited yet, pid will be 0 and ignored by
- # discard() below
- self.active_children.discard(pid)
- except ChildProcessError:
- # someone else reaped it
- self.active_children.discard(pid)
- except OSError:
- pass
-
- def handle_timeout(self):
- """Wait for zombies after self.timeout seconds of inactivity.
+if hasattr(os, "fork"):
+ class ForkingMixIn:
+ """Mix-in class to handle each request in a new process."""
- May be extended, do not override.
- """
- self.collect_children()
+ timeout = 300
+ active_children = None
+ max_children = 40
- def service_actions(self):
- """Collect the zombie child processes regularly in the ForkingMixIn.
-
- service_actions is called in the BaseServer's serve_forver loop.
- """
- self.collect_children()
-
- def process_request(self, request, client_address):
- """Fork a new subprocess to process the request."""
- pid = os.fork()
- if pid:
- # Parent process
+ def collect_children(self):
+ """Internal routine to wait for children that have exited."""
if self.active_children is None:
- self.active_children = set()
- self.active_children.add(pid)
- self.close_request(request)
- return
- else:
- # Child process.
- # This must never return, hence os._exit()!
- try:
- self.finish_request(request, client_address)
- self.shutdown_request(request)
- os._exit(0)
- except:
+ return
+
+ # If we're above the max number of children, wait and reap them until
+ # we go back below threshold. Note that we use waitpid(-1) below to be
+ # able to collect children in size(<defunct children>) syscalls instead
+ # of size(<children>): the downside is that this might reap children
+ # which we didn't spawn, which is why we only resort to this when we're
+ # above max_children.
+ while len(self.active_children) >= self.max_children:
+ try:
+ pid, _ = os.waitpid(-1, 0)
+ self.active_children.discard(pid)
+ except ChildProcessError:
+ # we don't have any children, we're done
+ self.active_children.clear()
+ except OSError:
+ break
+
+ # Now reap all defunct children.
+ for pid in self.active_children.copy():
try:
+ pid, _ = os.waitpid(pid, os.WNOHANG)
+ # if the child hasn't exited yet, pid will be 0 and ignored by
+ # discard() below
+ self.active_children.discard(pid)
+ except ChildProcessError:
+ # someone else reaped it
+ self.active_children.discard(pid)
+ except OSError:
+ pass
+
+ def handle_timeout(self):
+ """Wait for zombies after self.timeout seconds of inactivity.
+
+ May be extended, do not override.
+ """
+ self.collect_children()
+
+ def service_actions(self):
+ """Collect the zombie child processes regularly in the ForkingMixIn.
+
+ service_actions is called in the BaseServer's serve_forver loop.
+ """
+ self.collect_children()
+
+ def process_request(self, request, client_address):
+ """Fork a new subprocess to process the request."""
+ pid = os.fork()
+ if pid:
+ # Parent process
+ if self.active_children is None:
+ self.active_children = set()
+ self.active_children.add(pid)
+ self.close_request(request)
+ return
+ else:
+ # Child process.
+ # This must never return, hence os._exit()!
+ status = 1
+ try:
+ self.finish_request(request, client_address)
+ status = 0
+ except Exception:
self.handle_error(request, client_address)
- self.shutdown_request(request)
finally:
- os._exit(1)
+ try:
+ self.shutdown_request(request)
+ finally:
+ os._exit(status)
class ThreadingMixIn:
@@ -623,9 +637,9 @@ class ThreadingMixIn:
"""
try:
self.finish_request(request, client_address)
- self.shutdown_request(request)
- except:
+ except Exception:
self.handle_error(request, client_address)
+ finally:
self.shutdown_request(request)
def process_request(self, request, client_address):
@@ -636,8 +650,9 @@ class ThreadingMixIn:
t.start()
-class ForkingUDPServer(ForkingMixIn, UDPServer): pass
-class ForkingTCPServer(ForkingMixIn, TCPServer): pass
+if hasattr(os, "fork"):
+ class ForkingUDPServer(ForkingMixIn, UDPServer): pass
+ class ForkingTCPServer(ForkingMixIn, TCPServer): pass
class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass
class ThreadingTCPServer(ThreadingMixIn, TCPServer): pass
@@ -729,7 +744,10 @@ class StreamRequestHandler(BaseRequestHandler):
self.connection.setsockopt(socket.IPPROTO_TCP,
socket.TCP_NODELAY, True)
self.rfile = self.connection.makefile('rb', self.rbufsize)
- self.wfile = self.connection.makefile('wb', self.wbufsize)
+ if self.wbufsize == 0:
+ self.wfile = _SocketWriter(self.connection)
+ else:
+ self.wfile = self.connection.makefile('wb', self.wbufsize)
def finish(self):
if not self.wfile.closed:
@@ -742,6 +760,24 @@ class StreamRequestHandler(BaseRequestHandler):
self.wfile.close()
self.rfile.close()
+class _SocketWriter(BufferedIOBase):
+ """Simple writable BufferedIOBase implementation for a socket
+
+ Does not hold data in a buffer, avoiding any need to call flush()."""
+
+ def __init__(self, sock):
+ self._sock = sock
+
+ def writable(self):
+ return True
+
+ def write(self, b):
+ self._sock.sendall(b)
+ with memoryview(b) as view:
+ return view.nbytes
+
+ def fileno(self):
+ return self._sock.fileno()
class DatagramRequestHandler(BaseRequestHandler):
diff --git a/Lib/sqlite3/test/dbapi.py b/Lib/sqlite3/test/dbapi.py
index 7fb8d7e..cb85814 100644
--- a/Lib/sqlite3/test/dbapi.py
+++ b/Lib/sqlite3/test/dbapi.py
@@ -188,7 +188,10 @@ class CursorTests(unittest.TestCase):
def setUp(self):
self.cx = sqlite.connect(":memory:")
self.cu = self.cx.cursor()
- self.cu.execute("create table test(id integer primary key, name text, income number)")
+ self.cu.execute(
+ "create table test(id integer primary key, name text, "
+ "income number, unique_test text unique)"
+ )
self.cu.execute("insert into test(name) values (?)", ("foo",))
def tearDown(self):
@@ -462,6 +465,44 @@ class CursorTests(unittest.TestCase):
with self.assertRaises(TypeError):
cur = sqlite.Cursor(foo)
+ def CheckLastRowIDOnReplace(self):
+ """
+ INSERT OR REPLACE and REPLACE INTO should produce the same behavior.
+ """
+ sql = '{} INTO test(id, unique_test) VALUES (?, ?)'
+ for statement in ('INSERT OR REPLACE', 'REPLACE'):
+ with self.subTest(statement=statement):
+ self.cu.execute(sql.format(statement), (1, 'foo'))
+ self.assertEqual(self.cu.lastrowid, 1)
+
+ def CheckLastRowIDOnIgnore(self):
+ self.cu.execute(
+ "insert or ignore into test(unique_test) values (?)",
+ ('test',))
+ self.assertEqual(self.cu.lastrowid, 2)
+ self.cu.execute(
+ "insert or ignore into test(unique_test) values (?)",
+ ('test',))
+ self.assertEqual(self.cu.lastrowid, 2)
+
+ def CheckLastRowIDInsertOR(self):
+ results = []
+ for statement in ('FAIL', 'ABORT', 'ROLLBACK'):
+ sql = 'INSERT OR {} INTO test(unique_test) VALUES (?)'
+ with self.subTest(statement='INSERT OR {}'.format(statement)):
+ self.cu.execute(sql.format(statement), (statement,))
+ results.append((statement, self.cu.lastrowid))
+ with self.assertRaises(sqlite.IntegrityError):
+ self.cu.execute(sql.format(statement), (statement,))
+ results.append((statement, self.cu.lastrowid))
+ expected = [
+ ('FAIL', 2), ('FAIL', 2),
+ ('ABORT', 3), ('ABORT', 3),
+ ('ROLLBACK', 4), ('ROLLBACK', 4),
+ ]
+ self.assertEqual(results, expected)
+
+
@unittest.skipUnless(threading, 'This test requires threading.')
class ThreadTests(unittest.TestCase):
def setUp(self):
diff --git a/Lib/sqlite3/test/transactions.py b/Lib/sqlite3/test/transactions.py
index a25360a..45f1b04 100644
--- a/Lib/sqlite3/test/transactions.py
+++ b/Lib/sqlite3/test/transactions.py
@@ -52,13 +52,13 @@ class TransactionTests(unittest.TestCase):
except OSError:
pass
- def CheckDMLdoesAutoCommitBefore(self):
+ def CheckDMLDoesNotAutoCommitBefore(self):
self.cur1.execute("create table test(i)")
self.cur1.execute("insert into test(i) values (5)")
self.cur1.execute("create table test2(j)")
self.cur2.execute("select i from test")
res = self.cur2.fetchall()
- self.assertEqual(len(res), 1)
+ self.assertEqual(len(res), 0)
def CheckInsertStartsTransaction(self):
self.cur1.execute("create table test(i)")
@@ -153,11 +153,6 @@ class SpecialCommandTests(unittest.TestCase):
self.con = sqlite.connect(":memory:")
self.cur = self.con.cursor()
- def CheckVacuum(self):
- self.cur.execute("create table test(i)")
- self.cur.execute("insert into test(i) values (5)")
- self.cur.execute("vacuum")
-
def CheckDropTable(self):
self.cur.execute("create table test(i)")
self.cur.execute("insert into test(i) values (5)")
@@ -172,10 +167,35 @@ class SpecialCommandTests(unittest.TestCase):
self.cur.close()
self.con.close()
+class TransactionalDDL(unittest.TestCase):
+ def setUp(self):
+ self.con = sqlite.connect(":memory:")
+
+ def CheckDdlDoesNotAutostartTransaction(self):
+ # For backwards compatibility reasons, DDL statements should not
+ # implicitly start a transaction.
+ self.con.execute("create table test(i)")
+ self.con.rollback()
+ result = self.con.execute("select * from test").fetchall()
+ self.assertEqual(result, [])
+
+ def CheckTransactionalDDL(self):
+ # You can achieve transactional DDL by issuing a BEGIN
+ # statement manually.
+ self.con.execute("begin")
+ self.con.execute("create table test(i)")
+ self.con.rollback()
+ with self.assertRaises(sqlite.OperationalError):
+ self.con.execute("select * from test")
+
+ def tearDown(self):
+ self.con.close()
+
def suite():
default_suite = unittest.makeSuite(TransactionTests, "Check")
special_command_suite = unittest.makeSuite(SpecialCommandTests, "Check")
- return unittest.TestSuite((default_suite, special_command_suite))
+ ddl_suite = unittest.makeSuite(TransactionalDDL, "Check")
+ return unittest.TestSuite((default_suite, special_command_suite, ddl_suite))
def test():
runner = unittest.TextTestRunner()
diff --git a/Lib/sre_compile.py b/Lib/sre_compile.py
index 502b061..420d83d 100644
--- a/Lib/sre_compile.py
+++ b/Lib/sre_compile.py
@@ -71,7 +71,8 @@ def _compile(code, pattern, flags):
ASSERT_CODES = _ASSERT_CODES
if (flags & SRE_FLAG_IGNORECASE and
not (flags & SRE_FLAG_LOCALE) and
- flags & SRE_FLAG_UNICODE):
+ flags & SRE_FLAG_UNICODE and
+ not (flags & SRE_FLAG_ASCII)):
fixes = _ignorecase_fixes
else:
fixes = None
@@ -137,14 +138,15 @@ def _compile(code, pattern, flags):
else:
emit(MIN_UNTIL)
elif op is SUBPATTERN:
- if av[0]:
+ group, add_flags, del_flags, p = av
+ if group:
emit(MARK)
- emit((av[0]-1)*2)
- # _compile_info(code, av[1], flags)
- _compile(code, av[1], flags)
- if av[0]:
+ emit((group-1)*2)
+ # _compile_info(code, p, (flags | add_flags) & ~del_flags)
+ _compile(code, p, (flags | add_flags) & ~del_flags)
+ if group:
emit(MARK)
- emit((av[0]-1)*2+1)
+ emit((group-1)*2+1)
elif op in SUCCESS_CODES:
emit(op)
elif op in ASSERT_CODES:
@@ -172,7 +174,7 @@ def _compile(code, pattern, flags):
av = AT_MULTILINE.get(av, av)
if flags & SRE_FLAG_LOCALE:
av = AT_LOCALE.get(av, av)
- elif flags & SRE_FLAG_UNICODE:
+ elif (flags & SRE_FLAG_UNICODE) and not (flags & SRE_FLAG_ASCII):
av = AT_UNICODE.get(av, av)
emit(av)
elif op is BRANCH:
@@ -193,7 +195,7 @@ def _compile(code, pattern, flags):
emit(op)
if flags & SRE_FLAG_LOCALE:
av = CH_LOCALE[av]
- elif flags & SRE_FLAG_UNICODE:
+ elif (flags & SRE_FLAG_UNICODE) and not (flags & SRE_FLAG_ASCII):
av = CH_UNICODE[av]
emit(av)
elif op is GROUPREF:
@@ -237,7 +239,7 @@ def _compile_charset(charset, flags, code, fixup=None, fixes=None):
elif op is CATEGORY:
if flags & SRE_FLAG_LOCALE:
emit(CH_LOCALE[av])
- elif flags & SRE_FLAG_UNICODE:
+ elif (flags & SRE_FLAG_UNICODE) and not (flags & SRE_FLAG_ASCII):
emit(CH_UNICODE[av])
else:
emit(av)
@@ -409,42 +411,42 @@ def _generate_overlap_table(prefix):
table[i] = idx + 1
return table
-def _compile_info(code, pattern, flags):
- # internal: compile an info block. in the current version,
- # this contains min/max pattern width, and an optional literal
- # prefix or a character map
- lo, hi = pattern.getwidth()
- if hi > MAXCODE:
- hi = MAXCODE
- if lo == 0:
- code.extend([INFO, 4, 0, lo, hi])
- return
- # look for a literal prefix
+def _get_literal_prefix(pattern):
+ # look for literal prefix
prefix = []
prefixappend = prefix.append
- prefix_skip = 0
+ prefix_skip = None
+ for op, av in pattern.data:
+ if op is LITERAL:
+ prefixappend(av)
+ elif op is SUBPATTERN:
+ group, add_flags, del_flags, p = av
+ if add_flags & SRE_FLAG_IGNORECASE:
+ break
+ prefix1, prefix_skip1, got_all = _get_literal_prefix(p)
+ if prefix_skip is None:
+ if group is not None:
+ prefix_skip = len(prefix)
+ elif prefix_skip1 is not None:
+ prefix_skip = len(prefix) + prefix_skip1
+ prefix.extend(prefix1)
+ if not got_all:
+ break
+ else:
+ break
+ else:
+ return prefix, prefix_skip, True
+ return prefix, prefix_skip, False
+
+def _get_charset_prefix(pattern):
charset = [] # not used
charsetappend = charset.append
- if not (flags & SRE_FLAG_IGNORECASE):
- # look for literal prefix
- for op, av in pattern.data:
- if op is LITERAL:
- if len(prefix) == prefix_skip:
- prefix_skip = prefix_skip + 1
- prefixappend(av)
- elif op is SUBPATTERN and len(av[1]) == 1:
- op, av = av[1][0]
- if op is LITERAL:
- prefixappend(av)
- else:
- break
- else:
- break
- # if no prefix, look for charset prefix
- if not prefix and pattern.data:
- op, av = pattern.data[0]
- if op is SUBPATTERN and av[1]:
- op, av = av[1][0]
+ if pattern.data:
+ op, av = pattern.data[0]
+ if op is SUBPATTERN:
+ group, add_flags, del_flags, p = av
+ if p and not (add_flags & SRE_FLAG_IGNORECASE):
+ op, av = p[0]
if op is LITERAL:
charsetappend((op, av))
elif op is BRANCH:
@@ -460,21 +462,43 @@ def _compile_info(code, pattern, flags):
break
else:
charset = c
- elif op is BRANCH:
- c = []
- cappend = c.append
- for p in av[1]:
- if not p:
- break
- op, av = p[0]
- if op is LITERAL:
- cappend((op, av))
- else:
- break
+ elif op is BRANCH:
+ c = []
+ cappend = c.append
+ for p in av[1]:
+ if not p:
+ break
+ op, av = p[0]
+ if op is LITERAL:
+ cappend((op, av))
else:
- charset = c
- elif op is IN:
- charset = av
+ break
+ else:
+ charset = c
+ elif op is IN:
+ charset = av
+ return charset
+
+def _compile_info(code, pattern, flags):
+ # internal: compile an info block. in the current version,
+ # this contains min/max pattern width, and an optional literal
+ # prefix or a character map
+ lo, hi = pattern.getwidth()
+ if hi > MAXCODE:
+ hi = MAXCODE
+ if lo == 0:
+ code.extend([INFO, 4, 0, lo, hi])
+ return
+ # look for a literal prefix
+ prefix = []
+ prefix_skip = 0
+ charset = [] # not used
+ if not (flags & SRE_FLAG_IGNORECASE):
+ # look for literal prefix
+ prefix, prefix_skip, got_all = _get_literal_prefix(pattern)
+ # if no prefix, look for charset prefix
+ if not prefix:
+ charset = _get_charset_prefix(pattern)
## if prefix:
## print("*** PREFIX", prefix, prefix_skip)
## if charset:
@@ -487,7 +511,7 @@ def _compile_info(code, pattern, flags):
mask = 0
if prefix:
mask = SRE_INFO_PREFIX
- if len(prefix) == prefix_skip == len(pattern.data):
+ if prefix_skip is None and got_all:
mask = mask | SRE_INFO_LITERAL
elif charset:
mask = mask | SRE_INFO_CHARSET
@@ -502,6 +526,8 @@ def _compile_info(code, pattern, flags):
# add literal prefix
if prefix:
emit(len(prefix)) # length
+ if prefix_skip is None:
+ prefix_skip = len(prefix)
emit(prefix_skip) # skip
code.extend(prefix)
# generate overlap table
diff --git a/Lib/sre_parse.py b/Lib/sre_parse.py
index 4ff50d1..6aa49c3 100644
--- a/Lib/sre_parse.py
+++ b/Lib/sre_parse.py
@@ -65,6 +65,12 @@ FLAGS = {
"u": SRE_FLAG_UNICODE,
}
+GLOBAL_FLAGS = (SRE_FLAG_ASCII | SRE_FLAG_LOCALE | SRE_FLAG_UNICODE |
+ SRE_FLAG_DEBUG | SRE_FLAG_TEMPLATE)
+
+class Verbose(Exception):
+ pass
+
class Pattern:
# master pattern object. keeps track of global attributes
def __init__(self):
@@ -184,7 +190,7 @@ class SubPattern:
lo = lo + i
hi = hi + j
elif op is SUBPATTERN:
- i, j = av[1].getwidth()
+ i, j = av[-1].getwidth()
lo = lo + i
hi = hi + j
elif op in _REPEATCODES:
@@ -273,6 +279,9 @@ class Tokenizer:
break
result += c
return result
+ @property
+ def pos(self):
+ return self.index - len(self.next or '')
def tell(self):
return self.index - len(self.next or '')
def seek(self, index):
@@ -282,33 +291,6 @@ class Tokenizer:
def error(self, msg, offset=0):
return error(msg, self.string, self.tell() - offset)
-# The following three functions are not used in this module anymore, but we keep
-# them here (with DeprecationWarnings) for backwards compatibility.
-
-def isident(char):
- import warnings
- warnings.warn('sre_parse.isident() will be removed in 3.5',
- DeprecationWarning, stacklevel=2)
- return "a" <= char <= "z" or "A" <= char <= "Z" or char == "_"
-
-def isdigit(char):
- import warnings
- warnings.warn('sre_parse.isdigit() will be removed in 3.5',
- DeprecationWarning, stacklevel=2)
- return "0" <= char <= "9"
-
-def isname(name):
- import warnings
- warnings.warn('sre_parse.isname() will be removed in 3.5',
- DeprecationWarning, stacklevel=2)
- # check that group name is a valid string
- if not isident(name[0]):
- return False
- for char in name[1:]:
- if not isident(char) and not isdigit(char):
- return False
- return True
-
def _class_escape(source, escape):
# handle escape code inside character class
code = ESCAPES.get(escape)
@@ -351,9 +333,7 @@ def _class_escape(source, escape):
raise ValueError
if len(escape) == 2:
if c in ASCIILETTERS:
- import warnings
- warnings.warn('bad escape %s' % escape,
- DeprecationWarning, stacklevel=8)
+ raise source.error('bad escape %s' % escape, len(escape))
return LITERAL, ord(escape[1])
except ValueError:
pass
@@ -415,18 +395,16 @@ def _escape(source, escape, state):
len(escape))
state.checklookbehindgroup(group, source)
return GROUPREF, group
- raise source.error("invalid group reference", len(escape))
+ raise source.error("invalid group reference %d" % group, len(escape) - 1)
if len(escape) == 2:
if c in ASCIILETTERS:
- import warnings
- warnings.warn('bad escape %s' % escape,
- DeprecationWarning, stacklevel=8)
+ raise source.error("bad escape %s" % escape, len(escape))
return LITERAL, ord(escape[1])
except ValueError:
pass
raise source.error("bad escape %s" % escape, len(escape))
-def _parse_sub(source, state, nested=True):
+def _parse_sub(source, state, verbose, nested=True):
# parse an alternation: a|b|c
items = []
@@ -434,7 +412,7 @@ def _parse_sub(source, state, nested=True):
sourcematch = source.match
start = source.tell()
while True:
- itemsappend(_parse(source, state))
+ itemsappend(_parse(source, state, verbose))
if not sourcematch("|"):
break
@@ -476,10 +454,10 @@ def _parse_sub(source, state, nested=True):
subpattern.append((BRANCH, (None, items)))
return subpattern
-def _parse_sub_cond(source, state, condgroup):
- item_yes = _parse(source, state)
+def _parse_sub_cond(source, state, condgroup, verbose):
+ item_yes = _parse(source, state, verbose)
if source.match("|"):
- item_no = _parse(source, state)
+ item_no = _parse(source, state, verbose)
if source.next == "|":
raise source.error("conditional backref with more than two branches")
else:
@@ -488,7 +466,7 @@ def _parse_sub_cond(source, state, condgroup):
subpattern.append((GROUPREF_EXISTS, (condgroup, item_yes, item_no)))
return subpattern
-def _parse(source, state):
+def _parse(source, state, verbose):
# parse a simple pattern
subpattern = SubPattern(state)
@@ -498,7 +476,6 @@ def _parse(source, state):
sourcematch = source.match
_len = len
_ord = ord
- verbose = state.flags & SRE_FLAG_VERBOSE
while True:
@@ -652,6 +629,8 @@ def _parse(source, state):
group = True
name = None
condgroup = None
+ add_flags = 0
+ del_flags = 0
if sourcematch("?"):
# options
char = sourceget()
@@ -713,7 +692,7 @@ def _parse(source, state):
lookbehindgroups = state.lookbehindgroups
if lookbehindgroups is None:
state.lookbehindgroups = state.groups
- p = _parse_sub(source, state)
+ p = _parse_sub(source, state, verbose)
if dir < 0:
if lookbehindgroups is None:
state.lookbehindgroups = None
@@ -746,22 +725,26 @@ def _parse(source, state):
raise source.error("bad group number",
len(condname) + 1)
if condgroup >= MAXGROUPS:
- raise source.error("invalid group reference",
- len(condname) + 1)
+ msg = "invalid group reference %d" % condgroup
+ raise source.error(msg, len(condname) + 1)
state.checklookbehindgroup(condgroup, source)
- elif char in FLAGS:
+ elif char in FLAGS or char == "-":
# flags
- while True:
- state.flags |= FLAGS[char]
- char = sourceget()
- if char is None:
- raise source.error("missing )")
- if char == ")":
- break
- if char not in FLAGS:
- raise source.error("unknown flag", len(char))
- verbose = state.flags & SRE_FLAG_VERBOSE
- continue
+ pos = source.pos
+ flags = _parse_flags(source, state, char)
+ if flags is None: # global flags
+ if pos != 3: # "(?x"
+ import warnings
+ warnings.warn(
+ 'Flags not at the start of the expression %s%s' % (
+ source.string[:20], # truncate long regexes
+ ' (truncated)' if len(source.string) > 20 else '',
+ ),
+ DeprecationWarning, stacklevel=7
+ )
+ continue
+ add_flags, del_flags = flags
+ group = None
else:
raise source.error("unknown extension ?" + char,
len(char) + 1)
@@ -773,15 +756,17 @@ def _parse(source, state):
except error as err:
raise source.error(err.msg, len(name) + 1) from None
if condgroup:
- p = _parse_sub_cond(source, state, condgroup)
+ p = _parse_sub_cond(source, state, condgroup, verbose)
else:
- p = _parse_sub(source, state)
+ sub_verbose = ((verbose or (add_flags & SRE_FLAG_VERBOSE)) and
+ not (del_flags & SRE_FLAG_VERBOSE))
+ p = _parse_sub(source, state, sub_verbose)
if not source.match(")"):
raise source.error("missing ), unterminated subpattern",
source.tell() - start)
if group is not None:
state.closegroup(group, p)
- subpatternappend((SUBPATTERN, (group, p)))
+ subpatternappend((SUBPATTERN, (group, add_flags, del_flags, p)))
elif this == "^":
subpatternappend((AT, AT_BEGINNING))
@@ -794,14 +779,58 @@ def _parse(source, state):
return subpattern
+def _parse_flags(source, state, char):
+ sourceget = source.get
+ add_flags = 0
+ del_flags = 0
+ if char != "-":
+ while True:
+ add_flags |= FLAGS[char]
+ char = sourceget()
+ if char is None:
+ raise source.error("missing -, : or )")
+ if char in ")-:":
+ break
+ if char not in FLAGS:
+ msg = "unknown flag" if char.isalpha() else "missing -, : or )"
+ raise source.error(msg, len(char))
+ if char == ")":
+ if ((add_flags & SRE_FLAG_VERBOSE) and
+ not (state.flags & SRE_FLAG_VERBOSE)):
+ raise Verbose
+ state.flags |= add_flags
+ return None
+ if add_flags & GLOBAL_FLAGS:
+ raise source.error("bad inline flags: cannot turn on global flag", 1)
+ if char == "-":
+ char = sourceget()
+ if char is None:
+ raise source.error("missing flag")
+ if char not in FLAGS:
+ msg = "unknown flag" if char.isalpha() else "missing flag"
+ raise source.error(msg, len(char))
+ while True:
+ del_flags |= FLAGS[char]
+ char = sourceget()
+ if char is None:
+ raise source.error("missing :")
+ if char == ":":
+ break
+ if char not in FLAGS:
+ msg = "unknown flag" if char.isalpha() else "missing :"
+ raise source.error(msg, len(char))
+ assert char == ":"
+ if del_flags & GLOBAL_FLAGS:
+ raise source.error("bad inline flags: cannot turn off global flag", 1)
+ if add_flags & del_flags:
+ raise source.error("bad inline flags: flag turned on and off", 1)
+ return add_flags, del_flags
+
def fix_flags(src, flags):
# Check and fix flags according to the type of pattern (str or bytes)
if isinstance(src, str):
if flags & SRE_FLAG_LOCALE:
- import warnings
- warnings.warn("LOCALE flag with a str pattern is deprecated. "
- "Will be an error in 3.6",
- DeprecationWarning, stacklevel=6)
+ raise ValueError("cannot use LOCALE flag with a str pattern")
if not flags & SRE_FLAG_ASCII:
flags |= SRE_FLAG_UNICODE
elif flags & SRE_FLAG_UNICODE:
@@ -810,10 +839,7 @@ def fix_flags(src, flags):
if flags & SRE_FLAG_UNICODE:
raise ValueError("cannot use UNICODE flag with a bytes pattern")
if flags & SRE_FLAG_LOCALE and flags & SRE_FLAG_ASCII:
- import warnings
- warnings.warn("ASCII and LOCALE flags are incompatible. "
- "Will be an error in 3.6",
- DeprecationWarning, stacklevel=6)
+ raise ValueError("ASCII and LOCALE flags are incompatible")
return flags
def parse(str, flags=0, pattern=None):
@@ -826,18 +852,23 @@ def parse(str, flags=0, pattern=None):
pattern.flags = flags
pattern.str = str
- p = _parse_sub(source, pattern, 0)
+ try:
+ p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, False)
+ except Verbose:
+ # the VERBOSE flag was switched on inside the pattern. to be
+ # on the safe side, we'll parse the whole thing again...
+ pattern = Pattern()
+ pattern.flags = flags | SRE_FLAG_VERBOSE
+ pattern.str = str
+ source.seek(0)
+ p = _parse_sub(source, pattern, True, False)
+
p.pattern.flags = fix_flags(str, p.pattern.flags)
if source.next is not None:
assert source.next == ")"
raise source.error("unbalanced parenthesis")
- if not (flags & SRE_FLAG_VERBOSE) and p.pattern.flags & SRE_FLAG_VERBOSE:
- # the VERBOSE flag was switched on inside the pattern. to be
- # on the safe side, we'll parse the whole thing again...
- return parse(str, p.pattern.flags)
-
if flags & SRE_FLAG_DEBUG:
p.dump()
@@ -852,7 +883,9 @@ def parse_template(source, pattern):
literals = []
literal = []
lappend = literal.append
- def addgroup(index):
+ def addgroup(index, pos):
+ if index > pattern.groups:
+ raise s.error("invalid group reference %d" % index, pos)
if literal:
literals.append(''.join(literal))
del literal[:]
@@ -885,9 +918,9 @@ def parse_template(source, pattern):
raise s.error("bad character in group name %r" % name,
len(name) + 1) from None
if index >= MAXGROUPS:
- raise s.error("invalid group reference",
+ raise s.error("invalid group reference %d" % index,
len(name) + 1)
- addgroup(index)
+ addgroup(index, len(name) + 1)
elif c == "0":
if s.next in OCTDIGITS:
this += sget()
@@ -908,7 +941,7 @@ def parse_template(source, pattern):
'range 0-0o377' % this, len(this))
lappend(chr(c))
if not isoctal:
- addgroup(int(this[1:]))
+ addgroup(int(this[1:]), len(this) - 1)
else:
try:
this = chr(ESCAPES[this][1])
@@ -937,5 +970,5 @@ def expand_template(template, match):
for index, group in groups:
literals[index] = g(group) or empty
except IndexError:
- raise error("invalid group reference")
+ raise error("invalid group reference %d" % index)
return empty.join(literals)
diff --git a/Lib/ssl.py b/Lib/ssl.py
index e191390..8ad4a33 100644
--- a/Lib/ssl.py
+++ b/Lib/ssl.py
@@ -52,6 +52,8 @@ PROTOCOL_SSLv2
PROTOCOL_SSLv3
PROTOCOL_SSLv23
PROTOCOL_TLS
+PROTOCOL_TLS_CLIENT
+PROTOCOL_TLS_SERVER
PROTOCOL_TLSv1
PROTOCOL_TLSv1_1
PROTOCOL_TLSv1_2
@@ -94,17 +96,16 @@ import re
import sys
import os
from collections import namedtuple
-from enum import Enum as _Enum, IntEnum as _IntEnum
+from enum import Enum as _Enum, IntEnum as _IntEnum, IntFlag as _IntFlag
import _ssl # if we can't import it, let the error propagate
from _ssl import OPENSSL_VERSION_NUMBER, OPENSSL_VERSION_INFO, OPENSSL_VERSION
-from _ssl import _SSLContext, MemoryBIO
+from _ssl import _SSLContext, MemoryBIO, SSLSession
from _ssl import (
SSLError, SSLZeroReturnError, SSLWantReadError, SSLWantWriteError,
SSLSyscallError, SSLEOFError,
)
-from _ssl import CERT_NONE, CERT_OPTIONAL, CERT_REQUIRED
from _ssl import txt2obj as _txt2obj, nid2obj as _nid2obj
from _ssl import RAND_status, RAND_add, RAND_bytes, RAND_pseudo_bytes
try:
@@ -113,32 +114,47 @@ except ImportError:
# LibreSSL does not provide RAND_egd
pass
-def _import_symbols(prefix):
- for n in dir(_ssl):
- if n.startswith(prefix):
- globals()[n] = getattr(_ssl, n)
-
-_import_symbols('OP_')
-_import_symbols('ALERT_DESCRIPTION_')
-_import_symbols('SSL_ERROR_')
-_import_symbols('VERIFY_')
from _ssl import HAS_SNI, HAS_ECDH, HAS_NPN, HAS_ALPN
-
from _ssl import _OPENSSL_API_VERSION
+
+_IntEnum._convert(
+ '_SSLMethod', __name__,
+ lambda name: name.startswith('PROTOCOL_') and name != 'PROTOCOL_SSLv23',
+ source=_ssl)
+
+_IntFlag._convert(
+ 'Options', __name__,
+ lambda name: name.startswith('OP_'),
+ source=_ssl)
+
+_IntEnum._convert(
+ 'AlertDescription', __name__,
+ lambda name: name.startswith('ALERT_DESCRIPTION_'),
+ source=_ssl)
+
+_IntEnum._convert(
+ 'SSLErrorNumber', __name__,
+ lambda name: name.startswith('SSL_ERROR_'),
+ source=_ssl)
+
+_IntFlag._convert(
+ 'VerifyFlags', __name__,
+ lambda name: name.startswith('VERIFY_'),
+ source=_ssl)
+
_IntEnum._convert(
- '_SSLMethod', __name__,
- lambda name: name.startswith('PROTOCOL_') and name != 'PROTOCOL_SSLv23',
- source=_ssl)
+ 'VerifyMode', __name__,
+ lambda name: name.startswith('CERT_'),
+ source=_ssl)
+
PROTOCOL_SSLv23 = _SSLMethod.PROTOCOL_SSLv23 = _SSLMethod.PROTOCOL_TLS
_PROTOCOL_NAMES = {value: name for name, value in _SSLMethod.__members__.items()}
-try:
- _SSLv2_IF_EXISTS = PROTOCOL_SSLv2
-except NameError:
- _SSLv2_IF_EXISTS = None
+_SSLv2_IF_EXISTS = getattr(_SSLMethod, 'PROTOCOL_SSLv2', None)
+
if sys.platform == "win32":
from _ssl import enum_certificates, enum_crls
@@ -377,18 +393,18 @@ class SSLContext(_SSLContext):
def wrap_socket(self, sock, server_side=False,
do_handshake_on_connect=True,
suppress_ragged_eofs=True,
- server_hostname=None):
+ server_hostname=None, session=None):
return SSLSocket(sock=sock, server_side=server_side,
do_handshake_on_connect=do_handshake_on_connect,
suppress_ragged_eofs=suppress_ragged_eofs,
server_hostname=server_hostname,
- _context=self)
+ _context=self, _session=session)
def wrap_bio(self, incoming, outgoing, server_side=False,
- server_hostname=None):
+ server_hostname=None, session=None):
sslobj = self._wrap_bio(incoming, outgoing, server_side=server_side,
server_hostname=server_hostname)
- return SSLObject(sslobj)
+ return SSLObject(sslobj, session=session)
def set_npn_protocols(self, npn_protocols):
protos = bytearray()
@@ -434,6 +450,34 @@ class SSLContext(_SSLContext):
self._load_windows_store_certs(storename, purpose)
self.set_default_verify_paths()
+ @property
+ def options(self):
+ return Options(super().options)
+
+ @options.setter
+ def options(self, value):
+ super(SSLContext, SSLContext).options.__set__(self, value)
+
+ @property
+ def verify_flags(self):
+ return VerifyFlags(super().verify_flags)
+
+ @verify_flags.setter
+ def verify_flags(self, value):
+ super(SSLContext, SSLContext).verify_flags.__set__(self, value)
+
+ @property
+ def verify_mode(self):
+ value = super().verify_mode
+ try:
+ return VerifyMode(value)
+ except ValueError:
+ return value
+
+ @verify_mode.setter
+ def verify_mode(self, value):
+ super(SSLContext, SSLContext).verify_mode.__set__(self, value)
+
def create_default_context(purpose=Purpose.SERVER_AUTH, *, cafile=None,
capath=None, cadata=None):
@@ -446,32 +490,16 @@ def create_default_context(purpose=Purpose.SERVER_AUTH, *, cafile=None,
if not isinstance(purpose, _ASN1Object):
raise TypeError(purpose)
+ # SSLContext sets OP_NO_SSLv2, OP_NO_SSLv3, OP_NO_COMPRESSION,
+ # OP_CIPHER_SERVER_PREFERENCE, OP_SINGLE_DH_USE and OP_SINGLE_ECDH_USE
+ # by default.
context = SSLContext(PROTOCOL_TLS)
- # SSLv2 considered harmful.
- context.options |= OP_NO_SSLv2
-
- # SSLv3 has problematic security and is only required for really old
- # clients such as IE6 on Windows XP
- context.options |= OP_NO_SSLv3
-
- # disable compression to prevent CRIME attacks (OpenSSL 1.0+)
- context.options |= getattr(_ssl, "OP_NO_COMPRESSION", 0)
-
if purpose == Purpose.SERVER_AUTH:
# verify certs and host name in client mode
context.verify_mode = CERT_REQUIRED
context.check_hostname = True
elif purpose == Purpose.CLIENT_AUTH:
- # Prefer the server's ciphers by default so that we get stronger
- # encryption
- context.options |= getattr(_ssl, "OP_CIPHER_SERVER_PREFERENCE", 0)
-
- # Use single use keys in order to improve forward secrecy
- context.options |= getattr(_ssl, "OP_SINGLE_DH_USE", 0)
- context.options |= getattr(_ssl, "OP_SINGLE_ECDH_USE", 0)
-
- # disallow ciphers with known vulnerabilities
context.set_ciphers(_RESTRICTED_SERVER_CIPHERS)
if cafile or capath or cadata:
@@ -497,12 +525,10 @@ def _create_unverified_context(protocol=PROTOCOL_TLS, *, cert_reqs=None,
if not isinstance(purpose, _ASN1Object):
raise TypeError(purpose)
+ # SSLContext sets OP_NO_SSLv2, OP_NO_SSLv3, OP_NO_COMPRESSION,
+ # OP_CIPHER_SERVER_PREFERENCE, OP_SINGLE_DH_USE and OP_SINGLE_ECDH_USE
+ # by default.
context = SSLContext(protocol)
- # SSLv2 considered harmful.
- context.options |= OP_NO_SSLv2
- # SSLv3 has problematic security and is only required for really old
- # clients such as IE6 on Windows XP
- context.options |= OP_NO_SSLv3
if cert_reqs is not None:
context.verify_mode = cert_reqs
@@ -548,10 +574,12 @@ class SSLObject:
* The ``do_handshake_on_connect`` and ``suppress_ragged_eofs`` machinery.
"""
- def __init__(self, sslobj, owner=None):
+ def __init__(self, sslobj, owner=None, session=None):
self._sslobj = sslobj
# Note: _sslobj takes a weak reference to owner
self._sslobj.owner = owner or self
+ if session is not None:
+ self._sslobj.session = session
@property
def context(self):
@@ -563,6 +591,20 @@ class SSLObject:
self._sslobj.context = ctx
@property
+ def session(self):
+ """The SSLSession for client socket."""
+ return self._sslobj.session
+
+ @session.setter
+ def session(self, session):
+ self._sslobj.session = session
+
+ @property
+ def session_reused(self):
+ """Was the client session reused during handshake"""
+ return self._sslobj.session_reused
+
+ @property
def server_side(self):
"""Whether this is a server-side socket."""
return self._sslobj.server_side
@@ -679,7 +721,7 @@ class SSLSocket(socket):
family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None,
suppress_ragged_eofs=True, npn_protocols=None, ciphers=None,
server_hostname=None,
- _context=None):
+ _context=None, _session=None):
if _context:
self._context = _context
@@ -711,11 +753,16 @@ class SSLSocket(socket):
# mixed in.
if sock.getsockopt(SOL_SOCKET, SO_TYPE) != SOCK_STREAM:
raise NotImplementedError("only stream sockets are supported")
- if server_side and server_hostname:
- raise ValueError("server_hostname can only be specified "
- "in client mode")
+ if server_side:
+ if server_hostname:
+ raise ValueError("server_hostname can only be specified "
+ "in client mode")
+ if _session is not None:
+ raise ValueError("session can only be specified in "
+ "client mode")
if self._context.check_hostname and not server_hostname:
raise ValueError("check_hostname requires server_hostname")
+ self._session = _session
self.server_side = server_side
self.server_hostname = server_hostname
self.do_handshake_on_connect = do_handshake_on_connect
@@ -751,7 +798,8 @@ class SSLSocket(socket):
try:
sslobj = self._context._wrap_socket(self, server_side,
server_hostname)
- self._sslobj = SSLObject(sslobj, owner=self)
+ self._sslobj = SSLObject(sslobj, owner=self,
+ session=self._session)
if do_handshake_on_connect:
timeout = self.gettimeout()
if timeout == 0.0:
@@ -772,6 +820,24 @@ class SSLSocket(socket):
self._context = ctx
self._sslobj.context = ctx
+ @property
+ def session(self):
+ """The SSLSession for client socket."""
+ if self._sslobj is not None:
+ return self._sslobj.session
+
+ @session.setter
+ def session(self, session):
+ self._session = session
+ if self._sslobj is not None:
+ self._sslobj.session = session
+
+ @property
+ def session_reused(self):
+ """Was the client session reused during handshake"""
+ if self._sslobj is not None:
+ return self._sslobj.session_reused
+
def dup(self):
raise NotImplemented("Can't dup() %s instances" %
self.__class__.__name__)
@@ -898,7 +964,6 @@ class SSLSocket(socket):
while (count < amount):
v = self.send(data[count:])
count += v
- return amount
else:
return socket.sendall(self, data, flags)
@@ -1005,7 +1070,8 @@ class SSLSocket(socket):
if self._connected:
raise ValueError("attempt to connect already-connected SSLSocket!")
sslobj = self.context._wrap_socket(self, False, self.server_hostname)
- self._sslobj = SSLObject(sslobj, owner=self)
+ self._sslobj = SSLObject(sslobj, owner=self,
+ session=self._session)
try:
if connect_ex:
rc = socket.connect_ex(self, addr)
@@ -1068,7 +1134,6 @@ def wrap_socket(sock, keyfile=None, certfile=None,
do_handshake_on_connect=True,
suppress_ragged_eofs=True,
ciphers=None):
-
return SSLSocket(sock=sock, keyfile=keyfile, certfile=certfile,
server_side=server_side, cert_reqs=cert_reqs,
ssl_version=ssl_version, ca_certs=ca_certs,
diff --git a/Lib/statistics.py b/Lib/statistics.py
index 4f5c1c1..30fe55c 100644
--- a/Lib/statistics.py
+++ b/Lib/statistics.py
@@ -1,20 +1,3 @@
-## Module statistics.py
-##
-## Copyright (c) 2013 Steven D'Aprano <steve+python@pearwood.info>.
-##
-## Licensed under the Apache License, Version 2.0 (the "License");
-## you may not use this file except in compliance with the License.
-## You may obtain a copy of the License at
-##
-## http://www.apache.org/licenses/LICENSE-2.0
-##
-## Unless required by applicable law or agreed to in writing, software
-## distributed under the License is distributed on an "AS IS" BASIS,
-## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-## See the License for the specific language governing permissions and
-## limitations under the License.
-
-
"""
Basic statistics module.
@@ -28,6 +11,7 @@ Calculating averages
Function Description
================== =============================================
mean Arithmetic mean (average) of data.
+harmonic_mean Harmonic mean of data.
median Median (middle value) of data.
median_low Low median of data.
median_high High median of data.
@@ -95,16 +79,18 @@ A single exception is defined: StatisticsError is a subclass of ValueError.
__all__ = [ 'StatisticsError',
'pstdev', 'pvariance', 'stdev', 'variance',
'median', 'median_low', 'median_high', 'median_grouped',
- 'mean', 'mode',
+ 'mean', 'mode', 'harmonic_mean',
]
-
import collections
+import decimal
import math
+import numbers
from fractions import Fraction
from decimal import Decimal
-from itertools import groupby
+from itertools import groupby, chain
+from bisect import bisect_left, bisect_right
@@ -134,7 +120,8 @@ def _sum(data, start=0):
Some sources of round-off error will be avoided:
- >>> _sum([1e50, 1, -1e50] * 1000) # Built-in sum returns zero.
+ # Built-in sum returns zero.
+ >>> _sum([1e50, 1, -1e50] * 1000)
(<class 'float'>, Fraction(1000, 1), 3000)
Fractions and Decimals are also supported:
@@ -223,56 +210,26 @@ def _exact_ratio(x):
# Optimise the common case of floats. We expect that the most often
# used numeric type will be builtin floats, so try to make this as
# fast as possible.
- if type(x) is float:
+ if type(x) is float or type(x) is Decimal:
return x.as_integer_ratio()
try:
# x may be an int, Fraction, or Integral ABC.
return (x.numerator, x.denominator)
except AttributeError:
try:
- # x may be a float subclass.
+ # x may be a float or Decimal subclass.
return x.as_integer_ratio()
except AttributeError:
- try:
- # x may be a Decimal.
- return _decimal_to_ratio(x)
- except AttributeError:
- # Just give up?
- pass
+ # Just give up?
+ pass
except (OverflowError, ValueError):
# float NAN or INF.
- assert not math.isfinite(x)
+ assert not _isfinite(x)
return (x, None)
msg = "can't convert type '{}' to numerator/denominator"
raise TypeError(msg.format(type(x).__name__))
-# FIXME This is faster than Fraction.from_decimal, but still too slow.
-def _decimal_to_ratio(d):
- """Convert Decimal d to exact integer ratio (numerator, denominator).
-
- >>> from decimal import Decimal
- >>> _decimal_to_ratio(Decimal("2.6"))
- (26, 10)
-
- """
- sign, digits, exp = d.as_tuple()
- if exp in ('F', 'n', 'N'): # INF, NAN, sNAN
- assert not d.is_finite()
- return (d, None)
- num = 0
- for digit in digits:
- num = num*10 + digit
- if exp < 0:
- den = 10**-exp
- else:
- num *= 10**exp
- den = 1
- if sign:
- num = -num
- return (num, den)
-
-
def _convert(value, T):
"""Convert value to given numeric type T."""
if type(value) is T:
@@ -305,6 +262,30 @@ def _counts(data):
return table
+def _find_lteq(a, x):
+ 'Locate the leftmost value exactly equal to x'
+ i = bisect_left(a, x)
+ if i != len(a) and a[i] == x:
+ return i
+ raise ValueError
+
+
+def _find_rteq(a, l, x):
+ 'Locate the rightmost value exactly equal to x'
+ i = bisect_right(a, x, lo=l)
+ if i != (len(a)+1) and a[i-1] == x:
+ return i-1
+ raise ValueError
+
+
+def _fail_neg(values, errmsg='negative value'):
+ """Iterate over values, failing if any are less than zero."""
+ for x in values:
+ if x < 0:
+ raise StatisticsError(errmsg)
+ yield x
+
+
# === Measures of central tendency (averages) ===
def mean(data):
@@ -333,6 +314,52 @@ def mean(data):
return _convert(total/n, T)
+def harmonic_mean(data):
+ """Return the harmonic mean of data.
+
+ The harmonic mean, sometimes called the subcontrary mean, is the
+ reciprocal of the arithmetic mean of the reciprocals of the data,
+ and is often appropriate when averaging quantities which are rates
+ or ratios, for example speeds. Example:
+
+ Suppose an investor purchases an equal value of shares in each of
+ three companies, with P/E (price/earning) ratios of 2.5, 3 and 10.
+ What is the average P/E ratio for the investor's portfolio?
+
+ >>> harmonic_mean([2.5, 3, 10]) # For an equal investment portfolio.
+ 3.6
+
+ Using the arithmetic mean would give an average of about 5.167, which
+ is too high.
+
+ If ``data`` is empty, or any element is less than zero,
+ ``harmonic_mean`` will raise ``StatisticsError``.
+ """
+ # For a justification for using harmonic mean for P/E ratios, see
+ # http://fixthepitch.pellucid.com/comps-analysis-the-missing-harmony-of-summary-statistics/
+ # http://papers.ssrn.com/sol3/papers.cfm?abstract_id=2621087
+ if iter(data) is data:
+ data = list(data)
+ errmsg = 'harmonic mean does not support negative values'
+ n = len(data)
+ if n < 1:
+ raise StatisticsError('harmonic_mean requires at least one data point')
+ elif n == 1:
+ x = data[0]
+ if isinstance(x, (numbers.Real, Decimal)):
+ if x < 0:
+ raise StatisticsError(errmsg)
+ return x
+ else:
+ raise TypeError('unsupported type')
+ try:
+ T, total, count = _sum(1/x for x in _fail_neg(data, errmsg))
+ except ZeroDivisionError:
+ return 0
+ assert count == n
+ return _convert(n/total, T)
+
+
# FIXME: investigate ways to calculate medians without sorting? Quickselect?
def median(data):
"""Return the median (middle value) of numeric data.
@@ -442,9 +469,15 @@ def median_grouped(data, interval=1):
except TypeError:
# Mixed type. For now we just coerce to float.
L = float(x) - float(interval)/2
- cf = data.index(x) # Number of values below the median interval.
- # FIXME The following line could be more efficient for big lists.
- f = data.count(x) # Number of data points in the median interval.
+
+ # Uses bisection search to search for x in data with log(n) time complexity
+ # Find the position of leftmost occurrence of x in data
+ l1 = _find_lteq(data, x)
+ # Find the position of rightmost occurrence of x in data[l1...len(data)]
+ # Assuming always l1 <= l2
+ l2 = _find_rteq(data, l1, x)
+ cf = l1
+ f = l2 - l1 + 1
return L + interval*(n/2 - cf)/f
diff --git a/Lib/string.py b/Lib/string.py
index 89287c4..c902007 100644
--- a/Lib/string.py
+++ b/Lib/string.py
@@ -28,7 +28,7 @@ ascii_letters = ascii_lowercase + ascii_uppercase
digits = '0123456789'
hexdigits = digits + 'abcdef' + 'ABCDEF'
octdigits = '01234567'
-punctuation = """!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
+punctuation = r"""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
printable = digits + ascii_letters + punctuation + whitespace
# Functions which aren't available as string methods.
@@ -116,10 +116,7 @@ class Template(metaclass=_TemplateMetaclass):
# Check the most common path first.
named = mo.group('named') or mo.group('braced')
if named is not None:
- val = mapping[named]
- # We use this idiom instead of str() because the latter will
- # fail if val is a Unicode containing non-ASCII characters.
- return '%s' % (val,)
+ return str(mapping[named])
if mo.group('escaped') is not None:
return self.delimiter
if mo.group('invalid') is not None:
@@ -146,9 +143,7 @@ class Template(metaclass=_TemplateMetaclass):
named = mo.group('named') or mo.group('braced')
if named is not None:
try:
- # We use this idiom instead of str() because the latter
- # will fail if val is a Unicode containing non-ASCII
- return '%s' % (mapping[named],)
+ return str(mapping[named])
except KeyError:
return mo.group()
if mo.group('escaped') is not None:
diff --git a/Lib/subprocess.py b/Lib/subprocess.py
index 502e26b..13b9d44 100644
--- a/Lib/subprocess.py
+++ b/Lib/subprocess.py
@@ -71,7 +71,16 @@ class CalledProcessError(SubprocessError):
self.stderr = stderr
def __str__(self):
- return "Command '%s' returned non-zero exit status %d" % (self.cmd, self.returncode)
+ if self.returncode and self.returncode < 0:
+ try:
+ return "Command '%s' died with %r." % (
+ self.cmd, signal.Signals(-self.returncode))
+ except ValueError:
+ return "Command '%s' died with unknown signal %d." % (
+ self.cmd, -self.returncode)
+ else:
+ return "Command '%s' returned non-zero exit status %d." % (
+ self.cmd, self.returncode)
@property
def stdout(self):
@@ -161,7 +170,8 @@ if _mswindows:
__all__.extend(["CREATE_NEW_CONSOLE", "CREATE_NEW_PROCESS_GROUP",
"STD_INPUT_HANDLE", "STD_OUTPUT_HANDLE",
"STD_ERROR_HANDLE", "SW_HIDE",
- "STARTF_USESTDHANDLES", "STARTF_USESHOWWINDOW"])
+ "STARTF_USESTDHANDLES", "STARTF_USESHOWWINDOW",
+ "STARTUPINFO"])
class Handle(int):
closed = False
@@ -210,6 +220,16 @@ DEVNULL = -3
# but it's here so that it can be imported when Python is compiled without
# threads.
+def _optim_args_from_interpreter_flags():
+ """Return a list of command-line arguments reproducing the current
+ optimization settings in sys.flags."""
+ args = []
+ value = sys.flags.optimize
+ if value > 0:
+ args.append('-' + 'O' * value)
+ return args
+
+
def _args_from_interpreter_flags():
"""Return a list of command-line arguments reproducing the current
settings in sys.flags and sys.warnoptions."""
@@ -217,7 +237,6 @@ def _args_from_interpreter_flags():
'debug': 'd',
# 'inspect': 'i',
# 'interactive': 'i',
- 'optimize': 'O',
'dont_write_bytecode': 'B',
'no_user_site': 's',
'no_site': 'S',
@@ -225,8 +244,9 @@ def _args_from_interpreter_flags():
'verbose': 'v',
'bytes_warning': 'b',
'quiet': 'q',
+ # -O is handled in _optim_args_from_interpreter_flags()
}
- args = []
+ args = _optim_args_from_interpreter_flags()
for flag, opt in flag_opt_map.items():
v = getattr(sys.flags, flag)
if v > 0:
@@ -476,8 +496,8 @@ def getstatusoutput(cmd):
""" Return (status, output) of executing cmd in a shell.
Execute the string 'cmd' in a shell with 'check_output' and
- return a 2-tuple (status, output). Universal newlines mode is used,
- meaning that the result with be decoded to a string.
+ return a 2-tuple (status, output). The locale encoding is used
+ to decode the output and process newlines.
A trailing newline is stripped from the output.
The exit status for the command can be interpreted
@@ -555,6 +575,9 @@ class Popen(object):
pass_fds (POSIX only)
+ encoding and errors: Text mode encoding and error handling to use for
+ file objects stdin, stdout and stderr.
+
Attributes:
stdin, stdout, stderr, pid, returncode
"""
@@ -566,7 +589,7 @@ class Popen(object):
shell=False, cwd=None, env=None, universal_newlines=False,
startupinfo=None, creationflags=0,
restore_signals=True, start_new_session=False,
- pass_fds=()):
+ pass_fds=(), *, encoding=None, errors=None):
"""Create new Popen instance."""
_cleanup()
# Held while anything is calling waitpid before returncode has been
@@ -619,6 +642,8 @@ class Popen(object):
self.pid = None
self.returncode = None
self.universal_newlines = universal_newlines
+ self.encoding = encoding
+ self.errors = errors
# Input and output objects. The general principle is like
# this:
@@ -651,22 +676,28 @@ class Popen(object):
if errread != -1:
errread = msvcrt.open_osfhandle(errread.Detach(), 0)
- if p2cwrite != -1:
- self.stdin = io.open(p2cwrite, 'wb', bufsize)
- if universal_newlines:
- self.stdin = io.TextIOWrapper(self.stdin, write_through=True,
- line_buffering=(bufsize == 1))
- if c2pread != -1:
- self.stdout = io.open(c2pread, 'rb', bufsize)
- if universal_newlines:
- self.stdout = io.TextIOWrapper(self.stdout)
- if errread != -1:
- self.stderr = io.open(errread, 'rb', bufsize)
- if universal_newlines:
- self.stderr = io.TextIOWrapper(self.stderr)
+ text_mode = encoding or errors or universal_newlines
self._closed_child_pipe_fds = False
+
try:
+ if p2cwrite != -1:
+ self.stdin = io.open(p2cwrite, 'wb', bufsize)
+ if text_mode:
+ self.stdin = io.TextIOWrapper(self.stdin, write_through=True,
+ line_buffering=(bufsize == 1),
+ encoding=encoding, errors=errors)
+ if c2pread != -1:
+ self.stdout = io.open(c2pread, 'rb', bufsize)
+ if text_mode:
+ self.stdout = io.TextIOWrapper(self.stdout,
+ encoding=encoding, errors=errors)
+ if errread != -1:
+ self.stderr = io.open(errread, 'rb', bufsize)
+ if text_mode:
+ self.stderr = io.TextIOWrapper(self.stderr,
+ encoding=encoding, errors=errors)
+
self._execute_child(args, executable, preexec_fn, close_fds,
pass_fds, cwd, env,
startupinfo, creationflags, shell,
@@ -700,9 +731,8 @@ class Popen(object):
raise
-
- def _translate_newlines(self, data, encoding):
- data = data.decode(encoding)
+ def _translate_newlines(self, data, encoding, errors):
+ data = data.decode(encoding, errors)
return data.replace("\r\n", "\n").replace("\r", "\n")
def __enter__(self):
@@ -720,10 +750,15 @@ class Popen(object):
# Wait for the process to terminate, to avoid zombies.
self.wait()
- def __del__(self, _maxsize=sys.maxsize):
+ def __del__(self, _maxsize=sys.maxsize, _warn=warnings.warn):
if not self._child_created:
# We didn't get to successfully create a child process.
return
+ if self.returncode is None:
+ # Not reading subprocess exit status creates a zombi process which
+ # is only destroyed at the parent python process exit
+ _warn("subprocess %s is still running" % self.pid,
+ ResourceWarning, source=self)
# In case the child hasn't been waited on, check if it's done.
self._internal_poll(_deadstate=_maxsize)
if self.returncode is None and _active is not None:
@@ -996,6 +1031,10 @@ class Popen(object):
"""Wait for child process to terminate. Returns returncode
attribute."""
if endtime is not None:
+ warnings.warn(
+ "'endtime' argument is deprecated; use 'timeout'.",
+ DeprecationWarning,
+ stacklevel=2)
timeout = self._remaining_time(endtime)
if timeout is None:
timeout_millis = _winapi.INFINITE
@@ -1251,9 +1290,14 @@ class Popen(object):
if errpipe_data:
try:
- os.waitpid(self.pid, 0)
+ pid, sts = os.waitpid(self.pid, 0)
+ if pid == self.pid:
+ self._handle_exitstatus(sts)
+ else:
+ self.returncode = sys.maxsize
except ChildProcessError:
pass
+
try:
exception_name, hex_errno, err_msg = (
errpipe_data.split(b':', 2))
@@ -1352,8 +1396,11 @@ class Popen(object):
if self.returncode is not None:
return self.returncode
- # endtime is preferred to timeout. timeout is only used for
- # printing.
+ if endtime is not None:
+ warnings.warn(
+ "'endtime' argument is deprecated; use 'timeout'.",
+ DeprecationWarning,
+ stacklevel=2)
if endtime is not None or timeout is not None:
if endtime is None:
endtime = _time() + timeout
@@ -1479,13 +1526,15 @@ class Popen(object):
# Translate newlines, if requested.
# This also turns bytes into strings.
- if self.universal_newlines:
+ if self.encoding or self.errors or self.universal_newlines:
if stdout is not None:
stdout = self._translate_newlines(stdout,
- self.stdout.encoding)
+ self.stdout.encoding,
+ self.stdout.errors)
if stderr is not None:
stderr = self._translate_newlines(stderr,
- self.stderr.encoding)
+ self.stderr.encoding,
+ self.stderr.errors)
return (stdout, stderr)
@@ -1497,8 +1546,10 @@ class Popen(object):
if self.stdin and self._input is None:
self._input_offset = 0
self._input = input
- if self.universal_newlines and input is not None:
- self._input = self._input.encode(self.stdin.encoding)
+ if input is not None and (
+ self.encoding or self.errors or self.universal_newlines):
+ self._input = self._input.encode(self.stdin.encoding,
+ self.stdin.errors)
def send_signal(self, sig):
diff --git a/Lib/symbol.py b/Lib/symbol.py
index 7541497..d9f01e0 100755
--- a/Lib/symbol.py
+++ b/Lib/symbol.py
@@ -27,74 +27,75 @@ stmt = 269
simple_stmt = 270
small_stmt = 271
expr_stmt = 272
-testlist_star_expr = 273
-augassign = 274
-del_stmt = 275
-pass_stmt = 276
-flow_stmt = 277
-break_stmt = 278
-continue_stmt = 279
-return_stmt = 280
-yield_stmt = 281
-raise_stmt = 282
-import_stmt = 283
-import_name = 284
-import_from = 285
-import_as_name = 286
-dotted_as_name = 287
-import_as_names = 288
-dotted_as_names = 289
-dotted_name = 290
-global_stmt = 291
-nonlocal_stmt = 292
-assert_stmt = 293
-compound_stmt = 294
-async_stmt = 295
-if_stmt = 296
-while_stmt = 297
-for_stmt = 298
-try_stmt = 299
-with_stmt = 300
-with_item = 301
-except_clause = 302
-suite = 303
-test = 304
-test_nocond = 305
-lambdef = 306
-lambdef_nocond = 307
-or_test = 308
-and_test = 309
-not_test = 310
-comparison = 311
-comp_op = 312
-star_expr = 313
-expr = 314
-xor_expr = 315
-and_expr = 316
-shift_expr = 317
-arith_expr = 318
-term = 319
-factor = 320
-power = 321
-atom_expr = 322
-atom = 323
-testlist_comp = 324
-trailer = 325
-subscriptlist = 326
-subscript = 327
-sliceop = 328
-exprlist = 329
-testlist = 330
-dictorsetmaker = 331
-classdef = 332
-arglist = 333
-argument = 334
-comp_iter = 335
-comp_for = 336
-comp_if = 337
-encoding_decl = 338
-yield_expr = 339
-yield_arg = 340
+annassign = 273
+testlist_star_expr = 274
+augassign = 275
+del_stmt = 276
+pass_stmt = 277
+flow_stmt = 278
+break_stmt = 279
+continue_stmt = 280
+return_stmt = 281
+yield_stmt = 282
+raise_stmt = 283
+import_stmt = 284
+import_name = 285
+import_from = 286
+import_as_name = 287
+dotted_as_name = 288
+import_as_names = 289
+dotted_as_names = 290
+dotted_name = 291
+global_stmt = 292
+nonlocal_stmt = 293
+assert_stmt = 294
+compound_stmt = 295
+async_stmt = 296
+if_stmt = 297
+while_stmt = 298
+for_stmt = 299
+try_stmt = 300
+with_stmt = 301
+with_item = 302
+except_clause = 303
+suite = 304
+test = 305
+test_nocond = 306
+lambdef = 307
+lambdef_nocond = 308
+or_test = 309
+and_test = 310
+not_test = 311
+comparison = 312
+comp_op = 313
+star_expr = 314
+expr = 315
+xor_expr = 316
+and_expr = 317
+shift_expr = 318
+arith_expr = 319
+term = 320
+factor = 321
+power = 322
+atom_expr = 323
+atom = 324
+testlist_comp = 325
+trailer = 326
+subscriptlist = 327
+subscript = 328
+sliceop = 329
+exprlist = 330
+testlist = 331
+dictorsetmaker = 332
+classdef = 333
+arglist = 334
+argument = 335
+comp_iter = 336
+comp_for = 337
+comp_if = 338
+encoding_decl = 339
+yield_expr = 340
+yield_arg = 341
#--end constants--
sym_name = {}
diff --git a/Lib/symtable.py b/Lib/symtable.py
index 84fec4a..b0e5260 100644
--- a/Lib/symtable.py
+++ b/Lib/symtable.py
@@ -2,7 +2,7 @@
import _symtable
from _symtable import (USE, DEF_GLOBAL, DEF_LOCAL, DEF_PARAM,
- DEF_IMPORT, DEF_BOUND, SCOPE_OFF, SCOPE_MASK, FREE,
+ DEF_IMPORT, DEF_BOUND, DEF_ANNOT, SCOPE_OFF, SCOPE_MASK, FREE,
LOCAL, GLOBAL_IMPLICIT, GLOBAL_EXPLICIT, CELL)
import weakref
@@ -190,6 +190,9 @@ class Symbol(object):
def is_local(self):
return bool(self.__flags & DEF_BOUND)
+ def is_annotated(self):
+ return bool(self.__flags & DEF_ANNOT)
+
def is_free(self):
return bool(self.__scope == FREE)
diff --git a/Lib/sysconfig.py b/Lib/sysconfig.py
index 9c34be0..9314e71 100644
--- a/Lib/sysconfig.py
+++ b/Lib/sysconfig.py
@@ -86,8 +86,8 @@ _SCHEME_KEYS = ('stdlib', 'platstdlib', 'purelib', 'platlib', 'include',
# FIXME don't rely on sys.version here, its format is an implementation detail
# of CPython, use sys.version_info or sys.hexversion
_PY_VERSION = sys.version.split()[0]
-_PY_VERSION_SHORT = sys.version[:3]
-_PY_VERSION_SHORT_NO_DOT = _PY_VERSION[0] + _PY_VERSION[2]
+_PY_VERSION_SHORT = '%d.%d' % sys.version_info[:2]
+_PY_VERSION_SHORT_NO_DOT = '%d%d' % sys.version_info[:2]
_PREFIX = os.path.normpath(sys.prefix)
_BASE_PREFIX = os.path.normpath(sys.base_prefix)
_EXEC_PREFIX = os.path.normpath(sys.exec_prefix)
@@ -215,7 +215,7 @@ def _parse_makefile(filename, vars=None):
# Regexes needed for parsing Makefile (and similar syntaxes,
# like old-style Setup files).
import re
- _variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
+ _variable_rx = re.compile(r"([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
@@ -337,8 +337,20 @@ def get_makefile_filename():
config_dir_name = 'config-%s%s' % (_PY_VERSION_SHORT, sys.abiflags)
else:
config_dir_name = 'config'
+ if hasattr(sys.implementation, '_multiarch'):
+ config_dir_name += '-%s' % sys.implementation._multiarch
return os.path.join(get_path('stdlib'), config_dir_name, 'Makefile')
+
+def _get_sysconfigdata_name():
+ return os.environ.get('_PYTHON_SYSCONFIGDATA_NAME',
+ '_sysconfigdata_{abi}_{platform}_{multiarch}'.format(
+ abi=sys.abiflags,
+ platform=sys.platform,
+ multiarch=getattr(sys.implementation, '_multiarch', ''),
+ ))
+
+
def _generate_posix_vars():
"""Generate the Python module containing build-time variables."""
import pprint
@@ -379,14 +391,14 @@ def _generate_posix_vars():
# _sysconfigdata module manually and populate it with the build vars.
# This is more than sufficient for ensuring the subsequent call to
# get_platform() succeeds.
- name = '_sysconfigdata'
+ name = _get_sysconfigdata_name()
if 'darwin' in sys.platform:
import types
module = types.ModuleType(name)
module.build_time_vars = vars
sys.modules[name] = module
- pybuilddir = 'build/lib.%s-%s' % (get_platform(), sys.version[:3])
+ pybuilddir = 'build/lib.%s-%s' % (get_platform(), _PY_VERSION_SHORT)
if hasattr(sys, "gettotalrefcount"):
pybuilddir += '-pydebug'
os.makedirs(pybuilddir, exist_ok=True)
@@ -405,7 +417,9 @@ def _generate_posix_vars():
def _init_posix(vars):
"""Initialize the module as appropriate for POSIX systems."""
# _sysconfigdata is generated at build time, see _generate_posix_vars()
- from _sysconfigdata import build_time_vars
+ name = _get_sysconfigdata_name()
+ _temp = __import__(name, globals(), locals(), ['build_time_vars'], 0)
+ build_time_vars = _temp.build_time_vars
vars.update(build_time_vars)
def _init_non_posix(vars):
@@ -518,7 +532,7 @@ def get_config_vars(*args):
_CONFIG_VARS['exec_prefix'] = _EXEC_PREFIX
_CONFIG_VARS['py_version'] = _PY_VERSION
_CONFIG_VARS['py_version_short'] = _PY_VERSION_SHORT
- _CONFIG_VARS['py_version_nodot'] = _PY_VERSION[0] + _PY_VERSION[2]
+ _CONFIG_VARS['py_version_nodot'] = _PY_VERSION_SHORT_NO_DOT
_CONFIG_VARS['installed_base'] = _BASE_PREFIX
_CONFIG_VARS['base'] = _PREFIX
_CONFIG_VARS['installed_platbase'] = _BASE_EXEC_PREFIX
diff --git a/Lib/tarfile.py b/Lib/tarfile.py
index 999a99b..5d4c86c 100755
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -50,9 +50,13 @@ import copy
import re
try:
- import grp, pwd
+ import pwd
except ImportError:
- grp = pwd = None
+ pwd = None
+try:
+ import grp
+except ImportError:
+ grp = None
# os.symlink on Windows prior to 6.0 raises NotImplementedError
symlink_exception = (AttributeError, NotImplementedError)
@@ -64,7 +68,10 @@ except NameError:
pass
# from tarfile import *
-__all__ = ["TarFile", "TarInfo", "is_tarfile", "TarError"]
+__all__ = ["TarFile", "TarInfo", "is_tarfile", "TarError", "ReadError",
+ "CompressionError", "StreamError", "ExtractError", "HeaderError",
+ "ENCODING", "USTAR_FORMAT", "GNU_FORMAT", "PAX_FORMAT",
+ "DEFAULT_FORMAT", "open"]
#---------------------------------------------------------
# tar constants
@@ -141,7 +148,7 @@ PAX_NUMBER_FIELDS = {
#---------------------------------------------------------
# initialization
#---------------------------------------------------------
-if os.name in ("nt", "ce"):
+if os.name == "nt":
ENCODING = "utf-8"
else:
ENCODING = sys.getfilesystemencoding()
@@ -225,21 +232,21 @@ def calc_chksums(buf):
signed_chksum = 256 + sum(struct.unpack_from("148b8x356b", buf))
return unsigned_chksum, signed_chksum
-def copyfileobj(src, dst, length=None, exception=OSError):
+def copyfileobj(src, dst, length=None, exception=OSError, bufsize=None):
"""Copy length bytes from fileobj src to fileobj dst.
If length is None, copy the entire content.
"""
+ bufsize = bufsize or 16 * 1024
if length == 0:
return
if length is None:
- shutil.copyfileobj(src, dst)
+ shutil.copyfileobj(src, dst, bufsize)
return
- BUFSIZE = 16 * 1024
- blocks, remainder = divmod(length, BUFSIZE)
+ blocks, remainder = divmod(length, bufsize)
for b in range(blocks):
- buf = src.read(BUFSIZE)
- if len(buf) < BUFSIZE:
+ buf = src.read(bufsize)
+ if len(buf) < bufsize:
raise exception("unexpected end of data")
dst.write(buf)
@@ -1400,7 +1407,8 @@ class TarFile(object):
def __init__(self, name=None, mode="r", fileobj=None, format=None,
tarinfo=None, dereference=None, ignore_zeros=None, encoding=None,
- errors="surrogateescape", pax_headers=None, debug=None, errorlevel=None):
+ errors="surrogateescape", pax_headers=None, debug=None,
+ errorlevel=None, copybufsize=None):
"""Open an (uncompressed) tar archive `name'. `mode' is either 'r' to
read from an existing archive, 'a' to append data to an existing
file or 'w' to create a new file overwriting an existing one. `mode'
@@ -1456,6 +1464,7 @@ class TarFile(object):
self.errorlevel = errorlevel
# Init datastructures.
+ self.copybufsize = copybufsize
self.closed = False
self.members = [] # list of members as TarInfo objects
self._loaded = False # flag if all members have been read
@@ -1557,7 +1566,7 @@ class TarFile(object):
saved_pos = fileobj.tell()
try:
return func(name, "r", fileobj, **kwargs)
- except (ReadError, CompressionError) as e:
+ except (ReadError, CompressionError):
if fileobj is not None:
fileobj.seek(saved_pos)
continue
@@ -1962,10 +1971,10 @@ class TarFile(object):
buf = tarinfo.tobuf(self.format, self.encoding, self.errors)
self.fileobj.write(buf)
self.offset += len(buf)
-
+ bufsize=self.copybufsize
# If there's data to follow, append it.
if fileobj is not None:
- copyfileobj(fileobj, self.fileobj, tarinfo.size)
+ copyfileobj(fileobj, self.fileobj, tarinfo.size, bufsize=bufsize)
blocks, remainder = divmod(tarinfo.size, BLOCKSIZE)
if remainder > 0:
self.fileobj.write(NUL * (BLOCKSIZE - remainder))
@@ -2147,15 +2156,16 @@ class TarFile(object):
"""
source = self.fileobj
source.seek(tarinfo.offset_data)
+ bufsize = self.copybufsize
with bltn_open(targetpath, "wb") as target:
if tarinfo.sparse is not None:
for offset, size in tarinfo.sparse:
target.seek(offset)
- copyfileobj(source, target, size, ReadError)
+ copyfileobj(source, target, size, ReadError, bufsize)
target.seek(tarinfo.size)
target.truncate()
else:
- copyfileobj(source, target, tarinfo.size, ReadError)
+ copyfileobj(source, target, tarinfo.size, ReadError, bufsize)
def makeunknown(self, tarinfo, targetpath):
"""Make a file from a TarInfo object with an unknown type
@@ -2213,28 +2223,31 @@ class TarFile(object):
def chown(self, tarinfo, targetpath, numeric_owner):
"""Set owner of targetpath according to tarinfo. If numeric_owner
- is True, use .gid/.uid instead of .gname/.uname.
+ is True, use .gid/.uid instead of .gname/.uname. If numeric_owner
+ is False, fall back to .gid/.uid when the search based on name
+ fails.
"""
- if pwd and hasattr(os, "geteuid") and os.geteuid() == 0:
+ if hasattr(os, "geteuid") and os.geteuid() == 0:
# We have to be root to do so.
- if numeric_owner:
- g = tarinfo.gid
- u = tarinfo.uid
- else:
+ g = tarinfo.gid
+ u = tarinfo.uid
+ if not numeric_owner:
try:
- g = grp.getgrnam(tarinfo.gname)[2]
+ if grp:
+ g = grp.getgrnam(tarinfo.gname)[2]
except KeyError:
- g = tarinfo.gid
+ pass
try:
- u = pwd.getpwnam(tarinfo.uname)[2]
+ if pwd:
+ u = pwd.getpwnam(tarinfo.uname)[2]
except KeyError:
- u = tarinfo.uid
+ pass
try:
if tarinfo.issym() and hasattr(os, "lchown"):
os.lchown(targetpath, u, g)
else:
os.chown(targetpath, u, g)
- except OSError as e:
+ except OSError:
raise ExtractError("could not change owner")
def chmod(self, tarinfo, targetpath):
@@ -2243,7 +2256,7 @@ class TarFile(object):
if hasattr(os, 'chmod'):
try:
os.chmod(targetpath, tarinfo.mode)
- except OSError as e:
+ except OSError:
raise ExtractError("could not change mode")
def utime(self, tarinfo, targetpath):
@@ -2253,7 +2266,7 @@ class TarFile(object):
return
try:
os.utime(targetpath, (tarinfo.mtime, tarinfo.mtime))
- except OSError as e:
+ except OSError:
raise ExtractError("could not change modification time")
#--------------------------------------------------------------------------
@@ -2376,9 +2389,32 @@ class TarFile(object):
"""Provide an iterator object.
"""
if self._loaded:
- return iter(self.members)
- else:
- return TarIter(self)
+ yield from self.members
+ return
+
+ # Yield items using TarFile's next() method.
+ # When all members have been read, set TarFile as _loaded.
+ index = 0
+ # Fix for SF #1100429: Under rare circumstances it can
+ # happen that getmembers() is called during iteration,
+ # which will have already exhausted the next() method.
+ if self.firstmember is not None:
+ tarinfo = self.next()
+ index += 1
+ yield tarinfo
+
+ while True:
+ if index < len(self.members):
+ tarinfo = self.members[index]
+ elif not self._loaded:
+ tarinfo = self.next()
+ if not tarinfo:
+ self._loaded = True
+ return
+ else:
+ return
+ index += 1
+ yield tarinfo
def _dbg(self, level, msg):
"""Write debugging output to sys.stderr.
@@ -2399,45 +2435,6 @@ class TarFile(object):
if not self._extfileobj:
self.fileobj.close()
self.closed = True
-# class TarFile
-
-class TarIter:
- """Iterator Class.
-
- for tarinfo in TarFile(...):
- suite...
- """
-
- def __init__(self, tarfile):
- """Construct a TarIter object.
- """
- self.tarfile = tarfile
- self.index = 0
- def __iter__(self):
- """Return iterator object.
- """
- return self
- def __next__(self):
- """Return the next item using TarFile's next() method.
- When all members have been read, set TarFile as _loaded.
- """
- # Fix for SF #1100429: Under rare circumstances it can
- # happen that getmembers() is called during iteration,
- # which will cause TarIter to stop prematurely.
-
- if self.index == 0 and self.tarfile.firstmember is not None:
- tarinfo = self.tarfile.next()
- elif self.index < len(self.tarfile.members):
- tarinfo = self.tarfile.members[self.index]
- elif not self.tarfile._loaded:
- tarinfo = self.tarfile.next()
- if not tarinfo:
- self.tarfile._loaded = True
- raise StopIteration
- else:
- raise StopIteration
- self.index += 1
- return tarinfo
#--------------------
# exported functions
diff --git a/Lib/telnetlib.py b/Lib/telnetlib.py
index 72dabc7..b0863b1 100644
--- a/Lib/telnetlib.py
+++ b/Lib/telnetlib.py
@@ -637,6 +637,12 @@ class Telnet:
raise EOFError
return (-1, None, text)
+ def __enter__(self):
+ return self
+
+ def __exit__(self, type, value, traceback):
+ self.close()
+
def test():
"""Test program for telnetlib.
@@ -660,11 +666,10 @@ def test():
port = int(portstr)
except ValueError:
port = socket.getservbyname(portstr, 'tcp')
- tn = Telnet()
- tn.set_debuglevel(debuglevel)
- tn.open(host, port, timeout=0.5)
- tn.interact()
- tn.close()
+ with Telnet() as tn:
+ tn.set_debuglevel(debuglevel)
+ tn.open(host, port, timeout=0.5)
+ tn.interact()
if __name__ == '__main__':
test()
diff --git a/Lib/tempfile.py b/Lib/tempfile.py
index ad687b9..6146235 100644
--- a/Lib/tempfile.py
+++ b/Lib/tempfile.py
@@ -797,7 +797,6 @@ class TemporaryDirectory(object):
_shutil.rmtree(name)
_warnings.warn(warn_message, ResourceWarning)
-
def __repr__(self):
return "<{} {!r}>".format(self.__class__.__name__, self.name)
diff --git a/Lib/test/__main__.py b/Lib/test/__main__.py
index d5fbe15..19a6b2b 100644
--- a/Lib/test/__main__.py
+++ b/Lib/test/__main__.py
@@ -1,3 +1,2 @@
-from test import regrtest
-
-regrtest.main_in_temp_cwd()
+from test.libregrtest import main
+main()
diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py
index 9b789c2..b5f4782 100644
--- a/Lib/test/_test_multiprocessing.py
+++ b/Lib/test/_test_multiprocessing.py
@@ -1628,13 +1628,33 @@ class _TestContainers(BaseTestCase):
d = [a, b]
e = self.list(d)
self.assertEqual(
- e[:],
+ [element[:] for element in e],
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 0, 1, 2, 3, 4]]
)
f = self.list([a])
a.append('hello')
- self.assertEqual(f[:], [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'hello']])
+ self.assertEqual(f[0][:], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'hello'])
+
+ def test_list_proxy_in_list(self):
+ a = self.list([self.list(range(3)) for _i in range(3)])
+ self.assertEqual([inner[:] for inner in a], [[0, 1, 2]] * 3)
+
+ a[0][-1] = 55
+ self.assertEqual(a[0][:], [0, 1, 55])
+ for i in range(1, 3):
+ self.assertEqual(a[i][:], [0, 1, 2])
+
+ self.assertEqual(a[1].pop(), 2)
+ self.assertEqual(len(a[1]), 2)
+ for i in range(0, 3, 2):
+ self.assertEqual(len(a[i]), 3)
+
+ del a
+
+ b = self.list()
+ b.append(b)
+ del b
def test_dict(self):
d = self.dict()
@@ -1646,6 +1666,52 @@ class _TestContainers(BaseTestCase):
self.assertEqual(sorted(d.values()), [chr(i) for i in indices])
self.assertEqual(sorted(d.items()), [(i, chr(i)) for i in indices])
+ def test_dict_proxy_nested(self):
+ pets = self.dict(ferrets=2, hamsters=4)
+ supplies = self.dict(water=10, feed=3)
+ d = self.dict(pets=pets, supplies=supplies)
+
+ self.assertEqual(supplies['water'], 10)
+ self.assertEqual(d['supplies']['water'], 10)
+
+ d['supplies']['blankets'] = 5
+ self.assertEqual(supplies['blankets'], 5)
+ self.assertEqual(d['supplies']['blankets'], 5)
+
+ d['supplies']['water'] = 7
+ self.assertEqual(supplies['water'], 7)
+ self.assertEqual(d['supplies']['water'], 7)
+
+ del pets
+ del supplies
+ self.assertEqual(d['pets']['ferrets'], 2)
+ d['supplies']['blankets'] = 11
+ self.assertEqual(d['supplies']['blankets'], 11)
+
+ pets = d['pets']
+ supplies = d['supplies']
+ supplies['water'] = 7
+ self.assertEqual(supplies['water'], 7)
+ self.assertEqual(d['supplies']['water'], 7)
+
+ d.clear()
+ self.assertEqual(len(d), 0)
+ self.assertEqual(supplies['water'], 7)
+ self.assertEqual(pets['hamsters'], 4)
+
+ l = self.list([pets, supplies])
+ l[0]['marmots'] = 1
+ self.assertEqual(pets['marmots'], 1)
+ self.assertEqual(l[0]['marmots'], 1)
+
+ del pets
+ del supplies
+ self.assertEqual(l[0]['marmots'], 1)
+
+ outer = self.list([[88, 99], l])
+ self.assertIsInstance(outer[0], list) # Not a ListProxy
+ self.assertEqual(outer[-1][-1]['feed'], 3)
+
def test_namespace(self):
n = self.Namespace()
n.name = 'Bob'
@@ -1668,6 +1734,10 @@ def sqr(x, wait=0.0):
def mul(x, y):
return x*y
+def raise_large_valuerror(wait):
+ time.sleep(wait)
+ raise ValueError("x" * 1024**2)
+
class SayWhenError(ValueError): pass
def exception_throwing_generator(total, when):
@@ -1910,6 +1980,26 @@ class _TestPool(BaseTestCase):
with self.assertRaises(RuntimeError):
p.apply(self._test_wrapped_exception)
+ def test_map_no_failfast(self):
+ # Issue #23992: the fail-fast behaviour when an exception is raised
+ # during map() would make Pool.join() deadlock, because a worker
+ # process would fill the result queue (after the result handler thread
+ # terminated, hence not draining it anymore).
+
+ t_start = time.time()
+
+ with self.assertRaises(ValueError):
+ with self.Pool(2) as p:
+ try:
+ p.map(raise_large_valuerror, [0, 1])
+ finally:
+ time.sleep(0.5)
+ p.close()
+ p.join()
+
+ # check that we indeed waited for all jobs
+ self.assertGreater(time.time() - t_start, 0.9)
+
def raising():
raise KeyError("key")
@@ -3780,7 +3870,7 @@ class TestSemaphoreTracker(unittest.TestCase):
p.stderr.close()
expected = 'semaphore_tracker: There appear to be 2 leaked semaphores'
self.assertRegex(err, expected)
- self.assertRegex(err, 'semaphore_tracker: %r: \[Errno' % name1)
+ self.assertRegex(err, r'semaphore_tracker: %r: \[Errno' % name1)
#
# Mixins
diff --git a/Lib/test/ann_module.py b/Lib/test/ann_module.py
new file mode 100644
index 0000000..9e6b87d
--- /dev/null
+++ b/Lib/test/ann_module.py
@@ -0,0 +1,53 @@
+
+
+"""
+The module for testing variable annotations.
+Empty lines above are for good reason (testing for correct line numbers)
+"""
+
+from typing import Optional
+
+__annotations__[1] = 2
+
+class C:
+
+ x = 5; y: Optional['C'] = None
+
+from typing import Tuple
+x: int = 5; y: str = x; f: Tuple[int, int]
+
+class M(type):
+
+ __annotations__['123'] = 123
+ o: type = object
+
+(pars): bool = True
+
+class D(C):
+ j: str = 'hi'; k: str= 'bye'
+
+from types import new_class
+h_class = new_class('H', (C,))
+j_class = new_class('J')
+
+class F():
+ z: int = 5
+ def __init__(self, x):
+ pass
+
+class Y(F):
+ def __init__(self):
+ super(F, self).__init__(123)
+
+class Meta(type):
+ def __new__(meta, name, bases, namespace):
+ return super().__new__(meta, name, bases, namespace)
+
+class S(metaclass = Meta):
+ x: str = 'something'
+ y: str = 'something else'
+
+def foo(x: int = 10):
+ def bar(y: List[str]):
+ x: str = 'yes'
+ bar()
diff --git a/Lib/test/ann_module2.py b/Lib/test/ann_module2.py
new file mode 100644
index 0000000..76cf5b3
--- /dev/null
+++ b/Lib/test/ann_module2.py
@@ -0,0 +1,36 @@
+"""
+Some correct syntax for variable annotation here.
+More examples are in test_grammar and test_parser.
+"""
+
+from typing import no_type_check, ClassVar
+
+i: int = 1
+j: int
+x: float = i/10
+
+def f():
+ class C: ...
+ return C()
+
+f().new_attr: object = object()
+
+class C:
+ def __init__(self, x: int) -> None:
+ self.x = x
+
+c = C(5)
+c.new_attr: int = 10
+
+__annotations__ = {}
+
+
+@no_type_check
+class NTC:
+ def meth(self, param: complex) -> None:
+ ...
+
+class CV:
+ var: ClassVar['CV']
+
+CV.var = CV()
diff --git a/Lib/test/ann_module3.py b/Lib/test/ann_module3.py
new file mode 100644
index 0000000..eccd7be
--- /dev/null
+++ b/Lib/test/ann_module3.py
@@ -0,0 +1,18 @@
+"""
+Correct syntax for variable annotation that should fail at runtime
+in a certain manner. More examples are in test_grammar and test_parser.
+"""
+
+def f_bad_ann():
+ __annotations__[1] = 2
+
+class C_OK:
+ def __init__(self, x: int) -> None:
+ self.x: no_such_name = x # This one is OK as proposed by Guido
+
+class D_bad_ann:
+ def __init__(self, x: int) -> None:
+ sfel.y: int = 0
+
+def g_bad_ann():
+ no_such_name.attr: int = 0
diff --git a/Lib/test/audiotests.py b/Lib/test/audiotests.py
index 0ae2242..d3e8e9e 100644
--- a/Lib/test/audiotests.py
+++ b/Lib/test/audiotests.py
@@ -1,9 +1,8 @@
from test.support import findfile, TESTFN, unlink
-import unittest
import array
import io
import pickle
-import sys
+
class UnseekableIO(io.FileIO):
def tell(self):
diff --git a/Lib/test/autotest.py b/Lib/test/autotest.py
index 41c2088..fa85cc1 100644
--- a/Lib/test/autotest.py
+++ b/Lib/test/autotest.py
@@ -1,6 +1,5 @@
# This should be equivalent to running regrtest.py from the cmdline.
# It can be especially handy if you're in an interactive shell, e.g.,
# from test import autotest.
-
-from test import regrtest
-regrtest.main()
+from test.libregrtest import main
+main()
diff --git a/Lib/test/badsyntax_async1.py b/Lib/test/badsyntax_async1.py
deleted file mode 100644
index fb85e29..0000000
--- a/Lib/test/badsyntax_async1.py
+++ /dev/null
@@ -1,2 +0,0 @@
-async def foo(a=await something()):
- pass
diff --git a/Lib/test/badsyntax_async2.py b/Lib/test/badsyntax_async2.py
deleted file mode 100644
index fb85e29..0000000
--- a/Lib/test/badsyntax_async2.py
+++ /dev/null
@@ -1,2 +0,0 @@
-async def foo(a=await something()):
- pass
diff --git a/Lib/test/badsyntax_async3.py b/Lib/test/badsyntax_async3.py
deleted file mode 100644
index dde1bc5..0000000
--- a/Lib/test/badsyntax_async3.py
+++ /dev/null
@@ -1,2 +0,0 @@
-async def foo():
- [i async for i in els]
diff --git a/Lib/test/badsyntax_async4.py b/Lib/test/badsyntax_async4.py
deleted file mode 100644
index d033b28..0000000
--- a/Lib/test/badsyntax_async4.py
+++ /dev/null
@@ -1,2 +0,0 @@
-async def foo():
- await
diff --git a/Lib/test/badsyntax_async5.py b/Lib/test/badsyntax_async5.py
deleted file mode 100644
index 9d19af6..0000000
--- a/Lib/test/badsyntax_async5.py
+++ /dev/null
@@ -1,2 +0,0 @@
-def foo():
- await something()
diff --git a/Lib/test/badsyntax_async6.py b/Lib/test/badsyntax_async6.py
deleted file mode 100644
index cb0a23d..0000000
--- a/Lib/test/badsyntax_async6.py
+++ /dev/null
@@ -1,2 +0,0 @@
-async def foo():
- yield
diff --git a/Lib/test/badsyntax_async7.py b/Lib/test/badsyntax_async7.py
deleted file mode 100644
index 51e4bf9..0000000
--- a/Lib/test/badsyntax_async7.py
+++ /dev/null
@@ -1,2 +0,0 @@
-async def foo():
- yield from []
diff --git a/Lib/test/badsyntax_async8.py b/Lib/test/badsyntax_async8.py
deleted file mode 100644
index 3c636f9..0000000
--- a/Lib/test/badsyntax_async8.py
+++ /dev/null
@@ -1,2 +0,0 @@
-async def foo():
- await await fut
diff --git a/Lib/test/capath/0e4015b9.0 b/Lib/test/capath/0e4015b9.0
deleted file mode 100644
index b6d259b..0000000
--- a/Lib/test/capath/0e4015b9.0
+++ /dev/null
@@ -1,16 +0,0 @@
------BEGIN CERTIFICATE-----
-MIIClTCCAf6gAwIBAgIJAKGU95wKR8pTMA0GCSqGSIb3DQEBBQUAMHAxCzAJBgNV
-BAYTAlhZMRcwFQYDVQQHDA5DYXN0bGUgQW50aHJheDEjMCEGA1UECgwaUHl0aG9u
-IFNvZnR3YXJlIEZvdW5kYXRpb24xIzAhBgNVBAMMGnNlbGYtc2lnbmVkLnB5dGhv
-bnRlc3QubmV0MB4XDTE0MTEwMjE4MDkyOVoXDTI0MTAzMDE4MDkyOVowcDELMAkG
-A1UEBhMCWFkxFzAVBgNVBAcMDkNhc3RsZSBBbnRocmF4MSMwIQYDVQQKDBpQeXRo
-b24gU29mdHdhcmUgRm91bmRhdGlvbjEjMCEGA1UEAwwac2VsZi1zaWduZWQucHl0
-aG9udGVzdC5uZXQwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBANDXQXW9tjyZ
-Xt0Iv2tLL1+jinr4wGg36ioLDLFkMf+2Y1GL0v0BnKYG4N1OKlAU15LXGeGer8vm
-Sv/yIvmdrELvhAbbo3w4a9TMYQA4XkIVLdvu3mvNOAet+8PMJxn26dbDhG809ALv
-EHY57lQsBS3G59RZyBPVqAqmImWNJnVzAgMBAAGjNzA1MCUGA1UdEQQeMByCGnNl
-bGYtc2lnbmVkLnB5dGhvbnRlc3QubmV0MAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcN
-AQEFBQADgYEAIuzAhgMouJpNdf3URCHIineyoSt6WK/9+eyUcjlKOrDoXNZaD72h
-TXMeKYoWvJyVcSLKL8ckPtDobgP2OTt0UkyAaj0n+ZHaqq1lH2yVfGUA1ILJv515
-C8BqbvVZuqm3i7ygmw3bqE/lYMgOrYtXXnqOrz6nvsE6Yc9V9rFflOM=
------END CERTIFICATE-----
diff --git a/Lib/test/capath/b1930218.0 b/Lib/test/capath/b1930218.0
new file mode 100644
index 0000000..373349c
--- /dev/null
+++ b/Lib/test/capath/b1930218.0
@@ -0,0 +1,21 @@
+-----BEGIN CERTIFICATE-----
+MIIDbTCCAlWgAwIBAgIJALCSZLHy2iHQMA0GCSqGSIb3DQEBBQUAME0xCzAJBgNV
+BAYTAlhZMSYwJAYDVQQKDB1QeXRob24gU29mdHdhcmUgRm91bmRhdGlvbiBDQTEW
+MBQGA1UEAwwNb3VyLWNhLXNlcnZlcjAeFw0xMzAxMDQxOTQ3MDdaFw0yMzAxMDIx
+OTQ3MDdaME0xCzAJBgNVBAYTAlhZMSYwJAYDVQQKDB1QeXRob24gU29mdHdhcmUg
+Rm91bmRhdGlvbiBDQTEWMBQGA1UEAwwNb3VyLWNhLXNlcnZlcjCCASIwDQYJKoZI
+hvcNAQEBBQADggEPADCCAQoCggEBAOfe6eMMnwC2of0rW5bSb8zgvoa5IF7sA3pV
+q+qk6flJhdJm1e3HeupWji2P50LiYiipn9Ybjuu1tJyfFKvf5pSLdh0+bSRh7Qy/
+AIphDN9cyDZzFgDNR7ptpKR0iIMjChn8Cac8SkvT5x0t5OpMVCHzJtuJNxjUArtA
+Ml+k/y0c99S77I7PXIKs5nwIbEiFYQd/JeBc4Lw0X+C5BEd1yEcLjbzWyGhfM4Ni
+0iBENbGtgRqKzbw1sFyLR9YY6ZwYl8wBPCnM6B7k5MG43ufCERiHWpM02KYl9xRx
+6+QhotIPLi7UYgA109bvXGBLTKkU4t0VWEY3Mya35y5d7ULkxU0CAwEAAaNQME4w
+HQYDVR0OBBYEFLzdYtl22hvSVGvP4GabHh57VgwLMB8GA1UdIwQYMBaAFLzdYtl2
+2hvSVGvP4GabHh57VgwLMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADggEB
+AH0K9cuN0129mY74Kw+668LZpidPLnsvDmTYHDVQTu78kLmNbajFxgawr/Mtvzu4
+QgfdGH1tlVRXhRhgRy/reBv56Bf9Wg2HFyisTGrmvCn09FVwKULeheqrbCMGZDB1
+Ao5TvF4BMzfMHs24pP3K5F9lO4MchvFVAqA6j9uRt0AUtOeN0u5zuuPlNC28lG9O
+JAb3X4sOp45r3l519DKaULFEM5rQBeJ4gv/b2opj66nd0b+gYa3jnookXWIO50yR
+f+/fNDY7L131hLIvxG2TlhpvMCjx2hKaZLRAMx293itTqOq+1rxOlvVE+zIYrtUf
+9mmvtk57HVjsO6lTo15YyJ4=
+-----END CERTIFICATE-----
diff --git a/Lib/test/capath/ce7b8643.0 b/Lib/test/capath/ce7b8643.0
deleted file mode 100644
index b6d259b..0000000
--- a/Lib/test/capath/ce7b8643.0
+++ /dev/null
@@ -1,16 +0,0 @@
------BEGIN CERTIFICATE-----
-MIIClTCCAf6gAwIBAgIJAKGU95wKR8pTMA0GCSqGSIb3DQEBBQUAMHAxCzAJBgNV
-BAYTAlhZMRcwFQYDVQQHDA5DYXN0bGUgQW50aHJheDEjMCEGA1UECgwaUHl0aG9u
-IFNvZnR3YXJlIEZvdW5kYXRpb24xIzAhBgNVBAMMGnNlbGYtc2lnbmVkLnB5dGhv
-bnRlc3QubmV0MB4XDTE0MTEwMjE4MDkyOVoXDTI0MTAzMDE4MDkyOVowcDELMAkG
-A1UEBhMCWFkxFzAVBgNVBAcMDkNhc3RsZSBBbnRocmF4MSMwIQYDVQQKDBpQeXRo
-b24gU29mdHdhcmUgRm91bmRhdGlvbjEjMCEGA1UEAwwac2VsZi1zaWduZWQucHl0
-aG9udGVzdC5uZXQwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBANDXQXW9tjyZ
-Xt0Iv2tLL1+jinr4wGg36ioLDLFkMf+2Y1GL0v0BnKYG4N1OKlAU15LXGeGer8vm
-Sv/yIvmdrELvhAbbo3w4a9TMYQA4XkIVLdvu3mvNOAet+8PMJxn26dbDhG809ALv
-EHY57lQsBS3G59RZyBPVqAqmImWNJnVzAgMBAAGjNzA1MCUGA1UdEQQeMByCGnNl
-bGYtc2lnbmVkLnB5dGhvbnRlc3QubmV0MAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcN
-AQEFBQADgYEAIuzAhgMouJpNdf3URCHIineyoSt6WK/9+eyUcjlKOrDoXNZaD72h
-TXMeKYoWvJyVcSLKL8ckPtDobgP2OTt0UkyAaj0n+ZHaqq1lH2yVfGUA1ILJv515
-C8BqbvVZuqm3i7ygmw3bqE/lYMgOrYtXXnqOrz6nvsE6Yc9V9rFflOM=
------END CERTIFICATE-----
diff --git a/Lib/test/capath/ceff1710.0 b/Lib/test/capath/ceff1710.0
new file mode 100644
index 0000000..373349c
--- /dev/null
+++ b/Lib/test/capath/ceff1710.0
@@ -0,0 +1,21 @@
+-----BEGIN CERTIFICATE-----
+MIIDbTCCAlWgAwIBAgIJALCSZLHy2iHQMA0GCSqGSIb3DQEBBQUAME0xCzAJBgNV
+BAYTAlhZMSYwJAYDVQQKDB1QeXRob24gU29mdHdhcmUgRm91bmRhdGlvbiBDQTEW
+MBQGA1UEAwwNb3VyLWNhLXNlcnZlcjAeFw0xMzAxMDQxOTQ3MDdaFw0yMzAxMDIx
+OTQ3MDdaME0xCzAJBgNVBAYTAlhZMSYwJAYDVQQKDB1QeXRob24gU29mdHdhcmUg
+Rm91bmRhdGlvbiBDQTEWMBQGA1UEAwwNb3VyLWNhLXNlcnZlcjCCASIwDQYJKoZI
+hvcNAQEBBQADggEPADCCAQoCggEBAOfe6eMMnwC2of0rW5bSb8zgvoa5IF7sA3pV
+q+qk6flJhdJm1e3HeupWji2P50LiYiipn9Ybjuu1tJyfFKvf5pSLdh0+bSRh7Qy/
+AIphDN9cyDZzFgDNR7ptpKR0iIMjChn8Cac8SkvT5x0t5OpMVCHzJtuJNxjUArtA
+Ml+k/y0c99S77I7PXIKs5nwIbEiFYQd/JeBc4Lw0X+C5BEd1yEcLjbzWyGhfM4Ni
+0iBENbGtgRqKzbw1sFyLR9YY6ZwYl8wBPCnM6B7k5MG43ufCERiHWpM02KYl9xRx
+6+QhotIPLi7UYgA109bvXGBLTKkU4t0VWEY3Mya35y5d7ULkxU0CAwEAAaNQME4w
+HQYDVR0OBBYEFLzdYtl22hvSVGvP4GabHh57VgwLMB8GA1UdIwQYMBaAFLzdYtl2
+2hvSVGvP4GabHh57VgwLMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADggEB
+AH0K9cuN0129mY74Kw+668LZpidPLnsvDmTYHDVQTu78kLmNbajFxgawr/Mtvzu4
+QgfdGH1tlVRXhRhgRy/reBv56Bf9Wg2HFyisTGrmvCn09FVwKULeheqrbCMGZDB1
+Ao5TvF4BMzfMHs24pP3K5F9lO4MchvFVAqA6j9uRt0AUtOeN0u5zuuPlNC28lG9O
+JAb3X4sOp45r3l519DKaULFEM5rQBeJ4gv/b2opj66nd0b+gYa3jnookXWIO50yR
+f+/fNDY7L131hLIvxG2TlhpvMCjx2hKaZLRAMx293itTqOq+1rxOlvVE+zIYrtUf
+9mmvtk57HVjsO6lTo15YyJ4=
+-----END CERTIFICATE-----
diff --git a/Lib/test/cmath_testcases.txt b/Lib/test/cmath_testcases.txt
index 9b08653..dd7e458 100644
--- a/Lib/test/cmath_testcases.txt
+++ b/Lib/test/cmath_testcases.txt
@@ -53,6 +53,12 @@
-- MPFR homepage at http://www.mpfr.org for more information about the
-- MPFR project.
+-- A minority of the test cases were generated with the help of
+-- mpmath 0.19 at 100 bit accuracy (http://mpmath.org) to improve
+-- coverage of real functions with real-valued arguments. These are
+-- used in test.test_math.MathTests.test_testfile, as well as in
+-- test_cmath.
+
--------------------------
-- acos: Inverse cosine --
@@ -848,6 +854,18 @@ atan0302 atan 9.9999999999999999e-161 -1.0 -> 0.78539816339744828 -184.553381029
atan0303 atan -1e-165 1.0 -> -0.78539816339744828 190.30984376228875
atan0304 atan -9.9998886718268301e-321 -1.0 -> -0.78539816339744828 -368.76019403576692
+-- Additional real values (mpmath)
+atan0400 atan 1.7976931348623157e+308 0.0 -> 1.5707963267948966192 0.0
+atan0401 atan -1.7976931348623157e+308 0.0 -> -1.5707963267948966192 0.0
+atan0402 atan 1e-17 0.0 -> 1.0000000000000000715e-17 0.0
+atan0403 atan -1e-17 0.0 -> -1.0000000000000000715e-17 0.0
+atan0404 atan 0.0001 0.0 -> 0.000099999999666666673459 0.0
+atan0405 atan -0.0001 0.0 -> -0.000099999999666666673459 0.0
+atan0406 atan 0.999999999999999 0.0 -> 0.78539816339744781002 0.0
+atan0407 atan 1.000000000000001 0.0 -> 0.78539816339744886473 0.0
+atan0408 atan 14.101419947171719 0.0 -> 1.4999999999999999969 0.0
+atan0409 atan 1255.7655915007897 0.0 -> 1.5700000000000000622 0.0
+
-- special values
atan1000 atan -0.0 0.0 -> -0.0 0.0
atan1001 atan nan 0.0 -> nan 0.0
@@ -1514,6 +1532,11 @@ sqrt0131 sqrt -1.5477066694467245e-310 -0.0 -> 0.0 -1.2440685951533077e-155
sqrt0140 sqrt 1.6999999999999999e+308 -1.6999999999999999e+308 -> 1.4325088230154573e+154 -5.9336458271212207e+153
sqrt0141 sqrt -1.797e+308 -9.9999999999999999e+306 -> 3.7284476432057307e+152 -1.3410406899802901e+154
+-- Additional real values (mpmath)
+sqrt0150 sqrt 1.7976931348623157e+308 0.0 -> 1.3407807929942596355e+154 0.0
+sqrt0151 sqrt 2.2250738585072014e-308 0.0 -> 1.4916681462400413487e-154 0.0
+sqrt0152 sqrt 5e-324 0.0 -> 2.2227587494850774834e-162 0.0
+
-- special values
sqrt1000 sqrt 0.0 0.0 -> 0.0 0.0
sqrt1001 sqrt -0.0 0.0 -> 0.0 0.0
@@ -1616,6 +1639,20 @@ exp0052 exp 710.0 1.5 -> 1.5802653829857376e+307 inf overflow
exp0053 exp 710.0 1.6 -> -6.5231579995501372e+306 inf overflow
exp0054 exp 710.0 2.8 -> -inf 7.4836177417448528e+307 overflow
+-- Additional real values (mpmath)
+exp0070 exp 1e-08 0.0 -> 1.00000001000000005 0.0
+exp0071 exp 0.0003 0.0 -> 1.0003000450045003375 0.0
+exp0072 exp 0.2 0.0 -> 1.2214027581601698475 0.0
+exp0073 exp 1.0 0.0 -> 2.7182818284590452354 0.0
+exp0074 exp -1e-08 0.0 -> 0.99999999000000005 0.0
+exp0075 exp -0.0003 0.0 -> 0.99970004499550033751 0.0
+exp0076 exp -1.0 0.0 -> 0.3678794411714423216 0.0
+exp0077 exp 2.220446049250313e-16 0.0 -> 1.000000000000000222 0.0
+exp0078 exp -1.1102230246251565e-16 0.0 -> 0.99999999999999988898 0.0
+exp0079 exp 2.302585092994046 0.0 -> 10.000000000000002171 0.0
+exp0080 exp -2.302585092994046 0.0 -> 0.099999999999999978292 0.0
+exp0081 exp 709.7827 0.0 -> 1.7976699566638014654e+308 0.0
+
-- special values
exp1000 exp 0.0 0.0 -> 1.0 0.0
exp1001 exp -0.0 0.0 -> 1.0 0.0
@@ -1708,6 +1745,23 @@ cosh0023 cosh 2.218885944363501 2.0015727395883687 -> -1.94294321081968 4.129026
cosh0030 cosh 710.5 2.3519999999999999 -> -1.2967465239355998e+308 1.3076707908857333e+308
cosh0031 cosh -710.5 0.69999999999999996 -> 1.4085466381392499e+308 -1.1864024666450239e+308
+-- Additional real values (mpmath)
+cosh0050 cosh 1e-150 0.0 -> 1.0 0.0
+cosh0051 cosh 1e-18 0.0 -> 1.0 0.0
+cosh0052 cosh 1e-09 0.0 -> 1.0000000000000000005 0.0
+cosh0053 cosh 0.0003 0.0 -> 1.0000000450000003375 0.0
+cosh0054 cosh 0.2 0.0 -> 1.0200667556190758485 0.0
+cosh0055 cosh 1.0 0.0 -> 1.5430806348152437785 0.0
+cosh0056 cosh -1e-18 0.0 -> 1.0 -0.0
+cosh0057 cosh -0.0003 0.0 -> 1.0000000450000003375 -0.0
+cosh0058 cosh -1.0 0.0 -> 1.5430806348152437785 -0.0
+cosh0059 cosh 1.3169578969248168 0.0 -> 2.0000000000000001504 0.0
+cosh0060 cosh -1.3169578969248168 0.0 -> 2.0000000000000001504 -0.0
+cosh0061 cosh 17.328679513998633 0.0 -> 16777216.000000021938 0.0
+cosh0062 cosh 18.714973875118524 0.0 -> 67108864.000000043662 0.0
+cosh0063 cosh 709.7827 0.0 -> 8.9883497833190073272e+307 0.0
+cosh0064 cosh -709.7827 0.0 -> 8.9883497833190073272e+307 -0.0
+
-- special values
cosh1000 cosh 0.0 0.0 -> 1.0 0.0
cosh1001 cosh 0.0 inf -> nan 0.0 invalid ignore-imag-sign
@@ -1800,6 +1854,24 @@ sinh0023 sinh 0.043713693678420068 0.22512549887532657 -> 0.042624198673416713 0
sinh0030 sinh 710.5 -2.3999999999999999 -> -1.3579970564885919e+308 -1.24394470907798e+308
sinh0031 sinh -710.5 0.80000000000000004 -> -1.2830671601735164e+308 1.3210954193997678e+308
+-- Additional real values (mpmath)
+sinh0050 sinh 1e-100 0.0 -> 1.00000000000000002e-100 0.0
+sinh0051 sinh 5e-17 0.0 -> 4.9999999999999998955e-17 0.0
+sinh0052 sinh 1e-16 0.0 -> 9.999999999999999791e-17 0.0
+sinh0053 sinh 3.7e-08 0.0 -> 3.7000000000000008885e-8 0.0
+sinh0054 sinh 0.001 0.0 -> 0.0010000001666666750208 0.0
+sinh0055 sinh 0.2 0.0 -> 0.20133600254109399895 0.0
+sinh0056 sinh 1.0 0.0 -> 1.1752011936438014569 0.0
+sinh0057 sinh -3.7e-08 0.0 -> -3.7000000000000008885e-8 0.0
+sinh0058 sinh -0.001 0.0 -> -0.0010000001666666750208 0.0
+sinh0059 sinh -1.0 0.0 -> -1.1752011936438014569 0.0
+sinh0060 sinh 1.4436354751788103 0.0 -> 1.9999999999999999078 0.0
+sinh0061 sinh -1.4436354751788103 0.0 -> -1.9999999999999999078 0.0
+sinh0062 sinh 17.328679513998633 0.0 -> 16777215.999999992136 0.0
+sinh0063 sinh 18.714973875118524 0.0 -> 67108864.000000036211 0.0
+sinh0064 sinh 709.7827 0.0 -> 8.9883497833190073272e+307 0.0
+sinh0065 sinh -709.7827 0.0 -> -8.9883497833190073272e+307 0.0
+
-- special values
sinh1000 sinh 0.0 0.0 -> 0.0 0.0
sinh1001 sinh 0.0 inf -> 0.0 nan invalid ignore-real-sign
@@ -1897,6 +1969,24 @@ tanh0031 tanh -711 7.4000000000000004 -> -1.0 0.0
tanh0032 tanh 1000 -2.3199999999999998 -> 1.0 0.0
tanh0033 tanh -1.0000000000000001e+300 -9.6699999999999999 -> -1.0 -0.0
+-- Additional real values (mpmath)
+tanh0050 tanh 1e-100 0.0 -> 1.00000000000000002e-100 0.0
+tanh0051 tanh 5e-17 0.0 -> 4.9999999999999998955e-17 0.0
+tanh0052 tanh 1e-16 0.0 -> 9.999999999999999791e-17 0.0
+tanh0053 tanh 3.7e-08 0.0 -> 3.6999999999999983559e-8 0.0
+tanh0054 tanh 0.001 0.0 -> 0.00099999966666680002076 0.0
+tanh0055 tanh 0.2 0.0 -> 0.19737532022490401141 0.0
+tanh0056 tanh 1.0 0.0 -> 0.76159415595576488812 0.0
+tanh0057 tanh -3.7e-08 0.0 -> -3.6999999999999983559e-8 0.0
+tanh0058 tanh -0.001 0.0 -> -0.00099999966666680002076 0.0
+tanh0059 tanh -1.0 0.0 -> -0.76159415595576488812 0.0
+tanh0060 tanh 0.5493061443340549 0.0 -> 0.50000000000000003402 0.0
+tanh0061 tanh -0.5493061443340549 0.0 -> -0.50000000000000003402 0.0
+tanh0062 tanh 17.328679513998633 0.0 -> 0.99999999999999822364 0.0
+tanh0063 tanh 18.714973875118524 0.0 -> 0.99999999999999988898 0.0
+tanh0064 tanh 711 0.0 -> 1.0 0.0
+tanh0065 tanh 1.797e+308 0.0 -> 1.0 0.0
+
--special values
tanh1000 tanh 0.0 0.0 -> 0.0 0.0
tanh1001 tanh 0.0 inf -> nan nan invalid
@@ -1985,6 +2075,22 @@ cos0021 cos 4.8048375263775256 0.0062248852898515658 -> 0.092318702015846243 0.0
cos0022 cos 7.9914515433858515 0.71659966615501436 -> -0.17375439906936566 -0.77217043527294582
cos0023 cos 0.45124351152540226 1.6992693993812158 -> 2.543477948972237 -1.1528193694875477
+-- Additional real values (mpmath)
+cos0050 cos 1e-150 0.0 -> 1.0 -0.0
+cos0051 cos 1e-18 0.0 -> 1.0 -0.0
+cos0052 cos 1e-09 0.0 -> 0.9999999999999999995 -0.0
+cos0053 cos 0.0003 0.0 -> 0.9999999550000003375 -0.0
+cos0054 cos 0.2 0.0 -> 0.98006657784124162892 -0.0
+cos0055 cos 1.0 0.0 -> 0.5403023058681397174 -0.0
+cos0056 cos -1e-18 0.0 -> 1.0 0.0
+cos0057 cos -0.0003 0.0 -> 0.9999999550000003375 0.0
+cos0058 cos -1.0 0.0 -> 0.5403023058681397174 0.0
+cos0059 cos 1.0471975511965976 0.0 -> 0.50000000000000009945 -0.0
+cos0060 cos 2.5707963267948966 0.0 -> -0.84147098480789647357 -0.0
+cos0061 cos -2.5707963267948966 0.0 -> -0.84147098480789647357 0.0
+cos0062 cos 7.225663103256523 0.0 -> 0.58778525229247407559 -0.0
+cos0063 cos -8.79645943005142 0.0 -> -0.80901699437494722255 0.0
+
-- special values
cos1000 cos -0.0 0.0 -> 1.0 0.0
cos1001 cos -inf 0.0 -> nan 0.0 invalid ignore-imag-sign
@@ -2073,6 +2179,22 @@ sin0021 sin 1.4342727387492671 0.81361889790284347 -> 1.3370960060947923 0.12336
sin0022 sin 1.1518087354403725 4.8597235966150558 -> 58.919141989603041 26.237003403758852
sin0023 sin 0.00087773078406649192 34.792379211312095 -> 565548145569.38245 644329685822700.62
+-- Additional real values (mpmath)
+sin0050 sin 1e-100 0.0 -> 1.00000000000000002e-100 0.0
+sin0051 sin 3.7e-08 0.0 -> 3.6999999999999992001e-8 0.0
+sin0052 sin 0.001 0.0 -> 0.00099999983333334168748 0.0
+sin0053 sin 0.2 0.0 -> 0.19866933079506122634 0.0
+sin0054 sin 1.0 0.0 -> 0.84147098480789650665 0.0
+sin0055 sin -3.7e-08 0.0 -> -3.6999999999999992001e-8 0.0
+sin0056 sin -0.001 0.0 -> -0.00099999983333334168748 0.0
+sin0057 sin -1.0 0.0 -> -0.84147098480789650665 0.0
+sin0058 sin 0.5235987755982989 0.0 -> 0.50000000000000004642 0.0
+sin0059 sin -0.5235987755982989 0.0 -> -0.50000000000000004642 0.0
+sin0060 sin 2.6179938779914944 0.0 -> 0.49999999999999996018 -0.0
+sin0061 sin -2.6179938779914944 0.0 -> -0.49999999999999996018 -0.0
+sin0062 sin 7.225663103256523 0.0 -> 0.80901699437494673648 0.0
+sin0063 sin -8.79645943005142 0.0 -> -0.58778525229247340658 -0.0
+
-- special values
sin1000 sin -0.0 0.0 -> -0.0 0.0
sin1001 sin -inf 0.0 -> nan 0.0 invalid ignore-imag-sign
@@ -2161,6 +2283,25 @@ tan0021 tan 1.7809617968443272 1.5052381702853379 -> -0.044066222118946903 1.093
tan0022 tan 1.1615313900880577 1.7956298728647107 -> 0.041793186826390362 1.0375339546034792
tan0023 tan 0.067014779477908945 5.8517361577457097 -> 2.2088639754800034e-06 0.9999836182420061
+-- Additional real values (mpmath)
+tan0050 tan 1e-100 0.0 -> 1.00000000000000002e-100 0.0
+tan0051 tan 3.7e-08 0.0 -> 3.7000000000000017328e-8 0.0
+tan0052 tan 0.001 0.0 -> 0.0010000003333334666875 0.0
+tan0053 tan 0.2 0.0 -> 0.20271003550867249488 0.0
+tan0054 tan 1.0 0.0 -> 1.5574077246549022305 0.0
+tan0055 tan -3.7e-08 0.0 -> -3.7000000000000017328e-8 0.0
+tan0056 tan -0.001 0.0 -> -0.0010000003333334666875 0.0
+tan0057 tan -1.0 0.0 -> -1.5574077246549022305 0.0
+tan0058 tan 0.4636476090008061 0.0 -> 0.49999999999999997163 0.0
+tan0059 tan -0.4636476090008061 0.0 -> -0.49999999999999997163 0.0
+tan0060 tan 1.1071487177940904 0.0 -> 1.9999999999999995298 0.0
+tan0061 tan -1.1071487177940904 0.0 -> -1.9999999999999995298 0.0
+tan0062 tan 1.5 0.0 -> 14.101419947171719388 0.0
+tan0063 tan 1.57 0.0 -> 1255.7655915007896475 0.0
+tan0064 tan 1.5707963267948961 0.0 -> 1978937966095219.0538 0.0
+tan0065 tan 7.225663103256523 0.0 -> 1.3763819204711701522 0.0
+tan0066 tan -8.79645943005142 0.0 -> 0.7265425280053614098 0.0
+
-- special values
tan1000 tan -0.0 0.0 -> -0.0 0.0
tan1001 tan -inf 0.0 -> nan nan invalid
diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py
index 0860db8..d65186d 100644
--- a/Lib/test/datetimetester.py
+++ b/Lib/test/datetimetester.py
@@ -2,14 +2,22 @@
See http://www.zope.org/Members/fdrake/DateTimeWiki/TestCases
"""
+from test.support import is_resource_enabled
+
+import itertools
+import bisect
import copy
import decimal
import sys
+import os
import pickle
import random
+import struct
import unittest
+from array import array
+
from operator import lt, le, gt, ge, eq, ne, truediv, floordiv, mod
from test import support
@@ -284,7 +292,8 @@ class TestTimeZone(unittest.TestCase):
with self.assertRaises(TypeError): self.EST.dst(5)
def test_tzname(self):
- self.assertEqual('UTC+00:00', timezone(ZERO).tzname(None))
+ self.assertEqual('UTC', timezone.utc.tzname(None))
+ self.assertEqual('UTC', timezone(ZERO).tzname(None))
self.assertEqual('UTC-05:00', timezone(-5 * HOUR).tzname(None))
self.assertEqual('UTC+09:30', timezone(9.5 * HOUR).tzname(None))
self.assertEqual('UTC-00:01', timezone(timedelta(minutes=-1)).tzname(None))
@@ -1320,6 +1329,7 @@ class TestDate(HarmlessMixedComparison, unittest.TestCase):
green = pickler.dumps(orig, proto)
derived = unpickler.loads(green)
self.assertEqual(orig, derived)
+ self.assertEqual(orig.__reduce__(), orig.__reduce_ex__(2))
def test_compare(self):
t1 = self.theclass(2, 3, 4)
@@ -1558,13 +1568,32 @@ class TestDateTime(TestDate):
self.assertEqual(dt, dt2)
def test_isoformat(self):
- t = self.theclass(2, 3, 2, 4, 5, 1, 123)
- self.assertEqual(t.isoformat(), "0002-03-02T04:05:01.000123")
- self.assertEqual(t.isoformat('T'), "0002-03-02T04:05:01.000123")
- self.assertEqual(t.isoformat(' '), "0002-03-02 04:05:01.000123")
- self.assertEqual(t.isoformat('\x00'), "0002-03-02\x0004:05:01.000123")
+ t = self.theclass(1, 2, 3, 4, 5, 1, 123)
+ self.assertEqual(t.isoformat(), "0001-02-03T04:05:01.000123")
+ self.assertEqual(t.isoformat('T'), "0001-02-03T04:05:01.000123")
+ self.assertEqual(t.isoformat(' '), "0001-02-03 04:05:01.000123")
+ self.assertEqual(t.isoformat('\x00'), "0001-02-03\x0004:05:01.000123")
+ self.assertEqual(t.isoformat(timespec='hours'), "0001-02-03T04")
+ self.assertEqual(t.isoformat(timespec='minutes'), "0001-02-03T04:05")
+ self.assertEqual(t.isoformat(timespec='seconds'), "0001-02-03T04:05:01")
+ self.assertEqual(t.isoformat(timespec='milliseconds'), "0001-02-03T04:05:01.000")
+ self.assertEqual(t.isoformat(timespec='microseconds'), "0001-02-03T04:05:01.000123")
+ self.assertEqual(t.isoformat(timespec='auto'), "0001-02-03T04:05:01.000123")
+ self.assertEqual(t.isoformat(sep=' ', timespec='minutes'), "0001-02-03 04:05")
+ self.assertRaises(ValueError, t.isoformat, timespec='foo')
# str is ISO format with the separator forced to a blank.
- self.assertEqual(str(t), "0002-03-02 04:05:01.000123")
+ self.assertEqual(str(t), "0001-02-03 04:05:01.000123")
+
+ t = self.theclass(1, 2, 3, 4, 5, 1, 999500, tzinfo=timezone.utc)
+ self.assertEqual(t.isoformat(timespec='milliseconds'), "0001-02-03T04:05:01.999+00:00")
+
+ t = self.theclass(1, 2, 3, 4, 5, 1, 999500)
+ self.assertEqual(t.isoformat(timespec='milliseconds'), "0001-02-03T04:05:01.999")
+
+ t = self.theclass(1, 2, 3, 4, 5, 1)
+ self.assertEqual(t.isoformat(timespec='auto'), "0001-02-03T04:05:01")
+ self.assertEqual(t.isoformat(timespec='milliseconds'), "0001-02-03T04:05:01.000")
+ self.assertEqual(t.isoformat(timespec='microseconds'), "0001-02-03T04:05:01.000000")
t = self.theclass(2, 3, 2)
self.assertEqual(t.isoformat(), "0002-03-02T00:00:00")
@@ -1572,6 +1601,10 @@ class TestDateTime(TestDate):
self.assertEqual(t.isoformat(' '), "0002-03-02 00:00:00")
# str is ISO format with the separator forced to a blank.
self.assertEqual(str(t), "0002-03-02 00:00:00")
+ # ISO format with timezone
+ tz = FixedOffset(timedelta(seconds=16), 'XXX')
+ t = self.theclass(2, 3, 2, tzinfo=tz)
+ self.assertEqual(t.isoformat(), "0002-03-02T00:00:00+00:00:16")
def test_format(self):
dt = self.theclass(2007, 9, 10, 4, 5, 1, 123)
@@ -1691,6 +1724,14 @@ class TestDateTime(TestDate):
self.assertRaises(ValueError, self.theclass,
2000, 1, 31, 23, 59, 59,
1000000)
+ # bad fold
+ self.assertRaises(ValueError, self.theclass,
+ 2000, 1, 31, fold=-1)
+ self.assertRaises(ValueError, self.theclass,
+ 2000, 1, 31, fold=2)
+ # Positional fold:
+ self.assertRaises(TypeError, self.theclass,
+ 2000, 1, 31, 23, 59, 59, 0, None, 1)
def test_hash_equality(self):
d = self.theclass(2000, 12, 31, 23, 30, 17)
@@ -1790,6 +1831,7 @@ class TestDateTime(TestDate):
green = pickler.dumps(orig, proto)
derived = unpickler.loads(green)
self.assertEqual(orig, derived)
+ self.assertEqual(orig.__reduce__(), orig.__reduce_ex__(2))
def test_more_pickling(self):
a = self.theclass(2003, 2, 7, 16, 48, 37, 444116)
@@ -1874,16 +1916,20 @@ class TestDateTime(TestDate):
t = self.theclass(1970, 1, 1, 1, 2, 3, 4)
self.assertEqual(t.timestamp(),
18000.0 + 3600 + 2*60 + 3 + 4*1e-6)
- # Missing hour may produce platform-dependent result
- t = self.theclass(2012, 3, 11, 2, 30)
- self.assertIn(self.theclass.fromtimestamp(t.timestamp()),
- [t - timedelta(hours=1), t + timedelta(hours=1)])
+ # Missing hour
+ t0 = self.theclass(2012, 3, 11, 2, 30)
+ t1 = t0.replace(fold=1)
+ self.assertEqual(self.theclass.fromtimestamp(t1.timestamp()),
+ t0 - timedelta(hours=1))
+ self.assertEqual(self.theclass.fromtimestamp(t0.timestamp()),
+ t1 + timedelta(hours=1))
# Ambiguous hour defaults to DST
t = self.theclass(2012, 11, 4, 1, 30)
self.assertEqual(self.theclass.fromtimestamp(t.timestamp()), t)
# Timestamp may raise an overflow error on some platforms
- for t in [self.theclass(1,1,1), self.theclass(9999,12,12)]:
+ # XXX: Do we care to support the first and last year?
+ for t in [self.theclass(2,1,1), self.theclass(9998,12,12)]:
try:
s = t.timestamp()
except OverflowError:
@@ -1902,6 +1948,7 @@ class TestDateTime(TestDate):
self.assertEqual(t.timestamp(),
18000 + 3600 + 2*60 + 3 + 4*1e-6)
+ @support.run_with_tz('MSK-03') # Something east of Greenwich
def test_microsecond_rounding(self):
for fts in [self.theclass.fromtimestamp,
self.theclass.utcfromtimestamp]:
@@ -2076,11 +2123,22 @@ class TestDateTime(TestDate):
self.assertRaises(TypeError, combine) # need an arg
self.assertRaises(TypeError, combine, d) # need two args
self.assertRaises(TypeError, combine, t, d) # args reversed
- self.assertRaises(TypeError, combine, d, t, 1) # too many args
+ self.assertRaises(TypeError, combine, d, t, 1) # wrong tzinfo type
+ self.assertRaises(TypeError, combine, d, t, 1, 2) # too many args
self.assertRaises(TypeError, combine, "date", "time") # wrong types
self.assertRaises(TypeError, combine, d, "time") # wrong type
self.assertRaises(TypeError, combine, "date", t) # wrong type
+ # tzinfo= argument
+ dt = combine(d, t, timezone.utc)
+ self.assertIs(dt.tzinfo, timezone.utc)
+ dt = combine(d, t, tzinfo=timezone.utc)
+ self.assertIs(dt.tzinfo, timezone.utc)
+ t = time()
+ dt = combine(dt, t)
+ self.assertEqual(dt.date(), d)
+ self.assertEqual(dt.time(), t)
+
def test_replace(self):
cls = self.theclass
args = [1, 2, 3, 4, 5, 6, 7]
@@ -2107,6 +2165,7 @@ class TestDateTime(TestDate):
self.assertRaises(ValueError, base.replace, year=2001)
def test_astimezone(self):
+ return # The rest is no longer applicable
# Pretty boring! The TZ test is more interesting here. astimezone()
# simply can't be applied to a naive object.
dt = self.theclass.now()
@@ -2324,6 +2383,23 @@ class TestTime(HarmlessMixedComparison, unittest.TestCase):
self.assertEqual(t.isoformat(), "00:00:00.100000")
self.assertEqual(t.isoformat(), str(t))
+ t = self.theclass(hour=12, minute=34, second=56, microsecond=123456)
+ self.assertEqual(t.isoformat(timespec='hours'), "12")
+ self.assertEqual(t.isoformat(timespec='minutes'), "12:34")
+ self.assertEqual(t.isoformat(timespec='seconds'), "12:34:56")
+ self.assertEqual(t.isoformat(timespec='milliseconds'), "12:34:56.123")
+ self.assertEqual(t.isoformat(timespec='microseconds'), "12:34:56.123456")
+ self.assertEqual(t.isoformat(timespec='auto'), "12:34:56.123456")
+ self.assertRaises(ValueError, t.isoformat, timespec='monkey')
+
+ t = self.theclass(hour=12, minute=34, second=56, microsecond=999500)
+ self.assertEqual(t.isoformat(timespec='milliseconds'), "12:34:56.999")
+
+ t = self.theclass(hour=12, minute=34, second=56, microsecond=0)
+ self.assertEqual(t.isoformat(timespec='milliseconds'), "12:34:56.000")
+ self.assertEqual(t.isoformat(timespec='microseconds'), "12:34:56.000000")
+ self.assertEqual(t.isoformat(timespec='auto'), "12:34:56")
+
def test_1653736(self):
# verify it doesn't accept extra keyword arguments
t = self.theclass(second=1)
@@ -2395,6 +2471,7 @@ class TestTime(HarmlessMixedComparison, unittest.TestCase):
green = pickler.dumps(orig, proto)
derived = unpickler.loads(green)
self.assertEqual(orig, derived)
+ self.assertEqual(orig.__reduce__(), orig.__reduce_ex__(2))
def test_pickling_subclass_time(self):
args = 20, 59, 16, 64**2
@@ -2582,9 +2659,9 @@ class TZInfoBase:
self.assertRaises(ValueError, t.utcoffset)
self.assertRaises(ValueError, t.dst)
- # Not a whole number of minutes.
+ # Not a whole number of seconds.
class C7(tzinfo):
- def utcoffset(self, dt): return timedelta(seconds=61)
+ def utcoffset(self, dt): return timedelta(microseconds=61)
def dst(self, dt): return timedelta(microseconds=-81)
t = cls(1, 1, 1, tzinfo=C7())
self.assertRaises(ValueError, t.utcoffset)
@@ -2755,6 +2832,7 @@ class TestTimeTZ(TestTime, TZInfoBase, unittest.TestCase):
green = pickler.dumps(orig, proto)
derived = unpickler.loads(green)
self.assertEqual(orig, derived)
+ self.assertEqual(orig.__reduce__(), orig.__reduce_ex__(2))
# Try one with a tzinfo.
tinfo = PicklableFixedOffset(-300, 'cookie')
@@ -2766,6 +2844,7 @@ class TestTimeTZ(TestTime, TZInfoBase, unittest.TestCase):
self.assertIsInstance(derived.tzinfo, PicklableFixedOffset)
self.assertEqual(derived.utcoffset(), timedelta(minutes=-300))
self.assertEqual(derived.tzname(), 'cookie')
+ self.assertEqual(orig.__reduce__(), orig.__reduce_ex__(2))
def test_more_bool(self):
# time is always True.
@@ -2969,6 +3048,7 @@ class TestDateTimeTZ(TestDateTime, TZInfoBase, unittest.TestCase):
green = pickler.dumps(orig, proto)
derived = unpickler.loads(green)
self.assertEqual(orig, derived)
+ self.assertEqual(orig.__reduce__(), orig.__reduce_ex__(2))
# Try one with a tzinfo.
tinfo = PicklableFixedOffset(-300, 'cookie')
@@ -2981,6 +3061,7 @@ class TestDateTimeTZ(TestDateTime, TZInfoBase, unittest.TestCase):
self.assertIsInstance(derived.tzinfo, PicklableFixedOffset)
self.assertEqual(derived.utcoffset(), timedelta(minutes=-300))
self.assertEqual(derived.tzname(), 'cookie')
+ self.assertEqual(orig.__reduce__(), orig.__reduce_ex__(2))
def test_extreme_hashes(self):
# If an attempt is made to hash these via subtracting the offset
@@ -3927,12 +4008,12 @@ class Oddballs(unittest.TestCase):
datetime(xx, xx, xx, xx, xx, xx, xx))
with self.assertRaisesRegex(TypeError, '^an integer is required '
- '\(got type str\)$'):
+ r'\(got type str\)$'):
datetime(10, 10, '10')
f10 = Number(10.9)
with self.assertRaisesRegex(TypeError, '^__int__ returned non-int '
- '\(type float\)$'):
+ r'\(type float\)$'):
datetime(10, 10, f10)
class Float(float):
@@ -3957,5 +4038,790 @@ class Oddballs(unittest.TestCase):
with self.assertRaises(TypeError):
datetime(10, 10, 10, 10, 10, 10, 10.)
+#############################################################################
+# Local Time Disambiguation
+
+# An experimental reimplementation of fromutc that respects the "fold" flag.
+
+class tzinfo2(tzinfo):
+
+ def fromutc(self, dt):
+ "datetime in UTC -> datetime in local time."
+
+ if not isinstance(dt, datetime):
+ raise TypeError("fromutc() requires a datetime argument")
+ if dt.tzinfo is not self:
+ raise ValueError("dt.tzinfo is not self")
+ # Returned value satisfies
+ # dt + ldt.utcoffset() = ldt
+ off0 = dt.replace(fold=0).utcoffset()
+ off1 = dt.replace(fold=1).utcoffset()
+ if off0 is None or off1 is None or dt.dst() is None:
+ raise ValueError
+ if off0 == off1:
+ ldt = dt + off0
+ off1 = ldt.utcoffset()
+ if off0 == off1:
+ return ldt
+ # Now, we discovered both possible offsets, so
+ # we can just try four possible solutions:
+ for off in [off0, off1]:
+ ldt = dt + off
+ if ldt.utcoffset() == off:
+ return ldt
+ ldt = ldt.replace(fold=1)
+ if ldt.utcoffset() == off:
+ return ldt
+
+ raise ValueError("No suitable local time found")
+
+# Reimplementing simplified US timezones to respect the "fold" flag:
+
+class USTimeZone2(tzinfo2):
+
+ def __init__(self, hours, reprname, stdname, dstname):
+ self.stdoffset = timedelta(hours=hours)
+ self.reprname = reprname
+ self.stdname = stdname
+ self.dstname = dstname
+
+ def __repr__(self):
+ return self.reprname
+
+ def tzname(self, dt):
+ if self.dst(dt):
+ return self.dstname
+ else:
+ return self.stdname
+
+ def utcoffset(self, dt):
+ return self.stdoffset + self.dst(dt)
+
+ def dst(self, dt):
+ if dt is None or dt.tzinfo is None:
+ # An exception instead may be sensible here, in one or more of
+ # the cases.
+ return ZERO
+ assert dt.tzinfo is self
+
+ # Find first Sunday in April.
+ start = first_sunday_on_or_after(DSTSTART.replace(year=dt.year))
+ assert start.weekday() == 6 and start.month == 4 and start.day <= 7
+
+ # Find last Sunday in October.
+ end = first_sunday_on_or_after(DSTEND.replace(year=dt.year))
+ assert end.weekday() == 6 and end.month == 10 and end.day >= 25
+
+ # Can't compare naive to aware objects, so strip the timezone from
+ # dt first.
+ dt = dt.replace(tzinfo=None)
+ if start + HOUR <= dt < end:
+ # DST is in effect.
+ return HOUR
+ elif end <= dt < end + HOUR:
+ # Fold (an ambiguous hour): use dt.fold to disambiguate.
+ return ZERO if dt.fold else HOUR
+ elif start <= dt < start + HOUR:
+ # Gap (a non-existent hour): reverse the fold rule.
+ return HOUR if dt.fold else ZERO
+ else:
+ # DST is off.
+ return ZERO
+
+Eastern2 = USTimeZone2(-5, "Eastern2", "EST", "EDT")
+Central2 = USTimeZone2(-6, "Central2", "CST", "CDT")
+Mountain2 = USTimeZone2(-7, "Mountain2", "MST", "MDT")
+Pacific2 = USTimeZone2(-8, "Pacific2", "PST", "PDT")
+
+# Europe_Vilnius_1941 tzinfo implementation reproduces the following
+# 1941 transition from Olson's tzdist:
+#
+# Zone NAME GMTOFF RULES FORMAT [UNTIL]
+# ZoneEurope/Vilnius 1:00 - CET 1940 Aug 3
+# 3:00 - MSK 1941 Jun 24
+# 1:00 C-Eur CE%sT 1944 Aug
+#
+# $ zdump -v Europe/Vilnius | grep 1941
+# Europe/Vilnius Mon Jun 23 20:59:59 1941 UTC = Mon Jun 23 23:59:59 1941 MSK isdst=0 gmtoff=10800
+# Europe/Vilnius Mon Jun 23 21:00:00 1941 UTC = Mon Jun 23 23:00:00 1941 CEST isdst=1 gmtoff=7200
+
+class Europe_Vilnius_1941(tzinfo):
+ def _utc_fold(self):
+ return [datetime(1941, 6, 23, 21, tzinfo=self), # Mon Jun 23 21:00:00 1941 UTC
+ datetime(1941, 6, 23, 22, tzinfo=self)] # Mon Jun 23 22:00:00 1941 UTC
+
+ def _loc_fold(self):
+ return [datetime(1941, 6, 23, 23, tzinfo=self), # Mon Jun 23 23:00:00 1941 MSK / CEST
+ datetime(1941, 6, 24, 0, tzinfo=self)] # Mon Jun 24 00:00:00 1941 CEST
+
+ def utcoffset(self, dt):
+ fold_start, fold_stop = self._loc_fold()
+ if dt < fold_start:
+ return 3 * HOUR
+ if dt < fold_stop:
+ return (2 if dt.fold else 3) * HOUR
+ # if dt >= fold_stop
+ return 2 * HOUR
+
+ def dst(self, dt):
+ fold_start, fold_stop = self._loc_fold()
+ if dt < fold_start:
+ return 0 * HOUR
+ if dt < fold_stop:
+ return (1 if dt.fold else 0) * HOUR
+ # if dt >= fold_stop
+ return 1 * HOUR
+
+ def tzname(self, dt):
+ fold_start, fold_stop = self._loc_fold()
+ if dt < fold_start:
+ return 'MSK'
+ if dt < fold_stop:
+ return ('MSK', 'CEST')[dt.fold]
+ # if dt >= fold_stop
+ return 'CEST'
+
+ def fromutc(self, dt):
+ assert dt.fold == 0
+ assert dt.tzinfo is self
+ if dt.year != 1941:
+ raise NotImplementedError
+ fold_start, fold_stop = self._utc_fold()
+ if dt < fold_start:
+ return dt + 3 * HOUR
+ if dt < fold_stop:
+ return (dt + 2 * HOUR).replace(fold=1)
+ # if dt >= fold_stop
+ return dt + 2 * HOUR
+
+
+class TestLocalTimeDisambiguation(unittest.TestCase):
+
+ def test_vilnius_1941_fromutc(self):
+ Vilnius = Europe_Vilnius_1941()
+
+ gdt = datetime(1941, 6, 23, 20, 59, 59, tzinfo=timezone.utc)
+ ldt = gdt.astimezone(Vilnius)
+ self.assertEqual(ldt.strftime("%c %Z%z"),
+ 'Mon Jun 23 23:59:59 1941 MSK+0300')
+ self.assertEqual(ldt.fold, 0)
+ self.assertFalse(ldt.dst())
+
+ gdt = datetime(1941, 6, 23, 21, tzinfo=timezone.utc)
+ ldt = gdt.astimezone(Vilnius)
+ self.assertEqual(ldt.strftime("%c %Z%z"),
+ 'Mon Jun 23 23:00:00 1941 CEST+0200')
+ self.assertEqual(ldt.fold, 1)
+ self.assertTrue(ldt.dst())
+
+ gdt = datetime(1941, 6, 23, 22, tzinfo=timezone.utc)
+ ldt = gdt.astimezone(Vilnius)
+ self.assertEqual(ldt.strftime("%c %Z%z"),
+ 'Tue Jun 24 00:00:00 1941 CEST+0200')
+ self.assertEqual(ldt.fold, 0)
+ self.assertTrue(ldt.dst())
+
+ def test_vilnius_1941_toutc(self):
+ Vilnius = Europe_Vilnius_1941()
+
+ ldt = datetime(1941, 6, 23, 22, 59, 59, tzinfo=Vilnius)
+ gdt = ldt.astimezone(timezone.utc)
+ self.assertEqual(gdt.strftime("%c %Z"),
+ 'Mon Jun 23 19:59:59 1941 UTC')
+
+ ldt = datetime(1941, 6, 23, 23, 59, 59, tzinfo=Vilnius)
+ gdt = ldt.astimezone(timezone.utc)
+ self.assertEqual(gdt.strftime("%c %Z"),
+ 'Mon Jun 23 20:59:59 1941 UTC')
+
+ ldt = datetime(1941, 6, 23, 23, 59, 59, tzinfo=Vilnius, fold=1)
+ gdt = ldt.astimezone(timezone.utc)
+ self.assertEqual(gdt.strftime("%c %Z"),
+ 'Mon Jun 23 21:59:59 1941 UTC')
+
+ ldt = datetime(1941, 6, 24, 0, tzinfo=Vilnius)
+ gdt = ldt.astimezone(timezone.utc)
+ self.assertEqual(gdt.strftime("%c %Z"),
+ 'Mon Jun 23 22:00:00 1941 UTC')
+
+
+ def test_constructors(self):
+ t = time(0, fold=1)
+ dt = datetime(1, 1, 1, fold=1)
+ self.assertEqual(t.fold, 1)
+ self.assertEqual(dt.fold, 1)
+ with self.assertRaises(TypeError):
+ time(0, 0, 0, 0, None, 0)
+
+ def test_member(self):
+ dt = datetime(1, 1, 1, fold=1)
+ t = dt.time()
+ self.assertEqual(t.fold, 1)
+ t = dt.timetz()
+ self.assertEqual(t.fold, 1)
+
+ def test_replace(self):
+ t = time(0)
+ dt = datetime(1, 1, 1)
+ self.assertEqual(t.replace(fold=1).fold, 1)
+ self.assertEqual(dt.replace(fold=1).fold, 1)
+ self.assertEqual(t.replace(fold=0).fold, 0)
+ self.assertEqual(dt.replace(fold=0).fold, 0)
+ # Check that replacement of other fields does not change "fold".
+ t = t.replace(fold=1, tzinfo=Eastern)
+ dt = dt.replace(fold=1, tzinfo=Eastern)
+ self.assertEqual(t.replace(tzinfo=None).fold, 1)
+ self.assertEqual(dt.replace(tzinfo=None).fold, 1)
+ # Check that fold is a keyword-only argument
+ with self.assertRaises(TypeError):
+ t.replace(1, 1, 1, None, 1)
+ with self.assertRaises(TypeError):
+ dt.replace(1, 1, 1, 1, 1, 1, 1, None, 1)
+
+ def test_comparison(self):
+ t = time(0)
+ dt = datetime(1, 1, 1)
+ self.assertEqual(t, t.replace(fold=1))
+ self.assertEqual(dt, dt.replace(fold=1))
+
+ def test_hash(self):
+ t = time(0)
+ dt = datetime(1, 1, 1)
+ self.assertEqual(hash(t), hash(t.replace(fold=1)))
+ self.assertEqual(hash(dt), hash(dt.replace(fold=1)))
+
+ @support.run_with_tz('EST+05EDT,M3.2.0,M11.1.0')
+ def test_fromtimestamp(self):
+ s = 1414906200
+ dt0 = datetime.fromtimestamp(s)
+ dt1 = datetime.fromtimestamp(s + 3600)
+ self.assertEqual(dt0.fold, 0)
+ self.assertEqual(dt1.fold, 1)
+
+ @support.run_with_tz('Australia/Lord_Howe')
+ def test_fromtimestamp_lord_howe(self):
+ tm = _time.localtime(1.4e9)
+ if _time.strftime('%Z%z', tm) != 'LHST+1030':
+ self.skipTest('Australia/Lord_Howe timezone is not supported on this platform')
+ # $ TZ=Australia/Lord_Howe date -r 1428158700
+ # Sun Apr 5 01:45:00 LHDT 2015
+ # $ TZ=Australia/Lord_Howe date -r 1428160500
+ # Sun Apr 5 01:45:00 LHST 2015
+ s = 1428158700
+ t0 = datetime.fromtimestamp(s)
+ t1 = datetime.fromtimestamp(s + 1800)
+ self.assertEqual(t0, t1)
+ self.assertEqual(t0.fold, 0)
+ self.assertEqual(t1.fold, 1)
+
+
+ @support.run_with_tz('EST+05EDT,M3.2.0,M11.1.0')
+ def test_timestamp(self):
+ dt0 = datetime(2014, 11, 2, 1, 30)
+ dt1 = dt0.replace(fold=1)
+ self.assertEqual(dt0.timestamp() + 3600,
+ dt1.timestamp())
+
+ @support.run_with_tz('Australia/Lord_Howe')
+ def test_timestamp_lord_howe(self):
+ tm = _time.localtime(1.4e9)
+ if _time.strftime('%Z%z', tm) != 'LHST+1030':
+ self.skipTest('Australia/Lord_Howe timezone is not supported on this platform')
+ t = datetime(2015, 4, 5, 1, 45)
+ s0 = t.replace(fold=0).timestamp()
+ s1 = t.replace(fold=1).timestamp()
+ self.assertEqual(s0 + 1800, s1)
+
+
+ @support.run_with_tz('EST+05EDT,M3.2.0,M11.1.0')
+ def test_astimezone(self):
+ dt0 = datetime(2014, 11, 2, 1, 30)
+ dt1 = dt0.replace(fold=1)
+ # Convert both naive instances to aware.
+ adt0 = dt0.astimezone()
+ adt1 = dt1.astimezone()
+ # Check that the first instance in DST zone and the second in STD
+ self.assertEqual(adt0.tzname(), 'EDT')
+ self.assertEqual(adt1.tzname(), 'EST')
+ self.assertEqual(adt0 + HOUR, adt1)
+ # Aware instances with fixed offset tzinfo's always have fold=0
+ self.assertEqual(adt0.fold, 0)
+ self.assertEqual(adt1.fold, 0)
+
+
+ def test_pickle_fold(self):
+ t = time(fold=1)
+ dt = datetime(1, 1, 1, fold=1)
+ for pickler, unpickler, proto in pickle_choices:
+ for x in [t, dt]:
+ s = pickler.dumps(x, proto)
+ y = unpickler.loads(s)
+ self.assertEqual(x, y)
+ self.assertEqual((0 if proto < 4 else x.fold), y.fold)
+
+ def test_repr(self):
+ t = time(fold=1)
+ dt = datetime(1, 1, 1, fold=1)
+ self.assertEqual(repr(t), 'datetime.time(0, 0, fold=1)')
+ self.assertEqual(repr(dt),
+ 'datetime.datetime(1, 1, 1, 0, 0, fold=1)')
+
+ def test_dst(self):
+ # Let's first establish that things work in regular times.
+ dt_summer = datetime(2002, 10, 27, 1, tzinfo=Eastern2) - timedelta.resolution
+ dt_winter = datetime(2002, 10, 27, 2, tzinfo=Eastern2)
+ self.assertEqual(dt_summer.dst(), HOUR)
+ self.assertEqual(dt_winter.dst(), ZERO)
+ # The disambiguation flag is ignored
+ self.assertEqual(dt_summer.replace(fold=1).dst(), HOUR)
+ self.assertEqual(dt_winter.replace(fold=1).dst(), ZERO)
+
+ # Pick local time in the fold.
+ for minute in [0, 30, 59]:
+ dt = datetime(2002, 10, 27, 1, minute, tzinfo=Eastern2)
+ # With fold=0 (the default) it is in DST.
+ self.assertEqual(dt.dst(), HOUR)
+ # With fold=1 it is in STD.
+ self.assertEqual(dt.replace(fold=1).dst(), ZERO)
+
+ # Pick local time in the gap.
+ for minute in [0, 30, 59]:
+ dt = datetime(2002, 4, 7, 2, minute, tzinfo=Eastern2)
+ # With fold=0 (the default) it is in STD.
+ self.assertEqual(dt.dst(), ZERO)
+ # With fold=1 it is in DST.
+ self.assertEqual(dt.replace(fold=1).dst(), HOUR)
+
+
+ def test_utcoffset(self):
+ # Let's first establish that things work in regular times.
+ dt_summer = datetime(2002, 10, 27, 1, tzinfo=Eastern2) - timedelta.resolution
+ dt_winter = datetime(2002, 10, 27, 2, tzinfo=Eastern2)
+ self.assertEqual(dt_summer.utcoffset(), -4 * HOUR)
+ self.assertEqual(dt_winter.utcoffset(), -5 * HOUR)
+ # The disambiguation flag is ignored
+ self.assertEqual(dt_summer.replace(fold=1).utcoffset(), -4 * HOUR)
+ self.assertEqual(dt_winter.replace(fold=1).utcoffset(), -5 * HOUR)
+
+ def test_fromutc(self):
+ # Let's first establish that things work in regular times.
+ u_summer = datetime(2002, 10, 27, 6, tzinfo=Eastern2) - timedelta.resolution
+ u_winter = datetime(2002, 10, 27, 7, tzinfo=Eastern2)
+ t_summer = Eastern2.fromutc(u_summer)
+ t_winter = Eastern2.fromutc(u_winter)
+ self.assertEqual(t_summer, u_summer - 4 * HOUR)
+ self.assertEqual(t_winter, u_winter - 5 * HOUR)
+ self.assertEqual(t_summer.fold, 0)
+ self.assertEqual(t_winter.fold, 0)
+
+ # What happens in the fall-back fold?
+ u = datetime(2002, 10, 27, 5, 30, tzinfo=Eastern2)
+ t0 = Eastern2.fromutc(u)
+ u += HOUR
+ t1 = Eastern2.fromutc(u)
+ self.assertEqual(t0, t1)
+ self.assertEqual(t0.fold, 0)
+ self.assertEqual(t1.fold, 1)
+ # The tricky part is when u is in the local fold:
+ u = datetime(2002, 10, 27, 1, 30, tzinfo=Eastern2)
+ t = Eastern2.fromutc(u)
+ self.assertEqual((t.day, t.hour), (26, 21))
+ # .. or gets into the local fold after a standard time adjustment
+ u = datetime(2002, 10, 27, 6, 30, tzinfo=Eastern2)
+ t = Eastern2.fromutc(u)
+ self.assertEqual((t.day, t.hour), (27, 1))
+
+ # What happens in the spring-forward gap?
+ u = datetime(2002, 4, 7, 2, 0, tzinfo=Eastern2)
+ t = Eastern2.fromutc(u)
+ self.assertEqual((t.day, t.hour), (6, 21))
+
+ def test_mixed_compare_regular(self):
+ t = datetime(2000, 1, 1, tzinfo=Eastern2)
+ self.assertEqual(t, t.astimezone(timezone.utc))
+ t = datetime(2000, 6, 1, tzinfo=Eastern2)
+ self.assertEqual(t, t.astimezone(timezone.utc))
+
+ def test_mixed_compare_fold(self):
+ t_fold = datetime(2002, 10, 27, 1, 45, tzinfo=Eastern2)
+ t_fold_utc = t_fold.astimezone(timezone.utc)
+ self.assertNotEqual(t_fold, t_fold_utc)
+
+ def test_mixed_compare_gap(self):
+ t_gap = datetime(2002, 4, 7, 2, 45, tzinfo=Eastern2)
+ t_gap_utc = t_gap.astimezone(timezone.utc)
+ self.assertNotEqual(t_gap, t_gap_utc)
+
+ def test_hash_aware(self):
+ t = datetime(2000, 1, 1, tzinfo=Eastern2)
+ self.assertEqual(hash(t), hash(t.replace(fold=1)))
+ t_fold = datetime(2002, 10, 27, 1, 45, tzinfo=Eastern2)
+ t_gap = datetime(2002, 4, 7, 2, 45, tzinfo=Eastern2)
+ self.assertEqual(hash(t_fold), hash(t_fold.replace(fold=1)))
+ self.assertEqual(hash(t_gap), hash(t_gap.replace(fold=1)))
+
+SEC = timedelta(0, 1)
+
+def pairs(iterable):
+ a, b = itertools.tee(iterable)
+ next(b, None)
+ return zip(a, b)
+
+class ZoneInfo(tzinfo):
+ zoneroot = '/usr/share/zoneinfo'
+ def __init__(self, ut, ti):
+ """
+
+ :param ut: array
+ Array of transition point timestamps
+ :param ti: list
+ A list of (offset, isdst, abbr) tuples
+ :return: None
+ """
+ self.ut = ut
+ self.ti = ti
+ self.lt = self.invert(ut, ti)
+
+ @staticmethod
+ def invert(ut, ti):
+ lt = (array('q', ut), array('q', ut))
+ if ut:
+ offset = ti[0][0] // SEC
+ lt[0][0] += offset
+ lt[1][0] += offset
+ for i in range(1, len(ut)):
+ lt[0][i] += ti[i-1][0] // SEC
+ lt[1][i] += ti[i][0] // SEC
+ return lt
+
+ @classmethod
+ def fromfile(cls, fileobj):
+ if fileobj.read(4).decode() != "TZif":
+ raise ValueError("not a zoneinfo file")
+ fileobj.seek(32)
+ counts = array('i')
+ counts.fromfile(fileobj, 3)
+ if sys.byteorder != 'big':
+ counts.byteswap()
+
+ ut = array('i')
+ ut.fromfile(fileobj, counts[0])
+ if sys.byteorder != 'big':
+ ut.byteswap()
+
+ type_indices = array('B')
+ type_indices.fromfile(fileobj, counts[0])
+
+ ttis = []
+ for i in range(counts[1]):
+ ttis.append(struct.unpack(">lbb", fileobj.read(6)))
+
+ abbrs = fileobj.read(counts[2])
+
+ # Convert ttis
+ for i, (gmtoff, isdst, abbrind) in enumerate(ttis):
+ abbr = abbrs[abbrind:abbrs.find(0, abbrind)].decode()
+ ttis[i] = (timedelta(0, gmtoff), isdst, abbr)
+
+ ti = [None] * len(ut)
+ for i, idx in enumerate(type_indices):
+ ti[i] = ttis[idx]
+
+ self = cls(ut, ti)
+
+ return self
+
+ @classmethod
+ def fromname(cls, name):
+ path = os.path.join(cls.zoneroot, name)
+ with open(path, 'rb') as f:
+ return cls.fromfile(f)
+
+ EPOCHORDINAL = date(1970, 1, 1).toordinal()
+
+ def fromutc(self, dt):
+ """datetime in UTC -> datetime in local time."""
+
+ if not isinstance(dt, datetime):
+ raise TypeError("fromutc() requires a datetime argument")
+ if dt.tzinfo is not self:
+ raise ValueError("dt.tzinfo is not self")
+
+ timestamp = ((dt.toordinal() - self.EPOCHORDINAL) * 86400
+ + dt.hour * 3600
+ + dt.minute * 60
+ + dt.second)
+
+ if timestamp < self.ut[1]:
+ tti = self.ti[0]
+ fold = 0
+ else:
+ idx = bisect.bisect_right(self.ut, timestamp)
+ assert self.ut[idx-1] <= timestamp
+ assert idx == len(self.ut) or timestamp < self.ut[idx]
+ tti_prev, tti = self.ti[idx-2:idx]
+ # Detect fold
+ shift = tti_prev[0] - tti[0]
+ fold = (shift > timedelta(0, timestamp - self.ut[idx-1]))
+ dt += tti[0]
+ if fold:
+ return dt.replace(fold=1)
+ else:
+ return dt
+
+ def _find_ti(self, dt, i):
+ timestamp = ((dt.toordinal() - self.EPOCHORDINAL) * 86400
+ + dt.hour * 3600
+ + dt.minute * 60
+ + dt.second)
+ lt = self.lt[dt.fold]
+ idx = bisect.bisect_right(lt, timestamp)
+
+ return self.ti[max(0, idx - 1)][i]
+
+ def utcoffset(self, dt):
+ return self._find_ti(dt, 0)
+
+ def dst(self, dt):
+ isdst = self._find_ti(dt, 1)
+ # XXX: We cannot accurately determine the "save" value,
+ # so let's return 1h whenever DST is in effect. Since
+ # we don't use dst() in fromutc(), it is unlikely that
+ # it will be needed for anything more than bool(dst()).
+ return ZERO if isdst else HOUR
+
+ def tzname(self, dt):
+ return self._find_ti(dt, 2)
+
+ @classmethod
+ def zonenames(cls, zonedir=None):
+ if zonedir is None:
+ zonedir = cls.zoneroot
+ zone_tab = os.path.join(zonedir, 'zone.tab')
+ try:
+ f = open(zone_tab)
+ except OSError:
+ return
+ with f:
+ for line in f:
+ line = line.strip()
+ if line and not line.startswith('#'):
+ yield line.split()[2]
+
+ @classmethod
+ def stats(cls, start_year=1):
+ count = gap_count = fold_count = zeros_count = 0
+ min_gap = min_fold = timedelta.max
+ max_gap = max_fold = ZERO
+ min_gap_datetime = max_gap_datetime = datetime.min
+ min_gap_zone = max_gap_zone = None
+ min_fold_datetime = max_fold_datetime = datetime.min
+ min_fold_zone = max_fold_zone = None
+ stats_since = datetime(start_year, 1, 1) # Starting from 1970 eliminates a lot of noise
+ for zonename in cls.zonenames():
+ count += 1
+ tz = cls.fromname(zonename)
+ for dt, shift in tz.transitions():
+ if dt < stats_since:
+ continue
+ if shift > ZERO:
+ gap_count += 1
+ if (shift, dt) > (max_gap, max_gap_datetime):
+ max_gap = shift
+ max_gap_zone = zonename
+ max_gap_datetime = dt
+ if (shift, datetime.max - dt) < (min_gap, datetime.max - min_gap_datetime):
+ min_gap = shift
+ min_gap_zone = zonename
+ min_gap_datetime = dt
+ elif shift < ZERO:
+ fold_count += 1
+ shift = -shift
+ if (shift, dt) > (max_fold, max_fold_datetime):
+ max_fold = shift
+ max_fold_zone = zonename
+ max_fold_datetime = dt
+ if (shift, datetime.max - dt) < (min_fold, datetime.max - min_fold_datetime):
+ min_fold = shift
+ min_fold_zone = zonename
+ min_fold_datetime = dt
+ else:
+ zeros_count += 1
+ trans_counts = (gap_count, fold_count, zeros_count)
+ print("Number of zones: %5d" % count)
+ print("Number of transitions: %5d = %d (gaps) + %d (folds) + %d (zeros)" %
+ ((sum(trans_counts),) + trans_counts))
+ print("Min gap: %16s at %s in %s" % (min_gap, min_gap_datetime, min_gap_zone))
+ print("Max gap: %16s at %s in %s" % (max_gap, max_gap_datetime, max_gap_zone))
+ print("Min fold: %16s at %s in %s" % (min_fold, min_fold_datetime, min_fold_zone))
+ print("Max fold: %16s at %s in %s" % (max_fold, max_fold_datetime, max_fold_zone))
+
+
+ def transitions(self):
+ for (_, prev_ti), (t, ti) in pairs(zip(self.ut, self.ti)):
+ shift = ti[0] - prev_ti[0]
+ yield datetime.utcfromtimestamp(t), shift
+
+ def nondst_folds(self):
+ """Find all folds with the same value of isdst on both sides of the transition."""
+ for (_, prev_ti), (t, ti) in pairs(zip(self.ut, self.ti)):
+ shift = ti[0] - prev_ti[0]
+ if shift < ZERO and ti[1] == prev_ti[1]:
+ yield datetime.utcfromtimestamp(t), -shift, prev_ti[2], ti[2]
+
+ @classmethod
+ def print_all_nondst_folds(cls, same_abbr=False, start_year=1):
+ count = 0
+ for zonename in cls.zonenames():
+ tz = cls.fromname(zonename)
+ for dt, shift, prev_abbr, abbr in tz.nondst_folds():
+ if dt.year < start_year or same_abbr and prev_abbr != abbr:
+ continue
+ count += 1
+ print("%3d) %-30s %s %10s %5s -> %s" %
+ (count, zonename, dt, shift, prev_abbr, abbr))
+
+ def folds(self):
+ for t, shift in self.transitions():
+ if shift < ZERO:
+ yield t, -shift
+
+ def gaps(self):
+ for t, shift in self.transitions():
+ if shift > ZERO:
+ yield t, shift
+
+ def zeros(self):
+ for t, shift in self.transitions():
+ if not shift:
+ yield t
+
+
+class ZoneInfoTest(unittest.TestCase):
+ zonename = 'America/New_York'
+
+ def setUp(self):
+ if sys.platform == "win32":
+ self.skipTest("Skipping zoneinfo tests on Windows")
+ try:
+ self.tz = ZoneInfo.fromname(self.zonename)
+ except FileNotFoundError as err:
+ self.skipTest("Skipping %s: %s" % (self.zonename, err))
+
+ def assertEquivDatetimes(self, a, b):
+ self.assertEqual((a.replace(tzinfo=None), a.fold, id(a.tzinfo)),
+ (b.replace(tzinfo=None), b.fold, id(b.tzinfo)))
+
+ def test_folds(self):
+ tz = self.tz
+ for dt, shift in tz.folds():
+ for x in [0 * shift, 0.5 * shift, shift - timedelta.resolution]:
+ udt = dt + x
+ ldt = tz.fromutc(udt.replace(tzinfo=tz))
+ self.assertEqual(ldt.fold, 1)
+ adt = udt.replace(tzinfo=timezone.utc).astimezone(tz)
+ self.assertEquivDatetimes(adt, ldt)
+ utcoffset = ldt.utcoffset()
+ self.assertEqual(ldt.replace(tzinfo=None), udt + utcoffset)
+ # Round trip
+ self.assertEquivDatetimes(ldt.astimezone(timezone.utc),
+ udt.replace(tzinfo=timezone.utc))
+
+
+ for x in [-timedelta.resolution, shift]:
+ udt = dt + x
+ udt = udt.replace(tzinfo=tz)
+ ldt = tz.fromutc(udt)
+ self.assertEqual(ldt.fold, 0)
+
+ def test_gaps(self):
+ tz = self.tz
+ for dt, shift in tz.gaps():
+ for x in [0 * shift, 0.5 * shift, shift - timedelta.resolution]:
+ udt = dt + x
+ udt = udt.replace(tzinfo=tz)
+ ldt = tz.fromutc(udt)
+ self.assertEqual(ldt.fold, 0)
+ adt = udt.replace(tzinfo=timezone.utc).astimezone(tz)
+ self.assertEquivDatetimes(adt, ldt)
+ utcoffset = ldt.utcoffset()
+ self.assertEqual(ldt.replace(tzinfo=None), udt.replace(tzinfo=None) + utcoffset)
+ # Create a local time inside the gap
+ ldt = tz.fromutc(dt.replace(tzinfo=tz)) - shift + x
+ self.assertLess(ldt.replace(fold=1).utcoffset(),
+ ldt.replace(fold=0).utcoffset(),
+ "At %s." % ldt)
+
+ for x in [-timedelta.resolution, shift]:
+ udt = dt + x
+ ldt = tz.fromutc(udt.replace(tzinfo=tz))
+ self.assertEqual(ldt.fold, 0)
+
+ def test_system_transitions(self):
+ if ('Riyadh8' in self.zonename or
+ # From tzdata NEWS file:
+ # The files solar87, solar88, and solar89 are no longer distributed.
+ # They were a negative experiment - that is, a demonstration that
+ # tz data can represent solar time only with some difficulty and error.
+ # Their presence in the distribution caused confusion, as Riyadh
+ # civil time was generally not solar time in those years.
+ self.zonename.startswith('right/')):
+ self.skipTest("Skipping %s" % self.zonename)
+ tz = self.tz
+ TZ = os.environ.get('TZ')
+ os.environ['TZ'] = self.zonename
+ try:
+ _time.tzset()
+ for udt, shift in tz.transitions():
+ if udt.year >= 2037:
+ # System support for times around the end of 32-bit time_t
+ # and later is flaky on many systems.
+ break
+ s0 = (udt - datetime(1970, 1, 1)) // SEC
+ ss = shift // SEC # shift seconds
+ for x in [-40 * 3600, -20*3600, -1, 0,
+ ss - 1, ss + 20 * 3600, ss + 40 * 3600]:
+ s = s0 + x
+ sdt = datetime.fromtimestamp(s)
+ tzdt = datetime.fromtimestamp(s, tz).replace(tzinfo=None)
+ self.assertEquivDatetimes(sdt, tzdt)
+ s1 = sdt.timestamp()
+ self.assertEqual(s, s1)
+ if ss > 0: # gap
+ # Create local time inside the gap
+ dt = datetime.fromtimestamp(s0) - shift / 2
+ ts0 = dt.timestamp()
+ ts1 = dt.replace(fold=1).timestamp()
+ self.assertEqual(ts0, s0 + ss / 2)
+ self.assertEqual(ts1, s0 - ss / 2)
+ finally:
+ if TZ is None:
+ del os.environ['TZ']
+ else:
+ os.environ['TZ'] = TZ
+ _time.tzset()
+
+
+class ZoneInfoCompleteTest(unittest.TestSuite):
+ def __init__(self):
+ tests = []
+ if is_resource_enabled('tzdata'):
+ for name in ZoneInfo.zonenames():
+ Test = type('ZoneInfoTest[%s]' % name, (ZoneInfoTest,), {})
+ Test.zonename = name
+ for method in dir(Test):
+ if method.startswith('test_'):
+ tests.append(Test(method))
+ super().__init__(tests)
+
+# Iran had a sub-minute UTC offset before 1946.
+class IranTest(ZoneInfoTest):
+ zonename = 'Asia/Tehran'
+
+def load_tests(loader, standard_tests, pattern):
+ standard_tests.addTest(ZoneInfoCompleteTest())
+ return standard_tests
+
+
if __name__ == "__main__":
unittest.main()
diff --git a/Lib/test/dtracedata/assert_usable.d b/Lib/test/dtracedata/assert_usable.d
new file mode 100644
index 0000000..0b2d4da
--- /dev/null
+++ b/Lib/test/dtracedata/assert_usable.d
@@ -0,0 +1,5 @@
+BEGIN
+{
+ printf("probe: success\n");
+ exit(0);
+}
diff --git a/Lib/test/dtracedata/assert_usable.stp b/Lib/test/dtracedata/assert_usable.stp
new file mode 100644
index 0000000..88e7e68
--- /dev/null
+++ b/Lib/test/dtracedata/assert_usable.stp
@@ -0,0 +1,5 @@
+probe begin
+{
+ println("probe: success")
+ exit ()
+}
diff --git a/Lib/test/dtracedata/call_stack.d b/Lib/test/dtracedata/call_stack.d
new file mode 100644
index 0000000..450e939
--- /dev/null
+++ b/Lib/test/dtracedata/call_stack.d
@@ -0,0 +1,31 @@
+self int indent;
+
+python$target:::function-entry
+/copyinstr(arg1) == "start"/
+{
+ self->trace = 1;
+}
+
+python$target:::function-entry
+/self->trace/
+{
+ printf("%d\t%*s:", timestamp, 15, probename);
+ printf("%*s", self->indent, "");
+ printf("%s:%s:%d\n", basename(copyinstr(arg0)), copyinstr(arg1), arg2);
+ self->indent++;
+}
+
+python$target:::function-return
+/self->trace/
+{
+ self->indent--;
+ printf("%d\t%*s:", timestamp, 15, probename);
+ printf("%*s", self->indent, "");
+ printf("%s:%s:%d\n", basename(copyinstr(arg0)), copyinstr(arg1), arg2);
+}
+
+python$target:::function-return
+/copyinstr(arg1) == "start"/
+{
+ self->trace = 0;
+}
diff --git a/Lib/test/dtracedata/call_stack.d.expected b/Lib/test/dtracedata/call_stack.d.expected
new file mode 100644
index 0000000..27849d1
--- /dev/null
+++ b/Lib/test/dtracedata/call_stack.d.expected
@@ -0,0 +1,18 @@
+ function-entry:call_stack.py:start:23
+ function-entry: call_stack.py:function_1:1
+ function-entry: call_stack.py:function_3:9
+function-return: call_stack.py:function_3:10
+function-return: call_stack.py:function_1:2
+ function-entry: call_stack.py:function_2:5
+ function-entry: call_stack.py:function_1:1
+ function-entry: call_stack.py:function_3:9
+function-return: call_stack.py:function_3:10
+function-return: call_stack.py:function_1:2
+function-return: call_stack.py:function_2:6
+ function-entry: call_stack.py:function_3:9
+function-return: call_stack.py:function_3:10
+ function-entry: call_stack.py:function_4:13
+function-return: call_stack.py:function_4:14
+ function-entry: call_stack.py:function_5:18
+function-return: call_stack.py:function_5:21
+function-return:call_stack.py:start:28
diff --git a/Lib/test/dtracedata/call_stack.py b/Lib/test/dtracedata/call_stack.py
new file mode 100644
index 0000000..ee9f3ae
--- /dev/null
+++ b/Lib/test/dtracedata/call_stack.py
@@ -0,0 +1,30 @@
+def function_1():
+ function_3(1, 2)
+
+# Check stacktrace
+def function_2():
+ function_1()
+
+# CALL_FUNCTION_VAR
+def function_3(dummy, dummy2):
+ pass
+
+# CALL_FUNCTION_KW
+def function_4(**dummy):
+ return 1
+ return 2 # unreachable
+
+# CALL_FUNCTION_VAR_KW
+def function_5(dummy, dummy2, **dummy3):
+ if False:
+ return 7
+ return 8
+
+def start():
+ function_1()
+ function_2()
+ function_3(1, 2)
+ function_4(test=42)
+ function_5(*(1, 2), **{"test": 42})
+
+start()
diff --git a/Lib/test/dtracedata/call_stack.stp b/Lib/test/dtracedata/call_stack.stp
new file mode 100644
index 0000000..54082c2
--- /dev/null
+++ b/Lib/test/dtracedata/call_stack.stp
@@ -0,0 +1,41 @@
+global tracing
+
+function basename:string(path:string)
+{
+ last_token = token = tokenize(path, "/");
+ while (token != "") {
+ last_token = token;
+ token = tokenize("", "/");
+ }
+ return last_token;
+}
+
+probe process.mark("function__entry")
+{
+ funcname = user_string($arg2);
+
+ if (funcname == "start") {
+ tracing = 1;
+ }
+}
+
+probe process.mark("function__entry"), process.mark("function__return")
+{
+ filename = user_string($arg1);
+ funcname = user_string($arg2);
+ lineno = $arg3;
+
+ if (tracing) {
+ printf("%d\t%s:%s:%s:%d\n", gettimeofday_us(), $$name,
+ basename(filename), funcname, lineno);
+ }
+}
+
+probe process.mark("function__return")
+{
+ funcname = user_string($arg2);
+
+ if (funcname == "start") {
+ tracing = 0;
+ }
+}
diff --git a/Lib/test/dtracedata/call_stack.stp.expected b/Lib/test/dtracedata/call_stack.stp.expected
new file mode 100644
index 0000000..32cf396
--- /dev/null
+++ b/Lib/test/dtracedata/call_stack.stp.expected
@@ -0,0 +1,14 @@
+function__entry:call_stack.py:start:23
+function__entry:call_stack.py:function_1:1
+function__return:call_stack.py:function_1:2
+function__entry:call_stack.py:function_2:5
+function__entry:call_stack.py:function_1:1
+function__return:call_stack.py:function_1:2
+function__return:call_stack.py:function_2:6
+function__entry:call_stack.py:function_3:9
+function__return:call_stack.py:function_3:10
+function__entry:call_stack.py:function_4:13
+function__return:call_stack.py:function_4:14
+function__entry:call_stack.py:function_5:18
+function__return:call_stack.py:function_5:21
+function__return:call_stack.py:start:28
diff --git a/Lib/test/dtracedata/gc.d b/Lib/test/dtracedata/gc.d
new file mode 100644
index 0000000..4d91487
--- /dev/null
+++ b/Lib/test/dtracedata/gc.d
@@ -0,0 +1,18 @@
+python$target:::function-entry
+/copyinstr(arg1) == "start"/
+{
+ self->trace = 1;
+}
+
+python$target:::gc-start,
+python$target:::gc-done
+/self->trace/
+{
+ printf("%d\t%s:%ld\n", timestamp, probename, arg0);
+}
+
+python$target:::function-return
+/copyinstr(arg1) == "start"/
+{
+ self->trace = 0;
+}
diff --git a/Lib/test/dtracedata/gc.d.expected b/Lib/test/dtracedata/gc.d.expected
new file mode 100644
index 0000000..8e5ac2a
--- /dev/null
+++ b/Lib/test/dtracedata/gc.d.expected
@@ -0,0 +1,8 @@
+gc-start:0
+gc-done:0
+gc-start:1
+gc-done:0
+gc-start:2
+gc-done:0
+gc-start:2
+gc-done:1
diff --git a/Lib/test/dtracedata/gc.py b/Lib/test/dtracedata/gc.py
new file mode 100644
index 0000000..144a783
--- /dev/null
+++ b/Lib/test/dtracedata/gc.py
@@ -0,0 +1,13 @@
+import gc
+
+def start():
+ gc.collect(0)
+ gc.collect(1)
+ gc.collect(2)
+ l = []
+ l.append(l)
+ del l
+ gc.collect(2)
+
+gc.collect()
+start()
diff --git a/Lib/test/dtracedata/gc.stp b/Lib/test/dtracedata/gc.stp
new file mode 100644
index 0000000..162c6d3
--- /dev/null
+++ b/Lib/test/dtracedata/gc.stp
@@ -0,0 +1,26 @@
+global tracing
+
+probe process.mark("function__entry")
+{
+ funcname = user_string($arg2);
+
+ if (funcname == "start") {
+ tracing = 1;
+ }
+}
+
+probe process.mark("gc__start"), process.mark("gc__done")
+{
+ if (tracing) {
+ printf("%d\t%s:%ld\n", gettimeofday_us(), $$name, $arg1);
+ }
+}
+
+probe process.mark("function__return")
+{
+ funcname = user_string($arg2);
+
+ if (funcname == "start") {
+ tracing = 0;
+ }
+}
diff --git a/Lib/test/dtracedata/gc.stp.expected b/Lib/test/dtracedata/gc.stp.expected
new file mode 100644
index 0000000..7e6e622
--- /dev/null
+++ b/Lib/test/dtracedata/gc.stp.expected
@@ -0,0 +1,8 @@
+gc__start:0
+gc__done:0
+gc__start:1
+gc__done:0
+gc__start:2
+gc__done:0
+gc__start:2
+gc__done:1
diff --git a/Lib/test/dtracedata/instance.py b/Lib/test/dtracedata/instance.py
new file mode 100644
index 0000000..f142137
--- /dev/null
+++ b/Lib/test/dtracedata/instance.py
@@ -0,0 +1,24 @@
+import gc
+
+class old_style_class():
+ pass
+class new_style_class(object):
+ pass
+
+a = old_style_class()
+del a
+gc.collect()
+b = new_style_class()
+del b
+gc.collect()
+
+a = old_style_class()
+del old_style_class
+gc.collect()
+b = new_style_class()
+del new_style_class
+gc.collect()
+del a
+gc.collect()
+del b
+gc.collect()
diff --git a/Lib/test/dtracedata/line.d b/Lib/test/dtracedata/line.d
new file mode 100644
index 0000000..03f22db
--- /dev/null
+++ b/Lib/test/dtracedata/line.d
@@ -0,0 +1,7 @@
+python$target:::line
+/(copyinstr(arg1)=="test_line")/
+{
+ printf("%d\t%s:%s:%s:%d\n", timestamp,
+ probename, basename(copyinstr(arg0)),
+ copyinstr(arg1), arg2);
+}
diff --git a/Lib/test/dtracedata/line.d.expected b/Lib/test/dtracedata/line.d.expected
new file mode 100644
index 0000000..9b16ce7
--- /dev/null
+++ b/Lib/test/dtracedata/line.d.expected
@@ -0,0 +1,20 @@
+line:line.py:test_line:2
+line:line.py:test_line:3
+line:line.py:test_line:4
+line:line.py:test_line:5
+line:line.py:test_line:6
+line:line.py:test_line:7
+line:line.py:test_line:8
+line:line.py:test_line:9
+line:line.py:test_line:10
+line:line.py:test_line:11
+line:line.py:test_line:4
+line:line.py:test_line:5
+line:line.py:test_line:6
+line:line.py:test_line:7
+line:line.py:test_line:8
+line:line.py:test_line:10
+line:line.py:test_line:11
+line:line.py:test_line:4
+line:line.py:test_line:12
+line:line.py:test_line:13
diff --git a/Lib/test/dtracedata/line.py b/Lib/test/dtracedata/line.py
new file mode 100644
index 0000000..0930ff3
--- /dev/null
+++ b/Lib/test/dtracedata/line.py
@@ -0,0 +1,17 @@
+def test_line():
+ a = 1
+ print('# Preamble', a)
+ for i in range(2):
+ a = i
+ b = i+2
+ c = i+3
+ if c < 4:
+ a = c
+ d = a + b +c
+ print('#', a, b, c, d)
+ a = 1
+ print('# Epilogue', a)
+
+
+if __name__ == '__main__':
+ test_line()
diff --git a/Lib/test/eintrdata/eintr_tester.py b/Lib/test/eintrdata/eintr_tester.py
index e3f1aa5..d194e77 100644
--- a/Lib/test/eintrdata/eintr_tester.py
+++ b/Lib/test/eintrdata/eintr_tester.py
@@ -9,7 +9,7 @@ sub-second periodicity (contrarily to signal()).
"""
import contextlib
-import io
+import faulthandler
import os
import select
import signal
@@ -20,6 +20,7 @@ import time
import unittest
from test import support
+android_not_root = support.android_not_root
@contextlib.contextmanager
def kill_on_error(proc):
@@ -50,6 +51,10 @@ class EINTRBaseTest(unittest.TestCase):
signal.setitimer(signal.ITIMER_REAL, cls.signal_delay,
cls.signal_period)
+ # Issue #25277: Use faulthandler to try to debug a hang on FreeBSD
+ if hasattr(faulthandler, 'dump_traceback_later'):
+ faulthandler.dump_traceback_later(10 * 60, exit=True)
+
@classmethod
def stop_alarm(cls):
signal.setitimer(signal.ITIMER_REAL, 0, 0)
@@ -58,6 +63,8 @@ class EINTRBaseTest(unittest.TestCase):
def tearDownClass(cls):
cls.stop_alarm()
signal.signal(signal.SIGALRM, cls.orig_handler)
+ if hasattr(faulthandler, 'cancel_dump_traceback_later'):
+ faulthandler.cancel_dump_traceback_later()
def subprocess(self, *args, **kw):
cmd_args = (sys.executable, '-c') + args
@@ -77,6 +84,9 @@ class OSEINTRTest(EINTRBaseTest):
processes = [self.new_sleep_process() for _ in range(num)]
for _ in range(num):
wait_func()
+ # Call the Popen method to avoid a ResourceWarning
+ for proc in processes:
+ proc.wait()
def test_wait(self):
self._test_wait_multiple(os.wait)
@@ -88,6 +98,8 @@ class OSEINTRTest(EINTRBaseTest):
def _test_wait_single(self, wait_func):
proc = self.new_sleep_process()
wait_func(proc.pid)
+ # Call the Popen method to avoid a ResourceWarning
+ proc.wait()
def test_waitpid(self):
self._test_wait_single(lambda pid: os.waitpid(pid, 0))
@@ -300,6 +312,7 @@ class SocketEINTRTest(EINTRBaseTest):
# https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=203162
@support.requires_freebsd_version(10, 3)
@unittest.skipUnless(hasattr(os, 'mkfifo'), 'needs mkfifo()')
+ @unittest.skipIf(android_not_root, "mkfifo not allowed, non root user")
def _test_open(self, do_open_close_reader, do_open_close_writer):
filename = support.TESTFN
diff --git a/Lib/test/exception_hierarchy.txt b/Lib/test/exception_hierarchy.txt
index 0513765..7333b2a 100644
--- a/Lib/test/exception_hierarchy.txt
+++ b/Lib/test/exception_hierarchy.txt
@@ -14,6 +14,7 @@ BaseException
+-- BufferError
+-- EOFError
+-- ImportError
+ +-- ModuleNotFoundError
+-- LookupError
| +-- IndexError
| +-- KeyError
diff --git a/Lib/test/libregrtest/__init__.py b/Lib/test/libregrtest/__init__.py
new file mode 100644
index 0000000..7ba0e6e
--- /dev/null
+++ b/Lib/test/libregrtest/__init__.py
@@ -0,0 +1,5 @@
+# We import importlib *ASAP* in order to test #15386
+import importlib
+
+from test.libregrtest.cmdline import _parse_args, RESOURCE_NAMES
+from test.libregrtest.main import main
diff --git a/Lib/test/libregrtest/cmdline.py b/Lib/test/libregrtest/cmdline.py
new file mode 100644
index 0000000..d621f5f
--- /dev/null
+++ b/Lib/test/libregrtest/cmdline.py
@@ -0,0 +1,347 @@
+import argparse
+import os
+import sys
+from test import support
+
+
+USAGE = """\
+python -m test [options] [test_name1 [test_name2 ...]]
+python path/to/Lib/test/regrtest.py [options] [test_name1 [test_name2 ...]]
+"""
+
+DESCRIPTION = """\
+Run Python regression tests.
+
+If no arguments or options are provided, finds all files matching
+the pattern "test_*" in the Lib/test subdirectory and runs
+them in alphabetical order (but see -M and -u, below, for exceptions).
+
+For more rigorous testing, it is useful to use the following
+command line:
+
+python -E -Wd -m test [options] [test_name1 ...]
+"""
+
+EPILOG = """\
+Additional option details:
+
+-r randomizes test execution order. You can use --randseed=int to provide an
+int seed value for the randomizer; this is useful for reproducing troublesome
+test orders.
+
+-s On the first invocation of regrtest using -s, the first test file found
+or the first test file given on the command line is run, and the name of
+the next test is recorded in a file named pynexttest. If run from the
+Python build directory, pynexttest is located in the 'build' subdirectory,
+otherwise it is located in tempfile.gettempdir(). On subsequent runs,
+the test in pynexttest is run, and the next test is written to pynexttest.
+When the last test has been run, pynexttest is deleted. In this way it
+is possible to single step through the test files. This is useful when
+doing memory analysis on the Python interpreter, which process tends to
+consume too many resources to run the full regression test non-stop.
+
+-S is used to continue running tests after an aborted run. It will
+maintain the order a standard run (ie, this assumes -r is not used).
+This is useful after the tests have prematurely stopped for some external
+reason and you want to start running from where you left off rather
+than starting from the beginning.
+
+-f reads the names of tests from the file given as f's argument, one
+or more test names per line. Whitespace is ignored. Blank lines and
+lines beginning with '#' are ignored. This is especially useful for
+whittling down failures involving interactions among tests.
+
+-L causes the leaks(1) command to be run just before exit if it exists.
+leaks(1) is available on Mac OS X and presumably on some other
+FreeBSD-derived systems.
+
+-R runs each test several times and examines sys.gettotalrefcount() to
+see if the test appears to be leaking references. The argument should
+be of the form stab:run:fname where 'stab' is the number of times the
+test is run to let gettotalrefcount settle down, 'run' is the number
+of times further it is run and 'fname' is the name of the file the
+reports are written to. These parameters all have defaults (5, 4 and
+"reflog.txt" respectively), and the minimal invocation is '-R :'.
+
+-M runs tests that require an exorbitant amount of memory. These tests
+typically try to ascertain containers keep working when containing more than
+2 billion objects, which only works on 64-bit systems. There are also some
+tests that try to exhaust the address space of the process, which only makes
+sense on 32-bit systems with at least 2Gb of memory. The passed-in memlimit,
+which is a string in the form of '2.5Gb', determines howmuch memory the
+tests will limit themselves to (but they may go slightly over.) The number
+shouldn't be more memory than the machine has (including swap memory). You
+should also keep in mind that swap memory is generally much, much slower
+than RAM, and setting memlimit to all available RAM or higher will heavily
+tax the machine. On the other hand, it is no use running these tests with a
+limit of less than 2.5Gb, and many require more than 20Gb. Tests that expect
+to use more than memlimit memory will be skipped. The big-memory tests
+generally run very, very long.
+
+-u is used to specify which special resource intensive tests to run,
+such as those requiring large file support or network connectivity.
+The argument is a comma-separated list of words indicating the
+resources to test. Currently only the following are defined:
+
+ all - Enable all special resources.
+
+ none - Disable all special resources (this is the default).
+
+ audio - Tests that use the audio device. (There are known
+ cases of broken audio drivers that can crash Python or
+ even the Linux kernel.)
+
+ curses - Tests that use curses and will modify the terminal's
+ state and output modes.
+
+ largefile - It is okay to run some test that may create huge
+ files. These tests can take a long time and may
+ consume >2GB of disk space temporarily.
+
+ network - It is okay to run tests that use external network
+ resource, e.g. testing SSL support for sockets.
+
+ decimal - Test the decimal module against a large suite that
+ verifies compliance with standards.
+
+ cpu - Used for certain CPU-heavy tests.
+
+ subprocess Run all tests for the subprocess module.
+
+ urlfetch - It is okay to download files required on testing.
+
+ gui - Run tests that require a running GUI.
+
+ tzdata - Run tests that require timezone data.
+
+To enable all resources except one, use '-uall,-<resource>'. For
+example, to run all the tests except for the gui tests, give the
+option '-uall,-gui'.
+"""
+
+
+RESOURCE_NAMES = ('audio', 'curses', 'largefile', 'network',
+ 'decimal', 'cpu', 'subprocess', 'urlfetch', 'gui', 'tzdata')
+
+class _ArgParser(argparse.ArgumentParser):
+
+ def error(self, message):
+ super().error(message + "\nPass -h or --help for complete help.")
+
+
+def _create_parser():
+ # Set prog to prevent the uninformative "__main__.py" from displaying in
+ # error messages when using "python -m test ...".
+ parser = _ArgParser(prog='regrtest.py',
+ usage=USAGE,
+ description=DESCRIPTION,
+ epilog=EPILOG,
+ add_help=False,
+ formatter_class=argparse.RawDescriptionHelpFormatter)
+
+ # Arguments with this clause added to its help are described further in
+ # the epilog's "Additional option details" section.
+ more_details = ' See the section at bottom for more details.'
+
+ group = parser.add_argument_group('General options')
+ # We add help explicitly to control what argument group it renders under.
+ group.add_argument('-h', '--help', action='help',
+ help='show this help message and exit')
+ group.add_argument('--timeout', metavar='TIMEOUT', type=float,
+ help='dump the traceback and exit if a test takes '
+ 'more than TIMEOUT seconds; disabled if TIMEOUT '
+ 'is negative or equals to zero')
+ group.add_argument('--wait', action='store_true',
+ help='wait for user input, e.g., allow a debugger '
+ 'to be attached')
+ group.add_argument('--slaveargs', metavar='ARGS')
+ group.add_argument('-S', '--start', metavar='START',
+ help='the name of the test at which to start.' +
+ more_details)
+
+ group = parser.add_argument_group('Verbosity')
+ group.add_argument('-v', '--verbose', action='count',
+ help='run tests in verbose mode with output to stdout')
+ group.add_argument('-w', '--verbose2', action='store_true',
+ help='re-run failed tests in verbose mode')
+ group.add_argument('-W', '--verbose3', action='store_true',
+ help='display test output on failure')
+ group.add_argument('-q', '--quiet', action='store_true',
+ help='no output unless one or more tests fail')
+ group.add_argument('-o', '--slowest', action='store_true', dest='print_slow',
+ help='print the slowest 10 tests')
+ group.add_argument('--header', action='store_true',
+ help='print header with interpreter info')
+
+ group = parser.add_argument_group('Selecting tests')
+ group.add_argument('-r', '--randomize', action='store_true',
+ help='randomize test execution order.' + more_details)
+ group.add_argument('--randseed', metavar='SEED',
+ dest='random_seed', type=int,
+ help='pass a random seed to reproduce a previous '
+ 'random run')
+ group.add_argument('-f', '--fromfile', metavar='FILE',
+ help='read names of tests to run from a file.' +
+ more_details)
+ group.add_argument('-x', '--exclude', action='store_true',
+ help='arguments are tests to *exclude*')
+ group.add_argument('-s', '--single', action='store_true',
+ help='single step through a set of tests.' +
+ more_details)
+ group.add_argument('-m', '--match', metavar='PAT',
+ dest='match_tests',
+ help='match test cases and methods with glob pattern PAT')
+ group.add_argument('-G', '--failfast', action='store_true',
+ help='fail as soon as a test fails (only with -v or -W)')
+ group.add_argument('-u', '--use', metavar='RES1,RES2,...',
+ action='append', type=resources_list,
+ help='specify which special resource intensive tests '
+ 'to run.' + more_details)
+ group.add_argument('-M', '--memlimit', metavar='LIMIT',
+ help='run very large memory-consuming tests.' +
+ more_details)
+ group.add_argument('--testdir', metavar='DIR',
+ type=relative_filename,
+ help='execute test files in the specified directory '
+ '(instead of the Python stdlib test suite)')
+
+ group = parser.add_argument_group('Special runs')
+ group.add_argument('-l', '--findleaks', action='store_true',
+ help='if GC is available detect tests that leak memory')
+ group.add_argument('-L', '--runleaks', action='store_true',
+ help='run the leaks(1) command just before exit.' +
+ more_details)
+ group.add_argument('-R', '--huntrleaks', metavar='RUNCOUNTS',
+ type=huntrleaks,
+ help='search for reference leaks (needs debug build, '
+ 'very slow).' + more_details)
+ group.add_argument('-j', '--multiprocess', metavar='PROCESSES',
+ dest='use_mp', type=int,
+ help='run PROCESSES processes at once')
+ group.add_argument('-T', '--coverage', action='store_true',
+ dest='trace',
+ help='turn on code coverage tracing using the trace '
+ 'module')
+ group.add_argument('-D', '--coverdir', metavar='DIR',
+ type=relative_filename,
+ help='directory where coverage files are put')
+ group.add_argument('-N', '--nocoverdir',
+ action='store_const', const=None, dest='coverdir',
+ help='put coverage files alongside modules')
+ group.add_argument('-t', '--threshold', metavar='THRESHOLD',
+ type=int,
+ help='call gc.set_threshold(THRESHOLD)')
+ group.add_argument('-n', '--nowindows', action='store_true',
+ help='suppress error message boxes on Windows')
+ group.add_argument('-F', '--forever', action='store_true',
+ help='run the specified tests in a loop, until an '
+ 'error happens')
+ group.add_argument('--list-tests', action='store_true',
+ help="only write the name of tests that will be run, "
+ "don't execute them")
+ group.add_argument('-P', '--pgo', dest='pgo', action='store_true',
+ help='enable Profile Guided Optimization training')
+
+ return parser
+
+
+def relative_filename(string):
+ # CWD is replaced with a temporary dir before calling main(), so we
+ # join it with the saved CWD so it ends up where the user expects.
+ return os.path.join(support.SAVEDCWD, string)
+
+
+def huntrleaks(string):
+ args = string.split(':')
+ if len(args) not in (2, 3):
+ raise argparse.ArgumentTypeError(
+ 'needs 2 or 3 colon-separated arguments')
+ nwarmup = int(args[0]) if args[0] else 5
+ ntracked = int(args[1]) if args[1] else 4
+ fname = args[2] if len(args) > 2 and args[2] else 'reflog.txt'
+ return nwarmup, ntracked, fname
+
+
+def resources_list(string):
+ u = [x.lower() for x in string.split(',')]
+ for r in u:
+ if r == 'all' or r == 'none':
+ continue
+ if r[0] == '-':
+ r = r[1:]
+ if r not in RESOURCE_NAMES:
+ raise argparse.ArgumentTypeError('invalid resource: ' + r)
+ return u
+
+
+def _parse_args(args, **kwargs):
+ # Defaults
+ ns = argparse.Namespace(testdir=None, verbose=0, quiet=False,
+ exclude=False, single=False, randomize=False, fromfile=None,
+ findleaks=False, use_resources=None, trace=False, coverdir='coverage',
+ runleaks=False, huntrleaks=False, verbose2=False, print_slow=False,
+ random_seed=None, use_mp=None, verbose3=False, forever=False,
+ header=False, failfast=False, match_tests=None, pgo=False)
+ for k, v in kwargs.items():
+ if not hasattr(ns, k):
+ raise TypeError('%r is an invalid keyword argument '
+ 'for this function' % k)
+ setattr(ns, k, v)
+ if ns.use_resources is None:
+ ns.use_resources = []
+
+ parser = _create_parser()
+ # Issue #14191: argparse doesn't support "intermixed" positional and
+ # optional arguments. Use parse_known_args() as workaround.
+ ns.args = parser.parse_known_args(args=args, namespace=ns)[1]
+ for arg in ns.args:
+ if arg.startswith('-'):
+ parser.error("unrecognized arguments: %s" % arg)
+ sys.exit(1)
+
+ if ns.single and ns.fromfile:
+ parser.error("-s and -f don't go together!")
+ if ns.use_mp is not None and ns.trace:
+ parser.error("-T and -j don't go together!")
+ if ns.use_mp is not None and ns.findleaks:
+ parser.error("-l and -j don't go together!")
+ if ns.failfast and not (ns.verbose or ns.verbose3):
+ parser.error("-G/--failfast needs either -v or -W")
+ if ns.pgo and (ns.verbose or ns.verbose2 or ns.verbose3):
+ parser.error("--pgo/-v don't go together!")
+
+ if ns.nowindows:
+ print("Warning: the --nowindows (-n) option is deprecated. "
+ "Use -vv to display assertions in stderr.", file=sys.stderr)
+
+ if ns.quiet:
+ ns.verbose = 0
+ if ns.timeout is not None:
+ if ns.timeout <= 0:
+ ns.timeout = None
+ if ns.use_mp is not None:
+ if ns.use_mp <= 0:
+ # Use all cores + extras for tests that like to sleep
+ ns.use_mp = 2 + (os.cpu_count() or 1)
+ if ns.use:
+ for a in ns.use:
+ for r in a:
+ if r == 'all':
+ ns.use_resources[:] = RESOURCE_NAMES
+ continue
+ if r == 'none':
+ del ns.use_resources[:]
+ continue
+ remove = False
+ if r[0] == '-':
+ remove = True
+ r = r[1:]
+ if remove:
+ if r in ns.use_resources:
+ ns.use_resources.remove(r)
+ elif r not in ns.use_resources:
+ ns.use_resources.append(r)
+ if ns.random_seed is not None:
+ ns.randomize = True
+
+ return ns
diff --git a/Lib/test/libregrtest/main.py b/Lib/test/libregrtest/main.py
new file mode 100644
index 0000000..f0effc9
--- /dev/null
+++ b/Lib/test/libregrtest/main.py
@@ -0,0 +1,532 @@
+import datetime
+import faulthandler
+import locale
+import os
+import platform
+import random
+import re
+import sys
+import sysconfig
+import tempfile
+import textwrap
+import time
+from test.libregrtest.cmdline import _parse_args
+from test.libregrtest.runtest import (
+ findtests, runtest,
+ STDTESTS, NOTTESTS, PASSED, FAILED, ENV_CHANGED, SKIPPED, RESOURCE_DENIED,
+ INTERRUPTED, CHILD_ERROR,
+ PROGRESS_MIN_TIME, format_test_result)
+from test.libregrtest.setup import setup_tests
+from test import support
+try:
+ import gc
+except ImportError:
+ gc = None
+
+
+# When tests are run from the Python build directory, it is best practice
+# to keep the test files in a subfolder. This eases the cleanup of leftover
+# files using the "make distclean" command.
+if sysconfig.is_python_build():
+ TEMPDIR = os.path.join(sysconfig.get_config_var('srcdir'), 'build')
+else:
+ TEMPDIR = tempfile.gettempdir()
+TEMPDIR = os.path.abspath(TEMPDIR)
+
+
+def format_duration(seconds):
+ if seconds < 1.0:
+ return '%.0f ms' % (seconds * 1e3)
+ if seconds < 60.0:
+ return '%.0f sec' % seconds
+
+ minutes, seconds = divmod(seconds, 60.0)
+ return '%.0f min %.0f sec' % (minutes, seconds)
+
+
+class Regrtest:
+ """Execute a test suite.
+
+ This also parses command-line options and modifies its behavior
+ accordingly.
+
+ tests -- a list of strings containing test names (optional)
+ testdir -- the directory in which to look for tests (optional)
+
+ Users other than the Python test suite will certainly want to
+ specify testdir; if it's omitted, the directory containing the
+ Python test suite is searched for.
+
+ If the tests argument is omitted, the tests listed on the
+ command-line will be used. If that's empty, too, then all *.py
+ files beginning with test_ will be used.
+
+ The other default arguments (verbose, quiet, exclude,
+ single, randomize, findleaks, use_resources, trace, coverdir,
+ print_slow, and random_seed) allow programmers calling main()
+ directly to set the values that would normally be set by flags
+ on the command line.
+ """
+ def __init__(self):
+ # Namespace of command line options
+ self.ns = None
+
+ # tests
+ self.tests = []
+ self.selected = []
+
+ # test results
+ self.good = []
+ self.bad = []
+ self.skipped = []
+ self.resource_denieds = []
+ self.environment_changed = []
+ self.interrupted = False
+
+ # used by --slow
+ self.test_times = []
+
+ # used by --coverage, trace.Trace instance
+ self.tracer = None
+
+ # used by --findleaks, store for gc.garbage
+ self.found_garbage = []
+
+ # used to display the progress bar "[ 3/100]"
+ self.start_time = time.monotonic()
+ self.test_count = ''
+ self.test_count_width = 1
+
+ # used by --single
+ self.next_single_test = None
+ self.next_single_filename = None
+
+ def accumulate_result(self, test, result):
+ ok, test_time = result
+ if ok not in (CHILD_ERROR, INTERRUPTED):
+ self.test_times.append((test_time, test))
+ if ok == PASSED:
+ self.good.append(test)
+ elif ok == FAILED:
+ self.bad.append(test)
+ elif ok == ENV_CHANGED:
+ self.environment_changed.append(test)
+ elif ok == SKIPPED:
+ self.skipped.append(test)
+ elif ok == RESOURCE_DENIED:
+ self.skipped.append(test)
+ self.resource_denieds.append(test)
+
+ def display_progress(self, test_index, test):
+ if self.ns.quiet:
+ return
+ if self.bad and not self.ns.pgo:
+ fmt = "{time} [{test_index:{count_width}}{test_count}/{nbad}] {test_name}"
+ else:
+ fmt = "{time} [{test_index:{count_width}}{test_count}] {test_name}"
+ test_time = time.monotonic() - self.start_time
+ test_time = datetime.timedelta(seconds=int(test_time))
+ line = fmt.format(count_width=self.test_count_width,
+ test_index=test_index,
+ test_count=self.test_count,
+ nbad=len(self.bad),
+ test_name=test,
+ time=test_time)
+ print(line, flush=True)
+
+ def parse_args(self, kwargs):
+ ns = _parse_args(sys.argv[1:], **kwargs)
+
+ if ns.timeout and not hasattr(faulthandler, 'dump_traceback_later'):
+ print("Warning: The timeout option requires "
+ "faulthandler.dump_traceback_later", file=sys.stderr)
+ ns.timeout = None
+
+ if ns.threshold is not None and gc is None:
+ print('No GC available, ignore --threshold.', file=sys.stderr)
+ ns.threshold = None
+
+ if ns.findleaks:
+ if gc is not None:
+ # Uncomment the line below to report garbage that is not
+ # freeable by reference counting alone. By default only
+ # garbage that is not collectable by the GC is reported.
+ pass
+ #gc.set_debug(gc.DEBUG_SAVEALL)
+ else:
+ print('No GC available, disabling --findleaks',
+ file=sys.stderr)
+ ns.findleaks = False
+
+ # Strip .py extensions.
+ removepy(ns.args)
+
+ return ns
+
+ def find_tests(self, tests):
+ self.tests = tests
+
+ if self.ns.single:
+ self.next_single_filename = os.path.join(TEMPDIR, 'pynexttest')
+ try:
+ with open(self.next_single_filename, 'r') as fp:
+ next_test = fp.read().strip()
+ self.tests = [next_test]
+ except OSError:
+ pass
+
+ if self.ns.fromfile:
+ self.tests = []
+ # regex to match 'test_builtin' in line:
+ # '0:00:00 [ 4/400] test_builtin -- test_dict took 1 sec'
+ regex = (r'^(?:[0-9]+:[0-9]+:[0-9]+ *)?'
+ r'(?:\[[0-9/ ]+\] *)?'
+ r'(test_[a-zA-Z0-9_]+)')
+ regex = re.compile(regex)
+ with open(os.path.join(support.SAVEDCWD, self.ns.fromfile)) as fp:
+ for line in fp:
+ line = line.strip()
+ if line.startswith('#'):
+ continue
+ match = regex.match(line)
+ if match is None:
+ continue
+ self.tests.append(match.group(1))
+
+ removepy(self.tests)
+
+ stdtests = STDTESTS[:]
+ nottests = NOTTESTS.copy()
+ if self.ns.exclude:
+ for arg in self.ns.args:
+ if arg in stdtests:
+ stdtests.remove(arg)
+ nottests.add(arg)
+ self.ns.args = []
+
+ # if testdir is set, then we are not running the python tests suite, so
+ # don't add default tests to be executed or skipped (pass empty values)
+ if self.ns.testdir:
+ alltests = findtests(self.ns.testdir, list(), set())
+ else:
+ alltests = findtests(self.ns.testdir, stdtests, nottests)
+
+ if not self.ns.fromfile:
+ self.selected = self.tests or self.ns.args or alltests
+ else:
+ self.selected = self.tests
+ if self.ns.single:
+ self.selected = self.selected[:1]
+ try:
+ pos = alltests.index(self.selected[0])
+ self.next_single_test = alltests[pos + 1]
+ except IndexError:
+ pass
+
+ # Remove all the selected tests that precede start if it's set.
+ if self.ns.start:
+ try:
+ del self.selected[:self.selected.index(self.ns.start)]
+ except ValueError:
+ print("Couldn't find starting test (%s), using all tests"
+ % self.ns.start, file=sys.stderr)
+
+ if self.ns.randomize:
+ if self.ns.random_seed is None:
+ self.ns.random_seed = random.randrange(10000000)
+ random.seed(self.ns.random_seed)
+ random.shuffle(self.selected)
+
+ def list_tests(self):
+ for name in self.selected:
+ print(name)
+
+ def rerun_failed_tests(self):
+ self.ns.verbose = True
+ self.ns.failfast = False
+ self.ns.verbose3 = False
+ self.ns.match_tests = None
+
+ print("Re-running failed tests in verbose mode")
+ for test in self.bad[:]:
+ print("Re-running test %r in verbose mode" % test, flush=True)
+ try:
+ self.ns.verbose = True
+ ok = runtest(self.ns, test)
+ except KeyboardInterrupt:
+ self.interrupted = True
+ # print a newline separate from the ^C
+ print()
+ break
+ else:
+ if ok[0] in {PASSED, ENV_CHANGED, SKIPPED, RESOURCE_DENIED}:
+ self.bad.remove(test)
+ else:
+ if self.bad:
+ print(count(len(self.bad), 'test'), "failed again:")
+ printlist(self.bad)
+
+ def display_result(self):
+ if self.interrupted:
+ # print a newline after ^C
+ print()
+ print("Test suite interrupted by signal SIGINT.")
+ executed = set(self.good) | set(self.bad) | set(self.skipped)
+ omitted = set(self.selected) - executed
+ print(count(len(omitted), "test"), "omitted:")
+ printlist(omitted)
+
+ # If running the test suite for PGO then no one cares about
+ # results.
+ if self.ns.pgo:
+ return
+
+ if self.good and not self.ns.quiet:
+ if (not self.bad
+ and not self.skipped
+ and not self.interrupted
+ and len(self.good) > 1):
+ print("All", end=' ')
+ print(count(len(self.good), "test"), "OK.")
+
+ if self.ns.print_slow:
+ self.test_times.sort(reverse=True)
+ print()
+ print("10 slowest tests:")
+ for time, test in self.test_times[:10]:
+ print("- %s: %s" % (test, format_duration(time)))
+
+ if self.bad:
+ print()
+ print(count(len(self.bad), "test"), "failed:")
+ printlist(self.bad)
+
+ if self.environment_changed:
+ print()
+ print("{} altered the execution environment:".format(
+ count(len(self.environment_changed), "test")))
+ printlist(self.environment_changed)
+
+ if self.skipped and not self.ns.quiet:
+ print()
+ print(count(len(self.skipped), "test"), "skipped:")
+ printlist(self.skipped)
+
+ def run_tests_sequential(self):
+ if self.ns.trace:
+ import trace
+ self.tracer = trace.Trace(trace=False, count=True)
+
+ save_modules = sys.modules.keys()
+
+ print("Run tests sequentially")
+
+ previous_test = None
+ for test_index, test in enumerate(self.tests, 1):
+ start_time = time.monotonic()
+
+ text = test
+ if previous_test:
+ text = '%s -- %s' % (text, previous_test)
+ self.display_progress(test_index, text)
+
+ if self.tracer:
+ # If we're tracing code coverage, then we don't exit with status
+ # if on a false return value from main.
+ cmd = ('result = runtest(self.ns, test); '
+ 'self.accumulate_result(test, result)')
+ ns = dict(locals())
+ self.tracer.runctx(cmd, globals=globals(), locals=ns)
+ result = ns['result']
+ else:
+ try:
+ result = runtest(self.ns, test)
+ except KeyboardInterrupt:
+ self.interrupted = True
+ self.accumulate_result(test, (INTERRUPTED, None))
+ break
+ else:
+ self.accumulate_result(test, result)
+
+ previous_test = format_test_result(test, result[0])
+ test_time = time.monotonic() - start_time
+ if test_time >= PROGRESS_MIN_TIME:
+ previous_test = "%s in %s" % (previous_test, format_duration(test_time))
+ elif result[0] == PASSED:
+ # be quiet: say nothing if the test passed shortly
+ previous_test = None
+
+ if self.ns.findleaks:
+ gc.collect()
+ if gc.garbage:
+ print("Warning: test created", len(gc.garbage), end=' ')
+ print("uncollectable object(s).")
+ # move the uncollectable objects somewhere so we don't see
+ # them again
+ self.found_garbage.extend(gc.garbage)
+ del gc.garbage[:]
+
+ # Unload the newly imported modules (best effort finalization)
+ for module in sys.modules.keys():
+ if module not in save_modules and module.startswith("test."):
+ support.unload(module)
+
+ if previous_test:
+ print(previous_test)
+
+ def _test_forever(self, tests):
+ while True:
+ for test in tests:
+ yield test
+ if self.bad:
+ return
+
+ def run_tests(self):
+ # For a partial run, we do not need to clutter the output.
+ if (self.ns.verbose
+ or self.ns.header
+ or not (self.ns.pgo or self.ns.quiet or self.ns.single
+ or self.tests or self.ns.args)):
+ # Print basic platform information
+ print("==", platform.python_implementation(), *sys.version.split())
+ print("== ", platform.platform(aliased=True),
+ "%s-endian" % sys.byteorder)
+ print("== ", "hash algorithm:", sys.hash_info.algorithm,
+ "64bit" if sys.maxsize > 2**32 else "32bit")
+ print("== cwd:", os.getcwd())
+ print("== encodings: locale=%s, FS=%s"
+ % (locale.getpreferredencoding(False),
+ sys.getfilesystemencoding()))
+ print("Testing with flags:", sys.flags)
+
+ if self.ns.randomize:
+ print("Using random seed", self.ns.random_seed)
+
+ if self.ns.forever:
+ self.tests = self._test_forever(list(self.selected))
+ self.test_count = ''
+ self.test_count_width = 3
+ else:
+ self.tests = iter(self.selected)
+ self.test_count = '/{}'.format(len(self.selected))
+ self.test_count_width = len(self.test_count) - 1
+
+ if self.ns.use_mp:
+ from test.libregrtest.runtest_mp import run_tests_multiprocess
+ run_tests_multiprocess(self)
+ else:
+ self.run_tests_sequential()
+
+ def finalize(self):
+ if self.next_single_filename:
+ if self.next_single_test:
+ with open(self.next_single_filename, 'w') as fp:
+ fp.write(self.next_single_test + '\n')
+ else:
+ os.unlink(self.next_single_filename)
+
+ if self.tracer:
+ r = self.tracer.results()
+ r.write_results(show_missing=True, summary=True,
+ coverdir=self.ns.coverdir)
+
+ print()
+ duration = time.monotonic() - self.start_time
+ print("Total duration: %s" % format_duration(duration))
+
+ if self.bad:
+ result = "FAILURE"
+ elif self.interrupted:
+ result = "INTERRUPTED"
+ else:
+ result = "SUCCESS"
+ print("Tests result: %s" % result)
+
+ if self.ns.runleaks:
+ os.system("leaks %d" % os.getpid())
+
+ def main(self, tests=None, **kwargs):
+ global TEMPDIR
+
+ if sysconfig.is_python_build():
+ try:
+ os.mkdir(TEMPDIR)
+ except FileExistsError:
+ pass
+
+ # Define a writable temp dir that will be used as cwd while running
+ # the tests. The name of the dir includes the pid to allow parallel
+ # testing (see the -j option).
+ test_cwd = 'test_python_{}'.format(os.getpid())
+ test_cwd = os.path.join(TEMPDIR, test_cwd)
+
+ # Run the tests in a context manager that temporarily changes the CWD to a
+ # temporary and writable directory. If it's not possible to create or
+ # change the CWD, the original CWD will be used. The original CWD is
+ # available from support.SAVEDCWD.
+ with support.temp_cwd(test_cwd, quiet=True):
+ self._main(tests, kwargs)
+
+ def _main(self, tests, kwargs):
+ self.ns = self.parse_args(kwargs)
+
+ if self.ns.slaveargs is not None:
+ from test.libregrtest.runtest_mp import run_tests_slave
+ run_tests_slave(self.ns.slaveargs)
+
+ if self.ns.wait:
+ input("Press any key to continue...")
+
+ support.PGO = self.ns.pgo
+
+ setup_tests(self.ns)
+
+ self.find_tests(tests)
+
+ if self.ns.list_tests:
+ self.list_tests()
+ sys.exit(0)
+
+ self.run_tests()
+ self.display_result()
+
+ if self.ns.verbose2 and self.bad:
+ self.rerun_failed_tests()
+
+ self.finalize()
+ sys.exit(len(self.bad) > 0 or self.interrupted)
+
+
+def removepy(names):
+ if not names:
+ return
+ for idx, name in enumerate(names):
+ basename, ext = os.path.splitext(name)
+ if ext == '.py':
+ names[idx] = basename
+
+
+def count(n, word):
+ if n == 1:
+ return "%d %s" % (n, word)
+ else:
+ return "%d %ss" % (n, word)
+
+
+def printlist(x, width=70, indent=4):
+ """Print the elements of iterable x to stdout.
+
+ Optional arg width (default 70) is the maximum line length.
+ Optional arg indent (default 4) is the number of blanks with which to
+ begin each line.
+ """
+
+ blanks = ' ' * indent
+ # Print the sorted list: 'x' may be a '--random' list or a set()
+ print(textwrap.fill(' '.join(str(elt) for elt in sorted(x)), width,
+ initial_indent=blanks, subsequent_indent=blanks))
+
+
+def main(tests=None, **kwargs):
+ """Run the Python suite."""
+ Regrtest().main(tests=tests, **kwargs)
diff --git a/Lib/test/libregrtest/refleak.py b/Lib/test/libregrtest/refleak.py
new file mode 100644
index 0000000..3b3d245
--- /dev/null
+++ b/Lib/test/libregrtest/refleak.py
@@ -0,0 +1,269 @@
+import errno
+import os
+import re
+import sys
+import warnings
+from inspect import isabstract
+from test import support
+
+
+try:
+ MAXFD = os.sysconf("SC_OPEN_MAX")
+except Exception:
+ MAXFD = 256
+
+
+def fd_count():
+ """Count the number of open file descriptors"""
+ if sys.platform.startswith(('linux', 'freebsd')):
+ try:
+ names = os.listdir("/proc/self/fd")
+ return len(names)
+ except FileNotFoundError:
+ pass
+
+ count = 0
+ for fd in range(MAXFD):
+ try:
+ # Prefer dup() over fstat(). fstat() can require input/output
+ # whereas dup() doesn't.
+ fd2 = os.dup(fd)
+ except OSError as e:
+ if e.errno != errno.EBADF:
+ raise
+ else:
+ os.close(fd2)
+ count += 1
+ return count
+
+
+def dash_R(the_module, test, indirect_test, huntrleaks):
+ """Run a test multiple times, looking for reference leaks.
+
+ Returns:
+ False if the test didn't leak references; True if we detected refleaks.
+ """
+ # This code is hackish and inelegant, but it seems to do the job.
+ import copyreg
+ import collections.abc
+
+ if not hasattr(sys, 'gettotalrefcount'):
+ raise Exception("Tracking reference leaks requires a debug build "
+ "of Python")
+
+ # Save current values for dash_R_cleanup() to restore.
+ fs = warnings.filters[:]
+ ps = copyreg.dispatch_table.copy()
+ pic = sys.path_importer_cache.copy()
+ try:
+ import zipimport
+ except ImportError:
+ zdc = None # Run unmodified on platforms without zipimport support
+ else:
+ zdc = zipimport._zip_directory_cache.copy()
+ abcs = {}
+ for abc in [getattr(collections.abc, a) for a in collections.abc.__all__]:
+ if not isabstract(abc):
+ continue
+ for obj in abc.__subclasses__() + [abc]:
+ abcs[obj] = obj._abc_registry.copy()
+
+ nwarmup, ntracked, fname = huntrleaks
+ fname = os.path.join(support.SAVEDCWD, fname)
+ repcount = nwarmup + ntracked
+ rc_deltas = [0] * repcount
+ alloc_deltas = [0] * repcount
+ fd_deltas = [0] * repcount
+
+ print("beginning", repcount, "repetitions", file=sys.stderr)
+ print(("1234567890"*(repcount//10 + 1))[:repcount], file=sys.stderr,
+ flush=True)
+ # initialize variables to make pyflakes quiet
+ rc_before = alloc_before = fd_before = 0
+ for i in range(repcount):
+ indirect_test()
+ alloc_after, rc_after, fd_after = dash_R_cleanup(fs, ps, pic, zdc,
+ abcs)
+ print('.', end='', flush=True)
+ if i >= nwarmup:
+ rc_deltas[i] = rc_after - rc_before
+ alloc_deltas[i] = alloc_after - alloc_before
+ fd_deltas[i] = fd_after - fd_before
+ alloc_before = alloc_after
+ rc_before = rc_after
+ fd_before = fd_after
+ print(file=sys.stderr)
+ # These checkers return False on success, True on failure
+ def check_rc_deltas(deltas):
+ return any(deltas)
+ def check_alloc_deltas(deltas):
+ # At least 1/3rd of 0s
+ if 3 * deltas.count(0) < len(deltas):
+ return True
+ # Nothing else than 1s, 0s and -1s
+ if not set(deltas) <= {1,0,-1}:
+ return True
+ return False
+ failed = False
+ for deltas, item_name, checker in [
+ (rc_deltas, 'references', check_rc_deltas),
+ (alloc_deltas, 'memory blocks', check_alloc_deltas),
+ (fd_deltas, 'file descriptors', check_rc_deltas)]:
+ if checker(deltas):
+ msg = '%s leaked %s %s, sum=%s' % (
+ test, deltas[nwarmup:], item_name, sum(deltas))
+ print(msg, file=sys.stderr, flush=True)
+ with open(fname, "a") as refrep:
+ print(msg, file=refrep)
+ refrep.flush()
+ failed = True
+ return failed
+
+
+def dash_R_cleanup(fs, ps, pic, zdc, abcs):
+ import gc, copyreg
+ import collections.abc
+ from weakref import WeakSet
+
+ # Restore some original values.
+ warnings.filters[:] = fs
+ copyreg.dispatch_table.clear()
+ copyreg.dispatch_table.update(ps)
+ sys.path_importer_cache.clear()
+ sys.path_importer_cache.update(pic)
+ try:
+ import zipimport
+ except ImportError:
+ pass # Run unmodified on platforms without zipimport support
+ else:
+ zipimport._zip_directory_cache.clear()
+ zipimport._zip_directory_cache.update(zdc)
+
+ # clear type cache
+ sys._clear_type_cache()
+
+ # Clear ABC registries, restoring previously saved ABC registries.
+ for abc in [getattr(collections.abc, a) for a in collections.abc.__all__]:
+ if not isabstract(abc):
+ continue
+ for obj in abc.__subclasses__() + [abc]:
+ obj._abc_registry = abcs.get(obj, WeakSet()).copy()
+ obj._abc_cache.clear()
+ obj._abc_negative_cache.clear()
+
+ clear_caches()
+
+ # Collect cyclic trash and read memory statistics immediately after.
+ func1 = sys.getallocatedblocks
+ func2 = sys.gettotalrefcount
+ gc.collect()
+ return func1(), func2(), fd_count()
+
+
+def clear_caches():
+ import gc
+
+ # Clear the warnings registry, so they can be displayed again
+ for mod in sys.modules.values():
+ if hasattr(mod, '__warningregistry__'):
+ del mod.__warningregistry__
+
+ # Flush standard output, so that buffered data is sent to the OS and
+ # associated Python objects are reclaimed.
+ for stream in (sys.stdout, sys.stderr, sys.__stdout__, sys.__stderr__):
+ if stream is not None:
+ stream.flush()
+
+ # Clear assorted module caches.
+ # Don't worry about resetting the cache if the module is not loaded
+ try:
+ distutils_dir_util = sys.modules['distutils.dir_util']
+ except KeyError:
+ pass
+ else:
+ distutils_dir_util._path_created.clear()
+ re.purge()
+
+ try:
+ _strptime = sys.modules['_strptime']
+ except KeyError:
+ pass
+ else:
+ _strptime._regex_cache.clear()
+
+ try:
+ urllib_parse = sys.modules['urllib.parse']
+ except KeyError:
+ pass
+ else:
+ urllib_parse.clear_cache()
+
+ try:
+ urllib_request = sys.modules['urllib.request']
+ except KeyError:
+ pass
+ else:
+ urllib_request.urlcleanup()
+
+ try:
+ linecache = sys.modules['linecache']
+ except KeyError:
+ pass
+ else:
+ linecache.clearcache()
+
+ try:
+ mimetypes = sys.modules['mimetypes']
+ except KeyError:
+ pass
+ else:
+ mimetypes._default_mime_types()
+
+ try:
+ filecmp = sys.modules['filecmp']
+ except KeyError:
+ pass
+ else:
+ filecmp._cache.clear()
+
+ try:
+ struct = sys.modules['struct']
+ except KeyError:
+ pass
+ else:
+ struct._clearcache()
+
+ try:
+ doctest = sys.modules['doctest']
+ except KeyError:
+ pass
+ else:
+ doctest.master = None
+
+ try:
+ ctypes = sys.modules['ctypes']
+ except KeyError:
+ pass
+ else:
+ ctypes._reset_cache()
+
+ try:
+ typing = sys.modules['typing']
+ except KeyError:
+ pass
+ else:
+ for f in typing._cleanups:
+ f()
+
+ gc.collect()
+
+
+def warm_caches():
+ # char cache
+ s = bytes(range(256))
+ for i in range(256):
+ s[i:i+1]
+ # unicode cache
+ [chr(i) for i in range(256)]
+ # int cache
+ list(range(-5, 257))
diff --git a/Lib/test/libregrtest/runtest.py b/Lib/test/libregrtest/runtest.py
new file mode 100644
index 0000000..ba0df0a
--- /dev/null
+++ b/Lib/test/libregrtest/runtest.py
@@ -0,0 +1,245 @@
+import faulthandler
+import importlib
+import io
+import os
+import sys
+import time
+import traceback
+import unittest
+from test import support
+from test.libregrtest.refleak import dash_R, clear_caches
+from test.libregrtest.save_env import saved_test_environment
+
+
+# Test result constants.
+PASSED = 1
+FAILED = 0
+ENV_CHANGED = -1
+SKIPPED = -2
+RESOURCE_DENIED = -3
+INTERRUPTED = -4
+CHILD_ERROR = -5 # error in a child process
+
+_FORMAT_TEST_RESULT = {
+ PASSED: '%s passed',
+ FAILED: '%s failed',
+ ENV_CHANGED: '%s failed (env changed)',
+ SKIPPED: '%s skipped',
+ RESOURCE_DENIED: '%s skipped (resource denied)',
+ INTERRUPTED: '%s interrupted',
+ CHILD_ERROR: '%s crashed',
+}
+
+# Minimum duration of a test to display its duration or to mention that
+# the test is running in background
+PROGRESS_MIN_TIME = 30.0 # seconds
+
+# small set of tests to determine if we have a basically functioning interpreter
+# (i.e. if any of these fail, then anything else is likely to follow)
+STDTESTS = [
+ 'test_grammar',
+ 'test_opcodes',
+ 'test_dict',
+ 'test_builtin',
+ 'test_exceptions',
+ 'test_types',
+ 'test_unittest',
+ 'test_doctest',
+ 'test_doctest2',
+ 'test_support'
+]
+
+# set of tests that we don't want to be executed when using regrtest
+NOTTESTS = set()
+
+
+def format_test_result(test_name, result):
+ fmt = _FORMAT_TEST_RESULT.get(result, "%s")
+ return fmt % test_name
+
+
+def findtests(testdir=None, stdtests=STDTESTS, nottests=NOTTESTS):
+ """Return a list of all applicable test modules."""
+ testdir = findtestdir(testdir)
+ names = os.listdir(testdir)
+ tests = []
+ others = set(stdtests) | nottests
+ for name in names:
+ mod, ext = os.path.splitext(name)
+ if mod[:5] == "test_" and ext in (".py", "") and mod not in others:
+ tests.append(mod)
+ return stdtests + sorted(tests)
+
+
+def runtest(ns, test):
+ """Run a single test.
+
+ ns -- regrtest namespace of options
+ test -- the name of the test
+
+ Returns the tuple (result, test_time), where result is one of the
+ constants:
+
+ INTERRUPTED KeyboardInterrupt when run under -j
+ RESOURCE_DENIED test skipped because resource denied
+ SKIPPED test skipped for some other reason
+ ENV_CHANGED test failed because it changed the execution environment
+ FAILED test failed
+ PASSED test passed
+ """
+
+ output_on_failure = ns.verbose3
+
+ use_timeout = (ns.timeout is not None)
+ if use_timeout:
+ faulthandler.dump_traceback_later(ns.timeout, exit=True)
+ try:
+ support.match_tests = ns.match_tests
+ if ns.failfast:
+ support.failfast = True
+ if output_on_failure:
+ support.verbose = True
+
+ # Reuse the same instance to all calls to runtest(). Some
+ # tests keep a reference to sys.stdout or sys.stderr
+ # (eg. test_argparse).
+ if runtest.stringio is None:
+ stream = io.StringIO()
+ runtest.stringio = stream
+ else:
+ stream = runtest.stringio
+ stream.seek(0)
+ stream.truncate()
+
+ orig_stdout = sys.stdout
+ orig_stderr = sys.stderr
+ try:
+ sys.stdout = stream
+ sys.stderr = stream
+ result = runtest_inner(ns, test, display_failure=False)
+ if result[0] != PASSED:
+ output = stream.getvalue()
+ orig_stderr.write(output)
+ orig_stderr.flush()
+ finally:
+ sys.stdout = orig_stdout
+ sys.stderr = orig_stderr
+ else:
+ support.verbose = ns.verbose # Tell tests to be moderately quiet
+ result = runtest_inner(ns, test, display_failure=not ns.verbose)
+ return result
+ finally:
+ if use_timeout:
+ faulthandler.cancel_dump_traceback_later()
+ cleanup_test_droppings(test, ns.verbose)
+runtest.stringio = None
+
+
+def runtest_inner(ns, test, display_failure=True):
+ support.unload(test)
+
+ test_time = 0.0
+ refleak = False # True if the test leaked references.
+ try:
+ if test.startswith('test.') or ns.testdir:
+ abstest = test
+ else:
+ # Always import it from the test package
+ abstest = 'test.' + test
+ clear_caches()
+ with saved_test_environment(test, ns.verbose, ns.quiet, pgo=ns.pgo) as environment:
+ start_time = time.time()
+ the_module = importlib.import_module(abstest)
+ # If the test has a test_main, that will run the appropriate
+ # tests. If not, use normal unittest test loading.
+ test_runner = getattr(the_module, "test_main", None)
+ if test_runner is None:
+ def test_runner():
+ loader = unittest.TestLoader()
+ tests = loader.loadTestsFromModule(the_module)
+ for error in loader.errors:
+ print(error, file=sys.stderr)
+ if loader.errors:
+ raise Exception("errors while loading tests")
+ support.run_unittest(tests)
+ test_runner()
+ if ns.huntrleaks:
+ refleak = dash_R(the_module, test, test_runner, ns.huntrleaks)
+ test_time = time.time() - start_time
+ except support.ResourceDenied as msg:
+ if not ns.quiet and not ns.pgo:
+ print(test, "skipped --", msg, flush=True)
+ return RESOURCE_DENIED, test_time
+ except unittest.SkipTest as msg:
+ if not ns.quiet and not ns.pgo:
+ print(test, "skipped --", msg, flush=True)
+ return SKIPPED, test_time
+ except KeyboardInterrupt:
+ raise
+ except support.TestFailed as msg:
+ if not ns.pgo:
+ if display_failure:
+ print("test", test, "failed --", msg, file=sys.stderr,
+ flush=True)
+ else:
+ print("test", test, "failed", file=sys.stderr, flush=True)
+ return FAILED, test_time
+ except:
+ msg = traceback.format_exc()
+ if not ns.pgo:
+ print("test", test, "crashed --", msg, file=sys.stderr,
+ flush=True)
+ return FAILED, test_time
+ else:
+ if refleak:
+ return FAILED, test_time
+ if environment.changed:
+ return ENV_CHANGED, test_time
+ return PASSED, test_time
+
+
+def cleanup_test_droppings(testname, verbose):
+ import shutil
+ import stat
+ import gc
+
+ # First kill any dangling references to open files etc.
+ # This can also issue some ResourceWarnings which would otherwise get
+ # triggered during the following test run, and possibly produce failures.
+ gc.collect()
+
+ # Try to clean up junk commonly left behind. While tests shouldn't leave
+ # any files or directories behind, when a test fails that can be tedious
+ # for it to arrange. The consequences can be especially nasty on Windows,
+ # since if a test leaves a file open, it cannot be deleted by name (while
+ # there's nothing we can do about that here either, we can display the
+ # name of the offending test, which is a real help).
+ for name in (support.TESTFN,
+ "db_home",
+ ):
+ if not os.path.exists(name):
+ continue
+
+ if os.path.isdir(name):
+ kind, nuker = "directory", shutil.rmtree
+ elif os.path.isfile(name):
+ kind, nuker = "file", os.unlink
+ else:
+ raise SystemError("os.path says %r exists but is neither "
+ "directory nor file" % name)
+
+ if verbose:
+ print("%r left behind %s %r" % (testname, kind, name))
+ try:
+ # if we have chmod, fix possible permissions problems
+ # that might prevent cleanup
+ if (hasattr(os, 'chmod')):
+ os.chmod(name, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
+ nuker(name)
+ except Exception as msg:
+ print(("%r left behind %s %r and it couldn't be "
+ "removed: %s" % (testname, kind, name, msg)), file=sys.stderr)
+
+
+def findtestdir(path=None):
+ return path or os.path.dirname(os.path.dirname(__file__)) or os.curdir
diff --git a/Lib/test/libregrtest/runtest_mp.py b/Lib/test/libregrtest/runtest_mp.py
new file mode 100644
index 0000000..9604c16
--- /dev/null
+++ b/Lib/test/libregrtest/runtest_mp.py
@@ -0,0 +1,245 @@
+import faulthandler
+import json
+import os
+import queue
+import sys
+import time
+import traceback
+import types
+from test import support
+try:
+ import threading
+except ImportError:
+ print("Multiprocess option requires thread support")
+ sys.exit(2)
+
+from test.libregrtest.runtest import (
+ runtest, INTERRUPTED, CHILD_ERROR, PROGRESS_MIN_TIME,
+ format_test_result)
+from test.libregrtest.setup import setup_tests
+
+
+# Display the running tests if nothing happened last N seconds
+PROGRESS_UPDATE = 30.0 # seconds
+
+# If interrupted, display the wait progress every N seconds
+WAIT_PROGRESS = 2.0 # seconds
+
+
+def run_test_in_subprocess(testname, ns):
+ """Run the given test in a subprocess with --slaveargs.
+
+ ns is the option Namespace parsed from command-line arguments. regrtest
+ is invoked in a subprocess with the --slaveargs argument; when the
+ subprocess exits, its return code, stdout and stderr are returned as a
+ 3-tuple.
+ """
+ from subprocess import Popen, PIPE
+
+ ns_dict = vars(ns)
+ slaveargs = (ns_dict, testname)
+ slaveargs = json.dumps(slaveargs)
+
+ cmd = [sys.executable, *support.args_from_interpreter_flags(),
+ '-X', 'faulthandler',
+ '-m', 'test.regrtest',
+ '--slaveargs', slaveargs]
+ if ns.pgo:
+ cmd += ['--pgo']
+
+ # Running the child from the same working directory as regrtest's original
+ # invocation ensures that TEMPDIR for the child is the same when
+ # sysconfig.is_python_build() is true. See issue 15300.
+ popen = Popen(cmd,
+ stdout=PIPE, stderr=PIPE,
+ universal_newlines=True,
+ close_fds=(os.name != 'nt'),
+ cwd=support.SAVEDCWD)
+ with popen:
+ stdout, stderr = popen.communicate()
+ retcode = popen.wait()
+ return retcode, stdout, stderr
+
+
+def run_tests_slave(slaveargs):
+ ns_dict, testname = json.loads(slaveargs)
+ ns = types.SimpleNamespace(**ns_dict)
+
+ setup_tests(ns)
+
+ try:
+ result = runtest(ns, testname)
+ except KeyboardInterrupt:
+ result = INTERRUPTED, ''
+ except BaseException as e:
+ traceback.print_exc()
+ result = CHILD_ERROR, str(e)
+
+ print() # Force a newline (just in case)
+ print(json.dumps(result), flush=True)
+ sys.exit(0)
+
+
+# We do not use a generator so multiple threads can call next().
+class MultiprocessIterator:
+
+ """A thread-safe iterator over tests for multiprocess mode."""
+
+ def __init__(self, tests):
+ self.interrupted = False
+ self.lock = threading.Lock()
+ self.tests = tests
+
+ def __iter__(self):
+ return self
+
+ def __next__(self):
+ with self.lock:
+ if self.interrupted:
+ raise StopIteration('tests interrupted')
+ return next(self.tests)
+
+
+class MultiprocessThread(threading.Thread):
+ def __init__(self, pending, output, ns):
+ super().__init__()
+ self.pending = pending
+ self.output = output
+ self.ns = ns
+ self.current_test = None
+ self.start_time = None
+
+ def _runtest(self):
+ try:
+ test = next(self.pending)
+ except StopIteration:
+ self.output.put((None, None, None, None))
+ return True
+
+ try:
+ self.start_time = time.monotonic()
+ self.current_test = test
+
+ retcode, stdout, stderr = run_test_in_subprocess(test, self.ns)
+ finally:
+ self.current_test = None
+
+ stdout, _, result = stdout.strip().rpartition("\n")
+ if retcode != 0:
+ result = (CHILD_ERROR, "Exit code %s" % retcode)
+ self.output.put((test, stdout.rstrip(), stderr.rstrip(),
+ result))
+ return True
+
+ if not result:
+ self.output.put((None, None, None, None))
+ return True
+
+ result = json.loads(result)
+ self.output.put((test, stdout.rstrip(), stderr.rstrip(),
+ result))
+ return False
+
+ def run(self):
+ try:
+ stop = False
+ while not stop:
+ stop = self._runtest()
+ except BaseException:
+ self.output.put((None, None, None, None))
+ raise
+
+
+def run_tests_multiprocess(regrtest):
+ output = queue.Queue()
+ pending = MultiprocessIterator(regrtest.tests)
+ test_timeout = regrtest.ns.timeout
+ use_timeout = (test_timeout is not None)
+
+ workers = [MultiprocessThread(pending, output, regrtest.ns)
+ for i in range(regrtest.ns.use_mp)]
+ print("Run tests in parallel using %s child processes"
+ % len(workers))
+ for worker in workers:
+ worker.start()
+
+ def get_running(workers):
+ running = []
+ for worker in workers:
+ current_test = worker.current_test
+ if not current_test:
+ continue
+ dt = time.monotonic() - worker.start_time
+ if dt >= PROGRESS_MIN_TIME:
+ running.append('%s (%.0f sec)' % (current_test, dt))
+ return running
+
+ finished = 0
+ test_index = 1
+ get_timeout = max(PROGRESS_UPDATE, PROGRESS_MIN_TIME)
+ try:
+ while finished < regrtest.ns.use_mp:
+ if use_timeout:
+ faulthandler.dump_traceback_later(test_timeout, exit=True)
+
+ try:
+ item = output.get(timeout=get_timeout)
+ except queue.Empty:
+ running = get_running(workers)
+ if running and not regrtest.ns.pgo:
+ print('running: %s' % ', '.join(running))
+ continue
+
+ test, stdout, stderr, result = item
+ if test is None:
+ finished += 1
+ continue
+ regrtest.accumulate_result(test, result)
+
+ # Display progress
+ ok, test_time = result
+ text = format_test_result(test, ok)
+ if (ok not in (CHILD_ERROR, INTERRUPTED)
+ and test_time >= PROGRESS_MIN_TIME
+ and not regrtest.ns.pgo):
+ text += ' (%.0f sec)' % test_time
+ running = get_running(workers)
+ if running and not regrtest.ns.pgo:
+ text += ' -- running: %s' % ', '.join(running)
+ regrtest.display_progress(test_index, text)
+
+ # Copy stdout and stderr from the child process
+ if stdout:
+ print(stdout, flush=True)
+ if stderr and not regrtest.ns.pgo:
+ print(stderr, file=sys.stderr, flush=True)
+
+ if result[0] == INTERRUPTED:
+ raise KeyboardInterrupt
+ if result[0] == CHILD_ERROR:
+ msg = "Child error on {}: {}".format(test, result[1])
+ raise Exception(msg)
+ test_index += 1
+ except KeyboardInterrupt:
+ regrtest.interrupted = True
+ pending.interrupted = True
+ print()
+ finally:
+ if use_timeout:
+ faulthandler.cancel_dump_traceback_later()
+
+ # If tests are interrupted, wait until tests complete
+ wait_start = time.monotonic()
+ while True:
+ running = [worker.current_test for worker in workers]
+ running = list(filter(bool, running))
+ if not running:
+ break
+
+ dt = time.monotonic() - wait_start
+ line = "Waiting for %s (%s tests)" % (', '.join(running), len(running))
+ if dt >= WAIT_PROGRESS:
+ line = "%s since %.0f sec" % (line, dt)
+ print(line)
+ for worker in workers:
+ worker.join(WAIT_PROGRESS)
diff --git a/Lib/test/libregrtest/save_env.py b/Lib/test/libregrtest/save_env.py
new file mode 100644
index 0000000..96ad3af
--- /dev/null
+++ b/Lib/test/libregrtest/save_env.py
@@ -0,0 +1,285 @@
+import builtins
+import locale
+import logging
+import os
+import shutil
+import sys
+import sysconfig
+import warnings
+from test import support
+try:
+ import threading
+except ImportError:
+ threading = None
+try:
+ import _multiprocessing, multiprocessing.process
+except ImportError:
+ multiprocessing = None
+
+
+# Unit tests are supposed to leave the execution environment unchanged
+# once they complete. But sometimes tests have bugs, especially when
+# tests fail, and the changes to environment go on to mess up other
+# tests. This can cause issues with buildbot stability, since tests
+# are run in random order and so problems may appear to come and go.
+# There are a few things we can save and restore to mitigate this, and
+# the following context manager handles this task.
+
+class saved_test_environment:
+ """Save bits of the test environment and restore them at block exit.
+
+ with saved_test_environment(testname, verbose, quiet):
+ #stuff
+
+ Unless quiet is True, a warning is printed to stderr if any of
+ the saved items was changed by the test. The attribute 'changed'
+ is initially False, but is set to True if a change is detected.
+
+ If verbose is more than 1, the before and after state of changed
+ items is also printed.
+ """
+
+ changed = False
+
+ def __init__(self, testname, verbose=0, quiet=False, *, pgo=False):
+ self.testname = testname
+ self.verbose = verbose
+ self.quiet = quiet
+ self.pgo = pgo
+
+ # To add things to save and restore, add a name XXX to the resources list
+ # and add corresponding get_XXX/restore_XXX functions. get_XXX should
+ # return the value to be saved and compared against a second call to the
+ # get function when test execution completes. restore_XXX should accept
+ # the saved value and restore the resource using it. It will be called if
+ # and only if a change in the value is detected.
+ #
+ # Note: XXX will have any '.' replaced with '_' characters when determining
+ # the corresponding method names.
+
+ resources = ('sys.argv', 'cwd', 'sys.stdin', 'sys.stdout', 'sys.stderr',
+ 'os.environ', 'sys.path', 'sys.path_hooks', '__import__',
+ 'warnings.filters', 'asyncore.socket_map',
+ 'logging._handlers', 'logging._handlerList', 'sys.gettrace',
+ 'sys.warnoptions',
+ # multiprocessing.process._cleanup() may release ref
+ # to a thread, so check processes first.
+ 'multiprocessing.process._dangling', 'threading._dangling',
+ 'sysconfig._CONFIG_VARS', 'sysconfig._INSTALL_SCHEMES',
+ 'files', 'locale', 'warnings.showwarning',
+ 'shutil_archive_formats', 'shutil_unpack_formats',
+ )
+
+ def get_sys_argv(self):
+ return id(sys.argv), sys.argv, sys.argv[:]
+ def restore_sys_argv(self, saved_argv):
+ sys.argv = saved_argv[1]
+ sys.argv[:] = saved_argv[2]
+
+ def get_cwd(self):
+ return os.getcwd()
+ def restore_cwd(self, saved_cwd):
+ os.chdir(saved_cwd)
+
+ def get_sys_stdout(self):
+ return sys.stdout
+ def restore_sys_stdout(self, saved_stdout):
+ sys.stdout = saved_stdout
+
+ def get_sys_stderr(self):
+ return sys.stderr
+ def restore_sys_stderr(self, saved_stderr):
+ sys.stderr = saved_stderr
+
+ def get_sys_stdin(self):
+ return sys.stdin
+ def restore_sys_stdin(self, saved_stdin):
+ sys.stdin = saved_stdin
+
+ def get_os_environ(self):
+ return id(os.environ), os.environ, dict(os.environ)
+ def restore_os_environ(self, saved_environ):
+ os.environ = saved_environ[1]
+ os.environ.clear()
+ os.environ.update(saved_environ[2])
+
+ def get_sys_path(self):
+ return id(sys.path), sys.path, sys.path[:]
+ def restore_sys_path(self, saved_path):
+ sys.path = saved_path[1]
+ sys.path[:] = saved_path[2]
+
+ def get_sys_path_hooks(self):
+ return id(sys.path_hooks), sys.path_hooks, sys.path_hooks[:]
+ def restore_sys_path_hooks(self, saved_hooks):
+ sys.path_hooks = saved_hooks[1]
+ sys.path_hooks[:] = saved_hooks[2]
+
+ def get_sys_gettrace(self):
+ return sys.gettrace()
+ def restore_sys_gettrace(self, trace_fxn):
+ sys.settrace(trace_fxn)
+
+ def get___import__(self):
+ return builtins.__import__
+ def restore___import__(self, import_):
+ builtins.__import__ = import_
+
+ def get_warnings_filters(self):
+ return id(warnings.filters), warnings.filters, warnings.filters[:]
+ def restore_warnings_filters(self, saved_filters):
+ warnings.filters = saved_filters[1]
+ warnings.filters[:] = saved_filters[2]
+
+ def get_asyncore_socket_map(self):
+ asyncore = sys.modules.get('asyncore')
+ # XXX Making a copy keeps objects alive until __exit__ gets called.
+ return asyncore and asyncore.socket_map.copy() or {}
+ def restore_asyncore_socket_map(self, saved_map):
+ asyncore = sys.modules.get('asyncore')
+ if asyncore is not None:
+ asyncore.close_all(ignore_all=True)
+ asyncore.socket_map.update(saved_map)
+
+ def get_shutil_archive_formats(self):
+ # we could call get_archives_formats() but that only returns the
+ # registry keys; we want to check the values too (the functions that
+ # are registered)
+ return shutil._ARCHIVE_FORMATS, shutil._ARCHIVE_FORMATS.copy()
+ def restore_shutil_archive_formats(self, saved):
+ shutil._ARCHIVE_FORMATS = saved[0]
+ shutil._ARCHIVE_FORMATS.clear()
+ shutil._ARCHIVE_FORMATS.update(saved[1])
+
+ def get_shutil_unpack_formats(self):
+ return shutil._UNPACK_FORMATS, shutil._UNPACK_FORMATS.copy()
+ def restore_shutil_unpack_formats(self, saved):
+ shutil._UNPACK_FORMATS = saved[0]
+ shutil._UNPACK_FORMATS.clear()
+ shutil._UNPACK_FORMATS.update(saved[1])
+
+ def get_logging__handlers(self):
+ # _handlers is a WeakValueDictionary
+ return id(logging._handlers), logging._handlers, logging._handlers.copy()
+ def restore_logging__handlers(self, saved_handlers):
+ # Can't easily revert the logging state
+ pass
+
+ def get_logging__handlerList(self):
+ # _handlerList is a list of weakrefs to handlers
+ return id(logging._handlerList), logging._handlerList, logging._handlerList[:]
+ def restore_logging__handlerList(self, saved_handlerList):
+ # Can't easily revert the logging state
+ pass
+
+ def get_sys_warnoptions(self):
+ return id(sys.warnoptions), sys.warnoptions, sys.warnoptions[:]
+ def restore_sys_warnoptions(self, saved_options):
+ sys.warnoptions = saved_options[1]
+ sys.warnoptions[:] = saved_options[2]
+
+ # Controlling dangling references to Thread objects can make it easier
+ # to track reference leaks.
+ def get_threading__dangling(self):
+ if not threading:
+ return None
+ # This copies the weakrefs without making any strong reference
+ return threading._dangling.copy()
+ def restore_threading__dangling(self, saved):
+ if not threading:
+ return
+ threading._dangling.clear()
+ threading._dangling.update(saved)
+
+ # Same for Process objects
+ def get_multiprocessing_process__dangling(self):
+ if not multiprocessing:
+ return None
+ # Unjoined process objects can survive after process exits
+ multiprocessing.process._cleanup()
+ # This copies the weakrefs without making any strong reference
+ return multiprocessing.process._dangling.copy()
+ def restore_multiprocessing_process__dangling(self, saved):
+ if not multiprocessing:
+ return
+ multiprocessing.process._dangling.clear()
+ multiprocessing.process._dangling.update(saved)
+
+ def get_sysconfig__CONFIG_VARS(self):
+ # make sure the dict is initialized
+ sysconfig.get_config_var('prefix')
+ return (id(sysconfig._CONFIG_VARS), sysconfig._CONFIG_VARS,
+ dict(sysconfig._CONFIG_VARS))
+ def restore_sysconfig__CONFIG_VARS(self, saved):
+ sysconfig._CONFIG_VARS = saved[1]
+ sysconfig._CONFIG_VARS.clear()
+ sysconfig._CONFIG_VARS.update(saved[2])
+
+ def get_sysconfig__INSTALL_SCHEMES(self):
+ return (id(sysconfig._INSTALL_SCHEMES), sysconfig._INSTALL_SCHEMES,
+ sysconfig._INSTALL_SCHEMES.copy())
+ def restore_sysconfig__INSTALL_SCHEMES(self, saved):
+ sysconfig._INSTALL_SCHEMES = saved[1]
+ sysconfig._INSTALL_SCHEMES.clear()
+ sysconfig._INSTALL_SCHEMES.update(saved[2])
+
+ def get_files(self):
+ return sorted(fn + ('/' if os.path.isdir(fn) else '')
+ for fn in os.listdir())
+ def restore_files(self, saved_value):
+ fn = support.TESTFN
+ if fn not in saved_value and (fn + '/') not in saved_value:
+ if os.path.isfile(fn):
+ support.unlink(fn)
+ elif os.path.isdir(fn):
+ support.rmtree(fn)
+
+ _lc = [getattr(locale, lc) for lc in dir(locale)
+ if lc.startswith('LC_')]
+ def get_locale(self):
+ pairings = []
+ for lc in self._lc:
+ try:
+ pairings.append((lc, locale.setlocale(lc, None)))
+ except (TypeError, ValueError):
+ continue
+ return pairings
+ def restore_locale(self, saved):
+ for lc, setting in saved:
+ locale.setlocale(lc, setting)
+
+ def get_warnings_showwarning(self):
+ return warnings.showwarning
+ def restore_warnings_showwarning(self, fxn):
+ warnings.showwarning = fxn
+
+ def resource_info(self):
+ for name in self.resources:
+ method_suffix = name.replace('.', '_')
+ get_name = 'get_' + method_suffix
+ restore_name = 'restore_' + method_suffix
+ yield name, getattr(self, get_name), getattr(self, restore_name)
+
+ def __enter__(self):
+ self.saved_values = dict((name, get()) for name, get, restore
+ in self.resource_info())
+ return self
+
+ def __exit__(self, exc_type, exc_val, exc_tb):
+ saved_values = self.saved_values
+ del self.saved_values
+ support.gc_collect() # Some resources use weak references
+ for name, get, restore in self.resource_info():
+ current = get()
+ original = saved_values.pop(name)
+ # Check for changes to the resource's value
+ if current != original:
+ self.changed = True
+ restore(original)
+ if not self.quiet and not self.pgo:
+ print(f"Warning -- {name} was modified by {self.testname}",
+ file=sys.stderr, flush=True)
+ if self.verbose > 1:
+ print(f" Before: {original}\n After: {current} ",
+ file=sys.stderr, flush=True)
+ return False
diff --git a/Lib/test/libregrtest/setup.py b/Lib/test/libregrtest/setup.py
new file mode 100644
index 0000000..1d24531
--- /dev/null
+++ b/Lib/test/libregrtest/setup.py
@@ -0,0 +1,121 @@
+import atexit
+import faulthandler
+import os
+import signal
+import sys
+import unittest
+from test import support
+try:
+ import gc
+except ImportError:
+ gc = None
+
+from test.libregrtest.refleak import warm_caches
+
+
+def setup_tests(ns):
+ # Display the Python traceback on fatal errors (e.g. segfault)
+ faulthandler.enable(all_threads=True)
+
+ # Display the Python traceback on SIGALRM or SIGUSR1 signal
+ signals = []
+ if hasattr(signal, 'SIGALRM'):
+ signals.append(signal.SIGALRM)
+ if hasattr(signal, 'SIGUSR1'):
+ signals.append(signal.SIGUSR1)
+ for signum in signals:
+ faulthandler.register(signum, chain=True)
+
+ replace_stdout()
+ support.record_original_stdout(sys.stdout)
+
+ if ns.testdir:
+ # Prepend test directory to sys.path, so runtest() will be able
+ # to locate tests
+ sys.path.insert(0, os.path.abspath(ns.testdir))
+
+ # Some times __path__ and __file__ are not absolute (e.g. while running from
+ # Lib/) and, if we change the CWD to run the tests in a temporary dir, some
+ # imports might fail. This affects only the modules imported before os.chdir().
+ # These modules are searched first in sys.path[0] (so '' -- the CWD) and if
+ # they are found in the CWD their __file__ and __path__ will be relative (this
+ # happens before the chdir). All the modules imported after the chdir, are
+ # not found in the CWD, and since the other paths in sys.path[1:] are absolute
+ # (site.py absolutize them), the __file__ and __path__ will be absolute too.
+ # Therefore it is necessary to absolutize manually the __file__ and __path__ of
+ # the packages to prevent later imports to fail when the CWD is different.
+ for module in sys.modules.values():
+ if hasattr(module, '__path__'):
+ for index, path in enumerate(module.__path__):
+ module.__path__[index] = os.path.abspath(path)
+ if hasattr(module, '__file__'):
+ module.__file__ = os.path.abspath(module.__file__)
+
+ # MacOSX (a.k.a. Darwin) has a default stack size that is too small
+ # for deeply recursive regular expressions. We see this as crashes in
+ # the Python test suite when running test_re.py and test_sre.py. The
+ # fix is to set the stack limit to 2048.
+ # This approach may also be useful for other Unixy platforms that
+ # suffer from small default stack limits.
+ if sys.platform == 'darwin':
+ try:
+ import resource
+ except ImportError:
+ pass
+ else:
+ soft, hard = resource.getrlimit(resource.RLIMIT_STACK)
+ newsoft = min(hard, max(soft, 1024*2048))
+ resource.setrlimit(resource.RLIMIT_STACK, (newsoft, hard))
+
+ if ns.huntrleaks:
+ unittest.BaseTestSuite._cleanup = False
+
+ # Avoid false positives due to various caches
+ # filling slowly with random data:
+ warm_caches()
+
+ if ns.memlimit is not None:
+ support.set_memlimit(ns.memlimit)
+
+ if ns.threshold is not None:
+ gc.set_threshold(ns.threshold)
+
+ try:
+ import msvcrt
+ except ImportError:
+ pass
+ else:
+ msvcrt.SetErrorMode(msvcrt.SEM_FAILCRITICALERRORS|
+ msvcrt.SEM_NOALIGNMENTFAULTEXCEPT|
+ msvcrt.SEM_NOGPFAULTERRORBOX|
+ msvcrt.SEM_NOOPENFILEERRORBOX)
+ try:
+ msvcrt.CrtSetReportMode
+ except AttributeError:
+ # release build
+ pass
+ else:
+ for m in [msvcrt.CRT_WARN, msvcrt.CRT_ERROR, msvcrt.CRT_ASSERT]:
+ if ns.verbose and ns.verbose >= 2:
+ msvcrt.CrtSetReportMode(m, msvcrt.CRTDBG_MODE_FILE)
+ msvcrt.CrtSetReportFile(m, msvcrt.CRTDBG_FILE_STDERR)
+ else:
+ msvcrt.CrtSetReportMode(m, 0)
+
+ support.use_resources = ns.use_resources
+
+
+def replace_stdout():
+ """Set stdout encoder error handler to backslashreplace (as stderr error
+ handler) to avoid UnicodeEncodeError when printing a traceback"""
+ stdout = sys.stdout
+ sys.stdout = open(stdout.fileno(), 'w',
+ encoding=stdout.encoding,
+ errors="backslashreplace",
+ closefd=False,
+ newline='\n')
+
+ def restore_stdout():
+ sys.stdout.close()
+ sys.stdout = stdout
+ atexit.register(restore_stdout)
diff --git a/Lib/test/list_tests.py b/Lib/test/list_tests.py
index f20fdc0..26e9368 100644
--- a/Lib/test/list_tests.py
+++ b/Lib/test/list_tests.py
@@ -266,9 +266,21 @@ class CommonTest(seq_tests.CommonTest):
self.assertEqual(a, list("spameggs"))
self.assertRaises(TypeError, a.extend, None)
-
self.assertRaises(TypeError, a.extend)
+ # overflow test. issue1621
+ class CustomIter:
+ def __iter__(self):
+ return self
+ def __next__(self):
+ raise StopIteration
+ def __length_hint__(self):
+ return sys.maxsize
+ a = self.type2test([1,2,3,4])
+ a.extend(CustomIter())
+ self.assertEqual(a, [1,2,3,4])
+
+
def test_insert(self):
a = self.type2test([0, 1, 2])
a.insert(0, -2)
diff --git a/Lib/test/lock_tests.py b/Lib/test/lock_tests.py
index a64aa18..a6cb3b1 100644
--- a/Lib/test/lock_tests.py
+++ b/Lib/test/lock_tests.py
@@ -7,6 +7,7 @@ import time
from _thread import start_new_thread, TIMEOUT_MAX
import threading
import unittest
+import weakref
from test import support
@@ -198,6 +199,17 @@ class BaseLockTests(BaseTestCase):
self.assertFalse(results[0])
self.assertTimeout(results[1], 0.5)
+ def test_weakref_exists(self):
+ lock = self.locktype()
+ ref = weakref.ref(lock)
+ self.assertIsNotNone(ref())
+
+ def test_weakref_deleted(self):
+ lock = self.locktype()
+ ref = weakref.ref(lock)
+ del lock
+ self.assertIsNone(ref())
+
class LockTests(BaseLockTests):
"""
diff --git a/Lib/test/make_ssl_certs.py b/Lib/test/make_ssl_certs.py
index a1f298d..4d9f01b 100644
--- a/Lib/test/make_ssl_certs.py
+++ b/Lib/test/make_ssl_certs.py
@@ -3,7 +3,6 @@ and friends."""
import os
import shutil
-import sys
import tempfile
from subprocess import *
diff --git a/Lib/test/pickletester.py b/Lib/test/pickletester.py
index 7922b54..5c83361 100644
--- a/Lib/test/pickletester.py
+++ b/Lib/test/pickletester.py
@@ -1000,7 +1000,7 @@ class AbstractUnpickleTests(unittest.TestCase):
b'0', # POP
b'1', # POP_MARK
b'2', # DUP
- # b'(2', # PyUnpickler doesn't raise
+ b'(2',
b'R', # REDUCE
b')R',
b'a', # APPEND
@@ -1009,7 +1009,7 @@ class AbstractUnpickleTests(unittest.TestCase):
b'Nb',
b'd', # DICT
b'e', # APPENDS
- # b'(e', # PyUnpickler raises AttributeError
+ b'(e',
b'ibuiltins\nlist\n', # INST
b'l', # LIST
b'o', # OBJ
@@ -1022,7 +1022,7 @@ class AbstractUnpickleTests(unittest.TestCase):
b'NNs',
b't', # TUPLE
b'u', # SETITEMS
- # b'(u', # PyUnpickler doesn't raise
+ b'(u',
b'}(Nu',
b'\x81', # NEWOBJ
b')\x81',
@@ -1033,7 +1033,7 @@ class AbstractUnpickleTests(unittest.TestCase):
b'N\x87',
b'NN\x87',
b'\x90', # ADDITEMS
- # b'(\x90', # PyUnpickler raises AttributeError
+ b'(\x90',
b'\x91', # FROZENSET
b'\x92', # NEWOBJ_EX
b')}\x92',
@@ -1046,7 +1046,7 @@ class AbstractUnpickleTests(unittest.TestCase):
def test_bad_mark(self):
badpickles = [
- # b'N(.', # STOP
+ b'N(.', # STOP
b'N(2', # DUP
b'cbuiltins\nlist\n)(R', # REDUCE
b'cbuiltins\nlist\n()R',
@@ -1081,7 +1081,7 @@ class AbstractUnpickleTests(unittest.TestCase):
b'N(\x94', # MEMOIZE
]
for p in badpickles:
- self.check_unpickling_error(self.bad_mark_errors, p)
+ self.check_unpickling_error(self.bad_stack_errors, p)
def test_truncated_data(self):
self.check_unpickling_error(EOFError, b'')
@@ -1855,16 +1855,14 @@ class AbstractPickleTests(unittest.TestCase):
x.abc = 666
for proto in protocols:
with self.subTest(proto=proto):
- if 2 <= proto < 4:
- self.assertRaises(ValueError, self.dumps, x, proto)
- continue
s = self.dumps(x, proto)
if proto < 1:
self.assertIn(b'\nL64206', s) # LONG
elif proto < 2:
self.assertIn(b'M\xce\xfa', s) # BININT2
+ elif proto < 4:
+ self.assertIn(b'X\x04\x00\x00\x00FACE', s) # BINUNICODE
else:
- assert proto >= 4
self.assertIn(b'\x8c\x04FACE', s) # SHORT_BINUNICODE
self.assertFalse(opcode_in_pickle(pickle.NEWOBJ, s))
self.assertEqual(opcode_in_pickle(pickle.NEWOBJ_EX, s),
@@ -2583,11 +2581,6 @@ class AbstractPickleModuleTests(unittest.TestCase):
self.assertRaises(pickle.PicklingError, BadPickler().dump, 0)
self.assertRaises(pickle.UnpicklingError, BadUnpickler().load)
- def test_bad_input(self):
- # Test issue4298
- s = bytes([0x58, 0, 0, 0, 0x54])
- self.assertRaises(EOFError, pickle.loads, s)
-
class AbstractPersistentPicklerTests(unittest.TestCase):
diff --git a/Lib/test/pydoc_mod.py b/Lib/test/pydoc_mod.py
index cda1c9e..9c1fff5 100644
--- a/Lib/test/pydoc_mod.py
+++ b/Lib/test/pydoc_mod.py
@@ -12,7 +12,7 @@ class A:
pass
class B(object):
- NO_MEANING = "eggs"
+ NO_MEANING: str = "eggs"
pass
class C(object):
diff --git a/Lib/test/re_tests.py b/Lib/test/re_tests.py
index 8c158f8..a379d33 100755
--- a/Lib/test/re_tests.py
+++ b/Lib/test/re_tests.py
@@ -106,8 +106,8 @@ tests = [
('a.*b', 'acc\nccb', FAIL),
('a.{4,5}b', 'acc\nccb', FAIL),
('a.b', 'a\rb', SUCCEED, 'found', 'a\rb'),
- ('a.b(?s)', 'a\nb', SUCCEED, 'found', 'a\nb'),
- ('a.*(?s)b', 'acc\nccb', SUCCEED, 'found', 'acc\nccb'),
+ ('(?s)a.b', 'a\nb', SUCCEED, 'found', 'a\nb'),
+ ('(?s)a.*b', 'acc\nccb', SUCCEED, 'found', 'acc\nccb'),
('(?s)a.{4,5}b', 'acc\nccb', SUCCEED, 'found', 'acc\nccb'),
('(?s)a.b', 'a\nb', SUCCEED, 'found', 'a\nb'),
@@ -158,7 +158,7 @@ tests = [
('(abc', '-', SYNTAX_ERROR),
('a]', 'a]', SUCCEED, 'found', 'a]'),
('a[]]b', 'a]b', SUCCEED, 'found', 'a]b'),
- ('a[\]]b', 'a]b', SUCCEED, 'found', 'a]b'),
+ ('a[\\]]b', 'a]b', SUCCEED, 'found', 'a]b'),
('a[^bc]d', 'aed', SUCCEED, 'found', 'aed'),
('a[^bc]d', 'abd', FAIL),
('a[^-b]c', 'adc', SUCCEED, 'found', 'adc'),
@@ -551,7 +551,7 @@ tests = [
# lookbehind: split by : but not if it is escaped by -.
('(?<!-):(.*?)(?<!-):', 'a:bc-:de:f', SUCCEED, 'g1', 'bc-:de' ),
# escaping with \ as we know it
- ('(?<!\\\):(.*?)(?<!\\\):', 'a:bc\\:de:f', SUCCEED, 'g1', 'bc\\:de' ),
+ ('(?<!\\\\):(.*?)(?<!\\\\):', 'a:bc\\:de:f', SUCCEED, 'g1', 'bc\\:de' ),
# terminating with ' and escaping with ? as in edifact
("(?<!\\?)'(.*?)(?<!\\?)'", "a'bc?'de'f", SUCCEED, 'g1', "bc?'de" ),
@@ -563,7 +563,7 @@ tests = [
# Check odd placement of embedded pattern modifiers
# not an error under PCRE/PRE:
- ('w(?i)', 'W', SUCCEED, 'found', 'W'),
+ ('(?i)w', 'W', SUCCEED, 'found', 'W'),
# ('w(?i)', 'W', SYNTAX_ERROR),
# Comments using the x embedded pattern modifier
@@ -627,7 +627,7 @@ xyzabc
# bug 114033: nothing to repeat
(r'(x?)?', 'x', SUCCEED, 'found', 'x'),
# bug 115040: rescan if flags are modified inside pattern
- (r' (?x)foo ', 'foo', SUCCEED, 'found', 'foo'),
+ (r'(?x) foo ', 'foo', SUCCEED, 'found', 'foo'),
# bug 115618: negative lookahead
(r'(?<!abc)(d.f)', 'abcdefdof', SUCCEED, 'found', 'dof'),
# bug 116251: character class bug
diff --git a/Lib/test/regrtest.py b/Lib/test/regrtest.py
index c1d85f6..21b0edf 100755
--- a/Lib/test/regrtest.py
+++ b/Lib/test/regrtest.py
@@ -6,1670 +6,33 @@ Script to run Python regression tests.
Run this script with -h or --help for documentation.
"""
-USAGE = """\
-python -m test [options] [test_name1 [test_name2 ...]]
-python path/to/Lib/test/regrtest.py [options] [test_name1 [test_name2 ...]]
-"""
-
-DESCRIPTION = """\
-Run Python regression tests.
-
-If no arguments or options are provided, finds all files matching
-the pattern "test_*" in the Lib/test subdirectory and runs
-them in alphabetical order (but see -M and -u, below, for exceptions).
-
-For more rigorous testing, it is useful to use the following
-command line:
-
-python -E -Wd -m test [options] [test_name1 ...]
-"""
-
-EPILOG = """\
-Additional option details:
-
--r randomizes test execution order. You can use --randseed=int to provide an
-int seed value for the randomizer; this is useful for reproducing troublesome
-test orders.
-
--s On the first invocation of regrtest using -s, the first test file found
-or the first test file given on the command line is run, and the name of
-the next test is recorded in a file named pynexttest. If run from the
-Python build directory, pynexttest is located in the 'build' subdirectory,
-otherwise it is located in tempfile.gettempdir(). On subsequent runs,
-the test in pynexttest is run, and the next test is written to pynexttest.
-When the last test has been run, pynexttest is deleted. In this way it
-is possible to single step through the test files. This is useful when
-doing memory analysis on the Python interpreter, which process tends to
-consume too many resources to run the full regression test non-stop.
-
--S is used to continue running tests after an aborted run. It will
-maintain the order a standard run (ie, this assumes -r is not used).
-This is useful after the tests have prematurely stopped for some external
-reason and you want to start running from where you left off rather
-than starting from the beginning.
-
--f reads the names of tests from the file given as f's argument, one
-or more test names per line. Whitespace is ignored. Blank lines and
-lines beginning with '#' are ignored. This is especially useful for
-whittling down failures involving interactions among tests.
-
--L causes the leaks(1) command to be run just before exit if it exists.
-leaks(1) is available on Mac OS X and presumably on some other
-FreeBSD-derived systems.
-
--R runs each test several times and examines sys.gettotalrefcount() to
-see if the test appears to be leaking references. The argument should
-be of the form stab:run:fname where 'stab' is the number of times the
-test is run to let gettotalrefcount settle down, 'run' is the number
-of times further it is run and 'fname' is the name of the file the
-reports are written to. These parameters all have defaults (5, 4 and
-"reflog.txt" respectively), and the minimal invocation is '-R :'.
-
--M runs tests that require an exorbitant amount of memory. These tests
-typically try to ascertain containers keep working when containing more than
-2 billion objects, which only works on 64-bit systems. There are also some
-tests that try to exhaust the address space of the process, which only makes
-sense on 32-bit systems with at least 2Gb of memory. The passed-in memlimit,
-which is a string in the form of '2.5Gb', determines howmuch memory the
-tests will limit themselves to (but they may go slightly over.) The number
-shouldn't be more memory than the machine has (including swap memory). You
-should also keep in mind that swap memory is generally much, much slower
-than RAM, and setting memlimit to all available RAM or higher will heavily
-tax the machine. On the other hand, it is no use running these tests with a
-limit of less than 2.5Gb, and many require more than 20Gb. Tests that expect
-to use more than memlimit memory will be skipped. The big-memory tests
-generally run very, very long.
-
--u is used to specify which special resource intensive tests to run,
-such as those requiring large file support or network connectivity.
-The argument is a comma-separated list of words indicating the
-resources to test. Currently only the following are defined:
-
- all - Enable all special resources.
-
- none - Disable all special resources (this is the default).
-
- audio - Tests that use the audio device. (There are known
- cases of broken audio drivers that can crash Python or
- even the Linux kernel.)
-
- curses - Tests that use curses and will modify the terminal's
- state and output modes.
-
- largefile - It is okay to run some test that may create huge
- files. These tests can take a long time and may
- consume >2GB of disk space temporarily.
-
- network - It is okay to run tests that use external network
- resource, e.g. testing SSL support for sockets.
-
- decimal - Test the decimal module against a large suite that
- verifies compliance with standards.
-
- cpu - Used for certain CPU-heavy tests.
-
- subprocess Run all tests for the subprocess module.
-
- urlfetch - It is okay to download files required on testing.
-
- gui - Run tests that require a running GUI.
-
-To enable all resources except one, use '-uall,-<resource>'. For
-example, to run all the tests except for the gui tests, give the
-option '-uall,-gui'.
-"""
-
# We import importlib *ASAP* in order to test #15386
import importlib
-import argparse
-import builtins
-import faulthandler
-import io
-import json
-import locale
-import logging
import os
-import platform
-import random
-import re
-import shutil
-import signal
import sys
-import sysconfig
-import tempfile
-import time
-import traceback
-import unittest
-import warnings
-from inspect import isabstract
-
-try:
- import threading
-except ImportError:
- threading = None
-try:
- import _multiprocessing, multiprocessing.process
-except ImportError:
- multiprocessing = None
-
-
-# Some times __path__ and __file__ are not absolute (e.g. while running from
-# Lib/) and, if we change the CWD to run the tests in a temporary dir, some
-# imports might fail. This affects only the modules imported before os.chdir().
-# These modules are searched first in sys.path[0] (so '' -- the CWD) and if
-# they are found in the CWD their __file__ and __path__ will be relative (this
-# happens before the chdir). All the modules imported after the chdir, are
-# not found in the CWD, and since the other paths in sys.path[1:] are absolute
-# (site.py absolutize them), the __file__ and __path__ will be absolute too.
-# Therefore it is necessary to absolutize manually the __file__ and __path__ of
-# the packages to prevent later imports to fail when the CWD is different.
-for module in sys.modules.values():
- if hasattr(module, '__path__'):
- module.__path__ = [os.path.abspath(path) for path in module.__path__]
- if hasattr(module, '__file__'):
- module.__file__ = os.path.abspath(module.__file__)
-
-
-# MacOSX (a.k.a. Darwin) has a default stack size that is too small
-# for deeply recursive regular expressions. We see this as crashes in
-# the Python test suite when running test_re.py and test_sre.py. The
-# fix is to set the stack limit to 2048.
-# This approach may also be useful for other Unixy platforms that
-# suffer from small default stack limits.
-if sys.platform == 'darwin':
- try:
- import resource
- except ImportError:
- pass
- else:
- soft, hard = resource.getrlimit(resource.RLIMIT_STACK)
- newsoft = min(hard, max(soft, 1024*2048))
- resource.setrlimit(resource.RLIMIT_STACK, (newsoft, hard))
-
-# Test result constants.
-PASSED = 1
-FAILED = 0
-ENV_CHANGED = -1
-SKIPPED = -2
-RESOURCE_DENIED = -3
-INTERRUPTED = -4
-CHILD_ERROR = -5 # error in a child process
-
-from test import support
-
-RESOURCE_NAMES = ('audio', 'curses', 'largefile', 'network',
- 'decimal', 'cpu', 'subprocess', 'urlfetch', 'gui')
-
-# When tests are run from the Python build directory, it is best practice
-# to keep the test files in a subfolder. This eases the cleanup of leftover
-# files using the "make distclean" command.
-if sysconfig.is_python_build():
- TEMPDIR = os.path.join(sysconfig.get_config_var('srcdir'), 'build')
-else:
- TEMPDIR = tempfile.gettempdir()
-TEMPDIR = os.path.abspath(TEMPDIR)
-
-class _ArgParser(argparse.ArgumentParser):
-
- def error(self, message):
- super().error(message + "\nPass -h or --help for complete help.")
-
-def _create_parser():
- # Set prog to prevent the uninformative "__main__.py" from displaying in
- # error messages when using "python -m test ...".
- parser = _ArgParser(prog='regrtest.py',
- usage=USAGE,
- description=DESCRIPTION,
- epilog=EPILOG,
- add_help=False,
- formatter_class=argparse.RawDescriptionHelpFormatter)
-
- # Arguments with this clause added to its help are described further in
- # the epilog's "Additional option details" section.
- more_details = ' See the section at bottom for more details.'
-
- group = parser.add_argument_group('General options')
- # We add help explicitly to control what argument group it renders under.
- group.add_argument('-h', '--help', action='help',
- help='show this help message and exit')
- group.add_argument('--timeout', metavar='TIMEOUT', type=float,
- help='dump the traceback and exit if a test takes '
- 'more than TIMEOUT seconds; disabled if TIMEOUT '
- 'is negative or equals to zero')
- group.add_argument('--wait', action='store_true',
- help='wait for user input, e.g., allow a debugger '
- 'to be attached')
- group.add_argument('--slaveargs', metavar='ARGS')
- group.add_argument('-S', '--start', metavar='START',
- help='the name of the test at which to start.' +
- more_details)
-
- group = parser.add_argument_group('Verbosity')
- group.add_argument('-v', '--verbose', action='count',
- help='run tests in verbose mode with output to stdout')
- group.add_argument('-w', '--verbose2', action='store_true',
- help='re-run failed tests in verbose mode')
- group.add_argument('-W', '--verbose3', action='store_true',
- help='display test output on failure')
- group.add_argument('-q', '--quiet', action='store_true',
- help='no output unless one or more tests fail')
- group.add_argument('-o', '--slow', action='store_true', dest='print_slow',
- help='print the slowest 10 tests')
- group.add_argument('--header', action='store_true',
- help='print header with interpreter info')
-
- group = parser.add_argument_group('Selecting tests')
- group.add_argument('-r', '--randomize', action='store_true',
- help='randomize test execution order.' + more_details)
- group.add_argument('--randseed', metavar='SEED',
- dest='random_seed', type=int,
- help='pass a random seed to reproduce a previous '
- 'random run')
- group.add_argument('-f', '--fromfile', metavar='FILE',
- help='read names of tests to run from a file.' +
- more_details)
- group.add_argument('-x', '--exclude', action='store_true',
- help='arguments are tests to *exclude*')
- group.add_argument('-s', '--single', action='store_true',
- help='single step through a set of tests.' +
- more_details)
- group.add_argument('-m', '--match', metavar='PAT',
- dest='match_tests',
- help='match test cases and methods with glob pattern PAT')
- group.add_argument('-G', '--failfast', action='store_true',
- help='fail as soon as a test fails (only with -v or -W)')
- group.add_argument('-u', '--use', metavar='RES1,RES2,...',
- action='append', type=resources_list,
- help='specify which special resource intensive tests '
- 'to run.' + more_details)
- group.add_argument('-M', '--memlimit', metavar='LIMIT',
- help='run very large memory-consuming tests.' +
- more_details)
- group.add_argument('--testdir', metavar='DIR',
- type=relative_filename,
- help='execute test files in the specified directory '
- '(instead of the Python stdlib test suite)')
-
- group = parser.add_argument_group('Special runs')
- group.add_argument('-l', '--findleaks', action='store_true',
- help='if GC is available detect tests that leak memory')
- group.add_argument('-L', '--runleaks', action='store_true',
- help='run the leaks(1) command just before exit.' +
- more_details)
- group.add_argument('-R', '--huntrleaks', metavar='RUNCOUNTS',
- type=huntrleaks,
- help='search for reference leaks (needs debug build, '
- 'very slow).' + more_details)
- group.add_argument('-j', '--multiprocess', metavar='PROCESSES',
- dest='use_mp', type=int,
- help='run PROCESSES processes at once')
- group.add_argument('-T', '--coverage', action='store_true',
- dest='trace',
- help='turn on code coverage tracing using the trace '
- 'module')
- group.add_argument('-D', '--coverdir', metavar='DIR',
- type=relative_filename,
- help='directory where coverage files are put')
- group.add_argument('-N', '--nocoverdir',
- action='store_const', const=None, dest='coverdir',
- help='put coverage files alongside modules')
- group.add_argument('-t', '--threshold', metavar='THRESHOLD',
- type=int,
- help='call gc.set_threshold(THRESHOLD)')
- group.add_argument('-n', '--nowindows', action='store_true',
- help='suppress error message boxes on Windows')
- group.add_argument('-F', '--forever', action='store_true',
- help='run the specified tests in a loop, until an '
- 'error happens')
- group.add_argument('-P', '--pgo', dest='pgo', action='store_true',
- help='enable Profile Guided Optimization training')
-
- return parser
-
-def relative_filename(string):
- # CWD is replaced with a temporary dir before calling main(), so we
- # join it with the saved CWD so it ends up where the user expects.
- return os.path.join(support.SAVEDCWD, string)
-
-def huntrleaks(string):
- args = string.split(':')
- if len(args) not in (2, 3):
- raise argparse.ArgumentTypeError(
- 'needs 2 or 3 colon-separated arguments')
- nwarmup = int(args[0]) if args[0] else 5
- ntracked = int(args[1]) if args[1] else 4
- fname = args[2] if len(args) > 2 and args[2] else 'reflog.txt'
- return nwarmup, ntracked, fname
-
-def resources_list(string):
- u = [x.lower() for x in string.split(',')]
- for r in u:
- if r == 'all' or r == 'none':
- continue
- if r[0] == '-':
- r = r[1:]
- if r not in RESOURCE_NAMES:
- raise argparse.ArgumentTypeError('invalid resource: ' + r)
- return u
-
-def _parse_args(args, **kwargs):
- # Defaults
- ns = argparse.Namespace(testdir=None, verbose=0, quiet=False,
- exclude=False, single=False, randomize=False, fromfile=None,
- findleaks=False, use_resources=None, trace=False, coverdir='coverage',
- runleaks=False, huntrleaks=False, verbose2=False, print_slow=False,
- random_seed=None, use_mp=None, verbose3=False, forever=False,
- header=False, failfast=False, match_tests=None, pgo=False)
- for k, v in kwargs.items():
- if not hasattr(ns, k):
- raise TypeError('%r is an invalid keyword argument '
- 'for this function' % k)
- setattr(ns, k, v)
- if ns.use_resources is None:
- ns.use_resources = []
-
- parser = _create_parser()
- # Issue #14191: argparse doesn't support "intermixed" positional and
- # optional arguments. Use parse_known_args() as workaround.
- ns.args = parser.parse_known_args(args=args, namespace=ns)[1]
- for arg in ns.args:
- if arg.startswith('-'):
- parser.error("unrecognized arguments: %s" % arg)
- sys.exit(1)
-
- if ns.single and ns.fromfile:
- parser.error("-s and -f don't go together!")
- if ns.use_mp is not None and ns.trace:
- parser.error("-T and -j don't go together!")
- if ns.use_mp is not None and ns.findleaks:
- parser.error("-l and -j don't go together!")
- if ns.use_mp is not None and ns.memlimit:
- parser.error("-M and -j don't go together!")
- if ns.failfast and not (ns.verbose or ns.verbose3):
- parser.error("-G/--failfast needs either -v or -W")
-
- if ns.quiet:
- ns.verbose = 0
- if ns.timeout is not None:
- if hasattr(faulthandler, 'dump_traceback_later'):
- if ns.timeout <= 0:
- ns.timeout = None
- else:
- print("Warning: The timeout option requires "
- "faulthandler.dump_traceback_later")
- ns.timeout = None
- if ns.use_mp is not None:
- if ns.use_mp <= 0:
- # Use all cores + extras for tests that like to sleep
- ns.use_mp = 2 + (os.cpu_count() or 1)
- if ns.use_mp == 1:
- ns.use_mp = None
- if ns.use:
- for a in ns.use:
- for r in a:
- if r == 'all':
- ns.use_resources[:] = RESOURCE_NAMES
- continue
- if r == 'none':
- del ns.use_resources[:]
- continue
- remove = False
- if r[0] == '-':
- remove = True
- r = r[1:]
- if remove:
- if r in ns.use_resources:
- ns.use_resources.remove(r)
- elif r not in ns.use_resources:
- ns.use_resources.append(r)
- if ns.random_seed is not None:
- ns.randomize = True
-
- return ns
-
-
-def run_test_in_subprocess(testname, ns):
- """Run the given test in a subprocess with --slaveargs.
-
- ns is the option Namespace parsed from command-line arguments. regrtest
- is invoked in a subprocess with the --slaveargs argument; when the
- subprocess exits, its return code, stdout and stderr are returned as a
- 3-tuple.
- """
- from subprocess import Popen, PIPE
- base_cmd = ([sys.executable] + support.args_from_interpreter_flags() +
- ['-X', 'faulthandler', '-m', 'test.regrtest'])
- # required to spawn a new process with PGO flag on/off
- if ns.pgo:
- base_cmd = base_cmd + ['--pgo']
- slaveargs = (
- (testname, ns.verbose, ns.quiet),
- dict(huntrleaks=ns.huntrleaks,
- use_resources=ns.use_resources,
- output_on_failure=ns.verbose3,
- timeout=ns.timeout, failfast=ns.failfast,
- match_tests=ns.match_tests, pgo=ns.pgo))
- # Running the child from the same working directory as regrtest's original
- # invocation ensures that TEMPDIR for the child is the same when
- # sysconfig.is_python_build() is true. See issue 15300.
- popen = Popen(base_cmd + ['--slaveargs', json.dumps(slaveargs)],
- stdout=PIPE, stderr=PIPE,
- universal_newlines=True,
- close_fds=(os.name != 'nt'),
- cwd=support.SAVEDCWD)
- stdout, stderr = popen.communicate()
- retcode = popen.wait()
- return retcode, stdout, stderr
-
-
-def main(tests=None, **kwargs):
- """Execute a test suite.
-
- This also parses command-line options and modifies its behavior
- accordingly.
-
- tests -- a list of strings containing test names (optional)
- testdir -- the directory in which to look for tests (optional)
-
- Users other than the Python test suite will certainly want to
- specify testdir; if it's omitted, the directory containing the
- Python test suite is searched for.
-
- If the tests argument is omitted, the tests listed on the
- command-line will be used. If that's empty, too, then all *.py
- files beginning with test_ will be used.
-
- The other default arguments (verbose, quiet, exclude,
- single, randomize, findleaks, use_resources, trace, coverdir,
- print_slow, and random_seed) allow programmers calling main()
- directly to set the values that would normally be set by flags
- on the command line.
- """
- # Display the Python traceback on fatal errors (e.g. segfault)
- faulthandler.enable(all_threads=True)
-
- # Display the Python traceback on SIGALRM or SIGUSR1 signal
- signals = []
- if hasattr(signal, 'SIGALRM'):
- signals.append(signal.SIGALRM)
- if hasattr(signal, 'SIGUSR1'):
- signals.append(signal.SIGUSR1)
- for signum in signals:
- faulthandler.register(signum, chain=True)
-
- replace_stdout()
-
- support.record_original_stdout(sys.stdout)
-
- ns = _parse_args(sys.argv[1:], **kwargs)
-
- if ns.huntrleaks:
- # Avoid false positives due to various caches
- # filling slowly with random data:
- warm_caches()
- if ns.memlimit is not None:
- support.set_memlimit(ns.memlimit)
- if ns.threshold is not None:
- import gc
- gc.set_threshold(ns.threshold)
- if ns.nowindows:
- print('The --nowindows (-n) option is deprecated. '
- 'Use -vv to display assertions in stderr.')
- try:
- import msvcrt
- except ImportError:
- pass
- else:
- msvcrt.SetErrorMode(msvcrt.SEM_FAILCRITICALERRORS|
- msvcrt.SEM_NOALIGNMENTFAULTEXCEPT|
- msvcrt.SEM_NOGPFAULTERRORBOX|
- msvcrt.SEM_NOOPENFILEERRORBOX)
- try:
- msvcrt.CrtSetReportMode
- except AttributeError:
- # release build
- pass
- else:
- for m in [msvcrt.CRT_WARN, msvcrt.CRT_ERROR, msvcrt.CRT_ASSERT]:
- if ns.verbose and ns.verbose >= 2:
- msvcrt.CrtSetReportMode(m, msvcrt.CRTDBG_MODE_FILE)
- msvcrt.CrtSetReportFile(m, msvcrt.CRTDBG_FILE_STDERR)
- else:
- msvcrt.CrtSetReportMode(m, 0)
- if ns.wait:
- input("Press any key to continue...")
-
- if ns.slaveargs is not None:
- args, kwargs = json.loads(ns.slaveargs)
- if kwargs.get('huntrleaks'):
- unittest.BaseTestSuite._cleanup = False
- try:
- result = runtest(*args, **kwargs)
- except KeyboardInterrupt:
- result = INTERRUPTED, ''
- except BaseException as e:
- traceback.print_exc()
- result = CHILD_ERROR, str(e)
- sys.stdout.flush()
- print() # Force a newline (just in case)
- print(json.dumps(result))
- sys.exit(0)
-
- good = []
- bad = []
- skipped = []
- resource_denieds = []
- environment_changed = []
- interrupted = False
-
- if ns.findleaks:
- try:
- import gc
- except ImportError:
- print('No GC available, disabling findleaks.')
- ns.findleaks = False
- else:
- # Uncomment the line below to report garbage that is not
- # freeable by reference counting alone. By default only
- # garbage that is not collectable by the GC is reported.
- #gc.set_debug(gc.DEBUG_SAVEALL)
- found_garbage = []
-
- if ns.huntrleaks:
- unittest.BaseTestSuite._cleanup = False
-
- if ns.single:
- filename = os.path.join(TEMPDIR, 'pynexttest')
- try:
- with open(filename, 'r') as fp:
- next_test = fp.read().strip()
- tests = [next_test]
- except OSError:
- pass
-
- if ns.fromfile:
- tests = []
- with open(os.path.join(support.SAVEDCWD, ns.fromfile)) as fp:
- count_pat = re.compile(r'\[\s*\d+/\s*\d+\]')
- for line in fp:
- line = count_pat.sub('', line)
- guts = line.split() # assuming no test has whitespace in its name
- if guts and not guts[0].startswith('#'):
- tests.extend(guts)
-
- # Strip .py extensions.
- removepy(ns.args)
- removepy(tests)
-
- stdtests = STDTESTS[:]
- nottests = NOTTESTS.copy()
- if ns.exclude:
- for arg in ns.args:
- if arg in stdtests:
- stdtests.remove(arg)
- nottests.add(arg)
- ns.args = []
-
- # For a partial run, we do not need to clutter the output.
- if (ns.verbose or ns.header or
- not (ns.pgo or ns.quiet or ns.single or tests or ns.args)):
- # Print basic platform information
- print("==", platform.python_implementation(), *sys.version.split())
- print("== ", platform.platform(aliased=True),
- "%s-endian" % sys.byteorder)
- print("== ", "hash algorithm:", sys.hash_info.algorithm,
- "64bit" if sys.maxsize > 2**32 else "32bit")
- print("== ", os.getcwd())
- print("Testing with flags:", sys.flags)
-
- # if testdir is set, then we are not running the python tests suite, so
- # don't add default tests to be executed or skipped (pass empty values)
- if ns.testdir:
- alltests = findtests(ns.testdir, list(), set())
- else:
- alltests = findtests(ns.testdir, stdtests, nottests)
-
- selected = tests or ns.args or alltests
- if ns.single:
- selected = selected[:1]
- try:
- next_single_test = alltests[alltests.index(selected[0])+1]
- except IndexError:
- next_single_test = None
- # Remove all the selected tests that precede start if it's set.
- if ns.start:
- try:
- del selected[:selected.index(ns.start)]
- except ValueError:
- print("Couldn't find starting test (%s), using all tests" % ns.start)
- if ns.randomize:
- if ns.random_seed is None:
- ns.random_seed = random.randrange(10000000)
- random.seed(ns.random_seed)
- print("Using random seed", ns.random_seed)
- random.shuffle(selected)
- if ns.trace:
- import trace, tempfile
- tracer = trace.Trace(ignoredirs=[sys.base_prefix, sys.base_exec_prefix,
- tempfile.gettempdir()],
- trace=False, count=True)
-
- test_times = []
- support.verbose = ns.verbose # Tell tests to be moderately quiet
- support.use_resources = ns.use_resources
- save_modules = sys.modules.keys()
-
- def accumulate_result(test, result):
- ok, test_time = result
- if ok not in (CHILD_ERROR, INTERRUPTED):
- test_times.append((test_time, test))
- if ok == PASSED:
- good.append(test)
- elif ok == FAILED:
- bad.append(test)
- elif ok == ENV_CHANGED:
- environment_changed.append(test)
- elif ok == SKIPPED:
- skipped.append(test)
- elif ok == RESOURCE_DENIED:
- skipped.append(test)
- resource_denieds.append(test)
-
- if ns.forever:
- def test_forever(tests=list(selected)):
- while True:
- for test in tests:
- yield test
- if bad:
- return
- tests = test_forever()
- test_count = ''
- test_count_width = 3
- else:
- tests = iter(selected)
- test_count = '/{}'.format(len(selected))
- test_count_width = len(test_count) - 1
-
- if ns.use_mp:
- try:
- from threading import Thread
- except ImportError:
- print("Multiprocess option requires thread support")
- sys.exit(2)
- from queue import Queue
- debug_output_pat = re.compile(r"\[\d+ refs, \d+ blocks\]$")
- output = Queue()
- pending = MultiprocessTests(tests)
- def work():
- # A worker thread.
- try:
- while True:
- try:
- test = next(pending)
- except StopIteration:
- output.put((None, None, None, None))
- return
- retcode, stdout, stderr = run_test_in_subprocess(test, ns)
- # Strip last refcount output line if it exists, since it
- # comes from the shutdown of the interpreter in the subcommand.
- stderr = debug_output_pat.sub("", stderr)
- stdout, _, result = stdout.strip().rpartition("\n")
- if retcode != 0:
- result = (CHILD_ERROR, "Exit code %s" % retcode)
- output.put((test, stdout.rstrip(), stderr.rstrip(), result))
- return
- if not result:
- output.put((None, None, None, None))
- return
- result = json.loads(result)
- output.put((test, stdout.rstrip(), stderr.rstrip(), result))
- except BaseException:
- output.put((None, None, None, None))
- raise
- workers = [Thread(target=work) for i in range(ns.use_mp)]
- for worker in workers:
- worker.start()
- finished = 0
- test_index = 1
- try:
- while finished < ns.use_mp:
- test, stdout, stderr, result = output.get()
- if test is None:
- finished += 1
- continue
- accumulate_result(test, result)
- if not ns.quiet:
- if bad and not ns.pgo:
- fmt = "[{1:{0}}{2}/{3}] {4}"
- else:
- fmt = "[{1:{0}}{2}] {4}"
- print(fmt.format(
- test_count_width, test_index, test_count,
- len(bad), test))
- if stdout:
- print(stdout)
- if stderr and not ns.pgo:
- print(stderr, file=sys.stderr)
- sys.stdout.flush()
- sys.stderr.flush()
- if result[0] == INTERRUPTED:
- raise KeyboardInterrupt
- if result[0] == CHILD_ERROR:
- raise Exception("Child error on {}: {}".format(test, result[1]))
- test_index += 1
- except KeyboardInterrupt:
- interrupted = True
- pending.interrupted = True
- for worker in workers:
- worker.join()
- else:
- for test_index, test in enumerate(tests, 1):
- if not ns.quiet:
- if bad and not ns.pgo:
- fmt = "[{1:{0}}{2}/{3}] {4}"
- else:
- fmt = "[{1:{0}}{2}] {4}"
- print(fmt.format(
- test_count_width, test_index, test_count, len(bad), test))
- sys.stdout.flush()
- if ns.trace:
- # If we're tracing code coverage, then we don't exit with status
- # if on a false return value from main.
- tracer.runctx('runtest(test, ns.verbose, ns.quiet, timeout=ns.timeout)',
- globals=globals(), locals=vars())
- else:
- try:
- result = runtest(test, ns.verbose, ns.quiet,
- ns.huntrleaks,
- output_on_failure=ns.verbose3,
- timeout=ns.timeout, failfast=ns.failfast,
- match_tests=ns.match_tests, pgo=ns.pgo)
- accumulate_result(test, result)
- except KeyboardInterrupt:
- interrupted = True
- break
- if ns.findleaks:
- gc.collect()
- if gc.garbage:
- print("Warning: test created", len(gc.garbage), end=' ')
- print("uncollectable object(s).")
- # move the uncollectable objects somewhere so we don't see
- # them again
- found_garbage.extend(gc.garbage)
- del gc.garbage[:]
- # Unload the newly imported modules (best effort finalization)
- for module in sys.modules.keys():
- if module not in save_modules and module.startswith("test."):
- support.unload(module)
-
- if interrupted and not ns.pgo:
- # print a newline after ^C
- print()
- print("Test suite interrupted by signal SIGINT.")
- omitted = set(selected) - set(good) - set(bad) - set(skipped)
- print(count(len(omitted), "test"), "omitted:")
- printlist(omitted)
- if good and not ns.quiet and not ns.pgo:
- if not bad and not skipped and not interrupted and len(good) > 1:
- print("All", end=' ')
- print(count(len(good), "test"), "OK.")
- if ns.print_slow:
- test_times.sort(reverse=True)
- print("10 slowest tests:")
- for time, test in test_times[:10]:
- print("%s: %.1fs" % (test, time))
- if bad and not ns.pgo:
- print(count(len(bad), "test"), "failed:")
- printlist(bad)
- if environment_changed and not ns.pgo:
- print("{} altered the execution environment:".format(
- count(len(environment_changed), "test")))
- printlist(environment_changed)
- if skipped and not ns.quiet and not ns.pgo:
- print(count(len(skipped), "test"), "skipped:")
- printlist(skipped)
-
- if ns.verbose2 and bad:
- print("Re-running failed tests in verbose mode")
- for test in bad[:]:
- if not ns.pgo:
- print("Re-running test %r in verbose mode" % test)
- sys.stdout.flush()
- try:
- ns.verbose = True
- ok = runtest(test, True, ns.quiet, ns.huntrleaks,
- timeout=ns.timeout, pgo=ns.pgo)
- except KeyboardInterrupt:
- # print a newline separate from the ^C
- print()
- break
- else:
- if ok[0] in {PASSED, ENV_CHANGED, SKIPPED, RESOURCE_DENIED}:
- bad.remove(test)
- else:
- if bad:
- print(count(len(bad), 'test'), "failed again:")
- printlist(bad)
-
- if ns.single:
- if next_single_test:
- with open(filename, 'w') as fp:
- fp.write(next_single_test + '\n')
- else:
- os.unlink(filename)
-
- if ns.trace:
- r = tracer.results()
- r.write_results(show_missing=True, summary=True, coverdir=ns.coverdir)
-
- if ns.runleaks:
- os.system("leaks %d" % os.getpid())
-
- sys.exit(len(bad) > 0 or interrupted)
-
-
-# small set of tests to determine if we have a basically functioning interpreter
-# (i.e. if any of these fail, then anything else is likely to follow)
-STDTESTS = [
- 'test_grammar',
- 'test_opcodes',
- 'test_dict',
- 'test_builtin',
- 'test_exceptions',
- 'test_types',
- 'test_unittest',
- 'test_doctest',
- 'test_doctest2',
- 'test_support'
-]
-
-# set of tests that we don't want to be executed when using regrtest
-NOTTESTS = set()
-
-def findtests(testdir=None, stdtests=STDTESTS, nottests=NOTTESTS):
- """Return a list of all applicable test modules."""
- testdir = findtestdir(testdir)
- names = os.listdir(testdir)
- tests = []
- others = set(stdtests) | nottests
- for name in names:
- mod, ext = os.path.splitext(name)
- if mod[:5] == "test_" and ext in (".py", "") and mod not in others:
- tests.append(mod)
- return stdtests + sorted(tests)
+from test.libregrtest import main
-# We do not use a generator so multiple threads can call next().
-class MultiprocessTests(object):
- """A thread-safe iterator over tests for multiprocess mode."""
+# Alias for backward compatibility (just in case)
+main_in_temp_cwd = main
- def __init__(self, tests):
- self.interrupted = False
- self.lock = threading.Lock()
- self.tests = tests
- def __iter__(self):
- return self
+def _main():
+ global __file__
- def __next__(self):
- with self.lock:
- if self.interrupted:
- raise StopIteration('tests interrupted')
- return next(self.tests)
-
-def replace_stdout():
- """Set stdout encoder error handler to backslashreplace (as stderr error
- handler) to avoid UnicodeEncodeError when printing a traceback"""
- import atexit
-
- stdout = sys.stdout
- sys.stdout = open(stdout.fileno(), 'w',
- encoding=stdout.encoding,
- errors="backslashreplace",
- closefd=False,
- newline='\n')
-
- def restore_stdout():
- sys.stdout.close()
- sys.stdout = stdout
- atexit.register(restore_stdout)
-
-def runtest(test, verbose, quiet,
- huntrleaks=False, use_resources=None,
- output_on_failure=False, failfast=False, match_tests=None,
- timeout=None, *, pgo=False):
- """Run a single test.
-
- test -- the name of the test
- verbose -- if true, print more messages
- quiet -- if true, don't print 'skipped' messages (probably redundant)
- huntrleaks -- run multiple times to test for leaks; requires a debug
- build; a triple corresponding to -R's three arguments
- use_resources -- list of extra resources to use
- output_on_failure -- if true, display test output on failure
- timeout -- dump the traceback and exit if a test takes more than
- timeout seconds
- failfast, match_tests -- See regrtest command-line flags for these.
- pgo -- if true, do not print unnecessary info when running the test
- for Profile Guided Optimization build
-
- Returns the tuple result, test_time, where result is one of the constants:
- INTERRUPTED KeyboardInterrupt when run under -j
- RESOURCE_DENIED test skipped because resource denied
- SKIPPED test skipped for some other reason
- ENV_CHANGED test failed because it changed the execution environment
- FAILED test failed
- PASSED test passed
- """
- if use_resources is not None:
- support.use_resources = use_resources
- use_timeout = (timeout is not None)
- if use_timeout:
- faulthandler.dump_traceback_later(timeout, exit=True)
- try:
- support.match_tests = match_tests
- if failfast:
- support.failfast = True
- if output_on_failure:
- support.verbose = True
-
- # Reuse the same instance to all calls to runtest(). Some
- # tests keep a reference to sys.stdout or sys.stderr
- # (eg. test_argparse).
- if runtest.stringio is None:
- stream = io.StringIO()
- runtest.stringio = stream
- else:
- stream = runtest.stringio
- stream.seek(0)
- stream.truncate()
-
- orig_stdout = sys.stdout
- orig_stderr = sys.stderr
- try:
- sys.stdout = stream
- sys.stderr = stream
- result = runtest_inner(test, verbose, quiet, huntrleaks,
- display_failure=False, pgo=pgo)
- if result[0] != PASSED and not pgo:
- output = stream.getvalue()
- orig_stderr.write(output)
- orig_stderr.flush()
- finally:
- sys.stdout = orig_stdout
- sys.stderr = orig_stderr
- else:
- support.verbose = verbose # Tell tests to be moderately quiet
- result = runtest_inner(test, verbose, quiet, huntrleaks,
- display_failure=not verbose, pgo=pgo)
- return result
- finally:
- if use_timeout:
- faulthandler.cancel_dump_traceback_later()
- cleanup_test_droppings(test, verbose)
-runtest.stringio = None
-
-# Unit tests are supposed to leave the execution environment unchanged
-# once they complete. But sometimes tests have bugs, especially when
-# tests fail, and the changes to environment go on to mess up other
-# tests. This can cause issues with buildbot stability, since tests
-# are run in random order and so problems may appear to come and go.
-# There are a few things we can save and restore to mitigate this, and
-# the following context manager handles this task.
-
-class saved_test_environment:
- """Save bits of the test environment and restore them at block exit.
-
- with saved_test_environment(testname, verbose, quiet):
- #stuff
-
- Unless quiet is True, a warning is printed to stderr if any of
- the saved items was changed by the test. The attribute 'changed'
- is initially False, but is set to True if a change is detected.
-
- If verbose is more than 1, the before and after state of changed
- items is also printed.
- """
-
- changed = False
-
- def __init__(self, testname, verbose=0, quiet=False, *, pgo=False):
- self.testname = testname
- self.verbose = verbose
- self.quiet = quiet
- self.pgo = pgo
-
- # To add things to save and restore, add a name XXX to the resources list
- # and add corresponding get_XXX/restore_XXX functions. get_XXX should
- # return the value to be saved and compared against a second call to the
- # get function when test execution completes. restore_XXX should accept
- # the saved value and restore the resource using it. It will be called if
- # and only if a change in the value is detected.
- #
- # Note: XXX will have any '.' replaced with '_' characters when determining
- # the corresponding method names.
-
- resources = ('sys.argv', 'cwd', 'sys.stdin', 'sys.stdout', 'sys.stderr',
- 'os.environ', 'sys.path', 'sys.path_hooks', '__import__',
- 'asyncore.socket_map',
- 'logging._handlers', 'logging._handlerList', 'sys.gettrace',
- 'sys.warnoptions',
- # multiprocessing.process._cleanup() may release ref
- # to a thread, so check processes first.
- 'multiprocessing.process._dangling', 'threading._dangling',
- 'sysconfig._CONFIG_VARS', 'sysconfig._INSTALL_SCHEMES',
- 'files', 'locale', 'warnings.showwarning',
- 'shutil_archive_formats', 'shutil_unpack_formats',
- )
-
- def get_sys_argv(self):
- return id(sys.argv), sys.argv, sys.argv[:]
- def restore_sys_argv(self, saved_argv):
- sys.argv = saved_argv[1]
- sys.argv[:] = saved_argv[2]
-
- def get_cwd(self):
- return os.getcwd()
- def restore_cwd(self, saved_cwd):
- os.chdir(saved_cwd)
-
- def get_sys_stdout(self):
- return sys.stdout
- def restore_sys_stdout(self, saved_stdout):
- sys.stdout = saved_stdout
-
- def get_sys_stderr(self):
- return sys.stderr
- def restore_sys_stderr(self, saved_stderr):
- sys.stderr = saved_stderr
-
- def get_sys_stdin(self):
- return sys.stdin
- def restore_sys_stdin(self, saved_stdin):
- sys.stdin = saved_stdin
-
- def get_os_environ(self):
- return id(os.environ), os.environ, dict(os.environ)
- def restore_os_environ(self, saved_environ):
- os.environ = saved_environ[1]
- os.environ.clear()
- os.environ.update(saved_environ[2])
-
- def get_sys_path(self):
- return id(sys.path), sys.path, sys.path[:]
- def restore_sys_path(self, saved_path):
- sys.path = saved_path[1]
- sys.path[:] = saved_path[2]
-
- def get_sys_path_hooks(self):
- return id(sys.path_hooks), sys.path_hooks, sys.path_hooks[:]
- def restore_sys_path_hooks(self, saved_hooks):
- sys.path_hooks = saved_hooks[1]
- sys.path_hooks[:] = saved_hooks[2]
-
- def get_sys_gettrace(self):
- return sys.gettrace()
- def restore_sys_gettrace(self, trace_fxn):
- sys.settrace(trace_fxn)
-
- def get___import__(self):
- return builtins.__import__
- def restore___import__(self, import_):
- builtins.__import__ = import_
-
- def get_asyncore_socket_map(self):
- asyncore = sys.modules.get('asyncore')
- # XXX Making a copy keeps objects alive until __exit__ gets called.
- return asyncore and asyncore.socket_map.copy() or {}
- def restore_asyncore_socket_map(self, saved_map):
- asyncore = sys.modules.get('asyncore')
- if asyncore is not None:
- asyncore.close_all(ignore_all=True)
- asyncore.socket_map.update(saved_map)
-
- def get_shutil_archive_formats(self):
- # we could call get_archives_formats() but that only returns the
- # registry keys; we want to check the values too (the functions that
- # are registered)
- return shutil._ARCHIVE_FORMATS, shutil._ARCHIVE_FORMATS.copy()
- def restore_shutil_archive_formats(self, saved):
- shutil._ARCHIVE_FORMATS = saved[0]
- shutil._ARCHIVE_FORMATS.clear()
- shutil._ARCHIVE_FORMATS.update(saved[1])
-
- def get_shutil_unpack_formats(self):
- return shutil._UNPACK_FORMATS, shutil._UNPACK_FORMATS.copy()
- def restore_shutil_unpack_formats(self, saved):
- shutil._UNPACK_FORMATS = saved[0]
- shutil._UNPACK_FORMATS.clear()
- shutil._UNPACK_FORMATS.update(saved[1])
-
- def get_logging__handlers(self):
- # _handlers is a WeakValueDictionary
- return id(logging._handlers), logging._handlers, logging._handlers.copy()
- def restore_logging__handlers(self, saved_handlers):
- # Can't easily revert the logging state
- pass
-
- def get_logging__handlerList(self):
- # _handlerList is a list of weakrefs to handlers
- return id(logging._handlerList), logging._handlerList, logging._handlerList[:]
- def restore_logging__handlerList(self, saved_handlerList):
- # Can't easily revert the logging state
- pass
-
- def get_sys_warnoptions(self):
- return id(sys.warnoptions), sys.warnoptions, sys.warnoptions[:]
- def restore_sys_warnoptions(self, saved_options):
- sys.warnoptions = saved_options[1]
- sys.warnoptions[:] = saved_options[2]
-
- # Controlling dangling references to Thread objects can make it easier
- # to track reference leaks.
- def get_threading__dangling(self):
- if not threading:
- return None
- # This copies the weakrefs without making any strong reference
- return threading._dangling.copy()
- def restore_threading__dangling(self, saved):
- if not threading:
- return
- threading._dangling.clear()
- threading._dangling.update(saved)
-
- # Same for Process objects
- def get_multiprocessing_process__dangling(self):
- if not multiprocessing:
- return None
- # Unjoined process objects can survive after process exits
- multiprocessing.process._cleanup()
- # This copies the weakrefs without making any strong reference
- return multiprocessing.process._dangling.copy()
- def restore_multiprocessing_process__dangling(self, saved):
- if not multiprocessing:
- return
- multiprocessing.process._dangling.clear()
- multiprocessing.process._dangling.update(saved)
-
- def get_sysconfig__CONFIG_VARS(self):
- # make sure the dict is initialized
- sysconfig.get_config_var('prefix')
- return (id(sysconfig._CONFIG_VARS), sysconfig._CONFIG_VARS,
- dict(sysconfig._CONFIG_VARS))
- def restore_sysconfig__CONFIG_VARS(self, saved):
- sysconfig._CONFIG_VARS = saved[1]
- sysconfig._CONFIG_VARS.clear()
- sysconfig._CONFIG_VARS.update(saved[2])
-
- def get_sysconfig__INSTALL_SCHEMES(self):
- return (id(sysconfig._INSTALL_SCHEMES), sysconfig._INSTALL_SCHEMES,
- sysconfig._INSTALL_SCHEMES.copy())
- def restore_sysconfig__INSTALL_SCHEMES(self, saved):
- sysconfig._INSTALL_SCHEMES = saved[1]
- sysconfig._INSTALL_SCHEMES.clear()
- sysconfig._INSTALL_SCHEMES.update(saved[2])
-
- def get_files(self):
- return sorted(fn + ('/' if os.path.isdir(fn) else '')
- for fn in os.listdir())
- def restore_files(self, saved_value):
- fn = support.TESTFN
- if fn not in saved_value and (fn + '/') not in saved_value:
- if os.path.isfile(fn):
- support.unlink(fn)
- elif os.path.isdir(fn):
- support.rmtree(fn)
-
- _lc = [getattr(locale, lc) for lc in dir(locale)
- if lc.startswith('LC_')]
- def get_locale(self):
- pairings = []
- for lc in self._lc:
- try:
- pairings.append((lc, locale.setlocale(lc, None)))
- except (TypeError, ValueError):
- continue
- return pairings
- def restore_locale(self, saved):
- for lc, setting in saved:
- locale.setlocale(lc, setting)
-
- def get_warnings_showwarning(self):
- return warnings.showwarning
- def restore_warnings_showwarning(self, fxn):
- warnings.showwarning = fxn
-
- def resource_info(self):
- for name in self.resources:
- method_suffix = name.replace('.', '_')
- get_name = 'get_' + method_suffix
- restore_name = 'restore_' + method_suffix
- yield name, getattr(self, get_name), getattr(self, restore_name)
-
- def __enter__(self):
- self.saved_values = dict((name, get()) for name, get, restore
- in self.resource_info())
- return self
-
- def __exit__(self, exc_type, exc_val, exc_tb):
- saved_values = self.saved_values
- del self.saved_values
- support.gc_collect() # Some resources use weak references
- for name, get, restore in self.resource_info():
- current = get()
- original = saved_values.pop(name)
- # Check for changes to the resource's value
- if current != original:
- self.changed = True
- restore(original)
- if not self.quiet and not self.pgo:
- print("Warning -- {} was modified by {}".format(
- name, self.testname),
- file=sys.stderr)
- if self.verbose > 1 and not self.pgo:
- print(" Before: {}\n After: {} ".format(
- original, current),
- file=sys.stderr)
- return False
-
-
-def runtest_inner(test, verbose, quiet,
- huntrleaks=False, display_failure=True, pgo=False):
- support.unload(test)
-
- test_time = 0.0
- refleak = False # True if the test leaked references.
- try:
- if test.startswith('test.'):
- abstest = test
- else:
- # Always import it from the test package
- abstest = 'test.' + test
- clear_caches()
- with saved_test_environment(test, verbose, quiet, pgo=pgo) as environment:
- start_time = time.time()
- the_module = importlib.import_module(abstest)
- # If the test has a test_main, that will run the appropriate
- # tests. If not, use normal unittest test loading.
- test_runner = getattr(the_module, "test_main", None)
- if test_runner is None:
- def test_runner():
- loader = unittest.TestLoader()
- tests = loader.loadTestsFromModule(the_module)
- for error in loader.errors:
- print(error, file=sys.stderr)
- if loader.errors:
- raise Exception("errors while loading tests")
- support.run_unittest(tests)
- test_runner()
- if huntrleaks:
- refleak = dash_R(the_module, test, test_runner, huntrleaks)
- test_time = time.time() - start_time
- except support.ResourceDenied as msg:
- if not quiet and not pgo:
- print(test, "skipped --", msg)
- sys.stdout.flush()
- return RESOURCE_DENIED, test_time
- except unittest.SkipTest as msg:
- if not quiet and not pgo:
- print(test, "skipped --", msg)
- sys.stdout.flush()
- return SKIPPED, test_time
- except KeyboardInterrupt:
- raise
- except support.TestFailed as msg:
- if not pgo:
- if display_failure:
- print("test", test, "failed --", msg, file=sys.stderr)
- else:
- print("test", test, "failed", file=sys.stderr)
- sys.stderr.flush()
- return FAILED, test_time
- except:
- msg = traceback.format_exc()
- if not pgo:
- print("test", test, "crashed --", msg, file=sys.stderr)
- sys.stderr.flush()
- return FAILED, test_time
- else:
- if refleak:
- return FAILED, test_time
- if environment.changed:
- return ENV_CHANGED, test_time
- return PASSED, test_time
-
-def cleanup_test_droppings(testname, verbose):
- import shutil
- import stat
- import gc
-
- # First kill any dangling references to open files etc.
- # This can also issue some ResourceWarnings which would otherwise get
- # triggered during the following test run, and possibly produce failures.
- gc.collect()
-
- # Try to clean up junk commonly left behind. While tests shouldn't leave
- # any files or directories behind, when a test fails that can be tedious
- # for it to arrange. The consequences can be especially nasty on Windows,
- # since if a test leaves a file open, it cannot be deleted by name (while
- # there's nothing we can do about that here either, we can display the
- # name of the offending test, which is a real help).
- for name in (support.TESTFN,
- "db_home",
- ):
- if not os.path.exists(name):
- continue
-
- if os.path.isdir(name):
- kind, nuker = "directory", shutil.rmtree
- elif os.path.isfile(name):
- kind, nuker = "file", os.unlink
- else:
- raise SystemError("os.path says %r exists but is neither "
- "directory nor file" % name)
-
- if verbose:
- print("%r left behind %s %r" % (testname, kind, name))
- try:
- # if we have chmod, fix possible permissions problems
- # that might prevent cleanup
- if (hasattr(os, 'chmod')):
- os.chmod(name, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
- nuker(name)
- except Exception as msg:
- print(("%r left behind %s %r and it couldn't be "
- "removed: %s" % (testname, kind, name, msg)), file=sys.stderr)
-
-def dash_R(the_module, test, indirect_test, huntrleaks):
- """Run a test multiple times, looking for reference leaks.
-
- Returns:
- False if the test didn't leak references; True if we detected refleaks.
- """
- # This code is hackish and inelegant, but it seems to do the job.
- import copyreg
- import collections.abc
-
- if not hasattr(sys, 'gettotalrefcount'):
- raise Exception("Tracking reference leaks requires a debug build "
- "of Python")
-
- # Save current values for dash_R_cleanup() to restore.
- fs = warnings.filters[:]
- ps = copyreg.dispatch_table.copy()
- pic = sys.path_importer_cache.copy()
- try:
- import zipimport
- except ImportError:
- zdc = None # Run unmodified on platforms without zipimport support
- else:
- zdc = zipimport._zip_directory_cache.copy()
- abcs = {}
- for abc in [getattr(collections.abc, a) for a in collections.abc.__all__]:
- if not isabstract(abc):
- continue
- for obj in abc.__subclasses__() + [abc]:
- abcs[obj] = obj._abc_registry.copy()
-
- nwarmup, ntracked, fname = huntrleaks
- fname = os.path.join(support.SAVEDCWD, fname)
- repcount = nwarmup + ntracked
- rc_deltas = [0] * repcount
- alloc_deltas = [0] * repcount
-
- print("beginning", repcount, "repetitions", file=sys.stderr)
- print(("1234567890"*(repcount//10 + 1))[:repcount], file=sys.stderr)
- sys.stderr.flush()
- for i in range(repcount):
- indirect_test()
- alloc_after, rc_after = dash_R_cleanup(fs, ps, pic, zdc, abcs)
- sys.stderr.write('.')
- sys.stderr.flush()
- if i >= nwarmup:
- rc_deltas[i] = rc_after - rc_before
- alloc_deltas[i] = alloc_after - alloc_before
- alloc_before, rc_before = alloc_after, rc_after
- print(file=sys.stderr)
- # These checkers return False on success, True on failure
- def check_rc_deltas(deltas):
- return any(deltas)
- def check_alloc_deltas(deltas):
- # At least 1/3rd of 0s
- if 3 * deltas.count(0) < len(deltas):
- return True
- # Nothing else than 1s, 0s and -1s
- if not set(deltas) <= {1,0,-1}:
- return True
- return False
- failed = False
- for deltas, item_name, checker in [
- (rc_deltas, 'references', check_rc_deltas),
- (alloc_deltas, 'memory blocks', check_alloc_deltas)]:
- if checker(deltas):
- msg = '%s leaked %s %s, sum=%s' % (
- test, deltas[nwarmup:], item_name, sum(deltas))
- print(msg, file=sys.stderr)
- sys.stderr.flush()
- with open(fname, "a") as refrep:
- print(msg, file=refrep)
- refrep.flush()
- failed = True
- return failed
-
-def dash_R_cleanup(fs, ps, pic, zdc, abcs):
- import gc, copyreg
- import collections.abc
- from weakref import WeakSet
-
- # Restore some original values.
- warnings.filters[:] = fs
- copyreg.dispatch_table.clear()
- copyreg.dispatch_table.update(ps)
- sys.path_importer_cache.clear()
- sys.path_importer_cache.update(pic)
- try:
- import zipimport
- except ImportError:
- pass # Run unmodified on platforms without zipimport support
- else:
- zipimport._zip_directory_cache.clear()
- zipimport._zip_directory_cache.update(zdc)
-
- # clear type cache
- sys._clear_type_cache()
-
- # Clear ABC registries, restoring previously saved ABC registries.
- for abc in [getattr(collections.abc, a) for a in collections.abc.__all__]:
- if not isabstract(abc):
- continue
- for obj in abc.__subclasses__() + [abc]:
- obj._abc_registry = abcs.get(obj, WeakSet()).copy()
- obj._abc_cache.clear()
- obj._abc_negative_cache.clear()
-
- clear_caches()
-
- # Collect cyclic trash and read memory statistics immediately after.
- func1 = sys.getallocatedblocks
- func2 = sys.gettotalrefcount
- gc.collect()
- return func1(), func2()
-
-def clear_caches():
- import gc
-
- # Clear the warnings registry, so they can be displayed again
- for mod in sys.modules.values():
- if hasattr(mod, '__warningregistry__'):
- del mod.__warningregistry__
-
- # Flush standard output, so that buffered data is sent to the OS and
- # associated Python objects are reclaimed.
- for stream in (sys.stdout, sys.stderr, sys.__stdout__, sys.__stderr__):
- if stream is not None:
- stream.flush()
-
- # Clear assorted module caches.
- # Don't worry about resetting the cache if the module is not loaded
- try:
- distutils_dir_util = sys.modules['distutils.dir_util']
- except KeyError:
- pass
- else:
- distutils_dir_util._path_created.clear()
-
- re.purge()
-
- try:
- _strptime = sys.modules['_strptime']
- except KeyError:
- pass
- else:
- _strptime._regex_cache.clear()
-
- try:
- urllib_parse = sys.modules['urllib.parse']
- except KeyError:
- pass
- else:
- urllib_parse.clear_cache()
-
- try:
- urllib_request = sys.modules['urllib.request']
- except KeyError:
- pass
- else:
- urllib_request.urlcleanup()
-
- try:
- linecache = sys.modules['linecache']
- except KeyError:
- pass
- else:
- linecache.clearcache()
-
- try:
- mimetypes = sys.modules['mimetypes']
- except KeyError:
- pass
- else:
- mimetypes._default_mime_types()
-
- try:
- filecmp = sys.modules['filecmp']
- except KeyError:
- pass
- else:
- filecmp._cache.clear()
-
- try:
- struct = sys.modules['struct']
- except KeyError:
- pass
- else:
- struct._clearcache()
-
- try:
- doctest = sys.modules['doctest']
- except KeyError:
- pass
- else:
- doctest.master = None
-
- try:
- ctypes = sys.modules['ctypes']
- except KeyError:
- pass
- else:
- ctypes._reset_cache()
-
- try:
- typing = sys.modules['typing']
- except KeyError:
- pass
- else:
- for f in typing._cleanups:
- f()
-
- gc.collect()
-
-def warm_caches():
- # char cache
- s = bytes(range(256))
- for i in range(256):
- s[i:i+1]
- # unicode cache
- x = [chr(i) for i in range(256)]
- # int cache
- x = list(range(-5, 257))
-
-def findtestdir(path=None):
- return path or os.path.dirname(__file__) or os.curdir
-
-def removepy(names):
- if not names:
- return
- for idx, name in enumerate(names):
- basename, ext = os.path.splitext(name)
- if ext == '.py':
- names[idx] = basename
-
-def count(n, word):
- if n == 1:
- return "%d %s" % (n, word)
- else:
- return "%d %ss" % (n, word)
-
-def printlist(x, width=70, indent=4):
- """Print the elements of iterable x to stdout.
-
- Optional arg width (default 70) is the maximum line length.
- Optional arg indent (default 4) is the number of blanks with which to
- begin each line.
- """
-
- from textwrap import fill
- blanks = ' ' * indent
- # Print the sorted list: 'x' may be a '--random' list or a set()
- print(fill(' '.join(str(elt) for elt in sorted(x)), width,
- initial_indent=blanks, subsequent_indent=blanks))
-
-
-def main_in_temp_cwd():
- """Run main() in a temporary working directory."""
- if sysconfig.is_python_build():
- try:
- os.mkdir(TEMPDIR)
- except FileExistsError:
- pass
-
- # Define a writable temp dir that will be used as cwd while running
- # the tests. The name of the dir includes the pid to allow parallel
- # testing (see the -j option).
- test_cwd = 'test_python_{}'.format(os.getpid())
- test_cwd = os.path.join(TEMPDIR, test_cwd)
-
- # Run the tests in a context manager that temporarily changes the CWD to a
- # temporary and writable directory. If it's not possible to create or
- # change the CWD, the original CWD will be used. The original CWD is
- # available from support.SAVEDCWD.
- with support.temp_cwd(test_cwd, quiet=True):
- main()
-
-
-if __name__ == '__main__':
# Remove regrtest.py's own directory from the module search path. Despite
# the elimination of implicit relative imports, this is still needed to
# ensure that submodules of the test package do not inappropriately appear
# as top-level modules even when people (or buildbots!) invoke regrtest.py
# directly instead of using the -m switch
mydir = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0])))
- i = len(sys.path)
+ i = len(sys.path) - 1
while i >= 0:
- i -= 1
if os.path.abspath(os.path.normpath(sys.path[i])) == mydir:
del sys.path[i]
+ else:
+ i -= 1
# findtestdir() gets the dirname out of __file__, so we have to make it
# absolute before changing the working directory.
@@ -1680,4 +43,8 @@ if __name__ == '__main__':
# sanity check
assert __file__ == os.path.abspath(sys.argv[0])
- main_in_temp_cwd()
+ main()
+
+
+if __name__ == '__main__':
+ _main()
diff --git a/Lib/test/seq_tests.py b/Lib/test/seq_tests.py
index 72f4845..1e7a6f6 100644
--- a/Lib/test/seq_tests.py
+++ b/Lib/test/seq_tests.py
@@ -318,7 +318,6 @@ class CommonTest(unittest.TestCase):
self.assertEqual(id(s), id(s*1))
def test_bigrepeat(self):
- import sys
if sys.maxsize <= 2147483647:
x = self.type2test([0])
x *= 2**16
diff --git a/Lib/test/signalinterproctester.py b/Lib/test/signalinterproctester.py
new file mode 100644
index 0000000..d3ae170
--- /dev/null
+++ b/Lib/test/signalinterproctester.py
@@ -0,0 +1,84 @@
+import os
+import signal
+import subprocess
+import sys
+import time
+import unittest
+
+
+class SIGUSR1Exception(Exception):
+ pass
+
+
+class InterProcessSignalTests(unittest.TestCase):
+ def setUp(self):
+ self.got_signals = {'SIGHUP': 0, 'SIGUSR1': 0, 'SIGALRM': 0}
+
+ def sighup_handler(self, signum, frame):
+ self.got_signals['SIGHUP'] += 1
+
+ def sigusr1_handler(self, signum, frame):
+ self.got_signals['SIGUSR1'] += 1
+ raise SIGUSR1Exception
+
+ def wait_signal(self, child, signame, exc_class=None):
+ try:
+ if child is not None:
+ # This wait should be interrupted by exc_class
+ # (if set)
+ child.wait()
+
+ timeout = 10.0
+ deadline = time.monotonic() + timeout
+
+ while time.monotonic() < deadline:
+ if self.got_signals[signame]:
+ return
+ signal.pause()
+ except BaseException as exc:
+ if exc_class is not None and isinstance(exc, exc_class):
+ # got the expected exception
+ return
+ raise
+
+ self.fail('signal %s not received after %s seconds'
+ % (signame, timeout))
+
+ def subprocess_send_signal(self, pid, signame):
+ code = 'import os, signal; os.kill(%s, signal.%s)' % (pid, signame)
+ args = [sys.executable, '-I', '-c', code]
+ return subprocess.Popen(args)
+
+ def test_interprocess_signal(self):
+ # Install handlers. This function runs in a sub-process, so we
+ # don't worry about re-setting the default handlers.
+ signal.signal(signal.SIGHUP, self.sighup_handler)
+ signal.signal(signal.SIGUSR1, self.sigusr1_handler)
+ signal.signal(signal.SIGUSR2, signal.SIG_IGN)
+ signal.signal(signal.SIGALRM, signal.default_int_handler)
+
+ # Let the sub-processes know who to send signals to.
+ pid = str(os.getpid())
+
+ with self.subprocess_send_signal(pid, "SIGHUP") as child:
+ self.wait_signal(child, 'SIGHUP')
+ self.assertEqual(self.got_signals, {'SIGHUP': 1, 'SIGUSR1': 0,
+ 'SIGALRM': 0})
+
+ with self.subprocess_send_signal(pid, "SIGUSR1") as child:
+ self.wait_signal(child, 'SIGUSR1', SIGUSR1Exception)
+ self.assertEqual(self.got_signals, {'SIGHUP': 1, 'SIGUSR1': 1,
+ 'SIGALRM': 0})
+
+ with self.subprocess_send_signal(pid, "SIGUSR2") as child:
+ # Nothing should happen: SIGUSR2 is ignored
+ child.wait()
+
+ signal.alarm(1)
+ self.wait_signal(None, 'SIGALRM', KeyboardInterrupt)
+ self.assertEqual(self.got_signals, {'SIGHUP': 1, 'SIGUSR1': 1,
+ 'SIGALRM': 0})
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/Lib/test/sortperf.py b/Lib/test/sortperf.py
index 90722f7..171e5ce 100644
--- a/Lib/test/sortperf.py
+++ b/Lib/test/sortperf.py
@@ -64,7 +64,7 @@ def doit(L):
flush()
def tabulate(r):
- """Tabulate sort speed for lists of various sizes.
+ r"""Tabulate sort speed for lists of various sizes.
The sizes are 2**i for i in r (the argument, a list).
diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
index d0d126a..15d8fc8 100644
--- a/Lib/test/support/__init__.py
+++ b/Lib/test/support/__init__.py
@@ -26,6 +26,7 @@ import sys
import sysconfig
import tempfile
import time
+import types
import unittest
import urllib.error
import warnings
@@ -89,11 +90,13 @@ __all__ = [
"bigmemtest", "bigaddrspacetest", "cpython_only", "get_attribute",
"requires_IEEE_754", "skip_unless_xattr", "requires_zlib",
"anticipate_failure", "load_package_tests", "detect_api_mismatch",
- "requires_multiprocessing_queue",
+ "check__all__", "requires_android_level", "requires_multiprocessing_queue",
# sys
- "is_jython", "check_impl_detail",
+ "is_jython", "is_android", "check_impl_detail", "unix_shell",
+ "setswitchinterval", "android_not_root",
# network
"HOST", "IPV6_ENABLED", "find_unused_port", "bind_port", "open_urlresource",
+ "bind_unix_socket",
# processes
'temp_umask', "reap_children",
# logging
@@ -104,7 +107,7 @@ __all__ = [
"check_warnings", "check_no_resource_warning", "EnvironmentVarGuard",
"run_with_locale", "swap_item",
"swap_attr", "Matcher", "set_memlimit", "SuppressCrashReport", "sortdict",
- "run_with_tz",
+ "run_with_tz", "PGO", "missing_compiler_executable",
]
class Error(Exception):
@@ -706,6 +709,15 @@ def bind_port(sock, host=HOST):
port = sock.getsockname()[1]
return port
+def bind_unix_socket(sock, addr):
+ """Bind a unix socket, raising SkipTest if PermissionError is raised."""
+ assert sock.family == socket.AF_UNIX
+ try:
+ sock.bind(addr)
+ except PermissionError:
+ sock.close()
+ raise unittest.SkipTest('cannot bind AF_UNIX sockets')
+
def _is_ipv6_enabled():
"""Check whether IPv6 is enabled on this host."""
if socket.has_ipv6:
@@ -765,6 +777,15 @@ requires_lzma = unittest.skipUnless(lzma, 'requires lzma')
is_jython = sys.platform.startswith('java')
+_ANDROID_API_LEVEL = sysconfig.get_config_var('ANDROID_API_LEVEL')
+is_android = (_ANDROID_API_LEVEL is not None and _ANDROID_API_LEVEL > 0)
+android_not_root = (is_android and os.geteuid() != 0)
+
+if sys.platform != 'win32':
+ unix_shell = '/system/bin/sh' if is_android else '/bin/sh'
+else:
+ unix_shell = None
+
# Filename used for testing
if os.name == 'java':
# Jython disallows @ in module names
@@ -834,7 +855,7 @@ TESTFN_ENCODING = sys.getfilesystemencoding()
# encoded by the filesystem encoding (in strict mode). It can be None if we
# cannot generate such filename.
TESTFN_UNENCODABLE = None
-if os.name in ('nt', 'ce'):
+if os.name == 'nt':
# skip win32s (0) or Windows 9x/ME (1)
if sys.getwindowsversion().platform >= 2:
# Different kinds of characters from various languages to minimize the
@@ -901,6 +922,10 @@ else:
# Save the initial cwd
SAVEDCWD = os.getcwd()
+# Set by libregrtest/main.py so we can skip tests that are not
+# useful for PGO
+PGO = False
+
@contextlib.contextmanager
def temp_dir(path=None, quiet=False):
"""Return a context manager that creates a temporary directory.
@@ -1751,6 +1776,13 @@ def requires_resource(resource):
else:
return unittest.skip("resource {0!r} is not enabled".format(resource))
+def requires_android_level(level, reason):
+ if is_android and _ANDROID_API_LEVEL < level:
+ return unittest.skip('%s at Android API level %d' %
+ (reason, _ANDROID_API_LEVEL))
+ else:
+ return _id
+
def cpython_only(test):
"""
Decorator for tests only applicable on CPython.
@@ -2133,6 +2165,11 @@ def args_from_interpreter_flags():
settings in sys.flags and sys.warnoptions."""
return subprocess._args_from_interpreter_flags()
+def optim_args_from_interpreter_flags():
+ """Return a list of command-line arguments reproducing the current
+ optimization settings in sys.flags."""
+ return subprocess._optim_args_from_interpreter_flags()
+
#============================================================
# Support for assertions about logging.
#============================================================
@@ -2239,7 +2276,7 @@ def can_xattr():
os.setxattr(fp.fileno(), b"user.test", b"")
# Kernels < 2.6.39 don't respect setxattr flags.
kernel_version = platform.release()
- m = re.match("2.6.(\d{1,2})", kernel_version)
+ m = re.match(r"2.6.(\d{1,2})", kernel_version)
can = m is None or int(m.group(1)) >= 39
except OSError:
can = False
@@ -2284,6 +2321,65 @@ def detect_api_mismatch(ref_api, other_api, *, ignore=()):
return missing_items
+def check__all__(test_case, module, name_of_module=None, extra=(),
+ blacklist=()):
+ """Assert that the __all__ variable of 'module' contains all public names.
+
+ The module's public names (its API) are detected automatically based on
+ whether they match the public name convention and were defined in
+ 'module'.
+
+ The 'name_of_module' argument can specify (as a string or tuple thereof)
+ what module(s) an API could be defined in in order to be detected as a
+ public API. One case for this is when 'module' imports part of its public
+ API from other modules, possibly a C backend (like 'csv' and its '_csv').
+
+ The 'extra' argument can be a set of names that wouldn't otherwise be
+ automatically detected as "public", like objects without a proper
+ '__module__' attriubute. If provided, it will be added to the
+ automatically detected ones.
+
+ The 'blacklist' argument can be a set of names that must not be treated
+ as part of the public API even though their names indicate otherwise.
+
+ Usage:
+ import bar
+ import foo
+ import unittest
+ from test import support
+
+ class MiscTestCase(unittest.TestCase):
+ def test__all__(self):
+ support.check__all__(self, foo)
+
+ class OtherTestCase(unittest.TestCase):
+ def test__all__(self):
+ extra = {'BAR_CONST', 'FOO_CONST'}
+ blacklist = {'baz'} # Undocumented name.
+ # bar imports part of its API from _bar.
+ support.check__all__(self, bar, ('bar', '_bar'),
+ extra=extra, blacklist=blacklist)
+
+ """
+
+ if name_of_module is None:
+ name_of_module = (module.__name__, )
+ elif isinstance(name_of_module, str):
+ name_of_module = (name_of_module, )
+
+ expected = set(extra)
+
+ for name in dir(module):
+ if name.startswith('_') or name in blacklist:
+ continue
+ obj = getattr(module, name)
+ if (getattr(obj, '__module__', None) in name_of_module or
+ (not hasattr(obj, '__module__') and
+ not isinstance(obj, types.ModuleType))):
+ expected.add(name)
+ test_case.assertCountEqual(module.__all__, expected)
+
+
class SuppressCrashReport:
"""Try to prevent a crash report from popping up.
@@ -2445,3 +2541,43 @@ def check_free_after_iterating(test, iter, cls, args=()):
# The sequence should be deallocated just after the end of iterating
gc_collect()
test.assertTrue(done)
+
+
+def missing_compiler_executable(cmd_names=[]):
+ """Check if the compiler components used to build the interpreter exist.
+
+ Check for the existence of the compiler executables whose names are listed
+ in 'cmd_names' or all the compiler executables when 'cmd_names' is empty
+ and return the first missing executable or None when none is found
+ missing.
+
+ """
+ from distutils import ccompiler, sysconfig, spawn
+ compiler = ccompiler.new_compiler()
+ sysconfig.customize_compiler(compiler)
+ for name in compiler.executables:
+ if cmd_names and name not in cmd_names:
+ continue
+ cmd = getattr(compiler, name)
+ if cmd_names:
+ assert cmd is not None, \
+ "the '%s' executable is not configured" % name
+ elif cmd is None:
+ continue
+ if spawn.find_executable(cmd[0]) is None:
+ return cmd[0]
+
+
+_is_android_emulator = None
+def setswitchinterval(interval):
+ # Setting a very low gil interval on the Android emulator causes python
+ # to hang (issue #26939).
+ minimum_interval = 1e-5
+ if is_android and interval < minimum_interval:
+ global _is_android_emulator
+ if _is_android_emulator is None:
+ _is_android_emulator = (subprocess.check_output(
+ ['getprop', 'ro.kernel.qemu']).strip() == b'1')
+ if _is_android_emulator:
+ interval = minimum_interval
+ return sys.setswitchinterval(interval)
diff --git a/Lib/test/test___all__.py b/Lib/test/test___all__.py
index e94d984..ae9114e 100644
--- a/Lib/test/test___all__.py
+++ b/Lib/test/test___all__.py
@@ -38,6 +38,8 @@ class AllTest(unittest.TestCase):
modname, e.__class__.__name__, e))
if "__builtins__" in names:
del names["__builtins__"]
+ if '__annotations__' in names:
+ del names['__annotations__']
keys = set(names)
all_list = sys.modules[modname].__all__
all_set = set(all_list)
diff --git a/Lib/test/test__locale.py b/Lib/test/test__locale.py
index 58f2f04..ab4e247 100644
--- a/Lib/test/test__locale.py
+++ b/Lib/test/test__locale.py
@@ -4,7 +4,6 @@ try:
except ImportError:
nl_langinfo = None
-import codecs
import locale
import sys
import unittest
diff --git a/Lib/test/test__osx_support.py b/Lib/test/test__osx_support.py
index ac6325a..bcba8ca 100644
--- a/Lib/test/test__osx_support.py
+++ b/Lib/test/test__osx_support.py
@@ -4,7 +4,6 @@ Test suite for _osx_support: shared OS X support functions.
import os
import platform
-import shutil
import stat
import sys
import unittest
diff --git a/Lib/test/test_aifc.py b/Lib/test/test_aifc.py
index ab51437..1bd1f89 100644
--- a/Lib/test/test_aifc.py
+++ b/Lib/test/test_aifc.py
@@ -2,7 +2,6 @@ from test.support import findfile, TESTFN, unlink
import unittest
from test import audiotests
from audioop import byteswap
-import os
import io
import sys
import struct
diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py
index 4779a13..bc83161 100644
--- a/Lib/test/test_argparse.py
+++ b/Lib/test/test_argparse.py
@@ -4512,6 +4512,21 @@ class TestStrings(TestCase):
string = "Namespace(bar='spam', foo=42)"
self.assertStringEqual(ns, string)
+ def test_namespace_starkwargs_notidentifier(self):
+ ns = argparse.Namespace(**{'"': 'quote'})
+ string = """Namespace(**{'"': 'quote'})"""
+ self.assertStringEqual(ns, string)
+
+ def test_namespace_kwargs_and_starkwargs_notidentifier(self):
+ ns = argparse.Namespace(a=1, **{'"': 'quote'})
+ string = """Namespace(a=1, **{'"': 'quote'})"""
+ self.assertStringEqual(ns, string)
+
+ def test_namespace_starkwargs_identifier(self):
+ ns = argparse.Namespace(**{'valid': True})
+ string = "Namespace(valid=True)"
+ self.assertStringEqual(ns, string)
+
def test_parser(self):
parser = argparse.ArgumentParser(prog='PROG')
string = (
diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py
index 2a21e74..1f8967c 100644
--- a/Lib/test/test_array.py
+++ b/Lib/test/test_array.py
@@ -7,8 +7,6 @@ from test import support
import weakref
import pickle
import operator
-import io
-import math
import struct
import sys
import warnings
@@ -328,8 +326,19 @@ class BaseTest:
d = pickle.dumps((itorig, orig), proto)
it, a = pickle.loads(d)
a.fromlist(data2)
- self.assertEqual(type(it), type(itorig))
- self.assertEqual(list(it), data2)
+ self.assertEqual(list(it), [])
+
+ def test_exhausted_iterator(self):
+ a = array.array(self.typecode, self.example)
+ self.assertEqual(list(a), list(self.example))
+ exhit = iter(a)
+ empit = iter(a)
+ for x in exhit: # exhaust the iterator
+ next(empit) # not exhausted
+ a.append(self.outside)
+ self.assertEqual(list(exhit), [])
+ self.assertEqual(list(empit), [self.outside])
+ self.assertEqual(list(a), list(self.example) + [self.outside])
def test_insert(self):
a = array.array(self.typecode, self.example)
@@ -1080,6 +1089,12 @@ class BaseTest:
a = array.array('B', b"")
self.assertRaises(BufferError, getbuffer_with_null_view, a)
+ def test_free_after_iterating(self):
+ support.check_free_after_iterating(self, iter, array.array,
+ (self.typecode,))
+ support.check_free_after_iterating(self, reversed, array.array,
+ (self.typecode,))
+
class StringTest(BaseTest):
def test_setitem(self):
diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py
index d3e6d35..8c62408 100644
--- a/Lib/test/test_ast.py
+++ b/Lib/test/test_ast.py
@@ -1,7 +1,8 @@
+import ast
+import dis
import os
import sys
import unittest
-import ast
import weakref
from test import support
@@ -115,6 +116,8 @@ exec_tests = [
# PEP 448: Additional Unpacking Generalizations
"{**{1:2}, 2:3}",
"{*{1, 2}, 3}",
+ # Asynchronous comprehensions
+ "async def f():\n [i async for b in c]",
]
# These are compiled through "single"
@@ -238,7 +241,7 @@ class AST_Tests(unittest.TestCase):
ast_tree = compile(i, "?", kind, ast.PyCF_ONLY_AST)
self.assertEqual(to_tuple(ast_tree), o)
self._assertTrueorder(ast_tree, (0, 0))
- with self.subTest(action="compiling", input=i):
+ with self.subTest(action="compiling", input=i, kind=kind):
compile(ast_tree, "?", kind)
def test_slice(self):
@@ -547,6 +550,17 @@ class ASTHelpers_Test(unittest.TestCase):
compile(mod, 'test', 'exec')
self.assertIn("invalid integer value: None", str(cm.exception))
+ def test_level_as_none(self):
+ body = [ast.ImportFrom(module='time',
+ names=[ast.alias(name='sleep')],
+ level=None,
+ lineno=0, col_offset=0)]
+ mod = ast.Module(body)
+ code = compile(mod, 'test', 'exec')
+ ns = {}
+ exec(code, ns)
+ self.assertIn('sleep', ns)
+
class ASTValidatorTests(unittest.TestCase):
@@ -742,7 +756,7 @@ class ASTValidatorTests(unittest.TestCase):
def test_importfrom(self):
imp = ast.ImportFrom(None, [ast.alias("x", None)], -42)
- self.stmt(imp, "level less than -1")
+ self.stmt(imp, "Negative ImportFrom level")
self.stmt(ast.ImportFrom(None, [], 0), "empty names on ImportFrom")
def test_global(self):
@@ -797,21 +811,21 @@ class ASTValidatorTests(unittest.TestCase):
def _check_comprehension(self, fac):
self.expr(fac([]), "comprehension with no generators")
g = ast.comprehension(ast.Name("x", ast.Load()),
- ast.Name("x", ast.Load()), [])
+ ast.Name("x", ast.Load()), [], 0)
self.expr(fac([g]), "must have Store context")
g = ast.comprehension(ast.Name("x", ast.Store()),
- ast.Name("x", ast.Store()), [])
+ ast.Name("x", ast.Store()), [], 0)
self.expr(fac([g]), "must have Load context")
x = ast.Name("x", ast.Store())
y = ast.Name("y", ast.Load())
- g = ast.comprehension(x, y, [None])
+ g = ast.comprehension(x, y, [None], 0)
self.expr(fac([g]), "None disallowed")
- g = ast.comprehension(x, y, [ast.Name("x", ast.Store())])
+ g = ast.comprehension(x, y, [ast.Name("x", ast.Store())], 0)
self.expr(fac([g]), "must have Load context")
def _simple_comp(self, fac):
g = ast.comprehension(ast.Name("x", ast.Store()),
- ast.Name("x", ast.Load()), [])
+ ast.Name("x", ast.Load()), [], 0)
self.expr(fac(ast.Name("x", ast.Store()), [g]),
"must have Load context")
def wrap(gens):
@@ -829,7 +843,7 @@ class ASTValidatorTests(unittest.TestCase):
def test_dictcomp(self):
g = ast.comprehension(ast.Name("y", ast.Store()),
- ast.Name("p", ast.Load()), [])
+ ast.Name("p", ast.Load()), [], 0)
c = ast.DictComp(ast.Name("x", ast.Store()),
ast.Name("y", ast.Load()), [g])
self.expr(c, "must have Load context")
@@ -933,6 +947,125 @@ class ASTValidatorTests(unittest.TestCase):
compile(mod, fn, "exec")
+class ConstantTests(unittest.TestCase):
+ """Tests on the ast.Constant node type."""
+
+ def compile_constant(self, value):
+ tree = ast.parse("x = 123")
+
+ node = tree.body[0].value
+ new_node = ast.Constant(value=value)
+ ast.copy_location(new_node, node)
+ tree.body[0].value = new_node
+
+ code = compile(tree, "<string>", "exec")
+
+ ns = {}
+ exec(code, ns)
+ return ns['x']
+
+ def test_validation(self):
+ with self.assertRaises(TypeError) as cm:
+ self.compile_constant([1, 2, 3])
+ self.assertEqual(str(cm.exception),
+ "got an invalid type in Constant: list")
+
+ def test_singletons(self):
+ for const in (None, False, True, Ellipsis, b'', frozenset()):
+ with self.subTest(const=const):
+ value = self.compile_constant(const)
+ self.assertIs(value, const)
+
+ def test_values(self):
+ nested_tuple = (1,)
+ nested_frozenset = frozenset({1})
+ for level in range(3):
+ nested_tuple = (nested_tuple, 2)
+ nested_frozenset = frozenset({nested_frozenset, 2})
+ values = (123, 123.0, 123j,
+ "unicode", b'bytes',
+ tuple("tuple"), frozenset("frozenset"),
+ nested_tuple, nested_frozenset)
+ for value in values:
+ with self.subTest(value=value):
+ result = self.compile_constant(value)
+ self.assertEqual(result, value)
+
+ def test_assign_to_constant(self):
+ tree = ast.parse("x = 1")
+
+ target = tree.body[0].targets[0]
+ new_target = ast.Constant(value=1)
+ ast.copy_location(new_target, target)
+ tree.body[0].targets[0] = new_target
+
+ with self.assertRaises(ValueError) as cm:
+ compile(tree, "string", "exec")
+ self.assertEqual(str(cm.exception),
+ "expression which can't be assigned "
+ "to in Store context")
+
+ def test_get_docstring(self):
+ tree = ast.parse("'docstring'\nx = 1")
+ self.assertEqual(ast.get_docstring(tree), 'docstring')
+
+ tree.body[0].value = ast.Constant(value='constant docstring')
+ self.assertEqual(ast.get_docstring(tree), 'constant docstring')
+
+ def get_load_const(self, tree):
+ # Compile to bytecode, disassemble and get parameter of LOAD_CONST
+ # instructions
+ co = compile(tree, '<string>', 'exec')
+ consts = []
+ for instr in dis.get_instructions(co):
+ if instr.opname == 'LOAD_CONST':
+ consts.append(instr.argval)
+ return consts
+
+ @support.cpython_only
+ def test_load_const(self):
+ consts = [None,
+ True, False,
+ 124,
+ 2.0,
+ 3j,
+ "unicode",
+ b'bytes',
+ (1, 2, 3)]
+
+ code = '\n'.join(['x={!r}'.format(const) for const in consts])
+ code += '\nx = ...'
+ consts.extend((Ellipsis, None))
+
+ tree = ast.parse(code)
+ self.assertEqual(self.get_load_const(tree),
+ consts)
+
+ # Replace expression nodes with constants
+ for assign, const in zip(tree.body, consts):
+ assert isinstance(assign, ast.Assign), ast.dump(assign)
+ new_node = ast.Constant(value=const)
+ ast.copy_location(new_node, assign.value)
+ assign.value = new_node
+
+ self.assertEqual(self.get_load_const(tree),
+ consts)
+
+ def test_literal_eval(self):
+ tree = ast.parse("1 + 2")
+ binop = tree.body[0].value
+
+ new_left = ast.Constant(value=10)
+ ast.copy_location(new_left, binop.left)
+ binop.left = new_left
+
+ new_right = ast.Constant(value=20)
+ ast.copy_location(new_right, binop.right)
+ binop.right = new_right
+
+ self.assertEqual(ast.literal_eval(binop), 30)
+
+
def main():
if __name__ != '__main__':
return
@@ -940,8 +1073,9 @@ def main():
for statements, kind in ((exec_tests, "exec"), (single_tests, "single"),
(eval_tests, "eval")):
print(kind+"_results = [")
- for s in statements:
- print(repr(to_tuple(compile(s, "?", kind, 0x400)))+",")
+ for statement in statements:
+ tree = ast.parse(statement, "?", kind)
+ print("%r," % (to_tuple(tree),))
print("]")
print("main()")
raise SystemExit
@@ -979,19 +1113,20 @@ exec_results = [
('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Break', (1, 11))], [])]),
('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Continue', (1, 11))], [])]),
('Module', [('For', (1, 0), ('Tuple', (1, 4), [('Name', (1, 4), 'a', ('Store',)), ('Name', (1, 6), 'b', ('Store',))], ('Store',)), ('Name', (1, 11), 'c', ('Load',)), [('Pass', (1, 14))], [])]),
-('Module', [('Expr', (1, 0), ('ListComp', (1, 1), ('Tuple', (1, 2), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11), [('Name', (1, 11), 'a', ('Store',)), ('Name', (1, 13), 'b', ('Store',))], ('Store',)), ('Name', (1, 18), 'c', ('Load',)), [])]))]),
-('Module', [('Expr', (1, 0), ('GeneratorExp', (1, 1), ('Tuple', (1, 2), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11), [('Name', (1, 11), 'a', ('Store',)), ('Name', (1, 13), 'b', ('Store',))], ('Store',)), ('Name', (1, 18), 'c', ('Load',)), [])]))]),
-('Module', [('Expr', (1, 0), ('GeneratorExp', (1, 1), ('Tuple', (1, 2), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 12), [('Name', (1, 12), 'a', ('Store',)), ('Name', (1, 14), 'b', ('Store',))], ('Store',)), ('Name', (1, 20), 'c', ('Load',)), [])]))]),
-('Module', [('Expr', (1, 0), ('GeneratorExp', (2, 4), ('Tuple', (3, 4), [('Name', (3, 4), 'Aa', ('Load',)), ('Name', (5, 7), 'Bb', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (8, 4), [('Name', (8, 4), 'Aa', ('Store',)), ('Name', (10, 4), 'Bb', ('Store',))], ('Store',)), ('Name', (10, 10), 'Cc', ('Load',)), [])]))]),
-('Module', [('Expr', (1, 0), ('DictComp', (1, 0), ('Name', (1, 1), 'a', ('Load',)), ('Name', (1, 5), 'b', ('Load',)), [('comprehension', ('Name', (1, 11), 'w', ('Store',)), ('Name', (1, 16), 'x', ('Load',)), []), ('comprehension', ('Name', (1, 22), 'm', ('Store',)), ('Name', (1, 27), 'p', ('Load',)), [('Name', (1, 32), 'g', ('Load',))])]))]),
-('Module', [('Expr', (1, 0), ('DictComp', (1, 0), ('Name', (1, 1), 'a', ('Load',)), ('Name', (1, 5), 'b', ('Load',)), [('comprehension', ('Tuple', (1, 11), [('Name', (1, 11), 'v', ('Store',)), ('Name', (1, 13), 'w', ('Store',))], ('Store',)), ('Name', (1, 18), 'x', ('Load',)), [])]))]),
-('Module', [('Expr', (1, 0), ('SetComp', (1, 0), ('Name', (1, 1), 'r', ('Load',)), [('comprehension', ('Name', (1, 7), 'l', ('Store',)), ('Name', (1, 12), 'x', ('Load',)), [('Name', (1, 17), 'g', ('Load',))])]))]),
-('Module', [('Expr', (1, 0), ('SetComp', (1, 0), ('Name', (1, 1), 'r', ('Load',)), [('comprehension', ('Tuple', (1, 7), [('Name', (1, 7), 'l', ('Store',)), ('Name', (1, 9), 'm', ('Store',))], ('Store',)), ('Name', (1, 14), 'x', ('Load',)), [])]))]),
+('Module', [('Expr', (1, 0), ('ListComp', (1, 1), ('Tuple', (1, 2), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11), [('Name', (1, 11), 'a', ('Store',)), ('Name', (1, 13), 'b', ('Store',))], ('Store',)), ('Name', (1, 18), 'c', ('Load',)), [], 0)]))]),
+('Module', [('Expr', (1, 0), ('GeneratorExp', (1, 1), ('Tuple', (1, 2), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11), [('Name', (1, 11), 'a', ('Store',)), ('Name', (1, 13), 'b', ('Store',))], ('Store',)), ('Name', (1, 18), 'c', ('Load',)), [], 0)]))]),
+('Module', [('Expr', (1, 0), ('GeneratorExp', (1, 1), ('Tuple', (1, 2), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 12), [('Name', (1, 12), 'a', ('Store',)), ('Name', (1, 14), 'b', ('Store',))], ('Store',)), ('Name', (1, 20), 'c', ('Load',)), [], 0)]))]),
+('Module', [('Expr', (1, 0), ('GeneratorExp', (2, 4), ('Tuple', (3, 4), [('Name', (3, 4), 'Aa', ('Load',)), ('Name', (5, 7), 'Bb', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (8, 4), [('Name', (8, 4), 'Aa', ('Store',)), ('Name', (10, 4), 'Bb', ('Store',))], ('Store',)), ('Name', (10, 10), 'Cc', ('Load',)), [], 0)]))]),
+('Module', [('Expr', (1, 0), ('DictComp', (1, 0), ('Name', (1, 1), 'a', ('Load',)), ('Name', (1, 5), 'b', ('Load',)), [('comprehension', ('Name', (1, 11), 'w', ('Store',)), ('Name', (1, 16), 'x', ('Load',)), [], 0), ('comprehension', ('Name', (1, 22), 'm', ('Store',)), ('Name', (1, 27), 'p', ('Load',)), [('Name', (1, 32), 'g', ('Load',))], 0)]))]),
+('Module', [('Expr', (1, 0), ('DictComp', (1, 0), ('Name', (1, 1), 'a', ('Load',)), ('Name', (1, 5), 'b', ('Load',)), [('comprehension', ('Tuple', (1, 11), [('Name', (1, 11), 'v', ('Store',)), ('Name', (1, 13), 'w', ('Store',))], ('Store',)), ('Name', (1, 18), 'x', ('Load',)), [], 0)]))]),
+('Module', [('Expr', (1, 0), ('SetComp', (1, 0), ('Name', (1, 1), 'r', ('Load',)), [('comprehension', ('Name', (1, 7), 'l', ('Store',)), ('Name', (1, 12), 'x', ('Load',)), [('Name', (1, 17), 'g', ('Load',))], 0)]))]),
+('Module', [('Expr', (1, 0), ('SetComp', (1, 0), ('Name', (1, 1), 'r', ('Load',)), [('comprehension', ('Tuple', (1, 7), [('Name', (1, 7), 'l', ('Store',)), ('Name', (1, 9), 'm', ('Store',))], ('Store',)), ('Name', (1, 14), 'x', ('Load',)), [], 0)]))]),
('Module', [('AsyncFunctionDef', (1, 6), 'f', ('arguments', [], None, [], [], None, []), [('Expr', (2, 1), ('Await', (2, 1), ('Call', (2, 7), ('Name', (2, 7), 'something', ('Load',)), [], [])))], [], None)]),
('Module', [('AsyncFunctionDef', (1, 6), 'f', ('arguments', [], None, [], [], None, []), [('AsyncFor', (2, 7), ('Name', (2, 11), 'e', ('Store',)), ('Name', (2, 16), 'i', ('Load',)), [('Expr', (2, 19), ('Num', (2, 19), 1))], [('Expr', (3, 7), ('Num', (3, 7), 2))])], [], None)]),
('Module', [('AsyncFunctionDef', (1, 6), 'f', ('arguments', [], None, [], [], None, []), [('AsyncWith', (2, 7), [('withitem', ('Name', (2, 12), 'a', ('Load',)), ('Name', (2, 17), 'b', ('Store',)))], [('Expr', (2, 20), ('Num', (2, 20), 1))])], [], None)]),
('Module', [('Expr', (1, 0), ('Dict', (1, 0), [None, ('Num', (1, 10), 2)], [('Dict', (1, 3), [('Num', (1, 4), 1)], [('Num', (1, 6), 2)]), ('Num', (1, 12), 3)]))]),
('Module', [('Expr', (1, 0), ('Set', (1, 0), [('Starred', (1, 1), ('Set', (1, 2), [('Num', (1, 3), 1), ('Num', (1, 6), 2)]), ('Load',)), ('Num', (1, 10), 3)]))]),
+('Module', [('AsyncFunctionDef', (1, 6), 'f', ('arguments', [], None, [], [], None, []), [('Expr', (2, 1), ('ListComp', (2, 2), ('Name', (2, 2), 'i', ('Load',)), [('comprehension', ('Name', (2, 14), 'b', ('Store',)), ('Name', (2, 19), 'c', ('Load',)), [], 1)]))], [], None)]),
]
single_results = [
('Interactive', [('Expr', (1, 0), ('BinOp', (1, 0), ('Num', (1, 0), 1), ('Add',), ('Num', (1, 2), 2)))]),
@@ -1006,8 +1141,8 @@ eval_results = [
('Expression', ('Dict', (1, 0), [], [])),
('Expression', ('Set', (1, 0), [('NameConstant', (1, 1), None)])),
('Expression', ('Dict', (1, 0), [('Num', (2, 6), 1)], [('Num', (4, 10), 2)])),
-('Expression', ('ListComp', (1, 1), ('Name', (1, 1), 'a', ('Load',)), [('comprehension', ('Name', (1, 7), 'b', ('Store',)), ('Name', (1, 12), 'c', ('Load',)), [('Name', (1, 17), 'd', ('Load',))])])),
-('Expression', ('GeneratorExp', (1, 1), ('Name', (1, 1), 'a', ('Load',)), [('comprehension', ('Name', (1, 7), 'b', ('Store',)), ('Name', (1, 12), 'c', ('Load',)), [('Name', (1, 17), 'd', ('Load',))])])),
+('Expression', ('ListComp', (1, 1), ('Name', (1, 1), 'a', ('Load',)), [('comprehension', ('Name', (1, 7), 'b', ('Store',)), ('Name', (1, 12), 'c', ('Load',)), [('Name', (1, 17), 'd', ('Load',))], 0)])),
+('Expression', ('GeneratorExp', (1, 1), ('Name', (1, 1), 'a', ('Load',)), [('comprehension', ('Name', (1, 7), 'b', ('Store',)), ('Name', (1, 12), 'c', ('Load',)), [('Name', (1, 17), 'd', ('Load',))], 0)])),
('Expression', ('Compare', (1, 0), ('Num', (1, 0), 1), [('Lt',), ('Lt',)], [('Num', (1, 4), 2), ('Num', (1, 8), 3)])),
('Expression', ('Call', (1, 0), ('Name', (1, 0), 'f', ('Load',)), [('Num', (1, 2), 1), ('Num', (1, 4), 2), ('Starred', (1, 10), ('Name', (1, 11), 'd', ('Load',)), ('Load',))], [('keyword', 'c', ('Num', (1, 8), 3)), ('keyword', None, ('Name', (1, 15), 'e', ('Load',)))])),
('Expression', ('Num', (1, 0), 10)),
diff --git a/Lib/test/test_asyncgen.py b/Lib/test/test_asyncgen.py
new file mode 100644
index 0000000..34ab8a0
--- /dev/null
+++ b/Lib/test/test_asyncgen.py
@@ -0,0 +1,1044 @@
+import inspect
+import sys
+import types
+import unittest
+
+from unittest import mock
+
+from test.support import import_module
+asyncio = import_module("asyncio")
+
+
+class AwaitException(Exception):
+ pass
+
+
+@types.coroutine
+def awaitable(*, throw=False):
+ if throw:
+ yield ('throw',)
+ else:
+ yield ('result',)
+
+
+def run_until_complete(coro):
+ exc = False
+ while True:
+ try:
+ if exc:
+ exc = False
+ fut = coro.throw(AwaitException)
+ else:
+ fut = coro.send(None)
+ except StopIteration as ex:
+ return ex.args[0]
+
+ if fut == ('throw',):
+ exc = True
+
+
+def to_list(gen):
+ async def iterate():
+ res = []
+ async for i in gen:
+ res.append(i)
+ return res
+
+ return run_until_complete(iterate())
+
+
+class AsyncGenSyntaxTest(unittest.TestCase):
+
+ def test_async_gen_syntax_01(self):
+ code = '''async def foo():
+ await abc
+ yield from 123
+ '''
+
+ with self.assertRaisesRegex(SyntaxError, 'yield from.*inside async'):
+ exec(code, {}, {})
+
+ def test_async_gen_syntax_02(self):
+ code = '''async def foo():
+ yield from 123
+ '''
+
+ with self.assertRaisesRegex(SyntaxError, 'yield from.*inside async'):
+ exec(code, {}, {})
+
+ def test_async_gen_syntax_03(self):
+ code = '''async def foo():
+ await abc
+ yield
+ return 123
+ '''
+
+ with self.assertRaisesRegex(SyntaxError, 'return.*value.*async gen'):
+ exec(code, {}, {})
+
+ def test_async_gen_syntax_04(self):
+ code = '''async def foo():
+ yield
+ return 123
+ '''
+
+ with self.assertRaisesRegex(SyntaxError, 'return.*value.*async gen'):
+ exec(code, {}, {})
+
+ def test_async_gen_syntax_05(self):
+ code = '''async def foo():
+ if 0:
+ yield
+ return 12
+ '''
+
+ with self.assertRaisesRegex(SyntaxError, 'return.*value.*async gen'):
+ exec(code, {}, {})
+
+
+class AsyncGenTest(unittest.TestCase):
+
+ def compare_generators(self, sync_gen, async_gen):
+ def sync_iterate(g):
+ res = []
+ while True:
+ try:
+ res.append(g.__next__())
+ except StopIteration:
+ res.append('STOP')
+ break
+ except Exception as ex:
+ res.append(str(type(ex)))
+ return res
+
+ def async_iterate(g):
+ res = []
+ while True:
+ try:
+ g.__anext__().__next__()
+ except StopAsyncIteration:
+ res.append('STOP')
+ break
+ except StopIteration as ex:
+ if ex.args:
+ res.append(ex.args[0])
+ else:
+ res.append('EMPTY StopIteration')
+ break
+ except Exception as ex:
+ res.append(str(type(ex)))
+ return res
+
+ sync_gen_result = sync_iterate(sync_gen)
+ async_gen_result = async_iterate(async_gen)
+ self.assertEqual(sync_gen_result, async_gen_result)
+ return async_gen_result
+
+ def test_async_gen_iteration_01(self):
+ async def gen():
+ await awaitable()
+ a = yield 123
+ self.assertIs(a, None)
+ await awaitable()
+ yield 456
+ await awaitable()
+ yield 789
+
+ self.assertEqual(to_list(gen()), [123, 456, 789])
+
+ def test_async_gen_iteration_02(self):
+ async def gen():
+ await awaitable()
+ yield 123
+ await awaitable()
+
+ g = gen()
+ ai = g.__aiter__()
+ self.assertEqual(ai.__anext__().__next__(), ('result',))
+
+ try:
+ ai.__anext__().__next__()
+ except StopIteration as ex:
+ self.assertEqual(ex.args[0], 123)
+ else:
+ self.fail('StopIteration was not raised')
+
+ self.assertEqual(ai.__anext__().__next__(), ('result',))
+
+ try:
+ ai.__anext__().__next__()
+ except StopAsyncIteration as ex:
+ self.assertFalse(ex.args)
+ else:
+ self.fail('StopAsyncIteration was not raised')
+
+ def test_async_gen_exception_03(self):
+ async def gen():
+ await awaitable()
+ yield 123
+ await awaitable(throw=True)
+ yield 456
+
+ with self.assertRaises(AwaitException):
+ to_list(gen())
+
+ def test_async_gen_exception_04(self):
+ async def gen():
+ await awaitable()
+ yield 123
+ 1 / 0
+
+ g = gen()
+ ai = g.__aiter__()
+ self.assertEqual(ai.__anext__().__next__(), ('result',))
+
+ try:
+ ai.__anext__().__next__()
+ except StopIteration as ex:
+ self.assertEqual(ex.args[0], 123)
+ else:
+ self.fail('StopIteration was not raised')
+
+ with self.assertRaises(ZeroDivisionError):
+ ai.__anext__().__next__()
+
+ def test_async_gen_exception_05(self):
+ async def gen():
+ yield 123
+ raise StopAsyncIteration
+
+ with self.assertRaisesRegex(RuntimeError,
+ 'async generator.*StopAsyncIteration'):
+ to_list(gen())
+
+ def test_async_gen_exception_06(self):
+ async def gen():
+ yield 123
+ raise StopIteration
+
+ with self.assertRaisesRegex(RuntimeError,
+ 'async generator.*StopIteration'):
+ to_list(gen())
+
+ def test_async_gen_exception_07(self):
+ def sync_gen():
+ try:
+ yield 1
+ 1 / 0
+ finally:
+ yield 2
+ yield 3
+
+ yield 100
+
+ async def async_gen():
+ try:
+ yield 1
+ 1 / 0
+ finally:
+ yield 2
+ yield 3
+
+ yield 100
+
+ self.compare_generators(sync_gen(), async_gen())
+
+ def test_async_gen_exception_08(self):
+ def sync_gen():
+ try:
+ yield 1
+ finally:
+ yield 2
+ 1 / 0
+ yield 3
+
+ yield 100
+
+ async def async_gen():
+ try:
+ yield 1
+ await awaitable()
+ finally:
+ await awaitable()
+ yield 2
+ 1 / 0
+ yield 3
+
+ yield 100
+
+ self.compare_generators(sync_gen(), async_gen())
+
+ def test_async_gen_exception_09(self):
+ def sync_gen():
+ try:
+ yield 1
+ 1 / 0
+ finally:
+ yield 2
+ yield 3
+
+ yield 100
+
+ async def async_gen():
+ try:
+ await awaitable()
+ yield 1
+ 1 / 0
+ finally:
+ yield 2
+ await awaitable()
+ yield 3
+
+ yield 100
+
+ self.compare_generators(sync_gen(), async_gen())
+
+ def test_async_gen_exception_10(self):
+ async def gen():
+ yield 123
+ with self.assertRaisesRegex(TypeError,
+ "non-None value .* async generator"):
+ gen().__anext__().send(100)
+
+ def test_async_gen_api_01(self):
+ async def gen():
+ yield 123
+
+ g = gen()
+
+ self.assertEqual(g.__name__, 'gen')
+ g.__name__ = '123'
+ self.assertEqual(g.__name__, '123')
+
+ self.assertIn('.gen', g.__qualname__)
+ g.__qualname__ = '123'
+ self.assertEqual(g.__qualname__, '123')
+
+ self.assertIsNone(g.ag_await)
+ self.assertIsInstance(g.ag_frame, types.FrameType)
+ self.assertFalse(g.ag_running)
+ self.assertIsInstance(g.ag_code, types.CodeType)
+
+ self.assertTrue(inspect.isawaitable(g.aclose()))
+
+
+class AsyncGenAsyncioTest(unittest.TestCase):
+
+ def setUp(self):
+ self.loop = asyncio.new_event_loop()
+ asyncio.set_event_loop(None)
+
+ def tearDown(self):
+ self.loop.close()
+ self.loop = None
+
+ async def to_list(self, gen):
+ res = []
+ async for i in gen:
+ res.append(i)
+ return res
+
+ def test_async_gen_asyncio_01(self):
+ async def gen():
+ yield 1
+ await asyncio.sleep(0.01, loop=self.loop)
+ yield 2
+ await asyncio.sleep(0.01, loop=self.loop)
+ return
+ yield 3
+
+ res = self.loop.run_until_complete(self.to_list(gen()))
+ self.assertEqual(res, [1, 2])
+
+ def test_async_gen_asyncio_02(self):
+ async def gen():
+ yield 1
+ await asyncio.sleep(0.01, loop=self.loop)
+ yield 2
+ 1 / 0
+ yield 3
+
+ with self.assertRaises(ZeroDivisionError):
+ self.loop.run_until_complete(self.to_list(gen()))
+
+ def test_async_gen_asyncio_03(self):
+ loop = self.loop
+
+ class Gen:
+ async def __aiter__(self):
+ yield 1
+ await asyncio.sleep(0.01, loop=loop)
+ yield 2
+
+ res = loop.run_until_complete(self.to_list(Gen()))
+ self.assertEqual(res, [1, 2])
+
+ def test_async_gen_asyncio_anext_04(self):
+ async def foo():
+ yield 1
+ await asyncio.sleep(0.01, loop=self.loop)
+ try:
+ yield 2
+ yield 3
+ except ZeroDivisionError:
+ yield 1000
+ await asyncio.sleep(0.01, loop=self.loop)
+ yield 4
+
+ async def run1():
+ it = foo().__aiter__()
+
+ self.assertEqual(await it.__anext__(), 1)
+ self.assertEqual(await it.__anext__(), 2)
+ self.assertEqual(await it.__anext__(), 3)
+ self.assertEqual(await it.__anext__(), 4)
+ with self.assertRaises(StopAsyncIteration):
+ await it.__anext__()
+ with self.assertRaises(StopAsyncIteration):
+ await it.__anext__()
+
+ async def run2():
+ it = foo().__aiter__()
+
+ self.assertEqual(await it.__anext__(), 1)
+ self.assertEqual(await it.__anext__(), 2)
+ try:
+ it.__anext__().throw(ZeroDivisionError)
+ except StopIteration as ex:
+ self.assertEqual(ex.args[0], 1000)
+ else:
+ self.fail('StopIteration was not raised')
+ self.assertEqual(await it.__anext__(), 4)
+ with self.assertRaises(StopAsyncIteration):
+ await it.__anext__()
+
+ self.loop.run_until_complete(run1())
+ self.loop.run_until_complete(run2())
+
+ def test_async_gen_asyncio_anext_05(self):
+ async def foo():
+ v = yield 1
+ v = yield v
+ yield v * 100
+
+ async def run():
+ it = foo().__aiter__()
+
+ try:
+ it.__anext__().send(None)
+ except StopIteration as ex:
+ self.assertEqual(ex.args[0], 1)
+ else:
+ self.fail('StopIteration was not raised')
+
+ try:
+ it.__anext__().send(10)
+ except StopIteration as ex:
+ self.assertEqual(ex.args[0], 10)
+ else:
+ self.fail('StopIteration was not raised')
+
+ try:
+ it.__anext__().send(12)
+ except StopIteration as ex:
+ self.assertEqual(ex.args[0], 1200)
+ else:
+ self.fail('StopIteration was not raised')
+
+ with self.assertRaises(StopAsyncIteration):
+ await it.__anext__()
+
+ self.loop.run_until_complete(run())
+
+ def test_async_gen_asyncio_anext_06(self):
+ DONE = 0
+
+ # test synchronous generators
+ def foo():
+ try:
+ yield
+ except:
+ pass
+ g = foo()
+ g.send(None)
+ with self.assertRaises(StopIteration):
+ g.send(None)
+
+ # now with asynchronous generators
+
+ async def gen():
+ nonlocal DONE
+ try:
+ yield
+ except:
+ pass
+ DONE = 1
+
+ async def run():
+ nonlocal DONE
+ g = gen()
+ await g.asend(None)
+ with self.assertRaises(StopAsyncIteration):
+ await g.asend(None)
+ DONE += 10
+
+ self.loop.run_until_complete(run())
+ self.assertEqual(DONE, 11)
+
+ def test_async_gen_asyncio_anext_tuple(self):
+ async def foo():
+ try:
+ yield (1,)
+ except ZeroDivisionError:
+ yield (2,)
+
+ async def run():
+ it = foo().__aiter__()
+
+ self.assertEqual(await it.__anext__(), (1,))
+ with self.assertRaises(StopIteration) as cm:
+ it.__anext__().throw(ZeroDivisionError)
+ self.assertEqual(cm.exception.args[0], (2,))
+ with self.assertRaises(StopAsyncIteration):
+ await it.__anext__()
+
+ self.loop.run_until_complete(run())
+
+ def test_async_gen_asyncio_anext_stopiteration(self):
+ async def foo():
+ try:
+ yield StopIteration(1)
+ except ZeroDivisionError:
+ yield StopIteration(3)
+
+ async def run():
+ it = foo().__aiter__()
+
+ v = await it.__anext__()
+ self.assertIsInstance(v, StopIteration)
+ self.assertEqual(v.value, 1)
+ with self.assertRaises(StopIteration) as cm:
+ it.__anext__().throw(ZeroDivisionError)
+ v = cm.exception.args[0]
+ self.assertIsInstance(v, StopIteration)
+ self.assertEqual(v.value, 3)
+ with self.assertRaises(StopAsyncIteration):
+ await it.__anext__()
+
+ self.loop.run_until_complete(run())
+
+ def test_async_gen_asyncio_aclose_06(self):
+ async def foo():
+ try:
+ yield 1
+ 1 / 0
+ finally:
+ await asyncio.sleep(0.01, loop=self.loop)
+ yield 12
+
+ async def run():
+ gen = foo()
+ it = gen.__aiter__()
+ await it.__anext__()
+ await gen.aclose()
+
+ with self.assertRaisesRegex(
+ RuntimeError,
+ "async generator ignored GeneratorExit"):
+ self.loop.run_until_complete(run())
+
+ def test_async_gen_asyncio_aclose_07(self):
+ DONE = 0
+
+ async def foo():
+ nonlocal DONE
+ try:
+ yield 1
+ 1 / 0
+ finally:
+ await asyncio.sleep(0.01, loop=self.loop)
+ await asyncio.sleep(0.01, loop=self.loop)
+ DONE += 1
+ DONE += 1000
+
+ async def run():
+ gen = foo()
+ it = gen.__aiter__()
+ await it.__anext__()
+ await gen.aclose()
+
+ self.loop.run_until_complete(run())
+ self.assertEqual(DONE, 1)
+
+ def test_async_gen_asyncio_aclose_08(self):
+ DONE = 0
+
+ fut = asyncio.Future(loop=self.loop)
+
+ async def foo():
+ nonlocal DONE
+ try:
+ yield 1
+ await fut
+ DONE += 1000
+ yield 2
+ finally:
+ await asyncio.sleep(0.01, loop=self.loop)
+ await asyncio.sleep(0.01, loop=self.loop)
+ DONE += 1
+ DONE += 1000
+
+ async def run():
+ gen = foo()
+ it = gen.__aiter__()
+ self.assertEqual(await it.__anext__(), 1)
+ t = self.loop.create_task(it.__anext__())
+ await asyncio.sleep(0.01, loop=self.loop)
+ await gen.aclose()
+ return t
+
+ t = self.loop.run_until_complete(run())
+ self.assertEqual(DONE, 1)
+
+ # Silence ResourceWarnings
+ fut.cancel()
+ t.cancel()
+ self.loop.run_until_complete(asyncio.sleep(0.01, loop=self.loop))
+
+ def test_async_gen_asyncio_gc_aclose_09(self):
+ DONE = 0
+
+ async def gen():
+ nonlocal DONE
+ try:
+ while True:
+ yield 1
+ finally:
+ await asyncio.sleep(0.01, loop=self.loop)
+ await asyncio.sleep(0.01, loop=self.loop)
+ DONE = 1
+
+ async def run():
+ g = gen()
+ await g.__anext__()
+ await g.__anext__()
+ del g
+
+ await asyncio.sleep(0.1, loop=self.loop)
+
+ self.loop.run_until_complete(run())
+ self.assertEqual(DONE, 1)
+
+ def test_async_gen_asyncio_aclose_10(self):
+ DONE = 0
+
+ # test synchronous generators
+ def foo():
+ try:
+ yield
+ except:
+ pass
+ g = foo()
+ g.send(None)
+ g.close()
+
+ # now with asynchronous generators
+
+ async def gen():
+ nonlocal DONE
+ try:
+ yield
+ except:
+ pass
+ DONE = 1
+
+ async def run():
+ nonlocal DONE
+ g = gen()
+ await g.asend(None)
+ await g.aclose()
+ DONE += 10
+
+ self.loop.run_until_complete(run())
+ self.assertEqual(DONE, 11)
+
+ def test_async_gen_asyncio_aclose_11(self):
+ DONE = 0
+
+ # test synchronous generators
+ def foo():
+ try:
+ yield
+ except:
+ pass
+ yield
+ g = foo()
+ g.send(None)
+ with self.assertRaisesRegex(RuntimeError, 'ignored GeneratorExit'):
+ g.close()
+
+ # now with asynchronous generators
+
+ async def gen():
+ nonlocal DONE
+ try:
+ yield
+ except:
+ pass
+ yield
+ DONE += 1
+
+ async def run():
+ nonlocal DONE
+ g = gen()
+ await g.asend(None)
+ with self.assertRaisesRegex(RuntimeError, 'ignored GeneratorExit'):
+ await g.aclose()
+ DONE += 10
+
+ self.loop.run_until_complete(run())
+ self.assertEqual(DONE, 10)
+
+ def test_async_gen_asyncio_asend_01(self):
+ DONE = 0
+
+ # Sanity check:
+ def sgen():
+ v = yield 1
+ yield v * 2
+ sg = sgen()
+ v = sg.send(None)
+ self.assertEqual(v, 1)
+ v = sg.send(100)
+ self.assertEqual(v, 200)
+
+ async def gen():
+ nonlocal DONE
+ try:
+ await asyncio.sleep(0.01, loop=self.loop)
+ v = yield 1
+ await asyncio.sleep(0.01, loop=self.loop)
+ yield v * 2
+ await asyncio.sleep(0.01, loop=self.loop)
+ return
+ finally:
+ await asyncio.sleep(0.01, loop=self.loop)
+ await asyncio.sleep(0.01, loop=self.loop)
+ DONE = 1
+
+ async def run():
+ g = gen()
+
+ v = await g.asend(None)
+ self.assertEqual(v, 1)
+
+ v = await g.asend(100)
+ self.assertEqual(v, 200)
+
+ with self.assertRaises(StopAsyncIteration):
+ await g.asend(None)
+
+ self.loop.run_until_complete(run())
+ self.assertEqual(DONE, 1)
+
+ def test_async_gen_asyncio_asend_02(self):
+ DONE = 0
+
+ async def sleep_n_crash(delay):
+ await asyncio.sleep(delay, loop=self.loop)
+ 1 / 0
+
+ async def gen():
+ nonlocal DONE
+ try:
+ await asyncio.sleep(0.01, loop=self.loop)
+ v = yield 1
+ await sleep_n_crash(0.01)
+ DONE += 1000
+ yield v * 2
+ finally:
+ await asyncio.sleep(0.01, loop=self.loop)
+ await asyncio.sleep(0.01, loop=self.loop)
+ DONE = 1
+
+ async def run():
+ g = gen()
+
+ v = await g.asend(None)
+ self.assertEqual(v, 1)
+
+ await g.asend(100)
+
+ with self.assertRaises(ZeroDivisionError):
+ self.loop.run_until_complete(run())
+ self.assertEqual(DONE, 1)
+
+ def test_async_gen_asyncio_asend_03(self):
+ DONE = 0
+
+ async def sleep_n_crash(delay):
+ fut = asyncio.ensure_future(asyncio.sleep(delay, loop=self.loop),
+ loop=self.loop)
+ self.loop.call_later(delay / 2, lambda: fut.cancel())
+ return await fut
+
+ async def gen():
+ nonlocal DONE
+ try:
+ await asyncio.sleep(0.01, loop=self.loop)
+ v = yield 1
+ await sleep_n_crash(0.01)
+ DONE += 1000
+ yield v * 2
+ finally:
+ await asyncio.sleep(0.01, loop=self.loop)
+ await asyncio.sleep(0.01, loop=self.loop)
+ DONE = 1
+
+ async def run():
+ g = gen()
+
+ v = await g.asend(None)
+ self.assertEqual(v, 1)
+
+ await g.asend(100)
+
+ with self.assertRaises(asyncio.CancelledError):
+ self.loop.run_until_complete(run())
+ self.assertEqual(DONE, 1)
+
+ def test_async_gen_asyncio_athrow_01(self):
+ DONE = 0
+
+ class FooEr(Exception):
+ pass
+
+ # Sanity check:
+ def sgen():
+ try:
+ v = yield 1
+ except FooEr:
+ v = 1000
+ yield v * 2
+ sg = sgen()
+ v = sg.send(None)
+ self.assertEqual(v, 1)
+ v = sg.throw(FooEr)
+ self.assertEqual(v, 2000)
+ with self.assertRaises(StopIteration):
+ sg.send(None)
+
+ async def gen():
+ nonlocal DONE
+ try:
+ await asyncio.sleep(0.01, loop=self.loop)
+ try:
+ v = yield 1
+ except FooEr:
+ v = 1000
+ await asyncio.sleep(0.01, loop=self.loop)
+ yield v * 2
+ await asyncio.sleep(0.01, loop=self.loop)
+ # return
+ finally:
+ await asyncio.sleep(0.01, loop=self.loop)
+ await asyncio.sleep(0.01, loop=self.loop)
+ DONE = 1
+
+ async def run():
+ g = gen()
+
+ v = await g.asend(None)
+ self.assertEqual(v, 1)
+
+ v = await g.athrow(FooEr)
+ self.assertEqual(v, 2000)
+
+ with self.assertRaises(StopAsyncIteration):
+ await g.asend(None)
+
+ self.loop.run_until_complete(run())
+ self.assertEqual(DONE, 1)
+
+ def test_async_gen_asyncio_athrow_02(self):
+ DONE = 0
+
+ class FooEr(Exception):
+ pass
+
+ async def sleep_n_crash(delay):
+ fut = asyncio.ensure_future(asyncio.sleep(delay, loop=self.loop),
+ loop=self.loop)
+ self.loop.call_later(delay / 2, lambda: fut.cancel())
+ return await fut
+
+ async def gen():
+ nonlocal DONE
+ try:
+ await asyncio.sleep(0.01, loop=self.loop)
+ try:
+ v = yield 1
+ except FooEr:
+ await sleep_n_crash(0.01)
+ yield v * 2
+ await asyncio.sleep(0.01, loop=self.loop)
+ # return
+ finally:
+ await asyncio.sleep(0.01, loop=self.loop)
+ await asyncio.sleep(0.01, loop=self.loop)
+ DONE = 1
+
+ async def run():
+ g = gen()
+
+ v = await g.asend(None)
+ self.assertEqual(v, 1)
+
+ try:
+ await g.athrow(FooEr)
+ except asyncio.CancelledError:
+ self.assertEqual(DONE, 1)
+ raise
+ else:
+ self.fail('CancelledError was not raised')
+
+ with self.assertRaises(asyncio.CancelledError):
+ self.loop.run_until_complete(run())
+ self.assertEqual(DONE, 1)
+
+ def test_async_gen_asyncio_athrow_03(self):
+ DONE = 0
+
+ # test synchronous generators
+ def foo():
+ try:
+ yield
+ except:
+ pass
+ g = foo()
+ g.send(None)
+ with self.assertRaises(StopIteration):
+ g.throw(ValueError)
+
+ # now with asynchronous generators
+
+ async def gen():
+ nonlocal DONE
+ try:
+ yield
+ except:
+ pass
+ DONE = 1
+
+ async def run():
+ nonlocal DONE
+ g = gen()
+ await g.asend(None)
+ with self.assertRaises(StopAsyncIteration):
+ await g.athrow(ValueError)
+ DONE += 10
+
+ self.loop.run_until_complete(run())
+ self.assertEqual(DONE, 11)
+
+ def test_async_gen_asyncio_athrow_tuple(self):
+ async def gen():
+ try:
+ yield 1
+ except ZeroDivisionError:
+ yield (2,)
+
+ async def run():
+ g = gen()
+ v = await g.asend(None)
+ self.assertEqual(v, 1)
+ v = await g.athrow(ZeroDivisionError)
+ self.assertEqual(v, (2,))
+ with self.assertRaises(StopAsyncIteration):
+ await g.asend(None)
+
+ self.loop.run_until_complete(run())
+
+ def test_async_gen_asyncio_athrow_stopiteration(self):
+ async def gen():
+ try:
+ yield 1
+ except ZeroDivisionError:
+ yield StopIteration(2)
+
+ async def run():
+ g = gen()
+ v = await g.asend(None)
+ self.assertEqual(v, 1)
+ v = await g.athrow(ZeroDivisionError)
+ self.assertIsInstance(v, StopIteration)
+ self.assertEqual(v.value, 2)
+ with self.assertRaises(StopAsyncIteration):
+ await g.asend(None)
+
+ self.loop.run_until_complete(run())
+
+ def test_async_gen_asyncio_shutdown_01(self):
+ finalized = 0
+
+ async def waiter(timeout):
+ nonlocal finalized
+ try:
+ await asyncio.sleep(timeout, loop=self.loop)
+ yield 1
+ finally:
+ await asyncio.sleep(0, loop=self.loop)
+ finalized += 1
+
+ async def wait():
+ async for _ in waiter(1):
+ pass
+
+ t1 = self.loop.create_task(wait())
+ t2 = self.loop.create_task(wait())
+
+ self.loop.run_until_complete(asyncio.sleep(0.1, loop=self.loop))
+
+ self.loop.run_until_complete(self.loop.shutdown_asyncgens())
+ self.assertEqual(finalized, 2)
+
+ # Silence warnings
+ t1.cancel()
+ t2.cancel()
+ self.loop.run_until_complete(asyncio.sleep(0.1, loop=self.loop))
+
+ def test_async_gen_asyncio_shutdown_02(self):
+ logged = 0
+
+ def logger(loop, context):
+ nonlocal logged
+ self.assertIn('asyncgen', context)
+ expected = 'an error occurred during closing of asynchronous'
+ if expected in context['message']:
+ logged += 1
+
+ async def waiter(timeout):
+ try:
+ await asyncio.sleep(timeout, loop=self.loop)
+ yield 1
+ finally:
+ 1 / 0
+
+ async def wait():
+ async for _ in waiter(1):
+ pass
+
+ t = self.loop.create_task(wait())
+ self.loop.run_until_complete(asyncio.sleep(0.1, loop=self.loop))
+
+ self.loop.set_exception_handler(logger)
+ self.loop.run_until_complete(self.loop.shutdown_asyncgens())
+
+ self.assertEqual(logged, 1)
+
+ # Silence warnings
+ t.cancel()
+ self.loop.run_until_complete(asyncio.sleep(0.1, loop=self.loop))
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/Lib/test/test_asynchat.py b/Lib/test/test_asynchat.py
index 3a33fc8..0eba76d 100644
--- a/Lib/test/test_asynchat.py
+++ b/Lib/test/test_asynchat.py
@@ -12,7 +12,6 @@ import socket
import sys
import time
import unittest
-import warnings
import unittest.mock
try:
import threading
@@ -297,37 +296,6 @@ class TestHelperFunctions(unittest.TestCase):
self.assertEqual(asynchat.find_prefix_at_end("qwertydkjf", "\r\n"), 0)
-class TestFifo(unittest.TestCase):
- def test_basic(self):
- with self.assertWarns(DeprecationWarning) as cm:
- f = asynchat.fifo()
- self.assertEqual(str(cm.warning),
- "fifo class will be removed in Python 3.6")
- f.push(7)
- f.push(b'a')
- self.assertEqual(len(f), 2)
- self.assertEqual(f.first(), 7)
- self.assertEqual(f.pop(), (1, 7))
- self.assertEqual(len(f), 1)
- self.assertEqual(f.first(), b'a')
- self.assertEqual(f.is_empty(), False)
- self.assertEqual(f.pop(), (1, b'a'))
- self.assertEqual(len(f), 0)
- self.assertEqual(f.is_empty(), True)
- self.assertEqual(f.pop(), (0, None))
-
- def test_given_list(self):
- with self.assertWarns(DeprecationWarning) as cm:
- f = asynchat.fifo([b'x', 17, 3])
- self.assertEqual(str(cm.warning),
- "fifo class will be removed in Python 3.6")
- self.assertEqual(len(f), 3)
- self.assertEqual(f.pop(), (1, b'x'))
- self.assertEqual(f.pop(), (1, 17))
- self.assertEqual(f.pop(), (1, 3))
- self.assertEqual(f.pop(), (0, None))
-
-
class TestNotConnected(unittest.TestCase):
def test_disallow_negative_terminator(self):
# Issue #11259
diff --git a/Lib/test/test_asyncio/test_futures.py b/Lib/test/test_asyncio/test_futures.py
index c306b77..89afdca 100644
--- a/Lib/test/test_asyncio/test_futures.py
+++ b/Lib/test/test_asyncio/test_futures.py
@@ -9,6 +9,7 @@ from unittest import mock
import asyncio
from asyncio import test_utils
+from asyncio import futures
try:
from test import support
except ImportError:
@@ -94,7 +95,10 @@ class DuckTests(test_utils.TestCase):
assert g is f
-class FutureTests(test_utils.TestCase):
+class BaseFutureTests:
+
+ def _new_future(self, loop=None):
+ raise NotImplementedError
def setUp(self):
super().setUp()
@@ -110,22 +114,22 @@ class FutureTests(test_utils.TestCase):
self.assertFalse(asyncio.isfuture(MyFuture))
self.assertTrue(asyncio.isfuture(MyFuture()))
-
self.assertFalse(asyncio.isfuture(1))
- self.assertFalse(asyncio.isfuture(asyncio.Future))
# As `isinstance(Mock(), Future)` returns `False`
self.assertFalse(asyncio.isfuture(mock.Mock()))
+ f = self._new_future(loop=self.loop)
+ self.assertTrue(asyncio.isfuture(f))
+ self.assertFalse(asyncio.isfuture(type(f)))
+
# As `isinstance(Mock(Future), Future)` returns `True`
- self.assertTrue(asyncio.isfuture(mock.Mock(asyncio.Future)))
+ self.assertTrue(asyncio.isfuture(mock.Mock(type(f))))
- f = asyncio.Future(loop=self.loop)
- self.assertTrue(asyncio.isfuture(f))
f.cancel()
def test_initial_state(self):
- f = asyncio.Future(loop=self.loop)
+ f = self._new_future(loop=self.loop)
self.assertFalse(f.cancelled())
self.assertFalse(f.done())
f.cancel()
@@ -133,15 +137,15 @@ class FutureTests(test_utils.TestCase):
def test_init_constructor_default_loop(self):
asyncio.set_event_loop(self.loop)
- f = asyncio.Future()
+ f = self._new_future()
self.assertIs(f._loop, self.loop)
def test_constructor_positional(self):
# Make sure Future doesn't accept a positional argument
- self.assertRaises(TypeError, asyncio.Future, 42)
+ self.assertRaises(TypeError, self._new_future, 42)
def test_cancel(self):
- f = asyncio.Future(loop=self.loop)
+ f = self._new_future(loop=self.loop)
self.assertTrue(f.cancel())
self.assertTrue(f.cancelled())
self.assertTrue(f.done())
@@ -152,7 +156,7 @@ class FutureTests(test_utils.TestCase):
self.assertFalse(f.cancel())
def test_result(self):
- f = asyncio.Future(loop=self.loop)
+ f = self._new_future(loop=self.loop)
self.assertRaises(asyncio.InvalidStateError, f.result)
f.set_result(42)
@@ -166,7 +170,7 @@ class FutureTests(test_utils.TestCase):
def test_exception(self):
exc = RuntimeError()
- f = asyncio.Future(loop=self.loop)
+ f = self._new_future(loop=self.loop)
self.assertRaises(asyncio.InvalidStateError, f.exception)
# StopIteration cannot be raised into a Future - CPython issue26221
@@ -183,12 +187,12 @@ class FutureTests(test_utils.TestCase):
self.assertFalse(f.cancel())
def test_exception_class(self):
- f = asyncio.Future(loop=self.loop)
+ f = self._new_future(loop=self.loop)
f.set_exception(RuntimeError)
self.assertIsInstance(f.exception(), RuntimeError)
def test_yield_from_twice(self):
- f = asyncio.Future(loop=self.loop)
+ f = self._new_future(loop=self.loop)
def fixture():
yield 'A'
@@ -207,7 +211,7 @@ class FutureTests(test_utils.TestCase):
def test_future_repr(self):
self.loop.set_debug(True)
- f_pending_debug = asyncio.Future(loop=self.loop)
+ f_pending_debug = self._new_future(loop=self.loop)
frame = f_pending_debug._source_traceback[-1]
self.assertEqual(repr(f_pending_debug),
'<Future pending created at %s:%s>'
@@ -215,21 +219,21 @@ class FutureTests(test_utils.TestCase):
f_pending_debug.cancel()
self.loop.set_debug(False)
- f_pending = asyncio.Future(loop=self.loop)
+ f_pending = self._new_future(loop=self.loop)
self.assertEqual(repr(f_pending), '<Future pending>')
f_pending.cancel()
- f_cancelled = asyncio.Future(loop=self.loop)
+ f_cancelled = self._new_future(loop=self.loop)
f_cancelled.cancel()
self.assertEqual(repr(f_cancelled), '<Future cancelled>')
- f_result = asyncio.Future(loop=self.loop)
+ f_result = self._new_future(loop=self.loop)
f_result.set_result(4)
self.assertEqual(repr(f_result), '<Future finished result=4>')
self.assertEqual(f_result.result(), 4)
exc = RuntimeError()
- f_exception = asyncio.Future(loop=self.loop)
+ f_exception = self._new_future(loop=self.loop)
f_exception.set_exception(exc)
self.assertEqual(repr(f_exception),
'<Future finished exception=RuntimeError()>')
@@ -240,7 +244,7 @@ class FutureTests(test_utils.TestCase):
text = '%s() at %s:%s' % (func.__qualname__, filename, lineno)
return re.escape(text)
- f_one_callbacks = asyncio.Future(loop=self.loop)
+ f_one_callbacks = self._new_future(loop=self.loop)
f_one_callbacks.add_done_callback(_fakefunc)
fake_repr = func_repr(_fakefunc)
self.assertRegex(repr(f_one_callbacks),
@@ -249,7 +253,7 @@ class FutureTests(test_utils.TestCase):
self.assertEqual(repr(f_one_callbacks),
'<Future cancelled>')
- f_two_callbacks = asyncio.Future(loop=self.loop)
+ f_two_callbacks = self._new_future(loop=self.loop)
f_two_callbacks.add_done_callback(first_cb)
f_two_callbacks.add_done_callback(last_cb)
first_repr = func_repr(first_cb)
@@ -258,7 +262,7 @@ class FutureTests(test_utils.TestCase):
r'<Future pending cb=\[%s, %s\]>'
% (first_repr, last_repr))
- f_many_callbacks = asyncio.Future(loop=self.loop)
+ f_many_callbacks = self._new_future(loop=self.loop)
f_many_callbacks.add_done_callback(first_cb)
for i in range(8):
f_many_callbacks.add_done_callback(_fakefunc)
@@ -273,31 +277,31 @@ class FutureTests(test_utils.TestCase):
def test_copy_state(self):
from asyncio.futures import _copy_future_state
- f = asyncio.Future(loop=self.loop)
+ f = self._new_future(loop=self.loop)
f.set_result(10)
- newf = asyncio.Future(loop=self.loop)
+ newf = self._new_future(loop=self.loop)
_copy_future_state(f, newf)
self.assertTrue(newf.done())
self.assertEqual(newf.result(), 10)
- f_exception = asyncio.Future(loop=self.loop)
+ f_exception = self._new_future(loop=self.loop)
f_exception.set_exception(RuntimeError())
- newf_exception = asyncio.Future(loop=self.loop)
+ newf_exception = self._new_future(loop=self.loop)
_copy_future_state(f_exception, newf_exception)
self.assertTrue(newf_exception.done())
self.assertRaises(RuntimeError, newf_exception.result)
- f_cancelled = asyncio.Future(loop=self.loop)
+ f_cancelled = self._new_future(loop=self.loop)
f_cancelled.cancel()
- newf_cancelled = asyncio.Future(loop=self.loop)
+ newf_cancelled = self._new_future(loop=self.loop)
_copy_future_state(f_cancelled, newf_cancelled)
self.assertTrue(newf_cancelled.cancelled())
def test_iter(self):
- fut = asyncio.Future(loop=self.loop)
+ fut = self._new_future(loop=self.loop)
def coro():
yield from fut
@@ -310,20 +314,20 @@ class FutureTests(test_utils.TestCase):
@mock.patch('asyncio.base_events.logger')
def test_tb_logger_abandoned(self, m_log):
- fut = asyncio.Future(loop=self.loop)
+ fut = self._new_future(loop=self.loop)
del fut
self.assertFalse(m_log.error.called)
@mock.patch('asyncio.base_events.logger')
def test_tb_logger_result_unretrieved(self, m_log):
- fut = asyncio.Future(loop=self.loop)
+ fut = self._new_future(loop=self.loop)
fut.set_result(42)
del fut
self.assertFalse(m_log.error.called)
@mock.patch('asyncio.base_events.logger')
def test_tb_logger_result_retrieved(self, m_log):
- fut = asyncio.Future(loop=self.loop)
+ fut = self._new_future(loop=self.loop)
fut.set_result(42)
fut.result()
del fut
@@ -331,7 +335,7 @@ class FutureTests(test_utils.TestCase):
@mock.patch('asyncio.base_events.logger')
def test_tb_logger_exception_unretrieved(self, m_log):
- fut = asyncio.Future(loop=self.loop)
+ fut = self._new_future(loop=self.loop)
fut.set_exception(RuntimeError('boom'))
del fut
test_utils.run_briefly(self.loop)
@@ -340,7 +344,7 @@ class FutureTests(test_utils.TestCase):
@mock.patch('asyncio.base_events.logger')
def test_tb_logger_exception_retrieved(self, m_log):
- fut = asyncio.Future(loop=self.loop)
+ fut = self._new_future(loop=self.loop)
fut.set_exception(RuntimeError('boom'))
fut.exception()
del fut
@@ -348,7 +352,7 @@ class FutureTests(test_utils.TestCase):
@mock.patch('asyncio.base_events.logger')
def test_tb_logger_exception_result_retrieved(self, m_log):
- fut = asyncio.Future(loop=self.loop)
+ fut = self._new_future(loop=self.loop)
fut.set_exception(RuntimeError('boom'))
self.assertRaises(RuntimeError, fut.result)
del fut
@@ -362,12 +366,12 @@ class FutureTests(test_utils.TestCase):
f1 = ex.submit(run, 'oi')
f2 = asyncio.wrap_future(f1, loop=self.loop)
res, ident = self.loop.run_until_complete(f2)
- self.assertIsInstance(f2, asyncio.Future)
+ self.assertTrue(asyncio.isfuture(f2))
self.assertEqual(res, 'oi')
self.assertNotEqual(ident, threading.get_ident())
def test_wrap_future_future(self):
- f1 = asyncio.Future(loop=self.loop)
+ f1 = self._new_future(loop=self.loop)
f2 = asyncio.wrap_future(f1)
self.assertIs(f1, f2)
@@ -402,10 +406,10 @@ class FutureTests(test_utils.TestCase):
def test_future_source_traceback(self):
self.loop.set_debug(True)
- future = asyncio.Future(loop=self.loop)
+ future = self._new_future(loop=self.loop)
lineno = sys._getframe().f_lineno - 1
self.assertIsInstance(future._source_traceback, list)
- self.assertEqual(future._source_traceback[-1][:3],
+ self.assertEqual(future._source_traceback[-2][:3],
(__file__,
lineno,
'test_future_source_traceback'))
@@ -421,57 +425,18 @@ class FutureTests(test_utils.TestCase):
return exc
exc = memory_error()
- future = asyncio.Future(loop=self.loop)
- if debug:
- source_traceback = future._source_traceback
+ future = self._new_future(loop=self.loop)
future.set_exception(exc)
future = None
test_utils.run_briefly(self.loop)
support.gc_collect()
if sys.version_info >= (3, 4):
- if debug:
- frame = source_traceback[-1]
- regex = (r'^Future exception was never retrieved\n'
- r'future: <Future finished exception=MemoryError\(\) '
- r'created at {filename}:{lineno}>\n'
- r'source_traceback: Object '
- r'created at \(most recent call last\):\n'
- r' File'
- r'.*\n'
- r' File "{filename}", line {lineno}, '
- r'in check_future_exception_never_retrieved\n'
- r' future = asyncio\.Future\(loop=self\.loop\)$'
- ).format(filename=re.escape(frame[0]),
- lineno=frame[1])
- else:
- regex = (r'^Future exception was never retrieved\n'
- r'future: '
- r'<Future finished exception=MemoryError\(\)>$'
- )
+ regex = r'^Future exception was never retrieved\n'
exc_info = (type(exc), exc, exc.__traceback__)
m_log.error.assert_called_once_with(mock.ANY, exc_info=exc_info)
else:
- if debug:
- frame = source_traceback[-1]
- regex = (r'^Future/Task exception was never retrieved\n'
- r'Future/Task created at \(most recent call last\):\n'
- r' File'
- r'.*\n'
- r' File "{filename}", line {lineno}, '
- r'in check_future_exception_never_retrieved\n'
- r' future = asyncio\.Future\(loop=self\.loop\)\n'
- r'Traceback \(most recent call last\):\n'
- r'.*\n'
- r'MemoryError$'
- ).format(filename=re.escape(frame[0]),
- lineno=frame[1])
- else:
- regex = (r'^Future/Task exception was never retrieved\n'
- r'Traceback \(most recent call last\):\n'
- r'.*\n'
- r'MemoryError$'
- )
+ regex = r'^Future/Task exception was never retrieved\n'
m_log.error.assert_called_once_with(mock.ANY, exc_info=False)
message = m_log.error.call_args[0][0]
self.assertRegex(message, re.compile(regex, re.DOTALL))
@@ -483,14 +448,49 @@ class FutureTests(test_utils.TestCase):
self.check_future_exception_never_retrieved(True)
def test_set_result_unless_cancelled(self):
- from asyncio import futures
- fut = asyncio.Future(loop=self.loop)
+ fut = self._new_future(loop=self.loop)
fut.cancel()
futures._set_result_unless_cancelled(fut, 2)
self.assertTrue(fut.cancelled())
+ def test_future_stop_iteration_args(self):
+ fut = self._new_future(loop=self.loop)
+ fut.set_result((1, 2))
+ fi = fut.__iter__()
+ result = None
+ try:
+ fi.send(None)
+ except StopIteration as ex:
+ result = ex.args[0]
+ else:
+ self.fail('StopIteration was expected')
+ self.assertEqual(result, (1, 2))
+
+ def test_future_iter_throw(self):
+ fut = self._new_future(loop=self.loop)
+ fi = iter(fut)
+ self.assertRaises(TypeError, fi.throw,
+ Exception, Exception("elephant"), 32)
+ self.assertRaises(TypeError, fi.throw,
+ Exception("elephant"), Exception("elephant"))
+ self.assertRaises(TypeError, fi.throw, list)
+
+
+@unittest.skipUnless(hasattr(futures, '_CFuture'),
+ 'requires the C _asyncio module')
+class CFutureTests(BaseFutureTests, test_utils.TestCase):
+
+ def _new_future(self, *args, **kwargs):
+ return futures._CFuture(*args, **kwargs)
+
-class FutureDoneCallbackTests(test_utils.TestCase):
+class PyFutureTests(BaseFutureTests, test_utils.TestCase):
+
+ def _new_future(self, *args, **kwargs):
+ return futures._PyFuture(*args, **kwargs)
+
+
+class BaseFutureDoneCallbackTests():
def setUp(self):
super().setUp()
@@ -506,7 +506,7 @@ class FutureDoneCallbackTests(test_utils.TestCase):
return bag_appender
def _new_future(self):
- return asyncio.Future(loop=self.loop)
+ raise NotImplementedError
def test_callbacks_invoked_on_set_result(self):
bag = []
@@ -570,5 +570,21 @@ class FutureDoneCallbackTests(test_utils.TestCase):
self.assertEqual(f.result(), 'foo')
+@unittest.skipUnless(hasattr(futures, '_CFuture'),
+ 'requires the C _asyncio module')
+class CFutureDoneCallbackTests(BaseFutureDoneCallbackTests,
+ test_utils.TestCase):
+
+ def _new_future(self):
+ return futures._CFuture(loop=self.loop)
+
+
+class PyFutureDoneCallbackTests(BaseFutureDoneCallbackTests,
+ test_utils.TestCase):
+
+ def _new_future(self):
+ return futures._PyFuture(loop=self.loop)
+
+
if __name__ == '__main__':
unittest.main()
diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py
index 9872926..a18d49a 100644
--- a/Lib/test/test_asyncio/test_tasks.py
+++ b/Lib/test/test_asyncio/test_tasks.py
@@ -1,5 +1,6 @@
"""Tests for tasks.py."""
+import collections
import contextlib
import functools
import io
@@ -14,6 +15,8 @@ from unittest import mock
import asyncio
from asyncio import coroutines
+from asyncio import futures
+from asyncio import tasks
from asyncio import test_utils
try:
from test import support
@@ -72,15 +75,26 @@ class Dummy:
pass
-class TaskTests(test_utils.TestCase):
+class BaseTaskTests:
+
+ Task = None
+ Future = None
+
+ def new_task(self, loop, coro):
+ return self.__class__.Task(coro, loop=loop)
+
+ def new_future(self, loop):
+ return self.__class__.Future(loop=loop)
def setUp(self):
super().setUp()
self.loop = self.new_test_loop()
+ self.loop.set_task_factory(self.new_task)
+ self.loop.create_future = lambda: self.new_future(self.loop)
def test_other_loop_future(self):
other_loop = asyncio.new_event_loop()
- fut = asyncio.Future(loop=other_loop)
+ fut = self.new_future(other_loop)
@asyncio.coroutine
def run(fut):
@@ -108,7 +122,7 @@ class TaskTests(test_utils.TestCase):
@asyncio.coroutine
def notmuch():
return 'ok'
- t = asyncio.Task(notmuch(), loop=self.loop)
+ t = self.new_task(self.loop, notmuch())
self.loop.run_until_complete(t)
self.assertTrue(t.done())
self.assertEqual(t.result(), 'ok')
@@ -116,7 +130,7 @@ class TaskTests(test_utils.TestCase):
loop = asyncio.new_event_loop()
self.set_event_loop(loop)
- t = asyncio.Task(notmuch(), loop=loop)
+ t = self.new_task(loop, notmuch())
self.assertIs(t._loop, loop)
loop.run_until_complete(t)
loop.close()
@@ -139,7 +153,7 @@ class TaskTests(test_utils.TestCase):
loop.close()
def test_ensure_future_future(self):
- f_orig = asyncio.Future(loop=self.loop)
+ f_orig = self.new_future(self.loop)
f_orig.set_result('ko')
f = asyncio.ensure_future(f_orig)
@@ -163,7 +177,7 @@ class TaskTests(test_utils.TestCase):
@asyncio.coroutine
def notmuch():
return 'ok'
- t_orig = asyncio.Task(notmuch(), loop=self.loop)
+ t_orig = self.new_task(self.loop, notmuch())
t = asyncio.ensure_future(t_orig)
self.loop.run_until_complete(t)
self.assertTrue(t.done())
@@ -204,7 +218,7 @@ class TaskTests(test_utils.TestCase):
asyncio.ensure_future('ok')
def test_async_warning(self):
- f = asyncio.Future(loop=self.loop)
+ f = self.new_future(self.loop)
with self.assertWarnsRegex(DeprecationWarning,
'function is deprecated, use ensure_'):
self.assertIs(f, asyncio.async(f))
@@ -251,8 +265,8 @@ class TaskTests(test_utils.TestCase):
# test coroutine function
self.assertEqual(notmuch.__name__, 'notmuch')
if PY35:
- self.assertEqual(notmuch.__qualname__,
- 'TaskTests.test_task_repr.<locals>.notmuch')
+ self.assertRegex(notmuch.__qualname__,
+ r'\w+.test_task_repr.<locals>.notmuch')
self.assertEqual(notmuch.__module__, __name__)
filename, lineno = test_utils.get_function_source(notmuch)
@@ -261,7 +275,7 @@ class TaskTests(test_utils.TestCase):
# test coroutine object
gen = notmuch()
if coroutines._DEBUG or PY35:
- coro_qualname = 'TaskTests.test_task_repr.<locals>.notmuch'
+ coro_qualname = 'BaseTaskTests.test_task_repr.<locals>.notmuch'
else:
coro_qualname = 'notmuch'
self.assertEqual(gen.__name__, 'notmuch')
@@ -270,7 +284,7 @@ class TaskTests(test_utils.TestCase):
coro_qualname)
# test pending Task
- t = asyncio.Task(gen, loop=self.loop)
+ t = self.new_task(self.loop, gen)
t.add_done_callback(Dummy())
coro = format_coroutine(coro_qualname, 'running', src,
@@ -292,7 +306,7 @@ class TaskTests(test_utils.TestCase):
'<Task cancelled %s>' % coro)
# test finished Task
- t = asyncio.Task(notmuch(), loop=self.loop)
+ t = self.new_task(self.loop, notmuch())
self.loop.run_until_complete(t)
coro = format_coroutine(coro_qualname, 'done', src,
t._source_traceback)
@@ -311,9 +325,9 @@ class TaskTests(test_utils.TestCase):
# test coroutine function
self.assertEqual(notmuch.__name__, 'notmuch')
if PY35:
- self.assertEqual(notmuch.__qualname__,
- 'TaskTests.test_task_repr_coro_decorator'
- '.<locals>.notmuch')
+ self.assertRegex(notmuch.__qualname__,
+ r'\w+.test_task_repr_coro_decorator'
+ r'\.<locals>\.notmuch')
self.assertEqual(notmuch.__module__, __name__)
# test coroutine object
@@ -323,7 +337,7 @@ class TaskTests(test_utils.TestCase):
# function, as expected, and have a qualified name (__qualname__
# attribute).
coro_name = 'notmuch'
- coro_qualname = ('TaskTests.test_task_repr_coro_decorator'
+ coro_qualname = ('BaseTaskTests.test_task_repr_coro_decorator'
'.<locals>.notmuch')
else:
# On Python < 3.5, generators inherit the name of the code, not of
@@ -351,7 +365,7 @@ class TaskTests(test_utils.TestCase):
self.assertEqual(repr(gen), '<CoroWrapper %s>' % coro)
# test pending Task
- t = asyncio.Task(gen, loop=self.loop)
+ t = self.new_task(self.loop, gen)
t.add_done_callback(Dummy())
# format the coroutine object
@@ -374,8 +388,8 @@ class TaskTests(test_utils.TestCase):
def wait_for(fut):
return (yield from fut)
- fut = asyncio.Future(loop=self.loop)
- task = asyncio.Task(wait_for(fut), loop=self.loop)
+ fut = self.new_future(self.loop)
+ task = self.new_task(self.loop, wait_for(fut))
test_utils.run_briefly(self.loop)
self.assertRegex(repr(task),
'<Task .* wait_for=%s>' % re.escape(repr(fut)))
@@ -401,10 +415,11 @@ class TaskTests(test_utils.TestCase):
self.addCleanup(task._coro.close)
coro_repr = repr(task._coro)
- expected = ('<CoroWrapper TaskTests.test_task_repr_partial_corowrapper'
- '.<locals>.func(1)() running, ')
- self.assertTrue(coro_repr.startswith(expected),
- coro_repr)
+ expected = (
+ r'<CoroWrapper \w+.test_task_repr_partial_corowrapper'
+ r'\.<locals>\.func\(1\)\(\) running, '
+ )
+ self.assertRegex(coro_repr, expected)
def test_task_basics(self):
@asyncio.coroutine
@@ -438,7 +453,7 @@ class TaskTests(test_utils.TestCase):
yield from asyncio.sleep(10.0, loop=loop)
return 12
- t = asyncio.Task(task(), loop=loop)
+ t = self.new_task(loop, task())
loop.call_soon(t.cancel)
with self.assertRaises(asyncio.CancelledError):
loop.run_until_complete(t)
@@ -453,7 +468,7 @@ class TaskTests(test_utils.TestCase):
yield
return 12
- t = asyncio.Task(task(), loop=self.loop)
+ t = self.new_task(self.loop, task())
test_utils.run_briefly(self.loop) # start coro
t.cancel()
self.assertRaises(
@@ -463,14 +478,14 @@ class TaskTests(test_utils.TestCase):
self.assertFalse(t.cancel())
def test_cancel_inner_future(self):
- f = asyncio.Future(loop=self.loop)
+ f = self.new_future(self.loop)
@asyncio.coroutine
def task():
yield from f
return 12
- t = asyncio.Task(task(), loop=self.loop)
+ t = self.new_task(self.loop, task())
test_utils.run_briefly(self.loop) # start task
f.cancel()
with self.assertRaises(asyncio.CancelledError):
@@ -479,14 +494,14 @@ class TaskTests(test_utils.TestCase):
self.assertTrue(t.cancelled())
def test_cancel_both_task_and_inner_future(self):
- f = asyncio.Future(loop=self.loop)
+ f = self.new_future(self.loop)
@asyncio.coroutine
def task():
yield from f
return 12
- t = asyncio.Task(task(), loop=self.loop)
+ t = self.new_task(self.loop, task())
test_utils.run_briefly(self.loop)
f.cancel()
@@ -500,8 +515,8 @@ class TaskTests(test_utils.TestCase):
self.assertTrue(t.cancelled())
def test_cancel_task_catching(self):
- fut1 = asyncio.Future(loop=self.loop)
- fut2 = asyncio.Future(loop=self.loop)
+ fut1 = self.new_future(self.loop)
+ fut2 = self.new_future(self.loop)
@asyncio.coroutine
def task():
@@ -511,7 +526,7 @@ class TaskTests(test_utils.TestCase):
except asyncio.CancelledError:
return 42
- t = asyncio.Task(task(), loop=self.loop)
+ t = self.new_task(self.loop, task())
test_utils.run_briefly(self.loop)
self.assertIs(t._fut_waiter, fut1) # White-box test.
fut1.set_result(None)
@@ -524,9 +539,9 @@ class TaskTests(test_utils.TestCase):
self.assertFalse(t.cancelled())
def test_cancel_task_ignoring(self):
- fut1 = asyncio.Future(loop=self.loop)
- fut2 = asyncio.Future(loop=self.loop)
- fut3 = asyncio.Future(loop=self.loop)
+ fut1 = self.new_future(self.loop)
+ fut2 = self.new_future(self.loop)
+ fut3 = self.new_future(self.loop)
@asyncio.coroutine
def task():
@@ -538,7 +553,7 @@ class TaskTests(test_utils.TestCase):
res = yield from fut3
return res
- t = asyncio.Task(task(), loop=self.loop)
+ t = self.new_task(self.loop, task())
test_utils.run_briefly(self.loop)
self.assertIs(t._fut_waiter, fut1) # White-box test.
fut1.set_result(None)
@@ -566,7 +581,7 @@ class TaskTests(test_utils.TestCase):
yield from asyncio.sleep(100, loop=loop)
return 12
- t = asyncio.Task(task(), loop=loop)
+ t = self.new_task(loop, task())
self.assertRaises(
asyncio.CancelledError, loop.run_until_complete, t)
self.assertTrue(t.done())
@@ -599,7 +614,7 @@ class TaskTests(test_utils.TestCase):
if x == 2:
loop.stop()
- t = asyncio.Task(task(), loop=loop)
+ t = self.new_task(loop, task())
with self.assertRaises(RuntimeError) as cm:
loop.run_until_complete(t)
self.assertEqual(str(cm.exception),
@@ -637,7 +652,7 @@ class TaskTests(test_utils.TestCase):
foo_running = False
return 'done'
- fut = asyncio.Task(foo(), loop=loop)
+ fut = self.new_task(loop, foo())
with self.assertRaises(asyncio.TimeoutError):
loop.run_until_complete(asyncio.wait_for(fut, 0.1, loop=loop))
@@ -677,7 +692,7 @@ class TaskTests(test_utils.TestCase):
asyncio.set_event_loop(loop)
try:
- fut = asyncio.Task(foo(), loop=loop)
+ fut = self.new_task(loop, foo())
with self.assertRaises(asyncio.TimeoutError):
loop.run_until_complete(asyncio.wait_for(fut, 0.01))
finally:
@@ -696,7 +711,7 @@ class TaskTests(test_utils.TestCase):
loop = self.new_test_loop(gen)
- fut = asyncio.Future(loop=loop)
+ fut = self.new_future(loop)
task = asyncio.wait_for(fut, timeout=0.2, loop=loop)
loop.call_later(0.1, fut.set_result, "ok")
res = loop.run_until_complete(task)
@@ -713,8 +728,8 @@ class TaskTests(test_utils.TestCase):
loop = self.new_test_loop(gen)
- a = asyncio.Task(asyncio.sleep(0.1, loop=loop), loop=loop)
- b = asyncio.Task(asyncio.sleep(0.15, loop=loop), loop=loop)
+ a = self.new_task(loop, asyncio.sleep(0.1, loop=loop))
+ b = self.new_task(loop, asyncio.sleep(0.15, loop=loop))
@asyncio.coroutine
def foo():
@@ -723,12 +738,12 @@ class TaskTests(test_utils.TestCase):
self.assertEqual(pending, set())
return 42
- res = loop.run_until_complete(asyncio.Task(foo(), loop=loop))
+ res = loop.run_until_complete(self.new_task(loop, foo()))
self.assertEqual(res, 42)
self.assertAlmostEqual(0.15, loop.time())
# Doing it again should take no time and exercise a different path.
- res = loop.run_until_complete(asyncio.Task(foo(), loop=loop))
+ res = loop.run_until_complete(self.new_task(loop, foo()))
self.assertAlmostEqual(0.15, loop.time())
self.assertEqual(res, 42)
@@ -743,8 +758,8 @@ class TaskTests(test_utils.TestCase):
loop = self.new_test_loop(gen)
- a = asyncio.Task(asyncio.sleep(0.01, loop=loop), loop=loop)
- b = asyncio.Task(asyncio.sleep(0.015, loop=loop), loop=loop)
+ a = self.new_task(loop, asyncio.sleep(0.01, loop=loop))
+ b = self.new_task(loop, asyncio.sleep(0.015, loop=loop))
@asyncio.coroutine
def foo():
@@ -755,7 +770,7 @@ class TaskTests(test_utils.TestCase):
asyncio.set_event_loop(loop)
res = loop.run_until_complete(
- asyncio.Task(foo(), loop=loop))
+ self.new_task(loop, foo()))
self.assertEqual(res, 42)
@@ -765,9 +780,9 @@ class TaskTests(test_utils.TestCase):
return s
c = coro('test')
- task = asyncio.Task(
- asyncio.wait([c, c, coro('spam')], loop=self.loop),
- loop=self.loop)
+ task =self.new_task(
+ self.loop,
+ asyncio.wait([c, c, coro('spam')], loop=self.loop))
done, pending = self.loop.run_until_complete(task)
@@ -798,12 +813,12 @@ class TaskTests(test_utils.TestCase):
loop = self.new_test_loop(gen)
- a = asyncio.Task(asyncio.sleep(10.0, loop=loop), loop=loop)
- b = asyncio.Task(asyncio.sleep(0.1, loop=loop), loop=loop)
- task = asyncio.Task(
+ a = self.new_task(loop, asyncio.sleep(10.0, loop=loop))
+ b = self.new_task(loop, asyncio.sleep(0.1, loop=loop))
+ task = self.new_task(
+ loop,
asyncio.wait([b, a], return_when=asyncio.FIRST_COMPLETED,
- loop=loop),
- loop=loop)
+ loop=loop))
done, pending = loop.run_until_complete(task)
self.assertEqual({b}, done)
@@ -830,12 +845,12 @@ class TaskTests(test_utils.TestCase):
yield
yield
- a = asyncio.Task(coro1(), loop=self.loop)
- b = asyncio.Task(coro2(), loop=self.loop)
- task = asyncio.Task(
+ a = self.new_task(self.loop, coro1())
+ b = self.new_task(self.loop, coro2())
+ task = self.new_task(
+ self.loop,
asyncio.wait([b, a], return_when=asyncio.FIRST_COMPLETED,
- loop=self.loop),
- loop=self.loop)
+ loop=self.loop))
done, pending = self.loop.run_until_complete(task)
self.assertEqual({a, b}, done)
@@ -854,17 +869,17 @@ class TaskTests(test_utils.TestCase):
loop = self.new_test_loop(gen)
# first_exception, task already has exception
- a = asyncio.Task(asyncio.sleep(10.0, loop=loop), loop=loop)
+ a = self.new_task(loop, asyncio.sleep(10.0, loop=loop))
@asyncio.coroutine
def exc():
raise ZeroDivisionError('err')
- b = asyncio.Task(exc(), loop=loop)
- task = asyncio.Task(
+ b = self.new_task(loop, exc())
+ task = self.new_task(
+ loop,
asyncio.wait([b, a], return_when=asyncio.FIRST_EXCEPTION,
- loop=loop),
- loop=loop)
+ loop=loop))
done, pending = loop.run_until_complete(task)
self.assertEqual({b}, done)
@@ -887,14 +902,14 @@ class TaskTests(test_utils.TestCase):
loop = self.new_test_loop(gen)
# first_exception, exception during waiting
- a = asyncio.Task(asyncio.sleep(10.0, loop=loop), loop=loop)
+ a = self.new_task(loop, asyncio.sleep(10.0, loop=loop))
@asyncio.coroutine
def exc():
yield from asyncio.sleep(0.01, loop=loop)
raise ZeroDivisionError('err')
- b = asyncio.Task(exc(), loop=loop)
+ b = self.new_task(loop, exc())
task = asyncio.wait([b, a], return_when=asyncio.FIRST_EXCEPTION,
loop=loop)
@@ -918,14 +933,14 @@ class TaskTests(test_utils.TestCase):
loop = self.new_test_loop(gen)
- a = asyncio.Task(asyncio.sleep(0.1, loop=loop), loop=loop)
+ a = self.new_task(loop, asyncio.sleep(0.1, loop=loop))
@asyncio.coroutine
def sleeper():
yield from asyncio.sleep(0.15, loop=loop)
raise ZeroDivisionError('really')
- b = asyncio.Task(sleeper(), loop=loop)
+ b = self.new_task(loop, sleeper())
@asyncio.coroutine
def foo():
@@ -935,10 +950,10 @@ class TaskTests(test_utils.TestCase):
errors = set(f for f in done if f.exception() is not None)
self.assertEqual(len(errors), 1)
- loop.run_until_complete(asyncio.Task(foo(), loop=loop))
+ loop.run_until_complete(self.new_task(loop, foo()))
self.assertAlmostEqual(0.15, loop.time())
- loop.run_until_complete(asyncio.Task(foo(), loop=loop))
+ loop.run_until_complete(self.new_task(loop, foo()))
self.assertAlmostEqual(0.15, loop.time())
def test_wait_with_timeout(self):
@@ -954,8 +969,8 @@ class TaskTests(test_utils.TestCase):
loop = self.new_test_loop(gen)
- a = asyncio.Task(asyncio.sleep(0.1, loop=loop), loop=loop)
- b = asyncio.Task(asyncio.sleep(0.15, loop=loop), loop=loop)
+ a = self.new_task(loop, asyncio.sleep(0.1, loop=loop))
+ b = self.new_task(loop, asyncio.sleep(0.15, loop=loop))
@asyncio.coroutine
def foo():
@@ -964,7 +979,7 @@ class TaskTests(test_utils.TestCase):
self.assertEqual(done, set([a]))
self.assertEqual(pending, set([b]))
- loop.run_until_complete(asyncio.Task(foo(), loop=loop))
+ loop.run_until_complete(self.new_task(loop, foo()))
self.assertAlmostEqual(0.11, loop.time())
# move forward to close generator
@@ -984,8 +999,8 @@ class TaskTests(test_utils.TestCase):
loop = self.new_test_loop(gen)
- a = asyncio.Task(asyncio.sleep(0.1, loop=loop), loop=loop)
- b = asyncio.Task(asyncio.sleep(0.15, loop=loop), loop=loop)
+ a = self.new_task(loop, asyncio.sleep(0.1, loop=loop))
+ b = self.new_task(loop, asyncio.sleep(0.15, loop=loop))
done, pending = loop.run_until_complete(
asyncio.wait([b, a], timeout=0.1, loop=loop))
@@ -1033,14 +1048,14 @@ class TaskTests(test_utils.TestCase):
values.append((yield from f))
return values
- res = loop.run_until_complete(asyncio.Task(foo(), loop=loop))
+ res = loop.run_until_complete(self.new_task(loop, foo()))
self.assertAlmostEqual(0.15, loop.time())
self.assertTrue('a' in res[:2])
self.assertTrue('b' in res[:2])
self.assertEqual(res[2], 'c')
# Doing it again should take no time and exercise a different path.
- res = loop.run_until_complete(asyncio.Task(foo(), loop=loop))
+ res = loop.run_until_complete(self.new_task(loop, foo()))
self.assertAlmostEqual(0.15, loop.time())
def test_as_completed_with_timeout(self):
@@ -1069,7 +1084,7 @@ class TaskTests(test_utils.TestCase):
values.append((2, exc))
return values
- res = loop.run_until_complete(asyncio.Task(foo(), loop=loop))
+ res = loop.run_until_complete(self.new_task(loop, foo()))
self.assertEqual(len(res), 2, res)
self.assertEqual(res[0], (1, 'a'))
self.assertEqual(res[1][0], 2)
@@ -1097,7 +1112,7 @@ class TaskTests(test_utils.TestCase):
v = yield from f
self.assertEqual(v, 'a')
- loop.run_until_complete(asyncio.Task(foo(), loop=loop))
+ loop.run_until_complete(self.new_task(loop, foo()))
def test_as_completed_reverse_wait(self):
@@ -1157,7 +1172,7 @@ class TaskTests(test_utils.TestCase):
result.append((yield from f))
return result
- fut = asyncio.Task(runner(), loop=self.loop)
+ fut = self.new_task(self.loop, runner())
self.loop.run_until_complete(fut)
result = fut.result()
self.assertEqual(set(result), {'ham', 'spam'})
@@ -1180,7 +1195,7 @@ class TaskTests(test_utils.TestCase):
res = yield from asyncio.sleep(dt/2, arg, loop=loop)
return res
- t = asyncio.Task(sleeper(0.1, 'yeah'), loop=loop)
+ t = self.new_task(loop, sleeper(0.1, 'yeah'))
loop.run_until_complete(t)
self.assertTrue(t.done())
self.assertEqual(t.result(), 'yeah')
@@ -1195,8 +1210,7 @@ class TaskTests(test_utils.TestCase):
loop = self.new_test_loop(gen)
- t = asyncio.Task(asyncio.sleep(10.0, 'yeah', loop=loop),
- loop=loop)
+ t = self.new_task(loop, asyncio.sleep(10.0, 'yeah', loop=loop))
handle = None
orig_call_later = loop.call_later
@@ -1232,7 +1246,7 @@ class TaskTests(test_utils.TestCase):
@asyncio.coroutine
def doit():
- sleeper = asyncio.Task(sleep(5000), loop=loop)
+ sleeper = self.new_task(loop, sleep(5000))
loop.call_later(0.1, sleeper.cancel)
try:
yield from sleeper
@@ -1246,13 +1260,13 @@ class TaskTests(test_utils.TestCase):
self.assertAlmostEqual(0.1, loop.time())
def test_task_cancel_waiter_future(self):
- fut = asyncio.Future(loop=self.loop)
+ fut = self.new_future(self.loop)
@asyncio.coroutine
def coro():
yield from fut
- task = asyncio.Task(coro(), loop=self.loop)
+ task = self.new_task(self.loop, coro())
test_utils.run_briefly(self.loop)
self.assertIs(task._fut_waiter, fut)
@@ -1269,7 +1283,7 @@ class TaskTests(test_utils.TestCase):
return 'ko'
gen = notmuch()
- task = asyncio.Task(gen, loop=self.loop)
+ task = self.new_task(self.loop, gen)
task.set_result('ok')
self.assertRaises(AssertionError, task._step)
@@ -1305,7 +1319,7 @@ class TaskTests(test_utils.TestCase):
nonlocal result
result = yield from fut
- t = asyncio.Task(wait_for_future(), loop=self.loop)
+ t = self.new_task(self.loop, wait_for_future())
test_utils.run_briefly(self.loop)
self.assertTrue(fut.cb_added)
@@ -1321,7 +1335,7 @@ class TaskTests(test_utils.TestCase):
def notmutch():
raise BaseException()
- task = asyncio.Task(notmutch(), loop=self.loop)
+ task = self.new_task(self.loop, notmutch())
self.assertRaises(BaseException, task._step)
self.assertTrue(task.done())
@@ -1349,7 +1363,7 @@ class TaskTests(test_utils.TestCase):
except asyncio.CancelledError:
raise base_exc
- task = asyncio.Task(notmutch(), loop=loop)
+ task = self.new_task(loop, notmutch())
test_utils.run_briefly(loop)
task.cancel()
@@ -1379,7 +1393,7 @@ class TaskTests(test_utils.TestCase):
self.assertFalse(asyncio.iscoroutinefunction(mock.Mock()))
def test_yield_vs_yield_from(self):
- fut = asyncio.Future(loop=self.loop)
+ fut = self.new_future(self.loop)
@asyncio.coroutine
def wait_for_future():
@@ -1423,7 +1437,7 @@ class TaskTests(test_utils.TestCase):
self.assertEqual(res, 'test')
def test_coroutine_non_gen_function_return_future(self):
- fut = asyncio.Future(loop=self.loop)
+ fut = self.new_future(self.loop)
@asyncio.coroutine
def func():
@@ -1433,49 +1447,53 @@ class TaskTests(test_utils.TestCase):
def coro():
fut.set_result('test')
- t1 = asyncio.Task(func(), loop=self.loop)
- t2 = asyncio.Task(coro(), loop=self.loop)
+ t1 = self.new_task(self.loop, func())
+ t2 = self.new_task(self.loop, coro())
res = self.loop.run_until_complete(t1)
self.assertEqual(res, 'test')
self.assertIsNone(t2.result())
def test_current_task(self):
- self.assertIsNone(asyncio.Task.current_task(loop=self.loop))
+ Task = self.__class__.Task
+
+ self.assertIsNone(Task.current_task(loop=self.loop))
@asyncio.coroutine
def coro(loop):
- self.assertTrue(asyncio.Task.current_task(loop=loop) is task)
+ self.assertTrue(Task.current_task(loop=loop) is task)
- task = asyncio.Task(coro(self.loop), loop=self.loop)
+ task = self.new_task(self.loop, coro(self.loop))
self.loop.run_until_complete(task)
- self.assertIsNone(asyncio.Task.current_task(loop=self.loop))
+ self.assertIsNone(Task.current_task(loop=self.loop))
def test_current_task_with_interleaving_tasks(self):
- self.assertIsNone(asyncio.Task.current_task(loop=self.loop))
+ Task = self.__class__.Task
- fut1 = asyncio.Future(loop=self.loop)
- fut2 = asyncio.Future(loop=self.loop)
+ self.assertIsNone(Task.current_task(loop=self.loop))
+
+ fut1 = self.new_future(self.loop)
+ fut2 = self.new_future(self.loop)
@asyncio.coroutine
def coro1(loop):
- self.assertTrue(asyncio.Task.current_task(loop=loop) is task1)
+ self.assertTrue(Task.current_task(loop=loop) is task1)
yield from fut1
- self.assertTrue(asyncio.Task.current_task(loop=loop) is task1)
+ self.assertTrue(Task.current_task(loop=loop) is task1)
fut2.set_result(True)
@asyncio.coroutine
def coro2(loop):
- self.assertTrue(asyncio.Task.current_task(loop=loop) is task2)
+ self.assertTrue(Task.current_task(loop=loop) is task2)
fut1.set_result(True)
yield from fut2
- self.assertTrue(asyncio.Task.current_task(loop=loop) is task2)
+ self.assertTrue(Task.current_task(loop=loop) is task2)
- task1 = asyncio.Task(coro1(self.loop), loop=self.loop)
- task2 = asyncio.Task(coro2(self.loop), loop=self.loop)
+ task1 = self.new_task(self.loop, coro1(self.loop))
+ task2 = self.new_task(self.loop, coro2(self.loop))
self.loop.run_until_complete(asyncio.wait((task1, task2),
loop=self.loop))
- self.assertIsNone(asyncio.Task.current_task(loop=self.loop))
+ self.assertIsNone(Task.current_task(loop=self.loop))
# Some thorough tests for cancellation propagation through
# coroutines, tasks and wait().
@@ -1483,7 +1501,7 @@ class TaskTests(test_utils.TestCase):
def test_yield_future_passes_cancel(self):
# Cancelling outer() cancels inner() cancels waiter.
proof = 0
- waiter = asyncio.Future(loop=self.loop)
+ waiter = self.new_future(self.loop)
@asyncio.coroutine
def inner():
@@ -1517,7 +1535,7 @@ class TaskTests(test_utils.TestCase):
# Cancelling outer() makes wait() return early, leaves inner()
# running.
proof = 0
- waiter = asyncio.Future(loop=self.loop)
+ waiter = self.new_future(self.loop)
@asyncio.coroutine
def inner():
@@ -1541,14 +1559,14 @@ class TaskTests(test_utils.TestCase):
self.assertEqual(proof, 1)
def test_shield_result(self):
- inner = asyncio.Future(loop=self.loop)
+ inner = self.new_future(self.loop)
outer = asyncio.shield(inner)
inner.set_result(42)
res = self.loop.run_until_complete(outer)
self.assertEqual(res, 42)
def test_shield_exception(self):
- inner = asyncio.Future(loop=self.loop)
+ inner = self.new_future(self.loop)
outer = asyncio.shield(inner)
test_utils.run_briefly(self.loop)
exc = RuntimeError('expected')
@@ -1557,7 +1575,7 @@ class TaskTests(test_utils.TestCase):
self.assertIs(outer.exception(), exc)
def test_shield_cancel(self):
- inner = asyncio.Future(loop=self.loop)
+ inner = self.new_future(self.loop)
outer = asyncio.shield(inner)
test_utils.run_briefly(self.loop)
inner.cancel()
@@ -1565,7 +1583,7 @@ class TaskTests(test_utils.TestCase):
self.assertTrue(outer.cancelled())
def test_shield_shortcut(self):
- fut = asyncio.Future(loop=self.loop)
+ fut = self.new_future(self.loop)
fut.set_result(42)
res = self.loop.run_until_complete(asyncio.shield(fut))
self.assertEqual(res, 42)
@@ -1573,7 +1591,7 @@ class TaskTests(test_utils.TestCase):
def test_shield_effect(self):
# Cancelling outer() does not affect inner().
proof = 0
- waiter = asyncio.Future(loop=self.loop)
+ waiter = self.new_future(self.loop)
@asyncio.coroutine
def inner():
@@ -1597,8 +1615,8 @@ class TaskTests(test_utils.TestCase):
self.assertEqual(proof, 1)
def test_shield_gather(self):
- child1 = asyncio.Future(loop=self.loop)
- child2 = asyncio.Future(loop=self.loop)
+ child1 = self.new_future(self.loop)
+ child2 = self.new_future(self.loop)
parent = asyncio.gather(child1, child2, loop=self.loop)
outer = asyncio.shield(parent, loop=self.loop)
test_utils.run_briefly(self.loop)
@@ -1611,8 +1629,8 @@ class TaskTests(test_utils.TestCase):
self.assertEqual(parent.result(), [1, 2])
def test_gather_shield(self):
- child1 = asyncio.Future(loop=self.loop)
- child2 = asyncio.Future(loop=self.loop)
+ child1 = self.new_future(self.loop)
+ child2 = self.new_future(self.loop)
inner1 = asyncio.shield(child1, loop=self.loop)
inner2 = asyncio.shield(child2, loop=self.loop)
parent = asyncio.gather(inner1, inner2, loop=self.loop)
@@ -1628,7 +1646,7 @@ class TaskTests(test_utils.TestCase):
test_utils.run_briefly(self.loop)
def test_as_completed_invalid_args(self):
- fut = asyncio.Future(loop=self.loop)
+ fut = self.new_future(self.loop)
# as_completed() expects a list of futures, not a future instance
self.assertRaises(TypeError, self.loop.run_until_complete,
@@ -1639,7 +1657,7 @@ class TaskTests(test_utils.TestCase):
coro.close()
def test_wait_invalid_args(self):
- fut = asyncio.Future(loop=self.loop)
+ fut = self.new_future(self.loop)
# wait() expects a list of futures, not a future instance
self.assertRaises(TypeError, self.loop.run_until_complete,
@@ -1666,7 +1684,7 @@ class TaskTests(test_utils.TestCase):
yield from fut
# A completed Future used to run the coroutine.
- fut = asyncio.Future(loop=self.loop)
+ fut = self.new_future(self.loop)
fut.set_result(None)
# Call the coroutine.
@@ -1700,15 +1718,15 @@ class TaskTests(test_utils.TestCase):
@asyncio.coroutine
def t2():
- f = asyncio.Future(loop=self.loop)
- asyncio.Task(t3(f), loop=self.loop)
+ f = self.new_future(self.loop)
+ self.new_task(self.loop, t3(f))
return (yield from f)
@asyncio.coroutine
def t3(f):
f.set_result((1, 2, 3))
- task = asyncio.Task(t1(), loop=self.loop)
+ task = self.new_task(self.loop, t1())
val = self.loop.run_until_complete(task)
self.assertEqual(val, (1, 2, 3))
@@ -1771,9 +1789,11 @@ class TaskTests(test_utils.TestCase):
@unittest.skipUnless(PY34,
'need python 3.4 or later')
def test_log_destroyed_pending_task(self):
+ Task = self.__class__.Task
+
@asyncio.coroutine
def kill_me(loop):
- future = asyncio.Future(loop=loop)
+ future = self.new_future(loop)
yield from future
# at this point, the only reference to kill_me() task is
# the Task._wakeup() method in future._callbacks
@@ -1786,7 +1806,7 @@ class TaskTests(test_utils.TestCase):
# schedule the task
coro = kill_me(self.loop)
task = asyncio.ensure_future(coro, loop=self.loop)
- self.assertEqual(asyncio.Task.all_tasks(loop=self.loop), {task})
+ self.assertEqual(Task.all_tasks(loop=self.loop), {task})
# execute the task so it waits for future
self.loop._run_once()
@@ -1801,7 +1821,7 @@ class TaskTests(test_utils.TestCase):
# no more reference to kill_me() task: the task is destroyed by the GC
support.gc_collect()
- self.assertEqual(asyncio.Task.all_tasks(loop=self.loop), set())
+ self.assertEqual(Task.all_tasks(loop=self.loop), set())
mock_handler.assert_called_with(self.loop, {
'message': 'Task was destroyed but it is pending!',
@@ -1866,10 +1886,10 @@ class TaskTests(test_utils.TestCase):
def test_task_source_traceback(self):
self.loop.set_debug(True)
- task = asyncio.Task(coroutine_function(), loop=self.loop)
+ task = self.new_task(self.loop, coroutine_function())
lineno = sys._getframe().f_lineno - 1
self.assertIsInstance(task._source_traceback, list)
- self.assertEqual(task._source_traceback[-1][:3],
+ self.assertEqual(task._source_traceback[-2][:3],
(__file__,
lineno,
'test_task_source_traceback'))
@@ -1881,7 +1901,7 @@ class TaskTests(test_utils.TestCase):
@asyncio.coroutine
def blocking_coroutine():
- fut = asyncio.Future(loop=loop)
+ fut = self.new_future(loop)
# Block: fut result is never set
yield from fut
@@ -1908,7 +1928,7 @@ class TaskTests(test_utils.TestCase):
loop = asyncio.new_event_loop()
self.addCleanup(loop.close)
- fut = asyncio.Future(loop=loop)
+ fut = self.new_future(loop)
# The indirection fut->child_coro is needed since otherwise the
# gathering task is done at the same time as the child future
def child_coro():
@@ -1932,6 +1952,172 @@ class TaskTests(test_utils.TestCase):
self.assertFalse(gather_task.cancelled())
self.assertEqual(gather_task.result(), [42])
+ def test_exception_traceback(self):
+ # See http://bugs.python.org/issue28843
+
+ @asyncio.coroutine
+ def foo():
+ 1 / 0
+
+ @asyncio.coroutine
+ def main():
+ task = self.new_task(self.loop, foo())
+ yield # skip one loop iteration
+ self.assertIsNotNone(task.exception().__traceback__)
+
+ self.loop.run_until_complete(main())
+
+ @mock.patch('asyncio.base_events.logger')
+ def test_error_in_call_soon(self, m_log):
+ def call_soon(callback, *args):
+ raise ValueError
+ self.loop.call_soon = call_soon
+
+ @asyncio.coroutine
+ def coro():
+ pass
+
+ self.assertFalse(m_log.error.called)
+
+ with self.assertRaises(ValueError):
+ self.new_task(self.loop, coro())
+
+ self.assertTrue(m_log.error.called)
+ message = m_log.error.call_args[0][0]
+ self.assertIn('Task was destroyed but it is pending', message)
+
+ self.assertEqual(self.Task.all_tasks(self.loop), set())
+
+
+def add_subclass_tests(cls):
+ BaseTask = cls.Task
+ BaseFuture = cls.Future
+
+ if BaseTask is None or BaseFuture is None:
+ return cls
+
+ class CommonFuture:
+ def __init__(self, *args, **kwargs):
+ self.calls = collections.defaultdict(lambda: 0)
+ super().__init__(*args, **kwargs)
+
+ def _schedule_callbacks(self):
+ self.calls['_schedule_callbacks'] += 1
+ return super()._schedule_callbacks()
+
+ def add_done_callback(self, *args):
+ self.calls['add_done_callback'] += 1
+ return super().add_done_callback(*args)
+
+ class Task(CommonFuture, BaseTask):
+ def _step(self, *args):
+ self.calls['_step'] += 1
+ return super()._step(*args)
+
+ def _wakeup(self, *args):
+ self.calls['_wakeup'] += 1
+ return super()._wakeup(*args)
+
+ class Future(CommonFuture, BaseFuture):
+ pass
+
+ def test_subclasses_ctask_cfuture(self):
+ fut = self.Future(loop=self.loop)
+
+ async def func():
+ self.loop.call_soon(lambda: fut.set_result('spam'))
+ return await fut
+
+ task = self.Task(func(), loop=self.loop)
+
+ result = self.loop.run_until_complete(task)
+
+ self.assertEqual(result, 'spam')
+
+ self.assertEqual(
+ dict(task.calls),
+ {'_step': 2, '_wakeup': 1, 'add_done_callback': 1,
+ '_schedule_callbacks': 1})
+
+ self.assertEqual(
+ dict(fut.calls),
+ {'add_done_callback': 1, '_schedule_callbacks': 1})
+
+ # Add patched Task & Future back to the test case
+ cls.Task = Task
+ cls.Future = Future
+
+ # Add an extra unit-test
+ cls.test_subclasses_ctask_cfuture = test_subclasses_ctask_cfuture
+
+ # Disable the "test_task_source_traceback" test
+ # (the test is hardcoded for a particular call stack, which
+ # is slightly different for Task subclasses)
+ cls.test_task_source_traceback = None
+
+ return cls
+
+
+@unittest.skipUnless(hasattr(futures, '_CFuture'),
+ 'requires the C _asyncio module')
+class CTask_CFuture_Tests(BaseTaskTests, test_utils.TestCase):
+ Task = getattr(tasks, '_CTask', None)
+ Future = getattr(futures, '_CFuture', None)
+
+
+@unittest.skipUnless(hasattr(futures, '_CFuture'),
+ 'requires the C _asyncio module')
+@add_subclass_tests
+class CTask_CFuture_SubclassTests(BaseTaskTests, test_utils.TestCase):
+ Task = getattr(tasks, '_CTask', None)
+ Future = getattr(futures, '_CFuture', None)
+
+
+@unittest.skipUnless(hasattr(futures, '_CFuture'),
+ 'requires the C _asyncio module')
+class CTask_PyFuture_Tests(BaseTaskTests, test_utils.TestCase):
+ Task = getattr(tasks, '_CTask', None)
+ Future = futures._PyFuture
+
+
+@unittest.skipUnless(hasattr(futures, '_CFuture'),
+ 'requires the C _asyncio module')
+class PyTask_CFuture_Tests(BaseTaskTests, test_utils.TestCase):
+ Task = tasks._PyTask
+ Future = getattr(futures, '_CFuture', None)
+
+
+class PyTask_PyFuture_Tests(BaseTaskTests, test_utils.TestCase):
+ Task = tasks._PyTask
+ Future = futures._PyFuture
+
+
+@add_subclass_tests
+class PyTask_PyFuture_SubclassTests(BaseTaskTests, test_utils.TestCase):
+ Task = tasks._PyTask
+ Future = futures._PyFuture
+
+
+class GenericTaskTests(test_utils.TestCase):
+
+ def test_future_subclass(self):
+ self.assertTrue(issubclass(asyncio.Task, asyncio.Future))
+
+ def test_asyncio_module_compiled(self):
+ # Because of circular imports it's easy to make _asyncio
+ # module non-importable. This is a simple test that will
+ # fail on systems where C modules were successfully compiled
+ # (hence the test for _functools), but _asyncio somehow didn't.
+ try:
+ import _functools
+ except ImportError:
+ pass
+ else:
+ try:
+ import _asyncio
+ except ImportError:
+ self.fail('_asyncio module is missing')
+
class GatherTestsBase:
diff --git a/Lib/test/test_asyncore.py b/Lib/test/test_asyncore.py
index 3857916..51c6573 100644
--- a/Lib/test/test_asyncore.py
+++ b/Lib/test/test_asyncore.py
@@ -11,6 +11,9 @@ import struct
from test import support
from io import BytesIO
+if support.PGO:
+ raise unittest.SkipTest("test is not helpful for PGO")
+
try:
import threading
except ImportError:
@@ -92,7 +95,9 @@ def bind_af_aware(sock, addr):
if HAS_UNIX_SOCKETS and sock.family == socket.AF_UNIX:
# Make sure the path doesn't exist.
support.unlink(addr)
- sock.bind(addr)
+ support.bind_unix_socket(sock, addr)
+ else:
+ sock.bind(addr)
class HelperFunctionTests(unittest.TestCase):
diff --git a/Lib/test/test_augassign.py b/Lib/test/test_augassign.py
index 5093e9d..5930d9e 100644
--- a/Lib/test/test_augassign.py
+++ b/Lib/test/test_augassign.py
@@ -83,6 +83,10 @@ class AugAssignTest(unittest.TestCase):
def __iadd__(self, val):
return aug_test3(self.val + val)
+ class aug_test4(aug_test3):
+ """Blocks inheritance, and fallback to __add__"""
+ __iadd__ = None
+
x = aug_test(1)
y = x
x += 10
@@ -106,6 +110,10 @@ class AugAssignTest(unittest.TestCase):
self.assertTrue(y is not x)
self.assertEqual(x.val, 13)
+ x = aug_test4(4)
+ with self.assertRaises(TypeError):
+ x += 10
+
def testCustomMethods2(test_self):
output = []
diff --git a/Lib/test/test_pep352.py b/Lib/test/test_baseexception.py
index 7c98c46..27d514f 100644
--- a/Lib/test/test_pep352.py
+++ b/Lib/test/test_baseexception.py
@@ -1,6 +1,5 @@
import unittest
import builtins
-import warnings
import os
from platform import system as platform_system
diff --git a/Lib/test/test_bigmem.py b/Lib/test/test_bigmem.py
index 40ac9bf..6133bbc 100644
--- a/Lib/test/test_bigmem.py
+++ b/Lib/test/test_bigmem.py
@@ -14,7 +14,6 @@ from test.support import bigmemtest, _1G, _2G, _4G
import unittest
import operator
import sys
-import functools
# These tests all use one of the bigmemtest decorators to indicate how much
# memory they use and how much memory they need to be even meaningful. The
diff --git a/Lib/test/test_binascii.py b/Lib/test/test_binascii.py
index 034245d..6b3e437 100644
--- a/Lib/test/test_binascii.py
+++ b/Lib/test/test_binascii.py
@@ -159,11 +159,25 @@ class BinASCIITest(unittest.TestCase):
# Then calculate the hexbin4 binary-to-ASCII translation
rle = binascii.rlecode_hqx(self.data)
a = binascii.b2a_hqx(self.type2test(rle))
+
b, _ = binascii.a2b_hqx(self.type2test(a))
res = binascii.rledecode_hqx(b)
-
self.assertEqual(res, self.rawdata)
+ def test_rle(self):
+ # test repetition with a repetition longer than the limit of 255
+ data = (b'a' * 100 + b'b' + b'c' * 300)
+
+ encoded = binascii.rlecode_hqx(data)
+ self.assertEqual(encoded,
+ (b'a\x90d' # 'a' * 100
+ b'b' # 'b'
+ b'c\x90\xff' # 'c' * 255
+ b'c\x90-')) # 'c' * 45
+
+ decoded = binascii.rledecode_hqx(encoded)
+ self.assertEqual(decoded, data)
+
def test_hex(self):
# test hexlification
s = b'{s\005\000\000\000worldi\002\000\000\000s\005\000\000\000helloi\001\000\000\0000'
@@ -331,6 +345,16 @@ class BinASCIITest(unittest.TestCase):
# non-ASCII string
self.assertRaises(ValueError, a2b, "\x80")
+ def test_b2a_base64_newline(self):
+ # Issue #25357: test newline parameter
+ b = self.type2test(b'hello')
+ self.assertEqual(binascii.b2a_base64(b),
+ b'aGVsbG8=\n')
+ self.assertEqual(binascii.b2a_base64(b, newline=True),
+ b'aGVsbG8=\n')
+ self.assertEqual(binascii.b2a_base64(b, newline=False),
+ b'aGVsbG8=')
+
class ArrayBinASCIITest(BinASCIITest):
def type2test(self, s):
diff --git a/Lib/test/test_binhex.py b/Lib/test/test_binhex.py
index 9d4c85a..21f4463 100644
--- a/Lib/test/test_binhex.py
+++ b/Lib/test/test_binhex.py
@@ -4,7 +4,6 @@
Based on an original test by Roger E. Masse.
"""
import binhex
-import os
import unittest
from test import support
diff --git a/Lib/test/test_binop.py b/Lib/test/test_binop.py
index e9dbddc..3ed018e 100644
--- a/Lib/test/test_binop.py
+++ b/Lib/test/test_binop.py
@@ -2,7 +2,7 @@
import unittest
from test import support
-from operator import eq, ne, lt, gt, le, ge
+from operator import eq, le, ne
from abc import ABCMeta
def gcd(a, b):
@@ -388,6 +388,54 @@ class OperationOrderTests(unittest.TestCase):
self.assertEqual(op_sequence(eq, B, V), ['B.__eq__', 'V.__eq__'])
self.assertEqual(op_sequence(le, B, V), ['B.__le__', 'V.__ge__'])
+class SupEq(object):
+ """Class that can test equality"""
+ def __eq__(self, other):
+ return True
+
+class S(SupEq):
+ """Subclass of SupEq that should fail"""
+ __eq__ = None
+
+class F(object):
+ """Independent class that should fall back"""
+
+class X(object):
+ """Independent class that should fail"""
+ __eq__ = None
+
+class SN(SupEq):
+ """Subclass of SupEq that can test equality, but not non-equality"""
+ __ne__ = None
+
+class XN:
+ """Independent class that can test equality, but not non-equality"""
+ def __eq__(self, other):
+ return True
+ __ne__ = None
+
+class FallbackBlockingTests(unittest.TestCase):
+ """Unit tests for None method blocking"""
+
+ def test_fallback_rmethod_blocking(self):
+ e, f, s, x = SupEq(), F(), S(), X()
+ self.assertEqual(e, e)
+ self.assertEqual(e, f)
+ self.assertEqual(f, e)
+ # left operand is checked first
+ self.assertEqual(e, x)
+ self.assertRaises(TypeError, eq, x, e)
+ # S is a subclass, so it's always checked first
+ self.assertRaises(TypeError, eq, e, s)
+ self.assertRaises(TypeError, eq, s, e)
+
+ def test_fallback_ne_blocking(self):
+ e, sn, xn = SupEq(), SN(), XN()
+ self.assertFalse(e != e)
+ self.assertRaises(TypeError, ne, e, sn)
+ self.assertRaises(TypeError, ne, sn, e)
+ self.assertFalse(e != xn)
+ self.assertRaises(TypeError, ne, xn, e)
if __name__ == "__main__":
unittest.main()
diff --git a/Lib/test/test_bool.py b/Lib/test/test_bool.py
index d30a3b9..9f8f0e1 100644
--- a/Lib/test/test_bool.py
+++ b/Lib/test/test_bool.py
@@ -96,6 +96,13 @@ class BoolTest(unittest.TestCase):
self.assertEqual(False/1, 0)
self.assertIsNot(False/1, False)
+ self.assertEqual(True%1, 0)
+ self.assertIsNot(True%1, False)
+ self.assertEqual(True%2, 1)
+ self.assertIsNot(True%2, True)
+ self.assertEqual(False%1, 0)
+ self.assertIsNot(False%1, False)
+
for b in False, True:
for i in 0, 1, 2:
self.assertEqual(b**i, int(b)**i)
@@ -333,6 +340,17 @@ class BoolTest(unittest.TestCase):
except (Exception) as e_len:
self.assertEqual(str(e_bool), str(e_len))
+ def test_blocked(self):
+ class A:
+ __bool__ = None
+ self.assertRaises(TypeError, bool, A())
+
+ class B:
+ def __len__(self):
+ return 10
+ __bool__ = None
+ self.assertRaises(TypeError, bool, B())
+
def test_real_and_imag(self):
self.assertEqual(True.real, 1)
self.assertEqual(True.imag, 0)
diff --git a/Lib/test/test_buffer.py b/Lib/test/test_buffer.py
index 2eef9fc..b83f2f1 100644
--- a/Lib/test/test_buffer.py
+++ b/Lib/test/test_buffer.py
@@ -16,7 +16,6 @@ import unittest
from test import support
from itertools import permutations, product
from random import randrange, sample, choice
-from sysconfig import get_config_var
import warnings
import sys, array, io
from decimal import Decimal
diff --git a/Lib/test/test_bufio.py b/Lib/test/test_bufio.py
index 9931c84..fea6da4 100644
--- a/Lib/test/test_bufio.py
+++ b/Lib/test/test_bufio.py
@@ -59,7 +59,7 @@ class BufferSizeTest:
self.drive_one(b"1234567890\00\01\02\03\04\05\06")
def test_nullpat(self):
- self.drive_one(bytes(1000))
+ self.drive_one(b'\0' * 1000)
class CBufferSizeTest(BufferSizeTest, unittest.TestCase):
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index 0b03340..416316c 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -82,7 +82,7 @@ test_conv_no_sign = [
('', ValueError),
(' ', ValueError),
(' \t\t ', ValueError),
- (str(b'\u0663\u0661\u0664 ','raw-unicode-escape'), 314),
+ (str(br'\u0663\u0661\u0664 ','raw-unicode-escape'), 314),
(chr(0x200), ValueError),
]
@@ -104,7 +104,7 @@ test_conv_sign = [
('', ValueError),
(' ', ValueError),
(' \t\t ', ValueError),
- (str(b'\u0663\u0661\u0664 ','raw-unicode-escape'), 314),
+ (str(br'\u0663\u0661\u0664 ','raw-unicode-escape'), 314),
(chr(0x200), ValueError),
]
@@ -1627,6 +1627,16 @@ class TestSorted(unittest.TestCase):
self.assertEqual(data, sorted(copy, reverse=1))
self.assertNotEqual(data, copy)
+ def test_bad_arguments(self):
+ # Issue #29327: The first argument is positional-only.
+ sorted([])
+ with self.assertRaises(TypeError):
+ sorted(iterable=[])
+ # Other arguments are keyword-only
+ sorted([], key=None)
+ with self.assertRaises(TypeError):
+ sorted([], None)
+
def test_inputtypes(self):
s = 'abracadabra'
types = [list, tuple, str]
@@ -1708,21 +1718,11 @@ class TestType(unittest.TestCase):
self.assertEqual(x.spam(), 'spam42')
self.assertEqual(x.to_bytes(2, 'little'), b'\x2a\x00')
- def test_type_new_keywords(self):
- class B:
- def ham(self):
- return 'ham%d' % self
- C = type.__new__(type,
- name='C',
- bases=(B, int),
- dict={'spam': lambda self: 'spam%s' % self})
- self.assertEqual(C.__name__, 'C')
- self.assertEqual(C.__qualname__, 'C')
- self.assertEqual(C.__module__, __name__)
- self.assertEqual(C.__bases__, (B, int))
- self.assertIs(C.__base__, int)
- self.assertIn('spam', C.__dict__)
- self.assertNotIn('ham', C.__dict__)
+ def test_type_nokwargs(self):
+ with self.assertRaises(TypeError):
+ type('a', (), {}, x=5)
+ with self.assertRaises(TypeError):
+ type('a', (), dict={})
def test_type_name(self):
for name in 'A', '\xc4', '\U0001f40d', 'B.A', '42', '':
diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py
index 3288a5b..a103a7d 100644
--- a/Lib/test/test_bytes.py
+++ b/Lib/test/test_bytes.py
@@ -4,6 +4,7 @@ XXX This is a mess. Common tests should be unified with string_tests.py (and
the latter should be modernized).
"""
+import array
import os
import re
import sys
@@ -81,6 +82,18 @@ class BaseBytesTest:
self.assertRaises(ValueError, self.type2test, [Indexable(-1)])
self.assertRaises(ValueError, self.type2test, [Indexable(256)])
+ def test_from_buffer(self):
+ a = self.type2test(array.array('B', [1, 2, 3]))
+ self.assertEqual(a, b"\x01\x02\x03")
+
+ # http://bugs.python.org/issue29159
+ # Fallback when __index__ raises exception other than OverflowError
+ class B(bytes):
+ def __index__(self):
+ raise TypeError
+
+ self.assertEqual(self.type2test(B(b"foobar")), b"foobar")
+
def test_from_ssize(self):
self.assertEqual(self.type2test(0), b'')
self.assertEqual(self.type2test(1), b'\x00')
@@ -216,7 +229,7 @@ class BaseBytesTest:
self.assertEqual(b, self.type2test(sample[:-3], "utf-8"))
def test_decode(self):
- sample = "Hello world\n\u1234\u5678\u9abc\def0\def0"
+ sample = "Hello world\n\u1234\u5678\u9abc"
for enc in ("utf-8", "utf-16"):
b = self.type2test(sample, enc)
self.assertEqual(b.decode(enc), sample)
@@ -269,6 +282,7 @@ class BaseBytesTest:
self.assertNotIn(200, b)
self.assertRaises(ValueError, lambda: 300 in b)
self.assertRaises(ValueError, lambda: -1 in b)
+ self.assertRaises(ValueError, lambda: sys.maxsize+1 in b)
self.assertRaises(TypeError, lambda: None in b)
self.assertRaises(TypeError, lambda: float(ord('a')) in b)
self.assertRaises(TypeError, lambda: "a" in b)
@@ -300,6 +314,20 @@ class BaseBytesTest:
self.assertRaises(ValueError, self.type2test.fromhex, '\x00')
self.assertRaises(ValueError, self.type2test.fromhex, '12 \x00 34')
+ for data, pos in (
+ # invalid first hexadecimal character
+ ('12 x4 56', 3),
+ # invalid second hexadecimal character
+ ('12 3x 56', 4),
+ # two invalid hexadecimal characters
+ ('12 xy 56', 3),
+ # test non-ASCII string
+ ('12 3\xff 56', 4),
+ ):
+ with self.assertRaises(ValueError) as cm:
+ self.type2test.fromhex(data)
+ self.assertIn('at position %s' % pos, str(cm.exception))
+
def test_hex(self):
self.assertRaises(TypeError, self.type2test.hex)
self.assertRaises(TypeError, self.type2test.hex, 1)
@@ -674,6 +702,37 @@ class BaseBytesTest:
test.support.check_free_after_iterating(self, iter, self.type2test)
test.support.check_free_after_iterating(self, reversed, self.type2test)
+ def test_translate(self):
+ b = self.type2test(b'hello')
+ rosetta = bytearray(range(256))
+ rosetta[ord('o')] = ord('e')
+
+ self.assertRaises(TypeError, b.translate)
+ self.assertRaises(TypeError, b.translate, None, None)
+ self.assertRaises(ValueError, b.translate, bytes(range(255)))
+
+ c = b.translate(rosetta, b'hello')
+ self.assertEqual(b, b'hello')
+ self.assertIsInstance(c, self.type2test)
+
+ c = b.translate(rosetta)
+ d = b.translate(rosetta, b'')
+ self.assertEqual(c, d)
+ self.assertEqual(c, b'helle')
+
+ c = b.translate(rosetta, b'l')
+ self.assertEqual(c, b'hee')
+ c = b.translate(None, b'e')
+ self.assertEqual(c, b'hllo')
+
+ # test delete as a keyword argument
+ c = b.translate(rosetta, delete=b'')
+ self.assertEqual(c, b'helle')
+ c = b.translate(rosetta, delete=b'l')
+ self.assertEqual(c, b'hee')
+ c = b.translate(None, delete=b'e')
+ self.assertEqual(c, b'hllo')
+
class BytesTest(BaseBytesTest, unittest.TestCase):
type2test = bytes
@@ -722,31 +781,142 @@ class BytesTest(BaseBytesTest, unittest.TestCase):
# Test PyBytes_FromFormat()
def test_from_format(self):
- test.support.import_module('ctypes')
- from ctypes import pythonapi, py_object, c_int, c_char_p
+ ctypes = test.support.import_module('ctypes')
+ _testcapi = test.support.import_module('_testcapi')
+ from ctypes import pythonapi, py_object
+ from ctypes import (
+ c_int, c_uint,
+ c_long, c_ulong,
+ c_size_t, c_ssize_t,
+ c_char_p)
+
PyBytes_FromFormat = pythonapi.PyBytes_FromFormat
PyBytes_FromFormat.restype = py_object
+ # basic tests
self.assertEqual(PyBytes_FromFormat(b'format'),
b'format')
-
+ self.assertEqual(PyBytes_FromFormat(b'Hello %s !', b'world'),
+ b'Hello world !')
+
+ # test formatters
+ self.assertEqual(PyBytes_FromFormat(b'c=%c', c_int(0)),
+ b'c=\0')
+ self.assertEqual(PyBytes_FromFormat(b'c=%c', c_int(ord('@'))),
+ b'c=@')
+ self.assertEqual(PyBytes_FromFormat(b'c=%c', c_int(255)),
+ b'c=\xff')
+ self.assertEqual(PyBytes_FromFormat(b'd=%d ld=%ld zd=%zd',
+ c_int(1), c_long(2),
+ c_size_t(3)),
+ b'd=1 ld=2 zd=3')
+ self.assertEqual(PyBytes_FromFormat(b'd=%d ld=%ld zd=%zd',
+ c_int(-1), c_long(-2),
+ c_size_t(-3)),
+ b'd=-1 ld=-2 zd=-3')
+ self.assertEqual(PyBytes_FromFormat(b'u=%u lu=%lu zu=%zu',
+ c_uint(123), c_ulong(456),
+ c_size_t(789)),
+ b'u=123 lu=456 zu=789')
+ self.assertEqual(PyBytes_FromFormat(b'i=%i', c_int(123)),
+ b'i=123')
+ self.assertEqual(PyBytes_FromFormat(b'i=%i', c_int(-123)),
+ b'i=-123')
+ self.assertEqual(PyBytes_FromFormat(b'x=%x', c_int(0xabc)),
+ b'x=abc')
+
+ sizeof_ptr = ctypes.sizeof(c_char_p)
+
+ if os.name == 'nt':
+ # Windows (MSCRT)
+ ptr_format = '0x%0{}X'.format(2 * sizeof_ptr)
+ def ptr_formatter(ptr):
+ return (ptr_format % ptr)
+ else:
+ # UNIX (glibc)
+ def ptr_formatter(ptr):
+ return '%#x' % ptr
+
+ ptr = 0xabcdef
+ self.assertEqual(PyBytes_FromFormat(b'ptr=%p', c_char_p(ptr)),
+ ('ptr=' + ptr_formatter(ptr)).encode('ascii'))
+ self.assertEqual(PyBytes_FromFormat(b's=%s', c_char_p(b'cstr')),
+ b's=cstr')
+
+ # test minimum and maximum integer values
+ size_max = c_size_t(-1).value
+ for formatstr, ctypes_type, value, py_formatter in (
+ (b'%d', c_int, _testcapi.INT_MIN, str),
+ (b'%d', c_int, _testcapi.INT_MAX, str),
+ (b'%ld', c_long, _testcapi.LONG_MIN, str),
+ (b'%ld', c_long, _testcapi.LONG_MAX, str),
+ (b'%lu', c_ulong, _testcapi.ULONG_MAX, str),
+ (b'%zd', c_ssize_t, _testcapi.PY_SSIZE_T_MIN, str),
+ (b'%zd', c_ssize_t, _testcapi.PY_SSIZE_T_MAX, str),
+ (b'%zu', c_size_t, size_max, str),
+ (b'%p', c_char_p, size_max, ptr_formatter),
+ ):
+ self.assertEqual(PyBytes_FromFormat(formatstr, ctypes_type(value)),
+ py_formatter(value).encode('ascii')),
+
+ # width and precision (width is currently ignored)
+ self.assertEqual(PyBytes_FromFormat(b'%5s', b'a'),
+ b'a')
+ self.assertEqual(PyBytes_FromFormat(b'%.3s', b'abcdef'),
+ b'abc')
+
+ # '%%' formatter
+ self.assertEqual(PyBytes_FromFormat(b'%%'),
+ b'%')
+ self.assertEqual(PyBytes_FromFormat(b'[%%]'),
+ b'[%]')
+ self.assertEqual(PyBytes_FromFormat(b'%%%c', c_int(ord('_'))),
+ b'%_')
+ self.assertEqual(PyBytes_FromFormat(b'%%s'),
+ b'%s')
+
+ # Invalid formats and partial formatting
self.assertEqual(PyBytes_FromFormat(b'%'), b'%')
- self.assertEqual(PyBytes_FromFormat(b'%%'), b'%')
- self.assertEqual(PyBytes_FromFormat(b'%%s'), b'%s')
- self.assertEqual(PyBytes_FromFormat(b'[%%]'), b'[%]')
- self.assertEqual(PyBytes_FromFormat(b'%%%c', c_int(ord('_'))), b'%_')
-
- self.assertEqual(PyBytes_FromFormat(b'c:%c', c_int(255)),
- b'c:\xff')
- self.assertEqual(PyBytes_FromFormat(b's:%s', c_char_p(b'cstr')),
- b's:cstr')
+ self.assertEqual(PyBytes_FromFormat(b'x=%i y=%', c_int(2), c_int(3)),
+ b'x=2 y=%')
- # Issue #19969
+ # Issue #19969: %c must raise OverflowError for values
+ # not in the range [0; 255]
self.assertRaises(OverflowError,
PyBytes_FromFormat, b'%c', c_int(-1))
self.assertRaises(OverflowError,
PyBytes_FromFormat, b'%c', c_int(256))
+ def test_bytes_blocking(self):
+ class IterationBlocked(list):
+ __bytes__ = None
+ i = [0, 1, 2, 3]
+ self.assertEqual(bytes(i), b'\x00\x01\x02\x03')
+ self.assertRaises(TypeError, bytes, IterationBlocked(i))
+
+ # At least in CPython, because bytes.__new__ and the C API
+ # PyBytes_FromObject have different fallback rules, integer
+ # fallback is handled specially, so test separately.
+ class IntBlocked(int):
+ __bytes__ = None
+ self.assertEqual(bytes(3), b'\0\0\0')
+ self.assertRaises(TypeError, bytes, IntBlocked(3))
+
+ # While there is no separately-defined rule for handling bytes
+ # subclasses differently from other buffer-interface classes,
+ # an implementation may well special-case them (as CPython 2.x
+ # str did), so test them separately.
+ class BytesSubclassBlocked(bytes):
+ __bytes__ = None
+ self.assertEqual(bytes(b'ab'), b'ab')
+ self.assertRaises(TypeError, bytes, BytesSubclassBlocked(b'ab'))
+
+ class BufferBlocked(bytearray):
+ __bytes__ = None
+ ba, bb = bytearray(b'ab'), BufferBlocked(b'ab')
+ self.assertEqual(bytes(ba), b'ab')
+ self.assertRaises(TypeError, bytes, bb)
+
class ByteArrayTest(BaseBytesTest, unittest.TestCase):
type2test = bytearray
@@ -1332,24 +1502,6 @@ class AssortedBytesTest(unittest.TestCase):
self.assertRaises(SyntaxError, eval,
'b"%s"' % chr(c))
- def test_translate(self):
- b = b'hello'
- ba = bytearray(b)
- rosetta = bytearray(range(0, 256))
- rosetta[ord('o')] = ord('e')
- c = b.translate(rosetta, b'l')
- self.assertEqual(b, b'hello')
- self.assertEqual(c, b'hee')
- c = ba.translate(rosetta, b'l')
- self.assertEqual(ba, b'hello')
- self.assertEqual(c, b'hee')
- c = b.translate(None, b'e')
- self.assertEqual(c, b'hllo')
- c = ba.translate(None, b'e')
- self.assertEqual(c, b'hllo')
- self.assertRaises(TypeError, b.translate, None, None)
- self.assertRaises(TypeError, ba.translate, None, None)
-
def test_split_bytearray(self):
self.assertEqual(b'a b'.split(memoryview(b' ')), [b'a', b'b'])
@@ -1513,7 +1665,32 @@ class SubclassTest:
self.assertEqual(type(a), type(b))
self.assertEqual(type(a.y), type(b.y))
- test_fromhex = BaseBytesTest.test_fromhex
+ def test_fromhex(self):
+ b = self.type2test.fromhex('1a2B30')
+ self.assertEqual(b, b'\x1a\x2b\x30')
+ self.assertIs(type(b), self.type2test)
+
+ class B1(self.basetype):
+ def __new__(cls, value):
+ me = self.basetype.__new__(cls, value)
+ me.foo = 'bar'
+ return me
+
+ b = B1.fromhex('1a2B30')
+ self.assertEqual(b, b'\x1a\x2b\x30')
+ self.assertIs(type(b), B1)
+ self.assertEqual(b.foo, 'bar')
+
+ class B2(self.basetype):
+ def __init__(me, *args, **kwargs):
+ if self.basetype is not bytes:
+ self.basetype.__init__(me, *args, **kwargs)
+ me.foo = 'bar'
+
+ b = B2.fromhex('1a2B30')
+ self.assertEqual(b, b'\x1a\x2b\x30')
+ self.assertIs(type(b), B2)
+ self.assertEqual(b.foo, 'bar')
class ByteArraySubclass(bytearray):
diff --git a/Lib/test/test_bz2.py b/Lib/test/test_bz2.py
index 34f6478..eaa472a 100644
--- a/Lib/test/test_bz2.py
+++ b/Lib/test/test_bz2.py
@@ -6,6 +6,7 @@ from io import BytesIO, DEFAULT_BUFFER_SIZE
import os
import pickle
import glob
+import pathlib
import random
import shutil
import subprocess
@@ -553,13 +554,20 @@ class BZ2FileTest(BaseTest):
with BZ2File(str_filename, "rb") as f:
self.assertEqual(f.read(), self.DATA)
+ def testOpenPathLikeFilename(self):
+ filename = pathlib.Path(self.filename)
+ with BZ2File(filename, "wb") as f:
+ f.write(self.DATA)
+ with BZ2File(filename, "rb") as f:
+ self.assertEqual(f.read(), self.DATA)
+
def testDecompressLimited(self):
"""Decompressed data buffering should be limited"""
- bomb = bz2.compress(bytes(int(2e6)), compresslevel=9)
+ bomb = bz2.compress(b'\0' * int(2e6), compresslevel=9)
self.assertLess(len(bomb), _compression.BUFFER_SIZE)
decomp = BZ2File(BytesIO(bomb))
- self.assertEqual(bytes(1), decomp.read(1))
+ self.assertEqual(decomp.read(1), b'\0')
max_decomp = 1 + DEFAULT_BUFFER_SIZE
self.assertLessEqual(decomp._buffer.raw.tell(), max_decomp,
"Excessive amount of data was decompressed")
diff --git a/Lib/test/test_calendar.py b/Lib/test/test_calendar.py
index accf251..2bc4fee 100644
--- a/Lib/test/test_calendar.py
+++ b/Lib/test/test_calendar.py
@@ -722,19 +722,19 @@ class CommandLineTestCase(unittest.TestCase):
def assertFailure(self, *args):
rc, stdout, stderr = assert_python_failure('-m', 'calendar', *args)
- self.assertIn(b'Usage:', stderr)
+ self.assertIn(b'usage:', stderr)
self.assertEqual(rc, 2)
def test_help(self):
stdout = self.run_ok('-h')
- self.assertIn(b'Usage:', stdout)
+ self.assertIn(b'usage:', stdout)
self.assertIn(b'calendar.py', stdout)
self.assertIn(b'--help', stdout)
def test_illegal_arguments(self):
self.assertFailure('-z')
- #self.assertFailure('spam')
- #self.assertFailure('2004', 'spam')
+ self.assertFailure('spam')
+ self.assertFailure('2004', 'spam')
self.assertFailure('-t', 'html', '2004', '1')
def test_output_current_year(self):
@@ -835,5 +835,14 @@ class CommandLineTestCase(unittest.TestCase):
b'href="custom.css" />', stdout)
+class MiscTestCase(unittest.TestCase):
+ def test__all__(self):
+ blacklist = {'mdays', 'January', 'February', 'EPOCH',
+ 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY',
+ 'SATURDAY', 'SUNDAY', 'different_locale', 'c',
+ 'prweek', 'week', 'format', 'formatstring', 'main'}
+ support.check__all__(self, calendar, blacklist=blacklist)
+
+
if __name__ == "__main__":
unittest.main()
diff --git a/Lib/test/test_capi.py b/Lib/test/test_capi.py
index 1eadd22..6c3625d 100644
--- a/Lib/test/test_capi.py
+++ b/Lib/test/test_capi.py
@@ -4,8 +4,10 @@
import os
import pickle
import random
+import re
import subprocess
import sys
+import sysconfig
import textwrap
import time
import unittest
@@ -220,8 +222,8 @@ class CAPITest(unittest.TestCase):
br'result with an error set\n'
br'ValueError\n'
br'\n'
- br'During handling of the above exception, '
- br'another exception occurred:\n'
+ br'The above exception was the direct cause '
+ br'of the following exception:\n'
br'\n'
br'SystemError: <built-in '
br'function return_result_with_error> '
@@ -442,6 +444,7 @@ class EmbeddingTests(unittest.TestCase):
self.maxDiff = None
self.assertEqual(out.strip(), expected_output)
+
class SkipitemTest(unittest.TestCase):
def test_skipitem(self):
@@ -491,10 +494,10 @@ class SkipitemTest(unittest.TestCase):
_testcapi.parse_tuple_and_keywords(tuple_1, dict_b,
format.encode("ascii"), keywords)
when_not_skipped = False
- except TypeError as e:
+ except SystemError as e:
s = "argument 1 (impossible<bad format char>)"
when_not_skipped = (str(e) == s)
- except RuntimeError as e:
+ except TypeError:
when_not_skipped = False
# test the format unit when skipped
@@ -503,7 +506,7 @@ class SkipitemTest(unittest.TestCase):
_testcapi.parse_tuple_and_keywords(empty_tuple, dict_b,
optional_format.encode("ascii"), keywords)
when_skipped = False
- except RuntimeError as e:
+ except SystemError as e:
s = "impossible<bad format char>: '{}'".format(format)
when_skipped = (str(e) == s)
@@ -524,6 +527,32 @@ class SkipitemTest(unittest.TestCase):
self.assertRaises(ValueError, _testcapi.parse_tuple_and_keywords,
(), {}, b'', [42])
+ def test_positional_only(self):
+ parse = _testcapi.parse_tuple_and_keywords
+
+ parse((1, 2, 3), {}, b'OOO', ['', '', 'a'])
+ parse((1, 2), {'a': 3}, b'OOO', ['', '', 'a'])
+ with self.assertRaisesRegex(TypeError,
+ r'Function takes at least 2 positional arguments \(1 given\)'):
+ parse((1,), {'a': 3}, b'OOO', ['', '', 'a'])
+ parse((1,), {}, b'O|OO', ['', '', 'a'])
+ with self.assertRaisesRegex(TypeError,
+ r'Function takes at least 1 positional arguments \(0 given\)'):
+ parse((), {}, b'O|OO', ['', '', 'a'])
+ parse((1, 2), {'a': 3}, b'OO$O', ['', '', 'a'])
+ with self.assertRaisesRegex(TypeError,
+ r'Function takes exactly 2 positional arguments \(1 given\)'):
+ parse((1,), {'a': 3}, b'OO$O', ['', '', 'a'])
+ parse((1,), {}, b'O|O$O', ['', '', 'a'])
+ with self.assertRaisesRegex(TypeError,
+ r'Function takes at least 1 positional arguments \(0 given\)'):
+ parse((), {}, b'O|O$O', ['', '', 'a'])
+ with self.assertRaisesRegex(SystemError, r'Empty parameter name after \$'):
+ parse((1,), {}, b'O|$OO', ['', '', 'a'])
+ with self.assertRaisesRegex(SystemError, 'Empty keyword'):
+ parse((1,), {}, b'O|OO', ['', 'a', ''])
+
+
@unittest.skipUnless(threading, 'Threading required for this test.')
class TestThreadState(unittest.TestCase):
@@ -548,6 +577,7 @@ class TestThreadState(unittest.TestCase):
t.start()
t.join()
+
class Test_testcapi(unittest.TestCase):
def test__testcapi(self):
for name in dir(_testcapi):
@@ -556,5 +586,85 @@ class Test_testcapi(unittest.TestCase):
test = getattr(_testcapi, name)
test()
+
+class PyMemDebugTests(unittest.TestCase):
+ PYTHONMALLOC = 'debug'
+ # '0x04c06e0' or '04C06E0'
+ PTR_REGEX = r'(?:0x)?[0-9a-fA-F]+'
+
+ def check(self, code):
+ with support.SuppressCrashReport():
+ out = assert_python_failure('-c', code,
+ PYTHONMALLOC=self.PYTHONMALLOC)
+ stderr = out.err
+ return stderr.decode('ascii', 'replace')
+
+ def test_buffer_overflow(self):
+ out = self.check('import _testcapi; _testcapi.pymem_buffer_overflow()')
+ regex = (r"Debug memory block at address p={ptr}: API 'm'\n"
+ r" 16 bytes originally requested\n"
+ r" The [0-9] pad bytes at p-[0-9] are FORBIDDENBYTE, as expected.\n"
+ r" The [0-9] pad bytes at tail={ptr} are not all FORBIDDENBYTE \(0x[0-9a-f]{{2}}\):\n"
+ r" at tail\+0: 0x78 \*\*\* OUCH\n"
+ r" at tail\+1: 0xfb\n"
+ r" at tail\+2: 0xfb\n"
+ r" .*\n"
+ r" The block was made by call #[0-9]+ to debug malloc/realloc.\n"
+ r" Data at p: cb cb cb .*\n"
+ r"\n"
+ r"Fatal Python error: bad trailing pad byte")
+ regex = regex.format(ptr=self.PTR_REGEX)
+ regex = re.compile(regex, flags=re.DOTALL)
+ self.assertRegex(out, regex)
+
+ def test_api_misuse(self):
+ out = self.check('import _testcapi; _testcapi.pymem_api_misuse()')
+ regex = (r"Debug memory block at address p={ptr}: API 'm'\n"
+ r" 16 bytes originally requested\n"
+ r" The [0-9] pad bytes at p-[0-9] are FORBIDDENBYTE, as expected.\n"
+ r" The [0-9] pad bytes at tail={ptr} are FORBIDDENBYTE, as expected.\n"
+ r" The block was made by call #[0-9]+ to debug malloc/realloc.\n"
+ r" Data at p: cb cb cb .*\n"
+ r"\n"
+ r"Fatal Python error: bad ID: Allocated using API 'm', verified using API 'r'\n")
+ regex = regex.format(ptr=self.PTR_REGEX)
+ self.assertRegex(out, regex)
+
+ @unittest.skipUnless(threading, 'Test requires a GIL (multithreading)')
+ def check_malloc_without_gil(self, code):
+ out = self.check(code)
+ expected = ('Fatal Python error: Python memory allocator called '
+ 'without holding the GIL')
+ self.assertIn(expected, out)
+
+ def test_pymem_malloc_without_gil(self):
+ # Debug hooks must raise an error if PyMem_Malloc() is called
+ # without holding the GIL
+ code = 'import _testcapi; _testcapi.pymem_malloc_without_gil()'
+ self.check_malloc_without_gil(code)
+
+ def test_pyobject_malloc_without_gil(self):
+ # Debug hooks must raise an error if PyObject_Malloc() is called
+ # without holding the GIL
+ code = 'import _testcapi; _testcapi.pyobject_malloc_without_gil()'
+ self.check_malloc_without_gil(code)
+
+
+class PyMemMallocDebugTests(PyMemDebugTests):
+ PYTHONMALLOC = 'malloc_debug'
+
+
+@unittest.skipUnless(sysconfig.get_config_var('WITH_PYMALLOC') == 1,
+ 'need pymalloc')
+class PyMemPymallocDebugTests(PyMemDebugTests):
+ PYTHONMALLOC = 'pymalloc_debug'
+
+
+@unittest.skipUnless(Py_DEBUG, 'need Py_DEBUG')
+class PyMemDefaultTests(PyMemDebugTests):
+ # test default allocator of Python compiled in debug mode
+ PYTHONMALLOC = ''
+
+
if __name__ == "__main__":
unittest.main()
diff --git a/Lib/test/test_cgi.py b/Lib/test/test_cgi.py
index ab9f6ab..6373221 100644
--- a/Lib/test/test_cgi.py
+++ b/Lib/test/test_cgi.py
@@ -7,6 +7,7 @@ import unittest
import warnings
from collections import namedtuple
from io import StringIO, BytesIO
+from test import support
class HackedSysModule:
# The regression test will have real values in sys.argv, which
@@ -147,7 +148,7 @@ class CgiTests(unittest.TestCase):
def test_escape(self):
# cgi.escape() is deprecated.
with warnings.catch_warnings():
- warnings.filterwarnings('ignore', 'cgi\.escape',
+ warnings.filterwarnings('ignore', r'cgi\.escape',
DeprecationWarning)
self.assertEqual("test &amp; string", cgi.escape("test & string"))
self.assertEqual("&lt;test string&gt;", cgi.escape("<test string>"))
@@ -473,6 +474,11 @@ this is the content of the fake file
cgi.parse_header('form-data; name="files"; filename="fo\\"o;bar"'),
("form-data", {"name": "files", "filename": 'fo"o;bar'}))
+ def test_all(self):
+ blacklist = {"logfile", "logfp", "initlog", "dolog", "nolog",
+ "closelog", "log", "maxlen", "valid_boundary"}
+ support.check__all__(self, cgi, blacklist=blacklist)
+
BOUNDARY = "---------------------------721837373350705526688164684"
diff --git a/Lib/test/test_charmapcodec.py b/Lib/test/test_charmapcodec.py
index 4064aef..0d4594d 100644
--- a/Lib/test/test_charmapcodec.py
+++ b/Lib/test/test_charmapcodec.py
@@ -9,7 +9,7 @@ Written by Marc-Andre Lemburg (mal@lemburg.com).
"""#"
-import test.support, unittest
+import unittest
import codecs
diff --git a/Lib/test/test_cmath.py b/Lib/test/test_cmath.py
index 1f884e5..0451fb0 100644
--- a/Lib/test/test_cmath.py
+++ b/Lib/test/test_cmath.py
@@ -4,6 +4,8 @@ import test.test_math as test_math
import unittest
import cmath, math
from cmath import phase, polar, rect, pi
+import platform
+import sys
import sysconfig
INF = float('inf')
@@ -154,6 +156,23 @@ class CMathTests(unittest.TestCase):
self.assertAlmostEqual(cmath.e, e_expected, places=9,
msg="cmath.e is {}; should be {}".format(cmath.e, e_expected))
+ def test_infinity_and_nan_constants(self):
+ self.assertEqual(cmath.inf.real, math.inf)
+ self.assertEqual(cmath.inf.imag, 0.0)
+ self.assertEqual(cmath.infj.real, 0.0)
+ self.assertEqual(cmath.infj.imag, math.inf)
+
+ self.assertTrue(math.isnan(cmath.nan.real))
+ self.assertEqual(cmath.nan.imag, 0.0)
+ self.assertEqual(cmath.nanj.real, 0.0)
+ self.assertTrue(math.isnan(cmath.nanj.imag))
+
+ # Check consistency with reprs.
+ self.assertEqual(repr(cmath.inf), "inf")
+ self.assertEqual(repr(cmath.infj), "infj")
+ self.assertEqual(repr(cmath.nan), "nan")
+ self.assertEqual(repr(cmath.nanj), "nanj")
+
def test_user_object(self):
# Test automatic calling of __complex__ and __float__ by cmath
# functions
@@ -315,6 +334,18 @@ class CMathTests(unittest.TestCase):
@requires_IEEE_754
def test_specific_values(self):
+ # Some tests need to be skipped on ancient OS X versions.
+ # See issue #27953.
+ SKIP_ON_TIGER = {'tan0064'}
+
+ osx_version = None
+ if sys.platform == 'darwin':
+ version_txt = platform.mac_ver()[0]
+ try:
+ osx_version = tuple(map(int, version_txt.split('.')))
+ except ValueError:
+ pass
+
def rect_complex(z):
"""Wrapped version of rect that accepts a complex number instead of
two float arguments."""
@@ -328,6 +359,12 @@ class CMathTests(unittest.TestCase):
for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file):
arg = complex(ar, ai)
expected = complex(er, ei)
+
+ # Skip certain tests on OS X 10.4.
+ if osx_version is not None and osx_version < (10, 5):
+ if id in SKIP_ON_TIGER:
+ continue
+
if fn == 'rect':
function = rect_complex
elif fn == 'polar':
diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py
index 3d2f769..ae2bcd4 100644
--- a/Lib/test/test_cmd_line.py
+++ b/Lib/test/test_cmd_line.py
@@ -8,7 +8,7 @@ import shutil
import sys
import subprocess
import tempfile
-from test.support import script_helper
+from test.support import script_helper, is_android
from test.support.script_helper import (spawn_python, kill_python, assert_python_ok,
assert_python_failure)
@@ -43,7 +43,7 @@ class CmdLineTest(unittest.TestCase):
def test_version(self):
version = ('Python %d.%d' % sys.version_info[:2]).encode("ascii")
- for switch in '-V', '--version':
+ for switch in '-V', '--version', '-VV':
rc, out, err = assert_python_ok(switch)
self.assertFalse(err.startswith(version))
self.assertTrue(out.startswith(version))
@@ -178,15 +178,16 @@ class CmdLineTest(unittest.TestCase):
if not stdout.startswith(pattern):
raise AssertionError("%a doesn't start with %a" % (stdout, pattern))
- @unittest.skipUnless(sys.platform == 'darwin', 'test specific to Mac OS X')
- def test_osx_utf8(self):
+ @unittest.skipUnless((sys.platform == 'darwin' or
+ is_android), 'test specific to Mac OS X and Android')
+ def test_osx_android_utf8(self):
def check_output(text):
decoded = text.decode('utf-8', 'surrogateescape')
expected = ascii(decoded).encode('ascii') + b'\n'
env = os.environ.copy()
# C locale gives ASCII locale encoding, but Python uses UTF-8
- # to parse the command line arguments on Mac OS X
+ # to parse the command line arguments on Mac OS X and Android.
env['LC_ALL'] = 'C'
p = subprocess.Popen(
@@ -348,8 +349,9 @@ class CmdLineTest(unittest.TestCase):
test.support.SuppressCrashReport().__enter__()
sys.stdout.write('x')
os.close(sys.stdout.fileno())"""
- rc, out, err = assert_python_ok('-c', code)
+ rc, out, err = assert_python_failure('-c', code)
self.assertEqual(b'', out)
+ self.assertEqual(120, rc)
self.assertRegex(err.decode('ascii', 'ignore'),
'Exception ignored in.*\nOSError: .*')
diff --git a/Lib/test/test_cmd_line_script.py b/Lib/test/test_cmd_line_script.py
index 919f361..e058ecd 100644
--- a/Lib/test/test_cmd_line_script.py
+++ b/Lib/test/test_cmd_line_script.py
@@ -139,9 +139,8 @@ class CmdLineTest(unittest.TestCase):
expected_argv0, expected_path0,
expected_package, expected_loader,
*cmd_line_switches):
- if not __debug__:
- cmd_line_switches += ('-' + 'O' * sys.flags.optimize,)
- run_args = cmd_line_switches + (script_name,) + tuple(example_args)
+ run_args = [*support.optim_args_from_interpreter_flags(),
+ *cmd_line_switches, script_name, *example_args]
rc, out, err = assert_python_ok(*run_args, __isolated=False)
self._check_output(script_name, rc, out + err, expected_file,
expected_argv0, expected_path0,
@@ -430,7 +429,7 @@ class CmdLineTest(unittest.TestCase):
('builtins.x', br'Error while finding module specification.*'
br'AttributeError'),
('builtins.x.y', br'Error while finding module specification.*'
- br'ImportError.*No module named.*not a package'),
+ br'ModuleNotFoundError.*No module named.*not a package'),
('os.path', br'loader.*cannot handle'),
('importlib', br'No module named.*'
br'is a package and cannot be directly executed'),
diff --git a/Lib/test/test_code_module.py b/Lib/test/test_code_module.py
index 3394b39..1a8f699 100644
--- a/Lib/test/test_code_module.py
+++ b/Lib/test/test_code_module.py
@@ -69,7 +69,7 @@ class TestInteractiveConsole(unittest.TestCase):
# with banner
self.infunc.side_effect = EOFError('Finished')
self.console.interact(banner='Foo')
- self.assertEqual(len(self.stderr.method_calls), 2)
+ self.assertEqual(len(self.stderr.method_calls), 3)
banner_call = self.stderr.method_calls[0]
self.assertEqual(banner_call, ['write', ('Foo\n',), {}])
@@ -77,8 +77,36 @@ class TestInteractiveConsole(unittest.TestCase):
self.stderr.reset_mock()
self.infunc.side_effect = EOFError('Finished')
self.console.interact(banner='')
+ self.assertEqual(len(self.stderr.method_calls), 2)
+
+ def test_exit_msg(self):
+ # default exit message
+ self.infunc.side_effect = EOFError('Finished')
+ self.console.interact(banner='')
+ self.assertEqual(len(self.stderr.method_calls), 2)
+ err_msg = self.stderr.method_calls[1]
+ expected = 'now exiting InteractiveConsole...\n'
+ self.assertEqual(err_msg, ['write', (expected,), {}])
+
+ # no exit message
+ self.stderr.reset_mock()
+ self.infunc.side_effect = EOFError('Finished')
+ self.console.interact(banner='', exitmsg='')
self.assertEqual(len(self.stderr.method_calls), 1)
+ # custom exit message
+ self.stderr.reset_mock()
+ message = (
+ 'bye! \N{GREEK SMALL LETTER ZETA}\N{CYRILLIC SMALL LETTER ZHE}'
+ )
+ self.infunc.side_effect = EOFError('Finished')
+ self.console.interact(banner='', exitmsg=message)
+ self.assertEqual(len(self.stderr.method_calls), 2)
+ err_msg = self.stderr.method_calls[1]
+ expected = message + '\n'
+ self.assertEqual(err_msg, ['write', (expected,), {}])
+
+
def test_cause_tb(self):
self.infunc.side_effect = ["raise ValueError('') from AttributeError",
EOFError('Finished')]
diff --git a/Lib/test/test_codeccallbacks.py b/Lib/test/test_codeccallbacks.py
index ee1e28a..6a3e993 100644
--- a/Lib/test/test_codeccallbacks.py
+++ b/Lib/test/test_codeccallbacks.py
@@ -4,7 +4,6 @@ import sys
import test.support
import unicodedata
import unittest
-import warnings
class PosReturn:
# this can be used for configurable callbacks
@@ -281,12 +280,12 @@ class CodecCallbackTest(unittest.TestCase):
)
self.assertEqual(
- b"\\u3042\u3xxx".decode("unicode-escape", "test.handler1"),
+ b"\\u3042\\u3xxx".decode("unicode-escape", "test.handler1"),
"\u3042[<92><117><51>]xxx"
)
self.assertEqual(
- b"\\u3042\u3xx".decode("unicode-escape", "test.handler1"),
+ b"\\u3042\\u3xx".decode("unicode-escape", "test.handler1"),
"\u3042[<92><117><51>]xx"
)
diff --git a/Lib/test/test_codecencodings_cn.py b/Lib/test/test_codecencodings_cn.py
index d0e3a15..3bdf7d0 100644
--- a/Lib/test/test_codecencodings_cn.py
+++ b/Lib/test/test_codecencodings_cn.py
@@ -3,7 +3,6 @@
# Codec encoding tests for PRC encodings.
#
-from test import support
from test import multibytecodec_support
import unittest
diff --git a/Lib/test/test_codecencodings_hk.py b/Lib/test/test_codecencodings_hk.py
index bb9be11..c5e2f99 100644
--- a/Lib/test/test_codecencodings_hk.py
+++ b/Lib/test/test_codecencodings_hk.py
@@ -3,7 +3,6 @@
# Codec encoding tests for HongKong encodings.
#
-from test import support
from test import multibytecodec_support
import unittest
diff --git a/Lib/test/test_codecencodings_iso2022.py b/Lib/test/test_codecencodings_iso2022.py
index 8a3ca70..00ea1c3 100644
--- a/Lib/test/test_codecencodings_iso2022.py
+++ b/Lib/test/test_codecencodings_iso2022.py
@@ -1,6 +1,5 @@
# Codec encoding tests for ISO 2022 encodings.
-from test import support
from test import multibytecodec_support
import unittest
diff --git a/Lib/test/test_codecencodings_jp.py b/Lib/test/test_codecencodings_jp.py
index 44b63a0..94378d1 100644
--- a/Lib/test/test_codecencodings_jp.py
+++ b/Lib/test/test_codecencodings_jp.py
@@ -3,7 +3,6 @@
# Codec encoding tests for Japanese encodings.
#
-from test import support
from test import multibytecodec_support
import unittest
diff --git a/Lib/test/test_codecencodings_kr.py b/Lib/test/test_codecencodings_kr.py
index b6a74fb..863d16b 100644
--- a/Lib/test/test_codecencodings_kr.py
+++ b/Lib/test/test_codecencodings_kr.py
@@ -3,7 +3,6 @@
# Codec encoding tests for ROK encodings.
#
-from test import support
from test import multibytecodec_support
import unittest
diff --git a/Lib/test/test_codecencodings_tw.py b/Lib/test/test_codecencodings_tw.py
index 9174296..bb1d133 100644
--- a/Lib/test/test_codecencodings_tw.py
+++ b/Lib/test/test_codecencodings_tw.py
@@ -3,7 +3,6 @@
# Codec encoding tests for ROC encodings.
#
-from test import support
from test import multibytecodec_support
import unittest
diff --git a/Lib/test/test_codecmaps_cn.py b/Lib/test/test_codecmaps_cn.py
index f1bd384..89e51c6 100644
--- a/Lib/test/test_codecmaps_cn.py
+++ b/Lib/test/test_codecmaps_cn.py
@@ -3,7 +3,6 @@
# Codec mapping tests for PRC encodings
#
-from test import support
from test import multibytecodec_support
import unittest
diff --git a/Lib/test/test_codecmaps_hk.py b/Lib/test/test_codecmaps_hk.py
index 4c0c415..7a48d24 100644
--- a/Lib/test/test_codecmaps_hk.py
+++ b/Lib/test/test_codecmaps_hk.py
@@ -3,7 +3,6 @@
# Codec mapping tests for HongKong encodings
#
-from test import support
from test import multibytecodec_support
import unittest
diff --git a/Lib/test/test_codecmaps_jp.py b/Lib/test/test_codecmaps_jp.py
index 5773823..fdfec80 100644
--- a/Lib/test/test_codecmaps_jp.py
+++ b/Lib/test/test_codecmaps_jp.py
@@ -3,7 +3,6 @@
# Codec mapping tests for Japanese encodings
#
-from test import support
from test import multibytecodec_support
import unittest
diff --git a/Lib/test/test_codecmaps_kr.py b/Lib/test/test_codecmaps_kr.py
index 6cb41c8..471cd74 100644
--- a/Lib/test/test_codecmaps_kr.py
+++ b/Lib/test/test_codecmaps_kr.py
@@ -3,7 +3,6 @@
# Codec mapping tests for ROK encodings
#
-from test import support
from test import multibytecodec_support
import unittest
diff --git a/Lib/test/test_codecmaps_tw.py b/Lib/test/test_codecmaps_tw.py
index 2ea44b5..145a97d 100644
--- a/Lib/test/test_codecmaps_tw.py
+++ b/Lib/test/test_codecmaps_tw.py
@@ -3,7 +3,6 @@
# Codec mapping tests for ROC encodings
#
-from test import support
from test import multibytecodec_support
import unittest
diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py
index 013ec64..1e63ed8 100644
--- a/Lib/test/test_codecs.py
+++ b/Lib/test/test_codecs.py
@@ -4,16 +4,10 @@ import io
import locale
import sys
import unittest
-import warnings
import encodings
from test import support
-if sys.platform == 'win32':
- VISTA_OR_LATER = (sys.getwindowsversion().major >= 6)
-else:
- VISTA_OR_LATER = False
-
try:
import ctypes
except ImportError:
@@ -27,6 +21,7 @@ def coding_checker(self, coder):
self.assertEqual(coder(input), (expect, len(input)))
return check
+
class Queue(object):
"""
queue: write bytes at one end, read bytes from the other end
@@ -47,6 +42,7 @@ class Queue(object):
self._buffer = self._buffer[size:]
return s
+
class MixInCheckStateHandling:
def check_state_handling_decode(self, encoding, u, s):
for i in range(len(s)+1):
@@ -80,6 +76,7 @@ class MixInCheckStateHandling:
part2 = d.encode(u[i:], True)
self.assertEqual(s, part1+part2)
+
class ReadTest(MixInCheckStateHandling):
def check_partial(self, input, partialresults):
# get a StreamReader for the encoding and feed the bytestring version
@@ -358,6 +355,12 @@ class ReadTest(MixInCheckStateHandling):
self.assertEqual("[\uDC80]".encode(self.encoding, "replace"),
"[?]".encode(self.encoding))
+ # sequential surrogate characters
+ self.assertEqual("[\uD800\uDC80]".encode(self.encoding, "ignore"),
+ "[]".encode(self.encoding))
+ self.assertEqual("[\uD800\uDC80]".encode(self.encoding, "replace"),
+ "[??]".encode(self.encoding))
+
bom = "".encode(self.encoding)
for before, after in [("\U00010fff", "A"), ("[", "]"),
("A", "\U00010fff")]:
@@ -383,6 +386,7 @@ class ReadTest(MixInCheckStateHandling):
self.assertEqual(test_sequence.decode(self.encoding, "backslashreplace"),
before + backslashreplace + after)
+
class UTF32Test(ReadTest, unittest.TestCase):
encoding = "utf-32"
if sys.byteorder == 'little':
@@ -478,6 +482,7 @@ class UTF32Test(ReadTest, unittest.TestCase):
self.assertEqual('\U00010000' * 1024,
codecs.utf_32_decode(encoded_be)[0])
+
class UTF32LETest(ReadTest, unittest.TestCase):
encoding = "utf-32-le"
ill_formed_sequence = b"\x80\xdc\x00\x00"
@@ -523,6 +528,7 @@ class UTF32LETest(ReadTest, unittest.TestCase):
self.assertEqual('\U00010000' * 1024,
codecs.utf_32_le_decode(encoded)[0])
+
class UTF32BETest(ReadTest, unittest.TestCase):
encoding = "utf-32-be"
ill_formed_sequence = b"\x00\x00\xdc\x80"
@@ -747,6 +753,7 @@ class UTF8Test(ReadTest, unittest.TestCase):
encoding = "utf-8"
ill_formed_sequence = b"\xed\xb2\x80"
ill_formed_sequence_replace = "\ufffd" * 3
+ BOM = b''
def test_partial(self):
self.check_partial(
@@ -775,27 +782,49 @@ class UTF8Test(ReadTest, unittest.TestCase):
self.check_state_handling_decode(self.encoding,
u, u.encode(self.encoding))
+ def test_decode_error(self):
+ for data, error_handler, expected in (
+ (b'[\x80\xff]', 'ignore', '[]'),
+ (b'[\x80\xff]', 'replace', '[\ufffd\ufffd]'),
+ (b'[\x80\xff]', 'surrogateescape', '[\udc80\udcff]'),
+ (b'[\x80\xff]', 'backslashreplace', '[\\x80\\xff]'),
+ ):
+ with self.subTest(data=data, error_handler=error_handler,
+ expected=expected):
+ self.assertEqual(data.decode(self.encoding, error_handler),
+ expected)
+
def test_lone_surrogates(self):
super().test_lone_surrogates()
# not sure if this is making sense for
# UTF-16 and UTF-32
- self.assertEqual("[\uDC80]".encode('utf-8', "surrogateescape"),
- b'[\x80]')
+ self.assertEqual("[\uDC80]".encode(self.encoding, "surrogateescape"),
+ self.BOM + b'[\x80]')
+
+ with self.assertRaises(UnicodeEncodeError) as cm:
+ "[\uDC80\uD800\uDFFF]".encode(self.encoding, "surrogateescape")
+ exc = cm.exception
+ self.assertEqual(exc.object[exc.start:exc.end], '\uD800\uDFFF')
def test_surrogatepass_handler(self):
- self.assertEqual("abc\ud800def".encode("utf-8", "surrogatepass"),
- b"abc\xed\xa0\x80def")
- self.assertEqual(b"abc\xed\xa0\x80def".decode("utf-8", "surrogatepass"),
+ self.assertEqual("abc\ud800def".encode(self.encoding, "surrogatepass"),
+ self.BOM + b"abc\xed\xa0\x80def")
+ self.assertEqual("\U00010fff\uD800".encode(self.encoding, "surrogatepass"),
+ self.BOM + b"\xf0\x90\xbf\xbf\xed\xa0\x80")
+ self.assertEqual("[\uD800\uDC80]".encode(self.encoding, "surrogatepass"),
+ self.BOM + b'[\xed\xa0\x80\xed\xb2\x80]')
+
+ self.assertEqual(b"abc\xed\xa0\x80def".decode(self.encoding, "surrogatepass"),
"abc\ud800def")
- self.assertEqual("\U00010fff\uD800".encode("utf-8", "surrogatepass"),
- b"\xf0\x90\xbf\xbf\xed\xa0\x80")
- self.assertEqual(b"\xf0\x90\xbf\xbf\xed\xa0\x80".decode("utf-8", "surrogatepass"),
+ self.assertEqual(b"\xf0\x90\xbf\xbf\xed\xa0\x80".decode(self.encoding, "surrogatepass"),
"\U00010fff\uD800")
+
self.assertTrue(codecs.lookup_error("surrogatepass"))
with self.assertRaises(UnicodeDecodeError):
- b"abc\xed\xa0".decode("utf-8", "surrogatepass")
+ b"abc\xed\xa0".decode(self.encoding, "surrogatepass")
with self.assertRaises(UnicodeDecodeError):
- b"abc\xed\xa0z".decode("utf-8", "surrogatepass")
+ b"abc\xed\xa0z".decode(self.encoding, "surrogatepass")
+
@unittest.skipUnless(sys.platform == 'win32',
'cp65001 is a Windows-only codec')
@@ -807,18 +836,13 @@ class CP65001Test(ReadTest, unittest.TestCase):
('abc', 'strict', b'abc'),
('\xe9\u20ac', 'strict', b'\xc3\xa9\xe2\x82\xac'),
('\U0010ffff', 'strict', b'\xf4\x8f\xbf\xbf'),
+ ('\udc80', 'strict', None),
+ ('\udc80', 'ignore', b''),
+ ('\udc80', 'replace', b'?'),
+ ('\udc80', 'backslashreplace', b'\\udc80'),
+ ('\udc80', 'namereplace', b'\\udc80'),
+ ('\udc80', 'surrogatepass', b'\xed\xb2\x80'),
]
- if VISTA_OR_LATER:
- tests.extend((
- ('\udc80', 'strict', None),
- ('\udc80', 'ignore', b''),
- ('\udc80', 'replace', b'?'),
- ('\udc80', 'backslashreplace', b'\\udc80'),
- ('\udc80', 'namereplace', b'\\udc80'),
- ('\udc80', 'surrogatepass', b'\xed\xb2\x80'),
- ))
- else:
- tests.append(('\udc80', 'strict', b'\xed\xb2\x80'))
for text, errors, expected in tests:
if expected is not None:
try:
@@ -845,17 +869,10 @@ class CP65001Test(ReadTest, unittest.TestCase):
(b'[\xff]', 'ignore', '[]'),
(b'[\xff]', 'replace', '[\ufffd]'),
(b'[\xff]', 'surrogateescape', '[\udcff]'),
+ (b'[\xed\xb2\x80]', 'strict', None),
+ (b'[\xed\xb2\x80]', 'ignore', '[]'),
+ (b'[\xed\xb2\x80]', 'replace', '[\ufffd\ufffd\ufffd]'),
]
- if VISTA_OR_LATER:
- tests.extend((
- (b'[\xed\xb2\x80]', 'strict', None),
- (b'[\xed\xb2\x80]', 'ignore', '[]'),
- (b'[\xed\xb2\x80]', 'replace', '[\ufffd\ufffd\ufffd]'),
- ))
- else:
- tests.extend((
- (b'[\xed\xb2\x80]', 'strict', '[\udc80]'),
- ))
for raw, errors, expected in tests:
if expected is not None:
try:
@@ -870,7 +887,6 @@ class CP65001Test(ReadTest, unittest.TestCase):
self.assertRaises(UnicodeDecodeError,
raw.decode, 'cp65001', errors)
- @unittest.skipUnless(VISTA_OR_LATER, 'require Windows Vista or later')
def test_lone_surrogates(self):
self.assertRaises(UnicodeEncodeError, "\ud800".encode, "cp65001")
self.assertRaises(UnicodeDecodeError, b"\xed\xa0\x80".decode, "cp65001")
@@ -887,7 +903,6 @@ class CP65001Test(ReadTest, unittest.TestCase):
self.assertEqual("[\uDC80]".encode("cp65001", "replace"),
b'[?]')
- @unittest.skipUnless(VISTA_OR_LATER, 'require Windows Vista or later')
def test_surrogatepass_handler(self):
self.assertEqual("abc\ud800def".encode("cp65001", "surrogatepass"),
b"abc\xed\xa0\x80def")
@@ -1059,6 +1074,7 @@ class ReadBufferTest(unittest.TestCase):
class UTF8SigTest(UTF8Test, unittest.TestCase):
encoding = "utf-8-sig"
+ BOM = codecs.BOM_UTF8
def test_partial(self):
self.check_partial(
@@ -1159,7 +1175,7 @@ class EscapeDecodeTest(unittest.TestCase):
check(b"[\\\n]", b"[]")
check(br'[\"]', b'["]')
check(br"[\']", b"[']")
- check(br"[\\]", br"[\]")
+ check(br"[\\]", b"[\\]")
check(br"[\a]", b"[\x07]")
check(br"[\b]", b"[\x08]")
check(br"[\t]", b"[\x09]")
@@ -1168,7 +1184,6 @@ class EscapeDecodeTest(unittest.TestCase):
check(br"[\f]", b"[\x0c]")
check(br"[\r]", b"[\x0d]")
check(br"[\7]", b"[\x07]")
- check(br"[\8]", br"[\8]")
check(br"[\78]", b"[\x078]")
check(br"[\41]", b"[!]")
check(br"[\418]", b"[!8]")
@@ -1176,12 +1191,18 @@ class EscapeDecodeTest(unittest.TestCase):
check(br"[\1010]", b"[A0]")
check(br"[\501]", b"[A]")
check(br"[\x41]", b"[A]")
- check(br"[\X41]", br"[\X41]")
check(br"[\x410]", b"[A0]")
- for b in range(256):
- if b not in b'\n"\'\\abtnvfr01234567x':
- b = bytes([b])
- check(b'\\' + b, b'\\' + b)
+ for i in range(97, 123):
+ b = bytes([i])
+ if b not in b'abfnrtvx':
+ with self.assertWarns(DeprecationWarning):
+ check(b"\\" + b, b"\\" + b)
+ with self.assertWarns(DeprecationWarning):
+ check(b"\\" + b.upper(), b"\\" + b.upper())
+ with self.assertWarns(DeprecationWarning):
+ check(br"\8", b"\\8")
+ with self.assertWarns(DeprecationWarning):
+ check(br"\9", b"\\9")
def test_errors(self):
decode = codecs.escape_decode
@@ -1194,6 +1215,7 @@ class EscapeDecodeTest(unittest.TestCase):
self.assertEqual(decode(br"[\x0]\x0", "ignore"), (b"[]", 8))
self.assertEqual(decode(br"[\x0]\x0", "replace"), (b"[?]?", 8))
+
class RecodingTest(unittest.TestCase):
def test_recoding(self):
f = io.BytesIO()
@@ -1313,6 +1335,7 @@ for i in punycode_testcases:
if len(i)!=2:
print(repr(i))
+
class PunycodeTest(unittest.TestCase):
def test_encode(self):
for uni, puny in punycode_testcases:
@@ -1332,6 +1355,7 @@ class PunycodeTest(unittest.TestCase):
puny = puny.decode("ascii").encode("ascii")
self.assertEqual(uni, puny.decode("punycode"))
+
class UnicodeInternalTest(unittest.TestCase):
@unittest.skipUnless(SIZEOF_WCHAR_T == 4, 'specific to 32-bit wchar_t')
def test_bug1251300(self):
@@ -1586,6 +1610,7 @@ class NameprepTest(unittest.TestCase):
except Exception as e:
raise support.TestFailed("Test 3.%d: %s" % (pos+1, str(e)))
+
class IDNACodecTest(unittest.TestCase):
def test_builtin_decode(self):
self.assertEqual(str(b"python.org", "idna"), "python.org")
@@ -1672,6 +1697,7 @@ class IDNACodecTest(unittest.TestCase):
self.assertRaises(Exception,
b"python.org".decode, "idna", errors)
+
class CodecsModuleTest(unittest.TestCase):
def test_decode(self):
@@ -1780,6 +1806,7 @@ class CodecsModuleTest(unittest.TestCase):
self.assertRaises(UnicodeError,
codecs.decode, b'abc', 'undefined', errors)
+
class StreamReaderTest(unittest.TestCase):
def setUp(self):
@@ -1790,6 +1817,7 @@ class StreamReaderTest(unittest.TestCase):
f = self.reader(self.stream)
self.assertEqual(f.readlines(), ['\ud55c\n', '\uae00'])
+
class EncodedFileTest(unittest.TestCase):
def test_basic(self):
@@ -1909,6 +1937,8 @@ all_unicode_encodings = [
if hasattr(codecs, "mbcs_encode"):
all_unicode_encodings.append("mbcs")
+if hasattr(codecs, "oem_encode"):
+ all_unicode_encodings.append("oem")
# The following encoding is not tested, because it's not supposed
# to work:
@@ -1920,6 +1950,7 @@ broken_unicode_with_stateful = [
"unicode_internal"
]
+
class BasicUnicodeTest(unittest.TestCase, MixInCheckStateHandling):
def test_basics(self):
s = "abc123" # all codecs should be able to encode these
@@ -2082,6 +2113,7 @@ class BasicUnicodeTest(unittest.TestCase, MixInCheckStateHandling):
self.check_state_handling_decode(encoding, u, u.encode(encoding))
self.check_state_handling_encode(encoding, u, u.encode(encoding))
+
class CharmapTest(unittest.TestCase):
def test_decode_with_string_map(self):
self.assertEqual(
@@ -2332,6 +2364,7 @@ class WithStmtTest(unittest.TestCase):
info.streamwriter, 'strict') as srw:
self.assertEqual(srw.read(), "\xfc")
+
class TypesTest(unittest.TestCase):
def test_decode_unicode(self):
# Most decoders don't accept unicode input
@@ -2420,7 +2453,6 @@ class UnicodeEscapeTest(unittest.TestCase):
check(br"[\f]", "[\x0c]")
check(br"[\r]", "[\x0d]")
check(br"[\7]", "[\x07]")
- check(br"[\8]", r"[\8]")
check(br"[\78]", "[\x078]")
check(br"[\41]", "[!]")
check(br"[\418]", "[!8]")
@@ -2430,9 +2462,18 @@ class UnicodeEscapeTest(unittest.TestCase):
check(br"[\x410]", "[A0]")
check(br"\u20ac", "\u20ac")
check(br"\U0001d120", "\U0001d120")
- for b in range(256):
- if b not in b'\n"\'\\abtnvfr01234567xuUN':
- check(b'\\' + bytes([b]), '\\' + chr(b))
+ for i in range(97, 123):
+ b = bytes([i])
+ if b not in b'abfnrtuvx':
+ with self.assertWarns(DeprecationWarning):
+ check(b"\\" + b, "\\" + chr(i))
+ if b.upper() not in b'UN':
+ with self.assertWarns(DeprecationWarning):
+ check(b"\\" + b.upper(), "\\" + chr(i-32))
+ with self.assertWarns(DeprecationWarning):
+ check(br"\8", "\\8")
+ with self.assertWarns(DeprecationWarning):
+ check(br"\9", "\\9")
def test_decode_errors(self):
decode = codecs.unicode_escape_decode
@@ -2642,6 +2683,7 @@ else:
bytes_transform_encodings.append("bz2_codec")
transform_aliases["bz2_codec"] = ["bz2"]
+
class TransformCodecTest(unittest.TestCase):
def test_basics(self):
@@ -2694,8 +2736,8 @@ class TransformCodecTest(unittest.TestCase):
bad_input = "bad input type"
for encoding in bytes_transform_encodings:
with self.subTest(encoding=encoding):
- fmt = ( "{!r} is not a text encoding; "
- "use codecs.encode\(\) to handle arbitrary codecs")
+ fmt = (r"{!r} is not a text encoding; "
+ r"use codecs.encode\(\) to handle arbitrary codecs")
msg = fmt.format(encoding)
with self.assertRaisesRegex(LookupError, msg) as failure:
bad_input.encode(encoding)
@@ -2704,7 +2746,7 @@ class TransformCodecTest(unittest.TestCase):
def test_text_to_binary_blacklists_text_transforms(self):
# Check str.encode gives a good error message for str -> str codecs
msg = (r"^'rot_13' is not a text encoding; "
- "use codecs.encode\(\) to handle arbitrary codecs")
+ r"use codecs.encode\(\) to handle arbitrary codecs")
with self.assertRaisesRegex(LookupError, msg):
"just an example message".encode("rot_13")
@@ -2716,7 +2758,7 @@ class TransformCodecTest(unittest.TestCase):
with self.subTest(encoding=encoding):
encoded_data = codecs.encode(data, encoding)
fmt = (r"{!r} is not a text encoding; "
- "use codecs.decode\(\) to handle arbitrary codecs")
+ r"use codecs.decode\(\) to handle arbitrary codecs")
msg = fmt.format(encoding)
with self.assertRaisesRegex(LookupError, msg):
encoded_data.decode(encoding)
@@ -2728,7 +2770,7 @@ class TransformCodecTest(unittest.TestCase):
for bad_input in (b"immutable", bytearray(b"mutable")):
with self.subTest(bad_input=bad_input):
msg = (r"^'rot_13' is not a text encoding; "
- "use codecs.decode\(\) to handle arbitrary codecs")
+ r"use codecs.decode\(\) to handle arbitrary codecs")
with self.assertRaisesRegex(LookupError, msg) as failure:
bad_input.decode("rot_13")
self.assertIsNone(failure.exception.__cause__)
@@ -2947,12 +2989,12 @@ class ExceptionChainingTest(unittest.TestCase):
self.assertEqual(decoded, b"not str!")
# Text model methods should complain
fmt = (r"^{!r} encoder returned 'str' instead of 'bytes'; "
- "use codecs.encode\(\) to encode to arbitrary types$")
+ r"use codecs.encode\(\) to encode to arbitrary types$")
msg = fmt.format(self.codec_name)
with self.assertRaisesRegex(TypeError, msg):
"str_input".encode(self.codec_name)
fmt = (r"^{!r} decoder returned 'bytes' instead of 'str'; "
- "use codecs.decode\(\) to decode to arbitrary types$")
+ r"use codecs.decode\(\) to decode to arbitrary types$")
msg = fmt.format(self.codec_name)
with self.assertRaisesRegex(TypeError, msg):
b"bytes input".decode(self.codec_name)
@@ -3093,11 +3135,10 @@ class CodePageTest(unittest.TestCase):
(b'\xff\xf4\x8f\xbf\xbf', 'ignore', '\U0010ffff'),
(b'\xff\xf4\x8f\xbf\xbf', 'replace', '\ufffd\U0010ffff'),
))
- if VISTA_OR_LATER:
- self.check_encode(self.CP_UTF8, (
- ('[\U0010ffff\uDC80]', 'ignore', b'[\xf4\x8f\xbf\xbf]'),
- ('[\U0010ffff\uDC80]', 'replace', b'[\xf4\x8f\xbf\xbf?]'),
- ))
+ self.check_encode(self.CP_UTF8, (
+ ('[\U0010ffff\uDC80]', 'ignore', b'[\xf4\x8f\xbf\xbf]'),
+ ('[\U0010ffff\uDC80]', 'replace', b'[\xf4\x8f\xbf\xbf?]'),
+ ))
def test_incremental(self):
decoded = codecs.code_page_decode(932, b'\x82', 'strict', False)
@@ -3118,6 +3159,96 @@ class CodePageTest(unittest.TestCase):
False)
self.assertEqual(decoded, ('abc', 3))
+ def test_mbcs_alias(self):
+ # Check that looking up our 'default' codepage will return
+ # mbcs when we don't have a more specific one available
+ import _bootlocale
+ def _get_fake_codepage(*a):
+ return 'cp123'
+ old_getpreferredencoding = _bootlocale.getpreferredencoding
+ _bootlocale.getpreferredencoding = _get_fake_codepage
+ try:
+ codec = codecs.lookup('cp123')
+ self.assertEqual(codec.name, 'mbcs')
+ finally:
+ _bootlocale.getpreferredencoding = old_getpreferredencoding
+
+
+class ASCIITest(unittest.TestCase):
+ def test_encode(self):
+ self.assertEqual('abc123'.encode('ascii'), b'abc123')
+
+ def test_encode_error(self):
+ for data, error_handler, expected in (
+ ('[\x80\xff\u20ac]', 'ignore', b'[]'),
+ ('[\x80\xff\u20ac]', 'replace', b'[???]'),
+ ('[\x80\xff\u20ac]', 'xmlcharrefreplace', b'[&#128;&#255;&#8364;]'),
+ ('[\x80\xff\u20ac\U000abcde]', 'backslashreplace',
+ b'[\\x80\\xff\\u20ac\\U000abcde]'),
+ ('[\udc80\udcff]', 'surrogateescape', b'[\x80\xff]'),
+ ):
+ with self.subTest(data=data, error_handler=error_handler,
+ expected=expected):
+ self.assertEqual(data.encode('ascii', error_handler),
+ expected)
+
+ def test_encode_surrogateescape_error(self):
+ with self.assertRaises(UnicodeEncodeError):
+ # the first character can be decoded, but not the second
+ '\udc80\xff'.encode('ascii', 'surrogateescape')
+
+ def test_decode(self):
+ self.assertEqual(b'abc'.decode('ascii'), 'abc')
+
+ def test_decode_error(self):
+ for data, error_handler, expected in (
+ (b'[\x80\xff]', 'ignore', '[]'),
+ (b'[\x80\xff]', 'replace', '[\ufffd\ufffd]'),
+ (b'[\x80\xff]', 'surrogateescape', '[\udc80\udcff]'),
+ (b'[\x80\xff]', 'backslashreplace', '[\\x80\\xff]'),
+ ):
+ with self.subTest(data=data, error_handler=error_handler,
+ expected=expected):
+ self.assertEqual(data.decode('ascii', error_handler),
+ expected)
+
+
+class Latin1Test(unittest.TestCase):
+ def test_encode(self):
+ for data, expected in (
+ ('abc', b'abc'),
+ ('\x80\xe9\xff', b'\x80\xe9\xff'),
+ ):
+ with self.subTest(data=data, expected=expected):
+ self.assertEqual(data.encode('latin1'), expected)
+
+ def test_encode_errors(self):
+ for data, error_handler, expected in (
+ ('[\u20ac\udc80]', 'ignore', b'[]'),
+ ('[\u20ac\udc80]', 'replace', b'[??]'),
+ ('[\u20ac\U000abcde]', 'backslashreplace',
+ b'[\\u20ac\\U000abcde]'),
+ ('[\u20ac\udc80]', 'xmlcharrefreplace', b'[&#8364;&#56448;]'),
+ ('[\udc80\udcff]', 'surrogateescape', b'[\x80\xff]'),
+ ):
+ with self.subTest(data=data, error_handler=error_handler,
+ expected=expected):
+ self.assertEqual(data.encode('latin1', error_handler),
+ expected)
+
+ def test_encode_surrogateescape_error(self):
+ with self.assertRaises(UnicodeEncodeError):
+ # the first character can be decoded, but not the second
+ '\udc80\u20ac'.encode('latin1', 'surrogateescape')
+
+ def test_decode(self):
+ for data, expected in (
+ (b'abc', 'abc'),
+ (b'[\x80\xff]', '[\x80\xff]'),
+ ):
+ with self.subTest(data=data, expected=expected):
+ self.assertEqual(data.decode('latin1'), expected)
+
if __name__ == "__main__":
unittest.main()
diff --git a/Lib/test/test_codeop.py b/Lib/test/test_codeop.py
index 509bf5d..98da26f 100644
--- a/Lib/test/test_codeop.py
+++ b/Lib/test/test_codeop.py
@@ -282,7 +282,6 @@ class CodeopTests(unittest.TestCase):
ai("if (a == 1 and b = 2): pass")
ai("del 1")
- ai("del ()")
ai("del (1,)")
ai("del [1]")
ai("del '1'")
diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py
index 5238382..87454cc 100644
--- a/Lib/test/test_collections.py
+++ b/Lib/test/test_collections.py
@@ -19,11 +19,12 @@ from collections import namedtuple, Counter, OrderedDict, _count_elements
from collections import UserDict, UserString, UserList
from collections import ChainMap
from collections import deque
-from collections.abc import Awaitable, Coroutine, AsyncIterator, AsyncIterable
-from collections.abc import Hashable, Iterable, Iterator, Generator
-from collections.abc import Sized, Container, Callable
+from collections.abc import Awaitable, Coroutine
+from collections.abc import AsyncIterator, AsyncIterable, AsyncGenerator
+from collections.abc import Hashable, Iterable, Iterator, Generator, Reversible
+from collections.abc import Sized, Container, Callable, Collection
from collections.abc import Set, MutableSet
-from collections.abc import Mapping, MutableMapping, KeysView, ItemsView
+from collections.abc import Mapping, MutableMapping, KeysView, ItemsView, ValuesView
from collections.abc import Sequence, MutableSequence
from collections.abc import ByteString
@@ -242,6 +243,10 @@ class TestNamedTuple(unittest.TestCase):
]:
self.assertEqual(namedtuple('NT', spec, rename=True)._fields, renamed)
+ def test_module_parameter(self):
+ NT = namedtuple('NT', ['x', 'y'], module=collections)
+ self.assertEqual(NT.__module__, collections)
+
def test_instance(self):
Point = namedtuple('Point', 'x y')
p = Point(11, 22)
@@ -412,6 +417,18 @@ class TestNamedTuple(unittest.TestCase):
self.assertEqual(NTColor._fields, ('red', 'green', 'blue'))
globals().pop('NTColor', None) # clean-up after this test
+ def test_keyword_only_arguments(self):
+ # See issue 25628
+ with support.captured_stdout() as template:
+ NT = namedtuple('NT', ['x', 'y'], verbose=True)
+ self.assertIn('class NT', NT._source)
+ with self.assertRaises(TypeError):
+ NT = namedtuple('NT', ['x', 'y'], True)
+
+ NT = namedtuple('NT', ['abc', 'def'], rename=True)
+ self.assertEqual(NT._fields, ('abc', '_1'))
+ with self.assertRaises(TypeError):
+ NT = namedtuple('NT', ['abc', 'def'], False, True)
def test_namedtuple_subclass_issue_24931(self):
class Point(namedtuple('_Point', ['x', 'y'])):
@@ -487,6 +504,9 @@ class ABCTestCase(unittest.TestCase):
self.assertTrue(other.right_side,'Right side not called for %s.%s'
% (type(instance), name))
+def _test_gen():
+ yield
+
class TestOneTrickPonyABCs(ABCTestCase):
def test_Awaitable(self):
@@ -674,7 +694,7 @@ class TestOneTrickPonyABCs(ABCTestCase):
samples = [bytes(), str(),
tuple(), list(), set(), frozenset(), dict(),
dict().keys(), dict().items(), dict().values(),
- (lambda: (yield))(),
+ _test_gen(),
(x for x in []),
]
for x in samples:
@@ -688,6 +708,161 @@ class TestOneTrickPonyABCs(ABCTestCase):
self.assertFalse(issubclass(str, I))
self.validate_abstract_methods(Iterable, '__iter__')
self.validate_isinstance(Iterable, '__iter__')
+ # Check None blocking
+ class It:
+ def __iter__(self): return iter([])
+ class ItBlocked(It):
+ __iter__ = None
+ self.assertTrue(issubclass(It, Iterable))
+ self.assertTrue(isinstance(It(), Iterable))
+ self.assertFalse(issubclass(ItBlocked, Iterable))
+ self.assertFalse(isinstance(ItBlocked(), Iterable))
+
+ def test_Reversible(self):
+ # Check some non-reversibles
+ non_samples = [None, 42, 3.14, 1j, dict(), set(), frozenset()]
+ for x in non_samples:
+ self.assertNotIsInstance(x, Reversible)
+ self.assertFalse(issubclass(type(x), Reversible), repr(type(x)))
+ # Check some non-reversible iterables
+ non_reversibles = [dict().keys(), dict().items(), dict().values(),
+ Counter(), Counter().keys(), Counter().items(),
+ Counter().values(), _test_gen(),
+ (x for x in []), iter([]), reversed([])]
+ for x in non_reversibles:
+ self.assertNotIsInstance(x, Reversible)
+ self.assertFalse(issubclass(type(x), Reversible), repr(type(x)))
+ # Check some reversible iterables
+ samples = [bytes(), str(), tuple(), list(), OrderedDict(),
+ OrderedDict().keys(), OrderedDict().items(),
+ OrderedDict().values()]
+ for x in samples:
+ self.assertIsInstance(x, Reversible)
+ self.assertTrue(issubclass(type(x), Reversible), repr(type(x)))
+ # Check also Mapping, MutableMapping, and Sequence
+ self.assertTrue(issubclass(Sequence, Reversible), repr(Sequence))
+ self.assertFalse(issubclass(Mapping, Reversible), repr(Mapping))
+ self.assertFalse(issubclass(MutableMapping, Reversible), repr(MutableMapping))
+ # Check direct subclassing
+ class R(Reversible):
+ def __iter__(self):
+ return iter(list())
+ def __reversed__(self):
+ return iter(list())
+ self.assertEqual(list(reversed(R())), [])
+ self.assertFalse(issubclass(float, R))
+ self.validate_abstract_methods(Reversible, '__reversed__', '__iter__')
+ # Check reversible non-iterable (which is not Reversible)
+ class RevNoIter:
+ def __reversed__(self): return reversed([])
+ class RevPlusIter(RevNoIter):
+ def __iter__(self): return iter([])
+ self.assertFalse(issubclass(RevNoIter, Reversible))
+ self.assertFalse(isinstance(RevNoIter(), Reversible))
+ self.assertTrue(issubclass(RevPlusIter, Reversible))
+ self.assertTrue(isinstance(RevPlusIter(), Reversible))
+ # Check None blocking
+ class Rev:
+ def __iter__(self): return iter([])
+ def __reversed__(self): return reversed([])
+ class RevItBlocked(Rev):
+ __iter__ = None
+ class RevRevBlocked(Rev):
+ __reversed__ = None
+ self.assertTrue(issubclass(Rev, Reversible))
+ self.assertTrue(isinstance(Rev(), Reversible))
+ self.assertFalse(issubclass(RevItBlocked, Reversible))
+ self.assertFalse(isinstance(RevItBlocked(), Reversible))
+ self.assertFalse(issubclass(RevRevBlocked, Reversible))
+ self.assertFalse(isinstance(RevRevBlocked(), Reversible))
+
+ def test_Collection(self):
+ # Check some non-collections
+ non_collections = [None, 42, 3.14, 1j, lambda x: 2*x]
+ for x in non_collections:
+ self.assertNotIsInstance(x, Collection)
+ self.assertFalse(issubclass(type(x), Collection), repr(type(x)))
+ # Check some non-collection iterables
+ non_col_iterables = [_test_gen(), iter(b''), iter(bytearray()),
+ (x for x in []), dict().values()]
+ for x in non_col_iterables:
+ self.assertNotIsInstance(x, Collection)
+ self.assertFalse(issubclass(type(x), Collection), repr(type(x)))
+ # Check some collections
+ samples = [set(), frozenset(), dict(), bytes(), str(), tuple(),
+ list(), dict().keys(), dict().items()]
+ for x in samples:
+ self.assertIsInstance(x, Collection)
+ self.assertTrue(issubclass(type(x), Collection), repr(type(x)))
+ # Check also Mapping, MutableMapping, etc.
+ self.assertTrue(issubclass(Sequence, Collection), repr(Sequence))
+ self.assertTrue(issubclass(Mapping, Collection), repr(Mapping))
+ self.assertTrue(issubclass(MutableMapping, Collection),
+ repr(MutableMapping))
+ self.assertTrue(issubclass(Set, Collection), repr(Set))
+ self.assertTrue(issubclass(MutableSet, Collection), repr(MutableSet))
+ self.assertTrue(issubclass(Sequence, Collection), repr(MutableSet))
+ # Check direct subclassing
+ class Col(Collection):
+ def __iter__(self):
+ return iter(list())
+ def __len__(self):
+ return 0
+ def __contains__(self, item):
+ return False
+ class DerCol(Col): pass
+ self.assertEqual(list(iter(Col())), [])
+ self.assertFalse(issubclass(list, Col))
+ self.assertFalse(issubclass(set, Col))
+ self.assertFalse(issubclass(float, Col))
+ self.assertEqual(list(iter(DerCol())), [])
+ self.assertFalse(issubclass(list, DerCol))
+ self.assertFalse(issubclass(set, DerCol))
+ self.assertFalse(issubclass(float, DerCol))
+ self.validate_abstract_methods(Collection, '__len__', '__iter__',
+ '__contains__')
+ # Check sized container non-iterable (which is not Collection) etc.
+ class ColNoIter:
+ def __len__(self): return 0
+ def __contains__(self, item): return False
+ class ColNoSize:
+ def __iter__(self): return iter([])
+ def __contains__(self, item): return False
+ class ColNoCont:
+ def __iter__(self): return iter([])
+ def __len__(self): return 0
+ self.assertFalse(issubclass(ColNoIter, Collection))
+ self.assertFalse(isinstance(ColNoIter(), Collection))
+ self.assertFalse(issubclass(ColNoSize, Collection))
+ self.assertFalse(isinstance(ColNoSize(), Collection))
+ self.assertFalse(issubclass(ColNoCont, Collection))
+ self.assertFalse(isinstance(ColNoCont(), Collection))
+ # Check None blocking
+ class SizeBlock:
+ def __iter__(self): return iter([])
+ def __contains__(self): return False
+ __len__ = None
+ class IterBlock:
+ def __len__(self): return 0
+ def __contains__(self): return True
+ __iter__ = None
+ self.assertFalse(issubclass(SizeBlock, Collection))
+ self.assertFalse(isinstance(SizeBlock(), Collection))
+ self.assertFalse(issubclass(IterBlock, Collection))
+ self.assertFalse(isinstance(IterBlock(), Collection))
+ # Check None blocking in subclass
+ class ColImpl:
+ def __iter__(self):
+ return iter(list())
+ def __len__(self):
+ return 0
+ def __contains__(self, item):
+ return False
+ class NonCol(ColImpl):
+ __contains__ = None
+ self.assertFalse(issubclass(NonCol, Collection))
+ self.assertFalse(isinstance(NonCol(), Collection))
+
def test_Iterator(self):
non_samples = [None, 42, 3.14, 1j, b"", "", (), [], {}, set()]
@@ -699,7 +874,7 @@ class TestOneTrickPonyABCs(ABCTestCase):
iter(set()), iter(frozenset()),
iter(dict().keys()), iter(dict().items()),
iter(dict().values()),
- (lambda: (yield))(),
+ _test_gen(),
(x for x in []),
]
for x in samples:
@@ -785,9 +960,90 @@ class TestOneTrickPonyABCs(ABCTestCase):
self.assertRaises(RuntimeError, IgnoreGeneratorExit().close)
+ def test_AsyncGenerator(self):
+ class NonAGen1:
+ def __aiter__(self): return self
+ def __anext__(self): return None
+ def aclose(self): pass
+ def athrow(self, typ, val=None, tb=None): pass
+
+ class NonAGen2:
+ def __aiter__(self): return self
+ def __anext__(self): return None
+ def aclose(self): pass
+ def asend(self, value): return value
+
+ class NonAGen3:
+ def aclose(self): pass
+ def asend(self, value): return value
+ def athrow(self, typ, val=None, tb=None): pass
+
+ non_samples = [
+ None, 42, 3.14, 1j, b"", "", (), [], {}, set(),
+ iter(()), iter([]), NonAGen1(), NonAGen2(), NonAGen3()]
+ for x in non_samples:
+ self.assertNotIsInstance(x, AsyncGenerator)
+ self.assertFalse(issubclass(type(x), AsyncGenerator), repr(type(x)))
+
+ class Gen:
+ def __aiter__(self): return self
+ async def __anext__(self): return None
+ async def aclose(self): pass
+ async def asend(self, value): return value
+ async def athrow(self, typ, val=None, tb=None): pass
+
+ class MinimalAGen(AsyncGenerator):
+ async def asend(self, value):
+ return value
+ async def athrow(self, typ, val=None, tb=None):
+ await super().athrow(typ, val, tb)
+
+ async def gen():
+ yield 1
+
+ samples = [gen(), Gen(), MinimalAGen()]
+ for x in samples:
+ self.assertIsInstance(x, AsyncIterator)
+ self.assertIsInstance(x, AsyncGenerator)
+ self.assertTrue(issubclass(type(x), AsyncGenerator), repr(type(x)))
+ self.validate_abstract_methods(AsyncGenerator, 'asend', 'athrow')
+
+ def run_async(coro):
+ result = None
+ while True:
+ try:
+ coro.send(None)
+ except StopIteration as ex:
+ result = ex.args[0] if ex.args else None
+ break
+ return result
+
+ # mixin tests
+ mgen = MinimalAGen()
+ self.assertIs(mgen, mgen.__aiter__())
+ self.assertIs(run_async(mgen.asend(None)), run_async(mgen.__anext__()))
+ self.assertEqual(2, run_async(mgen.asend(2)))
+ self.assertIsNone(run_async(mgen.aclose()))
+ with self.assertRaises(ValueError):
+ run_async(mgen.athrow(ValueError))
+
+ class FailOnClose(AsyncGenerator):
+ async def asend(self, value): return value
+ async def athrow(self, *args): raise ValueError
+
+ with self.assertRaises(ValueError):
+ run_async(FailOnClose().aclose())
+
+ class IgnoreGeneratorExit(AsyncGenerator):
+ async def asend(self, value): return value
+ async def athrow(self, *args): pass
+
+ with self.assertRaises(RuntimeError):
+ run_async(IgnoreGeneratorExit().aclose())
+
def test_Sized(self):
non_samples = [None, 42, 3.14, 1j,
- (lambda: (yield))(),
+ _test_gen(),
(x for x in []),
]
for x in non_samples:
@@ -805,7 +1061,7 @@ class TestOneTrickPonyABCs(ABCTestCase):
def test_Container(self):
non_samples = [None, 42, 3.14, 1j,
- (lambda: (yield))(),
+ _test_gen(),
(x for x in []),
]
for x in non_samples:
@@ -824,7 +1080,7 @@ class TestOneTrickPonyABCs(ABCTestCase):
def test_Callable(self):
non_samples = [None, 42, 3.14, 1j,
"", b"", (), [], {}, set(),
- (lambda: (yield))(),
+ _test_gen(),
(x for x in []),
]
for x in non_samples:
@@ -842,14 +1098,14 @@ class TestOneTrickPonyABCs(ABCTestCase):
self.validate_isinstance(Callable, '__call__')
def test_direct_subclassing(self):
- for B in Hashable, Iterable, Iterator, Sized, Container, Callable:
+ for B in Hashable, Iterable, Iterator, Reversible, Sized, Container, Callable:
class C(B):
pass
self.assertTrue(issubclass(C, B))
self.assertFalse(issubclass(int, C))
def test_registration(self):
- for B in Hashable, Iterable, Iterator, Sized, Container, Callable:
+ for B in Hashable, Iterable, Iterator, Reversible, Sized, Container, Callable:
class C:
__hash__ = None # Make sure it isn't hashable by default
self.assertFalse(issubclass(C, B), B.__name__)
@@ -1049,6 +1305,26 @@ class TestCollectionABCs(ABCTestCase):
self.assertFalse(ncs > cs)
self.assertTrue(ncs >= cs)
+ def test_issue26915(self):
+ # Container membership test should check identity first
+ class CustomEqualObject:
+ def __eq__(self, other):
+ return False
+ class CustomSequence(list):
+ def __contains__(self, value):
+ return Sequence.__contains__(self, value)
+
+ nan = float('nan')
+ obj = CustomEqualObject()
+ containers = [
+ CustomSequence([nan, obj]),
+ ItemsView({1: nan, 2: obj}),
+ ValuesView({1: nan, 2: obj})
+ ]
+ for container in containers:
+ for elem in container:
+ self.assertIn(elem, container)
+
def assertSameSet(self, s1, s2):
# coerce both to a real set then check equality
self.assertSetEqual(set(s1), set(s2))
@@ -1219,6 +1495,7 @@ class TestCollectionABCs(ABCTestCase):
def __iter__(self):
return iter(())
self.validate_comparison(MyMapping())
+ self.assertRaises(TypeError, reversed, MyMapping())
def test_MutableMapping(self):
for sample in [dict]:
diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py
index 824e843..409ec86 100644
--- a/Lib/test/test_compile.py
+++ b/Lib/test/test_compile.py
@@ -473,10 +473,13 @@ if 1:
self.assertEqual(d, {1: 2, 3: 4})
def test_compile_filename(self):
- for filename in ('file.py', b'file.py',
- bytearray(b'file.py'), memoryview(b'file.py')):
+ for filename in 'file.py', b'file.py':
code = compile('pass', filename, 'exec')
self.assertEqual(code.co_filename, 'file.py')
+ for filename in bytearray(b'file.py'), memoryview(b'file.py'):
+ with self.assertWarns(DeprecationWarning):
+ code = compile('pass', filename, 'exec')
+ self.assertEqual(code.co_filename, 'file.py')
self.assertRaises(TypeError, compile, 'pass', list(b'file.py'), 'exec')
@support.cpython_only
@@ -661,6 +664,16 @@ if 1:
self.assertTrue(f1(0))
self.assertTrue(f2(0.0))
+ def test_path_like_objects(self):
+ # An implicit test for PyUnicode_FSDecoder().
+ class PathLike:
+ def __init__(self, path):
+ self._path = path
+ def __fspath__(self):
+ return self._path
+
+ compile("42", PathLike("test_compile_pathlike"), "single")
+
class TestStackSize(unittest.TestCase):
# These tests check that the computed stack size for a code object
diff --git a/Lib/test/test_compileall.py b/Lib/test/test_compileall.py
index 2ce8a61..2356efc 100644
--- a/Lib/test/test_compileall.py
+++ b/Lib/test/test_compileall.py
@@ -1,6 +1,7 @@
import sys
import compileall
import importlib.util
+import test.test_importlib.util
import os
import pathlib
import py_compile
@@ -40,6 +41,11 @@ class CompileallTests(unittest.TestCase):
def tearDown(self):
shutil.rmtree(self.directory)
+ def add_bad_source_file(self):
+ self.bad_source_path = os.path.join(self.directory, '_test_bad.py')
+ with open(self.bad_source_path, 'w') as file:
+ file.write('x (\n')
+
def data(self):
with open(self.bc_path, 'rb') as file:
data = file.read(8)
@@ -78,15 +84,47 @@ class CompileallTests(unittest.TestCase):
os.unlink(fn)
except:
pass
- compileall.compile_file(self.source_path, force=False, quiet=True)
+ self.assertTrue(compileall.compile_file(self.source_path,
+ force=False, quiet=True))
self.assertTrue(os.path.isfile(self.bc_path) and
not os.path.isfile(self.bc_path2))
os.unlink(self.bc_path)
- compileall.compile_dir(self.directory, force=False, quiet=True)
+ self.assertTrue(compileall.compile_dir(self.directory, force=False,
+ quiet=True))
self.assertTrue(os.path.isfile(self.bc_path) and
os.path.isfile(self.bc_path2))
os.unlink(self.bc_path)
os.unlink(self.bc_path2)
+ # Test against bad files
+ self.add_bad_source_file()
+ self.assertFalse(compileall.compile_file(self.bad_source_path,
+ force=False, quiet=2))
+ self.assertFalse(compileall.compile_dir(self.directory,
+ force=False, quiet=2))
+
+ def test_compile_file_pathlike(self):
+ self.assertFalse(os.path.isfile(self.bc_path))
+ # we should also test the output
+ with support.captured_stdout() as stdout:
+ self.assertTrue(compileall.compile_file(pathlib.Path(self.source_path)))
+ self.assertRegex(stdout.getvalue(), r'Compiling ([^WindowsPath|PosixPath].*)')
+ self.assertTrue(os.path.isfile(self.bc_path))
+
+ def test_compile_file_pathlike_ddir(self):
+ self.assertFalse(os.path.isfile(self.bc_path))
+ self.assertTrue(compileall.compile_file(pathlib.Path(self.source_path),
+ ddir=pathlib.Path('ddir_path'),
+ quiet=2))
+ self.assertTrue(os.path.isfile(self.bc_path))
+
+ def test_compile_path(self):
+ with test.test_importlib.util.import_state(path=[self.directory]):
+ self.assertTrue(compileall.compile_path(quiet=2))
+
+ with test.test_importlib.util.import_state(path=[self.directory]):
+ self.add_bad_source_file()
+ self.assertFalse(compileall.compile_path(skip_curdir=False,
+ force=True, quiet=2))
def test_no_pycache_in_non_package(self):
# Bug 8563 reported that __pycache__ directories got created by
@@ -115,6 +153,14 @@ class CompileallTests(unittest.TestCase):
optimization=opt)
self.assertTrue(os.path.isfile(cached3))
+ def test_compile_dir_pathlike(self):
+ self.assertFalse(os.path.isfile(self.bc_path))
+ with support.captured_stdout() as stdout:
+ compileall.compile_dir(pathlib.Path(self.directory))
+ line = stdout.getvalue().splitlines()[0]
+ self.assertRegex(line, r'Listing ([^WindowsPath|PosixPath].*)')
+ self.assertTrue(os.path.isfile(self.bc_path))
+
@mock.patch('compileall.ProcessPoolExecutor')
def test_compile_pool_called(self, pool_mock):
compileall.compile_dir(self.directory, quiet=True, workers=5)
@@ -197,10 +243,9 @@ class CommandLineTests(unittest.TestCase):
raise unittest.SkipTest('not all entries on sys.path are writable')
def _get_run_args(self, args):
- interp_args = ['-S']
- if sys.flags.optimize:
- interp_args.append({1 : '-O', 2 : '-OO'}[sys.flags.optimize])
- return interp_args + ['-m', 'compileall'] + list(args)
+ return [*support.optim_args_from_interpreter_flags(),
+ '-S', '-m', 'compileall',
+ *args]
def assertRunOK(self, *args, **env_vars):
rc, out, err = script_helper.assert_python_ok(
diff --git a/Lib/test/test_complex.py b/Lib/test/test_complex.py
index 403ee3b..c249ca7 100644
--- a/Lib/test/test_complex.py
+++ b/Lib/test/test_complex.py
@@ -1,5 +1,7 @@
import unittest
from test import support
+from test.test_grammar import (VALID_UNDERSCORE_LITERALS,
+ INVALID_UNDERSCORE_LITERALS)
from random import random
from math import atan2, isnan, copysign
@@ -385,6 +387,18 @@ class ComplexTest(unittest.TestCase):
self.assertAlmostEqual(complex(complex1(1j)), 2j)
self.assertRaises(TypeError, complex, complex2(1j))
+ def test_underscores(self):
+ # check underscores
+ for lit in VALID_UNDERSCORE_LITERALS:
+ if not any(ch in lit for ch in 'xXoObB'):
+ self.assertEqual(complex(lit), eval(lit))
+ self.assertEqual(complex(lit), complex(lit.replace('_', '')))
+ for lit in INVALID_UNDERSCORE_LITERALS:
+ if lit in ('0_7', '09_99'): # octals are not recognized here
+ continue
+ if not any(ch in lit for ch in 'xXoObB'):
+ self.assertRaises(ValueError, complex, lit)
+
def test_hash(self):
for x in range(-30, 30):
self.assertEqual(hash(x), hash(complex(x, 0)))
diff --git a/Lib/test/test_concurrent_futures.py b/Lib/test/test_concurrent_futures.py
index 7513815..23e95b2 100644
--- a/Lib/test/test_concurrent_futures.py
+++ b/Lib/test/test_concurrent_futures.py
@@ -154,6 +154,30 @@ class ThreadPoolShutdownTest(ThreadPoolMixin, ExecutorShutdownTest, unittest.Tes
for t in threads:
t.join()
+ def test_thread_names_assigned(self):
+ executor = futures.ThreadPoolExecutor(
+ max_workers=5, thread_name_prefix='SpecialPool')
+ executor.map(abs, range(-5, 5))
+ threads = executor._threads
+ del executor
+
+ for t in threads:
+ self.assertRegex(t.name, r'^SpecialPool_[0-4]$')
+ t.join()
+
+ def test_thread_names_default(self):
+ executor = futures.ThreadPoolExecutor(max_workers=5)
+ executor.map(abs, range(-5, 5))
+ threads = executor._threads
+ del executor
+
+ for t in threads:
+ # We don't particularly care what the default name is, just that
+ # it has a default name implying that it is a ThreadPoolExecutor
+ # followed by what looks like a thread number.
+ self.assertRegex(t.name, r'^.*ThreadPoolExecutor.*_[0-4]$')
+ t.join()
+
class ProcessPoolShutdownTest(ProcessPoolMixin, ExecutorShutdownTest, unittest.TestCase):
def _prime_executor(self):
diff --git a/Lib/test/test_configparser.py b/Lib/test/test_configparser.py
index 864a1d0..0d06080 100644
--- a/Lib/test/test_configparser.py
+++ b/Lib/test/test_configparser.py
@@ -2076,5 +2076,11 @@ class BlatantOverrideConvertersTestCase(unittest.TestCase):
self.assertEqual(cfg['two'].getlen('one'), 5)
+class MiscTestCase(unittest.TestCase):
+ def test__all__(self):
+ blacklist = {"Error"}
+ support.check__all__(self, configparser, blacklist=blacklist)
+
+
if __name__ == '__main__':
unittest.main()
diff --git a/Lib/test/test_contains.py b/Lib/test/test_contains.py
index 3c6bdef..036a1d0 100644
--- a/Lib/test/test_contains.py
+++ b/Lib/test/test_contains.py
@@ -84,6 +84,31 @@ class TestContains(unittest.TestCase):
self.assertTrue(container == constructor(values))
self.assertTrue(container == container)
+ def test_block_fallback(self):
+ # blocking fallback with __contains__ = None
+ class ByContains(object):
+ def __contains__(self, other):
+ return False
+ c = ByContains()
+ class BlockContains(ByContains):
+ """Is not a container
+
+ This class is a perfectly good iterable (as tested by
+ list(bc)), as well as inheriting from a perfectly good
+ container, but __contains__ = None prevents the usual
+ fallback to iteration in the container protocol. That
+ is, normally, 0 in bc would fall back to the equivalent
+ of any(x==0 for x in bc), but here it's blocked from
+ doing so.
+ """
+ def __iter__(self):
+ while False:
+ yield None
+ __contains__ = None
+ bc = BlockContains()
+ self.assertFalse(0 in c)
+ self.assertFalse(0 in list(bc))
+ self.assertRaises(TypeError, lambda: 0 in bc)
if __name__ == '__main__':
unittest.main()
diff --git a/Lib/test/test_contextlib.py b/Lib/test/test_contextlib.py
index 516403e..c04c804 100644
--- a/Lib/test/test_contextlib.py
+++ b/Lib/test/test_contextlib.py
@@ -12,6 +12,39 @@ except ImportError:
threading = None
+class TestAbstractContextManager(unittest.TestCase):
+
+ def test_enter(self):
+ class DefaultEnter(AbstractContextManager):
+ def __exit__(self, *args):
+ super().__exit__(*args)
+
+ manager = DefaultEnter()
+ self.assertIs(manager.__enter__(), manager)
+
+ def test_exit_is_abstract(self):
+ class MissingExit(AbstractContextManager):
+ pass
+
+ with self.assertRaises(TypeError):
+ MissingExit()
+
+ def test_structural_subclassing(self):
+ class ManagerFromScratch:
+ def __enter__(self):
+ return self
+ def __exit__(self, exc_type, exc_value, traceback):
+ return None
+
+ self.assertTrue(issubclass(ManagerFromScratch, AbstractContextManager))
+
+ class DefaultEnter(AbstractContextManager):
+ def __exit__(self, *args):
+ super().__exit__(*args)
+
+ self.assertTrue(issubclass(DefaultEnter, AbstractContextManager))
+
+
class ContextManagerTestCase(unittest.TestCase):
def test_contextmanager_plain(self):
@@ -89,7 +122,7 @@ class ContextManagerTestCase(unittest.TestCase):
def woohoo():
yield
try:
- with self.assertWarnsRegex(PendingDeprecationWarning,
+ with self.assertWarnsRegex(DeprecationWarning,
"StopIteration"):
with woohoo():
raise stop_exc
diff --git a/Lib/test/test_copy.py b/Lib/test/test_copy.py
index 0e1f670..45a6920 100644
--- a/Lib/test/test_copy.py
+++ b/Lib/test/test_copy.py
@@ -98,7 +98,7 @@ class TestCopy(unittest.TestCase):
tests = [None, ..., NotImplemented,
42, 2**100, 3.14, True, False, 1j,
"hello", "hello\u1234", f.__code__,
- b"world", bytes(range(256)), range(10),
+ b"world", bytes(range(256)), range(10), slice(1, 10, 2),
NewStyle, Classic, max, WithMetaclass]
for x in tests:
self.assertIs(copy.copy(x), x)
diff --git a/Lib/test/test_coroutines.py b/Lib/test/test_coroutines.py
index 4a327b5..78439a2 100644
--- a/Lib/test/test_coroutines.py
+++ b/Lib/test/test_coroutines.py
@@ -69,55 +69,130 @@ def silence_coro_gc():
class AsyncBadSyntaxTest(unittest.TestCase):
def test_badsyntax_1(self):
- with self.assertRaisesRegex(SyntaxError, "'await' outside"):
- import test.badsyntax_async1
+ samples = [
+ """def foo():
+ await something()
+ """,
- def test_badsyntax_2(self):
- with self.assertRaisesRegex(SyntaxError, "'await' outside"):
- import test.badsyntax_async2
+ """await something()""",
- def test_badsyntax_3(self):
- with self.assertRaisesRegex(SyntaxError, 'invalid syntax'):
- import test.badsyntax_async3
+ """async def foo():
+ yield from []
+ """,
- def test_badsyntax_4(self):
- with self.assertRaisesRegex(SyntaxError, 'invalid syntax'):
- import test.badsyntax_async4
+ """async def foo():
+ await await fut
+ """,
- def test_badsyntax_5(self):
- with self.assertRaisesRegex(SyntaxError, 'invalid syntax'):
- import test.badsyntax_async5
+ """async def foo(a=await something()):
+ pass
+ """,
- def test_badsyntax_6(self):
- with self.assertRaisesRegex(
- SyntaxError, "'yield' inside async function"):
+ """async def foo(a:await something()):
+ pass
+ """,
- import test.badsyntax_async6
+ """async def foo():
+ def bar():
+ [i async for i in els]
+ """,
- def test_badsyntax_7(self):
- with self.assertRaisesRegex(
- SyntaxError, "'yield from' inside async function"):
+ """async def foo():
+ def bar():
+ [await i for i in els]
+ """,
- import test.badsyntax_async7
+ """async def foo():
+ def bar():
+ [i for i in els
+ async for b in els]
+ """,
- def test_badsyntax_8(self):
- with self.assertRaisesRegex(SyntaxError, 'invalid syntax'):
- import test.badsyntax_async8
+ """async def foo():
+ def bar():
+ [i for i in els
+ for c in b
+ async for b in els]
+ """,
- def test_badsyntax_9(self):
- ns = {}
- for comp in {'(await a for a in b)',
- '[await a for a in b]',
- '{await a for a in b}',
- '{await a: c for a in b}'}:
+ """async def foo():
+ def bar():
+ [i for i in els
+ async for b in els
+ for c in b]
+ """,
- with self.assertRaisesRegex(SyntaxError, 'await.*in comprehen'):
- exec('async def f():\n\t{}'.format(comp), ns, ns)
+ """async def foo():
+ def bar():
+ [i for i in els
+ for b in await els]
+ """,
- def test_badsyntax_10(self):
- # Tests for issue 24619
+ """async def foo():
+ def bar():
+ [i for i in els
+ for b in els
+ if await b]
+ """,
+
+ """async def foo():
+ def bar():
+ [i for i in await els]
+ """,
+
+ """async def foo():
+ def bar():
+ [i for i in els if await i]
+ """,
+
+ """def bar():
+ [i async for i in els]
+ """,
+
+ """def bar():
+ [await i for i in els]
+ """,
+
+ """def bar():
+ [i for i in els
+ async for b in els]
+ """,
+
+ """def bar():
+ [i for i in els
+ for c in b
+ async for b in els]
+ """,
+
+ """def bar():
+ [i for i in els
+ async for b in els
+ for c in b]
+ """,
+
+ """def bar():
+ [i for i in els
+ for b in await els]
+ """,
+
+ """def bar():
+ [i for i in els
+ for b in els
+ if await b]
+ """,
+
+ """def bar():
+ [i for i in await els]
+ """,
+
+ """def bar():
+ [i for i in els if await i]
+ """,
+
+ """async def foo():
+ await
+ """,
- samples = [
"""async def foo():
def bar(): pass
await = 1
@@ -283,57 +358,110 @@ class AsyncBadSyntaxTest(unittest.TestCase):
with self.subTest(code=code), self.assertRaises(SyntaxError):
compile(code, "<test>", "exec")
- def test_goodsyntax_1(self):
- # Tests for issue 24619
+ def test_badsyntax_2(self):
+ samples = [
+ """def foo():
+ await = 1
+ """,
+
+ """class Bar:
+ def async(): pass
+ """,
- def foo(await):
- async def foo(): pass
- async def foo():
+ """class Bar:
+ async = 1
+ """,
+
+ """class async:
pass
- return await + 1
- self.assertEqual(foo(10), 11)
+ """,
- def foo(await):
- async def foo(): pass
- async def foo(): pass
- return await + 2
- self.assertEqual(foo(20), 22)
+ """class await:
+ pass
+ """,
- def foo(await):
+ """import math as await""",
- async def foo(): pass
+ """def async():
+ pass""",
- async def foo(): pass
+ """def foo(*, await=1):
+ pass"""
- return await + 2
- self.assertEqual(foo(20), 22)
+ """async = 1""",
- def foo(await):
- """spam"""
- async def foo(): \
- pass
- # 123
- async def foo(): pass
- # 456
- return await + 2
- self.assertEqual(foo(20), 22)
-
- def foo(await):
- def foo(): pass
- def foo(): pass
- async def bar(): return await_
- await_ = await
- try:
- bar().send(None)
- except StopIteration as ex:
- return ex.args[0]
- self.assertEqual(foo(42), 42)
+ """print(await=1)"""
+ ]
- async def f():
- async def g(): pass
- await z
- await = 1
- self.assertTrue(inspect.iscoroutinefunction(f))
+ for code in samples:
+ with self.subTest(code=code), self.assertWarnsRegex(
+ DeprecationWarning,
+ "'await' will become reserved keywords"):
+ compile(code, "<test>", "exec")
+
+ def test_badsyntax_3(self):
+ with self.assertRaises(DeprecationWarning):
+ with warnings.catch_warnings():
+ warnings.simplefilter("error")
+ compile("async = 1", "<test>", "exec")
+
+ def test_goodsyntax_1(self):
+ # Tests for issue 24619
+
+ samples = [
+ '''def foo(await):
+ async def foo(): pass
+ async def foo():
+ pass
+ return await + 1
+ ''',
+
+ '''def foo(await):
+ async def foo(): pass
+ async def foo(): pass
+ return await + 1
+ ''',
+
+ '''def foo(await):
+
+ async def foo(): pass
+
+ async def foo(): pass
+
+ return await + 1
+ ''',
+
+ '''def foo(await):
+ """spam"""
+ async def foo(): \
+ pass
+ # 123
+ async def foo(): pass
+ # 456
+ return await + 1
+ ''',
+
+ '''def foo(await):
+ def foo(): pass
+ def foo(): pass
+ async def bar(): return await_
+ await_ = await
+ try:
+ bar().send(None)
+ except StopIteration as ex:
+ return ex.args[0] + 1
+ '''
+ ]
+
+ for code in samples:
+ with self.subTest(code=code):
+ loc = {}
+
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore")
+ exec(code, loc, loc)
+
+ self.assertEqual(loc['foo'](10), 11)
class TokenizerRegrTest(unittest.TestCase):
@@ -906,7 +1034,7 @@ class CoroutineTest(unittest.TestCase):
return await Awaitable()
with self.assertRaisesRegex(
- TypeError, "__await__\(\) returned a coroutine"):
+ TypeError, r"__await__\(\) returned a coroutine"):
run_async(foo())
@@ -1270,7 +1398,7 @@ class CoroutineTest(unittest.TestCase):
buffer = []
async def test1():
- with self.assertWarnsRegex(PendingDeprecationWarning, "legacy"):
+ with self.assertWarnsRegex(DeprecationWarning, "legacy"):
async for i1, i2 in AsyncIter():
buffer.append(i1 + i2)
@@ -1284,7 +1412,7 @@ class CoroutineTest(unittest.TestCase):
buffer = []
async def test2():
nonlocal buffer
- with self.assertWarnsRegex(PendingDeprecationWarning, "legacy"):
+ with self.assertWarnsRegex(DeprecationWarning, "legacy"):
async for i in AsyncIter():
buffer.append(i[0])
if i[0] == 20:
@@ -1303,7 +1431,7 @@ class CoroutineTest(unittest.TestCase):
buffer = []
async def test3():
nonlocal buffer
- with self.assertWarnsRegex(PendingDeprecationWarning, "legacy"):
+ with self.assertWarnsRegex(DeprecationWarning, "legacy"):
async for i in AsyncIter():
if i[0] > 20:
continue
@@ -1348,7 +1476,7 @@ class CoroutineTest(unittest.TestCase):
with self.assertRaisesRegex(
TypeError,
- "async for' received an invalid object.*__aiter.*\: I"):
+ r"async for' received an invalid object.*__aiter.*\: I"):
run_async(foo())
@@ -1386,7 +1514,7 @@ class CoroutineTest(unittest.TestCase):
return 123
async def foo():
- with self.assertWarnsRegex(PendingDeprecationWarning, "legacy"):
+ with self.assertWarnsRegex(DeprecationWarning, "legacy"):
async for i in I():
print('never going to happen')
@@ -1495,7 +1623,7 @@ class CoroutineTest(unittest.TestCase):
1/0
async def foo():
nonlocal CNT
- with self.assertWarnsRegex(PendingDeprecationWarning, "legacy"):
+ with self.assertWarnsRegex(DeprecationWarning, "legacy"):
async for i in AI():
CNT += 1
CNT += 10
@@ -1522,7 +1650,7 @@ class CoroutineTest(unittest.TestCase):
self.assertEqual(CNT, 0)
def test_for_9(self):
- # Test that PendingDeprecationWarning can safely be converted into
+ # Test that DeprecationWarning can safely be converted into
# an exception (__aiter__ should not have a chance to raise
# a ZeroDivisionError.)
class AI:
@@ -1532,13 +1660,13 @@ class CoroutineTest(unittest.TestCase):
async for i in AI():
pass
- with self.assertRaises(PendingDeprecationWarning):
+ with self.assertRaises(DeprecationWarning):
with warnings.catch_warnings():
warnings.simplefilter("error")
run_async(foo())
def test_for_10(self):
- # Test that PendingDeprecationWarning can safely be converted into
+ # Test that DeprecationWarning can safely be converted into
# an exception.
class AI:
async def __aiter__(self):
@@ -1547,7 +1675,7 @@ class CoroutineTest(unittest.TestCase):
async for i in AI():
pass
- with self.assertRaises(PendingDeprecationWarning):
+ with self.assertRaises(DeprecationWarning):
with warnings.catch_warnings():
warnings.simplefilter("error")
run_async(foo())
@@ -1598,6 +1726,185 @@ class CoroutineTest(unittest.TestCase):
foo().send(None)
self.assertEqual(result, [42])
+ def test_comp_1(self):
+ async def f(i):
+ return i
+
+ async def run_list():
+ return [await c for c in [f(1), f(41)]]
+
+ async def run_set():
+ return {await c for c in [f(1), f(41)]}
+
+ async def run_dict1():
+ return {await c: 'a' for c in [f(1), f(41)]}
+
+ async def run_dict2():
+ return {i: await c for i, c in enumerate([f(1), f(41)])}
+
+ self.assertEqual(run_async(run_list()), ([], [1, 41]))
+ self.assertEqual(run_async(run_set()), ([], {1, 41}))
+ self.assertEqual(run_async(run_dict1()), ([], {1: 'a', 41: 'a'}))
+ self.assertEqual(run_async(run_dict2()), ([], {0: 1, 1: 41}))
+
+ def test_comp_2(self):
+ async def f(i):
+ return i
+
+ async def run_list():
+ return [s for c in [f(''), f('abc'), f(''), f(['de', 'fg'])]
+ for s in await c]
+
+ self.assertEqual(
+ run_async(run_list()),
+ ([], ['a', 'b', 'c', 'de', 'fg']))
+
+ async def run_set():
+ return {d
+ for c in [f([f([10, 30]),
+ f([20])])]
+ for s in await c
+ for d in await s}
+
+ self.assertEqual(
+ run_async(run_set()),
+ ([], {10, 20, 30}))
+
+ async def run_set2():
+ return {await s
+ for c in [f([f(10), f(20)])]
+ for s in await c}
+
+ self.assertEqual(
+ run_async(run_set2()),
+ ([], {10, 20}))
+
+ def test_comp_3(self):
+ async def f(it):
+ for i in it:
+ yield i
+
+ async def run_list():
+ return [i + 1 async for i in f([10, 20])]
+ self.assertEqual(
+ run_async(run_list()),
+ ([], [11, 21]))
+
+ async def run_set():
+ return {i + 1 async for i in f([10, 20])}
+ self.assertEqual(
+ run_async(run_set()),
+ ([], {11, 21}))
+
+ async def run_dict():
+ return {i + 1: i + 2 async for i in f([10, 20])}
+ self.assertEqual(
+ run_async(run_dict()),
+ ([], {11: 12, 21: 22}))
+
+ async def run_gen():
+ gen = (i + 1 async for i in f([10, 20]))
+ return [g + 100 async for g in gen]
+ self.assertEqual(
+ run_async(run_gen()),
+ ([], [111, 121]))
+
+ def test_comp_4(self):
+ async def f(it):
+ for i in it:
+ yield i
+
+ async def run_list():
+ return [i + 1 async for i in f([10, 20]) if i > 10]
+ self.assertEqual(
+ run_async(run_list()),
+ ([], [21]))
+
+ async def run_set():
+ return {i + 1 async for i in f([10, 20]) if i > 10}
+ self.assertEqual(
+ run_async(run_set()),
+ ([], {21}))
+
+ async def run_dict():
+ return {i + 1: i + 2 async for i in f([10, 20]) if i > 10}
+ self.assertEqual(
+ run_async(run_dict()),
+ ([], {21: 22}))
+
+ async def run_gen():
+ gen = (i + 1 async for i in f([10, 20]) if i > 10)
+ return [g + 100 async for g in gen]
+ self.assertEqual(
+ run_async(run_gen()),
+ ([], [121]))
+
+ def test_comp_5(self):
+ async def f(it):
+ for i in it:
+ yield i
+
+ async def run_list():
+ return [i + 1 for pair in ([10, 20], [30, 40]) if pair[0] > 10
+ async for i in f(pair) if i > 30]
+ self.assertEqual(
+ run_async(run_list()),
+ ([], [41]))
+
+ def test_comp_6(self):
+ async def f(it):
+ for i in it:
+ yield i
+
+ async def run_list():
+ return [i + 1 async for seq in f([(10, 20), (30,)])
+ for i in seq]
+
+ self.assertEqual(
+ run_async(run_list()),
+ ([], [11, 21, 31]))
+
+ def test_comp_7(self):
+ async def f():
+ yield 1
+ yield 2
+ raise Exception('aaa')
+
+ async def run_list():
+ return [i async for i in f()]
+
+ with self.assertRaisesRegex(Exception, 'aaa'):
+ run_async(run_list())
+
+ def test_comp_8(self):
+ async def f():
+ return [i for i in [1, 2, 3]]
+
+ self.assertEqual(
+ run_async(f()),
+ ([], [1, 2, 3]))
+
+ def test_comp_9(self):
+ async def gen():
+ yield 1
+ yield 2
+ async def f():
+ l = [i async for i in gen()]
+ return [i for i in l]
+
+ self.assertEqual(
+ run_async(f()),
+ ([], [1, 2]))
+
+ def test_comp_10(self):
+ async def f():
+ xx = {i for i in [1, 2, 3]}
+ return {x: x for x in xx}
+
+ self.assertEqual(
+ run_async(f()),
+ ([], {1: 1, 2: 2, 3: 3}))
+
def test_copy(self):
async def func(): pass
coro = func()
@@ -1728,8 +2035,8 @@ class SysSetCoroWrapperTest(unittest.TestCase):
try:
with silence_coro_gc(), self.assertRaisesRegex(
RuntimeError,
- "coroutine wrapper.*\.wrapper at 0x.*attempted to "
- "recursively wrap .* wrap .*"):
+ r"coroutine wrapper.*\.wrapper at 0x.*attempted to "
+ r"recursively wrap .* wrap .*"):
foo()
finally:
diff --git a/Lib/test/test_cprofile.py b/Lib/test/test_cprofile.py
index f18983f..53f8917 100644
--- a/Lib/test/test_cprofile.py
+++ b/Lib/test/test_cprofile.py
@@ -6,7 +6,7 @@ from test.support import run_unittest, TESTFN, unlink
# rip off all interesting stuff from test_profile
import cProfile
from test.test_profile import ProfileTest, regenerate_expected_output
-from test.profilee import testfunc
+
class CProfileTest(ProfileTest):
profilerclass = cProfile.Profile
diff --git a/Lib/test/test_csv.py b/Lib/test/test_csv.py
index 77c315e..03ab184 100644
--- a/Lib/test/test_csv.py
+++ b/Lib/test/test_csv.py
@@ -2,9 +2,7 @@
# csv package unit tests
import copy
-import io
import sys
-import os
import unittest
from io import StringIO
from tempfile import TemporaryFile
@@ -12,6 +10,9 @@ import csv
import gc
import pickle
from test import support
+from itertools import permutations
+from textwrap import dedent
+from collections import OrderedDict
class Test_Csv(unittest.TestCase):
"""
@@ -426,17 +427,16 @@ class TestDialectRegistry(unittest.TestCase):
self.assertRaises(TypeError, csv.reader, [], quoting = -1)
self.assertRaises(TypeError, csv.reader, [], quoting = 100)
- # See issue #22995
- ## def test_copy(self):
- ## for name in csv.list_dialects():
- ## dialect = csv.get_dialect(name)
- ## self.assertRaises(TypeError, copy.copy, dialect)
+ def test_copy(self):
+ for name in csv.list_dialects():
+ dialect = csv.get_dialect(name)
+ self.assertRaises(TypeError, copy.copy, dialect)
- ## def test_pickle(self):
- ## for name in csv.list_dialects():
- ## dialect = csv.get_dialect(name)
- ## for proto in range(pickle.HIGHEST_PROTOCOL + 1):
- ## self.assertRaises(TypeError, pickle.dumps, dialect, proto)
+ def test_pickle(self):
+ for name in csv.list_dialects():
+ dialect = csv.get_dialect(name)
+ for proto in range(pickle.HIGHEST_PROTOCOL + 1):
+ self.assertRaises(TypeError, pickle.dumps, dialect, proto)
class TestCsvBase(unittest.TestCase):
def readerAssertEqual(self, input, expected_result):
@@ -626,6 +626,24 @@ class TestDictFields(unittest.TestCase):
self.assertNotIn("'f2'", exception)
self.assertIn("1", exception)
+ def test_typo_in_extrasaction_raises_error(self):
+ fileobj = StringIO()
+ self.assertRaises(ValueError, csv.DictWriter, fileobj, ['f1', 'f2'],
+ extrasaction="raised")
+
+ def test_write_field_not_in_field_names_raise(self):
+ fileobj = StringIO()
+ writer = csv.DictWriter(fileobj, ['f1', 'f2'], extrasaction="raise")
+ dictrow = {'f0': 0, 'f1': 1, 'f2': 2, 'f3': 3}
+ self.assertRaises(ValueError, csv.DictWriter.writerow, writer, dictrow)
+
+ def test_write_field_not_in_field_names_ignore(self):
+ fileobj = StringIO()
+ writer = csv.DictWriter(fileobj, ['f1', 'f2'], extrasaction="ignore")
+ dictrow = {'f0': 0, 'f1': 1, 'f2': 2, 'f3': 3}
+ csv.DictWriter.writerow(writer, dictrow)
+ self.assertEqual(fileobj.getvalue(), "1,2\r\n")
+
def test_read_dict_fields(self):
with TemporaryFile("w+") as fileobj:
fileobj.write("1,2,abc\r\n")
@@ -1080,7 +1098,6 @@ class TestUnicode(unittest.TestCase):
"François Pinard"]
def test_unicode_read(self):
- import io
with TemporaryFile("w+", newline='', encoding="utf-8") as fileobj:
fileobj.write(",".join(self.names) + "\r\n")
fileobj.seek(0)
@@ -1089,7 +1106,6 @@ class TestUnicode(unittest.TestCase):
def test_unicode_write(self):
- import io
with TemporaryFile("w+", newline='', encoding="utf-8") as fileobj:
writer = csv.writer(fileobj)
writer.writerow(self.names)
@@ -1097,6 +1113,63 @@ class TestUnicode(unittest.TestCase):
fileobj.seek(0)
self.assertEqual(fileobj.read(), expected)
+class KeyOrderingTest(unittest.TestCase):
+
+ def test_ordering_for_the_dict_reader_and_writer(self):
+ resultset = set()
+ for keys in permutations("abcde"):
+ with TemporaryFile('w+', newline='', encoding="utf-8") as fileobject:
+ dw = csv.DictWriter(fileobject, keys)
+ dw.writeheader()
+ fileobject.seek(0)
+ dr = csv.DictReader(fileobject)
+ kt = tuple(dr.fieldnames)
+ self.assertEqual(keys, kt)
+ resultset.add(kt)
+ # Final sanity check: were all permutations unique?
+ self.assertEqual(len(resultset), 120, "Key ordering: some key permutations not collected (expected 120)")
+
+ def test_ordered_dict_reader(self):
+ data = dedent('''\
+ FirstName,LastName
+ Eric,Idle
+ Graham,Chapman,Over1,Over2
+
+ Under1
+ John,Cleese
+ ''').splitlines()
+
+ self.assertEqual(list(csv.DictReader(data)),
+ [OrderedDict([('FirstName', 'Eric'), ('LastName', 'Idle')]),
+ OrderedDict([('FirstName', 'Graham'), ('LastName', 'Chapman'),
+ (None, ['Over1', 'Over2'])]),
+ OrderedDict([('FirstName', 'Under1'), ('LastName', None)]),
+ OrderedDict([('FirstName', 'John'), ('LastName', 'Cleese')]),
+ ])
+
+ self.assertEqual(list(csv.DictReader(data, restkey='OtherInfo')),
+ [OrderedDict([('FirstName', 'Eric'), ('LastName', 'Idle')]),
+ OrderedDict([('FirstName', 'Graham'), ('LastName', 'Chapman'),
+ ('OtherInfo', ['Over1', 'Over2'])]),
+ OrderedDict([('FirstName', 'Under1'), ('LastName', None)]),
+ OrderedDict([('FirstName', 'John'), ('LastName', 'Cleese')]),
+ ])
+
+ del data[0] # Remove the header row
+ self.assertEqual(list(csv.DictReader(data, fieldnames=['fname', 'lname'])),
+ [OrderedDict([('fname', 'Eric'), ('lname', 'Idle')]),
+ OrderedDict([('fname', 'Graham'), ('lname', 'Chapman'),
+ (None, ['Over1', 'Over2'])]),
+ OrderedDict([('fname', 'Under1'), ('lname', None)]),
+ OrderedDict([('fname', 'John'), ('lname', 'Cleese')]),
+ ])
+
+
+class MiscTestCase(unittest.TestCase):
+ def test__all__(self):
+ extra = {'__doc__', '__version__'}
+ support.check__all__(self, csv, ('csv', '_csv'), extra=extra)
+
if __name__ == '__main__':
unittest.main()
diff --git a/Lib/test/test_datetime.py b/Lib/test/test_datetime.py
index 2d4eb52..242e1bb 100644
--- a/Lib/test/test_datetime.py
+++ b/Lib/test/test_datetime.py
@@ -23,9 +23,16 @@ test_suffixes = ["_Pure", "_Fast"]
test_classes = []
for module, suffix in zip(test_modules, test_suffixes):
+ test_classes = []
for name, cls in module.__dict__.items():
- if not (isinstance(cls, type) and issubclass(cls, unittest.TestCase)):
+ if not isinstance(cls, type):
continue
+ if issubclass(cls, unittest.TestCase):
+ test_classes.append(cls)
+ elif issubclass(cls, unittest.TestSuite):
+ suit = cls()
+ test_classes.extend(type(test) for test in suit)
+ for cls in test_classes:
cls.__name__ = name + suffix
@classmethod
def setUpClass(cls_, module=module):
@@ -39,7 +46,6 @@ for module, suffix in zip(test_modules, test_suffixes):
sys.modules.update(cls_._save_sys_modules)
cls.setUpClass = setUpClass
cls.tearDownClass = tearDownClass
- test_classes.append(cls)
def test_main():
run_unittest(*test_classes)
diff --git a/Lib/test/test_dbm.py b/Lib/test/test_dbm.py
index 623d992..f0a428d 100644
--- a/Lib/test/test_dbm.py
+++ b/Lib/test/test_dbm.py
@@ -1,6 +1,5 @@
"""Test script for the dbm.open function based on testdumbdbm.py"""
-import os
import unittest
import glob
import test.support
diff --git a/Lib/test/test_dbm_dumb.py b/Lib/test/test_dbm_dumb.py
index 235ab11..df531d6 100644
--- a/Lib/test/test_dbm_dumb.py
+++ b/Lib/test/test_dbm_dumb.py
@@ -7,6 +7,7 @@ import operator
import os
import stat
import unittest
+import warnings
import dbm.dumb as dumbdbm
from test import support
from functools import partial
@@ -79,6 +80,12 @@ class DumbDBMTestCase(unittest.TestCase):
self.init_db()
f = dumbdbm.open(_fname, 'r')
self.read_helper(f)
+ with self.assertWarnsRegex(DeprecationWarning,
+ 'The database is opened for reading only'):
+ f[b'g'] = b'x'
+ with self.assertWarnsRegex(DeprecationWarning,
+ 'The database is opened for reading only'):
+ del f[b'a']
f.close()
def test_dumbdbm_keys(self):
@@ -149,7 +156,7 @@ class DumbDBMTestCase(unittest.TestCase):
self.assertEqual(self._dict[key], f[key])
def init_db(self):
- f = dumbdbm.open(_fname, 'w')
+ f = dumbdbm.open(_fname, 'n')
for k in self._dict:
f[k] = self._dict[k]
f.close()
@@ -235,6 +242,24 @@ class DumbDBMTestCase(unittest.TestCase):
pass
self.assertEqual(stdout.getvalue(), '')
+ def test_warn_on_ignored_flags(self):
+ for value in ('r', 'w'):
+ _delete_files()
+ with self.assertWarnsRegex(DeprecationWarning,
+ "The database file is missing, the "
+ "semantics of the 'c' flag will "
+ "be used."):
+ f = dumbdbm.open(_fname, value)
+ f.close()
+
+ def test_invalid_flag(self):
+ for flag in ('x', 'rf', None):
+ with self.assertWarnsRegex(DeprecationWarning,
+ "Flag must be one of "
+ "'r', 'w', 'c', or 'n'"):
+ f = dumbdbm.open(_fname, flag)
+ f.close()
+
@unittest.skipUnless(hasattr(os, 'chmod'), 'test needs os.chmod()')
def test_readonly_files(self):
with support.temp_dir() as dir:
diff --git a/Lib/test/test_dbm_gnu.py b/Lib/test/test_dbm_gnu.py
index a7808f5..304b332 100644
--- a/Lib/test/test_dbm_gnu.py
+++ b/Lib/test/test_dbm_gnu.py
@@ -2,7 +2,7 @@ from test import support
gdbm = support.import_module("dbm.gnu") #skip if not supported
import unittest
import os
-from test.support import verbose, TESTFN, unlink
+from test.support import TESTFN, unlink
filename = TESTFN
diff --git a/Lib/test/test_dbm_ndbm.py b/Lib/test/test_dbm_ndbm.py
index 2291561..49f4426 100644
--- a/Lib/test/test_dbm_ndbm.py
+++ b/Lib/test/test_dbm_ndbm.py
@@ -1,8 +1,6 @@
from test import support
support.import_module("dbm.ndbm") #skip if not supported
import unittest
-import os
-import random
import dbm.ndbm
from dbm.ndbm import error
diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py
index 1aa0bf8..617a37e 100644
--- a/Lib/test/test_decimal.py
+++ b/Lib/test/test_decimal.py
@@ -554,6 +554,10 @@ class ExplicitConstructionTest(unittest.TestCase):
self.assertEqual(str(Decimal(' -7.89')), '-7.89')
self.assertEqual(str(Decimal(" 3.45679 ")), '3.45679')
+ # underscores
+ self.assertEqual(str(Decimal('1_3.3e4_0')), '1.33E+41')
+ self.assertEqual(str(Decimal('1_0_0_0')), '1000')
+
# unicode whitespace
for lead in ["", ' ', '\u00a0', '\u205f']:
for trail in ["", ' ', '\u00a0', '\u205f']:
@@ -578,6 +582,9 @@ class ExplicitConstructionTest(unittest.TestCase):
# embedded NUL
self.assertRaises(InvalidOperation, Decimal, "12\u00003")
+ # underscores don't prevent errors
+ self.assertRaises(InvalidOperation, Decimal, "1_2_\u00003")
+
@cpython_only
def test_from_legacy_strings(self):
import _testcapi
@@ -772,6 +779,9 @@ class ExplicitConstructionTest(unittest.TestCase):
self.assertRaises(InvalidOperation, nc.create_decimal, "xyz")
self.assertRaises(ValueError, nc.create_decimal, (1, "xyz", -25))
self.assertRaises(TypeError, nc.create_decimal, "1234", "5678")
+ # no whitespace and underscore stripping is done with this method
+ self.assertRaises(InvalidOperation, nc.create_decimal, " 1234")
+ self.assertRaises(InvalidOperation, nc.create_decimal, "12_34")
# too many NaN payload digits
nc.prec = 3
@@ -2047,6 +2057,39 @@ class UsabilityTest(unittest.TestCase):
d = Decimal( (1, (0, 2, 7, 1), 'F') )
self.assertEqual(d.as_tuple(), (1, (0,), 'F'))
+ def test_as_integer_ratio(self):
+ Decimal = self.decimal.Decimal
+
+ # exceptional cases
+ self.assertRaises(OverflowError,
+ Decimal.as_integer_ratio, Decimal('inf'))
+ self.assertRaises(OverflowError,
+ Decimal.as_integer_ratio, Decimal('-inf'))
+ self.assertRaises(ValueError,
+ Decimal.as_integer_ratio, Decimal('-nan'))
+ self.assertRaises(ValueError,
+ Decimal.as_integer_ratio, Decimal('snan123'))
+
+ for exp in range(-4, 2):
+ for coeff in range(1000):
+ for sign in '+', '-':
+ d = Decimal('%s%dE%d' % (sign, coeff, exp))
+ pq = d.as_integer_ratio()
+ p, q = pq
+
+ # check return type
+ self.assertIsInstance(pq, tuple)
+ self.assertIsInstance(p, int)
+ self.assertIsInstance(q, int)
+
+ # check normalization: q should be positive;
+ # p should be relatively prime to q.
+ self.assertGreater(q, 0)
+ self.assertEqual(math.gcd(p, q), 1)
+
+ # check that p/q actually gives the correct value
+ self.assertEqual(Decimal(p) / Decimal(q), d)
+
def test_subclassing(self):
# Different behaviours when subclassing Decimal
Decimal = self.decimal.Decimal
diff --git a/Lib/test/test_deque.py b/Lib/test/test_deque.py
index 18e1df0..ce517b5 100644
--- a/Lib/test/test_deque.py
+++ b/Lib/test/test_deque.py
@@ -289,7 +289,7 @@ class TestBasic(unittest.TestCase):
else:
self.assertEqual(d.index(element, start, stop), target)
- def test_insert_bug_24913(self):
+ def test_index_bug_24913(self):
d = deque('A' * 3)
with self.assertRaises(ValueError):
i = d.index("Hello world", 0, 4)
@@ -622,20 +622,22 @@ class TestBasic(unittest.TestCase):
self.assertEqual(list(d), list(e))
def test_pickle(self):
- d = deque(range(200))
- for i in range(pickle.HIGHEST_PROTOCOL + 1):
- s = pickle.dumps(d, i)
- e = pickle.loads(s)
- self.assertNotEqual(id(d), id(e))
- self.assertEqual(list(d), list(e))
-
-## def test_pickle_recursive(self):
-## d = deque('abc')
-## d.append(d)
-## for i in range(pickle.HIGHEST_PROTOCOL + 1):
-## e = pickle.loads(pickle.dumps(d, i))
-## self.assertNotEqual(id(d), id(e))
-## self.assertEqual(id(e), id(e[-1]))
+ for d in deque(range(200)), deque(range(200), 100):
+ for i in range(pickle.HIGHEST_PROTOCOL + 1):
+ s = pickle.dumps(d, i)
+ e = pickle.loads(s)
+ self.assertNotEqual(id(e), id(d))
+ self.assertEqual(list(e), list(d))
+ self.assertEqual(e.maxlen, d.maxlen)
+
+ def test_pickle_recursive(self):
+ for d in deque('abc'), deque('abc', 3):
+ d.append(d)
+ for i in range(pickle.HIGHEST_PROTOCOL + 1):
+ e = pickle.loads(pickle.dumps(d, i))
+ self.assertNotEqual(id(e), id(d))
+ self.assertEqual(id(e[-1]), id(e))
+ self.assertEqual(e.maxlen, d.maxlen)
def test_iterator_pickle(self):
orig = deque(range(200))
@@ -696,6 +698,15 @@ class TestBasic(unittest.TestCase):
self.assertNotEqual(id(d), id(e))
self.assertEqual(list(d), list(e))
+ for i in range(5):
+ for maxlen in range(-1, 6):
+ s = [random.random() for j in range(i)]
+ d = deque(s) if maxlen == -1 else deque(s, maxlen)
+ e = d.copy()
+ self.assertEqual(d, e)
+ self.assertEqual(d.maxlen, e.maxlen)
+ self.assertTrue(all(x is y for x, y in zip(d, e)))
+
def test_copy_method(self):
mut = [10]
d = deque([mut])
@@ -845,24 +856,26 @@ class TestSubclass(unittest.TestCase):
self.assertEqual(type(d), type(e))
self.assertEqual(list(d), list(e))
-## def test_pickle(self):
-## d = Deque('abc')
-## d.append(d)
-##
-## e = pickle.loads(pickle.dumps(d))
-## self.assertNotEqual(id(d), id(e))
-## self.assertEqual(type(d), type(e))
-## dd = d.pop()
-## ee = e.pop()
-## self.assertEqual(id(e), id(ee))
-## self.assertEqual(d, e)
-##
-## d.x = d
-## e = pickle.loads(pickle.dumps(d))
-## self.assertEqual(id(e), id(e.x))
-##
-## d = DequeWithBadIter('abc')
-## self.assertRaises(TypeError, pickle.dumps, d)
+ def test_pickle_recursive(self):
+ for proto in range(pickle.HIGHEST_PROTOCOL + 1):
+ for d in Deque('abc'), Deque('abc', 3):
+ d.append(d)
+
+ e = pickle.loads(pickle.dumps(d, proto))
+ self.assertNotEqual(id(e), id(d))
+ self.assertEqual(type(e), type(d))
+ self.assertEqual(e.maxlen, d.maxlen)
+ dd = d.pop()
+ ee = e.pop()
+ self.assertEqual(id(ee), id(e))
+ self.assertEqual(e, d)
+
+ d.x = d
+ e = pickle.loads(pickle.dumps(d, proto))
+ self.assertEqual(id(e.x), id(e))
+
+ for d in DequeWithBadIter('abc'), DequeWithBadIter('abc', 2):
+ self.assertRaises(TypeError, pickle.dumps, d, proto)
def test_weakref(self):
d = deque('gallahad')
diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py
index eda6fc1..c5bff77 100644
--- a/Lib/test/test_descr.py
+++ b/Lib/test/test_descr.py
@@ -4828,11 +4828,8 @@ class PicklingTests(unittest.TestCase):
return (args, kwargs)
obj = C3()
for proto in protocols:
- if proto >= 4:
+ if proto >= 2:
self._check_reduce(proto, obj, args, kwargs)
- elif proto >= 2:
- with self.assertRaises(ValueError):
- obj.__reduce_ex__(proto)
class C4:
def __getnewargs_ex__(self):
@@ -5151,10 +5148,6 @@ class PicklingTests(unittest.TestCase):
kwargs = getattr(cls, 'KWARGS', {})
obj = cls(*cls.ARGS, **kwargs)
proto = pickle_copier.proto
- if 2 <= proto < 4 and hasattr(cls, '__getnewargs_ex__'):
- with self.assertRaises(ValueError):
- pickle_copier.dumps(obj, proto)
- continue
objcopy = pickle_copier.copy(obj)
self._assert_is_copy(obj, objcopy)
# For test classes that supports this, make sure we didn't go
@@ -5213,12 +5206,14 @@ class SharedKeyTests(unittest.TestCase):
a, b = A(), B()
self.assertEqual(sys.getsizeof(vars(a)), sys.getsizeof(vars(b)))
self.assertLess(sys.getsizeof(vars(a)), sys.getsizeof({}))
- a.x, a.y, a.z, a.w = range(4)
+ # Initial hash table can contain at most 5 elements.
+ # Set 6 attributes to cause internal resizing.
+ a.x, a.y, a.z, a.w, a.v, a.u = range(6)
self.assertNotEqual(sys.getsizeof(vars(a)), sys.getsizeof(vars(b)))
a2 = A()
self.assertEqual(sys.getsizeof(vars(a)), sys.getsizeof(vars(a2)))
self.assertLess(sys.getsizeof(vars(a)), sys.getsizeof({}))
- b.u, b.v, b.w, b.t = range(4)
+ b.u, b.v, b.w, b.t, b.s, b.r = range(6)
self.assertLess(sys.getsizeof(vars(b)), sys.getsizeof({}))
diff --git a/Lib/test/test_descrtut.py b/Lib/test/test_descrtut.py
index 506d1ab..b84d644 100644
--- a/Lib/test/test_descrtut.py
+++ b/Lib/test/test_descrtut.py
@@ -182,6 +182,7 @@ You can get the information from the list type:
'__iadd__',
'__imul__',
'__init__',
+ '__init_subclass__',
'__iter__',
'__le__',
'__len__',
diff --git a/Lib/test/test_devpoll.py b/Lib/test/test_devpoll.py
index 955618a..c133c81 100644
--- a/Lib/test/test_devpoll.py
+++ b/Lib/test/test_devpoll.py
@@ -5,9 +5,8 @@
import os
import random
import select
-import sys
import unittest
-from test.support import TESTFN, run_unittest, cpython_only
+from test.support import run_unittest, cpython_only
if not hasattr(select, 'devpoll') :
raise unittest.SkipTest('test works only on Solaris OS family')
diff --git a/Lib/test/test_dict.py b/Lib/test/test_dict.py
index b85d333..832bb9c 100644
--- a/Lib/test/test_dict.py
+++ b/Lib/test/test_dict.py
@@ -1,10 +1,13 @@
-import unittest
-from test import support
-
-import collections, random, string
+import collections
import collections.abc
-import gc, weakref
+import gc
import pickle
+import random
+import string
+import sys
+import unittest
+import weakref
+from test import support
class DictTest(unittest.TestCase):
@@ -836,21 +839,127 @@ class DictTest(unittest.TestCase):
pass
self._tracked(MyDict())
+ def make_shared_key_dict(self, n):
+ class C:
+ pass
+
+ dicts = []
+ for i in range(n):
+ a = C()
+ a.x, a.y, a.z = 1, 2, 3
+ dicts.append(a.__dict__)
+
+ return dicts
+
+ @support.cpython_only
+ def test_splittable_setdefault(self):
+ """split table must be combined when setdefault()
+ breaks insertion order"""
+ a, b = self.make_shared_key_dict(2)
+
+ a['a'] = 1
+ size_a = sys.getsizeof(a)
+ a['b'] = 2
+ b.setdefault('b', 2)
+ size_b = sys.getsizeof(b)
+ b['a'] = 1
+
+ self.assertGreater(size_b, size_a)
+ self.assertEqual(list(a), ['x', 'y', 'z', 'a', 'b'])
+ self.assertEqual(list(b), ['x', 'y', 'z', 'b', 'a'])
+
+ @support.cpython_only
+ def test_splittable_del(self):
+ """split table must be combined when del d[k]"""
+ a, b = self.make_shared_key_dict(2)
+
+ orig_size = sys.getsizeof(a)
+
+ del a['y'] # split table is combined
+ with self.assertRaises(KeyError):
+ del a['y']
+
+ self.assertGreater(sys.getsizeof(a), orig_size)
+ self.assertEqual(list(a), ['x', 'z'])
+ self.assertEqual(list(b), ['x', 'y', 'z'])
+
+ # Two dicts have different insertion order.
+ a['y'] = 42
+ self.assertEqual(list(a), ['x', 'z', 'y'])
+ self.assertEqual(list(b), ['x', 'y', 'z'])
+
+ @support.cpython_only
+ def test_splittable_pop(self):
+ """split table must be combined when d.pop(k)"""
+ a, b = self.make_shared_key_dict(2)
+
+ orig_size = sys.getsizeof(a)
+
+ a.pop('y') # split table is combined
+ with self.assertRaises(KeyError):
+ a.pop('y')
+
+ self.assertGreater(sys.getsizeof(a), orig_size)
+ self.assertEqual(list(a), ['x', 'z'])
+ self.assertEqual(list(b), ['x', 'y', 'z'])
+
+ # Two dicts have different insertion order.
+ a['y'] = 42
+ self.assertEqual(list(a), ['x', 'z', 'y'])
+ self.assertEqual(list(b), ['x', 'y', 'z'])
+
+ @support.cpython_only
+ def test_splittable_pop_pending(self):
+ """pop a pending key in a splitted table should not crash"""
+ a, b = self.make_shared_key_dict(2)
+
+ a['a'] = 4
+ with self.assertRaises(KeyError):
+ b.pop('a')
+
+ @support.cpython_only
+ def test_splittable_popitem(self):
+ """split table must be combined when d.popitem()"""
+ a, b = self.make_shared_key_dict(2)
+
+ orig_size = sys.getsizeof(a)
+
+ item = a.popitem() # split table is combined
+ self.assertEqual(item, ('z', 3))
+ with self.assertRaises(KeyError):
+ del a['z']
+
+ self.assertGreater(sys.getsizeof(a), orig_size)
+ self.assertEqual(list(a), ['x', 'y'])
+ self.assertEqual(list(b), ['x', 'y', 'z'])
+
@support.cpython_only
def test_splittable_setattr_after_pop(self):
- """setattr must not convert combined table into split table"""
+ """setattr() must not convert combined table into split table."""
# Issue 28147
import _testcapi
class C:
pass
a = C()
+
+ a.a = 1
+ self.assertTrue(_testcapi.dict_hassplittable(a.__dict__))
+
+ # dict.pop() convert it to combined table
+ a.__dict__.pop('a')
+ self.assertFalse(_testcapi.dict_hassplittable(a.__dict__))
+
+ # But C should not convert a.__dict__ to split table again.
+ a.a = 1
+ self.assertFalse(_testcapi.dict_hassplittable(a.__dict__))
+
+ # Same for popitem()
+ a = C()
a.a = 2
self.assertTrue(_testcapi.dict_hassplittable(a.__dict__))
- # dict.popitem() convert it to combined table
a.__dict__.popitem()
self.assertFalse(_testcapi.dict_hassplittable(a.__dict__))
- # But C should not convert a.__dict__ to split table again.
a.a = 3
self.assertFalse(_testcapi.dict_hassplittable(a.__dict__))
@@ -976,6 +1085,37 @@ class DictTest(unittest.TestCase):
support.check_free_after_iterating(self, lambda d: iter(d.values()), dict)
support.check_free_after_iterating(self, lambda d: iter(d.items()), dict)
+
+class CAPITest(unittest.TestCase):
+
+ # Test _PyDict_GetItem_KnownHash()
+ @support.cpython_only
+ def test_getitem_knownhash(self):
+ from _testcapi import dict_getitem_knownhash
+
+ d = {'x': 1, 'y': 2, 'z': 3}
+ self.assertEqual(dict_getitem_knownhash(d, 'x', hash('x')), 1)
+ self.assertEqual(dict_getitem_knownhash(d, 'y', hash('y')), 2)
+ self.assertEqual(dict_getitem_knownhash(d, 'z', hash('z')), 3)
+
+ # not a dict
+ self.assertRaises(SystemError, dict_getitem_knownhash, [], 1, hash(1))
+ # key does not exist
+ self.assertRaises(KeyError, dict_getitem_knownhash, {}, 1, hash(1))
+
+ class Exc(Exception): pass
+ class BadEq:
+ def __eq__(self, other):
+ raise Exc
+ def __hash__(self):
+ return 7
+
+ k1, k2 = BadEq(), BadEq()
+ d = {k1: 1}
+ self.assertEqual(dict_getitem_knownhash(d, k1, hash(k1)), 1)
+ self.assertRaises(Exc, dict_getitem_knownhash, d, k2, hash(k2))
+
+
from test import mapping_tests
class GeneralMappingTests(mapping_tests.BasicTestMappingProtocol):
@@ -987,5 +1127,6 @@ class Dict(dict):
class SubclassMappingTests(mapping_tests.BasicTestMappingProtocol):
type2test = Dict
+
if __name__ == "__main__":
unittest.main()
diff --git a/Lib/test/test_dict_version.py b/Lib/test/test_dict_version.py
new file mode 100644
index 0000000..5671f9f
--- /dev/null
+++ b/Lib/test/test_dict_version.py
@@ -0,0 +1,186 @@
+"""
+Test implementation of the PEP 509: dictionary versionning.
+"""
+import unittest
+from test import support
+
+# PEP 509 is implemented in CPython but other Python implementations
+# don't require to implement it
+_testcapi = support.import_module('_testcapi')
+
+
+class DictVersionTests(unittest.TestCase):
+ type2test = dict
+
+ def setUp(self):
+ self.seen_versions = set()
+ self.dict = None
+
+ def check_version_unique(self, mydict):
+ version = _testcapi.dict_get_version(mydict)
+ self.assertNotIn(version, self.seen_versions)
+ self.seen_versions.add(version)
+
+ def check_version_changed(self, mydict, method, *args, **kw):
+ result = method(*args, **kw)
+ self.check_version_unique(mydict)
+ return result
+
+ def check_version_dont_change(self, mydict, method, *args, **kw):
+ version1 = _testcapi.dict_get_version(mydict)
+ self.seen_versions.add(version1)
+
+ result = method(*args, **kw)
+
+ version2 = _testcapi.dict_get_version(mydict)
+ self.assertEqual(version2, version1, "version changed")
+
+ return result
+
+ def new_dict(self, *args, **kw):
+ d = self.type2test(*args, **kw)
+ self.check_version_unique(d)
+ return d
+
+ def test_constructor(self):
+ # new empty dictionaries must all have an unique version
+ empty1 = self.new_dict()
+ empty2 = self.new_dict()
+ empty3 = self.new_dict()
+
+ # non-empty dictionaries must also have an unique version
+ nonempty1 = self.new_dict(x='x')
+ nonempty2 = self.new_dict(x='x', y='y')
+
+ def test_copy(self):
+ d = self.new_dict(a=1, b=2)
+
+ d2 = self.check_version_dont_change(d, d.copy)
+
+ # dict.copy() must create a dictionary with a new unique version
+ self.check_version_unique(d2)
+
+ def test_setitem(self):
+ d = self.new_dict()
+
+ # creating new keys must change the version
+ self.check_version_changed(d, d.__setitem__, 'x', 'x')
+ self.check_version_changed(d, d.__setitem__, 'y', 'y')
+
+ # changing values must change the version
+ self.check_version_changed(d, d.__setitem__, 'x', 1)
+ self.check_version_changed(d, d.__setitem__, 'y', 2)
+
+ def test_setitem_same_value(self):
+ value = object()
+ d = self.new_dict()
+
+ # setting a key must change the version
+ self.check_version_changed(d, d.__setitem__, 'key', value)
+
+ # setting a key to the same value with dict.__setitem__
+ # must change the version
+ self.check_version_changed(d, d.__setitem__, 'key', value)
+
+ # setting a key to the same value with dict.update
+ # must change the version
+ self.check_version_changed(d, d.update, key=value)
+
+ d2 = self.new_dict(key=value)
+ self.check_version_changed(d, d.update, d2)
+
+ def test_setitem_equal(self):
+ class AlwaysEqual:
+ def __eq__(self, other):
+ return True
+
+ value1 = AlwaysEqual()
+ value2 = AlwaysEqual()
+ self.assertTrue(value1 == value2)
+ self.assertFalse(value1 != value2)
+
+ d = self.new_dict()
+ self.check_version_changed(d, d.__setitem__, 'key', value1)
+
+ # setting a key to a value equal to the current value
+ # with dict.__setitem__() must change the version
+ self.check_version_changed(d, d.__setitem__, 'key', value2)
+
+ # setting a key to a value equal to the current value
+ # with dict.update() must change the version
+ self.check_version_changed(d, d.update, key=value1)
+
+ d2 = self.new_dict(key=value2)
+ self.check_version_changed(d, d.update, d2)
+
+ def test_setdefault(self):
+ d = self.new_dict()
+
+ # setting a key with dict.setdefault() must change the version
+ self.check_version_changed(d, d.setdefault, 'key', 'value1')
+
+ # don't change the version if the key already exists
+ self.check_version_dont_change(d, d.setdefault, 'key', 'value2')
+
+ def test_delitem(self):
+ d = self.new_dict(key='value')
+
+ # deleting a key with dict.__delitem__() must change the version
+ self.check_version_changed(d, d.__delitem__, 'key')
+
+ # don't change the version if the key doesn't exist
+ self.check_version_dont_change(d, self.assertRaises, KeyError,
+ d.__delitem__, 'key')
+
+ def test_pop(self):
+ d = self.new_dict(key='value')
+
+ # pop() must change the version if the key exists
+ self.check_version_changed(d, d.pop, 'key')
+
+ # pop() must not change the version if the key does not exist
+ self.check_version_dont_change(d, self.assertRaises, KeyError,
+ d.pop, 'key')
+
+ def test_popitem(self):
+ d = self.new_dict(key='value')
+
+ # popitem() must change the version if the dict is not empty
+ self.check_version_changed(d, d.popitem)
+
+ # popitem() must not change the version if the dict is empty
+ self.check_version_dont_change(d, self.assertRaises, KeyError,
+ d.popitem)
+
+ def test_update(self):
+ d = self.new_dict(key='value')
+
+ # update() calling with no argument must not change the version
+ self.check_version_dont_change(d, d.update)
+
+ # update() must change the version
+ self.check_version_changed(d, d.update, key='new value')
+
+ d2 = self.new_dict(key='value 3')
+ self.check_version_changed(d, d.update, d2)
+
+ def test_clear(self):
+ d = self.new_dict(key='value')
+
+ # clear() must change the version if the dict is not empty
+ self.check_version_changed(d, d.clear)
+
+ # clear() must not change the version if the dict is empty
+ self.check_version_dont_change(d, d.clear)
+
+
+class Dict(dict):
+ pass
+
+
+class DictSubtypeVersionTests(DictVersionTests):
+ type2test = Dict
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/Lib/test/test_dictcomps.py b/Lib/test/test_dictcomps.py
index 3c8b95c..0873071 100644
--- a/Lib/test/test_dictcomps.py
+++ b/Lib/test/test_dictcomps.py
@@ -1,7 +1,5 @@
import unittest
-from test import support
-
# For scope testing.
g = "Global variable"
diff --git a/Lib/test/test_dictviews.py b/Lib/test/test_dictviews.py
index 0c99652..49a9e9c 100644
--- a/Lib/test/test_dictviews.py
+++ b/Lib/test/test_dictviews.py
@@ -1,3 +1,4 @@
+import collections
import copy
import pickle
import unittest
@@ -245,6 +246,27 @@ class DictSetTest(unittest.TestCase):
self.assertRaises((TypeError, pickle.PicklingError),
pickle.dumps, d.items(), proto)
+ def test_abc_registry(self):
+ d = dict(a=1)
+
+ self.assertIsInstance(d.keys(), collections.KeysView)
+ self.assertIsInstance(d.keys(), collections.MappingView)
+ self.assertIsInstance(d.keys(), collections.Set)
+ self.assertIsInstance(d.keys(), collections.Sized)
+ self.assertIsInstance(d.keys(), collections.Iterable)
+ self.assertIsInstance(d.keys(), collections.Container)
+
+ self.assertIsInstance(d.values(), collections.ValuesView)
+ self.assertIsInstance(d.values(), collections.MappingView)
+ self.assertIsInstance(d.values(), collections.Sized)
+
+ self.assertIsInstance(d.items(), collections.ItemsView)
+ self.assertIsInstance(d.items(), collections.MappingView)
+ self.assertIsInstance(d.items(), collections.Set)
+ self.assertIsInstance(d.items(), collections.Sized)
+ self.assertIsInstance(d.items(), collections.Iterable)
+ self.assertIsInstance(d.items(), collections.Container)
+
if __name__ == "__main__":
unittest.main()
diff --git a/Lib/test/test_difflib.py b/Lib/test/test_difflib.py
index ab9debf..156b523 100644
--- a/Lib/test/test_difflib.py
+++ b/Lib/test/test_difflib.py
@@ -122,17 +122,17 @@ patch914575_nonascii_to1 = """
"""
patch914575_from2 = """
-\t\tLine 1: preceeded by from:[tt] to:[ssss]
- \t\tLine 2: preceeded by from:[sstt] to:[sssst]
- \t \tLine 3: preceeded by from:[sstst] to:[ssssss]
+\t\tLine 1: preceded by from:[tt] to:[ssss]
+ \t\tLine 2: preceded by from:[sstt] to:[sssst]
+ \t \tLine 3: preceded by from:[sstst] to:[ssssss]
Line 4: \thas from:[sst] to:[sss] after :
Line 5: has from:[t] to:[ss] at end\t
"""
patch914575_to2 = """
- Line 1: preceeded by from:[tt] to:[ssss]
- \tLine 2: preceeded by from:[sstt] to:[sssst]
- Line 3: preceeded by from:[sstst] to:[ssssss]
+ Line 1: preceded by from:[tt] to:[ssss]
+ \tLine 2: preceded by from:[sstt] to:[sssst]
+ Line 3: preceded by from:[sstst] to:[ssssss]
Line 4: has from:[sst] to:[sss] after :
Line 5: has from:[t] to:[ss] at end
"""
diff --git a/Lib/test/test_difflib_expect.html b/Lib/test/test_difflib_expect.html
index ea7a24e..3e6a7b7 100644
--- a/Lib/test/test_difflib_expect.html
+++ b/Lib/test/test_difflib_expect.html
@@ -387,9 +387,9 @@
<tbody>
<tr><td class="diff_next" id="difflib_chg_to9__0"><a href="#difflib_chg_to9__0">f</a></td><td class="diff_header" id="from9_1">1</td><td nowrap="nowrap"></td><td class="diff_next"><a href="#difflib_chg_to9__0">f</a></td><td class="diff_header" id="to9_1">1</td><td nowrap="nowrap"></td></tr>
- <tr><td class="diff_next"><a href="#difflib_chg_to9__top">t</a></td><td class="diff_header" id="from9_2">2</td><td nowrap="nowrap"><span class="diff_chg">&nbsp;&nbsp;&nbsp;&nbsp;</span>Line&nbsp;1:&nbsp;preceeded&nbsp;by&nbsp;from:[tt]&nbsp;to:[ssss]</td><td class="diff_next"><a href="#difflib_chg_to9__top">t</a></td><td class="diff_header" id="to9_2">2</td><td nowrap="nowrap"><span class="diff_chg">&nbsp;&nbsp;&nbsp;&nbsp;</span>Line&nbsp;1:&nbsp;preceeded&nbsp;by&nbsp;from:[tt]&nbsp;to:[ssss]</td></tr>
- <tr><td class="diff_next"></td><td class="diff_header" id="from9_3">3</td><td nowrap="nowrap">&nbsp;&nbsp;<span class="diff_chg">&nbsp;&nbsp;</span>&nbsp;&nbsp;Line&nbsp;2:&nbsp;preceeded&nbsp;by&nbsp;from:[sstt]&nbsp;to:[sssst]</td><td class="diff_next"></td><td class="diff_header" id="to9_3">3</td><td nowrap="nowrap">&nbsp;&nbsp;<span class="diff_chg">&nbsp;&nbsp;</span>&nbsp;&nbsp;Line&nbsp;2:&nbsp;preceeded&nbsp;by&nbsp;from:[sstt]&nbsp;to:[sssst]</td></tr>
- <tr><td class="diff_next"></td><td class="diff_header" id="from9_4">4</td><td nowrap="nowrap">&nbsp;&nbsp;<span class="diff_chg">&nbsp;&nbsp;&nbsp;&nbsp;</span>Line&nbsp;3:&nbsp;preceeded&nbsp;by&nbsp;from:[sstst]&nbsp;to:[ssssss]</td><td class="diff_next"></td><td class="diff_header" id="to9_4">4</td><td nowrap="nowrap">&nbsp;&nbsp;<span class="diff_chg">&nbsp;&nbsp;&nbsp;&nbsp;</span>Line&nbsp;3:&nbsp;preceeded&nbsp;by&nbsp;from:[sstst]&nbsp;to:[ssssss]</td></tr>
+ <tr><td class="diff_next"><a href="#difflib_chg_to9__top">t</a></td><td class="diff_header" id="from9_2">2</td><td nowrap="nowrap"><span class="diff_chg">&nbsp;&nbsp;&nbsp;&nbsp;</span>Line&nbsp;1:&nbsp;preceded&nbsp;by&nbsp;from:[tt]&nbsp;to:[ssss]</td><td class="diff_next"><a href="#difflib_chg_to9__top">t</a></td><td class="diff_header" id="to9_2">2</td><td nowrap="nowrap"><span class="diff_chg">&nbsp;&nbsp;&nbsp;&nbsp;</span>Line&nbsp;1:&nbsp;preceded&nbsp;by&nbsp;from:[tt]&nbsp;to:[ssss]</td></tr>
+ <tr><td class="diff_next"></td><td class="diff_header" id="from9_3">3</td><td nowrap="nowrap">&nbsp;&nbsp;<span class="diff_chg">&nbsp;&nbsp;</span>&nbsp;&nbsp;Line&nbsp;2:&nbsp;preceded&nbsp;by&nbsp;from:[sstt]&nbsp;to:[sssst]</td><td class="diff_next"></td><td class="diff_header" id="to9_3">3</td><td nowrap="nowrap">&nbsp;&nbsp;<span class="diff_chg">&nbsp;&nbsp;</span>&nbsp;&nbsp;Line&nbsp;2:&nbsp;preceded&nbsp;by&nbsp;from:[sstt]&nbsp;to:[sssst]</td></tr>
+ <tr><td class="diff_next"></td><td class="diff_header" id="from9_4">4</td><td nowrap="nowrap">&nbsp;&nbsp;<span class="diff_chg">&nbsp;&nbsp;&nbsp;&nbsp;</span>Line&nbsp;3:&nbsp;preceded&nbsp;by&nbsp;from:[sstst]&nbsp;to:[ssssss]</td><td class="diff_next"></td><td class="diff_header" id="to9_4">4</td><td nowrap="nowrap">&nbsp;&nbsp;<span class="diff_chg">&nbsp;&nbsp;&nbsp;&nbsp;</span>Line&nbsp;3:&nbsp;preceded&nbsp;by&nbsp;from:[sstst]&nbsp;to:[ssssss]</td></tr>
<tr><td class="diff_next"></td><td class="diff_header" id="from9_5">5</td><td nowrap="nowrap">Line&nbsp;4:&nbsp;&nbsp;<span class="diff_chg">&nbsp;</span>has&nbsp;from:[sst]&nbsp;to:[sss]&nbsp;after&nbsp;:</td><td class="diff_next"></td><td class="diff_header" id="to9_5">5</td><td nowrap="nowrap">Line&nbsp;4:&nbsp;&nbsp;<span class="diff_chg">&nbsp;</span>has&nbsp;from:[sst]&nbsp;to:[sss]&nbsp;after&nbsp;:</td></tr>
<tr><td class="diff_next"></td><td class="diff_header" id="from9_6">6</td><td nowrap="nowrap">Line&nbsp;5:&nbsp;has&nbsp;from:[t]&nbsp;to:[ss]&nbsp;at&nbsp;end<span class="diff_sub">&nbsp;</span></td><td class="diff_next"></td><td class="diff_header" id="to9_6">6</td><td nowrap="nowrap">Line&nbsp;5:&nbsp;has&nbsp;from:[t]&nbsp;to:[ss]&nbsp;at&nbsp;end</td></tr>
</tbody>
@@ -403,9 +403,9 @@
<tbody>
<tr><td class="diff_next" id="difflib_chg_to10__0"><a href="#difflib_chg_to10__0">f</a></td><td class="diff_header" id="from10_1">1</td><td nowrap="nowrap"></td><td class="diff_next"><a href="#difflib_chg_to10__0">f</a></td><td class="diff_header" id="to10_1">1</td><td nowrap="nowrap"></td></tr>
- <tr><td class="diff_next"><a href="#difflib_chg_to10__top">t</a></td><td class="diff_header" id="from10_2">2</td><td nowrap="nowrap"><span class="diff_chg">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>Line&nbsp;1:&nbsp;preceeded&nbsp;by&nbsp;from:[tt]&nbsp;to:[ssss]</td><td class="diff_next"><a href="#difflib_chg_to10__top">t</a></td><td class="diff_header" id="to10_2">2</td><td nowrap="nowrap"><span class="diff_chg">&nbsp;&nbsp;&nbsp;&nbsp;</span>Line&nbsp;1:&nbsp;preceeded&nbsp;by&nbsp;from:[tt]&nbsp;to:[ssss]</td></tr>
- <tr><td class="diff_next"></td><td class="diff_header" id="from10_3">3</td><td nowrap="nowrap">&nbsp;&nbsp;<span class="diff_chg">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>&nbsp;&nbsp;&nbsp;&nbsp;Line&nbsp;2:&nbsp;preceeded&nbsp;by&nbsp;from:[sstt]&nbsp;to:[sssst]</td><td class="diff_next"></td><td class="diff_header" id="to10_3">3</td><td nowrap="nowrap">&nbsp;&nbsp;<span class="diff_chg">&nbsp;&nbsp;</span>&nbsp;&nbsp;&nbsp;&nbsp;Line&nbsp;2:&nbsp;preceeded&nbsp;by&nbsp;from:[sstt]&nbsp;to:[sssst]</td></tr>
- <tr><td class="diff_next"></td><td class="diff_header" id="from10_4">4</td><td nowrap="nowrap">&nbsp;&nbsp;<span class="diff_chg">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>Line&nbsp;3:&nbsp;preceeded&nbsp;by&nbsp;from:[sstst]&nbsp;to:[ssssss]</td><td class="diff_next"></td><td class="diff_header" id="to10_4">4</td><td nowrap="nowrap">&nbsp;&nbsp;<span class="diff_chg">&nbsp;&nbsp;&nbsp;&nbsp;</span>Line&nbsp;3:&nbsp;preceeded&nbsp;by&nbsp;from:[sstst]&nbsp;to:[ssssss]</td></tr>
+ <tr><td class="diff_next"><a href="#difflib_chg_to10__top">t</a></td><td class="diff_header" id="from10_2">2</td><td nowrap="nowrap"><span class="diff_chg">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>Line&nbsp;1:&nbsp;preceded&nbsp;by&nbsp;from:[tt]&nbsp;to:[ssss]</td><td class="diff_next"><a href="#difflib_chg_to10__top">t</a></td><td class="diff_header" id="to10_2">2</td><td nowrap="nowrap"><span class="diff_chg">&nbsp;&nbsp;&nbsp;&nbsp;</span>Line&nbsp;1:&nbsp;preceded&nbsp;by&nbsp;from:[tt]&nbsp;to:[ssss]</td></tr>
+ <tr><td class="diff_next"></td><td class="diff_header" id="from10_3">3</td><td nowrap="nowrap">&nbsp;&nbsp;<span class="diff_chg">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>&nbsp;&nbsp;&nbsp;&nbsp;Line&nbsp;2:&nbsp;preceded&nbsp;by&nbsp;from:[sstt]&nbsp;to:[sssst]</td><td class="diff_next"></td><td class="diff_header" id="to10_3">3</td><td nowrap="nowrap">&nbsp;&nbsp;<span class="diff_chg">&nbsp;&nbsp;</span>&nbsp;&nbsp;&nbsp;&nbsp;Line&nbsp;2:&nbsp;preceded&nbsp;by&nbsp;from:[sstt]&nbsp;to:[sssst]</td></tr>
+ <tr><td class="diff_next"></td><td class="diff_header" id="from10_4">4</td><td nowrap="nowrap">&nbsp;&nbsp;<span class="diff_chg">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>Line&nbsp;3:&nbsp;preceded&nbsp;by&nbsp;from:[sstst]&nbsp;to:[ssssss]</td><td class="diff_next"></td><td class="diff_header" id="to10_4">4</td><td nowrap="nowrap">&nbsp;&nbsp;<span class="diff_chg">&nbsp;&nbsp;&nbsp;&nbsp;</span>Line&nbsp;3:&nbsp;preceded&nbsp;by&nbsp;from:[sstst]&nbsp;to:[ssssss]</td></tr>
<tr><td class="diff_next"></td><td class="diff_header" id="from10_5">5</td><td nowrap="nowrap">Line&nbsp;4:&nbsp;&nbsp;<span class="diff_chg">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>has&nbsp;from:[sst]&nbsp;to:[sss]&nbsp;after&nbsp;:</td><td class="diff_next"></td><td class="diff_header" id="to10_5">5</td><td nowrap="nowrap">Line&nbsp;4:&nbsp;&nbsp;<span class="diff_chg">&nbsp;</span>has&nbsp;from:[sst]&nbsp;to:[sss]&nbsp;after&nbsp;:</td></tr>
<tr><td class="diff_next"></td><td class="diff_header" id="from10_6">6</td><td nowrap="nowrap">Line&nbsp;5:&nbsp;has&nbsp;from:[t]&nbsp;to:[ss]&nbsp;at&nbsp;end<span class="diff_sub">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></td><td class="diff_next"></td><td class="diff_header" id="to10_6">6</td><td nowrap="nowrap">Line&nbsp;5:&nbsp;has&nbsp;from:[t]&nbsp;to:[ss]&nbsp;at&nbsp;end</td></tr>
</tbody>
diff --git a/Lib/test/test_dis.py b/Lib/test/test_dis.py
index 0fd1348..b9b5ac2 100644
--- a/Lib/test/test_dis.py
+++ b/Lib/test/test_dis.py
@@ -40,41 +40,41 @@ class _C:
dis_c_instance_method = """\
%3d 0 LOAD_FAST 1 (x)
- 3 LOAD_CONST 1 (1)
- 6 COMPARE_OP 2 (==)
- 9 LOAD_FAST 0 (self)
- 12 STORE_ATTR 0 (x)
- 15 LOAD_CONST 0 (None)
- 18 RETURN_VALUE
+ 2 LOAD_CONST 1 (1)
+ 4 COMPARE_OP 2 (==)
+ 6 LOAD_FAST 0 (self)
+ 8 STORE_ATTR 0 (x)
+ 10 LOAD_CONST 0 (None)
+ 12 RETURN_VALUE
""" % (_C.__init__.__code__.co_firstlineno + 1,)
dis_c_instance_method_bytes = """\
0 LOAD_FAST 1 (1)
- 3 LOAD_CONST 1 (1)
- 6 COMPARE_OP 2 (==)
- 9 LOAD_FAST 0 (0)
- 12 STORE_ATTR 0 (0)
- 15 LOAD_CONST 0 (0)
- 18 RETURN_VALUE
+ 2 LOAD_CONST 1 (1)
+ 4 COMPARE_OP 2 (==)
+ 6 LOAD_FAST 0 (0)
+ 8 STORE_ATTR 0 (0)
+ 10 LOAD_CONST 0 (0)
+ 12 RETURN_VALUE
"""
dis_c_class_method = """\
%3d 0 LOAD_FAST 1 (x)
- 3 LOAD_CONST 1 (1)
- 6 COMPARE_OP 2 (==)
- 9 LOAD_FAST 0 (cls)
- 12 STORE_ATTR 0 (x)
- 15 LOAD_CONST 0 (None)
- 18 RETURN_VALUE
+ 2 LOAD_CONST 1 (1)
+ 4 COMPARE_OP 2 (==)
+ 6 LOAD_FAST 0 (cls)
+ 8 STORE_ATTR 0 (x)
+ 10 LOAD_CONST 0 (None)
+ 12 RETURN_VALUE
""" % (_C.cm.__code__.co_firstlineno + 2,)
dis_c_static_method = """\
%3d 0 LOAD_FAST 0 (x)
- 3 LOAD_CONST 1 (1)
- 6 COMPARE_OP 2 (==)
- 9 STORE_FAST 0 (x)
- 12 LOAD_CONST 0 (None)
- 15 RETURN_VALUE
+ 2 LOAD_CONST 1 (1)
+ 4 COMPARE_OP 2 (==)
+ 6 STORE_FAST 0 (x)
+ 8 LOAD_CONST 0 (None)
+ 10 RETURN_VALUE
""" % (_C.sm.__code__.co_firstlineno + 2,)
# Class disassembling info has an extra newline at end.
@@ -95,23 +95,23 @@ def _f(a):
dis_f = """\
%3d 0 LOAD_GLOBAL 0 (print)
- 3 LOAD_FAST 0 (a)
- 6 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
- 9 POP_TOP
+ 2 LOAD_FAST 0 (a)
+ 4 CALL_FUNCTION 1
+ 6 POP_TOP
-%3d 10 LOAD_CONST 1 (1)
- 13 RETURN_VALUE
+%3d 8 LOAD_CONST 1 (1)
+ 10 RETURN_VALUE
""" % (_f.__code__.co_firstlineno + 1,
_f.__code__.co_firstlineno + 2)
dis_f_co_code = """\
0 LOAD_GLOBAL 0 (0)
- 3 LOAD_FAST 0 (0)
- 6 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
- 9 POP_TOP
- 10 LOAD_CONST 1 (1)
- 13 RETURN_VALUE
+ 2 LOAD_FAST 0 (0)
+ 4 CALL_FUNCTION 1
+ 6 POP_TOP
+ 8 LOAD_CONST 1 (1)
+ 10 RETURN_VALUE
"""
@@ -121,20 +121,20 @@ def bug708901():
pass
dis_bug708901 = """\
-%3d 0 SETUP_LOOP 23 (to 26)
- 3 LOAD_GLOBAL 0 (range)
- 6 LOAD_CONST 1 (1)
-
-%3d 9 LOAD_CONST 2 (10)
- 12 CALL_FUNCTION 2 (2 positional, 0 keyword pair)
- 15 GET_ITER
- >> 16 FOR_ITER 6 (to 25)
- 19 STORE_FAST 0 (res)
-
-%3d 22 JUMP_ABSOLUTE 16
- >> 25 POP_BLOCK
- >> 26 LOAD_CONST 0 (None)
- 29 RETURN_VALUE
+%3d 0 SETUP_LOOP 18 (to 20)
+ 2 LOAD_GLOBAL 0 (range)
+ 4 LOAD_CONST 1 (1)
+
+%3d 6 LOAD_CONST 2 (10)
+ 8 CALL_FUNCTION 2
+ 10 GET_ITER
+ >> 12 FOR_ITER 4 (to 18)
+ 14 STORE_FAST 0 (res)
+
+%3d 16 JUMP_ABSOLUTE 12
+ >> 18 POP_BLOCK
+ >> 20 LOAD_CONST 0 (None)
+ 22 RETURN_VALUE
""" % (bug708901.__code__.co_firstlineno + 1,
bug708901.__code__.co_firstlineno + 2,
bug708901.__code__.co_firstlineno + 3)
@@ -147,22 +147,22 @@ def bug1333982(x=[]):
dis_bug1333982 = """\
%3d 0 LOAD_CONST 1 (0)
- 3 POP_JUMP_IF_TRUE 35
- 6 LOAD_GLOBAL 0 (AssertionError)
- 9 LOAD_CONST 2 (<code object <listcomp> at 0x..., file "%s", line %d>)
- 12 LOAD_CONST 3 ('bug1333982.<locals>.<listcomp>')
- 15 MAKE_FUNCTION 0
- 18 LOAD_FAST 0 (x)
- 21 GET_ITER
- 22 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
-
-%3d 25 LOAD_CONST 4 (1)
- 28 BINARY_ADD
- 29 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
- 32 RAISE_VARARGS 1
-
-%3d >> 35 LOAD_CONST 0 (None)
- 38 RETURN_VALUE
+ 2 POP_JUMP_IF_TRUE 26
+ 4 LOAD_GLOBAL 0 (AssertionError)
+ 6 LOAD_CONST 2 (<code object <listcomp> at 0x..., file "%s", line %d>)
+ 8 LOAD_CONST 3 ('bug1333982.<locals>.<listcomp>')
+ 10 MAKE_FUNCTION 0
+ 12 LOAD_FAST 0 (x)
+ 14 GET_ITER
+ 16 CALL_FUNCTION 1
+
+%3d 18 LOAD_CONST 4 (1)
+ 20 BINARY_ADD
+ 22 CALL_FUNCTION 1
+ 24 RAISE_VARARGS 1
+
+%3d >> 26 LOAD_CONST 0 (None)
+ 28 RETURN_VALUE
""" % (bug1333982.__code__.co_firstlineno + 1,
__file__,
bug1333982.__code__.co_firstlineno + 1,
@@ -171,19 +171,19 @@ dis_bug1333982 = """\
_BIG_LINENO_FORMAT = """\
%3d 0 LOAD_GLOBAL 0 (spam)
- 3 POP_TOP
+ 2 POP_TOP
4 LOAD_CONST 0 (None)
- 7 RETURN_VALUE
+ 6 RETURN_VALUE
"""
dis_module_expected_results = """\
Disassembly of f:
4 0 LOAD_CONST 0 (None)
- 3 RETURN_VALUE
+ 2 RETURN_VALUE
Disassembly of g:
5 0 LOAD_CONST 0 (None)
- 3 RETURN_VALUE
+ 2 RETURN_VALUE
"""
@@ -191,20 +191,52 @@ expr_str = "x + 1"
dis_expr_str = """\
1 0 LOAD_NAME 0 (x)
- 3 LOAD_CONST 0 (1)
- 6 BINARY_ADD
- 7 RETURN_VALUE
+ 2 LOAD_CONST 0 (1)
+ 4 BINARY_ADD
+ 6 RETURN_VALUE
"""
simple_stmt_str = "x = x + 1"
dis_simple_stmt_str = """\
1 0 LOAD_NAME 0 (x)
- 3 LOAD_CONST 0 (1)
- 6 BINARY_ADD
- 7 STORE_NAME 0 (x)
- 10 LOAD_CONST 1 (None)
- 13 RETURN_VALUE
+ 2 LOAD_CONST 0 (1)
+ 4 BINARY_ADD
+ 6 STORE_NAME 0 (x)
+ 8 LOAD_CONST 1 (None)
+ 10 RETURN_VALUE
+"""
+
+annot_stmt_str = """\
+
+x: int = 1
+y: fun(1)
+lst[fun(0)]: int = 1
+"""
+# leading newline is for a reason (tests lineno)
+
+dis_annot_stmt_str = """\
+ 2 0 SETUP_ANNOTATIONS
+ 2 LOAD_CONST 0 (1)
+ 4 STORE_NAME 0 (x)
+ 6 LOAD_NAME 1 (int)
+ 8 STORE_ANNOTATION 0 (x)
+
+ 3 10 LOAD_NAME 2 (fun)
+ 12 LOAD_CONST 0 (1)
+ 14 CALL_FUNCTION 1
+ 16 STORE_ANNOTATION 3 (y)
+
+ 4 18 LOAD_CONST 0 (1)
+ 20 LOAD_NAME 4 (lst)
+ 22 LOAD_NAME 2 (fun)
+ 24 LOAD_CONST 1 (0)
+ 26 CALL_FUNCTION 1
+ 28 STORE_SUBSCR
+ 30 LOAD_NAME 1 (int)
+ 32 POP_TOP
+ 34 LOAD_CONST 2 (None)
+ 36 RETURN_VALUE
"""
compound_stmt_str = """\
@@ -215,60 +247,81 @@ while 1:
dis_compound_stmt_str = """\
1 0 LOAD_CONST 0 (0)
- 3 STORE_NAME 0 (x)
-
- 2 6 SETUP_LOOP 14 (to 23)
-
- 3 >> 9 LOAD_NAME 0 (x)
- 12 LOAD_CONST 1 (1)
- 15 INPLACE_ADD
- 16 STORE_NAME 0 (x)
- 19 JUMP_ABSOLUTE 9
- 22 POP_BLOCK
- >> 23 LOAD_CONST 2 (None)
- 26 RETURN_VALUE
+ 2 STORE_NAME 0 (x)
+
+ 2 4 SETUP_LOOP 12 (to 18)
+
+ 3 >> 6 LOAD_NAME 0 (x)
+ 8 LOAD_CONST 1 (1)
+ 10 INPLACE_ADD
+ 12 STORE_NAME 0 (x)
+ 14 JUMP_ABSOLUTE 6
+ 16 POP_BLOCK
+ >> 18 LOAD_CONST 2 (None)
+ 20 RETURN_VALUE
"""
dis_traceback = """\
-%3d 0 SETUP_EXCEPT 12 (to 15)
+%3d 0 SETUP_EXCEPT 12 (to 14)
-%3d 3 LOAD_CONST 1 (1)
- 6 LOAD_CONST 2 (0)
- --> 9 BINARY_TRUE_DIVIDE
- 10 POP_TOP
- 11 POP_BLOCK
- 12 JUMP_FORWARD 46 (to 61)
+%3d 2 LOAD_CONST 1 (1)
+ 4 LOAD_CONST 2 (0)
+ --> 6 BINARY_TRUE_DIVIDE
+ 8 POP_TOP
+ 10 POP_BLOCK
+ 12 JUMP_FORWARD 40 (to 54)
-%3d >> 15 DUP_TOP
+%3d >> 14 DUP_TOP
16 LOAD_GLOBAL 0 (Exception)
- 19 COMPARE_OP 10 (exception match)
- 22 POP_JUMP_IF_FALSE 60
- 25 POP_TOP
- 26 STORE_FAST 0 (e)
- 29 POP_TOP
- 30 SETUP_FINALLY 14 (to 47)
-
-%3d 33 LOAD_FAST 0 (e)
- 36 LOAD_ATTR 1 (__traceback__)
- 39 STORE_FAST 1 (tb)
- 42 POP_BLOCK
- 43 POP_EXCEPT
- 44 LOAD_CONST 0 (None)
- >> 47 LOAD_CONST 0 (None)
- 50 STORE_FAST 0 (e)
- 53 DELETE_FAST 0 (e)
- 56 END_FINALLY
- 57 JUMP_FORWARD 1 (to 61)
- >> 60 END_FINALLY
-
-%3d >> 61 LOAD_FAST 1 (tb)
- 64 RETURN_VALUE
+ 18 COMPARE_OP 10 (exception match)
+ 20 POP_JUMP_IF_FALSE 52
+ 22 POP_TOP
+ 24 STORE_FAST 0 (e)
+ 26 POP_TOP
+ 28 SETUP_FINALLY 12 (to 42)
+
+%3d 30 LOAD_FAST 0 (e)
+ 32 LOAD_ATTR 1 (__traceback__)
+ 34 STORE_FAST 1 (tb)
+ 36 POP_BLOCK
+ 38 POP_EXCEPT
+ 40 LOAD_CONST 0 (None)
+ >> 42 LOAD_CONST 0 (None)
+ 44 STORE_FAST 0 (e)
+ 46 DELETE_FAST 0 (e)
+ 48 END_FINALLY
+ 50 JUMP_FORWARD 2 (to 54)
+ >> 52 END_FINALLY
+
+%3d >> 54 LOAD_FAST 1 (tb)
+ 56 RETURN_VALUE
""" % (TRACEBACK_CODE.co_firstlineno + 1,
TRACEBACK_CODE.co_firstlineno + 2,
TRACEBACK_CODE.co_firstlineno + 3,
TRACEBACK_CODE.co_firstlineno + 4,
TRACEBACK_CODE.co_firstlineno + 5)
+def _fstring(a, b, c, d):
+ return f'{a} {b:4} {c!r} {d!r:4}'
+
+dis_fstring = """\
+%3d 0 LOAD_FAST 0 (a)
+ 2 FORMAT_VALUE 0
+ 4 LOAD_CONST 1 (' ')
+ 6 LOAD_FAST 1 (b)
+ 8 LOAD_CONST 2 ('4')
+ 10 FORMAT_VALUE 4 (with format)
+ 12 LOAD_CONST 1 (' ')
+ 14 LOAD_FAST 2 (c)
+ 16 FORMAT_VALUE 2 (repr)
+ 18 LOAD_CONST 1 (' ')
+ 20 LOAD_FAST 3 (d)
+ 22 LOAD_CONST 2 ('4')
+ 24 FORMAT_VALUE 6 (repr, with format)
+ 26 BUILD_STRING 7
+ 28 RETURN_VALUE
+""" % (_fstring.__code__.co_firstlineno + 1,)
+
def _g(x):
yield x
@@ -345,6 +398,7 @@ class DisTests(unittest.TestCase):
def test_disassemble_str(self):
self.do_disassembly_test(expr_str, dis_expr_str)
self.do_disassembly_test(simple_stmt_str, dis_simple_stmt_str)
+ self.do_disassembly_test(annot_stmt_str, dis_annot_stmt_str)
self.do_disassembly_test(compound_stmt_str, dis_compound_stmt_str)
def test_disassemble_bytes(self):
@@ -371,6 +425,9 @@ class DisTests(unittest.TestCase):
gen_disas = self.get_disassembly(_g(1)) # Disassemble generator itself
self.assertEqual(gen_disas, gen_func_disas)
+ def test_disassemble_fstring(self):
+ self.do_disassembly_test(_fstring, dis_fstring)
+
def test_dis_none(self):
try:
del sys.last_traceback
@@ -541,7 +598,7 @@ Argument count: 0
Kw-only arguments: 0
Number of locals: 2
Stack size: 17
-Flags: OPTIMIZED, NEWLOCALS, GENERATOR, NOFREE, COROUTINE
+Flags: OPTIMIZED, NEWLOCALS, NOFREE, COROUTINE
Constants:
0: None
1: 1"""
@@ -649,171 +706,169 @@ expected_jumpy_line = 1
Instruction = dis.Instruction
expected_opinfo_outer = [
- Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=3, argrepr='3', offset=0, starts_line=2, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=3, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='a', argrepr='a', offset=6, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='b', argrepr='b', offset=9, starts_line=None, is_jump_target=False),
- Instruction(opname='BUILD_TUPLE', opcode=102, arg=2, argval=2, argrepr='', offset=12, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=code_object_f, argrepr=repr(code_object_f), offset=15, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='outer.<locals>.f', argrepr="'outer.<locals>.f'", offset=18, starts_line=None, is_jump_target=False),
- Instruction(opname='MAKE_CLOSURE', opcode=134, arg=2, argval=2, argrepr='', offset=21, starts_line=None, is_jump_target=False),
- Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='f', argrepr='f', offset=24, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=27, starts_line=7, is_jump_target=False),
- Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=30, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=33, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval='', argrepr="''", offset=36, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval=1, argrepr='1', offset=39, starts_line=None, is_jump_target=False),
- Instruction(opname='BUILD_LIST', opcode=103, arg=0, argval=0, argrepr='', offset=42, starts_line=None, is_jump_target=False),
- Instruction(opname='BUILD_MAP', opcode=105, arg=0, argval=0, argrepr='', offset=45, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval='Hello world!', argrepr="'Hello world!'", offset=48, starts_line=None, is_jump_target=False),
- Instruction(opname='CALL_FUNCTION', opcode=131, arg=7, argval=7, argrepr='7 positional, 0 keyword pair', offset=51, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=54, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='f', argrepr='f', offset=55, starts_line=8, is_jump_target=False),
- Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=58, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=8, argval=(3, 4), argrepr='(3, 4)', offset=0, starts_line=2, is_jump_target=False),
+ Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False),
+ Instruction(opname='BUILD_TUPLE', opcode=102, arg=2, argval=2, argrepr='', offset=6, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=code_object_f, argrepr=repr(code_object_f), offset=8, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='outer.<locals>.f', argrepr="'outer.<locals>.f'", offset=10, starts_line=None, is_jump_target=False),
+ Instruction(opname='MAKE_FUNCTION', opcode=132, arg=9, argval=9, argrepr='', offset=12, starts_line=None, is_jump_target=False),
+ Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='f', argrepr='f', offset=14, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=16, starts_line=7, is_jump_target=False),
+ Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=18, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=20, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval='', argrepr="''", offset=22, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval=1, argrepr='1', offset=24, starts_line=None, is_jump_target=False),
+ Instruction(opname='BUILD_LIST', opcode=103, arg=0, argval=0, argrepr='', offset=26, starts_line=None, is_jump_target=False),
+ Instruction(opname='BUILD_MAP', opcode=105, arg=0, argval=0, argrepr='', offset=28, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval='Hello world!', argrepr="'Hello world!'", offset=30, starts_line=None, is_jump_target=False),
+ Instruction(opname='CALL_FUNCTION', opcode=131, arg=7, argval=7, argrepr='', offset=32, starts_line=None, is_jump_target=False),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=34, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='f', argrepr='f', offset=36, starts_line=8, is_jump_target=False),
+ Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=38, starts_line=None, is_jump_target=False),
]
expected_opinfo_f = [
- Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=5, argrepr='5', offset=0, starts_line=3, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=6, argrepr='6', offset=3, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_CLOSURE', opcode=135, arg=2, argval='a', argrepr='a', offset=6, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_CLOSURE', opcode=135, arg=3, argval='b', argrepr='b', offset=9, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='c', argrepr='c', offset=12, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='d', argrepr='d', offset=15, starts_line=None, is_jump_target=False),
- Instruction(opname='BUILD_TUPLE', opcode=102, arg=4, argval=4, argrepr='', offset=18, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=code_object_inner, argrepr=repr(code_object_inner), offset=21, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='outer.<locals>.f.<locals>.inner', argrepr="'outer.<locals>.f.<locals>.inner'", offset=24, starts_line=None, is_jump_target=False),
- Instruction(opname='MAKE_CLOSURE', opcode=134, arg=2, argval=2, argrepr='', offset=27, starts_line=None, is_jump_target=False),
- Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='inner', argrepr='inner', offset=30, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=33, starts_line=5, is_jump_target=False),
- Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='a', argrepr='a', offset=36, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='b', argrepr='b', offset=39, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='c', argrepr='c', offset=42, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='d', argrepr='d', offset=45, starts_line=None, is_jump_target=False),
- Instruction(opname='CALL_FUNCTION', opcode=131, arg=4, argval=4, argrepr='4 positional, 0 keyword pair', offset=48, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=51, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='inner', argrepr='inner', offset=52, starts_line=6, is_jump_target=False),
- Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=55, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=(5, 6), argrepr='(5, 6)', offset=0, starts_line=3, is_jump_target=False),
+ Instruction(opname='LOAD_CLOSURE', opcode=135, arg=2, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_CLOSURE', opcode=135, arg=3, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='c', argrepr='c', offset=6, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='d', argrepr='d', offset=8, starts_line=None, is_jump_target=False),
+ Instruction(opname='BUILD_TUPLE', opcode=102, arg=4, argval=4, argrepr='', offset=10, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=code_object_inner, argrepr=repr(code_object_inner), offset=12, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='outer.<locals>.f.<locals>.inner', argrepr="'outer.<locals>.f.<locals>.inner'", offset=14, starts_line=None, is_jump_target=False),
+ Instruction(opname='MAKE_FUNCTION', opcode=132, arg=9, argval=9, argrepr='', offset=16, starts_line=None, is_jump_target=False),
+ Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='inner', argrepr='inner', offset=18, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=20, starts_line=5, is_jump_target=False),
+ Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='a', argrepr='a', offset=22, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='b', argrepr='b', offset=24, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='c', argrepr='c', offset=26, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='d', argrepr='d', offset=28, starts_line=None, is_jump_target=False),
+ Instruction(opname='CALL_FUNCTION', opcode=131, arg=4, argval=4, argrepr='', offset=30, starts_line=None, is_jump_target=False),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=32, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='inner', argrepr='inner', offset=34, starts_line=6, is_jump_target=False),
+ Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=36, starts_line=None, is_jump_target=False),
]
expected_opinfo_inner = [
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=0, starts_line=4, is_jump_target=False),
- Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=3, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=6, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='c', argrepr='c', offset=9, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='d', argrepr='d', offset=12, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='e', argrepr='e', offset=15, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_FAST', opcode=124, arg=1, argval='f', argrepr='f', offset=18, starts_line=None, is_jump_target=False),
- Instruction(opname='CALL_FUNCTION', opcode=131, arg=6, argval=6, argrepr='6 positional, 0 keyword pair', offset=21, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=24, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=25, starts_line=None, is_jump_target=False),
- Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=28, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='c', argrepr='c', offset=6, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='d', argrepr='d', offset=8, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='e', argrepr='e', offset=10, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_FAST', opcode=124, arg=1, argval='f', argrepr='f', offset=12, starts_line=None, is_jump_target=False),
+ Instruction(opname='CALL_FUNCTION', opcode=131, arg=6, argval=6, argrepr='', offset=14, starts_line=None, is_jump_target=False),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=16, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=18, starts_line=None, is_jump_target=False),
+ Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=20, starts_line=None, is_jump_target=False),
]
expected_opinfo_jumpy = [
- Instruction(opname='SETUP_LOOP', opcode=120, arg=68, argval=71, argrepr='to 71', offset=0, starts_line=3, is_jump_target=False),
- Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='range', argrepr='range', offset=3, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=10, argrepr='10', offset=6, starts_line=None, is_jump_target=False),
- Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=9, starts_line=None, is_jump_target=False),
- Instruction(opname='GET_ITER', opcode=68, arg=None, argval=None, argrepr='', offset=12, starts_line=None, is_jump_target=False),
- Instruction(opname='FOR_ITER', opcode=93, arg=44, argval=60, argrepr='to 60', offset=13, starts_line=None, is_jump_target=True),
- Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=16, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=19, starts_line=4, is_jump_target=False),
- Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=22, starts_line=None, is_jump_target=False),
- Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=25, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=28, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=29, starts_line=5, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=32, starts_line=None, is_jump_target=False),
- Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=35, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=44, argval=44, argrepr='', offset=38, starts_line=None, is_jump_target=False),
- Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=13, argval=13, argrepr='', offset=41, starts_line=6, is_jump_target=False),
- Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=44, starts_line=7, is_jump_target=True),
- Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=47, starts_line=None, is_jump_target=False),
- Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=50, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=13, argval=13, argrepr='', offset=53, starts_line=None, is_jump_target=False),
- Instruction(opname='BREAK_LOOP', opcode=80, arg=None, argval=None, argrepr='', offset=56, starts_line=8, is_jump_target=False),
- Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=13, argval=13, argrepr='', offset=57, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=60, starts_line=None, is_jump_target=True),
- Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=61, starts_line=10, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='I can haz else clause?', argrepr="'I can haz else clause?'", offset=64, starts_line=None, is_jump_target=False),
- Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=67, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=70, starts_line=None, is_jump_target=False),
- Instruction(opname='SETUP_LOOP', opcode=120, arg=68, argval=142, argrepr='to 142', offset=71, starts_line=11, is_jump_target=True),
- Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=74, starts_line=None, is_jump_target=True),
- Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=131, argval=131, argrepr='', offset=77, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=80, starts_line=12, is_jump_target=False),
- Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=83, starts_line=None, is_jump_target=False),
- Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=86, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=89, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=90, starts_line=13, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=93, starts_line=None, is_jump_target=False),
- Instruction(opname='INPLACE_SUBTRACT', opcode=56, arg=None, argval=None, argrepr='', offset=96, starts_line=None, is_jump_target=False),
- Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=97, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=100, starts_line=14, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=103, starts_line=None, is_jump_target=False),
- Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=106, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=115, argval=115, argrepr='', offset=109, starts_line=None, is_jump_target=False),
- Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=74, argval=74, argrepr='', offset=112, starts_line=15, is_jump_target=False),
- Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=115, starts_line=16, is_jump_target=True),
- Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=118, starts_line=None, is_jump_target=False),
- Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=121, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=74, argval=74, argrepr='', offset=124, starts_line=None, is_jump_target=False),
- Instruction(opname='BREAK_LOOP', opcode=80, arg=None, argval=None, argrepr='', offset=127, starts_line=17, is_jump_target=False),
- Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=74, argval=74, argrepr='', offset=128, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=131, starts_line=None, is_jump_target=True),
- Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=132, starts_line=19, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval='Who let lolcatz into this test suite?', argrepr="'Who let lolcatz into this test suite?'", offset=135, starts_line=None, is_jump_target=False),
- Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=138, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=141, starts_line=None, is_jump_target=False),
- Instruction(opname='SETUP_FINALLY', opcode=122, arg=73, argval=218, argrepr='to 218', offset=142, starts_line=20, is_jump_target=True),
- Instruction(opname='SETUP_EXCEPT', opcode=121, arg=12, argval=160, argrepr='to 160', offset=145, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=148, starts_line=21, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval=0, argrepr='0', offset=151, starts_line=None, is_jump_target=False),
- Instruction(opname='BINARY_TRUE_DIVIDE', opcode=27, arg=None, argval=None, argrepr='', offset=154, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=155, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=156, starts_line=None, is_jump_target=False),
- Instruction(opname='JUMP_FORWARD', opcode=110, arg=28, argval=188, argrepr='to 188', offset=157, starts_line=None, is_jump_target=False),
- Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=160, starts_line=22, is_jump_target=True),
- Instruction(opname='LOAD_GLOBAL', opcode=116, arg=2, argval='ZeroDivisionError', argrepr='ZeroDivisionError', offset=161, starts_line=None, is_jump_target=False),
- Instruction(opname='COMPARE_OP', opcode=107, arg=10, argval='exception match', argrepr='exception match', offset=164, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=187, argval=187, argrepr='', offset=167, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=170, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=171, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=172, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=173, starts_line=23, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=8, argval='Here we go, here we go, here we go...', argrepr="'Here we go, here we go, here we go...'", offset=176, starts_line=None, is_jump_target=False),
- Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=179, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=182, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=183, starts_line=None, is_jump_target=False),
- Instruction(opname='JUMP_FORWARD', opcode=110, arg=27, argval=214, argrepr='to 214', offset=184, starts_line=None, is_jump_target=False),
- Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=187, starts_line=None, is_jump_target=True),
- Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=188, starts_line=25, is_jump_target=True),
- Instruction(opname='SETUP_WITH', opcode=143, arg=17, argval=211, argrepr='to 211', offset=191, starts_line=None, is_jump_target=False),
- Instruction(opname='STORE_FAST', opcode=125, arg=1, argval='dodgy', argrepr='dodgy', offset=194, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=197, starts_line=26, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=9, argval='Never reach this', argrepr="'Never reach this'", offset=200, starts_line=None, is_jump_target=False),
- Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=203, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=206, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=207, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=208, starts_line=None, is_jump_target=False),
- Instruction(opname='WITH_CLEANUP_START', opcode=81, arg=None, argval=None, argrepr='', offset=211, starts_line=None, is_jump_target=True),
- Instruction(opname='WITH_CLEANUP_FINISH', opcode=82, arg=None, argval=None, argrepr='', offset=212, starts_line=None, is_jump_target=False),
- Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=213, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=214, starts_line=None, is_jump_target=True),
- Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=215, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=218, starts_line=28, is_jump_target=True),
- Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=221, starts_line=None, is_jump_target=False),
- Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=224, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=227, starts_line=None, is_jump_target=False),
- Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=228, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=229, starts_line=None, is_jump_target=False),
- Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=232, starts_line=None, is_jump_target=False),
+ Instruction(opname='SETUP_LOOP', opcode=120, arg=52, argval=54, argrepr='to 54', offset=0, starts_line=3, is_jump_target=False),
+ Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='range', argrepr='range', offset=2, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=10, argrepr='10', offset=4, starts_line=None, is_jump_target=False),
+ Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=6, starts_line=None, is_jump_target=False),
+ Instruction(opname='GET_ITER', opcode=68, arg=None, argval=None, argrepr='', offset=8, starts_line=None, is_jump_target=False),
+ Instruction(opname='FOR_ITER', opcode=93, arg=32, argval=44, argrepr='to 44', offset=10, starts_line=None, is_jump_target=True),
+ Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=12, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=14, starts_line=4, is_jump_target=False),
+ Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=16, starts_line=None, is_jump_target=False),
+ Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=18, starts_line=None, is_jump_target=False),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=20, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=22, starts_line=5, is_jump_target=False),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=24, starts_line=None, is_jump_target=False),
+ Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=26, starts_line=None, is_jump_target=False),
+ Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=32, argval=32, argrepr='', offset=28, starts_line=None, is_jump_target=False),
+ Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=10, argval=10, argrepr='', offset=30, starts_line=6, is_jump_target=False),
+ Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=32, starts_line=7, is_jump_target=True),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=34, starts_line=None, is_jump_target=False),
+ Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=36, starts_line=None, is_jump_target=False),
+ Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=10, argval=10, argrepr='', offset=38, starts_line=None, is_jump_target=False),
+ Instruction(opname='BREAK_LOOP', opcode=80, arg=None, argval=None, argrepr='', offset=40, starts_line=8, is_jump_target=False),
+ Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=10, argval=10, argrepr='', offset=42, starts_line=None, is_jump_target=False),
+ Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=44, starts_line=None, is_jump_target=True),
+ Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=46, starts_line=10, is_jump_target=False),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='I can haz else clause?', argrepr="'I can haz else clause?'", offset=48, starts_line=None, is_jump_target=False),
+ Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=50, starts_line=None, is_jump_target=False),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=52, starts_line=None, is_jump_target=False),
+ Instruction(opname='SETUP_LOOP', opcode=120, arg=52, argval=108, argrepr='to 108', offset=54, starts_line=11, is_jump_target=True),
+ Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=56, starts_line=None, is_jump_target=True),
+ Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=98, argval=98, argrepr='', offset=58, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=60, starts_line=12, is_jump_target=False),
+ Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=62, starts_line=None, is_jump_target=False),
+ Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=64, starts_line=None, is_jump_target=False),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=66, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=68, starts_line=13, is_jump_target=False),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=70, starts_line=None, is_jump_target=False),
+ Instruction(opname='INPLACE_SUBTRACT', opcode=56, arg=None, argval=None, argrepr='', offset=72, starts_line=None, is_jump_target=False),
+ Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=74, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=76, starts_line=14, is_jump_target=False),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=78, starts_line=None, is_jump_target=False),
+ Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=80, starts_line=None, is_jump_target=False),
+ Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=86, argval=86, argrepr='', offset=82, starts_line=None, is_jump_target=False),
+ Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=56, argval=56, argrepr='', offset=84, starts_line=15, is_jump_target=False),
+ Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=86, starts_line=16, is_jump_target=True),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=88, starts_line=None, is_jump_target=False),
+ Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=90, starts_line=None, is_jump_target=False),
+ Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=56, argval=56, argrepr='', offset=92, starts_line=None, is_jump_target=False),
+ Instruction(opname='BREAK_LOOP', opcode=80, arg=None, argval=None, argrepr='', offset=94, starts_line=17, is_jump_target=False),
+ Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=56, argval=56, argrepr='', offset=96, starts_line=None, is_jump_target=False),
+ Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=98, starts_line=None, is_jump_target=True),
+ Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=100, starts_line=19, is_jump_target=False),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval='Who let lolcatz into this test suite?', argrepr="'Who let lolcatz into this test suite?'", offset=102, starts_line=None, is_jump_target=False),
+ Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=104, starts_line=None, is_jump_target=False),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=106, starts_line=None, is_jump_target=False),
+ Instruction(opname='SETUP_FINALLY', opcode=122, arg=70, argval=180, argrepr='to 180', offset=108, starts_line=20, is_jump_target=True),
+ Instruction(opname='SETUP_EXCEPT', opcode=121, arg=12, argval=124, argrepr='to 124', offset=110, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=112, starts_line=21, is_jump_target=False),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval=0, argrepr='0', offset=114, starts_line=None, is_jump_target=False),
+ Instruction(opname='BINARY_TRUE_DIVIDE', opcode=27, arg=None, argval=None, argrepr='', offset=116, starts_line=None, is_jump_target=False),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=118, starts_line=None, is_jump_target=False),
+ Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=120, starts_line=None, is_jump_target=False),
+ Instruction(opname='JUMP_FORWARD', opcode=110, arg=28, argval=152, argrepr='to 152', offset=122, starts_line=None, is_jump_target=False),
+ Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=124, starts_line=22, is_jump_target=True),
+ Instruction(opname='LOAD_GLOBAL', opcode=116, arg=2, argval='ZeroDivisionError', argrepr='ZeroDivisionError', offset=126, starts_line=None, is_jump_target=False),
+ Instruction(opname='COMPARE_OP', opcode=107, arg=10, argval='exception match', argrepr='exception match', offset=128, starts_line=None, is_jump_target=False),
+ Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=150, argval=150, argrepr='', offset=130, starts_line=None, is_jump_target=False),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=132, starts_line=None, is_jump_target=False),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=134, starts_line=None, is_jump_target=False),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=136, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=138, starts_line=23, is_jump_target=False),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=8, argval='Here we go, here we go, here we go...', argrepr="'Here we go, here we go, here we go...'", offset=140, starts_line=None, is_jump_target=False),
+ Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=142, starts_line=None, is_jump_target=False),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=144, starts_line=None, is_jump_target=False),
+ Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=146, starts_line=None, is_jump_target=False),
+ Instruction(opname='JUMP_FORWARD', opcode=110, arg=26, argval=176, argrepr='to 176', offset=148, starts_line=None, is_jump_target=False),
+ Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=150, starts_line=None, is_jump_target=True),
+ Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=152, starts_line=25, is_jump_target=True),
+ Instruction(opname='SETUP_WITH', opcode=143, arg=14, argval=170, argrepr='to 170', offset=154, starts_line=None, is_jump_target=False),
+ Instruction(opname='STORE_FAST', opcode=125, arg=1, argval='dodgy', argrepr='dodgy', offset=156, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=158, starts_line=26, is_jump_target=False),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=9, argval='Never reach this', argrepr="'Never reach this'", offset=160, starts_line=None, is_jump_target=False),
+ Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=162, starts_line=None, is_jump_target=False),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=164, starts_line=None, is_jump_target=False),
+ Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=166, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=168, starts_line=None, is_jump_target=False),
+ Instruction(opname='WITH_CLEANUP_START', opcode=81, arg=None, argval=None, argrepr='', offset=170, starts_line=None, is_jump_target=True),
+ Instruction(opname='WITH_CLEANUP_FINISH', opcode=82, arg=None, argval=None, argrepr='', offset=172, starts_line=None, is_jump_target=False),
+ Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=174, starts_line=None, is_jump_target=False),
+ Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=176, starts_line=None, is_jump_target=True),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=178, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=180, starts_line=28, is_jump_target=True),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=182, starts_line=None, is_jump_target=False),
+ Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=184, starts_line=None, is_jump_target=False),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=186, starts_line=None, is_jump_target=False),
+ Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=188, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=190, starts_line=None, is_jump_target=False),
+ Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=192, starts_line=None, is_jump_target=False),
]
# One last piece of inspect fodder to check the default line number handling
def simple(): pass
expected_opinfo_simple = [
Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=0, starts_line=simple.__code__.co_firstlineno, is_jump_target=False),
- Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=3, starts_line=None, is_jump_target=False)
+ Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=2, starts_line=None, is_jump_target=False)
]
diff --git a/Lib/test/test_doctest.py b/Lib/test/test_doctest.py
index 33163b9..2258c6b 100644
--- a/Lib/test/test_doctest.py
+++ b/Lib/test/test_doctest.py
@@ -291,7 +291,7 @@ constructor:
...
... Non-example text.
...
- ... >>> print('another\example')
+ ... >>> print('another\\example')
... another
... example
... '''
@@ -1876,7 +1876,6 @@ if not hasattr(sys, 'gettrace') or not sys.gettrace():
To demonstrate this, we'll create a fake standard input that
captures our debugger input:
- >>> import tempfile
>>> real_stdin = sys.stdin
>>> sys.stdin = _FakeInput([
... 'print(x)', # print data defined by the example
@@ -1917,7 +1916,7 @@ if not hasattr(sys, 'gettrace') or not sys.gettrace():
... finally:
... sys.stdin = real_stdin
--Return--
- > <doctest test.test_doctest.test_pdb_set_trace[8]>(3)calls_set_trace()->None
+ > <doctest test.test_doctest.test_pdb_set_trace[7]>(3)calls_set_trace()->None
-> import pdb; pdb.set_trace()
(Pdb) print(y)
2
@@ -2798,7 +2797,6 @@ text files).
... _ = f.write(" 'abc def'\n")
... _ = f.write("\n")
... _ = f.write(' \"\"\"\n')
- ... import shutil
... rc1, out1, err1 = script_helper.assert_python_failure(
... '-m', 'doctest', fn, fn2)
... rc2, out2, err2 = script_helper.assert_python_ok(
@@ -2942,12 +2940,11 @@ Invalid doctest option:
def test_main():
# Check the doctest cases in doctest itself:
ret = support.run_doctest(doctest, verbosity=True)
+
# Check the doctest cases defined here:
from test import test_doctest
support.run_doctest(test_doctest, verbosity=True)
-import sys, re, io
-
def test_coverage(coverdir):
trace = support.import_module('trace')
tracer = trace.Trace(ignoredirs=[sys.base_prefix, sys.base_exec_prefix,],
diff --git a/Lib/test/test_dtrace.py b/Lib/test/test_dtrace.py
new file mode 100644
index 0000000..47a5010
--- /dev/null
+++ b/Lib/test/test_dtrace.py
@@ -0,0 +1,178 @@
+import dis
+import os.path
+import re
+import subprocess
+import sys
+import types
+import unittest
+
+from test.support import findfile, run_unittest
+
+
+def abspath(filename):
+ return os.path.abspath(findfile(filename, subdir="dtracedata"))
+
+
+def normalize_trace_output(output):
+ """Normalize DTrace output for comparison.
+
+ DTrace keeps a per-CPU buffer, and when showing the fired probes, buffers
+ are concatenated. So if the operating system moves our thread around, the
+ straight result can be "non-causal". So we add timestamps to the probe
+ firing, sort by that field, then strip it from the output"""
+
+ # When compiling with '--with-pydebug', strip '[# refs]' debug output.
+ output = re.sub(r"\[[0-9]+ refs\]", "", output)
+ try:
+ result = [
+ row.split("\t")
+ for row in output.splitlines()
+ if row and not row.startswith('#')
+ ]
+ result.sort(key=lambda row: int(row[0]))
+ result = [row[1] for row in result]
+ return "\n".join(result)
+ except (IndexError, ValueError):
+ raise AssertionError(
+ "tracer produced unparseable output:\n{}".format(output)
+ )
+
+
+class TraceBackend:
+ EXTENSION = None
+ COMMAND = None
+ COMMAND_ARGS = []
+
+ def run_case(self, name, optimize_python=None):
+ actual_output = normalize_trace_output(self.trace_python(
+ script_file=abspath(name + self.EXTENSION),
+ python_file=abspath(name + ".py"),
+ optimize_python=optimize_python))
+
+ with open(abspath(name + self.EXTENSION + ".expected")) as f:
+ expected_output = f.read().rstrip()
+
+ return (expected_output, actual_output)
+
+ def generate_trace_command(self, script_file, subcommand=None):
+ command = self.COMMAND + [script_file]
+ if subcommand:
+ command += ["-c", subcommand]
+ return command
+
+ def trace(self, script_file, subcommand=None):
+ command = self.generate_trace_command(script_file, subcommand)
+ stdout, _ = subprocess.Popen(command,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.STDOUT,
+ universal_newlines=True).communicate()
+ return stdout
+
+ def trace_python(self, script_file, python_file, optimize_python=None):
+ python_flags = []
+ if optimize_python:
+ python_flags.extend(["-O"] * optimize_python)
+ subcommand = " ".join([sys.executable] + python_flags + [python_file])
+ return self.trace(script_file, subcommand)
+
+ def assert_usable(self):
+ try:
+ output = self.trace(abspath("assert_usable" + self.EXTENSION))
+ output = output.strip()
+ except (FileNotFoundError, PermissionError) as fnfe:
+ output = str(fnfe)
+ if output != "probe: success":
+ raise unittest.SkipTest(
+ "{}(1) failed: {}".format(self.COMMAND[0], output)
+ )
+
+
+class DTraceBackend(TraceBackend):
+ EXTENSION = ".d"
+ COMMAND = ["dtrace", "-q", "-s"]
+
+
+class SystemTapBackend(TraceBackend):
+ EXTENSION = ".stp"
+ COMMAND = ["stap", "-g"]
+
+
+class TraceTests(unittest.TestCase):
+ # unittest.TestCase options
+ maxDiff = None
+
+ # TraceTests options
+ backend = None
+ optimize_python = 0
+
+ @classmethod
+ def setUpClass(self):
+ self.backend.assert_usable()
+
+ def run_case(self, name):
+ actual_output, expected_output = self.backend.run_case(
+ name, optimize_python=self.optimize_python)
+ self.assertEqual(actual_output, expected_output)
+
+ def test_function_entry_return(self):
+ self.run_case("call_stack")
+
+ def test_verify_call_opcodes(self):
+ """Ensure our call stack test hits all function call opcodes"""
+
+ opcodes = set(["CALL_FUNCTION", "CALL_FUNCTION_EX", "CALL_FUNCTION_KW"])
+
+ with open(abspath("call_stack.py")) as f:
+ code_string = f.read()
+
+ def get_function_instructions(funcname):
+ # Recompile with appropriate optimization setting
+ code = compile(source=code_string,
+ filename="<string>",
+ mode="exec",
+ optimize=self.optimize_python)
+
+ for c in code.co_consts:
+ if isinstance(c, types.CodeType) and c.co_name == funcname:
+ return dis.get_instructions(c)
+ return []
+
+ for instruction in get_function_instructions('start'):
+ opcodes.discard(instruction.opname)
+
+ self.assertEqual(set(), opcodes)
+
+ def test_gc(self):
+ self.run_case("gc")
+
+ def test_line(self):
+ self.run_case("line")
+
+
+class DTraceNormalTests(TraceTests):
+ backend = DTraceBackend()
+ optimize_python = 0
+
+
+class DTraceOptimizedTests(TraceTests):
+ backend = DTraceBackend()
+ optimize_python = 2
+
+
+class SystemTapNormalTests(TraceTests):
+ backend = SystemTapBackend()
+ optimize_python = 0
+
+
+class SystemTapOptimizedTests(TraceTests):
+ backend = SystemTapBackend()
+ optimize_python = 2
+
+
+def test_main():
+ run_unittest(DTraceNormalTests, DTraceOptimizedTests, SystemTapNormalTests,
+ SystemTapOptimizedTests)
+
+
+if __name__ == '__main__':
+ test_main()
diff --git a/Lib/test/test_dynamic.py b/Lib/test/test_dynamic.py
index 5080ec9..3ae090f 100644
--- a/Lib/test/test_dynamic.py
+++ b/Lib/test/test_dynamic.py
@@ -1,7 +1,6 @@
# Test the most dynamic corner cases of Python's runtime semantics.
import builtins
-import contextlib
import unittest
from test.support import swap_item, swap_attr
diff --git a/Lib/test/test_eintr.py b/Lib/test/test_eintr.py
index aabad83..25f86d3 100644
--- a/Lib/test/test_eintr.py
+++ b/Lib/test/test_eintr.py
@@ -1,7 +1,5 @@
import os
import signal
-import subprocess
-import sys
import unittest
from test import support
@@ -16,14 +14,8 @@ class EINTRTests(unittest.TestCase):
# Run the tester in a sub-process, to make sure there is only one
# thread (for reliable signal delivery).
tester = support.findfile("eintr_tester.py", subdir="eintrdata")
-
- if support.verbose:
- args = [sys.executable, tester]
- with subprocess.Popen(args) as proc:
- exitcode = proc.wait()
- self.assertEqual(exitcode, 0)
- else:
- script_helper.assert_python_ok(tester)
+ # use -u to try to get the full output if the test hangs or crash
+ script_helper.assert_python_ok("-u", tester)
if __name__ == "__main__":
diff --git a/Lib/test/test_email/__init__.py b/Lib/test/test_email/__init__.py
index d4a03ee..888751e 100644
--- a/Lib/test/test_email/__init__.py
+++ b/Lib/test/test_email/__init__.py
@@ -1,5 +1,4 @@
import os
-import sys
import unittest
import collections
import email
diff --git a/Lib/test/test_email/test__header_value_parser.py b/Lib/test/test_email/test__header_value_parser.py
index f7ac0e3..26ece69 100644
--- a/Lib/test/test_email/test__header_value_parser.py
+++ b/Lib/test/test_email/test__header_value_parser.py
@@ -581,7 +581,7 @@ class TestParser(TestParserMixin, TestEmailBase):
def test_get_comment_quoted_parens(self):
self._test_get_x(parser.get_comment,
- '(foo\) \(\)bar)', '(foo\) \(\)bar)', ' ', [], '', ['foo) ()bar'])
+ r'(foo\) \(\)bar)', r'(foo\) \(\)bar)', ' ', [], '', ['foo) ()bar'])
def test_get_comment_non_printable(self):
self._test_get_x(parser.get_comment,
@@ -625,7 +625,7 @@ class TestParser(TestParserMixin, TestEmailBase):
def test_get_comment_qs_in_nested_comment(self):
comment = self._test_get_x(parser.get_comment,
- '(foo (b\)))', '(foo (b\)))', ' ', [], '', ['foo (b\))'])
+ r'(foo (b\)))', r'(foo (b\)))', ' ', [], '', [r'foo (b\))'])
self.assertEqual(comment[2].content, 'b)')
# get_cfws
diff --git a/Lib/test/test_email/test_asian_codecs.py b/Lib/test/test_email/test_asian_codecs.py
index f9cd7d9..1e0caee 100644
--- a/Lib/test/test_email/test_asian_codecs.py
+++ b/Lib/test/test_email/test_asian_codecs.py
@@ -3,7 +3,6 @@
# email package unit tests for (optional) Asian codecs
import unittest
-from test.support import run_unittest
from test.test_email import TestEmailBase
from email.charset import Charset
diff --git a/Lib/test/test_email/test_email.py b/Lib/test/test_email/test_email.py
index 9b86a2a..f97ccc6 100644
--- a/Lib/test/test_email/test_email.py
+++ b/Lib/test/test_email/test_email.py
@@ -32,6 +32,7 @@ from email.mime.image import MIMEImage
from email.mime.base import MIMEBase
from email.mime.message import MIMEMessage
from email.mime.multipart import MIMEMultipart
+from email.mime.nonmultipart import MIMENonMultipart
from email import utils
from email import errors
from email import encoders
@@ -2078,7 +2079,13 @@ YXNkZg==
--===============0012394164==--""")
self.assertEqual(m.get_payload(0).get_payload(), 'YXNkZg==')
+ def test_mimebase_default_policy(self):
+ m = MIMEBase('multipart', 'mixed')
+ self.assertIs(m.policy, email.policy.compat32)
+ def test_mimebase_custom_policy(self):
+ m = MIMEBase('multipart', 'mixed', policy=email.policy.default)
+ self.assertIs(m.policy, email.policy.default)
# Test some badly formatted messages
class TestNonConformant(TestEmailBase):
@@ -2680,6 +2687,19 @@ message 2
msg = MIMEMultipart()
self.assertTrue(msg.is_multipart())
+ def test_multipart_default_policy(self):
+ msg = MIMEMultipart()
+ msg['To'] = 'a@b.com'
+ msg['To'] = 'c@d.com'
+ self.assertEqual(msg.get_all('to'), ['a@b.com', 'c@d.com'])
+
+ def test_multipart_custom_policy(self):
+ msg = MIMEMultipart(policy=email.policy.default)
+ msg['To'] = 'a@b.com'
+ with self.assertRaises(ValueError) as cm:
+ msg['To'] = 'c@d.com'
+ self.assertEqual(str(cm.exception),
+ 'There may be at most 1 To headers in a message')
# A general test of parser->model->generator idempotency. IOW, read a message
# in, parse it into a message object tree, then without touching the tree,
@@ -3033,7 +3053,7 @@ class TestMiscellaneous(TestEmailBase):
def test_escape_backslashes(self):
self.assertEqual(
- utils.formataddr(('Arthur \Backslash\ Foobar', 'person@dom.ain')),
+ utils.formataddr((r'Arthur \Backslash\ Foobar', 'person@dom.ain')),
r'"Arthur \\Backslash\\ Foobar" <person@dom.ain>')
a = r'Arthur \Backslash\ Foobar'
b = 'person@dom.ain'
@@ -3340,6 +3360,27 @@ multipart/report
g.flatten(msg, linesep='\r\n')
self.assertEqual(s.getvalue(), msgtxt)
+ def test_mime_classes_policy_argument(self):
+ with openfile('audiotest.au', 'rb') as fp:
+ audiodata = fp.read()
+ with openfile('PyBanner048.gif', 'rb') as fp:
+ bindata = fp.read()
+ classes = [
+ (MIMEApplication, ('',)),
+ (MIMEAudio, (audiodata,)),
+ (MIMEImage, (bindata,)),
+ (MIMEMessage, (Message(),)),
+ (MIMENonMultipart, ('multipart', 'mixed')),
+ (MIMEText, ('',)),
+ ]
+ for cls, constructor in classes:
+ with self.subTest(cls=cls.__name__, policy='compat32'):
+ m = cls(*constructor)
+ self.assertIs(m.policy, email.policy.compat32)
+ with self.subTest(cls=cls.__name__, policy='default'):
+ m = cls(*constructor, policy=email.policy.default)
+ self.assertIs(m.policy, email.policy.default)
+
# Test the iterator/generators
class TestIterators(TestEmailBase):
@@ -3448,7 +3489,6 @@ Do you like this message?
class TestFeedParsers(TestEmailBase):
def parse(self, chunks):
- from email.feedparser import FeedParser
feedparser = FeedParser()
for chunk in chunks:
feedparser.feed(chunk)
diff --git a/Lib/test/test_email/test_generator.py b/Lib/test/test_email/test_generator.py
index b1cbce2..7c8877f 100644
--- a/Lib/test/test_email/test_generator.py
+++ b/Lib/test/test_email/test_generator.py
@@ -143,7 +143,7 @@ class TestGeneratorBase:
def test_set_mangle_from_via_policy(self):
source = textwrap.dedent("""\
Subject: test that
- from is mangeld in the body!
+ from is mangled in the body!
From time to time I write a rhyme.
""")
diff --git a/Lib/test/test_email/test_headerregistry.py b/Lib/test/test_email/test_headerregistry.py
index 55ecdea..af836dc 100644
--- a/Lib/test/test_email/test_headerregistry.py
+++ b/Lib/test/test_email/test_headerregistry.py
@@ -1,7 +1,6 @@
import datetime
import textwrap
import unittest
-import types
from email import errors
from email import policy
from email.message import Message
diff --git a/Lib/test/test_email/test_message.py b/Lib/test/test_email/test_message.py
index 4345162..f3a57df 100644
--- a/Lib/test/test_email/test_message.py
+++ b/Lib/test/test_email/test_message.py
@@ -764,6 +764,26 @@ class TestEmailMessage(TestEmailMessageBase, TestEmailBase):
m.set_content(content_manager=cm)
self.assertEqual(m['MIME-Version'], '1.0')
+ def test_as_string_uses_max_header_length_by_default(self):
+ m = self._str_msg('Subject: long line' + ' ab'*50 + '\n\n')
+ self.assertEqual(len(m.as_string().strip().splitlines()), 3)
+
+ def test_as_string_allows_maxheaderlen(self):
+ m = self._str_msg('Subject: long line' + ' ab'*50 + '\n\n')
+ self.assertEqual(len(m.as_string(maxheaderlen=0).strip().splitlines()),
+ 1)
+ self.assertEqual(len(m.as_string(maxheaderlen=34).strip().splitlines()),
+ 6)
+
+ def test_str_defaults_to_policy_max_line_length(self):
+ m = self._str_msg('Subject: long line' + ' ab'*50 + '\n\n')
+ self.assertEqual(len(str(m).strip().splitlines()), 3)
+
+ def test_str_defaults_to_utf8(self):
+ m = EmailMessage()
+ m['Subject'] = 'unicöde'
+ self.assertEqual(str(m), 'Subject: unicöde\n\n')
+
class TestMIMEPart(TestEmailMessageBase, TestEmailBase):
# Doing the full test run here may seem a bit redundant, since the two
diff --git a/Lib/test/test_email/test_parser.py b/Lib/test/test_email/test_parser.py
index 8ddc1763..06c8640 100644
--- a/Lib/test/test_email/test_parser.py
+++ b/Lib/test/test_email/test_parser.py
@@ -1,7 +1,7 @@
import io
import email
import unittest
-from email.message import Message
+from email.message import Message, EmailMessage
from email.policy import default
from test.test_email import TestEmailBase
@@ -39,38 +39,71 @@ class TestParserBase:
# The unicode line splitter splits on unicode linebreaks, which are
# more numerous than allowed by the email RFCs; make sure we are only
# splitting on those two.
- msg = self.parser(
- "Next-Line: not\x85broken\r\n"
- "Null: not\x00broken\r\n"
- "Vertical-Tab: not\vbroken\r\n"
- "Form-Feed: not\fbroken\r\n"
- "File-Separator: not\x1Cbroken\r\n"
- "Group-Separator: not\x1Dbroken\r\n"
- "Record-Separator: not\x1Ebroken\r\n"
- "Line-Separator: not\u2028broken\r\n"
- "Paragraph-Separator: not\u2029broken\r\n"
- "\r\n",
- policy=default,
- )
- self.assertEqual(msg.items(), [
- ("Next-Line", "not\x85broken"),
- ("Null", "not\x00broken"),
- ("Vertical-Tab", "not\vbroken"),
- ("Form-Feed", "not\fbroken"),
- ("File-Separator", "not\x1Cbroken"),
- ("Group-Separator", "not\x1Dbroken"),
- ("Record-Separator", "not\x1Ebroken"),
- ("Line-Separator", "not\u2028broken"),
- ("Paragraph-Separator", "not\u2029broken"),
- ])
- self.assertEqual(msg.get_payload(), "")
+ for parser in self.parsers:
+ with self.subTest(parser=parser.__name__):
+ msg = parser(
+ "Next-Line: not\x85broken\r\n"
+ "Null: not\x00broken\r\n"
+ "Vertical-Tab: not\vbroken\r\n"
+ "Form-Feed: not\fbroken\r\n"
+ "File-Separator: not\x1Cbroken\r\n"
+ "Group-Separator: not\x1Dbroken\r\n"
+ "Record-Separator: not\x1Ebroken\r\n"
+ "Line-Separator: not\u2028broken\r\n"
+ "Paragraph-Separator: not\u2029broken\r\n"
+ "\r\n",
+ policy=default,
+ )
+ self.assertEqual(msg.items(), [
+ ("Next-Line", "not\x85broken"),
+ ("Null", "not\x00broken"),
+ ("Vertical-Tab", "not\vbroken"),
+ ("Form-Feed", "not\fbroken"),
+ ("File-Separator", "not\x1Cbroken"),
+ ("Group-Separator", "not\x1Dbroken"),
+ ("Record-Separator", "not\x1Ebroken"),
+ ("Line-Separator", "not\u2028broken"),
+ ("Paragraph-Separator", "not\u2029broken"),
+ ])
+ self.assertEqual(msg.get_payload(), "")
+
+ class MyMessage(EmailMessage):
+ pass
+
+ def test_custom_message_factory_on_policy(self):
+ for parser in self.parsers:
+ with self.subTest(parser=parser.__name__):
+ MyPolicy = default.clone(message_factory=self.MyMessage)
+ msg = parser("To: foo\n\ntest", policy=MyPolicy)
+ self.assertIsInstance(msg, self.MyMessage)
+
+ def test_factory_arg_overrides_policy(self):
+ for parser in self.parsers:
+ with self.subTest(parser=parser.__name__):
+ MyPolicy = default.clone(message_factory=self.MyMessage)
+ msg = parser("To: foo\n\ntest", Message, policy=MyPolicy)
+ self.assertNotIsInstance(msg, self.MyMessage)
+ self.assertIsInstance(msg, Message)
+
+# Play some games to get nice output in subTest. This code could be clearer
+# if staticmethod supported __name__.
+
+def message_from_file(s, *args, **kw):
+ f = io.StringIO(s)
+ return email.message_from_file(f, *args, **kw)
class TestParser(TestParserBase, TestEmailBase):
- parser = staticmethod(email.message_from_string)
+ parsers = (email.message_from_string, message_from_file)
+
+def message_from_bytes(s, *args, **kw):
+ return email.message_from_bytes(s.encode(), *args, **kw)
+
+def message_from_binary_file(s, *args, **kw):
+ f = io.BytesIO(s.encode())
+ return email.message_from_binary_file(f, *args, **kw)
class TestBytesParser(TestParserBase, TestEmailBase):
- def parser(self, s, *args, **kw):
- return email.message_from_bytes(s.encode(), *args, **kw)
+ parsers = (message_from_bytes, message_from_binary_file)
if __name__ == '__main__':
diff --git a/Lib/test/test_email/test_policy.py b/Lib/test/test_email/test_policy.py
index 70ac4db..8fecb8a 100644
--- a/Lib/test/test_email/test_policy.py
+++ b/Lib/test/test_email/test_policy.py
@@ -5,6 +5,7 @@ import unittest
import email.policy
import email.parser
import email.generator
+import email.message
from email import headerregistry
def make_defaults(base_defaults, differences):
@@ -23,6 +24,7 @@ class PolicyAPITests(unittest.TestCase):
'cte_type': '8bit',
'raise_on_defect': False,
'mangle_from_': True,
+ 'message_factory': None,
}
# These default values are the ones set on email.policy.default.
# If any of these defaults change, the docs must be updated.
@@ -34,6 +36,7 @@ class PolicyAPITests(unittest.TestCase):
'refold_source': 'long',
'content_manager': email.policy.EmailPolicy.content_manager,
'mangle_from_': False,
+ 'message_factory': email.message.EmailMessage,
})
# For each policy under test, we give here what we expect the defaults to
@@ -62,20 +65,22 @@ class PolicyAPITests(unittest.TestCase):
def test_defaults(self):
for policy, expected in self.policies.items():
for attr, value in expected.items():
- self.assertEqual(getattr(policy, attr), value,
- ("change {} docs/docstrings if defaults have "
- "changed").format(policy))
+ with self.subTest(policy=policy, attr=attr):
+ self.assertEqual(getattr(policy, attr), value,
+ ("change {} docs/docstrings if defaults have "
+ "changed").format(policy))
def test_all_attributes_covered(self):
for policy, expected in self.policies.items():
for attr in dir(policy):
- if (attr.startswith('_') or
- isinstance(getattr(email.policy.EmailPolicy, attr),
- types.FunctionType)):
- continue
- else:
- self.assertIn(attr, expected,
- "{} is not fully tested".format(attr))
+ with self.subTest(policy=policy, attr=attr):
+ if (attr.startswith('_') or
+ isinstance(getattr(email.policy.EmailPolicy, attr),
+ types.FunctionType)):
+ continue
+ else:
+ self.assertIn(attr, expected,
+ "{} is not fully tested".format(attr))
def test_abc(self):
with self.assertRaises(TypeError) as cm:
@@ -237,6 +242,9 @@ class PolicyAPITests(unittest.TestCase):
# wins), but that the order still works (right overrides left).
+class TestException(Exception):
+ pass
+
class TestPolicyPropagation(unittest.TestCase):
# The abstract methods are used by the parser but not by the wrapper
@@ -244,40 +252,40 @@ class TestPolicyPropagation(unittest.TestCase):
# policy was actually propagated all the way to feedparser.
class MyPolicy(email.policy.Policy):
def badmethod(self, *args, **kw):
- raise Exception("test")
+ raise TestException("test")
fold = fold_binary = header_fetch_parser = badmethod
header_source_parse = header_store_parse = badmethod
def test_message_from_string(self):
- with self.assertRaisesRegex(Exception, "^test$"):
+ with self.assertRaisesRegex(TestException, "^test$"):
email.message_from_string("Subject: test\n\n",
policy=self.MyPolicy)
def test_message_from_bytes(self):
- with self.assertRaisesRegex(Exception, "^test$"):
+ with self.assertRaisesRegex(TestException, "^test$"):
email.message_from_bytes(b"Subject: test\n\n",
policy=self.MyPolicy)
def test_message_from_file(self):
f = io.StringIO('Subject: test\n\n')
- with self.assertRaisesRegex(Exception, "^test$"):
+ with self.assertRaisesRegex(TestException, "^test$"):
email.message_from_file(f, policy=self.MyPolicy)
def test_message_from_binary_file(self):
f = io.BytesIO(b'Subject: test\n\n')
- with self.assertRaisesRegex(Exception, "^test$"):
+ with self.assertRaisesRegex(TestException, "^test$"):
email.message_from_binary_file(f, policy=self.MyPolicy)
# These are redundant, but we need them for black-box completeness.
def test_parser(self):
p = email.parser.Parser(policy=self.MyPolicy)
- with self.assertRaisesRegex(Exception, "^test$"):
+ with self.assertRaisesRegex(TestException, "^test$"):
p.parsestr('Subject: test\n\n')
def test_bytes_parser(self):
p = email.parser.BytesParser(policy=self.MyPolicy)
- with self.assertRaisesRegex(Exception, "^test$"):
+ with self.assertRaisesRegex(TestException, "^test$"):
p.parsebytes(b'Subject: test\n\n')
# Now that we've established that all the parse methods get the
diff --git a/Lib/test/test_ensurepip.py b/Lib/test/test_ensurepip.py
index a78ca14..9b04c18 100644
--- a/Lib/test/test_ensurepip.py
+++ b/Lib/test/test_ensurepip.py
@@ -9,19 +9,6 @@ import sys
import ensurepip
import ensurepip._uninstall
-# pip currently requires ssl support, so we ensure we handle
-# it being missing (http://bugs.python.org/issue19744)
-ensurepip_no_ssl = test.support.import_fresh_module("ensurepip",
- blocked=["ssl"])
-try:
- import ssl
-except ImportError:
- def requires_usable_pip(f):
- deco = unittest.skip(ensurepip._MISSING_SSL_MESSAGE)
- return deco(f)
-else:
- def requires_usable_pip(f):
- return f
class TestEnsurePipVersion(unittest.TestCase):
@@ -47,7 +34,6 @@ class EnsurepipMixin:
class TestBootstrap(EnsurepipMixin, unittest.TestCase):
- @requires_usable_pip
def test_basic_bootstrapping(self):
ensurepip.bootstrap()
@@ -62,7 +48,6 @@ class TestBootstrap(EnsurepipMixin, unittest.TestCase):
additional_paths = self.run_pip.call_args[0][1]
self.assertEqual(len(additional_paths), 2)
- @requires_usable_pip
def test_bootstrapping_with_root(self):
ensurepip.bootstrap(root="/foo/bar/")
@@ -75,7 +60,6 @@ class TestBootstrap(EnsurepipMixin, unittest.TestCase):
unittest.mock.ANY,
)
- @requires_usable_pip
def test_bootstrapping_with_user(self):
ensurepip.bootstrap(user=True)
@@ -87,7 +71,6 @@ class TestBootstrap(EnsurepipMixin, unittest.TestCase):
unittest.mock.ANY,
)
- @requires_usable_pip
def test_bootstrapping_with_upgrade(self):
ensurepip.bootstrap(upgrade=True)
@@ -99,7 +82,6 @@ class TestBootstrap(EnsurepipMixin, unittest.TestCase):
unittest.mock.ANY,
)
- @requires_usable_pip
def test_bootstrapping_with_verbosity_1(self):
ensurepip.bootstrap(verbosity=1)
@@ -111,7 +93,6 @@ class TestBootstrap(EnsurepipMixin, unittest.TestCase):
unittest.mock.ANY,
)
- @requires_usable_pip
def test_bootstrapping_with_verbosity_2(self):
ensurepip.bootstrap(verbosity=2)
@@ -123,7 +104,6 @@ class TestBootstrap(EnsurepipMixin, unittest.TestCase):
unittest.mock.ANY,
)
- @requires_usable_pip
def test_bootstrapping_with_verbosity_3(self):
ensurepip.bootstrap(verbosity=3)
@@ -135,17 +115,14 @@ class TestBootstrap(EnsurepipMixin, unittest.TestCase):
unittest.mock.ANY,
)
- @requires_usable_pip
def test_bootstrapping_with_regular_install(self):
ensurepip.bootstrap()
self.assertEqual(self.os_environ["ENSUREPIP_OPTIONS"], "install")
- @requires_usable_pip
def test_bootstrapping_with_alt_install(self):
ensurepip.bootstrap(altinstall=True)
self.assertEqual(self.os_environ["ENSUREPIP_OPTIONS"], "altinstall")
- @requires_usable_pip
def test_bootstrapping_with_default_pip(self):
ensurepip.bootstrap(default_pip=True)
self.assertNotIn("ENSUREPIP_OPTIONS", self.os_environ)
@@ -155,7 +132,6 @@ class TestBootstrap(EnsurepipMixin, unittest.TestCase):
ensurepip.bootstrap(altinstall=True, default_pip=True)
self.assertFalse(self.run_pip.called)
- @requires_usable_pip
def test_pip_environment_variables_removed(self):
# ensurepip deliberately ignores all pip environment variables
# See http://bugs.python.org/issue19734 for details
@@ -163,7 +139,6 @@ class TestBootstrap(EnsurepipMixin, unittest.TestCase):
ensurepip.bootstrap()
self.assertNotIn("PIP_THIS_SHOULD_GO_AWAY", self.os_environ)
- @requires_usable_pip
def test_pip_config_file_disabled(self):
# ensurepip deliberately ignores the pip config file
# See http://bugs.python.org/issue20053 for details
@@ -205,7 +180,6 @@ class TestUninstall(EnsurepipMixin, unittest.TestCase):
self.assertFalse(self.run_pip.called)
- @requires_usable_pip
def test_uninstall(self):
with fake_pip():
ensurepip._uninstall_helper()
@@ -217,7 +191,6 @@ class TestUninstall(EnsurepipMixin, unittest.TestCase):
]
)
- @requires_usable_pip
def test_uninstall_with_verbosity_1(self):
with fake_pip():
ensurepip._uninstall_helper(verbosity=1)
@@ -229,7 +202,6 @@ class TestUninstall(EnsurepipMixin, unittest.TestCase):
]
)
- @requires_usable_pip
def test_uninstall_with_verbosity_2(self):
with fake_pip():
ensurepip._uninstall_helper(verbosity=2)
@@ -241,7 +213,6 @@ class TestUninstall(EnsurepipMixin, unittest.TestCase):
]
)
- @requires_usable_pip
def test_uninstall_with_verbosity_3(self):
with fake_pip():
ensurepip._uninstall_helper(verbosity=3)
@@ -253,7 +224,6 @@ class TestUninstall(EnsurepipMixin, unittest.TestCase):
]
)
- @requires_usable_pip
def test_pip_environment_variables_removed(self):
# ensurepip deliberately ignores all pip environment variables
# See http://bugs.python.org/issue19734 for details
@@ -262,7 +232,6 @@ class TestUninstall(EnsurepipMixin, unittest.TestCase):
ensurepip._uninstall_helper()
self.assertNotIn("PIP_THIS_SHOULD_GO_AWAY", self.os_environ)
- @requires_usable_pip
def test_pip_config_file_disabled(self):
# ensurepip deliberately ignores the pip config file
# See http://bugs.python.org/issue20053 for details
@@ -271,44 +240,12 @@ class TestUninstall(EnsurepipMixin, unittest.TestCase):
self.assertEqual(self.os_environ["PIP_CONFIG_FILE"], os.devnull)
-class TestMissingSSL(EnsurepipMixin, unittest.TestCase):
-
- def setUp(self):
- sys.modules["ensurepip"] = ensurepip_no_ssl
- @self.addCleanup
- def restore_module():
- sys.modules["ensurepip"] = ensurepip
- super().setUp()
-
- def test_bootstrap_requires_ssl(self):
- self.os_environ["PIP_THIS_SHOULD_STAY"] = "test fodder"
- with self.assertRaisesRegex(RuntimeError, "requires SSL/TLS"):
- ensurepip_no_ssl.bootstrap()
- self.assertFalse(self.run_pip.called)
- self.assertIn("PIP_THIS_SHOULD_STAY", self.os_environ)
-
- def test_uninstall_requires_ssl(self):
- self.os_environ["PIP_THIS_SHOULD_STAY"] = "test fodder"
- with self.assertRaisesRegex(RuntimeError, "requires SSL/TLS"):
- with fake_pip():
- ensurepip_no_ssl._uninstall_helper()
- self.assertFalse(self.run_pip.called)
- self.assertIn("PIP_THIS_SHOULD_STAY", self.os_environ)
-
- def test_main_exits_early_with_warning(self):
- with test.support.captured_stderr() as stderr:
- ensurepip_no_ssl._main(["--version"])
- warning = stderr.getvalue().strip()
- self.assertTrue(warning.endswith("requires SSL/TLS"), warning)
- self.assertFalse(self.run_pip.called)
-
# Basic testing of the main functions and their argument parsing
EXPECTED_VERSION_OUTPUT = "pip " + ensurepip._PIP_VERSION
class TestBootstrappingMainFunction(EnsurepipMixin, unittest.TestCase):
- @requires_usable_pip
def test_bootstrap_version(self):
with test.support.captured_stdout() as stdout:
with self.assertRaises(SystemExit):
@@ -317,7 +254,6 @@ class TestBootstrappingMainFunction(EnsurepipMixin, unittest.TestCase):
self.assertEqual(result, EXPECTED_VERSION_OUTPUT)
self.assertFalse(self.run_pip.called)
- @requires_usable_pip
def test_basic_bootstrapping(self):
ensurepip._main([])
@@ -342,7 +278,6 @@ class TestUninstallationMainFunction(EnsurepipMixin, unittest.TestCase):
self.assertEqual(result, EXPECTED_VERSION_OUTPUT)
self.assertFalse(self.run_pip.called)
- @requires_usable_pip
def test_basic_uninstall(self):
with fake_pip():
ensurepip._uninstall._main([])
diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py
index 630b155..e97ef94 100644
--- a/Lib/test/test_enum.py
+++ b/Lib/test/test_enum.py
@@ -3,9 +3,10 @@ import inspect
import pydoc
import unittest
from collections import OrderedDict
-from enum import Enum, IntEnum, EnumMeta, unique
+from enum import Enum, IntEnum, EnumMeta, Flag, IntFlag, unique, auto
from io import StringIO
from pickle import dumps, loads, PicklingError, HIGHEST_PROTOCOL
+from test import support
# for pickle tests
try:
@@ -32,6 +33,14 @@ try:
except Exception as exc:
FloatStooges = exc
+try:
+ class FlagStooges(Flag):
+ LARRY = 1
+ CURLY = 2
+ MOE = 3
+except Exception as exc:
+ FlagStooges = exc
+
# for pickle test and subclass tests
try:
class StrEnum(str, Enum):
@@ -60,9 +69,9 @@ except Exception as exc:
# for doctests
try:
class Fruit(Enum):
- tomato = 1
- banana = 2
- cherry = 3
+ TOMATO = 1
+ BANANA = 2
+ CHERRY = 3
except Exception:
pass
@@ -104,6 +113,7 @@ class TestHelpers(unittest.TestCase):
'__', '___', '____', '_____',):
self.assertFalse(enum._is_dunder(s))
+# tests
class TestEnum(unittest.TestCase):
@@ -283,6 +293,28 @@ class TestEnum(unittest.TestCase):
class Wrong(Enum):
_any_name_ = 9
+ def test_bool(self):
+ # plain Enum members are always True
+ class Logic(Enum):
+ true = True
+ false = False
+ self.assertTrue(Logic.true)
+ self.assertTrue(Logic.false)
+ # unless overridden
+ class RealLogic(Enum):
+ true = True
+ false = False
+ def __bool__(self):
+ return bool(self._value_)
+ self.assertTrue(RealLogic.true)
+ self.assertFalse(RealLogic.false)
+ # mixed Enums depend on mixed-in type
+ class IntLogic(int, Enum):
+ true = 1
+ false = 0
+ self.assertTrue(IntLogic.true)
+ self.assertFalse(IntLogic.false)
+
def test_contains(self):
Season = self.Season
self.assertIn(Season.AUTUMN, Season)
@@ -1547,6 +1579,701 @@ class TestEnum(unittest.TestCase):
self.assertEqual(LabelledList.unprocessed, 1)
self.assertEqual(LabelledList(1), LabelledList.unprocessed)
+ def test_auto_number(self):
+ class Color(Enum):
+ red = auto()
+ blue = auto()
+ green = auto()
+
+ self.assertEqual(list(Color), [Color.red, Color.blue, Color.green])
+ self.assertEqual(Color.red.value, 1)
+ self.assertEqual(Color.blue.value, 2)
+ self.assertEqual(Color.green.value, 3)
+
+ def test_auto_name(self):
+ class Color(Enum):
+ def _generate_next_value_(name, start, count, last):
+ return name
+ red = auto()
+ blue = auto()
+ green = auto()
+
+ self.assertEqual(list(Color), [Color.red, Color.blue, Color.green])
+ self.assertEqual(Color.red.value, 'red')
+ self.assertEqual(Color.blue.value, 'blue')
+ self.assertEqual(Color.green.value, 'green')
+
+ def test_auto_name_inherit(self):
+ class AutoNameEnum(Enum):
+ def _generate_next_value_(name, start, count, last):
+ return name
+ class Color(AutoNameEnum):
+ red = auto()
+ blue = auto()
+ green = auto()
+
+ self.assertEqual(list(Color), [Color.red, Color.blue, Color.green])
+ self.assertEqual(Color.red.value, 'red')
+ self.assertEqual(Color.blue.value, 'blue')
+ self.assertEqual(Color.green.value, 'green')
+
+ def test_auto_garbage(self):
+ class Color(Enum):
+ red = 'red'
+ blue = auto()
+ self.assertEqual(Color.blue.value, 1)
+
+ def test_auto_garbage_corrected(self):
+ class Color(Enum):
+ red = 'red'
+ blue = 2
+ green = auto()
+
+ self.assertEqual(list(Color), [Color.red, Color.blue, Color.green])
+ self.assertEqual(Color.red.value, 'red')
+ self.assertEqual(Color.blue.value, 2)
+ self.assertEqual(Color.green.value, 3)
+
+ def test_duplicate_auto(self):
+ class Dupes(Enum):
+ first = primero = auto()
+ second = auto()
+ third = auto()
+ self.assertEqual([Dupes.first, Dupes.second, Dupes.third], list(Dupes))
+
+
+class TestOrder(unittest.TestCase):
+
+ def test_same_members(self):
+ class Color(Enum):
+ _order_ = 'red green blue'
+ red = 1
+ green = 2
+ blue = 3
+
+ def test_same_members_with_aliases(self):
+ class Color(Enum):
+ _order_ = 'red green blue'
+ red = 1
+ green = 2
+ blue = 3
+ verde = green
+
+ def test_same_members_wrong_order(self):
+ with self.assertRaisesRegex(TypeError, 'member order does not match _order_'):
+ class Color(Enum):
+ _order_ = 'red green blue'
+ red = 1
+ blue = 3
+ green = 2
+
+ def test_order_has_extra_members(self):
+ with self.assertRaisesRegex(TypeError, 'member order does not match _order_'):
+ class Color(Enum):
+ _order_ = 'red green blue purple'
+ red = 1
+ green = 2
+ blue = 3
+
+ def test_order_has_extra_members_with_aliases(self):
+ with self.assertRaisesRegex(TypeError, 'member order does not match _order_'):
+ class Color(Enum):
+ _order_ = 'red green blue purple'
+ red = 1
+ green = 2
+ blue = 3
+ verde = green
+
+ def test_enum_has_extra_members(self):
+ with self.assertRaisesRegex(TypeError, 'member order does not match _order_'):
+ class Color(Enum):
+ _order_ = 'red green blue'
+ red = 1
+ green = 2
+ blue = 3
+ purple = 4
+
+ def test_enum_has_extra_members_with_aliases(self):
+ with self.assertRaisesRegex(TypeError, 'member order does not match _order_'):
+ class Color(Enum):
+ _order_ = 'red green blue'
+ red = 1
+ green = 2
+ blue = 3
+ purple = 4
+ verde = green
+
+
+class TestFlag(unittest.TestCase):
+ """Tests of the Flags."""
+
+ class Perm(Flag):
+ R, W, X = 4, 2, 1
+
+ class Open(Flag):
+ RO = 0
+ WO = 1
+ RW = 2
+ AC = 3
+ CE = 1<<19
+
+ def test_str(self):
+ Perm = self.Perm
+ self.assertEqual(str(Perm.R), 'Perm.R')
+ self.assertEqual(str(Perm.W), 'Perm.W')
+ self.assertEqual(str(Perm.X), 'Perm.X')
+ self.assertEqual(str(Perm.R | Perm.W), 'Perm.R|W')
+ self.assertEqual(str(Perm.R | Perm.W | Perm.X), 'Perm.R|W|X')
+ self.assertEqual(str(Perm(0)), 'Perm.0')
+ self.assertEqual(str(~Perm.R), 'Perm.W|X')
+ self.assertEqual(str(~Perm.W), 'Perm.R|X')
+ self.assertEqual(str(~Perm.X), 'Perm.R|W')
+ self.assertEqual(str(~(Perm.R | Perm.W)), 'Perm.X')
+ self.assertEqual(str(~(Perm.R | Perm.W | Perm.X)), 'Perm.0')
+ self.assertEqual(str(Perm(~0)), 'Perm.R|W|X')
+
+ Open = self.Open
+ self.assertEqual(str(Open.RO), 'Open.RO')
+ self.assertEqual(str(Open.WO), 'Open.WO')
+ self.assertEqual(str(Open.AC), 'Open.AC')
+ self.assertEqual(str(Open.RO | Open.CE), 'Open.CE')
+ self.assertEqual(str(Open.WO | Open.CE), 'Open.CE|WO')
+ self.assertEqual(str(~Open.RO), 'Open.CE|AC|RW|WO')
+ self.assertEqual(str(~Open.WO), 'Open.CE|RW')
+ self.assertEqual(str(~Open.AC), 'Open.CE')
+ self.assertEqual(str(~(Open.RO | Open.CE)), 'Open.AC')
+ self.assertEqual(str(~(Open.WO | Open.CE)), 'Open.RW')
+
+ def test_repr(self):
+ Perm = self.Perm
+ self.assertEqual(repr(Perm.R), '<Perm.R: 4>')
+ self.assertEqual(repr(Perm.W), '<Perm.W: 2>')
+ self.assertEqual(repr(Perm.X), '<Perm.X: 1>')
+ self.assertEqual(repr(Perm.R | Perm.W), '<Perm.R|W: 6>')
+ self.assertEqual(repr(Perm.R | Perm.W | Perm.X), '<Perm.R|W|X: 7>')
+ self.assertEqual(repr(Perm(0)), '<Perm.0: 0>')
+ self.assertEqual(repr(~Perm.R), '<Perm.W|X: 3>')
+ self.assertEqual(repr(~Perm.W), '<Perm.R|X: 5>')
+ self.assertEqual(repr(~Perm.X), '<Perm.R|W: 6>')
+ self.assertEqual(repr(~(Perm.R | Perm.W)), '<Perm.X: 1>')
+ self.assertEqual(repr(~(Perm.R | Perm.W | Perm.X)), '<Perm.0: 0>')
+ self.assertEqual(repr(Perm(~0)), '<Perm.R|W|X: 7>')
+
+ Open = self.Open
+ self.assertEqual(repr(Open.RO), '<Open.RO: 0>')
+ self.assertEqual(repr(Open.WO), '<Open.WO: 1>')
+ self.assertEqual(repr(Open.AC), '<Open.AC: 3>')
+ self.assertEqual(repr(Open.RO | Open.CE), '<Open.CE: 524288>')
+ self.assertEqual(repr(Open.WO | Open.CE), '<Open.CE|WO: 524289>')
+ self.assertEqual(repr(~Open.RO), '<Open.CE|AC|RW|WO: 524291>')
+ self.assertEqual(repr(~Open.WO), '<Open.CE|RW: 524290>')
+ self.assertEqual(repr(~Open.AC), '<Open.CE: 524288>')
+ self.assertEqual(repr(~(Open.RO | Open.CE)), '<Open.AC: 3>')
+ self.assertEqual(repr(~(Open.WO | Open.CE)), '<Open.RW: 2>')
+
+ def test_or(self):
+ Perm = self.Perm
+ for i in Perm:
+ for j in Perm:
+ self.assertEqual((i | j), Perm(i.value | j.value))
+ self.assertEqual((i | j).value, i.value | j.value)
+ self.assertIs(type(i | j), Perm)
+ for i in Perm:
+ self.assertIs(i | i, i)
+ Open = self.Open
+ self.assertIs(Open.RO | Open.CE, Open.CE)
+
+ def test_and(self):
+ Perm = self.Perm
+ RW = Perm.R | Perm.W
+ RX = Perm.R | Perm.X
+ WX = Perm.W | Perm.X
+ RWX = Perm.R | Perm.W | Perm.X
+ values = list(Perm) + [RW, RX, WX, RWX, Perm(0)]
+ for i in values:
+ for j in values:
+ self.assertEqual((i & j).value, i.value & j.value)
+ self.assertIs(type(i & j), Perm)
+ for i in Perm:
+ self.assertIs(i & i, i)
+ self.assertIs(i & RWX, i)
+ self.assertIs(RWX & i, i)
+ Open = self.Open
+ self.assertIs(Open.RO & Open.CE, Open.RO)
+
+ def test_xor(self):
+ Perm = self.Perm
+ for i in Perm:
+ for j in Perm:
+ self.assertEqual((i ^ j).value, i.value ^ j.value)
+ self.assertIs(type(i ^ j), Perm)
+ for i in Perm:
+ self.assertIs(i ^ Perm(0), i)
+ self.assertIs(Perm(0) ^ i, i)
+ Open = self.Open
+ self.assertIs(Open.RO ^ Open.CE, Open.CE)
+ self.assertIs(Open.CE ^ Open.CE, Open.RO)
+
+ def test_invert(self):
+ Perm = self.Perm
+ RW = Perm.R | Perm.W
+ RX = Perm.R | Perm.X
+ WX = Perm.W | Perm.X
+ RWX = Perm.R | Perm.W | Perm.X
+ values = list(Perm) + [RW, RX, WX, RWX, Perm(0)]
+ for i in values:
+ self.assertIs(type(~i), Perm)
+ self.assertEqual(~~i, i)
+ for i in Perm:
+ self.assertIs(~~i, i)
+ Open = self.Open
+ self.assertIs(Open.WO & ~Open.WO, Open.RO)
+ self.assertIs((Open.WO|Open.CE) & ~Open.WO, Open.CE)
+
+ def test_bool(self):
+ Perm = self.Perm
+ for f in Perm:
+ self.assertTrue(f)
+ Open = self.Open
+ for f in Open:
+ self.assertEqual(bool(f.value), bool(f))
+
+ def test_programatic_function_string(self):
+ Perm = Flag('Perm', 'R W X')
+ lst = list(Perm)
+ self.assertEqual(len(lst), len(Perm))
+ self.assertEqual(len(Perm), 3, Perm)
+ self.assertEqual(lst, [Perm.R, Perm.W, Perm.X])
+ for i, n in enumerate('R W X'.split()):
+ v = 1<<i
+ e = Perm(v)
+ self.assertEqual(e.value, v)
+ self.assertEqual(type(e.value), int)
+ self.assertEqual(e.name, n)
+ self.assertIn(e, Perm)
+ self.assertIs(type(e), Perm)
+
+ def test_programatic_function_string_with_start(self):
+ Perm = Flag('Perm', 'R W X', start=8)
+ lst = list(Perm)
+ self.assertEqual(len(lst), len(Perm))
+ self.assertEqual(len(Perm), 3, Perm)
+ self.assertEqual(lst, [Perm.R, Perm.W, Perm.X])
+ for i, n in enumerate('R W X'.split()):
+ v = 8<<i
+ e = Perm(v)
+ self.assertEqual(e.value, v)
+ self.assertEqual(type(e.value), int)
+ self.assertEqual(e.name, n)
+ self.assertIn(e, Perm)
+ self.assertIs(type(e), Perm)
+
+ def test_programatic_function_string_list(self):
+ Perm = Flag('Perm', ['R', 'W', 'X'])
+ lst = list(Perm)
+ self.assertEqual(len(lst), len(Perm))
+ self.assertEqual(len(Perm), 3, Perm)
+ self.assertEqual(lst, [Perm.R, Perm.W, Perm.X])
+ for i, n in enumerate('R W X'.split()):
+ v = 1<<i
+ e = Perm(v)
+ self.assertEqual(e.value, v)
+ self.assertEqual(type(e.value), int)
+ self.assertEqual(e.name, n)
+ self.assertIn(e, Perm)
+ self.assertIs(type(e), Perm)
+
+ def test_programatic_function_iterable(self):
+ Perm = Flag('Perm', (('R', 2), ('W', 8), ('X', 32)))
+ lst = list(Perm)
+ self.assertEqual(len(lst), len(Perm))
+ self.assertEqual(len(Perm), 3, Perm)
+ self.assertEqual(lst, [Perm.R, Perm.W, Perm.X])
+ for i, n in enumerate('R W X'.split()):
+ v = 1<<(2*i+1)
+ e = Perm(v)
+ self.assertEqual(e.value, v)
+ self.assertEqual(type(e.value), int)
+ self.assertEqual(e.name, n)
+ self.assertIn(e, Perm)
+ self.assertIs(type(e), Perm)
+
+ def test_programatic_function_from_dict(self):
+ Perm = Flag('Perm', OrderedDict((('R', 2), ('W', 8), ('X', 32))))
+ lst = list(Perm)
+ self.assertEqual(len(lst), len(Perm))
+ self.assertEqual(len(Perm), 3, Perm)
+ self.assertEqual(lst, [Perm.R, Perm.W, Perm.X])
+ for i, n in enumerate('R W X'.split()):
+ v = 1<<(2*i+1)
+ e = Perm(v)
+ self.assertEqual(e.value, v)
+ self.assertEqual(type(e.value), int)
+ self.assertEqual(e.name, n)
+ self.assertIn(e, Perm)
+ self.assertIs(type(e), Perm)
+
+ def test_pickle(self):
+ if isinstance(FlagStooges, Exception):
+ raise FlagStooges
+ test_pickle_dump_load(self.assertIs, FlagStooges.CURLY|FlagStooges.MOE)
+ test_pickle_dump_load(self.assertIs, FlagStooges)
+
+ def test_containment(self):
+ Perm = self.Perm
+ R, W, X = Perm
+ RW = R | W
+ RX = R | X
+ WX = W | X
+ RWX = R | W | X
+ self.assertTrue(R in RW)
+ self.assertTrue(R in RX)
+ self.assertTrue(R in RWX)
+ self.assertTrue(W in RW)
+ self.assertTrue(W in WX)
+ self.assertTrue(W in RWX)
+ self.assertTrue(X in RX)
+ self.assertTrue(X in WX)
+ self.assertTrue(X in RWX)
+ self.assertFalse(R in WX)
+ self.assertFalse(W in RX)
+ self.assertFalse(X in RW)
+
+ def test_auto_number(self):
+ class Color(Flag):
+ red = auto()
+ blue = auto()
+ green = auto()
+
+ self.assertEqual(list(Color), [Color.red, Color.blue, Color.green])
+ self.assertEqual(Color.red.value, 1)
+ self.assertEqual(Color.blue.value, 2)
+ self.assertEqual(Color.green.value, 4)
+
+ def test_auto_number_garbage(self):
+ with self.assertRaisesRegex(TypeError, 'Invalid Flag value: .not an int.'):
+ class Color(Flag):
+ red = 'not an int'
+ blue = auto()
+
+ def test_cascading_failure(self):
+ class Bizarre(Flag):
+ c = 3
+ d = 4
+ f = 6
+ # Bizarre.c | Bizarre.d
+ self.assertRaisesRegex(ValueError, "5 is not a valid Bizarre", Bizarre, 5)
+ self.assertRaisesRegex(ValueError, "5 is not a valid Bizarre", Bizarre, 5)
+ self.assertRaisesRegex(ValueError, "2 is not a valid Bizarre", Bizarre, 2)
+ self.assertRaisesRegex(ValueError, "2 is not a valid Bizarre", Bizarre, 2)
+ self.assertRaisesRegex(ValueError, "1 is not a valid Bizarre", Bizarre, 1)
+ self.assertRaisesRegex(ValueError, "1 is not a valid Bizarre", Bizarre, 1)
+
+ def test_duplicate_auto(self):
+ class Dupes(Enum):
+ first = primero = auto()
+ second = auto()
+ third = auto()
+ self.assertEqual([Dupes.first, Dupes.second, Dupes.third], list(Dupes))
+
+ def test_bizarre(self):
+ class Bizarre(Flag):
+ b = 3
+ c = 4
+ d = 6
+ self.assertEqual(repr(Bizarre(7)), '<Bizarre.d|c|b: 7>')
+
+
+class TestIntFlag(unittest.TestCase):
+ """Tests of the IntFlags."""
+
+ class Perm(IntFlag):
+ X = 1 << 0
+ W = 1 << 1
+ R = 1 << 2
+
+ class Open(IntFlag):
+ RO = 0
+ WO = 1
+ RW = 2
+ AC = 3
+ CE = 1<<19
+
+ def test_type(self):
+ Perm = self.Perm
+ Open = self.Open
+ for f in Perm:
+ self.assertTrue(isinstance(f, Perm))
+ self.assertEqual(f, f.value)
+ self.assertTrue(isinstance(Perm.W | Perm.X, Perm))
+ self.assertEqual(Perm.W | Perm.X, 3)
+ for f in Open:
+ self.assertTrue(isinstance(f, Open))
+ self.assertEqual(f, f.value)
+ self.assertTrue(isinstance(Open.WO | Open.RW, Open))
+ self.assertEqual(Open.WO | Open.RW, 3)
+
+
+ def test_str(self):
+ Perm = self.Perm
+ self.assertEqual(str(Perm.R), 'Perm.R')
+ self.assertEqual(str(Perm.W), 'Perm.W')
+ self.assertEqual(str(Perm.X), 'Perm.X')
+ self.assertEqual(str(Perm.R | Perm.W), 'Perm.R|W')
+ self.assertEqual(str(Perm.R | Perm.W | Perm.X), 'Perm.R|W|X')
+ self.assertEqual(str(Perm.R | 8), 'Perm.8|R')
+ self.assertEqual(str(Perm(0)), 'Perm.0')
+ self.assertEqual(str(Perm(8)), 'Perm.8')
+ self.assertEqual(str(~Perm.R), 'Perm.W|X')
+ self.assertEqual(str(~Perm.W), 'Perm.R|X')
+ self.assertEqual(str(~Perm.X), 'Perm.R|W')
+ self.assertEqual(str(~(Perm.R | Perm.W)), 'Perm.X')
+ self.assertEqual(str(~(Perm.R | Perm.W | Perm.X)), 'Perm.-8')
+ self.assertEqual(str(~(Perm.R | 8)), 'Perm.W|X')
+ self.assertEqual(str(Perm(~0)), 'Perm.R|W|X')
+ self.assertEqual(str(Perm(~8)), 'Perm.R|W|X')
+
+ Open = self.Open
+ self.assertEqual(str(Open.RO), 'Open.RO')
+ self.assertEqual(str(Open.WO), 'Open.WO')
+ self.assertEqual(str(Open.AC), 'Open.AC')
+ self.assertEqual(str(Open.RO | Open.CE), 'Open.CE')
+ self.assertEqual(str(Open.WO | Open.CE), 'Open.CE|WO')
+ self.assertEqual(str(Open(4)), 'Open.4')
+ self.assertEqual(str(~Open.RO), 'Open.CE|AC|RW|WO')
+ self.assertEqual(str(~Open.WO), 'Open.CE|RW')
+ self.assertEqual(str(~Open.AC), 'Open.CE')
+ self.assertEqual(str(~(Open.RO | Open.CE)), 'Open.AC|RW|WO')
+ self.assertEqual(str(~(Open.WO | Open.CE)), 'Open.RW')
+ self.assertEqual(str(Open(~4)), 'Open.CE|AC|RW|WO')
+
+ def test_repr(self):
+ Perm = self.Perm
+ self.assertEqual(repr(Perm.R), '<Perm.R: 4>')
+ self.assertEqual(repr(Perm.W), '<Perm.W: 2>')
+ self.assertEqual(repr(Perm.X), '<Perm.X: 1>')
+ self.assertEqual(repr(Perm.R | Perm.W), '<Perm.R|W: 6>')
+ self.assertEqual(repr(Perm.R | Perm.W | Perm.X), '<Perm.R|W|X: 7>')
+ self.assertEqual(repr(Perm.R | 8), '<Perm.8|R: 12>')
+ self.assertEqual(repr(Perm(0)), '<Perm.0: 0>')
+ self.assertEqual(repr(Perm(8)), '<Perm.8: 8>')
+ self.assertEqual(repr(~Perm.R), '<Perm.W|X: -5>')
+ self.assertEqual(repr(~Perm.W), '<Perm.R|X: -3>')
+ self.assertEqual(repr(~Perm.X), '<Perm.R|W: -2>')
+ self.assertEqual(repr(~(Perm.R | Perm.W)), '<Perm.X: -7>')
+ self.assertEqual(repr(~(Perm.R | Perm.W | Perm.X)), '<Perm.-8: -8>')
+ self.assertEqual(repr(~(Perm.R | 8)), '<Perm.W|X: -13>')
+ self.assertEqual(repr(Perm(~0)), '<Perm.R|W|X: -1>')
+ self.assertEqual(repr(Perm(~8)), '<Perm.R|W|X: -9>')
+
+ Open = self.Open
+ self.assertEqual(repr(Open.RO), '<Open.RO: 0>')
+ self.assertEqual(repr(Open.WO), '<Open.WO: 1>')
+ self.assertEqual(repr(Open.AC), '<Open.AC: 3>')
+ self.assertEqual(repr(Open.RO | Open.CE), '<Open.CE: 524288>')
+ self.assertEqual(repr(Open.WO | Open.CE), '<Open.CE|WO: 524289>')
+ self.assertEqual(repr(Open(4)), '<Open.4: 4>')
+ self.assertEqual(repr(~Open.RO), '<Open.CE|AC|RW|WO: -1>')
+ self.assertEqual(repr(~Open.WO), '<Open.CE|RW: -2>')
+ self.assertEqual(repr(~Open.AC), '<Open.CE: -4>')
+ self.assertEqual(repr(~(Open.RO | Open.CE)), '<Open.AC|RW|WO: -524289>')
+ self.assertEqual(repr(~(Open.WO | Open.CE)), '<Open.RW: -524290>')
+ self.assertEqual(repr(Open(~4)), '<Open.CE|AC|RW|WO: -5>')
+
+ def test_or(self):
+ Perm = self.Perm
+ for i in Perm:
+ for j in Perm:
+ self.assertEqual(i | j, i.value | j.value)
+ self.assertEqual((i | j).value, i.value | j.value)
+ self.assertIs(type(i | j), Perm)
+ for j in range(8):
+ self.assertEqual(i | j, i.value | j)
+ self.assertEqual((i | j).value, i.value | j)
+ self.assertIs(type(i | j), Perm)
+ self.assertEqual(j | i, j | i.value)
+ self.assertEqual((j | i).value, j | i.value)
+ self.assertIs(type(j | i), Perm)
+ for i in Perm:
+ self.assertIs(i | i, i)
+ self.assertIs(i | 0, i)
+ self.assertIs(0 | i, i)
+ Open = self.Open
+ self.assertIs(Open.RO | Open.CE, Open.CE)
+
+ def test_and(self):
+ Perm = self.Perm
+ RW = Perm.R | Perm.W
+ RX = Perm.R | Perm.X
+ WX = Perm.W | Perm.X
+ RWX = Perm.R | Perm.W | Perm.X
+ values = list(Perm) + [RW, RX, WX, RWX, Perm(0)]
+ for i in values:
+ for j in values:
+ self.assertEqual(i & j, i.value & j.value, 'i is %r, j is %r' % (i, j))
+ self.assertEqual((i & j).value, i.value & j.value, 'i is %r, j is %r' % (i, j))
+ self.assertIs(type(i & j), Perm, 'i is %r, j is %r' % (i, j))
+ for j in range(8):
+ self.assertEqual(i & j, i.value & j)
+ self.assertEqual((i & j).value, i.value & j)
+ self.assertIs(type(i & j), Perm)
+ self.assertEqual(j & i, j & i.value)
+ self.assertEqual((j & i).value, j & i.value)
+ self.assertIs(type(j & i), Perm)
+ for i in Perm:
+ self.assertIs(i & i, i)
+ self.assertIs(i & 7, i)
+ self.assertIs(7 & i, i)
+ Open = self.Open
+ self.assertIs(Open.RO & Open.CE, Open.RO)
+
+ def test_xor(self):
+ Perm = self.Perm
+ for i in Perm:
+ for j in Perm:
+ self.assertEqual(i ^ j, i.value ^ j.value)
+ self.assertEqual((i ^ j).value, i.value ^ j.value)
+ self.assertIs(type(i ^ j), Perm)
+ for j in range(8):
+ self.assertEqual(i ^ j, i.value ^ j)
+ self.assertEqual((i ^ j).value, i.value ^ j)
+ self.assertIs(type(i ^ j), Perm)
+ self.assertEqual(j ^ i, j ^ i.value)
+ self.assertEqual((j ^ i).value, j ^ i.value)
+ self.assertIs(type(j ^ i), Perm)
+ for i in Perm:
+ self.assertIs(i ^ 0, i)
+ self.assertIs(0 ^ i, i)
+ Open = self.Open
+ self.assertIs(Open.RO ^ Open.CE, Open.CE)
+ self.assertIs(Open.CE ^ Open.CE, Open.RO)
+
+ def test_invert(self):
+ Perm = self.Perm
+ RW = Perm.R | Perm.W
+ RX = Perm.R | Perm.X
+ WX = Perm.W | Perm.X
+ RWX = Perm.R | Perm.W | Perm.X
+ values = list(Perm) + [RW, RX, WX, RWX, Perm(0)]
+ for i in values:
+ self.assertEqual(~i, ~i.value)
+ self.assertEqual((~i).value, ~i.value)
+ self.assertIs(type(~i), Perm)
+ self.assertEqual(~~i, i)
+ for i in Perm:
+ self.assertIs(~~i, i)
+ Open = self.Open
+ self.assertIs(Open.WO & ~Open.WO, Open.RO)
+ self.assertIs((Open.WO|Open.CE) & ~Open.WO, Open.CE)
+
+ def test_programatic_function_string(self):
+ Perm = IntFlag('Perm', 'R W X')
+ lst = list(Perm)
+ self.assertEqual(len(lst), len(Perm))
+ self.assertEqual(len(Perm), 3, Perm)
+ self.assertEqual(lst, [Perm.R, Perm.W, Perm.X])
+ for i, n in enumerate('R W X'.split()):
+ v = 1<<i
+ e = Perm(v)
+ self.assertEqual(e.value, v)
+ self.assertEqual(type(e.value), int)
+ self.assertEqual(e, v)
+ self.assertEqual(e.name, n)
+ self.assertIn(e, Perm)
+ self.assertIs(type(e), Perm)
+
+ def test_programatic_function_string_with_start(self):
+ Perm = IntFlag('Perm', 'R W X', start=8)
+ lst = list(Perm)
+ self.assertEqual(len(lst), len(Perm))
+ self.assertEqual(len(Perm), 3, Perm)
+ self.assertEqual(lst, [Perm.R, Perm.W, Perm.X])
+ for i, n in enumerate('R W X'.split()):
+ v = 8<<i
+ e = Perm(v)
+ self.assertEqual(e.value, v)
+ self.assertEqual(type(e.value), int)
+ self.assertEqual(e, v)
+ self.assertEqual(e.name, n)
+ self.assertIn(e, Perm)
+ self.assertIs(type(e), Perm)
+
+ def test_programatic_function_string_list(self):
+ Perm = IntFlag('Perm', ['R', 'W', 'X'])
+ lst = list(Perm)
+ self.assertEqual(len(lst), len(Perm))
+ self.assertEqual(len(Perm), 3, Perm)
+ self.assertEqual(lst, [Perm.R, Perm.W, Perm.X])
+ for i, n in enumerate('R W X'.split()):
+ v = 1<<i
+ e = Perm(v)
+ self.assertEqual(e.value, v)
+ self.assertEqual(type(e.value), int)
+ self.assertEqual(e, v)
+ self.assertEqual(e.name, n)
+ self.assertIn(e, Perm)
+ self.assertIs(type(e), Perm)
+
+ def test_programatic_function_iterable(self):
+ Perm = IntFlag('Perm', (('R', 2), ('W', 8), ('X', 32)))
+ lst = list(Perm)
+ self.assertEqual(len(lst), len(Perm))
+ self.assertEqual(len(Perm), 3, Perm)
+ self.assertEqual(lst, [Perm.R, Perm.W, Perm.X])
+ for i, n in enumerate('R W X'.split()):
+ v = 1<<(2*i+1)
+ e = Perm(v)
+ self.assertEqual(e.value, v)
+ self.assertEqual(type(e.value), int)
+ self.assertEqual(e, v)
+ self.assertEqual(e.name, n)
+ self.assertIn(e, Perm)
+ self.assertIs(type(e), Perm)
+
+ def test_programatic_function_from_dict(self):
+ Perm = IntFlag('Perm', OrderedDict((('R', 2), ('W', 8), ('X', 32))))
+ lst = list(Perm)
+ self.assertEqual(len(lst), len(Perm))
+ self.assertEqual(len(Perm), 3, Perm)
+ self.assertEqual(lst, [Perm.R, Perm.W, Perm.X])
+ for i, n in enumerate('R W X'.split()):
+ v = 1<<(2*i+1)
+ e = Perm(v)
+ self.assertEqual(e.value, v)
+ self.assertEqual(type(e.value), int)
+ self.assertEqual(e, v)
+ self.assertEqual(e.name, n)
+ self.assertIn(e, Perm)
+ self.assertIs(type(e), Perm)
+
+
+ def test_containment(self):
+ Perm = self.Perm
+ R, W, X = Perm
+ RW = R | W
+ RX = R | X
+ WX = W | X
+ RWX = R | W | X
+ self.assertTrue(R in RW)
+ self.assertTrue(R in RX)
+ self.assertTrue(R in RWX)
+ self.assertTrue(W in RW)
+ self.assertTrue(W in WX)
+ self.assertTrue(W in RWX)
+ self.assertTrue(X in RX)
+ self.assertTrue(X in WX)
+ self.assertTrue(X in RWX)
+ self.assertFalse(R in WX)
+ self.assertFalse(W in RX)
+ self.assertFalse(X in RW)
+
+ def test_bool(self):
+ Perm = self.Perm
+ for f in Perm:
+ self.assertTrue(f)
+ Open = self.Open
+ for f in Open:
+ self.assertEqual(bool(f.value), bool(f))
class TestUnique(unittest.TestCase):
@@ -1739,5 +2466,47 @@ class TestStdLib(unittest.TestCase):
if failed:
self.fail("result does not equal expected, see print above")
+
+class MiscTestCase(unittest.TestCase):
+ def test__all__(self):
+ support.check__all__(self, enum)
+
+
+# These are unordered here on purpose to ensure that declaration order
+# makes no difference.
+CONVERT_TEST_NAME_D = 5
+CONVERT_TEST_NAME_C = 5
+CONVERT_TEST_NAME_B = 5
+CONVERT_TEST_NAME_A = 5 # This one should sort first.
+CONVERT_TEST_NAME_E = 5
+CONVERT_TEST_NAME_F = 5
+
+class TestIntEnumConvert(unittest.TestCase):
+ def test_convert_value_lookup_priority(self):
+ test_type = enum.IntEnum._convert(
+ 'UnittestConvert', 'test.test_enum',
+ filter=lambda x: x.startswith('CONVERT_TEST_'))
+ # We don't want the reverse lookup value to vary when there are
+ # multiple possible names for a given value. It should always
+ # report the first lexigraphical name in that case.
+ self.assertEqual(test_type(5).name, 'CONVERT_TEST_NAME_A')
+
+ def test_convert(self):
+ test_type = enum.IntEnum._convert(
+ 'UnittestConvert', 'test.test_enum',
+ filter=lambda x: x.startswith('CONVERT_TEST_'))
+ # Ensure that test_type has all of the desired names and values.
+ self.assertEqual(test_type.CONVERT_TEST_NAME_F,
+ test_type.CONVERT_TEST_NAME_A)
+ self.assertEqual(test_type.CONVERT_TEST_NAME_B, 5)
+ self.assertEqual(test_type.CONVERT_TEST_NAME_C, 5)
+ self.assertEqual(test_type.CONVERT_TEST_NAME_D, 5)
+ self.assertEqual(test_type.CONVERT_TEST_NAME_E, 5)
+ # Ensure that test_type only picked up names matching the filter.
+ self.assertEqual([name for name in dir(test_type)
+ if name[0:2] not in ('CO', '__')],
+ [], msg='Names other than CONVERT_TEST_* found.')
+
+
if __name__ == '__main__':
unittest.main()
diff --git a/Lib/test/test_enumerate.py b/Lib/test/test_enumerate.py
index 2630cf2..e455ade 100644
--- a/Lib/test/test_enumerate.py
+++ b/Lib/test/test_enumerate.py
@@ -223,7 +223,7 @@ class TestReversed(unittest.TestCase, PickleTest):
def test_objmethods(self):
# Objects must have __len__() and __getitem__() implemented.
class NoLen(object):
- def __getitem__(self): return 1
+ def __getitem__(self, i): return 1
nl = NoLen()
self.assertRaises(TypeError, reversed, nl)
@@ -232,6 +232,13 @@ class TestReversed(unittest.TestCase, PickleTest):
ngi = NoGetItem()
self.assertRaises(TypeError, reversed, ngi)
+ class Blocked(object):
+ def __getitem__(self, i): return 1
+ def __len__(self): return 2
+ __reversed__ = None
+ b = Blocked()
+ self.assertRaises(TypeError, reversed, b)
+
def test_pickle(self):
for data in 'abc', range(5), tuple(enumerate('abc')), range(1,17,5):
self.check_pickle(reversed(data), list(data)[::-1])
diff --git a/Lib/test/test_epoll.py b/Lib/test/test_epoll.py
index a7359e9..a7aff8a 100644
--- a/Lib/test/test_epoll.py
+++ b/Lib/test/test_epoll.py
@@ -28,7 +28,6 @@ import socket
import time
import unittest
-from test import support
if not hasattr(select, "epoll"):
raise unittest.SkipTest("test works only on Linux 2.6")
diff --git a/Lib/test/test_pep3151.py b/Lib/test/test_exception_hierarchy.py
index 7b0d465..8649596 100644
--- a/Lib/test/test_pep3151.py
+++ b/Lib/test/test_exception_hierarchy.py
@@ -2,7 +2,6 @@ import builtins
import os
import select
import socket
-import sys
import unittest
import errno
from errno import EEXIST
diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py
index 96c3a48..4837922 100644
--- a/Lib/test/test_exceptions.py
+++ b/Lib/test/test_exceptions.py
@@ -1109,7 +1109,6 @@ class ImportErrorTests(unittest.TestCase):
with self.assertRaisesRegex(TypeError, msg):
ImportError(invalid='keyword')
- msg = "'invalid|another' is an invalid keyword argument for this function"
with self.assertRaisesRegex(TypeError, msg):
ImportError('test', invalid='keyword', another=True)
diff --git a/Lib/test/test_extcall.py b/Lib/test/test_extcall.py
index 9cb0d38..043df01 100644
--- a/Lib/test/test_extcall.py
+++ b/Lib/test/test_extcall.py
@@ -238,6 +238,11 @@ What about willful misconduct?
...
TypeError: h() argument after * must be an iterable, not function
+ >>> h(*[1], *h)
+ Traceback (most recent call last):
+ ...
+ TypeError: h() argument after * must be an iterable, not function
+
>>> dir(*h)
Traceback (most recent call last):
...
diff --git a/Lib/test/test_faulthandler.py b/Lib/test/test_faulthandler.py
index 1562eec..bdd8d1a 100644
--- a/Lib/test/test_faulthandler.py
+++ b/Lib/test/test_faulthandler.py
@@ -7,7 +7,7 @@ import signal
import subprocess
import sys
from test import support
-from test.support import script_helper
+from test.support import script_helper, is_android, requires_android_level
import tempfile
import unittest
from textwrap import dedent
@@ -23,6 +23,7 @@ except ImportError:
_testcapi = None
TIMEOUT = 0.5
+MS_WINDOWS = (os.name == 'nt')
def expected_traceback(lineno1, lineno2, header, min_count=1):
regex = header
@@ -41,6 +42,10 @@ def temporary_filename():
finally:
support.unlink(filename)
+def requires_raise(test):
+ return (test if not is_android else
+ requires_android_level(24, 'raise() is buggy')(test))
+
class FaultHandlerTests(unittest.TestCase):
def get_output(self, code, filename=None, fd=None):
"""
@@ -58,8 +63,9 @@ class FaultHandlerTests(unittest.TestCase):
pass_fds.append(fd)
with support.SuppressCrashReport():
process = script_helper.spawn_python('-c', code, pass_fds=pass_fds)
- stdout, stderr = process.communicate()
- exitcode = process.wait()
+ with process:
+ stdout, stderr = process.communicate()
+ exitcode = process.wait()
output = support.strip_python_stderr(stdout)
output = output.decode('ascii', 'backslashreplace')
if filename:
@@ -73,14 +79,11 @@ class FaultHandlerTests(unittest.TestCase):
with open(fd, "rb", closefd=False) as fp:
output = fp.read()
output = output.decode('ascii', 'backslashreplace')
- output = re.sub('Current thread 0x[0-9a-f]+',
- 'Current thread XXX',
- output)
return output.splitlines(), exitcode
- def check_fatal_error(self, code, line_number, name_regex,
- filename=None, all_threads=True, other_regex=None,
- fd=None):
+ def check_error(self, code, line_number, fatal_error, *,
+ filename=None, all_threads=True, other_regex=None,
+ fd=None, know_current_thread=True):
"""
Check that the fault handler for fatal errors is enabled and check the
traceback from the child process output.
@@ -88,19 +91,22 @@ class FaultHandlerTests(unittest.TestCase):
Raise an error if the output doesn't match the expected format.
"""
if all_threads:
- header = 'Current thread XXX (most recent call first)'
+ if know_current_thread:
+ header = 'Current thread 0x[0-9a-f]+'
+ else:
+ header = 'Thread 0x[0-9a-f]+'
else:
- header = 'Stack (most recent call first)'
- regex = """
- ^Fatal Python error: {name}
+ header = 'Stack'
+ regex = r"""
+ ^{fatal_error}
- {header}:
+ {header} \(most recent call first\):
File "<string>", line {lineno} in <module>
"""
regex = dedent(regex.format(
lineno=line_number,
- name=name_regex,
- header=re.escape(header))).strip()
+ fatal_error=fatal_error,
+ header=header)).strip()
if other_regex:
regex += '|' + other_regex
output, exitcode = self.get_output(code, filename=filename, fd=fd)
@@ -108,26 +114,57 @@ class FaultHandlerTests(unittest.TestCase):
self.assertRegex(output, regex)
self.assertNotEqual(exitcode, 0)
+ def check_fatal_error(self, code, line_number, name_regex, **kw):
+ fatal_error = 'Fatal Python error: %s' % name_regex
+ self.check_error(code, line_number, fatal_error, **kw)
+
+ def check_windows_exception(self, code, line_number, name_regex, **kw):
+ fatal_error = 'Windows fatal exception: %s' % name_regex
+ self.check_error(code, line_number, fatal_error, **kw)
+
@unittest.skipIf(sys.platform.startswith('aix'),
"the first page of memory is a mapped read-only on AIX")
def test_read_null(self):
+ if not MS_WINDOWS:
+ self.check_fatal_error("""
+ import faulthandler
+ faulthandler.enable()
+ faulthandler._read_null()
+ """,
+ 3,
+ # Issue #12700: Read NULL raises SIGILL on Mac OS X Lion
+ '(?:Segmentation fault'
+ '|Bus error'
+ '|Illegal instruction)')
+ else:
+ self.check_windows_exception("""
+ import faulthandler
+ faulthandler.enable()
+ faulthandler._read_null()
+ """,
+ 3,
+ 'access violation')
+
+ @requires_raise
+ def test_sigsegv(self):
self.check_fatal_error("""
import faulthandler
faulthandler.enable()
- faulthandler._read_null()
+ faulthandler._sigsegv()
""",
3,
- # Issue #12700: Read NULL raises SIGILL on Mac OS X Lion
- '(?:Segmentation fault|Bus error|Illegal instruction)')
+ 'Segmentation fault')
- def test_sigsegv(self):
+ @unittest.skipIf(not HAVE_THREADS, 'need threads')
+ def test_fatal_error_c_thread(self):
self.check_fatal_error("""
import faulthandler
faulthandler.enable()
- faulthandler._sigsegv()
+ faulthandler._fatal_error_c_thread()
""",
3,
- 'Segmentation fault')
+ 'in new thread',
+ know_current_thread=False)
def test_sigabrt(self):
self.check_fatal_error("""
@@ -151,6 +188,7 @@ class FaultHandlerTests(unittest.TestCase):
@unittest.skipIf(_testcapi is None, 'need _testcapi')
@unittest.skipUnless(hasattr(signal, 'SIGBUS'), 'need signal.SIGBUS')
+ @requires_raise
def test_sigbus(self):
self.check_fatal_error("""
import _testcapi
@@ -165,6 +203,7 @@ class FaultHandlerTests(unittest.TestCase):
@unittest.skipIf(_testcapi is None, 'need _testcapi')
@unittest.skipUnless(hasattr(signal, 'SIGILL'), 'need signal.SIGILL')
+ @requires_raise
def test_sigill(self):
self.check_fatal_error("""
import _testcapi
@@ -208,6 +247,7 @@ class FaultHandlerTests(unittest.TestCase):
'(?:Segmentation fault|Bus error)',
other_regex='unable to raise a stack overflow')
+ @requires_raise
def test_gil_released(self):
self.check_fatal_error("""
import faulthandler
@@ -217,6 +257,7 @@ class FaultHandlerTests(unittest.TestCase):
3,
'Segmentation fault')
+ @requires_raise
def test_enable_file(self):
with temporary_filename() as filename:
self.check_fatal_error("""
@@ -231,6 +272,7 @@ class FaultHandlerTests(unittest.TestCase):
@unittest.skipIf(sys.platform == "win32",
"subprocess doesn't support pass_fds on Windows")
+ @requires_raise
def test_enable_fd(self):
with tempfile.TemporaryFile('wb+') as fp:
fd = fp.fileno()
@@ -244,6 +286,7 @@ class FaultHandlerTests(unittest.TestCase):
'Segmentation fault',
fd=fd)
+ @requires_raise
def test_enable_single_thread(self):
self.check_fatal_error("""
import faulthandler
@@ -254,6 +297,7 @@ class FaultHandlerTests(unittest.TestCase):
'Segmentation fault',
all_threads=False)
+ @requires_raise
def test_disable(self):
code = """
import faulthandler
@@ -458,14 +502,14 @@ class FaultHandlerTests(unittest.TestCase):
lineno = 8
else:
lineno = 10
- regex = """
+ regex = r"""
^Thread 0x[0-9a-f]+ \(most recent call first\):
(?: File ".*threading.py", line [0-9]+ in [_a-z]+
){{1,3}} File "<string>", line 23 in run
File ".*threading.py", line [0-9]+ in _bootstrap_inner
File ".*threading.py", line [0-9]+ in _bootstrap
- Current thread XXX \(most recent call first\):
+ Current thread 0x[0-9a-f]+ \(most recent call first\):
File "<string>", line {lineno} in dump
File "<string>", line 28 in <module>$
"""
@@ -637,9 +681,9 @@ class FaultHandlerTests(unittest.TestCase):
trace = '\n'.join(trace)
if not unregister:
if all_threads:
- regex = 'Current thread XXX \(most recent call first\):\n'
+ regex = r'Current thread 0x[0-9a-f]+ \(most recent call first\):\n'
else:
- regex = 'Stack \(most recent call first\):\n'
+ regex = r'Stack \(most recent call first\):\n'
regex = expected_traceback(14, 32, regex)
self.assertRegex(trace, regex)
else:
@@ -696,6 +740,22 @@ class FaultHandlerTests(unittest.TestCase):
with self.check_stderr_none():
faulthandler.register(signal.SIGUSR1)
+ @unittest.skipUnless(MS_WINDOWS, 'specific to Windows')
+ def test_raise_exception(self):
+ for exc, name in (
+ ('EXCEPTION_ACCESS_VIOLATION', 'access violation'),
+ ('EXCEPTION_INT_DIVIDE_BY_ZERO', 'int divide by zero'),
+ ('EXCEPTION_STACK_OVERFLOW', 'stack overflow'),
+ ):
+ self.check_windows_exception(f"""
+ import faulthandler
+ faulthandler.enable()
+ faulthandler._raise_exception(faulthandler._{exc})
+ """,
+ 3,
+ name)
+
+
if __name__ == "__main__":
unittest.main()
diff --git a/Lib/test/test_file.py b/Lib/test/test_file.py
index 4e392b7..65be30f 100644
--- a/Lib/test/test_file.py
+++ b/Lib/test/test_file.py
@@ -7,7 +7,7 @@ from weakref import proxy
import io
import _pyio as pyio
-from test.support import TESTFN, run_unittest
+from test.support import TESTFN
from collections import UserList
class AutoFileTests:
@@ -139,7 +139,7 @@ class OtherFileTests:
def testModeStrings(self):
# check invalid mode strings
- for mode in ("", "aU", "wU+"):
+ for mode in ("", "aU", "wU+", "U+", "+U", "rU+"):
try:
f = self.open(TESTFN, mode)
except ValueError:
diff --git a/Lib/test/test_fileinput.py b/Lib/test/test_fileinput.py
index 784bc92..565633f 100644
--- a/Lib/test/test_fileinput.py
+++ b/Lib/test/test_fileinput.py
@@ -22,8 +22,9 @@ except ImportError:
from io import BytesIO, StringIO
from fileinput import FileInput, hook_encoded
-from test.support import verbose, TESTFN, run_unittest, check_warnings
+from test.support import verbose, TESTFN, check_warnings
from test.support import unlink as safe_unlink
+from test import support
from unittest import mock
@@ -92,7 +93,11 @@ class BufferSizesTests(unittest.TestCase):
t2 = writeTmp(2, ["Line %s of file 2\n" % (i+1) for i in range(10)])
t3 = writeTmp(3, ["Line %s of file 3\n" % (i+1) for i in range(5)])
t4 = writeTmp(4, ["Line %s of file 4\n" % (i+1) for i in range(1)])
- self.buffer_size_test(t1, t2, t3, t4, bs, round)
+ if bs:
+ with self.assertWarns(DeprecationWarning):
+ self.buffer_size_test(t1, t2, t3, t4, bs, round)
+ else:
+ self.buffer_size_test(t1, t2, t3, t4, bs, round)
finally:
remove_tempfiles(t1, t2, t3, t4)
@@ -940,7 +945,8 @@ class Test_hook_encoded(unittest.TestCase):
def test(self):
encoding = object()
- result = fileinput.hook_encoded(encoding)
+ errors = object()
+ result = fileinput.hook_encoded(encoding, errors=errors)
fake_open = InvocationRecorder()
original_open = builtins.open
@@ -958,8 +964,26 @@ class Test_hook_encoded(unittest.TestCase):
self.assertIs(args[0], filename)
self.assertIs(args[1], mode)
self.assertIs(kwargs.pop('encoding'), encoding)
+ self.assertIs(kwargs.pop('errors'), errors)
self.assertFalse(kwargs)
+ def test_errors(self):
+ with open(TESTFN, 'wb') as f:
+ f.write(b'\x80abc')
+ self.addCleanup(safe_unlink, TESTFN)
+
+ def check(errors, expected_lines):
+ with FileInput(files=TESTFN, mode='r',
+ openhook=hook_encoded('utf-8', errors=errors)) as fi:
+ lines = list(fi)
+ self.assertEqual(lines, expected_lines)
+
+ check('ignore', ['abc'])
+ with self.assertRaises(UnicodeDecodeError):
+ check('strict', ['abc'])
+ check('replace', ['\ufffdabc'])
+ check('backslashreplace', ['\\x80abc'])
+
def test_modes(self):
with open(TESTFN, 'wb') as f:
# UTF-7 is a convenient, seldom used encoding
@@ -981,5 +1005,11 @@ class Test_hook_encoded(unittest.TestCase):
check('rb', ['A\n', 'B\r\n', 'C\r', 'D\u20ac'])
+class MiscTest(unittest.TestCase):
+
+ def test_all(self):
+ support.check__all__(self, fileinput)
+
+
if __name__ == "__main__":
unittest.main()
diff --git a/Lib/test/test_float.py b/Lib/test/test_float.py
index cb1f6db..ac8473d 100644
--- a/Lib/test/test_float.py
+++ b/Lib/test/test_float.py
@@ -1,6 +1,4 @@
-
import fractions
-import math
import operator
import os
import random
@@ -10,6 +8,8 @@ import time
import unittest
from test import support
+from test.test_grammar import (VALID_UNDERSCORE_LITERALS,
+ INVALID_UNDERSCORE_LITERALS)
from math import isinf, isnan, copysign, ldexp
INF = float("inf")
@@ -61,6 +61,27 @@ class GeneralFloatCases(unittest.TestCase):
float(b'.' + b'1'*1000)
float('.' + '1'*1000)
+ def test_underscores(self):
+ for lit in VALID_UNDERSCORE_LITERALS:
+ if not any(ch in lit for ch in 'jJxXoObB'):
+ self.assertEqual(float(lit), eval(lit))
+ self.assertEqual(float(lit), float(lit.replace('_', '')))
+ for lit in INVALID_UNDERSCORE_LITERALS:
+ if lit in ('0_7', '09_99'): # octals are not recognized here
+ continue
+ if not any(ch in lit for ch in 'jJxXoObB'):
+ self.assertRaises(ValueError, float, lit)
+ # Additional test cases; nan and inf are never valid as literals,
+ # only in the float() constructor, but we don't allow underscores
+ # in or around them.
+ self.assertRaises(ValueError, float, '_NaN')
+ self.assertRaises(ValueError, float, 'Na_N')
+ self.assertRaises(ValueError, float, 'IN_F')
+ self.assertRaises(ValueError, float, '-_INF')
+ self.assertRaises(ValueError, float, '-INF_')
+ # Check that we handle bytes values correctly.
+ self.assertRaises(ValueError, float, b'0_.\xff9')
+
def test_non_numeric_input_types(self):
# Test possible non-numeric types for the argument x, including
# subclasses of the explicitly documented accepted types.
@@ -162,11 +183,12 @@ class GeneralFloatCases(unittest.TestCase):
def __float__(self):
return float(str(self)) + 1
- self.assertAlmostEqual(float(Foo1()), 42.)
- self.assertAlmostEqual(float(Foo2()), 42.)
- self.assertAlmostEqual(float(Foo3(21)), 42.)
+ self.assertEqual(float(Foo1()), 42.)
+ self.assertEqual(float(Foo2()), 42.)
+ with self.assertWarns(DeprecationWarning):
+ self.assertEqual(float(Foo3(21)), 42.)
self.assertRaises(TypeError, float, Foo4(42))
- self.assertAlmostEqual(float(FooStr('8')), 9.)
+ self.assertEqual(float(FooStr('8')), 9.)
class Foo5:
def __float__(self):
@@ -177,10 +199,14 @@ class GeneralFloatCases(unittest.TestCase):
class F:
def __float__(self):
return OtherFloatSubclass(42.)
- self.assertAlmostEqual(float(F()), 42.)
- self.assertIs(type(float(F())), OtherFloatSubclass)
- self.assertAlmostEqual(FloatSubclass(F()), 42.)
- self.assertIs(type(FloatSubclass(F())), FloatSubclass)
+ with self.assertWarns(DeprecationWarning):
+ self.assertEqual(float(F()), 42.)
+ with self.assertWarns(DeprecationWarning):
+ self.assertIs(type(float(F())), float)
+ with self.assertWarns(DeprecationWarning):
+ self.assertEqual(FloatSubclass(F()), 42.)
+ with self.assertWarns(DeprecationWarning):
+ self.assertIs(type(FloatSubclass(F())), FloatSubclass)
def test_is_integer(self):
self.assertFalse((1.1).is_integer())
diff --git a/Lib/test/test_fnmatch.py b/Lib/test/test_fnmatch.py
index fa37f90..fb74246 100644
--- a/Lib/test/test_fnmatch.py
+++ b/Lib/test/test_fnmatch.py
@@ -62,14 +62,14 @@ class FnmatchTestCase(unittest.TestCase):
class TranslateTestCase(unittest.TestCase):
def test_translate(self):
- self.assertEqual(translate('*'), '.*\Z(?ms)')
- self.assertEqual(translate('?'), '.\Z(?ms)')
- self.assertEqual(translate('a?b*'), 'a.b.*\Z(?ms)')
- self.assertEqual(translate('[abc]'), '[abc]\Z(?ms)')
- self.assertEqual(translate('[]]'), '[]]\Z(?ms)')
- self.assertEqual(translate('[!x]'), '[^x]\Z(?ms)')
- self.assertEqual(translate('[^x]'), '[\\^x]\Z(?ms)')
- self.assertEqual(translate('[x'), '\\[x\Z(?ms)')
+ self.assertEqual(translate('*'), r'(?s:.*)\Z')
+ self.assertEqual(translate('?'), r'(?s:.)\Z')
+ self.assertEqual(translate('a?b*'), r'(?s:a.b.*)\Z')
+ self.assertEqual(translate('[abc]'), r'(?s:[abc])\Z')
+ self.assertEqual(translate('[]]'), r'(?s:[]])\Z')
+ self.assertEqual(translate('[!x]'), r'(?s:[^x])\Z')
+ self.assertEqual(translate('[^x]'), r'(?s:[\^x])\Z')
+ self.assertEqual(translate('[x'), r'(?s:\[x)\Z')
class FilterTestCase(unittest.TestCase):
diff --git a/Lib/test/test_format.py b/Lib/test/test_format.py
index 565bb79..83eb29f 100644
--- a/Lib/test/test_format.py
+++ b/Lib/test/test_format.py
@@ -278,7 +278,7 @@ class FormatTest(unittest.TestCase):
test_exc('%d', '1', TypeError, "%d format: a number is required, not str")
test_exc('%x', '1', TypeError, "%x format: an integer is required, not str")
test_exc('%x', 3.14, TypeError, "%x format: an integer is required, not float")
- test_exc('%g', '1', TypeError, "a float is required")
+ test_exc('%g', '1', TypeError, "must be real number, not str")
test_exc('no format', '1', TypeError,
"not all arguments converted during string formatting")
test_exc('%c', -1, OverflowError, "%c arg not in range(0x110000)")
@@ -304,6 +304,8 @@ class FormatTest(unittest.TestCase):
testcommon(b"%c", 7, b"\x07")
testcommon(b"%c", b"Z", b"Z")
testcommon(b"%c", bytearray(b"Z"), b"Z")
+ testcommon(b"%5c", 65, b" A")
+ testcommon(b"%-5c", 65, b"A ")
# %b will insert a series of bytes, either from a type that supports
# the Py_buffer protocol, or something that has a __bytes__ method
class FakeBytes(object):
diff --git a/Lib/test/test_fractions.py b/Lib/test/test_fractions.py
index 9df4a54..7905c36 100644
--- a/Lib/test/test_fractions.py
+++ b/Lib/test/test_fractions.py
@@ -150,6 +150,7 @@ class FractionTest(unittest.TestCase):
self.assertRaises(TypeError, F, "3/2", 3)
self.assertRaises(TypeError, F, 3, 0j)
self.assertRaises(TypeError, F, 3, 1j)
+ self.assertRaises(TypeError, F, 1, 2, 3)
@requires_IEEE_754
def testInitFromFloat(self):
@@ -263,13 +264,13 @@ class FractionTest(unittest.TestCase):
nan = inf - inf
# bug 16469: error types should be consistent with float -> int
self.assertRaisesMessage(
- OverflowError, "Cannot convert inf to Fraction.",
+ OverflowError, "cannot convert Infinity to integer ratio",
F.from_float, inf)
self.assertRaisesMessage(
- OverflowError, "Cannot convert -inf to Fraction.",
+ OverflowError, "cannot convert Infinity to integer ratio",
F.from_float, -inf)
self.assertRaisesMessage(
- ValueError, "Cannot convert nan to Fraction.",
+ ValueError, "cannot convert NaN to integer ratio",
F.from_float, nan)
def testFromDecimal(self):
@@ -284,16 +285,16 @@ class FractionTest(unittest.TestCase):
# bug 16469: error types should be consistent with decimal -> int
self.assertRaisesMessage(
- OverflowError, "Cannot convert Infinity to Fraction.",
+ OverflowError, "cannot convert Infinity to integer ratio",
F.from_decimal, Decimal("inf"))
self.assertRaisesMessage(
- OverflowError, "Cannot convert -Infinity to Fraction.",
+ OverflowError, "cannot convert Infinity to integer ratio",
F.from_decimal, Decimal("-inf"))
self.assertRaisesMessage(
- ValueError, "Cannot convert NaN to Fraction.",
+ ValueError, "cannot convert NaN to integer ratio",
F.from_decimal, Decimal("nan"))
self.assertRaisesMessage(
- ValueError, "Cannot convert sNaN to Fraction.",
+ ValueError, "cannot convert NaN to integer ratio",
F.from_decimal, Decimal("snan"))
def testLimitDenominator(self):
diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py
new file mode 100644
index 0000000..708ed25
--- /dev/null
+++ b/Lib/test/test_fstring.py
@@ -0,0 +1,760 @@
+import ast
+import types
+import decimal
+import unittest
+
+a_global = 'global variable'
+
+# You could argue that I'm too strict in looking for specific error
+# values with assertRaisesRegex, but without it it's way too easy to
+# make a syntax error in the test strings. Especially with all of the
+# triple quotes, raw strings, backslashes, etc. I think it's a
+# worthwhile tradeoff. When I switched to this method, I found many
+# examples where I wasn't testing what I thought I was.
+
+class TestCase(unittest.TestCase):
+ def assertAllRaise(self, exception_type, regex, error_strings):
+ for str in error_strings:
+ with self.subTest(str=str):
+ with self.assertRaisesRegex(exception_type, regex):
+ eval(str)
+
+ def test__format__lookup(self):
+ # Make sure __format__ is looked up on the type, not the instance.
+ class X:
+ def __format__(self, spec):
+ return 'class'
+
+ x = X()
+
+ # Add a bound __format__ method to the 'y' instance, but not
+ # the 'x' instance.
+ y = X()
+ y.__format__ = types.MethodType(lambda self, spec: 'instance', y)
+
+ self.assertEqual(f'{y}', format(y))
+ self.assertEqual(f'{y}', 'class')
+ self.assertEqual(format(x), format(y))
+
+ # __format__ is not called this way, but still make sure it
+ # returns what we expect (so we can make sure we're bypassing
+ # it).
+ self.assertEqual(x.__format__(''), 'class')
+ self.assertEqual(y.__format__(''), 'instance')
+
+ # This is how __format__ is actually called.
+ self.assertEqual(type(x).__format__(x, ''), 'class')
+ self.assertEqual(type(y).__format__(y, ''), 'class')
+
+ def test_ast(self):
+ # Inspired by http://bugs.python.org/issue24975
+ class X:
+ def __init__(self):
+ self.called = False
+ def __call__(self):
+ self.called = True
+ return 4
+ x = X()
+ expr = """
+a = 10
+f'{a * x()}'"""
+ t = ast.parse(expr)
+ c = compile(t, '', 'exec')
+
+ # Make sure x was not called.
+ self.assertFalse(x.called)
+
+ # Actually run the code.
+ exec(c)
+
+ # Make sure x was called.
+ self.assertTrue(x.called)
+
+ def test_docstring(self):
+ def f():
+ f'''Not a docstring'''
+ self.assertIsNone(f.__doc__)
+ def g():
+ '''Not a docstring''' \
+ f''
+ self.assertIsNone(g.__doc__)
+
+ def test_literal_eval(self):
+ with self.assertRaisesRegex(ValueError, 'malformed node or string'):
+ ast.literal_eval("f'x'")
+
+ def test_ast_compile_time_concat(self):
+ x = ['']
+
+ expr = """x[0] = 'foo' f'{3}'"""
+ t = ast.parse(expr)
+ c = compile(t, '', 'exec')
+ exec(c)
+ self.assertEqual(x[0], 'foo3')
+
+ def test_compile_time_concat_errors(self):
+ self.assertAllRaise(SyntaxError,
+ 'cannot mix bytes and nonbytes literals',
+ [r"""f'' b''""",
+ r"""b'' f''""",
+ ])
+
+ def test_literal(self):
+ self.assertEqual(f'', '')
+ self.assertEqual(f'a', 'a')
+ self.assertEqual(f' ', ' ')
+
+ def test_unterminated_string(self):
+ self.assertAllRaise(SyntaxError, 'f-string: unterminated string',
+ [r"""f'{"x'""",
+ r"""f'{"x}'""",
+ r"""f'{("x'""",
+ r"""f'{("x}'""",
+ ])
+
+ def test_mismatched_parens(self):
+ self.assertAllRaise(SyntaxError, 'f-string: mismatched',
+ ["f'{((}'",
+ ])
+
+ def test_double_braces(self):
+ self.assertEqual(f'{{', '{')
+ self.assertEqual(f'a{{', 'a{')
+ self.assertEqual(f'{{b', '{b')
+ self.assertEqual(f'a{{b', 'a{b')
+ self.assertEqual(f'}}', '}')
+ self.assertEqual(f'a}}', 'a}')
+ self.assertEqual(f'}}b', '}b')
+ self.assertEqual(f'a}}b', 'a}b')
+ self.assertEqual(f'{{}}', '{}')
+ self.assertEqual(f'a{{}}', 'a{}')
+ self.assertEqual(f'{{b}}', '{b}')
+ self.assertEqual(f'{{}}c', '{}c')
+ self.assertEqual(f'a{{b}}', 'a{b}')
+ self.assertEqual(f'a{{}}c', 'a{}c')
+ self.assertEqual(f'{{b}}c', '{b}c')
+ self.assertEqual(f'a{{b}}c', 'a{b}c')
+
+ self.assertEqual(f'{{{10}', '{10')
+ self.assertEqual(f'}}{10}', '}10')
+ self.assertEqual(f'}}{{{10}', '}{10')
+ self.assertEqual(f'}}a{{{10}', '}a{10')
+
+ self.assertEqual(f'{10}{{', '10{')
+ self.assertEqual(f'{10}}}', '10}')
+ self.assertEqual(f'{10}}}{{', '10}{')
+ self.assertEqual(f'{10}}}a{{' '}', '10}a{}')
+
+ # Inside of strings, don't interpret doubled brackets.
+ self.assertEqual(f'{"{{}}"}', '{{}}')
+
+ self.assertAllRaise(TypeError, 'unhashable type',
+ ["f'{ {{}} }'", # dict in a set
+ ])
+
+ def test_compile_time_concat(self):
+ x = 'def'
+ self.assertEqual('abc' f'## {x}ghi', 'abc## defghi')
+ self.assertEqual('abc' f'{x}' 'ghi', 'abcdefghi')
+ self.assertEqual('abc' f'{x}' 'gh' f'i{x:4}', 'abcdefghidef ')
+ self.assertEqual('{x}' f'{x}', '{x}def')
+ self.assertEqual('{x' f'{x}', '{xdef')
+ self.assertEqual('{x}' f'{x}', '{x}def')
+ self.assertEqual('{{x}}' f'{x}', '{{x}}def')
+ self.assertEqual('{{x' f'{x}', '{{xdef')
+ self.assertEqual('x}}' f'{x}', 'x}}def')
+ self.assertEqual(f'{x}' 'x}}', 'defx}}')
+ self.assertEqual(f'{x}' '', 'def')
+ self.assertEqual('' f'{x}' '', 'def')
+ self.assertEqual('' f'{x}', 'def')
+ self.assertEqual(f'{x}' '2', 'def2')
+ self.assertEqual('1' f'{x}' '2', '1def2')
+ self.assertEqual('1' f'{x}', '1def')
+ self.assertEqual(f'{x}' f'-{x}', 'def-def')
+ self.assertEqual('' f'', '')
+ self.assertEqual('' f'' '', '')
+ self.assertEqual('' f'' '' f'', '')
+ self.assertEqual(f'', '')
+ self.assertEqual(f'' '', '')
+ self.assertEqual(f'' '' f'', '')
+ self.assertEqual(f'' '' f'' '', '')
+
+ self.assertAllRaise(SyntaxError, "f-string: expecting '}'",
+ ["f'{3' f'}'", # can't concat to get a valid f-string
+ ])
+
+ def test_comments(self):
+ # These aren't comments, since they're in strings.
+ d = {'#': 'hash'}
+ self.assertEqual(f'{"#"}', '#')
+ self.assertEqual(f'{d["#"]}', 'hash')
+
+ self.assertAllRaise(SyntaxError, "f-string expression part cannot include '#'",
+ ["f'{1#}'", # error because the expression becomes "(1#)"
+ "f'{3(#)}'",
+ "f'{#}'",
+ "f'{)#}'", # When wrapped in parens, this becomes
+ # '()#)'. Make sure that doesn't compile.
+ ])
+
+ def test_many_expressions(self):
+ # Create a string with many expressions in it. Note that
+ # because we have a space in here as a literal, we're actually
+ # going to use twice as many ast nodes: one for each literal
+ # plus one for each expression.
+ def build_fstr(n, extra=''):
+ return "f'" + ('{x} ' * n) + extra + "'"
+
+ x = 'X'
+ width = 1
+
+ # Test around 256.
+ for i in range(250, 260):
+ self.assertEqual(eval(build_fstr(i)), (x+' ')*i)
+
+ # Test concatenating 2 largs fstrings.
+ self.assertEqual(eval(build_fstr(255)*256), (x+' ')*(255*256))
+
+ s = build_fstr(253, '{x:{width}} ')
+ self.assertEqual(eval(s), (x+' ')*254)
+
+ # Test lots of expressions and constants, concatenated.
+ s = "f'{1}' 'x' 'y'" * 1024
+ self.assertEqual(eval(s), '1xy' * 1024)
+
+ def test_format_specifier_expressions(self):
+ width = 10
+ precision = 4
+ value = decimal.Decimal('12.34567')
+ self.assertEqual(f'result: {value:{width}.{precision}}', 'result: 12.35')
+ self.assertEqual(f'result: {value:{width!r}.{precision}}', 'result: 12.35')
+ self.assertEqual(f'result: {value:{width:0}.{precision:1}}', 'result: 12.35')
+ self.assertEqual(f'result: {value:{1}{0:0}.{precision:1}}', 'result: 12.35')
+ self.assertEqual(f'result: {value:{ 1}{ 0:0}.{ precision:1}}', 'result: 12.35')
+ self.assertEqual(f'{10:#{1}0x}', ' 0xa')
+ self.assertEqual(f'{10:{"#"}1{0}{"x"}}', ' 0xa')
+ self.assertEqual(f'{-10:-{"#"}1{0}x}', ' -0xa')
+ self.assertEqual(f'{-10:{"-"}#{1}0{"x"}}', ' -0xa')
+ self.assertEqual(f'{10:#{3 != {4:5} and width}x}', ' 0xa')
+
+ self.assertAllRaise(SyntaxError, "f-string: expecting '}'",
+ ["""f'{"s"!r{":10"}}'""",
+
+ # This looks like a nested format spec.
+ ])
+
+ self.assertAllRaise(SyntaxError, "invalid syntax",
+ [# Invalid syntax inside a nested spec.
+ "f'{4:{/5}}'",
+ ])
+
+ self.assertAllRaise(SyntaxError, "f-string: expressions nested too deeply",
+ [# Can't nest format specifiers.
+ "f'result: {value:{width:{0}}.{precision:1}}'",
+ ])
+
+ self.assertAllRaise(SyntaxError, 'f-string: invalid conversion character',
+ [# No expansion inside conversion or for
+ # the : or ! itself.
+ """f'{"s"!{"r"}}'""",
+ ])
+
+ def test_side_effect_order(self):
+ class X:
+ def __init__(self):
+ self.i = 0
+ def __format__(self, spec):
+ self.i += 1
+ return str(self.i)
+
+ x = X()
+ self.assertEqual(f'{x} {x}', '1 2')
+
+ def test_missing_expression(self):
+ self.assertAllRaise(SyntaxError, 'f-string: empty expression not allowed',
+ ["f'{}'",
+ "f'{ }'"
+ "f' {} '",
+ "f'{!r}'",
+ "f'{ !r}'",
+ "f'{10:{ }}'",
+ "f' { } '",
+
+ # Catch the empty expression before the
+ # invalid conversion.
+ "f'{!x}'",
+ "f'{ !xr}'",
+ "f'{!x:}'",
+ "f'{!x:a}'",
+ "f'{ !xr:}'",
+ "f'{ !xr:a}'",
+
+ "f'{!}'",
+ "f'{:}'",
+
+ # We find the empty expression before the
+ # missing closing brace.
+ "f'{!'",
+ "f'{!s:'",
+ "f'{:'",
+ "f'{:x'",
+ ])
+
+ def test_parens_in_expressions(self):
+ self.assertEqual(f'{3,}', '(3,)')
+
+ # Add these because when an expression is evaluated, parens
+ # are added around it. But we shouldn't go from an invalid
+ # expression to a valid one. The added parens are just
+ # supposed to allow whitespace (including newlines).
+ self.assertAllRaise(SyntaxError, 'invalid syntax',
+ ["f'{,}'",
+ "f'{,}'", # this is (,), which is an error
+ ])
+
+ self.assertAllRaise(SyntaxError, "f-string: expecting '}'",
+ ["f'{3)+(4}'",
+ ])
+
+ self.assertAllRaise(SyntaxError, 'EOL while scanning string literal',
+ ["f'{\n}'",
+ ])
+
+ def test_backslashes_in_string_part(self):
+ self.assertEqual(f'\t', '\t')
+ self.assertEqual(r'\t', '\\t')
+ self.assertEqual(rf'\t', '\\t')
+ self.assertEqual(f'{2}\t', '2\t')
+ self.assertEqual(f'{2}\t{3}', '2\t3')
+ self.assertEqual(f'\t{3}', '\t3')
+
+ self.assertEqual(f'\u0394', '\u0394')
+ self.assertEqual(r'\u0394', '\\u0394')
+ self.assertEqual(rf'\u0394', '\\u0394')
+ self.assertEqual(f'{2}\u0394', '2\u0394')
+ self.assertEqual(f'{2}\u0394{3}', '2\u03943')
+ self.assertEqual(f'\u0394{3}', '\u03943')
+
+ self.assertEqual(f'\U00000394', '\u0394')
+ self.assertEqual(r'\U00000394', '\\U00000394')
+ self.assertEqual(rf'\U00000394', '\\U00000394')
+ self.assertEqual(f'{2}\U00000394', '2\u0394')
+ self.assertEqual(f'{2}\U00000394{3}', '2\u03943')
+ self.assertEqual(f'\U00000394{3}', '\u03943')
+
+ self.assertEqual(f'\N{GREEK CAPITAL LETTER DELTA}', '\u0394')
+ self.assertEqual(f'{2}\N{GREEK CAPITAL LETTER DELTA}', '2\u0394')
+ self.assertEqual(f'{2}\N{GREEK CAPITAL LETTER DELTA}{3}', '2\u03943')
+ self.assertEqual(f'\N{GREEK CAPITAL LETTER DELTA}{3}', '\u03943')
+ self.assertEqual(f'2\N{GREEK CAPITAL LETTER DELTA}', '2\u0394')
+ self.assertEqual(f'2\N{GREEK CAPITAL LETTER DELTA}3', '2\u03943')
+ self.assertEqual(f'\N{GREEK CAPITAL LETTER DELTA}3', '\u03943')
+
+ self.assertEqual(f'\x20', ' ')
+ self.assertEqual(r'\x20', '\\x20')
+ self.assertEqual(rf'\x20', '\\x20')
+ self.assertEqual(f'{2}\x20', '2 ')
+ self.assertEqual(f'{2}\x20{3}', '2 3')
+ self.assertEqual(f'\x20{3}', ' 3')
+
+ self.assertEqual(f'2\x20', '2 ')
+ self.assertEqual(f'2\x203', '2 3')
+ self.assertEqual(f'\x203', ' 3')
+
+ def test_misformed_unicode_character_name(self):
+ # These test are needed because unicode names are parsed
+ # differently inside f-strings.
+ self.assertAllRaise(SyntaxError, r"\(unicode error\) 'unicodeescape' codec can't decode bytes in position .*: malformed \\N character escape",
+ [r"f'\N'",
+ r"f'\N{'",
+ r"f'\N{GREEK CAPITAL LETTER DELTA'",
+
+ # Here are the non-f-string versions,
+ # which should give the same errors.
+ r"'\N'",
+ r"'\N{'",
+ r"'\N{GREEK CAPITAL LETTER DELTA'",
+ ])
+
+ def test_no_backslashes_in_expression_part(self):
+ self.assertAllRaise(SyntaxError, 'f-string expression part cannot include a backslash',
+ [r"f'{\'a\'}'",
+ r"f'{\t3}'",
+ r"f'{\}'",
+ r"rf'{\'a\'}'",
+ r"rf'{\t3}'",
+ r"rf'{\}'",
+ r"""rf'{"\N{LEFT CURLY BRACKET}"}'""",
+ r"f'{\n}'",
+ ])
+
+ def test_no_escapes_for_braces(self):
+ """
+ Only literal curly braces begin an expression.
+ """
+ # \x7b is '{'.
+ self.assertEqual(f'\x7b1+1}}', '{1+1}')
+ self.assertEqual(f'\x7b1+1', '{1+1')
+ self.assertEqual(f'\u007b1+1', '{1+1')
+ self.assertEqual(f'\N{LEFT CURLY BRACKET}1+1\N{RIGHT CURLY BRACKET}', '{1+1}')
+
+ def test_newlines_in_expressions(self):
+ self.assertEqual(f'{0}', '0')
+ self.assertEqual(rf'''{3+
+4}''', '7')
+
+ def test_lambda(self):
+ x = 5
+ self.assertEqual(f'{(lambda y:x*y)("8")!r}', "'88888'")
+ self.assertEqual(f'{(lambda y:x*y)("8")!r:10}', "'88888' ")
+ self.assertEqual(f'{(lambda y:x*y)("8"):10}', "88888 ")
+
+ # lambda doesn't work without parens, because the colon
+ # makes the parser think it's a format_spec
+ self.assertAllRaise(SyntaxError, 'unexpected EOF while parsing',
+ ["f'{lambda x:x}'",
+ ])
+
+ def test_yield(self):
+ # Not terribly useful, but make sure the yield turns
+ # a function into a generator
+ def fn(y):
+ f'y:{yield y*2}'
+
+ g = fn(4)
+ self.assertEqual(next(g), 8)
+
+ def test_yield_send(self):
+ def fn(x):
+ yield f'x:{yield (lambda i: x * i)}'
+
+ g = fn(10)
+ the_lambda = next(g)
+ self.assertEqual(the_lambda(4), 40)
+ self.assertEqual(g.send('string'), 'x:string')
+
+ def test_expressions_with_triple_quoted_strings(self):
+ self.assertEqual(f"{'''x'''}", 'x')
+ self.assertEqual(f"{'''eric's'''}", "eric's")
+
+ # Test concatenation within an expression
+ self.assertEqual(f'{"x" """eric"s""" "y"}', 'xeric"sy')
+ self.assertEqual(f'{"x" """eric"s"""}', 'xeric"s')
+ self.assertEqual(f'{"""eric"s""" "y"}', 'eric"sy')
+ self.assertEqual(f'{"""x""" """eric"s""" "y"}', 'xeric"sy')
+ self.assertEqual(f'{"""x""" """eric"s""" """y"""}', 'xeric"sy')
+ self.assertEqual(f'{r"""x""" """eric"s""" """y"""}', 'xeric"sy')
+
+ def test_multiple_vars(self):
+ x = 98
+ y = 'abc'
+ self.assertEqual(f'{x}{y}', '98abc')
+
+ self.assertEqual(f'X{x}{y}', 'X98abc')
+ self.assertEqual(f'{x}X{y}', '98Xabc')
+ self.assertEqual(f'{x}{y}X', '98abcX')
+
+ self.assertEqual(f'X{x}Y{y}', 'X98Yabc')
+ self.assertEqual(f'X{x}{y}Y', 'X98abcY')
+ self.assertEqual(f'{x}X{y}Y', '98XabcY')
+
+ self.assertEqual(f'X{x}Y{y}Z', 'X98YabcZ')
+
+ def test_closure(self):
+ def outer(x):
+ def inner():
+ return f'x:{x}'
+ return inner
+
+ self.assertEqual(outer('987')(), 'x:987')
+ self.assertEqual(outer(7)(), 'x:7')
+
+ def test_arguments(self):
+ y = 2
+ def f(x, width):
+ return f'x={x*y:{width}}'
+
+ self.assertEqual(f('foo', 10), 'x=foofoo ')
+ x = 'bar'
+ self.assertEqual(f(10, 10), 'x= 20')
+
+ def test_locals(self):
+ value = 123
+ self.assertEqual(f'v:{value}', 'v:123')
+
+ def test_missing_variable(self):
+ with self.assertRaises(NameError):
+ f'v:{value}'
+
+ def test_missing_format_spec(self):
+ class O:
+ def __format__(self, spec):
+ if not spec:
+ return '*'
+ return spec
+
+ self.assertEqual(f'{O():x}', 'x')
+ self.assertEqual(f'{O()}', '*')
+ self.assertEqual(f'{O():}', '*')
+
+ self.assertEqual(f'{3:}', '3')
+ self.assertEqual(f'{3!s:}', '3')
+
+ def test_global(self):
+ self.assertEqual(f'g:{a_global}', 'g:global variable')
+ self.assertEqual(f'g:{a_global!r}', "g:'global variable'")
+
+ a_local = 'local variable'
+ self.assertEqual(f'g:{a_global} l:{a_local}',
+ 'g:global variable l:local variable')
+ self.assertEqual(f'g:{a_global!r}',
+ "g:'global variable'")
+ self.assertEqual(f'g:{a_global} l:{a_local!r}',
+ "g:global variable l:'local variable'")
+
+ self.assertIn("module 'unittest' from", f'{unittest}')
+
+ def test_shadowed_global(self):
+ a_global = 'really a local'
+ self.assertEqual(f'g:{a_global}', 'g:really a local')
+ self.assertEqual(f'g:{a_global!r}', "g:'really a local'")
+
+ a_local = 'local variable'
+ self.assertEqual(f'g:{a_global} l:{a_local}',
+ 'g:really a local l:local variable')
+ self.assertEqual(f'g:{a_global!r}',
+ "g:'really a local'")
+ self.assertEqual(f'g:{a_global} l:{a_local!r}',
+ "g:really a local l:'local variable'")
+
+ def test_call(self):
+ def foo(x):
+ return 'x=' + str(x)
+
+ self.assertEqual(f'{foo(10)}', 'x=10')
+
+ def test_nested_fstrings(self):
+ y = 5
+ self.assertEqual(f'{f"{0}"*3}', '000')
+ self.assertEqual(f'{f"{y}"*3}', '555')
+
+ def test_invalid_string_prefixes(self):
+ self.assertAllRaise(SyntaxError, 'unexpected EOF while parsing',
+ ["fu''",
+ "uf''",
+ "Fu''",
+ "fU''",
+ "Uf''",
+ "uF''",
+ "ufr''",
+ "urf''",
+ "fur''",
+ "fru''",
+ "rfu''",
+ "ruf''",
+ "FUR''",
+ "Fur''",
+ "fb''",
+ "fB''",
+ "Fb''",
+ "FB''",
+ "bf''",
+ "bF''",
+ "Bf''",
+ "BF''",
+ ])
+
+ def test_leading_trailing_spaces(self):
+ self.assertEqual(f'{ 3}', '3')
+ self.assertEqual(f'{ 3}', '3')
+ self.assertEqual(f'{3 }', '3')
+ self.assertEqual(f'{3 }', '3')
+
+ self.assertEqual(f'expr={ {x: y for x, y in [(1, 2), ]}}',
+ 'expr={1: 2}')
+ self.assertEqual(f'expr={ {x: y for x, y in [(1, 2), ]} }',
+ 'expr={1: 2}')
+
+ def test_not_equal(self):
+ # There's a special test for this because there's a special
+ # case in the f-string parser to look for != as not ending an
+ # expression. Normally it would, while looking for !s or !r.
+
+ self.assertEqual(f'{3!=4}', 'True')
+ self.assertEqual(f'{3!=4:}', 'True')
+ self.assertEqual(f'{3!=4!s}', 'True')
+ self.assertEqual(f'{3!=4!s:.3}', 'Tru')
+
+ def test_conversions(self):
+ self.assertEqual(f'{3.14:10.10}', ' 3.14')
+ self.assertEqual(f'{3.14!s:10.10}', '3.14 ')
+ self.assertEqual(f'{3.14!r:10.10}', '3.14 ')
+ self.assertEqual(f'{3.14!a:10.10}', '3.14 ')
+
+ self.assertEqual(f'{"a"}', 'a')
+ self.assertEqual(f'{"a"!r}', "'a'")
+ self.assertEqual(f'{"a"!a}', "'a'")
+
+ # Not a conversion.
+ self.assertEqual(f'{"a!r"}', "a!r")
+
+ # Not a conversion, but show that ! is allowed in a format spec.
+ self.assertEqual(f'{3.14:!<10.10}', '3.14!!!!!!')
+
+ self.assertAllRaise(SyntaxError, 'f-string: invalid conversion character',
+ ["f'{3!g}'",
+ "f'{3!A}'",
+ "f'{3!3}'",
+ "f'{3!G}'",
+ "f'{3!!}'",
+ "f'{3!:}'",
+ "f'{3! s}'", # no space before conversion char
+ ])
+
+ self.assertAllRaise(SyntaxError, "f-string: expecting '}'",
+ ["f'{x!s{y}}'",
+ "f'{3!ss}'",
+ "f'{3!ss:}'",
+ "f'{3!ss:s}'",
+ ])
+
+ def test_assignment(self):
+ self.assertAllRaise(SyntaxError, 'invalid syntax',
+ ["f'' = 3",
+ "f'{0}' = x",
+ "f'{x}' = x",
+ ])
+
+ def test_del(self):
+ self.assertAllRaise(SyntaxError, 'invalid syntax',
+ ["del f''",
+ "del '' f''",
+ ])
+
+ def test_mismatched_braces(self):
+ self.assertAllRaise(SyntaxError, "f-string: single '}' is not allowed",
+ ["f'{{}'",
+ "f'{{}}}'",
+ "f'}'",
+ "f'x}'",
+ "f'x}x'",
+ r"f'\u007b}'",
+
+ # Can't have { or } in a format spec.
+ "f'{3:}>10}'",
+ "f'{3:}}>10}'",
+ ])
+
+ self.assertAllRaise(SyntaxError, "f-string: expecting '}'",
+ ["f'{3:{{>10}'",
+ "f'{3'",
+ "f'{3!'",
+ "f'{3:'",
+ "f'{3!s'",
+ "f'{3!s:'",
+ "f'{3!s:3'",
+ "f'x{'",
+ "f'x{x'",
+ "f'{x'",
+ "f'{3:s'",
+ "f'{{{'",
+ "f'{{}}{'",
+ "f'{'",
+ ])
+
+ # But these are just normal strings.
+ self.assertEqual(f'{"{"}', '{')
+ self.assertEqual(f'{"}"}', '}')
+ self.assertEqual(f'{3:{"}"}>10}', '}}}}}}}}}3')
+ self.assertEqual(f'{2:{"{"}>10}', '{{{{{{{{{2')
+
+ def test_if_conditional(self):
+ # There's special logic in compile.c to test if the
+ # conditional for an if (and while) are constants. Exercise
+ # that code.
+
+ def test_fstring(x, expected):
+ flag = 0
+ if f'{x}':
+ flag = 1
+ else:
+ flag = 2
+ self.assertEqual(flag, expected)
+
+ def test_concat_empty(x, expected):
+ flag = 0
+ if '' f'{x}':
+ flag = 1
+ else:
+ flag = 2
+ self.assertEqual(flag, expected)
+
+ def test_concat_non_empty(x, expected):
+ flag = 0
+ if ' ' f'{x}':
+ flag = 1
+ else:
+ flag = 2
+ self.assertEqual(flag, expected)
+
+ test_fstring('', 2)
+ test_fstring(' ', 1)
+
+ test_concat_empty('', 2)
+ test_concat_empty(' ', 1)
+
+ test_concat_non_empty('', 1)
+ test_concat_non_empty(' ', 1)
+
+ def test_empty_format_specifier(self):
+ x = 'test'
+ self.assertEqual(f'{x}', 'test')
+ self.assertEqual(f'{x:}', 'test')
+ self.assertEqual(f'{x!s:}', 'test')
+ self.assertEqual(f'{x!r:}', "'test'")
+
+ def test_str_format_differences(self):
+ d = {'a': 'string',
+ 0: 'integer',
+ }
+ a = 0
+ self.assertEqual(f'{d[0]}', 'integer')
+ self.assertEqual(f'{d["a"]}', 'string')
+ self.assertEqual(f'{d[a]}', 'integer')
+ self.assertEqual('{d[a]}'.format(d=d), 'string')
+ self.assertEqual('{d[0]}'.format(d=d), 'integer')
+
+ def test_invalid_expressions(self):
+ self.assertAllRaise(SyntaxError, 'invalid syntax',
+ [r"f'{a[4)}'",
+ r"f'{a(4]}'",
+ ])
+
+ def test_errors(self):
+ # see issue 26287
+ self.assertAllRaise(TypeError, 'unsupported',
+ [r"f'{(lambda: 0):x}'",
+ r"f'{(0,):x}'",
+ ])
+ self.assertAllRaise(ValueError, 'Unknown format code',
+ [r"f'{1000:j}'",
+ r"f'{1000:j}'",
+ ])
+
+ def test_loop(self):
+ for i in range(1000):
+ self.assertEqual(f'i:{i}', 'i:' + str(i))
+
+ def test_dict(self):
+ d = {'"': 'dquote',
+ "'": 'squote',
+ 'foo': 'bar',
+ }
+ self.assertEqual(f'''{d["'"]}''', 'squote')
+ self.assertEqual(f"""{d['"']}""", 'dquote')
+
+ self.assertEqual(f'{d["foo"]}', 'bar')
+ self.assertEqual(f"{d['foo']}", 'bar')
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/Lib/test/test_ftplib.py b/Lib/test/test_ftplib.py
index aef66da..12fabc5 100644
--- a/Lib/test/test_ftplib.py
+++ b/Lib/test/test_ftplib.py
@@ -311,10 +311,12 @@ if ssl is not None:
_ssl_closing = False
def secure_connection(self):
- socket = ssl.wrap_socket(self.socket, suppress_ragged_eofs=False,
- certfile=CERTFILE, server_side=True,
- do_handshake_on_connect=False,
- ssl_version=ssl.PROTOCOL_SSLv23)
+ context = ssl.SSLContext()
+ context.load_cert_chain(CERTFILE)
+ socket = context.wrap_socket(self.socket,
+ suppress_ragged_eofs=False,
+ server_side=True,
+ do_handshake_on_connect=False)
self.del_channel()
self.set_socket(socket)
self._ssl_accepting = True
@@ -1049,10 +1051,19 @@ class TestTimeouts(TestCase):
ftp.close()
+class MiscTestCase(TestCase):
+ def test__all__(self):
+ blacklist = {'MSG_OOB', 'FTP_PORT', 'MAXLINE', 'CRLF', 'B_CRLF',
+ 'Error', 'parse150', 'parse227', 'parse229', 'parse257',
+ 'print_line', 'ftpcp', 'test'}
+ support.check__all__(self, ftplib, blacklist=blacklist)
+
+
def test_main():
tests = [TestFTPClass, TestTimeouts,
TestIPv6Environment,
- TestTLS_FTPClassMixin, TestTLS_FTPClass]
+ TestTLS_FTPClassMixin, TestTLS_FTPClass,
+ MiscTestCase]
thread_info = support.threading_setup()
try:
diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py
index 9ccd0ca..824549b 100644
--- a/Lib/test/test_functools.py
+++ b/Lib/test/test_functools.py
@@ -10,6 +10,7 @@ from test import support
import time
import unittest
from weakref import proxy
+import contextlib
try:
import threading
except ImportError:
@@ -22,6 +23,14 @@ c_functools = support.import_fresh_module('functools', fresh=['_functools'])
decimal = support.import_fresh_module('decimal', fresh=['_decimal'])
+@contextlib.contextmanager
+def replaced_module(name, replacement):
+ original_module = sys.modules[name]
+ sys.modules[name] = replacement
+ try:
+ yield
+ finally:
+ sys.modules[name] = original_module
def capture(*args, **kw):
"""capture all positional and keyword arguments"""
@@ -169,58 +178,35 @@ class TestPartial:
p2.new_attr = 'spam'
self.assertEqual(p2.new_attr, 'spam')
-
-@unittest.skipUnless(c_functools, 'requires the C _functools module')
-class TestPartialC(TestPartial, unittest.TestCase):
- if c_functools:
- partial = c_functools.partial
-
- def test_attributes_unwritable(self):
- # attributes should not be writable
- p = self.partial(capture, 1, 2, a=10, b=20)
- self.assertRaises(AttributeError, setattr, p, 'func', map)
- self.assertRaises(AttributeError, setattr, p, 'args', (1, 2))
- self.assertRaises(AttributeError, setattr, p, 'keywords', dict(a=1, b=2))
-
- p = self.partial(hex)
- try:
- del p.__dict__
- except TypeError:
- pass
- else:
- self.fail('partial object allowed __dict__ to be deleted')
-
def test_repr(self):
args = (object(), object())
args_repr = ', '.join(repr(a) for a in args)
kwargs = {'a': object(), 'b': object()}
kwargs_reprs = ['a={a!r}, b={b!r}'.format_map(kwargs),
'b={b!r}, a={a!r}'.format_map(kwargs)]
- if self.partial is c_functools.partial:
+ if self.partial in (c_functools.partial, py_functools.partial):
name = 'functools.partial'
else:
name = self.partial.__name__
f = self.partial(capture)
- self.assertEqual('{}({!r})'.format(name, capture),
- repr(f))
+ self.assertEqual(f'{name}({capture!r})', repr(f))
f = self.partial(capture, *args)
- self.assertEqual('{}({!r}, {})'.format(name, capture, args_repr),
- repr(f))
+ self.assertEqual(f'{name}({capture!r}, {args_repr})', repr(f))
f = self.partial(capture, **kwargs)
self.assertIn(repr(f),
- ['{}({!r}, {})'.format(name, capture, kwargs_repr)
+ [f'{name}({capture!r}, {kwargs_repr})'
for kwargs_repr in kwargs_reprs])
f = self.partial(capture, *args, **kwargs)
self.assertIn(repr(f),
- ['{}({!r}, {}, {})'.format(name, capture, args_repr, kwargs_repr)
+ [f'{name}({capture!r}, {args_repr}, {kwargs_repr})'
for kwargs_repr in kwargs_reprs])
def test_recursive_repr(self):
- if self.partial is c_functools.partial:
+ if self.partial in (c_functools.partial, py_functools.partial):
name = 'functools.partial'
else:
name = self.partial.__name__
@@ -228,30 +214,31 @@ class TestPartialC(TestPartial, unittest.TestCase):
f = self.partial(capture)
f.__setstate__((f, (), {}, {}))
try:
- self.assertEqual(repr(f), '%s(%s(...))' % (name, name))
+ self.assertEqual(repr(f), '%s(...)' % (name,))
finally:
f.__setstate__((capture, (), {}, {}))
f = self.partial(capture)
f.__setstate__((capture, (f,), {}, {}))
try:
- self.assertEqual(repr(f), '%s(%r, %s(...))' % (name, capture, name))
+ self.assertEqual(repr(f), '%s(%r, ...)' % (name, capture,))
finally:
f.__setstate__((capture, (), {}, {}))
f = self.partial(capture)
f.__setstate__((capture, (), {'a': f}, {}))
try:
- self.assertEqual(repr(f), '%s(%r, a=%s(...))' % (name, capture, name))
+ self.assertEqual(repr(f), '%s(%r, a=...)' % (name, capture,))
finally:
f.__setstate__((capture, (), {}, {}))
def test_pickle(self):
- f = self.partial(signature, ['asdf'], bar=[True])
- f.attr = []
- for proto in range(pickle.HIGHEST_PROTOCOL + 1):
- f_copy = pickle.loads(pickle.dumps(f, proto))
- self.assertEqual(signature(f_copy), signature(f))
+ with self.AllowPickle():
+ f = self.partial(signature, ['asdf'], bar=[True])
+ f.attr = []
+ for proto in range(pickle.HIGHEST_PROTOCOL + 1):
+ f_copy = pickle.loads(pickle.dumps(f, proto))
+ self.assertEqual(signature(f_copy), signature(f))
def test_copy(self):
f = self.partial(signature, ['asdf'], bar=[True])
@@ -276,11 +263,13 @@ class TestPartialC(TestPartial, unittest.TestCase):
def test_setstate(self):
f = self.partial(signature)
f.__setstate__((capture, (1,), dict(a=10), dict(attr=[])))
+
self.assertEqual(signature(f),
(capture, (1,), dict(a=10), dict(attr=[])))
self.assertEqual(f(2, b=20), ((1, 2), {'a': 10, 'b': 20}))
f.__setstate__((capture, (1,), dict(a=10), None))
+
self.assertEqual(signature(f), (capture, (1,), dict(a=10), {}))
self.assertEqual(f(2, b=20), ((1, 2), {'a': 10, 'b': 20}))
@@ -327,38 +316,39 @@ class TestPartialC(TestPartial, unittest.TestCase):
self.assertIs(type(r[0]), tuple)
def test_recursive_pickle(self):
- f = self.partial(capture)
- f.__setstate__((f, (), {}, {}))
- try:
- for proto in range(pickle.HIGHEST_PROTOCOL + 1):
- with self.assertRaises(RecursionError):
- pickle.dumps(f, proto)
- finally:
- f.__setstate__((capture, (), {}, {}))
-
- f = self.partial(capture)
- f.__setstate__((capture, (f,), {}, {}))
- try:
- for proto in range(pickle.HIGHEST_PROTOCOL + 1):
- f_copy = pickle.loads(pickle.dumps(f, proto))
- try:
- self.assertIs(f_copy.args[0], f_copy)
- finally:
- f_copy.__setstate__((capture, (), {}, {}))
- finally:
- f.__setstate__((capture, (), {}, {}))
-
- f = self.partial(capture)
- f.__setstate__((capture, (), {'a': f}, {}))
- try:
- for proto in range(pickle.HIGHEST_PROTOCOL + 1):
- f_copy = pickle.loads(pickle.dumps(f, proto))
- try:
- self.assertIs(f_copy.keywords['a'], f_copy)
- finally:
- f_copy.__setstate__((capture, (), {}, {}))
- finally:
- f.__setstate__((capture, (), {}, {}))
+ with self.AllowPickle():
+ f = self.partial(capture)
+ f.__setstate__((f, (), {}, {}))
+ try:
+ for proto in range(pickle.HIGHEST_PROTOCOL + 1):
+ with self.assertRaises(RecursionError):
+ pickle.dumps(f, proto)
+ finally:
+ f.__setstate__((capture, (), {}, {}))
+
+ f = self.partial(capture)
+ f.__setstate__((capture, (f,), {}, {}))
+ try:
+ for proto in range(pickle.HIGHEST_PROTOCOL + 1):
+ f_copy = pickle.loads(pickle.dumps(f, proto))
+ try:
+ self.assertIs(f_copy.args[0], f_copy)
+ finally:
+ f_copy.__setstate__((capture, (), {}, {}))
+ finally:
+ f.__setstate__((capture, (), {}, {}))
+
+ f = self.partial(capture)
+ f.__setstate__((capture, (), {'a': f}, {}))
+ try:
+ for proto in range(pickle.HIGHEST_PROTOCOL + 1):
+ f_copy = pickle.loads(pickle.dumps(f, proto))
+ try:
+ self.assertIs(f_copy.keywords['a'], f_copy)
+ finally:
+ f_copy.__setstate__((capture, (), {}, {}))
+ finally:
+ f.__setstate__((capture, (), {}, {}))
# Issue 6083: Reference counting bug
def test_setstate_refcount(self):
@@ -377,24 +367,60 @@ class TestPartialC(TestPartial, unittest.TestCase):
f = self.partial(object)
self.assertRaises(TypeError, f.__setstate__, BadSequence())
+@unittest.skipUnless(c_functools, 'requires the C _functools module')
+class TestPartialC(TestPartial, unittest.TestCase):
+ if c_functools:
+ partial = c_functools.partial
+
+ class AllowPickle:
+ def __enter__(self):
+ return self
+ def __exit__(self, type, value, tb):
+ return False
+
+ def test_attributes_unwritable(self):
+ # attributes should not be writable
+ p = self.partial(capture, 1, 2, a=10, b=20)
+ self.assertRaises(AttributeError, setattr, p, 'func', map)
+ self.assertRaises(AttributeError, setattr, p, 'args', (1, 2))
+ self.assertRaises(AttributeError, setattr, p, 'keywords', dict(a=1, b=2))
+
+ p = self.partial(hex)
+ try:
+ del p.__dict__
+ except TypeError:
+ pass
+ else:
+ self.fail('partial object allowed __dict__ to be deleted')
class TestPartialPy(TestPartial, unittest.TestCase):
- partial = staticmethod(py_functools.partial)
+ partial = py_functools.partial
+ class AllowPickle:
+ def __init__(self):
+ self._cm = replaced_module("functools", py_functools)
+ def __enter__(self):
+ return self._cm.__enter__()
+ def __exit__(self, type, value, tb):
+ return self._cm.__exit__(type, value, tb)
if c_functools:
- class PartialSubclass(c_functools.partial):
+ class CPartialSubclass(c_functools.partial):
pass
+class PyPartialSubclass(py_functools.partial):
+ pass
@unittest.skipUnless(c_functools, 'requires the C _functools module')
class TestPartialCSubclass(TestPartialC):
if c_functools:
- partial = PartialSubclass
+ partial = CPartialSubclass
# partial subclasses are not optimized for nested calls
test_nested_optimization = None
+class TestPartialPySubclass(TestPartialPy):
+ partial = PyPartialSubclass
class TestPartialMethod(unittest.TestCase):
@@ -685,9 +711,10 @@ class TestWraps(TestUpdateWrapper):
self.assertEqual(wrapper.attr, 'This is a different test')
self.assertEqual(wrapper.dict_attr, f.dict_attr)
-
+@unittest.skipUnless(c_functools, 'requires the C _functools module')
class TestReduce(unittest.TestCase):
- func = functools.reduce
+ if c_functools:
+ func = c_functools.reduce
def test_reduce(self):
class Squares:
@@ -1280,6 +1307,16 @@ class TestLRU:
self.assertEqual(fib.cache_info(),
self.module._CacheInfo(hits=0, misses=0, maxsize=None, currsize=0))
+ def test_kwargs_order(self):
+ # PEP 468: Preserving Keyword Argument Order
+ @self.module.lru_cache(maxsize=10)
+ def f(**kwargs):
+ return list(kwargs.items())
+ self.assertEqual(f(a=1, b=2), [('a', 1), ('b', 2)])
+ self.assertEqual(f(b=2, a=1), [('b', 2), ('a', 1)])
+ self.assertEqual(f.cache_info(),
+ self.module._CacheInfo(hits=0, misses=2, maxsize=10, currsize=2))
+
def test_lru_cache_decoration(self):
def f(zomg: 'zomg_annotation'):
"""f doc string"""
@@ -1309,7 +1346,7 @@ class TestLRU:
f.cache_clear()
orig_si = sys.getswitchinterval()
- sys.setswitchinterval(1e-6)
+ support.setswitchinterval(1e-6)
try:
# create n threads in order to fill cache
threads = [threading.Thread(target=full, args=[k])
@@ -1595,13 +1632,15 @@ class TestSingleDispatch(unittest.TestCase):
bases = [c.Sequence, c.MutableMapping, c.Mapping, c.Set]
for haystack in permutations(bases):
m = mro(dict, haystack)
- self.assertEqual(m, [dict, c.MutableMapping, c.Mapping, c.Sized,
- c.Iterable, c.Container, object])
+ self.assertEqual(m, [dict, c.MutableMapping, c.Mapping,
+ c.Collection, c.Sized, c.Iterable,
+ c.Container, object])
bases = [c.Container, c.Mapping, c.MutableMapping, c.OrderedDict]
for haystack in permutations(bases):
m = mro(c.ChainMap, haystack)
self.assertEqual(m, [c.ChainMap, c.MutableMapping, c.Mapping,
- c.Sized, c.Iterable, c.Container, object])
+ c.Collection, c.Sized, c.Iterable,
+ c.Container, object])
# If there's a generic function with implementations registered for
# both Sized and Container, passing a defaultdict to it results in an
@@ -1622,9 +1661,9 @@ class TestSingleDispatch(unittest.TestCase):
bases = [c.MutableSequence, c.MutableMapping]
for haystack in permutations(bases):
m = mro(D, bases)
- self.assertEqual(m, [D, c.MutableSequence, c.Sequence,
- c.defaultdict, dict, c.MutableMapping,
- c.Mapping, c.Sized, c.Iterable, c.Container,
+ self.assertEqual(m, [D, c.MutableSequence, c.Sequence, c.Reversible,
+ c.defaultdict, dict, c.MutableMapping, c.Mapping,
+ c.Collection, c.Sized, c.Iterable, c.Container,
object])
# Container and Callable are registered on different base classes and
@@ -1637,7 +1676,8 @@ class TestSingleDispatch(unittest.TestCase):
for haystack in permutations(bases):
m = mro(C, haystack)
self.assertEqual(m, [C, c.Callable, c.defaultdict, dict, c.Mapping,
- c.Sized, c.Iterable, c.Container, object])
+ c.Collection, c.Sized, c.Iterable,
+ c.Container, object])
def test_register_abc(self):
c = collections
diff --git a/Lib/test/test_future.py b/Lib/test/test_future.py
index 7b072ad..2f1c410 100644
--- a/Lib/test/test_future.py
+++ b/Lib/test/test_future.py
@@ -5,7 +5,7 @@ from test import support
import os
import re
-rx = re.compile('\((\S+).py, line (\d+)')
+rx = re.compile(r'\((\S+).py, line (\d+)')
def get_error_location(msg):
mo = rx.search(str(msg))
diff --git a/Lib/test/test_gc.py b/Lib/test/test_gc.py
index a4d684b..e727499 100644
--- a/Lib/test/test_gc.py
+++ b/Lib/test/test_gc.py
@@ -684,7 +684,6 @@ class GCTests(unittest.TestCase):
# Create a reference cycle through the __main__ module and check
# it gets collected at interpreter shutdown.
code = """if 1:
- import weakref
class C:
def __del__(self):
print('__del__ called')
@@ -699,7 +698,6 @@ class GCTests(unittest.TestCase):
# Same as above, but with a non-__main__ module.
with temp_dir() as script_dir:
module = """if 1:
- import weakref
class C:
def __del__(self):
print('__del__ called')
diff --git a/Lib/test/test_gdb.py b/Lib/test/test_gdb.py
index 3db10b6..60f1d92 100644
--- a/Lib/test/test_gdb.py
+++ b/Lib/test/test_gdb.py
@@ -5,7 +5,6 @@
import os
import re
-import pprint
import subprocess
import sys
import sysconfig
@@ -111,6 +110,7 @@ HAS_PYUP_PYDOWN = gdb_has_frame_select()
BREAKPOINT_FN='builtin_id'
+@unittest.skipIf(support.PGO, "not useful for PGO")
class DebuggerTests(unittest.TestCase):
"""Test that the debugger can debug Python."""
@@ -177,6 +177,7 @@ class DebuggerTests(unittest.TestCase):
args = ['--eval-command=%s' % cmd for cmd in commands]
args += ["--args",
sys.executable]
+ args.extend(subprocess._args_from_interpreter_flags())
if not import_site:
# -S suppresses the default 'import site'
@@ -240,7 +241,7 @@ class DebuggerTests(unittest.TestCase):
# gdb can insert additional '\n' and space characters in various places
# in its output, depending on the width of the terminal it's connected
# to (using its "wrap_here" function)
- m = re.match('.*#0\s+builtin_id\s+\(self\=.*,\s+v=\s*(.*?)\)\s+at\s+\S*Python/bltinmodule.c.*',
+ m = re.match(r'.*#0\s+builtin_id\s+\(self\=.*,\s+v=\s*(.*?)\)\s+at\s+\S*Python/bltinmodule.c.*',
gdb_output, re.DOTALL)
if not m:
self.fail('Unexpected gdb output: %r\n%s' % (gdb_output, gdb_output))
@@ -292,7 +293,8 @@ class PrettyPrintTests(DebuggerTests):
'Verify the pretty-printing of dictionaries'
self.assertGdbRepr({})
self.assertGdbRepr({'foo': 'bar'}, "{'foo': 'bar'}")
- self.assertGdbRepr({'foo': 'bar', 'douglas': 42}, "{'douglas': 42, 'foo': 'bar'}")
+ # Python preserves insertion order since 3.6
+ self.assertGdbRepr({'foo': 'bar', 'douglas': 42}, "{'foo': 'bar', 'douglas': 42}")
def test_lists(self):
'Verify the pretty-printing of lists'
@@ -355,9 +357,12 @@ class PrettyPrintTests(DebuggerTests):
'Verify the pretty-printing of sets'
if (gdb_major_version, gdb_minor_version) < (7, 3):
self.skipTest("pretty-printing of sets needs gdb 7.3 or later")
- self.assertGdbRepr(set(), 'set()')
- self.assertGdbRepr(set(['a', 'b']), "{'a', 'b'}")
- self.assertGdbRepr(set([4, 5, 6]), "{4, 5, 6}")
+ self.assertGdbRepr(set(), "set()")
+ self.assertGdbRepr(set(['a']), "{'a'}")
+ # PYTHONHASHSEED is need to get the exact frozenset item order
+ if not sys.flags.ignore_environment:
+ self.assertGdbRepr(set(['a', 'b']), "{'a', 'b'}")
+ self.assertGdbRepr(set([4, 5, 6]), "{4, 5, 6}")
# Ensure that we handle sets containing the "dummy" key value,
# which happens on deletion:
@@ -370,9 +375,12 @@ id(s)''')
'Verify the pretty-printing of frozensets'
if (gdb_major_version, gdb_minor_version) < (7, 3):
self.skipTest("pretty-printing of frozensets needs gdb 7.3 or later")
- self.assertGdbRepr(frozenset(), 'frozenset()')
- self.assertGdbRepr(frozenset(['a', 'b']), "frozenset({'a', 'b'})")
- self.assertGdbRepr(frozenset([4, 5, 6]), "frozenset({4, 5, 6})")
+ self.assertGdbRepr(frozenset(), "frozenset()")
+ self.assertGdbRepr(frozenset(['a']), "frozenset({'a'})")
+ # PYTHONHASHSEED is need to get the exact frozenset item order
+ if not sys.flags.ignore_environment:
+ self.assertGdbRepr(frozenset(['a', 'b']), "frozenset({'a', 'b'})")
+ self.assertGdbRepr(frozenset([4, 5, 6]), "frozenset({4, 5, 6})")
def test_exceptions(self):
# Test a RuntimeError
@@ -501,6 +509,10 @@ id(foo)''')
def test_builtins_help(self):
'Ensure that the new-style class _Helper in site.py can be handled'
+
+ if sys.flags.no_site:
+ self.skipTest("need site module, but -S option was used")
+
# (this was the issue causing tracebacks in
# http://bugs.python.org/issue8032#msg100537 )
gdb_repr, gdb_output = self.get_gdb_repr('id(__builtins__.help)', import_site=True)
@@ -536,7 +548,7 @@ class Foo:
foo = Foo()
foo.an_attr = foo
id(foo)''')
- self.assertTrue(re.match('<Foo\(an_attr=<\.\.\.>\) at remote 0x-?[0-9a-f]+>',
+ self.assertTrue(re.match(r'<Foo\(an_attr=<\.\.\.>\) at remote 0x-?[0-9a-f]+>',
gdb_repr),
'Unexpected gdb representation: %r\n%s' % \
(gdb_repr, gdb_output))
@@ -549,7 +561,7 @@ class Foo(object):
foo = Foo()
foo.an_attr = foo
id(foo)''')
- self.assertTrue(re.match('<Foo\(an_attr=<\.\.\.>\) at remote 0x-?[0-9a-f]+>',
+ self.assertTrue(re.match(r'<Foo\(an_attr=<\.\.\.>\) at remote 0x-?[0-9a-f]+>',
gdb_repr),
'Unexpected gdb representation: %r\n%s' % \
(gdb_repr, gdb_output))
@@ -563,7 +575,7 @@ b = Foo()
a.an_attr = b
b.an_attr = a
id(a)''')
- self.assertTrue(re.match('<Foo\(an_attr=<Foo\(an_attr=<\.\.\.>\) at remote 0x-?[0-9a-f]+>\) at remote 0x-?[0-9a-f]+>',
+ self.assertTrue(re.match(r'<Foo\(an_attr=<Foo\(an_attr=<\.\.\.>\) at remote 0x-?[0-9a-f]+>\) at remote 0x-?[0-9a-f]+>',
gdb_repr),
'Unexpected gdb representation: %r\n%s' % \
(gdb_repr, gdb_output))
@@ -598,7 +610,7 @@ id(a)''')
def test_builtin_method(self):
gdb_repr, gdb_output = self.get_gdb_repr('import sys; id(sys.stdout.readlines)')
- self.assertTrue(re.match('<built-in method readlines of _io.TextIOWrapper object at remote 0x-?[0-9a-f]+>',
+ self.assertTrue(re.match(r'<built-in method readlines of _io.TextIOWrapper object at remote 0x-?[0-9a-f]+>',
gdb_repr),
'Unexpected gdb representation: %r\n%s' % \
(gdb_repr, gdb_output))
@@ -613,7 +625,7 @@ id(foo.__code__)''',
breakpoint='builtin_id',
cmds_after_breakpoint=['print (PyFrameObject*)(((PyCodeObject*)v)->co_zombieframe)']
)
- self.assertTrue(re.match('.*\s+\$1 =\s+Frame 0x-?[0-9a-f]+, for file <string>, line 3, in foo \(\)\s+.*',
+ self.assertTrue(re.match(r'.*\s+\$1 =\s+Frame 0x-?[0-9a-f]+, for file <string>, line 3, in foo \(\)\s+.*',
gdb_output,
re.DOTALL),
'Unexpected gdb representation: %r\n%s' % (gdb_output, gdb_output))
@@ -667,7 +679,7 @@ class StackNavigationTests(DebuggerTests):
def test_pyup_command(self):
'Verify that the "py-up" command works'
bt = self.get_stack_trace(script=self.get_sample_script(),
- cmds_after_breakpoint=['py-up'])
+ cmds_after_breakpoint=['py-up', 'py-up'])
self.assertMultilineMatches(bt,
r'''^.*
#[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 7, in bar \(a=1, b=2, c=3\)
@@ -686,7 +698,7 @@ $''')
def test_up_at_top(self):
'Verify handling of "py-up" at the top of the stack'
bt = self.get_stack_trace(script=self.get_sample_script(),
- cmds_after_breakpoint=['py-up'] * 4)
+ cmds_after_breakpoint=['py-up'] * 5)
self.assertEndsWith(bt,
'Unable to find an older python frame\n')
@@ -696,7 +708,7 @@ $''')
def test_up_then_down(self):
'Verify "py-up" followed by "py-down"'
bt = self.get_stack_trace(script=self.get_sample_script(),
- cmds_after_breakpoint=['py-up', 'py-down'])
+ cmds_after_breakpoint=['py-up', 'py-up', 'py-down'])
self.assertMultilineMatches(bt,
r'''^.*
#[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 7, in bar \(a=1, b=2, c=3\)
@@ -715,6 +727,7 @@ class PyBtTests(DebuggerTests):
self.assertMultilineMatches(bt,
r'''^.*
Traceback \(most recent call first\):
+ <built-in method id of module object .*>
File ".*gdb_sample.py", line 10, in baz
id\(42\)
File ".*gdb_sample.py", line 7, in bar
@@ -841,7 +854,7 @@ class PyPrintTests(DebuggerTests):
def test_basic_command(self):
'Verify that the "py-print" command works'
bt = self.get_stack_trace(script=self.get_sample_script(),
- cmds_after_breakpoint=['py-print args'])
+ cmds_after_breakpoint=['py-up', 'py-print args'])
self.assertMultilineMatches(bt,
r".*\nlocal 'args' = \(1, 2, 3\)\n.*")
@@ -850,7 +863,7 @@ class PyPrintTests(DebuggerTests):
@unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
def test_print_after_up(self):
bt = self.get_stack_trace(script=self.get_sample_script(),
- cmds_after_breakpoint=['py-up', 'py-print c', 'py-print b', 'py-print a'])
+ cmds_after_breakpoint=['py-up', 'py-up', 'py-print c', 'py-print b', 'py-print a'])
self.assertMultilineMatches(bt,
r".*\nlocal 'c' = 3\nlocal 'b' = 2\nlocal 'a' = 1\n.*")
@@ -858,7 +871,7 @@ class PyPrintTests(DebuggerTests):
"Python was compiled with optimizations")
def test_printing_global(self):
bt = self.get_stack_trace(script=self.get_sample_script(),
- cmds_after_breakpoint=['py-print __name__'])
+ cmds_after_breakpoint=['py-up', 'py-print __name__'])
self.assertMultilineMatches(bt,
r".*\nglobal '__name__' = '__main__'\n.*")
@@ -866,7 +879,7 @@ class PyPrintTests(DebuggerTests):
"Python was compiled with optimizations")
def test_printing_builtin(self):
bt = self.get_stack_trace(script=self.get_sample_script(),
- cmds_after_breakpoint=['py-print len'])
+ cmds_after_breakpoint=['py-up', 'py-print len'])
self.assertMultilineMatches(bt,
r".*\nbuiltin 'len' = <built-in method len of module object at remote 0x-?[0-9a-f]+>\n.*")
@@ -875,7 +888,7 @@ class PyLocalsTests(DebuggerTests):
"Python was compiled with optimizations")
def test_basic_command(self):
bt = self.get_stack_trace(script=self.get_sample_script(),
- cmds_after_breakpoint=['py-locals'])
+ cmds_after_breakpoint=['py-up', 'py-locals'])
self.assertMultilineMatches(bt,
r".*\nargs = \(1, 2, 3\)\n.*")
@@ -884,7 +897,7 @@ class PyLocalsTests(DebuggerTests):
"Python was compiled with optimizations")
def test_locals_after_up(self):
bt = self.get_stack_trace(script=self.get_sample_script(),
- cmds_after_breakpoint=['py-up', 'py-locals'])
+ cmds_after_breakpoint=['py-up', 'py-up', 'py-locals'])
self.assertMultilineMatches(bt,
r".*\na = 1\nb = 2\nc = 3\n.*")
diff --git a/Lib/test/test_pep479.py b/Lib/test/test_generator_stop.py
index bc235ce..bc235ce 100644
--- a/Lib/test/test_pep479.py
+++ b/Lib/test/test_generator_stop.py
diff --git a/Lib/test/test_generators.py b/Lib/test/test_generators.py
index cd6a43d..f81c82f 100644
--- a/Lib/test/test_generators.py
+++ b/Lib/test/test_generators.py
@@ -245,11 +245,11 @@ class ExceptionTest(unittest.TestCase):
yield
with self.assertRaises(StopIteration), \
- self.assertWarnsRegex(PendingDeprecationWarning, "StopIteration"):
+ self.assertWarnsRegex(DeprecationWarning, "StopIteration"):
next(gen())
- with self.assertRaisesRegex(PendingDeprecationWarning,
+ with self.assertRaisesRegex(DeprecationWarning,
"generator .* raised StopIteration"), \
warnings.catch_warnings():
@@ -268,7 +268,7 @@ class ExceptionTest(unittest.TestCase):
g = f()
self.assertEqual(next(g), 1)
- with self.assertWarnsRegex(PendingDeprecationWarning, "StopIteration"):
+ with self.assertWarnsRegex(DeprecationWarning, "StopIteration"):
with self.assertRaises(StopIteration):
next(g)
diff --git a/Lib/test/test_genericpath.py b/Lib/test/test_genericpath.py
index b77d1d7..f698e13 100644
--- a/Lib/test/test_genericpath.py
+++ b/Lib/test/test_genericpath.py
@@ -8,13 +8,12 @@ import sys
import unittest
import warnings
from test import support
+android_not_root = support.android_not_root
-def safe_rmdir(dirname):
- try:
- os.rmdir(dirname)
- except OSError:
- pass
+def create_file(filename, data=b'foo'):
+ with open(filename, 'xb', 0) as fp:
+ fp.write(data)
class GenericTest:
@@ -97,52 +96,47 @@ class GenericTest:
self.assertNotEqual(s1[n:n+1], s2[n:n+1])
def test_getsize(self):
- f = open(support.TESTFN, "wb")
- try:
- f.write(b"foo")
- f.close()
- self.assertEqual(self.pathmodule.getsize(support.TESTFN), 3)
- finally:
- if not f.closed:
- f.close()
- support.unlink(support.TESTFN)
+ filename = support.TESTFN
+ self.addCleanup(support.unlink, filename)
- def test_time(self):
- f = open(support.TESTFN, "wb")
- try:
- f.write(b"foo")
- f.close()
- f = open(support.TESTFN, "ab")
+ create_file(filename, b'Hello')
+ self.assertEqual(self.pathmodule.getsize(filename), 5)
+ os.remove(filename)
+
+ create_file(filename, b'Hello World!')
+ self.assertEqual(self.pathmodule.getsize(filename), 12)
+
+ def test_filetime(self):
+ filename = support.TESTFN
+ self.addCleanup(support.unlink, filename)
+
+ create_file(filename, b'foo')
+
+ with open(filename, "ab", 0) as f:
f.write(b"bar")
- f.close()
- f = open(support.TESTFN, "rb")
- d = f.read()
- f.close()
- self.assertEqual(d, b"foobar")
-
- self.assertLessEqual(
- self.pathmodule.getctime(support.TESTFN),
- self.pathmodule.getmtime(support.TESTFN)
- )
- finally:
- if not f.closed:
- f.close()
- support.unlink(support.TESTFN)
+
+ with open(filename, "rb", 0) as f:
+ data = f.read()
+ self.assertEqual(data, b"foobar")
+
+ self.assertLessEqual(
+ self.pathmodule.getctime(filename),
+ self.pathmodule.getmtime(filename)
+ )
def test_exists(self):
- self.assertIs(self.pathmodule.exists(support.TESTFN), False)
- f = open(support.TESTFN, "wb")
- try:
+ filename = support.TESTFN
+ self.addCleanup(support.unlink, filename)
+
+ self.assertIs(self.pathmodule.exists(filename), False)
+
+ with open(filename, "xb") as f:
f.write(b"foo")
- f.close()
- self.assertIs(self.pathmodule.exists(support.TESTFN), True)
- if not self.pathmodule == genericpath:
- self.assertIs(self.pathmodule.lexists(support.TESTFN),
- True)
- finally:
- if not f.close():
- f.close()
- support.unlink(support.TESTFN)
+
+ self.assertIs(self.pathmodule.exists(filename), True)
+
+ if not self.pathmodule == genericpath:
+ self.assertIs(self.pathmodule.lexists(filename), True)
@unittest.skipUnless(hasattr(os, "pipe"), "requires os.pipe()")
def test_exists_fd(self):
@@ -154,119 +148,126 @@ class GenericTest:
os.close(w)
self.assertFalse(self.pathmodule.exists(r))
- def test_isdir(self):
- self.assertIs(self.pathmodule.isdir(support.TESTFN), False)
- f = open(support.TESTFN, "wb")
- try:
- f.write(b"foo")
- f.close()
- self.assertIs(self.pathmodule.isdir(support.TESTFN), False)
- os.remove(support.TESTFN)
- os.mkdir(support.TESTFN)
- self.assertIs(self.pathmodule.isdir(support.TESTFN), True)
- os.rmdir(support.TESTFN)
- finally:
- if not f.close():
- f.close()
- support.unlink(support.TESTFN)
- safe_rmdir(support.TESTFN)
-
- def test_isfile(self):
- self.assertIs(self.pathmodule.isfile(support.TESTFN), False)
- f = open(support.TESTFN, "wb")
- try:
- f.write(b"foo")
- f.close()
- self.assertIs(self.pathmodule.isfile(support.TESTFN), True)
- os.remove(support.TESTFN)
- os.mkdir(support.TESTFN)
- self.assertIs(self.pathmodule.isfile(support.TESTFN), False)
- os.rmdir(support.TESTFN)
- finally:
- if not f.close():
- f.close()
- support.unlink(support.TESTFN)
- safe_rmdir(support.TESTFN)
+ def test_isdir_file(self):
+ filename = support.TESTFN
+ self.addCleanup(support.unlink, filename)
+ self.assertIs(self.pathmodule.isdir(filename), False)
+
+ create_file(filename)
+ self.assertIs(self.pathmodule.isdir(filename), False)
+
+ def test_isdir_dir(self):
+ filename = support.TESTFN
+ self.addCleanup(support.rmdir, filename)
+ self.assertIs(self.pathmodule.isdir(filename), False)
- @staticmethod
- def _create_file(filename):
- with open(filename, 'wb') as f:
- f.write(b'foo')
+ os.mkdir(filename)
+ self.assertIs(self.pathmodule.isdir(filename), True)
+
+ def test_isfile_file(self):
+ filename = support.TESTFN
+ self.addCleanup(support.unlink, filename)
+ self.assertIs(self.pathmodule.isfile(filename), False)
+
+ create_file(filename)
+ self.assertIs(self.pathmodule.isfile(filename), True)
+
+ def test_isfile_dir(self):
+ filename = support.TESTFN
+ self.addCleanup(support.rmdir, filename)
+ self.assertIs(self.pathmodule.isfile(filename), False)
+
+ os.mkdir(filename)
+ self.assertIs(self.pathmodule.isfile(filename), False)
def test_samefile(self):
- try:
- test_fn = support.TESTFN + "1"
- self._create_file(test_fn)
- self.assertTrue(self.pathmodule.samefile(test_fn, test_fn))
- self.assertRaises(TypeError, self.pathmodule.samefile)
- finally:
- os.remove(test_fn)
+ file1 = support.TESTFN
+ file2 = support.TESTFN + "2"
+ self.addCleanup(support.unlink, file1)
+ self.addCleanup(support.unlink, file2)
+
+ create_file(file1)
+ self.assertTrue(self.pathmodule.samefile(file1, file1))
+
+ create_file(file2)
+ self.assertFalse(self.pathmodule.samefile(file1, file2))
+
+ self.assertRaises(TypeError, self.pathmodule.samefile)
+
+ def _test_samefile_on_link_func(self, func):
+ test_fn1 = support.TESTFN
+ test_fn2 = support.TESTFN + "2"
+ self.addCleanup(support.unlink, test_fn1)
+ self.addCleanup(support.unlink, test_fn2)
+
+ create_file(test_fn1)
+
+ func(test_fn1, test_fn2)
+ self.assertTrue(self.pathmodule.samefile(test_fn1, test_fn2))
+ os.remove(test_fn2)
+
+ create_file(test_fn2)
+ self.assertFalse(self.pathmodule.samefile(test_fn1, test_fn2))
@support.skip_unless_symlink
def test_samefile_on_symlink(self):
self._test_samefile_on_link_func(os.symlink)
+ @unittest.skipIf(android_not_root, "hard links not allowed, non root user")
def test_samefile_on_link(self):
self._test_samefile_on_link_func(os.link)
- def _test_samefile_on_link_func(self, func):
- try:
- test_fn1 = support.TESTFN + "1"
- test_fn2 = support.TESTFN + "2"
- self._create_file(test_fn1)
+ def test_samestat(self):
+ test_fn1 = support.TESTFN
+ test_fn2 = support.TESTFN + "2"
+ self.addCleanup(support.unlink, test_fn1)
+ self.addCleanup(support.unlink, test_fn2)
- func(test_fn1, test_fn2)
- self.assertTrue(self.pathmodule.samefile(test_fn1, test_fn2))
- os.remove(test_fn2)
+ create_file(test_fn1)
+ stat1 = os.stat(test_fn1)
+ self.assertTrue(self.pathmodule.samestat(stat1, os.stat(test_fn1)))
- self._create_file(test_fn2)
- self.assertFalse(self.pathmodule.samefile(test_fn1, test_fn2))
- finally:
- os.remove(test_fn1)
- os.remove(test_fn2)
+ create_file(test_fn2)
+ stat2 = os.stat(test_fn2)
+ self.assertFalse(self.pathmodule.samestat(stat1, stat2))
- def test_samestat(self):
- try:
- test_fn = support.TESTFN + "1"
- self._create_file(test_fn)
- test_fns = [test_fn]*2
- stats = map(os.stat, test_fns)
- self.assertTrue(self.pathmodule.samestat(*stats))
- finally:
- os.remove(test_fn)
+ self.assertRaises(TypeError, self.pathmodule.samestat)
+
+ def _test_samestat_on_link_func(self, func):
+ test_fn1 = support.TESTFN + "1"
+ test_fn2 = support.TESTFN + "2"
+ self.addCleanup(support.unlink, test_fn1)
+ self.addCleanup(support.unlink, test_fn2)
+
+ create_file(test_fn1)
+ func(test_fn1, test_fn2)
+ self.assertTrue(self.pathmodule.samestat(os.stat(test_fn1),
+ os.stat(test_fn2)))
+ os.remove(test_fn2)
+
+ create_file(test_fn2)
+ self.assertFalse(self.pathmodule.samestat(os.stat(test_fn1),
+ os.stat(test_fn2)))
@support.skip_unless_symlink
def test_samestat_on_symlink(self):
self._test_samestat_on_link_func(os.symlink)
+ @unittest.skipIf(android_not_root, "hard links not allowed, non root user")
def test_samestat_on_link(self):
self._test_samestat_on_link_func(os.link)
- def _test_samestat_on_link_func(self, func):
- try:
- test_fn1 = support.TESTFN + "1"
- test_fn2 = support.TESTFN + "2"
- self._create_file(test_fn1)
- test_fns = (test_fn1, test_fn2)
- func(*test_fns)
- stats = map(os.stat, test_fns)
- self.assertTrue(self.pathmodule.samestat(*stats))
- os.remove(test_fn2)
-
- self._create_file(test_fn2)
- stats = map(os.stat, test_fns)
- self.assertFalse(self.pathmodule.samestat(*stats))
-
- self.assertRaises(TypeError, self.pathmodule.samestat)
- finally:
- os.remove(test_fn1)
- os.remove(test_fn2)
-
def test_sameopenfile(self):
- fname = support.TESTFN + "1"
- with open(fname, "wb") as a, open(fname, "wb") as b:
- self.assertTrue(self.pathmodule.sameopenfile(
- a.fileno(), b.fileno()))
+ filename = support.TESTFN
+ self.addCleanup(support.unlink, filename)
+ create_file(filename)
+
+ with open(filename, "rb", 0) as fp1:
+ fd1 = fp1.fileno()
+ with open(filename, "rb", 0) as fp2:
+ fd2 = fp2.fileno()
+ self.assertTrue(self.pathmodule.sameopenfile(fd1, fd2))
+
class TestGenericTest(GenericTest, unittest.TestCase):
# Issue 16852: GenericTest can't inherit from unittest.TestCase
@@ -390,10 +391,13 @@ class CommonTest(GenericTest):
warnings.simplefilter("ignore", DeprecationWarning)
self.assertIn(b"foo", self.pathmodule.abspath(b"foo"))
+ # avoid UnicodeDecodeError on Windows
+ undecodable_path = b'' if sys.platform == 'win32' else b'f\xf2\xf2'
+
# Abspath returns bytes when the arg is bytes
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
- for path in (b'', b'foo', b'f\xf2\xf2', b'/foo', b'C:\\'):
+ for path in (b'', b'foo', undecodable_path, b'/foo', b'C:\\'):
self.assertIsInstance(self.pathmodule.abspath(path), bytes)
def test_realpath(self):
@@ -452,16 +456,15 @@ class CommonTest(GenericTest):
with self.assertRaisesRegex(TypeError, errmsg):
self.pathmodule.join('str', b'bytes')
# regression, see #15377
- errmsg = r'join\(\) argument must be str or bytes, not %r'
- with self.assertRaisesRegex(TypeError, errmsg % 'int'):
+ with self.assertRaisesRegex(TypeError, 'int'):
self.pathmodule.join(42, 'str')
- with self.assertRaisesRegex(TypeError, errmsg % 'int'):
+ with self.assertRaisesRegex(TypeError, 'int'):
self.pathmodule.join('str', 42)
- with self.assertRaisesRegex(TypeError, errmsg % 'int'):
+ with self.assertRaisesRegex(TypeError, 'int'):
self.pathmodule.join(42)
- with self.assertRaisesRegex(TypeError, errmsg % 'list'):
+ with self.assertRaisesRegex(TypeError, 'list'):
self.pathmodule.join([])
- with self.assertRaisesRegex(TypeError, errmsg % 'bytearray'):
+ with self.assertRaisesRegex(TypeError, 'bytearray'):
self.pathmodule.join(bytearray(b'foo'), bytearray(b'bar'))
def test_relpath_errors(self):
@@ -473,14 +476,59 @@ class CommonTest(GenericTest):
self.pathmodule.relpath(b'bytes', 'str')
with self.assertRaisesRegex(TypeError, errmsg):
self.pathmodule.relpath('str', b'bytes')
- errmsg = r'relpath\(\) argument must be str or bytes, not %r'
- with self.assertRaisesRegex(TypeError, errmsg % 'int'):
+ with self.assertRaisesRegex(TypeError, 'int'):
self.pathmodule.relpath(42, 'str')
- with self.assertRaisesRegex(TypeError, errmsg % 'int'):
+ with self.assertRaisesRegex(TypeError, 'int'):
self.pathmodule.relpath('str', 42)
- with self.assertRaisesRegex(TypeError, errmsg % 'bytearray'):
+ with self.assertRaisesRegex(TypeError, 'bytearray'):
self.pathmodule.relpath(bytearray(b'foo'), bytearray(b'bar'))
+class PathLikeTests(unittest.TestCase):
+
+ class PathLike:
+ def __init__(self, path=''):
+ self.path = path
+ def __fspath__(self):
+ if isinstance(self.path, BaseException):
+ raise self.path
+ else:
+ return self.path
+
+ def setUp(self):
+ self.file_name = support.TESTFN.lower()
+ self.file_path = self.PathLike(support.TESTFN)
+ self.addCleanup(support.unlink, self.file_name)
+ create_file(self.file_name, b"test_genericpath.PathLikeTests")
+
+ def assertPathEqual(self, func):
+ self.assertEqual(func(self.file_path), func(self.file_name))
+
+ def test_path_exists(self):
+ self.assertPathEqual(os.path.exists)
+
+ def test_path_isfile(self):
+ self.assertPathEqual(os.path.isfile)
+
+ def test_path_isdir(self):
+ self.assertPathEqual(os.path.isdir)
+
+ def test_path_commonprefix(self):
+ self.assertEqual(os.path.commonprefix([self.file_path, self.file_name]),
+ self.file_name)
+
+ def test_path_getsize(self):
+ self.assertPathEqual(os.path.getsize)
+
+ def test_path_getmtime(self):
+ self.assertPathEqual(os.path.getatime)
+
+ def test_path_getctime(self):
+ self.assertPathEqual(os.path.getctime)
+
+ def test_path_samefile(self):
+ self.assertTrue(os.path.samefile(self.file_path, self.file_name))
+
+
if __name__=="__main__":
unittest.main()
diff --git a/Lib/test/test_getargs2.py b/Lib/test/test_getargs2.py
index 984aac7..8a194aa 100644
--- a/Lib/test/test_getargs2.py
+++ b/Lib/test/test_getargs2.py
@@ -5,10 +5,6 @@ from test import support
# Skip this test if the _testcapi module isn't available.
support.import_module('_testcapi')
from _testcapi import getargs_keywords, getargs_keyword_only
-try:
- from _testcapi import getargs_L, getargs_K
-except ImportError:
- getargs_L = None # PY_LONG_LONG not available
# > How about the following counterproposal. This also changes some of
# > the other format codes to be a little more regular.
@@ -309,7 +305,6 @@ class Signed_TestCase(unittest.TestCase):
self.assertRaises(OverflowError, getargs_n, VERY_LARGE)
-@unittest.skipIf(getargs_L is None, 'PY_LONG_LONG is not available')
class LongLong_TestCase(unittest.TestCase):
def test_L(self):
from _testcapi import getargs_L
@@ -365,7 +360,8 @@ class Float_TestCase(unittest.TestCase):
self.assertEqual(getargs_f(FloatSubclass(7.5)), 7.5)
self.assertEqual(getargs_f(FloatSubclass2(7.5)), 7.5)
self.assertRaises(TypeError, getargs_f, BadFloat())
- self.assertEqual(getargs_f(BadFloat2()), 4.25)
+ with self.assertWarns(DeprecationWarning):
+ self.assertEqual(getargs_f(BadFloat2()), 4.25)
self.assertEqual(getargs_f(BadFloat3(7.5)), 7.5)
for x in (FLT_MIN, -FLT_MIN, FLT_MAX, -FLT_MAX, INF, -INF):
@@ -390,7 +386,8 @@ class Float_TestCase(unittest.TestCase):
self.assertEqual(getargs_d(FloatSubclass(7.5)), 7.5)
self.assertEqual(getargs_d(FloatSubclass2(7.5)), 7.5)
self.assertRaises(TypeError, getargs_d, BadFloat())
- self.assertEqual(getargs_d(BadFloat2()), 4.25)
+ with self.assertWarns(DeprecationWarning):
+ self.assertEqual(getargs_d(BadFloat2()), 4.25)
self.assertEqual(getargs_d(BadFloat3(7.5)), 7.5)
for x in (DBL_MIN, -DBL_MIN, DBL_MAX, -DBL_MAX, INF, -INF):
@@ -474,7 +471,7 @@ class Tuple_TestCase(unittest.TestCase):
ret = get_args(*TupleSubclass([1, 2]))
self.assertEqual(ret, (1, 2))
- self.assertIsInstance(ret, tuple)
+ self.assertIs(type(ret), tuple)
ret = get_args()
self.assertIn(ret, ((), None))
@@ -512,7 +509,7 @@ class Keywords_TestCase(unittest.TestCase):
ret = get_kwargs(**DictSubclass({'a': 1, 'b': 2}))
self.assertEqual(ret, {'a': 1, 'b': 2})
- self.assertIsInstance(ret, dict)
+ self.assertIs(type(ret), dict)
ret = get_kwargs()
self.assertIn(ret, ({}, None))
@@ -628,20 +625,20 @@ class KeywordOnly_TestCase(unittest.TestCase):
)
# required arg missing
with self.assertRaisesRegex(TypeError,
- "Required argument 'required' \(pos 1\) not found"):
+ r"Required argument 'required' \(pos 1\) not found"):
getargs_keyword_only(optional=2)
with self.assertRaisesRegex(TypeError,
- "Required argument 'required' \(pos 1\) not found"):
+ r"Required argument 'required' \(pos 1\) not found"):
getargs_keyword_only(keyword_only=3)
def test_too_many_args(self):
with self.assertRaisesRegex(TypeError,
- "Function takes at most 2 positional arguments \(3 given\)"):
+ r"Function takes at most 2 positional arguments \(3 given\)"):
getargs_keyword_only(1, 2, 3)
with self.assertRaisesRegex(TypeError,
- "function takes at most 3 arguments \(4 given\)"):
+ r"function takes at most 3 arguments \(4 given\)"):
getargs_keyword_only(1, 2, 3, keyword_only=5)
def test_invalid_keyword(self):
@@ -656,6 +653,39 @@ class KeywordOnly_TestCase(unittest.TestCase):
getargs_keyword_only(1, 2, **{'\uDC80': 10})
+class PositionalOnlyAndKeywords_TestCase(unittest.TestCase):
+ from _testcapi import getargs_positional_only_and_keywords as getargs
+
+ def test_positional_args(self):
+ # using all possible positional args
+ self.assertEqual(self.getargs(1, 2, 3), (1, 2, 3))
+
+ def test_mixed_args(self):
+ # positional and keyword args
+ self.assertEqual(self.getargs(1, 2, keyword=3), (1, 2, 3))
+
+ def test_optional_args(self):
+ # missing optional args
+ self.assertEqual(self.getargs(1, 2), (1, 2, -1))
+ self.assertEqual(self.getargs(1, keyword=3), (1, -1, 3))
+
+ def test_required_args(self):
+ self.assertEqual(self.getargs(1), (1, -1, -1))
+ # required positional arg missing
+ with self.assertRaisesRegex(TypeError,
+ r"Function takes at least 1 positional arguments \(0 given\)"):
+ self.getargs()
+
+ with self.assertRaisesRegex(TypeError,
+ r"Function takes at least 1 positional arguments \(0 given\)"):
+ self.getargs(keyword=3)
+
+ def test_empty_keyword(self):
+ with self.assertRaisesRegex(TypeError,
+ "'' is an invalid keyword argument for this function"):
+ self.getargs(1, 2, **{'': 666})
+
+
class Bytes_TestCase(unittest.TestCase):
def test_c(self):
from _testcapi import getargs_c
@@ -822,10 +852,10 @@ class String_TestCase(unittest.TestCase):
self.assertEqual(getargs_es_hash('abc\xe9', 'latin1', buf), b'abc\xe9')
self.assertEqual(buf, bytearray(b'abc\xe9\x00'))
buf = bytearray(b'x'*4)
- self.assertRaises(TypeError, getargs_es_hash, 'abc\xe9', 'latin1', buf)
+ self.assertRaises(ValueError, getargs_es_hash, 'abc\xe9', 'latin1', buf)
self.assertEqual(buf, bytearray(b'x'*4))
buf = bytearray()
- self.assertRaises(TypeError, getargs_es_hash, 'abc\xe9', 'latin1', buf)
+ self.assertRaises(ValueError, getargs_es_hash, 'abc\xe9', 'latin1', buf)
def test_et_hash(self):
from _testcapi import getargs_et_hash
@@ -848,10 +878,10 @@ class String_TestCase(unittest.TestCase):
self.assertEqual(getargs_et_hash('abc\xe9', 'latin1', buf), b'abc\xe9')
self.assertEqual(buf, bytearray(b'abc\xe9\x00'))
buf = bytearray(b'x'*4)
- self.assertRaises(TypeError, getargs_et_hash, 'abc\xe9', 'latin1', buf)
+ self.assertRaises(ValueError, getargs_et_hash, 'abc\xe9', 'latin1', buf)
self.assertEqual(buf, bytearray(b'x'*4))
buf = bytearray()
- self.assertRaises(TypeError, getargs_et_hash, 'abc\xe9', 'latin1', buf)
+ self.assertRaises(ValueError, getargs_et_hash, 'abc\xe9', 'latin1', buf)
def test_u(self):
from _testcapi import getargs_u
diff --git a/Lib/test/test_gettext.py b/Lib/test/test_gettext.py
index f57e546..a852443 100644
--- a/Lib/test/test_gettext.py
+++ b/Lib/test/test_gettext.py
@@ -1,6 +1,5 @@
import os
import base64
-import shutil
import gettext
import unittest
@@ -527,6 +526,12 @@ class GettextCacheTestCase(GettextBaseTest):
self.assertEqual(t.__class__, DummyGNUTranslations)
+class MiscTestCase(unittest.TestCase):
+ def test__all__(self):
+ blacklist = {'c2py', 'ENOENT'}
+ support.check__all__(self, gettext, blacklist=blacklist)
+
+
def test_main():
support.run_unittest(__name__)
diff --git a/Lib/test/test_global.py b/Lib/test/test_global.py
index 37ec672..9eeccf1 100644
--- a/Lib/test/test_global.py
+++ b/Lib/test/test_global.py
@@ -24,7 +24,7 @@ def wrong1():
global a
global b
"""
- check_syntax_error(self, prog_text_1)
+ check_syntax_error(self, prog_text_1, lineno=4, offset=4)
def test2(self):
prog_text_2 = """\
@@ -32,7 +32,7 @@ def wrong2():
print(x)
global x
"""
- check_syntax_error(self, prog_text_2)
+ check_syntax_error(self, prog_text_2, lineno=3, offset=4)
def test3(self):
prog_text_3 = """\
@@ -41,7 +41,7 @@ def wrong3():
x = 2
global x
"""
- check_syntax_error(self, prog_text_3)
+ check_syntax_error(self, prog_text_3, lineno=4, offset=4)
def test4(self):
prog_text_4 = """\
diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py
index 154e3b6..65e26bf 100644
--- a/Lib/test/test_grammar.py
+++ b/Lib/test/test_grammar.py
@@ -8,6 +8,95 @@ import sys
# testing import *
from sys import *
+# different import patterns to check that __annotations__ does not interfere
+# with import machinery
+import test.ann_module as ann_module
+import typing
+from collections import ChainMap
+from test import ann_module2
+import test
+
+# These are shared with test_tokenize and other test modules.
+#
+# Note: since several test cases filter out floats by looking for "e" and ".",
+# don't add hexadecimal literals that contain "e" or "E".
+VALID_UNDERSCORE_LITERALS = [
+ '0_0_0',
+ '4_2',
+ '1_0000_0000',
+ '0b1001_0100',
+ '0xffff_ffff',
+ '0o5_7_7',
+ '1_00_00.5',
+ '1_00_00.5e5',
+ '1_00_00e5_1',
+ '1e1_0',
+ '.1_4',
+ '.1_4e1',
+ '0b_0',
+ '0x_f',
+ '0o_5',
+ '1_00_00j',
+ '1_00_00.5j',
+ '1_00_00e5_1j',
+ '.1_4j',
+ '(1_2.5+3_3j)',
+ '(.5_6j)',
+]
+INVALID_UNDERSCORE_LITERALS = [
+ # Trailing underscores:
+ '0_',
+ '42_',
+ '1.4j_',
+ '0x_',
+ '0b1_',
+ '0xf_',
+ '0o5_',
+ '0 if 1_Else 1',
+ # Underscores in the base selector:
+ '0_b0',
+ '0_xf',
+ '0_o5',
+ # Old-style octal, still disallowed:
+ '0_7',
+ '09_99',
+ # Multiple consecutive underscores:
+ '4_______2',
+ '0.1__4',
+ '0.1__4j',
+ '0b1001__0100',
+ '0xffff__ffff',
+ '0x___',
+ '0o5__77',
+ '1e1__0',
+ '1e1__0j',
+ # Underscore right before a dot:
+ '1_.4',
+ '1_.4j',
+ # Underscore right after a dot:
+ '1._4',
+ '1._4j',
+ '._5',
+ '._5j',
+ # Underscore right after a sign:
+ '1.0e+_1',
+ '1.0e+_1j',
+ # Underscore right before j:
+ '1.4_j',
+ '1.4e5_j',
+ # Underscore right before e:
+ '1_e1',
+ '1.4_e1',
+ '1.4_e1j',
+ # Underscore right after e:
+ '1e_1',
+ '1.4e_1',
+ '1.4e_1j',
+ # Complex cases with parens:
+ '(1+1.5_j_)',
+ '(1+1.5_j)',
+]
+
class TokenTests(unittest.TestCase):
@@ -87,6 +176,14 @@ class TokenTests(unittest.TestCase):
self.assertEqual(1 if 0else 0, 0)
self.assertRaises(SyntaxError, eval, "0 if 1Else 0")
+ def test_underscore_literals(self):
+ for lit in VALID_UNDERSCORE_LITERALS:
+ self.assertEqual(eval(lit), eval(lit.replace('_', '')))
+ for lit in INVALID_UNDERSCORE_LITERALS:
+ self.assertRaises(SyntaxError, eval, lit)
+ # Sanity check: no literal begins with an underscore
+ self.assertRaises(NameError, eval, "_0")
+
def test_string_literals(self):
x = ''; y = ""; self.assertTrue(len(x) == 0 and x == y)
x = '\''; y = "'"; self.assertTrue(len(x) == 1 and x == y and ord(x) == 39)
@@ -139,6 +236,19 @@ the \'lazy\' dog.\n\
compile(s, "<test>", "exec")
self.assertIn("unexpected EOF", str(cm.exception))
+var_annot_global: int # a global annotated is necessary for test_var_annot
+
+# custom namespace for testing __annotations__
+
+class CNS:
+ def __init__(self):
+ self._dct = {}
+ def __setitem__(self, item, value):
+ self._dct[item.lower()] = value
+ def __getitem__(self, item):
+ return self._dct[item]
+
+
class GrammarTests(unittest.TestCase):
# single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
@@ -154,6 +264,160 @@ class GrammarTests(unittest.TestCase):
# testlist ENDMARKER
x = eval('1, 0 or 1')
+ def test_var_annot_basics(self):
+ # all these should be allowed
+ var1: int = 5
+ var2: [int, str]
+ my_lst = [42]
+ def one():
+ return 1
+ int.new_attr: int
+ [list][0]: type
+ my_lst[one()-1]: int = 5
+ self.assertEqual(my_lst, [5])
+
+ def test_var_annot_syntax_errors(self):
+ # parser pass
+ check_syntax_error(self, "def f: int")
+ check_syntax_error(self, "x: int: str")
+ check_syntax_error(self, "def f():\n"
+ " nonlocal x: int\n")
+ # AST pass
+ check_syntax_error(self, "[x, 0]: int\n")
+ check_syntax_error(self, "f(): int\n")
+ check_syntax_error(self, "(x,): int")
+ check_syntax_error(self, "def f():\n"
+ " (x, y): int = (1, 2)\n")
+ # symtable pass
+ check_syntax_error(self, "def f():\n"
+ " x: int\n"
+ " global x\n")
+ check_syntax_error(self, "def f():\n"
+ " global x\n"
+ " x: int\n")
+
+ def test_var_annot_basic_semantics(self):
+ # execution order
+ with self.assertRaises(ZeroDivisionError):
+ no_name[does_not_exist]: no_name_again = 1/0
+ with self.assertRaises(NameError):
+ no_name[does_not_exist]: 1/0 = 0
+ global var_annot_global
+
+ # function semantics
+ def f():
+ st: str = "Hello"
+ a.b: int = (1, 2)
+ return st
+ self.assertEqual(f.__annotations__, {})
+ def f_OK():
+ x: 1/0
+ f_OK()
+ def fbad():
+ x: int
+ print(x)
+ with self.assertRaises(UnboundLocalError):
+ fbad()
+ def f2bad():
+ (no_such_global): int
+ print(no_such_global)
+ try:
+ f2bad()
+ except Exception as e:
+ self.assertIs(type(e), NameError)
+
+ # class semantics
+ class C:
+ __foo: int
+ s: str = "attr"
+ z = 2
+ def __init__(self, x):
+ self.x: int = x
+ self.assertEqual(C.__annotations__, {'_C__foo': int, 's': str})
+ with self.assertRaises(NameError):
+ class CBad:
+ no_such_name_defined.attr: int = 0
+ with self.assertRaises(NameError):
+ class Cbad2(C):
+ x: int
+ x.y: list = []
+
+ def test_var_annot_metaclass_semantics(self):
+ class CMeta(type):
+ @classmethod
+ def __prepare__(metacls, name, bases, **kwds):
+ return {'__annotations__': CNS()}
+ class CC(metaclass=CMeta):
+ XX: 'ANNOT'
+ self.assertEqual(CC.__annotations__['xx'], 'ANNOT')
+
+ def test_var_annot_module_semantics(self):
+ with self.assertRaises(AttributeError):
+ print(test.__annotations__)
+ self.assertEqual(ann_module.__annotations__,
+ {1: 2, 'x': int, 'y': str, 'f': typing.Tuple[int, int]})
+ self.assertEqual(ann_module.M.__annotations__,
+ {'123': 123, 'o': type})
+ self.assertEqual(ann_module2.__annotations__, {})
+
+ def test_var_annot_in_module(self):
+ # check that functions fail the same way when executed
+ # outside of module where they were defined
+ from test.ann_module3 import f_bad_ann, g_bad_ann, D_bad_ann
+ with self.assertRaises(NameError):
+ f_bad_ann()
+ with self.assertRaises(NameError):
+ g_bad_ann()
+ with self.assertRaises(NameError):
+ D_bad_ann(5)
+
+ def test_var_annot_simple_exec(self):
+ gns = {}; lns= {}
+ exec("'docstring'\n"
+ "__annotations__[1] = 2\n"
+ "x: int = 5\n", gns, lns)
+ self.assertEqual(lns["__annotations__"], {1: 2, 'x': int})
+ with self.assertRaises(KeyError):
+ gns['__annotations__']
+
+ def test_var_annot_custom_maps(self):
+ # tests with custom locals() and __annotations__
+ ns = {'__annotations__': CNS()}
+ exec('X: int; Z: str = "Z"; (w): complex = 1j', ns)
+ self.assertEqual(ns['__annotations__']['x'], int)
+ self.assertEqual(ns['__annotations__']['z'], str)
+ with self.assertRaises(KeyError):
+ ns['__annotations__']['w']
+ nonloc_ns = {}
+ class CNS2:
+ def __init__(self):
+ self._dct = {}
+ def __setitem__(self, item, value):
+ nonlocal nonloc_ns
+ self._dct[item] = value
+ nonloc_ns[item] = value
+ def __getitem__(self, item):
+ return self._dct[item]
+ exec('x: int = 1', {}, CNS2())
+ self.assertEqual(nonloc_ns['__annotations__']['x'], int)
+
+ def test_var_annot_refleak(self):
+ # complex case: custom locals plus custom __annotations__
+ # this was causing refleak
+ cns = CNS()
+ nonloc_ns = {'__annotations__': cns}
+ class CNS2:
+ def __init__(self):
+ self._dct = {'__annotations__': cns}
+ def __setitem__(self, item, value):
+ nonlocal nonloc_ns
+ self._dct[item] = value
+ nonloc_ns[item] = value
+ def __getitem__(self, item):
+ return self._dct[item]
+ exec('X: str', {}, CNS2())
+ self.assertEqual(nonloc_ns['__annotations__']['x'], str)
+
def test_funcdef(self):
### [decorators] 'def' NAME parameters ['->' test] ':' suite
### decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
@@ -295,6 +559,10 @@ class GrammarTests(unittest.TestCase):
pos2key2dict(1,2,k2=100,tokwarg1=100,tokwarg2=200)
pos2key2dict(1,2,tokwarg1=100,tokwarg2=200, k2=100)
+ self.assertRaises(SyntaxError, eval, "def f(*): pass")
+ self.assertRaises(SyntaxError, eval, "def f(*,): pass")
+ self.assertRaises(SyntaxError, eval, "def f(*, **kwds): pass")
+
# keyword arguments after *arglist
def f(*args, **kwargs):
return args, kwargs
@@ -341,7 +609,7 @@ class GrammarTests(unittest.TestCase):
def f(x) -> list: pass
self.assertEqual(f.__annotations__, {'return': list})
- # test MAKE_CLOSURE with a variety of oparg's
+ # test closures with a variety of opargs
closure = 1
def f(): return closure
def f(x=1): return closure
@@ -352,6 +620,23 @@ class GrammarTests(unittest.TestCase):
check_syntax_error(self, "f(*g(1=2))")
check_syntax_error(self, "f(**g(1=2))")
+ # Check trailing commas are permitted in funcdef argument list
+ def f(a,): pass
+ def f(*args,): pass
+ def f(**kwds,): pass
+ def f(a, *args,): pass
+ def f(a, **kwds,): pass
+ def f(*args, b,): pass
+ def f(*, b,): pass
+ def f(*args, **kwds,): pass
+ def f(a, *args, b,): pass
+ def f(a, *, b,): pass
+ def f(a, *args, **kwds,): pass
+ def f(*args, b, **kwds,): pass
+ def f(*, b, **kwds,): pass
+ def f(a, *args, b, **kwds,): pass
+ def f(a, *, b, **kwds,): pass
+
def test_lambdef(self):
### lambdef: 'lambda' [varargslist] ':' test
l1 = lambda : 0
@@ -370,6 +655,23 @@ class GrammarTests(unittest.TestCase):
self.assertEqual(l6(1,2), 1+2+20)
self.assertEqual(l6(1,2,k=10), 1+2+10)
+ # check that trailing commas are permitted
+ l10 = lambda a,: 0
+ l11 = lambda *args,: 0
+ l12 = lambda **kwds,: 0
+ l13 = lambda a, *args,: 0
+ l14 = lambda a, **kwds,: 0
+ l15 = lambda *args, b,: 0
+ l16 = lambda *, b,: 0
+ l17 = lambda *args, **kwds,: 0
+ l18 = lambda a, *args, b,: 0
+ l19 = lambda a, *, b,: 0
+ l20 = lambda a, *args, **kwds,: 0
+ l21 = lambda *args, b, **kwds,: 0
+ l22 = lambda *, b, **kwds,: 0
+ l23 = lambda a, *args, b, **kwds,: 0
+ l24 = lambda a, *, b, **kwds,: 0
+
### stmt: simple_stmt | compound_stmt
# Tested below
@@ -1040,18 +1342,6 @@ class GrammarTests(unittest.TestCase):
self.assertEqual(m.other, 42)
def test_async_await(self):
- async = 1
- await = 2
- self.assertEqual(async, 1)
-
- def async():
- nonlocal await
- await = 10
- async()
- self.assertEqual(await, 10)
-
- self.assertFalse(bool(async.__code__.co_flags & inspect.CO_COROUTINE))
-
async def test():
def sum():
pass
diff --git a/Lib/test/test_grp.py b/Lib/test/test_grp.py
index 272b086..69095a3 100644
--- a/Lib/test/test_grp.py
+++ b/Lib/test/test_grp.py
@@ -92,5 +92,15 @@ class GroupDatabaseTestCase(unittest.TestCase):
self.assertRaises(KeyError, grp.getgrgid, fakegid)
+ def test_noninteger_gid(self):
+ entries = grp.getgrall()
+ if not entries:
+ self.skipTest('no groups')
+ # Choose an existent gid.
+ gid = entries[0][2]
+ self.assertWarns(DeprecationWarning, grp.getgrgid, float(gid))
+ self.assertWarns(DeprecationWarning, grp.getgrgid, str(gid))
+
+
if __name__ == "__main__":
unittest.main()
diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py
index 3c51673..b457bd3 100644
--- a/Lib/test/test_gzip.py
+++ b/Lib/test/test_gzip.py
@@ -5,6 +5,7 @@ import unittest
from test import support
from test.support import bigmemtest, _4G
import os
+import pathlib
import io
import struct
import array
@@ -67,6 +68,18 @@ class TestGzip(BaseTest):
# Test multiple close() calls.
f.close()
+ def test_write_read_with_pathlike_file(self):
+ filename = pathlib.Path(self.filename)
+ with gzip.GzipFile(filename, 'w') as f:
+ f.write(data1 * 50)
+ self.assertIsInstance(f.name, str)
+ with gzip.GzipFile(filename, 'a') as f:
+ f.write(data1)
+ with gzip.GzipFile(filename) as f:
+ d = f.read()
+ self.assertEqual(d, data1 * 51)
+ self.assertIsInstance(f.name, str)
+
# The following test_write_xy methods test that write accepts
# the corresponding bytes-like object type as input
# and that the data written equals bytes(xy) in all cases.
@@ -434,12 +447,12 @@ class TestGzip(BaseTest):
def test_decompress_limited(self):
"""Decompressed data buffering should be limited"""
- bomb = gzip.compress(bytes(int(2e6)), compresslevel=9)
+ bomb = gzip.compress(b'\0' * int(2e6), compresslevel=9)
self.assertLess(len(bomb), io.DEFAULT_BUFFER_SIZE)
bomb = io.BytesIO(bomb)
decomp = gzip.GzipFile(fileobj=bomb)
- self.assertEqual(bytes(1), decomp.read(1))
+ self.assertEqual(decomp.read(1), b'\0')
max_decomp = 1 + io.DEFAULT_BUFFER_SIZE
self.assertLessEqual(decomp._buffer.raw.tell(), max_decomp,
"Excessive amount of data was decompressed")
@@ -521,6 +534,15 @@ class TestOpen(BaseTest):
file_data = gzip.decompress(f.read())
self.assertEqual(file_data, uncompressed)
+ def test_pathlike_file(self):
+ filename = pathlib.Path(self.filename)
+ with gzip.open(filename, "wb") as f:
+ f.write(data1 * 50)
+ with gzip.open(filename, "ab") as f:
+ f.write(data1)
+ with gzip.open(filename) as f:
+ self.assertEqual(f.read(), data1 * 51)
+
def test_implicit_binary_modes(self):
# Test implicit binary modes (no "b" or "t" in mode string).
uncompressed = data1 * 50
diff --git a/Lib/test/test_hashlib.py b/Lib/test/test_hashlib.py
index c9b113e..f748b46 100644
--- a/Lib/test/test_hashlib.py
+++ b/Lib/test/test_hashlib.py
@@ -7,6 +7,7 @@
#
import array
+from binascii import unhexlify
import hashlib
import itertools
import os
@@ -19,6 +20,7 @@ import unittest
import warnings
from test import support
from test.support import _4G, bigmemtest, import_fresh_module
+from http.client import HTTPException
# Were we compiled --with-pydebug or with #define Py_DEBUG?
COMPILED_WITH_PYDEBUG = hasattr(sys, 'gettotalrefcount')
@@ -26,6 +28,21 @@ COMPILED_WITH_PYDEBUG = hasattr(sys, 'gettotalrefcount')
c_hashlib = import_fresh_module('hashlib', fresh=['_hashlib'])
py_hashlib = import_fresh_module('hashlib', blocked=['_hashlib'])
+try:
+ import _blake2
+except ImportError:
+ _blake2 = None
+
+requires_blake2 = unittest.skipUnless(_blake2, 'requires _blake2')
+
+try:
+ import _sha3
+except ImportError:
+ _sha3 = None
+
+requires_sha3 = unittest.skipUnless(_sha3, 'requires _sha3')
+
+
def hexstr(s):
assert isinstance(s, bytes), repr(s)
h = "0123456789abcdef"
@@ -35,10 +52,33 @@ def hexstr(s):
return r
+URL = "http://www.pythontest.net/hashlib/{}.txt"
+
+def read_vectors(hash_name):
+ url = URL.format(hash_name)
+ try:
+ testdata = support.open_urlresource(url)
+ except (OSError, HTTPException):
+ raise unittest.SkipTest("Could not retrieve {}".format(url))
+ with testdata:
+ for line in testdata:
+ line = line.strip()
+ if line.startswith('#') or not line:
+ continue
+ parts = line.split(',')
+ parts[0] = bytes.fromhex(parts[0])
+ yield parts
+
+
class HashLibTestCase(unittest.TestCase):
supported_hash_names = ( 'md5', 'MD5', 'sha1', 'SHA1',
'sha224', 'SHA224', 'sha256', 'SHA256',
- 'sha384', 'SHA384', 'sha512', 'SHA512')
+ 'sha384', 'SHA384', 'sha512', 'SHA512',
+ 'blake2b', 'blake2s',
+ 'sha3_224', 'sha3_256', 'sha3_384', 'sha3_512',
+ 'shake_128', 'shake_256')
+
+ shakes = {'shake_128', 'shake_256'}
# Issue #14693: fallback modules are always compiled under POSIX
_warn_on_extension_import = os.name == 'posix' or COMPILED_WITH_PYDEBUG
@@ -56,6 +96,11 @@ class HashLibTestCase(unittest.TestCase):
algorithms = set()
for algorithm in self.supported_hash_names:
algorithms.add(algorithm.lower())
+
+ _blake2 = self._conditional_import_module('_blake2')
+ if _blake2:
+ algorithms.update({'blake2b', 'blake2s'})
+
self.constructors_to_test = {}
for algorithm in algorithms:
self.constructors_to_test[algorithm] = set()
@@ -64,10 +109,10 @@ class HashLibTestCase(unittest.TestCase):
# of hashlib.new given the algorithm name.
for algorithm, constructors in self.constructors_to_test.items():
constructors.add(getattr(hashlib, algorithm))
- def _test_algorithm_via_hashlib_new(data=None, _alg=algorithm):
+ def _test_algorithm_via_hashlib_new(data=None, _alg=algorithm, **kwargs):
if data is None:
- return hashlib.new(_alg)
- return hashlib.new(_alg, data)
+ return hashlib.new(_alg, **kwargs)
+ return hashlib.new(_alg, data, **kwargs)
constructors.add(_test_algorithm_via_hashlib_new)
_hashlib = self._conditional_import_module('_hashlib')
@@ -99,6 +144,18 @@ class HashLibTestCase(unittest.TestCase):
if _sha512:
add_builtin_constructor('sha384')
add_builtin_constructor('sha512')
+ if _blake2:
+ add_builtin_constructor('blake2s')
+ add_builtin_constructor('blake2b')
+
+ _sha3 = self._conditional_import_module('_sha3')
+ if _sha3:
+ add_builtin_constructor('sha3_224')
+ add_builtin_constructor('sha3_256')
+ add_builtin_constructor('sha3_384')
+ add_builtin_constructor('sha3_512')
+ add_builtin_constructor('shake_128')
+ add_builtin_constructor('shake_256')
super(HashLibTestCase, self).__init__(*args, **kwargs)
@@ -111,7 +168,10 @@ class HashLibTestCase(unittest.TestCase):
a = array.array("b", range(10))
for cons in self.hash_constructors:
c = cons(a)
- c.hexdigest()
+ if c.name in self.shakes:
+ c.hexdigest(16)
+ else:
+ c.hexdigest()
def test_algorithms_guaranteed(self):
self.assertEqual(hashlib.algorithms_guaranteed,
@@ -155,14 +215,21 @@ class HashLibTestCase(unittest.TestCase):
def test_hexdigest(self):
for cons in self.hash_constructors:
h = cons()
- self.assertIsInstance(h.digest(), bytes)
- self.assertEqual(hexstr(h.digest()), h.hexdigest())
+ if h.name in self.shakes:
+ self.assertIsInstance(h.digest(16), bytes)
+ self.assertEqual(hexstr(h.digest(16)), h.hexdigest(16))
+ else:
+ self.assertIsInstance(h.digest(), bytes)
+ self.assertEqual(hexstr(h.digest()), h.hexdigest())
def test_name_attribute(self):
for cons in self.hash_constructors:
h = cons()
self.assertIsInstance(h.name, str)
- self.assertIn(h.name, self.supported_hash_names)
+ if h.name in self.supported_hash_names:
+ self.assertIn(h.name, self.supported_hash_names)
+ else:
+ self.assertNotIn(h.name, self.supported_hash_names)
self.assertEqual(h.name, hashlib.new(h.name).name)
def test_large_update(self):
@@ -177,40 +244,46 @@ class HashLibTestCase(unittest.TestCase):
m1.update(bees)
m1.update(cees)
m1.update(dees)
+ if m1.name in self.shakes:
+ args = (16,)
+ else:
+ args = ()
m2 = cons()
m2.update(aas + bees + cees + dees)
- self.assertEqual(m1.digest(), m2.digest())
+ self.assertEqual(m1.digest(*args), m2.digest(*args))
m3 = cons(aas + bees + cees + dees)
- self.assertEqual(m1.digest(), m3.digest())
+ self.assertEqual(m1.digest(*args), m3.digest(*args))
# verify copy() doesn't touch original
m4 = cons(aas + bees + cees)
- m4_digest = m4.digest()
+ m4_digest = m4.digest(*args)
m4_copy = m4.copy()
m4_copy.update(dees)
- self.assertEqual(m1.digest(), m4_copy.digest())
- self.assertEqual(m4.digest(), m4_digest)
+ self.assertEqual(m1.digest(*args), m4_copy.digest(*args))
+ self.assertEqual(m4.digest(*args), m4_digest)
- def check(self, name, data, hexdigest):
+ def check(self, name, data, hexdigest, shake=False, **kwargs):
+ length = len(hexdigest)//2
hexdigest = hexdigest.lower()
constructors = self.constructors_to_test[name]
# 2 is for hashlib.name(...) and hashlib.new(name, ...)
self.assertGreaterEqual(len(constructors), 2)
for hash_object_constructor in constructors:
- m = hash_object_constructor(data)
- computed = m.hexdigest()
+ m = hash_object_constructor(data, **kwargs)
+ computed = m.hexdigest() if not shake else m.hexdigest(length)
self.assertEqual(
computed, hexdigest,
"Hash algorithm %s constructed using %s returned hexdigest"
" %r for %d byte input data that should have hashed to %r."
% (name, hash_object_constructor,
computed, len(data), hexdigest))
- computed = m.digest()
+ computed = m.digest() if not shake else m.digest(length)
digest = bytes.fromhex(hexdigest)
self.assertEqual(computed, digest)
- self.assertEqual(len(digest), m.digest_size)
+ if not shake:
+ self.assertEqual(len(digest), m.digest_size)
def check_no_unicode(self, algorithm_name):
# Unicode objects are not allowed as input.
@@ -226,13 +299,35 @@ class HashLibTestCase(unittest.TestCase):
self.check_no_unicode('sha384')
self.check_no_unicode('sha512')
- def check_blocksize_name(self, name, block_size=0, digest_size=0):
+ @requires_blake2
+ def test_no_unicode_blake2(self):
+ self.check_no_unicode('blake2b')
+ self.check_no_unicode('blake2s')
+
+ @requires_sha3
+ def test_no_unicode_sha3(self):
+ self.check_no_unicode('sha3_224')
+ self.check_no_unicode('sha3_256')
+ self.check_no_unicode('sha3_384')
+ self.check_no_unicode('sha3_512')
+ self.check_no_unicode('shake_128')
+ self.check_no_unicode('shake_256')
+
+ def check_blocksize_name(self, name, block_size=0, digest_size=0,
+ digest_length=None):
constructors = self.constructors_to_test[name]
for hash_object_constructor in constructors:
m = hash_object_constructor()
self.assertEqual(m.block_size, block_size)
self.assertEqual(m.digest_size, digest_size)
- self.assertEqual(len(m.digest()), digest_size)
+ if digest_length:
+ self.assertEqual(len(m.digest(digest_length)),
+ digest_length)
+ self.assertEqual(len(m.hexdigest(digest_length)),
+ 2*digest_length)
+ else:
+ self.assertEqual(len(m.digest()), digest_size)
+ self.assertEqual(len(m.hexdigest()), 2*digest_size)
self.assertEqual(m.name, name)
# split for sha3_512 / _sha3.sha3 object
self.assertIn(name.split("_")[0], repr(m))
@@ -245,6 +340,38 @@ class HashLibTestCase(unittest.TestCase):
self.check_blocksize_name('sha384', 128, 48)
self.check_blocksize_name('sha512', 128, 64)
+ @requires_sha3
+ def test_blocksize_name_sha3(self):
+ self.check_blocksize_name('sha3_224', 144, 28)
+ self.check_blocksize_name('sha3_256', 136, 32)
+ self.check_blocksize_name('sha3_384', 104, 48)
+ self.check_blocksize_name('sha3_512', 72, 64)
+ self.check_blocksize_name('shake_128', 168, 0, 32)
+ self.check_blocksize_name('shake_256', 136, 0, 64)
+
+ def check_sha3(self, name, capacity, rate, suffix):
+ constructors = self.constructors_to_test[name]
+ for hash_object_constructor in constructors:
+ m = hash_object_constructor()
+ self.assertEqual(capacity + rate, 1600)
+ self.assertEqual(m._capacity_bits, capacity)
+ self.assertEqual(m._rate_bits, rate)
+ self.assertEqual(m._suffix, suffix)
+
+ @requires_sha3
+ def test_extra_sha3(self):
+ self.check_sha3('sha3_224', 448, 1152, b'\x06')
+ self.check_sha3('sha3_256', 512, 1088, b'\x06')
+ self.check_sha3('sha3_384', 768, 832, b'\x06')
+ self.check_sha3('sha3_512', 1024, 576, b'\x06')
+ self.check_sha3('shake_128', 256, 1344, b'\x1f')
+ self.check_sha3('shake_256', 512, 1088, b'\x1f')
+
+ @requires_blake2
+ def test_blocksize_name_blake2(self):
+ self.check_blocksize_name('blake2b', 128, 64)
+ self.check_blocksize_name('blake2s', 64, 32)
+
def test_case_md5_0(self):
self.check('md5', b'', 'd41d8cd98f00b204e9800998ecf8427e')
@@ -373,6 +500,221 @@ class HashLibTestCase(unittest.TestCase):
"e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"+
"de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b")
+ def check_blake2(self, constructor, salt_size, person_size, key_size,
+ digest_size, max_offset):
+ self.assertEqual(constructor.SALT_SIZE, salt_size)
+ for i in range(salt_size + 1):
+ constructor(salt=b'a' * i)
+ salt = b'a' * (salt_size + 1)
+ self.assertRaises(ValueError, constructor, salt=salt)
+
+ self.assertEqual(constructor.PERSON_SIZE, person_size)
+ for i in range(person_size+1):
+ constructor(person=b'a' * i)
+ person = b'a' * (person_size + 1)
+ self.assertRaises(ValueError, constructor, person=person)
+
+ self.assertEqual(constructor.MAX_DIGEST_SIZE, digest_size)
+ for i in range(1, digest_size + 1):
+ constructor(digest_size=i)
+ self.assertRaises(ValueError, constructor, digest_size=-1)
+ self.assertRaises(ValueError, constructor, digest_size=0)
+ self.assertRaises(ValueError, constructor, digest_size=digest_size+1)
+
+ self.assertEqual(constructor.MAX_KEY_SIZE, key_size)
+ for i in range(key_size+1):
+ constructor(key=b'a' * i)
+ key = b'a' * (key_size + 1)
+ self.assertRaises(ValueError, constructor, key=key)
+ self.assertEqual(constructor().hexdigest(),
+ constructor(key=b'').hexdigest())
+
+ for i in range(0, 256):
+ constructor(fanout=i)
+ self.assertRaises(ValueError, constructor, fanout=-1)
+ self.assertRaises(ValueError, constructor, fanout=256)
+
+ for i in range(1, 256):
+ constructor(depth=i)
+ self.assertRaises(ValueError, constructor, depth=-1)
+ self.assertRaises(ValueError, constructor, depth=0)
+ self.assertRaises(ValueError, constructor, depth=256)
+
+ for i in range(0, 256):
+ constructor(node_depth=i)
+ self.assertRaises(ValueError, constructor, node_depth=-1)
+ self.assertRaises(ValueError, constructor, node_depth=256)
+
+ for i in range(0, digest_size + 1):
+ constructor(inner_size=i)
+ self.assertRaises(ValueError, constructor, inner_size=-1)
+ self.assertRaises(ValueError, constructor, inner_size=digest_size+1)
+
+ constructor(leaf_size=0)
+ constructor(leaf_size=(1<<32)-1)
+ self.assertRaises(OverflowError, constructor, leaf_size=-1)
+ self.assertRaises(OverflowError, constructor, leaf_size=1<<32)
+
+ constructor(node_offset=0)
+ constructor(node_offset=max_offset)
+ self.assertRaises(OverflowError, constructor, node_offset=-1)
+ self.assertRaises(OverflowError, constructor, node_offset=max_offset+1)
+
+ constructor(
+ string=b'',
+ key=b'',
+ salt=b'',
+ person=b'',
+ digest_size=17,
+ fanout=1,
+ depth=1,
+ leaf_size=256,
+ node_offset=512,
+ node_depth=1,
+ inner_size=7,
+ last_node=True
+ )
+
+ def blake2_rfc7693(self, constructor, md_len, in_len):
+ def selftest_seq(length, seed):
+ mask = (1<<32)-1
+ a = (0xDEAD4BAD * seed) & mask
+ b = 1
+ out = bytearray(length)
+ for i in range(length):
+ t = (a + b) & mask
+ a, b = b, t
+ out[i] = (t >> 24) & 0xFF
+ return out
+ outer = constructor(digest_size=32)
+ for outlen in md_len:
+ for inlen in in_len:
+ indata = selftest_seq(inlen, inlen)
+ key = selftest_seq(outlen, outlen)
+ unkeyed = constructor(indata, digest_size=outlen)
+ outer.update(unkeyed.digest())
+ keyed = constructor(indata, key=key, digest_size=outlen)
+ outer.update(keyed.digest())
+ return outer.hexdigest()
+
+ @requires_blake2
+ def test_blake2b(self):
+ self.check_blake2(hashlib.blake2b, 16, 16, 64, 64, (1<<64)-1)
+ b2b_md_len = [20, 32, 48, 64]
+ b2b_in_len = [0, 3, 128, 129, 255, 1024]
+ self.assertEqual(
+ self.blake2_rfc7693(hashlib.blake2b, b2b_md_len, b2b_in_len),
+ "c23a7800d98123bd10f506c61e29da5603d763b8bbad2e737f5e765a7bccd475")
+
+ @requires_blake2
+ def test_case_blake2b_0(self):
+ self.check('blake2b', b"",
+ "786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419"+
+ "d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce")
+
+ @requires_blake2
+ def test_case_blake2b_1(self):
+ self.check('blake2b', b"abc",
+ "ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d1"+
+ "7d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923")
+
+ @requires_blake2
+ def test_blake2b_vectors(self):
+ for msg, key, md in read_vectors('blake2b'):
+ key = bytes.fromhex(key)
+ self.check('blake2b', msg, md, key=key)
+
+ @requires_blake2
+ def test_blake2s(self):
+ self.check_blake2(hashlib.blake2s, 8, 8, 32, 32, (1<<48)-1)
+ b2s_md_len = [16, 20, 28, 32]
+ b2s_in_len = [0, 3, 64, 65, 255, 1024]
+ self.assertEqual(
+ self.blake2_rfc7693(hashlib.blake2s, b2s_md_len, b2s_in_len),
+ "6a411f08ce25adcdfb02aba641451cec53c598b24f4fc787fbdc88797f4c1dfe")
+
+ @requires_blake2
+ def test_case_blake2s_0(self):
+ self.check('blake2s', b"",
+ "69217a3079908094e11121d042354a7c1f55b6482ca1a51e1b250dfd1ed0eef9")
+
+ @requires_blake2
+ def test_case_blake2s_1(self):
+ self.check('blake2s', b"abc",
+ "508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982")
+
+ @requires_blake2
+ def test_blake2s_vectors(self):
+ for msg, key, md in read_vectors('blake2s'):
+ key = bytes.fromhex(key)
+ self.check('blake2s', msg, md, key=key)
+
+ @requires_sha3
+ def test_case_sha3_224_0(self):
+ self.check('sha3_224', b"",
+ "6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7")
+
+ @requires_sha3
+ def test_case_sha3_224_vector(self):
+ for msg, md in read_vectors('sha3_224'):
+ self.check('sha3_224', msg, md)
+
+ @requires_sha3
+ def test_case_sha3_256_0(self):
+ self.check('sha3_256', b"",
+ "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a")
+
+ @requires_sha3
+ def test_case_sha3_256_vector(self):
+ for msg, md in read_vectors('sha3_256'):
+ self.check('sha3_256', msg, md)
+
+ @requires_sha3
+ def test_case_sha3_384_0(self):
+ self.check('sha3_384', b"",
+ "0c63a75b845e4f7d01107d852e4c2485c51a50aaaa94fc61995e71bbee983a2a"+
+ "c3713831264adb47fb6bd1e058d5f004")
+
+ @requires_sha3
+ def test_case_sha3_384_vector(self):
+ for msg, md in read_vectors('sha3_384'):
+ self.check('sha3_384', msg, md)
+
+ @requires_sha3
+ def test_case_sha3_512_0(self):
+ self.check('sha3_512', b"",
+ "a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a6"+
+ "15b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26")
+
+ @requires_sha3
+ def test_case_sha3_512_vector(self):
+ for msg, md in read_vectors('sha3_512'):
+ self.check('sha3_512', msg, md)
+
+ @requires_sha3
+ def test_case_shake_128_0(self):
+ self.check('shake_128', b"",
+ "7f9c2ba4e88f827d616045507605853ed73b8093f6efbc88eb1a6eacfa66ef26",
+ True)
+ self.check('shake_128', b"", "7f9c", True)
+
+ @requires_sha3
+ def test_case_shake128_vector(self):
+ for msg, md in read_vectors('shake_128'):
+ self.check('shake_128', msg, md, True)
+
+ @requires_sha3
+ def test_case_shake_256_0(self):
+ self.check('shake_256', b"",
+ "46b9dd2b0ba88d13233b3feb743eeb243fcd52ea62b81b82b50c27646ed5762f",
+ True)
+ self.check('shake_256', b"", "46b9", True)
+
+ @requires_sha3
+ def test_case_shake256_vector(self):
+ for msg, md in read_vectors('shake_256'):
+ self.check('shake_256', msg, md, True)
+
def test_gil(self):
# Check things work fine with an input larger than the size required
# for multithreaded operation (which is hardwired to 2048).
@@ -447,6 +789,12 @@ class KDFTests(unittest.TestCase):
(b'pass\0word', b'sa\0lt', 4096, 16),
]
+ scrypt_test_vectors = [
+ (b'', b'', 16, 1, 1, unhexlify('77d6576238657b203b19ca42c18a0497f16b4844e3074ae8dfdffa3fede21442fcd0069ded0948f8326a753a0fc81f17e8d3e0fb2e0d3628cf35e20c38d18906')),
+ (b'password', b'NaCl', 1024, 8, 16, unhexlify('fdbabe1c9d3472007856e7190d01e9fe7c6ad7cbc8237830e77376634b3731622eaf30d92e22a3886ff109279d9830dac727afb94a83ee6d8360cbdfa2cc0640')),
+ (b'pleaseletmein', b'SodiumChloride', 16384, 8, 1, unhexlify('7023bdcb3afd7348461c06cd81fd38ebfda8fbba904f8e3ea9b543f6545da1f2d5432955613f0fcf62d49705242a9af9e61e85dc0d651e40dfcf017b45575887')),
+ ]
+
pbkdf2_results = {
"sha1": [
# official test vectors from RFC 6070
@@ -526,5 +874,45 @@ class KDFTests(unittest.TestCase):
self._test_pbkdf2_hmac(c_hashlib.pbkdf2_hmac)
+ @unittest.skipUnless(hasattr(c_hashlib, 'scrypt'),
+ ' test requires OpenSSL > 1.1')
+ def test_scrypt(self):
+ for password, salt, n, r, p, expected in self.scrypt_test_vectors:
+ result = hashlib.scrypt(password, salt=salt, n=n, r=r, p=p)
+ self.assertEqual(result, expected)
+
+ # this values should work
+ hashlib.scrypt(b'password', salt=b'salt', n=2, r=8, p=1)
+ # password and salt must be bytes-like
+ with self.assertRaises(TypeError):
+ hashlib.scrypt('password', salt=b'salt', n=2, r=8, p=1)
+ with self.assertRaises(TypeError):
+ hashlib.scrypt(b'password', salt='salt', n=2, r=8, p=1)
+ # require keyword args
+ with self.assertRaises(TypeError):
+ hashlib.scrypt(b'password')
+ with self.assertRaises(TypeError):
+ hashlib.scrypt(b'password', b'salt')
+ with self.assertRaises(TypeError):
+ hashlib.scrypt(b'password', 2, 8, 1, salt=b'salt')
+ for n in [-1, 0, 1, None]:
+ with self.assertRaises((ValueError, OverflowError, TypeError)):
+ hashlib.scrypt(b'password', salt=b'salt', n=n, r=8, p=1)
+ for r in [-1, 0, None]:
+ with self.assertRaises((ValueError, OverflowError, TypeError)):
+ hashlib.scrypt(b'password', salt=b'salt', n=2, r=r, p=1)
+ for p in [-1, 0, None]:
+ with self.assertRaises((ValueError, OverflowError, TypeError)):
+ hashlib.scrypt(b'password', salt=b'salt', n=2, r=8, p=p)
+ for maxmem in [-1, None]:
+ with self.assertRaises((ValueError, OverflowError, TypeError)):
+ hashlib.scrypt(b'password', salt=b'salt', n=2, r=8, p=1,
+ maxmem=maxmem)
+ for dklen in [-1, None]:
+ with self.assertRaises((ValueError, OverflowError, TypeError)):
+ hashlib.scrypt(b'password', salt=b'salt', n=2, r=8, p=1,
+ dklen=dklen)
+
+
if __name__ == "__main__":
unittest.main()
diff --git a/Lib/test/test_heapq.py b/Lib/test/test_heapq.py
index b7e8259..2f8c648 100644
--- a/Lib/test/test_heapq.py
+++ b/Lib/test/test_heapq.py
@@ -1,6 +1,5 @@
"""Unittests for heapq."""
-import sys
import random
import unittest
diff --git a/Lib/test/test_hmac.py b/Lib/test/test_hmac.py
index 98826b5..067e13f 100644
--- a/Lib/test/test_hmac.py
+++ b/Lib/test/test_hmac.py
@@ -3,7 +3,6 @@ import hmac
import hashlib
import unittest
import warnings
-from test import support
def ignore_warning(func):
diff --git a/Lib/test/test_htmlparser.py b/Lib/test/test_htmlparser.py
index 11420b2c..326e342 100644
--- a/Lib/test/test_htmlparser.py
+++ b/Lib/test/test_htmlparser.py
@@ -3,7 +3,6 @@
import html.parser
import pprint
import unittest
-from test import support
class EventCollector(html.parser.HTMLParser):
@@ -702,7 +701,7 @@ class AttributesTestCase(TestCaseBase):
def test_attr_funky_names2(self):
self._run_check(
- "<a $><b $=%><c \=/>",
+ r"<a $><b $=%><c \=/>",
[("starttag", "a", [("$", None)]),
("starttag", "b", [("$", "%")]),
("starttag", "c", [("\\", "/")])])
diff --git a/Lib/test/test_http_cookiejar.py b/Lib/test/test_http_cookiejar.py
index 49c01ae..6fee4df 100644
--- a/Lib/test/test_http_cookiejar.py
+++ b/Lib/test/test_http_cookiejar.py
@@ -1051,7 +1051,7 @@ class CookieTests(unittest.TestCase):
url = "http://foo.bar.com/"
interact_2965(c, url, "spam=eggs; Version=1; Port")
h = interact_2965(c, url)
- self.assertRegex(h, "\$Port([^=]|$)",
+ self.assertRegex(h, r"\$Port([^=]|$)",
"port with no value not returned with no value")
c = CookieJar(pol)
@@ -1396,9 +1396,9 @@ class LWPCookieTests(unittest.TestCase):
self.assertRegex(cookie, r'^\$Version="?1"?;')
self.assertRegex(cookie, r'Part_Number="?Rocket_Launcher_0001"?;'
- '\s*\$Path="\/acme"')
+ r'\s*\$Path="\/acme"')
self.assertRegex(cookie, r'Customer="?WILE_E_COYOTE"?;'
- '\s*\$Path="\/acme"')
+ r'\s*\$Path="\/acme"')
#
# 7. User Agent -> Server
diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py
index 61ed6bb..68f6946 100644
--- a/Lib/test/test_httplib.py
+++ b/Lib/test/test_httplib.py
@@ -344,6 +344,134 @@ class HeaderTests(TestCase):
conn.putheader(name, value)
+class TransferEncodingTest(TestCase):
+ expected_body = b"It's just a flesh wound"
+
+ def test_endheaders_chunked(self):
+ conn = client.HTTPConnection('example.com')
+ conn.sock = FakeSocket(b'')
+ conn.putrequest('POST', '/')
+ conn.endheaders(self._make_body(), encode_chunked=True)
+
+ _, _, body = self._parse_request(conn.sock.data)
+ body = self._parse_chunked(body)
+ self.assertEqual(body, self.expected_body)
+
+ def test_explicit_headers(self):
+ # explicit chunked
+ conn = client.HTTPConnection('example.com')
+ conn.sock = FakeSocket(b'')
+ # this shouldn't actually be automatically chunk-encoded because the
+ # calling code has explicitly stated that it's taking care of it
+ conn.request(
+ 'POST', '/', self._make_body(), {'Transfer-Encoding': 'chunked'})
+
+ _, headers, body = self._parse_request(conn.sock.data)
+ self.assertNotIn('content-length', [k.lower() for k in headers.keys()])
+ self.assertEqual(headers['Transfer-Encoding'], 'chunked')
+ self.assertEqual(body, self.expected_body)
+
+ # explicit chunked, string body
+ conn = client.HTTPConnection('example.com')
+ conn.sock = FakeSocket(b'')
+ conn.request(
+ 'POST', '/', self.expected_body.decode('latin-1'),
+ {'Transfer-Encoding': 'chunked'})
+
+ _, headers, body = self._parse_request(conn.sock.data)
+ self.assertNotIn('content-length', [k.lower() for k in headers.keys()])
+ self.assertEqual(headers['Transfer-Encoding'], 'chunked')
+ self.assertEqual(body, self.expected_body)
+
+ # User-specified TE, but request() does the chunk encoding
+ conn = client.HTTPConnection('example.com')
+ conn.sock = FakeSocket(b'')
+ conn.request('POST', '/',
+ headers={'Transfer-Encoding': 'gzip, chunked'},
+ encode_chunked=True,
+ body=self._make_body())
+ _, headers, body = self._parse_request(conn.sock.data)
+ self.assertNotIn('content-length', [k.lower() for k in headers])
+ self.assertEqual(headers['Transfer-Encoding'], 'gzip, chunked')
+ self.assertEqual(self._parse_chunked(body), self.expected_body)
+
+ def test_request(self):
+ for empty_lines in (False, True,):
+ conn = client.HTTPConnection('example.com')
+ conn.sock = FakeSocket(b'')
+ conn.request(
+ 'POST', '/', self._make_body(empty_lines=empty_lines))
+
+ _, headers, body = self._parse_request(conn.sock.data)
+ body = self._parse_chunked(body)
+ self.assertEqual(body, self.expected_body)
+ self.assertEqual(headers['Transfer-Encoding'], 'chunked')
+
+ # Content-Length and Transfer-Encoding SHOULD not be sent in the
+ # same request
+ self.assertNotIn('content-length', [k.lower() for k in headers])
+
+ def test_empty_body(self):
+ # Zero-length iterable should be treated like any other iterable
+ conn = client.HTTPConnection('example.com')
+ conn.sock = FakeSocket(b'')
+ conn.request('POST', '/', ())
+ _, headers, body = self._parse_request(conn.sock.data)
+ self.assertEqual(headers['Transfer-Encoding'], 'chunked')
+ self.assertNotIn('content-length', [k.lower() for k in headers])
+ self.assertEqual(body, b"0\r\n\r\n")
+
+ def _make_body(self, empty_lines=False):
+ lines = self.expected_body.split(b' ')
+ for idx, line in enumerate(lines):
+ # for testing handling empty lines
+ if empty_lines and idx % 2:
+ yield b''
+ if idx < len(lines) - 1:
+ yield line + b' '
+ else:
+ yield line
+
+ def _parse_request(self, data):
+ lines = data.split(b'\r\n')
+ request = lines[0]
+ headers = {}
+ n = 1
+ while n < len(lines) and len(lines[n]) > 0:
+ key, val = lines[n].split(b':')
+ key = key.decode('latin-1').strip()
+ headers[key] = val.decode('latin-1').strip()
+ n += 1
+
+ return request, headers, b'\r\n'.join(lines[n + 1:])
+
+ def _parse_chunked(self, data):
+ body = []
+ trailers = {}
+ n = 0
+ lines = data.split(b'\r\n')
+ # parse body
+ while True:
+ size, chunk = lines[n:n+2]
+ size = int(size, 16)
+
+ if size == 0:
+ n += 1
+ break
+
+ self.assertEqual(size, len(chunk))
+ body.append(chunk)
+
+ n += 2
+ # we /should/ hit the end chunk, but check against the size of
+ # lines so we're not stuck in an infinite loop should we get
+ # malformed data
+ if n > len(lines):
+ break
+
+ return b''.join(body)
+
+
class BasicTest(TestCase):
def test_status_lines(self):
# Test HTTP status lines
@@ -564,7 +692,9 @@ class BasicTest(TestCase):
def test_send_file(self):
expected = (b'GET /foo HTTP/1.1\r\nHost: example.com\r\n'
- b'Accept-Encoding: identity\r\nContent-Length:')
+ b'Accept-Encoding: identity\r\n'
+ b'Transfer-Encoding: chunked\r\n'
+ b'\r\n')
with open(__file__, 'rb') as body:
conn = client.HTTPConnection('example.com')
@@ -594,11 +724,11 @@ class BasicTest(TestCase):
yield None
yield 'data_two'
- class UpdatingFile():
+ class UpdatingFile(io.TextIOBase):
mode = 'r'
d = data()
def read(self, blocksize=-1):
- return self.d.__next__()
+ return next(self.d)
expected = b'data'
@@ -970,6 +1100,7 @@ class BasicTest(TestCase):
thread = threading.Thread(target=run_server)
thread.start()
+ self.addCleanup(thread.join, float(1))
conn = client.HTTPConnection(*serv.getsockname())
conn.request("CONNECT", "dummy:1234")
response = conn.getresponse()
@@ -983,7 +1114,7 @@ class BasicTest(TestCase):
finally:
response.close()
conn.close()
- thread.join()
+ thread.join()
self.assertEqual(result, b"proxied data\n")
class ExtendedReadTest(TestCase):
@@ -1511,14 +1642,16 @@ class HTTPSTest(TestCase):
with self.assertRaises(ssl.CertificateError):
h.request('GET', '/')
# Same with explicit check_hostname=True
- h = client.HTTPSConnection('localhost', server.port, context=context,
- check_hostname=True)
+ with support.check_warnings(('', DeprecationWarning)):
+ h = client.HTTPSConnection('localhost', server.port,
+ context=context, check_hostname=True)
with self.assertRaises(ssl.CertificateError):
h.request('GET', '/')
# With check_hostname=False, the mismatching is ignored
context.check_hostname = False
- h = client.HTTPSConnection('localhost', server.port, context=context,
- check_hostname=False)
+ with support.check_warnings(('', DeprecationWarning)):
+ h = client.HTTPSConnection('localhost', server.port,
+ context=context, check_hostname=False)
h.request('GET', '/nonexistent')
resp = h.getresponse()
resp.close()
@@ -1535,8 +1668,9 @@ class HTTPSTest(TestCase):
h.close()
# Passing check_hostname to HTTPSConnection should override the
# context's setting.
- h = client.HTTPSConnection('localhost', server.port, context=context,
- check_hostname=True)
+ with support.check_warnings(('', DeprecationWarning)):
+ h = client.HTTPSConnection('localhost', server.port,
+ context=context, check_hostname=True)
with self.assertRaises(ssl.CertificateError):
h.request('GET', '/')
@@ -1575,6 +1709,26 @@ class RequestBodyTest(TestCase):
message = client.parse_headers(f)
return message, f
+ def test_list_body(self):
+ # Note that no content-length is automatically calculated for
+ # an iterable. The request will fall back to send chunked
+ # transfer encoding.
+ cases = (
+ ([b'foo', b'bar'], b'3\r\nfoo\r\n3\r\nbar\r\n0\r\n\r\n'),
+ ((b'foo', b'bar'), b'3\r\nfoo\r\n3\r\nbar\r\n0\r\n\r\n'),
+ )
+ for body, expected in cases:
+ with self.subTest(body):
+ self.conn = client.HTTPConnection('example.com')
+ self.conn.sock = self.sock = FakeSocket('')
+
+ self.conn.request('PUT', '/url', body)
+ msg, f = self.get_headers_and_fp()
+ self.assertNotIn('Content-Type', msg)
+ self.assertNotIn('Content-Length', msg)
+ self.assertEqual(msg.get('Transfer-Encoding'), 'chunked')
+ self.assertEqual(expected, f.read())
+
def test_manual_content_length(self):
# Set an incorrect content-length so that we can verify that
# it will not be over-ridden by the library.
@@ -1608,7 +1762,7 @@ class RequestBodyTest(TestCase):
self.assertEqual("5", message.get("content-length"))
self.assertEqual(b'body\xc1', f.read())
- def test_file_body(self):
+ def test_text_file_body(self):
self.addCleanup(support.unlink, support.TESTFN)
with open(support.TESTFN, "w") as f:
f.write("body")
@@ -1617,8 +1771,11 @@ class RequestBodyTest(TestCase):
message, f = self.get_headers_and_fp()
self.assertEqual("text/plain", message.get_content_type())
self.assertIsNone(message.get_charset())
- self.assertEqual("4", message.get("content-length"))
- self.assertEqual(b'body', f.read())
+ # No content-length will be determined for files; the body
+ # will be sent using chunked transfer encoding instead.
+ self.assertIsNone(message.get("content-length"))
+ self.assertEqual("chunked", message.get("transfer-encoding"))
+ self.assertEqual(b'4\r\nbody\r\n0\r\n\r\n', f.read())
def test_binary_file_body(self):
self.addCleanup(support.unlink, support.TESTFN)
@@ -1629,8 +1786,9 @@ class RequestBodyTest(TestCase):
message, f = self.get_headers_and_fp()
self.assertEqual("text/plain", message.get_content_type())
self.assertIsNone(message.get_charset())
- self.assertEqual("5", message.get("content-length"))
- self.assertEqual(b'body\xc1', f.read())
+ self.assertEqual("chunked", message.get("Transfer-Encoding"))
+ self.assertNotIn("Content-Length", message)
+ self.assertEqual(b'5\r\nbody\xc1\r\n0\r\n\r\n', f.read())
class HTTPResponseTest(TestCase):
@@ -1741,13 +1899,5 @@ class TunnelTests(TestCase):
self.assertIn('header: {}'.format(expected_header), lines)
-@support.reap_threads
-def test_main(verbose=None):
- support.run_unittest(HeaderTests, OfflineTest, BasicTest, TimeoutTest,
- PersistenceTest,
- HTTPSTest, RequestBodyTest, SourceAddressTest,
- HTTPResponseTest, ExtendedReadTest,
- ExtendedReadTestChunked, TunnelTests)
-
if __name__ == '__main__':
- test_main()
+ unittest.main(verbosity=2)
diff --git a/Lib/test/test_httpservers.py b/Lib/test/test_httpservers.py
index 72e6e08..4e93144 100644
--- a/Lib/test/test_httpservers.py
+++ b/Lib/test/test_httpservers.py
@@ -18,6 +18,7 @@ import urllib.parse
import html
import http.client
import tempfile
+import time
from io import BytesIO
import unittest
@@ -189,7 +190,7 @@ class BaseHTTPServerTestCase(BaseTestCase):
res = self.con.getresponse()
self.assertEqual(res.status, HTTPStatus.NOT_IMPLEMENTED)
- def test_head_keep_alive(self):
+ def test_header_keep_alive(self):
self.con._http_vsn_str = 'HTTP/1.1'
self.con.putrequest('GET', '/')
self.con.putheader('Connection', 'keep-alive')
@@ -369,6 +370,8 @@ class SimpleHTTPServerTestCase(BaseTestCase):
return body
@support.requires_mac_ver(10, 5)
+ @unittest.skipIf(sys.platform == 'win32',
+ 'undecodable name cannot be decoded on win32')
@unittest.skipUnless(support.TESTFN_UNDECODABLE,
'need support.TESTFN_UNDECODABLE')
def test_undecodable_filename(self):
@@ -388,7 +391,7 @@ class SimpleHTTPServerTestCase(BaseTestCase):
quotedname = urllib.parse.quote(filename, errors='surrogatepass')
self.assertIn(('href="%s"' % quotedname)
.encode(enc, 'surrogateescape'), body)
- self.assertIn(('>%s<' % html.escape(filename))
+ self.assertIn(('>%s<' % html.escape(filename, quote=False))
.encode(enc, 'surrogateescape'), body)
response = self.request(self.base_url + '/' + quotedname)
self.check_status_and_reason(response, HTTPStatus.OK,
@@ -466,6 +469,27 @@ class SimpleHTTPServerTestCase(BaseTestCase):
self.assertEqual(response.getheader("Location"),
self.tempdir_name + "/?hi=1")
+ def test_html_escape_filename(self):
+ filename = '<test&>.txt'
+ fullpath = os.path.join(self.tempdir, filename)
+
+ try:
+ open(fullpath, 'w').close()
+ except OSError:
+ raise unittest.SkipTest('Can not create file %s on current file '
+ 'system' % filename)
+
+ try:
+ response = self.request(self.base_url + '/')
+ body = self.check_status_and_reason(response, HTTPStatus.OK)
+ enc = response.headers.get_content_charset()
+ finally:
+ os.unlink(fullpath) # avoid affecting test_undecodable_filename
+
+ self.assertIsNotNone(enc)
+ html_text = '>%s<' % html.escape(filename, quote=False)
+ self.assertIn(html_text.encode(enc), body)
+
cgi_file1 = """\
#!%s
@@ -916,7 +940,7 @@ class BaseHTTPRequestHandlerTestCase(unittest.TestCase):
# Issue #6791: same for headers
result = self.send_typical_request(
b'GET / HTTP/1.1\r\nX-Foo: bar' + b'r' * 65537 + b'\r\n\r\n')
- self.assertEqual(result[0], b'HTTP/1.1 400 Line too long\r\n')
+ self.assertEqual(result[0], b'HTTP/1.1 431 Line too long\r\n')
self.assertFalse(self.handler.get_called)
self.assertEqual(self.handler.requestline, 'GET / HTTP/1.1')
@@ -927,6 +951,13 @@ class BaseHTTPRequestHandlerTestCase(unittest.TestCase):
self.assertFalse(self.handler.get_called)
self.assertEqual(self.handler.requestline, 'GET / HTTP/1.1')
+ def test_html_escape_on_error(self):
+ result = self.send_typical_request(
+ b'<script>alert("hello")</script> / HTTP/1.1')
+ result = b''.join(result)
+ text = '<script>alert("hello")</script>'
+ self.assertIn(html.escape(text, quote=False).encode('ascii'), result)
+
def test_close_connection(self):
# handle_one_request() should be repeatedly called until
# it sets close_connection
@@ -942,6 +973,19 @@ class BaseHTTPRequestHandlerTestCase(unittest.TestCase):
self.handler.handle()
self.assertRaises(StopIteration, next, close_values)
+ def test_date_time_string(self):
+ now = time.time()
+ # this is the old code that formats the timestamp
+ year, month, day, hh, mm, ss, wd, y, z = time.gmtime(now)
+ expected = "%s, %02d %3s %4d %02d:%02d:%02d GMT" % (
+ self.handler.weekdayname[wd],
+ day,
+ self.handler.monthname[month],
+ year, hh, mm, ss
+ )
+ self.assertEqual(self.handler.date_time_string(timestamp=now), expected)
+
+
class SimpleHTTPRequestHandlerTestCase(unittest.TestCase):
""" Test url parsing """
def setUp(self):
diff --git a/Lib/test/test_idle.py b/Lib/test/test_idle.py
index 141e89e..da05da5 100644
--- a/Lib/test/test_idle.py
+++ b/Lib/test/test_idle.py
@@ -1,16 +1,23 @@
import unittest
-from test import support
from test.support import import_module
-# Skip test if _thread or _tkinter wasn't built or idlelib was deleted.
+# Skip test if _thread or _tkinter wasn't built, if idlelib is missing,
+# or if tcl/tk is not the 8.5+ needed for ttk widgets.
import_module('threading') # imported by PyShell, imports _thread
tk = import_module('tkinter') # imports _tkinter
-idletest = import_module('idlelib.idle_test')
+if tk.TkVersion < 8.5:
+ raise unittest.SkipTest("IDLE requires tk 8.5 or later.")
+idlelib = import_module('idlelib')
-# Without test_main present, regrtest.runtest_inner (line1219) calls
-# unittest.TestLoader().loadTestsFromModule(this_module) which calls
-# load_tests() if it finds it. (Unittest.main does the same.)
-load_tests = idletest.load_tests
+# Before test imports, tell IDLE to avoid changing the environment.
+idlelib.testing = True
+
+# unittest.main and test.libregrtest.runtest.runtest_inner
+# call load_tests, when present, to discover tests to run.
+from idlelib.idle_test import load_tests
if __name__ == '__main__':
- unittest.main(verbosity=2, exit=False)
+ tk.NoDefaultRoot()
+ unittest.main(exit=False)
+ tk._support_default_root = 1
+ tk._default_root = None
diff --git a/Lib/test/test_imaplib.py b/Lib/test/test_imaplib.py
index a29b40a..fac6b57 100644
--- a/Lib/test/test_imaplib.py
+++ b/Lib/test/test_imaplib.py
@@ -79,9 +79,9 @@ if ssl:
def get_request(self):
newsocket, fromaddr = self.socket.accept()
- connstream = ssl.wrap_socket(newsocket,
- server_side=True,
- certfile=CERTFILE)
+ context = ssl.SSLContext()
+ context.load_cert_chain(CERTFILE)
+ connstream = context.wrap_socket(newsocket, server_side=True)
return connstream, fromaddr
IMAP4_SSL = imaplib.IMAP4_SSL
@@ -573,6 +573,55 @@ class ThreadedNetworkedTests(unittest.TestCase):
client.shutdown()
@reap_threads
+ def test_bracket_flags(self):
+
+ # This violates RFC 3501, which disallows ']' characters in tag names,
+ # but imaplib has allowed producing such tags forever, other programs
+ # also produce them (eg: OtherInbox's Organizer app as of 20140716),
+ # and Gmail, for example, accepts them and produces them. So we
+ # support them. See issue #21815.
+
+ class BracketFlagHandler(SimpleIMAPHandler):
+
+ def handle(self):
+ self.flags = ['Answered', 'Flagged', 'Deleted', 'Seen', 'Draft']
+ super().handle()
+
+ def cmd_AUTHENTICATE(self, tag, args):
+ self._send_textline('+')
+ self.server.response = yield
+ self._send_tagged(tag, 'OK', 'FAKEAUTH successful')
+
+ def cmd_SELECT(self, tag, args):
+ flag_msg = ' \\'.join(self.flags)
+ self._send_line(('* FLAGS (%s)' % flag_msg).encode('ascii'))
+ self._send_line(b'* 2 EXISTS')
+ self._send_line(b'* 0 RECENT')
+ msg = ('* OK [PERMANENTFLAGS %s \\*)] Flags permitted.'
+ % flag_msg)
+ self._send_line(msg.encode('ascii'))
+ self._send_tagged(tag, 'OK', '[READ-WRITE] SELECT completed.')
+
+ def cmd_STORE(self, tag, args):
+ new_flags = args[2].strip('(').strip(')').split()
+ self.flags.extend(new_flags)
+ flags_msg = '(FLAGS (%s))' % ' \\'.join(self.flags)
+ msg = '* %s FETCH %s' % (args[0], flags_msg)
+ self._send_line(msg.encode('ascii'))
+ self._send_tagged(tag, 'OK', 'STORE completed.')
+
+ with self.reaped_pair(BracketFlagHandler) as (server, client):
+ code, data = client.authenticate('MYAUTH', lambda x: b'fake')
+ self.assertEqual(code, 'OK')
+ self.assertEqual(server.response, b'ZmFrZQ==\r\n')
+ client.select('test')
+ typ, [data] = client.store(b'1', "+FLAGS", "[test]")
+ self.assertIn(b'[test]', data)
+ client.select('test')
+ typ, [data] = client.response('PERMANENTFLAGS')
+ self.assertIn(b'[test]', data)
+
+ @reap_threads
def test_issue5949(self):
class EOFHandler(socketserver.StreamRequestHandler):
@@ -919,8 +968,10 @@ class RemoteIMAP_SSLTest(RemoteIMAPTest):
def test_logincapa_with_client_certfile(self):
with transient_internet(self.host):
- _server = self.imap_class(self.host, self.port, certfile=CERTFILE)
- self.check_logincapa(_server)
+ with support.check_warnings(('', DeprecationWarning)):
+ _server = self.imap_class(self.host, self.port,
+ certfile=CERTFILE)
+ self.check_logincapa(_server)
def test_logincapa_with_client_ssl_context(self):
with transient_internet(self.host):
diff --git a/Lib/test/test_imghdr.py b/Lib/test/test_imghdr.py
index b54daf8..476ba95 100644
--- a/Lib/test/test_imghdr.py
+++ b/Lib/test/test_imghdr.py
@@ -1,6 +1,7 @@
import imghdr
import io
import os
+import pathlib
import unittest
import warnings
from test.support import findfile, TESTFN, unlink
@@ -49,6 +50,12 @@ class TestImghdr(unittest.TestCase):
self.assertEqual(imghdr.what(None, data), expected)
self.assertEqual(imghdr.what(None, bytearray(data)), expected)
+ def test_pathlike_filename(self):
+ for filename, expected in TEST_FILES:
+ with self.subTest(filename=filename):
+ filename = findfile(filename, subdir='imghdrdata')
+ self.assertEqual(imghdr.what(pathlib.Path(filename)), expected)
+
def test_register_test(self):
def test_jumbo(h, file):
if h.startswith(b'eggs'):
diff --git a/Lib/test/test_imp.py b/Lib/test/test_imp.py
index ee9ee1a..4ece365 100644
--- a/Lib/test/test_imp.py
+++ b/Lib/test/test_imp.py
@@ -6,13 +6,12 @@ import importlib
import importlib.util
import os
import os.path
-import shutil
import sys
from test import support
import unittest
import warnings
with warnings.catch_warnings():
- warnings.simplefilter('ignore', PendingDeprecationWarning)
+ warnings.simplefilter('ignore', DeprecationWarning)
import imp
diff --git a/Lib/test/test_import/__init__.py b/Lib/test/test_import/__init__.py
index 1e33274..760908e 100644
--- a/Lib/test/test_import/__init__.py
+++ b/Lib/test/test_import/__init__.py
@@ -69,6 +69,18 @@ class ImportTests(unittest.TestCase):
def tearDown(self):
unload(TESTFN)
+ def test_import_raises_ModuleNotFoundError(self):
+ with self.assertRaises(ModuleNotFoundError):
+ import something_that_should_not_exist_anywhere
+
+ def test_from_import_missing_module_raises_ModuleNotFoundError(self):
+ with self.assertRaises(ModuleNotFoundError):
+ from something_that_should_not_exist_anywhere import blah
+
+ def test_from_import_missing_attr_raises_ImportError(self):
+ with self.assertRaises(ImportError):
+ from importlib import something_that_should_not_exist_anywhere
+
def test_case_sensitivity(self):
# Brief digression to test that import is case-sensitive: if we got
# this far, we know for sure that "random" exists.
diff --git a/Lib/test/test_importlib/extension/test_case_sensitivity.py b/Lib/test/test_importlib/extension/test_case_sensitivity.py
index c112ca7..0dd9c86 100644
--- a/Lib/test/test_importlib/extension/test_case_sensitivity.py
+++ b/Lib/test/test_importlib/extension/test_case_sensitivity.py
@@ -1,5 +1,4 @@
from importlib import _bootstrap_external
-import sys
from test import support
import unittest
@@ -9,8 +8,6 @@ importlib = util.import_importlib('importlib')
machinery = util.import_importlib('importlib.machinery')
-# XXX find_spec tests
-
@unittest.skipIf(util.EXTENSIONS.filename is None, '_testcapi not available')
@util.case_insensitive_tests
class ExtensionModuleCaseSensitivityTest(util.CASEOKTestBase):
diff --git a/Lib/test/test_importlib/extension/test_finder.py b/Lib/test/test_importlib/extension/test_finder.py
index 71bf67f..c9b4a37 100644
--- a/Lib/test/test_importlib/extension/test_finder.py
+++ b/Lib/test/test_importlib/extension/test_finder.py
@@ -6,7 +6,6 @@ machinery = util.import_importlib('importlib.machinery')
import unittest
import warnings
-# XXX find_spec tests
class FinderTests(abc.FinderTests):
diff --git a/Lib/test/test_importlib/extension/test_path_hook.py b/Lib/test/test_importlib/extension/test_path_hook.py
index 8f4b8bb..a4b5a64 100644
--- a/Lib/test/test_importlib/extension/test_path_hook.py
+++ b/Lib/test/test_importlib/extension/test_path_hook.py
@@ -2,8 +2,6 @@ from .. import util
machinery = util.import_importlib('importlib.machinery')
-import collections
-import sys
import unittest
diff --git a/Lib/test/test_importlib/frozen/test_loader.py b/Lib/test/test_importlib/frozen/test_loader.py
index 603c7d7..29ecff1 100644
--- a/Lib/test/test_importlib/frozen/test_loader.py
+++ b/Lib/test/test_importlib/frozen/test_loader.py
@@ -3,8 +3,6 @@ from .. import util
machinery = util.import_importlib('importlib.machinery')
-
-import sys
from test.support import captured_stdout
import types
import unittest
diff --git a/Lib/test/test_importlib/import_/test___package__.py b/Lib/test/test_importlib/import_/test___package__.py
index c7d3a2a..7f64548 100644
--- a/Lib/test/test_importlib/import_/test___package__.py
+++ b/Lib/test/test_importlib/import_/test___package__.py
@@ -5,6 +5,7 @@ of using the typical __path__/__name__ test).
"""
import unittest
+import warnings
from .. import util
@@ -33,31 +34,50 @@ class Using__package__:
"""
- def test_using___package__(self):
- # [__package__]
+ def import_module(self, globals_):
with self.mock_modules('pkg.__init__', 'pkg.fake') as importer:
with util.import_state(meta_path=[importer]):
self.__import__('pkg.fake')
module = self.__import__('',
- globals={'__package__': 'pkg.fake'},
- fromlist=['attr'], level=2)
+ globals=globals_,
+ fromlist=['attr'], level=2)
+ return module
+
+ def test_using___package__(self):
+ # [__package__]
+ module = self.import_module({'__package__': 'pkg.fake'})
self.assertEqual(module.__name__, 'pkg')
- def test_using___name__(self, package_as_None=False):
+ def test_using___name__(self):
# [__name__]
- globals_ = {'__name__': 'pkg.fake', '__path__': []}
- if package_as_None:
- globals_['__package__'] = None
- with self.mock_modules('pkg.__init__', 'pkg.fake') as importer:
- with util.import_state(meta_path=[importer]):
- self.__import__('pkg.fake')
- module = self.__import__('', globals= globals_,
- fromlist=['attr'], level=2)
- self.assertEqual(module.__name__, 'pkg')
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore")
+ module = self.import_module({'__name__': 'pkg.fake',
+ '__path__': []})
+ self.assertEqual(module.__name__, 'pkg')
+
+ def test_warn_when_using___name__(self):
+ with self.assertWarns(ImportWarning):
+ self.import_module({'__name__': 'pkg.fake', '__path__': []})
def test_None_as___package__(self):
# [None]
- self.test_using___name__(package_as_None=True)
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore")
+ module = self.import_module({
+ '__name__': 'pkg.fake', '__path__': [], '__package__': None })
+ self.assertEqual(module.__name__, 'pkg')
+
+ def test_spec_fallback(self):
+ # If __package__ isn't defined, fall back on __spec__.parent.
+ module = self.import_module({'__spec__': FakeSpec('pkg.fake')})
+ self.assertEqual(module.__name__, 'pkg')
+
+ def test_warn_when_package_and_spec_disagree(self):
+ # Raise an ImportWarning if __package__ != __spec__.parent.
+ with self.assertWarns(ImportWarning):
+ self.import_module({'__package__': 'pkg.fake',
+ '__spec__': FakeSpec('pkg.fakefake')})
def test_bad__package__(self):
globals = {'__package__': '<not real>'}
@@ -70,6 +90,11 @@ class Using__package__:
self.__import__('', globals, {}, ['relimport'], 1)
+class FakeSpec:
+ def __init__(self, parent):
+ self.parent = parent
+
+
class Using__package__PEP302(Using__package__):
mock_modules = util.mock_modules
diff --git a/Lib/test/test_importlib/import_/test_api.py b/Lib/test/test_importlib/import_/test_api.py
index 7069d9e..a7bf274 100644
--- a/Lib/test/test_importlib/import_/test_api.py
+++ b/Lib/test/test_importlib/import_/test_api.py
@@ -43,6 +43,10 @@ class APITest:
"""Test API-specific details for __import__ (e.g. raising the right
exception when passing in an int for the module name)."""
+ def test_raises_ModuleNotFoundError(self):
+ with self.assertRaises(ModuleNotFoundError):
+ util.import_importlib('some module that does not exist')
+
def test_name_requires_rparition(self):
# Raise TypeError if a non-string is passed in for the module name.
with self.assertRaises(TypeError):
diff --git a/Lib/test/test_importlib/import_/test_fromlist.py b/Lib/test/test_importlib/import_/test_fromlist.py
index 8045465..1464003 100644
--- a/Lib/test/test_importlib/import_/test_fromlist.py
+++ b/Lib/test/test_importlib/import_/test_fromlist.py
@@ -73,16 +73,16 @@ class HandlingFromlist:
self.assertTrue(hasattr(module, 'module'))
self.assertEqual(module.module.__name__, 'pkg.module')
- def test_module_from_package_triggers_ImportError(self):
- # If a submodule causes an ImportError because it tries to import
- # a module which doesn't exist, that should let the ImportError
- # propagate.
+ def test_module_from_package_triggers_ModuleNotFoundError(self):
+ # If a submodule causes an ModuleNotFoundError because it tries
+ # to import a module which doesn't exist, that should let the
+ # ModuleNotFoundError propagate.
def module_code():
import i_do_not_exist
with util.mock_modules('pkg.__init__', 'pkg.mod',
module_code={'pkg.mod': module_code}) as importer:
with util.import_state(meta_path=[importer]):
- with self.assertRaises(ImportError) as exc:
+ with self.assertRaises(ModuleNotFoundError) as exc:
self.__import__('pkg', fromlist=['mod'])
self.assertEqual('i_do_not_exist', exc.exception.name)
diff --git a/Lib/test/test_importlib/import_/test_packages.py b/Lib/test/test_importlib/import_/test_packages.py
index 3755b84..2439604 100644
--- a/Lib/test/test_importlib/import_/test_packages.py
+++ b/Lib/test/test_importlib/import_/test_packages.py
@@ -1,7 +1,6 @@
from .. import util
import sys
import unittest
-import importlib
from test import support
diff --git a/Lib/test/test_importlib/import_/test_path.py b/Lib/test/test_importlib/import_/test_path.py
index b32a876..7aa26b0 100644
--- a/Lib/test/test_importlib/import_/test_path.py
+++ b/Lib/test/test_importlib/import_/test_path.py
@@ -16,11 +16,14 @@ class FinderTests:
"""Tests for PathFinder."""
+ find = None
+ check_found = None
+
def test_failure(self):
# Test None returned upon not finding a suitable loader.
module = '<test module>'
with util.import_state():
- self.assertIsNone(self.machinery.PathFinder.find_module(module))
+ self.assertIsNone(self.find(module))
def test_sys_path(self):
# Test that sys.path is used when 'path' is None.
@@ -30,8 +33,8 @@ class FinderTests:
importer = util.mock_spec(module)
with util.import_state(path_importer_cache={path: importer},
path=[path]):
- loader = self.machinery.PathFinder.find_module(module)
- self.assertIs(loader, importer)
+ found = self.find(module)
+ self.check_found(found, importer)
def test_path(self):
# Test that 'path' is used when set.
@@ -40,8 +43,8 @@ class FinderTests:
path = '<test path>'
importer = util.mock_spec(module)
with util.import_state(path_importer_cache={path: importer}):
- loader = self.machinery.PathFinder.find_module(module, [path])
- self.assertIs(loader, importer)
+ found = self.find(module, [path])
+ self.check_found(found, importer)
def test_empty_list(self):
# An empty list should not count as asking for sys.path.
@@ -50,7 +53,7 @@ class FinderTests:
importer = util.mock_spec(module)
with util.import_state(path_importer_cache={path: importer},
path=[path]):
- self.assertIsNone(self.machinery.PathFinder.find_module('module', []))
+ self.assertIsNone(self.find('module', []))
def test_path_hooks(self):
# Test that sys.path_hooks is used.
@@ -60,8 +63,8 @@ class FinderTests:
importer = util.mock_spec(module)
hook = util.mock_path_hook(path, importer=importer)
with util.import_state(path_hooks=[hook]):
- loader = self.machinery.PathFinder.find_module(module, [path])
- self.assertIs(loader, importer)
+ found = self.find(module, [path])
+ self.check_found(found, importer)
self.assertIn(path, sys.path_importer_cache)
self.assertIs(sys.path_importer_cache[path], importer)
@@ -73,7 +76,7 @@ class FinderTests:
path=[path_entry]):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')
- self.assertIsNone(self.machinery.PathFinder.find_module('os'))
+ self.assertIsNone(self.find('os'))
self.assertIsNone(sys.path_importer_cache[path_entry])
self.assertEqual(len(w), 1)
self.assertTrue(issubclass(w[-1].category, ImportWarning))
@@ -85,8 +88,8 @@ class FinderTests:
importer = util.mock_spec(module)
hook = util.mock_path_hook(os.getcwd(), importer=importer)
with util.import_state(path=[path], path_hooks=[hook]):
- loader = self.machinery.PathFinder.find_module(module)
- self.assertIs(loader, importer)
+ found = self.find(module)
+ self.check_found(found, importer)
self.assertIn(os.getcwd(), sys.path_importer_cache)
def test_None_on_sys_path(self):
@@ -182,16 +185,33 @@ class FinderTests:
self.assertIsNone(self.machinery.PathFinder.find_spec('whatever'))
+class FindModuleTests(FinderTests):
+ def find(self, *args, **kwargs):
+ return self.machinery.PathFinder.find_module(*args, **kwargs)
+ def check_found(self, found, importer):
+ self.assertIs(found, importer)
+
+
+(Frozen_FindModuleTests,
+ Source_FindModuleTests
+) = util.test_both(FindModuleTests, importlib=importlib, machinery=machinery)
-(Frozen_FinderTests,
- Source_FinderTests
- ) = util.test_both(FinderTests, importlib=importlib, machinery=machinery)
+class FindSpecTests(FinderTests):
+ def find(self, *args, **kwargs):
+ return self.machinery.PathFinder.find_spec(*args, **kwargs)
+ def check_found(self, found, importer):
+ self.assertIs(found.loader, importer)
+
+
+(Frozen_FindSpecTests,
+ Source_FindSpecTests
+ ) = util.test_both(FindSpecTests, importlib=importlib, machinery=machinery)
class PathEntryFinderTests:
- def test_finder_with_failing_find_module(self):
+ def test_finder_with_failing_find_spec(self):
# PathEntryFinder with find_module() defined should work.
# Issue #20763.
class Finder:
@@ -209,6 +229,24 @@ class PathEntryFinderTests:
path_hooks=[Finder]):
self.machinery.PathFinder.find_spec('importlib')
+ def test_finder_with_failing_find_module(self):
+ # PathEntryFinder with find_module() defined should work.
+ # Issue #20763.
+ class Finder:
+ path_location = 'test_finder_with_find_module'
+ def __init__(self, path):
+ if path != self.path_location:
+ raise ImportError
+
+ @staticmethod
+ def find_module(fullname):
+ return None
+
+
+ with util.import_state(path=[Finder.path_location]+sys.path[:],
+ path_hooks=[Finder]):
+ self.machinery.PathFinder.find_module('importlib')
+
(Frozen_PEFTests,
Source_PEFTests
diff --git a/Lib/test/test_importlib/import_/test_relative_imports.py b/Lib/test/test_importlib/import_/test_relative_imports.py
index 3bb819f..8a95a32 100644
--- a/Lib/test/test_importlib/import_/test_relative_imports.py
+++ b/Lib/test/test_importlib/import_/test_relative_imports.py
@@ -1,7 +1,8 @@
"""Test relative imports (PEP 328)."""
from .. import util
-import sys
import unittest
+import warnings
+
class RelativeImports:
@@ -65,9 +66,11 @@ class RelativeImports:
uncache_names.append(name[:-len('.__init__')])
with util.mock_spec(*create) as importer:
with util.import_state(meta_path=[importer]):
- for global_ in globals_:
- with util.uncache(*uncache_names):
- callback(global_)
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore")
+ for global_ in globals_:
+ with util.uncache(*uncache_names):
+ callback(global_)
def test_module_from_module(self):
@@ -204,11 +207,18 @@ class RelativeImports:
def test_relative_import_no_globals(self):
# No globals for a relative import is an error.
- with self.assertRaises(KeyError):
- self.__import__('sys', level=1)
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore")
+ with self.assertRaises(KeyError):
+ self.__import__('sys', level=1)
+
+ def test_relative_import_no_package(self):
+ with self.assertRaises(ImportError):
+ self.__import__('a', {'__package__': '', '__spec__': None},
+ level=1)
def test_relative_import_no_package_exists_absolute(self):
- with self.assertRaises(SystemError):
+ with self.assertRaises(ImportError):
self.__import__('sys', {'__package__': '', '__spec__': None},
level=1)
diff --git a/Lib/test/test_importlib/regrtest.py b/Lib/test/test_importlib/regrtest.py
deleted file mode 100644
index a5be11f..0000000
--- a/Lib/test/test_importlib/regrtest.py
+++ /dev/null
@@ -1,17 +0,0 @@
-"""Run Python's standard test suite using importlib.__import__.
-
-Tests known to fail because of assumptions that importlib (properly)
-invalidates are automatically skipped if the entire test suite is run.
-Otherwise all command-line options valid for test.regrtest are also valid for
-this script.
-
-"""
-import importlib
-import sys
-from test import regrtest
-
-if __name__ == '__main__':
- __builtins__.__import__ = importlib.__import__
- sys.path_importer_cache.clear()
-
- regrtest.main(quiet=True, verbose2=True)
diff --git a/Lib/test/test_importlib/source/test_case_sensitivity.py b/Lib/test/test_importlib/source/test_case_sensitivity.py
index 34b86cd..12ce0cb 100644
--- a/Lib/test/test_importlib/source/test_case_sensitivity.py
+++ b/Lib/test/test_importlib/source/test_case_sensitivity.py
@@ -5,7 +5,6 @@ importlib = util.import_importlib('importlib')
machinery = util.import_importlib('importlib.machinery')
import os
-import sys
from test import support as test_support
import unittest
diff --git a/Lib/test/test_importlib/source/test_file_loader.py b/Lib/test/test_importlib/source/test_file_loader.py
index 73f4c62..a151149 100644
--- a/Lib/test/test_importlib/source/test_file_loader.py
+++ b/Lib/test/test_importlib/source/test_file_loader.py
@@ -217,7 +217,7 @@ class SimpleTest(abc.LoaderTests):
# PEP 302
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
- mod = loader.load_module('_temp') # XXX
+ mod = loader.load_module('_temp')
# Sanity checks.
self.assertEqual(mod.__cached__, compiled)
self.assertEqual(mod.x, 5)
@@ -245,12 +245,7 @@ class SimpleTest(abc.LoaderTests):
class BadBytecodeTest:
def import_(self, file, module_name):
- loader = self.loader(module_name, file)
- with warnings.catch_warnings():
- warnings.simplefilter('ignore', DeprecationWarning)
- # XXX Change to use exec_module().
- module = loader.load_module(module_name)
- self.assertIn(module_name, sys.modules)
+ raise NotImplementedError
def manipulate_bytecode(self, name, mapping, manipulator, *,
del_source=False):
diff --git a/Lib/test/test_importlib/source/test_path_hook.py b/Lib/test/test_importlib/source/test_path_hook.py
index e6a2415..795d436 100644
--- a/Lib/test/test_importlib/source/test_path_hook.py
+++ b/Lib/test/test_importlib/source/test_path_hook.py
@@ -16,10 +16,19 @@ class PathHookTest:
def test_success(self):
with util.create_modules('dummy') as mapping:
self.assertTrue(hasattr(self.path_hook()(mapping['.root']),
- 'find_module'))
+ 'find_spec'))
+
+ def test_success_legacy(self):
+ with util.create_modules('dummy') as mapping:
+ self.assertTrue(hasattr(self.path_hook()(mapping['.root']),
+ 'find_module'))
def test_empty_string(self):
# The empty string represents the cwd.
+ self.assertTrue(hasattr(self.path_hook()(''), 'find_spec'))
+
+ def test_empty_string_legacy(self):
+ # The empty string represents the cwd.
self.assertTrue(hasattr(self.path_hook()(''), 'find_module'))
diff --git a/Lib/test/test_importlib/source/test_source_encoding.py b/Lib/test/test_importlib/source/test_source_encoding.py
index 1e0771b..980855f 100644
--- a/Lib/test/test_importlib/source/test_source_encoding.py
+++ b/Lib/test/test_importlib/source/test_source_encoding.py
@@ -5,7 +5,6 @@ machinery = util.import_importlib('importlib.machinery')
import codecs
import importlib.util
import re
-import sys
import types
# Because sys.path gets essentially blanked, need to have unicodedata already
# imported for the parser to use.
diff --git a/Lib/test/test_importlib/test_abc.py b/Lib/test/test_importlib/test_abc.py
index d4bf915..c862480 100644
--- a/Lib/test/test_importlib/test_abc.py
+++ b/Lib/test/test_importlib/test_abc.py
@@ -207,6 +207,10 @@ class LoaderDefaultsTests(ABCTestHarness):
SPLIT = make_abc_subclasses(Loader)
+ def test_create_module(self):
+ spec = 'a spec'
+ self.assertIsNone(self.ins.create_module(spec))
+
def test_load_module(self):
with self.assertRaises(ImportError):
self.ins.load_module('something')
@@ -519,6 +523,12 @@ class InspectLoaderLoadModuleTests:
support.unload(self.module_name)
self.addCleanup(support.unload, self.module_name)
+ def load(self, loader):
+ spec = self.util.spec_from_loader(self.module_name, loader)
+ with warnings.catch_warnings():
+ warnings.simplefilter('ignore', DeprecationWarning)
+ return self.init._bootstrap._load_unlocked(spec)
+
def mock_get_code(self):
return mock.patch.object(self.InspectLoaderSubclass, 'get_code')
@@ -528,9 +538,7 @@ class InspectLoaderLoadModuleTests:
mocked_get_code.side_effect = ImportError
with self.assertRaises(ImportError):
loader = self.InspectLoaderSubclass()
- with warnings.catch_warnings():
- warnings.simplefilter('ignore', DeprecationWarning)
- loader.load_module(self.module_name)
+ self.load(loader)
def test_get_code_None(self):
# If get_code() returns None, raise ImportError.
@@ -538,7 +546,7 @@ class InspectLoaderLoadModuleTests:
mocked_get_code.return_value = None
with self.assertRaises(ImportError):
loader = self.InspectLoaderSubclass()
- loader.load_module(self.module_name)
+ self.load(loader)
def test_module_returned(self):
# The loaded module should be returned.
@@ -546,14 +554,16 @@ class InspectLoaderLoadModuleTests:
with self.mock_get_code() as mocked_get_code:
mocked_get_code.return_value = code
loader = self.InspectLoaderSubclass()
- module = loader.load_module(self.module_name)
+ module = self.load(loader)
self.assertEqual(module, sys.modules[self.module_name])
(Frozen_ILLoadModuleTests,
Source_ILLoadModuleTests
) = test_util.test_both(InspectLoaderLoadModuleTests,
- InspectLoaderSubclass=SPLIT_IL)
+ InspectLoaderSubclass=SPLIT_IL,
+ init=init,
+ util=util)
##### ExecutionLoader concrete methods #########################################
diff --git a/Lib/test/test_importlib/test_api.py b/Lib/test/test_importlib/test_api.py
index 6bc3c56..b0a94aa 100644
--- a/Lib/test/test_importlib/test_api.py
+++ b/Lib/test/test_importlib/test_api.py
@@ -99,9 +99,7 @@ class ImportModuleTests:
class FindLoaderTests:
- class FakeMetaFinder:
- @staticmethod
- def find_module(name, path=None): return name, path
+ FakeMetaFinder = None
def test_sys_modules(self):
# If a module with __loader__ is in sys.modules, then return it.
@@ -171,9 +169,30 @@ class FindLoaderTests:
self.assertIsNone(self.init.find_loader('nevergoingtofindthismodule'))
-(Frozen_FindLoaderTests,
- Source_FindLoaderTests
- ) = test_util.test_both(FindLoaderTests, init=init)
+class FindLoaderPEP451Tests(FindLoaderTests):
+
+ class FakeMetaFinder:
+ @staticmethod
+ def find_spec(name, path=None, target=None):
+ return machinery['Source'].ModuleSpec(name, (name, path))
+
+
+(Frozen_FindLoaderPEP451Tests,
+ Source_FindLoaderPEP451Tests
+ ) = test_util.test_both(FindLoaderPEP451Tests, init=init)
+
+
+class FindLoaderPEP302Tests(FindLoaderTests):
+
+ class FakeMetaFinder:
+ @staticmethod
+ def find_module(name, path=None):
+ return name, path
+
+
+(Frozen_FindLoaderPEP302Tests,
+ Source_FindLoaderPEP302Tests
+ ) = test_util.test_both(FindLoaderPEP302Tests, init=init)
class ReloadTests:
diff --git a/Lib/test/test_importlib/test_lazy.py b/Lib/test/test_importlib/test_lazy.py
index cc383c2..ffd8dc6 100644
--- a/Lib/test/test_importlib/test_lazy.py
+++ b/Lib/test/test_importlib/test_lazy.py
@@ -66,6 +66,8 @@ class LazyLoaderTests(unittest.TestCase):
spec = util.spec_from_loader(TestingImporter.module_name,
util.LazyLoader(loader))
module = spec.loader.create_module(spec)
+ if module is None:
+ module = types.ModuleType(TestingImporter.module_name)
module.__spec__ = spec
module.__loader__ = spec.loader
spec.loader.exec_module(module)
diff --git a/Lib/test/test_importlib/test_locks.py b/Lib/test/test_importlib/test_locks.py
index df0af12..dbce9c2 100644
--- a/Lib/test/test_importlib/test_locks.py
+++ b/Lib/test/test_importlib/test_locks.py
@@ -3,7 +3,6 @@ from . import util as test_util
init = test_util.import_importlib('importlib')
import sys
-import time
import unittest
import weakref
@@ -58,7 +57,7 @@ if threading is not None:
def setUp(self):
try:
self.old_switchinterval = sys.getswitchinterval()
- sys.setswitchinterval(0.000001)
+ support.setswitchinterval(0.000001)
except AttributeError:
self.old_switchinterval = None
diff --git a/Lib/test/test_importlib/test_namespace_pkgs.py b/Lib/test/test_importlib/test_namespace_pkgs.py
index 6639612..e37d8a1 100644
--- a/Lib/test/test_importlib/test_namespace_pkgs.py
+++ b/Lib/test/test_importlib/test_namespace_pkgs.py
@@ -1,13 +1,10 @@
import contextlib
-import importlib.abc
-import importlib.machinery
+import importlib
import os
import sys
-import types
import unittest
from test.test_importlib import util
-from test.support import run_unittest
# needed tests:
#
@@ -71,6 +68,7 @@ class NamespacePackageTest(unittest.TestCase):
# TODO: will we ever want to pass exc_info to __exit__?
self.ctx.__exit__(None, None, None)
+
class SingleNamespacePackage(NamespacePackageTest):
paths = ['portion1']
@@ -87,7 +85,7 @@ class SingleNamespacePackage(NamespacePackageTest):
self.assertEqual(repr(foo), "<module 'foo' (namespace)>")
-class DynamicPatheNamespacePackage(NamespacePackageTest):
+class DynamicPathNamespacePackage(NamespacePackageTest):
paths = ['portion1']
def test_dynamic_path(self):
@@ -289,5 +287,35 @@ class ModuleAndNamespacePackageInSameDir(NamespacePackageTest):
self.assertEqual(a_test.attr, 'in module')
+class ReloadTests(NamespacePackageTest):
+ paths = ['portion1']
+
+ def test_simple_package(self):
+ import foo.one
+ foo = importlib.reload(foo)
+ self.assertEqual(foo.one.attr, 'portion1 foo one')
+
+ def test_cant_import_other(self):
+ import foo
+ with self.assertRaises(ImportError):
+ import foo.two
+ foo = importlib.reload(foo)
+ with self.assertRaises(ImportError):
+ import foo.two
+
+ def test_dynamic_path(self):
+ import foo.one
+ with self.assertRaises(ImportError):
+ import foo.two
+
+ # Now modify sys.path and reload.
+ sys.path.append(os.path.join(self.root, 'portion2'))
+ foo = importlib.reload(foo)
+
+ # And make sure foo.two is now importable
+ import foo.two
+ self.assertEqual(foo.two.attr, 'portion2 foo two')
+
+
if __name__ == "__main__":
unittest.main()
diff --git a/Lib/test/test_importlib/test_spec.py b/Lib/test/test_importlib/test_spec.py
index 8b333e8..5a16a03 100644
--- a/Lib/test/test_importlib/test_spec.py
+++ b/Lib/test/test_importlib/test_spec.py
@@ -5,6 +5,7 @@ machinery = test_util.import_importlib('importlib.machinery')
util = test_util.import_importlib('importlib.util')
import os.path
+import pathlib
from test.support import CleanImport
import unittest
import sys
@@ -659,6 +660,11 @@ class FactoryTests:
self.assertEqual(spec.cached, self.cached)
self.assertTrue(spec.has_location)
+ def test_spec_from_file_location_path_like_arg(self):
+ spec = self.util.spec_from_file_location(self.name,
+ pathlib.PurePath(self.path))
+ self.assertEqual(spec.origin, self.path)
+
def test_spec_from_file_location_default_without_location(self):
spec = self.util.spec_from_file_location(self.name)
diff --git a/Lib/test/test_importlib/test_util.py b/Lib/test/test_importlib/test_util.py
index 41ca333..d615375 100644
--- a/Lib/test/test_importlib/test_util.py
+++ b/Lib/test/test_importlib/test_util.py
@@ -5,6 +5,7 @@ machinery = util.import_importlib('importlib.machinery')
importlib_util = util.import_importlib('importlib.util')
import os
+import pathlib
import string
import sys
from test import support
@@ -46,14 +47,8 @@ class ModuleFromSpecTests:
def exec_module(self, module):
pass
spec = self.machinery.ModuleSpec('test', Loader())
- with warnings.catch_warnings(record=True) as w:
- warnings.simplefilter('always')
+ with self.assertRaises(ImportError):
module = self.util.module_from_spec(spec)
- self.assertEqual(1, len(w))
- self.assertTrue(issubclass(w[0].category, DeprecationWarning))
- self.assertIn('create_module', str(w[0].message))
- self.assertIsInstance(module, types.ModuleType)
- self.assertEqual(module.__name__, spec.name)
def test_create_module_returns_None(self):
class Loader(self.abc.Loader):
@@ -677,6 +672,15 @@ class PEP3147Tests:
'\\foo\\bar\\baz\\__pycache__\\qux.{}.pyc'.format(self.tag))
@unittest.skipUnless(sys.implementation.cache_tag is not None,
+ 'requires sys.implementation.cache_tag not be None')
+ def test_source_from_cache_path_like_arg(self):
+ path = pathlib.PurePath('foo', 'bar', 'baz', 'qux.py')
+ expect = os.path.join('foo', 'bar', 'baz', '__pycache__',
+ 'qux.{}.pyc'.format(self.tag))
+ self.assertEqual(self.util.cache_from_source(path, optimization=''),
+ expect)
+
+ @unittest.skipUnless(sys.implementation.cache_tag is not None,
'requires sys.implementation.cache_tag to not be '
'None')
def test_source_from_cache(self):
@@ -738,6 +742,15 @@ class PEP3147Tests:
with self.assertRaises(ValueError):
self.util.source_from_cache(path)
+ @unittest.skipUnless(sys.implementation.cache_tag is not None,
+ 'requires sys.implementation.cache_tag to not be '
+ 'None')
+ def test_source_from_cache_path_like_arg(self):
+ path = pathlib.PurePath('foo', 'bar', 'baz', '__pycache__',
+ 'qux.{}.pyc'.format(self.tag))
+ expect = os.path.join('foo', 'bar', 'baz', 'qux.py')
+ self.assertEqual(self.util.source_from_cache(path), expect)
+
(Frozen_PEP3147Tests,
Source_PEP3147Tests
diff --git a/Lib/test/test_importlib/test_windows.py b/Lib/test/test_importlib/test_windows.py
index c893bcf..005b685 100644
--- a/Lib/test/test_importlib/test_windows.py
+++ b/Lib/test/test_importlib/test_windows.py
@@ -40,7 +40,7 @@ def setup_module(machinery, name, path=None):
else:
root = machinery.WindowsRegistryFinder.REGISTRY_KEY
key = root.format(fullname=name,
- sys_version=sys.version[:3])
+ sys_version='%d.%d' % sys.version_info[:2])
try:
with temp_module(name, "a = 1") as location:
subkey = CreateKey(HKEY_CURRENT_USER, key)
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
index d33de9e..88eaabe 100644
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -30,6 +30,7 @@ from test.support import MISSING_C_DOCSTRINGS, cpython_only
from test.support.script_helper import assert_python_ok, assert_python_failure
from test import inspect_fodder as mod
from test import inspect_fodder2 as mod2
+from test import support
from test.test_import import _ready_to_import
@@ -38,7 +39,7 @@ from test.test_import import _ready_to_import
# ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode,
# isbuiltin, isroutine, isgenerator, isgeneratorfunction, getmembers,
# getdoc, getfile, getmodule, getsourcefile, getcomments, getsource,
-# getclasstree, getargspec, getargvalues, formatargspec, formatargvalues,
+# getclasstree, getargvalues, formatargspec, formatargvalues,
# currentframe, stack, trace, isdatadescriptor
# NOTE: There are some additional tests relating to interaction with
@@ -64,7 +65,8 @@ class IsTestBase(unittest.TestCase):
inspect.isframe, inspect.isfunction, inspect.ismethod,
inspect.ismodule, inspect.istraceback,
inspect.isgenerator, inspect.isgeneratorfunction,
- inspect.iscoroutine, inspect.iscoroutinefunction])
+ inspect.iscoroutine, inspect.iscoroutinefunction,
+ inspect.isasyncgen, inspect.isasyncgenfunction])
def istest(self, predicate, exp):
obj = eval(exp)
@@ -72,6 +74,7 @@ class IsTestBase(unittest.TestCase):
for other in self.predicates - set([predicate]):
if (predicate == inspect.isgeneratorfunction or \
+ predicate == inspect.isasyncgenfunction or \
predicate == inspect.iscoroutinefunction) and \
other == inspect.isfunction:
continue
@@ -81,6 +84,10 @@ def generator_function_example(self):
for i in range(2):
yield i
+async def async_generator_function_example(self):
+ async for i in range(2):
+ yield i
+
async def coroutine_function_example(self):
return 'spam'
@@ -121,6 +128,10 @@ class TestPredicates(IsTestBase):
self.istest(inspect.isdatadescriptor, 'collections.defaultdict.default_factory')
self.istest(inspect.isgenerator, '(x for x in range(2))')
self.istest(inspect.isgeneratorfunction, 'generator_function_example')
+ self.istest(inspect.isasyncgen,
+ 'async_generator_function_example(1)')
+ self.istest(inspect.isasyncgenfunction,
+ 'async_generator_function_example')
with warnings.catch_warnings():
warnings.simplefilter("ignore")
@@ -405,7 +416,7 @@ class TestRetrievingSourceCode(GetSourceBase):
self.assertEqual(normcase(inspect.getsourcefile(mod.spam)), modfile)
self.assertEqual(normcase(inspect.getsourcefile(git.abuse)), modfile)
fn = "_non_existing_filename_used_for_sourcefile_test.py"
- co = compile("None", fn, "exec")
+ co = compile("x=1", fn, "exec")
self.assertEqual(inspect.getsourcefile(co), None)
linecache.cache[co.co_filename] = (1, None, "None", co.co_filename)
try:
@@ -2907,6 +2918,10 @@ class TestParameterObject(unittest.TestCase):
'is not a valid parameter name'):
inspect.Parameter('$', kind=inspect.Parameter.VAR_KEYWORD)
+ with self.assertRaisesRegex(ValueError,
+ 'is not a valid parameter name'):
+ inspect.Parameter('.a', kind=inspect.Parameter.VAR_KEYWORD)
+
with self.assertRaisesRegex(ValueError, 'cannot have default values'):
inspect.Parameter('a', default=42,
kind=inspect.Parameter.VAR_KEYWORD)
@@ -2990,6 +3005,17 @@ class TestParameterObject(unittest.TestCase):
with self.assertRaisesRegex(TypeError, 'name must be a str'):
inspect.Parameter(None, kind=inspect.Parameter.POSITIONAL_ONLY)
+ @cpython_only
+ def test_signature_parameter_implicit(self):
+ with self.assertRaisesRegex(ValueError,
+ 'implicit arguments must be passed in as'):
+ inspect.Parameter('.0', kind=inspect.Parameter.POSITIONAL_ONLY)
+
+ param = inspect.Parameter(
+ '.0', kind=inspect.Parameter.POSITIONAL_OR_KEYWORD)
+ self.assertEqual(param.kind, inspect.Parameter.POSITIONAL_ONLY)
+ self.assertEqual(param.name, 'implicit0')
+
def test_signature_parameter_immutability(self):
p = inspect.Parameter('spam', kind=inspect.Parameter.KEYWORD_ONLY)
@@ -3238,6 +3264,17 @@ class TestSignatureBind(unittest.TestCase):
ba = sig.bind(args=1)
self.assertEqual(ba.arguments, {'kwargs': {'args': 1}})
+ @cpython_only
+ def test_signature_bind_implicit_arg(self):
+ # Issue #19611: getcallargs should work with set comprehensions
+ def make_set():
+ return {z * z for z in range(5)}
+ setcomp_code = make_set.__code__.co_consts[1]
+ setcomp_func = types.FunctionType(setcomp_code, {})
+
+ iterator = iter(range(5))
+ self.assertEqual(self.call(setcomp_func, iterator), {0, 1, 4, 9, 16})
+
class TestBoundArguments(unittest.TestCase):
def test_signature_bound_arguments_unhashable(self):
@@ -3548,14 +3585,14 @@ class TestMain(unittest.TestCase):
def test_details(self):
module = importlib.import_module('unittest')
- rc, out, err = assert_python_ok('-m', 'inspect',
+ args = support.optim_args_from_interpreter_flags()
+ rc, out, err = assert_python_ok(*args, '-m', 'inspect',
'unittest', '--details')
output = out.decode()
# Just a quick sanity check on the output
self.assertIn(module.__name__, output)
self.assertIn(module.__file__, output)
- if not sys.flags.optimize:
- self.assertIn(module.__cached__, output)
+ self.assertIn(module.__cached__, output)
self.assertEqual(err, b'')
diff --git a/Lib/test/test_int.py b/Lib/test/test_int.py
index b66c5d6..14bbd61 100644
--- a/Lib/test/test_int.py
+++ b/Lib/test/test_int.py
@@ -2,6 +2,8 @@ import sys
import unittest
from test import support
+from test.test_grammar import (VALID_UNDERSCORE_LITERALS,
+ INVALID_UNDERSCORE_LITERALS)
L = [
('0', 0),
@@ -212,6 +214,25 @@ class IntTestCases(unittest.TestCase):
self.assertEqual(int('2br45qc', 35), 4294967297)
self.assertEqual(int('1z141z5', 36), 4294967297)
+ def test_underscores(self):
+ for lit in VALID_UNDERSCORE_LITERALS:
+ if any(ch in lit for ch in '.eEjJ'):
+ continue
+ self.assertEqual(int(lit, 0), eval(lit))
+ self.assertEqual(int(lit, 0), int(lit.replace('_', ''), 0))
+ for lit in INVALID_UNDERSCORE_LITERALS:
+ if any(ch in lit for ch in '.eEjJ'):
+ continue
+ self.assertRaises(ValueError, int, lit, 0)
+ # Additional test cases with bases != 0, only for the constructor:
+ self.assertEqual(int("1_00", 3), 9)
+ self.assertEqual(int("0_100"), 100) # not valid as a literal!
+ self.assertEqual(int(b"1_00"), 100) # byte underscore
+ self.assertRaises(ValueError, int, "_100")
+ self.assertRaises(ValueError, int, "+_100")
+ self.assertRaises(ValueError, int, "1__00")
+ self.assertRaises(ValueError, int, "100_")
+
@support.cpython_only
def test_small_ints(self):
# Bug #3236: Return small longs from PyLong_FromString
@@ -430,21 +451,24 @@ class IntTestCases(unittest.TestCase):
with self.assertWarns(DeprecationWarning):
n = int(bad_int)
self.assertEqual(n, 1)
+ self.assertIs(type(n), int)
bad_int = BadInt2()
with self.assertWarns(DeprecationWarning):
n = int(bad_int)
self.assertEqual(n, 1)
+ self.assertIs(type(n), int)
bad_int = TruncReturnsBadInt()
with self.assertWarns(DeprecationWarning):
n = int(bad_int)
self.assertEqual(n, 1)
+ self.assertIs(type(n), int)
good_int = TruncReturnsIntSubclass()
n = int(good_int)
self.assertEqual(n, 1)
- self.assertIs(type(n), bool)
+ self.assertIs(type(n), int)
n = IntSubclass(good_int)
self.assertEqual(n, 1)
self.assertIs(type(n), IntSubclass)
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
index 5111882..aaa64ea 100644
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -350,7 +350,10 @@ class IOTest(unittest.TestCase):
def large_file_ops(self, f):
assert f.readable()
assert f.writable()
- self.assertEqual(f.seek(self.LARGE), self.LARGE)
+ try:
+ self.assertEqual(f.seek(self.LARGE), self.LARGE)
+ except (OverflowError, ValueError):
+ self.skipTest("no largefile support")
self.assertEqual(f.tell(), self.LARGE)
self.assertEqual(f.write(b"xxx"), 3)
self.assertEqual(f.tell(), self.LARGE + 3)
@@ -496,7 +499,11 @@ class IOTest(unittest.TestCase):
def test_open_handles_NUL_chars(self):
fn_with_NUL = 'foo\0bar'
self.assertRaises(ValueError, self.open, fn_with_NUL, 'w')
- self.assertRaises(ValueError, self.open, bytes(fn_with_NUL, 'ascii'), 'w')
+
+ bytes_fn = bytes(fn_with_NUL, 'ascii')
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore", DeprecationWarning)
+ self.assertRaises(ValueError, self.open, bytes_fn, 'w')
def test_raw_file_io(self):
with self.open(support.TESTFN, "wb", buffering=0) as f:
@@ -856,6 +863,32 @@ class IOTest(unittest.TestCase):
self.assertEqual(getattr(stream, method)(buffer), 5)
self.assertEqual(bytes(buffer), b"12345")
+ def test_fspath_support(self):
+ class PathLike:
+ def __init__(self, path):
+ self.path = path
+
+ def __fspath__(self):
+ return self.path
+
+ def check_path_succeeds(path):
+ with self.open(path, "w") as f:
+ f.write("egg\n")
+
+ with self.open(path, "r") as f:
+ self.assertEqual(f.read(), "egg\n")
+
+ check_path_succeeds(PathLike(support.TESTFN))
+ check_path_succeeds(PathLike(support.TESTFN.encode('utf-8')))
+
+ bad_path = PathLike(TypeError)
+ with self.assertRaises(TypeError):
+ self.open(bad_path, 'w')
+
+ # ensure that refcounting is correct with some error conditions
+ with self.assertRaisesRegex(ValueError, 'read/write/append mode'):
+ self.open(PathLike(support.TESTFN), 'rwxa')
+
class CIOTest(IOTest):
@@ -1782,7 +1815,7 @@ class BufferedRWPairTest(unittest.TestCase):
with self.subTest(method):
pair = self.tp(self.BytesIO(b"abcdef"), self.MockRawIO())
- data = byteslike(5)
+ data = byteslike(b'\0' * 5)
self.assertEqual(getattr(pair, method)(data), 5)
self.assertEqual(bytes(data), b"abcde")
@@ -3246,8 +3279,7 @@ class CTextIOWrapperTest(TextIOWrapperTest):
class PyTextIOWrapperTest(TextIOWrapperTest):
io = pyio
- #shutdown_error = "LookupError: unknown encoding: ascii"
- shutdown_error = "TypeError: 'NoneType' object is not iterable"
+ shutdown_error = "LookupError: unknown encoding: ascii"
class IncrementalNewlineDecoderTest(unittest.TestCase):
diff --git a/Lib/test/test_ipaddress.py b/Lib/test/test_ipaddress.py
index 91ae8d8..0e39516 100644
--- a/Lib/test/test_ipaddress.py
+++ b/Lib/test/test_ipaddress.py
@@ -119,7 +119,7 @@ class CommonTestMixin_v4(CommonTestMixin):
def test_bad_packed_length(self):
def assertBadLength(length):
- addr = bytes(length)
+ addr = b'\0' * length
msg = "%r (len %d != 4) is not permitted as an IPv4 address"
with self.assertAddressError(re.escape(msg % (addr, length))):
self.factory(addr)
@@ -139,11 +139,11 @@ class CommonTestMixin_v6(CommonTestMixin):
self.assertInstancesEqual(3232235521, "::c0a8:1")
def test_packed(self):
- addr = bytes(12) + bytes.fromhex("00000000")
+ addr = b'\0'*12 + bytes.fromhex("00000000")
self.assertInstancesEqual(addr, "::")
- addr = bytes(12) + bytes.fromhex("c0a80001")
+ addr = b'\0'*12 + bytes.fromhex("c0a80001")
self.assertInstancesEqual(addr, "::c0a8:1")
- addr = bytes.fromhex("c0a80001") + bytes(12)
+ addr = bytes.fromhex("c0a80001") + b'\0'*12
self.assertInstancesEqual(addr, "c0a8:1::")
def test_negative_ints_rejected(self):
@@ -158,7 +158,7 @@ class CommonTestMixin_v6(CommonTestMixin):
def test_bad_packed_length(self):
def assertBadLength(length):
- addr = bytes(length)
+ addr = b'\0' * length
msg = "%r (len %d != 16) is not permitted as an IPv6 address"
with self.assertAddressError(re.escape(msg % (addr, length))):
self.factory(addr)
@@ -1176,6 +1176,7 @@ class IpaddrUnitTest(unittest.TestCase):
self.assertEqual(str(self.ipv6_network[5]),
'2001:658:22a:cafe::5')
+ self.assertRaises(IndexError, self.ipv6_network.__getitem__, 1 << 64)
def testGetitem(self):
# http://code.google.com/p/ipaddr-py/issues/detail?id=15
diff --git a/Lib/test/test_iter.py b/Lib/test/test_iter.py
index a91670b..542b284 100644
--- a/Lib/test/test_iter.py
+++ b/Lib/test/test_iter.py
@@ -54,6 +54,14 @@ class UnlimitedSequenceClass:
def __getitem__(self, i):
return i
+class DefaultIterClass:
+ pass
+
+class NoIterClass:
+ def __getitem__(self, i):
+ return i
+ __iter__ = None
+
# Main test suite
class TestCase(unittest.TestCase):
@@ -995,6 +1003,10 @@ class TestCase(unittest.TestCase):
def test_free_after_iterating(self):
check_free_after_iterating(self, iter, SequenceClass, (0,))
+ def test_error_iter(self):
+ for typ in (DefaultIterClass, NoIterClass):
+ self.assertRaises(TypeError, iter, typ())
+
def test_main():
run_unittest(TestCase)
diff --git a/Lib/test/test_iterlen.py b/Lib/test/test_iterlen.py
index 152f5fc..41c9752 100644
--- a/Lib/test/test_iterlen.py
+++ b/Lib/test/test_iterlen.py
@@ -42,7 +42,6 @@ enumerate(iter('abc')).
"""
import unittest
-from test import support
from itertools import repeat
from collections import deque
from operator import length_hint
diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py
index e054303..ea1f57c 100644
--- a/Lib/test/test_itertools.py
+++ b/Lib/test/test_itertools.py
@@ -4,7 +4,6 @@ from itertools import *
import weakref
from decimal import Decimal
from fractions import Fraction
-import sys
import operator
import random
import copy
@@ -644,24 +643,58 @@ class TestBasicOps(unittest.TestCase):
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
self.pickletest(proto, cycle('abc'))
+ for proto in range(pickle.HIGHEST_PROTOCOL + 1):
+ # test with partial consumed input iterable
+ it = iter('abcde')
+ c = cycle(it)
+ _ = [next(c) for i in range(2)] # consume 2 of 5 inputs
+ p = pickle.dumps(c, proto)
+ d = pickle.loads(p) # rebuild the cycle object
+ self.assertEqual(take(20, d), list('cdeabcdeabcdeabcdeab'))
+
+ # test with completely consumed input iterable
+ it = iter('abcde')
+ c = cycle(it)
+ _ = [next(c) for i in range(7)] # consume 7 of 5 inputs
+ p = pickle.dumps(c, proto)
+ d = pickle.loads(p) # rebuild the cycle object
+ self.assertEqual(take(20, d), list('cdeabcdeabcdeabcdeab'))
+
def test_cycle_setstate(self):
+ # Verify both modes for restoring state
+
+ # Mode 0 is efficient. It uses an incompletely consumed input
+ # iterator to build a cycle object and then passes in state with
+ # a list of previously consumed values. There is no data
+ # overlap between the two.
+ c = cycle('defg')
+ c.__setstate__((list('abc'), 0))
+ self.assertEqual(take(20, c), list('defgabcdefgabcdefgab'))
+
+ # Mode 1 is inefficient. It starts with a cycle object built
+ # from an iterator over the remaining elements in a partial
+ # cycle and then passes in state with all of the previously
+ # seen values (this overlaps values included in the iterator).
+ c = cycle('defg')
+ c.__setstate__((list('abcdefg'), 1))
+ self.assertEqual(take(20, c), list('defgabcdefgabcdefgab'))
+
+ # The first argument to setstate needs to be a tuple
+ with self.assertRaises(TypeError):
+ cycle('defg').__setstate__([list('abcdefg'), 0])
+
+ # The first argument in the setstate tuple must be a list
+ with self.assertRaises(TypeError):
+ c = cycle('defg')
+ c.__setstate__((tuple('defg'), 0))
+ take(20, c)
+
+ # The second argument in the setstate tuple must be an int
+ with self.assertRaises(TypeError):
+ cycle('defg').__setstate__((list('abcdefg'), 'x'))
+
self.assertRaises(TypeError, cycle('').__setstate__, ())
- self.assertRaises(TypeError, cycle('').__setstate__, [])
- self.assertRaises(TypeError, cycle('').__setstate__, 0)
self.assertRaises(TypeError, cycle('').__setstate__, ([],))
- self.assertRaises(TypeError, cycle('').__setstate__, ((), 0))
- it = cycle('abc')
- it.__setstate__((['de', 'fg'], 0))
- self.assertEqual(list(islice(it, 15)),
- ['a', 'b', 'c', 'de', 'fg',
- 'a', 'b', 'c', 'de', 'fg',
- 'a', 'b', 'c', 'de', 'fg'])
- it = cycle('abc')
- it.__setstate__((['de', 'fg'], 1))
- self.assertEqual(list(islice(it, 15)),
- ['a', 'b', 'c', 'de', 'fg',
- 'de', 'fg', 'de', 'fg', 'de',
- 'fg', 'de', 'fg', 'de', 'fg'])
def test_groupby(self):
# Check whether it accepts arguments correctly
diff --git a/Lib/test/test_json/__init__.py b/Lib/test/test_json/__init__.py
index 0807e6f..bac370d 100644
--- a/Lib/test/test_json/__init__.py
+++ b/Lib/test/test_json/__init__.py
@@ -1,5 +1,4 @@
import os
-import sys
import json
import doctest
import unittest
diff --git a/Lib/test/test_json/test_decode.py b/Lib/test/test_json/test_decode.py
index fdafeb6..7e568be 100644
--- a/Lib/test/test_json/test_decode.py
+++ b/Lib/test/test_json/test_decode.py
@@ -72,10 +72,8 @@ class TestDecode:
def test_invalid_input_type(self):
msg = 'the JSON object must be str'
- for value in [1, 3.14, b'bytes', b'\xff\x00', [], {}, None]:
+ for value in [1, 3.14, [], {}, None]:
self.assertRaisesRegex(TypeError, msg, self.loads, value)
- with self.assertRaisesRegex(TypeError, msg):
- self.json.load(BytesIO(b'[1,2,3]'))
def test_string_with_utf8_bom(self):
# see #18958
diff --git a/Lib/test/test_json/test_fail.py b/Lib/test/test_json/test_fail.py
index 95ff5b8..7910521 100644
--- a/Lib/test/test_json/test_fail.py
+++ b/Lib/test/test_json/test_fail.py
@@ -1,5 +1,4 @@
from test.test_json import PyTest, CTest
-import re
# 2007-10-05
JSONDOCS = [
diff --git a/Lib/test/test_json/test_unicode.py b/Lib/test/test_json/test_unicode.py
index c7cc8a7..2e8bba2 100644
--- a/Lib/test/test_json/test_unicode.py
+++ b/Lib/test/test_json/test_unicode.py
@@ -1,3 +1,4 @@
+import codecs
from collections import OrderedDict
from test.test_json import PyTest, CTest
@@ -52,9 +53,31 @@ class TestUnicode:
self.assertRaises(TypeError, self.dumps, [b"hi"])
def test_bytes_decode(self):
- self.assertRaises(TypeError, self.loads, b'"hi"')
- self.assertRaises(TypeError, self.loads, b'["hi"]')
-
+ for encoding, bom in [
+ ('utf-8', codecs.BOM_UTF8),
+ ('utf-16be', codecs.BOM_UTF16_BE),
+ ('utf-16le', codecs.BOM_UTF16_LE),
+ ('utf-32be', codecs.BOM_UTF32_BE),
+ ('utf-32le', codecs.BOM_UTF32_LE),
+ ]:
+ data = ["a\xb5\u20ac\U0001d120"]
+ encoded = self.dumps(data).encode(encoding)
+ self.assertEqual(self.loads(bom + encoded), data)
+ self.assertEqual(self.loads(encoded), data)
+ self.assertRaises(UnicodeDecodeError, self.loads, b'["\x80"]')
+ # RFC-7159 and ECMA-404 extend JSON to allow documents that
+ # consist of only a string, which can present a special case
+ # not covered by the encoding detection patterns specified in
+ # RFC-4627 for utf-16-le (XX 00 XX 00).
+ self.assertEqual(self.loads('"\u2600"'.encode('utf-16-le')),
+ '\u2600')
+ # Encoding detection for small (<4) bytes objects
+ # is implemented as a special case. RFC-7159 and ECMA-404
+ # allow single codepoint JSON documents which are only two
+ # bytes in utf-16 encodings w/o BOM.
+ self.assertEqual(self.loads(b'5\x00'), 5)
+ self.assertEqual(self.loads(b'\x007'), 7)
+ self.assertEqual(self.loads(b'57'), 57)
def test_object_pairs_hook_with_unicode(self):
s = '{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}'
diff --git a/Lib/test/test_kqueue.py b/Lib/test/test_kqueue.py
index f822024..9f49886 100644
--- a/Lib/test/test_kqueue.py
+++ b/Lib/test/test_kqueue.py
@@ -5,7 +5,6 @@ import errno
import os
import select
import socket
-import sys
import time
import unittest
diff --git a/Lib/test/test_linecache.py b/Lib/test/test_linecache.py
index 47e2edd..375d9c4 100644
--- a/Lib/test/test_linecache.py
+++ b/Lib/test/test_linecache.py
@@ -3,6 +3,8 @@
import linecache
import unittest
import os.path
+import tempfile
+import tokenize
from test import support
@@ -10,8 +12,6 @@ FILENAME = linecache.__file__
NONEXISTENT_FILENAME = FILENAME + '.missing'
INVALID_NAME = '!@$)(!@#_1'
EMPTY = ''
-TESTS = 'inspect_fodder inspect_fodder2 mapping_tests'
-TESTS = TESTS.split()
TEST_PATH = os.path.dirname(__file__)
MODULES = "linecache abc".split()
MODULE_PATH = os.path.dirname(FILENAME)
@@ -37,6 +37,65 @@ def f():
return 3''' # No ending newline
+class TempFile:
+
+ def setUp(self):
+ super().setUp()
+ with tempfile.NamedTemporaryFile(delete=False) as fp:
+ self.file_name = fp.name
+ fp.write(self.file_byte_string)
+ self.addCleanup(support.unlink, self.file_name)
+
+
+class GetLineTestsGoodData(TempFile):
+ # file_list = ['list\n', 'of\n', 'good\n', 'strings\n']
+
+ def setUp(self):
+ self.file_byte_string = ''.join(self.file_list).encode('utf-8')
+ super().setUp()
+
+ def test_getline(self):
+ with tokenize.open(self.file_name) as fp:
+ for index, line in enumerate(fp):
+ if not line.endswith('\n'):
+ line += '\n'
+
+ cached_line = linecache.getline(self.file_name, index + 1)
+ self.assertEqual(line, cached_line)
+
+ def test_getlines(self):
+ lines = linecache.getlines(self.file_name)
+ self.assertEqual(lines, self.file_list)
+
+
+class GetLineTestsBadData(TempFile):
+ # file_byte_string = b'Bad data goes here'
+
+ def test_getline(self):
+ self.assertRaises((SyntaxError, UnicodeDecodeError),
+ linecache.getline, self.file_name, 1)
+
+ def test_getlines(self):
+ self.assertRaises((SyntaxError, UnicodeDecodeError),
+ linecache.getlines, self.file_name)
+
+
+class EmptyFile(GetLineTestsGoodData, unittest.TestCase):
+ file_list = []
+
+
+class SingleEmptyLine(GetLineTestsGoodData, unittest.TestCase):
+ file_list = ['\n']
+
+
+class GoodUnicode(GetLineTestsGoodData, unittest.TestCase):
+ file_list = ['á\n', 'b\n', 'abcdef\n', 'ááááá\n']
+
+
+class BadUnicode(GetLineTestsBadData, unittest.TestCase):
+ file_byte_string = b'\x80abc'
+
+
class LineCacheTests(unittest.TestCase):
def test_getline(self):
@@ -53,13 +112,6 @@ class LineCacheTests(unittest.TestCase):
self.assertEqual(getline(EMPTY, 1), EMPTY)
self.assertEqual(getline(INVALID_NAME, 1), EMPTY)
- # Check whether lines correspond to those from file iteration
- for entry in TESTS:
- filename = os.path.join(TEST_PATH, entry) + '.py'
- with open(filename) as file:
- for index, line in enumerate(file):
- self.assertEqual(line, getline(filename, index + 1))
-
# Check module loading
for entry in MODULES:
filename = os.path.join(MODULE_PATH, entry) + '.py'
@@ -80,12 +132,13 @@ class LineCacheTests(unittest.TestCase):
def test_clearcache(self):
cached = []
- for entry in TESTS:
- filename = os.path.join(TEST_PATH, entry) + '.py'
+ for entry in MODULES:
+ filename = os.path.join(MODULE_PATH, entry) + '.py'
cached.append(filename)
linecache.getline(filename, 1)
# Are all files cached?
+ self.assertNotEqual(cached, [])
cached_empty = [fn for fn in cached if fn not in linecache.cache]
self.assertEqual(cached_empty, [])
diff --git a/Lib/test/test_list.py b/Lib/test/test_list.py
index 8f82ab5..aee62dc 100644
--- a/Lib/test/test_list.py
+++ b/Lib/test/test_list.py
@@ -1,5 +1,5 @@
import sys
-from test import support, list_tests
+from test import list_tests
import pickle
import unittest
diff --git a/Lib/test/test_locale.py b/Lib/test/test_locale.py
index fae2c3d..99fab58 100644
--- a/Lib/test/test_locale.py
+++ b/Lib/test/test_locale.py
@@ -1,4 +1,4 @@
-from test.support import verbose
+from test.support import verbose, is_android
import unittest
import locale
import sys
@@ -353,7 +353,7 @@ class TestEnUSCollation(BaseLocalizedTest, TestCollation):
enc = codecs.lookup(locale.getpreferredencoding(False) or 'ascii').name
if enc not in ('utf-8', 'iso8859-1', 'cp1252'):
raise unittest.SkipTest('encoding not suitable')
- if enc != 'iso8859-1' and (sys.platform == 'darwin' or
+ if enc != 'iso8859-1' and (sys.platform == 'darwin' or is_android or
sys.platform.startswith('freebsd')):
raise unittest.SkipTest('wcscoll/wcsxfrm have known bugs')
BaseLocalizedTest.setUp(self)
diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py
index 1c85045..9dedc09 100644
--- a/Lib/test/test_logging.py
+++ b/Lib/test/test_logging.py
@@ -1,4 +1,4 @@
-# Copyright 2001-2016 by Vinay Sajip. All Rights Reserved.
+# Copyright 2001-2017 by Vinay Sajip. All Rights Reserved.
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted,
@@ -16,7 +16,7 @@
"""Test harness for the logging module. Run all tests.
-Copyright (C) 2001-2016 Vinay Sajip. All Rights Reserved.
+Copyright (C) 2001-2017 Vinay Sajip. All Rights Reserved.
"""
import logging
@@ -26,6 +26,7 @@ import logging.config
import codecs
import configparser
import datetime
+import pathlib
import pickle
import io
import gc
@@ -308,6 +309,10 @@ class BuiltinLevelsTest(BaseTest):
self.assertEqual(logging.getLevelName('INFO'), logging.INFO)
self.assertEqual(logging.getLevelName(logging.INFO), 'INFO')
+ def test_issue27935(self):
+ fatal = logging.getLevelName('FATAL')
+ self.assertEqual(fatal, logging.FATAL)
+
def test_regression_29220(self):
"""See issue #29220 for more information."""
logging.addLevelName(logging.INFO, '')
@@ -583,6 +588,29 @@ class HandlerTest(BaseTest):
self.assertFalse(h.shouldFlush(r))
h.close()
+ def test_path_objects(self):
+ """
+ Test that Path objects are accepted as filename arguments to handlers.
+
+ See Issue #27493.
+ """
+ fd, fn = tempfile.mkstemp()
+ os.close(fd)
+ os.unlink(fn)
+ pfn = pathlib.Path(fn)
+ cases = (
+ (logging.FileHandler, (pfn, 'w')),
+ (logging.handlers.RotatingFileHandler, (pfn, 'a')),
+ (logging.handlers.TimedRotatingFileHandler, (pfn, 'h')),
+ )
+ if sys.platform in ('linux', 'darwin'):
+ cases += ((logging.handlers.WatchedFileHandler, (pfn, 'w')),)
+ for cls, args in cases:
+ h = cls(*args)
+ self.assertTrue(os.path.exists(fn))
+ h.close()
+ os.unlink(fn)
+
@unittest.skipIf(os.name == 'nt', 'WatchedFileHandler not appropriate for Windows.')
@unittest.skipUnless(threading, 'Threading required for this test.')
def test_race(self):
@@ -966,7 +994,7 @@ class MemoryHandlerTest(BaseTest):
def setUp(self):
BaseTest.setUp(self)
self.mem_hdlr = logging.handlers.MemoryHandler(10, logging.WARNING,
- self.root_hdlr)
+ self.root_hdlr)
self.mem_logger = logging.getLogger('mem')
self.mem_logger.propagate = 0
self.mem_logger.addHandler(self.mem_hdlr)
@@ -1003,6 +1031,36 @@ class MemoryHandlerTest(BaseTest):
self.mem_logger.debug(self.next_message())
self.assert_log_lines(lines)
+ def test_flush_on_close(self):
+ """
+ Test that the flush-on-close configuration works as expected.
+ """
+ self.mem_logger.debug(self.next_message())
+ self.assert_log_lines([])
+ self.mem_logger.info(self.next_message())
+ self.assert_log_lines([])
+ self.mem_logger.removeHandler(self.mem_hdlr)
+ # Default behaviour is to flush on close. Check that it happens.
+ self.mem_hdlr.close()
+ lines = [
+ ('DEBUG', '1'),
+ ('INFO', '2'),
+ ]
+ self.assert_log_lines(lines)
+ # Now configure for flushing not to be done on close.
+ self.mem_hdlr = logging.handlers.MemoryHandler(10, logging.WARNING,
+ self.root_hdlr,
+ False)
+ self.mem_logger.addHandler(self.mem_hdlr)
+ self.mem_logger.debug(self.next_message())
+ self.assert_log_lines(lines) # no change
+ self.mem_logger.info(self.next_message())
+ self.assert_log_lines(lines) # no change
+ self.mem_logger.removeHandler(self.mem_hdlr)
+ self.mem_hdlr.close()
+ # assert that no new lines have been added
+ self.assert_log_lines(lines) # no change
+
class ExceptionFormatter(logging.Formatter):
"""A special exception formatter."""
@@ -1390,9 +1448,17 @@ class SocketHandlerTest(BaseTest):
"""Set up a TCP server to receive log messages, and a SocketHandler
pointing to that server's address and port."""
BaseTest.setUp(self)
- self.server = server = self.server_class(self.address,
- self.handle_socket, 0.01)
- server.start()
+ # Issue #29177: deal with errors that happen during setup
+ self.server = self.sock_hdlr = self.server_exception = None
+ try:
+ self.server = server = self.server_class(self.address,
+ self.handle_socket, 0.01)
+ server.start()
+ # Uncomment next line to test error recovery in setUp()
+ # raise OSError('dummy error raised')
+ except OSError as e:
+ self.server_exception = e
+ return
server.ready.wait()
hcls = logging.handlers.SocketHandler
if isinstance(server.server_address, tuple):
@@ -1407,9 +1473,11 @@ class SocketHandlerTest(BaseTest):
def tearDown(self):
"""Shutdown the TCP server."""
try:
- self.server.stop(2.0)
- self.root_logger.removeHandler(self.sock_hdlr)
- self.sock_hdlr.close()
+ if self.server:
+ self.server.stop(2.0)
+ if self.sock_hdlr:
+ self.root_logger.removeHandler(self.sock_hdlr)
+ self.sock_hdlr.close()
finally:
BaseTest.tearDown(self)
@@ -1430,6 +1498,8 @@ class SocketHandlerTest(BaseTest):
def test_output(self):
# The log message sent to the SocketHandler is properly received.
+ if self.server_exception:
+ self.skipTest(self.server_exception)
logger = logging.getLogger("tcp")
logger.error("spam")
self.handled.acquire()
@@ -1438,6 +1508,8 @@ class SocketHandlerTest(BaseTest):
self.assertEqual(self.log_output, "spam\neggs\n")
def test_noserver(self):
+ if self.server_exception:
+ self.skipTest(self.server_exception)
# Avoid timing-related failures due to SocketHandler's own hard-wired
# one-second timeout on socket.create_connection() (issue #16264).
self.sock_hdlr.retryStart = 2.5
@@ -1478,7 +1550,7 @@ class UnixSocketHandlerTest(SocketHandlerTest):
def tearDown(self):
SocketHandlerTest.tearDown(self)
- os.remove(self.address)
+ support.unlink(self.address)
@unittest.skipUnless(threading, 'Threading required for this test.')
class DatagramHandlerTest(BaseTest):
@@ -1493,9 +1565,17 @@ class DatagramHandlerTest(BaseTest):
"""Set up a UDP server to receive log messages, and a DatagramHandler
pointing to that server's address and port."""
BaseTest.setUp(self)
- self.server = server = self.server_class(self.address,
- self.handle_datagram, 0.01)
- server.start()
+ # Issue #29177: deal with errors that happen during setup
+ self.server = self.sock_hdlr = self.server_exception = None
+ try:
+ self.server = server = self.server_class(self.address,
+ self.handle_datagram, 0.01)
+ server.start()
+ # Uncomment next line to test error recovery in setUp()
+ # raise OSError('dummy error raised')
+ except OSError as e:
+ self.server_exception = e
+ return
server.ready.wait()
hcls = logging.handlers.DatagramHandler
if isinstance(server.server_address, tuple):
@@ -1510,9 +1590,11 @@ class DatagramHandlerTest(BaseTest):
def tearDown(self):
"""Shutdown the UDP server."""
try:
- self.server.stop(2.0)
- self.root_logger.removeHandler(self.sock_hdlr)
- self.sock_hdlr.close()
+ if self.server:
+ self.server.stop(2.0)
+ if self.sock_hdlr:
+ self.root_logger.removeHandler(self.sock_hdlr)
+ self.sock_hdlr.close()
finally:
BaseTest.tearDown(self)
@@ -1526,6 +1608,8 @@ class DatagramHandlerTest(BaseTest):
def test_output(self):
# The log message sent to the DatagramHandler is properly received.
+ if self.server_exception:
+ self.skipTest(self.server_exception)
logger = logging.getLogger("udp")
logger.error("spam")
self.handled.wait()
@@ -1550,7 +1634,7 @@ class UnixDatagramHandlerTest(DatagramHandlerTest):
def tearDown(self):
DatagramHandlerTest.tearDown(self)
- os.remove(self.address)
+ support.unlink(self.address)
@unittest.skipUnless(threading, 'Threading required for this test.')
class SysLogHandlerTest(BaseTest):
@@ -1565,9 +1649,17 @@ class SysLogHandlerTest(BaseTest):
"""Set up a UDP server to receive log messages, and a SysLogHandler
pointing to that server's address and port."""
BaseTest.setUp(self)
- self.server = server = self.server_class(self.address,
- self.handle_datagram, 0.01)
- server.start()
+ # Issue #29177: deal with errors that happen during setup
+ self.server = self.sl_hdlr = self.server_exception = None
+ try:
+ self.server = server = self.server_class(self.address,
+ self.handle_datagram, 0.01)
+ server.start()
+ # Uncomment next line to test error recovery in setUp()
+ # raise OSError('dummy error raised')
+ except OSError as e:
+ self.server_exception = e
+ return
server.ready.wait()
hcls = logging.handlers.SysLogHandler
if isinstance(server.server_address, tuple):
@@ -1580,11 +1672,13 @@ class SysLogHandlerTest(BaseTest):
self.handled = threading.Event()
def tearDown(self):
- """Shutdown the UDP server."""
+ """Shutdown the server."""
try:
- self.server.stop(2.0)
- self.root_logger.removeHandler(self.sl_hdlr)
- self.sl_hdlr.close()
+ if self.server:
+ self.server.stop(2.0)
+ if self.sl_hdlr:
+ self.root_logger.removeHandler(self.sl_hdlr)
+ self.sl_hdlr.close()
finally:
BaseTest.tearDown(self)
@@ -1593,6 +1687,8 @@ class SysLogHandlerTest(BaseTest):
self.handled.set()
def test_output(self):
+ if self.server_exception:
+ self.skipTest(self.server_exception)
# The log message sent to the SysLogHandler is properly received.
logger = logging.getLogger("slh")
logger.error("sp\xe4m")
@@ -1625,7 +1721,7 @@ class UnixSysLogHandlerTest(SysLogHandlerTest):
def tearDown(self):
SysLogHandlerTest.tearDown(self)
- os.remove(self.address)
+ support.unlink(self.address)
@unittest.skipUnless(threading, 'Threading required for this test.')
class HTTPHandlerTest(BaseTest):
@@ -4249,6 +4345,17 @@ class NTEventLogHandlerTest(BaseTest):
msg = 'Record not found in event log, went back %d records' % GO_BACK
self.assertTrue(found, msg=msg)
+
+class MiscTestCase(unittest.TestCase):
+ def test__all__(self):
+ blacklist = {'logThreads', 'logMultiprocessing',
+ 'logProcesses', 'currentframe',
+ 'PercentStyle', 'StrFormatStyle', 'StringTemplateStyle',
+ 'Filterer', 'PlaceHolder', 'Manager', 'RootLogger',
+ 'root', 'threading'}
+ support.check__all__(self, logging, blacklist=blacklist)
+
+
# Set the locale to the platform-dependent default. I have no idea
# why the test does this, but in any case we save the current locale
# first and restore it at the end.
@@ -4266,6 +4373,7 @@ def test_main():
ExceptionTest, SysLogHandlerTest, HTTPHandlerTest,
NTEventLogHandlerTest, TimedRotatingFileHandlerTest,
UnixSocketHandlerTest, UnixDatagramHandlerTest, UnixSysLogHandlerTest,
+ MiscTestCase
]
if hasattr(logging.handlers, 'QueueListener'):
tests.append(QueueListenerTest)
diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py
index 4b2d81c..fd15f04 100644
--- a/Lib/test/test_long.py
+++ b/Lib/test/test_long.py
@@ -621,6 +621,8 @@ class LongTest(unittest.TestCase):
def test__format__(self):
self.assertEqual(format(123456789, 'd'), '123456789')
self.assertEqual(format(123456789, 'd'), '123456789')
+ self.assertEqual(format(123456789, ','), '123,456,789')
+ self.assertEqual(format(123456789, '_'), '123_456_789')
# sign and aligning are interdependent
self.assertEqual(format(1, "-"), '1')
@@ -649,8 +651,25 @@ class LongTest(unittest.TestCase):
self.assertEqual(format(int('be', 16), "X"), "BE")
self.assertEqual(format(-int('be', 16), "x"), "-be")
self.assertEqual(format(-int('be', 16), "X"), "-BE")
+ self.assertRaises(ValueError, format, 1234567890, ',x')
+ self.assertEqual(format(1234567890, '_x'), '4996_02d2')
+ self.assertEqual(format(1234567890, '_X'), '4996_02D2')
# octal
+ self.assertEqual(format(3, "o"), "3")
+ self.assertEqual(format(-3, "o"), "-3")
+ self.assertEqual(format(1234, "o"), "2322")
+ self.assertEqual(format(-1234, "o"), "-2322")
+ self.assertEqual(format(1234, "-o"), "2322")
+ self.assertEqual(format(-1234, "-o"), "-2322")
+ self.assertEqual(format(1234, " o"), " 2322")
+ self.assertEqual(format(-1234, " o"), "-2322")
+ self.assertEqual(format(1234, "+o"), "+2322")
+ self.assertEqual(format(-1234, "+o"), "-2322")
+ self.assertRaises(ValueError, format, 1234567890, ',o')
+ self.assertEqual(format(1234567890, '_o'), '111_4540_1322')
+
+ # binary
self.assertEqual(format(3, "b"), "11")
self.assertEqual(format(-3, "b"), "-11")
self.assertEqual(format(1234, "b"), "10011010010")
@@ -661,12 +680,21 @@ class LongTest(unittest.TestCase):
self.assertEqual(format(-1234, " b"), "-10011010010")
self.assertEqual(format(1234, "+b"), "+10011010010")
self.assertEqual(format(-1234, "+b"), "-10011010010")
+ self.assertRaises(ValueError, format, 1234567890, ',b')
+ self.assertEqual(format(12345, '_b'), '11_0000_0011_1001')
# make sure these are errors
self.assertRaises(ValueError, format, 3, "1.3") # precision disallowed
+ self.assertRaises(ValueError, format, 3, "_c") # underscore,
+ self.assertRaises(ValueError, format, 3, ",c") # comma, and
self.assertRaises(ValueError, format, 3, "+c") # sign not allowed
# with 'c'
+ self.assertRaisesRegex(ValueError, 'Cannot specify both', format, 3, '_,')
+ self.assertRaisesRegex(ValueError, 'Cannot specify both', format, 3, ',_')
+ self.assertRaisesRegex(ValueError, 'Cannot specify both', format, 3, '_,d')
+ self.assertRaisesRegex(ValueError, 'Cannot specify both', format, 3, ',_d')
+
# ensure that only int and float type specifiers work
for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +
[chr(x) for x in range(ord('A'), ord('Z')+1)]):
@@ -689,6 +717,20 @@ class LongTest(unittest.TestCase):
self.assertRaises(OverflowError, int, float('-inf'))
self.assertRaises(ValueError, int, float('nan'))
+ def test_mod_division(self):
+ with self.assertRaises(ZeroDivisionError):
+ _ = 1 % 0
+
+ self.assertEqual(13 % 10, 3)
+ self.assertEqual(-13 % 10, 7)
+ self.assertEqual(13 % -10, -7)
+ self.assertEqual(-13 % -10, -3)
+
+ self.assertEqual(12 % 4, 0)
+ self.assertEqual(-12 % 4, 0)
+ self.assertEqual(12 % -4, 0)
+ self.assertEqual(-12 % -4, 0)
+
def test_true_division(self):
huge = 1 << 40000
mhuge = -huge
@@ -723,6 +765,25 @@ class LongTest(unittest.TestCase):
for zero in ["huge / 0", "mhuge / 0"]:
self.assertRaises(ZeroDivisionError, eval, zero, namespace)
+ def test_floordiv(self):
+ with self.assertRaises(ZeroDivisionError):
+ _ = 1 // 0
+
+ self.assertEqual(2 // 3, 0)
+ self.assertEqual(2 // -3, -1)
+ self.assertEqual(-2 // 3, -1)
+ self.assertEqual(-2 // -3, 0)
+
+ self.assertEqual(-11 // -3, 3)
+ self.assertEqual(-11 // 3, -4)
+ self.assertEqual(11 // -3, -4)
+ self.assertEqual(11 // 3, 3)
+
+ self.assertEqual(-12 // -3, 4)
+ self.assertEqual(-12 // 3, -4)
+ self.assertEqual(12 // -3, -4)
+ self.assertEqual(12 // 3, 4)
+
def check_truediv(self, a, b, skip_small=True):
"""Verify that the result of a/b is correctly rounded, by
comparing it with a pure Python implementation of correctly
@@ -845,6 +906,21 @@ class LongTest(unittest.TestCase):
self.check_truediv(-x, y)
self.check_truediv(-x, -y)
+ def test_lshift_of_zero(self):
+ self.assertEqual(0 << 0, 0)
+ self.assertEqual(0 << 10, 0)
+ with self.assertRaises(ValueError):
+ 0 << -1
+
+ @support.cpython_only
+ def test_huge_lshift_of_zero(self):
+ # Shouldn't try to allocate memory for a huge shift. See issue #27870.
+ # Other implementations may have a different boundary for overflow,
+ # or not raise at all.
+ self.assertEqual(0 << sys.maxsize, 0)
+ with self.assertRaises(OverflowError):
+ 0 << (sys.maxsize + 1)
+
def test_small_ints(self):
for i in range(-5, 257):
self.assertIs(i, i + 0)
diff --git a/Lib/test/test_lzma.py b/Lib/test/test_lzma.py
index b69da06..d7a8576 100644
--- a/Lib/test/test_lzma.py
+++ b/Lib/test/test_lzma.py
@@ -1,6 +1,7 @@
import _compression
from io import BytesIO, UnsupportedOperation, DEFAULT_BUFFER_SIZE
import os
+import pathlib
import pickle
import random
import unittest
@@ -526,6 +527,16 @@ class FileTestCase(unittest.TestCase):
with LZMAFile(BytesIO(), "a") as f:
pass
+ def test_init_with_PathLike_filename(self):
+ filename = pathlib.Path(TESTFN)
+ with TempFile(filename, COMPRESSED_XZ):
+ with LZMAFile(filename) as f:
+ self.assertEqual(f.read(), INPUT)
+ with LZMAFile(filename, "a") as f:
+ f.write(INPUT)
+ with LZMAFile(filename) as f:
+ self.assertEqual(f.read(), INPUT * 2)
+
def test_init_with_filename(self):
with TempFile(TESTFN, COMPRESSED_XZ):
with LZMAFile(TESTFN) as f:
@@ -973,11 +984,11 @@ class FileTestCase(unittest.TestCase):
def test_decompress_limited(self):
"""Decompressed data buffering should be limited"""
- bomb = lzma.compress(bytes(int(2e6)), preset=6)
+ bomb = lzma.compress(b'\0' * int(2e6), preset=6)
self.assertLess(len(bomb), _compression.BUFFER_SIZE)
decomp = LZMAFile(BytesIO(bomb))
- self.assertEqual(bytes(1), decomp.read(1))
+ self.assertEqual(decomp.read(1), b'\0')
max_decomp = 1 + DEFAULT_BUFFER_SIZE
self.assertLessEqual(decomp._buffer.raw.tell(), max_decomp,
"Excessive amount of data was decompressed")
@@ -1218,6 +1229,17 @@ class OpenTestCase(unittest.TestCase):
with lzma.open(TESTFN, "rb") as f:
self.assertEqual(f.read(), INPUT * 2)
+ def test_with_pathlike_filename(self):
+ filename = pathlib.Path(TESTFN)
+ with TempFile(filename):
+ with lzma.open(filename, "wb") as f:
+ f.write(INPUT)
+ with open(filename, "rb") as f:
+ file_data = lzma.decompress(f.read())
+ self.assertEqual(file_data, INPUT)
+ with lzma.open(filename, "rb") as f:
+ self.assertEqual(f.read(), INPUT)
+
def test_bad_params(self):
# Test invalid parameter combinations.
with self.assertRaises(ValueError):
diff --git a/Lib/test/test_macpath.py b/Lib/test/test_macpath.py
index 80bec7a..0698ff5 100644
--- a/Lib/test/test_macpath.py
+++ b/Lib/test/test_macpath.py
@@ -1,5 +1,5 @@
import macpath
-from test import support, test_genericpath
+from test import test_genericpath
import unittest
diff --git a/Lib/test/test_mailbox.py b/Lib/test/test_mailbox.py
index 0991f74..2ba9443 100644
--- a/Lib/test/test_mailbox.py
+++ b/Lib/test/test_mailbox.py
@@ -7,17 +7,12 @@ import email
import email.message
import re
import io
-import shutil
import tempfile
from test import support
import unittest
import textwrap
import mailbox
import glob
-try:
- import fcntl
-except ImportError:
- pass
class TestBase:
@@ -2142,9 +2137,9 @@ class MaildirTestCase(unittest.TestCase):
if mbox:
fp.write(FROM_)
fp.write(DUMMY_MESSAGE)
- if hasattr(os, "link"):
+ try:
os.link(tmpname, newname)
- else:
+ except (AttributeError, PermissionError):
with open(newname, "w") as fp:
fp.write(DUMMY_MESSAGE)
self._msgfiles.append(newname)
@@ -2273,12 +2268,18 @@ Gregory K. Johnson
""")
+class MiscTestCase(unittest.TestCase):
+ def test__all__(self):
+ blacklist = {"linesep", "fcntl"}
+ support.check__all__(self, mailbox, blacklist=blacklist)
+
+
def test_main():
tests = (TestMailboxSuperclass, TestMaildir, TestMbox, TestMMDF, TestMH,
TestBabyl, TestMessage, TestMaildirMessage, TestMboxMessage,
TestMHMessage, TestBabylMessage, TestMMDFMessage,
TestMessageConversion, TestProxyFile, TestPartialFile,
- MaildirTestCase, TestFakeMailBox)
+ MaildirTestCase, TestFakeMailBox, MiscTestCase)
support.run_unittest(*tests)
support.reap_children()
diff --git a/Lib/test/test_mailcap.py b/Lib/test/test_mailcap.py
index a85c691..c08423c 100644
--- a/Lib/test/test_mailcap.py
+++ b/Lib/test/test_mailcap.py
@@ -1,6 +1,5 @@
import mailcap
import os
-import shutil
import copy
import test.support
import unittest
@@ -123,7 +122,7 @@ class HelperFunctionTest(unittest.TestCase):
(["echo foo", "audio/*", "foo.txt"], "echo foo"),
(["echo %s", "audio/*", "foo.txt"], "echo foo.txt"),
(["echo %t", "audio/*", "foo.txt"], "echo audio/*"),
- (["echo \%t", "audio/*", "foo.txt"], "echo %t"),
+ (["echo \\%t", "audio/*", "foo.txt"], "echo %t"),
(["echo foo", "audio/*", "foo.txt", plist], "echo foo"),
(["echo %{total}", "audio/*", "foo.txt", plist], "echo 3")
]
diff --git a/Lib/test/test_marshal.py b/Lib/test/test_marshal.py
index c7def9a..b378ffe 100644
--- a/Lib/test/test_marshal.py
+++ b/Lib/test/test_marshal.py
@@ -135,6 +135,13 @@ class ContainerTestCase(unittest.TestCase, HelperMixin):
for constructor in (set, frozenset):
self.helper(constructor(self.d.keys()))
+ @support.cpython_only
+ def test_empty_frozenset_singleton(self):
+ # marshal.loads() must reuse the empty frozenset singleton
+ obj = frozenset()
+ obj2 = marshal.loads(marshal.dumps(obj))
+ self.assertIs(obj2, obj)
+
class BufferTestCase(unittest.TestCase, HelperMixin):
diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py
index a379a6a..eaa41bc 100644
--- a/Lib/test/test_math.py
+++ b/Lib/test/test_math.py
@@ -7,14 +7,15 @@ import unittest
import math
import os
import platform
-import sys
import struct
+import sys
import sysconfig
eps = 1E-05
NAN = float('nan')
INF = float('inf')
NINF = float('-inf')
+FLOAT_MAX = sys.float_info.max
# detect evidence of double-rounding: fsum is not always correctly
# rounded on machines that suffer from double rounding.
@@ -30,6 +31,7 @@ test_dir = os.path.dirname(file) or os.curdir
math_testcases = os.path.join(test_dir, 'math_testcases.txt')
test_file = os.path.join(test_dir, 'cmath_testcases.txt')
+
def to_ulps(x):
"""Convert a non-NaN float x to an integer, in such a way that
adjacent floats are converted to adjacent integers. Then
@@ -37,25 +39,39 @@ def to_ulps(x):
floats.
The results from this function will only make sense on platforms
- where C doubles are represented in IEEE 754 binary64 format.
+ where native doubles are represented in IEEE 754 binary64 format.
+ Note: 0.0 and -0.0 are converted to 0 and -1, respectively.
"""
n = struct.unpack('<q', struct.pack('<d', x))[0]
if n < 0:
n = ~(n+2**63)
return n
-def ulps_check(expected, got, ulps=20):
- """Given non-NaN floats `expected` and `got`,
- check that they're equal to within the given number of ulps.
- Returns None on success and an error message on failure."""
+def ulp(x):
+ """Return the value of the least significant bit of a
+ float x, such that the first float bigger than x is x+ulp(x).
+ Then, given an expected result x and a tolerance of n ulps,
+ the result y should be such that abs(y-x) <= n * ulp(x).
+ The results from this function will only make sense on platforms
+ where native doubles are represented in IEEE 754 binary64 format.
+ """
+ x = abs(float(x))
+ if math.isnan(x) or math.isinf(x):
+ return x
- ulps_error = to_ulps(got) - to_ulps(expected)
- if abs(ulps_error) <= ulps:
- return None
- return "error = {} ulps; permitted error = {} ulps".format(ulps_error,
- ulps)
+ # Find next float up from x.
+ n = struct.unpack('<q', struct.pack('<d', x))[0]
+ x_next = struct.unpack('<d', struct.pack('<q', n + 1))[0]
+ if math.isinf(x_next):
+ # Corner case: x was the largest finite float. Then it's
+ # not an exact power of two, so we can take the difference
+ # between x and the previous float.
+ x_prev = struct.unpack('<d', struct.pack('<q', n - 1))[0]
+ return x - x_prev
+ else:
+ return x_next - x
# Here's a pure Python version of the math.factorial algorithm, for
# documentation and comparison purposes.
@@ -107,24 +123,23 @@ def py_factorial(n):
outer *= inner
return outer << (n - count_set_bits(n))
-def acc_check(expected, got, rel_err=2e-15, abs_err = 5e-323):
- """Determine whether non-NaN floats a and b are equal to within a
- (small) rounding error. The default values for rel_err and
- abs_err are chosen to be suitable for platforms where a float is
- represented by an IEEE 754 double. They allow an error of between
- 9 and 19 ulps."""
-
- # need to special case infinities, since inf - inf gives nan
- if math.isinf(expected) and got == expected:
- return None
+def ulp_abs_check(expected, got, ulp_tol, abs_tol):
+ """Given finite floats `expected` and `got`, check that they're
+ approximately equal to within the given number of ulps or the
+ given absolute tolerance, whichever is bigger.
- error = got - expected
+ Returns None on success and an error message on failure.
+ """
+ ulp_error = abs(to_ulps(expected) - to_ulps(got))
+ abs_error = abs(expected - got)
- permitted_error = max(abs_err, rel_err * abs(expected))
- if abs(error) < permitted_error:
+ # Succeed if either abs_error <= abs_tol or ulp_error <= ulp_tol.
+ if abs_error <= abs_tol or ulp_error <= ulp_tol:
return None
- return "error = {}; permitted error = {}".format(error,
- permitted_error)
+ else:
+ fmt = ("error = {:.3g} ({:d} ulps); "
+ "permitted error = {:.3g} or {:d} ulps")
+ return fmt.format(abs_error, ulp_error, abs_tol, ulp_tol)
def parse_mtestfile(fname):
"""Parse a file with test values
@@ -151,6 +166,7 @@ def parse_mtestfile(fname):
yield (id, fn, float(arg), float(exp), flags)
+
def parse_testfile(fname):
"""Parse a file with test values
@@ -172,8 +188,53 @@ def parse_testfile(fname):
yield (id, fn,
float(arg_real), float(arg_imag),
float(exp_real), float(exp_imag),
- flags
- )
+ flags)
+
+
+def result_check(expected, got, ulp_tol=5, abs_tol=0.0):
+ # Common logic of MathTests.(ftest, test_testcases, test_mtestcases)
+ """Compare arguments expected and got, as floats, if either
+ is a float, using a tolerance expressed in multiples of
+ ulp(expected) or absolutely (if given and greater).
+
+ As a convenience, when neither argument is a float, and for
+ non-finite floats, exact equality is demanded. Also, nan==nan
+ as far as this function is concerned.
+
+ Returns None on success and an error message on failure.
+ """
+
+ # Check exactly equal (applies also to strings representing exceptions)
+ if got == expected:
+ return None
+
+ failure = "not equal"
+
+ # Turn mixed float and int comparison (e.g. floor()) to all-float
+ if isinstance(expected, float) and isinstance(got, int):
+ got = float(got)
+ elif isinstance(got, float) and isinstance(expected, int):
+ expected = float(expected)
+
+ if isinstance(expected, float) and isinstance(got, float):
+ if math.isnan(expected) and math.isnan(got):
+ # Pass, since both nan
+ failure = None
+ elif math.isinf(expected) or math.isinf(got):
+ # We already know they're not equal, drop through to failure
+ pass
+ else:
+ # Both are finite floats (now). Are they close enough?
+ failure = ulp_abs_check(expected, got, ulp_tol, abs_tol)
+
+ # arguments are not equal, and if numeric, are too far apart
+ if failure is not None:
+ fail_fmt = "expected {!r}, got {!r}"
+ fail_msg = fail_fmt.format(expected, got)
+ fail_msg += ' ({})'.format(failure)
+ return fail_msg
+ else:
+ return None
# Class providing an __index__ method.
class MyIndexable(object):
@@ -185,18 +246,24 @@ class MyIndexable(object):
class MathTests(unittest.TestCase):
- def ftest(self, name, value, expected):
- if abs(value-expected) > eps:
- # Use %r instead of %f so the error message
- # displays full precision. Otherwise discrepancies
- # in the last few bits will lead to very confusing
- # error messages
- self.fail('%s returned %r, expected %r' %
- (name, value, expected))
+ def ftest(self, name, got, expected, ulp_tol=5, abs_tol=0.0):
+ """Compare arguments expected and got, as floats, if either
+ is a float, using a tolerance expressed in multiples of
+ ulp(expected) or absolutely, whichever is greater.
+
+ As a convenience, when neither argument is a float, and for
+ non-finite floats, exact equality is demanded. Also, nan==nan
+ in this function.
+ """
+ failure = result_check(expected, got, ulp_tol, abs_tol)
+ if failure is not None:
+ self.fail("{}: {}".format(name, failure))
def testConstants(self):
- self.ftest('pi', math.pi, 3.1415926)
- self.ftest('e', math.e, 2.7182818)
+ # Ref: Abramowitz & Stegun (Dover, 1965)
+ self.ftest('pi', math.pi, 3.141592653589793238462643)
+ self.ftest('e', math.e, 2.718281828459045235360287)
+ self.assertEqual(math.tau, 2*math.pi)
def testAcos(self):
self.assertRaises(TypeError, math.acos)
@@ -205,6 +272,8 @@ class MathTests(unittest.TestCase):
self.ftest('acos(1)', math.acos(1), 0)
self.assertRaises(ValueError, math.acos, INF)
self.assertRaises(ValueError, math.acos, NINF)
+ self.assertRaises(ValueError, math.acos, 1 + eps)
+ self.assertRaises(ValueError, math.acos, -1 - eps)
self.assertTrue(math.isnan(math.acos(NAN)))
def testAcosh(self):
@@ -224,6 +293,8 @@ class MathTests(unittest.TestCase):
self.ftest('asin(1)', math.asin(1), math.pi/2)
self.assertRaises(ValueError, math.asin, INF)
self.assertRaises(ValueError, math.asin, NINF)
+ self.assertRaises(ValueError, math.asin, 1 + eps)
+ self.assertRaises(ValueError, math.asin, -1 - eps)
self.assertTrue(math.isnan(math.asin(NAN)))
def testAsinh(self):
@@ -378,9 +449,9 @@ class MathTests(unittest.TestCase):
def testCos(self):
self.assertRaises(TypeError, math.cos)
- self.ftest('cos(-pi/2)', math.cos(-math.pi/2), 0)
+ self.ftest('cos(-pi/2)', math.cos(-math.pi/2), 0, abs_tol=ulp(1))
self.ftest('cos(0)', math.cos(0), 1)
- self.ftest('cos(pi/2)', math.cos(math.pi/2), 0)
+ self.ftest('cos(pi/2)', math.cos(math.pi/2), 0, abs_tol=ulp(1))
self.ftest('cos(pi)', math.cos(math.pi), -1)
try:
self.assertTrue(math.isnan(math.cos(INF)))
@@ -403,6 +474,7 @@ class MathTests(unittest.TestCase):
self.ftest('degrees(pi)', math.degrees(math.pi), 180.0)
self.ftest('degrees(pi/2)', math.degrees(math.pi/2), 90.0)
self.ftest('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0)
+ self.ftest('degrees(0)', math.degrees(0), 0)
def testExp(self):
self.assertRaises(TypeError, math.exp)
@@ -412,6 +484,7 @@ class MathTests(unittest.TestCase):
self.assertEqual(math.exp(INF), INF)
self.assertEqual(math.exp(NINF), 0.)
self.assertTrue(math.isnan(math.exp(NAN)))
+ self.assertRaises(OverflowError, math.exp, 1000000)
def testFabs(self):
self.assertRaises(TypeError, math.fabs)
@@ -654,6 +727,7 @@ class MathTests(unittest.TestCase):
self.assertEqual(math.hypot(INF, NAN), INF)
self.assertEqual(math.hypot(NAN, NINF), INF)
self.assertEqual(math.hypot(NINF, NAN), INF)
+ self.assertRaises(OverflowError, math.hypot, FLOAT_MAX, FLOAT_MAX)
self.assertTrue(math.isnan(math.hypot(1.0, NAN)))
self.assertTrue(math.isnan(math.hypot(NAN, -2.0)))
@@ -707,8 +781,10 @@ class MathTests(unittest.TestCase):
def testLog1p(self):
self.assertRaises(TypeError, math.log1p)
- n= 2**90
- self.assertAlmostEqual(math.log1p(n), math.log1p(float(n)))
+ for n in [2, 2**90, 2**300]:
+ self.assertAlmostEqual(math.log1p(n), math.log1p(float(n)))
+ self.assertRaises(ValueError, math.log1p, -1)
+ self.assertEqual(math.log1p(INF), INF)
@requires_IEEE_754
def testLog2(self):
@@ -922,6 +998,7 @@ class MathTests(unittest.TestCase):
self.ftest('radians(180)', math.radians(180), math.pi)
self.ftest('radians(90)', math.radians(90), math.pi/2)
self.ftest('radians(-45)', math.radians(-45), -math.pi/4)
+ self.ftest('radians(0)', math.radians(0), 0)
def testSin(self):
self.assertRaises(TypeError, math.sin)
@@ -951,6 +1028,7 @@ class MathTests(unittest.TestCase):
self.ftest('sqrt(1)', math.sqrt(1), 1)
self.ftest('sqrt(4)', math.sqrt(4), 2)
self.assertEqual(math.sqrt(INF), INF)
+ self.assertRaises(ValueError, math.sqrt, -1)
self.assertRaises(ValueError, math.sqrt, NINF)
self.assertTrue(math.isnan(math.sqrt(NAN)))
@@ -970,7 +1048,8 @@ class MathTests(unittest.TestCase):
def testTanh(self):
self.assertRaises(TypeError, math.tanh)
self.ftest('tanh(0)', math.tanh(0), 0)
- self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
+ self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0,
+ abs_tol=ulp(1))
self.ftest('tanh(inf)', math.tanh(INF), 1)
self.ftest('tanh(-inf)', math.tanh(NINF), -1)
self.assertTrue(math.isnan(math.tanh(NAN)))
@@ -1020,7 +1099,8 @@ class MathTests(unittest.TestCase):
def testIsnan(self):
self.assertTrue(math.isnan(float("nan")))
- self.assertTrue(math.isnan(float("inf")* 0.))
+ self.assertTrue(math.isnan(float("-nan")))
+ self.assertTrue(math.isnan(float("inf") * 0.))
self.assertFalse(math.isnan(float("inf")))
self.assertFalse(math.isnan(0.))
self.assertFalse(math.isnan(1.))
@@ -1084,30 +1164,64 @@ class MathTests(unittest.TestCase):
@requires_IEEE_754
def test_testfile(self):
+ # Some tests need to be skipped on ancient OS X versions.
+ # See issue #27953.
+ SKIP_ON_TIGER = {'tan0064'}
+
+ osx_version = None
+ if sys.platform == 'darwin':
+ version_txt = platform.mac_ver()[0]
+ try:
+ osx_version = tuple(map(int, version_txt.split('.')))
+ except ValueError:
+ pass
+
+ fail_fmt = "{}: {}({!r}): {}"
+
+ failures = []
for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file):
- # Skip if either the input or result is complex, or if
- # flags is nonempty
- if ai != 0. or ei != 0. or flags:
+ # Skip if either the input or result is complex
+ if ai != 0.0 or ei != 0.0:
continue
if fn in ['rect', 'polar']:
# no real versions of rect, polar
continue
+ # Skip certain tests on OS X 10.4.
+ if osx_version is not None and osx_version < (10, 5):
+ if id in SKIP_ON_TIGER:
+ continue
+
func = getattr(math, fn)
+
+ if 'invalid' in flags or 'divide-by-zero' in flags:
+ er = 'ValueError'
+ elif 'overflow' in flags:
+ er = 'OverflowError'
+
try:
result = func(ar)
- except ValueError as exc:
- message = (("Unexpected ValueError: %s\n " +
- "in test %s:%s(%r)\n") % (exc.args[0], id, fn, ar))
- self.fail(message)
+ except ValueError:
+ result = 'ValueError'
except OverflowError:
- message = ("Unexpected OverflowError in " +
- "test %s:%s(%r)\n" % (id, fn, ar))
- self.fail(message)
- self.ftest("%s:%s(%r)" % (id, fn, ar), result, er)
+ result = 'OverflowError'
+
+ # Default tolerances
+ ulp_tol, abs_tol = 5, 0.0
+
+ failure = result_check(er, result, ulp_tol, abs_tol)
+ if failure is None:
+ continue
+
+ msg = fail_fmt.format(id, fn, ar, failure)
+ failures.append(msg)
+
+ if failures:
+ self.fail('Failures in test_testfile:\n ' +
+ '\n '.join(failures))
@requires_IEEE_754
def test_mtestfile(self):
- fail_fmt = "{}:{}({!r}): expected {!r}, got {!r}"
+ fail_fmt = "{}: {}({!r}): {}"
failures = []
for id, fn, arg, expected, flags in parse_mtestfile(math_testcases):
@@ -1125,41 +1239,48 @@ class MathTests(unittest.TestCase):
except OverflowError:
got = 'OverflowError'
- accuracy_failure = None
- if isinstance(got, float) and isinstance(expected, float):
- if math.isnan(expected) and math.isnan(got):
- continue
- if not math.isnan(expected) and not math.isnan(got):
- if fn == 'lgamma':
- # we use a weaker accuracy test for lgamma;
- # lgamma only achieves an absolute error of
- # a few multiples of the machine accuracy, in
- # general.
- accuracy_failure = acc_check(expected, got,
- rel_err = 5e-15,
- abs_err = 5e-15)
- elif fn == 'erfc':
- # erfc has less-than-ideal accuracy for large
- # arguments (x ~ 25 or so), mainly due to the
- # error involved in computing exp(-x*x).
- #
- # XXX Would be better to weaken this test only
- # for large x, instead of for all x.
- accuracy_failure = ulps_check(expected, got, 2000)
-
- else:
- accuracy_failure = ulps_check(expected, got, 20)
- if accuracy_failure is None:
- continue
-
- if isinstance(got, str) and isinstance(expected, str):
- if got == expected:
- continue
+ # Default tolerances
+ ulp_tol, abs_tol = 5, 0.0
+
+ # Exceptions to the defaults
+ if fn == 'gamma':
+ # Experimental results on one platform gave
+ # an accuracy of <= 10 ulps across the entire float
+ # domain. We weaken that to require 20 ulp accuracy.
+ ulp_tol = 20
+
+ elif fn == 'lgamma':
+ # we use a weaker accuracy test for lgamma;
+ # lgamma only achieves an absolute error of
+ # a few multiples of the machine accuracy, in
+ # general.
+ abs_tol = 1e-15
+
+ elif fn == 'erfc' and arg >= 0.0:
+ # erfc has less-than-ideal accuracy for large
+ # arguments (x ~ 25 or so), mainly due to the
+ # error involved in computing exp(-x*x).
+ #
+ # Observed between CPython and mpmath at 25 dp:
+ # x < 0 : err <= 2 ulp
+ # 0 <= x < 1 : err <= 10 ulp
+ # 1 <= x < 10 : err <= 100 ulp
+ # 10 <= x < 20 : err <= 300 ulp
+ # 20 <= x : < 600 ulp
+ #
+ if arg < 1.0:
+ ulp_tol = 10
+ elif arg < 10.0:
+ ulp_tol = 100
+ else:
+ ulp_tol = 1000
+
+ failure = result_check(expected, got, ulp_tol, abs_tol)
+ if failure is None:
+ continue
- fail_msg = fail_fmt.format(id, fn, arg, expected, got)
- if accuracy_failure is not None:
- fail_msg += ' ({})'.format(accuracy_failure)
- failures.append(fail_msg)
+ msg = fail_fmt.format(id, fn, arg, failure)
+ failures.append(msg)
if failures:
self.fail('Failures in test_mtestfile:\n ' +
@@ -1272,7 +1393,8 @@ class IsCloseTests(unittest.TestCase):
decimal_examples = [(Decimal('1.00000001'), Decimal('1.0')),
(Decimal('1.00000001e-20'), Decimal('1.0e-20')),
- (Decimal('1.00000001e-100'), Decimal('1.0e-100'))]
+ (Decimal('1.00000001e-100'), Decimal('1.0e-100')),
+ (Decimal('1.00000001e20'), Decimal('1.0e20'))]
self.assertAllClose(decimal_examples, rel_tol=1e-8)
self.assertAllNotClose(decimal_examples, rel_tol=1e-9)
@@ -1280,8 +1402,10 @@ class IsCloseTests(unittest.TestCase):
# test with Fraction values
from fractions import Fraction
- # could use some more examples here!
- fraction_examples = [(Fraction(1, 100000000) + 1, Fraction(1))]
+ fraction_examples = [
+ (Fraction(1, 100000000) + 1, Fraction(1)),
+ (Fraction(100000001), Fraction(100000000)),
+ (Fraction(10**8 + 1, 10**28), Fraction(1, 10**20))]
self.assertAllClose(fraction_examples, rel_tol=1e-8)
self.assertAllNotClose(fraction_examples, rel_tol=1e-9)
diff --git a/Lib/test/test_mimetypes.py b/Lib/test/test_mimetypes.py
index 6856593..4e2c908 100644
--- a/Lib/test/test_mimetypes.py
+++ b/Lib/test/test_mimetypes.py
@@ -101,5 +101,11 @@ class Win32MimeTypesTestCase(unittest.TestCase):
eq(self.db.guess_type("image.jpg"), ("image/jpeg", None))
eq(self.db.guess_type("image.png"), ("image/png", None))
+
+class MiscTestCase(unittest.TestCase):
+ def test__all__(self):
+ support.check__all__(self, mimetypes)
+
+
if __name__ == "__main__":
unittest.main()
diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py
index f3f70cc..56d85e7 100644
--- a/Lib/test/test_mmap.py
+++ b/Lib/test/test_mmap.py
@@ -713,6 +713,13 @@ class MmapTests(unittest.TestCase):
gc_collect()
self.assertIs(wr(), None)
+ def test_write_returning_the_number_of_bytes_written(self):
+ mm = mmap.mmap(-1, 16)
+ self.assertEqual(mm.write(b""), 0)
+ self.assertEqual(mm.write(b"x"), 1)
+ self.assertEqual(mm.write(b"yz"), 2)
+ self.assertEqual(mm.write(b"python"), 6)
+
@unittest.skipIf(os.name == 'nt', 'cannot resize anonymous mmaps on Windows')
def test_resize_past_pos(self):
m = mmap.mmap(-1, 8192)
diff --git a/Lib/test/test_msilib.py b/Lib/test/test_msilib.py
index 8ef334f..f656f72 100644
--- a/Lib/test/test_msilib.py
+++ b/Lib/test/test_msilib.py
@@ -1,6 +1,5 @@
""" Test suite for the code in msilib """
import unittest
-import os
from test.support import import_module
msilib = import_module('msilib')
diff --git a/Lib/test/test_multibytecodec.py b/Lib/test/test_multibytecodec.py
index 8d7a213..01a1cd3 100644
--- a/Lib/test/test_multibytecodec.py
+++ b/Lib/test/test_multibytecodec.py
@@ -5,7 +5,7 @@
from test import support
from test.support import TESTFN
-import unittest, io, codecs, sys, os
+import unittest, io, codecs, sys
import _multibytecodec
ALL_CJKENCODINGS = [
diff --git a/Lib/test/test_multiprocessing_fork.py b/Lib/test/test_multiprocessing_fork.py
index 2bf4e75..9f22500 100644
--- a/Lib/test/test_multiprocessing_fork.py
+++ b/Lib/test/test_multiprocessing_fork.py
@@ -1,6 +1,12 @@
import unittest
import test._test_multiprocessing
+from test import support
+
+if support.PGO:
+ raise unittest.SkipTest("test is not helpful for PGO")
+
+
test._test_multiprocessing.install_tests_in_module_dict(globals(), 'fork')
if __name__ == '__main__':
diff --git a/Lib/test/test_multiprocessing_forkserver.py b/Lib/test/test_multiprocessing_forkserver.py
index 193a04a..407bb3f 100644
--- a/Lib/test/test_multiprocessing_forkserver.py
+++ b/Lib/test/test_multiprocessing_forkserver.py
@@ -1,6 +1,11 @@
import unittest
import test._test_multiprocessing
+from test import support
+
+if support.PGO:
+ raise unittest.SkipTest("test is not helpful for PGO")
+
test._test_multiprocessing.install_tests_in_module_dict(globals(), 'forkserver')
if __name__ == '__main__':
diff --git a/Lib/test/test_multiprocessing_main_handling.py b/Lib/test/test_multiprocessing_main_handling.py
index 52273ea..32593da 100644
--- a/Lib/test/test_multiprocessing_main_handling.py
+++ b/Lib/test/test_multiprocessing_main_handling.py
@@ -6,7 +6,6 @@ support.import_module('_multiprocessing')
import importlib
import importlib.machinery
-import zipimport
import unittest
import sys
import os
@@ -15,7 +14,10 @@ import py_compile
from test.support.script_helper import (
make_pkg, make_script, make_zip_pkg, make_zip_script,
- assert_python_ok, assert_python_failure, spawn_python, kill_python)
+ assert_python_ok)
+
+if support.PGO:
+ raise unittest.SkipTest("test is not helpful for PGO")
# Look up which start methods are available to test
import multiprocessing
diff --git a/Lib/test/test_multiprocessing_spawn.py b/Lib/test/test_multiprocessing_spawn.py
index 334ae9e..6558952 100644
--- a/Lib/test/test_multiprocessing_spawn.py
+++ b/Lib/test/test_multiprocessing_spawn.py
@@ -1,6 +1,11 @@
import unittest
import test._test_multiprocessing
+from test import support
+
+if support.PGO:
+ raise unittest.SkipTest("test is not helpful for PGO")
+
test._test_multiprocessing.install_tests_in_module_dict(globals(), 'spawn')
if __name__ == '__main__':
diff --git a/Lib/test/test_nis.py b/Lib/test/test_nis.py
index 387a4e7..21074c6 100644
--- a/Lib/test/test_nis.py
+++ b/Lib/test/test_nis.py
@@ -1,6 +1,5 @@
from test import support
import unittest
-import sys
# Skip test if nis module does not exist.
nis = support.import_module('nis')
diff --git a/Lib/test/test_nntplib.py b/Lib/test/test_nntplib.py
index 66ef930..c46ded1 100644
--- a/Lib/test/test_nntplib.py
+++ b/Lib/test/test_nntplib.py
@@ -1544,8 +1544,10 @@ class LocalServerTests(unittest.TestCase):
elif cmd == b'STARTTLS\r\n':
reader.close()
client.sendall(b'382 Begin TLS negotiation now\r\n')
- client = ssl.wrap_socket(
- client, server_side=True, certfile=certfile)
+ context = ssl.SSLContext()
+ context.load_cert_chain(certfile)
+ client = context.wrap_socket(
+ client, server_side=True)
cleanup.enter_context(client)
reader = cleanup.enter_context(client.makefile('rb'))
elif cmd == b'QUIT\r\n':
diff --git a/Lib/test/test_normalization.py b/Lib/test/test_normalization.py
index 30fa612..5b590e1 100644
--- a/Lib/test/test_normalization.py
+++ b/Lib/test/test_normalization.py
@@ -3,7 +3,6 @@ import unittest
from http.client import HTTPException
import sys
-import os
from unicodedata import normalize, unidata_version
TESTDATAFILE = "NormalizationTest.txt"
diff --git a/Lib/test/test_ntpath.py b/Lib/test/test_ntpath.py
index 580f203..90edb6d 100644
--- a/Lib/test/test_ntpath.py
+++ b/Lib/test/test_ntpath.py
@@ -452,5 +452,88 @@ class NtCommonTest(test_genericpath.CommonTest, unittest.TestCase):
attributes = ['relpath', 'splitunc']
+class PathLikeTests(unittest.TestCase):
+
+ path = ntpath
+
+ class PathLike:
+ def __init__(self, path=''):
+ self.path = path
+ def __fspath__(self):
+ if isinstance(self.path, BaseException):
+ raise self.path
+ else:
+ return self.path
+
+ def setUp(self):
+ self.file_name = support.TESTFN.lower()
+ self.file_path = self.PathLike(support.TESTFN)
+ self.addCleanup(support.unlink, self.file_name)
+ with open(self.file_name, 'xb', 0) as file:
+ file.write(b"test_ntpath.PathLikeTests")
+
+ def assertPathEqual(self, func):
+ self.assertEqual(func(self.file_path), func(self.file_name))
+
+ def test_path_normcase(self):
+ self.assertPathEqual(self.path.normcase)
+
+ def test_path_isabs(self):
+ self.assertPathEqual(self.path.isabs)
+
+ def test_path_join(self):
+ self.assertEqual(self.path.join('a', self.PathLike('b'), 'c'),
+ self.path.join('a', 'b', 'c'))
+
+ def test_path_split(self):
+ self.assertPathEqual(self.path.split)
+
+ def test_path_splitext(self):
+ self.assertPathEqual(self.path.splitext)
+
+ def test_path_splitdrive(self):
+ self.assertPathEqual(self.path.splitdrive)
+
+ def test_path_basename(self):
+ self.assertPathEqual(self.path.basename)
+
+ def test_path_dirname(self):
+ self.assertPathEqual(self.path.dirname)
+
+ def test_path_islink(self):
+ self.assertPathEqual(self.path.islink)
+
+ def test_path_lexists(self):
+ self.assertPathEqual(self.path.lexists)
+
+ def test_path_ismount(self):
+ self.assertPathEqual(self.path.ismount)
+
+ def test_path_expanduser(self):
+ self.assertPathEqual(self.path.expanduser)
+
+ def test_path_expandvars(self):
+ self.assertPathEqual(self.path.expandvars)
+
+ def test_path_normpath(self):
+ self.assertPathEqual(self.path.normpath)
+
+ def test_path_abspath(self):
+ self.assertPathEqual(self.path.abspath)
+
+ def test_path_realpath(self):
+ self.assertPathEqual(self.path.realpath)
+
+ def test_path_relpath(self):
+ self.assertPathEqual(self.path.relpath)
+
+ def test_path_commonpath(self):
+ common_path = self.path.commonpath([self.file_path, self.file_name])
+ self.assertEqual(common_path, self.file_name)
+
+ def test_path_isdir(self):
+ self.assertPathEqual(self.path.isdir)
+
+
if __name__ == "__main__":
unittest.main()
diff --git a/Lib/test/test_opcodes.py b/Lib/test/test_opcodes.py
index 6ef93d9..6806c61 100644
--- a/Lib/test/test_opcodes.py
+++ b/Lib/test/test_opcodes.py
@@ -1,6 +1,7 @@
# Python test set -- part 2, opcodes
import unittest
+from test import ann_module
class OpcodeTest(unittest.TestCase):
@@ -20,6 +21,32 @@ class OpcodeTest(unittest.TestCase):
if n != 90:
self.fail('try inside for')
+ def test_setup_annotations_line(self):
+ # check that SETUP_ANNOTATIONS does not create spurious line numbers
+ try:
+ with open(ann_module.__file__) as f:
+ txt = f.read()
+ co = compile(txt, ann_module.__file__, 'exec')
+ self.assertEqual(co.co_firstlineno, 6)
+ except OSError:
+ pass
+
+ def test_no_annotations_if_not_needed(self):
+ class C: pass
+ with self.assertRaises(AttributeError):
+ C.__annotations__
+
+ def test_use_existing_annotations(self):
+ ns = {'__annotations__': {1: 2}}
+ exec('x: int', ns)
+ self.assertEqual(ns['__annotations__'], {'x': int, 1: 2})
+
+ def test_do_not_recreate_annotations(self):
+ class C:
+ del __annotations__
+ with self.assertRaises(NameError):
+ x: int
+
def test_raise_class_exceptions(self):
class AClass(Exception): pass
diff --git a/Lib/test/test_operator.py b/Lib/test/test_operator.py
index b5ba976..6254091 100644
--- a/Lib/test/test_operator.py
+++ b/Lib/test/test_operator.py
@@ -120,63 +120,63 @@ class OperatorTestCase:
operator = self.module
self.assertRaises(TypeError, operator.add)
self.assertRaises(TypeError, operator.add, None, None)
- self.assertTrue(operator.add(3, 4) == 7)
+ self.assertEqual(operator.add(3, 4), 7)
def test_bitwise_and(self):
operator = self.module
self.assertRaises(TypeError, operator.and_)
self.assertRaises(TypeError, operator.and_, None, None)
- self.assertTrue(operator.and_(0xf, 0xa) == 0xa)
+ self.assertEqual(operator.and_(0xf, 0xa), 0xa)
def test_concat(self):
operator = self.module
self.assertRaises(TypeError, operator.concat)
self.assertRaises(TypeError, operator.concat, None, None)
- self.assertTrue(operator.concat('py', 'thon') == 'python')
- self.assertTrue(operator.concat([1, 2], [3, 4]) == [1, 2, 3, 4])
- self.assertTrue(operator.concat(Seq1([5, 6]), Seq1([7])) == [5, 6, 7])
- self.assertTrue(operator.concat(Seq2([5, 6]), Seq2([7])) == [5, 6, 7])
+ self.assertEqual(operator.concat('py', 'thon'), 'python')
+ self.assertEqual(operator.concat([1, 2], [3, 4]), [1, 2, 3, 4])
+ self.assertEqual(operator.concat(Seq1([5, 6]), Seq1([7])), [5, 6, 7])
+ self.assertEqual(operator.concat(Seq2([5, 6]), Seq2([7])), [5, 6, 7])
self.assertRaises(TypeError, operator.concat, 13, 29)
def test_countOf(self):
operator = self.module
self.assertRaises(TypeError, operator.countOf)
self.assertRaises(TypeError, operator.countOf, None, None)
- self.assertTrue(operator.countOf([1, 2, 1, 3, 1, 4], 3) == 1)
- self.assertTrue(operator.countOf([1, 2, 1, 3, 1, 4], 5) == 0)
+ self.assertEqual(operator.countOf([1, 2, 1, 3, 1, 4], 3), 1)
+ self.assertEqual(operator.countOf([1, 2, 1, 3, 1, 4], 5), 0)
def test_delitem(self):
operator = self.module
a = [4, 3, 2, 1]
self.assertRaises(TypeError, operator.delitem, a)
self.assertRaises(TypeError, operator.delitem, a, None)
- self.assertTrue(operator.delitem(a, 1) is None)
- self.assertTrue(a == [4, 2, 1])
+ self.assertIsNone(operator.delitem(a, 1))
+ self.assertEqual(a, [4, 2, 1])
def test_floordiv(self):
operator = self.module
self.assertRaises(TypeError, operator.floordiv, 5)
self.assertRaises(TypeError, operator.floordiv, None, None)
- self.assertTrue(operator.floordiv(5, 2) == 2)
+ self.assertEqual(operator.floordiv(5, 2), 2)
def test_truediv(self):
operator = self.module
self.assertRaises(TypeError, operator.truediv, 5)
self.assertRaises(TypeError, operator.truediv, None, None)
- self.assertTrue(operator.truediv(5, 2) == 2.5)
+ self.assertEqual(operator.truediv(5, 2), 2.5)
def test_getitem(self):
operator = self.module
a = range(10)
self.assertRaises(TypeError, operator.getitem)
self.assertRaises(TypeError, operator.getitem, a, None)
- self.assertTrue(operator.getitem(a, 2) == 2)
+ self.assertEqual(operator.getitem(a, 2), 2)
def test_indexOf(self):
operator = self.module
self.assertRaises(TypeError, operator.indexOf)
self.assertRaises(TypeError, operator.indexOf, None, None)
- self.assertTrue(operator.indexOf([4, 3, 2, 1], 3) == 1)
+ self.assertEqual(operator.indexOf([4, 3, 2, 1], 3), 1)
self.assertRaises(ValueError, operator.indexOf, [4, 3, 2, 1], 0)
def test_invert(self):
@@ -189,21 +189,21 @@ class OperatorTestCase:
operator = self.module
self.assertRaises(TypeError, operator.lshift)
self.assertRaises(TypeError, operator.lshift, None, 42)
- self.assertTrue(operator.lshift(5, 1) == 10)
- self.assertTrue(operator.lshift(5, 0) == 5)
+ self.assertEqual(operator.lshift(5, 1), 10)
+ self.assertEqual(operator.lshift(5, 0), 5)
self.assertRaises(ValueError, operator.lshift, 2, -1)
def test_mod(self):
operator = self.module
self.assertRaises(TypeError, operator.mod)
self.assertRaises(TypeError, operator.mod, None, 42)
- self.assertTrue(operator.mod(5, 2) == 1)
+ self.assertEqual(operator.mod(5, 2), 1)
def test_mul(self):
operator = self.module
self.assertRaises(TypeError, operator.mul)
self.assertRaises(TypeError, operator.mul, None, None)
- self.assertTrue(operator.mul(5, 2) == 10)
+ self.assertEqual(operator.mul(5, 2), 10)
def test_matmul(self):
operator = self.module
@@ -227,7 +227,7 @@ class OperatorTestCase:
operator = self.module
self.assertRaises(TypeError, operator.or_)
self.assertRaises(TypeError, operator.or_, None, None)
- self.assertTrue(operator.or_(0xa, 0x5) == 0xf)
+ self.assertEqual(operator.or_(0xa, 0x5), 0xf)
def test_pos(self):
operator = self.module
@@ -250,8 +250,8 @@ class OperatorTestCase:
operator = self.module
self.assertRaises(TypeError, operator.rshift)
self.assertRaises(TypeError, operator.rshift, None, 42)
- self.assertTrue(operator.rshift(5, 1) == 2)
- self.assertTrue(operator.rshift(5, 0) == 5)
+ self.assertEqual(operator.rshift(5, 1), 2)
+ self.assertEqual(operator.rshift(5, 0), 5)
self.assertRaises(ValueError, operator.rshift, 2, -1)
def test_contains(self):
@@ -266,15 +266,15 @@ class OperatorTestCase:
a = list(range(3))
self.assertRaises(TypeError, operator.setitem, a)
self.assertRaises(TypeError, operator.setitem, a, None, None)
- self.assertTrue(operator.setitem(a, 0, 2) is None)
- self.assertTrue(a == [2, 1, 2])
+ self.assertIsNone(operator.setitem(a, 0, 2))
+ self.assertEqual(a, [2, 1, 2])
self.assertRaises(IndexError, operator.setitem, a, 4, 2)
def test_sub(self):
operator = self.module
self.assertRaises(TypeError, operator.sub)
self.assertRaises(TypeError, operator.sub, None, None)
- self.assertTrue(operator.sub(5, 2) == 3)
+ self.assertEqual(operator.sub(5, 2), 3)
def test_truth(self):
operator = self.module
@@ -292,7 +292,7 @@ class OperatorTestCase:
operator = self.module
self.assertRaises(TypeError, operator.xor)
self.assertRaises(TypeError, operator.xor, None, None)
- self.assertTrue(operator.xor(0xb, 0xc) == 0x7)
+ self.assertEqual(operator.xor(0xb, 0xc), 0x7)
def test_is(self):
operator = self.module
diff --git a/Lib/test/test_optparse.py b/Lib/test/test_optparse.py
index 7621c24..91a0319 100644
--- a/Lib/test/test_optparse.py
+++ b/Lib/test/test_optparse.py
@@ -16,6 +16,7 @@ from io import StringIO
from test import support
+import optparse
from optparse import make_option, Option, \
TitledHelpFormatter, OptionParser, OptionGroup, \
SUPPRESS_USAGE, OptionError, OptionConflictError, \
@@ -1650,6 +1651,12 @@ class TestParseNumber(BaseTest):
"option -l: invalid integer value: '0x12x'")
+class MiscTestCase(unittest.TestCase):
+ def test__all__(self):
+ blacklist = {'check_builtin', 'AmbiguousOptionError', 'NO_DEFAULT'}
+ support.check__all__(self, optparse, blacklist=blacklist)
+
+
def test_main():
support.run_unittest(__name__)
diff --git a/Lib/test/test_ordered_dict.py b/Lib/test/test_ordered_dict.py
index 901d4b2..93f812a 100644
--- a/Lib/test/test_ordered_dict.py
+++ b/Lib/test/test_ordered_dict.py
@@ -1,3 +1,4 @@
+import builtins
import contextlib
import copy
import gc
@@ -51,6 +52,14 @@ class OrderedDictTests:
self.assertEqual(list(d.items()),
[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5), ('f', 6), ('g', 7)])
+ def test_468(self):
+ OrderedDict = self.OrderedDict
+ items = [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5), ('f', 6), ('g', 7)]
+ shuffle(items)
+ argdict = OrderedDict(items)
+ d = OrderedDict(**argdict)
+ self.assertEqual(list(d.items()), items)
+
def test_update(self):
OrderedDict = self.OrderedDict
with self.assertRaises(TypeError):
@@ -97,6 +106,19 @@ class OrderedDictTests:
self.assertRaises(TypeError, OrderedDict().update, (), ())
self.assertRaises(TypeError, OrderedDict.update)
+ def test_init_calls(self):
+ calls = []
+ class Spam:
+ def keys(self):
+ calls.append('keys')
+ return ()
+ def items(self):
+ calls.append('items')
+ return ()
+
+ self.OrderedDict(Spam())
+ self.assertEqual(calls, ['keys'])
+
def test_fromkeys(self):
OrderedDict = self.OrderedDict
od = OrderedDict.fromkeys('abc')
@@ -298,9 +320,11 @@ class OrderedDictTests:
# do not save instance dictionary if not needed
pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
od = OrderedDict(pairs)
+ self.assertIsInstance(od.__dict__, dict)
self.assertIsNone(od.__reduce__()[2])
od.x = 10
- self.assertIsNotNone(od.__reduce__()[2])
+ self.assertEqual(od.__dict__['x'], 10)
+ self.assertEqual(od.__reduce__()[2], {'x': 10})
def test_pickle_recursive(self):
OrderedDict = self.OrderedDict
@@ -403,6 +427,14 @@ class OrderedDictTests:
od = OrderedDict(**d)
self.assertGreater(sys.getsizeof(od), sys.getsizeof(d))
+ def test_views(self):
+ OrderedDict = self.OrderedDict
+ # See http://bugs.python.org/issue24286
+ s = 'the quick brown fox jumped over a lazy dog yesterday before dawn'.split()
+ od = OrderedDict.fromkeys(s)
+ self.assertEqual(od.keys(), dict(od).keys())
+ self.assertEqual(od.items(), dict(od).items())
+
def test_override_update(self):
OrderedDict = self.OrderedDict
# Verify that subclasses can override update() without breaking __init__()
@@ -611,6 +643,25 @@ class PurePythonOrderedDictTests(OrderedDictTests, unittest.TestCase):
OrderedDict = py_coll.OrderedDict
+class CPythonBuiltinDictTests(unittest.TestCase):
+ """Builtin dict preserves insertion order.
+
+ Reuse some of tests in OrderedDict selectively.
+ """
+
+ module = builtins
+ OrderedDict = dict
+
+for method in (
+ "test_init test_update test_abc test_clear test_delitem " +
+ "test_setitem test_detect_deletion_during_iteration " +
+ "test_popitem test_reinsert test_override_update " +
+ "test_highly_nested test_highly_nested_subclass " +
+ "test_delitem_hash_collision ").split():
+ setattr(CPythonBuiltinDictTests, method, getattr(OrderedDictTests, method))
+del method
+
+
@unittest.skipUnless(c_coll, 'requires the C version of the collections module')
class CPythonOrderedDictTests(OrderedDictTests, unittest.TestCase):
@@ -625,18 +676,20 @@ class CPythonOrderedDictTests(OrderedDictTests, unittest.TestCase):
size = support.calcobjsize
check = self.check_sizeof
- basicsize = size('n2P' + '3PnPn2P') + calcsize('2nPn')
- entrysize = calcsize('n2P') + calcsize('P')
+ basicsize = size('nQ2P' + '3PnPn2P') + calcsize('2nP2n')
+
+ entrysize = calcsize('n2P')
+ p = calcsize('P')
nodesize = calcsize('Pn2P')
od = OrderedDict()
- check(od, basicsize + 8*entrysize)
+ check(od, basicsize + 8*p + 8 + 5*entrysize) # 8byte indicies + 8*2//3 * entry table
od.x = 1
- check(od, basicsize + 8*entrysize)
+ check(od, basicsize + 8*p + 8 + 5*entrysize)
od.update([(i, i) for i in range(3)])
- check(od, basicsize + 8*entrysize + 3*nodesize)
+ check(od, basicsize + 8*p + 8 + 5*entrysize + 3*nodesize)
od.update([(i, i) for i in range(3, 10)])
- check(od, basicsize + 16*entrysize + 10*nodesize)
+ check(od, basicsize + 16*p + 16 + 10*entrysize + 10*nodesize)
check(od.keys(), size('P'))
check(od.items(), size('P'))
diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py
index 10383cf..e9fdb07 100644
--- a/Lib/test/test_os.py
+++ b/Lib/test/test_os.py
@@ -15,7 +15,6 @@ import locale
import mmap
import os
import pickle
-import platform
import re
import shutil
import signal
@@ -57,7 +56,7 @@ except ImportError:
try:
import pwd
all_users = [u.pw_uid for u in pwd.getpwall()]
-except ImportError:
+except (ImportError, AttributeError):
all_users = []
try:
from _testcapi import INT_MAX, PY_SSIZE_T_MAX
@@ -65,6 +64,8 @@ except ImportError:
INT_MAX = PY_SSIZE_T_MAX = sys.maxsize
from test.support.script_helper import assert_python_ok
+from test.support import unix_shell
+
root_in_posix = False
if hasattr(os, 'geteuid'):
@@ -82,6 +83,32 @@ else:
# Issue #14110: Some tests fail on FreeBSD if the user is in the wheel group.
HAVE_WHEEL_GROUP = sys.platform.startswith('freebsd') and os.getgid() == 0
+
+@contextlib.contextmanager
+def ignore_deprecation_warnings(msg_regex, quiet=False):
+ with support.check_warnings((msg_regex, DeprecationWarning), quiet=quiet):
+ yield
+
+
+def requires_os_func(name):
+ return unittest.skipUnless(hasattr(os, name), 'requires os.%s' % name)
+
+
+class _PathLike(os.PathLike):
+
+ def __init__(self, path=""):
+ self.path = path
+
+ def __str__(self):
+ return str(self.path)
+
+ def __fspath__(self):
+ if isinstance(self.path, BaseException):
+ raise self.path
+ else:
+ return self.path
+
+
def create_file(filename, content=b'content'):
with open(filename, "xb", 0) as fp:
fp.write(content)
@@ -145,9 +172,8 @@ class FileTests(unittest.TestCase):
"needs INT_MAX < PY_SSIZE_T_MAX")
@support.bigmemtest(size=INT_MAX + 10, memuse=1, dry_run=False)
def test_large_read(self, size):
- with open(support.TESTFN, "wb") as fp:
- fp.write(b'test')
self.addCleanup(support.unlink, support.TESTFN)
+ create_file(support.TESTFN, b'test')
# Issue #21932: Make sure that os.read() does not raise an
# OverflowError for size larger than INT_MAX
@@ -204,11 +230,12 @@ class FileTests(unittest.TestCase):
def test_replace(self):
TESTFN2 = support.TESTFN + ".2"
- with open(support.TESTFN, 'w') as f:
- f.write("1")
- with open(TESTFN2, 'w') as f:
- f.write("2")
- self.addCleanup(os.unlink, TESTFN2)
+ self.addCleanup(support.unlink, support.TESTFN)
+ self.addCleanup(support.unlink, TESTFN2)
+
+ create_file(support.TESTFN, b"1")
+ create_file(TESTFN2, b"2")
+
os.replace(support.TESTFN, TESTFN2)
self.assertRaises(FileNotFoundError, os.stat, support.TESTFN)
with open(TESTFN2, 'r') as f:
@@ -309,9 +336,7 @@ class StatAttributeTests(unittest.TestCase):
fname = self.fname.encode(sys.getfilesystemencoding())
except UnicodeEncodeError:
self.skipTest("cannot encode %a for the filesystem" % self.fname)
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", DeprecationWarning)
- self.check_stat_attributes(fname)
+ self.check_stat_attributes(fname)
def test_stat_result_pickle(self):
result = os.stat(self.fname)
@@ -462,19 +487,14 @@ class UtimeTests(unittest.TestCase):
self.addCleanup(support.rmtree, self.dirname)
os.mkdir(self.dirname)
- with open(self.fname, 'wb') as fp:
- fp.write(b"ABC")
+ create_file(self.fname)
def restore_float_times(state):
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", DeprecationWarning)
-
+ with ignore_deprecation_warnings('stat_float_times'):
os.stat_float_times(state)
# ensure that st_atime and st_mtime are float
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", DeprecationWarning)
-
+ with ignore_deprecation_warnings('stat_float_times'):
old_float_times = os.stat_float_times(-1)
self.addCleanup(restore_float_times, old_float_times)
@@ -566,7 +586,7 @@ class UtimeTests(unittest.TestCase):
"fd support for utime required for this test.")
def test_utime_fd(self):
def set_time(filename, ns):
- with open(filename, 'wb') as fp:
+ with open(filename, 'wb', 0) as fp:
# use a file descriptor to test futimens(timespec)
# or futimes(timeval)
os.utime(fp.fileno(), ns=ns)
@@ -678,18 +698,20 @@ class EnvironTests(mapping_tests.BasicTestMappingProtocol):
return os.environ
# Bug 1110478
- @unittest.skipUnless(os.path.exists('/bin/sh'), 'requires /bin/sh')
+ @unittest.skipUnless(unix_shell and os.path.exists(unix_shell),
+ 'requires a shell')
def test_update2(self):
os.environ.clear()
os.environ.update(HELLO="World")
- with os.popen("/bin/sh -c 'echo $HELLO'") as popen:
+ with os.popen("%s -c 'echo $HELLO'" % unix_shell) as popen:
value = popen.read().strip()
self.assertEqual(value, "World")
- @unittest.skipUnless(os.path.exists('/bin/sh'), 'requires /bin/sh')
+ @unittest.skipUnless(unix_shell and os.path.exists(unix_shell),
+ 'requires a shell')
def test_os_popen_iter(self):
- with os.popen(
- "/bin/sh -c 'echo \"line1\nline2\nline3\"'") as popen:
+ with os.popen("%s -c 'echo \"line1\nline2\nline3\"'"
+ % unix_shell) as popen:
it = iter(popen)
self.assertEqual(next(it), "line1\n")
self.assertEqual(next(it), "line2\n")
@@ -820,6 +842,7 @@ class WalkTests(unittest.TestCase):
def setUp(self):
join = os.path.join
+ self.addCleanup(support.rmtree, support.TESTFN)
# Build:
# TESTFN/
@@ -842,11 +865,11 @@ class WalkTests(unittest.TestCase):
self.sub1_path = join(self.walk_path, "SUB1")
self.sub11_path = join(self.sub1_path, "SUB11")
sub2_path = join(self.walk_path, "SUB2")
- self.sub21_path = join(sub2_path, "SUB21")
+ sub21_path = join(sub2_path, "SUB21")
tmp1_path = join(self.walk_path, "tmp1")
tmp2_path = join(self.sub1_path, "tmp2")
tmp3_path = join(sub2_path, "tmp3")
- tmp5_path = join(self.sub21_path, "tmp3")
+ tmp5_path = join(sub21_path, "tmp3")
self.link_path = join(sub2_path, "link")
t2_path = join(support.TESTFN, "TEST2")
tmp4_path = join(support.TESTFN, "TEST2", "tmp4")
@@ -857,7 +880,7 @@ class WalkTests(unittest.TestCase):
# Create stuff.
os.makedirs(self.sub11_path)
os.makedirs(sub2_path)
- os.makedirs(self.sub21_path)
+ os.makedirs(sub21_path)
os.makedirs(t2_path)
for path in tmp1_path, tmp2_path, tmp3_path, tmp4_path, tmp5_path:
@@ -875,16 +898,15 @@ class WalkTests(unittest.TestCase):
else:
self.sub2_tree = (sub2_path, [], ["tmp3"])
- os.chmod(self.sub21_path, 0)
+ os.chmod(sub21_path, 0)
try:
- os.listdir(self.sub21_path)
+ os.listdir(sub21_path)
except PermissionError:
- pass
+ self.addCleanup(os.chmod, sub21_path, stat.S_IRWXU)
else:
- os.chmod(self.sub21_path, stat.S_IRWXU)
+ os.chmod(sub21_path, stat.S_IRWXU)
os.unlink(tmp5_path)
- os.rmdir(self.sub21_path)
- self.sub21_path = None
+ os.rmdir(sub21_path)
del self.sub2_tree[1][:1]
def test_walk_topdown(self):
@@ -904,10 +926,12 @@ class WalkTests(unittest.TestCase):
self.assertEqual(all[2 + flipped], (self.sub11_path, [], []))
self.assertEqual(all[3 - 2 * flipped], self.sub2_tree)
- def test_walk_prune(self):
+ def test_walk_prune(self, walk_path=None):
+ if walk_path is None:
+ walk_path = self.walk_path
# Prune the search.
all = []
- for root, dirs, files in self.walk(self.walk_path):
+ for root, dirs, files in self.walk(walk_path):
all.append((root, dirs, files))
# Don't descend into SUB1.
if 'SUB1' in dirs:
@@ -916,17 +940,20 @@ class WalkTests(unittest.TestCase):
self.assertEqual(len(all), 2)
self.assertEqual(all[0],
- (self.walk_path, ["SUB2"], ["tmp1"]))
+ (str(walk_path), ["SUB2"], ["tmp1"]))
all[1][-1].sort()
all[1][1].sort()
self.assertEqual(all[1], self.sub2_tree)
+ def test_file_like_path(self):
+ self.test_walk_prune(_PathLike(self.walk_path))
+
def test_walk_bottom_up(self):
# Walk bottom-up.
all = list(self.walk(self.walk_path, topdown=False))
- self.assertEqual(len(all), 4)
+ self.assertEqual(len(all), 4, all)
# We can't know which order SUB1 and SUB2 will appear in.
# Not flipped: SUB11, SUB1, SUB2, TESTFN
# flipped: SUB2, SUB11, SUB1, TESTFN
@@ -957,24 +984,6 @@ class WalkTests(unittest.TestCase):
else:
self.fail("Didn't follow symlink with followlinks=True")
- def tearDown(self):
- # Tear everything down. This is a decent use for bottom-up on
- # Windows, which doesn't have a recursive delete command. The
- # (not so) subtlety is that rmdir will fail unless the dir's
- # kids are removed first, so bottom up is essential.
- if self.sub21_path:
- os.chmod(self.sub21_path, stat.S_IRWXU)
- for root, dirs, files in os.walk(support.TESTFN, topdown=False):
- for name in files:
- os.remove(os.path.join(root, name))
- for name in dirs:
- dirname = os.path.join(root, name)
- if not os.path.islink(dirname):
- os.rmdir(dirname)
- else:
- os.remove(dirname)
- os.rmdir(support.TESTFN)
-
def test_walk_bad_dir(self):
# Walk top-down.
errors = []
@@ -1062,29 +1071,11 @@ class FwalkTests(WalkTests):
self.addCleanup(os.close, newfd)
self.assertEqual(newfd, minfd)
- def tearDown(self):
- # cleanup
- if self.sub21_path:
- os.chmod(self.sub21_path, stat.S_IRWXU)
- for root, dirs, files, rootfd in os.fwalk(support.TESTFN, topdown=False):
- for name in files:
- os.unlink(name, dir_fd=rootfd)
- for name in dirs:
- st = os.stat(name, dir_fd=rootfd, follow_symlinks=False)
- if stat.S_ISDIR(st.st_mode):
- os.rmdir(name, dir_fd=rootfd)
- else:
- os.unlink(name, dir_fd=rootfd)
- os.rmdir(support.TESTFN)
-
class BytesWalkTests(WalkTests):
"""Tests for os.walk() with bytes."""
def setUp(self):
super().setUp()
self.stack = contextlib.ExitStack()
- if os.name == 'nt':
- self.stack.enter_context(warnings.catch_warnings())
- warnings.simplefilter("ignore", DeprecationWarning)
def tearDown(self):
self.stack.close()
@@ -1261,8 +1252,7 @@ class RemoveDirsTests(unittest.TestCase):
os.mkdir(dira)
dirb = os.path.join(dira, 'dirb')
os.mkdir(dirb)
- with open(os.path.join(dira, 'file.txt'), 'w') as f:
- f.write('text')
+ create_file(os.path.join(dira, 'file.txt'))
os.removedirs(dirb)
self.assertFalse(os.path.exists(dirb))
self.assertTrue(os.path.exists(dira))
@@ -1273,8 +1263,7 @@ class RemoveDirsTests(unittest.TestCase):
os.mkdir(dira)
dirb = os.path.join(dira, 'dirb')
os.mkdir(dirb)
- with open(os.path.join(dirb, 'file.txt'), 'w') as f:
- f.write('text')
+ create_file(os.path.join(dirb, 'file.txt'))
with self.assertRaises(OSError):
os.removedirs(dirb)
self.assertTrue(os.path.exists(dirb))
@@ -1284,7 +1273,7 @@ class RemoveDirsTests(unittest.TestCase):
class DevNullTests(unittest.TestCase):
def test_devnull(self):
- with open(os.devnull, 'wb') as f:
+ with open(os.devnull, 'wb', 0) as f:
f.write(b'hello')
f.close()
with open(os.devnull, 'rb') as f:
@@ -1301,6 +1290,7 @@ class URandomTests(unittest.TestCase):
def test_urandom_value(self):
data1 = os.urandom(16)
+ self.assertIsInstance(data1, bytes)
data2 = os.urandom(16)
self.assertNotEqual(data1, data2)
@@ -1321,6 +1311,49 @@ class URandomTests(unittest.TestCase):
self.assertNotEqual(data1, data2)
+@unittest.skipUnless(hasattr(os, 'getrandom'), 'need os.getrandom()')
+class GetRandomTests(unittest.TestCase):
+ @classmethod
+ def setUpClass(cls):
+ try:
+ os.getrandom(1)
+ except OSError as exc:
+ if exc.errno == errno.ENOSYS:
+ # Python compiled on a more recent Linux version
+ # than the current Linux kernel
+ raise unittest.SkipTest("getrandom() syscall fails with ENOSYS")
+ else:
+ raise
+
+ def test_getrandom_type(self):
+ data = os.getrandom(16)
+ self.assertIsInstance(data, bytes)
+ self.assertEqual(len(data), 16)
+
+ def test_getrandom0(self):
+ empty = os.getrandom(0)
+ self.assertEqual(empty, b'')
+
+ def test_getrandom_random(self):
+ self.assertTrue(hasattr(os, 'GRND_RANDOM'))
+
+ # Don't test os.getrandom(1, os.GRND_RANDOM) to not consume the rare
+ # resource /dev/random
+
+ def test_getrandom_nonblock(self):
+ # The call must not fail. Check also that the flag exists
+ try:
+ os.getrandom(1, os.GRND_NONBLOCK)
+ except BlockingIOError:
+ # System urandom is not initialized yet
+ pass
+
+ def test_getrandom_value(self):
+ data1 = os.getrandom(16)
+ data2 = os.getrandom(16)
+ self.assertNotEqual(data1, data2)
+
+
# os.urandom() doesn't use a file descriptor when it is implemented with the
# getentropy() function, the getrandom() function or the getrandom() syscall
OS_URANDOM_DONT_USE_FD = (
@@ -1371,9 +1404,9 @@ class URandomFDTests(unittest.TestCase):
def test_urandom_fd_reopened(self):
# Issue #21207: urandom() should detect its fd to /dev/urandom
# changed to something else, and reopen it.
- with open(support.TESTFN, 'wb') as f:
- f.write(b"x" * 256)
- self.addCleanup(os.unlink, support.TESTFN)
+ self.addCleanup(support.unlink, support.TESTFN)
+ create_file(support.TESTFN, b"x" * 256)
+
code = """if 1:
import os
import sys
@@ -1390,7 +1423,12 @@ class URandomFDTests(unittest.TestCase):
break
os.closerange(3, 256)
with open({TESTFN!r}, 'rb') as f:
- os.dup2(f.fileno(), fd)
+ new_fd = f.fileno()
+ # Issue #26935: posix allows new_fd and fd to be equal but
+ # some libc implementations have dup2 return an error in this
+ # case.
+ if new_fd != fd:
+ os.dup2(new_fd, fd)
sys.stdout.buffer.write(os.urandom(4))
sys.stdout.buffer.write(os.urandom(4))
""".format(TESTFN=support.TESTFN)
@@ -1435,6 +1473,7 @@ def _execvpe_mockup(defpath=None):
os.execve = orig_execve
os.defpath = orig_defpath
+
class ExecTests(unittest.TestCase):
@unittest.skipIf(USING_LINUXTHREADS,
"avoid triggering a linuxthreads bug: see issue #4970")
@@ -1442,8 +1481,16 @@ class ExecTests(unittest.TestCase):
self.assertRaises(OSError, os.execvpe, 'no such app-',
['no such app-'], None)
+ def test_execv_with_bad_arglist(self):
+ self.assertRaises(ValueError, os.execv, 'notepad', ())
+ self.assertRaises(ValueError, os.execv, 'notepad', [])
+ self.assertRaises(ValueError, os.execv, 'notepad', ('',))
+ self.assertRaises(ValueError, os.execv, 'notepad', [''])
+
def test_execvpe_with_bad_arglist(self):
self.assertRaises(ValueError, os.execvpe, 'notepad', [], None)
+ self.assertRaises(ValueError, os.execvpe, 'notepad', [], {})
+ self.assertRaises(ValueError, os.execvpe, 'notepad', [''], {})
@unittest.skipUnless(hasattr(os, '_execvpe'),
"No internal os._execvpe function to test.")
@@ -1502,6 +1549,18 @@ class ExecTests(unittest.TestCase):
@unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
class Win32ErrorTests(unittest.TestCase):
+ def setUp(self):
+ try:
+ os.stat(support.TESTFN)
+ except FileNotFoundError:
+ exists = False
+ except OSError as exc:
+ exists = True
+ self.fail("file %s must not exist; os.stat failed with %s"
+ % (support.TESTFN, exc))
+ else:
+ self.fail("file %s must not exist" % support.TESTFN)
+
def test_rename(self):
self.assertRaises(OSError, os.rename, support.TESTFN, support.TESTFN+".bak")
@@ -1512,12 +1571,10 @@ class Win32ErrorTests(unittest.TestCase):
self.assertRaises(OSError, os.chdir, support.TESTFN)
def test_mkdir(self):
- f = open(support.TESTFN, "w")
- try:
+ self.addCleanup(support.unlink, support.TESTFN)
+
+ with open(support.TESTFN, "x") as f:
self.assertRaises(OSError, os.mkdir, support.TESTFN)
- finally:
- f.close()
- os.unlink(support.TESTFN)
def test_utime(self):
self.assertRaises(OSError, os.utime, support.TESTFN, None)
@@ -1525,6 +1582,7 @@ class Win32ErrorTests(unittest.TestCase):
def test_chmod(self):
self.assertRaises(OSError, os.chmod, support.TESTFN, 0)
+
class TestInvalidFD(unittest.TestCase):
singles = ["fchdir", "dup", "fdopen", "fdatasync", "fstat",
"fstatvfs", "fsync", "tcgetpgrp", "ttyname"]
@@ -1636,12 +1694,9 @@ class LinkTests(unittest.TestCase):
os.unlink(file)
def _test_link(self, file1, file2):
- with open(file1, "w") as f1:
- f1.write("test")
+ create_file(file1)
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", DeprecationWarning)
- os.link(file1, file2)
+ os.link(file1, file2)
with open(file1, "r") as f1, open(file2, "r") as f2:
self.assertTrue(os.path.sameopenfile(f1.fileno(), f2.fileno()))
@@ -1932,6 +1987,7 @@ class Win32ListdirTests(unittest.TestCase):
self.assertEqual(
sorted(os.listdir(support.TESTFN)),
self.created_paths)
+
# bytes
self.assertEqual(
sorted(os.listdir(os.fsencode(support.TESTFN))),
@@ -1945,6 +2001,7 @@ class Win32ListdirTests(unittest.TestCase):
self.assertEqual(
sorted(os.listdir(path)),
self.created_paths)
+
# bytes
path = b'\\\\?\\' + os.fsencode(os.path.abspath(support.TESTFN))
self.assertEqual(
@@ -2024,51 +2081,43 @@ class Win32SymlinkTests(unittest.TestCase):
self.assertNotEqual(os.lstat(link), os.stat(link))
bytes_link = os.fsencode(link)
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", DeprecationWarning)
- self.assertEqual(os.stat(bytes_link), os.stat(target))
- self.assertNotEqual(os.lstat(bytes_link), os.stat(bytes_link))
+ self.assertEqual(os.stat(bytes_link), os.stat(target))
+ self.assertNotEqual(os.lstat(bytes_link), os.stat(bytes_link))
def test_12084(self):
level1 = os.path.abspath(support.TESTFN)
level2 = os.path.join(level1, "level2")
level3 = os.path.join(level2, "level3")
- try:
- os.mkdir(level1)
- os.mkdir(level2)
- os.mkdir(level3)
+ self.addCleanup(support.rmtree, level1)
- file1 = os.path.abspath(os.path.join(level1, "file1"))
+ os.mkdir(level1)
+ os.mkdir(level2)
+ os.mkdir(level3)
- with open(file1, "w") as f:
- f.write("file1")
+ file1 = os.path.abspath(os.path.join(level1, "file1"))
+ create_file(file1)
- orig_dir = os.getcwd()
- try:
- os.chdir(level2)
- link = os.path.join(level2, "link")
- os.symlink(os.path.relpath(file1), "link")
- self.assertIn("link", os.listdir(os.getcwd()))
-
- # Check os.stat calls from the same dir as the link
- self.assertEqual(os.stat(file1), os.stat("link"))
-
- # Check os.stat calls from a dir below the link
- os.chdir(level1)
- self.assertEqual(os.stat(file1),
- os.stat(os.path.relpath(link)))
-
- # Check os.stat calls from a dir above the link
- os.chdir(level3)
- self.assertEqual(os.stat(file1),
- os.stat(os.path.relpath(link)))
- finally:
- os.chdir(orig_dir)
- except OSError as err:
- self.fail(err)
+ orig_dir = os.getcwd()
+ try:
+ os.chdir(level2)
+ link = os.path.join(level2, "link")
+ os.symlink(os.path.relpath(file1), "link")
+ self.assertIn("link", os.listdir(os.getcwd()))
+
+ # Check os.stat calls from the same dir as the link
+ self.assertEqual(os.stat(file1), os.stat("link"))
+
+ # Check os.stat calls from a dir below the link
+ os.chdir(level1)
+ self.assertEqual(os.stat(file1),
+ os.stat(os.path.relpath(link)))
+
+ # Check os.stat calls from a dir above the link
+ os.chdir(level3)
+ self.assertEqual(os.stat(file1),
+ os.stat(os.path.relpath(link)))
finally:
- os.remove(file1)
- shutil.rmtree(level1)
+ os.chdir(orig_dir)
@unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
@@ -2106,7 +2155,7 @@ class Win32JunctionTests(unittest.TestCase):
class NonLocalSymlinkTests(unittest.TestCase):
def setUp(self):
- """
+ r"""
Create this structure:
base
@@ -2177,11 +2226,137 @@ class PidTests(unittest.TestCase):
def test_waitpid(self):
args = [sys.executable, '-c', 'pass']
- pid = os.spawnv(os.P_NOWAIT, args[0], args)
+ # Add an implicit test for PyUnicode_FSConverter().
+ pid = os.spawnv(os.P_NOWAIT, _PathLike(args[0]), args)
status = os.waitpid(pid, 0)
self.assertEqual(status, (pid, 0))
+class SpawnTests(unittest.TestCase):
+ def create_args(self, *, with_env=False, use_bytes=False):
+ self.exitcode = 17
+
+ filename = support.TESTFN
+ self.addCleanup(support.unlink, filename)
+
+ if not with_env:
+ code = 'import sys; sys.exit(%s)' % self.exitcode
+ else:
+ self.env = dict(os.environ)
+ # create an unique key
+ self.key = str(uuid.uuid4())
+ self.env[self.key] = self.key
+ # read the variable from os.environ to check that it exists
+ code = ('import sys, os; magic = os.environ[%r]; sys.exit(%s)'
+ % (self.key, self.exitcode))
+
+ with open(filename, "w") as fp:
+ fp.write(code)
+
+ args = [sys.executable, filename]
+ if use_bytes:
+ args = [os.fsencode(a) for a in args]
+ self.env = {os.fsencode(k): os.fsencode(v)
+ for k, v in self.env.items()}
+
+ return args
+
+ @requires_os_func('spawnl')
+ def test_spawnl(self):
+ args = self.create_args()
+ exitcode = os.spawnl(os.P_WAIT, args[0], *args)
+ self.assertEqual(exitcode, self.exitcode)
+
+ @requires_os_func('spawnle')
+ def test_spawnle(self):
+ args = self.create_args(with_env=True)
+ exitcode = os.spawnle(os.P_WAIT, args[0], *args, self.env)
+ self.assertEqual(exitcode, self.exitcode)
+
+ @requires_os_func('spawnlp')
+ def test_spawnlp(self):
+ args = self.create_args()
+ exitcode = os.spawnlp(os.P_WAIT, args[0], *args)
+ self.assertEqual(exitcode, self.exitcode)
+
+ @requires_os_func('spawnlpe')
+ def test_spawnlpe(self):
+ args = self.create_args(with_env=True)
+ exitcode = os.spawnlpe(os.P_WAIT, args[0], *args, self.env)
+ self.assertEqual(exitcode, self.exitcode)
+
+ @requires_os_func('spawnv')
+ def test_spawnv(self):
+ args = self.create_args()
+ exitcode = os.spawnv(os.P_WAIT, args[0], args)
+ self.assertEqual(exitcode, self.exitcode)
+
+ @requires_os_func('spawnve')
+ def test_spawnve(self):
+ args = self.create_args(with_env=True)
+ exitcode = os.spawnve(os.P_WAIT, args[0], args, self.env)
+ self.assertEqual(exitcode, self.exitcode)
+
+ @requires_os_func('spawnvp')
+ def test_spawnvp(self):
+ args = self.create_args()
+ exitcode = os.spawnvp(os.P_WAIT, args[0], args)
+ self.assertEqual(exitcode, self.exitcode)
+
+ @requires_os_func('spawnvpe')
+ def test_spawnvpe(self):
+ args = self.create_args(with_env=True)
+ exitcode = os.spawnvpe(os.P_WAIT, args[0], args, self.env)
+ self.assertEqual(exitcode, self.exitcode)
+
+ @requires_os_func('spawnv')
+ def test_nowait(self):
+ args = self.create_args()
+ pid = os.spawnv(os.P_NOWAIT, args[0], args)
+ result = os.waitpid(pid, 0)
+ self.assertEqual(result[0], pid)
+ status = result[1]
+ if hasattr(os, 'WIFEXITED'):
+ self.assertTrue(os.WIFEXITED(status))
+ self.assertEqual(os.WEXITSTATUS(status), self.exitcode)
+ else:
+ self.assertEqual(status, self.exitcode << 8)
+
+ @requires_os_func('spawnve')
+ def test_spawnve_bytes(self):
+ # Test bytes handling in parse_arglist and parse_envlist (#28114)
+ args = self.create_args(with_env=True, use_bytes=True)
+ exitcode = os.spawnve(os.P_WAIT, args[0], args, self.env)
+ self.assertEqual(exitcode, self.exitcode)
+
+ @requires_os_func('spawnl')
+ def test_spawnl_noargs(self):
+ args = self.create_args()
+ self.assertRaises(ValueError, os.spawnl, os.P_NOWAIT, args[0])
+ self.assertRaises(ValueError, os.spawnl, os.P_NOWAIT, args[0], '')
+
+ @requires_os_func('spawnle')
+ def test_spawnle_noargs(self):
+ args = self.create_args()
+ self.assertRaises(ValueError, os.spawnle, os.P_NOWAIT, args[0], {})
+ self.assertRaises(ValueError, os.spawnle, os.P_NOWAIT, args[0], '', {})
+
+ @requires_os_func('spawnv')
+ def test_spawnv_noargs(self):
+ args = self.create_args()
+ self.assertRaises(ValueError, os.spawnv, os.P_NOWAIT, args[0], ())
+ self.assertRaises(ValueError, os.spawnv, os.P_NOWAIT, args[0], [])
+ self.assertRaises(ValueError, os.spawnv, os.P_NOWAIT, args[0], ('',))
+ self.assertRaises(ValueError, os.spawnv, os.P_NOWAIT, args[0], [''])
+
+ @requires_os_func('spawnve')
+ def test_spawnve_noargs(self):
+ args = self.create_args()
+ self.assertRaises(ValueError, os.spawnve, os.P_NOWAIT, args[0], (), {})
+ self.assertRaises(ValueError, os.spawnve, os.P_NOWAIT, args[0], [], {})
+ self.assertRaises(ValueError, os.spawnve, os.P_NOWAIT, args[0], ('',), {})
+ self.assertRaises(ValueError, os.spawnve, os.P_NOWAIT, args[0], [''], {})
+
# The introduction of this TestCase caused at least two different errors on
# *nix buildbots. Temporarily skip this to let the buildbots move along.
@unittest.skip("Skip due to platform/environment differences on *NIX buildbots")
@@ -2204,8 +2379,8 @@ class ProgramPriorityTests(unittest.TestCase):
try:
new_prio = os.getpriority(os.PRIO_PROCESS, os.getpid())
if base >= 19 and new_prio <= 19:
- raise unittest.SkipTest(
- "unable to reliably test setpriority at current nice level of %s" % base)
+ raise unittest.SkipTest("unable to reliably test setpriority "
+ "at current nice level of %s" % base)
else:
self.assertEqual(new_prio, base + 1)
finally:
@@ -2315,8 +2490,7 @@ class TestSendfile(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.key = support.threading_setup()
- with open(support.TESTFN, "wb") as f:
- f.write(cls.DATA)
+ create_file(support.TESTFN, cls.DATA)
@classmethod
def tearDownClass(cls):
@@ -2466,10 +2640,11 @@ class TestSendfile(unittest.TestCase):
def test_trailers(self):
TESTFN2 = support.TESTFN + "2"
file_data = b"abcdef"
- with open(TESTFN2, 'wb') as f:
- f.write(file_data)
- with open(TESTFN2, 'rb')as f:
- self.addCleanup(os.remove, TESTFN2)
+
+ self.addCleanup(support.unlink, TESTFN2)
+ create_file(TESTFN2, file_data)
+
+ with open(TESTFN2, 'rb') as f:
os.sendfile(self.sockno, f.fileno(), 0, len(file_data),
trailers=[b"1234"])
self.client.close()
@@ -2492,35 +2667,37 @@ class TestSendfile(unittest.TestCase):
def supports_extended_attributes():
if not hasattr(os, "setxattr"):
return False
+
try:
- with open(support.TESTFN, "wb") as fp:
+ with open(support.TESTFN, "xb", 0) as fp:
try:
os.setxattr(fp.fileno(), b"user.test", b"")
except OSError:
return False
finally:
support.unlink(support.TESTFN)
- # Kernels < 2.6.39 don't respect setxattr flags.
- kernel_version = platform.release()
- m = re.match("2.6.(\d{1,2})", kernel_version)
- return m is None or int(m.group(1)) >= 39
+
+ return True
@unittest.skipUnless(supports_extended_attributes(),
"no non-broken extended attribute support")
+# Kernels < 2.6.39 don't respect setxattr flags.
+@support.requires_linux_version(2, 6, 39)
class ExtendedAttributeTests(unittest.TestCase):
- def tearDown(self):
- support.unlink(support.TESTFN)
-
def _check_xattrs_str(self, s, getxattr, setxattr, removexattr, listxattr, **kwargs):
fn = support.TESTFN
- open(fn, "wb").close()
+ self.addCleanup(support.unlink, fn)
+ create_file(fn)
+
with self.assertRaises(OSError) as cm:
getxattr(fn, s("user.test"), **kwargs)
self.assertEqual(cm.exception.errno, errno.ENODATA)
+
init_xattr = listxattr(fn)
self.assertIsInstance(init_xattr, list)
+
setxattr(fn, s("user.test"), b"", **kwargs)
xattr = set(init_xattr)
xattr.add("user.test")
@@ -2528,19 +2705,24 @@ class ExtendedAttributeTests(unittest.TestCase):
self.assertEqual(getxattr(fn, b"user.test", **kwargs), b"")
setxattr(fn, s("user.test"), b"hello", os.XATTR_REPLACE, **kwargs)
self.assertEqual(getxattr(fn, b"user.test", **kwargs), b"hello")
+
with self.assertRaises(OSError) as cm:
setxattr(fn, s("user.test"), b"bye", os.XATTR_CREATE, **kwargs)
self.assertEqual(cm.exception.errno, errno.EEXIST)
+
with self.assertRaises(OSError) as cm:
setxattr(fn, s("user.test2"), b"bye", os.XATTR_REPLACE, **kwargs)
self.assertEqual(cm.exception.errno, errno.ENODATA)
+
setxattr(fn, s("user.test2"), b"foo", os.XATTR_CREATE, **kwargs)
xattr.add("user.test2")
self.assertEqual(set(listxattr(fn)), xattr)
removexattr(fn, s("user.test"), **kwargs)
+
with self.assertRaises(OSError) as cm:
getxattr(fn, s("user.test"), **kwargs)
self.assertEqual(cm.exception.errno, errno.ENODATA)
+
xattr.remove("user.test")
self.assertEqual(set(listxattr(fn)), xattr)
self.assertEqual(getxattr(fn, s("user.test2"), **kwargs), b"foo")
@@ -2553,11 +2735,11 @@ class ExtendedAttributeTests(unittest.TestCase):
self.assertEqual(set(listxattr(fn)), set(init_xattr) | set(many))
def _check_xattrs(self, *args, **kwargs):
- def make_bytes(s):
- return bytes(s, "ascii")
self._check_xattrs_str(str, *args, **kwargs)
support.unlink(support.TESTFN)
- self._check_xattrs_str(make_bytes, *args, **kwargs)
+
+ self._check_xattrs_str(os.fsencode, *args, **kwargs)
+ support.unlink(support.TESTFN)
def test_simple(self):
self._check_xattrs(os.getxattr, os.setxattr, os.removexattr,
@@ -2572,10 +2754,10 @@ class ExtendedAttributeTests(unittest.TestCase):
with open(path, "rb") as fp:
return os.getxattr(fp.fileno(), *args)
def setxattr(path, *args):
- with open(path, "wb") as fp:
+ with open(path, "wb", 0) as fp:
os.setxattr(fp.fileno(), *args)
def removexattr(path, *args):
- with open(path, "wb") as fp:
+ with open(path, "wb", 0) as fp:
os.removexattr(fp.fileno(), *args)
def listxattr(path, *args):
with open(path, "rb") as fp:
@@ -2583,43 +2765,6 @@ class ExtendedAttributeTests(unittest.TestCase):
self._check_xattrs(getxattr, setxattr, removexattr, listxattr)
-@unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
-class Win32DeprecatedBytesAPI(unittest.TestCase):
- def test_deprecated(self):
- import nt
- filename = os.fsencode(support.TESTFN)
- with warnings.catch_warnings():
- warnings.simplefilter("error", DeprecationWarning)
- for func, *args in (
- (nt._getfullpathname, filename),
- (nt._isdir, filename),
- (os.access, filename, os.R_OK),
- (os.chdir, filename),
- (os.chmod, filename, 0o777),
- (os.getcwdb,),
- (os.link, filename, filename),
- (os.listdir, filename),
- (os.lstat, filename),
- (os.mkdir, filename),
- (os.open, filename, os.O_RDONLY),
- (os.rename, filename, filename),
- (os.rmdir, filename),
- (os.startfile, filename),
- (os.stat, filename),
- (os.unlink, filename),
- (os.utime, filename),
- ):
- self.assertRaises(DeprecationWarning, func, *args)
-
- @support.skip_unless_symlink
- def test_symlink(self):
- filename = os.fsencode(support.TESTFN)
- with warnings.catch_warnings():
- warnings.simplefilter("error", DeprecationWarning)
- self.assertRaises(DeprecationWarning,
- os.symlink, filename, filename)
-
-
@unittest.skipUnless(hasattr(os, 'get_terminal_size'), "requires os.get_terminal_size")
class TermsizeTests(unittest.TestCase):
def test_does_not_crash(self):
@@ -2682,6 +2827,7 @@ class OSErrorTests(unittest.TestCase):
else:
encoded = os.fsencode(support.TESTFN)
self.bytes_filenames.append(encoded)
+ self.bytes_filenames.append(bytearray(encoded))
self.bytes_filenames.append(memoryview(encoded))
self.filenames = self.bytes_filenames + self.unicode_filenames
@@ -2702,16 +2848,7 @@ class OSErrorTests(unittest.TestCase):
(self.bytes_filenames, os.replace, b"dst"),
(self.unicode_filenames, os.rename, "dst"),
(self.unicode_filenames, os.replace, "dst"),
- # Issue #16414: Don't test undecodable names with listdir()
- # because of a Windows bug.
- #
- # With the ANSI code page 932, os.listdir(b'\xe7') return an
- # empty list (instead of failing), whereas os.listdir(b'\xff')
- # raises a FileNotFoundError. It looks like a Windows bug:
- # b'\xe7' directory does not exist, FindFirstFileA(b'\xe7')
- # fails with ERROR_FILE_NOT_FOUND (2), instead of
- # ERROR_PATH_NOT_FOUND (3).
- (self.unicode_filenames, os.listdir,),
+ (self.unicode_filenames, os.listdir, ),
))
else:
funcs.extend((
@@ -2752,12 +2889,19 @@ class OSErrorTests(unittest.TestCase):
else:
funcs.append((self.filenames, os.readlink,))
+
for filenames, func, *func_args in funcs:
for name in filenames:
try:
- func(name, *func_args)
+ if isinstance(name, (str, bytes)):
+ func(name, *func_args)
+ else:
+ with self.assertWarnsRegex(DeprecationWarning, 'should be'):
+ func(name, *func_args)
except OSError as err:
- self.assertIs(err.filename, name)
+ self.assertIs(err.filename, name, str(func))
+ except UnicodeDecodeError:
+ pass
else:
self.fail("No exception thrown by {}".format(func))
@@ -2855,6 +2999,63 @@ class FDInheritanceTests(unittest.TestCase):
self.assertEqual(os.get_inheritable(slave_fd), False)
+class PathTConverterTests(unittest.TestCase):
+ # tuples of (function name, allows fd arguments, additional arguments to
+ # function, cleanup function)
+ functions = [
+ ('stat', True, (), None),
+ ('lstat', False, (), None),
+ ('access', False, (os.F_OK,), None),
+ ('chflags', False, (0,), None),
+ ('lchflags', False, (0,), None),
+ ('open', False, (0,), getattr(os, 'close', None)),
+ ]
+
+ def test_path_t_converter(self):
+ str_filename = support.TESTFN
+ if os.name == 'nt':
+ bytes_fspath = bytes_filename = None
+ else:
+ bytes_filename = support.TESTFN.encode('ascii')
+ bytes_fspath = _PathLike(bytes_filename)
+ fd = os.open(_PathLike(str_filename), os.O_WRONLY|os.O_CREAT)
+ self.addCleanup(support.unlink, support.TESTFN)
+ self.addCleanup(os.close, fd)
+
+ int_fspath = _PathLike(fd)
+ str_fspath = _PathLike(str_filename)
+
+ for name, allow_fd, extra_args, cleanup_fn in self.functions:
+ with self.subTest(name=name):
+ try:
+ fn = getattr(os, name)
+ except AttributeError:
+ continue
+
+ for path in (str_filename, bytes_filename, str_fspath,
+ bytes_fspath):
+ if path is None:
+ continue
+ with self.subTest(name=name, path=path):
+ result = fn(path, *extra_args)
+ if cleanup_fn is not None:
+ cleanup_fn(result)
+
+ with self.assertRaisesRegex(
+ TypeError, 'should be string, bytes'):
+ fn(int_fspath, *extra_args)
+
+ if allow_fd:
+ result = fn(fd, *extra_args) # should not fail
+ if cleanup_fn is not None:
+ cleanup_fn(result)
+ else:
+ with self.assertRaisesRegex(
+ TypeError,
+ 'os.PathLike'):
+ fn(fd, *extra_args)
+
+
@unittest.skipUnless(hasattr(os, 'get_blocking'),
'needs os.get_blocking() and os.set_blocking()')
class BlockingTests(unittest.TestCase):
@@ -2878,15 +3079,18 @@ class ExportsTests(unittest.TestCase):
class TestScandir(unittest.TestCase):
+ check_no_resource_warning = support.check_no_resource_warning
+
def setUp(self):
self.path = os.path.realpath(support.TESTFN)
+ self.bytes_path = os.fsencode(self.path)
self.addCleanup(support.rmtree, self.path)
os.mkdir(self.path)
def create_file(self, name="file.txt"):
- filename = os.path.join(self.path, name)
- with open(filename, "wb") as fp:
- fp.write(b'python')
+ path = self.bytes_path if isinstance(name, bytes) else self.path
+ filename = os.path.join(path, name)
+ create_file(filename, b'python')
return filename
def get_entries(self, names):
@@ -2909,6 +3113,7 @@ class TestScandir(unittest.TestCase):
self.assertEqual(stat1, stat2)
def check_entry(self, entry, name, is_dir, is_file, is_symlink):
+ self.assertIsInstance(entry, os.DirEntry)
self.assertEqual(entry.name, name)
self.assertEqual(entry.path, os.path.join(self.path, name))
self.assertEqual(entry.inode(),
@@ -2974,15 +3179,16 @@ class TestScandir(unittest.TestCase):
self.check_entry(entry, 'symlink_file.txt', False, True, True)
def get_entry(self, name):
- entries = list(os.scandir(self.path))
+ path = self.bytes_path if isinstance(name, bytes) else self.path
+ entries = list(os.scandir(path))
self.assertEqual(len(entries), 1)
entry = entries[0]
self.assertEqual(entry.name, name)
return entry
- def create_file_entry(self):
- filename = self.create_file()
+ def create_file_entry(self, name='file.txt'):
+ filename = self.create_file(name=name)
return self.get_entry(os.path.basename(filename))
def test_current_directory(self):
@@ -3003,6 +3209,18 @@ class TestScandir(unittest.TestCase):
entry = self.create_file_entry()
self.assertEqual(repr(entry), "<DirEntry 'file.txt'>")
+ def test_fspath_protocol(self):
+ entry = self.create_file_entry()
+ self.assertEqual(os.fspath(entry), os.path.join(self.path, 'file.txt'))
+
+ def test_fspath_protocol_bytes(self):
+ bytes_filename = os.fsencode('bytesfile.txt')
+ bytes_entry = self.create_file_entry(name=bytes_filename)
+ fspath = os.fspath(bytes_entry)
+ self.assertIsInstance(fspath, bytes)
+ self.assertEqual(fspath,
+ os.path.join(os.fsencode(self.path),bytes_filename))
+
def test_removed_dir(self):
path = os.path.join(self.path, 'dir')
@@ -3066,11 +3284,6 @@ class TestScandir(unittest.TestCase):
entry.stat(follow_symlinks=False)
def test_bytes(self):
- if os.name == "nt":
- # On Windows, os.scandir(bytes) must raise an exception
- self.assertRaises(TypeError, os.scandir, b'.')
- return
-
self.create_file("file.txt")
path_bytes = os.fsencode(self.path)
@@ -3100,6 +3313,112 @@ class TestScandir(unittest.TestCase):
for obj in [1234, 1.234, {}, []]:
self.assertRaises(TypeError, os.scandir, obj)
+ def test_close(self):
+ self.create_file("file.txt")
+ self.create_file("file2.txt")
+ iterator = os.scandir(self.path)
+ next(iterator)
+ iterator.close()
+ # multiple closes
+ iterator.close()
+ with self.check_no_resource_warning():
+ del iterator
+
+ def test_context_manager(self):
+ self.create_file("file.txt")
+ self.create_file("file2.txt")
+ with os.scandir(self.path) as iterator:
+ next(iterator)
+ with self.check_no_resource_warning():
+ del iterator
+
+ def test_context_manager_close(self):
+ self.create_file("file.txt")
+ self.create_file("file2.txt")
+ with os.scandir(self.path) as iterator:
+ next(iterator)
+ iterator.close()
+
+ def test_context_manager_exception(self):
+ self.create_file("file.txt")
+ self.create_file("file2.txt")
+ with self.assertRaises(ZeroDivisionError):
+ with os.scandir(self.path) as iterator:
+ next(iterator)
+ 1/0
+ with self.check_no_resource_warning():
+ del iterator
+
+ def test_resource_warning(self):
+ self.create_file("file.txt")
+ self.create_file("file2.txt")
+ iterator = os.scandir(self.path)
+ next(iterator)
+ with self.assertWarns(ResourceWarning):
+ del iterator
+ support.gc_collect()
+ # exhausted iterator
+ iterator = os.scandir(self.path)
+ list(iterator)
+ with self.check_no_resource_warning():
+ del iterator
+
+
+class TestPEP519(unittest.TestCase):
+
+ # Abstracted so it can be overridden to test pure Python implementation
+ # if a C version is provided.
+ fspath = staticmethod(os.fspath)
+
+ def test_return_bytes(self):
+ for b in b'hello', b'goodbye', b'some/path/and/file':
+ self.assertEqual(b, self.fspath(b))
+
+ def test_return_string(self):
+ for s in 'hello', 'goodbye', 'some/path/and/file':
+ self.assertEqual(s, self.fspath(s))
+
+ def test_fsencode_fsdecode(self):
+ for p in "path/like/object", b"path/like/object":
+ pathlike = _PathLike(p)
+
+ self.assertEqual(p, self.fspath(pathlike))
+ self.assertEqual(b"path/like/object", os.fsencode(pathlike))
+ self.assertEqual("path/like/object", os.fsdecode(pathlike))
+
+ def test_pathlike(self):
+ self.assertEqual('#feelthegil', self.fspath(_PathLike('#feelthegil')))
+ self.assertTrue(issubclass(_PathLike, os.PathLike))
+ self.assertTrue(isinstance(_PathLike(), os.PathLike))
+
+ def test_garbage_in_exception_out(self):
+ vapor = type('blah', (), {})
+ for o in int, type, os, vapor():
+ self.assertRaises(TypeError, self.fspath, o)
+
+ def test_argument_required(self):
+ self.assertRaises(TypeError, self.fspath)
+
+ def test_bad_pathlike(self):
+ # __fspath__ returns a value other than str or bytes.
+ self.assertRaises(TypeError, self.fspath, _PathLike(42))
+ # __fspath__ attribute that is not callable.
+ c = type('foo', (), {})
+ c.__fspath__ = 1
+ self.assertRaises(TypeError, self.fspath, c())
+ # __fspath__ raises an exception.
+ self.assertRaises(ZeroDivisionError, self.fspath,
+ _PathLike(ZeroDivisionError()))
+
+# Only test if the C version is provided, otherwise TestPEP519 already tested
+# the pure Python implementation.
+if hasattr(os, "_fspath"):
+ class TestPEP519PurePython(TestPEP519):
+
+ """Explicitly test the pure Python implementation of os.fspath()."""
+
+ fspath = staticmethod(os._fspath)
+
if __name__ == "__main__":
unittest.main()
diff --git a/Lib/test/test_parser.py b/Lib/test/test_parser.py
index ab6577f..d6e6f71 100644
--- a/Lib/test/test_parser.py
+++ b/Lib/test/test_parser.py
@@ -1,6 +1,5 @@
import parser
import unittest
-import sys
import operator
import struct
from test import support
@@ -139,6 +138,45 @@ class RoundtripLegalSyntaxTestCase(unittest.TestCase):
self.check_suite("a = b")
self.check_suite("a = b = c = d = e")
+ def test_var_annot(self):
+ self.check_suite("x: int = 5")
+ self.check_suite("y: List[T] = []; z: [list] = fun()")
+ self.check_suite("x: tuple = (1, 2)")
+ self.check_suite("d[f()]: int = 42")
+ self.check_suite("f(d[x]): str = 'abc'")
+ self.check_suite("x.y.z.w: complex = 42j")
+ self.check_suite("x: int")
+ self.check_suite("def f():\n"
+ " x: str\n"
+ " y: int = 5\n")
+ self.check_suite("class C:\n"
+ " x: str\n"
+ " y: int = 5\n")
+ self.check_suite("class C:\n"
+ " def __init__(self, x: int) -> None:\n"
+ " self.x: int = x\n")
+ # double check for nonsense
+ with self.assertRaises(SyntaxError):
+ exec("2+2: int", {}, {})
+ with self.assertRaises(SyntaxError):
+ exec("[]: int = 5", {}, {})
+ with self.assertRaises(SyntaxError):
+ exec("x, *y, z: int = range(5)", {}, {})
+ with self.assertRaises(SyntaxError):
+ exec("t: tuple = 1, 2", {}, {})
+ with self.assertRaises(SyntaxError):
+ exec("u = v: int", {}, {})
+ with self.assertRaises(SyntaxError):
+ exec("False: int", {}, {})
+ with self.assertRaises(SyntaxError):
+ exec("x.False: int", {}, {})
+ with self.assertRaises(SyntaxError):
+ exec("x.y,: int", {}, {})
+ with self.assertRaises(SyntaxError):
+ exec("[0]: int", {}, {})
+ with self.assertRaises(SyntaxError):
+ exec("f(): int", {}, {})
+
def test_simple_augmented_assignments(self):
self.check_suite("a += b")
self.check_suite("a -= b")
@@ -633,12 +671,18 @@ class CompileTestCase(unittest.TestCase):
self.assertEqual(code.co_filename, '<syntax-tree>')
code = st.compile()
self.assertEqual(code.co_filename, '<syntax-tree>')
- for filename in ('file.py', b'file.py',
- bytearray(b'file.py'), memoryview(b'file.py')):
+ for filename in 'file.py', b'file.py':
code = parser.compilest(st, filename)
self.assertEqual(code.co_filename, 'file.py')
code = st.compile(filename)
self.assertEqual(code.co_filename, 'file.py')
+ for filename in bytearray(b'file.py'), memoryview(b'file.py'):
+ with self.assertWarns(DeprecationWarning):
+ code = parser.compilest(st, filename)
+ self.assertEqual(code.co_filename, 'file.py')
+ with self.assertWarns(DeprecationWarning):
+ code = st.compile(filename)
+ self.assertEqual(code.co_filename, 'file.py')
self.assertRaises(TypeError, parser.compilest, st, list(b'file.py'))
self.assertRaises(TypeError, st.compile, list(b'file.py'))
diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py
index fa96d9f..ce1ca15 100644
--- a/Lib/test/test_pathlib.py
+++ b/Lib/test/test_pathlib.py
@@ -10,6 +10,7 @@ import tempfile
import unittest
from test import support
+android_not_root = support.android_not_root
TESTFN = support.TESTFN
try:
@@ -190,13 +191,18 @@ class _BasePurePathTest(object):
P = self.cls
p = P('a')
self.assertIsInstance(p, P)
+ class PathLike:
+ def __fspath__(self):
+ return "a/b/c"
P('a', 'b', 'c')
P('/a', 'b', 'c')
P('a/b/c')
P('/a/b/c')
+ P(PathLike())
self.assertEqual(P(P('a')), P('a'))
self.assertEqual(P(P('a'), 'b'), P('a/b'))
self.assertEqual(P(P('a'), P('b')), P('a/b'))
+ self.assertEqual(P(P('a'), P('b'), P('c')), P(PathLike()))
def _check_str_subclass(self, *args):
# Issue #21127: it should be possible to construct a PurePath object
@@ -384,6 +390,12 @@ class _BasePurePathTest(object):
parts = p.parts
self.assertEqual(parts, (sep, 'a', 'b'))
+ def test_fspath_common(self):
+ P = self.cls
+ p = P('a/b')
+ self._check_str(p.__fspath__(), ('a/b',))
+ self._check_str(os.fspath(p), ('a/b',))
+
def test_equivalences(self):
for k, tuples in self.equivalences.items():
canon = k.replace('/', self.sep)
@@ -1474,21 +1486,30 @@ class _BasePathTest(object):
self.assertEqual(set(p.glob("dirA/../file*")), { P(BASE, "dirA/../fileA") })
self.assertEqual(set(p.glob("../xyzzy")), set())
- def _check_resolve_relative(self, p, expected):
- q = p.resolve()
- self.assertEqual(q, expected)
- def _check_resolve_absolute(self, p, expected):
- q = p.resolve()
+ def _check_resolve(self, p, expected, strict=True):
+ q = p.resolve(strict)
self.assertEqual(q, expected)
+ # this can be used to check both relative and absolute resolutions
+ _check_resolve_relative = _check_resolve_absolute = _check_resolve
+
@with_symlinks
def test_resolve_common(self):
P = self.cls
p = P(BASE, 'foo')
with self.assertRaises(OSError) as cm:
- p.resolve()
+ p.resolve(strict=True)
self.assertEqual(cm.exception.errno, errno.ENOENT)
+ # Non-strict
+ self.assertEqual(str(p.resolve(strict=False)),
+ os.path.join(BASE, 'foo'))
+ p = P(BASE, 'foo', 'in', 'spam')
+ self.assertEqual(str(p.resolve(strict=False)),
+ os.path.join(BASE, 'foo'))
+ p = P(BASE, '..', 'foo', 'in', 'spam')
+ self.assertEqual(str(p.resolve(strict=False)),
+ os.path.abspath(os.path.join('foo')))
# These are all relative symlinks
p = P(BASE, 'dirB', 'fileB')
self._check_resolve_relative(p, p)
@@ -1498,6 +1519,18 @@ class _BasePathTest(object):
self._check_resolve_relative(p, P(BASE, 'dirB', 'fileB'))
p = P(BASE, 'dirB', 'linkD', 'fileB')
self._check_resolve_relative(p, P(BASE, 'dirB', 'fileB'))
+ # Non-strict
+ p = P(BASE, 'dirA', 'linkC', 'fileB', 'foo', 'in', 'spam')
+ self._check_resolve_relative(p, P(BASE, 'dirB', 'fileB', 'foo'), False)
+ p = P(BASE, 'dirA', 'linkC', '..', 'foo', 'in', 'spam')
+ if os.name == 'nt':
+ # In Windows, if linkY points to dirB, 'dirA\linkY\..'
+ # resolves to 'dirA' without resolving linkY first.
+ self._check_resolve_relative(p, P(BASE, 'dirA', 'foo'), False)
+ else:
+ # In Posix, if linkY points to dirB, 'dirA/linkY/..'
+ # resolves to 'dirB/..' first before resolving to parent of dirB.
+ self._check_resolve_relative(p, P(BASE, 'foo'), False)
# Now create absolute symlinks
d = tempfile.mkdtemp(suffix='-dirD')
self.addCleanup(support.rmtree, d)
@@ -1505,6 +1538,18 @@ class _BasePathTest(object):
os.symlink(join('dirB'), os.path.join(d, 'linkY'))
p = P(BASE, 'dirA', 'linkX', 'linkY', 'fileB')
self._check_resolve_absolute(p, P(BASE, 'dirB', 'fileB'))
+ # Non-strict
+ p = P(BASE, 'dirA', 'linkX', 'linkY', 'foo', 'in', 'spam')
+ self._check_resolve_relative(p, P(BASE, 'dirB', 'foo'), False)
+ p = P(BASE, 'dirA', 'linkX', 'linkY', '..', 'foo', 'in', 'spam')
+ if os.name == 'nt':
+ # In Windows, if linkY points to dirB, 'dirA\linkY\..'
+ # resolves to 'dirA' without resolving linkY first.
+ self._check_resolve_relative(p, P(d, 'foo'), False)
+ else:
+ # In Posix, if linkY points to dirB, 'dirA/linkY/..'
+ # resolves to 'dirB/..' first before resolving to parent of dirB.
+ self._check_resolve_relative(p, P(BASE, 'foo'), False)
@with_symlinks
def test_resolve_dot(self):
@@ -1514,7 +1559,11 @@ class _BasePathTest(object):
self.dirlink(os.path.join('0', '0'), join('1'))
self.dirlink(os.path.join('1', '1'), join('2'))
q = p / '2'
- self.assertEqual(q.resolve(), p)
+ self.assertEqual(q.resolve(strict=True), p)
+ r = q / '3' / '4'
+ self.assertRaises(FileNotFoundError, r.resolve, strict=True)
+ # Non-strict
+ self.assertEqual(r.resolve(strict=False), p / '3')
def test_with(self):
p = self.cls(BASE)
@@ -1816,6 +1865,7 @@ class _BasePathTest(object):
self.assertFalse((P / 'fileA' / 'bah').is_fifo())
@unittest.skipUnless(hasattr(os, "mkfifo"), "os.mkfifo() required")
+ @unittest.skipIf(android_not_root, "mkfifo not allowed, non root user")
def test_is_fifo_true(self):
P = self.cls(BASE, 'myfifo')
os.mkfifo(str(P))
@@ -1838,7 +1888,8 @@ class _BasePathTest(object):
try:
sock.bind(str(P))
except OSError as e:
- if "AF_UNIX path too long" in str(e):
+ if (isinstance(e, PermissionError) or
+ "AF_UNIX path too long" in str(e)):
self.skipTest("cannot bind Unix socket: " + str(e))
self.assertTrue(P.is_socket())
self.assertFalse(P.is_fifo())
@@ -1961,10 +2012,10 @@ class PathTest(_BasePathTest, unittest.TestCase):
class PosixPathTest(_BasePathTest, unittest.TestCase):
cls = pathlib.PosixPath
- def _check_symlink_loop(self, *args):
+ def _check_symlink_loop(self, *args, strict=True):
path = self.cls(*args)
with self.assertRaises(RuntimeError):
- print(path.resolve())
+ print(path.resolve(strict))
def test_open_mode(self):
old_mask = os.umask(0)
@@ -1997,7 +2048,6 @@ class PosixPathTest(_BasePathTest, unittest.TestCase):
@with_symlinks
def test_resolve_loop(self):
- # Loop detection for broken symlinks under POSIX
# Loops with relative symlinks
os.symlink('linkX/inside', join('linkX'))
self._check_symlink_loop(BASE, 'linkX')
@@ -2005,6 +2055,8 @@ class PosixPathTest(_BasePathTest, unittest.TestCase):
self._check_symlink_loop(BASE, 'linkY')
os.symlink('linkZ/../linkZ', join('linkZ'))
self._check_symlink_loop(BASE, 'linkZ')
+ # Non-strict
+ self._check_symlink_loop(BASE, 'linkZ', 'foo', strict=False)
# Loops with absolute symlinks
os.symlink(join('linkU/inside'), join('linkU'))
self._check_symlink_loop(BASE, 'linkU')
@@ -2012,6 +2064,8 @@ class PosixPathTest(_BasePathTest, unittest.TestCase):
self._check_symlink_loop(BASE, 'linkV')
os.symlink(join('linkW/../linkW'), join('linkW'))
self._check_symlink_loop(BASE, 'linkW')
+ # Non-strict
+ self._check_symlink_loop(BASE, 'linkW', 'foo', strict=False)
def test_glob(self):
P = self.cls
@@ -2029,6 +2083,8 @@ class PosixPathTest(_BasePathTest, unittest.TestCase):
self.assertEqual(given, expect)
self.assertEqual(set(p.rglob("FILEd*")), set())
+ @unittest.skipUnless(hasattr(pwd, 'getpwall'),
+ 'pwd module does not expose getpwall()')
def test_expanduser(self):
P = self.cls
support.import_module('pwd')
diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py
index 66a7b55..0ea2af5 100644
--- a/Lib/test/test_pdb.py
+++ b/Lib/test/test_pdb.py
@@ -1,6 +1,7 @@
# A test suite for pdb; not very comprehensive at the moment.
import doctest
+import os
import pdb
import sys
import types
@@ -34,7 +35,7 @@ def test_pdb_displayhook():
"""This tests the custom displayhook for pdb.
>>> def test_function(foo, bar):
- ... import pdb; pdb.Pdb(nosigint=True).set_trace()
+ ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
... pass
>>> with PdbTestInput([
@@ -74,7 +75,7 @@ def test_pdb_basic_commands():
... return foo.upper()
>>> def test_function():
- ... import pdb; pdb.Pdb(nosigint=True).set_trace()
+ ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
... ret = test_function_2('baz')
... print(ret)
@@ -173,7 +174,7 @@ def test_pdb_breakpoint_commands():
"""Test basic commands related to breakpoints.
>>> def test_function():
- ... import pdb; pdb.Pdb(nosigint=True).set_trace()
+ ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
... print(1)
... print(2)
... print(3)
@@ -305,7 +306,7 @@ def test_list_commands():
... return foo
>>> def test_function():
- ... import pdb; pdb.Pdb(nosigint=True).set_trace()
+ ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
... ret = test_function_2('baz')
>>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
@@ -328,7 +329,7 @@ def test_list_commands():
-> ret = test_function_2('baz')
(Pdb) list
1 def test_function():
- 2 import pdb; pdb.Pdb(nosigint=True).set_trace()
+ 2 import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
3 -> ret = test_function_2('baz')
[EOF]
(Pdb) step
@@ -391,7 +392,7 @@ def test_post_mortem():
... print('Exception!')
>>> def test_function():
- ... import pdb; pdb.Pdb(nosigint=True).set_trace()
+ ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
... test_function_2()
... print('Not reached.')
@@ -424,7 +425,7 @@ def test_post_mortem():
-> 1/0
(Pdb) list
1 def test_function():
- 2 import pdb; pdb.Pdb(nosigint=True).set_trace()
+ 2 import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
3 -> test_function_2()
4 print('Not reached.')
[EOF]
@@ -448,7 +449,7 @@ def test_pdb_skip_modules():
>>> def skip_module():
... import string
- ... import pdb; pdb.Pdb(skip=['stri*'], nosigint=True).set_trace()
+ ... import pdb; pdb.Pdb(skip=['stri*'], nosigint=True, readrc=False).set_trace()
... string.capwords('FOO')
>>> with PdbTestInput([
@@ -477,7 +478,7 @@ def test_pdb_skip_modules_with_callback():
>>> def skip_module():
... def callback():
... return None
- ... import pdb; pdb.Pdb(skip=['module_to_skip*'], nosigint=True).set_trace()
+ ... import pdb; pdb.Pdb(skip=['module_to_skip*'], nosigint=True, readrc=False).set_trace()
... mod.foo_pony(callback)
>>> with PdbTestInput([
@@ -518,7 +519,7 @@ def test_pdb_continue_in_bottomframe():
"""Test that "continue" and "next" work properly in bottom frame (issue #5294).
>>> def test_function():
- ... import pdb, sys; inst = pdb.Pdb(nosigint=True)
+ ... import pdb, sys; inst = pdb.Pdb(nosigint=True, readrc=False)
... inst.set_trace()
... inst.botframe = sys._getframe() # hackery to get the right botframe
... print(1)
@@ -558,8 +559,7 @@ def test_pdb_continue_in_bottomframe():
def pdb_invoke(method, arg):
"""Run pdb.method(arg)."""
- import pdb
- getattr(pdb.Pdb(nosigint=True), method)(arg)
+ getattr(pdb.Pdb(nosigint=True, readrc=False), method)(arg)
def test_pdb_run_with_incorrect_argument():
@@ -608,7 +608,7 @@ def test_next_until_return_at_return_event():
... x = 2
>>> def test_function():
- ... import pdb; pdb.Pdb(nosigint=True).set_trace()
+ ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
... test_function_2()
... test_function_2()
... test_function_2()
@@ -674,7 +674,7 @@ def test_pdb_next_command_for_generator():
... yield 2
>>> def test_function():
- ... import pdb; pdb.Pdb(nosigint=True).set_trace()
+ ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
... it = test_gen()
... try:
... if next(it) != 0:
@@ -734,7 +734,7 @@ def test_pdb_return_command_for_generator():
... yield 2
>>> def test_function():
- ... import pdb; pdb.Pdb(nosigint=True).set_trace()
+ ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
... it = test_gen()
... try:
... if next(it) != 0:
@@ -789,7 +789,7 @@ def test_pdb_until_command_for_generator():
... yield 2
>>> def test_function():
- ... import pdb; pdb.Pdb(nosigint=True).set_trace()
+ ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
... for i in test_gen():
... print(i)
... print("finished")
@@ -831,7 +831,7 @@ def test_pdb_next_command_in_generator_for_loop():
... return 1
>>> def test_function():
- ... import pdb; pdb.Pdb(nosigint=True).set_trace()
+ ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
... for i in test_gen():
... print('value', i)
... x = 123
@@ -876,7 +876,7 @@ def test_pdb_next_command_subiterator():
... return x
>>> def test_function():
- ... import pdb; pdb.Pdb(nosigint=True).set_trace()
+ ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
... for i in test_gen():
... print('value', i)
... x = 123
@@ -1049,7 +1049,7 @@ class PdbTestCase(unittest.TestCase):
import pdb
def start_pdb():
- pdb.Pdb().set_trace()
+ pdb.Pdb(readrc=False).set_trace()
x = 1
y = 1
@@ -1078,6 +1078,38 @@ class PdbTestCase(unittest.TestCase):
.format(expected, stdout))
+ def test_readrc_kwarg(self):
+ script = textwrap.dedent("""
+ import pdb; pdb.Pdb(readrc=False).set_trace()
+
+ print('hello')
+ """)
+
+ save_home = os.environ.pop('HOME', None)
+ try:
+ with support.temp_cwd():
+ with open('.pdbrc', 'w') as f:
+ f.write("invalid\n")
+
+ with open('main.py', 'w') as f:
+ f.write(script)
+
+ cmd = [sys.executable, 'main.py']
+ proc = subprocess.Popen(
+ cmd,
+ stdout=subprocess.PIPE,
+ stdin=subprocess.PIPE,
+ stderr=subprocess.PIPE,
+ )
+ with proc:
+ stdout, stderr = proc.communicate(b'q\n')
+ self.assertNotIn("NameError: name 'invalid' is not defined",
+ stdout.decode())
+
+ finally:
+ if save_home is not None:
+ os.environ['HOME'] = save_home
+
def tearDown(self):
support.unlink(support.TESTFN)
diff --git a/Lib/test/test_peepholer.py b/Lib/test/test_peepholer.py
index 41e5091..b033640 100644
--- a/Lib/test/test_peepholer.py
+++ b/Lib/test/test_peepholer.py
@@ -1,9 +1,8 @@
import dis
import re
import sys
-from io import StringIO
+import textwrap
import unittest
-from math import copysign
from test.bytecode_helper import BytecodeTestCase
@@ -30,22 +29,25 @@ class TestTranforms(BytecodeTestCase):
def test_global_as_constant(self):
# LOAD_GLOBAL None/True/False --> LOAD_CONST None/True/False
- def f(x):
- None
- None
+ def f():
+ x = None
+ x = None
return x
- def g(x):
- True
+ def g():
+ x = True
return x
- def h(x):
- False
+ def h():
+ x = False
return x
+
for func, elem in ((f, None), (g, True), (h, False)):
self.assertNotInBytecode(func, 'LOAD_GLOBAL')
self.assertInBytecode(func, 'LOAD_CONST', elem)
+
def f():
'Adding a docstring made this test fail in Py2.5.0'
return None
+
self.assertNotInBytecode(f, 'LOAD_GLOBAL')
self.assertInBytecode(f, 'LOAD_CONST', None)
diff --git a/Lib/test/test_pep247.py b/Lib/test/test_pep247.py
deleted file mode 100644
index c17ceed..0000000
--- a/Lib/test/test_pep247.py
+++ /dev/null
@@ -1,66 +0,0 @@
-"""
-Test suite to check compliance with PEP 247, the standard API
-for hashing algorithms
-"""
-
-import hmac
-import unittest
-from hashlib import md5, sha1, sha224, sha256, sha384, sha512
-
-class Pep247Test(unittest.TestCase):
-
- def check_module(self, module, key=None):
- self.assertTrue(hasattr(module, 'digest_size'))
- self.assertTrue(module.digest_size is None or module.digest_size > 0)
- self.check_object(module.new, module.digest_size, key)
-
- def check_object(self, cls, digest_size, key, digestmod=None):
- if key is not None:
- if digestmod is None:
- digestmod = md5
- obj1 = cls(key, digestmod=digestmod)
- obj2 = cls(key, b'string', digestmod=digestmod)
- h1 = cls(key, b'string', digestmod=digestmod).digest()
- obj3 = cls(key, digestmod=digestmod)
- obj3.update(b'string')
- h2 = obj3.digest()
- else:
- obj1 = cls()
- obj2 = cls(b'string')
- h1 = cls(b'string').digest()
- obj3 = cls()
- obj3.update(b'string')
- h2 = obj3.digest()
- self.assertEqual(h1, h2)
- self.assertTrue(hasattr(obj1, 'digest_size'))
-
- if digest_size is not None:
- self.assertEqual(obj1.digest_size, digest_size)
-
- self.assertEqual(obj1.digest_size, len(h1))
- obj1.update(b'string')
- obj_copy = obj1.copy()
- self.assertEqual(obj1.digest(), obj_copy.digest())
- self.assertEqual(obj1.hexdigest(), obj_copy.hexdigest())
-
- digest, hexdigest = obj1.digest(), obj1.hexdigest()
- hd2 = ""
- for byte in digest:
- hd2 += '%02x' % byte
- self.assertEqual(hd2, hexdigest)
-
- def test_md5(self):
- self.check_object(md5, None, None)
-
- def test_sha(self):
- self.check_object(sha1, None, None)
- self.check_object(sha224, None, None)
- self.check_object(sha256, None, None)
- self.check_object(sha384, None, None)
- self.check_object(sha512, None, None)
-
- def test_hmac(self):
- self.check_module(hmac, key=b'abc')
-
-if __name__ == '__main__':
- unittest.main()
diff --git a/Lib/test/test_pickle.py b/Lib/test/test_pickle.py
index d467d52..e6c5d08 100644
--- a/Lib/test/test_pickle.py
+++ b/Lib/test/test_pickle.py
@@ -34,8 +34,6 @@ class PyUnpicklerTests(AbstractUnpickleTests):
unpickler = pickle._Unpickler
bad_stack_errors = (IndexError,)
- bad_mark_errors = (IndexError, pickle.UnpicklingError,
- TypeError, AttributeError, EOFError)
truncated_errors = (pickle.UnpicklingError, EOFError,
AttributeError, ValueError,
struct.error, IndexError, ImportError)
@@ -70,8 +68,6 @@ class InMemoryPickleTests(AbstractPickleTests, AbstractUnpickleTests,
pickler = pickle._Pickler
unpickler = pickle._Unpickler
bad_stack_errors = (pickle.UnpicklingError, IndexError)
- bad_mark_errors = (pickle.UnpicklingError, IndexError,
- TypeError, AttributeError, EOFError)
truncated_errors = (pickle.UnpicklingError, EOFError,
AttributeError, ValueError,
struct.error, IndexError, ImportError)
@@ -143,9 +139,7 @@ if has_c_implementation:
class CUnpicklerTests(PyUnpicklerTests):
unpickler = _pickle.Unpickler
bad_stack_errors = (pickle.UnpicklingError,)
- bad_mark_errors = (EOFError,)
- truncated_errors = (pickle.UnpicklingError, EOFError,
- AttributeError, ValueError)
+ truncated_errors = (pickle.UnpicklingError,)
class CPicklerTests(PyPicklerTests):
pickler = _pickle.Pickler
@@ -341,6 +335,9 @@ class CompatPickleTests(unittest.TestCase):
if (module2, name2) == ('exceptions', 'OSError'):
attr = getattribute(module3, name3)
self.assertTrue(issubclass(attr, OSError))
+ elif (module2, name2) == ('exceptions', 'ImportError'):
+ attr = getattribute(module3, name3)
+ self.assertTrue(issubclass(attr, ImportError))
else:
module, name = mapping(module2, name2)
if module3[:1] != '_':
@@ -407,6 +404,11 @@ class CompatPickleTests(unittest.TestCase):
if exc is not OSError and issubclass(exc, OSError):
self.assertEqual(reverse_mapping('builtins', name),
('exceptions', 'OSError'))
+ elif exc is not ImportError and issubclass(exc, ImportError):
+ self.assertEqual(reverse_mapping('builtins', name),
+ ('exceptions', 'ImportError'))
+ self.assertEqual(mapping('exceptions', name),
+ ('exceptions', name))
else:
self.assertEqual(reverse_mapping('builtins', name),
('exceptions', name))
diff --git a/Lib/test/test_pickletools.py b/Lib/test/test_pickletools.py
index bbe6875..86bebfa 100644
--- a/Lib/test/test_pickletools.py
+++ b/Lib/test/test_pickletools.py
@@ -1,9 +1,9 @@
-import struct
import pickle
import pickletools
from test import support
from test.pickletester import AbstractPickleTests
from test.pickletester import AbstractPickleModuleTests
+import unittest
class OptimizedPickleTests(AbstractPickleTests, AbstractPickleModuleTests):
@@ -59,8 +59,40 @@ class OptimizedPickleTests(AbstractPickleTests, AbstractPickleModuleTests):
self.assertNotIn(pickle.BINPUT, pickled2)
+class MiscTestCase(unittest.TestCase):
+ def test__all__(self):
+ blacklist = {'bytes_types',
+ 'UP_TO_NEWLINE', 'TAKEN_FROM_ARGUMENT1',
+ 'TAKEN_FROM_ARGUMENT4', 'TAKEN_FROM_ARGUMENT4U',
+ 'TAKEN_FROM_ARGUMENT8U', 'ArgumentDescriptor',
+ 'read_uint1', 'read_uint2', 'read_int4', 'read_uint4',
+ 'read_uint8', 'read_stringnl', 'read_stringnl_noescape',
+ 'read_stringnl_noescape_pair', 'read_string1',
+ 'read_string4', 'read_bytes1', 'read_bytes4',
+ 'read_bytes8', 'read_unicodestringnl',
+ 'read_unicodestring1', 'read_unicodestring4',
+ 'read_unicodestring8', 'read_decimalnl_short',
+ 'read_decimalnl_long', 'read_floatnl', 'read_float8',
+ 'read_long1', 'read_long4',
+ 'uint1', 'uint2', 'int4', 'uint4', 'uint8', 'stringnl',
+ 'stringnl_noescape', 'stringnl_noescape_pair', 'string1',
+ 'string4', 'bytes1', 'bytes4', 'bytes8',
+ 'unicodestringnl', 'unicodestring1', 'unicodestring4',
+ 'unicodestring8', 'decimalnl_short', 'decimalnl_long',
+ 'floatnl', 'float8', 'long1', 'long4',
+ 'StackObject',
+ 'pyint', 'pylong', 'pyinteger_or_bool', 'pybool', 'pyfloat',
+ 'pybytes_or_str', 'pystring', 'pybytes', 'pyunicode',
+ 'pynone', 'pytuple', 'pylist', 'pydict', 'pyset',
+ 'pyfrozenset', 'anyobject', 'markobject', 'stackslice',
+ 'OpcodeInfo', 'opcodes', 'code2op',
+ }
+ support.check__all__(self, pickletools, blacklist=blacklist)
+
+
def test_main():
support.run_unittest(OptimizedPickleTests)
+ support.run_unittest(MiscTestCase)
support.run_doctest(pickletools)
diff --git a/Lib/test/test_pipes.py b/Lib/test/test_pipes.py
index 6a7b45f..ad01d08 100644
--- a/Lib/test/test_pipes.py
+++ b/Lib/test/test_pipes.py
@@ -2,6 +2,7 @@ import pipes
import os
import string
import unittest
+import shutil
from test.support import TESTFN, run_unittest, unlink, reap_children
if os.name != 'posix':
@@ -18,6 +19,8 @@ class SimplePipeTests(unittest.TestCase):
unlink(f)
def testSimplePipe1(self):
+ if shutil.which('tr') is None:
+ self.skipTest('tr is not available')
t = pipes.Template()
t.append(s_command, pipes.STDIN_STDOUT)
f = t.open(TESTFN, 'w')
@@ -27,6 +30,8 @@ class SimplePipeTests(unittest.TestCase):
self.assertEqual(f.read(), 'HELLO WORLD #1')
def testSimplePipe2(self):
+ if shutil.which('tr') is None:
+ self.skipTest('tr is not available')
with open(TESTFN, 'w') as f:
f.write('hello world #2')
t = pipes.Template()
@@ -36,6 +41,8 @@ class SimplePipeTests(unittest.TestCase):
self.assertEqual(f.read(), 'HELLO WORLD #2')
def testSimplePipe3(self):
+ if shutil.which('tr') is None:
+ self.skipTest('tr is not available')
with open(TESTFN, 'w') as f:
f.write('hello world #2')
t = pipes.Template()
diff --git a/Lib/test/test_pkgutil.py b/Lib/test/test_pkgutil.py
index a820587..fc04dcf 100644
--- a/Lib/test/test_pkgutil.py
+++ b/Lib/test/test_pkgutil.py
@@ -81,8 +81,9 @@ class PkgutilTests(unittest.TestCase):
self.assertEqual(res2, RESOURCE_DATA)
names = []
- for loader, name, ispkg in pkgutil.iter_modules([zip_file]):
- names.append(name)
+ for moduleinfo in pkgutil.iter_modules([zip_file]):
+ self.assertIsInstance(moduleinfo, pkgutil.ModuleInfo)
+ names.append(moduleinfo.name)
self.assertEqual(names, ['test_getdata_zipfile'])
del sys.path[0]
@@ -413,6 +414,7 @@ class ImportlibMigrationTests(unittest.TestCase):
self.assertIsNotNone(pkgutil.get_loader("test.support"))
self.assertEqual(len(w.warnings), 0)
+ @unittest.skipIf(__name__ == '__main__', 'not compatible with __main__')
def test_get_loader_handles_missing_loader_attribute(self):
global __loader__
this_loader = __loader__
diff --git a/Lib/test/test_platform.py b/Lib/test/test_platform.py
index 18de110..5f875ef 100644
--- a/Lib/test/test_platform.py
+++ b/Lib/test/test_platform.py
@@ -33,6 +33,7 @@ class PlatformTest(unittest.TestCase):
'import platform; print(platform.architecture())']
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
return p.communicate()
+
real = os.path.realpath(sys.executable)
link = os.path.abspath(support.TESTFN)
os.symlink(real, link)
@@ -262,7 +263,7 @@ class PlatformTest(unittest.TestCase):
with warnings.catch_warnings():
warnings.filterwarnings(
'ignore',
- 'dist\(\) and linux_distribution\(\) '
+ r'dist\(\) and linux_distribution\(\) '
'functions are deprecated .*',
PendingDeprecationWarning,
)
@@ -338,7 +339,7 @@ class PlatformTest(unittest.TestCase):
with warnings.catch_warnings():
warnings.filterwarnings(
'ignore',
- 'dist\(\) and linux_distribution\(\) '
+ r'dist\(\) and linux_distribution\(\) '
'functions are deprecated .*',
PendingDeprecationWarning,
)
diff --git a/Lib/test/test_plistlib.py b/Lib/test/test_plistlib.py
index 692cac4..c77a6bf 100644
--- a/Lib/test/test_plistlib.py
+++ b/Lib/test/test_plistlib.py
@@ -7,7 +7,6 @@ import datetime
import codecs
import binascii
import collections
-import struct
from test import support
from io import BytesIO
@@ -534,8 +533,14 @@ class TestPlistlibDeprecated(unittest.TestCase):
self.assertEqual(cur, in_data)
+class MiscTestCase(unittest.TestCase):
+ def test__all__(self):
+ blacklist = {"PlistFormat", "PLISTHEADER"}
+ support.check__all__(self, plistlib, blacklist=blacklist)
+
+
def test_main():
- support.run_unittest(TestPlistlib, TestPlistlibDeprecated)
+ support.run_unittest(TestPlistlib, TestPlistlibDeprecated, MiscTestCase)
if __name__ == '__main__':
diff --git a/Lib/test/test_poplib.py b/Lib/test/test_poplib.py
index bceeb93..e5b16dc 100644
--- a/Lib/test/test_poplib.py
+++ b/Lib/test/test_poplib.py
@@ -8,7 +8,6 @@ import asyncore
import asynchat
import socket
import os
-import time
import errno
from unittest import TestCase, skipUnless
@@ -153,10 +152,12 @@ class DummyPOP3Handler(asynchat.async_chat):
def cmd_stls(self, arg):
if self.tls_active is False:
self.push('+OK Begin TLS negotiation')
- tls_sock = ssl.wrap_socket(self.socket, certfile=CERTFILE,
- server_side=True,
- do_handshake_on_connect=False,
- suppress_ragged_eofs=False)
+ context = ssl.SSLContext()
+ context.load_cert_chain(CERTFILE)
+ tls_sock = context.wrap_socket(self.socket,
+ server_side=True,
+ do_handshake_on_connect=False,
+ suppress_ragged_eofs=False)
self.del_channel()
self.set_socket(tls_sock)
self.tls_active = True
diff --git a/Lib/test/test_posix.py b/Lib/test/test_posix.py
index 2a59c38..029d081 100644
--- a/Lib/test/test_posix.py
+++ b/Lib/test/test_posix.py
@@ -1,6 +1,7 @@
"Test posix functions"
from test import support
+android_not_root = support.android_not_root
# Skip these tests if there is no posix module.
posix = support.import_module('posix')
@@ -11,7 +12,6 @@ import time
import os
import platform
import pwd
-import shutil
import stat
import tempfile
import unittest
@@ -398,7 +398,7 @@ class PosixTester(unittest.TestCase):
self.assertTrue(posix.stat(fp.fileno()))
self.assertRaisesRegex(TypeError,
- 'should be string, bytes or integer, not',
+ 'should be string, bytes, os.PathLike or integer, not',
posix.stat, float(fp.fileno()))
finally:
fp.close()
@@ -408,19 +408,22 @@ class PosixTester(unittest.TestCase):
def test_stat(self):
self.assertTrue(posix.stat(support.TESTFN))
self.assertTrue(posix.stat(os.fsencode(support.TESTFN)))
- self.assertTrue(posix.stat(bytearray(os.fsencode(support.TESTFN))))
+ self.assertWarnsRegex(DeprecationWarning,
+ 'should be string, bytes, os.PathLike or integer, not',
+ posix.stat, bytearray(os.fsencode(support.TESTFN)))
self.assertRaisesRegex(TypeError,
- 'can\'t specify None for path argument',
+ 'should be string, bytes, os.PathLike or integer, not',
posix.stat, None)
self.assertRaisesRegex(TypeError,
- 'should be string, bytes or integer, not',
+ 'should be string, bytes, os.PathLike or integer, not',
posix.stat, list(support.TESTFN))
self.assertRaisesRegex(TypeError,
- 'should be string, bytes or integer, not',
+ 'should be string, bytes, os.PathLike or integer, not',
posix.stat, list(os.fsencode(support.TESTFN)))
@unittest.skipUnless(hasattr(posix, 'mkfifo'), "don't have mkfifo()")
+ @unittest.skipIf(android_not_root, "mkfifo not allowed, non root user")
def test_mkfifo(self):
support.unlink(support.TESTFN)
posix.mkfifo(support.TESTFN, stat.S_IRUSR | stat.S_IWUSR)
@@ -428,6 +431,7 @@ class PosixTester(unittest.TestCase):
@unittest.skipUnless(hasattr(posix, 'mknod') and hasattr(stat, 'S_IFIFO'),
"don't have mknod()/S_IFIFO")
+ @unittest.skipIf(android_not_root, "mknod not allowed, non root user")
def test_mknod(self):
# Test using mknod() to create a FIFO (the only use specified
# by POSIX).
@@ -798,7 +802,11 @@ class PosixTester(unittest.TestCase):
groups = idg.read().strip()
ret = idg.close()
- if ret is not None or not groups:
+ try:
+ idg_groups = set(int(g) for g in groups.split())
+ except ValueError:
+ idg_groups = set()
+ if ret is not None or not idg_groups:
raise unittest.SkipTest("need working 'id -G'")
# Issues 16698: OS X ABIs prior to 10.6 have limits on getgroups()
@@ -809,12 +817,11 @@ class PosixTester(unittest.TestCase):
raise unittest.SkipTest("getgroups(2) is broken prior to 10.6")
# 'id -G' and 'os.getgroups()' should return the same
- # groups, ignoring order and duplicates.
- # #10822 - it is implementation defined whether posix.getgroups()
- # includes the effective gid so we include it anyway, since id -G does
- self.assertEqual(
- set([int(x) for x in groups.split()]),
- set(posix.getgroups() + [posix.getegid()]))
+ # groups, ignoring order, duplicates, and the effective gid.
+ # #10822/#26944 - It is implementation defined whether
+ # posix.getgroups() includes the effective gid.
+ symdiff = idg_groups.symmetric_difference(posix.getgroups())
+ self.assertTrue(not symdiff or symdiff == {posix.getegid()})
# tests for the posix *at functions follow
@@ -863,9 +870,9 @@ class PosixTester(unittest.TestCase):
self.assertEqual(s1, s2)
s2 = posix.stat(support.TESTFN, dir_fd=None)
self.assertEqual(s1, s2)
- self.assertRaisesRegex(TypeError, 'should be integer, not',
+ self.assertRaisesRegex(TypeError, 'should be integer or None, not',
posix.stat, support.TESTFN, dir_fd=posix.getcwd())
- self.assertRaisesRegex(TypeError, 'should be integer, not',
+ self.assertRaisesRegex(TypeError, 'should be integer or None, not',
posix.stat, support.TESTFN, dir_fd=float(f))
self.assertRaises(OverflowError,
posix.stat, support.TESTFN, dir_fd=10**20)
@@ -903,6 +910,7 @@ class PosixTester(unittest.TestCase):
posix.close(f)
@unittest.skipUnless(os.link in os.supports_dir_fd, "test needs dir_fd support in os.link()")
+ @unittest.skipIf(android_not_root, "hard link not allowed, non root user")
def test_link_dir_fd(self):
f = posix.open(posix.getcwd(), posix.O_RDONLY)
try:
@@ -926,6 +934,7 @@ class PosixTester(unittest.TestCase):
@unittest.skipUnless((os.mknod in os.supports_dir_fd) and hasattr(stat, 'S_IFIFO'),
"test requires both stat.S_IFIFO and dir_fd support for os.mknod()")
+ @unittest.skipIf(android_not_root, "mknod not allowed, non root user")
def test_mknod_dir_fd(self):
# Test using mknodat() to create a FIFO (the only use specified
# by POSIX).
@@ -1009,6 +1018,7 @@ class PosixTester(unittest.TestCase):
posix.close(f)
@unittest.skipUnless(os.mkfifo in os.supports_dir_fd, "test needs dir_fd support in os.mkfifo()")
+ @unittest.skipIf(android_not_root, "mkfifo not allowed, non root user")
def test_mkfifo_dir_fd(self):
support.unlink(support.TESTFN)
f = posix.open(posix.getcwd(), posix.O_RDONLY)
diff --git a/Lib/test/test_posixpath.py b/Lib/test/test_posixpath.py
index 0783c36..8a1e33b 100644
--- a/Lib/test/test_posixpath.py
+++ b/Lib/test/test_posixpath.py
@@ -596,5 +596,85 @@ class PosixCommonTest(test_genericpath.CommonTest, unittest.TestCase):
attributes = ['relpath', 'samefile', 'sameopenfile', 'samestat']
+class PathLikeTests(unittest.TestCase):
+
+ path = posixpath
+
+ class PathLike:
+ def __init__(self, path=''):
+ self.path = path
+ def __fspath__(self):
+ if isinstance(self.path, BaseException):
+ raise self.path
+ else:
+ return self.path
+
+ def setUp(self):
+ self.file_name = support.TESTFN.lower()
+ self.file_path = self.PathLike(support.TESTFN)
+ self.addCleanup(support.unlink, self.file_name)
+ with open(self.file_name, 'xb', 0) as file:
+ file.write(b"test_posixpath.PathLikeTests")
+
+ def assertPathEqual(self, func):
+ self.assertEqual(func(self.file_path), func(self.file_name))
+
+ def test_path_normcase(self):
+ self.assertPathEqual(self.path.normcase)
+
+ def test_path_isabs(self):
+ self.assertPathEqual(self.path.isabs)
+
+ def test_path_join(self):
+ self.assertEqual(self.path.join('a', self.PathLike('b'), 'c'),
+ self.path.join('a', 'b', 'c'))
+
+ def test_path_split(self):
+ self.assertPathEqual(self.path.split)
+
+ def test_path_splitext(self):
+ self.assertPathEqual(self.path.splitext)
+
+ def test_path_splitdrive(self):
+ self.assertPathEqual(self.path.splitdrive)
+
+ def test_path_basename(self):
+ self.assertPathEqual(self.path.basename)
+
+ def test_path_dirname(self):
+ self.assertPathEqual(self.path.dirname)
+
+ def test_path_islink(self):
+ self.assertPathEqual(self.path.islink)
+
+ def test_path_lexists(self):
+ self.assertPathEqual(self.path.lexists)
+
+ def test_path_ismount(self):
+ self.assertPathEqual(self.path.ismount)
+
+ def test_path_expanduser(self):
+ self.assertPathEqual(self.path.expanduser)
+
+ def test_path_expandvars(self):
+ self.assertPathEqual(self.path.expandvars)
+
+ def test_path_normpath(self):
+ self.assertPathEqual(self.path.normpath)
+
+ def test_path_abspath(self):
+ self.assertPathEqual(self.path.abspath)
+
+ def test_path_realpath(self):
+ self.assertPathEqual(self.path.realpath)
+
+ def test_path_relpath(self):
+ self.assertPathEqual(self.path.relpath)
+
+ def test_path_commonpath(self):
+ common_path = self.path.commonpath([self.file_path, self.file_name])
+ self.assertEqual(common_path, self.file_name)
+
+
if __name__=="__main__":
unittest.main()
diff --git a/Lib/test/test_pow.py b/Lib/test/test_pow.py
index ba608fb..cac1ae5 100644
--- a/Lib/test/test_pow.py
+++ b/Lib/test/test_pow.py
@@ -1,4 +1,4 @@
-import test.support, unittest
+import unittest
class PowTest(unittest.TestCase):
diff --git a/Lib/test/test_pty.py b/Lib/test/test_pty.py
index ef5e99e..15f88e4 100644
--- a/Lib/test/test_pty.py
+++ b/Lib/test/test_pty.py
@@ -277,7 +277,6 @@ class SmallPtyTests(unittest.TestCase):
socketpair = self._socketpair()
masters = [s.fileno() for s in socketpair]
- os.close(masters[1])
socketpair[1].close()
os.close(write_to_stdin_fd)
diff --git a/Lib/test/test_pulldom.py b/Lib/test/test_pulldom.py
index 1932c6b..3d89e3a 100644
--- a/Lib/test/test_pulldom.py
+++ b/Lib/test/test_pulldom.py
@@ -1,6 +1,5 @@
import io
import unittest
-import sys
import xml.sax
from xml.sax.xmlreader import AttributesImpl
diff --git a/Lib/test/test_pwd.py b/Lib/test/test_pwd.py
index b7b1a4a..ac9cff7 100644
--- a/Lib/test/test_pwd.py
+++ b/Lib/test/test_pwd.py
@@ -4,10 +4,19 @@ from test import support
pwd = support.import_module('pwd')
+def _getpwall():
+ # Android does not have getpwall.
+ if hasattr(pwd, 'getpwall'):
+ return pwd.getpwall()
+ elif hasattr(pwd, 'getpwuid'):
+ return [pwd.getpwuid(0)]
+ else:
+ return []
+
class PwdTest(unittest.TestCase):
def test_values(self):
- entries = pwd.getpwall()
+ entries = _getpwall()
for e in entries:
self.assertEqual(len(e), 7)
@@ -33,7 +42,7 @@ class PwdTest(unittest.TestCase):
# and check afterwards (done in test_values_extended)
def test_values_extended(self):
- entries = pwd.getpwall()
+ entries = _getpwall()
entriesbyname = {}
entriesbyuid = {}
@@ -57,12 +66,13 @@ class PwdTest(unittest.TestCase):
self.assertRaises(TypeError, pwd.getpwuid, 3.14)
self.assertRaises(TypeError, pwd.getpwnam)
self.assertRaises(TypeError, pwd.getpwnam, 42)
- self.assertRaises(TypeError, pwd.getpwall, 42)
+ if hasattr(pwd, 'getpwall'):
+ self.assertRaises(TypeError, pwd.getpwall, 42)
# try to get some errors
bynames = {}
byuids = {}
- for (n, p, u, g, gecos, d, s) in pwd.getpwall():
+ for (n, p, u, g, gecos, d, s) in _getpwall():
bynames[n] = u
byuids[u] = n
@@ -96,13 +106,17 @@ class PwdTest(unittest.TestCase):
# loop, say), pwd.getpwuid() might still be able to find data for that
# uid. Using sys.maxint may provoke the same problems, but hopefully
# it will be a more repeatable failure.
+ # Android accepts a very large span of uids including sys.maxsize and
+ # -1; it raises KeyError with 1 or 2 for example.
fakeuid = sys.maxsize
self.assertNotIn(fakeuid, byuids)
- self.assertRaises(KeyError, pwd.getpwuid, fakeuid)
+ if not support.is_android:
+ self.assertRaises(KeyError, pwd.getpwuid, fakeuid)
# -1 shouldn't be a valid uid because it has a special meaning in many
# uid-related functions
- self.assertRaises(KeyError, pwd.getpwuid, -1)
+ if not support.is_android:
+ self.assertRaises(KeyError, pwd.getpwuid, -1)
# should be out of uid_t range
self.assertRaises(KeyError, pwd.getpwuid, 2**128)
self.assertRaises(KeyError, pwd.getpwuid, -2**128)
diff --git a/Lib/test/test_pyclbr.py b/Lib/test/test_pyclbr.py
index 6ffbbbd..2cff1c5 100644
--- a/Lib/test/test_pyclbr.py
+++ b/Lib/test/test_pyclbr.py
@@ -156,9 +156,9 @@ class PyclbrTest(TestCase):
# These were once about the 10 longest modules
cm('random', ignore=('Random',)) # from _random import Random as CoreGenerator
cm('cgi', ignore=('log',)) # set with = in module
- cm('pickle')
+ cm('pickle', ignore=('partial',))
cm('aifc', ignore=('openfp', '_aifc_params')) # set with = in module
- cm('sre_parse', ignore=('dump', 'groups')) # from sre_constants import *; property
+ cm('sre_parse', ignore=('dump', 'groups', 'pos')) # from sre_constants import *; property
cm('pdb')
cm('pydoc')
diff --git a/Lib/test/test_pydoc.py b/Lib/test/test_pydoc.py
index 1f7ab7f..ed438d5 100644
--- a/Lib/test/test_pydoc.py
+++ b/Lib/test/test_pydoc.py
@@ -84,6 +84,8 @@ CLASSES
| Data and other attributes defined here:
|\x20\x20
| NO_MEANING = 'eggs'
+ |\x20\x20
+ | __annotations__ = {'NO_MEANING': <class 'str'>}
\x20\x20\x20\x20
class C(builtins.object)
| Methods defined here:
@@ -196,6 +198,8 @@ Data descriptors defined here:<br>
Data and other attributes defined here:<br>
<dl><dt><strong>NO_MEANING</strong> = 'eggs'</dl>
+<dl><dt><strong>__annotations__</strong> = {'NO_MEANING': &lt;class 'str'&gt;}</dl>
+
</td></tr></table> <p>
<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#ffc8d8">
@@ -264,7 +268,7 @@ Use help() to get the interactive help utility.
Use help(str) for help on the str class.'''.replace('\n', os.linesep)
# output pattern for module with bad imports
-badimport_pattern = "problem in %s - ImportError: No module named %r"
+badimport_pattern = "problem in %s - ModuleNotFoundError: No module named %r"
expected_dynamicattribute_pattern = """
Help on class DA in module %s:
@@ -639,8 +643,9 @@ class PydocDocTest(unittest.TestCase):
del expected['__doc__']
del expected['__class__']
# inspect resolves descriptors on type into methods, but vars doesn't,
- # so we need to update __subclasshook__.
+ # so we need to update __subclasshook__ and __init_subclass__.
expected['__subclasshook__'] = TestClass.__subclasshook__
+ expected['__init_subclass__'] = TestClass.__init_subclass__
methods = pydoc.allmethods(TestClass)
self.assertDictEqual(methods, expected)
@@ -868,6 +873,22 @@ class TestDescriptions(unittest.TestCase):
self.assertEqual(self._get_summary_line(t.wrap),
"wrap(text) method of textwrap.TextWrapper instance")
+ def test_field_order_for_named_tuples(self):
+ Person = namedtuple('Person', ['nickname', 'firstname', 'agegroup'])
+ s = pydoc.render_doc(Person)
+ self.assertLess(s.index('nickname'), s.index('firstname'))
+ self.assertLess(s.index('firstname'), s.index('agegroup'))
+
+ class NonIterableFields:
+ _fields = None
+
+ class NonHashableFields:
+ _fields = [[]]
+
+ # Make sure these doesn't fail
+ pydoc.render_doc(NonIterableFields)
+ pydoc.render_doc(NonHashableFields)
+
@requires_docstrings
def test_bound_builtin_method(self):
s = StringIO()
diff --git a/Lib/test/test_quopri.py b/Lib/test/test_quopri.py
index 7cac013..715544c 100644
--- a/Lib/test/test_quopri.py
+++ b/Lib/test/test_quopri.py
@@ -1,6 +1,6 @@
import unittest
-import sys, os, io, subprocess
+import sys, io, subprocess
import quopri
diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py
index e80ed17..5b6a4f0 100644
--- a/Lib/test/test_random.py
+++ b/Lib/test/test_random.py
@@ -7,6 +7,7 @@ import warnings
from functools import partial
from math import log, exp, pi, fsum, sin
from test import support
+from fractions import Fraction
class TestBasicOps:
# Superclass with tests common to all generators.
@@ -109,6 +110,7 @@ class TestBasicOps:
self.assertEqual(self.gen.sample([], 0), []) # test edge case N==k==0
# Exception raised if size of sample exceeds that of population
self.assertRaises(ValueError, self.gen.sample, population, N+1)
+ self.assertRaises(ValueError, self.gen.sample, [], -1)
def test_sample_distribution(self):
# For the entire allowable range of 0 <= k <= N, validate that
@@ -141,6 +143,83 @@ class TestBasicOps:
def test_sample_on_dicts(self):
self.assertRaises(TypeError, self.gen.sample, dict.fromkeys('abcdef'), 2)
+ def test_choices(self):
+ choices = self.gen.choices
+ data = ['red', 'green', 'blue', 'yellow']
+ str_data = 'abcd'
+ range_data = range(4)
+ set_data = set(range(4))
+
+ # basic functionality
+ for sample in [
+ choices(data, k=5),
+ choices(data, range(4), k=5),
+ choices(k=5, population=data, weights=range(4)),
+ choices(k=5, population=data, cum_weights=range(4)),
+ ]:
+ self.assertEqual(len(sample), 5)
+ self.assertEqual(type(sample), list)
+ self.assertTrue(set(sample) <= set(data))
+
+ # test argument handling
+ with self.assertRaises(TypeError): # missing arguments
+ choices(2)
+
+ self.assertEqual(choices(data, k=0), []) # k == 0
+ self.assertEqual(choices(data, k=-1), []) # negative k behaves like ``[0] * -1``
+ with self.assertRaises(TypeError):
+ choices(data, k=2.5) # k is a float
+
+ self.assertTrue(set(choices(str_data, k=5)) <= set(str_data)) # population is a string sequence
+ self.assertTrue(set(choices(range_data, k=5)) <= set(range_data)) # population is a range
+ with self.assertRaises(TypeError):
+ choices(set_data, k=2) # population is not a sequence
+
+ self.assertTrue(set(choices(data, None, k=5)) <= set(data)) # weights is None
+ self.assertTrue(set(choices(data, weights=None, k=5)) <= set(data))
+ with self.assertRaises(ValueError):
+ choices(data, [1,2], k=5) # len(weights) != len(population)
+ with self.assertRaises(TypeError):
+ choices(data, 10, k=5) # non-iterable weights
+ with self.assertRaises(TypeError):
+ choices(data, [None]*4, k=5) # non-numeric weights
+ for weights in [
+ [15, 10, 25, 30], # integer weights
+ [15.1, 10.2, 25.2, 30.3], # float weights
+ [Fraction(1, 3), Fraction(2, 6), Fraction(3, 6), Fraction(4, 6)], # fractional weights
+ [True, False, True, False] # booleans (include / exclude)
+ ]:
+ self.assertTrue(set(choices(data, weights, k=5)) <= set(data))
+
+ with self.assertRaises(ValueError):
+ choices(data, cum_weights=[1,2], k=5) # len(weights) != len(population)
+ with self.assertRaises(TypeError):
+ choices(data, cum_weights=10, k=5) # non-iterable cum_weights
+ with self.assertRaises(TypeError):
+ choices(data, cum_weights=[None]*4, k=5) # non-numeric cum_weights
+ with self.assertRaises(TypeError):
+ choices(data, range(4), cum_weights=range(4), k=5) # both weights and cum_weights
+ for weights in [
+ [15, 10, 25, 30], # integer cum_weights
+ [15.1, 10.2, 25.2, 30.3], # float cum_weights
+ [Fraction(1, 3), Fraction(2, 6), Fraction(3, 6), Fraction(4, 6)], # fractional cum_weights
+ ]:
+ self.assertTrue(set(choices(data, cum_weights=weights, k=5)) <= set(data))
+
+ # Test weight focused on a single element of the population
+ self.assertEqual(choices('abcd', [1, 0, 0, 0]), ['a'])
+ self.assertEqual(choices('abcd', [0, 1, 0, 0]), ['b'])
+ self.assertEqual(choices('abcd', [0, 0, 1, 0]), ['c'])
+ self.assertEqual(choices('abcd', [0, 0, 0, 1]), ['d'])
+
+ # Test consistency with random.choice() for empty population
+ with self.assertRaises(IndexError):
+ choices([], k=1)
+ with self.assertRaises(IndexError):
+ choices([], weights=[], k=1)
+ with self.assertRaises(IndexError):
+ choices([], cum_weights=[], k=5)
+
def test_gauss(self):
# Ensure that the seed() method initializes all the hidden state. In
# particular, through 2.2.1 it failed to reset a piece of state used
@@ -551,6 +630,39 @@ class MersenneTwister_TestBasicOps(TestBasicOps, unittest.TestCase):
self.assertTrue(stop < x <= start)
self.assertEqual((x+stop)%step, 0)
+ def test_choices_algorithms(self):
+ # The various ways of specifying weights should produce the same results
+ choices = self.gen.choices
+ n = 104729
+
+ self.gen.seed(8675309)
+ a = self.gen.choices(range(n), k=10000)
+
+ self.gen.seed(8675309)
+ b = self.gen.choices(range(n), [1]*n, k=10000)
+ self.assertEqual(a, b)
+
+ self.gen.seed(8675309)
+ c = self.gen.choices(range(n), cum_weights=range(1, n+1), k=10000)
+ self.assertEqual(a, c)
+
+ # Amerian Roulette
+ population = ['Red', 'Black', 'Green']
+ weights = [18, 18, 2]
+ cum_weights = [18, 36, 38]
+ expanded_population = ['Red'] * 18 + ['Black'] * 18 + ['Green'] * 2
+
+ self.gen.seed(9035768)
+ a = self.gen.choices(expanded_population, k=10000)
+
+ self.gen.seed(9035768)
+ b = self.gen.choices(population, weights, k=10000)
+ self.assertEqual(a, b)
+
+ self.gen.seed(9035768)
+ c = self.gen.choices(population, cum_weights=cum_weights, k=10000)
+ self.assertEqual(a, c)
+
def gamma(z, sqrt2pi=(2.0*pi)**0.5):
# Reflection to right half of complex plane
if z < 0.5:
diff --git a/Lib/test/test_range.py b/Lib/test/test_range.py
index c022f07..9e11e51 100644
--- a/Lib/test/test_range.py
+++ b/Lib/test/test_range.py
@@ -1,9 +1,10 @@
# Python test set -- built-in functions
-import test.support, unittest
+import unittest
import sys
import pickle
import itertools
+import test.support
# pure Python implementations (3 args only), for comparison
def pyrange(start, stop, step):
@@ -498,29 +499,32 @@ class RangeTest(unittest.TestCase):
import _testcapi
rangeiter_type = type(iter(range(0)))
- # rangeiter_new doesn't take keyword arguments
- with self.assertRaises(TypeError):
- rangeiter_type(a=1)
-
- # rangeiter_new takes exactly 3 arguments
- self.assertRaises(TypeError, rangeiter_type)
- self.assertRaises(TypeError, rangeiter_type, 1)
- self.assertRaises(TypeError, rangeiter_type, 1, 1)
- self.assertRaises(TypeError, rangeiter_type, 1, 1, 1, 1)
-
- # start, stop and stop must fit in C long
- for good_val in [_testcapi.LONG_MAX, _testcapi.LONG_MIN]:
- rangeiter_type(good_val, good_val, good_val)
- for bad_val in [_testcapi.LONG_MAX + 1, _testcapi.LONG_MIN - 1]:
- self.assertRaises(OverflowError,
- rangeiter_type, bad_val, 1, 1)
- self.assertRaises(OverflowError,
- rangeiter_type, 1, bad_val, 1)
- self.assertRaises(OverflowError,
- rangeiter_type, 1, 1, bad_val)
-
- # step mustn't be zero
- self.assertRaises(ValueError, rangeiter_type, 1, 1, 0)
+ self.assertWarns(DeprecationWarning, rangeiter_type, 1, 3, 1)
+
+ with test.support.check_warnings(('', DeprecationWarning)):
+ # rangeiter_new doesn't take keyword arguments
+ with self.assertRaises(TypeError):
+ rangeiter_type(a=1)
+
+ # rangeiter_new takes exactly 3 arguments
+ self.assertRaises(TypeError, rangeiter_type)
+ self.assertRaises(TypeError, rangeiter_type, 1)
+ self.assertRaises(TypeError, rangeiter_type, 1, 1)
+ self.assertRaises(TypeError, rangeiter_type, 1, 1, 1, 1)
+
+ # start, stop and stop must fit in C long
+ for good_val in [_testcapi.LONG_MAX, _testcapi.LONG_MIN]:
+ rangeiter_type(good_val, good_val, good_val)
+ for bad_val in [_testcapi.LONG_MAX + 1, _testcapi.LONG_MIN - 1]:
+ self.assertRaises(OverflowError,
+ rangeiter_type, bad_val, 1, 1)
+ self.assertRaises(OverflowError,
+ rangeiter_type, 1, bad_val, 1)
+ self.assertRaises(OverflowError,
+ rangeiter_type, 1, 1, bad_val)
+
+ # step mustn't be zero
+ self.assertRaises(ValueError, rangeiter_type, 1, 1, 0)
def test_slice(self):
def check(start, stop, step=None):
diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py
index 0834fe0..4bdaa4b 100644
--- a/Lib/test/test_re.py
+++ b/Lib/test/test_re.py
@@ -3,13 +3,13 @@ from test.support import verbose, run_unittest, gc_collect, bigmemtest, _2G, \
import io
import locale
import re
-from re import Scanner
import sre_compile
-import sre_constants
-import sys
import string
+import sys
import traceback
import unittest
+import warnings
+from re import Scanner
from weakref import proxy
# Misc tests from Tim Peters' re.doc
@@ -115,10 +115,10 @@ class ReTests(unittest.TestCase):
self.assertEqual(re.sub('(.)', re.escape(s), 'x'), s)
self.assertEqual(re.sub('(.)', lambda m: s, 'x'), s)
- self.assertEqual(re.sub('(?P<a>x)', '\g<a>\g<a>', 'xx'), 'xxxx')
- self.assertEqual(re.sub('(?P<a>x)', '\g<a>\g<1>', 'xx'), 'xxxx')
- self.assertEqual(re.sub('(?P<unk>x)', '\g<unk>\g<unk>', 'xx'), 'xxxx')
- self.assertEqual(re.sub('(?P<unk>x)', '\g<1>\g<1>', 'xx'), 'xxxx')
+ self.assertEqual(re.sub('(?P<a>x)', r'\g<a>\g<a>', 'xx'), 'xxxx')
+ self.assertEqual(re.sub('(?P<a>x)', r'\g<a>\g<1>', 'xx'), 'xxxx')
+ self.assertEqual(re.sub('(?P<unk>x)', r'\g<unk>\g<unk>', 'xx'), 'xxxx')
+ self.assertEqual(re.sub('(?P<unk>x)', r'\g<1>\g<1>', 'xx'), 'xxxx')
self.assertEqual(re.sub('a', r'\t\n\v\r\f\a\b', 'a'), '\t\n\v\r\f\a\b')
self.assertEqual(re.sub('a', '\t\n\v\r\f\a\b', 'a'), '\t\n\v\r\f\a\b')
@@ -129,11 +129,11 @@ class ReTests(unittest.TestCase):
with self.assertWarns(DeprecationWarning):
self.assertEqual(re.sub('a', '\\' + c, 'a'), '\\' + c)
- self.assertEqual(re.sub('^\s*', 'X', 'test'), 'Xtest')
+ self.assertEqual(re.sub(r'^\s*', 'X', 'test'), 'Xtest')
def test_bug_449964(self):
# fails for group followed by other escape
- self.assertEqual(re.sub(r'(?P<unk>x)', '\g<1>\g<1>\\b', 'xx'),
+ self.assertEqual(re.sub(r'(?P<unk>x)', r'\g<1>\g<1>\b', 'xx'),
'xx\bxx\b')
def test_bug_449000(self):
@@ -186,18 +186,19 @@ class ReTests(unittest.TestCase):
r'octal escape value \777 outside of '
r'range 0-0o377', 0)
- self.checkTemplateError('x', r'\1', 'x', 'invalid group reference')
- self.checkTemplateError('x', r'\8', 'x', 'invalid group reference')
- self.checkTemplateError('x', r'\9', 'x', 'invalid group reference')
- self.checkTemplateError('x', r'\11', 'x', 'invalid group reference')
- self.checkTemplateError('x', r'\18', 'x', 'invalid group reference')
- self.checkTemplateError('x', r'\1a', 'x', 'invalid group reference')
- self.checkTemplateError('x', r'\90', 'x', 'invalid group reference')
- self.checkTemplateError('x', r'\99', 'x', 'invalid group reference')
- self.checkTemplateError('x', r'\118', 'x', 'invalid group reference') # r'\11' + '8'
- self.checkTemplateError('x', r'\11a', 'x', 'invalid group reference')
- self.checkTemplateError('x', r'\181', 'x', 'invalid group reference') # r'\18' + '1'
- self.checkTemplateError('x', r'\800', 'x', 'invalid group reference') # r'\80' + '0'
+ self.checkTemplateError('x', r'\1', 'x', 'invalid group reference 1', 1)
+ self.checkTemplateError('x', r'\8', 'x', 'invalid group reference 8', 1)
+ self.checkTemplateError('x', r'\9', 'x', 'invalid group reference 9', 1)
+ self.checkTemplateError('x', r'\11', 'x', 'invalid group reference 11', 1)
+ self.checkTemplateError('x', r'\18', 'x', 'invalid group reference 18', 1)
+ self.checkTemplateError('x', r'\1a', 'x', 'invalid group reference 1', 1)
+ self.checkTemplateError('x', r'\90', 'x', 'invalid group reference 90', 1)
+ self.checkTemplateError('x', r'\99', 'x', 'invalid group reference 99', 1)
+ self.checkTemplateError('x', r'\118', 'x', 'invalid group reference 11', 1)
+ self.checkTemplateError('x', r'\11a', 'x', 'invalid group reference 11', 1)
+ self.checkTemplateError('x', r'\181', 'x', 'invalid group reference 18', 1)
+ self.checkTemplateError('x', r'\800', 'x', 'invalid group reference 80', 1)
+ self.checkTemplateError('x', r'\8', '', 'invalid group reference 8', 1)
# in python2.3 (etc), these loop endlessly in sre_parser.py
self.assertEqual(re.sub('(((((((((((x)))))))))))', r'\11', 'x'), 'x')
@@ -221,26 +222,26 @@ class ReTests(unittest.TestCase):
self.assertEqual(re.sub('x+', '-', 'abxd'), 'ab-d')
def test_symbolic_groups(self):
- re.compile('(?P<a>x)(?P=a)(?(a)y)')
- re.compile('(?P<a1>x)(?P=a1)(?(a1)y)')
- re.compile('(?P<a1>x)\1(?(1)y)')
- self.checkPatternError('(?P<a>)(?P<a>)',
+ re.compile(r'(?P<a>x)(?P=a)(?(a)y)')
+ re.compile(r'(?P<a1>x)(?P=a1)(?(a1)y)')
+ re.compile(r'(?P<a1>x)\1(?(1)y)')
+ self.checkPatternError(r'(?P<a>)(?P<a>)',
"redefinition of group name 'a' as group 2; "
"was group 1")
- self.checkPatternError('(?P<a>(?P=a))',
+ self.checkPatternError(r'(?P<a>(?P=a))',
"cannot refer to an open group", 10)
- self.checkPatternError('(?Pxy)', 'unknown extension ?Px')
- self.checkPatternError('(?P<a>)(?P=a', 'missing ), unterminated name', 11)
- self.checkPatternError('(?P=', 'missing group name', 4)
- self.checkPatternError('(?P=)', 'missing group name', 4)
- self.checkPatternError('(?P=1)', "bad character in group name '1'", 4)
- self.checkPatternError('(?P=a)', "unknown group name 'a'")
- self.checkPatternError('(?P=a1)', "unknown group name 'a1'")
- self.checkPatternError('(?P=a.)', "bad character in group name 'a.'", 4)
- self.checkPatternError('(?P<)', 'missing >, unterminated name', 4)
- self.checkPatternError('(?P<a', 'missing >, unterminated name', 4)
- self.checkPatternError('(?P<', 'missing group name', 4)
- self.checkPatternError('(?P<>)', 'missing group name', 4)
+ self.checkPatternError(r'(?Pxy)', 'unknown extension ?Px')
+ self.checkPatternError(r'(?P<a>)(?P=a', 'missing ), unterminated name', 11)
+ self.checkPatternError(r'(?P=', 'missing group name', 4)
+ self.checkPatternError(r'(?P=)', 'missing group name', 4)
+ self.checkPatternError(r'(?P=1)', "bad character in group name '1'", 4)
+ self.checkPatternError(r'(?P=a)', "unknown group name 'a'")
+ self.checkPatternError(r'(?P=a1)', "unknown group name 'a1'")
+ self.checkPatternError(r'(?P=a.)', "bad character in group name 'a.'", 4)
+ self.checkPatternError(r'(?P<)', 'missing >, unterminated name', 4)
+ self.checkPatternError(r'(?P<a', 'missing >, unterminated name', 4)
+ self.checkPatternError(r'(?P<', 'missing group name', 4)
+ self.checkPatternError(r'(?P<>)', 'missing group name', 4)
self.checkPatternError(r'(?P<1>)', "bad character in group name '1'", 4)
self.checkPatternError(r'(?P<a.>)', "bad character in group name 'a.'", 4)
self.checkPatternError(r'(?(', 'missing group name', 3)
@@ -259,35 +260,35 @@ class ReTests(unittest.TestCase):
self.assertEqual(re.match(pat, 'xc8yz').span(), (0, 5))
def test_symbolic_refs(self):
- self.checkTemplateError('(?P<a>x)', '\g<a', 'xx',
+ self.checkTemplateError('(?P<a>x)', r'\g<a', 'xx',
'missing >, unterminated name', 3)
- self.checkTemplateError('(?P<a>x)', '\g<', 'xx',
+ self.checkTemplateError('(?P<a>x)', r'\g<', 'xx',
'missing group name', 3)
- self.checkTemplateError('(?P<a>x)', '\g', 'xx', 'missing <', 2)
- self.checkTemplateError('(?P<a>x)', '\g<a a>', 'xx',
+ self.checkTemplateError('(?P<a>x)', r'\g', 'xx', 'missing <', 2)
+ self.checkTemplateError('(?P<a>x)', r'\g<a a>', 'xx',
"bad character in group name 'a a'", 3)
- self.checkTemplateError('(?P<a>x)', '\g<>', 'xx',
+ self.checkTemplateError('(?P<a>x)', r'\g<>', 'xx',
'missing group name', 3)
- self.checkTemplateError('(?P<a>x)', '\g<1a1>', 'xx',
+ self.checkTemplateError('(?P<a>x)', r'\g<1a1>', 'xx',
"bad character in group name '1a1'", 3)
self.checkTemplateError('(?P<a>x)', r'\g<2>', 'xx',
- 'invalid group reference')
+ 'invalid group reference 2', 3)
self.checkTemplateError('(?P<a>x)', r'\2', 'xx',
- 'invalid group reference')
+ 'invalid group reference 2', 1)
with self.assertRaisesRegex(IndexError, "unknown group name 'ab'"):
- re.sub('(?P<a>x)', '\g<ab>', 'xx')
+ re.sub('(?P<a>x)', r'\g<ab>', 'xx')
self.assertEqual(re.sub('(?P<a>x)|(?P<b>y)', r'\g<b>', 'xx'), '')
self.assertEqual(re.sub('(?P<a>x)|(?P<b>y)', r'\2', 'xx'), '')
- self.checkTemplateError('(?P<a>x)', '\g<-1>', 'xx',
+ self.checkTemplateError('(?P<a>x)', r'\g<-1>', 'xx',
"bad character in group name '-1'", 3)
# New valid/invalid identifiers in Python 3
self.assertEqual(re.sub('(?P<µ>x)', r'\g<µ>', 'xx'), 'xx')
self.assertEqual(re.sub('(?P<𝔘𝔫𝔦𝔠𝔬𝔡𝔢>x)', r'\g<𝔘𝔫𝔦𝔠𝔬𝔡𝔢>', 'xx'), 'xx')
- self.checkTemplateError('(?P<a>x)', '\g<©>', 'xx',
+ self.checkTemplateError('(?P<a>x)', r'\g<©>', 'xx',
"bad character in group name '©'", 3)
# Support > 100 groups.
pat = '|'.join('x(?P<a%d>%x)y' % (i, i) for i in range(1, 200 + 1))
- self.assertEqual(re.sub(pat, '\g<200>', 'xc8yzxc8y'), 'c8zc8')
+ self.assertEqual(re.sub(pat, r'\g<200>', 'xc8yzxc8y'), 'c8zc8')
def test_re_subn(self):
self.assertEqual(re.subn("(?i)b+", "x", "bbbb BBBB"), ('x x', 2))
@@ -419,19 +420,77 @@ class ReTests(unittest.TestCase):
self.assertEqual(pat.match('bc').groups(), ('b', None, 'b', 'c'))
self.assertEqual(pat.match('bc').groups(""), ('b', "", 'b', 'c'))
- # A single group
- m = re.match('(a)', 'a')
- self.assertEqual(m.group(0), 'a')
- self.assertEqual(m.group(0), 'a')
- self.assertEqual(m.group(1), 'a')
- self.assertEqual(m.group(1, 1), ('a', 'a'))
-
pat = re.compile('(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?')
self.assertEqual(pat.match('a').group(1, 2, 3), ('a', None, None))
self.assertEqual(pat.match('b').group('a1', 'b2', 'c3'),
(None, 'b', None))
self.assertEqual(pat.match('ac').group(1, 'b2', 3), ('a', None, 'c'))
+ def test_group(self):
+ class Index:
+ def __init__(self, value):
+ self.value = value
+ def __index__(self):
+ return self.value
+ # A single group
+ m = re.match('(a)(b)', 'ab')
+ self.assertEqual(m.group(), 'ab')
+ self.assertEqual(m.group(0), 'ab')
+ self.assertEqual(m.group(1), 'a')
+ self.assertEqual(m.group(Index(1)), 'a')
+ self.assertRaises(IndexError, m.group, -1)
+ self.assertRaises(IndexError, m.group, 3)
+ self.assertRaises(IndexError, m.group, 1<<1000)
+ self.assertRaises(IndexError, m.group, Index(1<<1000))
+ self.assertRaises(IndexError, m.group, 'x')
+ # Multiple groups
+ self.assertEqual(m.group(2, 1), ('b', 'a'))
+ self.assertEqual(m.group(Index(2), Index(1)), ('b', 'a'))
+
+ def test_match_getitem(self):
+ pat = re.compile('(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?')
+
+ m = pat.match('a')
+ self.assertEqual(m['a1'], 'a')
+ self.assertEqual(m['b2'], None)
+ self.assertEqual(m['c3'], None)
+ self.assertEqual('a1={a1} b2={b2} c3={c3}'.format_map(m), 'a1=a b2=None c3=None')
+ self.assertEqual(m[0], 'a')
+ self.assertEqual(m[1], 'a')
+ self.assertEqual(m[2], None)
+ self.assertEqual(m[3], None)
+ with self.assertRaisesRegex(IndexError, 'no such group'):
+ m['X']
+ with self.assertRaisesRegex(IndexError, 'no such group'):
+ m[-1]
+ with self.assertRaisesRegex(IndexError, 'no such group'):
+ m[4]
+ with self.assertRaisesRegex(IndexError, 'no such group'):
+ m[0, 1]
+ with self.assertRaisesRegex(IndexError, 'no such group'):
+ m[(0,)]
+ with self.assertRaisesRegex(IndexError, 'no such group'):
+ m[(0, 1)]
+ with self.assertRaisesRegex(KeyError, 'a2'):
+ 'a1={a2}'.format_map(m)
+
+ m = pat.match('ac')
+ self.assertEqual(m['a1'], 'a')
+ self.assertEqual(m['b2'], None)
+ self.assertEqual(m['c3'], 'c')
+ self.assertEqual('a1={a1} b2={b2} c3={c3}'.format_map(m), 'a1=a b2=None c3=c')
+ self.assertEqual(m[0], 'ac')
+ self.assertEqual(m[1], 'a')
+ self.assertEqual(m[2], None)
+ self.assertEqual(m[3], 'c')
+
+ # Cannot assign.
+ with self.assertRaises(TypeError):
+ m[0] = 1
+
+ # No len().
+ self.assertRaises(TypeError, len, m)
+
def test_re_fullmatch(self):
# Issue 16203: Proposal: add re.fullmatch() method.
self.assertEqual(re.fullmatch(r"a", "a").span(), (0, 1))
@@ -463,19 +522,19 @@ class ReTests(unittest.TestCase):
re.compile(r".*?").fullmatch("abcd", pos=1, endpos=3).span(), (1, 3))
def test_re_groupref_exists(self):
- self.assertEqual(re.match('^(\()?([^()]+)(?(1)\))$', '(a)').groups(),
+ self.assertEqual(re.match(r'^(\()?([^()]+)(?(1)\))$', '(a)').groups(),
('(', 'a'))
- self.assertEqual(re.match('^(\()?([^()]+)(?(1)\))$', 'a').groups(),
+ self.assertEqual(re.match(r'^(\()?([^()]+)(?(1)\))$', 'a').groups(),
(None, 'a'))
- self.assertIsNone(re.match('^(\()?([^()]+)(?(1)\))$', 'a)'))
- self.assertIsNone(re.match('^(\()?([^()]+)(?(1)\))$', '(a'))
+ self.assertIsNone(re.match(r'^(\()?([^()]+)(?(1)\))$', 'a)'))
+ self.assertIsNone(re.match(r'^(\()?([^()]+)(?(1)\))$', '(a'))
self.assertEqual(re.match('^(?:(a)|c)((?(1)b|d))$', 'ab').groups(),
('a', 'b'))
- self.assertEqual(re.match('^(?:(a)|c)((?(1)b|d))$', 'cd').groups(),
+ self.assertEqual(re.match(r'^(?:(a)|c)((?(1)b|d))$', 'cd').groups(),
(None, 'd'))
- self.assertEqual(re.match('^(?:(a)|c)((?(1)|d))$', 'cd').groups(),
+ self.assertEqual(re.match(r'^(?:(a)|c)((?(1)|d))$', 'cd').groups(),
(None, 'd'))
- self.assertEqual(re.match('^(?:(a)|c)((?(1)|d))$', 'a').groups(),
+ self.assertEqual(re.match(r'^(?:(a)|c)((?(1)|d))$', 'a').groups(),
('a', ''))
# Tests for bug #1177831: exercise groups other than the first group
@@ -500,10 +559,11 @@ class ReTests(unittest.TestCase):
'two branches', 10)
def test_re_groupref_overflow(self):
- self.checkTemplateError('()', '\g<%s>' % sre_constants.MAXGROUPS, 'xx',
- 'invalid group reference', 3)
- self.checkPatternError(r'(?P<a>)(?(%d))' % sre_constants.MAXGROUPS,
- 'invalid group reference', 10)
+ from sre_constants import MAXGROUPS
+ self.checkTemplateError('()', r'\g<%s>' % MAXGROUPS, 'xx',
+ 'invalid group reference %d' % MAXGROUPS, 3)
+ self.checkPatternError(r'(?P<a>)(?(%d))' % MAXGROUPS,
+ 'invalid group reference %d' % MAXGROUPS, 10)
def test_re_groupref(self):
self.assertEqual(re.match(r'^(\|)?([^()]+)\1$', '|a|').groups(),
@@ -535,37 +595,37 @@ class ReTests(unittest.TestCase):
" ")
def test_repeat_minmax(self):
- self.assertIsNone(re.match("^(\w){1}$", "abc"))
- self.assertIsNone(re.match("^(\w){1}?$", "abc"))
- self.assertIsNone(re.match("^(\w){1,2}$", "abc"))
- self.assertIsNone(re.match("^(\w){1,2}?$", "abc"))
-
- self.assertEqual(re.match("^(\w){3}$", "abc").group(1), "c")
- self.assertEqual(re.match("^(\w){1,3}$", "abc").group(1), "c")
- self.assertEqual(re.match("^(\w){1,4}$", "abc").group(1), "c")
- self.assertEqual(re.match("^(\w){3,4}?$", "abc").group(1), "c")
- self.assertEqual(re.match("^(\w){3}?$", "abc").group(1), "c")
- self.assertEqual(re.match("^(\w){1,3}?$", "abc").group(1), "c")
- self.assertEqual(re.match("^(\w){1,4}?$", "abc").group(1), "c")
- self.assertEqual(re.match("^(\w){3,4}?$", "abc").group(1), "c")
-
- self.assertIsNone(re.match("^x{1}$", "xxx"))
- self.assertIsNone(re.match("^x{1}?$", "xxx"))
- self.assertIsNone(re.match("^x{1,2}$", "xxx"))
- self.assertIsNone(re.match("^x{1,2}?$", "xxx"))
-
- self.assertTrue(re.match("^x{3}$", "xxx"))
- self.assertTrue(re.match("^x{1,3}$", "xxx"))
- self.assertTrue(re.match("^x{3,3}$", "xxx"))
- self.assertTrue(re.match("^x{1,4}$", "xxx"))
- self.assertTrue(re.match("^x{3,4}?$", "xxx"))
- self.assertTrue(re.match("^x{3}?$", "xxx"))
- self.assertTrue(re.match("^x{1,3}?$", "xxx"))
- self.assertTrue(re.match("^x{1,4}?$", "xxx"))
- self.assertTrue(re.match("^x{3,4}?$", "xxx"))
-
- self.assertIsNone(re.match("^x{}$", "xxx"))
- self.assertTrue(re.match("^x{}$", "x{}"))
+ self.assertIsNone(re.match(r"^(\w){1}$", "abc"))
+ self.assertIsNone(re.match(r"^(\w){1}?$", "abc"))
+ self.assertIsNone(re.match(r"^(\w){1,2}$", "abc"))
+ self.assertIsNone(re.match(r"^(\w){1,2}?$", "abc"))
+
+ self.assertEqual(re.match(r"^(\w){3}$", "abc").group(1), "c")
+ self.assertEqual(re.match(r"^(\w){1,3}$", "abc").group(1), "c")
+ self.assertEqual(re.match(r"^(\w){1,4}$", "abc").group(1), "c")
+ self.assertEqual(re.match(r"^(\w){3,4}?$", "abc").group(1), "c")
+ self.assertEqual(re.match(r"^(\w){3}?$", "abc").group(1), "c")
+ self.assertEqual(re.match(r"^(\w){1,3}?$", "abc").group(1), "c")
+ self.assertEqual(re.match(r"^(\w){1,4}?$", "abc").group(1), "c")
+ self.assertEqual(re.match(r"^(\w){3,4}?$", "abc").group(1), "c")
+
+ self.assertIsNone(re.match(r"^x{1}$", "xxx"))
+ self.assertIsNone(re.match(r"^x{1}?$", "xxx"))
+ self.assertIsNone(re.match(r"^x{1,2}$", "xxx"))
+ self.assertIsNone(re.match(r"^x{1,2}?$", "xxx"))
+
+ self.assertTrue(re.match(r"^x{3}$", "xxx"))
+ self.assertTrue(re.match(r"^x{1,3}$", "xxx"))
+ self.assertTrue(re.match(r"^x{3,3}$", "xxx"))
+ self.assertTrue(re.match(r"^x{1,4}$", "xxx"))
+ self.assertTrue(re.match(r"^x{3,4}?$", "xxx"))
+ self.assertTrue(re.match(r"^x{3}?$", "xxx"))
+ self.assertTrue(re.match(r"^x{1,3}?$", "xxx"))
+ self.assertTrue(re.match(r"^x{1,4}?$", "xxx"))
+ self.assertTrue(re.match(r"^x{3,4}?$", "xxx"))
+
+ self.assertIsNone(re.match(r"^x{}$", "xxx"))
+ self.assertTrue(re.match(r"^x{}$", "x{}"))
self.checkPatternError(r'x{2,1}',
'min repeat greater than max repeat', 2)
@@ -638,14 +698,10 @@ class ReTests(unittest.TestCase):
re.purge() # for warnings
for c in 'ceghijklmopqyzCEFGHIJKLMNOPQRTVXY':
with self.subTest(c):
- with self.assertWarns(DeprecationWarning):
- self.assertEqual(re.fullmatch('\\%c' % c, c).group(), c)
- self.assertIsNone(re.match('\\%c' % c, 'a'))
+ self.assertRaises(re.error, re.compile, '\\%c' % c)
for c in 'ceghijklmopqyzABCEFGHIJKLMNOPQRTVXYZ':
with self.subTest(c):
- with self.assertWarns(DeprecationWarning):
- self.assertEqual(re.fullmatch('[\\%c]' % c, c).group(), c)
- self.assertIsNone(re.match('[\\%c]' % c, 'a'))
+ self.assertRaises(re.error, re.compile, '[\\%c]' % c)
def test_string_boundaries(self):
# See http://bugs.python.org/issue10713
@@ -692,10 +748,10 @@ class ReTests(unittest.TestCase):
"a\n\nb")
def test_lookahead(self):
- self.assertEqual(re.match("(a(?=\s[^a]))", "a b").group(1), "a")
- self.assertEqual(re.match("(a(?=\s[^a]*))", "a b").group(1), "a")
- self.assertEqual(re.match("(a(?=\s[abc]))", "a b").group(1), "a")
- self.assertEqual(re.match("(a(?=\s[abc]*))", "a bc").group(1), "a")
+ self.assertEqual(re.match(r"(a(?=\s[^a]))", "a b").group(1), "a")
+ self.assertEqual(re.match(r"(a(?=\s[^a]*))", "a b").group(1), "a")
+ self.assertEqual(re.match(r"(a(?=\s[abc]))", "a b").group(1), "a")
+ self.assertEqual(re.match(r"(a(?=\s[abc]*))", "a bc").group(1), "a")
self.assertEqual(re.match(r"(a)(?=\s\1)", "a a").group(1), "a")
self.assertEqual(re.match(r"(a)(?=\s\1*)", "a aa").group(1), "a")
self.assertEqual(re.match(r"(a)(?=\s(abc|a))", "a a").group(1), "a")
@@ -843,12 +899,12 @@ class ReTests(unittest.TestCase):
self.assertEqual(re.match(b"abc", b"ABC", re.I|re.L).group(0), b"ABC")
def test_not_literal(self):
- self.assertEqual(re.search("\s([^a])", " b").group(1), "b")
- self.assertEqual(re.search("\s([^a]*)", " bb").group(1), "bb")
+ self.assertEqual(re.search(r"\s([^a])", " b").group(1), "b")
+ self.assertEqual(re.search(r"\s([^a]*)", " bb").group(1), "bb")
def test_search_coverage(self):
- self.assertEqual(re.search("\s(b)", " b").group(1), "b")
- self.assertEqual(re.search("a\s", "a ").group(0), "a ")
+ self.assertEqual(re.search(r"\s(b)", " b").group(1), "b")
+ self.assertEqual(re.search(r"a\s", "a ").group(0), "a ")
def assertMatch(self, pattern, text, match=None, span=None,
matcher=re.match):
@@ -953,7 +1009,7 @@ class ReTests(unittest.TestCase):
self.checkPatternError(r"\567",
r'octal escape value \567 outside of '
r'range 0-0o377', 0)
- self.checkPatternError(r"\911", 'invalid group reference', 0)
+ self.checkPatternError(r"\911", 'invalid group reference 91', 1)
self.checkPatternError(r"\x1", r'incomplete escape \x1', 0)
self.checkPatternError(r"\x1z", r'incomplete escape \x1', 0)
self.checkPatternError(r"\u123", r'incomplete escape \u123', 0)
@@ -998,10 +1054,8 @@ class ReTests(unittest.TestCase):
self.assertTrue(re.match((r"\x%02x" % i).encode(), bytes([i])))
self.assertTrue(re.match((r"\x%02x0" % i).encode(), bytes([i])+b"0"))
self.assertTrue(re.match((r"\x%02xz" % i).encode(), bytes([i])+b"z"))
- with self.assertWarns(DeprecationWarning):
- self.assertTrue(re.match(br"\u1234", b'u1234'))
- with self.assertWarns(DeprecationWarning):
- self.assertTrue(re.match(br"\U00012345", b'U00012345'))
+ self.assertRaises(re.error, re.compile, br"\u1234")
+ self.assertRaises(re.error, re.compile, br"\U00012345")
self.assertTrue(re.match(br"\0", b"\000"))
self.assertTrue(re.match(br"\08", b"\0008"))
self.assertTrue(re.match(br"\01", b"\001"))
@@ -1009,7 +1063,7 @@ class ReTests(unittest.TestCase):
self.checkPatternError(br"\567",
r'octal escape value \567 outside of '
r'range 0-0o377', 0)
- self.checkPatternError(br"\911", 'invalid group reference', 0)
+ self.checkPatternError(br"\911", 'invalid group reference 91', 1)
self.checkPatternError(br"\x1", r'incomplete escape \x1', 0)
self.checkPatternError(br"\x1z", r'incomplete escape \x1', 0)
@@ -1023,10 +1077,8 @@ class ReTests(unittest.TestCase):
self.assertTrue(re.match((r"[\x%02x]" % i).encode(), bytes([i])))
self.assertTrue(re.match((r"[\x%02x0]" % i).encode(), bytes([i])))
self.assertTrue(re.match((r"[\x%02xz]" % i).encode(), bytes([i])))
- with self.assertWarns(DeprecationWarning):
- self.assertTrue(re.match(br"[\u1234]", b'u'))
- with self.assertWarns(DeprecationWarning):
- self.assertTrue(re.match(br"[\U00012345]", b'U'))
+ self.assertRaises(re.error, re.compile, br"[\u1234]")
+ self.assertRaises(re.error, re.compile, br"[\U00012345]")
self.checkPatternError(br"[\567]",
r'octal escape value \567 outside of '
r'range 0-0o377', 1)
@@ -1054,8 +1106,8 @@ class ReTests(unittest.TestCase):
self.assertIsNone(re.match(r'(a)?a','a').lastindex)
self.assertEqual(re.match(r'(a)(b)?b','ab').lastindex, 1)
self.assertEqual(re.match(r'(?P<a>a)(?P<b>b)?b','ab').lastgroup, 'a')
- self.assertEqual(re.match("(?P<a>a(b))", "ab").lastgroup, 'a')
- self.assertEqual(re.match("((a))", "a").lastindex, 1)
+ self.assertEqual(re.match(r"(?P<a>a(b))", "ab").lastgroup, 'a')
+ self.assertEqual(re.match(r"((a))", "a").lastindex, 1)
def test_bug_418626(self):
# bugs 418626 at al. -- Testing Greg Chapman's addition of op code
@@ -1227,7 +1279,7 @@ class ReTests(unittest.TestCase):
'\uff10', # '\N{FULLWIDTH DIGIT ZERO}', category 'Nd'
]
for x in decimal_digits:
- self.assertEqual(re.match('^\d$', x).group(0), x)
+ self.assertEqual(re.match(r'^\d$', x).group(0), x)
not_decimal_digits = [
'\u2165', # '\N{ROMAN NUMERAL SIX}', category 'Nl'
@@ -1236,7 +1288,7 @@ class ReTests(unittest.TestCase):
'\u32b4', # '\N{CIRCLED NUMBER THIRTY NINE}', category 'No'
]
for x in not_decimal_digits:
- self.assertIsNone(re.match('^\d$', x))
+ self.assertIsNone(re.match(r'^\d$', x))
def test_empty_array(self):
# SF buf 1647541
@@ -1278,6 +1330,22 @@ class ReTests(unittest.TestCase):
self.assertTrue(re.match('(?ixu) ' + upper_char, lower_char))
self.assertTrue(re.match('(?ixu) ' + lower_char, upper_char))
+ p = upper_char + '(?i)'
+ with self.assertWarns(DeprecationWarning) as warns:
+ self.assertTrue(re.match(p, lower_char))
+ self.assertEqual(
+ str(warns.warnings[0].message),
+ 'Flags not at the start of the expression %s' % p
+ )
+
+ p = upper_char + '(?i)%s' % ('.?' * 100)
+ with self.assertWarns(DeprecationWarning) as warns:
+ self.assertTrue(re.match(p, lower_char))
+ self.assertEqual(
+ str(warns.warnings[0].message),
+ 'Flags not at the start of the expression %s (truncated)' % p[:20]
+ )
+
def test_dollar_matches_twice(self):
"$ matches the end of string, and just before the terminating \n"
pattern = re.compile('$')
@@ -1308,29 +1376,29 @@ class ReTests(unittest.TestCase):
for flags in (0, re.UNICODE):
pat = re.compile('\xc0', flags | re.IGNORECASE)
self.assertTrue(pat.match('\xe0'))
- pat = re.compile('\w', flags)
+ pat = re.compile(r'\w', flags)
self.assertTrue(pat.match('\xe0'))
pat = re.compile('\xc0', re.ASCII | re.IGNORECASE)
self.assertIsNone(pat.match('\xe0'))
pat = re.compile('(?a)\xc0', re.IGNORECASE)
self.assertIsNone(pat.match('\xe0'))
- pat = re.compile('\w', re.ASCII)
+ pat = re.compile(r'\w', re.ASCII)
self.assertIsNone(pat.match('\xe0'))
- pat = re.compile('(?a)\w')
+ pat = re.compile(r'(?a)\w')
self.assertIsNone(pat.match('\xe0'))
# Bytes patterns
for flags in (0, re.ASCII):
pat = re.compile(b'\xc0', flags | re.IGNORECASE)
self.assertIsNone(pat.match(b'\xe0'))
- pat = re.compile(b'\w', flags)
+ pat = re.compile(br'\w', flags)
self.assertIsNone(pat.match(b'\xe0'))
# Incompatibilities
- self.assertRaises(ValueError, re.compile, b'\w', re.UNICODE)
- self.assertRaises(ValueError, re.compile, b'(?u)\w')
- self.assertRaises(ValueError, re.compile, '\w', re.UNICODE | re.ASCII)
- self.assertRaises(ValueError, re.compile, '(?u)\w', re.ASCII)
- self.assertRaises(ValueError, re.compile, '(?a)\w', re.UNICODE)
- self.assertRaises(ValueError, re.compile, '(?au)\w')
+ self.assertRaises(ValueError, re.compile, br'\w', re.UNICODE)
+ self.assertRaises(ValueError, re.compile, br'(?u)\w')
+ self.assertRaises(ValueError, re.compile, r'\w', re.UNICODE | re.ASCII)
+ self.assertRaises(ValueError, re.compile, r'(?u)\w', re.ASCII)
+ self.assertRaises(ValueError, re.compile, r'(?a)\w', re.UNICODE)
+ self.assertRaises(ValueError, re.compile, r'(?au)\w')
def test_locale_flag(self):
import locale
@@ -1361,27 +1429,59 @@ class ReTests(unittest.TestCase):
pat = re.compile(bpat, re.IGNORECASE)
if bletter:
self.assertIsNone(pat.match(bletter))
- pat = re.compile(b'\w', re.LOCALE)
+ pat = re.compile(br'\w', re.LOCALE)
if bletter:
self.assertTrue(pat.match(bletter))
- pat = re.compile(b'(?L)\w')
+ pat = re.compile(br'(?L)\w')
if bletter:
self.assertTrue(pat.match(bletter))
- pat = re.compile(b'\w')
+ pat = re.compile(br'\w')
if bletter:
self.assertIsNone(pat.match(bletter))
# Incompatibilities
- self.assertWarns(DeprecationWarning, re.compile, '', re.LOCALE)
- self.assertWarns(DeprecationWarning, re.compile, '(?L)')
- self.assertWarns(DeprecationWarning, re.compile, b'', re.LOCALE | re.ASCII)
- self.assertWarns(DeprecationWarning, re.compile, b'(?L)', re.ASCII)
- self.assertWarns(DeprecationWarning, re.compile, b'(?a)', re.LOCALE)
- self.assertWarns(DeprecationWarning, re.compile, b'(?aL)')
+ self.assertRaises(ValueError, re.compile, '', re.LOCALE)
+ self.assertRaises(ValueError, re.compile, '(?L)')
+ self.assertRaises(ValueError, re.compile, b'', re.LOCALE | re.ASCII)
+ self.assertRaises(ValueError, re.compile, b'(?L)', re.ASCII)
+ self.assertRaises(ValueError, re.compile, b'(?a)', re.LOCALE)
+ self.assertRaises(ValueError, re.compile, b'(?aL)')
+
+ def test_scoped_flags(self):
+ self.assertTrue(re.match(r'(?i:a)b', 'Ab'))
+ self.assertIsNone(re.match(r'(?i:a)b', 'aB'))
+ self.assertIsNone(re.match(r'(?-i:a)b', 'Ab', re.IGNORECASE))
+ self.assertTrue(re.match(r'(?-i:a)b', 'aB', re.IGNORECASE))
+ self.assertIsNone(re.match(r'(?i:(?-i:a)b)', 'Ab'))
+ self.assertTrue(re.match(r'(?i:(?-i:a)b)', 'aB'))
+
+ self.assertTrue(re.match(r'(?x: a) b', 'a b'))
+ self.assertIsNone(re.match(r'(?x: a) b', ' a b'))
+ self.assertTrue(re.match(r'(?-x: a) b', ' ab', re.VERBOSE))
+ self.assertIsNone(re.match(r'(?-x: a) b', 'ab', re.VERBOSE))
+
+ self.checkPatternError(r'(?a:\w)',
+ 'bad inline flags: cannot turn on global flag', 3)
+ self.checkPatternError(r'(?a)(?-a:\w)',
+ 'bad inline flags: cannot turn off global flag', 8)
+ self.checkPatternError(r'(?i-i:a)',
+ 'bad inline flags: flag turned on and off', 5)
+
+ self.checkPatternError(r'(?-', 'missing flag', 3)
+ self.checkPatternError(r'(?-+', 'missing flag', 3)
+ self.checkPatternError(r'(?-z', 'unknown flag', 3)
+ self.checkPatternError(r'(?-i', 'missing :', 4)
+ self.checkPatternError(r'(?-i)', 'missing :', 4)
+ self.checkPatternError(r'(?-i+', 'missing :', 4)
+ self.checkPatternError(r'(?-iz', 'unknown flag', 4)
+ self.checkPatternError(r'(?i:', 'missing ), unterminated subpattern', 0)
+ self.checkPatternError(r'(?i', 'missing -, : or )', 3)
+ self.checkPatternError(r'(?i+', 'missing -, : or )', 3)
+ self.checkPatternError(r'(?iz', 'unknown flag', 3)
def test_bug_6509(self):
# Replacement strings of both types must parse properly.
# all strings
- pat = re.compile('a(\w)')
+ pat = re.compile(r'a(\w)')
self.assertEqual(pat.sub('b\\1', 'ac'), 'bc')
pat = re.compile('a(.)')
self.assertEqual(pat.sub('b\\1', 'a\u1234'), 'b\u1234')
@@ -1389,7 +1489,7 @@ class ReTests(unittest.TestCase):
self.assertEqual(pat.sub(lambda m: 'str', 'a5'), 'str')
# all bytes
- pat = re.compile(b'a(\w)')
+ pat = re.compile(br'a(\w)')
self.assertEqual(pat.sub(b'b\\1', b'ac'), b'bc')
pat = re.compile(b'a(.)')
self.assertEqual(pat.sub(b'b\\1', b'a\xCD'), b'b\xCD')
@@ -1427,13 +1527,6 @@ class ReTests(unittest.TestCase):
# Test behaviour when not given a string or pattern as parameter
self.assertRaises(TypeError, re.compile, 0)
- def test_bug_13899(self):
- # Issue #13899: re pattern r"[\A]" should work like "A" but matches
- # nothing. Ditto B and Z.
- with self.assertWarns(DeprecationWarning):
- self.assertEqual(re.findall(r'[\A\B\b\C\Z]', 'AB\bCZ'),
- ['A', 'B', '\b', 'C', 'Z'])
-
@bigmemtest(size=_2G, memuse=1)
def test_large_search(self, size):
# Issue #10182: indices were 32-bit-truncated.
@@ -1518,7 +1611,7 @@ class ReTests(unittest.TestCase):
for string in (b'[abracadabra]', B(b'[abracadabra]'),
bytearray(b'[abracadabra]'),
memoryview(b'[abracadabra]')):
- m = re.search(rb'(.+)(.*?)\1', string)
+ m = re.search(br'(.+)(.*?)\1', string)
self.assertEqual(repr(m), "<%s.%s object; "
"span=(1, 12), match=b'abracadabra'>" %
(type(m).__module__, type(m).__qualname__))
@@ -1547,9 +1640,9 @@ class ReTests(unittest.TestCase):
with captured_stdout() as out:
re.compile(pat, re.DEBUG)
dump = '''\
-SUBPATTERN 1
+SUBPATTERN 1 0 0
LITERAL 46
-SUBPATTERN None
+SUBPATTERN None 0 0
BRANCH
IN
LITERAL 99
@@ -1557,7 +1650,7 @@ SUBPATTERN None
OR
LITERAL 112
LITERAL 121
-SUBPATTERN None
+SUBPATTERN None 0 0
GROUPREF_EXISTS 1
AT AT_END
ELSE
@@ -1673,12 +1766,64 @@ SUBPATTERN None
self.checkPatternError(r'(?P', 'unexpected end of pattern', 3)
self.checkPatternError(r'(?z)', 'unknown extension ?z', 1)
self.checkPatternError(r'(?iz)', 'unknown flag', 3)
- self.checkPatternError(r'(?i', 'missing )', 3)
+ self.checkPatternError(r'(?i', 'missing -, : or )', 3)
self.checkPatternError(r'(?#abc', 'missing ), unterminated comment', 0)
self.checkPatternError(r'(?<', 'unexpected end of pattern', 3)
self.checkPatternError(r'(?<>)', 'unknown extension ?<>', 1)
self.checkPatternError(r'(?', 'unexpected end of pattern', 2)
+ def test_enum(self):
+ # Issue #28082: Check that str(flag) returns a human readable string
+ # instead of an integer
+ self.assertIn('ASCII', str(re.A))
+ self.assertIn('DOTALL', str(re.S))
+
+ def test_pattern_compare(self):
+ pattern1 = re.compile('abc', re.IGNORECASE)
+
+ # equal to itself
+ self.assertEqual(pattern1, pattern1)
+ self.assertFalse(pattern1 != pattern1)
+
+ # equal
+ re.purge()
+ pattern2 = re.compile('abc', re.IGNORECASE)
+ self.assertEqual(hash(pattern2), hash(pattern1))
+ self.assertEqual(pattern2, pattern1)
+
+ # not equal: different pattern
+ re.purge()
+ pattern3 = re.compile('XYZ', re.IGNORECASE)
+ # Don't test hash(pattern3) != hash(pattern1) because there is no
+ # warranty that hash values are different
+ self.assertNotEqual(pattern3, pattern1)
+
+ # not equal: different flag (flags=0)
+ re.purge()
+ pattern4 = re.compile('abc')
+ self.assertNotEqual(pattern4, pattern1)
+
+ # only == and != comparison operators are supported
+ with self.assertRaises(TypeError):
+ pattern1 < pattern2
+
+ def test_pattern_compare_bytes(self):
+ pattern1 = re.compile(b'abc')
+
+ # equal: test bytes patterns
+ re.purge()
+ pattern2 = re.compile(b'abc')
+ self.assertEqual(hash(pattern2), hash(pattern1))
+ self.assertEqual(pattern2, pattern1)
+
+ # not equal: pattern of a different types (str vs bytes),
+ # comparison must not raise a BytesWarning
+ re.purge()
+ pattern3 = re.compile('abc')
+ with warnings.catch_warnings():
+ warnings.simplefilter('error', BytesWarning)
+ self.assertNotEqual(pattern3, pattern1)
+
class PatternReprTests(unittest.TestCase):
def check(self, pattern, expected):
diff --git a/Lib/test/test_readline.py b/Lib/test/test_readline.py
index 2c73df2..06a9149 100644
--- a/Lib/test/test_readline.py
+++ b/Lib/test/test_readline.py
@@ -121,6 +121,21 @@ class TestReadline(unittest.TestCase):
TERM='xterm-256color')
self.assertEqual(stdout, b'')
+ auto_history_script = """\
+import readline
+readline.set_auto_history({})
+input()
+print("History length:", readline.get_current_history_length())
+"""
+
+ def test_auto_history_enabled(self):
+ output = run_pty(self.auto_history_script.format(True))
+ self.assertIn(b"History length: 1\r\n", output)
+
+ def test_auto_history_disabled(self):
+ output = run_pty(self.auto_history_script.format(False))
+ self.assertIn(b"History length: 0\r\n", output)
+
def test_nonascii(self):
try:
readline.add_history("\xEB\xEF")
diff --git a/Lib/test/test_regrtest.py b/Lib/test/test_regrtest.py
index e2c2f2d..751df15 100644
--- a/Lib/test/test_regrtest.py
+++ b/Lib/test/test_regrtest.py
@@ -1,21 +1,48 @@
"""
Tests of regrtest.py.
+
+Note: test_regrtest cannot be run twice in parallel.
"""
-import argparse
+import contextlib
import faulthandler
-import getopt
+import io
import os.path
+import platform
+import re
+import subprocess
+import sys
+import sysconfig
+import tempfile
+import textwrap
import unittest
-from test import regrtest, support
+from test import libregrtest
+from test import support
-class ParseArgsTestCase(unittest.TestCase):
- """Test regrtest's argument parsing."""
+Py_DEBUG = hasattr(sys, 'getobjects')
+ROOT_DIR = os.path.join(os.path.dirname(__file__), '..', '..')
+ROOT_DIR = os.path.abspath(os.path.normpath(ROOT_DIR))
+
+TEST_INTERRUPTED = textwrap.dedent("""
+ from signal import SIGINT
+ try:
+ from _testcapi import raise_signal
+ raise_signal(SIGINT)
+ except ImportError:
+ import os
+ os.kill(os.getpid(), SIGINT)
+ """)
+
+
+class ParseArgsTestCase(unittest.TestCase):
+ """
+ Test regrtest's argument parsing, function _parse_args().
+ """
def checkError(self, args, msg):
with support.captured_stderr() as err, self.assertRaises(SystemExit):
- regrtest._parse_args(args)
+ libregrtest._parse_args(args)
self.assertIn(msg, err.getvalue())
def test_help(self):
@@ -23,82 +50,82 @@ class ParseArgsTestCase(unittest.TestCase):
with self.subTest(opt=opt):
with support.captured_stdout() as out, \
self.assertRaises(SystemExit):
- regrtest._parse_args([opt])
+ libregrtest._parse_args([opt])
self.assertIn('Run Python regression tests.', out.getvalue())
@unittest.skipUnless(hasattr(faulthandler, 'dump_traceback_later'),
"faulthandler.dump_traceback_later() required")
def test_timeout(self):
- ns = regrtest._parse_args(['--timeout', '4.2'])
+ ns = libregrtest._parse_args(['--timeout', '4.2'])
self.assertEqual(ns.timeout, 4.2)
self.checkError(['--timeout'], 'expected one argument')
self.checkError(['--timeout', 'foo'], 'invalid float value')
def test_wait(self):
- ns = regrtest._parse_args(['--wait'])
+ ns = libregrtest._parse_args(['--wait'])
self.assertTrue(ns.wait)
def test_slaveargs(self):
- ns = regrtest._parse_args(['--slaveargs', '[[], {}]'])
+ ns = libregrtest._parse_args(['--slaveargs', '[[], {}]'])
self.assertEqual(ns.slaveargs, '[[], {}]')
self.checkError(['--slaveargs'], 'expected one argument')
def test_start(self):
for opt in '-S', '--start':
with self.subTest(opt=opt):
- ns = regrtest._parse_args([opt, 'foo'])
+ ns = libregrtest._parse_args([opt, 'foo'])
self.assertEqual(ns.start, 'foo')
self.checkError([opt], 'expected one argument')
def test_verbose(self):
- ns = regrtest._parse_args(['-v'])
+ ns = libregrtest._parse_args(['-v'])
self.assertEqual(ns.verbose, 1)
- ns = regrtest._parse_args(['-vvv'])
+ ns = libregrtest._parse_args(['-vvv'])
self.assertEqual(ns.verbose, 3)
- ns = regrtest._parse_args(['--verbose'])
+ ns = libregrtest._parse_args(['--verbose'])
self.assertEqual(ns.verbose, 1)
- ns = regrtest._parse_args(['--verbose'] * 3)
+ ns = libregrtest._parse_args(['--verbose'] * 3)
self.assertEqual(ns.verbose, 3)
- ns = regrtest._parse_args([])
+ ns = libregrtest._parse_args([])
self.assertEqual(ns.verbose, 0)
def test_verbose2(self):
for opt in '-w', '--verbose2':
with self.subTest(opt=opt):
- ns = regrtest._parse_args([opt])
+ ns = libregrtest._parse_args([opt])
self.assertTrue(ns.verbose2)
def test_verbose3(self):
for opt in '-W', '--verbose3':
with self.subTest(opt=opt):
- ns = regrtest._parse_args([opt])
+ ns = libregrtest._parse_args([opt])
self.assertTrue(ns.verbose3)
def test_quiet(self):
for opt in '-q', '--quiet':
with self.subTest(opt=opt):
- ns = regrtest._parse_args([opt])
+ ns = libregrtest._parse_args([opt])
self.assertTrue(ns.quiet)
self.assertEqual(ns.verbose, 0)
def test_slow(self):
- for opt in '-o', '--slow':
+ for opt in '-o', '--slowest':
with self.subTest(opt=opt):
- ns = regrtest._parse_args([opt])
+ ns = libregrtest._parse_args([opt])
self.assertTrue(ns.print_slow)
def test_header(self):
- ns = regrtest._parse_args(['--header'])
+ ns = libregrtest._parse_args(['--header'])
self.assertTrue(ns.header)
def test_randomize(self):
for opt in '-r', '--randomize':
with self.subTest(opt=opt):
- ns = regrtest._parse_args([opt])
+ ns = libregrtest._parse_args([opt])
self.assertTrue(ns.randomize)
def test_randseed(self):
- ns = regrtest._parse_args(['--randseed', '12345'])
+ ns = libregrtest._parse_args(['--randseed', '12345'])
self.assertEqual(ns.random_seed, 12345)
self.assertTrue(ns.randomize)
self.checkError(['--randseed'], 'expected one argument')
@@ -107,7 +134,7 @@ class ParseArgsTestCase(unittest.TestCase):
def test_fromfile(self):
for opt in '-f', '--fromfile':
with self.subTest(opt=opt):
- ns = regrtest._parse_args([opt, 'foo'])
+ ns = libregrtest._parse_args([opt, 'foo'])
self.assertEqual(ns.fromfile, 'foo')
self.checkError([opt], 'expected one argument')
self.checkError([opt, 'foo', '-s'], "don't go together")
@@ -115,42 +142,42 @@ class ParseArgsTestCase(unittest.TestCase):
def test_exclude(self):
for opt in '-x', '--exclude':
with self.subTest(opt=opt):
- ns = regrtest._parse_args([opt])
+ ns = libregrtest._parse_args([opt])
self.assertTrue(ns.exclude)
def test_single(self):
for opt in '-s', '--single':
with self.subTest(opt=opt):
- ns = regrtest._parse_args([opt])
+ ns = libregrtest._parse_args([opt])
self.assertTrue(ns.single)
self.checkError([opt, '-f', 'foo'], "don't go together")
def test_match(self):
for opt in '-m', '--match':
with self.subTest(opt=opt):
- ns = regrtest._parse_args([opt, 'pattern'])
+ ns = libregrtest._parse_args([opt, 'pattern'])
self.assertEqual(ns.match_tests, 'pattern')
self.checkError([opt], 'expected one argument')
def test_failfast(self):
for opt in '-G', '--failfast':
with self.subTest(opt=opt):
- ns = regrtest._parse_args([opt, '-v'])
+ ns = libregrtest._parse_args([opt, '-v'])
self.assertTrue(ns.failfast)
- ns = regrtest._parse_args([opt, '-W'])
+ ns = libregrtest._parse_args([opt, '-W'])
self.assertTrue(ns.failfast)
self.checkError([opt], '-G/--failfast needs either -v or -W')
def test_use(self):
for opt in '-u', '--use':
with self.subTest(opt=opt):
- ns = regrtest._parse_args([opt, 'gui,network'])
+ ns = libregrtest._parse_args([opt, 'gui,network'])
self.assertEqual(ns.use_resources, ['gui', 'network'])
- ns = regrtest._parse_args([opt, 'gui,none,network'])
+ ns = libregrtest._parse_args([opt, 'gui,none,network'])
self.assertEqual(ns.use_resources, ['network'])
- expected = list(regrtest.RESOURCE_NAMES)
+ expected = list(libregrtest.RESOURCE_NAMES)
expected.remove('gui')
- ns = regrtest._parse_args([opt, 'all,-gui'])
+ ns = libregrtest._parse_args([opt, 'all,-gui'])
self.assertEqual(ns.use_resources, expected)
self.checkError([opt], 'expected one argument')
self.checkError([opt, 'foo'], 'invalid resource')
@@ -158,31 +185,31 @@ class ParseArgsTestCase(unittest.TestCase):
def test_memlimit(self):
for opt in '-M', '--memlimit':
with self.subTest(opt=opt):
- ns = regrtest._parse_args([opt, '4G'])
+ ns = libregrtest._parse_args([opt, '4G'])
self.assertEqual(ns.memlimit, '4G')
self.checkError([opt], 'expected one argument')
def test_testdir(self):
- ns = regrtest._parse_args(['--testdir', 'foo'])
+ ns = libregrtest._parse_args(['--testdir', 'foo'])
self.assertEqual(ns.testdir, os.path.join(support.SAVEDCWD, 'foo'))
self.checkError(['--testdir'], 'expected one argument')
def test_runleaks(self):
for opt in '-L', '--runleaks':
with self.subTest(opt=opt):
- ns = regrtest._parse_args([opt])
+ ns = libregrtest._parse_args([opt])
self.assertTrue(ns.runleaks)
def test_huntrleaks(self):
for opt in '-R', '--huntrleaks':
with self.subTest(opt=opt):
- ns = regrtest._parse_args([opt, ':'])
+ ns = libregrtest._parse_args([opt, ':'])
self.assertEqual(ns.huntrleaks, (5, 4, 'reflog.txt'))
- ns = regrtest._parse_args([opt, '6:'])
+ ns = libregrtest._parse_args([opt, '6:'])
self.assertEqual(ns.huntrleaks, (6, 4, 'reflog.txt'))
- ns = regrtest._parse_args([opt, ':3'])
+ ns = libregrtest._parse_args([opt, ':3'])
self.assertEqual(ns.huntrleaks, (5, 3, 'reflog.txt'))
- ns = regrtest._parse_args([opt, '6:3:leaks.log'])
+ ns = libregrtest._parse_args([opt, '6:3:leaks.log'])
self.assertEqual(ns.huntrleaks, (6, 3, 'leaks.log'))
self.checkError([opt], 'expected one argument')
self.checkError([opt, '6'],
@@ -193,27 +220,25 @@ class ParseArgsTestCase(unittest.TestCase):
def test_multiprocess(self):
for opt in '-j', '--multiprocess':
with self.subTest(opt=opt):
- ns = regrtest._parse_args([opt, '2'])
+ ns = libregrtest._parse_args([opt, '2'])
self.assertEqual(ns.use_mp, 2)
self.checkError([opt], 'expected one argument')
self.checkError([opt, 'foo'], 'invalid int value')
self.checkError([opt, '2', '-T'], "don't go together")
self.checkError([opt, '2', '-l'], "don't go together")
- self.checkError([opt, '2', '-M', '4G'], "don't go together")
self.checkError([opt, '0', '-T'], "don't go together")
self.checkError([opt, '0', '-l'], "don't go together")
- self.checkError([opt, '0', '-M', '4G'], "don't go together")
def test_coverage(self):
for opt in '-T', '--coverage':
with self.subTest(opt=opt):
- ns = regrtest._parse_args([opt])
+ ns = libregrtest._parse_args([opt])
self.assertTrue(ns.trace)
def test_coverdir(self):
for opt in '-D', '--coverdir':
with self.subTest(opt=opt):
- ns = regrtest._parse_args([opt, 'foo'])
+ ns = libregrtest._parse_args([opt, 'foo'])
self.assertEqual(ns.coverdir,
os.path.join(support.SAVEDCWD, 'foo'))
self.checkError([opt], 'expected one argument')
@@ -221,13 +246,13 @@ class ParseArgsTestCase(unittest.TestCase):
def test_nocoverdir(self):
for opt in '-N', '--nocoverdir':
with self.subTest(opt=opt):
- ns = regrtest._parse_args([opt])
+ ns = libregrtest._parse_args([opt])
self.assertIsNone(ns.coverdir)
def test_threshold(self):
for opt in '-t', '--threshold':
with self.subTest(opt=opt):
- ns = regrtest._parse_args([opt, '1000'])
+ ns = libregrtest._parse_args([opt, '1000'])
self.assertEqual(ns.threshold, 1000)
self.checkError([opt], 'expected one argument')
self.checkError([opt, 'foo'], 'invalid int value')
@@ -235,13 +260,16 @@ class ParseArgsTestCase(unittest.TestCase):
def test_nowindows(self):
for opt in '-n', '--nowindows':
with self.subTest(opt=opt):
- ns = regrtest._parse_args([opt])
+ with contextlib.redirect_stderr(io.StringIO()) as stderr:
+ ns = libregrtest._parse_args([opt])
self.assertTrue(ns.nowindows)
+ err = stderr.getvalue()
+ self.assertIn('the --nowindows (-n) option is deprecated', err)
def test_forever(self):
for opt in '-F', '--forever':
with self.subTest(opt=opt):
- ns = regrtest._parse_args([opt])
+ ns = libregrtest._parse_args([opt])
self.assertTrue(ns.forever)
@@ -249,32 +277,32 @@ class ParseArgsTestCase(unittest.TestCase):
self.checkError(['--xxx'], 'usage:')
def test_long_option__partial(self):
- ns = regrtest._parse_args(['--qui'])
+ ns = libregrtest._parse_args(['--qui'])
self.assertTrue(ns.quiet)
self.assertEqual(ns.verbose, 0)
def test_two_options(self):
- ns = regrtest._parse_args(['--quiet', '--exclude'])
+ ns = libregrtest._parse_args(['--quiet', '--exclude'])
self.assertTrue(ns.quiet)
self.assertEqual(ns.verbose, 0)
self.assertTrue(ns.exclude)
def test_option_with_empty_string_value(self):
- ns = regrtest._parse_args(['--start', ''])
+ ns = libregrtest._parse_args(['--start', ''])
self.assertEqual(ns.start, '')
def test_arg(self):
- ns = regrtest._parse_args(['foo'])
+ ns = libregrtest._parse_args(['foo'])
self.assertEqual(ns.args, ['foo'])
def test_option_and_arg(self):
- ns = regrtest._parse_args(['--quiet', 'foo'])
+ ns = libregrtest._parse_args(['--quiet', 'foo'])
self.assertTrue(ns.quiet)
self.assertEqual(ns.verbose, 0)
self.assertEqual(ns.args, ['foo'])
def test_arg_option_arg(self):
- ns = regrtest._parse_args(['test_unaryop', '-v', 'test_binop'])
+ ns = libregrtest._parse_args(['test_unaryop', '-v', 'test_binop'])
self.assertEqual(ns.verbose, 1)
self.assertEqual(ns.args, ['test_unaryop', 'test_binop'])
@@ -283,6 +311,496 @@ class ParseArgsTestCase(unittest.TestCase):
'unrecognized arguments: --unknown-option')
+class BaseTestCase(unittest.TestCase):
+ TEST_UNIQUE_ID = 1
+ TESTNAME_PREFIX = 'test_regrtest_'
+ TESTNAME_REGEX = r'test_[a-zA-Z0-9_]+'
+
+ def setUp(self):
+ self.testdir = os.path.realpath(os.path.dirname(__file__))
+
+ self.tmptestdir = tempfile.mkdtemp()
+ self.addCleanup(support.rmtree, self.tmptestdir)
+
+ def create_test(self, name=None, code=''):
+ if not name:
+ name = 'noop%s' % BaseTestCase.TEST_UNIQUE_ID
+ BaseTestCase.TEST_UNIQUE_ID += 1
+
+ # test_regrtest cannot be run twice in parallel because
+ # of setUp() and create_test()
+ name = self.TESTNAME_PREFIX + name
+ path = os.path.join(self.tmptestdir, name + '.py')
+
+ self.addCleanup(support.unlink, path)
+ # Use 'x' mode to ensure that we do not override existing tests
+ try:
+ with open(path, 'x', encoding='utf-8') as fp:
+ fp.write(code)
+ except PermissionError as exc:
+ if not sysconfig.is_python_build():
+ self.skipTest("cannot write %s: %s" % (path, exc))
+ raise
+ return name
+
+ def regex_search(self, regex, output):
+ match = re.search(regex, output, re.MULTILINE)
+ if not match:
+ self.fail("%r not found in %r" % (regex, output))
+ return match
+
+ def check_line(self, output, regex):
+ regex = re.compile(r'^' + regex, re.MULTILINE)
+ self.assertRegex(output, regex)
+
+ def parse_executed_tests(self, output):
+ regex = (r'^[0-9]+:[0-9]+:[0-9]+ \[ *[0-9]+(?:/ *[0-9]+)?\] (%s)'
+ % self.TESTNAME_REGEX)
+ parser = re.finditer(regex, output, re.MULTILINE)
+ return list(match.group(1) for match in parser)
+
+ def check_executed_tests(self, output, tests, skipped=(), failed=(),
+ omitted=(), randomize=False, interrupted=False):
+ if isinstance(tests, str):
+ tests = [tests]
+ if isinstance(skipped, str):
+ skipped = [skipped]
+ if isinstance(failed, str):
+ failed = [failed]
+ if isinstance(omitted, str):
+ omitted = [omitted]
+ ntest = len(tests)
+ nskipped = len(skipped)
+ nfailed = len(failed)
+ nomitted = len(omitted)
+
+ executed = self.parse_executed_tests(output)
+ if randomize:
+ self.assertEqual(set(executed), set(tests), output)
+ else:
+ self.assertEqual(executed, tests, output)
+
+ def plural(count):
+ return 's' if count != 1 else ''
+
+ def list_regex(line_format, tests):
+ count = len(tests)
+ names = ' '.join(sorted(tests))
+ regex = line_format % (count, plural(count))
+ regex = r'%s:\n %s$' % (regex, names)
+ return regex
+
+ if skipped:
+ regex = list_regex('%s test%s skipped', skipped)
+ self.check_line(output, regex)
+
+ if failed:
+ regex = list_regex('%s test%s failed', failed)
+ self.check_line(output, regex)
+
+ if omitted:
+ regex = list_regex('%s test%s omitted', omitted)
+ self.check_line(output, regex)
+
+ good = ntest - nskipped - nfailed - nomitted
+ if good:
+ regex = r'%s test%s OK\.$' % (good, plural(good))
+ if not skipped and not failed and good > 1:
+ regex = 'All %s' % regex
+ self.check_line(output, regex)
+
+ if interrupted:
+ self.check_line(output, 'Test suite interrupted by signal SIGINT.')
+
+ if nfailed:
+ result = 'FAILURE'
+ elif interrupted:
+ result = 'INTERRUPTED'
+ else:
+ result = 'SUCCESS'
+ self.check_line(output, 'Tests result: %s' % result)
+
+ def parse_random_seed(self, output):
+ match = self.regex_search(r'Using random seed ([0-9]+)', output)
+ randseed = int(match.group(1))
+ self.assertTrue(0 <= randseed <= 10000000, randseed)
+ return randseed
+
+ def run_command(self, args, input=None, exitcode=0, **kw):
+ if not input:
+ input = ''
+ if 'stderr' not in kw:
+ kw['stderr'] = subprocess.PIPE
+ proc = subprocess.run(args,
+ universal_newlines=True,
+ input=input,
+ stdout=subprocess.PIPE,
+ **kw)
+ if proc.returncode != exitcode:
+ msg = ("Command %s failed with exit code %s\n"
+ "\n"
+ "stdout:\n"
+ "---\n"
+ "%s\n"
+ "---\n"
+ % (str(args), proc.returncode, proc.stdout))
+ if proc.stderr:
+ msg += ("\n"
+ "stderr:\n"
+ "---\n"
+ "%s"
+ "---\n"
+ % proc.stderr)
+ self.fail(msg)
+ return proc
+
+
+ def run_python(self, args, **kw):
+ args = [sys.executable, '-X', 'faulthandler', '-I', *args]
+ proc = self.run_command(args, **kw)
+ return proc.stdout
+
+
+class ProgramsTestCase(BaseTestCase):
+ """
+ Test various ways to run the Python test suite. Use options close
+ to options used on the buildbot.
+ """
+
+ NTEST = 4
+
+ def setUp(self):
+ super().setUp()
+
+ # Create NTEST tests doing nothing
+ self.tests = [self.create_test() for index in range(self.NTEST)]
+
+ self.python_args = ['-Wd', '-E', '-bb']
+ self.regrtest_args = ['-uall', '-rwW',
+ '--testdir=%s' % self.tmptestdir]
+ if hasattr(faulthandler, 'dump_traceback_later'):
+ self.regrtest_args.extend(('--timeout', '3600', '-j4'))
+ if sys.platform == 'win32':
+ self.regrtest_args.append('-n')
+
+ def check_output(self, output):
+ self.parse_random_seed(output)
+ self.check_executed_tests(output, self.tests, randomize=True)
+
+ def run_tests(self, args):
+ output = self.run_python(args)
+ self.check_output(output)
+
+ def test_script_regrtest(self):
+ # Lib/test/regrtest.py
+ script = os.path.join(self.testdir, 'regrtest.py')
+
+ args = [*self.python_args, script, *self.regrtest_args, *self.tests]
+ self.run_tests(args)
+
+ def test_module_test(self):
+ # -m test
+ args = [*self.python_args, '-m', 'test',
+ *self.regrtest_args, *self.tests]
+ self.run_tests(args)
+
+ def test_module_regrtest(self):
+ # -m test.regrtest
+ args = [*self.python_args, '-m', 'test.regrtest',
+ *self.regrtest_args, *self.tests]
+ self.run_tests(args)
+
+ def test_module_autotest(self):
+ # -m test.autotest
+ args = [*self.python_args, '-m', 'test.autotest',
+ *self.regrtest_args, *self.tests]
+ self.run_tests(args)
+
+ def test_module_from_test_autotest(self):
+ # from test import autotest
+ code = 'from test import autotest'
+ args = [*self.python_args, '-c', code,
+ *self.regrtest_args, *self.tests]
+ self.run_tests(args)
+
+ def test_script_autotest(self):
+ # Lib/test/autotest.py
+ script = os.path.join(self.testdir, 'autotest.py')
+ args = [*self.python_args, script, *self.regrtest_args, *self.tests]
+ self.run_tests(args)
+
+ @unittest.skipUnless(sysconfig.is_python_build(),
+ 'run_tests.py script is not installed')
+ def test_tools_script_run_tests(self):
+ # Tools/scripts/run_tests.py
+ script = os.path.join(ROOT_DIR, 'Tools', 'scripts', 'run_tests.py')
+ args = [script, *self.regrtest_args, *self.tests]
+ self.run_tests(args)
+
+ def run_batch(self, *args):
+ proc = self.run_command(args)
+ self.check_output(proc.stdout)
+
+ @unittest.skipUnless(sysconfig.is_python_build(),
+ 'test.bat script is not installed')
+ @unittest.skipUnless(sys.platform == 'win32', 'Windows only')
+ def test_tools_buildbot_test(self):
+ # Tools\buildbot\test.bat
+ script = os.path.join(ROOT_DIR, 'Tools', 'buildbot', 'test.bat')
+ test_args = ['--testdir=%s' % self.tmptestdir]
+ if platform.architecture()[0] == '64bit':
+ test_args.append('-x64') # 64-bit build
+ if not Py_DEBUG:
+ test_args.append('+d') # Release build, use python.exe
+ self.run_batch(script, *test_args, *self.tests)
+
+ @unittest.skipUnless(sys.platform == 'win32', 'Windows only')
+ def test_pcbuild_rt(self):
+ # PCbuild\rt.bat
+ script = os.path.join(ROOT_DIR, r'PCbuild\rt.bat')
+ rt_args = ["-q"] # Quick, don't run tests twice
+ if platform.architecture()[0] == '64bit':
+ rt_args.append('-x64') # 64-bit build
+ if Py_DEBUG:
+ rt_args.append('-d') # Debug build, use python_d.exe
+ self.run_batch(script, *rt_args, *self.regrtest_args, *self.tests)
+
+
+class ArgsTestCase(BaseTestCase):
+ """
+ Test arguments of the Python test suite.
+ """
+
+ def run_tests(self, *testargs, **kw):
+ cmdargs = ['-m', 'test', '--testdir=%s' % self.tmptestdir, *testargs]
+ return self.run_python(cmdargs, **kw)
+
+ def test_failing_test(self):
+ # test a failing test
+ code = textwrap.dedent("""
+ import unittest
+
+ class FailingTest(unittest.TestCase):
+ def test_failing(self):
+ self.fail("bug")
+ """)
+ test_ok = self.create_test('ok')
+ test_failing = self.create_test('failing', code=code)
+ tests = [test_ok, test_failing]
+
+ output = self.run_tests(*tests, exitcode=1)
+ self.check_executed_tests(output, tests, failed=test_failing)
+
+ def test_resources(self):
+ # test -u command line option
+ tests = {}
+ for resource in ('audio', 'network'):
+ code = 'from test import support\nsupport.requires(%r)' % resource
+ tests[resource] = self.create_test(resource, code)
+ test_names = sorted(tests.values())
+
+ # -u all: 2 resources enabled
+ output = self.run_tests('-u', 'all', *test_names)
+ self.check_executed_tests(output, test_names)
+
+ # -u audio: 1 resource enabled
+ output = self.run_tests('-uaudio', *test_names)
+ self.check_executed_tests(output, test_names,
+ skipped=tests['network'])
+
+ # no option: 0 resources enabled
+ output = self.run_tests(*test_names)
+ self.check_executed_tests(output, test_names,
+ skipped=test_names)
+
+ def test_random(self):
+ # test -r and --randseed command line option
+ code = textwrap.dedent("""
+ import random
+ print("TESTRANDOM: %s" % random.randint(1, 1000))
+ """)
+ test = self.create_test('random', code)
+
+ # first run to get the output with the random seed
+ output = self.run_tests('-r', test)
+ randseed = self.parse_random_seed(output)
+ match = self.regex_search(r'TESTRANDOM: ([0-9]+)', output)
+ test_random = int(match.group(1))
+
+ # try to reproduce with the random seed
+ output = self.run_tests('-r', '--randseed=%s' % randseed, test)
+ randseed2 = self.parse_random_seed(output)
+ self.assertEqual(randseed2, randseed)
+
+ match = self.regex_search(r'TESTRANDOM: ([0-9]+)', output)
+ test_random2 = int(match.group(1))
+ self.assertEqual(test_random2, test_random)
+
+ def test_fromfile(self):
+ # test --fromfile
+ tests = [self.create_test() for index in range(5)]
+
+ # Write the list of files using a format similar to regrtest output:
+ # [1/2] test_1
+ # [2/2] test_2
+ filename = support.TESTFN
+ self.addCleanup(support.unlink, filename)
+
+ # test format '0:00:00 [2/7] test_opcodes -- test_grammar took 0 sec'
+ with open(filename, "w") as fp:
+ previous = None
+ for index, name in enumerate(tests, 1):
+ line = ("00:00:%02i [%s/%s] %s"
+ % (index, index, len(tests), name))
+ if previous:
+ line += " -- %s took 0 sec" % previous
+ print(line, file=fp)
+ previous = name
+
+ output = self.run_tests('--fromfile', filename)
+ self.check_executed_tests(output, tests)
+
+ # test format '[2/7] test_opcodes'
+ with open(filename, "w") as fp:
+ for index, name in enumerate(tests, 1):
+ print("[%s/%s] %s" % (index, len(tests), name), file=fp)
+
+ output = self.run_tests('--fromfile', filename)
+ self.check_executed_tests(output, tests)
+
+ # test format 'test_opcodes'
+ with open(filename, "w") as fp:
+ for name in tests:
+ print(name, file=fp)
+
+ output = self.run_tests('--fromfile', filename)
+ self.check_executed_tests(output, tests)
+
+ def test_interrupted(self):
+ code = TEST_INTERRUPTED
+ test = self.create_test('sigint', code=code)
+ output = self.run_tests(test, exitcode=1)
+ self.check_executed_tests(output, test, omitted=test,
+ interrupted=True)
+
+ def test_slowest(self):
+ # test --slowest
+ tests = [self.create_test() for index in range(3)]
+ output = self.run_tests("--slowest", *tests)
+ self.check_executed_tests(output, tests)
+ regex = ('10 slowest tests:\n'
+ '(?:- %s: .*\n){%s}'
+ % (self.TESTNAME_REGEX, len(tests)))
+ self.check_line(output, regex)
+
+ def test_slow_interrupted(self):
+ # Issue #25373: test --slowest with an interrupted test
+ code = TEST_INTERRUPTED
+ test = self.create_test("sigint", code=code)
+
+ try:
+ import threading
+ tests = (False, True)
+ except ImportError:
+ tests = (False,)
+ for multiprocessing in tests:
+ if multiprocessing:
+ args = ("--slowest", "-j2", test)
+ else:
+ args = ("--slowest", test)
+ output = self.run_tests(*args, exitcode=1)
+ self.check_executed_tests(output, test,
+ omitted=test, interrupted=True)
+
+ regex = ('10 slowest tests:\n')
+ self.check_line(output, regex)
+
+ def test_coverage(self):
+ # test --coverage
+ test = self.create_test('coverage')
+ output = self.run_tests("--coverage", test)
+ self.check_executed_tests(output, [test])
+ regex = (r'lines +cov% +module +\(path\)\n'
+ r'(?: *[0-9]+ *[0-9]{1,2}% *[^ ]+ +\([^)]+\)+)+')
+ self.check_line(output, regex)
+
+ def test_wait(self):
+ # test --wait
+ test = self.create_test('wait')
+ output = self.run_tests("--wait", test, input='key')
+ self.check_line(output, 'Press any key to continue')
+
+ def test_forever(self):
+ # test --forever
+ code = textwrap.dedent("""
+ import builtins
+ import unittest
+
+ class ForeverTester(unittest.TestCase):
+ def test_run(self):
+ # Store the state in the builtins module, because the test
+ # module is reload at each run
+ if 'RUN' in builtins.__dict__:
+ builtins.__dict__['RUN'] += 1
+ if builtins.__dict__['RUN'] >= 3:
+ self.fail("fail at the 3rd runs")
+ else:
+ builtins.__dict__['RUN'] = 1
+ """)
+ test = self.create_test('forever', code=code)
+ output = self.run_tests('--forever', test, exitcode=1)
+ self.check_executed_tests(output, [test]*3, failed=test)
+
+ @unittest.skipUnless(Py_DEBUG, 'need a debug build')
+ def test_huntrleaks_fd_leak(self):
+ # test --huntrleaks for file descriptor leak
+ code = textwrap.dedent("""
+ import os
+ import unittest
+
+ # Issue #25306: Disable popups and logs to stderr on assertion
+ # failures in MSCRT
+ try:
+ import msvcrt
+ msvcrt.CrtSetReportMode
+ except (ImportError, AttributeError):
+ # no Windows, o release build
+ pass
+ else:
+ for m in [msvcrt.CRT_WARN, msvcrt.CRT_ERROR, msvcrt.CRT_ASSERT]:
+ msvcrt.CrtSetReportMode(m, 0)
+
+ class FDLeakTest(unittest.TestCase):
+ def test_leak(self):
+ fd = os.open(__file__, os.O_RDONLY)
+ # bug: never cloes the file descriptor
+ """)
+ test = self.create_test('huntrleaks', code=code)
+
+ filename = 'reflog.txt'
+ self.addCleanup(support.unlink, filename)
+ output = self.run_tests('--huntrleaks', '3:3:', test,
+ exitcode=1,
+ stderr=subprocess.STDOUT)
+ self.check_executed_tests(output, [test], failed=test)
+
+ line = 'beginning 6 repetitions\n123456\n......\n'
+ self.check_line(output, re.escape(line))
+
+ line2 = '%s leaked [1, 1, 1] file descriptors, sum=3\n' % test
+ self.assertIn(line2, output)
+
+ with open(filename) as fp:
+ reflog = fp.read()
+ self.assertIn(line2, reflog)
+
+ def test_list_tests(self):
+ # test --list-tests
+ tests = [self.create_test() for i in range(5)]
+ output = self.run_tests('--list-tests', *tests)
+ self.assertEqual(output.rstrip().splitlines(),
+ tests)
+
if __name__ == '__main__':
unittest.main()
diff --git a/Lib/test/test_richcmp.py b/Lib/test/test_richcmp.py
index 1582caa..58729a9 100644
--- a/Lib/test/test_richcmp.py
+++ b/Lib/test/test_richcmp.py
@@ -253,6 +253,31 @@ class MiscTest(unittest.TestCase):
self.assertTrue(a != b)
self.assertTrue(a < b)
+ def test_exception_message(self):
+ class Spam:
+ pass
+
+ tests = [
+ (lambda: 42 < None, r"'<' .* of 'int' and 'NoneType'"),
+ (lambda: None < 42, r"'<' .* of 'NoneType' and 'int'"),
+ (lambda: 42 > None, r"'>' .* of 'int' and 'NoneType'"),
+ (lambda: "foo" < None, r"'<' .* of 'str' and 'NoneType'"),
+ (lambda: "foo" >= 666, r"'>=' .* of 'str' and 'int'"),
+ (lambda: 42 <= None, r"'<=' .* of 'int' and 'NoneType'"),
+ (lambda: 42 >= None, r"'>=' .* of 'int' and 'NoneType'"),
+ (lambda: 42 < [], r"'<' .* of 'int' and 'list'"),
+ (lambda: () > [], r"'>' .* of 'tuple' and 'list'"),
+ (lambda: None >= None, r"'>=' .* of 'NoneType' and 'NoneType'"),
+ (lambda: Spam() < 42, r"'<' .* of 'Spam' and 'int'"),
+ (lambda: 42 < Spam(), r"'<' .* of 'int' and 'Spam'"),
+ (lambda: Spam() <= Spam(), r"'<=' .* of 'Spam' and 'Spam'"),
+ ]
+ for i, test in enumerate(tests):
+ with self.subTest(test=i):
+ with self.assertRaisesRegex(TypeError, test[1]):
+ test[0]()
+
+
class DictTest(unittest.TestCase):
def test_dicts(self):
diff --git a/Lib/test/test_rlcompleter.py b/Lib/test/test_rlcompleter.py
index 853e773..0dc1080 100644
--- a/Lib/test/test_rlcompleter.py
+++ b/Lib/test/test_rlcompleter.py
@@ -1,11 +1,12 @@
import unittest
-import unittest.mock
+from unittest.mock import patch
import builtins
import rlcompleter
class CompleteMe:
""" Trivial class used in testing rlcompleter.Completer. """
spam = 1
+ _ham = 2
class TestRlcompleter(unittest.TestCase):
@@ -52,18 +53,32 @@ class TestRlcompleter(unittest.TestCase):
['str.{}('.format(x) for x in dir(str)
if x.startswith('s')])
self.assertEqual(self.stdcompleter.attr_matches('tuple.foospamegg'), [])
+ expected = sorted({'None.%s%s' % (x, '(' if x != '__doc__' else '')
+ for x in dir(None)})
+ self.assertEqual(self.stdcompleter.attr_matches('None.'), expected)
+ self.assertEqual(self.stdcompleter.attr_matches('None._'), expected)
+ self.assertEqual(self.stdcompleter.attr_matches('None.__'), expected)
# test with a customized namespace
self.assertEqual(self.completer.attr_matches('CompleteMe.sp'),
['CompleteMe.spam'])
self.assertEqual(self.completer.attr_matches('Completeme.egg'), [])
+ self.assertEqual(self.completer.attr_matches('CompleteMe.'),
+ ['CompleteMe.mro(', 'CompleteMe.spam'])
+ self.assertEqual(self.completer.attr_matches('CompleteMe._'),
+ ['CompleteMe._ham'])
+ matches = self.completer.attr_matches('CompleteMe.__')
+ for x in matches:
+ self.assertTrue(x.startswith('CompleteMe.__'), x)
+ self.assertIn('CompleteMe.__name__', matches)
+ self.assertIn('CompleteMe.__new__(', matches)
- CompleteMe.me = CompleteMe
- self.assertEqual(self.completer.attr_matches('CompleteMe.me.me.sp'),
- ['CompleteMe.me.me.spam'])
- self.assertEqual(self.completer.attr_matches('egg.s'),
- ['egg.{}('.format(x) for x in dir(str)
- if x.startswith('s')])
+ with patch.object(CompleteMe, "me", CompleteMe, create=True):
+ self.assertEqual(self.completer.attr_matches('CompleteMe.me.me.sp'),
+ ['CompleteMe.me.me.spam'])
+ self.assertEqual(self.completer.attr_matches('egg.s'),
+ ['egg.{}('.format(x) for x in dir(str)
+ if x.startswith('s')])
def test_excessive_getattr(self):
# Ensure getattr() is invoked no more than once per attribute
@@ -78,14 +93,27 @@ class TestRlcompleter(unittest.TestCase):
self.assertEqual(completer.complete('f.b', 0), 'f.bar')
self.assertEqual(f.calls, 1)
+ def test_uncreated_attr(self):
+ # Attributes like properties and slots should be completed even when
+ # they haven't been created on an instance
+ class Foo:
+ __slots__ = ("bar",)
+ completer = rlcompleter.Completer(dict(f=Foo()))
+ self.assertEqual(completer.complete('f.', 0), 'f.bar')
+
@unittest.mock.patch('rlcompleter._readline_available', False)
def test_complete(self):
completer = rlcompleter.Completer()
self.assertEqual(completer.complete('', 0), '\t')
- self.assertEqual(completer.complete('a', 0), 'and')
- self.assertEqual(completer.complete('a', 1), 'as')
- self.assertEqual(completer.complete('as', 2), 'assert')
- self.assertEqual(completer.complete('an', 0), 'and')
+ self.assertEqual(completer.complete('a', 0), 'and ')
+ self.assertEqual(completer.complete('a', 1), 'as ')
+ self.assertEqual(completer.complete('as', 2), 'assert ')
+ self.assertEqual(completer.complete('an', 0), 'and ')
+ self.assertEqual(completer.complete('pa', 0), 'pass')
+ self.assertEqual(completer.complete('Fa', 0), 'False')
+ self.assertEqual(completer.complete('el', 0), 'elif ')
+ self.assertEqual(completer.complete('el', 1), 'else')
+ self.assertEqual(completer.complete('tr', 0), 'try:')
def test_duplicate_globals(self):
namespace = {
@@ -98,9 +126,10 @@ class TestRlcompleter(unittest.TestCase):
completer = rlcompleter.Completer(namespace)
self.assertEqual(completer.complete('False', 0), 'False')
self.assertIsNone(completer.complete('False', 1)) # No duplicates
- self.assertEqual(completer.complete('assert', 0), 'assert')
+ # Space or colon added due to being a reserved keyword
+ self.assertEqual(completer.complete('assert', 0), 'assert ')
self.assertIsNone(completer.complete('assert', 1))
- self.assertEqual(completer.complete('try', 0), 'try')
+ self.assertEqual(completer.complete('try', 0), 'try:')
self.assertIsNone(completer.complete('try', 1))
# No opening bracket "(" because we overrode the built-in class
self.assertEqual(completer.complete('memoryview', 0), 'memoryview')
diff --git a/Lib/test/test_robotparser.py b/Lib/test/test_robotparser.py
index d01266f..0f64ba8 100644
--- a/Lib/test/test_robotparser.py
+++ b/Lib/test/test_robotparser.py
@@ -1,8 +1,8 @@
import io
+import os
import unittest
import urllib.robotparser
-from urllib.error import URLError, HTTPError
-from urllib.request import urlopen
+from collections import namedtuple
from test import support
from http.server import BaseHTTPRequestHandler, HTTPServer
try:
@@ -11,246 +11,239 @@ except ImportError:
threading = None
-class RobotTestCase(unittest.TestCase):
- def __init__(self, index=None, parser=None, url=None, good=None, agent=None):
- # workaround to make unittest discovery work (see #17066)
- if not isinstance(index, int):
- return
- unittest.TestCase.__init__(self)
- if good:
- self.str = "RobotTest(%d, good, %s)" % (index, url)
- else:
- self.str = "RobotTest(%d, bad, %s)" % (index, url)
- self.parser = parser
- self.url = url
- self.good = good
- self.agent = agent
-
- def runTest(self):
- if isinstance(self.url, tuple):
- agent, url = self.url
- else:
- url = self.url
- agent = self.agent
- if self.good:
- self.assertTrue(self.parser.can_fetch(agent, url))
- else:
- self.assertFalse(self.parser.can_fetch(agent, url))
-
- def __str__(self):
- return self.str
-
-tests = unittest.TestSuite()
-
-def RobotTest(index, robots_txt, good_urls, bad_urls,
- agent="test_robotparser"):
-
- lines = io.StringIO(robots_txt).readlines()
- parser = urllib.robotparser.RobotFileParser()
- parser.parse(lines)
- for url in good_urls:
- tests.addTest(RobotTestCase(index, parser, url, 1, agent))
- for url in bad_urls:
- tests.addTest(RobotTestCase(index, parser, url, 0, agent))
-
-# Examples from http://www.robotstxt.org/wc/norobots.html (fetched 2002)
-
-# 1.
-doc = """
+class BaseRobotTest:
+ robots_txt = ''
+ agent = 'test_robotparser'
+ good = []
+ bad = []
+
+ def setUp(self):
+ lines = io.StringIO(self.robots_txt).readlines()
+ self.parser = urllib.robotparser.RobotFileParser()
+ self.parser.parse(lines)
+
+ def get_agent_and_url(self, url):
+ if isinstance(url, tuple):
+ agent, url = url
+ return agent, url
+ return self.agent, url
+
+ def test_good_urls(self):
+ for url in self.good:
+ agent, url = self.get_agent_and_url(url)
+ with self.subTest(url=url, agent=agent):
+ self.assertTrue(self.parser.can_fetch(agent, url))
+
+ def test_bad_urls(self):
+ for url in self.bad:
+ agent, url = self.get_agent_and_url(url)
+ with self.subTest(url=url, agent=agent):
+ self.assertFalse(self.parser.can_fetch(agent, url))
+
+
+class UserAgentWildcardTest(BaseRobotTest, unittest.TestCase):
+ robots_txt = """\
User-agent: *
Disallow: /cyberworld/map/ # This is an infinite virtual URL space
Disallow: /tmp/ # these will soon disappear
Disallow: /foo.html
-"""
-
-good = ['/','/test.html']
-bad = ['/cyberworld/map/index.html','/tmp/xxx','/foo.html']
+ """
+ good = ['/', '/test.html']
+ bad = ['/cyberworld/map/index.html', '/tmp/xxx', '/foo.html']
-RobotTest(1, doc, good, bad)
-# 2.
-doc = """
+class CrawlDelayAndCustomAgentTest(BaseRobotTest, unittest.TestCase):
+ robots_txt = """\
# robots.txt for http://www.example.com/
User-agent: *
+Crawl-delay: 1
+Request-rate: 3/15
Disallow: /cyberworld/map/ # This is an infinite virtual URL space
# Cybermapper knows where to go.
User-agent: cybermapper
Disallow:
+ """
+ good = ['/', '/test.html', ('cybermapper', '/cyberworld/map/index.html')]
+ bad = ['/cyberworld/map/index.html']
-"""
-
-good = ['/','/test.html',('cybermapper','/cyberworld/map/index.html')]
-bad = ['/cyberworld/map/index.html']
-
-RobotTest(2, doc, good, bad)
-# 3.
-doc = """
+class RejectAllRobotsTest(BaseRobotTest, unittest.TestCase):
+ robots_txt = """\
# go away
User-agent: *
Disallow: /
-"""
-
-good = []
-bad = ['/cyberworld/map/index.html','/','/tmp/']
-
-RobotTest(3, doc, good, bad)
-
-# Examples from http://www.robotstxt.org/wc/norobots-rfc.html (fetched 2002)
-
-# 4.
-doc = """
+ """
+ good = []
+ bad = ['/cyberworld/map/index.html', '/', '/tmp/']
+
+
+class BaseRequestRateTest(BaseRobotTest):
+
+ def test_request_rate(self):
+ for url in self.good + self.bad:
+ agent, url = self.get_agent_and_url(url)
+ with self.subTest(url=url, agent=agent):
+ if self.crawl_delay:
+ self.assertEqual(
+ self.parser.crawl_delay(agent), self.crawl_delay
+ )
+ if self.request_rate:
+ self.assertEqual(
+ self.parser.request_rate(agent).requests,
+ self.request_rate.requests
+ )
+ self.assertEqual(
+ self.parser.request_rate(agent).seconds,
+ self.request_rate.seconds
+ )
+
+
+class CrawlDelayAndRequestRateTest(BaseRequestRateTest, unittest.TestCase):
+ robots_txt = """\
User-agent: figtree
+Crawl-delay: 3
+Request-rate: 9/30
Disallow: /tmp
Disallow: /a%3cd.html
Disallow: /a%2fb.html
Disallow: /%7ejoe/index.html
-"""
+ """
+ agent = 'figtree'
+ request_rate = namedtuple('req_rate', 'requests seconds')(9, 30)
+ crawl_delay = 3
+ good = [('figtree', '/foo.html')]
+ bad = ['/tmp', '/tmp.html', '/tmp/a.html', '/a%3cd.html', '/a%3Cd.html',
+ '/a%2fb.html', '/~joe/index.html']
+
-good = [] # XFAIL '/a/b.html'
-bad = ['/tmp','/tmp.html','/tmp/a.html',
- '/a%3cd.html','/a%3Cd.html','/a%2fb.html',
- '/~joe/index.html'
- ]
+class DifferentAgentTest(CrawlDelayAndRequestRateTest):
+ agent = 'FigTree Robot libwww-perl/5.04'
+ # these are not actually tested, but we still need to parse it
+ # in order to accommodate the input parameters
+ request_rate = None
+ crawl_delay = None
-RobotTest(4, doc, good, bad, 'figtree')
-RobotTest(5, doc, good, bad, 'FigTree Robot libwww-perl/5.04')
-# 6.
-doc = """
+class InvalidRequestRateTest(BaseRobotTest, unittest.TestCase):
+ robots_txt = """\
User-agent: *
Disallow: /tmp/
Disallow: /a%3Cd.html
Disallow: /a/b.html
Disallow: /%7ejoe/index.html
-"""
-
-good = ['/tmp',] # XFAIL: '/a%2fb.html'
-bad = ['/tmp/','/tmp/a.html',
- '/a%3cd.html','/a%3Cd.html',"/a/b.html",
- '/%7Ejoe/index.html']
-
-RobotTest(6, doc, good, bad)
-
-# From bug report #523041
-
-# 7.
-doc = """
+Crawl-delay: 3
+Request-rate: 9/banana
+ """
+ good = ['/tmp']
+ bad = ['/tmp/', '/tmp/a.html', '/a%3cd.html', '/a%3Cd.html', '/a/b.html',
+ '/%7Ejoe/index.html']
+ crawl_delay = 3
+
+
+class InvalidCrawlDelayTest(BaseRobotTest, unittest.TestCase):
+ # From bug report #523041
+ robots_txt = """\
User-Agent: *
Disallow: /.
-"""
-
-good = ['/foo.html']
-bad = [] # Bug report says "/" should be denied, but that is not in the RFC
+Crawl-delay: pears
+ """
+ good = ['/foo.html']
+ # bug report says "/" should be denied, but that is not in the RFC
+ bad = []
-RobotTest(7, doc, good, bad)
-# From Google: http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=40364
-
-# 8.
-doc = """
+class AnotherInvalidRequestRateTest(BaseRobotTest, unittest.TestCase):
+ # also test that Allow and Diasallow works well with each other
+ robots_txt = """\
User-agent: Googlebot
Allow: /folder1/myfile.html
Disallow: /folder1/
-"""
-
-good = ['/folder1/myfile.html']
-bad = ['/folder1/anotherfile.html']
-
-RobotTest(8, doc, good, bad, agent="Googlebot")
-
-# 9. This file is incorrect because "Googlebot" is a substring of
-# "Googlebot-Mobile", so test 10 works just like test 9.
-doc = """
+Request-rate: whale/banana
+ """
+ agent = 'Googlebot'
+ good = ['/folder1/myfile.html']
+ bad = ['/folder1/anotherfile.html']
+
+
+class UserAgentOrderingTest(BaseRobotTest, unittest.TestCase):
+ # the order of User-agent should be correct. note
+ # that this file is incorrect because "Googlebot" is a
+ # substring of "Googlebot-Mobile"
+ robots_txt = """\
User-agent: Googlebot
Disallow: /
User-agent: Googlebot-Mobile
Allow: /
-"""
-
-good = []
-bad = ['/something.jpg']
+ """
+ agent = 'Googlebot'
+ bad = ['/something.jpg']
-RobotTest(9, doc, good, bad, agent="Googlebot")
-good = []
-bad = ['/something.jpg']
+class UserAgentGoogleMobileTest(UserAgentOrderingTest):
+ agent = 'Googlebot-Mobile'
-RobotTest(10, doc, good, bad, agent="Googlebot-Mobile")
-
-# 11. Get the order correct.
-doc = """
-User-agent: Googlebot-Mobile
-Allow: /
-User-agent: Googlebot
-Disallow: /
-"""
-
-good = []
-bad = ['/something.jpg']
-
-RobotTest(11, doc, good, bad, agent="Googlebot")
-
-good = ['/something.jpg']
-bad = []
-
-RobotTest(12, doc, good, bad, agent="Googlebot-Mobile")
-
-
-# 13. Google also got the order wrong in #8. You need to specify the
-# URLs from more specific to more general.
-doc = """
+class GoogleURLOrderingTest(BaseRobotTest, unittest.TestCase):
+ # Google also got the order wrong. You need
+ # to specify the URLs from more specific to more general
+ robots_txt = """\
User-agent: Googlebot
Allow: /folder1/myfile.html
Disallow: /folder1/
-"""
-
-good = ['/folder1/myfile.html']
-bad = ['/folder1/anotherfile.html']
-
-RobotTest(13, doc, good, bad, agent="googlebot")
+ """
+ agent = 'googlebot'
+ good = ['/folder1/myfile.html']
+ bad = ['/folder1/anotherfile.html']
-# 14. For issue #6325 (query string support)
-doc = """
+class DisallowQueryStringTest(BaseRobotTest, unittest.TestCase):
+ # see issue #6325 for details
+ robots_txt = """\
User-agent: *
Disallow: /some/path?name=value
-"""
+ """
+ good = ['/some/path']
+ bad = ['/some/path?name=value']
-good = ['/some/path']
-bad = ['/some/path?name=value']
-RobotTest(14, doc, good, bad)
-
-# 15. For issue #4108 (obey first * entry)
-doc = """
+class UseFirstUserAgentWildcardTest(BaseRobotTest, unittest.TestCase):
+ # obey first * entry (#4108)
+ robots_txt = """\
User-agent: *
Disallow: /some/path
User-agent: *
Disallow: /another/path
-"""
-
-good = ['/another/path']
-bad = ['/some/path']
+ """
+ good = ['/another/path']
+ bad = ['/some/path']
-RobotTest(15, doc, good, bad)
-# 16. Empty query (issue #17403). Normalizing the url first.
-doc = """
+class EmptyQueryStringTest(BaseRobotTest, unittest.TestCase):
+ # normalize the URL first (#17403)
+ robots_txt = """\
User-agent: *
Allow: /some/path?
Disallow: /another/path?
-"""
+ """
+ good = ['/some/path?']
+ bad = ['/another/path?']
-good = ['/some/path?']
-bad = ['/another/path?']
-RobotTest(16, doc, good, bad)
+class DefaultEntryTest(BaseRequestRateTest, unittest.TestCase):
+ robots_txt = """\
+User-agent: *
+Crawl-delay: 1
+Request-rate: 3/15
+Disallow: /cyberworld/map/
+ """
+ request_rate = namedtuple('req_rate', 'requests seconds')(3, 15)
+ crawl_delay = 1
+ good = ['/', '/test.html']
+ bad = ['/cyberworld/map/index.html']
class RobotHandler(BaseHTTPRequestHandler):
@@ -283,9 +276,7 @@ class PasswordProtectedSiteTestCase(unittest.TestCase):
self.t.join()
self.server.server_close()
- def runTest(self):
- self.testPasswordProtectedSite()
-
+ @support.reap_threads
def testPasswordProtectedSite(self):
addr = self.server.server_address
url = 'http://' + support.HOST + ':' + str(addr[1])
@@ -295,26 +286,47 @@ class PasswordProtectedSiteTestCase(unittest.TestCase):
parser.read()
self.assertFalse(parser.can_fetch("*", robots_url))
- def __str__(self):
- return '%s' % self.__class__.__name__
class NetworkTestCase(unittest.TestCase):
- @unittest.skip('does not handle the gzip encoding delivered by pydotorg')
- def testPythonOrg(self):
+ base_url = 'http://www.pythontest.net/'
+ robots_txt = '{}elsewhere/robots.txt'.format(base_url)
+
+ @classmethod
+ def setUpClass(cls):
support.requires('network')
- with support.transient_internet('www.python.org'):
- parser = urllib.robotparser.RobotFileParser(
- "http://www.python.org/robots.txt")
- parser.read()
- self.assertTrue(
- parser.can_fetch("*", "http://www.python.org/robots.txt"))
-
-def load_tests(loader, suite, pattern):
- suite = unittest.makeSuite(NetworkTestCase)
- suite.addTest(tests)
- suite.addTest(PasswordProtectedSiteTestCase())
- return suite
+ with support.transient_internet(cls.base_url):
+ cls.parser = urllib.robotparser.RobotFileParser(cls.robots_txt)
+ cls.parser.read()
+
+ def url(self, path):
+ return '{}{}{}'.format(
+ self.base_url, path, '/' if not os.path.splitext(path)[1] else ''
+ )
+
+ def test_basic(self):
+ self.assertFalse(self.parser.disallow_all)
+ self.assertFalse(self.parser.allow_all)
+ self.assertGreater(self.parser.mtime(), 0)
+ self.assertFalse(self.parser.crawl_delay('*'))
+ self.assertFalse(self.parser.request_rate('*'))
+
+ def test_can_fetch(self):
+ self.assertTrue(self.parser.can_fetch('*', self.url('elsewhere')))
+ self.assertFalse(self.parser.can_fetch('Nutch', self.base_url))
+ self.assertFalse(self.parser.can_fetch('Nutch', self.url('brian')))
+ self.assertFalse(self.parser.can_fetch('Nutch', self.url('webstats')))
+ self.assertFalse(self.parser.can_fetch('*', self.url('webstats')))
+ self.assertTrue(self.parser.can_fetch('*', self.base_url))
+
+ def test_read_404(self):
+ parser = urllib.robotparser.RobotFileParser(self.url('i-robot.txt'))
+ parser.read()
+ self.assertTrue(parser.allow_all)
+ self.assertFalse(parser.disallow_all)
+ self.assertEqual(parser.mtime(), 0)
+ self.assertIsNone(parser.crawl_delay('*'))
+ self.assertIsNone(parser.request_rate('*'))
if __name__=='__main__':
unittest.main()
diff --git a/Lib/test/test_runpy.py b/Lib/test/test_runpy.py
index db55db7..02b4d62 100644
--- a/Lib/test/test_runpy.py
+++ b/Lib/test/test_runpy.py
@@ -577,13 +577,14 @@ from ..uncle.cousin import nephew
self.addCleanup(self._del_pkg, pkg_dir)
for depth in range(2, max_depth+1):
self._add_relative_modules(pkg_dir, "", depth)
- for finder, mod_name, ispkg in pkgutil.walk_packages([pkg_dir]):
- self.assertIsInstance(finder,
+ for moduleinfo in pkgutil.walk_packages([pkg_dir]):
+ self.assertIsInstance(moduleinfo, pkgutil.ModuleInfo)
+ self.assertIsInstance(moduleinfo.module_finder,
importlib.machinery.FileFinder)
- if ispkg:
- expected_packages.remove(mod_name)
+ if moduleinfo.ispkg:
+ expected_packages.remove(moduleinfo.name)
else:
- expected_modules.remove(mod_name)
+ expected_modules.remove(moduleinfo.name)
self.assertEqual(len(expected_packages), 0, expected_packages)
self.assertEqual(len(expected_modules), 0, expected_modules)
diff --git a/Lib/test/test_sched.py b/Lib/test/test_sched.py
index fe8e785..ebf8856 100644
--- a/Lib/test/test_sched.py
+++ b/Lib/test/test_sched.py
@@ -2,7 +2,6 @@ import queue
import sched
import time
import unittest
-from test import support
try:
import threading
except ImportError:
@@ -173,17 +172,23 @@ class TestCase(unittest.TestCase):
self.assertEqual(scheduler.queue, [e1, e2, e3, e4, e5])
def test_args_kwargs(self):
- flag = []
-
+ seq = []
def fun(*a, **b):
- flag.append(None)
- self.assertEqual(a, (1,2,3))
- self.assertEqual(b, {"foo":1})
+ seq.append((a, b))
+ now = time.time()
scheduler = sched.scheduler(time.time, time.sleep)
- z = scheduler.enterabs(0.01, 1, fun, argument=(1,2,3), kwargs={"foo":1})
+ scheduler.enterabs(now, 1, fun)
+ scheduler.enterabs(now, 1, fun, argument=(1, 2))
+ scheduler.enterabs(now, 1, fun, argument=('a', 'b'))
+ scheduler.enterabs(now, 1, fun, argument=(1, 2), kwargs={"foo": 3})
scheduler.run()
- self.assertEqual(flag, [None])
+ self.assertCountEqual(seq, [
+ ((), {}),
+ ((1, 2), {}),
+ (('a', 'b'), {}),
+ ((1, 2), {'foo': 3})
+ ])
def test_run_non_blocking(self):
l = []
diff --git a/Lib/test/test_secrets.py b/Lib/test/test_secrets.py
new file mode 100644
index 0000000..d31d07e
--- /dev/null
+++ b/Lib/test/test_secrets.py
@@ -0,0 +1,124 @@
+"""Test the secrets module.
+
+As most of the functions in secrets are thin wrappers around functions
+defined elsewhere, we don't need to test them exhaustively.
+"""
+
+
+import secrets
+import unittest
+import string
+
+
+# === Unit tests ===
+
+class Compare_Digest_Tests(unittest.TestCase):
+ """Test secrets.compare_digest function."""
+
+ def test_equal(self):
+ # Test compare_digest functionality with equal (byte/text) strings.
+ for s in ("a", "bcd", "xyz123"):
+ a = s*100
+ b = s*100
+ self.assertTrue(secrets.compare_digest(a, b))
+ self.assertTrue(secrets.compare_digest(a.encode('utf-8'), b.encode('utf-8')))
+
+ def test_unequal(self):
+ # Test compare_digest functionality with unequal (byte/text) strings.
+ self.assertFalse(secrets.compare_digest("abc", "abcd"))
+ self.assertFalse(secrets.compare_digest(b"abc", b"abcd"))
+ for s in ("x", "mn", "a1b2c3"):
+ a = s*100 + "q"
+ b = s*100 + "k"
+ self.assertFalse(secrets.compare_digest(a, b))
+ self.assertFalse(secrets.compare_digest(a.encode('utf-8'), b.encode('utf-8')))
+
+ def test_bad_types(self):
+ # Test that compare_digest raises with mixed types.
+ a = 'abcde'
+ b = a.encode('utf-8')
+ assert isinstance(a, str)
+ assert isinstance(b, bytes)
+ self.assertRaises(TypeError, secrets.compare_digest, a, b)
+ self.assertRaises(TypeError, secrets.compare_digest, b, a)
+
+ def test_bool(self):
+ # Test that compare_digest returns a bool.
+ self.assertIsInstance(secrets.compare_digest("abc", "abc"), bool)
+ self.assertIsInstance(secrets.compare_digest("abc", "xyz"), bool)
+
+
+class Random_Tests(unittest.TestCase):
+ """Test wrappers around SystemRandom methods."""
+
+ def test_randbits(self):
+ # Test randbits.
+ errmsg = "randbits(%d) returned %d"
+ for numbits in (3, 12, 30):
+ for i in range(6):
+ n = secrets.randbits(numbits)
+ self.assertTrue(0 <= n < 2**numbits, errmsg % (numbits, n))
+
+ def test_choice(self):
+ # Test choice.
+ items = [1, 2, 4, 8, 16, 32, 64]
+ for i in range(10):
+ self.assertTrue(secrets.choice(items) in items)
+
+ def test_randbelow(self):
+ # Test randbelow.
+ for i in range(2, 10):
+ self.assertIn(secrets.randbelow(i), range(i))
+ self.assertRaises(ValueError, secrets.randbelow, 0)
+ self.assertRaises(ValueError, secrets.randbelow, -1)
+
+
+class Token_Tests(unittest.TestCase):
+ """Test token functions."""
+
+ def test_token_defaults(self):
+ # Test that token_* functions handle default size correctly.
+ for func in (secrets.token_bytes, secrets.token_hex,
+ secrets.token_urlsafe):
+ with self.subTest(func=func):
+ name = func.__name__
+ try:
+ func()
+ except TypeError:
+ self.fail("%s cannot be called with no argument" % name)
+ try:
+ func(None)
+ except TypeError:
+ self.fail("%s cannot be called with None" % name)
+ size = secrets.DEFAULT_ENTROPY
+ self.assertEqual(len(secrets.token_bytes(None)), size)
+ self.assertEqual(len(secrets.token_hex(None)), 2*size)
+
+ def test_token_bytes(self):
+ # Test token_bytes.
+ for n in (1, 8, 17, 100):
+ with self.subTest(n=n):
+ self.assertIsInstance(secrets.token_bytes(n), bytes)
+ self.assertEqual(len(secrets.token_bytes(n)), n)
+
+ def test_token_hex(self):
+ # Test token_hex.
+ for n in (1, 12, 25, 90):
+ with self.subTest(n=n):
+ s = secrets.token_hex(n)
+ self.assertIsInstance(s, str)
+ self.assertEqual(len(s), 2*n)
+ self.assertTrue(all(c in string.hexdigits for c in s))
+
+ def test_token_urlsafe(self):
+ # Test token_urlsafe.
+ legal = string.ascii_letters + string.digits + '-_'
+ for n in (1, 11, 28, 76):
+ with self.subTest(n=n):
+ s = secrets.token_urlsafe(n)
+ self.assertIsInstance(s, str)
+ self.assertTrue(all(c in legal for c in s))
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/Lib/test/test_set.py b/Lib/test/test_set.py
index 7594303..0202981 100644
--- a/Lib/test/test_set.py
+++ b/Lib/test/test_set.py
@@ -6,10 +6,11 @@ import operator
import copy
import pickle
from random import randrange, shuffle
-import sys
import warnings
import collections
import collections.abc
+import itertools
+import string
class PassThru(Exception):
pass
@@ -729,6 +730,25 @@ class TestFrozenSet(TestJointOps, unittest.TestCase):
addhashvalue(hash(frozenset([e for e, m in elemmasks if m&i])))
self.assertEqual(len(hashvalues), 2**n)
+ def zf_range(n):
+ # https://en.wikipedia.org/wiki/Set-theoretic_definition_of_natural_numbers
+ nums = [frozenset()]
+ for i in range(n-1):
+ num = frozenset(nums)
+ nums.append(num)
+ return nums[:n]
+
+ def powerset(s):
+ for i in range(len(s)+1):
+ yield from map(frozenset, itertools.combinations(s, i))
+
+ for n in range(18):
+ t = 2 ** n
+ mask = t - 1
+ for nums in (range, zf_range):
+ u = len({h & mask for h in map(hash, powerset(nums(n)))})
+ self.assertGreater(4*u, t)
+
class FrozenSetSubclass(frozenset):
pass
diff --git a/Lib/test/test_shlex.py b/Lib/test/test_shlex.py
index 55b533d..fd35788 100644
--- a/Lib/test/test_shlex.py
+++ b/Lib/test/test_shlex.py
@@ -173,16 +173,124 @@ class ShlexTest(unittest.TestCase):
"%s: %s != %s" %
(self.data[i][0], l, self.data[i][1:]))
+ def testSyntaxSplitAmpersandAndPipe(self):
+ """Test handling of syntax splitting of &, |"""
+ # Could take these forms: &&, &, |&, ;&, ;;&
+ # of course, the same applies to | and ||
+ # these should all parse to the same output
+ for delimiter in ('&&', '&', '|&', ';&', ';;&',
+ '||', '|', '&|', ';|', ';;|'):
+ src = ['echo hi %s echo bye' % delimiter,
+ 'echo hi%secho bye' % delimiter]
+ ref = ['echo', 'hi', delimiter, 'echo', 'bye']
+ for ss in src:
+ s = shlex.shlex(ss, punctuation_chars=True)
+ result = list(s)
+ self.assertEqual(ref, result, "While splitting '%s'" % ss)
+
+ def testSyntaxSplitSemicolon(self):
+ """Test handling of syntax splitting of ;"""
+ # Could take these forms: ;, ;;, ;&, ;;&
+ # these should all parse to the same output
+ for delimiter in (';', ';;', ';&', ';;&'):
+ src = ['echo hi %s echo bye' % delimiter,
+ 'echo hi%s echo bye' % delimiter,
+ 'echo hi%secho bye' % delimiter]
+ ref = ['echo', 'hi', delimiter, 'echo', 'bye']
+ for ss in src:
+ s = shlex.shlex(ss, punctuation_chars=True)
+ result = list(s)
+ self.assertEqual(ref, result, "While splitting '%s'" % ss)
+
+ def testSyntaxSplitRedirect(self):
+ """Test handling of syntax splitting of >"""
+ # of course, the same applies to <, |
+ # these should all parse to the same output
+ for delimiter in ('<', '|'):
+ src = ['echo hi %s out' % delimiter,
+ 'echo hi%s out' % delimiter,
+ 'echo hi%sout' % delimiter]
+ ref = ['echo', 'hi', delimiter, 'out']
+ for ss in src:
+ s = shlex.shlex(ss, punctuation_chars=True)
+ result = list(s)
+ self.assertEqual(ref, result, "While splitting '%s'" % ss)
+
+ def testSyntaxSplitParen(self):
+ """Test handling of syntax splitting of ()"""
+ # these should all parse to the same output
+ src = ['( echo hi )',
+ '(echo hi)']
+ ref = ['(', 'echo', 'hi', ')']
+ for ss in src:
+ s = shlex.shlex(ss, punctuation_chars=True)
+ result = list(s)
+ self.assertEqual(ref, result, "While splitting '%s'" % ss)
+
+ def testSyntaxSplitCustom(self):
+ """Test handling of syntax splitting with custom chars"""
+ ref = ['~/a', '&', '&', 'b-c', '--color=auto', '||', 'd', '*.py?']
+ ss = "~/a && b-c --color=auto || d *.py?"
+ s = shlex.shlex(ss, punctuation_chars="|")
+ result = list(s)
+ self.assertEqual(ref, result, "While splitting '%s'" % ss)
+
+ def testTokenTypes(self):
+ """Test that tokens are split with types as expected."""
+ for source, expected in (
+ ('a && b || c',
+ [('a', 'a'), ('&&', 'c'), ('b', 'a'),
+ ('||', 'c'), ('c', 'a')]),
+ ):
+ s = shlex.shlex(source, punctuation_chars=True)
+ observed = []
+ while True:
+ t = s.get_token()
+ if t == s.eof:
+ break
+ if t[0] in s.punctuation_chars:
+ tt = 'c'
+ else:
+ tt = 'a'
+ observed.append((t, tt))
+ self.assertEqual(observed, expected)
+
+ def testPunctuationInWordChars(self):
+ """Test that any punctuation chars are removed from wordchars"""
+ s = shlex.shlex('a_b__c', punctuation_chars='_')
+ self.assertNotIn('_', s.wordchars)
+ self.assertEqual(list(s), ['a', '_', 'b', '__', 'c'])
+
+ def testPunctuationWithWhitespaceSplit(self):
+ """Test that with whitespace_split, behaviour is as expected"""
+ s = shlex.shlex('a && b || c', punctuation_chars='&')
+ # whitespace_split is False, so splitting will be based on
+ # punctuation_chars
+ self.assertEqual(list(s), ['a', '&&', 'b', '|', '|', 'c'])
+ s = shlex.shlex('a && b || c', punctuation_chars='&')
+ s.whitespace_split = True
+ # whitespace_split is True, so splitting will be based on
+ # white space
+ self.assertEqual(list(s), ['a', '&&', 'b', '||', 'c'])
+
+ def testPunctuationWithPosix(self):
+ """Test that punctuation_chars and posix behave correctly together."""
+ # see Issue #29132
+ s = shlex.shlex('f >"abc"', posix=True, punctuation_chars=True)
+ self.assertEqual(list(s), ['f', '>', 'abc'])
+ s = shlex.shlex('f >\\"abc\\"', posix=True, punctuation_chars=True)
+ self.assertEqual(list(s), ['f', '>', '"abc"'])
+
def testEmptyStringHandling(self):
"""Test that parsing of empty strings is correctly handled."""
# see Issue #21999
expected = ['', ')', 'abc']
-
- s = shlex.shlex("'')abc", posix=True)
- slist = list(s)
- self.assertEqual(slist, expected)
+ for punct in (False, True):
+ s = shlex.shlex("'')abc", posix=True, punctuation_chars=punct)
+ slist = list(s)
+ self.assertEqual(slist, expected)
expected = ["''", ')', 'abc']
- s = shlex.shlex("'')abc")
+ s = shlex.shlex("'')abc", punctuation_chars=True)
self.assertEqual(list(s), expected)
def testQuote(self):
diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py
index 709feeb..2ad3a21 100644
--- a/Lib/test/test_shutil.py
+++ b/Lib/test/test_shutil.py
@@ -23,7 +23,8 @@ import zipfile
import warnings
from test import support
-from test.support import TESTFN, check_warnings, captured_stdout
+from test.support import (TESTFN, check_warnings, captured_stdout,
+ android_not_root)
TESTFN2 = TESTFN + "2"
@@ -115,9 +116,7 @@ class TestShutil(unittest.TestCase):
write_file(os.path.join(victim, 'somefile'), 'foo')
victim = os.fsencode(victim)
self.assertIsInstance(victim, bytes)
- win = (os.name == 'nt')
- with self.assertWarns(DeprecationWarning) if win else ExitStack():
- shutil.rmtree(victim)
+ shutil.rmtree(victim)
@support.skip_unless_symlink
def test_rmtree_fails_on_symlink(self):
@@ -772,6 +771,7 @@ class TestShutil(unittest.TestCase):
@unittest.skipIf(os.name == 'nt', 'temporarily disabled on Windows')
@unittest.skipUnless(hasattr(os, 'link'), 'requires os.link')
+ @unittest.skipIf(android_not_root, "hard links not allowed, non root user")
def test_dont_copy_file_onto_link_to_itself(self):
# bug 851123.
os.mkdir(TESTFN)
@@ -824,6 +824,7 @@ class TestShutil(unittest.TestCase):
# Issue #3002: copyfile and copytree block indefinitely on named pipes
@unittest.skipUnless(hasattr(os, "mkfifo"), 'requires os.mkfifo()')
+ @unittest.skipIf(android_not_root, "mkfifo not allowed, non root user")
def test_copyfile_named_pipe(self):
os.mkfifo(TESTFN)
try:
@@ -834,6 +835,7 @@ class TestShutil(unittest.TestCase):
finally:
os.remove(TESTFN)
+ @unittest.skipIf(android_not_root, "mkfifo not allowed, non root user")
@unittest.skipUnless(hasattr(os, "mkfifo"), 'requires os.mkfifo()')
@support.skip_unless_symlink
def test_copytree_named_pipe(self):
@@ -1312,10 +1314,10 @@ class TestShutil(unittest.TestCase):
shutil.chown(filename)
with self.assertRaises(LookupError):
- shutil.chown(filename, user='non-exising username')
+ shutil.chown(filename, user='non-existing username')
with self.assertRaises(LookupError):
- shutil.chown(filename, group='non-exising groupname')
+ shutil.chown(filename, group='non-existing groupname')
with self.assertRaises(TypeError):
shutil.chown(filename, b'spam')
@@ -1864,7 +1866,8 @@ class TermsizeTests(unittest.TestCase):
"""
try:
size = subprocess.check_output(['stty', 'size']).decode().split()
- except (FileNotFoundError, subprocess.CalledProcessError):
+ except (FileNotFoundError, PermissionError,
+ subprocess.CalledProcessError):
self.skipTest("stty invocation failed")
expected = (int(size[1]), int(size[0])) # reversed order
diff --git a/Lib/test/test_signal.py b/Lib/test/test_signal.py
index 1b80ff0..ab42ed7 100644
--- a/Lib/test/test_signal.py
+++ b/Lib/test/test_signal.py
@@ -22,29 +22,6 @@ except ImportError:
_testcapi = None
-class HandlerBCalled(Exception):
- pass
-
-
-def exit_subprocess():
- """Use os._exit(0) to exit the current subprocess.
-
- Otherwise, the test catches the SystemExit and continues executing
- in parallel with the original test, so you wind up with an
- exponential number of tests running concurrently.
- """
- os._exit(0)
-
-
-def ignoring_eintr(__func, *args, **kwargs):
- try:
- return __func(*args, **kwargs)
- except OSError as e:
- if e.errno != errno.EINTR:
- raise
- return None
-
-
class GenericTests(unittest.TestCase):
@unittest.skipIf(threading is None, "test needs threading module")
@@ -63,145 +40,6 @@ class GenericTests(unittest.TestCase):
@unittest.skipIf(sys.platform == "win32", "Not valid on Windows")
-class InterProcessSignalTests(unittest.TestCase):
- MAX_DURATION = 20 # Entire test should last at most 20 sec.
-
- def setUp(self):
- self.using_gc = gc.isenabled()
- gc.disable()
-
- def tearDown(self):
- if self.using_gc:
- gc.enable()
-
- def format_frame(self, frame, limit=None):
- return ''.join(traceback.format_stack(frame, limit=limit))
-
- def handlerA(self, signum, frame):
- self.a_called = True
-
- def handlerB(self, signum, frame):
- self.b_called = True
- raise HandlerBCalled(signum, self.format_frame(frame))
-
- def wait(self, child):
- """Wait for child to finish, ignoring EINTR."""
- while True:
- try:
- child.wait()
- return
- except OSError as e:
- if e.errno != errno.EINTR:
- raise
-
- def run_test(self):
- # Install handlers. This function runs in a sub-process, so we
- # don't worry about re-setting the default handlers.
- signal.signal(signal.SIGHUP, self.handlerA)
- signal.signal(signal.SIGUSR1, self.handlerB)
- signal.signal(signal.SIGUSR2, signal.SIG_IGN)
- signal.signal(signal.SIGALRM, signal.default_int_handler)
-
- # Variables the signals will modify:
- self.a_called = False
- self.b_called = False
-
- # Let the sub-processes know who to send signals to.
- pid = os.getpid()
-
- child = ignoring_eintr(subprocess.Popen, ['kill', '-HUP', str(pid)])
- if child:
- self.wait(child)
- if not self.a_called:
- time.sleep(1) # Give the signal time to be delivered.
- self.assertTrue(self.a_called)
- self.assertFalse(self.b_called)
- self.a_called = False
-
- # Make sure the signal isn't delivered while the previous
- # Popen object is being destroyed, because __del__ swallows
- # exceptions.
- del child
- try:
- child = subprocess.Popen(['kill', '-USR1', str(pid)])
- # This wait should be interrupted by the signal's exception.
- self.wait(child)
- time.sleep(1) # Give the signal time to be delivered.
- self.fail('HandlerBCalled exception not raised')
- except HandlerBCalled:
- self.assertTrue(self.b_called)
- self.assertFalse(self.a_called)
-
- child = ignoring_eintr(subprocess.Popen, ['kill', '-USR2', str(pid)])
- if child:
- self.wait(child) # Nothing should happen.
-
- try:
- signal.alarm(1)
- # The race condition in pause doesn't matter in this case,
- # since alarm is going to raise a KeyboardException, which
- # will skip the call.
- signal.pause()
- # But if another signal arrives before the alarm, pause
- # may return early.
- time.sleep(1)
- except KeyboardInterrupt:
- pass
- except:
- self.fail("Some other exception woke us from pause: %s" %
- traceback.format_exc())
- else:
- self.fail("pause returned of its own accord, and the signal"
- " didn't arrive after another second.")
-
- # Issue 3864, unknown if this affects earlier versions of freebsd also
- @unittest.skipIf(sys.platform=='freebsd6',
- 'inter process signals not reliable (do not mix well with threading) '
- 'on freebsd6')
- def test_main(self):
- # This function spawns a child process to insulate the main
- # test-running process from all the signals. It then
- # communicates with that child process over a pipe and
- # re-raises information about any exceptions the child
- # raises. The real work happens in self.run_test().
- os_done_r, os_done_w = os.pipe()
- with closing(os.fdopen(os_done_r, 'rb')) as done_r, \
- closing(os.fdopen(os_done_w, 'wb')) as done_w:
- child = os.fork()
- if child == 0:
- # In the child process; run the test and report results
- # through the pipe.
- try:
- done_r.close()
- # Have to close done_w again here because
- # exit_subprocess() will skip the enclosing with block.
- with closing(done_w):
- try:
- self.run_test()
- except:
- pickle.dump(traceback.format_exc(), done_w)
- else:
- pickle.dump(None, done_w)
- except:
- print('Uh oh, raised from pickle.')
- traceback.print_exc()
- finally:
- exit_subprocess()
-
- done_w.close()
- # Block for up to MAX_DURATION seconds for the test to finish.
- r, w, x = select.select([done_r], [], [], self.MAX_DURATION)
- if done_r in r:
- tb = pickle.load(done_r)
- if tb:
- self.fail(tb)
- else:
- os.kill(child, signal.SIGKILL)
- self.fail('Test deadlocked after %d seconds.' %
- self.MAX_DURATION)
-
-
-@unittest.skipIf(sys.platform == "win32", "Not valid on Windows")
class PosixTests(unittest.TestCase):
def trivial_signal_handler(self, *args):
pass
@@ -224,6 +62,15 @@ class PosixTests(unittest.TestCase):
signal.signal(signal.SIGHUP, hup)
self.assertEqual(signal.getsignal(signal.SIGHUP), hup)
+ # Issue 3864, unknown if this affects earlier versions of freebsd also
+ @unittest.skipIf(sys.platform=='freebsd6',
+ 'inter process signals not reliable (do not mix well with threading) '
+ 'on freebsd6')
+ def test_interprocess_signal(self):
+ dirname = os.path.dirname(__file__)
+ script = os.path.join(dirname, 'signalinterproctester.py')
+ assert_python_ok(script)
+
@unittest.skipUnless(sys.platform == "win32", "Windows specific")
class WindowsSignalTests(unittest.TestCase):
diff --git a/Lib/test/test_site.py b/Lib/test/test_site.py
index da20a3d..d245fd5 100644
--- a/Lib/test/test_site.py
+++ b/Lib/test/test_site.py
@@ -14,6 +14,7 @@ import re
import encodings
import urllib.request
import urllib.error
+import shutil
import subprocess
import sysconfig
from copy import copy
@@ -75,7 +76,7 @@ class HelperFunctionsTests(unittest.TestCase):
def test_init_pathinfo(self):
dir_set = site._init_pathinfo()
for entry in [site.makepath(path)[1] for path in sys.path
- if path and os.path.isdir(path)]:
+ if path and os.path.exists(path)]:
self.assertIn(entry, dir_set,
"%s from sys.path not found in set returned "
"by _init_pathinfo(): %s" % (entry, dir_set))
@@ -138,10 +139,8 @@ class HelperFunctionsTests(unittest.TestCase):
re.escape(os.path.join(pth_dir, pth_fn)))
# XXX: ditto previous XXX comment.
self.assertRegex(err_out.getvalue(), 'Traceback')
- self.assertRegex(err_out.getvalue(), 'ImportError')
+ self.assertRegex(err_out.getvalue(), 'ModuleNotFoundError')
- @unittest.skipIf(sys.platform == "win32", "Windows does not raise an "
- "error for file paths containing null characters")
def test_addpackage_import_bad_pth_file(self):
# Issue 5258
pth_dir, pth_fn = self.make_pth("abc\x00def\n")
@@ -243,13 +242,14 @@ class HelperFunctionsTests(unittest.TestCase):
self.assertEqual(len(dirs), 2)
wanted = os.path.join('/Library',
sysconfig.get_config_var("PYTHONFRAMEWORK"),
- sys.version[:3],
+ '%d.%d' % sys.version_info[:2],
'site-packages')
self.assertEqual(dirs[1], wanted)
elif os.sep == '/':
# OS X non-framwework builds, Linux, FreeBSD, etc
self.assertEqual(len(dirs), 1)
- wanted = os.path.join('xoxo', 'lib', 'python' + sys.version[:3],
+ wanted = os.path.join('xoxo', 'lib',
+ 'python%d.%d' % sys.version_info[:2],
'site-packages')
self.assertEqual(dirs[0], wanted)
else:
@@ -446,10 +446,9 @@ class StartupImportTests(unittest.TestCase):
popen = subprocess.Popen([sys.executable, '-I', '-v', '-c',
'import sys; print(set(sys.modules))'],
stdout=subprocess.PIPE,
- stderr=subprocess.PIPE)
+ stderr=subprocess.PIPE,
+ encoding='utf-8')
stdout, stderr = popen.communicate()
- stdout = stdout.decode('utf-8')
- stderr = stderr.decode('utf-8')
modules = eval(stdout)
self.assertIn('site', modules)
@@ -469,7 +468,100 @@ class StartupImportTests(unittest.TestCase):
'heapq', 'itertools', 'keyword', 'operator',
'reprlib', 'types', 'weakref'
}.difference(sys.builtin_module_names)
- self.assertFalse(modules.intersection(collection_mods), stderr)
+ # http://bugs.python.org/issue28095
+ if sys.platform != 'darwin':
+ self.assertFalse(modules.intersection(collection_mods), stderr)
+
+ def test_startup_interactivehook(self):
+ r = subprocess.Popen([sys.executable, '-c',
+ 'import sys; sys.exit(hasattr(sys, "__interactivehook__"))']).wait()
+ self.assertTrue(r, "'__interactivehook__' not added by site")
+
+ def test_startup_interactivehook_isolated(self):
+ # issue28192 readline is not automatically enabled in isolated mode
+ r = subprocess.Popen([sys.executable, '-I', '-c',
+ 'import sys; sys.exit(hasattr(sys, "__interactivehook__"))']).wait()
+ self.assertFalse(r, "'__interactivehook__' added in isolated mode")
+
+ def test_startup_interactivehook_isolated_explicit(self):
+ # issue28192 readline can be explicitly enabled in isolated mode
+ r = subprocess.Popen([sys.executable, '-I', '-c',
+ 'import site, sys; site.enablerlcompleter(); sys.exit(hasattr(sys, "__interactivehook__"))']).wait()
+ self.assertTrue(r, "'__interactivehook__' not added by enablerlcompleter()")
+
+ @classmethod
+ def _create_underpth_exe(self, lines):
+ exe_file = os.path.join(os.getenv('TEMP'), os.path.split(sys.executable)[1])
+ shutil.copy(sys.executable, exe_file)
+
+ _pth_file = os.path.splitext(exe_file)[0] + '._pth'
+ try:
+ with open(_pth_file, 'w') as f:
+ for line in lines:
+ print(line, file=f)
+ return exe_file
+ except:
+ os.unlink(_pth_file)
+ os.unlink(exe_file)
+ raise
+
+ @classmethod
+ def _cleanup_underpth_exe(self, exe_file):
+ _pth_file = os.path.splitext(exe_file)[0] + '._pth'
+ os.unlink(_pth_file)
+ os.unlink(exe_file)
+
+ @unittest.skipUnless(sys.platform == 'win32', "only supported on Windows")
+ def test_underpth_nosite_file(self):
+ libpath = os.path.dirname(os.path.dirname(encodings.__file__))
+ exe_prefix = os.path.dirname(sys.executable)
+ exe_file = self._create_underpth_exe([
+ 'fake-path-name',
+ *[libpath for _ in range(200)],
+ '# comment',
+ 'import site'
+ ])
+
+ try:
+ env = os.environ.copy()
+ env['PYTHONPATH'] = 'from-env'
+ env['PATH'] = '{};{}'.format(exe_prefix, os.getenv('PATH'))
+ rc = subprocess.call([exe_file, '-c',
+ 'import sys; sys.exit(sys.flags.no_site and '
+ 'len(sys.path) > 200 and '
+ '%r in sys.path and %r in sys.path and %r not in sys.path)' % (
+ os.path.join(sys.prefix, 'fake-path-name'),
+ libpath,
+ os.path.join(sys.prefix, 'from-env'),
+ )], env=env)
+ finally:
+ self._cleanup_underpth_exe(exe_file)
+ self.assertEqual(rc, 0)
+
+ @unittest.skipUnless(sys.platform == 'win32', "only supported on Windows")
+ def test_underpth_file(self):
+ libpath = os.path.dirname(os.path.dirname(encodings.__file__))
+ exe_prefix = os.path.dirname(sys.executable)
+ exe_file = self._create_underpth_exe([
+ 'fake-path-name',
+ *[libpath for _ in range(200)],
+ '# comment',
+ 'import site'
+ ])
+ try:
+ env = os.environ.copy()
+ env['PYTHONPATH'] = 'from-env'
+ env['PATH'] = '{};{}'.format(exe_prefix, os.getenv('PATH'))
+ rc = subprocess.call([exe_file, '-c',
+ 'import sys; sys.exit(not sys.flags.no_site and '
+ '%r in sys.path and %r in sys.path and %r not in sys.path)' % (
+ os.path.join(sys.prefix, 'fake-path-name'),
+ libpath,
+ os.path.join(sys.prefix, 'from-env'),
+ )], env=env)
+ finally:
+ self._cleanup_underpth_exe(exe_file)
+ self.assertEqual(rc, 0)
if __name__ == "__main__":
diff --git a/Lib/test/test_smtpd.py b/Lib/test/test_smtpd.py
index 88dbfdf..3eebe94 100644
--- a/Lib/test/test_smtpd.py
+++ b/Lib/test/test_smtpd.py
@@ -53,10 +53,6 @@ class SMTPDServerTest(unittest.TestCase):
write_line(b'DATA')
self.assertRaises(NotImplementedError, write_line, b'spam\r\n.\r\n')
- def test_decode_data_default_warns(self):
- with self.assertWarns(DeprecationWarning):
- smtpd.SMTPServer((support.HOST, 0), ('b', 0))
-
def test_decode_data_and_enable_SMTPUTF8_raises(self):
self.assertRaises(
ValueError,
@@ -108,10 +104,9 @@ class DebuggingServerTest(unittest.TestCase):
"""))
def test_process_message_with_decode_data_false(self):
- server = smtpd.DebuggingServer((support.HOST, 0), ('b', 0),
- decode_data=False)
+ server = smtpd.DebuggingServer((support.HOST, 0), ('b', 0))
conn, addr = server.accept()
- channel = smtpd.SMTPChannel(server, conn, addr, decode_data=False)
+ channel = smtpd.SMTPChannel(server, conn, addr)
with support.captured_stdout() as s:
self.send_data(channel, b'From: test\n\nh\xc3\xa9llo\xff\n')
stdout = s.getvalue()
@@ -175,13 +170,11 @@ class TestFamilyDetection(unittest.TestCase):
@unittest.skipUnless(support.IPV6_ENABLED, "IPv6 not enabled")
def test_socket_uses_IPv6(self):
- server = smtpd.SMTPServer((support.HOSTv6, 0), (support.HOST, 0),
- decode_data=False)
+ server = smtpd.SMTPServer((support.HOSTv6, 0), (support.HOST, 0))
self.assertEqual(server.socket.family, socket.AF_INET6)
def test_socket_uses_IPv4(self):
- server = smtpd.SMTPServer((support.HOST, 0), (support.HOSTv6, 0),
- decode_data=False)
+ server = smtpd.SMTPServer((support.HOST, 0), (support.HOSTv6, 0))
self.assertEqual(server.socket.family, socket.AF_INET)
@@ -204,18 +197,18 @@ class TestRcptOptionParsing(unittest.TestCase):
channel.handle_read()
def test_params_rejected(self):
- server = DummyServer((support.HOST, 0), ('b', 0), decode_data=False)
+ server = DummyServer((support.HOST, 0), ('b', 0))
conn, addr = server.accept()
- channel = smtpd.SMTPChannel(server, conn, addr, decode_data=False)
+ channel = smtpd.SMTPChannel(server, conn, addr)
self.write_line(channel, b'EHLO example')
self.write_line(channel, b'MAIL from: <foo@example.com> size=20')
self.write_line(channel, b'RCPT to: <foo@example.com> foo=bar')
self.assertEqual(channel.socket.last, self.error_response)
def test_nothing_accepted(self):
- server = DummyServer((support.HOST, 0), ('b', 0), decode_data=False)
+ server = DummyServer((support.HOST, 0), ('b', 0))
conn, addr = server.accept()
- channel = smtpd.SMTPChannel(server, conn, addr, decode_data=False)
+ channel = smtpd.SMTPChannel(server, conn, addr)
self.write_line(channel, b'EHLO example')
self.write_line(channel, b'MAIL from: <foo@example.com> size=20')
self.write_line(channel, b'RCPT to: <foo@example.com>')
@@ -257,9 +250,9 @@ class TestMailOptionParsing(unittest.TestCase):
self.assertEqual(channel.socket.last, b'250 OK\r\n')
def test_with_decode_data_false(self):
- server = DummyServer((support.HOST, 0), ('b', 0), decode_data=False)
+ server = DummyServer((support.HOST, 0), ('b', 0))
conn, addr = server.accept()
- channel = smtpd.SMTPChannel(server, conn, addr, decode_data=False)
+ channel = smtpd.SMTPChannel(server, conn, addr)
self.write_line(channel, b'EHLO example')
for line in [
b'MAIL from: <foo@example.com> size=20 SMTPUTF8',
@@ -765,13 +758,6 @@ class SMTPDChannelTest(unittest.TestCase):
with support.check_warnings(('', DeprecationWarning)):
self.channel._SMTPChannel__addr = 'spam'
- def test_decode_data_default_warning(self):
- with self.assertWarns(DeprecationWarning):
- server = DummyServer((support.HOST, 0), ('b', 0))
- conn, addr = self.server.accept()
- with self.assertWarns(DeprecationWarning):
- smtpd.SMTPChannel(server, conn, addr)
-
@unittest.skipUnless(support.IPV6_ENABLED, "IPv6 not enabled")
class SMTPDChannelIPv6Test(SMTPDChannelTest):
def setUp(self):
@@ -845,12 +831,9 @@ class SMTPDChannelWithDecodeDataFalse(unittest.TestCase):
smtpd.socket = asyncore.socket = mock_socket
self.old_debugstream = smtpd.DEBUGSTREAM
self.debug = smtpd.DEBUGSTREAM = io.StringIO()
- self.server = DummyServer((support.HOST, 0), ('b', 0),
- decode_data=False)
+ self.server = DummyServer((support.HOST, 0), ('b', 0))
conn, addr = self.server.accept()
- # Set decode_data to False
- self.channel = smtpd.SMTPChannel(self.server, conn, addr,
- decode_data=False)
+ self.channel = smtpd.SMTPChannel(self.server, conn, addr)
def tearDown(self):
asyncore.close_all()
@@ -1015,5 +998,16 @@ class SMTPDChannelTestWithEnableSMTPUTF8True(unittest.TestCase):
self.write_line(b'test\r\n.')
self.assertEqual(self.channel.socket.last[0:3], b'250')
+
+class MiscTestCase(unittest.TestCase):
+ def test__all__(self):
+ blacklist = {
+ "program", "Devnull", "DEBUGSTREAM", "NEWLINE", "COMMASPACE",
+ "DATA_SIZE_DEFAULT", "usage", "Options", "parseargs",
+
+ }
+ support.check__all__(self, smtpd, blacklist=blacklist)
+
+
if __name__ == "__main__":
unittest.main()
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
index 70c03f9..97dc3cd 100644
--- a/Lib/test/test_socket.py
+++ b/Lib/test/test_socket.py
@@ -13,7 +13,6 @@ import queue
import sys
import os
import array
-import platform
import contextlib
from weakref import proxy
import signal
@@ -66,10 +65,22 @@ def _have_socket_rds():
s.close()
return True
+def _have_socket_alg():
+ """Check whether AF_ALG sockets are supported on this host."""
+ try:
+ s = socket.socket(socket.AF_ALG, socket.SOCK_SEQPACKET, 0)
+ except (AttributeError, OSError):
+ return False
+ else:
+ s.close()
+ return True
+
HAVE_SOCKET_CAN = _have_socket_can()
HAVE_SOCKET_RDS = _have_socket_rds()
+HAVE_SOCKET_ALG = _have_socket_alg()
+
# Size in bytes of the int type
SIZEOF_INT = array.array("i").itemsize
@@ -267,8 +278,14 @@ class ThreadableTest:
def clientRun(self, test_func):
self.server_ready.wait()
- self.clientSetUp()
- self.client_ready.set()
+ try:
+ self.clientSetUp()
+ except BaseException as e:
+ self.queue.put(e)
+ self.clientTearDown()
+ return
+ finally:
+ self.client_ready.set()
if self.server_crashed:
self.clientTearDown()
return
@@ -509,8 +526,11 @@ class ConnectedStreamTestMixin(SocketListeningTestMixin,
self.serv_conn = self.cli
def clientTearDown(self):
- self.serv_conn.close()
- self.serv_conn = None
+ try:
+ self.serv_conn.close()
+ self.serv_conn = None
+ except AttributeError:
+ pass
super().clientTearDown()
@@ -529,7 +549,7 @@ class UnixSocketTestBase(SocketTestBase):
def bindSock(self, sock):
path = tempfile.mktemp(dir=self.dir_path)
- sock.bind(path)
+ support.bind_unix_socket(sock, path)
self.addCleanup(support.unlink, path)
class UnixStreamBase(UnixSocketTestBase):
@@ -1161,6 +1181,17 @@ class GeneralModuleTests(unittest.TestCase):
sock.close()
self.assertRaises(OSError, sock.send, b"spam")
+ def testCloseException(self):
+ sock = socket.socket()
+ socket.socket(fileno=sock.fileno()).close()
+ try:
+ sock.close()
+ except OSError as err:
+ # Winsock apparently raises ENOTSOCK
+ self.assertIn(err.errno, (errno.EBADF, errno.ENOTSOCK))
+ else:
+ self.fail("close() should raise EBADF/ENOTSOCK")
+
def testNewAttributes(self):
# testing .family, .type and .protocol
@@ -1207,6 +1238,22 @@ class GeneralModuleTests(unittest.TestCase):
self.assertRaises(ValueError, s.ioctl, -1, None)
s.ioctl(socket.SIO_KEEPALIVE_VALS, (1, 100, 100))
+ @unittest.skipUnless(os.name == "nt", "Windows specific")
+ @unittest.skipUnless(hasattr(socket, 'SIO_LOOPBACK_FAST_PATH'),
+ 'Loopback fast path support required for this test')
+ def test_sio_loopback_fast_path(self):
+ s = socket.socket()
+ self.addCleanup(s.close)
+ try:
+ s.ioctl(socket.SIO_LOOPBACK_FAST_PATH, True)
+ except OSError as exc:
+ WSAEOPNOTSUPP = 10045
+ if exc.winerror == WSAEOPNOTSUPP:
+ self.skipTest("SIO_LOOPBACK_FAST_PATH is defined but "
+ "doesn't implemented in this Windows version")
+ raise
+ self.assertRaises(TypeError, s.ioctl, socket.SIO_LOOPBACK_FAST_PATH, None)
+
def testGetaddrinfo(self):
try:
socket.getaddrinfo('localhost', 80)
@@ -2843,6 +2890,7 @@ class SCMRightsTest(SendrecvmsgServerTimeoutBase):
nbytes = self.sendmsgToServer([msg])
self.assertEqual(nbytes, len(msg))
+ @unittest.skipIf(sys.platform == "darwin", "see issue #24725")
def testFDPassEmpty(self):
# Try to pass an empty FD array. Can receive either no array
# or an empty array.
@@ -4514,6 +4562,19 @@ class TestExceptions(unittest.TestCase):
self.assertTrue(issubclass(socket.gaierror, OSError))
self.assertTrue(issubclass(socket.timeout, OSError))
+ def test_setblocking_invalidfd(self):
+ # Regression test for issue #28471
+
+ sock0 = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
+ sock = socket.socket(
+ socket.AF_INET, socket.SOCK_STREAM, 0, sock0.fileno())
+ sock0.close()
+ self.addCleanup(sock.detach)
+
+ with self.assertRaises(OSError):
+ sock.setblocking(False)
+
+
@unittest.skipUnless(sys.platform == 'linux', 'Linux specific test')
class TestLinuxAbstractNamespace(unittest.TestCase):
@@ -4579,7 +4640,7 @@ class TestUnixDomain(unittest.TestCase):
def bind(self, sock, path):
# Bind the socket
try:
- sock.bind(path)
+ support.bind_unix_socket(sock, path)
except OSError as e:
if str(e) == "AF_UNIX path too long":
self.skipTest(
@@ -5325,6 +5386,183 @@ class SendfileUsingSendfileTest(SendfileUsingSendTest):
return getattr(sock, "_sendfile_use_sendfile")
+@unittest.skipUnless(HAVE_SOCKET_ALG, 'AF_ALG required')
+class LinuxKernelCryptoAPI(unittest.TestCase):
+ # tests for AF_ALG
+ def create_alg(self, typ, name):
+ sock = socket.socket(socket.AF_ALG, socket.SOCK_SEQPACKET, 0)
+ try:
+ sock.bind((typ, name))
+ except FileNotFoundError as e:
+ # type / algorithm is not available
+ sock.close()
+ raise unittest.SkipTest(str(e), typ, name)
+ else:
+ return sock
+
+ def test_sha256(self):
+ expected = bytes.fromhex("ba7816bf8f01cfea414140de5dae2223b00361a396"
+ "177a9cb410ff61f20015ad")
+ with self.create_alg('hash', 'sha256') as algo:
+ op, _ = algo.accept()
+ with op:
+ op.sendall(b"abc")
+ self.assertEqual(op.recv(512), expected)
+
+ op, _ = algo.accept()
+ with op:
+ op.send(b'a', socket.MSG_MORE)
+ op.send(b'b', socket.MSG_MORE)
+ op.send(b'c', socket.MSG_MORE)
+ op.send(b'')
+ self.assertEqual(op.recv(512), expected)
+
+ def test_hmac_sha1(self):
+ expected = bytes.fromhex("effcdf6ae5eb2fa2d27416d5f184df9c259a7c79")
+ with self.create_alg('hash', 'hmac(sha1)') as algo:
+ algo.setsockopt(socket.SOL_ALG, socket.ALG_SET_KEY, b"Jefe")
+ op, _ = algo.accept()
+ with op:
+ op.sendall(b"what do ya want for nothing?")
+ self.assertEqual(op.recv(512), expected)
+
+ # Although it should work with 3.19 and newer the test blocks on
+ # Ubuntu 15.10 with Kernel 4.2.0-19.
+ @support.requires_linux_version(4, 3)
+ def test_aes_cbc(self):
+ key = bytes.fromhex('06a9214036b8a15b512e03d534120006')
+ iv = bytes.fromhex('3dafba429d9eb430b422da802c9fac41')
+ msg = b"Single block msg"
+ ciphertext = bytes.fromhex('e353779c1079aeb82708942dbe77181a')
+ msglen = len(msg)
+ with self.create_alg('skcipher', 'cbc(aes)') as algo:
+ algo.setsockopt(socket.SOL_ALG, socket.ALG_SET_KEY, key)
+ op, _ = algo.accept()
+ with op:
+ op.sendmsg_afalg(op=socket.ALG_OP_ENCRYPT, iv=iv,
+ flags=socket.MSG_MORE)
+ op.sendall(msg)
+ self.assertEqual(op.recv(msglen), ciphertext)
+
+ op, _ = algo.accept()
+ with op:
+ op.sendmsg_afalg([ciphertext],
+ op=socket.ALG_OP_DECRYPT, iv=iv)
+ self.assertEqual(op.recv(msglen), msg)
+
+ # long message
+ multiplier = 1024
+ longmsg = [msg] * multiplier
+ op, _ = algo.accept()
+ with op:
+ op.sendmsg_afalg(longmsg,
+ op=socket.ALG_OP_ENCRYPT, iv=iv)
+ enc = op.recv(msglen * multiplier)
+ self.assertEqual(len(enc), msglen * multiplier)
+ self.assertTrue(enc[:msglen], ciphertext)
+
+ op, _ = algo.accept()
+ with op:
+ op.sendmsg_afalg([enc],
+ op=socket.ALG_OP_DECRYPT, iv=iv)
+ dec = op.recv(msglen * multiplier)
+ self.assertEqual(len(dec), msglen * multiplier)
+ self.assertEqual(dec, msg * multiplier)
+
+ @support.requires_linux_version(4, 3) # see test_aes_cbc
+ def test_aead_aes_gcm(self):
+ key = bytes.fromhex('c939cc13397c1d37de6ae0e1cb7c423c')
+ iv = bytes.fromhex('b3d8cc017cbb89b39e0f67e2')
+ plain = bytes.fromhex('c3b3c41f113a31b73d9a5cd432103069')
+ assoc = bytes.fromhex('24825602bd12a984e0092d3e448eda5f')
+ expected_ct = bytes.fromhex('93fe7d9e9bfd10348a5606e5cafa7354')
+ expected_tag = bytes.fromhex('0032a1dc85f1c9786925a2e71d8272dd')
+
+ taglen = len(expected_tag)
+ assoclen = len(assoc)
+
+ with self.create_alg('aead', 'gcm(aes)') as algo:
+ algo.setsockopt(socket.SOL_ALG, socket.ALG_SET_KEY, key)
+ algo.setsockopt(socket.SOL_ALG, socket.ALG_SET_AEAD_AUTHSIZE,
+ None, taglen)
+
+ # send assoc, plain and tag buffer in separate steps
+ op, _ = algo.accept()
+ with op:
+ op.sendmsg_afalg(op=socket.ALG_OP_ENCRYPT, iv=iv,
+ assoclen=assoclen, flags=socket.MSG_MORE)
+ op.sendall(assoc, socket.MSG_MORE)
+ op.sendall(plain, socket.MSG_MORE)
+ op.sendall(b'\x00' * taglen)
+ res = op.recv(assoclen + len(plain) + taglen)
+ self.assertEqual(expected_ct, res[assoclen:-taglen])
+ self.assertEqual(expected_tag, res[-taglen:])
+
+ # now with msg
+ op, _ = algo.accept()
+ with op:
+ msg = assoc + plain + b'\x00' * taglen
+ op.sendmsg_afalg([msg], op=socket.ALG_OP_ENCRYPT, iv=iv,
+ assoclen=assoclen)
+ res = op.recv(assoclen + len(plain) + taglen)
+ self.assertEqual(expected_ct, res[assoclen:-taglen])
+ self.assertEqual(expected_tag, res[-taglen:])
+
+ # create anc data manually
+ pack_uint32 = struct.Struct('I').pack
+ op, _ = algo.accept()
+ with op:
+ msg = assoc + plain + b'\x00' * taglen
+ op.sendmsg(
+ [msg],
+ ([socket.SOL_ALG, socket.ALG_SET_OP, pack_uint32(socket.ALG_OP_ENCRYPT)],
+ [socket.SOL_ALG, socket.ALG_SET_IV, pack_uint32(len(iv)) + iv],
+ [socket.SOL_ALG, socket.ALG_SET_AEAD_ASSOCLEN, pack_uint32(assoclen)],
+ )
+ )
+ res = op.recv(len(msg))
+ self.assertEqual(expected_ct, res[assoclen:-taglen])
+ self.assertEqual(expected_tag, res[-taglen:])
+
+ # decrypt and verify
+ op, _ = algo.accept()
+ with op:
+ msg = assoc + expected_ct + expected_tag
+ op.sendmsg_afalg([msg], op=socket.ALG_OP_DECRYPT, iv=iv,
+ assoclen=assoclen)
+ res = op.recv(len(msg))
+ self.assertEqual(plain, res[assoclen:-taglen])
+
+ @support.requires_linux_version(4, 3) # see test_aes_cbc
+ def test_drbg_pr_sha256(self):
+ # deterministic random bit generator, prediction resistance, sha256
+ with self.create_alg('rng', 'drbg_pr_sha256') as algo:
+ extra_seed = os.urandom(32)
+ algo.setsockopt(socket.SOL_ALG, socket.ALG_SET_KEY, extra_seed)
+ op, _ = algo.accept()
+ with op:
+ rn = op.recv(32)
+ self.assertEqual(len(rn), 32)
+
+ def test_sendmsg_afalg_args(self):
+ sock = socket.socket(socket.AF_ALG, socket.SOCK_SEQPACKET, 0)
+ with sock:
+ with self.assertRaises(TypeError):
+ sock.sendmsg_afalg()
+
+ with self.assertRaises(TypeError):
+ sock.sendmsg_afalg(op=None)
+
+ with self.assertRaises(TypeError):
+ sock.sendmsg_afalg(1)
+
+ with self.assertRaises(TypeError):
+ sock.sendmsg_afalg(op=socket.ALG_OP_ENCRYPT, assoclen=None)
+
+ with self.assertRaises(TypeError):
+ sock.sendmsg_afalg(op=socket.ALG_OP_ENCRYPT, assoclen=-1)
+
+
def test_main():
tests = [GeneralModuleTests, BasicTCPTest, TCPCloserTest, TCPTimeoutTest,
TestExceptions, BufferIOTest, BasicTCPTest2, BasicUDPTest, UDPTimeoutTest ]
@@ -5351,6 +5589,7 @@ def test_main():
tests.extend([TIPCTest, TIPCThreadableTest])
tests.extend([BasicCANTest, CANTest])
tests.extend([BasicRDSTest, RDSTest])
+ tests.append(LinuxKernelCryptoAPI)
tests.extend([
CmsgMacroTests,
SendmsgUDPTest,
diff --git a/Lib/test/test_socketserver.py b/Lib/test/test_socketserver.py
index 0d0f86f..140a6ab 100644
--- a/Lib/test/test_socketserver.py
+++ b/Lib/test/test_socketserver.py
@@ -3,12 +3,11 @@ Test suite for socketserver.
"""
import contextlib
+import io
import os
import select
import signal
import socket
-import select
-import errno
import tempfile
import unittest
import socketserver
@@ -46,7 +45,7 @@ def receive(sock, n, timeout=20):
else:
raise RuntimeError("timed out on %r" % (sock,))
-if HAVE_UNIX_SOCKETS:
+if HAVE_UNIX_SOCKETS and HAVE_FORKING:
class ForkingUnixStreamServer(socketserver.ForkingMixIn,
socketserver.UnixStreamServer):
pass
@@ -58,6 +57,7 @@ if HAVE_UNIX_SOCKETS:
@contextlib.contextmanager
def simple_subprocess(testcase):
+ """Tests that a custom child process is not waited on (Issue 1540386)"""
pid = os.fork()
if pid == 0:
# Don't raise an exception; it would be caught by the test harness.
@@ -103,7 +103,6 @@ class SocketServerTest(unittest.TestCase):
class MyServer(svrcls):
def handle_error(self, request, client_address):
self.close_request(request)
- self.server_close()
raise
class MyHandler(hdlrbase):
@@ -279,6 +278,182 @@ class SocketServerTest(unittest.TestCase):
socketserver.TCPServer((HOST, -1),
socketserver.StreamRequestHandler)
+ def test_context_manager(self):
+ with socketserver.TCPServer((HOST, 0),
+ socketserver.StreamRequestHandler) as server:
+ pass
+ self.assertEqual(-1, server.socket.fileno())
+
+
+class ErrorHandlerTest(unittest.TestCase):
+ """Test that the servers pass normal exceptions from the handler to
+ handle_error(), and that exiting exceptions like SystemExit and
+ KeyboardInterrupt are not passed."""
+
+ def tearDown(self):
+ test.support.unlink(test.support.TESTFN)
+
+ def test_sync_handled(self):
+ BaseErrorTestServer(ValueError)
+ self.check_result(handled=True)
+
+ def test_sync_not_handled(self):
+ with self.assertRaises(SystemExit):
+ BaseErrorTestServer(SystemExit)
+ self.check_result(handled=False)
+
+ @unittest.skipUnless(threading, 'Threading required for this test.')
+ def test_threading_handled(self):
+ ThreadingErrorTestServer(ValueError)
+ self.check_result(handled=True)
+
+ @unittest.skipUnless(threading, 'Threading required for this test.')
+ def test_threading_not_handled(self):
+ ThreadingErrorTestServer(SystemExit)
+ self.check_result(handled=False)
+
+ @requires_forking
+ def test_forking_handled(self):
+ ForkingErrorTestServer(ValueError)
+ self.check_result(handled=True)
+
+ @requires_forking
+ def test_forking_not_handled(self):
+ ForkingErrorTestServer(SystemExit)
+ self.check_result(handled=False)
+
+ def check_result(self, handled):
+ with open(test.support.TESTFN) as log:
+ expected = 'Handler called\n' + 'Error handled\n' * handled
+ self.assertEqual(log.read(), expected)
+
+
+class BaseErrorTestServer(socketserver.TCPServer):
+ def __init__(self, exception):
+ self.exception = exception
+ super().__init__((HOST, 0), BadHandler)
+ with socket.create_connection(self.server_address):
+ pass
+ try:
+ self.handle_request()
+ finally:
+ self.server_close()
+ self.wait_done()
+
+ def handle_error(self, request, client_address):
+ with open(test.support.TESTFN, 'a') as log:
+ log.write('Error handled\n')
+
+ def wait_done(self):
+ pass
+
+
+class BadHandler(socketserver.BaseRequestHandler):
+ def handle(self):
+ with open(test.support.TESTFN, 'a') as log:
+ log.write('Handler called\n')
+ raise self.server.exception('Test error')
+
+
+class ThreadingErrorTestServer(socketserver.ThreadingMixIn,
+ BaseErrorTestServer):
+ def __init__(self, *pos, **kw):
+ self.done = threading.Event()
+ super().__init__(*pos, **kw)
+
+ def shutdown_request(self, *pos, **kw):
+ super().shutdown_request(*pos, **kw)
+ self.done.set()
+
+ def wait_done(self):
+ self.done.wait()
+
+
+if HAVE_FORKING:
+ class ForkingErrorTestServer(socketserver.ForkingMixIn, BaseErrorTestServer):
+ def wait_done(self):
+ [child] = self.active_children
+ os.waitpid(child, 0)
+ self.active_children.clear()
+
+
+class SocketWriterTest(unittest.TestCase):
+ def test_basics(self):
+ class Handler(socketserver.StreamRequestHandler):
+ def handle(self):
+ self.server.wfile = self.wfile
+ self.server.wfile_fileno = self.wfile.fileno()
+ self.server.request_fileno = self.request.fileno()
+
+ server = socketserver.TCPServer((HOST, 0), Handler)
+ self.addCleanup(server.server_close)
+ s = socket.socket(
+ server.address_family, socket.SOCK_STREAM, socket.IPPROTO_TCP)
+ with s:
+ s.connect(server.server_address)
+ server.handle_request()
+ self.assertIsInstance(server.wfile, io.BufferedIOBase)
+ self.assertEqual(server.wfile_fileno, server.request_fileno)
+
+ @unittest.skipUnless(threading, 'Threading required for this test.')
+ def test_write(self):
+ # Test that wfile.write() sends data immediately, and that it does
+ # not truncate sends when interrupted by a Unix signal
+ pthread_kill = test.support.get_attribute(signal, 'pthread_kill')
+
+ class Handler(socketserver.StreamRequestHandler):
+ def handle(self):
+ self.server.sent1 = self.wfile.write(b'write data\n')
+ # Should be sent immediately, without requiring flush()
+ self.server.received = self.rfile.readline()
+ big_chunk = b'\0' * test.support.SOCK_MAX_SIZE
+ self.server.sent2 = self.wfile.write(big_chunk)
+
+ server = socketserver.TCPServer((HOST, 0), Handler)
+ self.addCleanup(server.server_close)
+ interrupted = threading.Event()
+
+ def signal_handler(signum, frame):
+ interrupted.set()
+
+ original = signal.signal(signal.SIGUSR1, signal_handler)
+ self.addCleanup(signal.signal, signal.SIGUSR1, original)
+ response1 = None
+ received2 = None
+ main_thread = threading.get_ident()
+
+ def run_client():
+ s = socket.socket(server.address_family, socket.SOCK_STREAM,
+ socket.IPPROTO_TCP)
+ with s, s.makefile('rb') as reader:
+ s.connect(server.server_address)
+ nonlocal response1
+ response1 = reader.readline()
+ s.sendall(b'client response\n')
+
+ reader.read(100)
+ # The main thread should now be blocking in a send() syscall.
+ # But in theory, it could get interrupted by other signals,
+ # and then retried. So keep sending the signal in a loop, in
+ # case an earlier signal happens to be delivered at an
+ # inconvenient moment.
+ while True:
+ pthread_kill(main_thread, signal.SIGUSR1)
+ if interrupted.wait(timeout=float(1)):
+ break
+ nonlocal received2
+ received2 = len(reader.read())
+
+ background = threading.Thread(target=run_client)
+ background.start()
+ server.handle_request()
+ background.join()
+ self.assertEqual(server.sent1, len(response1))
+ self.assertEqual(response1, b'write data\n')
+ self.assertEqual(server.received, b'client response\n')
+ self.assertEqual(server.sent2, test.support.SOCK_MAX_SIZE)
+ self.assertEqual(received2, test.support.SOCK_MAX_SIZE - 100)
+
class MiscTestCase(unittest.TestCase):
diff --git a/Lib/test/test_sort.py b/Lib/test/test_sort.py
index a5d0ebf..98ccab5 100644
--- a/Lib/test/test_sort.py
+++ b/Lib/test/test_sort.py
@@ -1,6 +1,5 @@
from test import support
import random
-import sys
import unittest
from functools import cmp_to_key
diff --git a/Lib/test/test_spwd.py b/Lib/test/test_spwd.py
index bea7ab1..e893f3a 100644
--- a/Lib/test/test_spwd.py
+++ b/Lib/test/test_spwd.py
@@ -56,5 +56,20 @@ class TestSpwdRoot(unittest.TestCase):
self.assertRaises(TypeError, spwd.getspnam, bytes_name)
+@unittest.skipUnless(hasattr(os, 'geteuid') and os.geteuid() != 0,
+ 'non-root user required')
+class TestSpwdNonRoot(unittest.TestCase):
+
+ def test_getspnam_exception(self):
+ name = 'bin'
+ try:
+ with self.assertRaises(PermissionError) as cm:
+ spwd.getspnam(name)
+ except KeyError as exc:
+ self.skipTest("spwd entry %r doesn't exist: %s" % (name, exc))
+ else:
+ self.assertEqual(str(cm.exception), '[Errno 13] Permission denied')
+
+
if __name__ == "__main__":
unittest.main()
diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py
index 5515583..d203cdd 100644
--- a/Lib/test/test_ssl.py
+++ b/Lib/test/test_ssl.py
@@ -21,6 +21,13 @@ import functools
ssl = support.import_module("ssl")
+try:
+ import threading
+except ImportError:
+ _have_threads = False
+else:
+ _have_threads = True
+
PROTOCOLS = sorted(ssl._PROTOCOL_NAMES)
HOST = support.HOST
IS_LIBRESSL = ssl.OPENSSL_VERSION.startswith('LibreSSL')
@@ -56,12 +63,12 @@ CRLFILE = data_file("revocation.crl")
# Two keys and certs signed by the same CA (for SNI tests)
SIGNED_CERTFILE = data_file("keycert3.pem")
SIGNED_CERTFILE2 = data_file("keycert4.pem")
-SIGNING_CA = data_file("pycacert.pem")
+# Same certificate as pycacert.pem, but without extra text in file
+SIGNING_CA = data_file("capath", "ceff1710.0")
# cert with all kinds of subject alt names
ALLSANFILE = data_file("allsans.pem")
REMOTE_HOST = "self-signed.pythontest.net"
-REMOTE_ROOT_CERT = data_file("selfsigned_pythontestdotnet.pem")
EMPTYCERT = data_file("nullcert.pem")
BADCERT = data_file("badcert.pem")
@@ -73,6 +80,12 @@ NULLBYTECERT = data_file("nullbytecert.pem")
DHFILE = data_file("dh1024.pem")
BYTES_DHFILE = os.fsencode(DHFILE)
+# Not defined in all versions of OpenSSL
+OP_NO_COMPRESSION = getattr(ssl, "OP_NO_COMPRESSION", 0)
+OP_SINGLE_DH_USE = getattr(ssl, "OP_SINGLE_DH_USE", 0)
+OP_SINGLE_ECDH_USE = getattr(ssl, "OP_SINGLE_ECDH_USE", 0)
+OP_CIPHER_SERVER_PREFERENCE = getattr(ssl, "OP_CIPHER_SERVER_PREFERENCE", 0)
+
def handle_error(prefix):
exc_format = ' '.join(traceback.format_exception(*sys.exc_info()))
@@ -130,6 +143,21 @@ def skip_if_broken_ubuntu_ssl(func):
needs_sni = unittest.skipUnless(ssl.HAS_SNI, "SNI support needed for this test")
+def test_wrap_socket(sock, ssl_version=ssl.PROTOCOL_TLS, *,
+ cert_reqs=ssl.CERT_NONE, ca_certs=None,
+ ciphers=None, certfile=None, keyfile=None,
+ **kwargs):
+ context = ssl.SSLContext(ssl_version)
+ if cert_reqs is not None:
+ context.verify_mode = cert_reqs
+ if ca_certs is not None:
+ context.load_verify_locations(ca_certs)
+ if certfile is not None or keyfile is not None:
+ context.load_cert_chain(certfile, keyfile)
+ if ciphers is not None:
+ context.set_ciphers(ciphers)
+ return context.wrap_socket(sock, **kwargs)
+
class BasicSocketTests(unittest.TestCase):
def test_constants(self):
@@ -350,17 +378,17 @@ class BasicSocketTests(unittest.TestCase):
# Issue #7943: an SSL object doesn't create reference cycles with
# itself.
s = socket.socket(socket.AF_INET)
- ss = ssl.wrap_socket(s)
+ ss = test_wrap_socket(s)
wr = weakref.ref(ss)
with support.check_warnings(("", ResourceWarning)):
del ss
- self.assertEqual(wr(), None)
+ self.assertEqual(wr(), None)
def test_wrapped_unconnected(self):
# Methods on an unconnected SSLSocket propagate the original
# OSError raise by the underlying socket object.
s = socket.socket(socket.AF_INET)
- with ssl.wrap_socket(s) as ss:
+ with test_wrap_socket(s) as ss:
self.assertRaises(OSError, ss.recv, 1)
self.assertRaises(OSError, ss.recv_into, bytearray(b'x'))
self.assertRaises(OSError, ss.recvfrom, 1)
@@ -374,10 +402,10 @@ class BasicSocketTests(unittest.TestCase):
for timeout in (None, 0.0, 5.0):
s = socket.socket(socket.AF_INET)
s.settimeout(timeout)
- with ssl.wrap_socket(s) as ss:
+ with test_wrap_socket(s) as ss:
self.assertEqual(timeout, ss.gettimeout())
- def test_errors(self):
+ def test_errors_sslwrap(self):
sock = socket.socket()
self.assertRaisesRegex(ValueError,
"certfile must be specified",
@@ -387,10 +415,10 @@ class BasicSocketTests(unittest.TestCase):
ssl.wrap_socket, sock, server_side=True)
self.assertRaisesRegex(ValueError,
"certfile must be specified for server-side operations",
- ssl.wrap_socket, sock, server_side=True, certfile="")
+ ssl.wrap_socket, sock, server_side=True, certfile="")
with ssl.wrap_socket(sock, server_side=True, certfile=CERTFILE) as s:
self.assertRaisesRegex(ValueError, "can't connect in server-side mode",
- s.connect, (HOST, 8080))
+ s.connect, (HOST, 8080))
with self.assertRaises(OSError) as cm:
with socket.socket() as sock:
ssl.wrap_socket(sock, certfile=NONEXISTINGCERT)
@@ -413,7 +441,7 @@ class BasicSocketTests(unittest.TestCase):
sock = socket.socket()
self.addCleanup(sock.close)
with self.assertRaises(ssl.SSLError):
- ssl.wrap_socket(sock,
+ test_wrap_socket(sock,
certfile=certfile,
ssl_version=ssl.PROTOCOL_TLSv1)
@@ -600,7 +628,7 @@ class BasicSocketTests(unittest.TestCase):
s.listen()
c = socket.socket(socket.AF_INET)
c.connect(s.getsockname())
- with ssl.wrap_socket(c, do_handshake_on_connect=False) as ss:
+ with test_wrap_socket(c, do_handshake_on_connect=False) as ss:
with self.assertRaises(ValueError):
ss.get_channel_binding("unknown-type")
s.close()
@@ -610,15 +638,15 @@ class BasicSocketTests(unittest.TestCase):
def test_tls_unique_channel_binding(self):
# unconnected should return None for known type
s = socket.socket(socket.AF_INET)
- with ssl.wrap_socket(s) as ss:
+ with test_wrap_socket(s) as ss:
self.assertIsNone(ss.get_channel_binding("tls-unique"))
# the same for server-side
s = socket.socket(socket.AF_INET)
- with ssl.wrap_socket(s, server_side=True, certfile=CERTFILE) as ss:
+ with test_wrap_socket(s, server_side=True, certfile=CERTFILE) as ss:
self.assertIsNone(ss.get_channel_binding("tls-unique"))
def test_dealloc_warn(self):
- ss = ssl.wrap_socket(socket.socket(socket.AF_INET))
+ ss = test_wrap_socket(socket.socket(socket.AF_INET))
r = repr(ss)
with self.assertWarns(ResourceWarning) as cm:
ss = None
@@ -737,7 +765,7 @@ class BasicSocketTests(unittest.TestCase):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.addCleanup(s.close)
with self.assertRaises(NotImplementedError) as cx:
- ssl.wrap_socket(s, cert_reqs=ssl.CERT_NONE)
+ test_wrap_socket(s, cert_reqs=ssl.CERT_NONE)
self.assertEqual(str(cx.exception), "only stream sockets are supported")
ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
with self.assertRaises(NotImplementedError) as cx:
@@ -809,6 +837,22 @@ class BasicSocketTests(unittest.TestCase):
self.cert_time_ok("Feb 9 00:00:00 2007 GMT", 1170979200.0)
self.cert_time_fail(local_february_name() + " 9 00:00:00 2007 GMT")
+ def test_connect_ex_error(self):
+ server = socket.socket(socket.AF_INET)
+ self.addCleanup(server.close)
+ port = support.bind_port(server) # Reserve port but don't listen
+ s = test_wrap_socket(socket.socket(socket.AF_INET),
+ cert_reqs=ssl.CERT_REQUIRED)
+ self.addCleanup(s.close)
+ rc = s.connect_ex((HOST, port))
+ # Issue #19919: Windows machines or VMs hosted on Windows
+ # machines sometimes return EWOULDBLOCK.
+ errors = (
+ errno.ECONNREFUSED, errno.EHOSTUNREACH, errno.ETIMEDOUT,
+ errno.EWOULDBLOCK,
+ )
+ self.assertIn(rc, errors)
+
class ContextTests(unittest.TestCase):
@@ -834,13 +878,22 @@ class ContextTests(unittest.TestCase):
with self.assertRaisesRegex(ssl.SSLError, "No cipher can be selected"):
ctx.set_ciphers("^$:,;?*'dorothyx")
+ @unittest.skipIf(ssl.OPENSSL_VERSION_INFO < (1, 0, 2, 0, 0), 'OpenSSL too old')
+ def test_get_ciphers(self):
+ ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
+ ctx.set_ciphers('AESGCM')
+ names = set(d['name'] for d in ctx.get_ciphers())
+ self.assertIn('AES256-GCM-SHA384', names)
+ self.assertIn('AES128-GCM-SHA256', names)
+
@skip_if_broken_ubuntu_ssl
def test_options(self):
ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
# OP_ALL | OP_NO_SSLv2 | OP_NO_SSLv3 is the default value
default = (ssl.OP_ALL | ssl.OP_NO_SSLv2 | ssl.OP_NO_SSLv3)
- if not IS_LIBRESSL and ssl.OPENSSL_VERSION_INFO >= (1, 1, 0):
- default |= ssl.OP_NO_COMPRESSION
+ # SSLContext also enables these by default
+ default |= (OP_NO_COMPRESSION | OP_CIPHER_SERVER_PREFERENCE |
+ OP_SINGLE_DH_USE | OP_SINGLE_ECDH_USE)
self.assertEqual(default, ctx.options)
ctx.options |= ssl.OP_NO_TLSv1
self.assertEqual(default | ssl.OP_NO_TLSv1, ctx.options)
@@ -1205,16 +1258,29 @@ class ContextTests(unittest.TestCase):
stats["x509"] += 1
self.assertEqual(ctx.cert_store_stats(), stats)
+ def _assert_context_options(self, ctx):
+ self.assertEqual(ctx.options & ssl.OP_NO_SSLv2, ssl.OP_NO_SSLv2)
+ if OP_NO_COMPRESSION != 0:
+ self.assertEqual(ctx.options & OP_NO_COMPRESSION,
+ OP_NO_COMPRESSION)
+ if OP_SINGLE_DH_USE != 0:
+ self.assertEqual(ctx.options & OP_SINGLE_DH_USE,
+ OP_SINGLE_DH_USE)
+ if OP_SINGLE_ECDH_USE != 0:
+ self.assertEqual(ctx.options & OP_SINGLE_ECDH_USE,
+ OP_SINGLE_ECDH_USE)
+ if OP_CIPHER_SERVER_PREFERENCE != 0:
+ self.assertEqual(ctx.options & OP_CIPHER_SERVER_PREFERENCE,
+ OP_CIPHER_SERVER_PREFERENCE)
+
def test_create_default_context(self):
ctx = ssl.create_default_context()
+
self.assertEqual(ctx.protocol, ssl.PROTOCOL_SSLv23)
self.assertEqual(ctx.verify_mode, ssl.CERT_REQUIRED)
self.assertTrue(ctx.check_hostname)
- self.assertEqual(ctx.options & ssl.OP_NO_SSLv2, ssl.OP_NO_SSLv2)
- self.assertEqual(
- ctx.options & getattr(ssl, "OP_NO_COMPRESSION", 0),
- getattr(ssl, "OP_NO_COMPRESSION", 0),
- )
+ self._assert_context_options(ctx)
+
with open(SIGNING_CA) as f:
cadata = f.read()
@@ -1222,40 +1288,24 @@ class ContextTests(unittest.TestCase):
cadata=cadata)
self.assertEqual(ctx.protocol, ssl.PROTOCOL_SSLv23)
self.assertEqual(ctx.verify_mode, ssl.CERT_REQUIRED)
- self.assertEqual(ctx.options & ssl.OP_NO_SSLv2, ssl.OP_NO_SSLv2)
- self.assertEqual(
- ctx.options & getattr(ssl, "OP_NO_COMPRESSION", 0),
- getattr(ssl, "OP_NO_COMPRESSION", 0),
- )
+ self._assert_context_options(ctx)
ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
self.assertEqual(ctx.protocol, ssl.PROTOCOL_SSLv23)
self.assertEqual(ctx.verify_mode, ssl.CERT_NONE)
- self.assertEqual(ctx.options & ssl.OP_NO_SSLv2, ssl.OP_NO_SSLv2)
- self.assertEqual(
- ctx.options & getattr(ssl, "OP_NO_COMPRESSION", 0),
- getattr(ssl, "OP_NO_COMPRESSION", 0),
- )
- self.assertEqual(
- ctx.options & getattr(ssl, "OP_SINGLE_DH_USE", 0),
- getattr(ssl, "OP_SINGLE_DH_USE", 0),
- )
- self.assertEqual(
- ctx.options & getattr(ssl, "OP_SINGLE_ECDH_USE", 0),
- getattr(ssl, "OP_SINGLE_ECDH_USE", 0),
- )
+ self._assert_context_options(ctx)
def test__create_stdlib_context(self):
ctx = ssl._create_stdlib_context()
self.assertEqual(ctx.protocol, ssl.PROTOCOL_SSLv23)
self.assertEqual(ctx.verify_mode, ssl.CERT_NONE)
self.assertFalse(ctx.check_hostname)
- self.assertEqual(ctx.options & ssl.OP_NO_SSLv2, ssl.OP_NO_SSLv2)
+ self._assert_context_options(ctx)
ctx = ssl._create_stdlib_context(ssl.PROTOCOL_TLSv1)
self.assertEqual(ctx.protocol, ssl.PROTOCOL_TLSv1)
self.assertEqual(ctx.verify_mode, ssl.CERT_NONE)
- self.assertEqual(ctx.options & ssl.OP_NO_SSLv2, ssl.OP_NO_SSLv2)
+ self._assert_context_options(ctx)
ctx = ssl._create_stdlib_context(ssl.PROTOCOL_TLSv1,
cert_reqs=ssl.CERT_REQUIRED,
@@ -1263,12 +1313,12 @@ class ContextTests(unittest.TestCase):
self.assertEqual(ctx.protocol, ssl.PROTOCOL_TLSv1)
self.assertEqual(ctx.verify_mode, ssl.CERT_REQUIRED)
self.assertTrue(ctx.check_hostname)
- self.assertEqual(ctx.options & ssl.OP_NO_SSLv2, ssl.OP_NO_SSLv2)
+ self._assert_context_options(ctx)
ctx = ssl._create_stdlib_context(purpose=ssl.Purpose.CLIENT_AUTH)
self.assertEqual(ctx.protocol, ssl.PROTOCOL_SSLv23)
self.assertEqual(ctx.verify_mode, ssl.CERT_NONE)
- self.assertEqual(ctx.options & ssl.OP_NO_SSLv2, ssl.OP_NO_SSLv2)
+ self._assert_context_options(ctx)
def test_check_hostname(self):
ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
@@ -1292,6 +1342,17 @@ class ContextTests(unittest.TestCase):
ctx.check_hostname = False
self.assertFalse(ctx.check_hostname)
+ def test_context_client_server(self):
+ # PROTOCOL_TLS_CLIENT has sane defaults
+ ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
+ self.assertTrue(ctx.check_hostname)
+ self.assertEqual(ctx.verify_mode, ssl.CERT_REQUIRED)
+
+ # PROTOCOL_TLS_SERVER has different but also sane defaults
+ ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
+ self.assertFalse(ctx.check_hostname)
+ self.assertEqual(ctx.verify_mode, ssl.CERT_NONE)
+
class SSLErrorTests(unittest.TestCase):
@@ -1397,140 +1458,105 @@ class MemoryBIOTests(unittest.TestCase):
self.assertRaises(TypeError, bio.write, 1)
-class NetworkedTests(unittest.TestCase):
+@unittest.skipUnless(_have_threads, "Needs threading module")
+class SimpleBackgroundTests(unittest.TestCase):
- def test_connect(self):
- with support.transient_internet(REMOTE_HOST):
- s = ssl.wrap_socket(socket.socket(socket.AF_INET),
- cert_reqs=ssl.CERT_NONE)
- try:
- s.connect((REMOTE_HOST, 443))
- self.assertEqual({}, s.getpeercert())
- finally:
- s.close()
+ """Tests that connect to a simple server running in the background"""
- # this should fail because we have no verification certs
- s = ssl.wrap_socket(socket.socket(socket.AF_INET),
- cert_reqs=ssl.CERT_REQUIRED)
- self.assertRaisesRegex(ssl.SSLError, "certificate verify failed",
- s.connect, (REMOTE_HOST, 443))
- s.close()
+ def setUp(self):
+ server = ThreadedEchoServer(SIGNED_CERTFILE)
+ self.server_addr = (HOST, server.port)
+ server.__enter__()
+ self.addCleanup(server.__exit__, None, None, None)
- # this should succeed because we specify the root cert
- s = ssl.wrap_socket(socket.socket(socket.AF_INET),
- cert_reqs=ssl.CERT_REQUIRED,
- ca_certs=REMOTE_ROOT_CERT)
- try:
- s.connect((REMOTE_HOST, 443))
- self.assertTrue(s.getpeercert())
- finally:
- s.close()
+ def test_connect(self):
+ with test_wrap_socket(socket.socket(socket.AF_INET),
+ cert_reqs=ssl.CERT_NONE) as s:
+ s.connect(self.server_addr)
+ self.assertEqual({}, s.getpeercert())
+ self.assertFalse(s.server_side)
+
+ # this should succeed because we specify the root cert
+ with test_wrap_socket(socket.socket(socket.AF_INET),
+ cert_reqs=ssl.CERT_REQUIRED,
+ ca_certs=SIGNING_CA) as s:
+ s.connect(self.server_addr)
+ self.assertTrue(s.getpeercert())
+ self.assertFalse(s.server_side)
+
+ def test_connect_fail(self):
+ # This should fail because we have no verification certs. Connection
+ # failure crashes ThreadedEchoServer, so run this in an independent
+ # test method.
+ s = test_wrap_socket(socket.socket(socket.AF_INET),
+ cert_reqs=ssl.CERT_REQUIRED)
+ self.addCleanup(s.close)
+ self.assertRaisesRegex(ssl.SSLError, "certificate verify failed",
+ s.connect, self.server_addr)
def test_connect_ex(self):
# Issue #11326: check connect_ex() implementation
- with support.transient_internet(REMOTE_HOST):
- s = ssl.wrap_socket(socket.socket(socket.AF_INET),
- cert_reqs=ssl.CERT_REQUIRED,
- ca_certs=REMOTE_ROOT_CERT)
- try:
- self.assertEqual(0, s.connect_ex((REMOTE_HOST, 443)))
- self.assertTrue(s.getpeercert())
- finally:
- s.close()
+ s = test_wrap_socket(socket.socket(socket.AF_INET),
+ cert_reqs=ssl.CERT_REQUIRED,
+ ca_certs=SIGNING_CA)
+ self.addCleanup(s.close)
+ self.assertEqual(0, s.connect_ex(self.server_addr))
+ self.assertTrue(s.getpeercert())
def test_non_blocking_connect_ex(self):
# Issue #11326: non-blocking connect_ex() should allow handshake
# to proceed after the socket gets ready.
- with support.transient_internet(REMOTE_HOST):
- s = ssl.wrap_socket(socket.socket(socket.AF_INET),
- cert_reqs=ssl.CERT_REQUIRED,
- ca_certs=REMOTE_ROOT_CERT,
- do_handshake_on_connect=False)
+ s = test_wrap_socket(socket.socket(socket.AF_INET),
+ cert_reqs=ssl.CERT_REQUIRED,
+ ca_certs=SIGNING_CA,
+ do_handshake_on_connect=False)
+ self.addCleanup(s.close)
+ s.setblocking(False)
+ rc = s.connect_ex(self.server_addr)
+ # EWOULDBLOCK under Windows, EINPROGRESS elsewhere
+ self.assertIn(rc, (0, errno.EINPROGRESS, errno.EWOULDBLOCK))
+ # Wait for connect to finish
+ select.select([], [s], [], 5.0)
+ # Non-blocking handshake
+ while True:
try:
- s.setblocking(False)
- rc = s.connect_ex((REMOTE_HOST, 443))
- # EWOULDBLOCK under Windows, EINPROGRESS elsewhere
- self.assertIn(rc, (0, errno.EINPROGRESS, errno.EWOULDBLOCK))
- # Wait for connect to finish
+ s.do_handshake()
+ break
+ except ssl.SSLWantReadError:
+ select.select([s], [], [], 5.0)
+ except ssl.SSLWantWriteError:
select.select([], [s], [], 5.0)
- # Non-blocking handshake
- while True:
- try:
- s.do_handshake()
- break
- except ssl.SSLWantReadError:
- select.select([s], [], [], 5.0)
- except ssl.SSLWantWriteError:
- select.select([], [s], [], 5.0)
- # SSL established
- self.assertTrue(s.getpeercert())
- finally:
- s.close()
-
- def test_timeout_connect_ex(self):
- # Issue #12065: on a timeout, connect_ex() should return the original
- # errno (mimicking the behaviour of non-SSL sockets).
- with support.transient_internet(REMOTE_HOST):
- s = ssl.wrap_socket(socket.socket(socket.AF_INET),
- cert_reqs=ssl.CERT_REQUIRED,
- ca_certs=REMOTE_ROOT_CERT,
- do_handshake_on_connect=False)
- try:
- s.settimeout(0.0000001)
- rc = s.connect_ex((REMOTE_HOST, 443))
- if rc == 0:
- self.skipTest("REMOTE_HOST responded too quickly")
- self.assertIn(rc, (errno.EAGAIN, errno.EWOULDBLOCK))
- finally:
- s.close()
-
- def test_connect_ex_error(self):
- with support.transient_internet(REMOTE_HOST):
- s = ssl.wrap_socket(socket.socket(socket.AF_INET),
- cert_reqs=ssl.CERT_REQUIRED,
- ca_certs=REMOTE_ROOT_CERT)
- try:
- rc = s.connect_ex((REMOTE_HOST, 444))
- # Issue #19919: Windows machines or VMs hosted on Windows
- # machines sometimes return EWOULDBLOCK.
- errors = (
- errno.ECONNREFUSED, errno.EHOSTUNREACH, errno.ETIMEDOUT,
- errno.EWOULDBLOCK,
- )
- self.assertIn(rc, errors)
- finally:
- s.close()
+ # SSL established
+ self.assertTrue(s.getpeercert())
def test_connect_with_context(self):
- with support.transient_internet(REMOTE_HOST):
- # Same as test_connect, but with a separately created context
- ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
- s = ctx.wrap_socket(socket.socket(socket.AF_INET))
- s.connect((REMOTE_HOST, 443))
- try:
- self.assertEqual({}, s.getpeercert())
- finally:
- s.close()
- # Same with a server hostname
- s = ctx.wrap_socket(socket.socket(socket.AF_INET),
- server_hostname=REMOTE_HOST)
- s.connect((REMOTE_HOST, 443))
- s.close()
- # This should fail because we have no verification certs
- ctx.verify_mode = ssl.CERT_REQUIRED
- s = ctx.wrap_socket(socket.socket(socket.AF_INET))
- self.assertRaisesRegex(ssl.SSLError, "certificate verify failed",
- s.connect, (REMOTE_HOST, 443))
- s.close()
- # This should succeed because we specify the root cert
- ctx.load_verify_locations(REMOTE_ROOT_CERT)
- s = ctx.wrap_socket(socket.socket(socket.AF_INET))
- s.connect((REMOTE_HOST, 443))
- try:
- cert = s.getpeercert()
- self.assertTrue(cert)
- finally:
- s.close()
+ # Same as test_connect, but with a separately created context
+ ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
+ with ctx.wrap_socket(socket.socket(socket.AF_INET)) as s:
+ s.connect(self.server_addr)
+ self.assertEqual({}, s.getpeercert())
+ # Same with a server hostname
+ with ctx.wrap_socket(socket.socket(socket.AF_INET),
+ server_hostname="dummy") as s:
+ s.connect(self.server_addr)
+ ctx.verify_mode = ssl.CERT_REQUIRED
+ # This should succeed because we specify the root cert
+ ctx.load_verify_locations(SIGNING_CA)
+ with ctx.wrap_socket(socket.socket(socket.AF_INET)) as s:
+ s.connect(self.server_addr)
+ cert = s.getpeercert()
+ self.assertTrue(cert)
+
+ def test_connect_with_context_fail(self):
+ # This should fail because we have no verification certs. Connection
+ # failure crashes ThreadedEchoServer, so run this in an independent
+ # test method.
+ ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
+ ctx.verify_mode = ssl.CERT_REQUIRED
+ s = ctx.wrap_socket(socket.socket(socket.AF_INET))
+ self.addCleanup(s.close)
+ self.assertRaisesRegex(ssl.SSLError, "certificate verify failed",
+ s.connect, self.server_addr)
def test_connect_capath(self):
# Verify server certificates using the `capath` argument
@@ -1538,198 +1564,130 @@ class NetworkedTests(unittest.TestCase):
# OpenSSL 0.9.8n and 1.0.0, as a result the capath directory must
# contain both versions of each certificate (same content, different
# filename) for this test to be portable across OpenSSL releases.
- with support.transient_internet(REMOTE_HOST):
- ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
- ctx.verify_mode = ssl.CERT_REQUIRED
- ctx.load_verify_locations(capath=CAPATH)
- s = ctx.wrap_socket(socket.socket(socket.AF_INET))
- s.connect((REMOTE_HOST, 443))
- try:
- cert = s.getpeercert()
- self.assertTrue(cert)
- finally:
- s.close()
- # Same with a bytes `capath` argument
- ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
- ctx.verify_mode = ssl.CERT_REQUIRED
- ctx.load_verify_locations(capath=BYTES_CAPATH)
- s = ctx.wrap_socket(socket.socket(socket.AF_INET))
- s.connect((REMOTE_HOST, 443))
- try:
- cert = s.getpeercert()
- self.assertTrue(cert)
- finally:
- s.close()
+ ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
+ ctx.verify_mode = ssl.CERT_REQUIRED
+ ctx.load_verify_locations(capath=CAPATH)
+ with ctx.wrap_socket(socket.socket(socket.AF_INET)) as s:
+ s.connect(self.server_addr)
+ cert = s.getpeercert()
+ self.assertTrue(cert)
+ # Same with a bytes `capath` argument
+ ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
+ ctx.verify_mode = ssl.CERT_REQUIRED
+ ctx.load_verify_locations(capath=BYTES_CAPATH)
+ with ctx.wrap_socket(socket.socket(socket.AF_INET)) as s:
+ s.connect(self.server_addr)
+ cert = s.getpeercert()
+ self.assertTrue(cert)
def test_connect_cadata(self):
- with open(REMOTE_ROOT_CERT) as f:
+ with open(SIGNING_CA) as f:
pem = f.read()
der = ssl.PEM_cert_to_DER_cert(pem)
- with support.transient_internet(REMOTE_HOST):
- ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
- ctx.verify_mode = ssl.CERT_REQUIRED
- ctx.load_verify_locations(cadata=pem)
- with ctx.wrap_socket(socket.socket(socket.AF_INET)) as s:
- s.connect((REMOTE_HOST, 443))
- cert = s.getpeercert()
- self.assertTrue(cert)
+ ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
+ ctx.verify_mode = ssl.CERT_REQUIRED
+ ctx.load_verify_locations(cadata=pem)
+ with ctx.wrap_socket(socket.socket(socket.AF_INET)) as s:
+ s.connect(self.server_addr)
+ cert = s.getpeercert()
+ self.assertTrue(cert)
- # same with DER
- ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
- ctx.verify_mode = ssl.CERT_REQUIRED
- ctx.load_verify_locations(cadata=der)
- with ctx.wrap_socket(socket.socket(socket.AF_INET)) as s:
- s.connect((REMOTE_HOST, 443))
- cert = s.getpeercert()
- self.assertTrue(cert)
+ # same with DER
+ ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
+ ctx.verify_mode = ssl.CERT_REQUIRED
+ ctx.load_verify_locations(cadata=der)
+ with ctx.wrap_socket(socket.socket(socket.AF_INET)) as s:
+ s.connect(self.server_addr)
+ cert = s.getpeercert()
+ self.assertTrue(cert)
@unittest.skipIf(os.name == "nt", "Can't use a socket as a file under Windows")
def test_makefile_close(self):
# Issue #5238: creating a file-like object with makefile() shouldn't
# delay closing the underlying "real socket" (here tested with its
# file descriptor, hence skipping the test under Windows).
- with support.transient_internet(REMOTE_HOST):
- ss = ssl.wrap_socket(socket.socket(socket.AF_INET))
- ss.connect((REMOTE_HOST, 443))
- fd = ss.fileno()
- f = ss.makefile()
- f.close()
- # The fd is still open
+ ss = test_wrap_socket(socket.socket(socket.AF_INET))
+ ss.connect(self.server_addr)
+ fd = ss.fileno()
+ f = ss.makefile()
+ f.close()
+ # The fd is still open
+ os.read(fd, 0)
+ # Closing the SSL socket should close the fd too
+ ss.close()
+ gc.collect()
+ with self.assertRaises(OSError) as e:
os.read(fd, 0)
- # Closing the SSL socket should close the fd too
- ss.close()
- gc.collect()
- with self.assertRaises(OSError) as e:
- os.read(fd, 0)
- self.assertEqual(e.exception.errno, errno.EBADF)
+ self.assertEqual(e.exception.errno, errno.EBADF)
def test_non_blocking_handshake(self):
- with support.transient_internet(REMOTE_HOST):
- s = socket.socket(socket.AF_INET)
- s.connect((REMOTE_HOST, 443))
- s.setblocking(False)
- s = ssl.wrap_socket(s,
- cert_reqs=ssl.CERT_NONE,
- do_handshake_on_connect=False)
- count = 0
- while True:
- try:
- count += 1
- s.do_handshake()
- break
- except ssl.SSLWantReadError:
- select.select([s], [], [])
- except ssl.SSLWantWriteError:
- select.select([], [s], [])
- s.close()
- if support.verbose:
- sys.stdout.write("\nNeeded %d calls to do_handshake() to establish session.\n" % count)
+ s = socket.socket(socket.AF_INET)
+ s.connect(self.server_addr)
+ s.setblocking(False)
+ s = test_wrap_socket(s,
+ cert_reqs=ssl.CERT_NONE,
+ do_handshake_on_connect=False)
+ self.addCleanup(s.close)
+ count = 0
+ while True:
+ try:
+ count += 1
+ s.do_handshake()
+ break
+ except ssl.SSLWantReadError:
+ select.select([s], [], [])
+ except ssl.SSLWantWriteError:
+ select.select([], [s], [])
+ if support.verbose:
+ sys.stdout.write("\nNeeded %d calls to do_handshake() to establish session.\n" % count)
def test_get_server_certificate(self):
- def _test_get_server_certificate(host, port, cert=None):
- with support.transient_internet(host):
- pem = ssl.get_server_certificate((host, port))
- if not pem:
- self.fail("No server certificate on %s:%s!" % (host, port))
+ _test_get_server_certificate(self, *self.server_addr, cert=SIGNING_CA)
- try:
- pem = ssl.get_server_certificate((host, port),
- ca_certs=CERTFILE)
- except ssl.SSLError as x:
- #should fail
- if support.verbose:
- sys.stdout.write("%s\n" % x)
- else:
- self.fail("Got server certificate %s for %s:%s!" % (pem, host, port))
-
- pem = ssl.get_server_certificate((host, port),
- ca_certs=cert)
- if not pem:
- self.fail("No server certificate on %s:%s!" % (host, port))
- if support.verbose:
- sys.stdout.write("\nVerified certificate for %s:%s is\n%s\n" % (host, port ,pem))
-
- _test_get_server_certificate(REMOTE_HOST, 443, REMOTE_ROOT_CERT)
- if support.IPV6_ENABLED:
- _test_get_server_certificate('ipv6.google.com', 443)
+ def test_get_server_certificate_fail(self):
+ # Connection failure crashes ThreadedEchoServer, so run this in an
+ # independent test method
+ _test_get_server_certificate_fail(self, *self.server_addr)
def test_ciphers(self):
- remote = (REMOTE_HOST, 443)
- with support.transient_internet(remote[0]):
- with ssl.wrap_socket(socket.socket(socket.AF_INET),
- cert_reqs=ssl.CERT_NONE, ciphers="ALL") as s:
- s.connect(remote)
- with ssl.wrap_socket(socket.socket(socket.AF_INET),
- cert_reqs=ssl.CERT_NONE, ciphers="DEFAULT") as s:
- s.connect(remote)
- # Error checking can happen at instantiation or when connecting
- with self.assertRaisesRegex(ssl.SSLError, "No cipher can be selected"):
- with socket.socket(socket.AF_INET) as sock:
- s = ssl.wrap_socket(sock,
- cert_reqs=ssl.CERT_NONE, ciphers="^$:,;?*'dorothyx")
- s.connect(remote)
-
- def test_algorithms(self):
- # Issue #8484: all algorithms should be available when verifying a
- # certificate.
- # SHA256 was added in OpenSSL 0.9.8
- if ssl.OPENSSL_VERSION_INFO < (0, 9, 8, 0, 15):
- self.skipTest("SHA256 not available on %r" % ssl.OPENSSL_VERSION)
- # sha256.tbs-internet.com needs SNI to use the correct certificate
- if not ssl.HAS_SNI:
- self.skipTest("SNI needed for this test")
- # https://sha2.hboeck.de/ was used until 2011-01-08 (no route to host)
- remote = ("sha256.tbs-internet.com", 443)
- sha256_cert = os.path.join(os.path.dirname(__file__), "sha256.pem")
- with support.transient_internet("sha256.tbs-internet.com"):
- ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
- ctx.verify_mode = ssl.CERT_REQUIRED
- ctx.load_verify_locations(sha256_cert)
- s = ctx.wrap_socket(socket.socket(socket.AF_INET),
- server_hostname="sha256.tbs-internet.com")
- try:
- s.connect(remote)
- if support.verbose:
- sys.stdout.write("\nCipher with %r is %r\n" %
- (remote, s.cipher()))
- sys.stdout.write("Certificate is:\n%s\n" %
- pprint.pformat(s.getpeercert()))
- finally:
- s.close()
+ with test_wrap_socket(socket.socket(socket.AF_INET),
+ cert_reqs=ssl.CERT_NONE, ciphers="ALL") as s:
+ s.connect(self.server_addr)
+ with test_wrap_socket(socket.socket(socket.AF_INET),
+ cert_reqs=ssl.CERT_NONE, ciphers="DEFAULT") as s:
+ s.connect(self.server_addr)
+ # Error checking can happen at instantiation or when connecting
+ with self.assertRaisesRegex(ssl.SSLError, "No cipher can be selected"):
+ with socket.socket(socket.AF_INET) as sock:
+ s = test_wrap_socket(sock,
+ cert_reqs=ssl.CERT_NONE, ciphers="^$:,;?*'dorothyx")
+ s.connect(self.server_addr)
def test_get_ca_certs_capath(self):
# capath certs are loaded on request
- with support.transient_internet(REMOTE_HOST):
- ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
- ctx.verify_mode = ssl.CERT_REQUIRED
- ctx.load_verify_locations(capath=CAPATH)
- self.assertEqual(ctx.get_ca_certs(), [])
- s = ctx.wrap_socket(socket.socket(socket.AF_INET))
- s.connect((REMOTE_HOST, 443))
- try:
- cert = s.getpeercert()
- self.assertTrue(cert)
- finally:
- s.close()
- self.assertEqual(len(ctx.get_ca_certs()), 1)
+ ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
+ ctx.verify_mode = ssl.CERT_REQUIRED
+ ctx.load_verify_locations(capath=CAPATH)
+ self.assertEqual(ctx.get_ca_certs(), [])
+ with ctx.wrap_socket(socket.socket(socket.AF_INET)) as s:
+ s.connect(self.server_addr)
+ cert = s.getpeercert()
+ self.assertTrue(cert)
+ self.assertEqual(len(ctx.get_ca_certs()), 1)
@needs_sni
def test_context_setget(self):
# Check that the context of a connected socket can be replaced.
- with support.transient_internet(REMOTE_HOST):
- ctx1 = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
- ctx2 = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
- s = socket.socket(socket.AF_INET)
- with ctx1.wrap_socket(s) as ss:
- ss.connect((REMOTE_HOST, 443))
- self.assertIs(ss.context, ctx1)
- self.assertIs(ss._sslobj.context, ctx1)
- ss.context = ctx2
- self.assertIs(ss.context, ctx2)
- self.assertIs(ss._sslobj.context, ctx2)
-
-
-class NetworkedBIOTests(unittest.TestCase):
+ ctx1 = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
+ ctx2 = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
+ s = socket.socket(socket.AF_INET)
+ with ctx1.wrap_socket(s) as ss:
+ ss.connect(self.server_addr)
+ self.assertIs(ss.context, ctx1)
+ self.assertIs(ss._sslobj.context, ctx1)
+ ss.context = ctx2
+ self.assertIs(ss.context, ctx2)
+ self.assertIs(ss._sslobj.context, ctx2)
def ssl_io_loop(self, sock, incoming, outgoing, func, *args, **kwargs):
# A simple IO loop. Call func(*args) depending on the error we get
@@ -1765,64 +1723,128 @@ class NetworkedBIOTests(unittest.TestCase):
% (count, func.__name__))
return ret
- def test_handshake(self):
+ def test_bio_handshake(self):
+ sock = socket.socket(socket.AF_INET)
+ self.addCleanup(sock.close)
+ sock.connect(self.server_addr)
+ incoming = ssl.MemoryBIO()
+ outgoing = ssl.MemoryBIO()
+ ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
+ ctx.verify_mode = ssl.CERT_REQUIRED
+ ctx.load_verify_locations(SIGNING_CA)
+ ctx.check_hostname = True
+ sslobj = ctx.wrap_bio(incoming, outgoing, False, 'localhost')
+ self.assertIs(sslobj._sslobj.owner, sslobj)
+ self.assertIsNone(sslobj.cipher())
+ self.assertIsNotNone(sslobj.shared_ciphers())
+ self.assertRaises(ValueError, sslobj.getpeercert)
+ if 'tls-unique' in ssl.CHANNEL_BINDING_TYPES:
+ self.assertIsNone(sslobj.get_channel_binding('tls-unique'))
+ self.ssl_io_loop(sock, incoming, outgoing, sslobj.do_handshake)
+ self.assertTrue(sslobj.cipher())
+ self.assertIsNotNone(sslobj.shared_ciphers())
+ self.assertTrue(sslobj.getpeercert())
+ if 'tls-unique' in ssl.CHANNEL_BINDING_TYPES:
+ self.assertTrue(sslobj.get_channel_binding('tls-unique'))
+ try:
+ self.ssl_io_loop(sock, incoming, outgoing, sslobj.unwrap)
+ except ssl.SSLSyscallError:
+ # If the server shuts down the TCP connection without sending a
+ # secure shutdown message, this is reported as SSL_ERROR_SYSCALL
+ pass
+ self.assertRaises(ssl.SSLError, sslobj.write, b'foo')
+
+ def test_bio_read_write_data(self):
+ sock = socket.socket(socket.AF_INET)
+ self.addCleanup(sock.close)
+ sock.connect(self.server_addr)
+ incoming = ssl.MemoryBIO()
+ outgoing = ssl.MemoryBIO()
+ ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
+ ctx.verify_mode = ssl.CERT_NONE
+ sslobj = ctx.wrap_bio(incoming, outgoing, False)
+ self.ssl_io_loop(sock, incoming, outgoing, sslobj.do_handshake)
+ req = b'FOO\n'
+ self.ssl_io_loop(sock, incoming, outgoing, sslobj.write, req)
+ buf = self.ssl_io_loop(sock, incoming, outgoing, sslobj.read, 1024)
+ self.assertEqual(buf, b'foo\n')
+ self.ssl_io_loop(sock, incoming, outgoing, sslobj.unwrap)
+
+
+class NetworkedTests(unittest.TestCase):
+
+ def test_timeout_connect_ex(self):
+ # Issue #12065: on a timeout, connect_ex() should return the original
+ # errno (mimicking the behaviour of non-SSL sockets).
with support.transient_internet(REMOTE_HOST):
- sock = socket.socket(socket.AF_INET)
- sock.connect((REMOTE_HOST, 443))
- incoming = ssl.MemoryBIO()
- outgoing = ssl.MemoryBIO()
- ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
+ s = test_wrap_socket(socket.socket(socket.AF_INET),
+ cert_reqs=ssl.CERT_REQUIRED,
+ do_handshake_on_connect=False)
+ self.addCleanup(s.close)
+ s.settimeout(0.0000001)
+ rc = s.connect_ex((REMOTE_HOST, 443))
+ if rc == 0:
+ self.skipTest("REMOTE_HOST responded too quickly")
+ self.assertIn(rc, (errno.EAGAIN, errno.EWOULDBLOCK))
+
+ @unittest.skipUnless(support.IPV6_ENABLED, 'Needs IPv6')
+ def test_get_server_certificate_ipv6(self):
+ with support.transient_internet('ipv6.google.com'):
+ _test_get_server_certificate(self, 'ipv6.google.com', 443)
+ _test_get_server_certificate_fail(self, 'ipv6.google.com', 443)
+
+ def test_algorithms(self):
+ # Issue #8484: all algorithms should be available when verifying a
+ # certificate.
+ # SHA256 was added in OpenSSL 0.9.8
+ if ssl.OPENSSL_VERSION_INFO < (0, 9, 8, 0, 15):
+ self.skipTest("SHA256 not available on %r" % ssl.OPENSSL_VERSION)
+ # sha256.tbs-internet.com needs SNI to use the correct certificate
+ if not ssl.HAS_SNI:
+ self.skipTest("SNI needed for this test")
+ # https://sha2.hboeck.de/ was used until 2011-01-08 (no route to host)
+ remote = ("sha256.tbs-internet.com", 443)
+ sha256_cert = os.path.join(os.path.dirname(__file__), "sha256.pem")
+ with support.transient_internet("sha256.tbs-internet.com"):
+ ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
ctx.verify_mode = ssl.CERT_REQUIRED
- ctx.load_verify_locations(REMOTE_ROOT_CERT)
- ctx.check_hostname = True
- sslobj = ctx.wrap_bio(incoming, outgoing, False, REMOTE_HOST)
- self.assertIs(sslobj._sslobj.owner, sslobj)
- self.assertIsNone(sslobj.cipher())
- self.assertIsNotNone(sslobj.shared_ciphers())
- self.assertRaises(ValueError, sslobj.getpeercert)
- if 'tls-unique' in ssl.CHANNEL_BINDING_TYPES:
- self.assertIsNone(sslobj.get_channel_binding('tls-unique'))
- self.ssl_io_loop(sock, incoming, outgoing, sslobj.do_handshake)
- self.assertTrue(sslobj.cipher())
- self.assertIsNotNone(sslobj.shared_ciphers())
- self.assertTrue(sslobj.getpeercert())
- if 'tls-unique' in ssl.CHANNEL_BINDING_TYPES:
- self.assertTrue(sslobj.get_channel_binding('tls-unique'))
+ ctx.load_verify_locations(sha256_cert)
+ s = ctx.wrap_socket(socket.socket(socket.AF_INET),
+ server_hostname="sha256.tbs-internet.com")
try:
- self.ssl_io_loop(sock, incoming, outgoing, sslobj.unwrap)
- except ssl.SSLSyscallError:
- # self-signed.pythontest.net probably shuts down the TCP
- # connection without sending a secure shutdown message, and
- # this is reported as SSL_ERROR_SYSCALL
- pass
- self.assertRaises(ssl.SSLError, sslobj.write, b'foo')
- sock.close()
+ s.connect(remote)
+ if support.verbose:
+ sys.stdout.write("\nCipher with %r is %r\n" %
+ (remote, s.cipher()))
+ sys.stdout.write("Certificate is:\n%s\n" %
+ pprint.pformat(s.getpeercert()))
+ finally:
+ s.close()
- def test_read_write_data(self):
- with support.transient_internet(REMOTE_HOST):
- sock = socket.socket(socket.AF_INET)
- sock.connect((REMOTE_HOST, 443))
- incoming = ssl.MemoryBIO()
- outgoing = ssl.MemoryBIO()
- ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
- ctx.verify_mode = ssl.CERT_NONE
- sslobj = ctx.wrap_bio(incoming, outgoing, False)
- self.ssl_io_loop(sock, incoming, outgoing, sslobj.do_handshake)
- req = b'GET / HTTP/1.0\r\n\r\n'
- self.ssl_io_loop(sock, incoming, outgoing, sslobj.write, req)
- buf = self.ssl_io_loop(sock, incoming, outgoing, sslobj.read, 1024)
- self.assertEqual(buf[:5], b'HTTP/')
- self.ssl_io_loop(sock, incoming, outgoing, sslobj.unwrap)
- sock.close()
+def _test_get_server_certificate(test, host, port, cert=None):
+ pem = ssl.get_server_certificate((host, port))
+ if not pem:
+ test.fail("No server certificate on %s:%s!" % (host, port))
+
+ pem = ssl.get_server_certificate((host, port), ca_certs=cert)
+ if not pem:
+ test.fail("No server certificate on %s:%s!" % (host, port))
+ if support.verbose:
+ sys.stdout.write("\nVerified certificate for %s:%s is\n%s\n" % (host, port ,pem))
+
+def _test_get_server_certificate_fail(test, host, port):
+ try:
+ pem = ssl.get_server_certificate((host, port), ca_certs=CERTFILE)
+ except ssl.SSLError as x:
+ #should fail
+ if support.verbose:
+ sys.stdout.write("%s\n" % x)
+ else:
+ test.fail("Got server certificate %s for %s:%s!" % (pem, host, port))
-try:
- import threading
-except ImportError:
- _have_threads = False
-else:
- _have_threads = True
+if _have_threads:
from test.ssl_servers import make_https_server
class ThreadedEchoServer(threading.Thread):
@@ -1910,6 +1932,15 @@ else:
if not stripped:
# eof, so quit this handler
self.running = False
+ try:
+ self.sock = self.sslconn.unwrap()
+ except OSError:
+ # Many tests shut the TCP connection down
+ # without an SSL shutdown. This causes
+ # unwrap() to raise OSError with errno=0!
+ pass
+ else:
+ self.sslconn = None
self.close()
elif stripped == b'over':
if support.verbose and self.server.connectionchatty:
@@ -2037,7 +2068,7 @@ else:
class ConnectionHandler (asyncore.dispatcher_with_send):
def __init__(self, conn, certfile):
- self.socket = ssl.wrap_socket(conn, server_side=True,
+ self.socket = test_wrap_socket(conn, server_side=True,
certfile=certfile,
do_handshake_on_connect=False)
asyncore.dispatcher_with_send.__init__(self, self.socket)
@@ -2145,7 +2176,8 @@ else:
self.server.close()
def server_params_test(client_context, server_context, indata=b"FOO\n",
- chatty=True, connectionchatty=False, sni_name=None):
+ chatty=True, connectionchatty=False, sni_name=None,
+ session=None):
"""
Launch a server, connect a client to it and try various reads
and writes.
@@ -2156,7 +2188,7 @@ else:
connectionchatty=False)
with server:
with client_context.wrap_socket(socket.socket(),
- server_hostname=sni_name) as s:
+ server_hostname=sni_name, session=session) as s:
s.connect((HOST, server.port))
for arg in [indata, bytearray(indata), memoryview(indata)]:
if connectionchatty:
@@ -2184,6 +2216,8 @@ else:
'client_alpn_protocol': s.selected_alpn_protocol(),
'client_npn_protocol': s.selected_npn_protocol(),
'version': s.version(),
+ 'session_reused': s.session_reused,
+ 'session': s.session,
})
s.close()
stats['server_alpn_protocols'] = server.selected_alpn_protocols
@@ -2259,12 +2293,53 @@ else:
if support.verbose:
sys.stdout.write("\n")
for protocol in PROTOCOLS:
+ if protocol in {ssl.PROTOCOL_TLS_CLIENT, ssl.PROTOCOL_TLS_SERVER}:
+ continue
with self.subTest(protocol=ssl._PROTOCOL_NAMES[protocol]):
context = ssl.SSLContext(protocol)
context.load_cert_chain(CERTFILE)
server_params_test(context, context,
chatty=True, connectionchatty=True)
+ client_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
+ client_context.load_verify_locations(SIGNING_CA)
+ server_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
+ # server_context.load_verify_locations(SIGNING_CA)
+ server_context.load_cert_chain(SIGNED_CERTFILE2)
+
+ with self.subTest(client=ssl.PROTOCOL_TLS_CLIENT, server=ssl.PROTOCOL_TLS_SERVER):
+ server_params_test(client_context=client_context,
+ server_context=server_context,
+ chatty=True, connectionchatty=True,
+ sni_name='fakehostname')
+
+ client_context.check_hostname = False
+ with self.subTest(client=ssl.PROTOCOL_TLS_SERVER, server=ssl.PROTOCOL_TLS_CLIENT):
+ with self.assertRaises(ssl.SSLError) as e:
+ server_params_test(client_context=server_context,
+ server_context=client_context,
+ chatty=True, connectionchatty=True,
+ sni_name='fakehostname')
+ self.assertIn('called a function you should not call',
+ str(e.exception))
+
+ with self.subTest(client=ssl.PROTOCOL_TLS_SERVER, server=ssl.PROTOCOL_TLS_SERVER):
+ with self.assertRaises(ssl.SSLError) as e:
+ server_params_test(client_context=server_context,
+ server_context=server_context,
+ chatty=True, connectionchatty=True)
+ self.assertIn('called a function you should not call',
+ str(e.exception))
+
+ with self.subTest(client=ssl.PROTOCOL_TLS_CLIENT, server=ssl.PROTOCOL_TLS_CLIENT):
+ with self.assertRaises(ssl.SSLError) as e:
+ server_params_test(client_context=server_context,
+ server_context=client_context,
+ chatty=True, connectionchatty=True)
+ self.assertIn('called a function you should not call',
+ str(e.exception))
+
+
def test_getpeercert(self):
if support.verbose:
sys.stdout.write("\n")
@@ -2398,7 +2473,7 @@ else:
connectionchatty=False)
with server, \
socket.socket() as sock, \
- ssl.wrap_socket(sock,
+ test_wrap_socket(sock,
certfile=certfile,
ssl_version=ssl.PROTOCOL_TLSv1) as s:
try:
@@ -2445,7 +2520,7 @@ else:
c.connect((HOST, port))
listener_gone.wait()
try:
- ssl_sock = ssl.wrap_socket(c)
+ ssl_sock = test_wrap_socket(c)
except OSError:
pass
else:
@@ -2635,7 +2710,7 @@ else:
sys.stdout.write(
" client: read %r from server, starting TLS...\n"
% msg)
- conn = ssl.wrap_socket(s, ssl_version=ssl.PROTOCOL_TLSv1)
+ conn = test_wrap_socket(s, ssl_version=ssl.PROTOCOL_TLSv1)
wrapped = True
elif indata == b"ENDTLS" and msg.startswith(b"ok"):
# ENDTLS ok, switch back to clear text
@@ -2694,7 +2769,7 @@ else:
indata = b"FOO\n"
server = AsyncoreEchoServer(CERTFILE)
with server:
- s = ssl.wrap_socket(socket.socket())
+ s = test_wrap_socket(socket.socket())
s.connect(('127.0.0.1', server.port))
if support.verbose:
sys.stdout.write(
@@ -2727,7 +2802,7 @@ else:
chatty=True,
connectionchatty=False)
with server:
- s = ssl.wrap_socket(socket.socket(),
+ s = test_wrap_socket(socket.socket(),
server_side=False,
certfile=CERTFILE,
ca_certs=CERTFILE,
@@ -2745,12 +2820,13 @@ else:
count, addr = s.recvfrom_into(b)
return b[:count]
- # (name, method, whether to expect success, *args)
+ # (name, method, expect success?, *args, return value func)
send_methods = [
- ('send', s.send, True, []),
- ('sendto', s.sendto, False, ["some.address"]),
- ('sendall', s.sendall, True, []),
+ ('send', s.send, True, [], len),
+ ('sendto', s.sendto, False, ["some.address"], len),
+ ('sendall', s.sendall, True, [], lambda x: None),
]
+ # (name, method, whether to expect success, *args)
recv_methods = [
('recv', s.recv, True, []),
('recvfrom', s.recvfrom, False, ["some.address"]),
@@ -2759,10 +2835,13 @@ else:
]
data_prefix = "PREFIX_"
- for meth_name, send_meth, expect_success, args in send_methods:
+ for (meth_name, send_meth, expect_success, args,
+ ret_val_meth) in send_methods:
indata = (data_prefix + meth_name).encode('ascii')
try:
- send_meth(indata, *args)
+ ret = send_meth(indata, *args)
+ msg = "sending with {}".format(meth_name)
+ self.assertEqual(ret, ret_val_meth(indata), msg=msg)
outdata = s.read()
if outdata != indata.lower():
self.fail(
@@ -2847,7 +2926,7 @@ else:
self.addCleanup(server.__exit__, None, None)
s = socket.create_connection((HOST, server.port))
self.addCleanup(s.close)
- s = ssl.wrap_socket(s, suppress_ragged_eofs=False)
+ s = test_wrap_socket(s, suppress_ragged_eofs=False)
self.addCleanup(s.close)
# recv/read(0) should return no data
@@ -2869,7 +2948,7 @@ else:
chatty=True,
connectionchatty=False)
with server:
- s = ssl.wrap_socket(socket.socket(),
+ s = test_wrap_socket(socket.socket(),
server_side=False,
certfile=CERTFILE,
ca_certs=CERTFILE,
@@ -2923,12 +3002,12 @@ else:
c.connect((host, port))
# Will attempt handshake and time out
self.assertRaisesRegex(socket.timeout, "timed out",
- ssl.wrap_socket, c)
+ test_wrap_socket, c)
finally:
c.close()
try:
c = socket.socket(socket.AF_INET)
- c = ssl.wrap_socket(c)
+ c = test_wrap_socket(c)
c.settimeout(0.2)
# Will attempt handshake and time out
self.assertRaisesRegex(socket.timeout, "timed out",
@@ -2951,6 +3030,7 @@ else:
host = "127.0.0.1"
port = support.bind_port(server)
server = context.wrap_socket(server, server_side=True)
+ self.assertTrue(server.server_side)
evt = threading.Event()
remote = None
@@ -3053,7 +3133,7 @@ else:
chatty=True,
connectionchatty=False)
with server:
- s = ssl.wrap_socket(socket.socket(),
+ s = test_wrap_socket(socket.socket(),
server_side=False,
certfile=CERTFILE,
ca_certs=CERTFILE,
@@ -3078,7 +3158,7 @@ else:
s.close()
# now, again
- s = ssl.wrap_socket(socket.socket(),
+ s = test_wrap_socket(socket.socket(),
server_side=False,
certfile=CERTFILE,
ca_certs=CERTFILE,
@@ -3388,6 +3468,111 @@ else:
s.sendfile(file)
self.assertEqual(s.recv(1024), TEST_DATA)
+ def test_session(self):
+ server_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
+ server_context.load_cert_chain(SIGNED_CERTFILE)
+ client_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
+ client_context.verify_mode = ssl.CERT_REQUIRED
+ client_context.load_verify_locations(SIGNING_CA)
+
+ # first connection without session
+ stats = server_params_test(client_context, server_context)
+ session = stats['session']
+ self.assertTrue(session.id)
+ self.assertGreater(session.time, 0)
+ self.assertGreater(session.timeout, 0)
+ self.assertTrue(session.has_ticket)
+ if ssl.OPENSSL_VERSION_INFO > (1, 0, 1):
+ self.assertGreater(session.ticket_lifetime_hint, 0)
+ self.assertFalse(stats['session_reused'])
+ sess_stat = server_context.session_stats()
+ self.assertEqual(sess_stat['accept'], 1)
+ self.assertEqual(sess_stat['hits'], 0)
+
+ # reuse session
+ stats = server_params_test(client_context, server_context, session=session)
+ sess_stat = server_context.session_stats()
+ self.assertEqual(sess_stat['accept'], 2)
+ self.assertEqual(sess_stat['hits'], 1)
+ self.assertTrue(stats['session_reused'])
+ session2 = stats['session']
+ self.assertEqual(session2.id, session.id)
+ self.assertEqual(session2, session)
+ self.assertIsNot(session2, session)
+ self.assertGreaterEqual(session2.time, session.time)
+ self.assertGreaterEqual(session2.timeout, session.timeout)
+
+ # another one without session
+ stats = server_params_test(client_context, server_context)
+ self.assertFalse(stats['session_reused'])
+ session3 = stats['session']
+ self.assertNotEqual(session3.id, session.id)
+ self.assertNotEqual(session3, session)
+ sess_stat = server_context.session_stats()
+ self.assertEqual(sess_stat['accept'], 3)
+ self.assertEqual(sess_stat['hits'], 1)
+
+ # reuse session again
+ stats = server_params_test(client_context, server_context, session=session)
+ self.assertTrue(stats['session_reused'])
+ session4 = stats['session']
+ self.assertEqual(session4.id, session.id)
+ self.assertEqual(session4, session)
+ self.assertGreaterEqual(session4.time, session.time)
+ self.assertGreaterEqual(session4.timeout, session.timeout)
+ sess_stat = server_context.session_stats()
+ self.assertEqual(sess_stat['accept'], 4)
+ self.assertEqual(sess_stat['hits'], 2)
+
+ def test_session_handling(self):
+ context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
+ context.verify_mode = ssl.CERT_REQUIRED
+ context.load_verify_locations(CERTFILE)
+ context.load_cert_chain(CERTFILE)
+
+ context2 = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
+ context2.verify_mode = ssl.CERT_REQUIRED
+ context2.load_verify_locations(CERTFILE)
+ context2.load_cert_chain(CERTFILE)
+
+ server = ThreadedEchoServer(context=context, chatty=False)
+ with server:
+ with context.wrap_socket(socket.socket()) as s:
+ # session is None before handshake
+ self.assertEqual(s.session, None)
+ self.assertEqual(s.session_reused, None)
+ s.connect((HOST, server.port))
+ session = s.session
+ self.assertTrue(session)
+ with self.assertRaises(TypeError) as e:
+ s.session = object
+ self.assertEqual(str(e.exception), 'Value is not a SSLSession.')
+
+ with context.wrap_socket(socket.socket()) as s:
+ s.connect((HOST, server.port))
+ # cannot set session after handshake
+ with self.assertRaises(ValueError) as e:
+ s.session = session
+ self.assertEqual(str(e.exception),
+ 'Cannot set session after handshake.')
+
+ with context.wrap_socket(socket.socket()) as s:
+ # can set session before handshake and before the
+ # connection was established
+ s.session = session
+ s.connect((HOST, server.port))
+ self.assertEqual(s.session.id, session.id)
+ self.assertEqual(s.session, session)
+ self.assertEqual(s.session_reused, True)
+
+ with context2.wrap_socket(socket.socket()) as s:
+ # cannot re-use session with a different SSLContext
+ with self.assertRaises(ValueError) as e:
+ s.session = session
+ s.connect((HOST, server.port))
+ self.assertEqual(str(e.exception),
+ 'Session refers to a different SSLContext.')
+
def test_main(verbose=False):
if support.verbose:
@@ -3400,7 +3585,7 @@ def test_main(verbose=False):
with warnings.catch_warnings():
warnings.filterwarnings(
'ignore',
- 'dist\(\) and linux_distribution\(\) '
+ r'dist\(\) and linux_distribution\(\) '
'functions are deprecated .*',
PendingDeprecationWarning,
)
@@ -3422,18 +3607,20 @@ def test_main(verbose=False):
pass
for filename in [
- CERTFILE, REMOTE_ROOT_CERT, BYTES_CERTFILE,
+ CERTFILE, BYTES_CERTFILE,
ONLYCERT, ONLYKEY, BYTES_ONLYCERT, BYTES_ONLYKEY,
SIGNED_CERTFILE, SIGNED_CERTFILE2, SIGNING_CA,
BADCERT, BADKEY, EMPTYCERT]:
if not os.path.exists(filename):
raise support.TestFailed("Can't read certificate file %r" % filename)
- tests = [ContextTests, BasicSocketTests, SSLErrorTests, MemoryBIOTests]
+ tests = [
+ ContextTests, BasicSocketTests, SSLErrorTests, MemoryBIOTests,
+ SimpleBackgroundTests,
+ ]
if support.is_resource_enabled('network'):
tests.append(NetworkedTests)
- tests.append(NetworkedBIOTests)
if _have_threads:
thread_info = support.threading_setup()
diff --git a/Lib/test/test_stat.py b/Lib/test/test_stat.py
index f1a5938..cd02a6e 100644
--- a/Lib/test/test_stat.py
+++ b/Lib/test/test_stat.py
@@ -1,7 +1,7 @@
import unittest
import os
import sys
-from test.support import TESTFN, import_fresh_module
+from test.support import TESTFN, import_fresh_module, android_not_root
c_stat = import_fresh_module('stat', fresh=['_stat'])
py_stat = import_fresh_module('stat', blocked=['_stat'])
@@ -168,6 +168,7 @@ class TestFilemode:
self.assertS_IS("LNK", st_mode)
@unittest.skipUnless(hasattr(os, 'mkfifo'), 'os.mkfifo not available')
+ @unittest.skipIf(android_not_root, "mkfifo not allowed, non root user")
def test_fifo(self):
os.mkfifo(TESTFN, 0o700)
st_mode, modestr = self.get_mode()
diff --git a/Lib/test/test_statistics.py b/Lib/test/test_statistics.py
index 0089ae8..4b3fd36 100644
--- a/Lib/test/test_statistics.py
+++ b/Lib/test/test_statistics.py
@@ -21,6 +21,10 @@ import statistics
# === Helper functions and class ===
+def sign(x):
+ """Return -1.0 for negatives, including -0.0, otherwise +1.0."""
+ return math.copysign(1, x)
+
def _nan_equal(a, b):
"""Return True if a and b are both the same kind of NAN.
@@ -264,6 +268,13 @@ class NumericTestCase(unittest.TestCase):
# === Test the helpers ===
# ========================
+class TestSign(unittest.TestCase):
+ """Test that the helper function sign() works correctly."""
+ def testZeroes(self):
+ # Test that signed zeroes report their sign correctly.
+ self.assertEqual(sign(0.0), +1)
+ self.assertEqual(sign(-0.0), -1)
+
# --- Tests for approx_equal ---
@@ -659,7 +670,7 @@ class DocTests(unittest.TestCase):
@unittest.skipIf(sys.flags.optimize >= 2,
"Docstrings are omitted with -OO and above")
def test_doc_tests(self):
- failed, tried = doctest.testmod(statistics)
+ failed, tried = doctest.testmod(statistics, optionflags=doctest.ELLIPSIS)
self.assertGreater(tried, 0)
self.assertEqual(failed, 0)
@@ -702,9 +713,9 @@ class ExactRatioTest(unittest.TestCase):
def test_decimal(self):
D = Decimal
_exact_ratio = statistics._exact_ratio
- self.assertEqual(_exact_ratio(D("0.125")), (125, 1000))
- self.assertEqual(_exact_ratio(D("12.345")), (12345, 1000))
- self.assertEqual(_exact_ratio(D("-1.98")), (-198, 100))
+ self.assertEqual(_exact_ratio(D("0.125")), (1, 8))
+ self.assertEqual(_exact_ratio(D("12.345")), (2469, 200))
+ self.assertEqual(_exact_ratio(D("-1.98")), (-99, 50))
def test_inf(self):
INF = float("INF")
@@ -743,18 +754,18 @@ class ExactRatioTest(unittest.TestCase):
class DecimalToRatioTest(unittest.TestCase):
- # Test _decimal_to_ratio private function.
+ # Test _exact_ratio private function.
def test_infinity(self):
# Test that INFs are handled correctly.
inf = Decimal('INF')
- self.assertEqual(statistics._decimal_to_ratio(inf), (inf, None))
- self.assertEqual(statistics._decimal_to_ratio(-inf), (-inf, None))
+ self.assertEqual(statistics._exact_ratio(inf), (inf, None))
+ self.assertEqual(statistics._exact_ratio(-inf), (-inf, None))
def test_nan(self):
# Test that NANs are handled correctly.
for nan in (Decimal('NAN'), Decimal('sNAN')):
- num, den = statistics._decimal_to_ratio(nan)
+ num, den = statistics._exact_ratio(nan)
# Because NANs always compare non-equal, we cannot use assertEqual.
# Nor can we use an identity test, as we don't guarantee anything
# about the object identity.
@@ -767,30 +778,30 @@ class DecimalToRatioTest(unittest.TestCase):
for d in numbers:
# First test positive decimals.
assert d > 0
- num, den = statistics._decimal_to_ratio(d)
+ num, den = statistics._exact_ratio(d)
self.assertGreaterEqual(num, 0)
self.assertGreater(den, 0)
# Then test negative decimals.
- num, den = statistics._decimal_to_ratio(-d)
+ num, den = statistics._exact_ratio(-d)
self.assertLessEqual(num, 0)
self.assertGreater(den, 0)
def test_negative_exponent(self):
# Test result when the exponent is negative.
- t = statistics._decimal_to_ratio(Decimal("0.1234"))
- self.assertEqual(t, (1234, 10000))
+ t = statistics._exact_ratio(Decimal("0.1234"))
+ self.assertEqual(t, (617, 5000))
def test_positive_exponent(self):
# Test results when the exponent is positive.
- t = statistics._decimal_to_ratio(Decimal("1.234e7"))
+ t = statistics._exact_ratio(Decimal("1.234e7"))
self.assertEqual(t, (12340000, 1))
def test_regression_20536(self):
# Regression test for issue 20536.
# See http://bugs.python.org/issue20536
- t = statistics._decimal_to_ratio(Decimal("1e2"))
+ t = statistics._exact_ratio(Decimal("1e2"))
self.assertEqual(t, (100, 1))
- t = statistics._decimal_to_ratio(Decimal("1.47e5"))
+ t = statistics._exact_ratio(Decimal("1.47e5"))
self.assertEqual(t, (147000, 1))
@@ -971,6 +982,34 @@ class ConvertTest(unittest.TestCase):
self.assertTrue(_nan_equal(x, nan))
+class FailNegTest(unittest.TestCase):
+ """Test _fail_neg private function."""
+
+ def test_pass_through(self):
+ # Test that values are passed through unchanged.
+ values = [1, 2.0, Fraction(3), Decimal(4)]
+ new = list(statistics._fail_neg(values))
+ self.assertEqual(values, new)
+
+ def test_negatives_raise(self):
+ # Test that negatives raise an exception.
+ for x in [1, 2.0, Fraction(3), Decimal(4)]:
+ seq = [-x]
+ it = statistics._fail_neg(seq)
+ self.assertRaises(statistics.StatisticsError, next, it)
+
+ def test_error_msg(self):
+ # Test that a given error message is used.
+ msg = "badness #%d" % random.randint(10000, 99999)
+ try:
+ next(statistics._fail_neg([-1], msg))
+ except statistics.StatisticsError as e:
+ errmsg = e.args[0]
+ else:
+ self.fail("expected exception, but it didn't happen")
+ self.assertEqual(errmsg, msg)
+
+
# === Tests for public functions ===
class UnivariateCommonMixin:
@@ -1082,13 +1121,13 @@ class UnivariateTypeMixin:
Not all tests to do with types need go in this class. Only those that
rely on the function returning the same type as its input data.
"""
- def test_types_conserved(self):
- # Test that functions keeps the same type as their data points.
- # (Excludes mixed data types.) This only tests the type of the return
- # result, not the value.
+ def prepare_types_for_conservation_test(self):
+ """Return the types which are expected to be conserved."""
class MyFloat(float):
def __truediv__(self, other):
return type(self)(super().__truediv__(other))
+ def __rtruediv__(self, other):
+ return type(self)(super().__rtruediv__(other))
def __sub__(self, other):
return type(self)(super().__sub__(other))
def __rsub__(self, other):
@@ -1098,9 +1137,14 @@ class UnivariateTypeMixin:
def __add__(self, other):
return type(self)(super().__add__(other))
__radd__ = __add__
+ return (float, Decimal, Fraction, MyFloat)
+ def test_types_conserved(self):
+ # Test that functions keeps the same type as their data points.
+ # (Excludes mixed data types.) This only tests the type of the return
+ # result, not the value.
data = self.prepare_data()
- for kind in (float, Decimal, Fraction, MyFloat):
+ for kind in self.prepare_types_for_conservation_test():
d = [kind(x) for x in data]
result = self.func(d)
self.assertIs(type(result), kind)
@@ -1275,12 +1319,16 @@ class AverageMixin(UnivariateCommonMixin):
for x in (23, 42.5, 1.3e15, Fraction(15, 19), Decimal('0.28')):
self.assertEqual(self.func([x]), x)
+ def prepare_values_for_repeated_single_test(self):
+ return (3.5, 17, 2.5e15, Fraction(61, 67), Decimal('4.9712'))
+
def test_repeated_single_value(self):
# The average of a single repeated value is the value itself.
- for x in (3.5, 17, 2.5e15, Fraction(61, 67), Decimal('4.9712')):
+ for x in self.prepare_values_for_repeated_single_test():
for count in (2, 5, 10, 20):
- data = [x]*count
- self.assertEqual(self.func(data), x)
+ with self.subTest(x=x, count=count):
+ data = [x]*count
+ self.assertEqual(self.func(data), x)
class TestMean(NumericTestCase, AverageMixin, UnivariateTypeMixin):
@@ -1304,7 +1352,7 @@ class TestMean(NumericTestCase, AverageMixin, UnivariateTypeMixin):
self.assertEqual(self.func(data), 22.015625)
def test_decimals(self):
- # Test mean with ints.
+ # Test mean with Decimals.
D = Decimal
data = [D("1.634"), D("2.517"), D("3.912"), D("4.072"), D("5.813")]
random.shuffle(data)
@@ -1379,6 +1427,94 @@ class TestMean(NumericTestCase, AverageMixin, UnivariateTypeMixin):
self.assertEqual(statistics.mean([tiny]*n), tiny)
+class TestHarmonicMean(NumericTestCase, AverageMixin, UnivariateTypeMixin):
+ def setUp(self):
+ self.func = statistics.harmonic_mean
+
+ def prepare_data(self):
+ # Override mixin method.
+ values = super().prepare_data()
+ values.remove(0)
+ return values
+
+ def prepare_values_for_repeated_single_test(self):
+ # Override mixin method.
+ return (3.5, 17, 2.5e15, Fraction(61, 67), Decimal('4.125'))
+
+ def test_zero(self):
+ # Test that harmonic mean returns zero when given zero.
+ values = [1, 0, 2]
+ self.assertEqual(self.func(values), 0)
+
+ def test_negative_error(self):
+ # Test that harmonic mean raises when given a negative value.
+ exc = statistics.StatisticsError
+ for values in ([-1], [1, -2, 3]):
+ with self.subTest(values=values):
+ self.assertRaises(exc, self.func, values)
+
+ def test_ints(self):
+ # Test harmonic mean with ints.
+ data = [2, 4, 4, 8, 16, 16]
+ random.shuffle(data)
+ self.assertEqual(self.func(data), 6*4/5)
+
+ def test_floats_exact(self):
+ # Test harmonic mean with some carefully chosen floats.
+ data = [1/8, 1/4, 1/4, 1/2, 1/2]
+ random.shuffle(data)
+ self.assertEqual(self.func(data), 1/4)
+ self.assertEqual(self.func([0.25, 0.5, 1.0, 1.0]), 0.5)
+
+ def test_singleton_lists(self):
+ # Test that harmonic mean([x]) returns (approximately) x.
+ for x in range(1, 101):
+ self.assertEqual(self.func([x]), x)
+
+ def test_decimals_exact(self):
+ # Test harmonic mean with some carefully chosen Decimals.
+ D = Decimal
+ self.assertEqual(self.func([D(15), D(30), D(60), D(60)]), D(30))
+ data = [D("0.05"), D("0.10"), D("0.20"), D("0.20")]
+ random.shuffle(data)
+ self.assertEqual(self.func(data), D("0.10"))
+ data = [D("1.68"), D("0.32"), D("5.94"), D("2.75")]
+ random.shuffle(data)
+ self.assertEqual(self.func(data), D(66528)/70723)
+
+ def test_fractions(self):
+ # Test harmonic mean with Fractions.
+ F = Fraction
+ data = [F(1, 2), F(2, 3), F(3, 4), F(4, 5), F(5, 6), F(6, 7), F(7, 8)]
+ random.shuffle(data)
+ self.assertEqual(self.func(data), F(7*420, 4029))
+
+ def test_inf(self):
+ # Test harmonic mean with infinity.
+ values = [2.0, float('inf'), 1.0]
+ self.assertEqual(self.func(values), 2.0)
+
+ def test_nan(self):
+ # Test harmonic mean with NANs.
+ values = [2.0, float('nan'), 1.0]
+ self.assertTrue(math.isnan(self.func(values)))
+
+ def test_multiply_data_points(self):
+ # Test multiplying every data point by a constant.
+ c = 111
+ data = [3.4, 4.5, 4.9, 6.7, 6.8, 7.2, 8.0, 8.1, 9.4]
+ expected = self.func(data)*c
+ result = self.func([x*c for x in data])
+ self.assertEqual(result, expected)
+
+ def test_doubled_data(self):
+ # Harmonic mean of [a,b...z] should be same as for [a,a,b,b...z,z].
+ data = [random.uniform(1, 5) for _ in range(1000)]
+ expected = self.func(data)
+ actual = self.func(data*2)
+ self.assertApproxEqual(actual, expected)
+
+
class TestMedian(NumericTestCase, AverageMixin):
# Common tests for median and all median.* functions.
def setUp(self):
@@ -1600,6 +1736,22 @@ class TestMedianGrouped(TestMedian):
data = [220, 220, 240, 260, 260, 260, 260, 280, 280, 300, 320, 340]
self.assertEqual(self.func(data, 20), 265.0)
+ def test_data_type_error(self):
+ # Test median_grouped with str, bytes data types for data and interval
+ data = ["", "", ""]
+ self.assertRaises(TypeError, self.func, data)
+ #---
+ data = [b"", b"", b""]
+ self.assertRaises(TypeError, self.func, data)
+ #---
+ data = [1, 2, 3]
+ interval = ""
+ self.assertRaises(TypeError, self.func, data, interval)
+ #---
+ data = [1, 2, 3]
+ interval = b""
+ self.assertRaises(TypeError, self.func, data, interval)
+
class TestMode(NumericTestCase, AverageMixin, UnivariateTypeMixin):
# Test cases for the discrete version of mode.
diff --git a/Lib/test/test_strftime.py b/Lib/test/test_strftime.py
index 772cd06..72b1910 100644
--- a/Lib/test/test_strftime.py
+++ b/Lib/test/test_strftime.py
@@ -23,9 +23,9 @@ def escapestr(text, ampm):
"""
new_text = re.escape(text)
new_text = new_text.replace(re.escape(ampm), ampm)
- new_text = new_text.replace('\%', '%')
- new_text = new_text.replace('\:', ':')
- new_text = new_text.replace('\?', '?')
+ new_text = new_text.replace(r'\%', '%')
+ new_text = new_text.replace(r'\:', ':')
+ new_text = new_text.replace(r'\?', '?')
return new_text
diff --git a/Lib/test/test_strlit.py b/Lib/test/test_string_literals.py
index 87cffe8..aba4fc4 100644
--- a/Lib/test/test_strlit.py
+++ b/Lib/test/test_string_literals.py
@@ -31,6 +31,7 @@ import os
import sys
import shutil
import tempfile
+import warnings
import unittest
@@ -104,6 +105,29 @@ class TestLiterals(unittest.TestCase):
self.assertRaises(SyntaxError, eval, r""" '\U000000' """)
self.assertRaises(SyntaxError, eval, r""" '\U0000000' """)
+ def test_eval_str_invalid_escape(self):
+ for b in range(1, 128):
+ if b in b"""\n\r"'01234567NU\\abfnrtuvx""":
+ continue
+ with self.assertWarns(DeprecationWarning):
+ self.assertEqual(eval(r"'\%c'" % b), '\\' + chr(b))
+
+ with warnings.catch_warnings(record=True) as w:
+ warnings.simplefilter('always', category=DeprecationWarning)
+ eval("'''\n\\z'''")
+ self.assertEqual(len(w), 1)
+ self.assertEqual(w[0].filename, '<string>')
+ self.assertEqual(w[0].lineno, 2)
+
+ with warnings.catch_warnings(record=True) as w:
+ warnings.simplefilter('error', category=DeprecationWarning)
+ with self.assertRaises(SyntaxError) as cm:
+ eval("'''\n\\z'''")
+ exc = cm.exception
+ self.assertEqual(w, [])
+ self.assertEqual(exc.filename, '<string>')
+ self.assertEqual(exc.lineno, 2)
+
def test_eval_str_raw(self):
self.assertEqual(eval(""" r'x' """), 'x')
self.assertEqual(eval(r""" r'\x01' """), '\\' + 'x01')
@@ -121,15 +145,38 @@ class TestLiterals(unittest.TestCase):
self.assertEqual(eval(""" b'\x01' """), byte(1))
self.assertEqual(eval(r""" b'\x81' """), byte(0x81))
self.assertRaises(SyntaxError, eval, """ b'\x81' """)
- self.assertEqual(eval(r""" b'\u1881' """), b'\\' + b'u1881')
+ self.assertEqual(eval(r""" br'\u1881' """), b'\\' + b'u1881')
self.assertRaises(SyntaxError, eval, """ b'\u1881' """)
- self.assertEqual(eval(r""" b'\U0001d120' """), b'\\' + b'U0001d120')
+ self.assertEqual(eval(r""" br'\U0001d120' """), b'\\' + b'U0001d120')
self.assertRaises(SyntaxError, eval, """ b'\U0001d120' """)
def test_eval_bytes_incomplete(self):
self.assertRaises(SyntaxError, eval, r""" b'\x' """)
self.assertRaises(SyntaxError, eval, r""" b'\x0' """)
+ def test_eval_bytes_invalid_escape(self):
+ for b in range(1, 128):
+ if b in b"""\n\r"'01234567\\abfnrtvx""":
+ continue
+ with self.assertWarns(DeprecationWarning):
+ self.assertEqual(eval(r"b'\%c'" % b), b'\\' + bytes([b]))
+
+ with warnings.catch_warnings(record=True) as w:
+ warnings.simplefilter('always', category=DeprecationWarning)
+ eval("b'''\n\\z'''")
+ self.assertEqual(len(w), 1)
+ self.assertEqual(w[0].filename, '<string>')
+ self.assertEqual(w[0].lineno, 2)
+
+ with warnings.catch_warnings(record=True) as w:
+ warnings.simplefilter('error', category=DeprecationWarning)
+ with self.assertRaises(SyntaxError) as cm:
+ eval("b'''\n\\z'''")
+ exc = cm.exception
+ self.assertEqual(w, [])
+ self.assertEqual(exc.filename, '<string>')
+ self.assertEqual(exc.lineno, 2)
+
def test_eval_bytes_raw(self):
self.assertEqual(eval(""" br'x' """), b'x')
self.assertEqual(eval(""" rb'x' """), b'x')
diff --git a/Lib/test/test_strptime.py b/Lib/test/test_strptime.py
index 85126e6..2cf0926 100644
--- a/Lib/test/test_strptime.py
+++ b/Lib/test/test_strptime.py
@@ -5,7 +5,6 @@ import time
import locale
import re
import os
-import sys
from test import support
from datetime import date as datetime_date
@@ -130,7 +129,7 @@ class TimeRETests(unittest.TestCase):
def test_pattern_escaping(self):
# Make sure any characters in the format string that might be taken as
# regex syntax is escaped.
- pattern_string = self.time_re.pattern("\d+")
+ pattern_string = self.time_re.pattern(r"\d+")
self.assertIn(r"\\d\+", pattern_string,
"%s does not have re characters escaped properly" %
pattern_string)
@@ -153,8 +152,8 @@ class TimeRETests(unittest.TestCase):
"'%s' using '%s'; group 'a' = '%s', group 'b' = %s'" %
(found.string, found.re.pattern, found.group('a'),
found.group('b')))
- for directive in ('a','A','b','B','c','d','H','I','j','m','M','p','S',
- 'U','w','W','x','X','y','Y','Z','%'):
+ for directive in ('a','A','b','B','c','d','G','H','I','j','m','M','p',
+ 'S','u','U','V','w','W','x','X','y','Y','Z','%'):
compiled = self.time_re.compile("%" + directive)
found = compiled.match(time.strftime("%" + directive))
self.assertTrue(found, "Matching failed on '%s' using '%s' regex" %
@@ -171,9 +170,9 @@ class TimeRETests(unittest.TestCase):
def test_matching_with_escapes(self):
# Make sure a format that requires escaping of characters works
- compiled_re = self.time_re.compile("\w+ %m")
- found = compiled_re.match("\w+ 10")
- self.assertTrue(found, "Escaping failed of format '\w+ 10'")
+ compiled_re = self.time_re.compile(r"\w+ %m")
+ found = compiled_re.match(r"\w+ 10")
+ self.assertTrue(found, r"Escaping failed of format '\w+ 10'")
def test_locale_data_w_regex_metacharacters(self):
# Check that if locale data contains regex metacharacters they are
@@ -219,6 +218,26 @@ class StrptimeTests(unittest.TestCase):
else:
self.fail("'%s' did not raise ValueError" % bad_format)
+ # Ambiguous or incomplete cases using ISO year/week/weekday directives
+ # 1. ISO week (%V) is specified, but the year is specified with %Y
+ # instead of %G
+ with self.assertRaises(ValueError):
+ _strptime._strptime("1999 50", "%Y %V")
+ # 2. ISO year (%G) and ISO week (%V) are specified, but weekday is not
+ with self.assertRaises(ValueError):
+ _strptime._strptime("1999 51", "%G %V")
+ # 3. ISO year (%G) and weekday are specified, but ISO week (%V) is not
+ for w in ('A', 'a', 'w', 'u'):
+ with self.assertRaises(ValueError):
+ _strptime._strptime("1999 51","%G %{}".format(w))
+ # 4. ISO year is specified alone (e.g. time.strptime('2015', '%G'))
+ with self.assertRaises(ValueError):
+ _strptime._strptime("2015", "%G")
+ # 5. Julian/ordinal day (%j) is specified with %G, but not %Y
+ with self.assertRaises(ValueError):
+ _strptime._strptime("1999 256", "%G %j")
+
+
def test_strptime_exception_context(self):
# check that this doesn't chain exceptions needlessly (see #17572)
with self.assertRaises(ValueError) as e:
@@ -290,7 +309,7 @@ class StrptimeTests(unittest.TestCase):
def test_weekday(self):
# Test weekday directives
- for directive in ('A', 'a', 'w'):
+ for directive in ('A', 'a', 'w', 'u'):
self.helper(directive,6)
def test_julian(self):
@@ -384,7 +403,7 @@ class StrptimeTests(unittest.TestCase):
# unbalanced parentheses when the regex is compiled if they are not
# escaped.
# Test instigated by bug #796149 .
- need_escaping = ".^$*+?{}\[]|)("
+ need_escaping = r".^$*+?{}\[]|)("
self.assertTrue(_strptime._strptime_time(need_escaping, need_escaping))
def test_feb29_on_leap_year_without_year(self):
@@ -453,20 +472,37 @@ class CalculationTests(unittest.TestCase):
"Calculation of day of the week failed;"
"%s != %s" % (result.tm_wday, self.time_tuple.tm_wday))
+ if support.is_android:
+ # Issue #26929: strftime() on Android incorrectly formats %V or %G for
+ # the last or the first incomplete week in a year.
+ _ymd_excluded = ((1905, 1, 1), (1906, 12, 31), (2008, 12, 29),
+ (1917, 12, 31))
+ _formats_excluded = ('%G %V',)
+ else:
+ _ymd_excluded = ()
+ _formats_excluded = ()
+
def test_week_of_year_and_day_of_week_calculation(self):
# Should be able to infer date if given year, week of year (%U or %W)
# and day of the week
def test_helper(ymd_tuple, test_reason):
- for directive in ('W', 'U'):
- format_string = "%%Y %%%s %%w" % directive
- dt_date = datetime_date(*ymd_tuple)
- strp_input = dt_date.strftime(format_string)
- strp_output = _strptime._strptime_time(strp_input, format_string)
- self.assertTrue(strp_output[:3] == ymd_tuple,
- "%s(%s) test failed w/ '%s': %s != %s (%s != %s)" %
- (test_reason, directive, strp_input,
- strp_output[:3], ymd_tuple,
- strp_output[7], dt_date.timetuple()[7]))
+ for year_week_format in ('%Y %W', '%Y %U', '%G %V'):
+ if (year_week_format in self._formats_excluded and
+ ymd_tuple in self._ymd_excluded):
+ return
+ for weekday_format in ('%w', '%u', '%a', '%A'):
+ format_string = year_week_format + ' ' + weekday_format
+ with self.subTest(test_reason,
+ date=ymd_tuple,
+ format=format_string):
+ dt_date = datetime_date(*ymd_tuple)
+ strp_input = dt_date.strftime(format_string)
+ strp_output = _strptime._strptime_time(strp_input,
+ format_string)
+ msg = "%r: %s != %s" % (strp_input,
+ strp_output[7],
+ dt_date.timetuple()[7])
+ self.assertEqual(strp_output[:3], ymd_tuple, msg)
test_helper((1901, 1, 3), "week 0")
test_helper((1901, 1, 8), "common case")
test_helper((1901, 1, 13), "day on Sunday")
@@ -498,33 +534,48 @@ class CalculationTests(unittest.TestCase):
self.assertEqual(_strptime._strptime_time(value, format)[:-1], expected)
check('2015 0 0', '%Y %U %w', 2014, 12, 28, 0, 0, 0, 6, 362)
check('2015 0 0', '%Y %W %w', 2015, 1, 4, 0, 0, 0, 6, 4)
+ check('2015 1 1', '%G %V %u', 2014, 12, 29, 0, 0, 0, 0, 363)
check('2015 0 1', '%Y %U %w', 2014, 12, 29, 0, 0, 0, 0, 363)
check('2015 0 1', '%Y %W %w', 2014, 12, 29, 0, 0, 0, 0, 363)
+ check('2015 1 2', '%G %V %u', 2014, 12, 30, 0, 0, 0, 1, 364)
check('2015 0 2', '%Y %U %w', 2014, 12, 30, 0, 0, 0, 1, 364)
check('2015 0 2', '%Y %W %w', 2014, 12, 30, 0, 0, 0, 1, 364)
+ check('2015 1 3', '%G %V %u', 2014, 12, 31, 0, 0, 0, 2, 365)
check('2015 0 3', '%Y %U %w', 2014, 12, 31, 0, 0, 0, 2, 365)
check('2015 0 3', '%Y %W %w', 2014, 12, 31, 0, 0, 0, 2, 365)
+ check('2015 1 4', '%G %V %u', 2015, 1, 1, 0, 0, 0, 3, 1)
check('2015 0 4', '%Y %U %w', 2015, 1, 1, 0, 0, 0, 3, 1)
check('2015 0 4', '%Y %W %w', 2015, 1, 1, 0, 0, 0, 3, 1)
+ check('2015 1 5', '%G %V %u', 2015, 1, 2, 0, 0, 0, 4, 2)
check('2015 0 5', '%Y %U %w', 2015, 1, 2, 0, 0, 0, 4, 2)
check('2015 0 5', '%Y %W %w', 2015, 1, 2, 0, 0, 0, 4, 2)
+ check('2015 1 6', '%G %V %u', 2015, 1, 3, 0, 0, 0, 5, 3)
check('2015 0 6', '%Y %U %w', 2015, 1, 3, 0, 0, 0, 5, 3)
check('2015 0 6', '%Y %W %w', 2015, 1, 3, 0, 0, 0, 5, 3)
+ check('2015 1 7', '%G %V %u', 2015, 1, 4, 0, 0, 0, 6, 4)
check('2009 0 0', '%Y %U %w', 2008, 12, 28, 0, 0, 0, 6, 363)
check('2009 0 0', '%Y %W %w', 2009, 1, 4, 0, 0, 0, 6, 4)
+ check('2009 1 1', '%G %V %u', 2008, 12, 29, 0, 0, 0, 0, 364)
check('2009 0 1', '%Y %U %w', 2008, 12, 29, 0, 0, 0, 0, 364)
check('2009 0 1', '%Y %W %w', 2008, 12, 29, 0, 0, 0, 0, 364)
+ check('2009 1 2', '%G %V %u', 2008, 12, 30, 0, 0, 0, 1, 365)
check('2009 0 2', '%Y %U %w', 2008, 12, 30, 0, 0, 0, 1, 365)
check('2009 0 2', '%Y %W %w', 2008, 12, 30, 0, 0, 0, 1, 365)
+ check('2009 1 3', '%G %V %u', 2008, 12, 31, 0, 0, 0, 2, 366)
check('2009 0 3', '%Y %U %w', 2008, 12, 31, 0, 0, 0, 2, 366)
check('2009 0 3', '%Y %W %w', 2008, 12, 31, 0, 0, 0, 2, 366)
+ check('2009 1 4', '%G %V %u', 2009, 1, 1, 0, 0, 0, 3, 1)
check('2009 0 4', '%Y %U %w', 2009, 1, 1, 0, 0, 0, 3, 1)
check('2009 0 4', '%Y %W %w', 2009, 1, 1, 0, 0, 0, 3, 1)
+ check('2009 1 5', '%G %V %u', 2009, 1, 2, 0, 0, 0, 4, 2)
check('2009 0 5', '%Y %U %w', 2009, 1, 2, 0, 0, 0, 4, 2)
check('2009 0 5', '%Y %W %w', 2009, 1, 2, 0, 0, 0, 4, 2)
+ check('2009 1 6', '%G %V %u', 2009, 1, 3, 0, 0, 0, 5, 3)
check('2009 0 6', '%Y %U %w', 2009, 1, 3, 0, 0, 0, 5, 3)
check('2009 0 6', '%Y %W %w', 2009, 1, 3, 0, 0, 0, 5, 3)
+ check('2009 1 7', '%G %V %u', 2009, 1, 4, 0, 0, 0, 6, 4)
+
class CacheTests(unittest.TestCase):
"""Test that caching works properly."""
diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py
index efbdbfc..4d9d601 100644
--- a/Lib/test/test_struct.py
+++ b/Lib/test/test_struct.py
@@ -1,5 +1,6 @@
from collections import abc
import array
+import math
import operator
import unittest
import struct
@@ -15,22 +16,10 @@ byteorders = '', '@', '=', '<', '>', '!'
def iter_integer_formats(byteorders=byteorders):
for code in integer_codes:
for byteorder in byteorders:
- if (byteorder in ('', '@') and code in ('q', 'Q') and
- not HAVE_LONG_LONG):
- continue
if (byteorder not in ('', '@') and code in ('n', 'N')):
continue
yield code, byteorder
-# Native 'q' packing isn't available on systems that don't have the C
-# long long type.
-try:
- struct.pack('q', 5)
-except struct.error:
- HAVE_LONG_LONG = False
-else:
- HAVE_LONG_LONG = True
-
def string_reverse(s):
return s[::-1]
@@ -158,9 +147,7 @@ class StructTest(unittest.TestCase):
self.assertEqual(size, expected_size[code])
# native integer sizes
- native_pairs = 'bB', 'hH', 'iI', 'lL', 'nN'
- if HAVE_LONG_LONG:
- native_pairs += 'qQ',
+ native_pairs = 'bB', 'hH', 'iI', 'lL', 'nN', 'qQ'
for format_pair in native_pairs:
for byteorder in '', '@':
signed_size = struct.calcsize(byteorder + format_pair[0])
@@ -173,9 +160,8 @@ class StructTest(unittest.TestCase):
self.assertLessEqual(4, struct.calcsize('l'))
self.assertLessEqual(struct.calcsize('h'), struct.calcsize('i'))
self.assertLessEqual(struct.calcsize('i'), struct.calcsize('l'))
- if HAVE_LONG_LONG:
- self.assertLessEqual(8, struct.calcsize('q'))
- self.assertLessEqual(struct.calcsize('l'), struct.calcsize('q'))
+ self.assertLessEqual(8, struct.calcsize('q'))
+ self.assertLessEqual(struct.calcsize('l'), struct.calcsize('q'))
self.assertGreaterEqual(struct.calcsize('n'), struct.calcsize('i'))
self.assertGreaterEqual(struct.calcsize('n'), struct.calcsize('P'))
@@ -366,8 +352,6 @@ class StructTest(unittest.TestCase):
# SF bug 705836. "<f" and ">f" had a severe rounding bug, where a carry
# from the low-order discarded bits could propagate into the exponent
# field, causing the result to be wrong by a factor of 2.
- import math
-
for base in range(1, 33):
# smaller <- largest representable float less than base.
delta = 0.5
@@ -659,6 +643,110 @@ class UnpackIteratorTest(unittest.TestCase):
self.assertRaises(StopIteration, next, it)
self.assertRaises(StopIteration, next, it)
+ def test_half_float(self):
+ # Little-endian examples from:
+ # http://en.wikipedia.org/wiki/Half_precision_floating-point_format
+ format_bits_float__cleanRoundtrip_list = [
+ (b'\x00\x3c', 1.0),
+ (b'\x00\xc0', -2.0),
+ (b'\xff\x7b', 65504.0), # (max half precision)
+ (b'\x00\x04', 2**-14), # ~= 6.10352 * 10**-5 (min pos normal)
+ (b'\x01\x00', 2**-24), # ~= 5.96046 * 10**-8 (min pos subnormal)
+ (b'\x00\x00', 0.0),
+ (b'\x00\x80', -0.0),
+ (b'\x00\x7c', float('+inf')),
+ (b'\x00\xfc', float('-inf')),
+ (b'\x55\x35', 0.333251953125), # ~= 1/3
+ ]
+
+ for le_bits, f in format_bits_float__cleanRoundtrip_list:
+ be_bits = le_bits[::-1]
+ self.assertEqual(f, struct.unpack('<e', le_bits)[0])
+ self.assertEqual(le_bits, struct.pack('<e', f))
+ self.assertEqual(f, struct.unpack('>e', be_bits)[0])
+ self.assertEqual(be_bits, struct.pack('>e', f))
+ if sys.byteorder == 'little':
+ self.assertEqual(f, struct.unpack('e', le_bits)[0])
+ self.assertEqual(le_bits, struct.pack('e', f))
+ else:
+ self.assertEqual(f, struct.unpack('e', be_bits)[0])
+ self.assertEqual(be_bits, struct.pack('e', f))
+
+ # Check for NaN handling:
+ format_bits__nan_list = [
+ ('<e', b'\x01\xfc'),
+ ('<e', b'\x00\xfe'),
+ ('<e', b'\xff\xff'),
+ ('<e', b'\x01\x7c'),
+ ('<e', b'\x00\x7e'),
+ ('<e', b'\xff\x7f'),
+ ]
+
+ for formatcode, bits in format_bits__nan_list:
+ self.assertTrue(math.isnan(struct.unpack('<e', bits)[0]))
+ self.assertTrue(math.isnan(struct.unpack('>e', bits[::-1])[0]))
+
+ # Check that packing produces a bit pattern representing a quiet NaN:
+ # all exponent bits and the msb of the fraction should all be 1.
+ packed = struct.pack('<e', math.nan)
+ self.assertEqual(packed[1] & 0x7e, 0x7e)
+ packed = struct.pack('<e', -math.nan)
+ self.assertEqual(packed[1] & 0x7e, 0x7e)
+
+ # Checks for round-to-even behavior
+ format_bits_float__rounding_list = [
+ ('>e', b'\x00\x01', 2.0**-25 + 2.0**-35), # Rounds to minimum subnormal
+ ('>e', b'\x00\x00', 2.0**-25), # Underflows to zero (nearest even mode)
+ ('>e', b'\x00\x00', 2.0**-26), # Underflows to zero
+ ('>e', b'\x03\xff', 2.0**-14 - 2.0**-24), # Largest subnormal.
+ ('>e', b'\x03\xff', 2.0**-14 - 2.0**-25 - 2.0**-65),
+ ('>e', b'\x04\x00', 2.0**-14 - 2.0**-25),
+ ('>e', b'\x04\x00', 2.0**-14), # Smallest normal.
+ ('>e', b'\x3c\x01', 1.0+2.0**-11 + 2.0**-16), # rounds to 1.0+2**(-10)
+ ('>e', b'\x3c\x00', 1.0+2.0**-11), # rounds to 1.0 (nearest even mode)
+ ('>e', b'\x3c\x00', 1.0+2.0**-12), # rounds to 1.0
+ ('>e', b'\x7b\xff', 65504), # largest normal
+ ('>e', b'\x7b\xff', 65519), # rounds to 65504
+ ('>e', b'\x80\x01', -2.0**-25 - 2.0**-35), # Rounds to minimum subnormal
+ ('>e', b'\x80\x00', -2.0**-25), # Underflows to zero (nearest even mode)
+ ('>e', b'\x80\x00', -2.0**-26), # Underflows to zero
+ ('>e', b'\xbc\x01', -1.0-2.0**-11 - 2.0**-16), # rounds to 1.0+2**(-10)
+ ('>e', b'\xbc\x00', -1.0-2.0**-11), # rounds to 1.0 (nearest even mode)
+ ('>e', b'\xbc\x00', -1.0-2.0**-12), # rounds to 1.0
+ ('>e', b'\xfb\xff', -65519), # rounds to 65504
+ ]
+
+ for formatcode, bits, f in format_bits_float__rounding_list:
+ self.assertEqual(bits, struct.pack(formatcode, f))
+
+ # This overflows, and so raises an error
+ format_bits_float__roundingError_list = [
+ # Values that round to infinity.
+ ('>e', 65520.0),
+ ('>e', 65536.0),
+ ('>e', 1e300),
+ ('>e', -65520.0),
+ ('>e', -65536.0),
+ ('>e', -1e300),
+ ('<e', 65520.0),
+ ('<e', 65536.0),
+ ('<e', 1e300),
+ ('<e', -65520.0),
+ ('<e', -65536.0),
+ ('<e', -1e300),
+ ]
+
+ for formatcode, f in format_bits_float__roundingError_list:
+ self.assertRaises(OverflowError, struct.pack, formatcode, f)
+
+ # Double rounding
+ format_bits_float__doubleRoundingError_list = [
+ ('>e', b'\x67\xff', 0x1ffdffffff * 2**-26), # should be 2047, if double-rounded 64>32>16, becomes 2048
+ ]
+
+ for formatcode, bits, f in format_bits_float__doubleRoundingError_list:
+ self.assertEqual(bits, struct.pack(formatcode, f))
+
if __name__ == '__main__':
unittest.main()
diff --git a/Lib/test/test_subclassinit.py b/Lib/test/test_subclassinit.py
new file mode 100644
index 0000000..3c331bb9
--- /dev/null
+++ b/Lib/test/test_subclassinit.py
@@ -0,0 +1,284 @@
+import sys
+import types
+import unittest
+
+
+class Test(unittest.TestCase):
+ def test_init_subclass(self):
+ class A:
+ initialized = False
+
+ def __init_subclass__(cls):
+ super().__init_subclass__()
+ cls.initialized = True
+
+ class B(A):
+ pass
+
+ self.assertFalse(A.initialized)
+ self.assertTrue(B.initialized)
+
+ def test_init_subclass_dict(self):
+ class A(dict):
+ initialized = False
+
+ def __init_subclass__(cls):
+ super().__init_subclass__()
+ cls.initialized = True
+
+ class B(A):
+ pass
+
+ self.assertFalse(A.initialized)
+ self.assertTrue(B.initialized)
+
+ def test_init_subclass_kwargs(self):
+ class A:
+ def __init_subclass__(cls, **kwargs):
+ cls.kwargs = kwargs
+
+ class B(A, x=3):
+ pass
+
+ self.assertEqual(B.kwargs, dict(x=3))
+
+ def test_init_subclass_error(self):
+ class A:
+ def __init_subclass__(cls):
+ raise RuntimeError
+
+ with self.assertRaises(RuntimeError):
+ class B(A):
+ pass
+
+ def test_init_subclass_wrong(self):
+ class A:
+ def __init_subclass__(cls, whatever):
+ pass
+
+ with self.assertRaises(TypeError):
+ class B(A):
+ pass
+
+ def test_init_subclass_skipped(self):
+ class BaseWithInit:
+ def __init_subclass__(cls, **kwargs):
+ super().__init_subclass__(**kwargs)
+ cls.initialized = cls
+
+ class BaseWithoutInit(BaseWithInit):
+ pass
+
+ class A(BaseWithoutInit):
+ pass
+
+ self.assertIs(A.initialized, A)
+ self.assertIs(BaseWithoutInit.initialized, BaseWithoutInit)
+
+ def test_init_subclass_diamond(self):
+ class Base:
+ def __init_subclass__(cls, **kwargs):
+ super().__init_subclass__(**kwargs)
+ cls.calls = []
+
+ class Left(Base):
+ pass
+
+ class Middle:
+ def __init_subclass__(cls, middle, **kwargs):
+ super().__init_subclass__(**kwargs)
+ cls.calls += [middle]
+
+ class Right(Base):
+ def __init_subclass__(cls, right="right", **kwargs):
+ super().__init_subclass__(**kwargs)
+ cls.calls += [right]
+
+ class A(Left, Middle, Right, middle="middle"):
+ pass
+
+ self.assertEqual(A.calls, ["right", "middle"])
+ self.assertEqual(Left.calls, [])
+ self.assertEqual(Right.calls, [])
+
+ def test_set_name(self):
+ class Descriptor:
+ def __set_name__(self, owner, name):
+ self.owner = owner
+ self.name = name
+
+ class A:
+ d = Descriptor()
+
+ self.assertEqual(A.d.name, "d")
+ self.assertIs(A.d.owner, A)
+
+ def test_set_name_metaclass(self):
+ class Meta(type):
+ def __new__(cls, name, bases, ns):
+ ret = super().__new__(cls, name, bases, ns)
+ self.assertEqual(ret.d.name, "d")
+ self.assertIs(ret.d.owner, ret)
+ return 0
+
+ class Descriptor:
+ def __set_name__(self, owner, name):
+ self.owner = owner
+ self.name = name
+
+ class A(metaclass=Meta):
+ d = Descriptor()
+ self.assertEqual(A, 0)
+
+ def test_set_name_error(self):
+ class Descriptor:
+ def __set_name__(self, owner, name):
+ 1/0
+
+ with self.assertRaises(RuntimeError) as cm:
+ class NotGoingToWork:
+ attr = Descriptor()
+
+ exc = cm.exception
+ self.assertRegex(str(exc), r'\bNotGoingToWork\b')
+ self.assertRegex(str(exc), r'\battr\b')
+ self.assertRegex(str(exc), r'\bDescriptor\b')
+ self.assertIsInstance(exc.__cause__, ZeroDivisionError)
+
+ def test_set_name_wrong(self):
+ class Descriptor:
+ def __set_name__(self):
+ pass
+
+ with self.assertRaises(RuntimeError) as cm:
+ class NotGoingToWork:
+ attr = Descriptor()
+
+ exc = cm.exception
+ self.assertRegex(str(exc), r'\bNotGoingToWork\b')
+ self.assertRegex(str(exc), r'\battr\b')
+ self.assertRegex(str(exc), r'\bDescriptor\b')
+ self.assertIsInstance(exc.__cause__, TypeError)
+
+ def test_set_name_lookup(self):
+ resolved = []
+ class NonDescriptor:
+ def __getattr__(self, name):
+ resolved.append(name)
+
+ class A:
+ d = NonDescriptor()
+
+ self.assertNotIn('__set_name__', resolved,
+ '__set_name__ is looked up in instance dict')
+
+ def test_set_name_init_subclass(self):
+ class Descriptor:
+ def __set_name__(self, owner, name):
+ self.owner = owner
+ self.name = name
+
+ class Meta(type):
+ def __new__(cls, name, bases, ns):
+ self = super().__new__(cls, name, bases, ns)
+ self.meta_owner = self.owner
+ self.meta_name = self.name
+ return self
+
+ class A:
+ def __init_subclass__(cls):
+ cls.owner = cls.d.owner
+ cls.name = cls.d.name
+
+ class B(A, metaclass=Meta):
+ d = Descriptor()
+
+ self.assertIs(B.owner, B)
+ self.assertEqual(B.name, 'd')
+ self.assertIs(B.meta_owner, B)
+ self.assertEqual(B.name, 'd')
+
+ def test_set_name_modifying_dict(self):
+ notified = []
+ class Descriptor:
+ def __set_name__(self, owner, name):
+ setattr(owner, name + 'x', None)
+ notified.append(name)
+
+ class A:
+ a = Descriptor()
+ b = Descriptor()
+ c = Descriptor()
+ d = Descriptor()
+ e = Descriptor()
+
+ self.assertCountEqual(notified, ['a', 'b', 'c', 'd', 'e'])
+
+ def test_errors(self):
+ class MyMeta(type):
+ pass
+
+ with self.assertRaises(TypeError):
+ class MyClass(metaclass=MyMeta, otherarg=1):
+ pass
+
+ with self.assertRaises(TypeError):
+ types.new_class("MyClass", (object,),
+ dict(metaclass=MyMeta, otherarg=1))
+ types.prepare_class("MyClass", (object,),
+ dict(metaclass=MyMeta, otherarg=1))
+
+ class MyMeta(type):
+ def __init__(self, name, bases, namespace, otherarg):
+ super().__init__(name, bases, namespace)
+
+ with self.assertRaises(TypeError):
+ class MyClass(metaclass=MyMeta, otherarg=1):
+ pass
+
+ class MyMeta(type):
+ def __new__(cls, name, bases, namespace, otherarg):
+ return super().__new__(cls, name, bases, namespace)
+
+ def __init__(self, name, bases, namespace, otherarg):
+ super().__init__(name, bases, namespace)
+ self.otherarg = otherarg
+
+ class MyClass(metaclass=MyMeta, otherarg=1):
+ pass
+
+ self.assertEqual(MyClass.otherarg, 1)
+
+ def test_errors_changed_pep487(self):
+ # These tests failed before Python 3.6, PEP 487
+ class MyMeta(type):
+ def __new__(cls, name, bases, namespace):
+ return super().__new__(cls, name=name, bases=bases,
+ dict=namespace)
+
+ with self.assertRaises(TypeError):
+ class MyClass(metaclass=MyMeta):
+ pass
+
+ class MyMeta(type):
+ def __new__(cls, name, bases, namespace, otherarg):
+ self = super().__new__(cls, name, bases, namespace)
+ self.otherarg = otherarg
+ return self
+
+ class MyClass(metaclass=MyMeta, otherarg=1):
+ pass
+
+ self.assertEqual(MyClass.otherarg, 1)
+
+ def test_type(self):
+ t = type('NewClass', (object,), {})
+ self.assertIsInstance(t, type)
+ self.assertEqual(t.__name__, 'NewClass')
+
+ with self.assertRaises(TypeError):
+ type(name='NewClass', bases=(object,), dict={})
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py
index 758e094..89de6d1 100644
--- a/Lib/test/test_subprocess.py
+++ b/Lib/test/test_subprocess.py
@@ -1,20 +1,16 @@
import unittest
from unittest import mock
-from test.support import script_helper
from test import support
import subprocess
import sys
import signal
import io
-import locale
import os
import errno
import tempfile
import time
-import re
import selectors
import sysconfig
-import warnings
import select
import shutil
import gc
@@ -25,6 +21,9 @@ try:
except ImportError:
threading = None
+if support.PGO:
+ raise unittest.SkipTest("test is not helpful for PGO")
+
mswindows = (sys.platform == "win32")
#
@@ -294,7 +293,8 @@ class ProcessTestCase(BaseTestCase):
# Verify first that the call succeeds without the executable arg.
pre_args = [sys.executable, "-c"]
self._assert_python(pre_args)
- self.assertRaises(FileNotFoundError, self._assert_python, pre_args,
+ self.assertRaises((FileNotFoundError, PermissionError),
+ self._assert_python, pre_args,
executable="doesnotexist")
@unittest.skipIf(mswindows, "executable argument replaces shell")
@@ -448,8 +448,8 @@ class ProcessTestCase(BaseTestCase):
p = subprocess.Popen([sys.executable, "-c",
'import sys; sys.stdout.write("orange")'],
stdout=subprocess.PIPE)
- self.addCleanup(p.stdout.close)
- self.assertEqual(p.stdout.read(), b"orange")
+ with p:
+ self.assertEqual(p.stdout.read(), b"orange")
def test_stdout_filedes(self):
# stdout is set to open file descriptor
@@ -479,8 +479,8 @@ class ProcessTestCase(BaseTestCase):
p = subprocess.Popen([sys.executable, "-c",
'import sys; sys.stderr.write("strawberry")'],
stderr=subprocess.PIPE)
- self.addCleanup(p.stderr.close)
- self.assertStderrEqual(p.stderr.read(), b"strawberry")
+ with p:
+ self.assertStderrEqual(p.stderr.read(), b"strawberry")
def test_stderr_filedes(self):
# stderr is set to open file descriptor
@@ -535,8 +535,8 @@ class ProcessTestCase(BaseTestCase):
'sys.stderr.write("orange")'],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
- self.addCleanup(p.stdout.close)
- self.assertStderrEqual(p.stdout.read(), b"appleorange")
+ with p:
+ self.assertStderrEqual(p.stdout.read(), b"appleorange")
def test_stdout_stderr_file(self):
# capture stdout and stderr to the same open file
@@ -794,18 +794,19 @@ class ProcessTestCase(BaseTestCase):
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
universal_newlines=1)
- p.stdin.write("line1\n")
- p.stdin.flush()
- self.assertEqual(p.stdout.readline(), "line1\n")
- p.stdin.write("line3\n")
- p.stdin.close()
- self.addCleanup(p.stdout.close)
- self.assertEqual(p.stdout.readline(),
- "line2\n")
- self.assertEqual(p.stdout.read(6),
- "line3\n")
- self.assertEqual(p.stdout.read(),
- "line4\nline5\nline6\nline7\nline8")
+ with p:
+ p.stdin.write("line1\n")
+ p.stdin.flush()
+ self.assertEqual(p.stdout.readline(), "line1\n")
+ p.stdin.write("line3\n")
+ p.stdin.close()
+ self.addCleanup(p.stdout.close)
+ self.assertEqual(p.stdout.readline(),
+ "line2\n")
+ self.assertEqual(p.stdout.read(6),
+ "line3\n")
+ self.assertEqual(p.stdout.read(),
+ "line4\nline5\nline6\nline7\nline8")
def test_universal_newlines_communicate(self):
# universal newlines through communicate()
@@ -894,31 +895,42 @@ class ProcessTestCase(BaseTestCase):
#
# UTF-16 and UTF-32-BE are sufficient to check both with BOM and
# without, and UTF-16 and UTF-32.
- import _bootlocale
for encoding in ['utf-16', 'utf-32-be']:
- old_getpreferredencoding = _bootlocale.getpreferredencoding
- # Indirectly via io.TextIOWrapper, Popen() defaults to
- # locale.getpreferredencoding(False) and earlier in Python 3.2 to
- # locale.getpreferredencoding().
- def getpreferredencoding(do_setlocale=True):
- return encoding
code = ("import sys; "
r"sys.stdout.buffer.write('1\r\n2\r3\n4'.encode('%s'))" %
encoding)
args = [sys.executable, '-c', code]
- try:
- _bootlocale.getpreferredencoding = getpreferredencoding
- # We set stdin to be non-None because, as of this writing,
- # a different code path is used when the number of pipes is
- # zero or one.
- popen = subprocess.Popen(args, universal_newlines=True,
- stdin=subprocess.PIPE,
- stdout=subprocess.PIPE)
- stdout, stderr = popen.communicate(input='')
- finally:
- _bootlocale.getpreferredencoding = old_getpreferredencoding
+ # We set stdin to be non-None because, as of this writing,
+ # a different code path is used when the number of pipes is
+ # zero or one.
+ popen = subprocess.Popen(args,
+ stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE,
+ encoding=encoding)
+ stdout, stderr = popen.communicate(input='')
self.assertEqual(stdout, '1\n2\n3\n4')
+ def test_communicate_errors(self):
+ for errors, expected in [
+ ('ignore', ''),
+ ('replace', '\ufffd\ufffd'),
+ ('surrogateescape', '\udc80\udc80'),
+ ('backslashreplace', '\\x80\\x80'),
+ ]:
+ code = ("import sys; "
+ r"sys.stdout.buffer.write(b'[\x80\x80]')")
+ args = [sys.executable, '-c', code]
+ # We set stdin to be non-None because, as of this writing,
+ # a different code path is used when the number of pipes is
+ # zero or one.
+ popen = subprocess.Popen(args,
+ stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE,
+ encoding='utf-8',
+ errors=errors)
+ stdout, stderr = popen.communicate(input='')
+ self.assertEqual(stdout, '[{}]'.format(expected))
+
def test_no_leaking(self):
# Make sure we leak no resources
if not mswindows:
@@ -1003,6 +1015,19 @@ class ProcessTestCase(BaseTestCase):
# time to start.
self.assertEqual(p.wait(timeout=3), 0)
+ def test_wait_endtime(self):
+ """Confirm that the deprecated endtime parameter warns."""
+ p = subprocess.Popen([sys.executable, "-c", "pass"])
+ try:
+ with self.assertWarns(DeprecationWarning) as warn_cm:
+ p.wait(endtime=time.time()+0.01)
+ except subprocess.TimeoutExpired:
+ pass # We're not testing endtime timeout behavior.
+ finally:
+ p.kill()
+ self.assertIn('test_subprocess.py', warn_cm.filename)
+ self.assertIn('endtime', str(warn_cm.warning))
+
def test_invalid_bufsize(self):
# an invalid type of the bufsize argument should raise
# TypeError.
@@ -1431,6 +1456,27 @@ class POSIXProcessTestCase(BaseTestCase):
p.wait()
self.assertEqual(-p.returncode, signal.SIGABRT)
+ def test_CalledProcessError_str_signal(self):
+ err = subprocess.CalledProcessError(-int(signal.SIGABRT), "fake cmd")
+ error_string = str(err)
+ # We're relying on the repr() of the signal.Signals intenum to provide
+ # the word signal, the signal name and the numeric value.
+ self.assertIn("signal", error_string.lower())
+ # We're not being specific about the signal name as some signals have
+ # multiple names and which name is revealed can vary.
+ self.assertIn("SIG", error_string)
+ self.assertIn(str(signal.SIGABRT), error_string)
+
+ def test_CalledProcessError_str_unknown_signal(self):
+ err = subprocess.CalledProcessError(-9876543, "fake cmd")
+ error_string = str(err)
+ self.assertIn("unknown signal 9876543.", error_string)
+
+ def test_CalledProcessError_str_non_zero(self):
+ err = subprocess.CalledProcessError(2, "fake cmd")
+ error_string = str(err)
+ self.assertIn("non-zero exit status 2.", error_string)
+
def test_preexec(self):
# DISCLAIMER: Setting environment variables is *not* a good use
# of a preexec_fn. This is merely a test.
@@ -1439,8 +1485,8 @@ class POSIXProcessTestCase(BaseTestCase):
'sys.stdout.write(os.getenv("FRUIT"))'],
stdout=subprocess.PIPE,
preexec_fn=lambda: os.putenv("FRUIT", "apple"))
- self.addCleanup(p.stdout.close)
- self.assertEqual(p.stdout.read(), b"apple")
+ with p:
+ self.assertEqual(p.stdout.read(), b"apple")
def test_preexec_exception(self):
def raise_it():
@@ -1561,7 +1607,7 @@ class POSIXProcessTestCase(BaseTestCase):
fd, fname = tempfile.mkstemp()
# reopen in text mode
with open(fd, "w", errors="surrogateescape") as fobj:
- fobj.write("#!/bin/sh\n")
+ fobj.write("#!%s\n" % support.unix_shell)
fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" %
sys.executable)
os.chmod(fname, 0o700)
@@ -1588,8 +1634,8 @@ class POSIXProcessTestCase(BaseTestCase):
p = subprocess.Popen(["echo $FRUIT"], shell=1,
stdout=subprocess.PIPE,
env=newenv)
- self.addCleanup(p.stdout.close)
- self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple")
+ with p:
+ self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple")
def test_shell_string(self):
# Run command through the shell (string)
@@ -1598,15 +1644,15 @@ class POSIXProcessTestCase(BaseTestCase):
p = subprocess.Popen("echo $FRUIT", shell=1,
stdout=subprocess.PIPE,
env=newenv)
- self.addCleanup(p.stdout.close)
- self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple")
+ with p:
+ self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple")
def test_call_string(self):
# call() function with string argument on UNIX
fd, fname = tempfile.mkstemp()
# reopen in text mode
with open(fd, "w", errors="surrogateescape") as fobj:
- fobj.write("#!/bin/sh\n")
+ fobj.write("#!%s\n" % support.unix_shell)
fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" %
sys.executable)
os.chmod(fname, 0o700)
@@ -1631,8 +1677,8 @@ class POSIXProcessTestCase(BaseTestCase):
for sh in shells:
p = subprocess.Popen("echo $0", executable=sh, shell=True,
stdout=subprocess.PIPE)
- self.addCleanup(p.stdout.close)
- self.assertEqual(p.stdout.read().strip(), bytes(sh, 'ascii'))
+ with p:
+ self.assertEqual(p.stdout.read().strip(), bytes(sh, 'ascii'))
def _kill_process(self, method, *args):
# Do not inherit file handles from the parent.
@@ -2290,7 +2336,9 @@ class POSIXProcessTestCase(BaseTestCase):
self.addCleanup(p.stderr.close)
ident = id(p)
pid = p.pid
- del p
+ with support.check_warnings(('', ResourceWarning)):
+ p = None
+
# check that p is in the active processes list
self.assertIn(ident, [id(o) for o in subprocess._active])
@@ -2309,7 +2357,9 @@ class POSIXProcessTestCase(BaseTestCase):
self.addCleanup(p.stderr.close)
ident = id(p)
pid = p.pid
- del p
+ with support.check_warnings(('', ResourceWarning)):
+ p = None
+
os.kill(pid, signal.SIGKILL)
# check that p is in the active processes list
self.assertIn(ident, [id(o) for o in subprocess._active])
@@ -2501,8 +2551,8 @@ class Win32ProcessTestCase(BaseTestCase):
p = subprocess.Popen(["set"], shell=1,
stdout=subprocess.PIPE,
env=newenv)
- self.addCleanup(p.stdout.close)
- self.assertIn(b"physalis", p.stdout.read())
+ with p:
+ self.assertIn(b"physalis", p.stdout.read())
def test_shell_string(self):
# Run command through the shell (string)
@@ -2511,8 +2561,20 @@ class Win32ProcessTestCase(BaseTestCase):
p = subprocess.Popen("set", shell=1,
stdout=subprocess.PIPE,
env=newenv)
- self.addCleanup(p.stdout.close)
- self.assertIn(b"physalis", p.stdout.read())
+ with p:
+ self.assertIn(b"physalis", p.stdout.read())
+
+ def test_shell_encodings(self):
+ # Run command through the shell (string)
+ for enc in ['ansi', 'oem']:
+ newenv = os.environ.copy()
+ newenv["FRUIT"] = "physalis"
+ p = subprocess.Popen("set", shell=1,
+ stdout=subprocess.PIPE,
+ env=newenv,
+ encoding=enc)
+ with p:
+ self.assertIn("physalis", p.stdout.read(), enc)
def test_call_string(self):
# call() function with string argument on Windows
@@ -2531,16 +2593,14 @@ class Win32ProcessTestCase(BaseTestCase):
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
- self.addCleanup(p.stdout.close)
- self.addCleanup(p.stderr.close)
- self.addCleanup(p.stdin.close)
- # Wait for the interpreter to be completely initialized before
- # sending any signal.
- p.stdout.read(1)
- getattr(p, method)(*args)
- _, stderr = p.communicate()
- self.assertStderrEqual(stderr, b'')
- returncode = p.wait()
+ with p:
+ # Wait for the interpreter to be completely initialized before
+ # sending any signal.
+ p.stdout.read(1)
+ getattr(p, method)(*args)
+ _, stderr = p.communicate()
+ self.assertStderrEqual(stderr, b'')
+ returncode = p.wait()
self.assertNotEqual(returncode, 0)
def _kill_dead_process(self, method, *args):
@@ -2553,19 +2613,17 @@ class Win32ProcessTestCase(BaseTestCase):
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
- self.addCleanup(p.stdout.close)
- self.addCleanup(p.stderr.close)
- self.addCleanup(p.stdin.close)
- # Wait for the interpreter to be completely initialized before
- # sending any signal.
- p.stdout.read(1)
- # The process should end after this
- time.sleep(1)
- # This shouldn't raise even though the child is now dead
- getattr(p, method)(*args)
- _, stderr = p.communicate()
- self.assertStderrEqual(stderr, b'')
- rc = p.wait()
+ with p:
+ # Wait for the interpreter to be completely initialized before
+ # sending any signal.
+ p.stdout.read(1)
+ # The process should end after this
+ time.sleep(1)
+ # This shouldn't raise even though the child is now dead
+ getattr(p, method)(*args)
+ _, stderr = p.communicate()
+ self.assertStderrEqual(stderr, b'')
+ rc = p.wait()
self.assertEqual(rc, 42)
def test_send_signal(self):
@@ -2608,8 +2666,7 @@ class MiscTests(unittest.TestCase):
def test__all__(self):
"""Ensure that __all__ is populated properly."""
- # STARTUPINFO added to __all__ in 3.6
- intentionally_excluded = {"list2cmdline", "STARTUPINFO", "Handle"}
+ intentionally_excluded = {"list2cmdline", "Handle"}
exported = set(subprocess.__all__)
possible_exports = set()
import types
@@ -2654,11 +2711,11 @@ class CommandsWithSpaces (BaseTestCase):
def with_spaces(self, *args, **kwargs):
kwargs['stdout'] = subprocess.PIPE
p = subprocess.Popen(*args, **kwargs)
- self.addCleanup(p.stdout.close)
- self.assertEqual(
- p.stdout.read ().decode("mbcs"),
- "2 [%r, 'ab cd']" % self.fname
- )
+ with p:
+ self.assertEqual(
+ p.stdout.read ().decode("mbcs"),
+ "2 [%r, 'ab cd']" % self.fname
+ )
def test_shell_string_with_spaces(self):
# call() function with string argument with spaces on Windows
@@ -2710,7 +2767,7 @@ class ContextManagerTests(BaseTestCase):
self.assertEqual(proc.returncode, 1)
def test_invalid_args(self):
- with self.assertRaises(FileNotFoundError) as c:
+ with self.assertRaises((FileNotFoundError, PermissionError)) as c:
with subprocess.Popen(['nonexisting_i_hope'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE) as proc:
@@ -2733,19 +2790,5 @@ class ContextManagerTests(BaseTestCase):
self.assertTrue(proc.stdin.closed)
-def test_main():
- unit_tests = (ProcessTestCase,
- POSIXProcessTestCase,
- Win32ProcessTestCase,
- MiscTests,
- ProcessTestCaseNoPoll,
- CommandsWithSpaces,
- ContextManagerTests,
- RunFuncTestCase,
- )
-
- support.run_unittest(*unit_tests)
- support.reap_children()
-
if __name__ == "__main__":
unittest.main()
diff --git a/Lib/test/test_sunau.py b/Lib/test/test_sunau.py
index 0f4134e..bc1f46c 100644
--- a/Lib/test/test_sunau.py
+++ b/Lib/test/test_sunau.py
@@ -1,4 +1,3 @@
-from test.support import TESTFN
import unittest
from test import audiotests
from audioop import byteswap
diff --git a/Lib/test/test_super.py b/Lib/test/test_super.py
index b84863f..447dec9 100644
--- a/Lib/test/test_super.py
+++ b/Lib/test/test_super.py
@@ -1,7 +1,9 @@
-"""Unit tests for new super() implementation."""
+"""Unit tests for zero-argument super() & related machinery."""
import sys
import unittest
+import warnings
+from test.support import check_warnings
class A:
@@ -143,6 +145,166 @@ class TestSuper(unittest.TestCase):
return __class__
self.assertIs(X.f(), X)
+ def test___class___new(self):
+ # See issue #23722
+ # Ensure zero-arg super() works as soon as type.__new__() is completed
+ test_class = None
+
+ class Meta(type):
+ def __new__(cls, name, bases, namespace):
+ nonlocal test_class
+ self = super().__new__(cls, name, bases, namespace)
+ test_class = self.f()
+ return self
+
+ class A(metaclass=Meta):
+ @staticmethod
+ def f():
+ return __class__
+
+ self.assertIs(test_class, A)
+
+ def test___class___delayed(self):
+ # See issue #23722
+ test_namespace = None
+
+ class Meta(type):
+ def __new__(cls, name, bases, namespace):
+ nonlocal test_namespace
+ test_namespace = namespace
+ return None
+
+ # This case shouldn't trigger the __classcell__ deprecation warning
+ with check_warnings() as w:
+ warnings.simplefilter("always", DeprecationWarning)
+ class A(metaclass=Meta):
+ @staticmethod
+ def f():
+ return __class__
+ self.assertEqual(w.warnings, [])
+
+ self.assertIs(A, None)
+
+ B = type("B", (), test_namespace)
+ self.assertIs(B.f(), B)
+
+ def test___class___mro(self):
+ # See issue #23722
+ test_class = None
+
+ class Meta(type):
+ def mro(self):
+ # self.f() doesn't work yet...
+ self.__dict__["f"]()
+ return super().mro()
+
+ class A(metaclass=Meta):
+ def f():
+ nonlocal test_class
+ test_class = __class__
+
+ self.assertIs(test_class, A)
+
+ def test___classcell___expected_behaviour(self):
+ # See issue #23722
+ class Meta(type):
+ def __new__(cls, name, bases, namespace):
+ nonlocal namespace_snapshot
+ namespace_snapshot = namespace.copy()
+ return super().__new__(cls, name, bases, namespace)
+
+ # __classcell__ is injected into the class namespace by the compiler
+ # when at least one method needs it, and should be omitted otherwise
+ namespace_snapshot = None
+ class WithoutClassRef(metaclass=Meta):
+ pass
+ self.assertNotIn("__classcell__", namespace_snapshot)
+
+ # With zero-arg super() or an explicit __class__ reference,
+ # __classcell__ is the exact cell reference to be populated by
+ # type.__new__
+ namespace_snapshot = None
+ class WithClassRef(metaclass=Meta):
+ def f(self):
+ return __class__
+
+ class_cell = namespace_snapshot["__classcell__"]
+ method_closure = WithClassRef.f.__closure__
+ self.assertEqual(len(method_closure), 1)
+ self.assertIs(class_cell, method_closure[0])
+ # Ensure the cell reference *doesn't* get turned into an attribute
+ with self.assertRaises(AttributeError):
+ WithClassRef.__classcell__
+
+ def test___classcell___missing(self):
+ # See issue #23722
+ # Some metaclasses may not pass the original namespace to type.__new__
+ # We test that case here by forcibly deleting __classcell__
+ class Meta(type):
+ def __new__(cls, name, bases, namespace):
+ namespace.pop('__classcell__', None)
+ return super().__new__(cls, name, bases, namespace)
+
+ # The default case should continue to work without any warnings
+ with check_warnings() as w:
+ warnings.simplefilter("always", DeprecationWarning)
+ class WithoutClassRef(metaclass=Meta):
+ pass
+ self.assertEqual(w.warnings, [])
+
+ # With zero-arg super() or an explicit __class__ reference, we expect
+ # __build_class__ to emit a DeprecationWarning complaining that
+ # __class__ was not set, and asking if __classcell__ was propagated
+ # to type.__new__.
+ # In Python 3.7, that warning will become a RuntimeError.
+ expected_warning = (
+ '__class__ not set.*__classcell__ propagated',
+ DeprecationWarning
+ )
+ with check_warnings(expected_warning):
+ warnings.simplefilter("always", DeprecationWarning)
+ class WithClassRef(metaclass=Meta):
+ def f(self):
+ return __class__
+ # Check __class__ still gets set despite the warning
+ self.assertIs(WithClassRef().f(), WithClassRef)
+
+ # Check the warning is turned into an error as expected
+ with warnings.catch_warnings():
+ warnings.simplefilter("error", DeprecationWarning)
+ with self.assertRaises(DeprecationWarning):
+ class WithClassRef(metaclass=Meta):
+ def f(self):
+ return __class__
+
+ def test___classcell___overwrite(self):
+ # See issue #23722
+ # Overwriting __classcell__ with nonsense is explicitly prohibited
+ class Meta(type):
+ def __new__(cls, name, bases, namespace, cell):
+ namespace['__classcell__'] = cell
+ return super().__new__(cls, name, bases, namespace)
+
+ for bad_cell in (None, 0, "", object()):
+ with self.subTest(bad_cell=bad_cell):
+ with self.assertRaises(TypeError):
+ class A(metaclass=Meta, cell=bad_cell):
+ pass
+
+ def test___classcell___wrong_cell(self):
+ # See issue #23722
+ # Pointing the cell reference at the wrong class is also prohibited
+ class Meta(type):
+ def __new__(cls, name, bases, namespace):
+ cls = super().__new__(cls, name, bases, namespace)
+ B = type("B", (), namespace)
+ return cls
+
+ with self.assertRaises(TypeError):
+ class A(metaclass=Meta):
+ def f(self):
+ return __class__
+
def test_obscure_super_errors(self):
def f():
super()
diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py
index 5e0f990..e83a4d6 100644
--- a/Lib/test/test_support.py
+++ b/Lib/test/test_support.py
@@ -241,9 +241,8 @@ class TestSupport(unittest.TestCase):
def test_check_syntax_error(self):
support.check_syntax_error(self, "def class", lineno=1, offset=9)
- self.assertRaises(AssertionError, support.check_syntax_error, self, "1")
- #with self.assertRaises(AssertionError):
- #support.check_syntax_error(self, "x=1")
+ with self.assertRaises(AssertionError):
+ support.check_syntax_error(self, "x=1")
def test_CleanImport(self):
import importlib
@@ -327,6 +326,28 @@ class TestSupport(unittest.TestCase):
self.OtherClass, self.RefClass, ignore=ignore)
self.assertEqual(set(), missing_items)
+ def test_check__all__(self):
+ extra = {'tempdir'}
+ blacklist = {'template'}
+ support.check__all__(self,
+ tempfile,
+ extra=extra,
+ blacklist=blacklist)
+
+ extra = {'TextTestResult', 'installHandler'}
+ blacklist = {'load_tests', "TestProgram", "BaseTestSuite"}
+
+ support.check__all__(self,
+ unittest,
+ ("unittest.result", "unittest.case",
+ "unittest.suite", "unittest.loader",
+ "unittest.main", "unittest.runner",
+ "unittest.signals"),
+ extra=extra,
+ blacklist=blacklist)
+
+ self.assertRaises(AssertionError, support.check__all__, self, unittest)
+
# XXX -follows a list of untested API
# make_legacy_pyc
# is_resource_enabled
diff --git a/Lib/test/test_symbol.py b/Lib/test/test_symbol.py
new file mode 100644
index 0000000..c1306f5
--- /dev/null
+++ b/Lib/test/test_symbol.py
@@ -0,0 +1,54 @@
+import unittest
+from test import support
+import os
+import sys
+import subprocess
+
+
+SYMBOL_FILE = support.findfile('symbol.py')
+GRAMMAR_FILE = os.path.join(os.path.dirname(__file__),
+ '..', '..', 'Include', 'graminit.h')
+TEST_PY_FILE = 'symbol_test.py'
+
+
+class TestSymbolGeneration(unittest.TestCase):
+
+ def _copy_file_without_generated_symbols(self, source_file, dest_file):
+ with open(source_file) as fp:
+ lines = fp.readlines()
+ with open(dest_file, 'w') as fp:
+ fp.writelines(lines[:lines.index("#--start constants--\n") + 1])
+ fp.writelines(lines[lines.index("#--end constants--\n"):])
+
+ def _generate_symbols(self, grammar_file, target_symbol_py_file):
+ proc = subprocess.Popen([sys.executable,
+ SYMBOL_FILE,
+ grammar_file,
+ target_symbol_py_file], stderr=subprocess.PIPE)
+ stderr = proc.communicate()[1]
+ return proc.returncode, stderr
+
+ def compare_files(self, file1, file2):
+ with open(file1) as fp:
+ lines1 = fp.readlines()
+ with open(file2) as fp:
+ lines2 = fp.readlines()
+ self.assertEqual(lines1, lines2)
+
+ @unittest.skipIf(not os.path.exists(GRAMMAR_FILE),
+ 'test only works from source build directory')
+ def test_real_grammar_and_symbol_file(self):
+ output = support.TESTFN
+ self.addCleanup(support.unlink, output)
+
+ self._copy_file_without_generated_symbols(SYMBOL_FILE, output)
+
+ exitcode, stderr = self._generate_symbols(GRAMMAR_FILE, output)
+ self.assertEqual(b'', stderr)
+ self.assertEqual(0, exitcode)
+
+ self.compare_files(SYMBOL_FILE, output)
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/Lib/test/test_symtable.py b/Lib/test/test_symtable.py
index fbb1bdc..dfaee17 100644
--- a/Lib/test/test_symtable.py
+++ b/Lib/test/test_symtable.py
@@ -133,6 +133,17 @@ class SymtableTest(unittest.TestCase):
self.assertTrue(self.Mine.lookup("a_method").is_assigned())
self.assertFalse(self.internal.lookup("x").is_assigned())
+ def test_annotated(self):
+ st1 = symtable.symtable('def f():\n x: int\n', 'test', 'exec')
+ st2 = st1.get_children()[0]
+ self.assertTrue(st2.lookup('x').is_local())
+ self.assertTrue(st2.lookup('x').is_annotated())
+ self.assertFalse(st2.lookup('x').is_global())
+ st3 = symtable.symtable('def f():\n x = 1\n', 'test', 'exec')
+ st4 = st3.get_children()[0]
+ self.assertTrue(st4.lookup('x').is_local())
+ self.assertFalse(st4.lookup('x').is_annotated())
+
def test_imported(self):
self.assertTrue(self.top.lookup("sys").is_imported())
@@ -160,9 +171,11 @@ class SymtableTest(unittest.TestCase):
checkfilename("def f(x): foo)(", 14) # parse-time
checkfilename("def f(x): global x", 10) # symtable-build-time
symtable.symtable("pass", b"spam", "exec")
- with self.assertRaises(TypeError):
+ with self.assertWarns(DeprecationWarning), \
+ self.assertRaises(TypeError):
symtable.symtable("pass", bytearray(b"spam"), "exec")
- symtable.symtable("pass", memoryview(b"spam"), "exec")
+ with self.assertWarns(DeprecationWarning):
+ symtable.symtable("pass", memoryview(b"spam"), "exec")
with self.assertRaises(TypeError):
symtable.symtable("pass", list(b"spam"), "exec")
diff --git a/Lib/test/test_syntax.py b/Lib/test/test_syntax.py
index 301c142..7f7e6da 100644
--- a/Lib/test/test_syntax.py
+++ b/Lib/test/test_syntax.py
@@ -35,14 +35,6 @@ SyntaxError: invalid syntax
Traceback (most recent call last):
SyntaxError: can't assign to keyword
-It's a syntax error to assign to the empty tuple. Why isn't it an
-error to assign to the empty list? It will always raise some error at
-runtime.
-
->>> () = 1
-Traceback (most recent call last):
-SyntaxError: can't assign to ()
-
>>> f() = 1
Traceback (most recent call last):
SyntaxError: can't assign to function call
@@ -374,7 +366,23 @@ build. The number of blocks must be greater than CO_MAXBLOCKS. SF #1565514
...
SyntaxError: too many statically nested blocks
-Misuse of the nonlocal statement can lead to a few unique syntax errors.
+Misuse of the nonlocal and global statement can lead to a few unique syntax errors.
+
+ >>> def f():
+ ... x = 1
+ ... global x
+ Traceback (most recent call last):
+ ...
+ SyntaxError: name 'x' is assigned to before global declaration
+
+ >>> def f():
+ ... x = 1
+ ... def g():
+ ... print(x)
+ ... nonlocal x
+ Traceback (most recent call last):
+ ...
+ SyntaxError: name 'x' is used prior to nonlocal declaration
>>> def f(x):
... nonlocal x
@@ -493,10 +501,6 @@ Traceback (most recent call last):
...
SyntaxError: keyword argument repeated
->>> del ()
-Traceback (most recent call last):
-SyntaxError: can't delete ()
-
>>> {1, 2, 3} = 42
Traceback (most recent call last):
SyntaxError: can't assign to literal
@@ -580,7 +584,7 @@ class SyntaxTestCase(unittest.TestCase):
global b # SyntaxWarning
"""
warnings.filterwarnings(action='ignore', category=SyntaxWarning)
- self._check_error(source, "global", lineno=3, offset=16)
+ self._check_error(source, "global")
warnings.filters.pop(0)
def test_break_outside_loop(self):
diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py
index 4435d69..df9ebd4 100644
--- a/Lib/test/test_sys.py
+++ b/Lib/test/test_sys.py
@@ -10,6 +10,7 @@ import codecs
import gc
import sysconfig
import platform
+import locale
# count the number of test runs, used to create unique
# strings to intern in test_intern()
@@ -627,6 +628,8 @@ class SysModuleTest(unittest.TestCase):
@unittest.skipUnless(test.support.FS_NONASCII,
'requires OS support of non-ASCII encodings')
+ @unittest.skipUnless(sys.getfilesystemencoding() == locale.getpreferredencoding(False),
+ 'requires FS encoding to match locale')
def test_ioencoding_nonascii(self):
env = dict(os.environ)
@@ -669,8 +672,6 @@ class SysModuleTest(unittest.TestCase):
fs_encoding = sys.getfilesystemencoding()
if sys.platform == 'darwin':
expected = 'utf-8'
- elif sys.platform == 'win32':
- expected = 'mbcs'
else:
expected = None
self.check_fsencoding(fs_encoding, expected)
@@ -912,13 +913,13 @@ class SizeofTest(unittest.TestCase):
return inner
check(get_cell().__closure__[0], size('P'))
# code
- check(get_cell().__code__, size('5i9Pi3P'))
- check(get_cell.__code__, size('5i9Pi3P'))
+ check(get_cell().__code__, size('6i13P'))
+ check(get_cell.__code__, size('6i13P'))
def get_cell2(x):
def inner():
return x
return inner
- check(get_cell2.__code__, size('5i9Pi3P') + 1)
+ check(get_cell2.__code__, size('6i13P') + 1)
# complex
check(complex(0,1), size('2d'))
# method_descriptor (descriptor object)
@@ -936,9 +937,9 @@ class SizeofTest(unittest.TestCase):
# method-wrapper (descriptor object)
check({}.__iter__, size('2P'))
# dict
- check({}, size('n2P') + calcsize('2nPn') + 8*calcsize('n2P'))
+ check({}, size('nQ2P') + calcsize('2nP2n') + 8 + (8*2//3)*calcsize('n2P'))
longdict = {1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8}
- check(longdict, size('n2P') + calcsize('2nPn') + 16*calcsize('n2P'))
+ check(longdict, size('nQ2P') + calcsize('2nP2n') + 16 + (16*2//3)*calcsize('n2P'))
# dictionary-keyview
check({}.keys(), size('P'))
# dictionary-valueview
@@ -1096,13 +1097,13 @@ class SizeofTest(unittest.TestCase):
'10P' # PySequenceMethods
'2P' # PyBufferProcs
'4P')
- # Separate block for PyDictKeysObject with 4 entries
- s += calcsize("2nPn") + 4*calcsize("n2P")
+ # Separate block for PyDictKeysObject with 8 keys and 5 entries
+ s += calcsize("2nP2n") + 8 + 5*calcsize("n2P")
# class
class newstyleclass(object): pass
check(newstyleclass, s)
# dict with shared keys
- check(newstyleclass().__dict__, size('n2P' + '2nPn'))
+ check(newstyleclass().__dict__, size('nQ2P' + '2nP2n'))
# unicode
# each tuple contains a string and its expected character size
# don't put any static strings here, as they may contain
@@ -1191,6 +1192,32 @@ class SizeofTest(unittest.TestCase):
# sys.flags
check(sys.flags, vsize('') + self.P * len(sys.flags))
+ def test_asyncgen_hooks(self):
+ old = sys.get_asyncgen_hooks()
+ self.assertIsNone(old.firstiter)
+ self.assertIsNone(old.finalizer)
+
+ firstiter = lambda *a: None
+ sys.set_asyncgen_hooks(firstiter=firstiter)
+ hooks = sys.get_asyncgen_hooks()
+ self.assertIs(hooks.firstiter, firstiter)
+ self.assertIs(hooks[0], firstiter)
+ self.assertIs(hooks.finalizer, None)
+ self.assertIs(hooks[1], None)
+
+ finalizer = lambda *a: None
+ sys.set_asyncgen_hooks(finalizer=finalizer)
+ hooks = sys.get_asyncgen_hooks()
+ self.assertIs(hooks.firstiter, firstiter)
+ self.assertIs(hooks[0], firstiter)
+ self.assertIs(hooks.finalizer, finalizer)
+ self.assertIs(hooks[1], finalizer)
+
+ sys.set_asyncgen_hooks(*old)
+ cur = sys.get_asyncgen_hooks()
+ self.assertIsNone(cur.firstiter)
+ self.assertIsNone(cur.finalizer)
+
def test_main():
test.support.run_unittest(SysModuleTest, SizeofTest)
diff --git a/Lib/test/test_sys_settrace.py b/Lib/test/test_sys_settrace.py
index 509bc3e..25c5835 100644
--- a/Lib/test/test_sys_settrace.py
+++ b/Lib/test/test_sys_settrace.py
@@ -338,8 +338,8 @@ class TraceTestCase(unittest.TestCase):
def test_14_onliner_if(self):
def onliners():
- if True: False
- else: True
+ if True: x=False
+ else: x=True
return 0
self.run_and_compare(
onliners,
diff --git a/Lib/test/test_sysconfig.py b/Lib/test/test_sysconfig.py
index 3830679..a29ca96 100644
--- a/Lib/test/test_sysconfig.py
+++ b/Lib/test/test_sysconfig.py
@@ -386,7 +386,9 @@ class TestSysConfig(unittest.TestCase):
self.assertIsNotNone(vars['SO'])
self.assertEqual(vars['SO'], vars['EXT_SUFFIX'])
- @unittest.skipUnless(sys.platform == 'linux', 'Linux-specific test')
+ @unittest.skipUnless(sys.platform == 'linux' and
+ hasattr(sys.implementation, '_multiarch'),
+ 'multiarch-specific test')
def test_triplet_in_ext_suffix(self):
ctypes = import_module('ctypes')
import platform, re
@@ -396,8 +398,8 @@ class TestSysConfig(unittest.TestCase):
self.assertTrue('linux' in suffix, suffix)
if re.match('(i[3-6]86|x86_64)$', machine):
if ctypes.sizeof(ctypes.c_char_p()) == 4:
- self.assertTrue(suffix.endswith('i386-linux-gnu.so') \
- or suffix.endswith('x86_64-linux-gnux32.so'),
+ self.assertTrue(suffix.endswith('i386-linux-gnu.so') or
+ suffix.endswith('x86_64-linux-gnux32.so'),
suffix)
else: # 8 byte pointer size
self.assertTrue(suffix.endswith('x86_64-linux-gnu.so'), suffix)
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py
index 1efb841..619cbc0 100644
--- a/Lib/test/test_tarfile.py
+++ b/Lib/test/test_tarfile.py
@@ -2080,6 +2080,24 @@ class MiscTest(unittest.TestCase):
with self.assertRaises(ValueError):
tarfile.itn(0x10000000000, 6, tarfile.GNU_FORMAT)
+ def test__all__(self):
+ blacklist = {'version', 'grp', 'pwd', 'symlink_exception',
+ 'NUL', 'BLOCKSIZE', 'RECORDSIZE', 'GNU_MAGIC',
+ 'POSIX_MAGIC', 'LENGTH_NAME', 'LENGTH_LINK',
+ 'LENGTH_PREFIX', 'REGTYPE', 'AREGTYPE', 'LNKTYPE',
+ 'SYMTYPE', 'CHRTYPE', 'BLKTYPE', 'DIRTYPE', 'FIFOTYPE',
+ 'CONTTYPE', 'GNUTYPE_LONGNAME', 'GNUTYPE_LONGLINK',
+ 'GNUTYPE_SPARSE', 'XHDTYPE', 'XGLTYPE', 'SOLARIS_XHDTYPE',
+ 'SUPPORTED_TYPES', 'REGULAR_TYPES', 'GNU_TYPES',
+ 'PAX_FIELDS', 'PAX_NAME_FIELDS', 'PAX_NUMBER_FIELDS',
+ 'stn', 'nts', 'nti', 'itn', 'calc_chksums', 'copyfileobj',
+ 'filemode',
+ 'EmptyHeaderError', 'TruncatedHeaderError',
+ 'EOFHeaderError', 'InvalidHeaderError',
+ 'SubsequentHeaderError', 'ExFileObject',
+ 'main'}
+ support.check__all__(self, tarfile, blacklist=blacklist)
+
class CommandLineTest(unittest.TestCase):
diff --git a/Lib/test/test_telnetlib.py b/Lib/test/test_telnetlib.py
index 8e219f4..51d82e1 100644
--- a/Lib/test/test_telnetlib.py
+++ b/Lib/test/test_telnetlib.py
@@ -1,7 +1,6 @@
import socket
import selectors
import telnetlib
-import time
import contextlib
from test import support
@@ -42,6 +41,11 @@ class GeneralTests(unittest.TestCase):
telnet = telnetlib.Telnet(HOST, self.port)
telnet.sock.close()
+ def testContextManager(self):
+ with telnetlib.Telnet(HOST, self.port) as tn:
+ self.assertIsNotNone(tn.get_socket())
+ self.assertIsNone(tn.get_socket())
+
def testTimeoutDefault(self):
self.assertTrue(socket.getdefaulttimeout() is None)
socket.setdefaulttimeout(30)
diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py
index b630509..2c2914f 100644
--- a/Lib/test/test_threading.py
+++ b/Lib/test/test_threading.py
@@ -8,7 +8,6 @@ from test.support import (verbose, import_module, cpython_only,
from test.support.script_helper import assert_python_ok, assert_python_failure
import random
-import re
import sys
_thread = import_module('_thread')
threading = import_module('threading')
@@ -19,6 +18,7 @@ import os
import subprocess
from test import lock_tests
+from test import support
# Between fork() and exec(), only async-safe functions are allowed (issues
@@ -462,7 +462,7 @@ class ThreadTests(BaseTestCase):
self.addCleanup(sys.setswitchinterval, old_interval)
# Make the bug more likely to manifest.
- sys.setswitchinterval(1e-6)
+ test.support.setswitchinterval(1e-6)
for i in range(20):
t = threading.Thread(target=lambda: None)
@@ -1117,5 +1117,12 @@ class BoundedSemaphoreTests(lock_tests.BoundedSemaphoreTests):
class BarrierTests(lock_tests.BarrierTests):
barriertype = staticmethod(threading.Barrier)
+class MiscTestCase(unittest.TestCase):
+ def test__all__(self):
+ extra = {"ThreadError"}
+ blacklist = {'currentThread', 'activeCount'}
+ support.check__all__(self, threading, ('threading', '_thread'),
+ extra=extra, blacklist=blacklist)
+
if __name__ == "__main__":
unittest.main()
diff --git a/Lib/test/test_time.py b/Lib/test/test_time.py
index 76b894e..f224212 100644
--- a/Lib/test/test_time.py
+++ b/Lib/test/test_time.py
@@ -1,6 +1,8 @@
from test import support
+import decimal
import enum
import locale
+import math
import platform
import sys
import sysconfig
@@ -21,17 +23,27 @@ SIZEOF_INT = sysconfig.get_config_var('SIZEOF_INT') or 4
TIME_MAXYEAR = (1 << 8 * SIZEOF_INT - 1) - 1
TIME_MINYEAR = -TIME_MAXYEAR - 1
+SEC_TO_US = 10 ** 6
US_TO_NS = 10 ** 3
MS_TO_NS = 10 ** 6
SEC_TO_NS = 10 ** 9
+NS_TO_SEC = 10 ** 9
class _PyTime(enum.IntEnum):
# Round towards minus infinity (-inf)
ROUND_FLOOR = 0
# Round towards infinity (+inf)
ROUND_CEILING = 1
+ # Round to nearest with ties going to nearest even integer
+ ROUND_HALF_EVEN = 2
-ALL_ROUNDING_METHODS = (_PyTime.ROUND_FLOOR, _PyTime.ROUND_CEILING)
+# Rounding modes supported by PyTime
+ROUNDING_MODES = (
+ # (PyTime rounding method, decimal rounding method)
+ (_PyTime.ROUND_FLOOR, decimal.ROUND_FLOOR),
+ (_PyTime.ROUND_CEILING, decimal.ROUND_CEILING),
+ (_PyTime.ROUND_HALF_EVEN, decimal.ROUND_HALF_EVEN),
+)
class TimeTestCase(unittest.TestCase):
@@ -607,79 +619,6 @@ class TestStrftime4dyear(_TestStrftimeYear, _Test4dYear, unittest.TestCase):
class TestPytime(unittest.TestCase):
- def setUp(self):
- self.invalid_values = (
- -(2 ** 100), 2 ** 100,
- -(2.0 ** 100.0), 2.0 ** 100.0,
- )
-
- @support.cpython_only
- def test_time_t(self):
- from _testcapi import pytime_object_to_time_t
- for obj, time_t, rnd in (
- # Round towards minus infinity (-inf)
- (0, 0, _PyTime.ROUND_FLOOR),
- (-1, -1, _PyTime.ROUND_FLOOR),
- (-1.0, -1, _PyTime.ROUND_FLOOR),
- (-1.9, -2, _PyTime.ROUND_FLOOR),
- (1.0, 1, _PyTime.ROUND_FLOOR),
- (1.9, 1, _PyTime.ROUND_FLOOR),
- # Round towards infinity (+inf)
- (0, 0, _PyTime.ROUND_CEILING),
- (-1, -1, _PyTime.ROUND_CEILING),
- (-1.0, -1, _PyTime.ROUND_CEILING),
- (-1.9, -1, _PyTime.ROUND_CEILING),
- (1.0, 1, _PyTime.ROUND_CEILING),
- (1.9, 2, _PyTime.ROUND_CEILING),
- ):
- self.assertEqual(pytime_object_to_time_t(obj, rnd), time_t)
-
- rnd = _PyTime.ROUND_FLOOR
- for invalid in self.invalid_values:
- self.assertRaises(OverflowError,
- pytime_object_to_time_t, invalid, rnd)
-
- @support.cpython_only
- def test_timespec(self):
- from _testcapi import pytime_object_to_timespec
- for obj, timespec, rnd in (
- # Round towards minus infinity (-inf)
- (0, (0, 0), _PyTime.ROUND_FLOOR),
- (-1, (-1, 0), _PyTime.ROUND_FLOOR),
- (-1.0, (-1, 0), _PyTime.ROUND_FLOOR),
- (1e-9, (0, 1), _PyTime.ROUND_FLOOR),
- (1e-10, (0, 0), _PyTime.ROUND_FLOOR),
- (-1e-9, (-1, 999999999), _PyTime.ROUND_FLOOR),
- (-1e-10, (-1, 999999999), _PyTime.ROUND_FLOOR),
- (-1.2, (-2, 800000000), _PyTime.ROUND_FLOOR),
- (0.9999999999, (0, 999999999), _PyTime.ROUND_FLOOR),
- (1.1234567890, (1, 123456789), _PyTime.ROUND_FLOOR),
- (1.1234567899, (1, 123456789), _PyTime.ROUND_FLOOR),
- (-1.1234567890, (-2, 876543211), _PyTime.ROUND_FLOOR),
- (-1.1234567891, (-2, 876543210), _PyTime.ROUND_FLOOR),
- # Round towards infinity (+inf)
- (0, (0, 0), _PyTime.ROUND_CEILING),
- (-1, (-1, 0), _PyTime.ROUND_CEILING),
- (-1.0, (-1, 0), _PyTime.ROUND_CEILING),
- (1e-9, (0, 1), _PyTime.ROUND_CEILING),
- (1e-10, (0, 1), _PyTime.ROUND_CEILING),
- (-1e-9, (-1, 999999999), _PyTime.ROUND_CEILING),
- (-1e-10, (0, 0), _PyTime.ROUND_CEILING),
- (-1.2, (-2, 800000000), _PyTime.ROUND_CEILING),
- (0.9999999999, (1, 0), _PyTime.ROUND_CEILING),
- (1.1234567890, (1, 123456790), _PyTime.ROUND_CEILING),
- (1.1234567899, (1, 123456790), _PyTime.ROUND_CEILING),
- (-1.1234567890, (-2, 876543211), _PyTime.ROUND_CEILING),
- (-1.1234567891, (-2, 876543211), _PyTime.ROUND_CEILING),
- ):
- with self.subTest(obj=obj, round=rnd, timespec=timespec):
- self.assertEqual(pytime_object_to_timespec(obj, rnd), timespec)
-
- rnd = _PyTime.ROUND_FLOOR
- for invalid in self.invalid_values:
- self.assertRaises(OverflowError,
- pytime_object_to_timespec, invalid, rnd)
-
@unittest.skipUnless(time._STRUCT_TM_ITEMS == 11, "needs tm_zone support")
def test_localtime_timezone(self):
@@ -734,266 +673,291 @@ class TestPytime(unittest.TestCase):
self.assertIs(lt.tm_zone, None)
-@unittest.skipUnless(_testcapi is not None,
- 'need the _testcapi module')
-class TestPyTime_t(unittest.TestCase):
+@unittest.skipIf(_testcapi is None, 'need the _testcapi module')
+class CPyTimeTestCase:
+ """
+ Base class to test the C _PyTime_t API.
+ """
+ OVERFLOW_SECONDS = None
+
+ def setUp(self):
+ from _testcapi import SIZEOF_TIME_T
+ bits = SIZEOF_TIME_T * 8 - 1
+ self.time_t_min = -2 ** bits
+ self.time_t_max = 2 ** bits - 1
+
+ def time_t_filter(self, seconds):
+ return (self.time_t_min <= seconds <= self.time_t_max)
+
+ def _rounding_values(self, use_float):
+ "Build timestamps used to test rounding."
+
+ units = [1, US_TO_NS, MS_TO_NS, SEC_TO_NS]
+ if use_float:
+ # picoseconds are only tested to pytime_converter accepting floats
+ units.append(1e-3)
+
+ values = (
+ # small values
+ 1, 2, 5, 7, 123, 456, 1234,
+ # 10^k - 1
+ 9,
+ 99,
+ 999,
+ 9999,
+ 99999,
+ 999999,
+ # test half even rounding near 0.5, 1.5, 2.5, 3.5, 4.5
+ 499, 500, 501,
+ 1499, 1500, 1501,
+ 2500,
+ 3500,
+ 4500,
+ )
+
+ ns_timestamps = [0]
+ for unit in units:
+ for value in values:
+ ns = value * unit
+ ns_timestamps.extend((-ns, ns))
+ for pow2 in (0, 5, 10, 15, 22, 23, 24, 30, 33):
+ ns = (2 ** pow2) * SEC_TO_NS
+ ns_timestamps.extend((
+ -ns-1, -ns, -ns+1,
+ ns-1, ns, ns+1
+ ))
+ for seconds in (_testcapi.INT_MIN, _testcapi.INT_MAX):
+ ns_timestamps.append(seconds * SEC_TO_NS)
+ if use_float:
+ # numbers with an exact representation in IEEE 754 (base 2)
+ for pow2 in (3, 7, 10, 15):
+ ns = 2.0 ** (-pow2)
+ ns_timestamps.extend((-ns, ns))
+
+ # seconds close to _PyTime_t type limit
+ ns = (2 ** 63 // SEC_TO_NS) * SEC_TO_NS
+ ns_timestamps.extend((-ns, ns))
+
+ return ns_timestamps
+
+ def _check_rounding(self, pytime_converter, expected_func,
+ use_float, unit_to_sec, value_filter=None):
+
+ def convert_values(ns_timestamps):
+ if use_float:
+ unit_to_ns = SEC_TO_NS / float(unit_to_sec)
+ values = [ns / unit_to_ns for ns in ns_timestamps]
+ else:
+ unit_to_ns = SEC_TO_NS // unit_to_sec
+ values = [ns // unit_to_ns for ns in ns_timestamps]
+
+ if value_filter:
+ values = filter(value_filter, values)
+
+ # remove duplicates and sort
+ return sorted(set(values))
+
+ # test rounding
+ ns_timestamps = self._rounding_values(use_float)
+ valid_values = convert_values(ns_timestamps)
+ for time_rnd, decimal_rnd in ROUNDING_MODES :
+ context = decimal.getcontext()
+ context.rounding = decimal_rnd
+
+ for value in valid_values:
+ debug_info = {'value': value, 'rounding': decimal_rnd}
+ try:
+ result = pytime_converter(value, time_rnd)
+ expected = expected_func(value)
+ except Exception as exc:
+ self.fail("Error on timestamp conversion: %s" % debug_info)
+ self.assertEqual(result,
+ expected,
+ debug_info)
+
+ # test overflow
+ ns = self.OVERFLOW_SECONDS * SEC_TO_NS
+ ns_timestamps = (-ns, ns)
+ overflow_values = convert_values(ns_timestamps)
+ for time_rnd, _ in ROUNDING_MODES :
+ for value in overflow_values:
+ debug_info = {'value': value, 'rounding': time_rnd}
+ with self.assertRaises(OverflowError, msg=debug_info):
+ pytime_converter(value, time_rnd)
+
+ def check_int_rounding(self, pytime_converter, expected_func,
+ unit_to_sec=1, value_filter=None):
+ self._check_rounding(pytime_converter, expected_func,
+ False, unit_to_sec, value_filter)
+
+ def check_float_rounding(self, pytime_converter, expected_func,
+ unit_to_sec=1, value_filter=None):
+ self._check_rounding(pytime_converter, expected_func,
+ True, unit_to_sec, value_filter)
+
+ def decimal_round(self, x):
+ d = decimal.Decimal(x)
+ d = d.quantize(1)
+ return int(d)
+
+
+class TestCPyTime(CPyTimeTestCase, unittest.TestCase):
+ """
+ Test the C _PyTime_t API.
+ """
+ # _PyTime_t is a 64-bit signed integer
+ OVERFLOW_SECONDS = math.ceil((2**63 + 1) / SEC_TO_NS)
+
def test_FromSeconds(self):
from _testcapi import PyTime_FromSeconds
- for seconds in (0, 3, -456, _testcapi.INT_MAX, _testcapi.INT_MIN):
- with self.subTest(seconds=seconds):
- self.assertEqual(PyTime_FromSeconds(seconds),
- seconds * SEC_TO_NS)
+
+ # PyTime_FromSeconds() expects a C int, reject values out of range
+ def c_int_filter(secs):
+ return (_testcapi.INT_MIN <= secs <= _testcapi.INT_MAX)
+
+ self.check_int_rounding(lambda secs, rnd: PyTime_FromSeconds(secs),
+ lambda secs: secs * SEC_TO_NS,
+ value_filter=c_int_filter)
def test_FromSecondsObject(self):
from _testcapi import PyTime_FromSecondsObject
- # Conversion giving the same result for all rounding methods
- for rnd in ALL_ROUNDING_METHODS:
- for obj, ts in (
- # integers
- (0, 0),
- (1, SEC_TO_NS),
- (-3, -3 * SEC_TO_NS),
-
- # float: subseconds
- (0.0, 0),
- (1e-9, 1),
- (1e-6, 10 ** 3),
- (1e-3, 10 ** 6),
-
- # float: seconds
- (2.0, 2 * SEC_TO_NS),
- (123.0, 123 * SEC_TO_NS),
- (-7.0, -7 * SEC_TO_NS),
-
- # nanosecond are kept for value <= 2^23 seconds
- (2**22 - 1e-9, 4194303999999999),
- (2**22, 4194304000000000),
- (2**22 + 1e-9, 4194304000000001),
- (2**23 - 1e-9, 8388607999999999),
- (2**23, 8388608000000000),
-
- # start losing precision for value > 2^23 seconds
- (2**23 + 1e-9, 8388608000000002),
-
- # nanoseconds are lost for value > 2^23 seconds
- (2**24 - 1e-9, 16777215999999998),
- (2**24, 16777216000000000),
- (2**24 + 1e-9, 16777216000000000),
- (2**25 - 1e-9, 33554432000000000),
- (2**25 , 33554432000000000),
- (2**25 + 1e-9, 33554432000000000),
-
- # close to 2^63 nanoseconds (_PyTime_t limit)
- (9223372036, 9223372036 * SEC_TO_NS),
- (9223372036.0, 9223372036 * SEC_TO_NS),
- (-9223372036, -9223372036 * SEC_TO_NS),
- (-9223372036.0, -9223372036 * SEC_TO_NS),
- ):
- with self.subTest(obj=obj, round=rnd, timestamp=ts):
- self.assertEqual(PyTime_FromSecondsObject(obj, rnd), ts)
-
- with self.subTest(round=rnd):
- with self.assertRaises(OverflowError):
- PyTime_FromSecondsObject(9223372037, rnd)
- PyTime_FromSecondsObject(9223372037.0, rnd)
- PyTime_FromSecondsObject(-9223372037, rnd)
- PyTime_FromSecondsObject(-9223372037.0, rnd)
-
- # Conversion giving different results depending on the rounding method
- FLOOR = _PyTime.ROUND_FLOOR
- CEILING = _PyTime.ROUND_CEILING
- for obj, ts, rnd in (
- # close to zero
- ( 1e-10, 0, FLOOR),
- ( 1e-10, 1, CEILING),
- (-1e-10, -1, FLOOR),
- (-1e-10, 0, CEILING),
-
- # test rounding of the last nanosecond
- ( 1.1234567899, 1123456789, FLOOR),
- ( 1.1234567899, 1123456790, CEILING),
- (-1.1234567899, -1123456790, FLOOR),
- (-1.1234567899, -1123456789, CEILING),
-
- # close to 1 second
- ( 0.9999999999, 999999999, FLOOR),
- ( 0.9999999999, 1000000000, CEILING),
- (-0.9999999999, -1000000000, FLOOR),
- (-0.9999999999, -999999999, CEILING),
- ):
- with self.subTest(obj=obj, round=rnd, timestamp=ts):
- self.assertEqual(PyTime_FromSecondsObject(obj, rnd), ts)
+ self.check_int_rounding(
+ PyTime_FromSecondsObject,
+ lambda secs: secs * SEC_TO_NS)
+
+ self.check_float_rounding(
+ PyTime_FromSecondsObject,
+ lambda ns: self.decimal_round(ns * SEC_TO_NS))
def test_AsSecondsDouble(self):
from _testcapi import PyTime_AsSecondsDouble
- for nanoseconds, seconds in (
- # near 1 nanosecond
- ( 0, 0.0),
- ( 1, 1e-9),
- (-1, -1e-9),
-
- # near 1 second
- (SEC_TO_NS + 1, 1.0 + 1e-9),
- (SEC_TO_NS, 1.0),
- (SEC_TO_NS - 1, 1.0 - 1e-9),
-
- # a few seconds
- (123 * SEC_TO_NS, 123.0),
- (-567 * SEC_TO_NS, -567.0),
-
- # nanosecond are kept for value <= 2^23 seconds
- (4194303999999999, 2**22 - 1e-9),
- (4194304000000000, 2**22),
- (4194304000000001, 2**22 + 1e-9),
-
- # start losing precision for value > 2^23 seconds
- (8388608000000002, 2**23 + 1e-9),
-
- # nanoseconds are lost for value > 2^23 seconds
- (16777215999999998, 2**24 - 1e-9),
- (16777215999999999, 2**24 - 1e-9),
- (16777216000000000, 2**24 ),
- (16777216000000001, 2**24 ),
- (16777216000000002, 2**24 + 2e-9),
-
- (33554432000000000, 2**25 ),
- (33554432000000002, 2**25 ),
- (33554432000000004, 2**25 + 4e-9),
-
- # close to 2^63 nanoseconds (_PyTime_t limit)
- (9223372036 * SEC_TO_NS, 9223372036.0),
- (-9223372036 * SEC_TO_NS, -9223372036.0),
- ):
- with self.subTest(nanoseconds=nanoseconds, seconds=seconds):
- self.assertEqual(PyTime_AsSecondsDouble(nanoseconds),
- seconds)
-
- def test_timeval(self):
+ def float_converter(ns):
+ if abs(ns) % SEC_TO_NS == 0:
+ return float(ns // SEC_TO_NS)
+ else:
+ return float(ns) / SEC_TO_NS
+
+ self.check_int_rounding(lambda ns, rnd: PyTime_AsSecondsDouble(ns),
+ float_converter,
+ NS_TO_SEC)
+
+ def create_decimal_converter(self, denominator):
+ denom = decimal.Decimal(denominator)
+
+ def converter(value):
+ d = decimal.Decimal(value) / denom
+ return self.decimal_round(d)
+
+ return converter
+
+ def test_AsTimeval(self):
from _testcapi import PyTime_AsTimeval
- for rnd in ALL_ROUNDING_METHODS:
- for ns, tv in (
- # microseconds
- (0, (0, 0)),
- (1000, (0, 1)),
- (-1000, (-1, 999999)),
-
- # seconds
- (2 * SEC_TO_NS, (2, 0)),
- (-3 * SEC_TO_NS, (-3, 0)),
- ):
- with self.subTest(nanoseconds=ns, timeval=tv, round=rnd):
- self.assertEqual(PyTime_AsTimeval(ns, rnd), tv)
-
- FLOOR = _PyTime.ROUND_FLOOR
- CEILING = _PyTime.ROUND_CEILING
- for ns, tv, rnd in (
- # nanoseconds
- (1, (0, 0), FLOOR),
- (1, (0, 1), CEILING),
- (-1, (-1, 999999), FLOOR),
- (-1, (0, 0), CEILING),
-
- # seconds + nanoseconds
- (1234567001, (1, 234567), FLOOR),
- (1234567001, (1, 234568), CEILING),
- (-1234567001, (-2, 765432), FLOOR),
- (-1234567001, (-2, 765433), CEILING),
- ):
- with self.subTest(nanoseconds=ns, timeval=tv, round=rnd):
- self.assertEqual(PyTime_AsTimeval(ns, rnd), tv)
+
+ us_converter = self.create_decimal_converter(US_TO_NS)
+
+ def timeval_converter(ns):
+ us = us_converter(ns)
+ return divmod(us, SEC_TO_US)
+
+ if sys.platform == 'win32':
+ from _testcapi import LONG_MIN, LONG_MAX
+
+ # On Windows, timeval.tv_sec type is a C long
+ def seconds_filter(secs):
+ return LONG_MIN <= secs <= LONG_MAX
+ else:
+ seconds_filter = self.time_t_filter
+
+ self.check_int_rounding(PyTime_AsTimeval,
+ timeval_converter,
+ NS_TO_SEC,
+ value_filter=seconds_filter)
@unittest.skipUnless(hasattr(_testcapi, 'PyTime_AsTimespec'),
'need _testcapi.PyTime_AsTimespec')
- def test_timespec(self):
+ def test_AsTimespec(self):
from _testcapi import PyTime_AsTimespec
- for ns, ts in (
- # nanoseconds
- (0, (0, 0)),
- (1, (0, 1)),
- (-1, (-1, 999999999)),
-
- # seconds
- (2 * SEC_TO_NS, (2, 0)),
- (-3 * SEC_TO_NS, (-3, 0)),
-
- # seconds + nanoseconds
- (1234567890, (1, 234567890)),
- (-1234567890, (-2, 765432110)),
- ):
- with self.subTest(nanoseconds=ns, timespec=ts):
- self.assertEqual(PyTime_AsTimespec(ns), ts)
-
- def test_milliseconds(self):
+
+ def timespec_converter(ns):
+ return divmod(ns, SEC_TO_NS)
+
+ self.check_int_rounding(lambda ns, rnd: PyTime_AsTimespec(ns),
+ timespec_converter,
+ NS_TO_SEC,
+ value_filter=self.time_t_filter)
+
+ def test_AsMilliseconds(self):
from _testcapi import PyTime_AsMilliseconds
- for rnd in ALL_ROUNDING_METHODS:
- for ns, tv in (
- # milliseconds
- (1 * MS_TO_NS, 1),
- (-2 * MS_TO_NS, -2),
-
- # seconds
- (2 * SEC_TO_NS, 2000),
- (-3 * SEC_TO_NS, -3000),
- ):
- with self.subTest(nanoseconds=ns, timeval=tv, round=rnd):
- self.assertEqual(PyTime_AsMilliseconds(ns, rnd), tv)
-
- FLOOR = _PyTime.ROUND_FLOOR
- CEILING = _PyTime.ROUND_CEILING
- for ns, ms, rnd in (
- # nanoseconds
- (1, 0, FLOOR),
- (1, 1, CEILING),
- (-1, -1, FLOOR),
- (-1, 0, CEILING),
-
- # seconds + nanoseconds
- (1234 * MS_TO_NS + 1, 1234, FLOOR),
- (1234 * MS_TO_NS + 1, 1235, CEILING),
- (-1234 * MS_TO_NS - 1, -1235, FLOOR),
- (-1234 * MS_TO_NS - 1, -1234, CEILING),
- ):
- with self.subTest(nanoseconds=ns, milliseconds=ms, round=rnd):
- self.assertEqual(PyTime_AsMilliseconds(ns, rnd), ms)
-
- def test_microseconds(self):
+
+ self.check_int_rounding(PyTime_AsMilliseconds,
+ self.create_decimal_converter(MS_TO_NS),
+ NS_TO_SEC)
+
+ def test_AsMicroseconds(self):
from _testcapi import PyTime_AsMicroseconds
- for rnd in ALL_ROUNDING_METHODS:
- for ns, tv in (
- # microseconds
- (1 * US_TO_NS, 1),
- (-2 * US_TO_NS, -2),
-
- # milliseconds
- (1 * MS_TO_NS, 1000),
- (-2 * MS_TO_NS, -2000),
-
- # seconds
- (2 * SEC_TO_NS, 2000000),
- (-3 * SEC_TO_NS, -3000000),
- ):
- with self.subTest(nanoseconds=ns, timeval=tv, round=rnd):
- self.assertEqual(PyTime_AsMicroseconds(ns, rnd), tv)
-
- FLOOR = _PyTime.ROUND_FLOOR
- CEILING = _PyTime.ROUND_CEILING
- for ns, ms, rnd in (
- # nanoseconds
- (1, 0, FLOOR),
- (1, 1, CEILING),
- (-1, -1, FLOOR),
- (-1, 0, CEILING),
-
- # seconds + nanoseconds
- (1234 * US_TO_NS + 1, 1234, FLOOR),
- (1234 * US_TO_NS + 1, 1235, CEILING),
- (-1234 * US_TO_NS - 1, -1235, FLOOR),
- (-1234 * US_TO_NS - 1, -1234, CEILING),
- ):
- with self.subTest(nanoseconds=ns, milliseconds=ms, round=rnd):
- self.assertEqual(PyTime_AsMicroseconds(ns, rnd), ms)
+
+ self.check_int_rounding(PyTime_AsMicroseconds,
+ self.create_decimal_converter(US_TO_NS),
+ NS_TO_SEC)
+
+
+class TestOldPyTime(CPyTimeTestCase, unittest.TestCase):
+ """
+ Test the old C _PyTime_t API: _PyTime_ObjectToXXX() functions.
+ """
+
+ # time_t is a 32-bit or 64-bit signed integer
+ OVERFLOW_SECONDS = 2 ** 64
+
+ def test_object_to_time_t(self):
+ from _testcapi import pytime_object_to_time_t
+
+ self.check_int_rounding(pytime_object_to_time_t,
+ lambda secs: secs,
+ value_filter=self.time_t_filter)
+
+ self.check_float_rounding(pytime_object_to_time_t,
+ self.decimal_round,
+ value_filter=self.time_t_filter)
+
+ def create_converter(self, sec_to_unit):
+ def converter(secs):
+ floatpart, intpart = math.modf(secs)
+ intpart = int(intpart)
+ floatpart *= sec_to_unit
+ floatpart = self.decimal_round(floatpart)
+ if floatpart < 0:
+ floatpart += sec_to_unit
+ intpart -= 1
+ elif floatpart >= sec_to_unit:
+ floatpart -= sec_to_unit
+ intpart += 1
+ return (intpart, floatpart)
+ return converter
+
+ def test_object_to_timeval(self):
+ from _testcapi import pytime_object_to_timeval
+
+ self.check_int_rounding(pytime_object_to_timeval,
+ lambda secs: (secs, 0),
+ value_filter=self.time_t_filter)
+
+ self.check_float_rounding(pytime_object_to_timeval,
+ self.create_converter(SEC_TO_US),
+ value_filter=self.time_t_filter)
+
+ def test_object_to_timespec(self):
+ from _testcapi import pytime_object_to_timespec
+
+ self.check_int_rounding(pytime_object_to_timespec,
+ lambda secs: (secs, 0),
+ value_filter=self.time_t_filter)
+
+ self.check_float_rounding(pytime_object_to_timespec,
+ self.create_converter(SEC_TO_NS),
+ value_filter=self.time_t_filter)
if __name__ == "__main__":
diff --git a/Lib/test/test_timeit.py b/Lib/test/test_timeit.py
index 2db3c1b..1a95e29 100644
--- a/Lib/test/test_timeit.py
+++ b/Lib/test/test_timeit.py
@@ -354,6 +354,28 @@ class TestTimeit(unittest.TestCase):
s = self.run_main(switches=['-n1', '1/0'])
self.assert_exc_string(error_stringio.getvalue(), 'ZeroDivisionError')
+ def autorange(self, callback=None):
+ timer = FakeTimer(seconds_per_increment=0.001)
+ t = timeit.Timer(stmt=self.fake_stmt, setup=self.fake_setup, timer=timer)
+ return t.autorange(callback)
+
+ def test_autorange(self):
+ num_loops, time_taken = self.autorange()
+ self.assertEqual(num_loops, 1000)
+ self.assertEqual(time_taken, 1.0)
+
+ def test_autorange_with_callback(self):
+ def callback(a, b):
+ print("{} {:.3f}".format(a, b))
+ with captured_stdout() as s:
+ num_loops, time_taken = self.autorange(callback)
+ self.assertEqual(num_loops, 1000)
+ self.assertEqual(time_taken, 1.0)
+ expected = ('10 0.010\n'
+ '100 0.100\n'
+ '1000 1.000\n')
+ self.assertEqual(s.getvalue(), expected)
+
if __name__ == '__main__':
unittest.main()
diff --git a/Lib/test/test_tokenize.py b/Lib/test/test_tokenize.py
index 3b17ca6..5a81a5f 100644
--- a/Lib/test/test_tokenize.py
+++ b/Lib/test/test_tokenize.py
@@ -4,6 +4,8 @@ from tokenize import (tokenize, _tokenize, untokenize, NUMBER, NAME, OP,
open as tokenize_open, Untokenizer)
from io import BytesIO
from unittest import TestCase, mock
+from test.test_grammar import (VALID_UNDERSCORE_LITERALS,
+ INVALID_UNDERSCORE_LITERALS)
import os
import token
@@ -24,8 +26,7 @@ class TokenizeTest(TestCase):
if type == ENDMARKER:
break
type = tok_name[type]
- result.append(" %(type)-10.10s %(token)-13.13r %(start)s %(end)s" %
- locals())
+ result.append(f" {type:10} {token!r:13} {start} {end}")
self.assertEqual(result,
[" ENCODING 'utf-8' (0, 0) (0, 0)"] +
expected.rstrip().splitlines())
@@ -132,18 +133,18 @@ def k(x):
self.check_tokenize("x = 0xfffffffffff", """\
NAME 'x' (1, 0) (1, 1)
OP '=' (1, 2) (1, 3)
- NUMBER '0xffffffffff (1, 4) (1, 17)
+ NUMBER '0xfffffffffff' (1, 4) (1, 17)
""")
self.check_tokenize("x = 123141242151251616110", """\
NAME 'x' (1, 0) (1, 1)
OP '=' (1, 2) (1, 3)
- NUMBER '123141242151 (1, 4) (1, 25)
+ NUMBER '123141242151251616110' (1, 4) (1, 25)
""")
self.check_tokenize("x = -15921590215012591", """\
NAME 'x' (1, 0) (1, 1)
OP '=' (1, 2) (1, 3)
OP '-' (1, 4) (1, 5)
- NUMBER '159215902150 (1, 5) (1, 22)
+ NUMBER '15921590215012591' (1, 5) (1, 22)
""")
def test_float(self):
@@ -186,6 +187,21 @@ def k(x):
NUMBER '3.14e159' (1, 4) (1, 12)
""")
+ def test_underscore_literals(self):
+ def number_token(s):
+ f = BytesIO(s.encode('utf-8'))
+ for toktype, token, start, end, line in tokenize(f.readline):
+ if toktype == NUMBER:
+ return token
+ return 'invalid token'
+ for lit in VALID_UNDERSCORE_LITERALS:
+ if '(' in lit:
+ # this won't work with compound complex inputs
+ continue
+ self.assertEqual(number_token(lit), lit)
+ for lit in INVALID_UNDERSCORE_LITERALS:
+ self.assertNotEqual(number_token(lit), lit)
+
def test_string(self):
# String literals
self.check_tokenize("x = ''; y = \"\"", """\
@@ -307,6 +323,50 @@ def k(x):
OP '+' (1, 28) (1, 29)
STRING 'RB"abc"' (1, 30) (1, 37)
""")
+ # Check 0, 1, and 2 character string prefixes.
+ self.check_tokenize(r'"a\
+de\
+fg"', """\
+ STRING '"a\\\\\\nde\\\\\\nfg"\' (1, 0) (3, 3)
+ """)
+ self.check_tokenize(r'u"a\
+de"', """\
+ STRING 'u"a\\\\\\nde"\' (1, 0) (2, 3)
+ """)
+ self.check_tokenize(r'rb"a\
+d"', """\
+ STRING 'rb"a\\\\\\nd"\' (1, 0) (2, 2)
+ """)
+ self.check_tokenize(r'"""a\
+b"""', """\
+ STRING '\"\""a\\\\\\nb\"\""' (1, 0) (2, 4)
+ """)
+ self.check_tokenize(r'u"""a\
+b"""', """\
+ STRING 'u\"\""a\\\\\\nb\"\""' (1, 0) (2, 4)
+ """)
+ self.check_tokenize(r'rb"""a\
+b\
+c"""', """\
+ STRING 'rb"\""a\\\\\\nb\\\\\\nc"\""' (1, 0) (3, 4)
+ """)
+ self.check_tokenize('f"abc"', """\
+ STRING 'f"abc"' (1, 0) (1, 6)
+ """)
+ self.check_tokenize('fR"a{b}c"', """\
+ STRING 'fR"a{b}c"' (1, 0) (1, 9)
+ """)
+ self.check_tokenize('f"""abc"""', """\
+ STRING 'f\"\"\"abc\"\"\"' (1, 0) (1, 10)
+ """)
+ self.check_tokenize(r'f"abc\
+def"', """\
+ STRING 'f"abc\\\\\\ndef"' (1, 0) (2, 4)
+ """)
+ self.check_tokenize(r'Rf"abc\
+def"', """\
+ STRING 'Rf"abc\\\\\\ndef"' (1, 0) (2, 4)
+ """)
def test_function(self):
self.check_tokenize("def d22(a, b, c=2, d=2, *k): pass", """\
@@ -505,7 +565,7 @@ def k(x):
# Methods
self.check_tokenize("@staticmethod\ndef foo(x,y): pass", """\
OP '@' (1, 0) (1, 1)
- NAME 'staticmethod (1, 1) (1, 13)
+ NAME 'staticmethod' (1, 1) (1, 13)
NEWLINE '\\n' (1, 13) (1, 14)
NAME 'def' (2, 0) (2, 3)
NAME 'foo' (2, 4) (2, 7)
@@ -1488,10 +1548,10 @@ class TestRoundtrip(TestCase):
# Tokenize is broken on test_pep3131.py because regular expressions are
# broken on the obscure unicode identifiers in it. *sigh*
- # With roundtrip extended to test the 5-tuple mode of untokenize,
+ # With roundtrip extended to test the 5-tuple mode of untokenize,
# 7 more testfiles fail. Remove them also until the failure is diagnosed.
- testfiles.remove(os.path.join(tempdir, "test_pep3131.py"))
+ testfiles.remove(os.path.join(tempdir, "test_unicode_identifiers.py"))
for f in ('buffer', 'builtin', 'fileio', 'inspect', 'os', 'platform', 'sys'):
testfiles.remove(os.path.join(tempdir, "test_%s.py") % f)
diff --git a/Lib/test/test_tools/__init__.py b/Lib/test/test_tools/__init__.py
index 04c8726..4d0fca3 100644
--- a/Lib/test/test_tools/__init__.py
+++ b/Lib/test/test_tools/__init__.py
@@ -3,7 +3,6 @@ import os
import unittest
import importlib
from test import support
-from fnmatch import fnmatch
basepath = os.path.dirname( # <src/install dir>
os.path.dirname( # Lib
diff --git a/Lib/test/test_tools/test_gprof2html.py b/Lib/test/test_tools/test_gprof2html.py
index 0c294ec..9489ed9 100644
--- a/Lib/test/test_tools/test_gprof2html.py
+++ b/Lib/test/test_tools/test_gprof2html.py
@@ -2,12 +2,11 @@
import os
import sys
-import importlib
import unittest
from unittest import mock
import tempfile
-from test.test_tools import scriptsdir, skip_if_missing, import_tool
+from test.test_tools import skip_if_missing, import_tool
skip_if_missing()
diff --git a/Lib/test/test_tools/test_md5sum.py b/Lib/test/test_tools/test_md5sum.py
index 1305295..fb565b7 100644
--- a/Lib/test/test_tools/test_md5sum.py
+++ b/Lib/test/test_tools/test_md5sum.py
@@ -1,12 +1,11 @@
"""Tests for the md5sum script in the Tools directory."""
import os
-import sys
import unittest
from test import support
from test.support.script_helper import assert_python_ok, assert_python_failure
-from test.test_tools import scriptsdir, import_tool, skip_if_missing
+from test.test_tools import scriptsdir, skip_if_missing
skip_if_missing()
diff --git a/Lib/test/test_tools/test_pdeps.py b/Lib/test/test_tools/test_pdeps.py
index 091fa6a..27cbfe2 100644
--- a/Lib/test/test_tools/test_pdeps.py
+++ b/Lib/test/test_tools/test_pdeps.py
@@ -1,12 +1,10 @@
"""Tests for the pdeps script in the Tools directory."""
import os
-import sys
import unittest
import tempfile
-from test import support
-from test.test_tools import scriptsdir, skip_if_missing, import_tool
+from test.test_tools import skip_if_missing, import_tool
skip_if_missing()
diff --git a/Lib/test/test_tools/test_unparse.py b/Lib/test/test_tools/test_unparse.py
index 734bbc2..65dee1b 100644
--- a/Lib/test/test_tools/test_unparse.py
+++ b/Lib/test/test_tools/test_unparse.py
@@ -134,6 +134,11 @@ class ASTTestCase(unittest.TestCase):
class UnparseTestCase(ASTTestCase):
# Tests for specific bugs found in earlier versions of unparse
+ def test_fstrings(self):
+ # See issue 25180
+ self.check_roundtrip(r"""f'{f"{0}"*3}'""")
+ self.check_roundtrip(r"""f'{f"{y}"*3}'""")
+
def test_del_statement(self):
self.check_roundtrip("del x, y, z")
@@ -279,8 +284,18 @@ class DirectoryTestCase(ASTTestCase):
for filename in names:
if test.support.verbose:
print('Testing %s' % filename)
- source = read_pyfile(filename)
- self.check_roundtrip(source)
+
+ # Some f-strings are not correctly round-tripped by
+ # Tools/parser/unparse.py. See issue 28002 for details.
+ # We need to skip files that contain such f-strings.
+ if os.path.basename(filename) in ('test_fstring.py', ):
+ if test.support.verbose:
+ print(f'Skipping {filename}: see issue 28002')
+ continue
+
+ with self.subTest(filename=filename):
+ source = read_pyfile(filename)
+ self.check_roundtrip(source)
if __name__ == '__main__':
diff --git a/Lib/test/test_trace.py b/Lib/test/test_trace.py
index f66a3bc..1d87aea 100644
--- a/Lib/test/test_trace.py
+++ b/Lib/test/test_trace.py
@@ -1,11 +1,11 @@
import os
-import io
import sys
from test.support import TESTFN, rmtree, unlink, captured_stdout
+from test.support.script_helper import assert_python_ok, assert_python_failure
import unittest
import trace
-from trace import CoverageResults, Trace
+from trace import Trace
from test.tracedmodules import testmod
@@ -365,51 +365,27 @@ class Test_Ignore(unittest.TestCase):
# Matched before.
self.assertTrue(ignore.names(jn('bar', 'baz.py'), 'baz'))
-
-class TestDeprecatedMethods(unittest.TestCase):
-
- def test_deprecated_usage(self):
- sio = io.StringIO()
- with self.assertWarns(DeprecationWarning):
- trace.usage(sio)
- self.assertIn('Usage:', sio.getvalue())
-
- def test_deprecated_Ignore(self):
- with self.assertWarns(DeprecationWarning):
- trace.Ignore()
-
- def test_deprecated_modname(self):
- with self.assertWarns(DeprecationWarning):
- self.assertEqual("spam", trace.modname("spam"))
-
- def test_deprecated_fullmodname(self):
- with self.assertWarns(DeprecationWarning):
- self.assertEqual("spam", trace.fullmodname("spam"))
-
- def test_deprecated_find_lines_from_code(self):
- with self.assertWarns(DeprecationWarning):
- def foo():
- pass
- trace.find_lines_from_code(foo.__code__, ["eggs"])
-
- def test_deprecated_find_lines(self):
- with self.assertWarns(DeprecationWarning):
- def foo():
- pass
- trace.find_lines(foo.__code__, ["eggs"])
-
- def test_deprecated_find_strings(self):
- with open(TESTFN, 'w') as fd:
- self.addCleanup(unlink, TESTFN)
- with self.assertWarns(DeprecationWarning):
- trace.find_strings(fd.name)
-
- def test_deprecated_find_executable_linenos(self):
+class TestCommandLine(unittest.TestCase):
+
+ def test_failures(self):
+ _errors = (
+ (b'filename is missing: required with the main options', '-l', '-T'),
+ (b'cannot specify both --listfuncs and (--trace or --count)', '-lc'),
+ (b'argument -R/--no-report: not allowed with argument -r/--report', '-rR'),
+ (b'must specify one of --trace, --count, --report, --listfuncs, or --trackcalls', '-g'),
+ (b'-r/--report requires -f/--file', '-r'),
+ (b'--summary can only be used with --count or --report', '-sT'),
+ (b'unrecognized arguments: -y', '-y'))
+ for message, *args in _errors:
+ *_, stderr = assert_python_failure('-m', 'trace', *args)
+ self.assertIn(message, stderr)
+
+ def test_listfuncs_flag_success(self):
with open(TESTFN, 'w') as fd:
self.addCleanup(unlink, TESTFN)
- with self.assertWarns(DeprecationWarning):
- trace.find_executable_linenos(fd.name)
-
+ fd.write("a = 1\n")
+ status, stdout, stderr = assert_python_ok('-m', 'trace', '-l', TESTFN)
+ self.assertIn(b'functions called:', stdout)
if __name__ == '__main__':
unittest.main()
diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py
index 49cedda..7276bc7 100644
--- a/Lib/test/test_traceback.py
+++ b/Lib/test/test_traceback.py
@@ -125,12 +125,12 @@ class TracebackCases(unittest.TestCase):
def do_test(firstlines, message, charset, lineno):
# Raise the message in a subprocess, and catch the output
try:
- output = open(TESTFN, "w", encoding=charset)
- output.write("""{0}if 1:
- import traceback;
- raise RuntimeError('{1}')
- """.format(firstlines, message))
- output.close()
+ with open(TESTFN, "w", encoding=charset) as output:
+ output.write("""{0}if 1:
+ import traceback;
+ raise RuntimeError('{1}')
+ """.format(firstlines, message))
+
process = subprocess.Popen([sys.executable, TESTFN],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout, stderr = process.communicate()
@@ -171,8 +171,8 @@ class TracebackCases(unittest.TestCase):
text, charset, 5)
do_test(" \t\f\n# coding: {0}\n".format(charset),
text, charset, 5)
- # Issue #18960: coding spec should has no effect
- do_test("0\n# coding: GBK\n", "h\xe9 ho", 'utf-8', 5)
+ # Issue #18960: coding spec should have no effect
+ do_test("x=0\n# coding: GBK\n", "h\xe9 ho", 'utf-8', 5)
@support.requires_type_collecting
def test_print_traceback_at_exit(self):
@@ -299,6 +299,137 @@ class TracebackFormatTests(unittest.TestCase):
' traceback.print_stack()',
])
+ # issue 26823 - Shrink recursive tracebacks
+ def _check_recursive_traceback_display(self, render_exc):
+ # Always show full diffs when this test fails
+ # Note that rearranging things may require adjusting
+ # the relative line numbers in the expected tracebacks
+ self.maxDiff = None
+
+ # Check hitting the recursion limit
+ def f():
+ f()
+
+ with captured_output("stderr") as stderr_f:
+ try:
+ f()
+ except RecursionError as exc:
+ render_exc()
+ else:
+ self.fail("no recursion occurred")
+
+ lineno_f = f.__code__.co_firstlineno
+ result_f = (
+ 'Traceback (most recent call last):\n'
+ f' File "{__file__}", line {lineno_f+5}, in _check_recursive_traceback_display\n'
+ ' f()\n'
+ f' File "{__file__}", line {lineno_f+1}, in f\n'
+ ' f()\n'
+ f' File "{__file__}", line {lineno_f+1}, in f\n'
+ ' f()\n'
+ f' File "{__file__}", line {lineno_f+1}, in f\n'
+ ' f()\n'
+ # XXX: The following line changes depending on whether the tests
+ # are run through the interactive interpreter or with -m
+ # It also varies depending on the platform (stack size)
+ # Fortunately, we don't care about exactness here, so we use regex
+ r' \[Previous line repeated (\d+) more times\]' '\n'
+ 'RecursionError: maximum recursion depth exceeded\n'
+ )
+
+ expected = result_f.splitlines()
+ actual = stderr_f.getvalue().splitlines()
+
+ # Check the output text matches expectations
+ # 2nd last line contains the repetition count
+ self.assertEqual(actual[:-2], expected[:-2])
+ self.assertRegex(actual[-2], expected[-2])
+ self.assertEqual(actual[-1], expected[-1])
+
+ # Check the recursion count is roughly as expected
+ rec_limit = sys.getrecursionlimit()
+ self.assertIn(int(re.search(r"\d+", actual[-2]).group()), range(rec_limit-60, rec_limit))
+
+ # Check a known (limited) number of recursive invocations
+ def g(count=10):
+ if count:
+ return g(count-1)
+ raise ValueError
+
+ with captured_output("stderr") as stderr_g:
+ try:
+ g()
+ except ValueError as exc:
+ render_exc()
+ else:
+ self.fail("no value error was raised")
+
+ lineno_g = g.__code__.co_firstlineno
+ result_g = (
+ f' File "{__file__}", line {lineno_g+2}, in g\n'
+ ' return g(count-1)\n'
+ f' File "{__file__}", line {lineno_g+2}, in g\n'
+ ' return g(count-1)\n'
+ f' File "{__file__}", line {lineno_g+2}, in g\n'
+ ' return g(count-1)\n'
+ ' [Previous line repeated 6 more times]\n'
+ f' File "{__file__}", line {lineno_g+3}, in g\n'
+ ' raise ValueError\n'
+ 'ValueError\n'
+ )
+ tb_line = (
+ 'Traceback (most recent call last):\n'
+ f' File "{__file__}", line {lineno_g+7}, in _check_recursive_traceback_display\n'
+ ' g()\n'
+ )
+ expected = (tb_line + result_g).splitlines()
+ actual = stderr_g.getvalue().splitlines()
+ self.assertEqual(actual, expected)
+
+ # Check 2 different repetitive sections
+ def h(count=10):
+ if count:
+ return h(count-1)
+ g()
+
+ with captured_output("stderr") as stderr_h:
+ try:
+ h()
+ except ValueError as exc:
+ render_exc()
+ else:
+ self.fail("no value error was raised")
+
+ lineno_h = h.__code__.co_firstlineno
+ result_h = (
+ 'Traceback (most recent call last):\n'
+ f' File "{__file__}", line {lineno_h+7}, in _check_recursive_traceback_display\n'
+ ' h()\n'
+ f' File "{__file__}", line {lineno_h+2}, in h\n'
+ ' return h(count-1)\n'
+ f' File "{__file__}", line {lineno_h+2}, in h\n'
+ ' return h(count-1)\n'
+ f' File "{__file__}", line {lineno_h+2}, in h\n'
+ ' return h(count-1)\n'
+ ' [Previous line repeated 6 more times]\n'
+ f' File "{__file__}", line {lineno_h+3}, in h\n'
+ ' g()\n'
+ )
+ expected = (result_h + result_g).splitlines()
+ actual = stderr_h.getvalue().splitlines()
+ self.assertEqual(actual, expected)
+
+ def test_recursive_traceback_python(self):
+ self._check_recursive_traceback_display(traceback.print_exc)
+
+ @cpython_only
+ def test_recursive_traceback_cpython_internal(self):
+ from _testcapi import exception_print
+ def render_exc():
+ exc_type, exc_value, exc_tb = sys.exc_info()
+ exception_print(exc_value)
+ self._check_recursive_traceback_display(render_exc)
+
def test_format_stack(self):
def fmt():
return traceback.format_stack()
diff --git a/Lib/test/test_tracemalloc.py b/Lib/test/test_tracemalloc.py
index da89a9a..790ab7e 100644
--- a/Lib/test/test_tracemalloc.py
+++ b/Lib/test/test_tracemalloc.py
@@ -11,9 +11,15 @@ try:
import threading
except ImportError:
threading = None
+try:
+ import _testcapi
+except ImportError:
+ _testcapi = None
+
EMPTY_STRING_SIZE = sys.getsizeof(b'')
+
def get_frames(nframe, lineno_delta):
frames = []
frame = sys._getframe(1)
@@ -37,28 +43,31 @@ def allocate_bytes(size):
def create_snapshots():
traceback_limit = 2
+ # _tracemalloc._get_traces() returns a list of (domain, size,
+ # traceback_frames) tuples. traceback_frames is a tuple of (filename,
+ # line_number) tuples.
raw_traces = [
- (10, (('a.py', 2), ('b.py', 4))),
- (10, (('a.py', 2), ('b.py', 4))),
- (10, (('a.py', 2), ('b.py', 4))),
+ (0, 10, (('a.py', 2), ('b.py', 4))),
+ (0, 10, (('a.py', 2), ('b.py', 4))),
+ (0, 10, (('a.py', 2), ('b.py', 4))),
- (2, (('a.py', 5), ('b.py', 4))),
+ (1, 2, (('a.py', 5), ('b.py', 4))),
- (66, (('b.py', 1),)),
+ (2, 66, (('b.py', 1),)),
- (7, (('<unknown>', 0),)),
+ (3, 7, (('<unknown>', 0),)),
]
snapshot = tracemalloc.Snapshot(raw_traces, traceback_limit)
raw_traces2 = [
- (10, (('a.py', 2), ('b.py', 4))),
- (10, (('a.py', 2), ('b.py', 4))),
- (10, (('a.py', 2), ('b.py', 4))),
+ (0, 10, (('a.py', 2), ('b.py', 4))),
+ (0, 10, (('a.py', 2), ('b.py', 4))),
+ (0, 10, (('a.py', 2), ('b.py', 4))),
- (2, (('a.py', 5), ('b.py', 4))),
- (5000, (('a.py', 5), ('b.py', 4))),
+ (2, 2, (('a.py', 5), ('b.py', 4))),
+ (2, 5000, (('a.py', 5), ('b.py', 4))),
- (400, (('c.py', 578),)),
+ (4, 400, (('c.py', 578),)),
]
snapshot2 = tracemalloc.Snapshot(raw_traces2, traceback_limit)
@@ -126,7 +135,7 @@ class TestTracemallocEnabled(unittest.TestCase):
def find_trace(self, traces, traceback):
for trace in traces:
- if trace[1] == traceback._frames:
+ if trace[2] == traceback._frames:
return trace
self.fail("trace not found")
@@ -140,7 +149,7 @@ class TestTracemallocEnabled(unittest.TestCase):
trace = self.find_trace(traces, obj_traceback)
self.assertIsInstance(trace, tuple)
- size, traceback = trace
+ domain, size, traceback = trace
self.assertEqual(size, obj_size)
self.assertEqual(traceback, obj_traceback._frames)
@@ -167,9 +176,8 @@ class TestTracemallocEnabled(unittest.TestCase):
trace1 = self.find_trace(traces, obj1_traceback)
trace2 = self.find_trace(traces, obj2_traceback)
- size1, traceback1 = trace1
- size2, traceback2 = trace2
- self.assertEqual(traceback2, traceback1)
+ domain1, size1, traceback1 = trace1
+ domain2, size2, traceback2 = trace2
self.assertIs(traceback2, traceback1)
def test_get_traced_memory(self):
@@ -292,7 +300,7 @@ class TestSnapshot(unittest.TestCase):
maxDiff = 4000
def test_create_snapshot(self):
- raw_traces = [(5, (('a.py', 2),))]
+ raw_traces = [(0, 5, (('a.py', 2),))]
with contextlib.ExitStack() as stack:
stack.enter_context(patch.object(tracemalloc, 'is_tracing',
@@ -322,11 +330,11 @@ class TestSnapshot(unittest.TestCase):
# exclude b.py
snapshot3 = snapshot.filter_traces((filter1,))
self.assertEqual(snapshot3.traces._traces, [
- (10, (('a.py', 2), ('b.py', 4))),
- (10, (('a.py', 2), ('b.py', 4))),
- (10, (('a.py', 2), ('b.py', 4))),
- (2, (('a.py', 5), ('b.py', 4))),
- (7, (('<unknown>', 0),)),
+ (0, 10, (('a.py', 2), ('b.py', 4))),
+ (0, 10, (('a.py', 2), ('b.py', 4))),
+ (0, 10, (('a.py', 2), ('b.py', 4))),
+ (1, 2, (('a.py', 5), ('b.py', 4))),
+ (3, 7, (('<unknown>', 0),)),
])
# filter_traces() must not touch the original snapshot
@@ -335,10 +343,10 @@ class TestSnapshot(unittest.TestCase):
# only include two lines of a.py
snapshot4 = snapshot3.filter_traces((filter2, filter3))
self.assertEqual(snapshot4.traces._traces, [
- (10, (('a.py', 2), ('b.py', 4))),
- (10, (('a.py', 2), ('b.py', 4))),
- (10, (('a.py', 2), ('b.py', 4))),
- (2, (('a.py', 5), ('b.py', 4))),
+ (0, 10, (('a.py', 2), ('b.py', 4))),
+ (0, 10, (('a.py', 2), ('b.py', 4))),
+ (0, 10, (('a.py', 2), ('b.py', 4))),
+ (1, 2, (('a.py', 5), ('b.py', 4))),
])
# No filter: just duplicate the snapshot
@@ -349,6 +357,54 @@ class TestSnapshot(unittest.TestCase):
self.assertRaises(TypeError, snapshot.filter_traces, filter1)
+ def test_filter_traces_domain(self):
+ snapshot, snapshot2 = create_snapshots()
+ filter1 = tracemalloc.Filter(False, "a.py", domain=1)
+ filter2 = tracemalloc.Filter(True, "a.py", domain=1)
+
+ original_traces = list(snapshot.traces._traces)
+
+ # exclude a.py of domain 1
+ snapshot3 = snapshot.filter_traces((filter1,))
+ self.assertEqual(snapshot3.traces._traces, [
+ (0, 10, (('a.py', 2), ('b.py', 4))),
+ (0, 10, (('a.py', 2), ('b.py', 4))),
+ (0, 10, (('a.py', 2), ('b.py', 4))),
+ (2, 66, (('b.py', 1),)),
+ (3, 7, (('<unknown>', 0),)),
+ ])
+
+ # include domain 1
+ snapshot3 = snapshot.filter_traces((filter1,))
+ self.assertEqual(snapshot3.traces._traces, [
+ (0, 10, (('a.py', 2), ('b.py', 4))),
+ (0, 10, (('a.py', 2), ('b.py', 4))),
+ (0, 10, (('a.py', 2), ('b.py', 4))),
+ (2, 66, (('b.py', 1),)),
+ (3, 7, (('<unknown>', 0),)),
+ ])
+
+ def test_filter_traces_domain_filter(self):
+ snapshot, snapshot2 = create_snapshots()
+ filter1 = tracemalloc.DomainFilter(False, domain=3)
+ filter2 = tracemalloc.DomainFilter(True, domain=3)
+
+ # exclude domain 2
+ snapshot3 = snapshot.filter_traces((filter1,))
+ self.assertEqual(snapshot3.traces._traces, [
+ (0, 10, (('a.py', 2), ('b.py', 4))),
+ (0, 10, (('a.py', 2), ('b.py', 4))),
+ (0, 10, (('a.py', 2), ('b.py', 4))),
+ (1, 2, (('a.py', 5), ('b.py', 4))),
+ (2, 66, (('b.py', 1),)),
+ ])
+
+ # include domain 2
+ snapshot3 = snapshot.filter_traces((filter2,))
+ self.assertEqual(snapshot3.traces._traces, [
+ (3, 7, (('<unknown>', 0),)),
+ ])
+
def test_snapshot_group_by_line(self):
snapshot, snapshot2 = create_snapshots()
tb_0 = traceback_lineno('<unknown>', 0)
@@ -816,12 +872,121 @@ class TestCommandLine(unittest.TestCase):
assert_python_ok('-X', 'tracemalloc', '-c', code)
+@unittest.skipIf(_testcapi is None, 'need _testcapi')
+class TestCAPI(unittest.TestCase):
+ maxDiff = 80 * 20
+
+ def setUp(self):
+ if tracemalloc.is_tracing():
+ self.skipTest("tracemalloc must be stopped before the test")
+
+ self.domain = 5
+ self.size = 123
+ self.obj = allocate_bytes(self.size)[0]
+
+ # for the type "object", id(obj) is the address of its memory block.
+ # This type is not tracked by the garbage collector
+ self.ptr = id(self.obj)
+
+ def tearDown(self):
+ tracemalloc.stop()
+
+ def get_traceback(self):
+ frames = _testcapi.tracemalloc_get_traceback(self.domain, self.ptr)
+ if frames is not None:
+ return tracemalloc.Traceback(frames)
+ else:
+ return None
+
+ def track(self, release_gil=False, nframe=1):
+ frames = get_frames(nframe, 2)
+ _testcapi.tracemalloc_track(self.domain, self.ptr, self.size,
+ release_gil)
+ return frames
+
+ def untrack(self):
+ _testcapi.tracemalloc_untrack(self.domain, self.ptr)
+
+ def get_traced_memory(self):
+ # Get the traced size in the domain
+ snapshot = tracemalloc.take_snapshot()
+ domain_filter = tracemalloc.DomainFilter(True, self.domain)
+ snapshot = snapshot.filter_traces([domain_filter])
+ return sum(trace.size for trace in snapshot.traces)
+
+ def check_track(self, release_gil):
+ nframe = 5
+ tracemalloc.start(nframe)
+
+ size = tracemalloc.get_traced_memory()[0]
+
+ frames = self.track(release_gil, nframe)
+ self.assertEqual(self.get_traceback(),
+ tracemalloc.Traceback(frames))
+
+ self.assertEqual(self.get_traced_memory(), self.size)
+
+ def test_track(self):
+ self.check_track(False)
+
+ def test_track_without_gil(self):
+ # check that calling _PyTraceMalloc_Track() without holding the GIL
+ # works too
+ self.check_track(True)
+
+ def test_track_already_tracked(self):
+ nframe = 5
+ tracemalloc.start(nframe)
+
+ # track a first time
+ self.track()
+
+ # calling _PyTraceMalloc_Track() must remove the old trace and add
+ # a new trace with the new traceback
+ frames = self.track(nframe=nframe)
+ self.assertEqual(self.get_traceback(),
+ tracemalloc.Traceback(frames))
+
+ def test_untrack(self):
+ tracemalloc.start()
+
+ self.track()
+ self.assertIsNotNone(self.get_traceback())
+ self.assertEqual(self.get_traced_memory(), self.size)
+
+ # untrack must remove the trace
+ self.untrack()
+ self.assertIsNone(self.get_traceback())
+ self.assertEqual(self.get_traced_memory(), 0)
+
+ # calling _PyTraceMalloc_Untrack() multiple times must not crash
+ self.untrack()
+ self.untrack()
+
+ def test_stop_track(self):
+ tracemalloc.start()
+ tracemalloc.stop()
+
+ with self.assertRaises(RuntimeError):
+ self.track()
+ self.assertIsNone(self.get_traceback())
+
+ def test_stop_untrack(self):
+ tracemalloc.start()
+ self.track()
+
+ tracemalloc.stop()
+ with self.assertRaises(RuntimeError):
+ self.untrack()
+
+
def test_main():
support.run_unittest(
TestTracemallocEnabled,
TestSnapshot,
TestFilters,
TestCommandLine,
+ TestCAPI,
)
if __name__ == "__main__":
diff --git a/Lib/test/test_ttk_guionly.py b/Lib/test/test_ttk_guionly.py
index 490e723..462665d 100644
--- a/Lib/test/test_ttk_guionly.py
+++ b/Lib/test/test_ttk_guionly.py
@@ -1,4 +1,3 @@
-import os
import unittest
from test import support
diff --git a/Lib/test/test_ttk_textonly.py b/Lib/test/test_ttk_textonly.py
index 566fc9d..7540a431 100644
--- a/Lib/test/test_ttk_textonly.py
+++ b/Lib/test/test_ttk_textonly.py
@@ -1,4 +1,3 @@
-import os
from test import support
# Skip this test if _tkinter does not exist.
diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py
index 5e74115..382ca03 100644
--- a/Lib/test/test_types.py
+++ b/Lib/test/test_types.py
@@ -48,6 +48,7 @@ class TypesTests(unittest.TestCase):
def test_float_constructor(self):
self.assertRaises(ValueError, float, '')
self.assertRaises(ValueError, float, '5\0')
+ self.assertRaises(ValueError, float, '5_5\0')
def test_zero_division(self):
try: 5.0 / 0.0
@@ -1000,6 +1001,24 @@ class ClassCreationTests(unittest.TestCase):
with self.assertRaises(TypeError):
X = types.new_class("X", (int(), C))
+ def test_one_argument_type(self):
+ expected_message = 'type.__new__() takes exactly 3 arguments (1 given)'
+
+ # Only type itself can use the one-argument form (#27157)
+ self.assertIs(type(5), int)
+
+ class M(type):
+ pass
+ with self.assertRaises(TypeError) as cm:
+ M(5)
+ self.assertEqual(str(cm.exception), expected_message)
+
+ class N(type, metaclass=M):
+ pass
+ with self.assertRaises(TypeError) as cm:
+ N(5)
+ self.assertEqual(str(cm.exception), expected_message)
+
class SimpleNamespaceTests(unittest.TestCase):
diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py
index 3136ea1..86ebd45 100644
--- a/Lib/test/test_unicode.py
+++ b/Lib/test/test_unicode.py
@@ -10,6 +10,7 @@ import codecs
import itertools
import operator
import struct
+import string
import sys
import unittest
import warnings
@@ -993,6 +994,19 @@ class UnicodeTest(string_tests.CommonTest,
def __format__(self, format_spec):
return int.__format__(self * 2, format_spec)
+ class M:
+ def __init__(self, x):
+ self.x = x
+ def __repr__(self):
+ return 'M(' + self.x + ')'
+ __str__ = None
+
+ class N:
+ def __init__(self, x):
+ self.x = x
+ def __repr__(self):
+ return 'N(' + self.x + ')'
+ __format__ = None
self.assertEqual(''.format(), '')
self.assertEqual('abc'.format(), 'abc')
@@ -1207,6 +1221,16 @@ class UnicodeTest(string_tests.CommonTest,
self.assertEqual("0x{:0{:d}X}".format(0x0,16), "0x0000000000000000")
+ # Blocking fallback
+ m = M('data')
+ self.assertEqual("{!r}".format(m), 'M(data)')
+ self.assertRaises(TypeError, "{!s}".format, m)
+ self.assertRaises(TypeError, "{}".format, m)
+ n = N('data')
+ self.assertEqual("{!r}".format(n), 'N(data)')
+ self.assertEqual("{!s}".format(n), 'N(data)')
+ self.assertRaises(TypeError, "{}".format, n)
+
def test_format_map(self):
self.assertEqual(''.format_map({}), '')
self.assertEqual('a'.format_map({}), 'a')
@@ -1548,7 +1572,7 @@ class UnicodeTest(string_tests.CommonTest,
('+', b'+-'),
('+-', b'+--'),
('+?', b'+-?'),
- ('\?', b'+AFw?'),
+ (r'\?', b'+AFw?'),
('+?', b'+-?'),
(r'\\?', b'+AFwAXA?'),
(r'\\\?', b'+AFwAXABc?'),
@@ -2427,7 +2451,7 @@ class CAPITest(unittest.TestCase):
# non-ascii format, ascii argument: ensure that PyUnicode_FromFormatV()
# raises an error
self.assertRaisesRegex(ValueError,
- '^PyUnicode_FromFormatV\(\) expects an ASCII-encoded format '
+ r'^PyUnicode_FromFormatV\(\) expects an ASCII-encoded format '
'string, got a non-ASCII byte: 0xe9$',
PyUnicode_FromFormat, b'unicode\xe9=%s', 'ascii')
@@ -2799,7 +2823,6 @@ class CAPITest(unittest.TestCase):
# Check that the second call returns the same result
self.assertEqual(getargs_s_hash(s), chr(k).encode() * (i + 1))
-
class StringModuleTest(unittest.TestCase):
def test_formatter_parser(self):
def parse(format):
diff --git a/Lib/test/test_pep277.py b/Lib/test/test_unicode_file_functions.py
index 98c716b..98c716b 100644
--- a/Lib/test/test_pep277.py
+++ b/Lib/test/test_unicode_file_functions.py
diff --git a/Lib/test/test_pep3131.py b/Lib/test/test_unicode_identifiers.py
index 0679845..0679845 100644
--- a/Lib/test/test_pep3131.py
+++ b/Lib/test/test_unicode_identifiers.py
diff --git a/Lib/test/test_unicodedata.py b/Lib/test/test_unicodedata.py
index 6ecc913..4fc11ec 100644
--- a/Lib/test/test_unicodedata.py
+++ b/Lib/test/test_unicodedata.py
@@ -20,7 +20,7 @@ errors = 'surrogatepass'
class UnicodeMethodsTest(unittest.TestCase):
# update this, if the database changes
- expectedchecksum = '5971760872b2f98bb9c701e6c0db3273d756b3ec'
+ expectedchecksum = 'c1fa98674a683aa8a8d8dee0c84494f8d36346e6'
def test_method_checksum(self):
h = hashlib.sha1()
@@ -80,7 +80,7 @@ class UnicodeFunctionsTest(UnicodeDatabaseTest):
# Update this if the database changes. Make sure to do a full rebuild
# (e.g. 'make distclean && make') to get the correct checksum.
- expectedchecksum = '5e74827cd07f9e546a30f34b7bcf6cc2eac38c8c'
+ expectedchecksum = 'f891b1e6430c712531b9bc935a38e22d78ba1bf3'
def test_function_checksum(self):
data = []
h = hashlib.sha1()
@@ -222,6 +222,10 @@ class UnicodeFunctionsTest(UnicodeDatabaseTest):
self.assertEqual(eaw('\u2010'), 'A')
self.assertEqual(eaw('\U00020000'), 'W')
+ def test_east_asian_width_9_0_changes(self):
+ self.assertEqual(self.db.ucd_3_2_0.east_asian_width('\u231a'), 'N')
+ self.assertEqual(self.db.east_asian_width('\u231a'), 'W')
+
class UnicodeMiscTest(UnicodeDatabaseTest):
def test_failed_import_during_compiling(self):
diff --git a/Lib/test/test_unpack.py b/Lib/test/test_unpack.py
index d1ccb38..3fcb18f 100644
--- a/Lib/test/test_unpack.py
+++ b/Lib/test/test_unpack.py
@@ -117,6 +117,27 @@ error)
...
test.test_unpack.BozoError
+Allow unpacking empty iterables
+
+ >>> () = []
+ >>> [] = ()
+ >>> [] = []
+ >>> () = ()
+
+Unpacking non-iterables should raise TypeError
+
+ >>> () = 42
+ Traceback (most recent call last):
+ ...
+ TypeError: 'int' object is not iterable
+
+Unpacking to an empty iterable should raise ValueError
+
+ >>> () = [42]
+ Traceback (most recent call last):
+ ...
+ ValueError: too many values to unpack (expected 0)
+
"""
__test__ = {'doctests' : doctests}
diff --git a/Lib/test/test_unpack_ex.py b/Lib/test/test_unpack_ex.py
index 74346b4..6be8f55 100644
--- a/Lib/test/test_unpack_ex.py
+++ b/Lib/test/test_unpack_ex.py
@@ -357,7 +357,6 @@ Some size constraints (all fail.)
__test__ = {'doctests' : doctests}
def test_main(verbose=False):
- import sys
from test import support
from test import test_unpack_ex
support.run_doctest(test_unpack_ex, verbose)
diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py
index 1772399..5084486 100644
--- a/Lib/test/test_urllib.py
+++ b/Lib/test/test_urllib.py
@@ -470,10 +470,11 @@ Connection: close
@unittest.skipUnless(ssl, "ssl module required")
def test_cafile_and_context(self):
context = ssl.create_default_context()
- with self.assertRaises(ValueError):
- urllib.request.urlopen(
- "https://localhost", cafile="/nonexistent/path", context=context
- )
+ with support.check_warnings(('', DeprecationWarning)):
+ with self.assertRaises(ValueError):
+ urllib.request.urlopen(
+ "https://localhost", cafile="/nonexistent/path", context=context
+ )
class urlopen_DataTests(unittest.TestCase):
"""Test urlopen() opening a data URL."""
@@ -730,7 +731,7 @@ FF
class QuotingTests(unittest.TestCase):
- """Tests for urllib.quote() and urllib.quote_plus()
+ r"""Tests for urllib.quote() and urllib.quote_plus()
According to RFC 2396 (Uniform Resource Identifiers), to escape a
character you write it as '%' + <2 character US-ASCII hex value>.
@@ -805,7 +806,7 @@ class QuotingTests(unittest.TestCase):
# Make sure all characters that should be quoted are by default sans
# space (separate test for that).
should_quote = [chr(num) for num in range(32)] # For 0x00 - 0x1F
- should_quote.append('<>#%"{}|\^[]`')
+ should_quote.append(r'<>#%"{}|\^[]`')
should_quote.append(chr(127)) # For 0x7F
should_quote = ''.join(should_quote)
for char in should_quote:
diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py
index eda7ccc..34329f8 100644
--- a/Lib/test/test_urllib2.py
+++ b/Lib/test/test_urllib2.py
@@ -7,6 +7,8 @@ import io
import socket
import array
import sys
+import tempfile
+import subprocess
import urllib.request
# The proxy bypass method imported below has logic specific to the OSX
@@ -335,7 +337,8 @@ class MockHTTPClass:
else:
self._tunnel_headers.clear()
- def request(self, method, url, body=None, headers=None):
+ def request(self, method, url, body=None, headers=None, *,
+ encode_chunked=False):
self.method = method
self.selector = url
if headers is not None:
@@ -343,6 +346,7 @@ class MockHTTPClass:
self.req_headers.sort()
if body:
self.data = body
+ self.encode_chunked = encode_chunked
if self.raise_on_endheaders:
raise OSError()
@@ -908,41 +912,110 @@ class HandlerTests(unittest.TestCase):
self.assertEqual(req.unredirected_hdrs["Host"], "baz")
self.assertEqual(req.unredirected_hdrs["Spam"], "foo")
- # Check iterable body support
- def iterable_body():
- yield b"one"
- yield b"two"
- yield b"three"
+ def test_http_body_file(self):
+ # A regular file - chunked encoding is used unless Content Length is
+ # already set.
- for headers in {}, {"Content-Length": 11}:
- req = Request("http://example.com/", iterable_body(), headers)
- if not headers:
- # Having an iterable body without a Content-Length should
- # raise an exception
- self.assertRaises(ValueError, h.do_request_, req)
- else:
- newreq = h.do_request_(req)
+ h = urllib.request.AbstractHTTPHandler()
+ o = h.parent = MockOpener()
- # A file object.
- # Test only Content-Length attribute of request.
+ file_obj = tempfile.NamedTemporaryFile(mode='w+b', delete=False)
+ file_path = file_obj.name
+ file_obj.close()
+ self.addCleanup(os.unlink, file_path)
+
+ with open(file_path, "rb") as f:
+ req = Request("http://example.com/", f, {})
+ newreq = h.do_request_(req)
+ te = newreq.get_header('Transfer-encoding')
+ self.assertEqual(te, "chunked")
+ self.assertFalse(newreq.has_header('Content-length'))
+ with open(file_path, "rb") as f:
+ req = Request("http://example.com/", f, {"Content-Length": 30})
+ newreq = h.do_request_(req)
+ self.assertEqual(int(newreq.get_header('Content-length')), 30)
+ self.assertFalse(newreq.has_header("Transfer-encoding"))
+
+ def test_http_body_fileobj(self):
+ # A file object - chunked encoding is used
+ # unless Content Length is already set.
+ # (Note that there are some subtle differences to a regular
+ # file, that is why we are testing both cases.)
+
+ h = urllib.request.AbstractHTTPHandler()
+ o = h.parent = MockOpener()
file_obj = io.BytesIO()
- file_obj.write(b"Something\nSomething\nSomething\n")
+ req = Request("http://example.com/", file_obj, {})
+ newreq = h.do_request_(req)
+ self.assertEqual(newreq.get_header('Transfer-encoding'), 'chunked')
+ self.assertFalse(newreq.has_header('Content-length'))
+
+ headers = {"Content-Length": 30}
+ req = Request("http://example.com/", file_obj, headers)
+ newreq = h.do_request_(req)
+ self.assertEqual(int(newreq.get_header('Content-length')), 30)
+ self.assertFalse(newreq.has_header("Transfer-encoding"))
+
+ file_obj.close()
+
+ def test_http_body_pipe(self):
+ # A file reading from a pipe.
+ # A pipe cannot be seek'ed. There is no way to determine the
+ # content length up front. Thus, do_request_() should fall
+ # back to Transfer-encoding chunked.
+
+ h = urllib.request.AbstractHTTPHandler()
+ o = h.parent = MockOpener()
+
+ cmd = [sys.executable, "-c", r"pass"]
for headers in {}, {"Content-Length": 30}:
- req = Request("http://example.com/", file_obj, headers)
+ with subprocess.Popen(cmd, stdout=subprocess.PIPE) as proc:
+ req = Request("http://example.com/", proc.stdout, headers)
+ newreq = h.do_request_(req)
+ if not headers:
+ self.assertEqual(newreq.get_header('Content-length'), None)
+ self.assertEqual(newreq.get_header('Transfer-encoding'),
+ 'chunked')
+ else:
+ self.assertEqual(int(newreq.get_header('Content-length')),
+ 30)
+
+ def test_http_body_iterable(self):
+ # Generic iterable. There is no way to determine the content
+ # length up front. Fall back to Transfer-encoding chunked.
+
+ h = urllib.request.AbstractHTTPHandler()
+ o = h.parent = MockOpener()
+
+ def iterable_body():
+ yield b"one"
+
+ for headers in {}, {"Content-Length": 11}:
+ req = Request("http://example.com/", iterable_body(), headers)
+ newreq = h.do_request_(req)
if not headers:
- # Having an iterable body without a Content-Length should
- # raise an exception
- self.assertRaises(ValueError, h.do_request_, req)
+ self.assertEqual(newreq.get_header('Content-length'), None)
+ self.assertEqual(newreq.get_header('Transfer-encoding'),
+ 'chunked')
else:
- newreq = h.do_request_(req)
- self.assertEqual(int(newreq.get_header('Content-length')), 30)
+ self.assertEqual(int(newreq.get_header('Content-length')), 11)
- file_obj.close()
+ def test_http_body_empty_seq(self):
+ # Zero-length iterable body should be treated like any other iterable
+ h = urllib.request.AbstractHTTPHandler()
+ h.parent = MockOpener()
+ req = h.do_request_(Request("http://example.com/", ()))
+ self.assertEqual(req.get_header("Transfer-encoding"), "chunked")
+ self.assertFalse(req.has_header("Content-length"))
+ def test_http_body_array(self):
# array.array Iterable - Content Length is calculated
+ h = urllib.request.AbstractHTTPHandler()
+ o = h.parent = MockOpener()
+
iterable_array = array.array("I",[1,2,3,4])
for headers in {}, {"Content-Length": 16}:
diff --git a/Lib/test/test_urllib2_localnet.py b/Lib/test/test_urllib2_localnet.py
index 68c523a..70c4c01 100644
--- a/Lib/test/test_urllib2_localnet.py
+++ b/Lib/test/test_urllib2_localnet.py
@@ -557,26 +557,28 @@ class TestUrlopen(unittest.TestCase):
def test_https_with_cafile(self):
handler = self.start_https_server(certfile=CERT_localhost)
- # Good cert
- data = self.urlopen("https://localhost:%s/bizarre" % handler.port,
- cafile=CERT_localhost)
- self.assertEqual(data, b"we care a bit")
- # Bad cert
- with self.assertRaises(urllib.error.URLError) as cm:
- self.urlopen("https://localhost:%s/bizarre" % handler.port,
- cafile=CERT_fakehostname)
- # Good cert, but mismatching hostname
- handler = self.start_https_server(certfile=CERT_fakehostname)
- with self.assertRaises(ssl.CertificateError) as cm:
- self.urlopen("https://localhost:%s/bizarre" % handler.port,
- cafile=CERT_fakehostname)
+ with support.check_warnings(('', DeprecationWarning)):
+ # Good cert
+ data = self.urlopen("https://localhost:%s/bizarre" % handler.port,
+ cafile=CERT_localhost)
+ self.assertEqual(data, b"we care a bit")
+ # Bad cert
+ with self.assertRaises(urllib.error.URLError) as cm:
+ self.urlopen("https://localhost:%s/bizarre" % handler.port,
+ cafile=CERT_fakehostname)
+ # Good cert, but mismatching hostname
+ handler = self.start_https_server(certfile=CERT_fakehostname)
+ with self.assertRaises(ssl.CertificateError) as cm:
+ self.urlopen("https://localhost:%s/bizarre" % handler.port,
+ cafile=CERT_fakehostname)
def test_https_with_cadefault(self):
handler = self.start_https_server(certfile=CERT_localhost)
# Self-signed cert should fail verification with system certificate store
- with self.assertRaises(urllib.error.URLError) as cm:
- self.urlopen("https://localhost:%s/bizarre" % handler.port,
- cadefault=True)
+ with support.check_warnings(('', DeprecationWarning)):
+ with self.assertRaises(urllib.error.URLError) as cm:
+ self.urlopen("https://localhost:%s/bizarre" % handler.port,
+ cadefault=True)
def test_https_sni(self):
if ssl is None:
diff --git a/Lib/test/test_urllibnet.py b/Lib/test/test_urllibnet.py
index b811930..949716c 100644
--- a/Lib/test/test_urllibnet.py
+++ b/Lib/test/test_urllibnet.py
@@ -4,7 +4,6 @@ from test import support
import contextlib
import socket
import urllib.request
-import sys
import os
import email.message
import time
diff --git a/Lib/test/test_urlparse.py b/Lib/test/test_urlparse.py
index 71abc14..99c5c03 100644
--- a/Lib/test/test_urlparse.py
+++ b/Lib/test/test_urlparse.py
@@ -607,29 +607,27 @@ class UrlParseTestCase(unittest.TestCase):
self.assertEqual(p.port, 80)
self.assertEqual(p.geturl(), url)
- # Verify an illegal port is returned as None
+ # Verify an illegal port raises ValueError
url = b"HTTP://WWW.PYTHON.ORG:65536/doc/#frag"
p = urllib.parse.urlsplit(url)
- self.assertEqual(p.port, None)
+ with self.assertRaisesRegex(ValueError, "out of range"):
+ p.port
def test_attributes_bad_port(self):
- """Check handling of non-integer ports."""
- p = urllib.parse.urlsplit("http://www.example.net:foo")
- self.assertEqual(p.netloc, "www.example.net:foo")
- self.assertRaises(ValueError, lambda: p.port)
-
- p = urllib.parse.urlparse("http://www.example.net:foo")
- self.assertEqual(p.netloc, "www.example.net:foo")
- self.assertRaises(ValueError, lambda: p.port)
-
- # Once again, repeat ourselves to test bytes
- p = urllib.parse.urlsplit(b"http://www.example.net:foo")
- self.assertEqual(p.netloc, b"www.example.net:foo")
- self.assertRaises(ValueError, lambda: p.port)
-
- p = urllib.parse.urlparse(b"http://www.example.net:foo")
- self.assertEqual(p.netloc, b"www.example.net:foo")
- self.assertRaises(ValueError, lambda: p.port)
+ """Check handling of invalid ports."""
+ for bytes in (False, True):
+ for parse in (urllib.parse.urlsplit, urllib.parse.urlparse):
+ for port in ("foo", "1.5", "-1", "0x10"):
+ with self.subTest(bytes=bytes, parse=parse, port=port):
+ netloc = "www.example.net:" + port
+ url = "http://" + netloc
+ if bytes:
+ netloc = netloc.encode("ascii")
+ url = url.encode("ascii")
+ p = parse(url)
+ self.assertEqual(p.netloc, netloc)
+ with self.assertRaises(ValueError):
+ p.port
def test_attributes_without_netloc(self):
# This example is straight from RFC 3261. It looks like it
diff --git a/Lib/test/test_userdict.py b/Lib/test/test_userdict.py
index 8357f8b..662c7f6 100644
--- a/Lib/test/test_userdict.py
+++ b/Lib/test/test_userdict.py
@@ -1,6 +1,6 @@
# Check every path through every method of UserDict
-from test import support, mapping_tests
+from test import mapping_tests
import unittest
import collections
@@ -30,7 +30,7 @@ class UserDictTest(mapping_tests.TestHashMappingProtocol):
self.assertEqual(collections.UserDict(one=1, two=2), d2)
# item sequence constructor
self.assertEqual(collections.UserDict([('one',1), ('two',2)]), d2)
- with self.assertWarnsRegex(PendingDeprecationWarning, "'dict'"):
+ with self.assertWarnsRegex(DeprecationWarning, "'dict'"):
self.assertEqual(collections.UserDict(dict=[('one',1), ('two',2)]), d2)
# both together
self.assertEqual(collections.UserDict([('one',1), ('two',2)], two=3, three=5), d3)
@@ -149,7 +149,7 @@ class UserDictTest(mapping_tests.TestHashMappingProtocol):
[('dict', 42)])
self.assertEqual(list(collections.UserDict({}, dict=None).items()),
[('dict', None)])
- with self.assertWarnsRegex(PendingDeprecationWarning, "'dict'"):
+ with self.assertWarnsRegex(DeprecationWarning, "'dict'"):
self.assertEqual(list(collections.UserDict(dict={'a': 42}).items()),
[('a', 42)])
self.assertRaises(TypeError, collections.UserDict, 42)
diff --git a/Lib/test/test_userlist.py b/Lib/test/test_userlist.py
index f92e4d3..8de6c14 100644
--- a/Lib/test/test_userlist.py
+++ b/Lib/test/test_userlist.py
@@ -1,7 +1,7 @@
# Check every path through every method of UserList
from collections import UserList
-from test import support, list_tests
+from test import list_tests
import unittest
class UserListTest(list_tests.CommonTest):
diff --git a/Lib/test/test_userstring.py b/Lib/test/test_userstring.py
index 9bc8edd..7152822 100644
--- a/Lib/test/test_userstring.py
+++ b/Lib/test/test_userstring.py
@@ -1,9 +1,8 @@
# UserString is a wrapper around the native builtin string type.
# UserString instances should behave similar to builtin string objects.
-import string
import unittest
-from test import support, string_tests
+from test import string_tests
from collections import UserString
diff --git a/Lib/test/test_pep3120.py b/Lib/test/test_utf8source.py
index 97dced8..97dced8 100644
--- a/Lib/test/test_pep3120.py
+++ b/Lib/test/test_utf8source.py
diff --git a/Lib/test/test_uu.py b/Lib/test/test_uu.py
index 25fffbf..ad2f2c5 100644
--- a/Lib/test/test_uu.py
+++ b/Lib/test/test_uu.py
@@ -8,7 +8,6 @@ from test import support
import sys, os
import uu
-from io import BytesIO
import io
plaintext = b"The smooth-scaled python crept over the sleeping dog\n"
diff --git a/Lib/test/test_venv.py b/Lib/test/test_venv.py
index 3999d1f..0ff978f 100644
--- a/Lib/test/test_venv.py
+++ b/Lib/test/test_venv.py
@@ -15,16 +15,9 @@ import sys
import tempfile
from test.support import (captured_stdout, captured_stderr,
can_symlink, EnvironmentVarGuard, rmtree)
-import textwrap
import unittest
import venv
-# pip currently requires ssl support, so we ensure we handle
-# it being missing (http://bugs.python.org/issue19744)
-try:
- import ssl
-except ImportError:
- ssl = None
try:
import threading
@@ -39,15 +32,9 @@ except ImportError:
skipInVenv = unittest.skipIf(sys.prefix != sys.base_prefix,
'Test not appropriate in a venv')
-# os.path.exists('nul') is False: http://bugs.python.org/issue20541
-if os.devnull.lower() == 'nul':
- failsOnWindows = unittest.expectedFailure
-else:
- def failsOnWindows(f):
- return f
-
class BaseTest(unittest.TestCase):
"""Base class for venv tests."""
+ maxDiff = 80 * 50
def setUp(self):
self.env_dir = os.path.realpath(tempfile.mkdtemp())
@@ -57,7 +44,7 @@ class BaseTest(unittest.TestCase):
self.include = 'Include'
else:
self.bindir = 'bin'
- self.lib = ('lib', 'python%s' % sys.version[:3])
+ self.lib = ('lib', 'python%d.%d' % sys.version_info[:2])
self.include = 'include'
if sys.platform == 'darwin' and '__PYVENV_LAUNCHER__' in os.environ:
executable = os.environ['__PYVENV_LAUNCHER__']
@@ -121,6 +108,17 @@ class BasicTest(BaseTest):
print(' %r' % os.listdir(bd))
self.assertTrue(os.path.exists(fn), 'File %r should exist.' % fn)
+ def test_prompt(self):
+ env_name = os.path.split(self.env_dir)[1]
+
+ builder = venv.EnvBuilder()
+ context = builder.ensure_directories(self.env_dir)
+ self.assertEqual(context.prompt, '(%s) ' % env_name)
+
+ builder = venv.EnvBuilder(prompt='My prompt')
+ context = builder.ensure_directories(self.env_dir)
+ self.assertEqual(context.prompt, '(My prompt) ')
+
@skipInVenv
def test_prefixes(self):
"""
@@ -318,18 +316,21 @@ class EnsurePipTest(BaseTest):
self.run_with_capture(venv.create, self.env_dir, with_pip=False)
self.assert_pip_not_installed()
- @failsOnWindows
- def test_devnull_exists_and_is_empty(self):
+ def test_devnull(self):
# Fix for issue #20053 uses os.devnull to force a config file to
# appear empty. However http://bugs.python.org/issue20541 means
# that doesn't currently work properly on Windows. Once that is
# fixed, the "win_location" part of test_with_pip should be restored
- self.assertTrue(os.path.exists(os.devnull))
with open(os.devnull, "rb") as f:
self.assertEqual(f.read(), b"")
- # Requesting pip fails without SSL (http://bugs.python.org/issue19744)
- @unittest.skipIf(ssl is None, ensurepip._MISSING_SSL_MESSAGE)
+ # Issue #20541: os.path.exists('nul') is False on Windows
+ if os.devnull.lower() == 'nul':
+ self.assertFalse(os.path.exists(os.devnull))
+ else:
+ self.assertTrue(os.path.exists(os.devnull))
+
+
@unittest.skipUnless(threading, 'some dependencies of pip import threading'
' module unconditionally')
# Issue #26610: pip/pep425tags.py requires ctypes
diff --git a/Lib/test/test_warnings/__init__.py b/Lib/test/test_warnings/__init__.py
index 84a6fb5..0cddf4a 100644
--- a/Lib/test/test_warnings/__init__.py
+++ b/Lib/test/test_warnings/__init__.py
@@ -2,7 +2,9 @@ from contextlib import contextmanager
import linecache
import os
from io import StringIO
+import re
import sys
+import textwrap
import unittest
from test import support
from test.support.script_helper import assert_python_ok, assert_python_failure
@@ -720,6 +722,17 @@ class _WarningsTests(BaseTest, unittest.TestCase):
result = stream.getvalue()
self.assertIn(text, result)
+ def test_showwarnmsg_missing(self):
+ # Test that _showwarnmsg() missing is okay.
+ text = 'del _showwarnmsg test'
+ with original_warnings.catch_warnings(module=self.module):
+ self.module.filterwarnings("always", category=UserWarning)
+ del self.module._showwarnmsg
+ with support.captured_output('stderr') as stream:
+ self.module.warn(text)
+ result = stream.getvalue()
+ self.assertIn(text, result)
+
def test_showwarning_not_callable(self):
with original_warnings.catch_warnings(module=self.module):
self.module.filterwarnings("always", category=UserWarning)
@@ -821,12 +834,44 @@ class WarningsDisplayTests(BaseTest):
file_object, expected_file_line)
self.assertEqual(expect, file_object.getvalue())
+
class CWarningsDisplayTests(WarningsDisplayTests, unittest.TestCase):
module = c_warnings
class PyWarningsDisplayTests(WarningsDisplayTests, unittest.TestCase):
module = py_warnings
+ def test_tracemalloc(self):
+ self.addCleanup(support.unlink, support.TESTFN)
+
+ with open(support.TESTFN, 'w') as fp:
+ fp.write(textwrap.dedent("""
+ def func():
+ f = open(__file__)
+ # Emit ResourceWarning
+ f = None
+
+ func()
+ """))
+
+ res = assert_python_ok('-Wd', '-X', 'tracemalloc=2', support.TESTFN)
+
+ stderr = res.err.decode('ascii', 'replace')
+ # normalize newlines
+ stderr = '\n'.join(stderr.splitlines())
+ stderr = re.sub('<.*>', '<...>', stderr)
+ expected = textwrap.dedent('''
+ {fname}:5: ResourceWarning: unclosed file <...>
+ f = None
+ Object allocated at (most recent call first):
+ File "{fname}", lineno 3
+ f = open(__file__)
+ File "{fname}", lineno 7
+ func()
+ ''')
+ expected = expected.format(fname=support.TESTFN).strip()
+ self.assertEqual(stderr, expected)
+
class CatchWarningTests(BaseTest):
@@ -899,6 +944,51 @@ class CatchWarningTests(BaseTest):
self.assertTrue(wmod.filters is not orig_filters)
self.assertTrue(wmod.filters is orig_filters)
+ def test_record_override_showwarning_before(self):
+ # Issue #28835: If warnings.showwarning() was overriden, make sure
+ # that catch_warnings(record=True) overrides it again.
+ text = "This is a warning"
+ wmod = self.module
+ my_log = []
+
+ def my_logger(message, category, filename, lineno, file=None, line=None):
+ nonlocal my_log
+ my_log.append(message)
+
+ # Override warnings.showwarning() before calling catch_warnings()
+ with support.swap_attr(wmod, 'showwarning', my_logger):
+ with wmod.catch_warnings(module=wmod, record=True) as log:
+ self.assertIsNot(wmod.showwarning, my_logger)
+
+ wmod.simplefilter("always")
+ wmod.warn(text)
+
+ self.assertIs(wmod.showwarning, my_logger)
+
+ self.assertEqual(len(log), 1, log)
+ self.assertEqual(log[0].message.args[0], text)
+ self.assertEqual(my_log, [])
+
+ def test_record_override_showwarning_inside(self):
+ # Issue #28835: It is possible to override warnings.showwarning()
+ # in the catch_warnings(record=True) context manager.
+ text = "This is a warning"
+ wmod = self.module
+ my_log = []
+
+ def my_logger(message, category, filename, lineno, file=None, line=None):
+ nonlocal my_log
+ my_log.append(message)
+
+ with wmod.catch_warnings(module=wmod, record=True) as log:
+ wmod.simplefilter("always")
+ wmod.showwarning = my_logger
+ wmod.warn(text)
+
+ self.assertEqual(len(my_log), 1, my_log)
+ self.assertEqual(my_log[0].args[0], text)
+ self.assertEqual(log, [])
+
def test_check_warnings(self):
# Explicit tests for the test.support convenience wrapper
wmod = self.module
diff --git a/Lib/test/test_wave.py b/Lib/test/test_wave.py
index 3eff773..8666f72 100644
--- a/Lib/test/test_wave.py
+++ b/Lib/test/test_wave.py
@@ -1,6 +1,6 @@
-from test.support import TESTFN
import unittest
from test import audiotests
+from test import support
from audioop import byteswap
import sys
import wave
@@ -103,5 +103,11 @@ class WavePCM32Test(WaveTest, unittest.TestCase):
frames = byteswap(frames, 4)
+class MiscTestCase(unittest.TestCase):
+ def test__all__(self):
+ blacklist = {'WAVE_FORMAT_PCM'}
+ support.check__all__(self, wave, blacklist=blacklist)
+
+
if __name__ == '__main__':
unittest.main()
diff --git a/Lib/test/test_weakref.py b/Lib/test/test_weakref.py
index 1aa3540..43cf2c0 100644
--- a/Lib/test/test_weakref.py
+++ b/Lib/test/test_weakref.py
@@ -943,7 +943,7 @@ class SubclassableWeakrefTestCase(TestBase):
self.assertFalse(hasattr(r, "__dict__"))
def test_subclass_refs_with_cycle(self):
- # Bug #3110
+ """Confirm https://bugs.python.org/issue3100 is fixed."""
# An instance of a weakref subclass can have attributes.
# If such a weakref holds the only strong reference to the object,
# deleting the weakref will delete the object. In this case,
@@ -1357,13 +1357,16 @@ class MappingTestCase(TestBase):
o = Object(123456)
with testcontext():
n = len(dict)
- dict.popitem()
+ # Since underlaying dict is ordered, first item is popped
+ dict.pop(next(dict.keys()))
self.assertEqual(len(dict), n - 1)
dict[o] = o
self.assertEqual(len(dict), n)
+ # last item in objects is removed from dict in context shutdown
with testcontext():
self.assertEqual(len(dict), n - 1)
- dict.pop(next(dict.keys()))
+ # Then, (o, o) is popped
+ dict.popitem()
self.assertEqual(len(dict), n - 2)
with testcontext():
self.assertEqual(len(dict), n - 3)
diff --git a/Lib/test/test_weakset.py b/Lib/test/test_weakset.py
index 9ce672b..691b95e 100644
--- a/Lib/test/test_weakset.py
+++ b/Lib/test/test_weakset.py
@@ -1,13 +1,6 @@
import unittest
-from weakref import proxy, ref, WeakSet
-import operator
-import copy
+from weakref import WeakSet
import string
-import os
-from random import randrange, shuffle
-import sys
-import warnings
-import collections
from collections import UserString as ustr
import gc
import contextlib
diff --git a/Lib/test/test_winconsoleio.py b/Lib/test/test_winconsoleio.py
new file mode 100644
index 0000000..b1a2f7a
--- /dev/null
+++ b/Lib/test/test_winconsoleio.py
@@ -0,0 +1,147 @@
+'''Tests for WindowsConsoleIO
+'''
+
+import io
+import unittest
+import sys
+
+if sys.platform != 'win32':
+ raise unittest.SkipTest("test only relevant on win32")
+
+from _testconsole import write_input
+
+ConIO = io._WindowsConsoleIO
+
+class WindowsConsoleIOTests(unittest.TestCase):
+ def test_abc(self):
+ self.assertTrue(issubclass(ConIO, io.RawIOBase))
+ self.assertFalse(issubclass(ConIO, io.BufferedIOBase))
+ self.assertFalse(issubclass(ConIO, io.TextIOBase))
+
+ def test_open_fd(self):
+ try:
+ f = ConIO(0)
+ except ValueError:
+ # cannot open console because it's not a real console
+ pass
+ else:
+ self.assertTrue(f.readable())
+ self.assertFalse(f.writable())
+ self.assertEqual(0, f.fileno())
+ f.close() # multiple close should not crash
+ f.close()
+
+ try:
+ f = ConIO(1, 'w')
+ except ValueError:
+ # cannot open console because it's not a real console
+ pass
+ else:
+ self.assertFalse(f.readable())
+ self.assertTrue(f.writable())
+ self.assertEqual(1, f.fileno())
+ f.close()
+ f.close()
+
+ try:
+ f = ConIO(2, 'w')
+ except ValueError:
+ # cannot open console because it's not a real console
+ pass
+ else:
+ self.assertFalse(f.readable())
+ self.assertTrue(f.writable())
+ self.assertEqual(2, f.fileno())
+ f.close()
+ f.close()
+
+ def test_open_name(self):
+ f = ConIO("CON")
+ self.assertTrue(f.readable())
+ self.assertFalse(f.writable())
+ self.assertIsNotNone(f.fileno())
+ f.close() # multiple close should not crash
+ f.close()
+
+ f = ConIO('CONIN$')
+ self.assertTrue(f.readable())
+ self.assertFalse(f.writable())
+ self.assertIsNotNone(f.fileno())
+ f.close()
+ f.close()
+
+ f = ConIO('CONOUT$', 'w')
+ self.assertFalse(f.readable())
+ self.assertTrue(f.writable())
+ self.assertIsNotNone(f.fileno())
+ f.close()
+ f.close()
+
+ def assertStdinRoundTrip(self, text):
+ stdin = open('CONIN$', 'r')
+ old_stdin = sys.stdin
+ try:
+ sys.stdin = stdin
+ write_input(
+ stdin.buffer.raw,
+ (text + '\r\n').encode('utf-16-le', 'surrogatepass')
+ )
+ actual = input()
+ finally:
+ sys.stdin = old_stdin
+ self.assertEqual(actual, text)
+
+ def test_input(self):
+ # ASCII
+ self.assertStdinRoundTrip('abc123')
+ # Non-ASCII
+ self.assertStdinRoundTrip('ϼўТλФЙ')
+ # Combining characters
+ self.assertStdinRoundTrip('A͏B ﬖ̳AA̝')
+ # Non-BMP
+ self.assertStdinRoundTrip('\U00100000\U0010ffff\U0010fffd')
+
+ def test_partial_reads(self):
+ # Test that reading less than 1 full character works when stdin
+ # contains multibyte UTF-8 sequences
+ source = 'ϼўТλФЙ\r\n'.encode('utf-16-le')
+ expected = 'ϼўТλФЙ\r\n'.encode('utf-8')
+ for read_count in range(1, 16):
+ with open('CONIN$', 'rb', buffering=0) as stdin:
+ write_input(stdin, source)
+
+ actual = b''
+ while not actual.endswith(b'\n'):
+ b = stdin.read(read_count)
+ actual += b
+
+ self.assertEqual(actual, expected, 'stdin.read({})'.format(read_count))
+
+ def test_partial_surrogate_reads(self):
+ # Test that reading less than 1 full character works when stdin
+ # contains surrogate pairs that cannot be decoded to UTF-8 without
+ # reading an extra character.
+ source = '\U00101FFF\U00101001\r\n'.encode('utf-16-le')
+ expected = '\U00101FFF\U00101001\r\n'.encode('utf-8')
+ for read_count in range(1, 16):
+ with open('CONIN$', 'rb', buffering=0) as stdin:
+ write_input(stdin, source)
+
+ actual = b''
+ while not actual.endswith(b'\n'):
+ b = stdin.read(read_count)
+ actual += b
+
+ self.assertEqual(actual, expected, 'stdin.read({})'.format(read_count))
+
+ def test_ctrl_z(self):
+ with open('CONIN$', 'rb', buffering=0) as stdin:
+ source = '\xC4\x1A\r\n'.encode('utf-16-le')
+ expected = '\xC4'.encode('utf-8')
+ write_input(stdin, source)
+ a, b = stdin.read(1), stdin.readall()
+ self.assertEqual(expected[0:1], a)
+ self.assertEqual(expected[1:], b)
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/Lib/test/test_winreg.py b/Lib/test/test_winreg.py
index 60207fb..2be61ae 100644
--- a/Lib/test/test_winreg.py
+++ b/Lib/test/test_winreg.py
@@ -37,6 +37,7 @@ test_reflect_key_name = "SOFTWARE\\Classes\\" + test_key_base
test_data = [
("Int Value", 45, REG_DWORD),
+ ("Qword Value", 0x1122334455667788, REG_QWORD),
("String Val", "A string value", REG_SZ),
("StringExpand", "The path is %path%", REG_EXPAND_SZ),
("Multi-string", ["Lots", "of", "string", "values"], REG_MULTI_SZ),
@@ -56,7 +57,7 @@ class BaseWinregTests(unittest.TestCase):
def delete_tree(self, root, subkey):
try:
- hkey = OpenKey(root, subkey, KEY_ALL_ACCESS)
+ hkey = OpenKey(root, subkey, 0, KEY_ALL_ACCESS)
except OSError:
# subkey does not exist
return
@@ -367,6 +368,18 @@ class LocalWinregTests(BaseWinregTests):
finally:
DeleteKey(HKEY_CURRENT_USER, test_key_name)
+ def test_read_string_containing_null(self):
+ # Test for issue 25778: REG_SZ should not contain null characters
+ try:
+ with CreateKey(HKEY_CURRENT_USER, test_key_name) as ck:
+ self.assertNotEqual(ck.handle, 0)
+ test_val = "A string\x00 with a null"
+ SetValueEx(ck, "test_name", 0, REG_SZ, test_val)
+ ret_val, ret_type = QueryValueEx(ck, "test_name")
+ self.assertEqual(ret_type, REG_SZ)
+ self.assertEqual(ret_val, "A string")
+ finally:
+ DeleteKey(HKEY_CURRENT_USER, test_key_name)
@unittest.skipUnless(REMOTE_NAME, "Skipping remote registry tests")
diff --git a/Lib/test/test_winsound.py b/Lib/test/test_winsound.py
index 4a8ab7d..179e069 100644
--- a/Lib/test/test_winsound.py
+++ b/Lib/test/test_winsound.py
@@ -51,6 +51,10 @@ class BeepTest(unittest.TestCase):
for i in range(100, 2000, 100):
safe_Beep(i, 75)
+ def test_keyword_args(self):
+ safe_Beep(duration=75, frequency=2000)
+
+
class MessageBeepTest(unittest.TestCase):
def tearDown(self):
@@ -76,6 +80,9 @@ class MessageBeepTest(unittest.TestCase):
def test_question(self):
safe_MessageBeep(winsound.MB_ICONQUESTION)
+ def test_keyword_args(self):
+ safe_MessageBeep(type=winsound.MB_OK)
+
class PlaySoundTest(unittest.TestCase):
@@ -87,6 +94,25 @@ class PlaySoundTest(unittest.TestCase):
winsound.PlaySound,
"none", winsound.SND_ASYNC | winsound.SND_MEMORY
)
+ self.assertRaises(TypeError, winsound.PlaySound, b"bad", 0)
+ self.assertRaises(TypeError, winsound.PlaySound, "bad",
+ winsound.SND_MEMORY)
+ self.assertRaises(TypeError, winsound.PlaySound, 1, 0)
+
+ def test_keyword_args(self):
+ safe_PlaySound(flags=winsound.SND_ALIAS, sound="SystemExit")
+
+ def test_snd_memory(self):
+ with open(support.findfile('pluck-pcm8.wav',
+ subdir='audiodata'), 'rb') as f:
+ audio_data = f.read()
+ safe_PlaySound(audio_data, winsound.SND_MEMORY)
+ audio_data = bytearray(audio_data)
+ safe_PlaySound(audio_data, winsound.SND_MEMORY)
+
+ def test_snd_filename(self):
+ fn = support.findfile('pluck-pcm8.wav', subdir='audiodata')
+ safe_PlaySound(fn, winsound.SND_FILENAME | winsound.SND_NODEFAULT)
def test_aliases(self):
aliases = [
diff --git a/Lib/test/test_with.py b/Lib/test/test_with.py
index e8d789b..c70f685 100644
--- a/Lib/test/test_with.py
+++ b/Lib/test/test_with.py
@@ -109,7 +109,7 @@ class FailureTestCase(unittest.TestCase):
with foo: pass
self.assertRaises(NameError, fooNotDeclared)
- def testEnterAttributeError(self):
+ def testEnterAttributeError1(self):
class LacksEnter(object):
def __exit__(self, type, value, traceback):
pass
@@ -117,7 +117,16 @@ class FailureTestCase(unittest.TestCase):
def fooLacksEnter():
foo = LacksEnter()
with foo: pass
- self.assertRaises(AttributeError, fooLacksEnter)
+ self.assertRaisesRegex(AttributeError, '__enter__', fooLacksEnter)
+
+ def testEnterAttributeError2(self):
+ class LacksEnterAndExit(object):
+ pass
+
+ def fooLacksEnterAndExit():
+ foo = LacksEnterAndExit()
+ with foo: pass
+ self.assertRaisesRegex(AttributeError, '__enter__', fooLacksEnterAndExit)
def testExitAttributeError(self):
class LacksExit(object):
@@ -127,7 +136,7 @@ class FailureTestCase(unittest.TestCase):
def fooLacksExit():
foo = LacksExit()
with foo: pass
- self.assertRaises(AttributeError, fooLacksExit)
+ self.assertRaisesRegex(AttributeError, '__exit__', fooLacksExit)
def assertRaisesSyntaxError(self, codestr):
def shouldRaiseSyntaxError(s):
@@ -140,11 +149,6 @@ class FailureTestCase(unittest.TestCase):
'with mock as (None):\n'
' pass')
- def testAssignmentToEmptyTupleError(self):
- self.assertRaisesSyntaxError(
- 'with mock as ():\n'
- ' pass')
-
def testAssignmentToTupleOnlyContainingNoneError(self):
self.assertRaisesSyntaxError('with mock as None,:\n pass')
self.assertRaisesSyntaxError(
@@ -454,7 +458,7 @@ class ExceptionalTestCase(ContextmanagerAssertionMixin, unittest.TestCase):
with cm():
raise StopIteration("from with")
- with self.assertWarnsRegex(PendingDeprecationWarning, "StopIteration"):
+ with self.assertWarnsRegex(DeprecationWarning, "StopIteration"):
self.assertRaises(StopIteration, shouldThrow)
def testRaisedStopIteration2(self):
@@ -482,7 +486,7 @@ class ExceptionalTestCase(ContextmanagerAssertionMixin, unittest.TestCase):
with cm():
raise next(iter([]))
- with self.assertWarnsRegex(PendingDeprecationWarning, "StopIteration"):
+ with self.assertWarnsRegex(DeprecationWarning, "StopIteration"):
self.assertRaises(StopIteration, shouldThrow)
def testRaisedGeneratorExit1(self):
diff --git a/Lib/test/test_wsgiref.py b/Lib/test/test_wsgiref.py
index 61a750c..7708e20 100644
--- a/Lib/test/test_wsgiref.py
+++ b/Lib/test/test_wsgiref.py
@@ -258,7 +258,7 @@ class IntegrationTests(TestCase):
def app(environ, start_response):
start_response("200 OK", [])
- return [bytes(support.SOCK_MAX_SIZE)]
+ return [b'\0' * support.SOCK_MAX_SIZE]
class WsgiHandler(NoLogRequestHandler, WSGIRequestHandler):
pass
diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py
index 6c7616b..c0144d1 100644
--- a/Lib/test/test_xml_etree.py
+++ b/Lib/test/test_xml_etree.py
@@ -91,8 +91,6 @@ ENTITY_XML = """\
class ModuleTest(unittest.TestCase):
- # TODO: this should be removed once we get rid of the global module vars
-
def test_sanity(self):
# Import sanity.
@@ -100,6 +98,10 @@ class ModuleTest(unittest.TestCase):
from xml.etree import ElementInclude
from xml.etree import ElementPath
+ def test_all(self):
+ names = ("xml.etree.ElementTree", "_elementtree")
+ support.check__all__(self, ET, names, blacklist=("HTML_EMPTY",))
+
def serialize(elem, to_string=True, encoding='unicode', **options):
if encoding != 'unicode':
@@ -182,10 +184,12 @@ class ElementTreeTest(unittest.TestCase):
def check_element(element):
self.assertTrue(ET.iselement(element), msg="not an element")
- self.assertTrue(hasattr(element, "tag"), msg="no tag member")
- self.assertTrue(hasattr(element, "attrib"), msg="no attrib member")
- self.assertTrue(hasattr(element, "text"), msg="no text member")
- self.assertTrue(hasattr(element, "tail"), msg="no tail member")
+ direlem = dir(element)
+ for attr in 'tag', 'attrib', 'text', 'tail':
+ self.assertTrue(hasattr(element, attr),
+ msg='no %s member' % attr)
+ self.assertIn(attr, direlem,
+ msg='no %s visible by dir' % attr)
check_string(element.tag)
check_mapping(element.attrib)
diff --git a/Lib/test/test_xml_etree_c.py b/Lib/test/test_xml_etree_c.py
index bfced12..7c60699 100644
--- a/Lib/test/test_xml_etree_c.py
+++ b/Lib/test/test_xml_etree_c.py
@@ -1,5 +1,5 @@
# xml.etree test for cElementTree
-import sys, struct
+import struct
from test import support
from test.support import import_fresh_module
import types
@@ -118,7 +118,7 @@ class SizeofTest(unittest.TestCase):
struct.calcsize('8P'))
def test_main():
- from test import test_xml_etree, test_xml_etree_c
+ from test import test_xml_etree
# Run the tests specific to the C implementation
support.run_unittest(
diff --git a/Lib/test/test_xmlrpc.py b/Lib/test/test_xmlrpc.py
index 02d9f5c..df9c79e 100644
--- a/Lib/test/test_xmlrpc.py
+++ b/Lib/test/test_xmlrpc.py
@@ -1,5 +1,6 @@
import base64
import datetime
+import decimal
import sys
import time
import unittest
@@ -9,7 +10,6 @@ import xmlrpc.server
import http.client
import http, http.server
import socket
-import os
import re
import io
import contextlib
@@ -238,6 +238,54 @@ class XMLRPCTestCase(unittest.TestCase):
'</struct></value></param></params>')
self.assertRaises(ResponseError, xmlrpclib.loads, data)
+ def check_loads(self, s, value, **kwargs):
+ dump = '<params><param><value>%s</value></param></params>' % s
+ result, m = xmlrpclib.loads(dump, **kwargs)
+ (newvalue,) = result
+ self.assertEqual(newvalue, value)
+ self.assertIs(type(newvalue), type(value))
+ self.assertIsNone(m)
+
+ def test_load_standard_types(self):
+ check = self.check_loads
+ check('string', 'string')
+ check('<string>string</string>', 'string')
+ check('<string>𝔘𝔫𝔦𝔠𝔬𝔡𝔢 string</string>', '𝔘𝔫𝔦𝔠𝔬𝔡𝔢 string')
+ check('<int>2056183947</int>', 2056183947)
+ check('<int>-2056183947</int>', -2056183947)
+ check('<i4>2056183947</i4>', 2056183947)
+ check('<double>46093.78125</double>', 46093.78125)
+ check('<boolean>0</boolean>', False)
+ check('<base64>AGJ5dGUgc3RyaW5n/w==</base64>',
+ xmlrpclib.Binary(b'\x00byte string\xff'))
+ check('<base64>AGJ5dGUgc3RyaW5n/w==</base64>',
+ b'\x00byte string\xff', use_builtin_types=True)
+ check('<dateTime.iso8601>20050210T11:41:23</dateTime.iso8601>',
+ xmlrpclib.DateTime('20050210T11:41:23'))
+ check('<dateTime.iso8601>20050210T11:41:23</dateTime.iso8601>',
+ datetime.datetime(2005, 2, 10, 11, 41, 23),
+ use_builtin_types=True)
+ check('<array><data>'
+ '<value><int>1</int></value><value><int>2</int></value>'
+ '</data></array>', [1, 2])
+ check('<struct>'
+ '<member><name>b</name><value><int>2</int></value></member>'
+ '<member><name>a</name><value><int>1</int></value></member>'
+ '</struct>', {'a': 1, 'b': 2})
+
+ def test_load_extension_types(self):
+ check = self.check_loads
+ check('<nil/>', None)
+ check('<ex:nil/>', None)
+ check('<i1>205</i1>', 205)
+ check('<i2>20561</i2>', 20561)
+ check('<i8>9876543210</i8>', 9876543210)
+ check('<biginteger>98765432100123456789</biginteger>',
+ 98765432100123456789)
+ check('<float>93.78125</float>', 93.78125)
+ check('<bigdecimal>9876543210.0123456789</bigdecimal>',
+ decimal.Decimal('9876543210.0123456789'))
+
def test_get_host_info(self):
# see bug #3613, this raised a TypeError
transp = xmlrpc.client.Transport()
@@ -1147,7 +1195,6 @@ def captured_stdout(encoding='utf-8'):
"""A variation on support.captured_stdout() which gives a text stream
having a `buffer` attribute.
"""
- import io
orig_stdout = sys.stdout
sys.stdout = io.TextIOWrapper(io.BytesIO(), encoding=encoding)
try:
@@ -1220,7 +1267,7 @@ class CGIHandlerTestCase(unittest.TestCase):
content = handle[handle.find("<?xml"):]
self.assertEqual(
- int(re.search('Content-Length: (\d+)', handle).group(1)),
+ int(re.search(r'Content-Length: (\d+)', handle).group(1)),
len(content))
diff --git a/Lib/test/test_xmlrpc_net.py b/Lib/test/test_xmlrpc_net.py
index b60b82f..ae0a28e 100644
--- a/Lib/test/test_xmlrpc_net.py
+++ b/Lib/test/test_xmlrpc_net.py
@@ -1,7 +1,4 @@
import collections.abc
-import errno
-import socket
-import sys
import unittest
from test import support
diff --git a/Lib/test/test_pep380.py b/Lib/test/test_yield_from.py
index 23ffbed..7e9711e 100644
--- a/Lib/test/test_pep380.py
+++ b/Lib/test/test_yield_from.py
@@ -384,9 +384,10 @@ class TestPEP380Operation(unittest.TestCase):
trace.append("Starting g1")
yield "g1 ham"
ret = yield from g2()
- trace.append("g2 returned %s" % (ret,))
- ret = yield from g2(42)
- trace.append("g2 returned %s" % (ret,))
+ trace.append("g2 returned %r" % (ret,))
+ for v in 1, (2,), StopIteration(3):
+ ret = yield from g2(v)
+ trace.append("g2 returned %r" % (ret,))
yield "g1 eggs"
trace.append("Finishing g1")
def g2(v = None):
@@ -410,7 +411,17 @@ class TestPEP380Operation(unittest.TestCase):
"Yielded g2 spam",
"Yielded g2 more spam",
"Finishing g2",
- "g2 returned 42",
+ "g2 returned 1",
+ "Starting g2",
+ "Yielded g2 spam",
+ "Yielded g2 more spam",
+ "Finishing g2",
+ "g2 returned (2,)",
+ "Starting g2",
+ "Yielded g2 spam",
+ "Yielded g2 more spam",
+ "Finishing g2",
+ "g2 returned StopIteration(3,)",
"Yielded g1 eggs",
"Finishing g1",
])
@@ -670,14 +681,16 @@ class TestPEP380Operation(unittest.TestCase):
next(gi)
trace.append("f SHOULD NOT BE HERE")
except StopIteration as e:
- trace.append("f caught %s" % (repr(e),))
+ trace.append("f caught %r" % (e,))
def g(r):
trace.append("g starting")
yield
- trace.append("g returning %s" % (r,))
+ trace.append("g returning %r" % (r,))
return r
f(None)
- f(42)
+ f(1)
+ f((2,))
+ f(StopIteration(3))
self.assertEqual(trace,[
"g starting",
"f resuming g",
@@ -685,8 +698,16 @@ class TestPEP380Operation(unittest.TestCase):
"f caught StopIteration()",
"g starting",
"f resuming g",
- "g returning 42",
- "f caught StopIteration(42,)",
+ "g returning 1",
+ "f caught StopIteration(1,)",
+ "g starting",
+ "f resuming g",
+ "g returning (2,)",
+ "f caught StopIteration((2,),)",
+ "g starting",
+ "f resuming g",
+ "g returning StopIteration(3,)",
+ "f caught StopIteration(StopIteration(3,),)",
])
def test_send_and_return_with_value(self):
@@ -706,22 +727,34 @@ class TestPEP380Operation(unittest.TestCase):
def g(r):
trace.append("g starting")
x = yield
- trace.append("g received %s" % (x,))
- trace.append("g returning %s" % (r,))
+ trace.append("g received %r" % (x,))
+ trace.append("g returning %r" % (r,))
return r
f(None)
- f(42)
- self.assertEqual(trace,[
+ f(1)
+ f((2,))
+ f(StopIteration(3))
+ self.assertEqual(trace, [
"g starting",
"f sending spam to g",
- "g received spam",
+ "g received 'spam'",
"g returning None",
"f caught StopIteration()",
"g starting",
"f sending spam to g",
- "g received spam",
- "g returning 42",
- "f caught StopIteration(42,)",
+ "g received 'spam'",
+ "g returning 1",
+ 'f caught StopIteration(1,)',
+ 'g starting',
+ 'f sending spam to g',
+ "g received 'spam'",
+ 'g returning (2,)',
+ 'f caught StopIteration((2,),)',
+ 'g starting',
+ 'f sending spam to g',
+ "g received 'spam'",
+ 'g returning StopIteration(3,)',
+ 'f caught StopIteration(StopIteration(3,),)'
])
def test_catching_exception_from_subgen_and_returning(self):
@@ -729,27 +762,29 @@ class TestPEP380Operation(unittest.TestCase):
Test catching an exception thrown into a
subgenerator and returning a value
"""
- trace = []
def inner():
try:
yield 1
except ValueError:
trace.append("inner caught ValueError")
- return 2
+ return value
def outer():
v = yield from inner()
- trace.append("inner returned %r to outer" % v)
+ trace.append("inner returned %r to outer" % (v,))
yield v
- g = outer()
- trace.append(next(g))
- trace.append(g.throw(ValueError))
- self.assertEqual(trace,[
- 1,
- "inner caught ValueError",
- "inner returned 2 to outer",
- 2,
- ])
+
+ for value in 2, (2,), StopIteration(2):
+ trace = []
+ g = outer()
+ trace.append(next(g))
+ trace.append(repr(g.throw(ValueError)))
+ self.assertEqual(trace, [
+ 1,
+ "inner caught ValueError",
+ "inner returned %r to outer" % (value,),
+ repr(value),
+ ])
def test_throwing_GeneratorExit_into_subgen_that_returns(self):
"""
diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py
index 0dd4bca..0a43b20 100644
--- a/Lib/test/test_zipfile.py
+++ b/Lib/test/test_zipfile.py
@@ -1,8 +1,8 @@
import contextlib
import io
import os
-import sys
import importlib.util
+import posixpath
import time
import struct
import zipfile
@@ -39,10 +39,6 @@ def get_files(test):
yield f
test.assertFalse(f.closed)
-def openU(zipfp, fn):
- with check_warnings(('', DeprecationWarning)):
- return zipfp.open(fn, 'rU')
-
class AbstractTestsWithSourceFile:
@classmethod
def setUpClass(cls):
@@ -62,6 +58,9 @@ class AbstractTestsWithSourceFile:
zipfp.write(TESTFN, "another.name")
zipfp.write(TESTFN, TESTFN)
zipfp.writestr("strfile", self.data)
+ with zipfp.open('written-open-w', mode='w') as f:
+ for line in self.line_gen:
+ f.write(line)
def zip_test(self, f, compression):
self.make_test_archive(f, compression)
@@ -77,7 +76,7 @@ class AbstractTestsWithSourceFile:
zipfp.printdir(file=fp)
directory = fp.getvalue()
lines = directory.splitlines()
- self.assertEqual(len(lines), 4) # Number of files + header
+ self.assertEqual(len(lines), 5) # Number of files + header
self.assertIn('File Name', lines[0])
self.assertIn('Modified', lines[0])
@@ -91,23 +90,25 @@ class AbstractTestsWithSourceFile:
# Check the namelist
names = zipfp.namelist()
- self.assertEqual(len(names), 3)
+ self.assertEqual(len(names), 4)
self.assertIn(TESTFN, names)
self.assertIn("another.name", names)
self.assertIn("strfile", names)
+ self.assertIn("written-open-w", names)
# Check infolist
infos = zipfp.infolist()
names = [i.filename for i in infos]
- self.assertEqual(len(names), 3)
+ self.assertEqual(len(names), 4)
self.assertIn(TESTFN, names)
self.assertIn("another.name", names)
self.assertIn("strfile", names)
+ self.assertIn("written-open-w", names)
for i in infos:
self.assertEqual(i.file_size, len(self.data))
# check getinfo
- for nm in (TESTFN, "another.name", "strfile"):
+ for nm in (TESTFN, "another.name", "strfile", "written-open-w"):
info = zipfp.getinfo(nm)
self.assertEqual(info.filename, nm)
self.assertEqual(info.file_size, len(self.data))
@@ -373,14 +374,18 @@ class StoredTestsWithSourceFile(AbstractTestsWithSourceFile,
test_low_compression = None
def zip_test_writestr_permissions(self, f, compression):
- # Make sure that writestr creates files with mode 0600,
- # when it is passed a name rather than a ZipInfo instance.
+ # Make sure that writestr and open(... mode='w') create files with
+ # mode 0600, when they are passed a name rather than a ZipInfo
+ # instance.
self.make_test_archive(f, compression)
with zipfile.ZipFile(f, "r") as zipfp:
zinfo = zipfp.getinfo('strfile')
self.assertEqual(zinfo.external_attr, 0o600 << 16)
+ zinfo2 = zipfp.getinfo('written-open-w')
+ self.assertEqual(zinfo2.external_attr, 0o600 << 16)
+
def test_writestr_permissions(self):
for f in get_files(self):
self.zip_test_writestr_permissions(f, zipfile.ZIP_STORED)
@@ -488,12 +493,16 @@ class StoredTestsWithSourceFile(AbstractTestsWithSourceFile,
def test_write_to_readonly(self):
"""Check that trying to call write() on a readonly ZipFile object
- raises a RuntimeError."""
+ raises a ValueError."""
with zipfile.ZipFile(TESTFN2, mode="w") as zipfp:
zipfp.writestr("somefile.txt", "bogus")
with zipfile.ZipFile(TESTFN2, mode="r") as zipfp:
- self.assertRaises(RuntimeError, zipfp.write, TESTFN)
+ self.assertRaises(ValueError, zipfp.write, TESTFN)
+
+ with zipfile.ZipFile(TESTFN2, mode="r") as zipfp:
+ with self.assertRaises(ValueError):
+ zipfp.open(TESTFN, mode='w')
def test_add_file_before_1980(self):
# Set atime and mtime to 1970-01-01
@@ -1066,32 +1075,6 @@ class OtherTests(unittest.TestCase):
data += zipfp.read(info)
self.assertIn(data, {b"foobar", b"barfoo"})
- def test_universal_deprecation(self):
- f = io.BytesIO()
- with zipfile.ZipFile(f, "w") as zipfp:
- zipfp.writestr('spam.txt', b'ababagalamaga')
-
- with zipfile.ZipFile(f, "r") as zipfp:
- for mode in 'U', 'rU':
- with self.assertWarns(DeprecationWarning):
- zipopen = zipfp.open('spam.txt', mode)
- zipopen.close()
-
- def test_universal_readaheads(self):
- f = io.BytesIO()
-
- data = b'a\r\n' * 16 * 1024
- with zipfile.ZipFile(f, 'w', zipfile.ZIP_STORED) as zipfp:
- zipfp.writestr(TESTFN, data)
-
- data2 = b''
- with zipfile.ZipFile(f, 'r') as zipfp, \
- openU(zipfp, TESTFN) as zipopen:
- for line in zipopen:
- data2 += line
-
- self.assertEqual(data, data2.replace(b'\n', b'\r\n'))
-
def test_writestr_extended_local_header_issue1202(self):
with zipfile.ZipFile(TESTFN2, 'w') as orig_zip:
for data in 'abcdefghijklmnop':
@@ -1271,27 +1254,27 @@ class OtherTests(unittest.TestCase):
fp.write("short file")
self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN)
- def test_closed_zip_raises_RuntimeError(self):
+ def test_closed_zip_raises_ValueError(self):
"""Verify that testzip() doesn't swallow inappropriate exceptions."""
data = io.BytesIO()
with zipfile.ZipFile(data, mode="w") as zipf:
zipf.writestr("foo.txt", "O, for a Muse of Fire!")
# This is correct; calling .read on a closed ZipFile should raise
- # a RuntimeError, and so should calling .testzip. An earlier
+ # a ValueError, and so should calling .testzip. An earlier
# version of .testzip would swallow this exception (and any other)
# and report that the first file in the archive was corrupt.
- self.assertRaises(RuntimeError, zipf.read, "foo.txt")
- self.assertRaises(RuntimeError, zipf.open, "foo.txt")
- self.assertRaises(RuntimeError, zipf.testzip)
- self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus")
+ self.assertRaises(ValueError, zipf.read, "foo.txt")
+ self.assertRaises(ValueError, zipf.open, "foo.txt")
+ self.assertRaises(ValueError, zipf.testzip)
+ self.assertRaises(ValueError, zipf.writestr, "bogus.txt", "bogus")
with open(TESTFN, 'w') as f:
f.write('zipfile test data')
- self.assertRaises(RuntimeError, zipf.write, TESTFN)
+ self.assertRaises(ValueError, zipf.write, TESTFN)
def test_bad_constructor_mode(self):
"""Check that bad modes passed to ZipFile constructor are caught."""
- self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "q")
+ self.assertRaises(ValueError, zipfile.ZipFile, TESTFN, "q")
def test_bad_open_mode(self):
"""Check that bad modes passed to ZipFile.open are caught."""
@@ -1299,9 +1282,12 @@ class OtherTests(unittest.TestCase):
zipf.writestr("foo.txt", "O, for a Muse of Fire!")
with zipfile.ZipFile(TESTFN, mode="r") as zipf:
- # read the data to make sure the file is there
+ # read the data to make sure the file is there
zipf.read("foo.txt")
- self.assertRaises(RuntimeError, zipf.open, "foo.txt", "q")
+ self.assertRaises(ValueError, zipf.open, "foo.txt", "q")
+ # universal newlines support is removed
+ self.assertRaises(ValueError, zipf.open, "foo.txt", "U")
+ self.assertRaises(ValueError, zipf.open, "foo.txt", "rU")
def test_read0(self):
"""Check that calling read(0) on a ZipExtFile object returns an empty
@@ -1324,7 +1310,7 @@ class OtherTests(unittest.TestCase):
def test_bad_compression_mode(self):
"""Check that bad compression methods passed to ZipFile.open are
caught."""
- self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "w", -1)
+ self.assertRaises(NotImplementedError, zipfile.ZipFile, TESTFN, "w", -1)
def test_unsupported_compression(self):
# data is declared as shrunk, but actually deflated
@@ -1472,6 +1458,35 @@ class OtherTests(unittest.TestCase):
# testzip returns the name of the first corrupt file, or None
self.assertIsNone(zipf.testzip())
+ def test_open_conflicting_handles(self):
+ # It's only possible to open one writable file handle at a time
+ msg1 = b"It's fun to charter an accountant!"
+ msg2 = b"And sail the wide accountant sea"
+ msg3 = b"To find, explore the funds offshore"
+ with zipfile.ZipFile(TESTFN2, 'w', zipfile.ZIP_STORED) as zipf:
+ with zipf.open('foo', mode='w') as w2:
+ w2.write(msg1)
+ with zipf.open('bar', mode='w') as w1:
+ with self.assertRaises(ValueError):
+ zipf.open('handle', mode='w')
+ with self.assertRaises(ValueError):
+ zipf.open('foo', mode='r')
+ with self.assertRaises(ValueError):
+ zipf.writestr('str', 'abcde')
+ with self.assertRaises(ValueError):
+ zipf.write(__file__, 'file')
+ with self.assertRaises(ValueError):
+ zipf.close()
+ w1.write(msg2)
+ with zipf.open('baz', mode='w') as w2:
+ w2.write(msg3)
+
+ with zipfile.ZipFile(TESTFN2, 'r') as zipf:
+ self.assertEqual(zipf.read('foo'), msg1)
+ self.assertEqual(zipf.read('bar'), msg2)
+ self.assertEqual(zipf.read('baz'), msg3)
+ self.assertEqual(zipf.namelist(), ['foo', 'bar', 'baz'])
+
def tearDown(self):
unlink(TESTFN)
unlink(TESTFN2)
@@ -1805,6 +1820,22 @@ class UnseekableTests(unittest.TestCase):
with zipf.open('twos') as zopen:
self.assertEqual(zopen.read(), b'222')
+ def test_open_write(self):
+ for wrapper in (lambda f: f), Tellable, Unseekable:
+ with self.subTest(wrapper=wrapper):
+ f = io.BytesIO()
+ f.write(b'abc')
+ bf = io.BufferedWriter(f)
+ with zipfile.ZipFile(wrapper(bf), 'w', zipfile.ZIP_STORED) as zipf:
+ with zipf.open('ones', 'w') as zopen:
+ zopen.write(b'111')
+ with zipf.open('twos', 'w') as zopen:
+ zopen.write(b'222')
+ self.assertEqual(f.getvalue()[:5], b'abcPK')
+ with zipfile.ZipFile(f) as zipf:
+ self.assertEqual(zipf.read('ones'), b'111')
+ self.assertEqual(zipf.read('twos'), b'222')
+
@requires_zlib
class TestsWithMultipleOpens(unittest.TestCase):
@@ -1915,6 +1946,19 @@ class TestsWithMultipleOpens(unittest.TestCase):
with open(os.devnull) as f:
self.assertLess(f.fileno(), 100)
+ def test_write_while_reading(self):
+ with zipfile.ZipFile(TESTFN2, 'w', zipfile.ZIP_DEFLATED) as zipf:
+ zipf.writestr('ones', self.data1)
+ with zipfile.ZipFile(TESTFN2, 'a', zipfile.ZIP_DEFLATED) as zipf:
+ with zipf.open('ones', 'r') as r1:
+ data1 = r1.read(500)
+ with zipf.open('twos', 'w') as w1:
+ w1.write(self.data2)
+ data1 += r1.read()
+ self.assertEqual(data1, self.data1)
+ with zipfile.ZipFile(TESTFN2) as zipf:
+ self.assertEqual(zipf.read('twos'), self.data2)
+
def tearDown(self):
unlink(TESTFN2)
@@ -1984,137 +2028,19 @@ class TestWithDirectory(unittest.TestCase):
unlink(TESTFN)
-class AbstractUniversalNewlineTests:
- @classmethod
- def setUpClass(cls):
- cls.line_gen = [bytes("Test of zipfile line %d." % i, "ascii")
- for i in range(FIXEDTEST_SIZE)]
- cls.seps = (b'\r', b'\r\n', b'\n')
- cls.arcdata = {}
- for n, s in enumerate(cls.seps):
- cls.arcdata[s] = s.join(cls.line_gen) + s
-
- def setUp(self):
- self.arcfiles = {}
- for n, s in enumerate(self.seps):
- self.arcfiles[s] = '%s-%d' % (TESTFN, n)
- with open(self.arcfiles[s], "wb") as f:
- f.write(self.arcdata[s])
-
- def make_test_archive(self, f, compression):
- # Create the ZIP archive
- with zipfile.ZipFile(f, "w", compression) as zipfp:
- for fn in self.arcfiles.values():
- zipfp.write(fn, fn)
-
- def read_test(self, f, compression):
- self.make_test_archive(f, compression)
-
- # Read the ZIP archive
- with zipfile.ZipFile(f, "r") as zipfp:
- for sep, fn in self.arcfiles.items():
- with openU(zipfp, fn) as fp:
- zipdata = fp.read()
- self.assertEqual(self.arcdata[sep], zipdata)
-
- def test_read(self):
- for f in get_files(self):
- self.read_test(f, self.compression)
-
- def readline_read_test(self, f, compression):
- self.make_test_archive(f, compression)
+class ZipInfoTests(unittest.TestCase):
+ def test_from_file(self):
+ zi = zipfile.ZipInfo.from_file(__file__)
+ self.assertEqual(posixpath.basename(zi.filename), 'test_zipfile.py')
+ self.assertFalse(zi.is_dir())
- # Read the ZIP archive
- with zipfile.ZipFile(f, "r") as zipfp:
- for sep, fn in self.arcfiles.items():
- with openU(zipfp, fn) as zipopen:
- data = b''
- while True:
- read = zipopen.readline()
- if not read:
- break
- data += read
-
- read = zipopen.read(5)
- if not read:
- break
- data += read
-
- self.assertEqual(data, self.arcdata[b'\n'])
-
- def test_readline_read(self):
- for f in get_files(self):
- self.readline_read_test(f, self.compression)
-
- def readline_test(self, f, compression):
- self.make_test_archive(f, compression)
-
- # Read the ZIP archive
- with zipfile.ZipFile(f, "r") as zipfp:
- for sep, fn in self.arcfiles.items():
- with openU(zipfp, fn) as zipopen:
- for line in self.line_gen:
- linedata = zipopen.readline()
- self.assertEqual(linedata, line + b'\n')
-
- def test_readline(self):
- for f in get_files(self):
- self.readline_test(f, self.compression)
-
- def readlines_test(self, f, compression):
- self.make_test_archive(f, compression)
-
- # Read the ZIP archive
- with zipfile.ZipFile(f, "r") as zipfp:
- for sep, fn in self.arcfiles.items():
- with openU(zipfp, fn) as fp:
- ziplines = fp.readlines()
- for line, zipline in zip(self.line_gen, ziplines):
- self.assertEqual(zipline, line + b'\n')
-
- def test_readlines(self):
- for f in get_files(self):
- self.readlines_test(f, self.compression)
-
- def iterlines_test(self, f, compression):
- self.make_test_archive(f, compression)
-
- # Read the ZIP archive
- with zipfile.ZipFile(f, "r") as zipfp:
- for sep, fn in self.arcfiles.items():
- with openU(zipfp, fn) as fp:
- for line, zipline in zip(self.line_gen, fp):
- self.assertEqual(zipline, line + b'\n')
-
- def test_iterlines(self):
- for f in get_files(self):
- self.iterlines_test(f, self.compression)
-
- def tearDown(self):
- for sep, fn in self.arcfiles.items():
- unlink(fn)
- unlink(TESTFN)
- unlink(TESTFN2)
-
-
-class StoredUniversalNewlineTests(AbstractUniversalNewlineTests,
- unittest.TestCase):
- compression = zipfile.ZIP_STORED
-
-@requires_zlib
-class DeflateUniversalNewlineTests(AbstractUniversalNewlineTests,
- unittest.TestCase):
- compression = zipfile.ZIP_DEFLATED
-
-@requires_bz2
-class Bzip2UniversalNewlineTests(AbstractUniversalNewlineTests,
- unittest.TestCase):
- compression = zipfile.ZIP_BZIP2
-
-@requires_lzma
-class LzmaUniversalNewlineTests(AbstractUniversalNewlineTests,
- unittest.TestCase):
- compression = zipfile.ZIP_LZMA
+ def test_from_dir(self):
+ dirpath = os.path.dirname(os.path.abspath(__file__))
+ zi = zipfile.ZipInfo.from_file(dirpath, 'stdlib_tests')
+ self.assertEqual(zi.filename, 'stdlib_tests/')
+ self.assertTrue(zi.is_dir())
+ self.assertEqual(zi.compress_type, zipfile.ZIP_STORED)
+ self.assertEqual(zi.file_size, 0)
class CommandLineTest(unittest.TestCase):
@@ -2175,7 +2101,7 @@ class CommandLineTest(unittest.TestCase):
for zi in zf.infolist():
path = os.path.join(extdir,
zi.filename.replace('/', os.sep))
- if zi.filename.endswith('/'):
+ if zi.is_dir():
self.assertTrue(os.path.isdir(path))
else:
self.assertTrue(os.path.isfile(path))
diff --git a/Lib/test/test_zipimport.py b/Lib/test/test_zipimport.py
index d5b3b22..7ddbc50 100644
--- a/Lib/test/test_zipimport.py
+++ b/Lib/test/test_zipimport.py
@@ -398,7 +398,8 @@ class UncompressedZipImportTestCase(ImportHooksBaseTestCase):
packdir2 = packdir + TESTPACK2 + os.sep
files = {packdir + "__init__" + pyc_ext: (NOW, test_pyc),
packdir2 + "__init__" + pyc_ext: (NOW, test_pyc),
- packdir2 + TESTMOD + pyc_ext: (NOW, test_pyc)}
+ packdir2 + TESTMOD + pyc_ext: (NOW, test_pyc),
+ "spam" + pyc_ext: (NOW, test_pyc)}
z = ZipFile(TEMP_ZIP, "w")
try:
@@ -412,6 +413,14 @@ class UncompressedZipImportTestCase(ImportHooksBaseTestCase):
zi = zipimport.zipimporter(TEMP_ZIP)
self.assertEqual(zi.archive, TEMP_ZIP)
self.assertEqual(zi.is_package(TESTPACK), True)
+
+ find_mod = zi.find_module('spam')
+ self.assertIsNotNone(find_mod)
+ self.assertIsInstance(find_mod, zipimport.zipimporter)
+ self.assertFalse(find_mod.is_package('spam'))
+ load_mod = find_mod.load_module('spam')
+ self.assertEqual(find_mod.get_filename('spam'), load_mod.__file__)
+
mod = zi.load_module(TESTPACK)
self.assertEqual(zi.get_filename(TESTPACK), mod.__file__)
@@ -471,6 +480,16 @@ class UncompressedZipImportTestCase(ImportHooksBaseTestCase):
self.assertEqual(
zi.is_package(TESTPACK2 + os.sep + TESTMOD), False)
+ pkg_path = TEMP_ZIP + os.sep + packdir + TESTPACK2
+ zi2 = zipimport.zipimporter(pkg_path)
+ find_mod_dotted = zi2.find_module(TESTMOD)
+ self.assertIsNotNone(find_mod_dotted)
+ self.assertIsInstance(find_mod_dotted, zipimport.zipimporter)
+ self.assertFalse(zi2.is_package(TESTMOD))
+ load_mod = find_mod_dotted.load_module(TESTMOD)
+ self.assertEqual(
+ find_mod_dotted.get_filename(TESTMOD), load_mod.__file__)
+
mod_path = TESTPACK2 + os.sep + TESTMOD
mod_name = module_path_to_dotted_name(mod_path)
mod = importlib.import_module(mod_name)
@@ -623,8 +642,10 @@ class UncompressedZipImportTestCase(ImportHooksBaseTestCase):
zipimport.zipimporter(filename)
zipimport.zipimporter(os.fsencode(filename))
- zipimport.zipimporter(bytearray(os.fsencode(filename)))
- zipimport.zipimporter(memoryview(os.fsencode(filename)))
+ with self.assertWarns(DeprecationWarning):
+ zipimport.zipimporter(bytearray(os.fsencode(filename)))
+ with self.assertWarns(DeprecationWarning):
+ zipimport.zipimporter(memoryview(os.fsencode(filename)))
@support.requires_zlib
diff --git a/Lib/test/test_zipimport_support.py b/Lib/test/test_zipimport_support.py
index 5913622..84d526c 100644
--- a/Lib/test/test_zipimport_support.py
+++ b/Lib/test/test_zipimport_support.py
@@ -12,7 +12,6 @@ import zipimport
import doctest
import inspect
import linecache
-import pdb
import unittest
from test.support.script_helper import (spawn_python, kill_python, assert_python_ok,
make_script, make_zip_script)
diff --git a/Lib/test/test_zlib.py b/Lib/test/test_zlib.py
index 6fea893..20174d8 100644
--- a/Lib/test/test_zlib.py
+++ b/Lib/test/test_zlib.py
@@ -164,6 +164,16 @@ class CompressTestCase(BaseCompressTestCase, unittest.TestCase):
x = zlib.compress(HAMLET_SCENE)
self.assertEqual(zlib.decompress(x), HAMLET_SCENE)
+ def test_keywords(self):
+ x = zlib.compress(HAMLET_SCENE, level=3)
+ self.assertEqual(zlib.decompress(x), HAMLET_SCENE)
+ with self.assertRaises(TypeError):
+ zlib.compress(data=HAMLET_SCENE, level=3)
+ self.assertEqual(zlib.decompress(x,
+ wbits=zlib.MAX_WBITS,
+ bufsize=zlib.DEF_BUF_SIZE),
+ HAMLET_SCENE)
+
def test_speech128(self):
# compress more data
data = HAMLET_SCENE * 128
@@ -234,6 +244,27 @@ class CompressObjectTestCase(BaseCompressTestCase, unittest.TestCase):
self.assertIsInstance(dco.unconsumed_tail, bytes)
self.assertIsInstance(dco.unused_data, bytes)
+ def test_keywords(self):
+ level = 2
+ method = zlib.DEFLATED
+ wbits = -12
+ memLevel = 9
+ strategy = zlib.Z_FILTERED
+ co = zlib.compressobj(level=level,
+ method=method,
+ wbits=wbits,
+ memLevel=memLevel,
+ strategy=strategy,
+ zdict=b"")
+ do = zlib.decompressobj(wbits=wbits, zdict=b"")
+ with self.assertRaises(TypeError):
+ co.compress(data=HAMLET_SCENE)
+ with self.assertRaises(TypeError):
+ do.decompress(data=zlib.compress(HAMLET_SCENE))
+ x = co.compress(HAMLET_SCENE) + co.flush()
+ y = do.decompress(x, max_length=len(HAMLET_SCENE)) + do.flush()
+ self.assertEqual(HAMLET_SCENE, y)
+
def test_compressoptions(self):
# specify lots of options to compressobj()
level = 2
@@ -249,10 +280,6 @@ class CompressObjectTestCase(BaseCompressTestCase, unittest.TestCase):
y2 = dco.flush()
self.assertEqual(HAMLET_SCENE, y1 + y2)
- # keyword arguments should also be supported
- zlib.compressobj(level=level, method=method, wbits=wbits,
- memLevel=memLevel, strategy=strategy, zdict=b"")
-
def test_compressincremental(self):
# compress object in steps, decompress object as one-shot
data = HAMLET_SCENE * 128
diff --git a/Lib/threading.py b/Lib/threading.py
index 06b7b9b..4829ff4 100644
--- a/Lib/threading.py
+++ b/Lib/threading.py
@@ -22,9 +22,11 @@ except ImportError:
# with the multiprocessing module, which doesn't provide the old
# Java inspired names.
-__all__ = ['active_count', 'Condition', 'current_thread', 'enumerate', 'Event',
- 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread', 'Barrier',
- 'Timer', 'ThreadError', 'setprofile', 'settrace', 'local', 'stack_size']
+__all__ = ['get_ident', 'active_count', 'Condition', 'current_thread',
+ 'enumerate', 'main_thread', 'TIMEOUT_MAX',
+ 'Event', 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread',
+ 'Barrier', 'BrokenBarrierError', 'Timer', 'ThreadError',
+ 'setprofile', 'settrace', 'local', 'stack_size']
# Rename some stuff so "from threading import *" is safe
_start_new_thread = _thread.start_new_thread
diff --git a/Lib/timeit.py b/Lib/timeit.py
index 2de88f7..2770efa 100755..100644
--- a/Lib/timeit.py
+++ b/Lib/timeit.py
@@ -207,6 +207,26 @@ class Timer:
r.append(t)
return r
+ def autorange(self, callback=None):
+ """Return the number of loops so that total time >= 0.2.
+
+ Calls the timeit method with *number* set to successive powers of
+ ten (10, 100, 1000, ...) up to a maximum of one billion, until
+ the time taken is at least 0.2 second, or the maximum is reached.
+ Returns ``(number, time_taken)``.
+
+ If *callback* is given and is not None, it will be called after
+ each trial with two arguments: ``callback(number, time_taken)``.
+ """
+ for i in range(1, 10):
+ number = 10**i
+ time_taken = self.timeit(number)
+ if callback:
+ callback(number, time_taken)
+ if time_taken >= 0.2:
+ break
+ return (number, time_taken)
+
def timeit(stmt="pass", setup="pass", timer=default_timer,
number=default_number, globals=None):
"""Convenience function to create Timer object and call timeit method."""
@@ -295,17 +315,16 @@ def main(args=None, *, _wrap_timer=None):
t = Timer(stmt, setup, timer)
if number == 0:
# determine number so that 0.2 <= total time < 2.0
- for i in range(1, 10):
- number = 10**i
- try:
- x = t.timeit(number)
- except:
- t.print_exc()
- return 1
- if verbose:
- print("%d loops -> %.*g secs" % (number, precision, x))
- if x >= 0.2:
- break
+ callback = None
+ if verbose:
+ def callback(number, time_taken):
+ msg = "{num} loops -> {secs:.{prec}g} secs"
+ print(msg.format(num=number, secs=time_taken, prec=precision))
+ try:
+ number, _ = t.autorange(callback)
+ except:
+ t.print_exc()
+ return 1
try:
r = t.repeat(repeat, number)
except:
@@ -317,20 +336,26 @@ def main(args=None, *, _wrap_timer=None):
print("%d loops," % number, end=' ')
usec = best * 1e6 / number
if time_unit is not None:
- print("best of %d: %.*g %s per loop" % (repeat, precision,
- usec/units[time_unit], time_unit))
+ scale = units[time_unit]
else:
- if usec < 1000:
- print("best of %d: %.*g usec per loop" % (repeat, precision, usec))
- else:
- msec = usec / 1000
- if msec < 1000:
- print("best of %d: %.*g msec per loop" % (repeat,
- precision, msec))
- else:
- sec = msec / 1000
- print("best of %d: %.*g sec per loop" % (repeat,
- precision, sec))
+ scales = [(scale, unit) for unit, scale in units.items()]
+ scales.sort(reverse=True)
+ for scale, time_unit in scales:
+ if usec >= scale:
+ break
+ print("best of %d: %.*g %s per loop" % (repeat, precision,
+ usec/scale, time_unit))
+ best = min(r)
+ usec = best * 1e6 / number
+ worst = max(r)
+ if worst >= best * 4:
+ usec = worst * 1e6 / number
+ import warnings
+ warnings.warn_explicit(
+ "The test results are likely unreliable. The worst\n"
+ "time (%.*g %s) was more than four times slower than the best time." %
+ (precision, usec/scale, time_unit),
+ UserWarning, '', 0)
return None
if __name__ == "__main__":
diff --git a/Lib/tkinter/__init__.py b/Lib/tkinter/__init__.py
index 1eaab44..ee2415d 100644
--- a/Lib/tkinter/__init__.py
+++ b/Lib/tkinter/__init__.py
@@ -30,6 +30,7 @@ button.pack(side=BOTTOM)
tk.mainloop()
"""
+import enum
import sys
import _tkinter # If this fails your Python may not be configured for Tk
@@ -132,6 +133,50 @@ def _splitdict(tk, v, cut_minus=True, conv=None):
dict[key] = value
return dict
+
+class EventType(str, enum.Enum):
+ KeyPress = '2'
+ Key = KeyPress,
+ KeyRelease = '3'
+ ButtonPress = '4'
+ Button = ButtonPress,
+ ButtonRelease = '5'
+ Motion = '6'
+ Enter = '7'
+ Leave = '8'
+ FocusIn = '9'
+ FocusOut = '10'
+ Keymap = '11' # undocumented
+ Expose = '12'
+ GraphicsExpose = '13' # undocumented
+ NoExpose = '14' # undocumented
+ Visibility = '15'
+ Create = '16'
+ Destroy = '17'
+ Unmap = '18'
+ Map = '19'
+ MapRequest = '20'
+ Reparent = '21'
+ Configure = '22'
+ ConfigureRequest = '23'
+ Gravity = '24'
+ ResizeRequest = '25'
+ Circulate = '26'
+ CirculateRequest = '27'
+ Property = '28'
+ SelectionClear = '29' # undocumented
+ SelectionRequest = '30' # undocumented
+ Selection = '31' # undocumented
+ Colormap = '32'
+ ClientMessage = '33' # undocumented
+ Mapping = '34' # undocumented
+ VirtualEvent = '35', # undocumented
+ Activate = '36',
+ Deactivate = '37',
+ MouseWheel = '38',
+ def __str__(self):
+ return self.name
+
class Event:
"""Container for the properties of an event.
@@ -174,7 +219,43 @@ class Event:
widget - widget in which the event occurred
delta - delta of wheel movement (MouseWheel)
"""
- pass
+ def __repr__(self):
+ attrs = {k: v for k, v in self.__dict__.items() if v != '??'}
+ if not self.char:
+ del attrs['char']
+ elif self.char != '??':
+ attrs['char'] = repr(self.char)
+ if not getattr(self, 'send_event', True):
+ del attrs['send_event']
+ if self.state == 0:
+ del attrs['state']
+ elif isinstance(self.state, int):
+ state = self.state
+ mods = ('Shift', 'Lock', 'Control',
+ 'Mod1', 'Mod2', 'Mod3', 'Mod4', 'Mod5',
+ 'Button1', 'Button2', 'Button3', 'Button4', 'Button5')
+ s = []
+ for i, n in enumerate(mods):
+ if state & (1 << i):
+ s.append(n)
+ state = state & ~((1<< len(mods)) - 1)
+ if state or not s:
+ s.append(hex(state))
+ attrs['state'] = '|'.join(s)
+ if self.delta == 0:
+ del attrs['delta']
+ # widget usually is known
+ # serial and time are not very interesting
+ # keysym_num duplicates keysym
+ # x_root and y_root mostly duplicate x and y
+ keys = ('send_event',
+ 'state', 'keysym', 'keycode', 'char',
+ 'num', 'delta', 'focus',
+ 'x', 'y', 'width', 'height')
+ return '<%s event%s>' % (
+ self.type,
+ ''.join(' %s=%s' % (k, attrs[k]) for k in keys if k in attrs)
+ )
_support_default_root = 1
_default_root = None
@@ -262,15 +343,8 @@ class Variable:
def get(self):
"""Return value of variable."""
return self._tk.globalgetvar(self._name)
- def trace_variable(self, mode, callback):
- """Define a trace callback for the variable.
- MODE is one of "r", "w", "u" for read, write, undefine.
- CALLBACK must be a function which is called when
- the variable is read, written or undefined.
-
- Return the name of the callback.
- """
+ def _register(self, callback):
f = CallWrapper(callback, None, self._root).__call__
cbname = repr(id(f))
try:
@@ -285,18 +359,80 @@ class Variable:
if self._tclCommands is None:
self._tclCommands = []
self._tclCommands.append(cbname)
+ return cbname
+
+ def trace_add(self, mode, callback):
+ """Define a trace callback for the variable.
+
+ Mode is one of "read", "write", "unset", or a list or tuple of
+ such strings.
+ Callback must be a function which is called when the variable is
+ read, written or unset.
+
+ Return the name of the callback.
+ """
+ cbname = self._register(callback)
+ self._tk.call('trace', 'add', 'variable',
+ self._name, mode, (cbname,))
+ return cbname
+
+ def trace_remove(self, mode, cbname):
+ """Delete the trace callback for a variable.
+
+ Mode is one of "read", "write", "unset" or a list or tuple of
+ such strings. Must be same as were specified in trace_add().
+ cbname is the name of the callback returned from trace_add().
+ """
+ self._tk.call('trace', 'remove', 'variable',
+ self._name, mode, cbname)
+ for m, ca in self.trace_info():
+ if self._tk.splitlist(ca)[0] == cbname:
+ break
+ else:
+ self._tk.deletecommand(cbname)
+ try:
+ self._tclCommands.remove(cbname)
+ except ValueError:
+ pass
+
+ def trace_info(self):
+ """Return all trace callback information."""
+ splitlist = self._tk.splitlist
+ return [(splitlist(k), v) for k, v in map(splitlist,
+ splitlist(self._tk.call('trace', 'info', 'variable', self._name)))]
+
+ def trace_variable(self, mode, callback):
+ """Define a trace callback for the variable.
+
+ MODE is one of "r", "w", "u" for read, write, undefine.
+ CALLBACK must be a function which is called when
+ the variable is read, written or undefined.
+
+ Return the name of the callback.
+
+ This deprecated method wraps a deprecated Tcl method that will
+ likely be removed in the future. Use trace_add() instead.
+ """
+ # TODO: Add deprecation warning
+ cbname = self._register(callback)
self._tk.call("trace", "variable", self._name, mode, cbname)
return cbname
+
trace = trace_variable
+
def trace_vdelete(self, mode, cbname):
"""Delete the trace callback for a variable.
MODE is one of "r", "w", "u" for read, write, undefine.
CBNAME is the name of the callback returned from trace_variable or trace.
+
+ This deprecated method wraps a deprecated Tcl method that will
+ likely be removed in the future. Use trace_remove() instead.
"""
+ # TODO: Add deprecation warning
self._tk.call("trace", "vdelete", self._name, mode, cbname)
cbname = self._tk.splitlist(cbname)[0]
- for m, ca in self.trace_vinfo():
+ for m, ca in self.trace_info():
if self._tk.splitlist(ca)[0] == cbname:
break
else:
@@ -305,10 +441,17 @@ class Variable:
self._tclCommands.remove(cbname)
except ValueError:
pass
+
def trace_vinfo(self):
- """Return all trace callback information."""
+ """Return all trace callback information.
+
+ This deprecated method wraps a deprecated Tcl method that will
+ likely be removed in the future. Use trace_info() instead.
+ """
+ # TODO: Add deprecation warning
return [self._tk.splitlist(x) for x in self._tk.splitlist(
self._tk.call("trace", "vinfo", self._name))]
+
def __eq__(self, other):
"""Comparison for equality (==).
@@ -430,6 +573,9 @@ class Misc:
Base class which defines methods common for interior widgets."""
+ # used for generating child widget names
+ _last_child_ids = None
+
# XXX font command?
_tclCommands = None
def destroy(self):
@@ -477,12 +623,6 @@ class Misc:
disabledForeground, insertBackground, troughColor."""
self.tk.call(('tk_setPalette',)
+ _flatten(args) + _flatten(list(kw.items())))
- def tk_menuBar(self, *args):
- """Do not use. Needed in Tk 3.6 and earlier."""
- # obsolete since Tk 4.0
- import warnings
- warnings.warn('tk_menuBar() does nothing and will be removed in 3.6',
- DeprecationWarning, stacklevel=2)
def wait_variable(self, name='PY_VAR'):
"""Wait until the variable is modified.
@@ -854,8 +994,7 @@ class Misc:
self.tk.call('winfo', 'height', self._w))
def winfo_id(self):
"""Return identifier ID for this widget."""
- return self.tk.getint(
- self.tk.call('winfo', 'id', self._w))
+ return int(self.tk.call('winfo', 'id', self._w), 0)
def winfo_interps(self, displayof=0):
"""Return the name of all Tcl interpreters for this display."""
args = ('winfo', 'interps') + self._displayof(displayof)
@@ -975,18 +1114,16 @@ class Misc:
def winfo_visualid(self):
"""Return the X identifier for the visual for this widget."""
return self.tk.call('winfo', 'visualid', self._w)
- def winfo_visualsavailable(self, includeids=0):
+ def winfo_visualsavailable(self, includeids=False):
"""Return a list of all visuals available for the screen
of this widget.
Each item in the list consists of a visual name (see winfo_visual), a
- depth and if INCLUDEIDS=1 is given also the X identifier."""
- data = self.tk.split(
- self.tk.call('winfo', 'visualsavailable', self._w,
- includeids and 'includeids' or None))
- if isinstance(data, str):
- data = [self.tk.split(data)]
- return [self.__winfo_parseitem(x) for x in data]
+ depth and if includeids is true is given also the X identifier."""
+ data = self.tk.call('winfo', 'visualsavailable', self._w,
+ 'includeids' if includeids else None)
+ data = [self.tk.splitlist(x) for x in self.tk.splitlist(data)]
+ return [self.__winfo_parseitem(x) for x in data]
def __winfo_parseitem(self, t):
"""Internal function."""
return t[:1] + tuple(map(self.__winfo_getint, t[1:]))
@@ -1287,7 +1424,10 @@ class Misc:
except TclError: pass
e.keysym = K
e.keysym_num = getint_event(N)
- e.type = T
+ try:
+ e.type = EventType(T)
+ except ValueError:
+ e.type = T
try:
e.widget = self._nametowidget(W)
except KeyError:
@@ -1897,9 +2037,6 @@ class Tk(Misc, Wm):
if tcl_version != _tkinter.TCL_VERSION:
raise RuntimeError("tcl.h version (%s) doesn't match libtcl.a version (%s)" \
% (_tkinter.TCL_VERSION, tcl_version))
- if TkVersion < 4.0:
- raise RuntimeError("Tk 4.0 or higher is required; found Tk %s"
- % str(TkVersion))
# Create and register the tkerror and exit commands
# We need to inline parts of _register here, _ register
# would register differently-named commands.
@@ -2122,7 +2259,15 @@ class BaseWidget(Misc):
name = cnf['name']
del cnf['name']
if not name:
- name = repr(id(self))
+ name = self.__class__.__name__.lower()
+ if master._last_child_ids is None:
+ master._last_child_ids = {}
+ count = master._last_child_ids.get(name, 0) + 1
+ master._last_child_ids[name] = count
+ if count == 1:
+ name = '!%s' % (name,)
+ else:
+ name = '!%s%d' % (name, count)
self._name = name
if master._w=='.':
self._w = '.' + name
@@ -2718,12 +2863,6 @@ class Menu(Widget):
def tk_popup(self, x, y, entry=""):
"""Post the menu at position X,Y with entry ENTRY."""
self.tk.call('tk_popup', self._w, x, y, entry)
- def tk_bindForTraversal(self):
- # obsolete since Tk 4.0
- import warnings
- warnings.warn('tk_bindForTraversal() does nothing and '
- 'will be removed in 3.6',
- DeprecationWarning, stacklevel=2)
def activate(self, index):
"""Activate entry at INDEX."""
self.tk.call(self._w, 'activate', index)
@@ -3346,9 +3485,6 @@ class Image:
if not name:
Image._last_id += 1
name = "pyimage%r" % (Image._last_id,) # tk itself would use image<x>
- # The following is needed for systems where id(x)
- # can return a negative number, such as Linux/m68k:
- if name[0] == '-': name = '_' + name[1:]
if kw and cnf: cnf = _cnfmerge((cnf, kw))
elif kw: cnf = kw
options = ()
diff --git a/Lib/tkinter/commondialog.py b/Lib/tkinter/commondialog.py
index d2688db..1e75cae 100644
--- a/Lib/tkinter/commondialog.py
+++ b/Lib/tkinter/commondialog.py
@@ -15,11 +15,6 @@ class Dialog:
command = None
def __init__(self, master=None, **options):
-
- # FIXME: should this be placed on the module level instead?
- if TkVersion < 4.2:
- raise TclError("this module requires Tk 4.2 or newer")
-
self.master = master
self.options = options
if not master and options.get('parent'):
diff --git a/Lib/tkinter/dialog.py b/Lib/tkinter/dialog.py
index be085ab..f61c5f7 100644
--- a/Lib/tkinter/dialog.py
+++ b/Lib/tkinter/dialog.py
@@ -3,10 +3,7 @@
from tkinter import *
from tkinter import _cnfmerge
-if TkVersion <= 3.6:
- DIALOG_ICON = 'warning'
-else:
- DIALOG_ICON = 'questhead'
+DIALOG_ICON = 'questhead'
class Dialog(Widget):
diff --git a/Lib/tkinter/test/runtktests.py b/Lib/tkinter/test/runtktests.py
index dbe5e88..33dc54a 100644
--- a/Lib/tkinter/test/runtktests.py
+++ b/Lib/tkinter/test/runtktests.py
@@ -7,8 +7,6 @@ Extensions also should live in packages following the same rule as above.
"""
import os
-import sys
-import unittest
import importlib
import test.support
diff --git a/Lib/tkinter/test/test_tkinter/test_misc.py b/Lib/tkinter/test/test_tkinter/test_misc.py
index 85ee2c7..9dc1e37 100644
--- a/Lib/tkinter/test/test_tkinter/test_misc.py
+++ b/Lib/tkinter/test/test_tkinter/test_misc.py
@@ -12,6 +12,14 @@ class MiscTest(AbstractTkTest, unittest.TestCase):
f = tkinter.Frame(t, name='child')
self.assertEqual(repr(f), '<tkinter.Frame object .top.child>')
+ def test_generated_names(self):
+ t = tkinter.Toplevel(self.root)
+ f = tkinter.Frame(t)
+ f2 = tkinter.Frame(t)
+ b = tkinter.Button(f2)
+ for name in str(b).split('.'):
+ self.assertFalse(name.isidentifier(), msg=repr(name))
+
def test_tk_setPalette(self):
root = self.root
root.tk_setPalette('black')
diff --git a/Lib/tkinter/test/test_tkinter/test_variables.py b/Lib/tkinter/test/test_tkinter/test_variables.py
index d8ba9ce..2eb1e12 100644
--- a/Lib/tkinter/test/test_tkinter/test_variables.py
+++ b/Lib/tkinter/test/test_tkinter/test_variables.py
@@ -87,7 +87,8 @@ class TestVariable(TestBase):
v.set("value")
self.assertTrue(v.side_effect)
- def test_trace(self):
+ def test_trace_old(self):
+ # Old interface
v = Variable(self.root)
vname = str(v)
trace = []
@@ -136,6 +137,55 @@ class TestVariable(TestBase):
gc.collect()
self.assertEqual(trace, [('write', vname, '', 'u')])
+ def test_trace(self):
+ v = Variable(self.root)
+ vname = str(v)
+ trace = []
+ def read_tracer(*args):
+ trace.append(('read',) + args)
+ def write_tracer(*args):
+ trace.append(('write',) + args)
+ tr1 = v.trace_add('read', read_tracer)
+ tr2 = v.trace_add(['write', 'unset'], write_tracer)
+ self.assertEqual(sorted(v.trace_info()), [
+ (('read',), tr1),
+ (('write', 'unset'), tr2)])
+ self.assertEqual(trace, [])
+
+ v.set('spam')
+ self.assertEqual(trace, [('write', vname, '', 'write')])
+
+ trace = []
+ v.get()
+ self.assertEqual(trace, [('read', vname, '', 'read')])
+
+ trace = []
+ info = sorted(v.trace_info())
+ v.trace_remove('write', tr1) # Wrong mode
+ self.assertEqual(sorted(v.trace_info()), info)
+ with self.assertRaises(TclError):
+ v.trace_remove('read', 'spam') # Wrong command name
+ self.assertEqual(sorted(v.trace_info()), info)
+ v.get()
+ self.assertEqual(trace, [('read', vname, '', 'read')])
+
+ trace = []
+ v.trace_remove('read', tr1)
+ self.assertEqual(v.trace_info(), [(('write', 'unset'), tr2)])
+ v.get()
+ self.assertEqual(trace, [])
+
+ trace = []
+ del write_tracer
+ gc.collect()
+ v.set('eggs')
+ self.assertEqual(trace, [('write', vname, '', 'write')])
+
+ trace = []
+ del v
+ gc.collect()
+ self.assertEqual(trace, [('write', vname, '', 'unset')])
+
class TestStringVar(TestBase):
diff --git a/Lib/tkinter/test/test_tkinter/test_widgets.py b/Lib/tkinter/test/test_tkinter/test_widgets.py
index c924d55..81b52ea 100644
--- a/Lib/tkinter/test/test_tkinter/test_widgets.py
+++ b/Lib/tkinter/test/test_tkinter/test_widgets.py
@@ -91,9 +91,10 @@ class ToplevelTest(AbstractToplevelTest, unittest.TestCase):
widget = self.create()
self.assertEqual(widget['use'], '')
parent = self.create(container=True)
- wid = parent.winfo_id()
- widget2 = self.create(use=wid)
- self.assertEqual(int(widget2['use']), wid)
+ wid = hex(parent.winfo_id())
+ with self.subTest(wid=wid):
+ widget2 = self.create(use=wid)
+ self.assertEqual(widget2['use'], wid)
@add_standard_options(StandardOptionsTests)
diff --git a/Lib/tkinter/test/test_ttk/test_functions.py b/Lib/tkinter/test/test_ttk/test_functions.py
index c68a650..a1b7cdf 100644
--- a/Lib/tkinter/test/test_ttk/test_functions.py
+++ b/Lib/tkinter/test/test_ttk/test_functions.py
@@ -1,6 +1,5 @@
# -*- encoding: utf-8 -*-
import unittest
-import tkinter
from tkinter import ttk
class MockTkApp:
diff --git a/Lib/tkinter/test/test_ttk/test_widgets.py b/Lib/tkinter/test/test_ttk/test_widgets.py
index 8bd22d0..26766a8 100644
--- a/Lib/tkinter/test/test_ttk/test_widgets.py
+++ b/Lib/tkinter/test/test_ttk/test_widgets.py
@@ -1487,6 +1487,7 @@ class TreeviewTest(AbstractWidgetTest, unittest.TestCase):
def test_selection(self):
+ self.assertRaises(TypeError, self.tv.selection, 'spam')
# item 'none' doesn't exist
self.assertRaises(tkinter.TclError, self.tv.selection_set, 'none')
self.assertRaises(tkinter.TclError, self.tv.selection_add, 'none')
@@ -1500,25 +1501,31 @@ class TreeviewTest(AbstractWidgetTest, unittest.TestCase):
c3 = self.tv.insert(item1, 'end')
self.assertEqual(self.tv.selection(), ())
- self.tv.selection_set((c1, item2))
+ self.tv.selection_set(c1, item2)
self.assertEqual(self.tv.selection(), (c1, item2))
self.tv.selection_set(c2)
self.assertEqual(self.tv.selection(), (c2,))
- self.tv.selection_add((c1, item2))
+ self.tv.selection_add(c1, item2)
self.assertEqual(self.tv.selection(), (c1, c2, item2))
self.tv.selection_add(item1)
self.assertEqual(self.tv.selection(), (item1, c1, c2, item2))
+ self.tv.selection_add()
+ self.assertEqual(self.tv.selection(), (item1, c1, c2, item2))
- self.tv.selection_remove((item1, c3))
+ self.tv.selection_remove(item1, c3)
self.assertEqual(self.tv.selection(), (c1, c2, item2))
self.tv.selection_remove(c2)
self.assertEqual(self.tv.selection(), (c1, item2))
+ self.tv.selection_remove()
+ self.assertEqual(self.tv.selection(), (c1, item2))
- self.tv.selection_toggle((c1, c3))
+ self.tv.selection_toggle(c1, c3)
self.assertEqual(self.tv.selection(), (c3, item2))
self.tv.selection_toggle(item2)
self.assertEqual(self.tv.selection(), (c3,))
+ self.tv.selection_toggle()
+ self.assertEqual(self.tv.selection(), (c3,))
self.tv.insert('', 'end', id='with spaces')
self.tv.selection_set('with spaces')
@@ -1536,6 +1543,40 @@ class TreeviewTest(AbstractWidgetTest, unittest.TestCase):
self.tv.selection_set(b'bytes\xe2\x82\xac')
self.assertEqual(self.tv.selection(), ('bytes\xe2\x82\xac',))
+ self.tv.selection_set()
+ self.assertEqual(self.tv.selection(), ())
+
+ # Old interface
+ self.tv.selection_set((c1, item2))
+ self.assertEqual(self.tv.selection(), (c1, item2))
+ self.tv.selection_add((c1, item1))
+ self.assertEqual(self.tv.selection(), (item1, c1, item2))
+ self.tv.selection_remove((item1, c3))
+ self.assertEqual(self.tv.selection(), (c1, item2))
+ self.tv.selection_toggle((c1, c3))
+ self.assertEqual(self.tv.selection(), (c3, item2))
+
+ if sys.version_info >= (3, 7):
+ import warnings
+ warnings.warn(
+ 'Deprecated API of Treeview.selection() should be removed')
+ self.tv.selection_set()
+ self.assertEqual(self.tv.selection(), ())
+ with self.assertWarns(DeprecationWarning):
+ self.tv.selection('set', (c1, item2))
+ self.assertEqual(self.tv.selection(), (c1, item2))
+ with self.assertWarns(DeprecationWarning):
+ self.tv.selection('add', (c1, item1))
+ self.assertEqual(self.tv.selection(), (item1, c1, item2))
+ with self.assertWarns(DeprecationWarning):
+ self.tv.selection('remove', (item1, c3))
+ self.assertEqual(self.tv.selection(), (c1, item2))
+ with self.assertWarns(DeprecationWarning):
+ self.tv.selection('toggle', (c1, c3))
+ self.assertEqual(self.tv.selection(), (c3, item2))
+ with self.assertWarns(DeprecationWarning):
+ selection = self.tv.selection(None)
+ self.assertEqual(selection, (c3, item2))
def test_set(self):
self.tv['columns'] = ['A', 'B']
diff --git a/Lib/tkinter/tix.py b/Lib/tkinter/tix.py
index a1006bd..d9c097a 100644
--- a/Lib/tkinter/tix.py
+++ b/Lib/tkinter/tix.py
@@ -27,10 +27,6 @@ import tkinter
from tkinter import *
from tkinter import _cnfmerge
-# WARNING - TkVersion is a limited precision floating point number
-if TkVersion < 3.999:
- raise ImportError("This version of Tix.py requires Tk 4.0 or higher")
-
import _tkinter # If this fails your Python may not be configured for Tk
# Some more constants (for consistency with Tkinter)
@@ -472,16 +468,17 @@ class DisplayStyle:
"""DisplayStyle - handle configuration options shared by
(multiple) Display Items"""
- def __init__(self, itemtype, cnf={}, **kw):
- if 'refwindow' in kw:
- master = kw['refwindow']
- elif 'refwindow' in cnf:
- master = cnf['refwindow']
- else:
- master = tkinter._default_root
- if not master:
- raise RuntimeError("Too early to create display style: "
- "no root window")
+ def __init__(self, itemtype, cnf={}, *, master=None, **kw):
+ if not master:
+ if 'refwindow' in kw:
+ master = kw['refwindow']
+ elif 'refwindow' in cnf:
+ master = cnf['refwindow']
+ else:
+ master = tkinter._default_root
+ if not master:
+ raise RuntimeError("Too early to create display style: "
+ "no root window")
self.tk = master.tk
self.stylename = self.tk.call('tixDisplayStyle', itemtype,
*self._options(cnf,kw) )
@@ -1116,7 +1113,7 @@ class ListNoteBook(TixWidget):
def pages(self):
# Can't call subwidgets_all directly because we don't want .nbframe
- names = self.tk.split(self.tk.call(self._w, 'pages'))
+ names = self.tk.splitlist(self.tk.call(self._w, 'pages'))
ret = []
for x in names:
ret.append(self.subwidget(x))
@@ -1162,7 +1159,7 @@ class NoteBook(TixWidget):
def pages(self):
# Can't call subwidgets_all directly because we don't want .nbframe
- names = self.tk.split(self.tk.call(self._w, 'pages'))
+ names = self.tk.splitlist(self.tk.call(self._w, 'pages'))
ret = []
for x in names:
ret.append(self.subwidget(x))
@@ -1585,8 +1582,7 @@ class CheckList(TixWidget):
'''Returns a list of items whose status matches status. If status is
not specified, the list of items in the "on" status will be returned.
Mode can be on, off, default'''
- c = self.tk.split(self.tk.call(self._w, 'getselection', mode))
- return self.tk.splitlist(c)
+ return self.tk.splitlist(self.tk.call(self._w, 'getselection', mode))
def getstatus(self, entrypath):
'''Returns the current status of entryPath.'''
@@ -1907,7 +1903,7 @@ class Grid(TixWidget, XView, YView):
or a real number following by the word chars
(e.g. 3.4chars) that sets the width of the column to the
given number of characters."""
- return self.tk.split(self.tk.call(self._w, 'size', 'column', index,
+ return self.tk.splitlist(self.tk.call(self._w, 'size', 'column', index,
*self._options({}, kw)))
def size_row(self, index, **kw):
@@ -1932,7 +1928,7 @@ class Grid(TixWidget, XView, YView):
or a real number following by the word chars
(e.g. 3.4chars) that sets the height of the row to the
given number of characters."""
- return self.tk.split(self.tk.call(
+ return self.tk.splitlist(self.tk.call(
self, 'size', 'row', index, *self._options({}, kw)))
def unset(self, x, y):
diff --git a/Lib/tkinter/ttk.py b/Lib/tkinter/ttk.py
index f4a6d8c..c474e60 100644
--- a/Lib/tkinter/ttk.py
+++ b/Lib/tkinter/ttk.py
@@ -28,6 +28,8 @@ __all__ = ["Button", "Checkbutton", "Combobox", "Entry", "Frame", "Label",
import tkinter
from tkinter import _flatten, _join, _stringify, _splitdict
+_sentinel = object()
+
# Verify if Tk is new enough to not need the Tile package
_REQUIRE_TILE = True if tkinter.TkVersion < 8.5 else False
@@ -381,7 +383,9 @@ class Style(object):
a sequence identifying the value for that option."""
if query_opt is not None:
kw[query_opt] = None
- return _val_or_dict(self.tk, kw, self._name, "configure", style)
+ result = _val_or_dict(self.tk, kw, self._name, "configure", style)
+ if result or query_opt:
+ return result
def map(self, style, query_opt=None, **kw):
@@ -466,12 +470,14 @@ class Style(object):
def element_names(self):
"""Returns the list of elements defined in the current theme."""
- return self.tk.splitlist(self.tk.call(self._name, "element", "names"))
+ return tuple(n.lstrip('-') for n in self.tk.splitlist(
+ self.tk.call(self._name, "element", "names")))
def element_options(self, elementname):
"""Return the list of elementname's options."""
- return self.tk.splitlist(self.tk.call(self._name, "element", "options", elementname))
+ return tuple(o.lstrip('-') for o in self.tk.splitlist(
+ self.tk.call(self._name, "element", "options", elementname)))
def theme_create(self, themename, parent=None, settings=None):
@@ -1390,31 +1396,53 @@ class Treeview(Widget, tkinter.XView, tkinter.YView):
self.tk.call(self._w, "see", item)
- def selection(self, selop=None, items=None):
- """If selop is not specified, returns selected items."""
- if isinstance(items, (str, bytes)):
- items = (items,)
+ def selection(self, selop=_sentinel, items=None):
+ """Returns the tuple of selected items."""
+ if selop is _sentinel:
+ selop = None
+ elif selop is None:
+ import warnings
+ warnings.warn(
+ "The selop=None argument of selection() is deprecated "
+ "and will be removed in Python 3.7",
+ DeprecationWarning, 3)
+ elif selop in ('set', 'add', 'remove', 'toggle'):
+ import warnings
+ warnings.warn(
+ "The selop argument of selection() is deprecated "
+ "and will be removed in Python 3.7, "
+ "use selection_%s() instead" % (selop,),
+ DeprecationWarning, 3)
+ else:
+ raise TypeError('Unsupported operation')
return self.tk.splitlist(self.tk.call(self._w, "selection", selop, items))
- def selection_set(self, items):
- """items becomes the new selection."""
- self.selection("set", items)
+ def _selection(self, selop, items):
+ if len(items) == 1 and isinstance(items[0], (tuple, list)):
+ items = items[0]
+
+ self.tk.call(self._w, "selection", selop, items)
+
+
+ def selection_set(self, *items):
+ """The specified items becomes the new selection."""
+ self._selection("set", items)
- def selection_add(self, items):
- """Add items to the selection."""
- self.selection("add", items)
+ def selection_add(self, *items):
+ """Add all of the specified items to the selection."""
+ self._selection("add", items)
- def selection_remove(self, items):
- """Remove items from the selection."""
- self.selection("remove", items)
+ def selection_remove(self, *items):
+ """Remove all of the specified items from the selection."""
+ self._selection("remove", items)
- def selection_toggle(self, items):
- """Toggle the selection state of each item in items."""
- self.selection("toggle", items)
+ def selection_toggle(self, *items):
+ """Toggle the selection state of each specified item."""
+ self._selection("toggle", items)
def set(self, item, column=None, value=None):
diff --git a/Lib/tokenize.py b/Lib/tokenize.py
index b1d0c83..825aa90 100644
--- a/Lib/tokenize.py
+++ b/Lib/tokenize.py
@@ -29,6 +29,7 @@ from codecs import lookup, BOM_UTF8
import collections
from io import TextIOWrapper
from itertools import chain
+import itertools as _itertools
import re
import sys
from token import *
@@ -119,19 +120,41 @@ Comment = r'#[^\r\n]*'
Ignore = Whitespace + any(r'\\\r?\n' + Whitespace) + maybe(Comment)
Name = r'\w+'
-Hexnumber = r'0[xX][0-9a-fA-F]+'
-Binnumber = r'0[bB][01]+'
-Octnumber = r'0[oO][0-7]+'
-Decnumber = r'(?:0+|[1-9][0-9]*)'
+Hexnumber = r'0[xX](?:_?[0-9a-fA-F])+'
+Binnumber = r'0[bB](?:_?[01])+'
+Octnumber = r'0[oO](?:_?[0-7])+'
+Decnumber = r'(?:0(?:_?0)*|[1-9](?:_?[0-9])*)'
Intnumber = group(Hexnumber, Binnumber, Octnumber, Decnumber)
-Exponent = r'[eE][-+]?[0-9]+'
-Pointfloat = group(r'[0-9]+\.[0-9]*', r'\.[0-9]+') + maybe(Exponent)
-Expfloat = r'[0-9]+' + Exponent
+Exponent = r'[eE][-+]?[0-9](?:_?[0-9])*'
+Pointfloat = group(r'[0-9](?:_?[0-9])*\.(?:[0-9](?:_?[0-9])*)?',
+ r'\.[0-9](?:_?[0-9])*') + maybe(Exponent)
+Expfloat = r'[0-9](?:_?[0-9])*' + Exponent
Floatnumber = group(Pointfloat, Expfloat)
-Imagnumber = group(r'[0-9]+[jJ]', Floatnumber + r'[jJ]')
+Imagnumber = group(r'[0-9](?:_?[0-9])*[jJ]', Floatnumber + r'[jJ]')
Number = group(Imagnumber, Floatnumber, Intnumber)
-StringPrefix = r'(?:[bB][rR]?|[rR][bB]?|[uU])?'
+# Return the empty string, plus all of the valid string prefixes.
+def _all_string_prefixes():
+ # The valid string prefixes. Only contain the lower case versions,
+ # and don't contain any permuations (include 'fr', but not
+ # 'rf'). The various permutations will be generated.
+ _valid_string_prefixes = ['b', 'r', 'u', 'f', 'br', 'fr']
+ # if we add binary f-strings, add: ['fb', 'fbr']
+ result = set([''])
+ for prefix in _valid_string_prefixes:
+ for t in _itertools.permutations(prefix):
+ # create a list with upper and lower versions of each
+ # character
+ for u in _itertools.product(*[(c, c.upper()) for c in t]):
+ result.add(''.join(u))
+ return result
+
+def _compile(expr):
+ return re.compile(expr, re.UNICODE)
+
+# Note that since _all_string_prefixes includes the empty string,
+# StringPrefix can be the empty string (making it optional).
+StringPrefix = group(*_all_string_prefixes())
# Tail end of ' string.
Single = r"[^'\\]*(?:\\.[^'\\]*)*'"
@@ -169,50 +192,25 @@ ContStr = group(StringPrefix + r"'[^\n'\\]*(?:\\.[^\n'\\]*)*" +
PseudoExtras = group(r'\\\r?\n|\Z', Comment, Triple)
PseudoToken = Whitespace + group(PseudoExtras, Number, Funny, ContStr, Name)
-def _compile(expr):
- return re.compile(expr, re.UNICODE)
-
-endpats = {"'": Single, '"': Double,
- "'''": Single3, '"""': Double3,
- "r'''": Single3, 'r"""': Double3,
- "b'''": Single3, 'b"""': Double3,
- "R'''": Single3, 'R"""': Double3,
- "B'''": Single3, 'B"""': Double3,
- "br'''": Single3, 'br"""': Double3,
- "bR'''": Single3, 'bR"""': Double3,
- "Br'''": Single3, 'Br"""': Double3,
- "BR'''": Single3, 'BR"""': Double3,
- "rb'''": Single3, 'rb"""': Double3,
- "Rb'''": Single3, 'Rb"""': Double3,
- "rB'''": Single3, 'rB"""': Double3,
- "RB'''": Single3, 'RB"""': Double3,
- "u'''": Single3, 'u"""': Double3,
- "U'''": Single3, 'U"""': Double3,
- 'r': None, 'R': None, 'b': None, 'B': None,
- 'u': None, 'U': None}
-
-triple_quoted = {}
-for t in ("'''", '"""',
- "r'''", 'r"""', "R'''", 'R"""',
- "b'''", 'b"""', "B'''", 'B"""',
- "br'''", 'br"""', "Br'''", 'Br"""',
- "bR'''", 'bR"""', "BR'''", 'BR"""',
- "rb'''", 'rb"""', "rB'''", 'rB"""',
- "Rb'''", 'Rb"""', "RB'''", 'RB"""',
- "u'''", 'u"""', "U'''", 'U"""',
- ):
- triple_quoted[t] = t
-single_quoted = {}
-for t in ("'", '"',
- "r'", 'r"', "R'", 'R"',
- "b'", 'b"', "B'", 'B"',
- "br'", 'br"', "Br'", 'Br"',
- "bR'", 'bR"', "BR'", 'BR"' ,
- "rb'", 'rb"', "rB'", 'rB"',
- "Rb'", 'Rb"', "RB'", 'RB"' ,
- "u'", 'u"', "U'", 'U"',
- ):
- single_quoted[t] = t
+# For a given string prefix plus quotes, endpats maps it to a regex
+# to match the remainder of that string. _prefix can be empty, for
+# a normal single or triple quoted string (with no prefix).
+endpats = {}
+for _prefix in _all_string_prefixes():
+ endpats[_prefix + "'"] = Single
+ endpats[_prefix + '"'] = Double
+ endpats[_prefix + "'''"] = Single3
+ endpats[_prefix + '"""'] = Double3
+
+# A set of all of the single and triple quoted string prefixes,
+# including the opening quotes.
+single_quoted = set()
+triple_quoted = set()
+for t in _all_string_prefixes():
+ for u in (t + '"', t + "'"):
+ single_quoted.add(u)
+ for u in (t + '"""', t + "'''"):
+ triple_quoted.add(u)
tabsize = 8
@@ -626,6 +624,7 @@ def _tokenize(readline, encoding):
yield stashed
stashed = None
yield TokenInfo(COMMENT, token, spos, epos, line)
+
elif token in triple_quoted:
endprog = _compile(endpats[token])
endmatch = endprog.match(line, pos)
@@ -638,19 +637,37 @@ def _tokenize(readline, encoding):
contstr = line[start:]
contline = line
break
- elif initial in single_quoted or \
- token[:2] in single_quoted or \
- token[:3] in single_quoted:
+
+ # Check up to the first 3 chars of the token to see if
+ # they're in the single_quoted set. If so, they start
+ # a string.
+ # We're using the first 3, because we're looking for
+ # "rb'" (for example) at the start of the token. If
+ # we switch to longer prefixes, this needs to be
+ # adjusted.
+ # Note that initial == token[:1].
+ # Also note that single quote checking must come after
+ # triple quote checking (above).
+ elif (initial in single_quoted or
+ token[:2] in single_quoted or
+ token[:3] in single_quoted):
if token[-1] == '\n': # continued string
strstart = (lnum, start)
- endprog = _compile(endpats[initial] or
- endpats[token[1]] or
- endpats[token[2]])
+ # Again, using the first 3 chars of the
+ # token. This is looking for the matching end
+ # regex for the correct type of quote
+ # character. So it's really looking for
+ # endpats["'"] or endpats['"'], by trying to
+ # skip string prefix characters, if any.
+ endprog = _compile(endpats.get(initial) or
+ endpats.get(token[1]) or
+ endpats.get(token[2]))
contstr, needcont = line[start:], 1
contline = line
break
else: # ordinary string
yield TokenInfo(STRING, token, spos, epos, line)
+
elif initial.isidentifier(): # ordinary name
if token in ('async', 'await'):
if async_def:
diff --git a/Lib/trace.py b/Lib/trace.py
index f108266..ae15461 100755
--- a/Lib/trace.py
+++ b/Lib/trace.py
@@ -48,6 +48,7 @@ Sample use, programmatically
r.write_results(show_missing=True, coverdir="/tmp")
"""
__all__ = ['Trace', 'CoverageResults']
+import argparse
import linecache
import os
import re
@@ -58,7 +59,6 @@ import inspect
import gc
import dis
import pickle
-from warnings import warn as _warn
from time import monotonic as _time
try:
@@ -77,51 +77,6 @@ else:
sys.settrace(None)
threading.settrace(None)
-def _usage(outfile):
- outfile.write("""Usage: %s [OPTIONS] <file> [ARGS]
-
-Meta-options:
---help Display this help then exit.
---version Output version information then exit.
-
-Otherwise, exactly one of the following three options must be given:
--t, --trace Print each line to sys.stdout before it is executed.
--c, --count Count the number of times each line is executed
- and write the counts to <module>.cover for each
- module executed, in the module's directory.
- See also `--coverdir', `--file', `--no-report' below.
--l, --listfuncs Keep track of which functions are executed at least
- once and write the results to sys.stdout after the
- program exits.
--T, --trackcalls Keep track of caller/called pairs and write the
- results to sys.stdout after the program exits.
--r, --report Generate a report from a counts file; do not execute
- any code. `--file' must specify the results file to
- read, which must have been created in a previous run
- with `--count --file=FILE'.
-
-Modifiers:
--f, --file=<file> File to accumulate counts over several runs.
--R, --no-report Do not generate the coverage report files.
- Useful if you want to accumulate over several runs.
--C, --coverdir=<dir> Directory where the report files. The coverage
- report for <package>.<module> is written to file
- <dir>/<package>/<module>.cover.
--m, --missing Annotate executable lines that were not executed
- with '>>>>>> '.
--s, --summary Write a brief summary on stdout for each file.
- (Can only be used with --count or --report.)
--g, --timing Prefix each line with the time since the program started.
- Only used while tracing.
-
-Filters, may be repeated multiple times:
---ignore-module=<mod> Ignore the given module(s) and its submodules
- (if it is a package). Accepts comma separated
- list of module names
---ignore-dir=<dir> Ignore files in the given directory (multiple
- directories can be joined by os.pathsep).
-""" % sys.argv[0])
-
PRAGMA_NOCOVER = "#pragma NO COVER"
# Simple rx to find lines with no code.
@@ -265,7 +220,13 @@ class CoverageResults:
def write_results(self, show_missing=True, summary=False, coverdir=None):
"""
- @param coverdir
+ Write the coverage results.
+
+ :param show_missing: Show lines that had no hits.
+ :param summary: Include coverage summary per module.
+ :param coverdir: If None, the results of each module are placed in its
+ directory, otherwise it is included in the directory
+ specified.
"""
if self.calledfuncs:
print()
@@ -647,210 +608,135 @@ class Trace:
calledfuncs=self._calledfuncs,
callers=self._callers)
-def _err_exit(msg):
- sys.stderr.write("%s: %s\n" % (sys.argv[0], msg))
- sys.exit(1)
-
-def main(argv=None):
- import getopt
-
- if argv is None:
- argv = sys.argv
+def main():
+
+ parser = argparse.ArgumentParser()
+ parser.add_argument('--version', action='version', version='trace 2.0')
+
+ grp = parser.add_argument_group('Main options',
+ 'One of these (or --report) must be given')
+
+ grp.add_argument('-c', '--count', action='store_true',
+ help='Count the number of times each line is executed and write '
+ 'the counts to <module>.cover for each module executed, in '
+ 'the module\'s directory. See also --coverdir, --file, '
+ '--no-report below.')
+ grp.add_argument('-t', '--trace', action='store_true',
+ help='Print each line to sys.stdout before it is executed')
+ grp.add_argument('-l', '--listfuncs', action='store_true',
+ help='Keep track of which functions are executed at least once '
+ 'and write the results to sys.stdout after the program exits. '
+ 'Cannot be specified alongside --trace or --count.')
+ grp.add_argument('-T', '--trackcalls', action='store_true',
+ help='Keep track of caller/called pairs and write the results to '
+ 'sys.stdout after the program exits.')
+
+ grp = parser.add_argument_group('Modifiers')
+
+ _grp = grp.add_mutually_exclusive_group()
+ _grp.add_argument('-r', '--report', action='store_true',
+ help='Generate a report from a counts file; does not execute any '
+ 'code. --file must specify the results file to read, which '
+ 'must have been created in a previous run with --count '
+ '--file=FILE')
+ _grp.add_argument('-R', '--no-report', action='store_true',
+ help='Do not generate the coverage report files. '
+ 'Useful if you want to accumulate over several runs.')
+
+ grp.add_argument('-f', '--file',
+ help='File to accumulate counts over several runs')
+ grp.add_argument('-C', '--coverdir',
+ help='Directory where the report files go. The coverage report '
+ 'for <package>.<module> will be written to file '
+ '<dir>/<package>/<module>.cover')
+ grp.add_argument('-m', '--missing', action='store_true',
+ help='Annotate executable lines that were not executed with '
+ '">>>>>> "')
+ grp.add_argument('-s', '--summary', action='store_true',
+ help='Write a brief summary for each file to sys.stdout. '
+ 'Can only be used with --count or --report')
+ grp.add_argument('-g', '--timing', action='store_true',
+ help='Prefix each line with the time since the program started. '
+ 'Only used while tracing')
+
+ grp = parser.add_argument_group('Filters',
+ 'Can be specified multiple times')
+ grp.add_argument('--ignore-module', action='append', default=[],
+ help='Ignore the given module(s) and its submodules'
+ '(if it is a package). Accepts comma separated list of '
+ 'module names.')
+ grp.add_argument('--ignore-dir', action='append', default=[],
+ help='Ignore files in the given directory '
+ '(multiple directories can be joined by os.pathsep).')
+
+ parser.add_argument('filename', nargs='?',
+ help='file to run as main program')
+ parser.add_argument('arguments', nargs=argparse.REMAINDER,
+ help='arguments to the program')
+
+ opts = parser.parse_args()
+
+ if opts.ignore_dir:
+ rel_path = 'lib', 'python{0.major}.{0.minor}'.format(sys.version_info)
+ _prefix = os.path.join(sys.base_prefix, *rel_path)
+ _exec_prefix = os.path.join(sys.base_exec_prefix, *rel_path)
+
+ def parse_ignore_dir(s):
+ s = os.path.expanduser(os.path.expandvars(s))
+ s = s.replace('$prefix', _prefix).replace('$exec_prefix', _exec_prefix)
+ return os.path.normpath(s)
+
+ opts.ignore_module = [mod.strip()
+ for i in opts.ignore_module for mod in i.split(',')]
+ opts.ignore_dir = [parse_ignore_dir(s)
+ for i in opts.ignore_dir for s in i.split(os.pathsep)]
+
+ if opts.report:
+ if not opts.file:
+ parser.error('-r/--report requires -f/--file')
+ results = CoverageResults(infile=opts.file, outfile=opts.file)
+ return results.write_results(opts.missing, opts.summary, opts.coverdir)
+
+ if not any([opts.trace, opts.count, opts.listfuncs, opts.trackcalls]):
+ parser.error('must specify one of --trace, --count, --report, '
+ '--listfuncs, or --trackcalls')
+
+ if opts.listfuncs and (opts.count or opts.trace):
+ parser.error('cannot specify both --listfuncs and (--trace or --count)')
+
+ if opts.summary and not opts.count:
+ parser.error('--summary can only be used with --count or --report')
+
+ if opts.filename is None:
+ parser.error('filename is missing: required with the main options')
+
+ sys.argv = opts.filename, *opts.arguments
+ sys.path[0] = os.path.dirname(opts.filename)
+
+ t = Trace(opts.count, opts.trace, countfuncs=opts.listfuncs,
+ countcallers=opts.trackcalls, ignoremods=opts.ignore_module,
+ ignoredirs=opts.ignore_dir, infile=opts.file,
+ outfile=opts.file, timing=opts.timing)
try:
- opts, prog_argv = getopt.getopt(argv[1:], "tcrRf:d:msC:lTg",
- ["help", "version", "trace", "count",
- "report", "no-report", "summary",
- "file=", "missing",
- "ignore-module=", "ignore-dir=",
- "coverdir=", "listfuncs",
- "trackcalls", "timing"])
-
- except getopt.error as msg:
- sys.stderr.write("%s: %s\n" % (sys.argv[0], msg))
- sys.stderr.write("Try `%s --help' for more information\n"
- % sys.argv[0])
- sys.exit(1)
-
- trace = 0
- count = 0
- report = 0
- no_report = 0
- counts_file = None
- missing = 0
- ignore_modules = []
- ignore_dirs = []
- coverdir = None
- summary = 0
- listfuncs = False
- countcallers = False
- timing = False
-
- for opt, val in opts:
- if opt == "--help":
- _usage(sys.stdout)
- sys.exit(0)
-
- if opt == "--version":
- sys.stdout.write("trace 2.0\n")
- sys.exit(0)
-
- if opt == "-T" or opt == "--trackcalls":
- countcallers = True
- continue
-
- if opt == "-l" or opt == "--listfuncs":
- listfuncs = True
- continue
-
- if opt == "-g" or opt == "--timing":
- timing = True
- continue
-
- if opt == "-t" or opt == "--trace":
- trace = 1
- continue
-
- if opt == "-c" or opt == "--count":
- count = 1
- continue
-
- if opt == "-r" or opt == "--report":
- report = 1
- continue
-
- if opt == "-R" or opt == "--no-report":
- no_report = 1
- continue
-
- if opt == "-f" or opt == "--file":
- counts_file = val
- continue
-
- if opt == "-m" or opt == "--missing":
- missing = 1
- continue
-
- if opt == "-C" or opt == "--coverdir":
- coverdir = val
- continue
-
- if opt == "-s" or opt == "--summary":
- summary = 1
- continue
-
- if opt == "--ignore-module":
- for mod in val.split(","):
- ignore_modules.append(mod.strip())
- continue
-
- if opt == "--ignore-dir":
- for s in val.split(os.pathsep):
- s = os.path.expandvars(s)
- # should I also call expanduser? (after all, could use $HOME)
-
- s = s.replace("$prefix",
- os.path.join(sys.base_prefix, "lib",
- "python" + sys.version[:3]))
- s = s.replace("$exec_prefix",
- os.path.join(sys.base_exec_prefix, "lib",
- "python" + sys.version[:3]))
- s = os.path.normpath(s)
- ignore_dirs.append(s)
- continue
-
- assert 0, "Should never get here"
-
- if listfuncs and (count or trace):
- _err_exit("cannot specify both --listfuncs and (--trace or --count)")
-
- if not (count or trace or report or listfuncs or countcallers):
- _err_exit("must specify one of --trace, --count, --report, "
- "--listfuncs, or --trackcalls")
-
- if report and no_report:
- _err_exit("cannot specify both --report and --no-report")
-
- if report and not counts_file:
- _err_exit("--report requires a --file")
-
- if no_report and len(prog_argv) == 0:
- _err_exit("missing name of file to run")
-
- # everything is ready
- if report:
- results = CoverageResults(infile=counts_file, outfile=counts_file)
- results.write_results(missing, summary=summary, coverdir=coverdir)
- else:
- sys.argv = prog_argv
- progname = prog_argv[0]
- sys.path[0] = os.path.split(progname)[0]
-
- t = Trace(count, trace, countfuncs=listfuncs,
- countcallers=countcallers, ignoremods=ignore_modules,
- ignoredirs=ignore_dirs, infile=counts_file,
- outfile=counts_file, timing=timing)
- try:
- with open(progname) as fp:
- code = compile(fp.read(), progname, 'exec')
- # try to emulate __main__ namespace as much as possible
- globs = {
- '__file__': progname,
- '__name__': '__main__',
- '__package__': None,
- '__cached__': None,
- }
- t.runctx(code, globs, globs)
- except OSError as err:
- _err_exit("Cannot run file %r because: %s" % (sys.argv[0], err))
- except SystemExit:
- pass
-
- results = t.results()
-
- if not no_report:
- results.write_results(missing, summary=summary, coverdir=coverdir)
-
-# Deprecated API
-def usage(outfile):
- _warn("The trace.usage() function is deprecated",
- DeprecationWarning, 2)
- _usage(outfile)
-
-class Ignore(_Ignore):
- def __init__(self, modules=None, dirs=None):
- _warn("The class trace.Ignore is deprecated",
- DeprecationWarning, 2)
- _Ignore.__init__(self, modules, dirs)
-
-def modname(path):
- _warn("The trace.modname() function is deprecated",
- DeprecationWarning, 2)
- return _modname(path)
-
-def fullmodname(path):
- _warn("The trace.fullmodname() function is deprecated",
- DeprecationWarning, 2)
- return _fullmodname(path)
-
-def find_lines_from_code(code, strs):
- _warn("The trace.find_lines_from_code() function is deprecated",
- DeprecationWarning, 2)
- return _find_lines_from_code(code, strs)
-
-def find_lines(code, strs):
- _warn("The trace.find_lines() function is deprecated",
- DeprecationWarning, 2)
- return _find_lines(code, strs)
+ with open(opts.filename) as fp:
+ code = compile(fp.read(), opts.filename, 'exec')
+ # try to emulate __main__ namespace as much as possible
+ globs = {
+ '__file__': opts.filename,
+ '__name__': '__main__',
+ '__package__': None,
+ '__cached__': None,
+ }
+ t.runctx(code, globs, globs)
+ except OSError as err:
+ sys.exit("Cannot run file %r because: %s" % (sys.argv[0], err))
+ except SystemExit:
+ pass
-def find_strings(filename, encoding=None):
- _warn("The trace.find_strings() function is deprecated",
- DeprecationWarning, 2)
- return _find_strings(filename, encoding=None)
+ results = t.results()
-def find_executable_linenos(filename):
- _warn("The trace.find_executable_linenos() function is deprecated",
- DeprecationWarning, 2)
- return _find_executable_linenos(filename)
+ if not opts.no_report:
+ results.write_results(opts.missing, opts.summary, opts.coverdir)
if __name__=='__main__':
main()
diff --git a/Lib/traceback.py b/Lib/traceback.py
index c5fd617..09bda71 100644
--- a/Lib/traceback.py
+++ b/Lib/traceback.py
@@ -385,9 +385,30 @@ class StackSummary(list):
resulting list corresponds to a single frame from the stack.
Each string ends in a newline; the strings may contain internal
newlines as well, for those items with source text lines.
+
+ For long sequences of the same frame and line, the first few
+ repetitions are shown, followed by a summary line stating the exact
+ number of further repetitions.
"""
result = []
+ last_file = None
+ last_line = None
+ last_name = None
+ count = 0
for frame in self:
+ if (last_file is not None and last_file == frame.filename and
+ last_line is not None and last_line == frame.lineno and
+ last_name is not None and last_name == frame.name):
+ count += 1
+ else:
+ if count > 3:
+ result.append(f' [Previous line repeated {count-3} more times]\n')
+ last_file = frame.filename
+ last_line = frame.lineno
+ last_name = frame.name
+ count = 0
+ if count >= 3:
+ continue
row = []
row.append(' File "{}", line {}, in {}\n'.format(
frame.filename, frame.lineno, frame.name))
@@ -397,6 +418,8 @@ class StackSummary(list):
for name, value in sorted(frame.locals.items()):
row.append(' {name} = {value}\n'.format(name=name, value=value))
result.append(''.join(row))
+ if count > 3:
+ result.append(f' [Previous line repeated {count-3} more times]\n')
return result
@@ -487,10 +510,9 @@ class TracebackException:
self._load_lines()
@classmethod
- def from_exception(self, exc, *args, **kwargs):
+ def from_exception(cls, exc, *args, **kwargs):
"""Create a TracebackException from an exception."""
- return TracebackException(
- type(exc), exc, exc.__traceback__, *args, **kwargs)
+ return cls(type(exc), exc, exc.__traceback__, *args, **kwargs)
def _load_lines(self):
"""Private API. force all lines in the stack to be loaded."""
diff --git a/Lib/tracemalloc.py b/Lib/tracemalloc.py
index 6288da8..75b3918 100644
--- a/Lib/tracemalloc.py
+++ b/Lib/tracemalloc.py
@@ -244,17 +244,21 @@ class Trace:
__slots__ = ("_trace",)
def __init__(self, trace):
- # trace is a tuple: (size, traceback), see Traceback constructor
- # for the format of the traceback tuple
+ # trace is a tuple: (domain: int, size: int, traceback: tuple).
+ # See Traceback constructor for the format of the traceback tuple.
self._trace = trace
@property
- def size(self):
+ def domain(self):
return self._trace[0]
@property
+ def size(self):
+ return self._trace[1]
+
+ @property
def traceback(self):
- return Traceback(self._trace[1])
+ return Traceback(self._trace[2])
def __eq__(self, other):
return (self._trace == other._trace)
@@ -266,8 +270,8 @@ class Trace:
return "%s: %s" % (self.traceback, _format_size(self.size, False))
def __repr__(self):
- return ("<Trace size=%s, traceback=%r>"
- % (_format_size(self.size, False), self.traceback))
+ return ("<Trace domain=%s size=%s, traceback=%r>"
+ % (self.domain, _format_size(self.size, False), self.traceback))
class _Traces(Sequence):
@@ -302,19 +306,29 @@ def _normalize_filename(filename):
return filename
-class Filter:
+class BaseFilter:
+ def __init__(self, inclusive):
+ self.inclusive = inclusive
+
+ def _match(self, trace):
+ raise NotImplementedError
+
+
+class Filter(BaseFilter):
def __init__(self, inclusive, filename_pattern,
- lineno=None, all_frames=False):
+ lineno=None, all_frames=False, domain=None):
+ super().__init__(inclusive)
self.inclusive = inclusive
self._filename_pattern = _normalize_filename(filename_pattern)
self.lineno = lineno
self.all_frames = all_frames
+ self.domain = domain
@property
def filename_pattern(self):
return self._filename_pattern
- def __match_frame(self, filename, lineno):
+ def _match_frame_impl(self, filename, lineno):
filename = _normalize_filename(filename)
if not fnmatch.fnmatch(filename, self._filename_pattern):
return False
@@ -324,11 +338,11 @@ class Filter:
return (lineno == self.lineno)
def _match_frame(self, filename, lineno):
- return self.__match_frame(filename, lineno) ^ (not self.inclusive)
+ return self._match_frame_impl(filename, lineno) ^ (not self.inclusive)
def _match_traceback(self, traceback):
if self.all_frames:
- if any(self.__match_frame(filename, lineno)
+ if any(self._match_frame_impl(filename, lineno)
for filename, lineno in traceback):
return self.inclusive
else:
@@ -337,6 +351,30 @@ class Filter:
filename, lineno = traceback[0]
return self._match_frame(filename, lineno)
+ def _match(self, trace):
+ domain, size, traceback = trace
+ res = self._match_traceback(traceback)
+ if self.domain is not None:
+ if self.inclusive:
+ return res and (domain == self.domain)
+ else:
+ return res or (domain != self.domain)
+ return res
+
+
+class DomainFilter(BaseFilter):
+ def __init__(self, inclusive, domain):
+ super().__init__(inclusive)
+ self._domain = domain
+
+ @property
+ def domain(self):
+ return self._domain
+
+ def _match(self, trace):
+ domain, size, traceback = trace
+ return (domain == self.domain) ^ (not self.inclusive)
+
class Snapshot:
"""
@@ -365,13 +403,12 @@ class Snapshot:
return pickle.load(fp)
def _filter_trace(self, include_filters, exclude_filters, trace):
- traceback = trace[1]
if include_filters:
- if not any(trace_filter._match_traceback(traceback)
+ if not any(trace_filter._match(trace)
for trace_filter in include_filters):
return False
if exclude_filters:
- if any(not trace_filter._match_traceback(traceback)
+ if any(not trace_filter._match(trace)
for trace_filter in exclude_filters):
return False
return True
@@ -379,8 +416,8 @@ class Snapshot:
def filter_traces(self, filters):
"""
Create a new Snapshot instance with a filtered traces sequence, filters
- is a list of Filter instances. If filters is an empty list, return a
- new Snapshot instance with a copy of the traces.
+ is a list of Filter or DomainFilter instances. If filters is an empty
+ list, return a new Snapshot instance with a copy of the traces.
"""
if not isinstance(filters, Iterable):
raise TypeError("filters must be a list of filters, not %s"
@@ -412,7 +449,7 @@ class Snapshot:
tracebacks = {}
if not cumulative:
for trace in self.traces._traces:
- size, trace_traceback = trace
+ domain, size, trace_traceback = trace
try:
traceback = tracebacks[trace_traceback]
except KeyError:
@@ -433,7 +470,7 @@ class Snapshot:
else:
# cumulative statistics
for trace in self.traces._traces:
- size, trace_traceback = trace
+ domain, size, trace_traceback = trace
for frame in trace_traceback:
try:
traceback = tracebacks[frame]
diff --git a/Lib/turtledemo/__main__.py b/Lib/turtledemo/__main__.py
index 711d0ab..0a58332 100644
--- a/Lib/turtledemo/__main__.py
+++ b/Lib/turtledemo/__main__.py
@@ -89,13 +89,12 @@ import sys
import os
from tkinter import *
-from idlelib.ColorDelegator import ColorDelegator, color_config
-from idlelib.Percolator import Percolator
-from idlelib.textView import view_text
+from idlelib.colorizer import ColorDelegator, color_config
+from idlelib.percolator import Percolator
+from idlelib.textview import view_text
from turtledemo import __doc__ as about_turtledemo
import turtle
-import time
demo_dir = os.path.dirname(os.path.abspath(__file__))
darwin = sys.platform == 'darwin'
diff --git a/Lib/turtledemo/bytedesign.py b/Lib/turtledemo/bytedesign.py
index 64b1d7d..b3b095b 100755
--- a/Lib/turtledemo/bytedesign.py
+++ b/Lib/turtledemo/bytedesign.py
@@ -22,7 +22,6 @@ to 0, this animation runs in "line per line"
mode as fast as possible.
"""
-import math
from turtle import Turtle, mainloop
from time import clock
diff --git a/Lib/turtledemo/planet_and_moon.py b/Lib/turtledemo/planet_and_moon.py
index 26abfdb..021ff99 100755
--- a/Lib/turtledemo/planet_and_moon.py
+++ b/Lib/turtledemo/planet_and_moon.py
@@ -18,7 +18,6 @@ mouse over the scrollbar of the canvas.
"""
from turtle import Shape, Turtle, mainloop, Vec2D as Vec
-from time import sleep
G = 8
diff --git a/Lib/types.py b/Lib/types.py
index 48891cd..d8d8470 100644
--- a/Lib/types.py
+++ b/Lib/types.py
@@ -24,6 +24,11 @@ _c = _c()
CoroutineType = type(_c)
_c.close() # Prevent ResourceWarning
+async def _ag():
+ yield
+_ag = _ag()
+AsyncGeneratorType = type(_ag)
+
class _C:
def _m(self): pass
MethodType = type(_C()._m)
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 0512cca..b6b3836 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -60,18 +60,6 @@ def _is_exception(obj):
)
-class _slotted(object):
- __slots__ = ['a']
-
-
-# Do not use this tuple. It was never documented as a public API.
-# It will be removed. It has no obvious signs of users on github.
-DescriptorTypes = (
- type(_slotted.a),
- property,
-)
-
-
def _get_signature_object(func, as_instance, eat_self):
"""
Given an arbitrary, possibly callable object, try to create a suitable
@@ -205,6 +193,12 @@ def _setup_func(funcopy, mock):
def assert_called_with(*args, **kwargs):
return mock.assert_called_with(*args, **kwargs)
+ def assert_called(*args, **kwargs):
+ return mock.assert_called(*args, **kwargs)
+ def assert_not_called(*args, **kwargs):
+ return mock.assert_not_called(*args, **kwargs)
+ def assert_called_once(*args, **kwargs):
+ return mock.assert_called_once(*args, **kwargs)
def assert_called_once_with(*args, **kwargs):
return mock.assert_called_once_with(*args, **kwargs)
def assert_has_calls(*args, **kwargs):
@@ -235,6 +229,9 @@ def _setup_func(funcopy, mock):
funcopy.assert_has_calls = assert_has_calls
funcopy.assert_any_call = assert_any_call
funcopy.reset_mock = reset_mock
+ funcopy.assert_called = assert_called
+ funcopy.assert_not_called = assert_not_called
+ funcopy.assert_called_once = assert_called_once
mock._mock_delegate = funcopy
@@ -525,7 +522,7 @@ class NonCallableMock(Base):
side_effect = property(__get_side_effect, __set_side_effect)
- def reset_mock(self, visited=None):
+ def reset_mock(self, visited=None,*, return_value=False, side_effect=False):
"Restore the mock object to its initial state."
if visited is None:
visited = []
@@ -540,6 +537,11 @@ class NonCallableMock(Base):
self.call_args_list = _CallList()
self.method_calls = _CallList()
+ if return_value:
+ self._mock_return_value = DEFAULT
+ if side_effect:
+ self._mock_side_effect = None
+
for child in self._mock_children.values():
if isinstance(child, _SpecState):
continue
@@ -774,6 +776,24 @@ class NonCallableMock(Base):
(self._mock_name or 'mock', self.call_count))
raise AssertionError(msg)
+ def assert_called(_mock_self):
+ """assert that the mock was called at least once
+ """
+ self = _mock_self
+ if self.call_count == 0:
+ msg = ("Expected '%s' to have been called." %
+ self._mock_name or 'mock')
+ raise AssertionError(msg)
+
+ def assert_called_once(_mock_self):
+ """assert that the mock was called only once.
+ """
+ self = _mock_self
+ if not self.call_count == 1:
+ msg = ("Expected '%s' to have been called once. Called %s times." %
+ (self._mock_name or 'mock', self.call_count))
+ raise AssertionError(msg)
+
def assert_called_with(_mock_self, *args, **kwargs):
"""assert that the mock was called with the specified arguments.
@@ -822,7 +842,7 @@ class NonCallableMock(Base):
if expected not in all_calls:
raise AssertionError(
'Calls not found.\nExpected: %r\n'
- 'Actual: %r' % (calls, self.mock_calls)
+ 'Actual: %r' % (_CallList(calls), self.mock_calls)
) from cause
return
@@ -1945,9 +1965,8 @@ class _Call(tuple):
If the _Call has no name then it will match any name.
"""
- def __new__(cls, value=(), name=None, parent=None, two=False,
+ def __new__(cls, value=(), name='', parent=None, two=False,
from_kall=True):
- name = ''
args = ()
kwargs = {}
_len = len(value)
diff --git a/Lib/unittest/runner.py b/Lib/unittest/runner.py
index 2112262..2c5ea4a 100644
--- a/Lib/unittest/runner.py
+++ b/Lib/unittest/runner.py
@@ -167,7 +167,7 @@ class TextTestRunner(object):
if self.warnings in ['default', 'always']:
warnings.filterwarnings('module',
category=DeprecationWarning,
- message='Please use assert\w+ instead.')
+ message=r'Please use assert\w+ instead.')
startTime = time.time()
startTestRun = getattr(result, 'startTestRun', None)
if startTestRun is not None:
diff --git a/Lib/unittest/test/test_assertions.py b/Lib/unittest/test/test_assertions.py
index e6e2bc2..1b0e833 100644
--- a/Lib/unittest/test/test_assertions.py
+++ b/Lib/unittest/test/test_assertions.py
@@ -240,7 +240,7 @@ class TestLongMessage(unittest.TestCase):
# Error messages are multiline so not testing on full message
# assertTupleEqual and assertListEqual delegate to this method
self.assertMessages('assertSequenceEqual', ([], [None]),
- ["\+ \[None\]$", "^oops$", r"\+ \[None\]$",
+ [r"\+ \[None\]$", "^oops$", r"\+ \[None\]$",
r"\+ \[None\] : oops$"])
def testAssertSetEqual(self):
@@ -250,21 +250,21 @@ class TestLongMessage(unittest.TestCase):
def testAssertIn(self):
self.assertMessages('assertIn', (None, []),
- ['^None not found in \[\]$', "^oops$",
- '^None not found in \[\]$',
- '^None not found in \[\] : oops$'])
+ [r'^None not found in \[\]$', "^oops$",
+ r'^None not found in \[\]$',
+ r'^None not found in \[\] : oops$'])
def testAssertNotIn(self):
self.assertMessages('assertNotIn', (None, [None]),
- ['^None unexpectedly found in \[None\]$', "^oops$",
- '^None unexpectedly found in \[None\]$',
- '^None unexpectedly found in \[None\] : oops$'])
+ [r'^None unexpectedly found in \[None\]$', "^oops$",
+ r'^None unexpectedly found in \[None\]$',
+ r'^None unexpectedly found in \[None\] : oops$'])
def testAssertDictEqual(self):
self.assertMessages('assertDictEqual', ({}, {'key': 'value'}),
[r"\+ \{'key': 'value'\}$", "^oops$",
- "\+ \{'key': 'value'\}$",
- "\+ \{'key': 'value'\} : oops$"])
+ r"\+ \{'key': 'value'\}$",
+ r"\+ \{'key': 'value'\} : oops$"])
def testAssertDictContainsSubset(self):
with warnings.catch_warnings():
diff --git a/Lib/unittest/test/test_loader.py b/Lib/unittest/test/test_loader.py
index 31e2f0f..1131a75 100644
--- a/Lib/unittest/test/test_loader.py
+++ b/Lib/unittest/test/test_loader.py
@@ -390,7 +390,7 @@ class Test_TestLoader(unittest.TestCase):
suite = loader.loadTestsFromName('abc () //')
error, test = self.check_deferred_error(loader, suite)
expected = "Failed to import test module: abc () //"
- expected_regex = "Failed to import test module: abc \(\) //"
+ expected_regex = r"Failed to import test module: abc \(\) //"
self.assertIn(
expected, error,
'missing error string in %r' % error)
@@ -502,7 +502,7 @@ class Test_TestLoader(unittest.TestCase):
suite = loader.loadTestsFromName('abc () //', unittest)
error, test = self.check_deferred_error(loader, suite)
expected = "module 'unittest' has no attribute 'abc () //'"
- expected_regex = "module 'unittest' has no attribute 'abc \(\) //'"
+ expected_regex = r"module 'unittest' has no attribute 'abc \(\) //'"
self.assertIn(
expected, error,
'missing error string in %r' % error)
@@ -809,7 +809,7 @@ class Test_TestLoader(unittest.TestCase):
suite = loader.loadTestsFromNames(['abc () //'])
error, test = self.check_deferred_error(loader, list(suite)[0])
expected = "Failed to import test module: abc () //"
- expected_regex = "Failed to import test module: abc \(\) //"
+ expected_regex = r"Failed to import test module: abc \(\) //"
self.assertIn(
expected, error,
'missing error string in %r' % error)
@@ -928,7 +928,7 @@ class Test_TestLoader(unittest.TestCase):
suite = loader.loadTestsFromNames(['abc () //'], unittest)
error, test = self.check_deferred_error(loader, list(suite)[0])
expected = "module 'unittest' has no attribute 'abc () //'"
- expected_regex = "module 'unittest' has no attribute 'abc \(\) //'"
+ expected_regex = r"module 'unittest' has no attribute 'abc \(\) //'"
self.assertIn(
expected, error,
'missing error string in %r' % error)
diff --git a/Lib/unittest/test/testmock/support.py b/Lib/unittest/test/testmock/support.py
index f473879..205431a 100644
--- a/Lib/unittest/test/testmock/support.py
+++ b/Lib/unittest/test/testmock/support.py
@@ -1,5 +1,3 @@
-import sys
-
def is_instance(obj, klass):
"""Version of is_instance that doesn't access __class__"""
return issubclass(type(obj), klass)
diff --git a/Lib/unittest/test/testmock/testhelpers.py b/Lib/unittest/test/testmock/testhelpers.py
index 3477634..d5f9e7c 100644
--- a/Lib/unittest/test/testmock/testhelpers.py
+++ b/Lib/unittest/test/testmock/testhelpers.py
@@ -306,6 +306,20 @@ class CallTest(unittest.TestCase):
other_args = _Call(((1, 2), {'a': 3}))
self.assertEqual(args, other_args)
+ def test_call_with_name(self):
+ self.assertEqual(
+ 'foo',
+ _Call((), 'foo')[0],
+ )
+ self.assertEqual(
+ '',
+ _Call((('bar', 'barz'), ), )[0]
+ )
+ self.assertEqual(
+ '',
+ _Call((('bar', 'barz'), {'hello': 'world'}), )[0]
+ )
+
class SpecSignatureTest(unittest.TestCase):
diff --git a/Lib/unittest/test/testmock/testmagicmethods.py b/Lib/unittest/test/testmock/testmagicmethods.py
index bb9b956..24569b5 100644
--- a/Lib/unittest/test/testmock/testmagicmethods.py
+++ b/Lib/unittest/test/testmock/testmagicmethods.py
@@ -1,5 +1,4 @@
import unittest
-import inspect
import sys
from unittest.mock import Mock, MagicMock, _magics
diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py
index a96ec68..b64c866 100644
--- a/Lib/unittest/test/testmock/testmock.py
+++ b/Lib/unittest/test/testmock/testmock.py
@@ -1250,6 +1250,27 @@ class MockTest(unittest.TestCase):
with self.assertRaises(AssertionError):
m.hello.assert_not_called()
+ def test_assert_called(self):
+ m = Mock()
+ with self.assertRaises(AssertionError):
+ m.hello.assert_called()
+ m.hello()
+ m.hello.assert_called()
+
+ m.hello()
+ m.hello.assert_called()
+
+ def test_assert_called_once(self):
+ m = Mock()
+ with self.assertRaises(AssertionError):
+ m.hello.assert_called_once()
+ m.hello()
+ m.hello.assert_called_once()
+
+ m.hello()
+ with self.assertRaises(AssertionError):
+ m.hello.assert_called_once()
+
#Issue21256 printout of keyword args should be in deterministic order
def test_sorted_call_signature(self):
m = Mock()
@@ -1267,6 +1288,24 @@ class MockTest(unittest.TestCase):
self.assertEqual(m.method_calls[0], c)
self.assertEqual(m.method_calls[1], i)
+ def test_reset_return_sideeffect(self):
+ m = Mock(return_value=10, side_effect=[2,3])
+ m.reset_mock(return_value=True, side_effect=True)
+ self.assertIsInstance(m.return_value, Mock)
+ self.assertEqual(m.side_effect, None)
+
+ def test_reset_return(self):
+ m = Mock(return_value=10, side_effect=[2,3])
+ m.reset_mock(return_value=True)
+ self.assertIsInstance(m.return_value, Mock)
+ self.assertNotEqual(m.side_effect, None)
+
+ def test_reset_sideeffect(self):
+ m = Mock(return_value=10, side_effect=[2,3])
+ m.reset_mock(side_effect=True)
+ self.assertEqual(m.return_value, 10)
+ self.assertEqual(m.side_effect, None)
+
def test_mock_add_spec(self):
class _One(object):
one = 1
diff --git a/Lib/unittest/test/testmock/testpatch.py b/Lib/unittest/test/testmock/testpatch.py
index dfce369..fe4ecef 100644
--- a/Lib/unittest/test/testmock/testpatch.py
+++ b/Lib/unittest/test/testmock/testpatch.py
@@ -10,9 +10,9 @@ from unittest.test.testmock import support
from unittest.test.testmock.support import SomeClass, is_instance
from unittest.mock import (
- NonCallableMock, CallableMixin, patch, sentinel,
+ NonCallableMock, CallableMixin, sentinel,
MagicMock, Mock, NonCallableMagicMock, patch, _patch,
- DEFAULT, call, _get_target, _patch
+ DEFAULT, call, _get_target
)
@@ -969,8 +969,14 @@ class PatchTest(unittest.TestCase):
def test_autospec_function(self):
@patch('%s.function' % __name__, autospec=True)
def test(mock):
+ function.assert_not_called()
+ self.assertRaises(AssertionError, function.assert_called)
+ self.assertRaises(AssertionError, function.assert_called_once)
function(1)
+ self.assertRaises(AssertionError, function.assert_not_called)
function.assert_called_with(1)
+ function.assert_called()
+ function.assert_called_once()
function(2, 3)
function.assert_called_with(2, 3)
diff --git a/Lib/urllib/parse.py b/Lib/urllib/parse.py
index 566fbf7..958767a 100644
--- a/Lib/urllib/parse.py
+++ b/Lib/urllib/parse.py
@@ -157,9 +157,8 @@ class _NetlocResultMixinBase(object):
port = self._hostinfo[1]
if port is not None:
port = int(port, 10)
- # Return None on an illegal port
if not ( 0 <= port <= 65535):
- return None
+ raise ValueError("Port out of range 0-65535")
return port
@@ -226,8 +225,71 @@ class _NetlocResultMixinBytes(_NetlocResultMixinBase, _ResultMixinBytes):
from collections import namedtuple
_DefragResultBase = namedtuple('DefragResult', 'url fragment')
-_SplitResultBase = namedtuple('SplitResult', 'scheme netloc path query fragment')
-_ParseResultBase = namedtuple('ParseResult', 'scheme netloc path params query fragment')
+_SplitResultBase = namedtuple(
+ 'SplitResult', 'scheme netloc path query fragment')
+_ParseResultBase = namedtuple(
+ 'ParseResult', 'scheme netloc path params query fragment')
+
+_DefragResultBase.__doc__ = """
+DefragResult(url, fragment)
+
+A 2-tuple that contains the url without fragment identifier and the fragment
+identifier as a separate argument.
+"""
+
+_DefragResultBase.url.__doc__ = """The URL with no fragment identifier."""
+
+_DefragResultBase.fragment.__doc__ = """
+Fragment identifier separated from URL, that allows indirect identification of a
+secondary resource by reference to a primary resource and additional identifying
+information.
+"""
+
+_SplitResultBase.__doc__ = """
+SplitResult(scheme, netloc, path, query, fragment)
+
+A 5-tuple that contains the different components of a URL. Similar to
+ParseResult, but does not split params.
+"""
+
+_SplitResultBase.scheme.__doc__ = """Specifies URL scheme for the request."""
+
+_SplitResultBase.netloc.__doc__ = """
+Network location where the request is made to.
+"""
+
+_SplitResultBase.path.__doc__ = """
+The hierarchical path, such as the path to a file to download.
+"""
+
+_SplitResultBase.query.__doc__ = """
+The query component, that contains non-hierarchical data, that along with data
+in path component, identifies a resource in the scope of URI's scheme and
+network location.
+"""
+
+_SplitResultBase.fragment.__doc__ = """
+Fragment identifier, that allows indirect identification of a secondary resource
+by reference to a primary resource and additional identifying information.
+"""
+
+_ParseResultBase.__doc__ = """
+ParseResult(scheme, netloc, path, params, query, fragment)
+
+A 6-tuple that contains components of a parsed URL.
+"""
+
+_ParseResultBase.scheme.__doc__ = _SplitResultBase.scheme.__doc__
+_ParseResultBase.netloc.__doc__ = _SplitResultBase.netloc.__doc__
+_ParseResultBase.path.__doc__ = _SplitResultBase.path.__doc__
+_ParseResultBase.params.__doc__ = """
+Parameters for last path element used to dereference the URI in order to provide
+access to perform some operation on the resource.
+"""
+
+_ParseResultBase.query.__doc__ = _SplitResultBase.query.__doc__
+_ParseResultBase.fragment.__doc__ = _SplitResultBase.fragment.__doc__
+
# For backwards compatibility, alias _NetlocResultMixinStr
# ResultBase is no longer part of the documented API, but it is
diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py
index a46c689..b6690c3 100644
--- a/Lib/urllib/request.py
+++ b/Lib/urllib/request.py
@@ -134,13 +134,73 @@ __all__ = [
]
# used in User-Agent header sent
-__version__ = sys.version[:3]
+__version__ = '%d.%d' % sys.version_info[:2]
_opener = None
def urlopen(url, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
*, cafile=None, capath=None, cadefault=False, context=None):
+ '''Open the URL url, which can be either a string or a Request object.
+
+ *data* must be an object specifying additional data to be sent to
+ the server, or None if no such data is needed. See Request for
+ details.
+
+ urllib.request module uses HTTP/1.1 and includes a "Connection:close"
+ header in its HTTP requests.
+
+ The optional *timeout* parameter specifies a timeout in seconds for
+ blocking operations like the connection attempt (if not specified, the
+ global default timeout setting will be used). This only works for HTTP,
+ HTTPS and FTP connections.
+
+ If *context* is specified, it must be a ssl.SSLContext instance describing
+ the various SSL options. See HTTPSConnection for more details.
+
+ The optional *cafile* and *capath* parameters specify a set of trusted CA
+ certificates for HTTPS requests. cafile should point to a single file
+ containing a bundle of CA certificates, whereas capath should point to a
+ directory of hashed certificate files. More information can be found in
+ ssl.SSLContext.load_verify_locations().
+
+ The *cadefault* parameter is ignored.
+
+ This function always returns an object which can work as a context
+ manager and has methods such as
+
+ * geturl() - return the URL of the resource retrieved, commonly used to
+ determine if a redirect was followed
+
+ * info() - return the meta-information of the page, such as headers, in the
+ form of an email.message_from_string() instance (see Quick Reference to
+ HTTP Headers)
+
+ * getcode() - return the HTTP status code of the response. Raises URLError
+ on errors.
+
+ For HTTP and HTTPS URLs, this function returns a http.client.HTTPResponse
+ object slightly modified. In addition to the three new methods above, the
+ msg attribute contains the same information as the reason attribute ---
+ the reason phrase returned by the server --- instead of the response
+ headers as it is specified in the documentation for HTTPResponse.
+
+ For FTP, file, and data URLs and requests explicitly handled by legacy
+ URLopener and FancyURLopener classes, this function returns a
+ urllib.response.addinfourl object.
+
+ Note that None may be returned if no handler handles the request (though
+ the default installed global OpenerDirector uses UnknownHandler to ensure
+ this never happens).
+
+ In addition, if proxy settings are detected (for example, when a *_proxy
+ environment variable like http_proxy is set), ProxyHandler is default
+ installed and makes sure the requests are handled through the proxy.
+
+ '''
global _opener
if cafile or capath or cadefault:
+ import warnings
+ warnings.warn("cafile, cpath and cadefault are deprecated, use a "
+ "custom context instead.", DeprecationWarning, 2)
if context is not None:
raise ValueError(
"You can't pass both context and any of cafile, capath, and "
@@ -1170,6 +1230,11 @@ class AbstractHTTPHandler(BaseHandler):
def set_http_debuglevel(self, level):
self._debuglevel = level
+ def _get_content_length(self, request):
+ return http.client.HTTPConnection._get_content_length(
+ request.data,
+ request.get_method())
+
def do_request_(self, request):
host = request.host
if not host:
@@ -1178,24 +1243,22 @@ class AbstractHTTPHandler(BaseHandler):
if request.data is not None: # POST
data = request.data
if isinstance(data, str):
- msg = "POST data should be bytes or an iterable of bytes. " \
- "It cannot be of type str."
+ msg = "POST data should be bytes, an iterable of bytes, " \
+ "or a file object. It cannot be of type str."
raise TypeError(msg)
if not request.has_header('Content-type'):
request.add_unredirected_header(
'Content-type',
'application/x-www-form-urlencoded')
- if not request.has_header('Content-length'):
- try:
- mv = memoryview(data)
- except TypeError:
- if isinstance(data, collections.Iterable):
- raise ValueError("Content-Length should be specified "
- "for iterable data of type %r %r" % (type(data),
- data))
+ if (not request.has_header('Content-length')
+ and not request.has_header('Transfer-encoding')):
+ content_length = self._get_content_length(request)
+ if content_length is not None:
+ request.add_unredirected_header(
+ 'Content-length', str(content_length))
else:
request.add_unredirected_header(
- 'Content-length', '%d' % (len(mv) * mv.itemsize))
+ 'Transfer-encoding', 'chunked')
sel_host = host
if request.has_proxy():
@@ -1251,7 +1314,8 @@ class AbstractHTTPHandler(BaseHandler):
try:
try:
- h.request(req.get_method(), req.selector, req.data, headers)
+ h.request(req.get_method(), req.selector, req.data, headers,
+ encode_chunked=req.has_header('Transfer-encoding'))
except OSError as err: # timeout error
raise URLError(err)
r = h.getresponse()
diff --git a/Lib/urllib/robotparser.py b/Lib/urllib/robotparser.py
index 8b69fd9..9dab4c1 100644
--- a/Lib/urllib/robotparser.py
+++ b/Lib/urllib/robotparser.py
@@ -10,7 +10,9 @@
http://www.robotstxt.org/norobots-rfc.txt
"""
-import urllib.parse, urllib.request
+import collections
+import urllib.parse
+import urllib.request
__all__ = ["RobotFileParser"]
@@ -120,10 +122,29 @@ class RobotFileParser:
if state != 0:
entry.rulelines.append(RuleLine(line[1], True))
state = 2
+ elif line[0] == "crawl-delay":
+ if state != 0:
+ # before trying to convert to int we need to make
+ # sure that robots.txt has valid syntax otherwise
+ # it will crash
+ if line[1].strip().isdigit():
+ entry.delay = int(line[1])
+ state = 2
+ elif line[0] == "request-rate":
+ if state != 0:
+ numbers = line[1].split('/')
+ # check if all values are sane
+ if (len(numbers) == 2 and numbers[0].strip().isdigit()
+ and numbers[1].strip().isdigit()):
+ req_rate = collections.namedtuple('req_rate',
+ 'requests seconds')
+ entry.req_rate = req_rate
+ entry.req_rate.requests = int(numbers[0])
+ entry.req_rate.seconds = int(numbers[1])
+ state = 2
if state == 2:
self._add_entry(entry)
-
def can_fetch(self, useragent, url):
"""using the parsed robots.txt decide if useragent can fetch url"""
if self.disallow_all:
@@ -153,6 +174,22 @@ class RobotFileParser:
# agent not found ==> access granted
return True
+ def crawl_delay(self, useragent):
+ if not self.mtime():
+ return None
+ for entry in self.entries:
+ if entry.applies_to(useragent):
+ return entry.delay
+ return self.default_entry.delay
+
+ def request_rate(self, useragent):
+ if not self.mtime():
+ return None
+ for entry in self.entries:
+ if entry.applies_to(useragent):
+ return entry.req_rate
+ return self.default_entry.req_rate
+
def __str__(self):
return ''.join([str(entry) + "\n" for entry in self.entries])
@@ -180,6 +217,8 @@ class Entry:
def __init__(self):
self.useragents = []
self.rulelines = []
+ self.delay = None
+ self.req_rate = None
def __str__(self):
ret = []
diff --git a/Lib/uuid.py b/Lib/uuid.py
index e96e7e0..200c800 100644
--- a/Lib/uuid.py
+++ b/Lib/uuid.py
@@ -487,7 +487,6 @@ try:
# Assume that the uuid_generate functions are broken from 10.5 onward,
# the test can be adjusted when a later version is fixed.
if sys.platform == 'darwin':
- import os
if int(os.uname().release.split('.')[0]) >= 9:
_uuid_generate_time = None
diff --git a/Lib/venv/__init__.py b/Lib/venv/__init__.py
index 74245ab..0a094e3 100644
--- a/Lib/venv/__init__.py
+++ b/Lib/venv/__init__.py
@@ -3,28 +3,6 @@ Virtual environment (venv) package for Python. Based on PEP 405.
Copyright (C) 2011-2014 Vinay Sajip.
Licensed to the PSF under a contributor agreement.
-
-usage: python -m venv [-h] [--system-site-packages] [--symlinks] [--clear]
- [--upgrade]
- ENV_DIR [ENV_DIR ...]
-
-Creates virtual Python environments in one or more target directories.
-
-positional arguments:
- ENV_DIR A directory to create the environment in.
-
-optional arguments:
- -h, --help show this help message and exit
- --system-site-packages
- Give the virtual environment access to the system
- site-packages dir.
- --symlinks Attempt to symlink rather than copy.
- --clear Delete the contents of the environment directory if it
- already exists, before environment creation.
- --upgrade Upgrade the environment directory to use this version
- of Python, assuming Python has been upgraded in-place.
- --without-pip Skips installing or upgrading pip in the virtual
- environment (pip is bootstrapped by default)
"""
import logging
import os
@@ -58,15 +36,17 @@ class EnvBuilder:
:param upgrade: If True, upgrade an existing virtual environment.
:param with_pip: If True, ensure pip is installed in the virtual
environment
+ :param prompt: Alternative terminal prefix for the environment.
"""
def __init__(self, system_site_packages=False, clear=False,
- symlinks=False, upgrade=False, with_pip=False):
+ symlinks=False, upgrade=False, with_pip=False, prompt=None):
self.system_site_packages = system_site_packages
self.clear = clear
self.symlinks = symlinks
self.upgrade = upgrade
self.with_pip = with_pip
+ self.prompt = prompt
def create(self, env_dir):
"""
@@ -112,7 +92,8 @@ class EnvBuilder:
context = types.SimpleNamespace()
context.env_dir = env_dir
context.env_name = os.path.split(env_dir)[1]
- context.prompt = '(%s) ' % context.env_name
+ prompt = self.prompt if self.prompt is not None else context.env_name
+ context.prompt = '(%s) ' % prompt
create_if_needed(env_dir)
env = os.environ
if sys.platform == 'darwin' and '__PYVENV_LAUNCHER__' in env:
@@ -348,26 +329,11 @@ class EnvBuilder:
def create(env_dir, system_site_packages=False, clear=False,
- symlinks=False, with_pip=False):
- """
- Create a virtual environment in a directory.
-
- By default, makes the system (global) site-packages dir *un*available to
- the created environment, and uses copying rather than symlinking for files
- obtained from the source Python installation.
-
- :param env_dir: The target directory to create an environment in.
- :param system_site_packages: If True, the system (global) site-packages
- dir is available to the environment.
- :param clear: If True, delete the contents of the environment directory if
- it already exists, before environment creation.
- :param symlinks: If True, attempt to symlink rather than copy files into
- virtual environment.
- :param with_pip: If True, ensure pip is installed in the virtual
- environment
- """
+ symlinks=False, with_pip=False, prompt=None):
+ """Create a virtual environment in a directory."""
builder = EnvBuilder(system_site_packages=system_site_packages,
- clear=clear, symlinks=symlinks, with_pip=with_pip)
+ clear=clear, symlinks=symlinks, with_pip=with_pip,
+ prompt=prompt)
builder.create(env_dir)
def main(args=None):
@@ -427,6 +393,9 @@ def main(args=None):
help='Skips installing or upgrading pip in the '
'virtual environment (pip is bootstrapped '
'by default)')
+ parser.add_argument('--prompt',
+ help='Provides an alternative prompt prefix for '
+ 'this environment.')
options = parser.parse_args(args)
if options.upgrade and options.clear:
raise ValueError('you cannot supply --upgrade and --clear together.')
@@ -434,7 +403,8 @@ def main(args=None):
clear=options.clear,
symlinks=options.symlinks,
upgrade=options.upgrade,
- with_pip=options.with_pip)
+ with_pip=options.with_pip,
+ prompt=options.prompt)
for d in options.dirs:
builder.create(d)
diff --git a/Lib/warnings.py b/Lib/warnings.py
index c6631fc..5badb0b 100644
--- a/Lib/warnings.py
+++ b/Lib/warnings.py
@@ -2,39 +2,120 @@
import sys
+
__all__ = ["warn", "warn_explicit", "showwarning",
"formatwarning", "filterwarnings", "simplefilter",
"resetwarnings", "catch_warnings"]
-
def showwarning(message, category, filename, lineno, file=None, line=None):
"""Hook to write a warning to a file; replace if you like."""
+ msg = WarningMessage(message, category, filename, lineno, file, line)
+ _showwarnmsg_impl(msg)
+
+def formatwarning(message, category, filename, lineno, line=None):
+ """Function to format a warning the standard way."""
+ msg = WarningMessage(message, category, filename, lineno, None, line)
+ return _formatwarnmsg_impl(msg)
+
+def _showwarnmsg_impl(msg):
+ file = msg.file
if file is None:
file = sys.stderr
if file is None:
- # sys.stderr is None when run with pythonw.exe - warnings get lost
+ # sys.stderr is None when run with pythonw.exe:
+ # warnings get lost
return
+ text = _formatwarnmsg(msg)
try:
- file.write(formatwarning(message, category, filename, lineno, line))
+ file.write(text)
except OSError:
- pass # the file (probably stderr) is invalid - this warning gets lost.
+ # the file (probably stderr) is invalid - this warning gets lost.
+ pass
-def formatwarning(message, category, filename, lineno, line=None):
- """Function to format a warning the standard way."""
- s = "%s:%s: %s: %s\n" % (filename, lineno, category.__name__, message)
- if line is None:
+def _formatwarnmsg_impl(msg):
+ s = ("%s:%s: %s: %s\n"
+ % (msg.filename, msg.lineno, msg.category.__name__,
+ msg.message))
+
+ if msg.line is None:
try:
import linecache
- line = linecache.getline(filename, lineno)
+ line = linecache.getline(msg.filename, msg.lineno)
except Exception:
# When a warning is logged during Python shutdown, linecache
# and the import machinery don't work anymore
line = None
+ linecache = None
+ else:
+ line = msg.line
if line:
line = line.strip()
s += " %s\n" % line
+
+ if msg.source is not None:
+ try:
+ import tracemalloc
+ tb = tracemalloc.get_object_traceback(msg.source)
+ except Exception:
+ # When a warning is logged during Python shutdown, tracemalloc
+ # and the import machinery don't work anymore
+ tb = None
+
+ if tb is not None:
+ s += 'Object allocated at (most recent call first):\n'
+ for frame in tb:
+ s += (' File "%s", lineno %s\n'
+ % (frame.filename, frame.lineno))
+
+ try:
+ if linecache is not None:
+ line = linecache.getline(frame.filename, frame.lineno)
+ else:
+ line = None
+ except Exception:
+ line = None
+ if line:
+ line = line.strip()
+ s += ' %s\n' % line
return s
+# Keep a reference to check if the function was replaced
+_showwarning_orig = showwarning
+
+def _showwarnmsg(msg):
+ """Hook to write a warning to a file; replace if you like."""
+ try:
+ sw = showwarning
+ except NameError:
+ pass
+ else:
+ if sw is not _showwarning_orig:
+ # warnings.showwarning() was replaced
+ if not callable(sw):
+ raise TypeError("warnings.showwarning() must be set to a "
+ "function or method")
+
+ sw(msg.message, msg.category, msg.filename, msg.lineno,
+ msg.file, msg.line)
+ return
+ _showwarnmsg_impl(msg)
+
+# Keep a reference to check if the function was replaced
+_formatwarning_orig = formatwarning
+
+def _formatwarnmsg(msg):
+ """Function to format a warning the standard way."""
+ try:
+ fw = formatwarning
+ except NameError:
+ pass
+ else:
+ if fw is not _formatwarning_orig:
+ # warnings.formatwarning() was replaced
+ return fw(msg.message, msg.category,
+ msg.filename, msg.lineno, line=msg.line)
+ return _formatwarnmsg_impl(msg)
+
def filterwarnings(action, message="", category=Warning, module="", lineno=0,
append=False):
"""Insert an entry into the list of warnings filters (at the front).
@@ -185,7 +266,7 @@ def _next_external_frame(frame):
# Code typically replaced by _warnings
-def warn(message, category=None, stacklevel=1):
+def warn(message, category=None, stacklevel=1, source=None):
"""Issue a warning, or maybe ignore it or raise an exception."""
# Check if message is already a Warning object
if isinstance(message, Warning):
@@ -235,10 +316,11 @@ def warn(message, category=None, stacklevel=1):
filename = module
registry = globals.setdefault("__warningregistry__", {})
warn_explicit(message, category, filename, lineno, module, registry,
- globals)
+ globals, source)
def warn_explicit(message, category, filename, lineno,
- module=None, registry=None, module_globals=None):
+ module=None, registry=None, module_globals=None,
+ source=None):
lineno = int(lineno)
if module is None:
module = filename or "<unknown>"
@@ -303,22 +385,18 @@ def warn_explicit(message, category, filename, lineno,
raise RuntimeError(
"Unrecognized action (%r) in warnings.filters:\n %s" %
(action, item))
- if not callable(showwarning):
- raise TypeError("warnings.showwarning() must be set to a "
- "function or method")
# Print message and context
- showwarning(message, category, filename, lineno)
+ msg = WarningMessage(message, category, filename, lineno, source)
+ _showwarnmsg(msg)
class WarningMessage(object):
- """Holds the result of a single showwarning() call."""
-
_WARNING_DETAILS = ("message", "category", "filename", "lineno", "file",
- "line")
+ "line", "source")
def __init__(self, message, category, filename, lineno, file=None,
- line=None):
+ line=None, source=None):
local_values = locals()
for attr in self._WARNING_DETAILS:
setattr(self, attr, local_values[attr])
@@ -376,11 +454,13 @@ class catch_warnings(object):
self._module.filters = self._filters[:]
self._module._filters_mutated()
self._showwarning = self._module.showwarning
+ self._showwarnmsg_impl = self._module._showwarnmsg_impl
if self._record:
log = []
- def showwarning(*args, **kwargs):
- log.append(WarningMessage(*args, **kwargs))
- self._module.showwarning = showwarning
+ self._module._showwarnmsg_impl = log.append
+ # Reset showwarning() to the default implementation to make sure
+ # that _showwarnmsg() calls _showwarnmsg_impl()
+ self._module.showwarning = self._module._showwarning_orig
return log
else:
return None
@@ -391,6 +471,7 @@ class catch_warnings(object):
self._module.filters = self._filters
self._module._filters_mutated()
self._module.showwarning = self._showwarning
+ self._module._showwarnmsg_impl = self._showwarnmsg_impl
# filters contains a sequence of filter 5-tuples
diff --git a/Lib/wave.py b/Lib/wave.py
index 8a101e3..f71f7e5 100644
--- a/Lib/wave.py
+++ b/Lib/wave.py
@@ -73,7 +73,7 @@ is destroyed.
import builtins
-__all__ = ["open", "openfp", "Error"]
+__all__ = ["open", "openfp", "Error", "Wave_read", "Wave_write"]
class Error(Exception):
pass
diff --git a/Lib/wsgiref/simple_server.py b/Lib/wsgiref/simple_server.py
index 7fddbe8..f71563a 100644
--- a/Lib/wsgiref/simple_server.py
+++ b/Lib/wsgiref/simple_server.py
@@ -11,7 +11,6 @@ module. See also the BaseHTTPServer module docs for other API information.
"""
from http.server import BaseHTTPRequestHandler, HTTPServer
-from io import BufferedWriter
import sys
import urllib.parse
from wsgiref.handlers import SimpleHandler
@@ -127,17 +126,11 @@ class WSGIRequestHandler(BaseHTTPRequestHandler):
if not self.parse_request(): # An error code has been sent, just exit
return
- # Avoid passing the raw file object wfile, which can do partial
- # writes (Issue 24291)
- stdout = BufferedWriter(self.wfile)
- try:
- handler = ServerHandler(
- self.rfile, stdout, self.get_stderr(), self.get_environ()
- )
- handler.request_handler = self # backpointer for logging
- handler.run(self.server.get_app())
- finally:
- stdout.detach()
+ handler = ServerHandler(
+ self.rfile, self.wfile, self.get_stderr(), self.get_environ()
+ )
+ handler.request_handler = self # backpointer for logging
+ handler.run(self.server.get_app())
@@ -163,10 +156,9 @@ def make_server(
if __name__ == '__main__':
- httpd = make_server('', 8000, demo_app)
- sa = httpd.socket.getsockname()
- print("Serving HTTP on", sa[0], "port", sa[1], "...")
- import webbrowser
- webbrowser.open('http://localhost:8000/xyz?abc')
- httpd.handle_request() # serve one request, then exit
- httpd.server_close()
+ with make_server('', 8000, demo_app) as httpd:
+ sa = httpd.socket.getsockname()
+ print("Serving HTTP on", sa[0], "port", sa[1], "...")
+ import webbrowser
+ webbrowser.open('http://localhost:8000/xyz?abc')
+ httpd.handle_request() # serve one request, then exit
diff --git a/Lib/xml/dom/xmlbuilder.py b/Lib/xml/dom/xmlbuilder.py
index 444f0b2..e9a1536 100644
--- a/Lib/xml/dom/xmlbuilder.py
+++ b/Lib/xml/dom/xmlbuilder.py
@@ -353,14 +353,14 @@ class _AsyncDeprecatedProperty:
class DocumentLS:
"""Mixin to create documents that conform to the load/save spec."""
- async = _AsyncDeprecatedProperty()
async_ = False
+ locals()['async'] = _AsyncDeprecatedProperty() # Avoid DeprecationWarning
def _get_async(self):
return False
- def _set_async(self, async):
- if async:
+ def _set_async(self, flag):
+ if flag:
raise xml.dom.NotSupportedErr(
"asynchronous document loading is not supported")
diff --git a/Lib/xml/etree/ElementPath.py b/Lib/xml/etree/ElementPath.py
index 5de4232..ab6b79a 100644
--- a/Lib/xml/etree/ElementPath.py
+++ b/Lib/xml/etree/ElementPath.py
@@ -59,15 +59,15 @@
import re
xpath_tokenizer_re = re.compile(
- "("
- "'[^']*'|\"[^\"]*\"|"
- "::|"
- "//?|"
- "\.\.|"
- "\(\)|"
- "[/.*:\[\]\(\)@=])|"
- "((?:\{[^}]+\})?[^/\[\]\(\)@=\s]+)|"
- "\s+"
+ r"("
+ r"'[^']*'|\"[^\"]*\"|"
+ r"::|"
+ r"//?|"
+ r"\.\.|"
+ r"\(\)|"
+ r"[/.*:\[\]\(\)@=])|"
+ r"((?:\{[^}]+\})?[^/\[\]\(\)@=\s]+)|"
+ r"\s+"
)
def xpath_tokenizer(pattern, namespaces=None):
@@ -180,7 +180,7 @@ def prepare_predicate(next, token):
if elem.get(key) == value:
yield elem
return select
- if signature == "-" and not re.match("\-?\d+$", predicate[0]):
+ if signature == "-" and not re.match(r"\-?\d+$", predicate[0]):
# [tag]
tag = predicate[0]
def select(context, result):
@@ -188,7 +188,7 @@ def prepare_predicate(next, token):
if elem.find(tag) is not None:
yield elem
return select
- if signature == "-='" and not re.match("\-?\d+$", predicate[0]):
+ if signature == "-='" and not re.match(r"\-?\d+$", predicate[0]):
# [tag='value']
tag = predicate[0]
value = predicate[-1]
diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py
index 92821c5..7354056 100644
--- a/Lib/xml/etree/ElementTree.py
+++ b/Lib/xml/etree/ElementTree.py
@@ -85,7 +85,7 @@ __all__ = [
"TreeBuilder",
"VERSION",
"XML", "XMLID",
- "XMLParser",
+ "XMLParser", "XMLPullParser",
"register_namespace",
]
@@ -95,6 +95,7 @@ import sys
import re
import warnings
import io
+import collections
import contextlib
from . import ElementPath
@@ -1029,7 +1030,7 @@ def register_namespace(prefix, uri):
ValueError is raised if prefix is reserved or is invalid.
"""
- if re.match("ns\d+$", prefix):
+ if re.match(r"ns\d+$", prefix):
raise ValueError("Prefix format reserved for internal use")
for k, v in list(_namespace_map.items()):
if k == uri or v == prefix:
@@ -1084,7 +1085,7 @@ def _escape_attrib(text):
if "\"" in text:
text = text.replace("\"", "&quot;")
# The following business with carriage returns is to satisfy
- # Section 2.11 of the XML specification, stating that
+ # Section 2.11 of the XML specification, stating that
# CR or CR LN should be replaced with just LN
# http://www.w3.org/TR/REC-xml/#sec-line-ends
if "\r\n" in text:
@@ -1211,16 +1212,37 @@ def iterparse(source, events=None, parser=None):
Returns an iterator providing (event, elem) pairs.
"""
+ # Use the internal, undocumented _parser argument for now; When the
+ # parser argument of iterparse is removed, this can be killed.
+ pullparser = XMLPullParser(events=events, _parser=parser)
+ def iterator():
+ try:
+ while True:
+ yield from pullparser.read_events()
+ # load event buffer
+ data = source.read(16 * 1024)
+ if not data:
+ break
+ pullparser.feed(data)
+ root = pullparser._close_and_return_root()
+ yield from pullparser.read_events()
+ it.root = root
+ finally:
+ if close_source:
+ source.close()
+
+ class IterParseIterator(collections.Iterator):
+ __next__ = iterator().__next__
+ it = IterParseIterator()
+ it.root = None
+ del iterator, IterParseIterator
+
close_source = False
if not hasattr(source, "read"):
source = open(source, "rb")
close_source = True
- try:
- return _IterParseIterator(source, events, parser, close_source)
- except:
- if close_source:
- source.close()
- raise
+
+ return it
class XMLPullParser:
@@ -1230,9 +1252,7 @@ class XMLPullParser:
# upon in user code. It will be removed in a future release.
# See http://bugs.python.org/issue17741 for more details.
- # _elementtree.c expects a list, not a deque
- self._events_queue = []
- self._index = 0
+ self._events_queue = collections.deque()
self._parser = _parser or XMLParser(target=TreeBuilder())
# wire up the parser for event reporting
if events is None:
@@ -1270,64 +1290,14 @@ class XMLPullParser:
retrieved from the iterator.
"""
events = self._events_queue
- while True:
- index = self._index
- try:
- event = events[self._index]
- # Avoid retaining references to past events
- events[self._index] = None
- except IndexError:
- break
- index += 1
- # Compact the list in a O(1) amortized fashion
- # As noted above, _elementree.c needs a list, not a deque
- if index * 2 >= len(events):
- events[:index] = []
- self._index = 0
- else:
- self._index = index
+ while events:
+ event = events.popleft()
if isinstance(event, Exception):
raise event
else:
yield event
-class _IterParseIterator:
-
- def __init__(self, source, events, parser, close_source=False):
- # Use the internal, undocumented _parser argument for now; When the
- # parser argument of iterparse is removed, this can be killed.
- self._parser = XMLPullParser(events=events, _parser=parser)
- self._file = source
- self._close_file = close_source
- self.root = self._root = None
-
- def __next__(self):
- try:
- while 1:
- for event in self._parser.read_events():
- return event
- if self._parser._parser is None:
- break
- # load event buffer
- data = self._file.read(16 * 1024)
- if data:
- self._parser.feed(data)
- else:
- self._root = self._parser._close_and_return_root()
- self.root = self._root
- except:
- if self._close_file:
- self._file.close()
- raise
- if self._close_file:
- self._file.close()
- raise StopIteration
-
- def __iter__(self):
- return self
-
-
def XML(text, parser=None):
"""Parse XML document from string constant.
diff --git a/Lib/xmlrpc/client.py b/Lib/xmlrpc/client.py
index bbf9ee6..bd3278e 100644
--- a/Lib/xmlrpc/client.py
+++ b/Lib/xmlrpc/client.py
@@ -132,6 +132,7 @@ import base64
import sys
import time
from datetime import datetime
+from decimal import Decimal
import http.client
import urllib.parse
from xml.parsers import expat
@@ -151,7 +152,7 @@ def escape(s):
return s.replace(">", "&gt;",)
# used in User-Agent header sent
-__version__ = sys.version[:3]
+__version__ = '%d.%d' % sys.version_info[:2]
# xmlrpc integer limits
MAXINT = 2**31-1
@@ -667,6 +668,8 @@ class Unmarshaller:
def start(self, tag, attrs):
# prepare to handle this element
+ if ':' in tag:
+ tag = tag.split(':')[-1]
if tag == "array" or tag == "struct":
self._marks.append(len(self._stack))
self._data = []
@@ -682,9 +685,13 @@ class Unmarshaller:
try:
f = self.dispatch[tag]
except KeyError:
- pass # unknown tag ?
- else:
- return f(self, "".join(self._data))
+ if ':' not in tag:
+ return # unknown tag ?
+ try:
+ f = self.dispatch[tag.split(':')[-1]]
+ except KeyError:
+ return # unknown tag ?
+ return f(self, "".join(self._data))
#
# accelerator support
@@ -694,9 +701,13 @@ class Unmarshaller:
try:
f = self.dispatch[tag]
except KeyError:
- pass # unknown tag ?
- else:
- return f(self, data)
+ if ':' not in tag:
+ return # unknown tag ?
+ try:
+ f = self.dispatch[tag.split(':')[-1]]
+ except KeyError:
+ return # unknown tag ?
+ return f(self, data)
#
# element decoders
@@ -721,14 +732,23 @@ class Unmarshaller:
def end_int(self, data):
self.append(int(data))
self._value = 0
+ dispatch["i1"] = end_int
+ dispatch["i2"] = end_int
dispatch["i4"] = end_int
dispatch["i8"] = end_int
dispatch["int"] = end_int
+ dispatch["biginteger"] = end_int
def end_double(self, data):
self.append(float(data))
self._value = 0
dispatch["double"] = end_double
+ dispatch["float"] = end_double
+
+ def end_bigdecimal(self, data):
+ self.append(Decimal(data))
+ self._value = 0
+ dispatch["bigdecimal"] = end_bigdecimal
def end_string(self, data):
if self._encoding:
diff --git a/Lib/xmlrpc/server.py b/Lib/xmlrpc/server.py
index 7817693..849bfdd 100644
--- a/Lib/xmlrpc/server.py
+++ b/Lib/xmlrpc/server.py
@@ -971,16 +971,15 @@ if __name__ == '__main__':
def getCurrentTime():
return datetime.datetime.now()
- server = SimpleXMLRPCServer(("localhost", 8000))
- server.register_function(pow)
- server.register_function(lambda x,y: x+y, 'add')
- server.register_instance(ExampleService(), allow_dotted_names=True)
- server.register_multicall_functions()
- print('Serving XML-RPC on localhost port 8000')
- print('It is advisable to run this example server within a secure, closed network.')
- try:
- server.serve_forever()
- except KeyboardInterrupt:
- print("\nKeyboard interrupt received, exiting.")
- server.server_close()
- sys.exit(0)
+ with SimpleXMLRPCServer(("localhost", 8000)) as server:
+ server.register_function(pow)
+ server.register_function(lambda x,y: x+y, 'add')
+ server.register_instance(ExampleService(), allow_dotted_names=True)
+ server.register_multicall_functions()
+ print('Serving XML-RPC on localhost port 8000')
+ print('It is advisable to run this example server within a secure, closed network.')
+ try:
+ server.serve_forever()
+ except KeyboardInterrupt:
+ print("\nKeyboard interrupt received, exiting.")
+ sys.exit(0)
diff --git a/Lib/zipfile.py b/Lib/zipfile.py
index 048f60a..7f2b43c 100644
--- a/Lib/zipfile.py
+++ b/Lib/zipfile.py
@@ -371,7 +371,7 @@ class ZipInfo (object):
result.append(' filemode=%r' % stat.filemode(hi))
if lo:
result.append(' external_attr=%#x' % lo)
- isdir = self.filename[-1:] == '/'
+ isdir = self.is_dir()
if not isdir or self.file_size:
result.append(' file_size=%r' % self.file_size)
if ((not isdir or self.compress_size) and
@@ -449,7 +449,7 @@ class ZipInfo (object):
elif ln == 0:
counts = ()
else:
- raise RuntimeError("Corrupt extra field %s"%(ln,))
+ raise BadZipFile("Corrupt extra field %04x (size=%d)" % (tp, ln))
idx = 0
@@ -469,6 +469,42 @@ class ZipInfo (object):
extra = extra[ln+4:]
+ @classmethod
+ def from_file(cls, filename, arcname=None):
+ """Construct an appropriate ZipInfo for a file on the filesystem.
+
+ filename should be the path to a file or directory on the filesystem.
+
+ arcname is the name which it will have within the archive (by default,
+ this will be the same as filename, but without a drive letter and with
+ leading path separators removed).
+ """
+ st = os.stat(filename)
+ isdir = stat.S_ISDIR(st.st_mode)
+ mtime = time.localtime(st.st_mtime)
+ date_time = mtime[0:6]
+ # Create ZipInfo instance to store file information
+ if arcname is None:
+ arcname = filename
+ arcname = os.path.normpath(os.path.splitdrive(arcname)[1])
+ while arcname[0] in (os.sep, os.altsep):
+ arcname = arcname[1:]
+ if isdir:
+ arcname += '/'
+ zinfo = cls(arcname, date_time)
+ zinfo.external_attr = (st.st_mode & 0xFFFF) << 16 # Unix attributes
+ if isdir:
+ zinfo.file_size = 0
+ zinfo.external_attr |= 0x10 # MS-DOS directory flag
+ else:
+ zinfo.file_size = st.st_size
+
+ return zinfo
+
+ def is_dir(self):
+ """Return True if this archive member is a directory."""
+ return self.filename[-1] == '/'
+
class _ZipDecrypter:
"""Class to handle decryption of files stored within a ZIP archive.
@@ -618,7 +654,7 @@ def _check_compression(compression):
raise RuntimeError(
"Compression requires the (missing) lzma module")
else:
- raise RuntimeError("That compression method is not supported")
+ raise NotImplementedError("That compression method is not supported")
def _get_compressor(compress_type):
@@ -651,14 +687,19 @@ def _get_decompressor(compress_type):
class _SharedFile:
- def __init__(self, file, pos, close, lock):
+ def __init__(self, file, pos, close, lock, writing):
self._file = file
self._pos = pos
self._close = close
self._lock = lock
+ self._writing = writing
def read(self, n=-1):
with self._lock:
+ if self._writing():
+ raise ValueError("Can't read from the ZIP file while there "
+ "is an open writing handle on it. "
+ "Close the writing handle before trying to read.")
self._file.seek(self._pos)
data = self._file.read(n)
self._pos = self._file.tell()
@@ -702,9 +743,6 @@ class ZipExtFile(io.BufferedIOBase):
# Read from compressed files in 4k blocks.
MIN_READ_SIZE = 4096
- # Search for universal newlines or line chunks.
- PATTERN = re.compile(br'^(?P<chunk>[^\r\n]+)|(?P<newline>\n|\r\n?)')
-
def __init__(self, fileobj, mode, zipinfo, decrypter=None,
close_fileobj=False):
self._fileobj = fileobj
@@ -721,7 +759,6 @@ class ZipExtFile(io.BufferedIOBase):
self._readbuffer = b''
self._offset = 0
- self._universal = 'U' in mode
self.newlines = None
# Adjust read size for encrypted files since the first 12 bytes
@@ -758,7 +795,7 @@ class ZipExtFile(io.BufferedIOBase):
If limit is specified, at most limit bytes will be read.
"""
- if not self._universal and limit < 0:
+ if limit < 0:
# Shortcut common case - newline found in buffer.
i = self._readbuffer.find(b'\n', self._offset) + 1
if i > 0:
@@ -766,41 +803,7 @@ class ZipExtFile(io.BufferedIOBase):
self._offset = i
return line
- if not self._universal:
- return io.BufferedIOBase.readline(self, limit)
-
- line = b''
- while limit < 0 or len(line) < limit:
- readahead = self.peek(2)
- if readahead == b'':
- return line
-
- #
- # Search for universal newlines or line chunks.
- #
- # The pattern returns either a line chunk or a newline, but not
- # both. Combined with peek(2), we are assured that the sequence
- # '\r\n' is always retrieved completely and never split into
- # separate newlines - '\r', '\n' due to coincidental readaheads.
- #
- match = self.PATTERN.search(readahead)
- newline = match.group('newline')
- if newline is not None:
- if self.newlines is None:
- self.newlines = []
- if newline not in self.newlines:
- self.newlines.append(newline)
- self._offset += len(newline)
- return line + b'\n'
-
- chunk = match.group('chunk')
- if limit >= 0:
- chunk = chunk[: limit - len(line)]
-
- self._offset += len(chunk)
- line += chunk
-
- return line
+ return io.BufferedIOBase.readline(self, limit)
def peek(self, n=1):
"""Returns buffered bytes without advancing the position."""
@@ -958,6 +961,76 @@ class ZipExtFile(io.BufferedIOBase):
super().close()
+class _ZipWriteFile(io.BufferedIOBase):
+ def __init__(self, zf, zinfo, zip64):
+ self._zinfo = zinfo
+ self._zip64 = zip64
+ self._zipfile = zf
+ self._compressor = _get_compressor(zinfo.compress_type)
+ self._file_size = 0
+ self._compress_size = 0
+ self._crc = 0
+
+ @property
+ def _fileobj(self):
+ return self._zipfile.fp
+
+ def writable(self):
+ return True
+
+ def write(self, data):
+ nbytes = len(data)
+ self._file_size += nbytes
+ self._crc = crc32(data, self._crc)
+ if self._compressor:
+ data = self._compressor.compress(data)
+ self._compress_size += len(data)
+ self._fileobj.write(data)
+ return nbytes
+
+ def close(self):
+ super().close()
+ # Flush any data from the compressor, and update header info
+ if self._compressor:
+ buf = self._compressor.flush()
+ self._compress_size += len(buf)
+ self._fileobj.write(buf)
+ self._zinfo.compress_size = self._compress_size
+ else:
+ self._zinfo.compress_size = self._file_size
+ self._zinfo.CRC = self._crc
+ self._zinfo.file_size = self._file_size
+
+ # Write updated header info
+ if self._zinfo.flag_bits & 0x08:
+ # Write CRC and file sizes after the file data
+ fmt = '<LQQ' if self._zip64 else '<LLL'
+ self._fileobj.write(struct.pack(fmt, self._zinfo.CRC,
+ self._zinfo.compress_size, self._zinfo.file_size))
+ self._zipfile.start_dir = self._fileobj.tell()
+ else:
+ if not self._zip64:
+ if self._file_size > ZIP64_LIMIT:
+ raise RuntimeError('File size unexpectedly exceeded ZIP64 '
+ 'limit')
+ if self._compress_size > ZIP64_LIMIT:
+ raise RuntimeError('Compressed size unexpectedly exceeded '
+ 'ZIP64 limit')
+ # Seek backwards and write file header (which will now include
+ # correct CRC and file sizes)
+
+ # Preserve current position in file
+ self._zipfile.start_dir = self._fileobj.tell()
+ self._fileobj.seek(self._zinfo.header_offset)
+ self._fileobj.write(self._zinfo.FileHeader(self._zip64))
+ self._fileobj.seek(self._zipfile.start_dir)
+
+ self._zipfile._writing = False
+
+ # Successfully written: Add file to our caches
+ self._zipfile.filelist.append(self._zinfo)
+ self._zipfile.NameToInfo[self._zinfo.filename] = self._zinfo
+
class ZipFile:
""" Class with methods to open, read, write, close, list zip files.
@@ -982,7 +1055,7 @@ class ZipFile:
"""Open the ZIP file with mode read 'r', write 'w', exclusive create 'x',
or append 'a'."""
if mode not in ('r', 'w', 'x', 'a'):
- raise RuntimeError("ZipFile requires mode 'r', 'w', 'x', or 'a'")
+ raise ValueError("ZipFile requires mode 'r', 'w', 'x', or 'a'")
_check_compression(compression)
@@ -1020,6 +1093,7 @@ class ZipFile:
self._fileRefCnt = 1
self._lock = threading.RLock()
self._seekable = True
+ self._writing = False
try:
if mode == 'r':
@@ -1056,7 +1130,7 @@ class ZipFile:
self._didModify = True
self.start_dir = self._start_disk = self.fp.tell()
else:
- raise RuntimeError("Mode must be 'r', 'w', 'x', or 'a'")
+ raise ValueError("Mode must be 'r', 'w', 'x', or 'a'")
except:
fp = self.fp
self.fp = None
@@ -1205,7 +1279,7 @@ class ZipFile:
def setpassword(self, pwd):
"""Set default password for encrypted files."""
if pwd and not isinstance(pwd, bytes):
- raise TypeError("pwd: expected bytes, got %s" % type(pwd))
+ raise TypeError("pwd: expected bytes, got %s" % type(pwd).__name__)
if pwd:
self.pwd = pwd
else:
@@ -1219,7 +1293,7 @@ class ZipFile:
@comment.setter
def comment(self, comment):
if not isinstance(comment, bytes):
- raise TypeError("comment: expected bytes, got %s" % type(comment))
+ raise TypeError("comment: expected bytes, got %s" % type(comment).__name__)
# check for valid comment length
if len(comment) > ZIP_MAX_COMMENT:
import warnings
@@ -1234,30 +1308,55 @@ class ZipFile:
with self.open(name, "r", pwd) as fp:
return fp.read()
- def open(self, name, mode="r", pwd=None):
- """Return file-like object for 'name'."""
- if mode not in ("r", "U", "rU"):
- raise RuntimeError('open() requires mode "r", "U", or "rU"')
- if 'U' in mode:
- import warnings
- warnings.warn("'U' mode is deprecated",
- DeprecationWarning, 2)
+ def open(self, name, mode="r", pwd=None, *, force_zip64=False):
+ """Return file-like object for 'name'.
+
+ name is a string for the file name within the ZIP file, or a ZipInfo
+ object.
+
+ mode should be 'r' to read a file already in the ZIP file, or 'w' to
+ write to a file newly added to the archive.
+
+ pwd is the password to decrypt files (only used for reading).
+
+ When writing, if the file size is not known in advance but may exceed
+ 2 GiB, pass force_zip64 to use the ZIP64 format, which can handle large
+ files. If the size is known in advance, it is best to pass a ZipInfo
+ instance for name, with zinfo.file_size set.
+ """
+ if mode not in {"r", "w"}:
+ raise ValueError('open() requires mode "r" or "w"')
if pwd and not isinstance(pwd, bytes):
- raise TypeError("pwd: expected bytes, got %s" % type(pwd))
+ raise TypeError("pwd: expected bytes, got %s" % type(pwd).__name__)
+ if pwd and (mode == "w"):
+ raise ValueError("pwd is only supported for reading files")
if not self.fp:
- raise RuntimeError(
- "Attempt to read ZIP archive that was already closed")
+ raise ValueError(
+ "Attempt to use ZIP archive that was already closed")
# Make sure we have an info object
if isinstance(name, ZipInfo):
# 'name' is already an info object
zinfo = name
+ elif mode == 'w':
+ zinfo = ZipInfo(name)
+ zinfo.compress_type = self.compression
else:
# Get info object for name
zinfo = self.getinfo(name)
+ if mode == 'w':
+ return self._open_to_write(zinfo, force_zip64=force_zip64)
+
+ if self._writing:
+ raise ValueError("Can't read from the ZIP file while there "
+ "is an open writing handle on it. "
+ "Close the writing handle before trying to read.")
+
+ # Open for reading:
self._fileRefCnt += 1
- zef_file = _SharedFile(self.fp, zinfo.header_offset, self._fpclose, self._lock)
+ zef_file = _SharedFile(self.fp, zinfo.header_offset,
+ self._fpclose, self._lock, lambda: self._writing)
try:
# Skip the file header:
fheader = zef_file.read(sizeFileHeader)
@@ -1297,7 +1396,7 @@ class ZipFile:
if not pwd:
pwd = self.pwd
if not pwd:
- raise RuntimeError("File %s is encrypted, password "
+ raise RuntimeError("File %r is encrypted, password "
"required for extraction" % name)
zd = _ZipDecrypter(pwd)
@@ -1315,13 +1414,56 @@ class ZipFile:
# compare against the CRC otherwise
check_byte = (zinfo.CRC >> 24) & 0xff
if h[11] != check_byte:
- raise RuntimeError("Bad password for file", name)
+ raise RuntimeError("Bad password for file %r" % name)
return ZipExtFile(zef_file, mode, zinfo, zd, True)
except:
zef_file.close()
raise
+ def _open_to_write(self, zinfo, force_zip64=False):
+ if force_zip64 and not self._allowZip64:
+ raise ValueError(
+ "force_zip64 is True, but allowZip64 was False when opening "
+ "the ZIP file."
+ )
+ if self._writing:
+ raise ValueError("Can't write to the ZIP file while there is "
+ "another write handle open on it. "
+ "Close the first handle before opening another.")
+
+ # Sizes and CRC are overwritten with correct data after processing the file
+ if not hasattr(zinfo, 'file_size'):
+ zinfo.file_size = 0
+ zinfo.compress_size = 0
+ zinfo.CRC = 0
+
+ zinfo.flag_bits = 0x00
+ if zinfo.compress_type == ZIP_LZMA:
+ # Compressed data includes an end-of-stream (EOS) marker
+ zinfo.flag_bits |= 0x02
+ if not self._seekable:
+ zinfo.flag_bits |= 0x08
+
+ if not zinfo.external_attr:
+ zinfo.external_attr = 0o600 << 16 # permissions: ?rw-------
+
+ # Compressed size can be larger than uncompressed size
+ zip64 = self._allowZip64 and \
+ (force_zip64 or zinfo.file_size * 1.05 > ZIP64_LIMIT)
+
+ if self._seekable:
+ self.fp.seek(self.start_dir)
+ zinfo.header_offset = self.fp.tell()
+
+ self._writecheck(zinfo)
+ self._didModify = True
+
+ self.fp.write(zinfo.FileHeader(zip64))
+
+ self._writing = True
+ return _ZipWriteFile(self, zinfo, zip64)
+
def extract(self, member, path=None, pwd=None):
"""Extract a member from the archive to the current working directory,
using its full name. Its file information is extracted as accurately
@@ -1391,7 +1533,7 @@ class ZipFile:
if upperdirs and not os.path.exists(upperdirs):
os.makedirs(upperdirs)
- if member.filename[-1] == '/':
+ if member.is_dir():
if not os.path.isdir(targetpath):
os.mkdir(targetpath)
return targetpath
@@ -1408,9 +1550,9 @@ class ZipFile:
import warnings
warnings.warn('Duplicate name: %r' % zinfo.filename, stacklevel=3)
if self.mode not in ('w', 'x', 'a'):
- raise RuntimeError("write() requires mode 'w', 'x', or 'a'")
+ raise ValueError("write() requires mode 'w', 'x', or 'a'")
if not self.fp:
- raise RuntimeError(
+ raise ValueError(
"Attempt to write ZIP archive that was already closed")
_check_compression(zinfo.compress_type)
if not self._allowZip64:
@@ -1429,105 +1571,43 @@ class ZipFile:
"""Put the bytes from filename into the archive under the name
arcname."""
if not self.fp:
- raise RuntimeError(
+ raise ValueError(
"Attempt to write to ZIP archive that was already closed")
+ if self._writing:
+ raise ValueError(
+ "Can't write to ZIP archive while an open writing handle exists"
+ )
- st = os.stat(filename)
- isdir = stat.S_ISDIR(st.st_mode)
- mtime = time.localtime(st.st_mtime)
- date_time = mtime[0:6]
- # Create ZipInfo instance to store file information
- if arcname is None:
- arcname = filename
- arcname = os.path.normpath(os.path.splitdrive(arcname)[1])
- while arcname[0] in (os.sep, os.altsep):
- arcname = arcname[1:]
- if isdir:
- arcname += '/'
- zinfo = ZipInfo(arcname, date_time)
- zinfo.external_attr = (st[0] & 0xFFFF) << 16 # Unix attributes
- if isdir:
- zinfo.compress_type = ZIP_STORED
- elif compress_type is None:
- zinfo.compress_type = self.compression
+ zinfo = ZipInfo.from_file(filename, arcname)
+
+ if zinfo.is_dir():
+ zinfo.compress_size = 0
+ zinfo.CRC = 0
else:
- zinfo.compress_type = compress_type
+ if compress_type is not None:
+ zinfo.compress_type = compress_type
+ else:
+ zinfo.compress_type = self.compression
- zinfo.file_size = st.st_size
- zinfo.flag_bits = 0x00
- with self._lock:
- if self._seekable:
- self.fp.seek(self.start_dir)
- zinfo.header_offset = self.fp.tell() # Start of header bytes
- if zinfo.compress_type == ZIP_LZMA:
+ if zinfo.is_dir():
+ with self._lock:
+ if self._seekable:
+ self.fp.seek(self.start_dir)
+ zinfo.header_offset = self.fp.tell() # Start of header bytes
+ if zinfo.compress_type == ZIP_LZMA:
# Compressed data includes an end-of-stream (EOS) marker
- zinfo.flag_bits |= 0x02
+ zinfo.flag_bits |= 0x02
- self._writecheck(zinfo)
- self._didModify = True
+ self._writecheck(zinfo)
+ self._didModify = True
- if isdir:
- zinfo.file_size = 0
- zinfo.compress_size = 0
- zinfo.CRC = 0
- zinfo.external_attr |= 0x10 # MS-DOS directory flag
self.filelist.append(zinfo)
self.NameToInfo[zinfo.filename] = zinfo
self.fp.write(zinfo.FileHeader(False))
self.start_dir = self.fp.tell()
- return
-
- cmpr = _get_compressor(zinfo.compress_type)
- if not self._seekable:
- zinfo.flag_bits |= 0x08
- with open(filename, "rb") as fp:
- # Must overwrite CRC and sizes with correct data later
- zinfo.CRC = CRC = 0
- zinfo.compress_size = compress_size = 0
- # Compressed size can be larger than uncompressed size
- zip64 = self._allowZip64 and \
- zinfo.file_size * 1.05 > ZIP64_LIMIT
- self.fp.write(zinfo.FileHeader(zip64))
- file_size = 0
- while 1:
- buf = fp.read(1024 * 8)
- if not buf:
- break
- file_size = file_size + len(buf)
- CRC = crc32(buf, CRC)
- if cmpr:
- buf = cmpr.compress(buf)
- compress_size = compress_size + len(buf)
- self.fp.write(buf)
- if cmpr:
- buf = cmpr.flush()
- compress_size = compress_size + len(buf)
- self.fp.write(buf)
- zinfo.compress_size = compress_size
- else:
- zinfo.compress_size = file_size
- zinfo.CRC = CRC
- zinfo.file_size = file_size
- if zinfo.flag_bits & 0x08:
- # Write CRC and file sizes after the file data
- fmt = '<LQQ' if zip64 else '<LLL'
- self.fp.write(struct.pack(fmt, zinfo.CRC, zinfo.compress_size,
- zinfo.file_size))
- self.start_dir = self.fp.tell()
- else:
- if not zip64 and self._allowZip64:
- if file_size > ZIP64_LIMIT:
- raise RuntimeError('File size has increased during compressing')
- if compress_size > ZIP64_LIMIT:
- raise RuntimeError('Compressed size larger than uncompressed size')
- # Seek backwards and write file header (which will now include
- # correct CRC and file sizes)
- self.start_dir = self.fp.tell() # Preserve current position in file
- self.fp.seek(zinfo.header_offset)
- self.fp.write(zinfo.FileHeader(zip64))
- self.fp.seek(self.start_dir)
- self.filelist.append(zinfo)
- self.NameToInfo[zinfo.filename] = zinfo
+ else:
+ with open(filename, "rb") as src, self.open(zinfo, 'w') as dest:
+ shutil.copyfileobj(src, dest, 1024*8)
def writestr(self, zinfo_or_arcname, data, compress_type=None):
"""Write a file into the archive. The contents is 'data', which
@@ -1550,47 +1630,20 @@ class ZipFile:
zinfo = zinfo_or_arcname
if not self.fp:
- raise RuntimeError(
+ raise ValueError(
"Attempt to write to ZIP archive that was already closed")
+ if self._writing:
+ raise ValueError(
+ "Can't write to ZIP archive while an open writing handle exists."
+ )
+
+ if compress_type is not None:
+ zinfo.compress_type = compress_type
zinfo.file_size = len(data) # Uncompressed size
with self._lock:
- if self._seekable:
- self.fp.seek(self.start_dir)
- zinfo.header_offset = self.fp.tell() # Start of header data
- if compress_type is not None:
- zinfo.compress_type = compress_type
- zinfo.header_offset = self.fp.tell() # Start of header data
- if compress_type is not None:
- zinfo.compress_type = compress_type
- if zinfo.compress_type == ZIP_LZMA:
- # Compressed data includes an end-of-stream (EOS) marker
- zinfo.flag_bits |= 0x02
-
- self._writecheck(zinfo)
- self._didModify = True
- zinfo.CRC = crc32(data) # CRC-32 checksum
- co = _get_compressor(zinfo.compress_type)
- if co:
- data = co.compress(data) + co.flush()
- zinfo.compress_size = len(data) # Compressed size
- else:
- zinfo.compress_size = zinfo.file_size
- zip64 = zinfo.file_size > ZIP64_LIMIT or \
- zinfo.compress_size > ZIP64_LIMIT
- if zip64 and not self._allowZip64:
- raise LargeZipFile("Filesize would require ZIP64 extensions")
- self.fp.write(zinfo.FileHeader(zip64))
- self.fp.write(data)
- if zinfo.flag_bits & 0x08:
- # Write CRC and file sizes after the file data
- fmt = '<LQQ' if zip64 else '<LLL'
- self.fp.write(struct.pack(fmt, zinfo.CRC, zinfo.compress_size,
- zinfo.file_size))
- self.fp.flush()
- self.start_dir = self.fp.tell()
- self.filelist.append(zinfo)
- self.NameToInfo[zinfo.filename] = zinfo
+ with self.open(zinfo, mode='w') as dest:
+ dest.write(data)
def __del__(self):
"""Call the "close()" method in case the user forgot."""
@@ -1602,6 +1655,11 @@ class ZipFile:
if self.fp is None:
return
+ if self._writing:
+ raise ValueError("Can't close the ZIP file while there is "
+ "an open writing handle on it. "
+ "Close the writing handle before closing the zip.")
+
try:
if self.mode in ('w', 'x', 'a') and self._didModify: # write ending records
with self._lock:
@@ -1746,7 +1804,7 @@ class PyZipFile(ZipFile):
if filterfunc and not filterfunc(pathname):
if self.debug:
label = 'path' if os.path.isdir(pathname) else 'file'
- print('%s "%s" skipped by filterfunc' % (label, pathname))
+ print('%s %r skipped by filterfunc' % (label, pathname))
return
dir, name = os.path.split(pathname)
if os.path.isdir(pathname):
@@ -1777,7 +1835,7 @@ class PyZipFile(ZipFile):
elif ext == ".py":
if filterfunc and not filterfunc(path):
if self.debug:
- print('file "%s" skipped by filterfunc' % path)
+ print('file %r skipped by filterfunc' % path)
continue
fname, arcname = self._get_codename(path[0:-3],
basename)
@@ -1794,7 +1852,7 @@ class PyZipFile(ZipFile):
if ext == ".py":
if filterfunc and not filterfunc(path):
if self.debug:
- print('file "%s" skipped by filterfunc' % path)
+ print('file %r skipped by filterfunc' % path)
continue
fname, arcname = self._get_codename(path[0:-3],
basename)
diff --git a/Mac/BuildScript/build-installer.py b/Mac/BuildScript/build-installer.py
index c76c4f1..8dfd092 100755
--- a/Mac/BuildScript/build-installer.py
+++ b/Mac/BuildScript/build-installer.py
@@ -101,6 +101,7 @@ def getFullVersion():
FW_PREFIX = ["Library", "Frameworks", "Python.framework"]
FW_VERSION_PREFIX = "--undefined--" # initialized in parseOptions
+FW_SSL_DIRECTORY = "--undefined--" # initialized in parseOptions
# The directory we'll use to create the build (will be erased and recreated)
WORKDIR = "/tmp/_py"
@@ -206,41 +207,11 @@ def library_recipes():
LT_10_5 = bool(getDeptargetTuple() < (10, 5))
- if not (10, 5) < getDeptargetTuple() < (10, 10):
- # The OpenSSL libs shipped with OS X 10.5 and earlier are
- # hopelessly out-of-date and do not include Apple's tie-in to
- # the root certificates in the user and system keychains via TEA
- # that was introduced in OS X 10.6. Note that this applies to
- # programs built and linked with a 10.5 SDK even when run on
- # newer versions of OS X.
- #
- # Dealing with CAs is messy. For now, just supply a
- # local libssl and libcrypto for the older installer variants
- # (e.g. the python.org 10.5+ 32-bit-only installer) that use the
- # same default ssl certfile location as the system libs do:
- # /System/Library/OpenSSL/cert.pem
- # Then at least TLS connections can be negotiated with sites that
- # use sha-256 certs like python.org, assuming the proper CA certs
- # have been supplied. The default CA cert management issues for
- # 10.5 and earlier builds are the same as before, other than it is
- # now more obvious with cert checking enabled by default in the
- # standard library.
- #
- # For builds with 10.6 through 10.9 SDKs,
- # continue to use the deprecated but
- # less out-of-date Apple 0.9.8 libs for now. While they are less
- # secure than using an up-to-date 1.0.1 version, doing so
- # avoids the big problems of forcing users to have to manage
- # default CAs themselves, thanks to the Apple libs using private TEA
- # APIs for cert validation from keychains if validation using the
- # standard OpenSSL locations (/System/Library/OpenSSL, normally empty)
- # fails.
- #
- # Since Apple removed the header files for the deprecated system
- # OpenSSL as of the Xcode 7 release (for OS X 10.10+), we do not
- # have much choice but to build our own copy here, too.
+ # Since Apple removed the header files for the deprecated system
+ # OpenSSL as of the Xcode 7 release (for OS X 10.10+), we do not
+ # have much choice but to build our own copy here, too.
- result.extend([
+ result.extend([
dict(
name="OpenSSL 1.0.2j",
url="https://www.openssl.org/source/openssl-1.0.2j.tar.gz",
@@ -252,7 +223,7 @@ def library_recipes():
configure=None,
install=None,
),
- ])
+ ])
# Disable for now
if False: # if getDeptargetTuple() > (10, 5):
@@ -299,9 +270,9 @@ def library_recipes():
if PYTHON_3:
result.extend([
dict(
- name="XZ 5.0.5",
- url="http://tukaani.org/xz/xz-5.0.5.tar.gz",
- checksum='19d924e066b6fff0bc9d1981b4e53196',
+ name="XZ 5.2.2",
+ url="http://tukaani.org/xz/xz-5.2.2.tar.gz",
+ checksum='7cf6a8544a7dae8e8106fdf7addfa28c',
configure_pre=[
'--disable-dependency-tracking',
]
@@ -344,10 +315,11 @@ def library_recipes():
),
),
dict(
- name="SQLite 3.8.11",
- url="https://www.sqlite.org/2015/sqlite-autoconf-3081100.tar.gz",
- checksum='77b451925121028befbddbf45ea2bc49',
+ name="SQLite 3.14.2",
+ url="https://www.sqlite.org/2016/sqlite-autoconf-3140200.tar.gz",
+ checksum='90c53cacb811db27f990b8292bd96159',
extra_cflags=('-Os '
+ '-DSQLITE_ENABLE_FTS5 '
'-DSQLITE_ENABLE_FTS4 '
'-DSQLITE_ENABLE_FTS3_PARENTHESIS '
'-DSQLITE_ENABLE_RTREE '
@@ -675,6 +647,7 @@ def parseOptions(args=None):
global WORKDIR, DEPSRC, SDKPATH, SRCDIR, DEPTARGET
global UNIVERSALOPTS, UNIVERSALARCHS, ARCHLIST, CC, CXX
global FW_VERSION_PREFIX
+ global FW_SSL_DIRECTORY
if args is None:
args = sys.argv[1:]
@@ -735,6 +708,7 @@ def parseOptions(args=None):
CC, CXX = getTargetCompilers()
FW_VERSION_PREFIX = FW_PREFIX[:] + ["Versions", getVersion()]
+ FW_SSL_DIRECTORY = FW_VERSION_PREFIX[:] + ["etc", "openssl"]
print("-- Settings:")
print(" * Source directory: %s" % SRCDIR)
@@ -876,7 +850,7 @@ def build_universal_openssl(basedir, archList):
"shared",
"--install_prefix=%s"%shellQuote(archbase),
"--prefix=%s"%os.path.join("/", *FW_VERSION_PREFIX),
- "--openssldir=/System/Library/OpenSSL",
+ "--openssldir=%s"%os.path.join("/", *FW_SSL_DIRECTORY),
]
if no_asm:
configure_opts.append("no-asm")
@@ -1194,12 +1168,14 @@ def buildPython():
'Python.framework', 'Versions', getVersion(),
'lib'))))
- path_to_lib = os.path.join(rootDir, 'Library', 'Frameworks',
- 'Python.framework', 'Versions',
- version, 'lib', 'python%s'%(version,))
+ frmDir = os.path.join(rootDir, 'Library', 'Frameworks', 'Python.framework')
+ frmDirVersioned = os.path.join(frmDir, 'Versions', version)
+ path_to_lib = os.path.join(frmDirVersioned, 'lib', 'python%s'%(version,))
+ # create directory for OpenSSL certificates
+ sslDir = os.path.join(frmDirVersioned, 'etc', 'openssl')
+ os.makedirs(sslDir)
print("Fix file modes")
- frmDir = os.path.join(rootDir, 'Library', 'Frameworks', 'Python.framework')
gid = grp.getgrnam('admin').gr_gid
shared_lib_error = False
@@ -1249,6 +1225,8 @@ def buildPython():
LDVERSION = LDVERSION.replace('$(VERSION)', VERSION)
LDVERSION = LDVERSION.replace('$(ABIFLAGS)', ABIFLAGS)
config_suffix = '-' + LDVERSION
+ if getVersionMajorMinor() >= (3, 6):
+ config_suffix = config_suffix + '-darwin'
else:
config_suffix = '' # Python 2.x
@@ -1274,7 +1252,7 @@ def buildPython():
fp.write(data)
fp.close()
- # fix _sysconfigdata if it exists
+ # fix _sysconfigdata
#
# TODO: make this more robust! test_sysconfig_module of
# distutils.tests.test_sysconfig.SysconfigTestCase tests that
@@ -1288,28 +1266,31 @@ def buildPython():
# _sysconfigdata.py).
import pprint
- path = os.path.join(path_to_lib, '_sysconfigdata.py')
- if os.path.exists(path):
- fp = open(path, 'r')
- data = fp.read()
- fp.close()
- # create build_time_vars dict
- exec(data)
- vars = {}
- for k, v in build_time_vars.items():
- if type(v) == type(''):
- for p in (include_path, lib_path):
- v = v.replace(' ' + p, '')
- v = v.replace(p + ' ', '')
- vars[k] = v
-
- fp = open(path, 'w')
- # duplicated from sysconfig._generate_posix_vars()
- fp.write('# system configuration generated and used by'
- ' the sysconfig module\n')
- fp.write('build_time_vars = ')
- pprint.pprint(vars, stream=fp)
- fp.close()
+ if getVersionMajorMinor() >= (3, 6):
+ # XXX this is extra-fragile
+ path = os.path.join(path_to_lib, '_sysconfigdata_m_darwin_darwin.py')
+ else:
+ path = os.path.join(path_to_lib, '_sysconfigdata.py')
+ fp = open(path, 'r')
+ data = fp.read()
+ fp.close()
+ # create build_time_vars dict
+ exec(data)
+ vars = {}
+ for k, v in build_time_vars.items():
+ if type(v) == type(''):
+ for p in (include_path, lib_path):
+ v = v.replace(' ' + p, '')
+ v = v.replace(p + ' ', '')
+ vars[k] = v
+
+ fp = open(path, 'w')
+ # duplicated from sysconfig._generate_posix_vars()
+ fp.write('# system configuration generated and used by'
+ ' the sysconfig module\n')
+ fp.write('build_time_vars = ')
+ pprint.pprint(vars, stream=fp)
+ fp.close()
# Add symlinks in /usr/local/bin, using relative links
usr_local_bin = os.path.join(rootDir, 'usr', 'local', 'bin')
@@ -1636,6 +1617,8 @@ def main():
patchFile("resources/ReadMe.rtf", fn)
fn = os.path.join(folder, "Update Shell Profile.command")
patchScript("scripts/postflight.patch-profile", fn)
+ fn = os.path.join(folder, "Install Certificates.command")
+ patchScript("resources/install_certificates.command", fn)
os.chmod(folder, STAT_0o755)
setIcon(folder, "../Icons/Python Folder.icns")
diff --git a/Mac/BuildScript/resources/ReadMe.rtf b/Mac/BuildScript/resources/ReadMe.rtf
index 65e3f14..ac68786 100644
--- a/Mac/BuildScript/resources/ReadMe.rtf
+++ b/Mac/BuildScript/resources/ReadMe.rtf
@@ -1,106 +1,76 @@
-{\rtf1\ansi\ansicpg1252\cocoartf1348\cocoasubrtf170
+{\rtf1\ansi\ansicpg1252\cocoartf1504\cocoasubrtf750
{\fonttbl\f0\fswiss\fcharset0 Helvetica;\f1\fmodern\fcharset0 CourierNewPSMT;}
{\colortbl;\red255\green255\blue255;}
+{\*\expandedcolortbl;;}
\margl1440\margr1440\vieww13380\viewh14600\viewkind0
-\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural
+\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0
\f0\fs24 \cf0 This package will install Python $FULL_VERSION for Mac OS X $MACOSX_DEPLOYMENT_TARGET for the following architecture(s): $ARCHITECTURES.\
\
-\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural
+\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0
\b \cf0 \ul \ulc0 Which installer variant should I use?
\b0 \ulnone \
\
-Python.org provides two installer variants for download: one that installs a
+\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0
+
+\b \cf0 **NEW**
+\b0 For Python 3.6, the python.org website now provides only one installer variant for download: one that installs a
\i 64-bit/32-bit Intel
\i0 Python capable of running on
\i Mac OS X 10.6 (Snow Leopard)
-\i0 or later; and one that installs a
-\i 32-bit-only (Intel and PPC)
-\i0 Python capable of running on
-\i Mac OS X 10.5 (Leopard)
\i0 or later. This ReadMe was installed with the
\i $MACOSX_DEPLOYMENT_TARGET
-\i0 variant. Unless you are installing to an 10.5 system or you need to build applications that can run on 10.5 systems, use the 10.6 variant if possible. There are some additional operating system functions that are supported starting with 10.6 and you may see better performance using 64-bit mode. By default, Python will automatically run in 64-bit mode if your system supports it. Also see
-\i Certificate verification and OpenSSL
-\i0 below. The Pythons installed by these installers are built with private copies of some third-party libraries not included with or newer than those in OS X itself. The list of these libraries varies by installer variant and is included at the end of the License.rtf file.
+\i0 variant. By default, Python will automatically run in 64-bit mode if your system supports it. The Python installed by this installer is built with private copies of some third-party libraries not included with or newer than those in OS X itself. The list of these libraries is included at the end of the License.rtf file.
\b \ul \
\
-Update your version of Tcl/Tk to use IDLE or other Tk applications
-\b0 \ulnone \
-\
-To use IDLE or other programs that use the Tkinter graphical user interface toolkit, you need to install a newer third-party version of the
-\i Tcl/Tk
-\i0 frameworks. Visit {\field{\*\fldinst{HYPERLINK "https://www.python.org/download/mac/tcltk/"}}{\fldrslt https://www.python.org/download/mac/tcltk/}} for current information about supported and recommended versions of
-\i Tcl/Tk
-\i0 for this version of Python and of Mac OS X.\
-
-\b \ul \
Certificate verification and OpenSSL\
+\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0
-\b0 \ulnone \
-Python 3.5 includes a number of network security enhancements that were released in Python 3.4.3 and Python 2.7.10. {\field{\*\fldinst{HYPERLINK "https://www.python.org/dev/peps/pep-0476/"}}{\fldrslt PEP 476}} changes several standard library modules, like
-\i httplib
-\i0 ,
-\i urllib
-\i0 , and
-\i xmlrpclib
-\i0 , to by default verify certificates presented by servers over secure (TLS) connections. The verification is performed by the OpenSSL libraries that Python is linked to. Prior to 3.4.3, both python.org installers dynamically linked with Apple-supplied OpenSSL libraries shipped with OS X. OS X provides a multiple level security framework that stores trust certificates in system and user keychains managed by the
+\b0 \cf0 \ulnone \
+\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0
+
+\b \cf0 **NEW**
+\b0 This variant of Python 3.6 now includes its own private copy of OpenSSL 1.0.2. Unlike previous releases, the deprecated Apple-supplied OpenSSL libraries are no longer used. This also means that the trust certificates in system and user keychains managed by the
\i Keychain Access
\i0 application and the
\i security
-\i0 command line utility.\
-\
-For OS X 10.5, Apple provides
-\i OpenSSL 0.9.7
-\i0 libraries. This version of Apple's OpenSSL
-\b does not
-\b0 use the certificates from the system security framework, even when used on newer versions of OS X. Instead it consults a traditional OpenSSL concatenated certificate file (
-\i cafile
-\i0 ) or certificate directory (
-\i capath
-\i0 ), located in
-\f1 /System/Library/OpenSSL
-\f0 . These directories are typically empty and not managed by OS X; you must manage them yourself or supply your own SSL contexts. OpenSSL 0.9.7 is obsolete by current security standards, lacking a number of important features found in later versions. Among the problems this causes is the inability to verify higher-security certificates now used by python.org services, including
-\i t{\field{\*\fldinst{HYPERLINK "https://pypi.python.org/pypi"}}{\fldrslt he Python Package Index, PyPI}}
-\i0 . To solve this problem, the
-\i 10.5+ 32-bit-only python.org variant
-\i0 is linked with a private copy of
-\i OpenSSL 1.0.2
-\i0 ; it consults the same default certificate directory,
-\f1 /System/Library/OpenSSL
-\f0 . As before, it is still necessary to manage certificates yourself when you use this Python variant and, with certificate verification now enabled by default, you may now need to take additional steps to ensure your Python programs have access to CA certificates you trust. If you use this Python variant to build standalone applications with third-party tools like {\field{\*\fldinst{HYPERLINK "https://pypi.python.org/pypi/py2app/"}}{\fldrslt
-\f1 py2app}}, you may now need to bundle CA certificates in them or otherwise supply non-default SSL contexts.\
-\
-For OS X 10.6+, Apple also provides
-\i OpenSSL
-\i0
-\i 0.9.8 libraries
-\i0 . Apple's 0.9.8 version includes an important additional feature: if a certificate cannot be verified using the manually administered certificates in
-\f1 /System/Library/OpenSSL
-\f0 , the certificates managed by the system security framework In the user and system keychains are also consulted (using Apple private APIs). For this reason, the
-\i 64-bit/32-bit 10.6+ python.org variant
-\i0 continues to be dynamically linked with Apple's OpenSSL 0.9.8 since it was felt that the loss of the system-provided certificates and management tools outweighs the additional security features provided by newer versions of OpenSSL. This will likely change in future releases of the python.org installers as Apple has deprecated use of the system-supplied OpenSSL libraries. If you do need features from newer versions of OpenSSL, there are third-party OpenSSL wrapper packages available through
-\i PyPI
-\i0 .\
+\i0 command line utility are no longer used as defaults by the Python
+\f1 ssl
+\f0 module. For 3.6.0, a sample command script is included in
+\f1 /Applications/Python 3.6
+\f0 to install a curated bundle of default root certificates from the third-party
+\f1 certifi
+\f0 package ({\field{\*\fldinst{HYPERLINK "https://pypi.python.org/pypi/certifi"}}{\fldrslt https://pypi.python.org/pypi/certifi}}). If you choose to use
+\f1 certifi
+\f0 , you should consider subscribing to the{\field{\*\fldinst{HYPERLINK "https://certifi.io/en/latest/"}}{\fldrslt project's email update service}} to be notified when the certificate bundle is updated.\
\
The bundled
\f1 pip
-\f0 included with the Python 3.5 installers has its own default certificate store for verifying download connections.\
+\f0 included with the Python 3.6 installer has its own default certificate store for verifying download connections.\
\
+\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0
-\b \ul Other changes\
+\b \cf0 \ul Update your version of Tcl/Tk to use IDLE or other Tk applications
+\b0 \ulnone \
+\
+To use IDLE or other programs that use the Tkinter graphical user interface toolkit, you need to install a newer third-party version of the
+\i Tcl/Tk
+\i0 frameworks. Visit {\field{\*\fldinst{HYPERLINK "https://www.python.org/download/mac/tcltk/"}}{\fldrslt https://www.python.org/download/mac/tcltk/}} for current information about supported and recommended versions of
+\i Tcl/Tk
+\i0 for this version of Python and of Mac OS X. For the initial release of Python 3.6, the installer is still linked with Tcl/Tk 8.5.\
+
+\b \ul \
+Other changes\
\b0 \ulnone \
-\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural
-\cf0 For other changes in this release, see the
+For other changes in this release, see the
\i What's new
\i0 section in the {\field{\*\fldinst{HYPERLINK "https://www.python.org/doc/"}}{\fldrslt Documentation Set}} for this release and its
\i Release Notes
\i0 link at {\field{\*\fldinst{HYPERLINK "https://www.python.org/downloads/"}}{\fldrslt https://www.python.org/downloads/}}.\
-\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural
-\b \cf0 \ul \
+\b \ul \
Python 3 and Python 2 Co-existence\
\b0 \ulnone \
diff --git a/Mac/BuildScript/resources/Welcome.rtf b/Mac/BuildScript/resources/Welcome.rtf
index dfb75d8..3a9ab04 100644
--- a/Mac/BuildScript/resources/Welcome.rtf
+++ b/Mac/BuildScript/resources/Welcome.rtf
@@ -1,8 +1,9 @@
-{\rtf1\ansi\ansicpg1252\cocoartf1343\cocoasubrtf160
+{\rtf1\ansi\ansicpg1252\cocoartf1504
\cocoascreenfonts1{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
+{\*\expandedcolortbl;\csgray\c100000;}
\paperw11905\paperh16837\margl1440\margr1440\vieww12200\viewh10880\viewkind0
-\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640
+\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\partightenfactor0
\f0\fs24 \cf0 This package will install
\b Python $FULL_VERSION
@@ -16,8 +17,14 @@
\b IDLE
\b0 .\
\
+\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\partightenfactor0
-\b IMPORTANT:
+\b \cf0 NEW:
+\b0 There are important changes in this release regarding network security and trust certificates. Please see the ReadMe for more details.\
+\
+\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\partightenfactor0
+
+\b \cf0 IMPORTANT:
\b0
\b IDLE
\b0 and other programs using the
diff --git a/Mac/BuildScript/resources/install_certificates.command b/Mac/BuildScript/resources/install_certificates.command
new file mode 100755
index 0000000..1d2e2d8
--- /dev/null
+++ b/Mac/BuildScript/resources/install_certificates.command
@@ -0,0 +1,48 @@
+#!/bin/sh
+
+/Library/Frameworks/Python.framework/Versions/@PYVER@/bin/python@PYVER@ << "EOF"
+
+# install_certifi.py
+#
+# sample script to install or update a set of default Root Certificates
+# for the ssl module. Uses the certificates provided by the certifi package:
+# https://pypi.python.org/pypi/certifi
+
+import os
+import os.path
+import ssl
+import stat
+import subprocess
+import sys
+
+STAT_0o775 = ( stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR
+ | stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP
+ | stat.S_IROTH | stat.S_IXOTH )
+
+def main():
+ openssl_dir, openssl_cafile = os.path.split(
+ ssl.get_default_verify_paths().openssl_cafile)
+
+ print(" -- pip install --upgrade certifi")
+ subprocess.check_call([sys.executable,
+ "-E", "-s", "-m", "pip", "install", "--upgrade", "certifi"])
+
+ import certifi
+
+ # change working directory to the default SSL directory
+ os.chdir(openssl_dir)
+ relpath_to_certifi_cafile = os.path.relpath(certifi.where())
+ print(" -- removing any existing file or link")
+ try:
+ os.remove(openssl_cafile)
+ except FileNotFoundError:
+ pass
+ print(" -- creating symlink to certifi certificate bundle")
+ os.symlink(relpath_to_certifi_cafile, openssl_cafile)
+ print(" -- setting permissions")
+ os.chmod(openssl_cafile, STAT_0o775)
+ print(" -- update complete")
+
+if __name__ == '__main__':
+ main()
+EOF
diff --git a/Mac/IDLE/IDLE.app/Contents/Resources/idlemain.py b/Mac/IDLE/IDLE.app/Contents/Resources/idlemain.py
index 986760d..5994c18 100644
--- a/Mac/IDLE/IDLE.app/Contents/Resources/idlemain.py
+++ b/Mac/IDLE/IDLE.app/Contents/Resources/idlemain.py
@@ -68,6 +68,6 @@ for idx, value in enumerate(sys.argv):
break
# Now it is safe to import idlelib.
-from idlelib.PyShell import main
+from idlelib.pyshell import main
if __name__ == '__main__':
main()
diff --git a/Makefile.pre.in b/Makefile.pre.in
index dc0a40e..93aae91 100644
--- a/Makefile.pre.in
+++ b/Makefile.pre.in
@@ -49,6 +49,10 @@ PGO_PROF_USE_FLAG=@PGO_PROF_USE_FLAG@
LLVM_PROF_MERGER=@LLVM_PROF_MERGER@
LLVM_PROF_FILE=@LLVM_PROF_FILE@
LLVM_PROF_ERR=@LLVM_PROF_ERR@
+DTRACE= @DTRACE@
+DFLAGS= @DFLAGS@
+DTRACE_HEADERS= @DTRACE_HEADERS@
+DTRACE_OBJS= @DTRACE_OBJS@
GNULD= @GNULD@
@@ -110,6 +114,7 @@ MACHDEP= @MACHDEP@
# Multiarch directory (may be empty)
MULTIARCH= @MULTIARCH@
+MULTIARCH_CPPFLAGS = @MULTIARCH_CPPFLAGS@
# Install prefix for architecture-independent files
prefix= @prefix@
@@ -235,7 +240,7 @@ TCLTK_LIBS= @TCLTK_LIBS@
# The task to run while instrumented when building the profile-opt target.
# We exclude unittests with -x that take a rediculious amount of time to
# run in the instrumented training build or do not provide much value.
-PROFILE_TASK=-m test.regrtest --pgo -x test_asyncore test_gdb test_multiprocessing_fork test_multiprocessing_forkserver test_multiprocessing_main_handling test_multiprocessing_spawn test_subprocess
+PROFILE_TASK=-m test.regrtest --pgo
# report files for gcov / lcov coverage report
COVERAGE_INFO= $(abs_builddir)/coverage.info
@@ -254,9 +259,6 @@ MODULE_OBJS= \
Modules/main.o \
Modules/gcmodule.o
-# Used of signalmodule.o is not available
-SIGNAL_OBJS= @SIGNAL_OBJS@
-
IO_H= Modules/_io/_iomodule.h
IO_OBJS= \
@@ -281,18 +283,6 @@ LIBFFI_INCLUDEDIR= @LIBFFI_INCLUDEDIR@
# Parser
PGEN= Parser/pgen$(EXE)
-PSRCS= \
- Parser/acceler.c \
- Parser/grammar1.c \
- Parser/listnode.c \
- Parser/node.c \
- Parser/parser.c \
- Parser/bitset.c \
- Parser/metagrammar.c \
- Parser/firstsets.c \
- Parser/grammar.c \
- Parser/pgen.c
-
POBJS= \
Parser/acceler.o \
Parser/grammar1.o \
@@ -307,16 +297,6 @@ POBJS= \
PARSER_OBJS= $(POBJS) Parser/myreadline.o Parser/parsetok.o Parser/tokenizer.o
-PGSRCS= \
- Objects/obmalloc.c \
- Python/dynamic_annotations.c \
- Python/mysnprintf.c \
- Python/pyctype.c \
- Parser/tokenizer_pgen.c \
- Parser/printgrammar.c \
- Parser/parsetok_pgen.c \
- Parser/pgenmain.c
-
PGOBJS= \
Objects/obmalloc.o \
Python/dynamic_annotations.o \
@@ -332,7 +312,6 @@ PARSER_HEADERS= \
$(srcdir)/Include/parsetok.h \
$(srcdir)/Parser/tokenizer.h
-PGENSRCS= $(PSRCS) $(PGSRCS)
PGENOBJS= $(POBJS) $(PGOBJS)
##########################################################################
@@ -341,7 +320,7 @@ OPCODE_H_DIR= $(srcdir)/Include
OPCODE_H_SCRIPT= $(srcdir)/Tools/scripts/generate_opcode_h.py
OPCODE_H= $(OPCODE_H_DIR)/opcode.h
OPCODE_H_GEN= $(PYTHON_FOR_GEN) $(OPCODE_H_SCRIPT) $(srcdir)/Lib/opcode.py $(OPCODE_H)
-#
+
##########################################################################
# AST
AST_H_DIR= Include
@@ -417,7 +396,8 @@ PYTHON_OBJS= \
Python/$(DYNLOADFILE) \
$(LIBOBJS) \
$(MACHDEP_OBJS) \
- $(THREADOBJ)
+ $(THREADOBJ) \
+ $(DTRACE_OBJS)
##########################################################################
@@ -471,13 +451,21 @@ LIBRARY_OBJS_OMIT_FROZEN= \
$(OBJECT_OBJS) \
$(PYTHON_OBJS) \
$(MODULE_OBJS) \
- $(SIGNAL_OBJS) \
$(MODOBJS)
LIBRARY_OBJS= \
$(LIBRARY_OBJS_OMIT_FROZEN) \
Python/frozen.o
+##########################################################################
+# DTrace
+
+# On some systems, object files that reference DTrace probes need to be modified
+# in-place by dtrace(1).
+DTRACE_DEPS = \
+ Python/ceval.o
+# XXX: should gcmodule, etc. be here, too?
+
#########################################################################
# Rules
@@ -533,15 +521,23 @@ coverage-lcov:
--base-directory $(realpath $(abs_builddir)) \
--path $(realpath $(abs_srcdir)) \
--output-file $(COVERAGE_INFO)
- : # remove 3rd party modules and system headers
+ : # remove 3rd party modules, system headers and internal files with
+ : # debug, test or dummy functions.
@lcov --remove $(COVERAGE_INFO) \
+ '*/Modules/_blake2/impl/*' \
'*/Modules/_ctypes/libffi*/*' \
'*/Modules/_decimal/libmpdec/*' \
+ '*/Modules/_sha3/kcp/*' \
'*/Modules/expat/*' \
'*/Modules/zlib/*' \
'*/Include/*' \
+ '*/Modules/xx*.c' \
+ '*/Parser/listnode.c' \
+ '*/Python/pyfpe.c' \
+ '*/Python/pystrcmp.c' \
'/usr/include/*' \
'/usr/local/include/*' \
+ '/usr/lib/gcc/*' \
--output-file $(COVERAGE_INFO)
@genhtml $(COVERAGE_INFO) --output-directory $(COVERAGE_REPORT) \
$(COVERAGE_REPORT_OPTIONS)
@@ -564,7 +560,7 @@ coverage-report:
# Run "Argument Clinic" over all source files
# (depends on python having already been built)
.PHONY=clinic
-clinic: $(BUILDPYTHON)
+clinic: $(BUILDPYTHON) $(srcdir)/Modules/_blake2/blake2s_impl.c
$(RUNSHARED) $(PYTHON_FOR_BUILD) ./Tools/clinic/clinic.py --make
# Build the interpreter
@@ -572,7 +568,7 @@ $(BUILDPYTHON): Programs/python.o $(LIBRARY) $(LDLIBRARY) $(PY3LIBRARY)
$(LINKCC) $(PY_LDFLAGS) $(LINKFORSHARED) -o $@ Programs/python.o $(BLDLIBRARY) $(LIBS) $(MODLIBS) $(SYSLIBS) $(LDLAST)
platform: $(BUILDPYTHON) pybuilddir.txt
- $(RUNSHARED) $(PYTHON_FOR_BUILD) -c 'import sys ; from sysconfig import get_platform ; print(get_platform()+"-"+sys.version[0:3])' >platform
+ $(RUNSHARED) $(PYTHON_FOR_BUILD) -c 'import sys ; from sysconfig import get_platform ; print("%s-%d.%d" % (get_platform(), *sys.version_info[:2]))' >platform
# Create build directory and generate the sysconfig build-time data there.
# pybuilddir.txt contains the name of the build dir and is used for
@@ -594,6 +590,11 @@ pybuilddir.txt: $(BUILDPYTHON)
Modules/_math.o: Modules/_math.c Modules/_math.h
$(CC) -c $(CCSHARED) $(PY_CORE_CFLAGS) -o $@ $<
+# blake2s is auto-generated from blake2b
+$(srcdir)/Modules/_blake2/blake2s_impl.c: $(BUILDPYTHON) $(srcdir)/Modules/_blake2/blake2b_impl.c $(srcdir)/Modules/_blake2/blake2b2s.py
+ $(RUNSHARED) $(PYTHON_FOR_BUILD) $(srcdir)/Modules/_blake2/blake2b2s.py
+ $(RUNSHARED) $(PYTHON_FOR_BUILD) $(srcdir)/Tools/clinic/clinic.py -f $@
+
# Build the shared modules
# Under GNU make, MAKEFLAGS are sorted and normalized; the 's' for
# -s, --silent or --quiet is always the first char.
@@ -607,6 +608,7 @@ sharedmods: $(BUILDPYTHON) pybuilddir.txt Modules/_math.o
_TCLTK_INCLUDES='$(TCLTK_INCLUDES)' _TCLTK_LIBS='$(TCLTK_LIBS)' \
$(PYTHON_FOR_BUILD) $(srcdir)/setup.py $$quiet build
+
# Build static library
# avoid long command lines, same as LIBRARY_OBJS
$(LIBRARY): $(LIBRARY_OBJS)
@@ -615,7 +617,7 @@ $(LIBRARY): $(LIBRARY_OBJS)
$(AR) $(ARFLAGS) $@ $(PARSER_OBJS)
$(AR) $(ARFLAGS) $@ $(OBJECT_OBJS)
$(AR) $(ARFLAGS) $@ $(PYTHON_OBJS) Python/frozen.o
- $(AR) $(ARFLAGS) $@ $(MODULE_OBJS) $(SIGNAL_OBJS)
+ $(AR) $(ARFLAGS) $@ $(MODULE_OBJS)
$(AR) $(ARFLAGS) $@ $(MODOBJS)
$(RANLIB) $@
@@ -719,11 +721,11 @@ Programs/_freeze_importlib.o: Programs/_freeze_importlib.c Makefile
Programs/_freeze_importlib: Programs/_freeze_importlib.o $(LIBRARY_OBJS_OMIT_FROZEN)
$(LINKCC) $(PY_LDFLAGS) -o $@ Programs/_freeze_importlib.o $(LIBRARY_OBJS_OMIT_FROZEN) $(LIBS) $(MODLIBS) $(SYSLIBS) $(LDLAST)
-Python/importlib_external.h: @GENERATED_COMMENT@ $(srcdir)/Lib/importlib/_bootstrap_external.py Programs/_freeze_importlib
+Python/importlib_external.h: @GENERATED_COMMENT@ $(srcdir)/Lib/importlib/_bootstrap_external.py Programs/_freeze_importlib Python/marshal.c
./Programs/_freeze_importlib \
$(srcdir)/Lib/importlib/_bootstrap_external.py Python/importlib_external.h
-Python/importlib.h: @GENERATED_COMMENT@ $(srcdir)/Lib/importlib/_bootstrap.py Programs/_freeze_importlib
+Python/importlib.h: @GENERATED_COMMENT@ $(srcdir)/Lib/importlib/_bootstrap.py Programs/_freeze_importlib Python/marshal.c
./Programs/_freeze_importlib \
$(srcdir)/Lib/importlib/_bootstrap.py Python/importlib.h
@@ -735,7 +737,6 @@ Modules/getbuildinfo.o: $(PARSER_OBJS) \
$(OBJECT_OBJS) \
$(PYTHON_OBJS) \
$(MODULE_OBJS) \
- $(SIGNAL_OBJS) \
$(MODOBJS) \
$(srcdir)/Modules/getbuildinfo.c
$(CC) -c $(PY_CORE_CFLAGS) \
@@ -781,6 +782,7 @@ Python/dynload_hpux.o: $(srcdir)/Python/dynload_hpux.c Makefile
Python/sysmodule.o: $(srcdir)/Python/sysmodule.c Makefile
$(CC) -c $(PY_CORE_CFLAGS) \
-DABIFLAGS='"$(ABIFLAGS)"' \
+ $(MULTIARCH_CPPFLAGS) \
-o $@ $(srcdir)/Python/sysmodule.c
$(IO_OBJS): $(IO_H)
@@ -856,13 +858,14 @@ UNICODE_DEPS = \
$(srcdir)/Objects/stringlib/unicode_format.h \
$(srcdir)/Objects/stringlib/unicodedefs.h
+Objects/bytes_methods.o: $(srcdir)/Objects/bytes_methods.c $(BYTESTR_DEPS)
Objects/bytesobject.o: $(srcdir)/Objects/bytesobject.c $(BYTESTR_DEPS)
-
Objects/bytearrayobject.o: $(srcdir)/Objects/bytearrayobject.c $(BYTESTR_DEPS)
Objects/unicodeobject.o: $(srcdir)/Objects/unicodeobject.c $(UNICODE_DEPS)
-Objects/dictobject.o: $(srcdir)/Objects/stringlib/eq.h
+Objects/odictobject.o: $(srcdir)/Objects/dict-common.h
+Objects/dictobject.o: $(srcdir)/Objects/stringlib/eq.h $(srcdir)/Objects/dict-common.h
Objects/setobject.o: $(srcdir)/Objects/stringlib/eq.h
$(OPCODETARGETS_H): $(OPCODETARGETGEN_FILES)
@@ -872,6 +875,18 @@ Python/ceval.o: $(OPCODETARGETS_H) $(srcdir)/Python/ceval_gil.h
Python/frozen.o: Python/importlib.h Python/importlib_external.h
+# Generate DTrace probe macros, then rename them (PYTHON_ -> PyDTrace_) to
+# follow our naming conventions. dtrace(1) uses the output filename to generate
+# an include guard, so we can't use a pipeline to transform its output.
+Include/pydtrace_probes.h: $(srcdir)/Include/pydtrace.d
+ $(DTRACE) $(DFLAGS) -o $@ -h -s $<
+ : sed in-place edit with POSIX-only tools
+ sed 's/PYTHON_/PyDTrace_/' $@ > $@.tmp
+ mv $@.tmp $@
+
+Python/pydtrace.o: $(srcdir)/Include/pydtrace.d $(DTRACE_DEPS)
+ $(DTRACE) $(DFLAGS) -o $@ -G -s $< $(DTRACE_DEPS)
+
Objects/typeobject.o: Objects/typeslots.inc
Objects/typeslots.inc: $(srcdir)/Include/typeslots.h $(srcdir)/Objects/typeslots.py
$(PYTHON_FOR_GEN) $(srcdir)/Objects/typeslots.py < $(srcdir)/Include/typeslots.h Objects/typeslots.inc
@@ -929,6 +944,7 @@ PYTHON_HEADERS= \
$(srcdir)/Include/objimpl.h \
$(OPCODE_H) \
$(srcdir)/Include/osdefs.h \
+ $(srcdir)/Include/osmodule.h \
$(srcdir)/Include/patchlevel.h \
$(srcdir)/Include/pgen.h \
$(srcdir)/Include/pgenheaders.h \
@@ -937,6 +953,7 @@ PYTHON_HEADERS= \
$(srcdir)/Include/pycapsule.h \
$(srcdir)/Include/pyctype.h \
$(srcdir)/Include/pydebug.h \
+ $(srcdir)/Include/pydtrace.h \
$(srcdir)/Include/pyerrors.h \
$(srcdir)/Include/pyfpe.h \
$(srcdir)/Include/pyhash.h \
@@ -968,7 +985,8 @@ PYTHON_HEADERS= \
$(srcdir)/Include/weakrefobject.h \
pyconfig.h \
$(PARSER_HEADERS) \
- $(AST_H)
+ $(AST_H) \
+ $(DTRACE_HEADERS)
$(LIBRARY_OBJS) $(MODOBJS) Programs/python.o: $(PYTHON_HEADERS)
@@ -978,7 +996,7 @@ $(LIBRARY_OBJS) $(MODOBJS) Programs/python.o: $(PYTHON_HEADERS)
TESTOPTS= $(EXTRATESTOPTS)
TESTPYTHON= $(RUNSHARED) ./$(BUILDPYTHON) $(TESTPYTHONOPTS)
TESTRUNNER= $(TESTPYTHON) $(srcdir)/Tools/scripts/run_tests.py
-TESTTIMEOUT= 3600
+TESTTIMEOUT= 1200
# Run a basic set of regression tests.
# This excludes some tests that are particularly resource-intensive.
@@ -1016,7 +1034,7 @@ buildbottest: build_all platform
-@if which pybuildbot.identify >/dev/null 2>&1; then \
pybuildbot.identify "CC='$(CC)'" "CXX='$(CXX)'"; \
fi
- $(TESTRUNNER) -j 1 -u all -W --timeout=$(TESTTIMEOUT) $(TESTOPTS)
+ $(TESTRUNNER) -j 1 -u all -W --slowest --timeout=$(TESTTIMEOUT) $(TESTOPTS)
QUICKTESTOPTS= $(TESTOPTS) -x test_subprocess test_io test_lib2to3 \
test_multibytecodec test_urllib2_localnet test_itertools \
@@ -1169,16 +1187,16 @@ maninstall: altmaninstall
(cd $(DESTDIR)$(MANDIR)/man1; $(LN) -s python$(VERSION).1 python3.1)
# Install the library
-PLATDIR= @PLATDIR@
-MACHDEPS= $(PLATDIR)
XMLLIBSUBDIRS= xml xml/dom xml/etree xml/parsers xml/sax
LIBSUBDIRS= tkinter tkinter/test tkinter/test/test_tkinter \
tkinter/test/test_ttk site-packages test \
test/audiodata \
test/capath test/data \
test/cjkencodings test/decimaltestdata test/xmltestdata \
+ test/dtracedata \
test/eintrdata \
test/imghdrdata \
+ test/libregrtest \
test/subprocessdata test/sndhdrdata test/support \
test/tracedmodules test/encoded_modules \
test/test_import \
@@ -1227,8 +1245,8 @@ LIBSUBDIRS= tkinter tkinter/test tkinter/test/test_tkinter \
multiprocessing multiprocessing/dummy \
unittest unittest/test unittest/test/testmock \
venv venv/scripts venv/scripts/common venv/scripts/posix \
- curses pydoc_data $(MACHDEPS)
-libinstall: build_all $(srcdir)/Lib/$(PLATDIR) $(srcdir)/Modules/xxmodule.c
+ curses pydoc_data
+libinstall: build_all $(srcdir)/Modules/xxmodule.c
@for i in $(SCRIPTDIR) $(LIBDEST); \
do \
if test ! -d $(DESTDIR)$$i; then \
@@ -1248,7 +1266,7 @@ libinstall: build_all $(srcdir)/Lib/$(PLATDIR) $(srcdir)/Modules/xxmodule.c
else true; \
fi; \
done
- @for i in $(srcdir)/Lib/*.py `cat pybuilddir.txt`/_sysconfigdata.py; \
+ @for i in $(srcdir)/Lib/*.py; \
do \
if test -x $$i; then \
$(INSTALL_SCRIPT) $$i $(DESTDIR)$(LIBDEST); \
@@ -1283,6 +1301,8 @@ libinstall: build_all $(srcdir)/Lib/$(PLATDIR) $(srcdir)/Modules/xxmodule.c
esac; \
done; \
done
+ $(INSTALL_DATA) `cat pybuilddir.txt`/_sysconfigdata_$(ABIFLAGS)_$(MACHDEP)_$(MULTIARCH).py \
+ $(DESTDIR)$(LIBDEST); \
$(INSTALL_DATA) $(srcdir)/LICENSE $(DESTDIR)$(LIBDEST)/LICENSE.txt
if test -d $(DESTDIR)$(LIBDEST)/distutils/tests; then \
$(INSTALL_DATA) $(srcdir)/Modules/xxmodule.c \
@@ -1320,23 +1340,6 @@ libinstall: build_all $(srcdir)/Lib/$(PLATDIR) $(srcdir)/Modules/xxmodule.c
-PYTHONPATH=$(DESTDIR)$(LIBDEST) $(RUNSHARED) \
$(PYTHON_FOR_BUILD) -m lib2to3.pgen2.driver $(DESTDIR)$(LIBDEST)/lib2to3/PatternGrammar.txt
-# Create the PLATDIR source directory, if one wasn't distributed..
-$(srcdir)/Lib/$(PLATDIR):
- mkdir $(srcdir)/Lib/$(PLATDIR)
- cp $(srcdir)/Lib/plat-generic/regen $(srcdir)/Lib/$(PLATDIR)/regen
- export PATH; PATH="`pwd`:$$PATH"; \
- export PYTHONPATH; PYTHONPATH="`pwd`/Lib"; \
- export DYLD_FRAMEWORK_PATH; DYLD_FRAMEWORK_PATH="`pwd`"; \
- export EXE; EXE="$(BUILDEXE)"; \
- if [ -n "$(MULTIARCH)" ]; then export MULTIARCH; MULTIARCH=$(MULTIARCH); fi; \
- export PYTHON_FOR_BUILD; \
- if [ "$(BUILD_GNU_TYPE)" = "$(HOST_GNU_TYPE)" ]; then \
- PYTHON_FOR_BUILD="$(BUILDPYTHON)"; \
- else \
- PYTHON_FOR_BUILD="$(PYTHON_FOR_BUILD)"; \
- fi; \
- cd $(srcdir)/Lib/$(PLATDIR); $(RUNSHARED) ./regen
-
python-config: $(srcdir)/Misc/python-config.in Misc/python-config.sh
# Substitution happens here, as the completely-expanded BINDIR
# is not available in configure
@@ -1433,7 +1436,7 @@ sharedinstall: sharedmods
--install-scripts=$(BINDIR) \
--install-platlib=$(DESTSHARED) \
--root=$(DESTDIR)/
- -rm $(DESTDIR)$(DESTSHARED)/_sysconfigdata.py
+ -rm $(DESTDIR)$(DESTSHARED)/_sysconfigdata_$(ABIFLAGS)_$(MACHDEP)_$(MULTIARCH).py
-rm -r $(DESTDIR)$(DESTSHARED)/__pycache__
# Here are a couple of targets for MacOSX again, to install a full
@@ -1479,10 +1482,10 @@ frameworkinstallstructure: $(LDLIBRARY)
# Install a number of symlinks to keep software that expects a normal unix
# install (which includes python-config) happy.
frameworkinstallmaclib:
- $(LN) -fs "../../../$(PYTHONFRAMEWORK)" "$(DESTDIR)$(prefix)/lib/python$(VERSION)/config-$(LDVERSION)/libpython$(LDVERSION).a"
- $(LN) -fs "../../../$(PYTHONFRAMEWORK)" "$(DESTDIR)$(prefix)/lib/python$(VERSION)/config-$(LDVERSION)/libpython$(LDVERSION).dylib"
- $(LN) -fs "../../../$(PYTHONFRAMEWORK)" "$(DESTDIR)$(prefix)/lib/python$(VERSION)/config-$(LDVERSION)/libpython$(VERSION).a"
- $(LN) -fs "../../../$(PYTHONFRAMEWORK)" "$(DESTDIR)$(prefix)/lib/python$(VERSION)/config-$(LDVERSION)/libpython$(VERSION).dylib"
+ $(LN) -fs "../../../$(PYTHONFRAMEWORK)" "$(DESTDIR)$(LIBPL)/libpython$(LDVERSION).a"
+ $(LN) -fs "../../../$(PYTHONFRAMEWORK)" "$(DESTDIR)$(LIBPL)/libpython$(LDVERSION).dylib"
+ $(LN) -fs "../../../$(PYTHONFRAMEWORK)" "$(DESTDIR)$(LIBPL)/libpython$(VERSION).a"
+ $(LN) -fs "../../../$(PYTHONFRAMEWORK)" "$(DESTDIR)$(LIBPL)/libpython$(VERSION).dylib"
$(LN) -fs "../$(PYTHONFRAMEWORK)" "$(DESTDIR)$(prefix)/lib/libpython$(LDVERSION).dylib"
$(LN) -fs "../$(PYTHONFRAMEWORK)" "$(DESTDIR)$(prefix)/lib/libpython$(VERSION).dylib"
@@ -1597,6 +1600,7 @@ clean: pycremoval
-rm -f Lib/lib2to3/*Grammar*.pickle
-rm -f Programs/_testembed Programs/_freeze_importlib
-find build -type f -a ! -name '*.gc??' -exec rm -f {} ';'
+ -rm -f Include/pydtrace_probes.h
profile-removal:
find . -name '*.gc??' -exec rm -f {} ';'
@@ -1661,7 +1665,6 @@ funny:
-o -name '*.tmSnippet' \
-o -name 'Setup' \
-o -name 'Setup.*' \
- -o -name regen \
-o -name README \
-o -name NEWS \
-o -name HISTORY \
diff --git a/Misc/ACKS b/Misc/ACKS
index 59b7704..31e4df1 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS